13c434
From b800e25258353dbb1a88506123c21ac3298fd2d0 Mon Sep 17 00:00:00 2001
13c434
From: Carlos Santos <casantos@redhat.com>
13c434
Date: Tue, 18 Oct 2022 08:59:16 -0300
13c434
Subject: [PATCH 2/2] Always set the home directory permissions according to
13c434
 HOME_MODE
13c434
13c434
Currently the home directory permissions are set by taking the /etc/skel
13c434
mode and masking it with HOME_MODE:
13c434
13c434
    override_umask = 0777 & ~get_umask(&configured_umask, "HOME_MODE");
13c434
    stat(skel, &sb); /* performed by nftw() */
13c434
    oddjob_selinux_mkdir(newpath, sb->st_mode & ~override_umask, uid, gid);
13c434
13c434
The problem is that when HOME_MODE is more permissive than /etc/skel,
13c434
the masking will not produce the desired result, e.g.
13c434
13c434
    skel_mode = 0755
13c434
    HOME_MODE = 0775
13c434
    override_umask = 0777 & ~HOME_MODE /* 0002 */
13c434
    mode = skel_mode & ~override_umask /* 0755 & 0775 = 0755 */
13c434
13c434
In order to fix the problem, always use 0777 & ~override_umask for the
13c434
top home directory.
13c434
13c434
Signed-off-by: Carlos Santos <casantos@redhat.com>
13c434
Fixes: https://pagure.io/oddjob/issue/17
13c434
---
13c434
 src/mkhomedir.c | 45 +++++++++++++++++++++------------------------
13c434
 1 file changed, 21 insertions(+), 24 deletions(-)
13c434
13c434
diff --git a/src/mkhomedir.c b/src/mkhomedir.c
13c434
index ac813a9..932918f 100644
13c434
--- a/src/mkhomedir.c
13c434
+++ b/src/mkhomedir.c
13c434
@@ -67,6 +67,7 @@ copy_single_item(const char *source, const struct stat *sb,
13c434
 {
13c434
 	uid_t uid = pwd->pw_uid;
13c434
 	gid_t gid = pwd->pw_gid;
13c434
+	mode_t mode = sb->st_mode & ~override_umask;
13c434
 	int sfd, dfd, i, res;
13c434
 	char target[PATH_MAX + 1], newpath[PATH_MAX + 1];
13c434
 	unsigned char buf[BUFSIZ];
13c434
@@ -112,8 +113,7 @@ copy_single_item(const char *source, const struct stat *sb,
13c434
 			oddjob_set_selinux_file_creation_context(newpath,
13c434
 								 sb->st_mode |
13c434
 								 S_IFREG);
13c434
-			dfd = open(newpath, O_WRONLY | O_CREAT | O_EXCL,
13c434
-				   sb->st_mode & ~override_umask);
13c434
+			dfd = open(newpath, O_WRONLY | O_CREAT | O_EXCL, mode);
13c434
 			if (dfd != -1) {
13c434
 				while ((i = read(sfd, buf, sizeof(buf))) > 0) {
13c434
 					retry_write(dfd, buf, i);
13c434
@@ -156,20 +156,22 @@ copy_single_item(const char *source, const struct stat *sb,
13c434
 		}
13c434
 		return 0;
13c434
 	case FTW_D:
13c434
-		/* It's the home directory itself. Don't give it to the
13c434
-		 * target user just yet to avoid potential race conditions
13c434
-		 * involving symlink attacks when we copy over the skeleton
13c434
-		 * tree. */
13c434
-		if (status->level == 0 && !owner_mkdir_first) {
13c434
-			uid = 0;
13c434
-			gid = 0;
13c434
-		}
13c434
-
13c434
 		/* It's a directory.  Make one with the same name and
13c434
 		 * permissions, but owned by the target user. */
13c434
-		res = oddjob_selinux_mkdir(newpath,
13c434
-					   sb->st_mode & ~override_umask,
13c434
-					   uid, gid);
13c434
+		if (status->level == 0) {
13c434
+			/* It's the home directory itself.  Use the configured
13c434
+			 * (or overriden) mode, not the source mode & umask. */
13c434
+			mode = 0777 & ~override_umask;
13c434
+
13c434
+			/* Don't give it to the target user just yet to avoid
13c434
+			 * potential race conditions involving symlink attacks
13c434
+			 * when we copy over the skeleton tree. */
13c434
+			if (!owner_mkdir_first) {
13c434
+				uid = 0;
13c434
+				gid = 0;
13c434
+			}
13c434
+		}
13c434
+		res = oddjob_selinux_mkdir(newpath, mode, uid, gid);
13c434
 
13c434
 		/* on unexpected errors, or if the home directory itself
13c434
 		 * suddenly already exists, abort the copy operation. */
13c434
@@ -248,12 +250,8 @@ mkhomedir(const char *user, int flags)
13c434
 
13c434
 				return res;
13c434
 			} else {
13c434
-				if (stat(skel, &st) != 0) {
13c434
-					st.st_mode = S_IRWXU;
13c434
-				}
13c434
 				if ((oddjob_selinux_mkdir(pwd->pw_dir,
13c434
-							  st.st_mode &
13c434
-							  ~override_umask,
13c434
+							  0777 & ~override_umask,
13c434
 							  pwd->pw_uid,
13c434
 							  pwd->pw_gid) != 0) &&
13c434
 				    (errno != EEXIST)) {
13c434
@@ -269,11 +267,11 @@ mkhomedir(const char *user, int flags)
13c434
 }
13c434
 
13c434
 static mode_t
13c434
-get_umask(int *configured, const char *variable)
13c434
+get_umask(int *configured, const char *variable, mode_t default_value)
13c434
 {
13c434
 	FILE *fp;
13c434
 	char buf[BUFSIZ], *p, *end;
13c434
-	mode_t mask = umask(0777);
13c434
+	mode_t mask = default_value;
13c434
 	long tmp;
13c434
 	size_t vlen = strlen(variable);
13c434
 
13c434
@@ -315,11 +313,10 @@ main(int argc, char **argv)
13c434
 
13c434
 	openlog(PACKAGE "-mkhomedir", LOG_PID, LOG_DAEMON);
13c434
 	/* Unlike UMASK, HOME_MODE is the file mode, so needs to be reverted */
13c434
-	override_umask = 0777 & ~get_umask(&configured_umask, "HOME_MODE");
13c434
+	override_umask = 0777 & ~get_umask(&configured_umask, "HOME_MODE", 0);
13c434
 	if (configured_umask == 0) {
13c434
-		override_umask = get_umask(&configured_umask, "UMASK");
13c434
+		override_umask = get_umask(&configured_umask, "UMASK", 022);
13c434
 	}
13c434
-	umask(override_umask);
13c434
 	skel_dir = "/etc/skel";
13c434
 
13c434
 	while ((i = getopt(argc, argv, "nqfs:u:")) != -1) {
13c434
-- 
13c434
2.38.1
13c434