Blame SOURCES/oddjob-cve-2020-10737-reversal-option.patch

d6cb39
From 71b0389fbb31833d827f5f0fec18880c2f602753 Mon Sep 17 00:00:00 2001
d6cb39
From: Alexander Bokovoy <abokovoy@redhat.com>
d6cb39
Date: Thu, 19 May 2022 13:52:22 +0300
d6cb39
Subject: [PATCH] mkhomedir: add support for pre-CVE-2020-10737 behavior
d6cb39
d6cb39
Pre-CVE-2020-10737 behavior was used to allow creating home directories
d6cb39
on NFS mounts when non-Kerberos authentication method is in use. This is
d6cb39
exactly the case where a race condition addressed by the CVE-2020-10737
d6cb39
fix could have happened. However, there are legit use cases where this
d6cb39
setup is needed.
d6cb39
d6cb39
Add '-f' option to mkhomedir helper to activate previous behavior. In
d6cb39
order to enable it, a change to oddjobd-mkhomedir.conf configuration
d6cb39
file is needed by explicitly adding '-f' option to the executable file
d6cb39
definition.
d6cb39
d6cb39
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2050079
d6cb39
d6cb39
Signed-off-by: Alexander Bokovoy <abokovoy@redhat.com>
d6cb39
---
d6cb39
 src/mkhomedir.c                 | 16 +++++++++++++---
d6cb39
 src/oddjobd-mkhomedir.conf.5.in |  9 +++++++++
d6cb39
 2 files changed, 22 insertions(+), 3 deletions(-)
d6cb39
d6cb39
diff --git a/src/mkhomedir.c b/src/mkhomedir.c
d6cb39
index be85959..ac813a9 100644
d6cb39
--- a/src/mkhomedir.c
d6cb39
+++ b/src/mkhomedir.c
d6cb39
@@ -53,9 +53,11 @@ static const char *skel;
d6cb39
 static const char *skel_dir;
d6cb39
 static struct passwd *pwd;
d6cb39
 static mode_t override_umask;
d6cb39
+static int owner_mkdir_first = 0;
d6cb39
 
d6cb39
 #define FLAG_POPULATE	(1 << 0)
d6cb39
 #define FLAG_QUIET	(1 << 1)
d6cb39
+#define FLAG_OWNER_MKDIR_FIRST (1 << 2)
d6cb39
 
d6cb39
 /* Given the path of an item somewhere in the skeleton directory, create as
d6cb39
  * identical as possible a copy in the destination tree. */
d6cb39
@@ -158,7 +160,7 @@ copy_single_item(const char *source, const struct stat *sb,
d6cb39
 		 * target user just yet to avoid potential race conditions
d6cb39
 		 * involving symlink attacks when we copy over the skeleton
d6cb39
 		 * tree. */
d6cb39
-		if (status->level == 0) {
d6cb39
+		if (status->level == 0 && !owner_mkdir_first) {
d6cb39
 			uid = 0;
d6cb39
 			gid = 0;
d6cb39
 		}
d6cb39
@@ -222,6 +224,9 @@ mkhomedir(const char *user, int flags)
d6cb39
 		       pwd->pw_dir);
d6cb39
 		return HANDLER_INVALID_INVOCATION;
d6cb39
 	}
d6cb39
+	if (flags & FLAG_OWNER_MKDIR_FIRST) {
d6cb39
+		owner_mkdir_first = 1;
d6cb39
+	}
d6cb39
 	if ((lstat(pwd->pw_dir, &st) == -1) && (errno == ENOENT)) {
d6cb39
 		/* Figure out which location we're using as a
d6cb39
 		 * template. */
d6cb39
@@ -237,7 +242,7 @@ mkhomedir(const char *user, int flags)
d6cb39
 				int res = nftw(get_skel_dir(), copy_single_item, 5,
d6cb39
 					       FTW_PHYS);
d6cb39
 				/* only now give ownership to the target user */
d6cb39
-				if (res == 0) {
d6cb39
+				if (res == 0 && !owner_mkdir_first) {
d6cb39
 					res = chown(pwd->pw_dir, pwd->pw_uid, pwd->pw_gid);
d6cb39
 				}
d6cb39
 
d6cb39
@@ -317,8 +322,11 @@ main(int argc, char **argv)
d6cb39
 	umask(override_umask);
d6cb39
 	skel_dir = "/etc/skel";
d6cb39
 
d6cb39
-	while ((i = getopt(argc, argv, "nqs:u:")) != -1) {
d6cb39
+	while ((i = getopt(argc, argv, "nqfs:u:")) != -1) {
d6cb39
 		switch (i) {
d6cb39
+		case 'f':
d6cb39
+			flags |= FLAG_OWNER_MKDIR_FIRST;
d6cb39
+			break;
d6cb39
 		case 'n':
d6cb39
 			flags &= ~FLAG_POPULATE;
d6cb39
 			break;
d6cb39
@@ -339,6 +347,8 @@ main(int argc, char **argv)
d6cb39
 			break;
d6cb39
 		default:
d6cb39
 			fprintf(stderr, "Valid options:\n"
d6cb39
+				"-f\tCreate home directory initially owned by user, "
d6cb39
+				"not root. See man page for security issues.\n"
d6cb39
 				"-n\tDo not populate home directories, "
d6cb39
 				"just create them.\n"
d6cb39
 				"-q\tDo not print messages when creating "
d6cb39
diff --git a/src/oddjobd-mkhomedir.conf.5.in b/src/oddjobd-mkhomedir.conf.5.in
d6cb39
index d7a2429..6e35ad5 100644
d6cb39
--- a/src/oddjobd-mkhomedir.conf.5.in
d6cb39
+++ b/src/oddjobd-mkhomedir.conf.5.in
d6cb39
@@ -10,6 +10,15 @@ directory.
d6cb39
 
d6cb39
 The mkhomedir helper itself accepts these options:
d6cb39
 .TP
d6cb39
+-f
d6cb39
+Restore behavior before CVE-2020-10737 was fixed: create the home directory
d6cb39
+with user's ownership directly rather than create it as a root and only after
d6cb39
+populating it change to the user's ownership. The former behavior is insecure
d6cb39
+but may be used to allow creation of NFS-mounted home directories when
d6cb39
+non-Kerberos authentication is in use. It is prone for a race condition that
d6cb39
+could be exploited in the NFS-mounted home directories use case. To avoid
d6cb39
+CVE-2020-10737, do not use \fB-f\fR option in production environments.
d6cb39
+.TP
d6cb39
 -q
d6cb39
 Refrain from outputting the usual "Creating home directory..." message when it
d6cb39
 creates a home directory.
d6cb39
-- 
d6cb39
2.37.1
d6cb39