Blame SOURCES/sudo-1.8.31-CVE-2021-3156.patch

1c24e9
diff -up ./plugins/sudoers/policy.c.heap-buffer ./plugins/sudoers/policy.c
1c24e9
--- ./plugins/sudoers/policy.c.heap-buffer	2019-10-28 13:28:52.000000000 +0100
1c24e9
+++ ./plugins/sudoers/policy.c	2021-01-20 11:38:06.481807015 +0100
1c24e9
@@ -100,10 +100,11 @@ parse_bool(const char *line, int varlen,
1c24e9
 int
1c24e9
 sudoers_policy_deserialize_info(void *v, char **runas_user, char **runas_group)
1c24e9
 {
1c24e9
+    const int edit_mask = MODE_EDIT|MODE_IGNORE_TICKET|MODE_NONINTERACTIVE;
1c24e9
     struct sudoers_policy_open_info *info = v;
1c24e9
-    char * const *cur;
1c24e9
     const char *p, *errstr, *groups = NULL;
1c24e9
     const char *remhost = NULL;
1c24e9
+    char * const *cur;
1c24e9
     int flags = 0;
1c24e9
     debug_decl(sudoers_policy_deserialize_info, SUDOERS_DEBUG_PLUGIN)
1c24e9
 
1c24e9
@@ -332,6 +333,12 @@ sudoers_policy_deserialize_info(void *v,
1c24e9
 #endif
1c24e9
     }
1c24e9
 
1c24e9
+    /* Sudo front-end should restrict mode flags for sudoedit. */
1c24e9
+    if (ISSET(flags, MODE_EDIT) && (flags & edit_mask) != flags) {
1c24e9
+	sudo_warnx(U_("invalid mode flags from sudo front end: 0x%x"), flags);
1c24e9
+	goto bad;
1c24e9
+    }
1c24e9
+
1c24e9
     user_gid = (gid_t)-1;
1c24e9
     user_sid = (pid_t)-1;
1c24e9
     user_uid = (gid_t)-1;
1c24e9
diff -up ./plugins/sudoers/sudoers.c.heap-buffer ./plugins/sudoers/sudoers.c
1c24e9
--- ./plugins/sudoers/sudoers.c.heap-buffer	2021-01-20 11:34:57.523317977 +0100
1c24e9
+++ ./plugins/sudoers/sudoers.c	2021-01-20 12:08:01.331553520 +0100
1c24e9
@@ -451,7 +451,7 @@ sudoers_policy_main(int argc, char * con
1c24e9
 
1c24e9
     /* If run as root with SUDO_USER set, set sudo_user.pw to that user. */
1c24e9
     /* XXX - causes confusion when root is not listed in sudoers */
1c24e9
-    if (sudo_mode & (MODE_RUN | MODE_EDIT) && prev_user != NULL) {
1c24e9
+    if (ISSET(sudo_mode, MODE_RUN|MODE_EDIT) && prev_user != NULL) {
1c24e9
 	if (user_uid == 0 && strcmp(prev_user, "root") != 0) {
1c24e9
 	    struct passwd *pw;
1c24e9
 
1c24e9
@@ -834,8 +834,8 @@ set_cmnd(void)
1c24e9
     if (user_cmnd == NULL)
1c24e9
 	user_cmnd = NewArgv[0];
1c24e9
 
1c24e9
-    if (sudo_mode & (MODE_RUN | MODE_EDIT | MODE_CHECK)) {
1c24e9
-	if (ISSET(sudo_mode, MODE_RUN | MODE_CHECK)) {
1c24e9
+    if (ISSET(sudo_mode, MODE_RUN|MODE_EDIT|MODE_CHECK)) {
1c24e9
+	if (!ISSET(sudo_mode, MODE_EDIT)) {
1c24e9
 	    if (def_secure_path && !user_is_exempt())
1c24e9
 		path = def_secure_path;
1c24e9
 	    if (!set_perms(PERM_RUNAS))
1c24e9
@@ -873,7 +873,8 @@ set_cmnd(void)
1c24e9
 		sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1c24e9
 		debug_return_int(-1);
1c24e9
 	    }
1c24e9
-	    if (ISSET(sudo_mode, MODE_SHELL|MODE_LOGIN_SHELL)) {
1c24e9
+	    if (ISSET(sudo_mode, MODE_SHELL|MODE_LOGIN_SHELL) &&
1c24e9
+            ISSET(sudo_mode, MODE_RUN)) {
1c24e9
 		/*
1c24e9
 		 * When running a command via a shell, the sudo front-end
1c24e9
 		 * escapes potential meta chars.  We unescape non-spaces
1c24e9
@@ -881,10 +882,22 @@ set_cmnd(void)
1c24e9
 		 */
1c24e9
 		for (to = user_args, av = NewArgv + 1; (from = *av); av++) {
1c24e9
 		    while (*from) {
1c24e9
-			if (from[0] == '\\' && !isspace((unsigned char)from[1]))
1c24e9
+			if (from[0] == '\\' && from[1] != '\0' &&
1c24e9
+				!isspace((unsigned char)from[1])) {
1c24e9
 			    from++;
1c24e9
+			}
1c24e9
+			if (size - (to - user_args) < 1) {
1c24e9
+			    sudo_warnx(U_("internal error, %s overflow"),
1c24e9
+				__func__);
1c24e9
+			    debug_return_int(NOT_FOUND_ERROR);
1c24e9
+			}
1c24e9
 			*to++ = *from++;
1c24e9
 		    }
1c24e9
+		    if (size - (to - user_args) < 1) {
1c24e9
+			sudo_warnx(U_("internal error, %s overflow"),
1c24e9
+			    __func__);
1c24e9
+			debug_return_int(NOT_FOUND_ERROR);
1c24e9
+		    }
1c24e9
 		    *to++ = ' ';
1c24e9
 		}
1c24e9
 		*--to = '\0';
1c24e9
diff -up ./plugins/sudoers/timestamp.c.heap-buffer ./plugins/sudoers/timestamp.c
1c24e9
--- ./plugins/sudoers/timestamp.c.heap-buffer	2021-01-20 12:11:28.218774128 +0100
1c24e9
+++ ./plugins/sudoers/timestamp.c	2021-01-20 12:20:41.772324808 +0100
1c24e9
@@ -652,8 +652,8 @@ timestamp_lock(void *vcookie, struct pas
1c24e9
     } else if (entry.type != TS_LOCKEXCL) {
1c24e9
 	/* Old sudo record, convert it to TS_LOCKEXCL. */
1c24e9
 	entry.type = TS_LOCKEXCL;
1c24e9
-	memset((char *)&entry + offsetof(struct timestamp_entry, type), 0,
1c24e9
-	    nread - offsetof(struct timestamp_entry, type));
1c24e9
+	memset((char *)&entry + offsetof(struct timestamp_entry, flags), 0,
1c24e9
+	    nread - offsetof(struct timestamp_entry, flags));
1c24e9
 	if (ts_write(cookie->fd, cookie->fname, &entry, 0) == -1)
1c24e9
 	    debug_return_bool(false);
1c24e9
     }
1c24e9
diff -up ./src/parse_args.c.heap-buffer ./src/parse_args.c
1c24e9
--- ./src/parse_args.c.heap-buffer	2021-01-20 11:35:18.231043445 +0100
1c24e9
+++ ./src/parse_args.c	2021-01-20 12:26:31.290591673 +0100
1c24e9
@@ -124,7 +124,10 @@ struct environment {
1c24e9
 /*
1c24e9
  * Default flags allowed when running a command.
1c24e9
  */
1c24e9
-#define DEFAULT_VALID_FLAGS	(MODE_BACKGROUND|MODE_PRESERVE_ENV|MODE_RESET_HOME|MODE_LOGIN_SHELL|MODE_NONINTERACTIVE|MODE_SHELL)
1c24e9
+#define DEFAULT_VALID_FLAGS	(MODE_BACKGROUND|MODE_PRESERVE_ENV|MODE_RESET_HOME|MODE_LOGIN_SHELL|MODE_NONINTERACTIVE|MODE_PRESERVE_GROUPS|MODE_SHELL)
1c24e9
+#define EDIT_VALID_FLAGS	MODE_NONINTERACTIVE
1c24e9
+#define LIST_VALID_FLAGS	(MODE_NONINTERACTIVE|MODE_LONG_LIST)
1c24e9
+#define VALIDATE_VALID_FLAGS	MODE_NONINTERACTIVE
1c24e9
 
1c24e9
 /* Option number for the --host long option due to ambiguity of the -h flag. */
1c24e9
 #define OPT_HOSTNAME	256
1c24e9
@@ -269,6 +272,7 @@ parse_args(int argc, char **argv, int *n
1c24e9
 	progname = "sudoedit";
1c24e9
 	mode = MODE_EDIT;
1c24e9
 	sudo_settings[ARG_SUDOEDIT].value = "true";
1c24e9
+	valid_flags = EDIT_VALID_FLAGS;
1c24e9
     }
1c24e9
 
1c24e9
     /* Load local IP addresses and masks. */
1c24e9
@@ -360,7 +364,7 @@ parse_args(int argc, char **argv, int *n
1c24e9
 			usage_excl(1);
1c24e9
 		    mode = MODE_EDIT;
1c24e9
 		    sudo_settings[ARG_SUDOEDIT].value = "true";
1c24e9
-		    valid_flags = MODE_NONINTERACTIVE;
1c24e9
+		    valid_flags = EDIT_VALID_FLAGS;
1c24e9
 		    break;
1c24e9
 		case 'g':
1c24e9
 		    assert(optarg != NULL);
1c24e9
@@ -371,6 +375,7 @@ parse_args(int argc, char **argv, int *n
1c24e9
 		    break;
1c24e9
 		case 'H':
1c24e9
 		    sudo_settings[ARG_SET_HOME].value = "true";
1c24e9
+		    SET(flags, MODE_RESET_HOME);
1c24e9
 		    break;
1c24e9
 		case 'h':
1c24e9
 		    if (optarg == NULL) {
1c24e9
@@ -421,7 +426,7 @@ parse_args(int argc, char **argv, int *n
1c24e9
 			    usage_excl(1);
1c24e9
 		    }
1c24e9
 		    mode = MODE_LIST;
1c24e9
-		    valid_flags = MODE_NONINTERACTIVE|MODE_LONG_LIST;
1c24e9
+		    valid_flags = LIST_VALID_FLAGS;
1c24e9
 		    break;
1c24e9
 		case 'n':
1c24e9
 		    SET(flags, MODE_NONINTERACTIVE);
1c24e9
@@ -429,6 +434,7 @@ parse_args(int argc, char **argv, int *n
1c24e9
 		    break;
1c24e9
 		case 'P':
1c24e9
 		    sudo_settings[ARG_PRESERVE_GROUPS].value = "true";
1c24e9
+		    SET(flags, MODE_PRESERVE_GROUPS);
1c24e9
 		    break;
1c24e9
 		case 'p':
1c24e9
 		    /* An empty prompt is allowed. */
1c24e9
@@ -478,7 +484,7 @@ parse_args(int argc, char **argv, int *n
1c24e9
 		    if (mode && mode != MODE_VALIDATE)
1c24e9
 			usage_excl(1);
1c24e9
 		    mode = MODE_VALIDATE;
1c24e9
-		    valid_flags = MODE_NONINTERACTIVE;
1c24e9
+		    valid_flags = VALIDATE_VALID_FLAGS;
1c24e9
 		    break;
1c24e9
 		case 'V':
1c24e9
 		    if (mode && mode != MODE_VERSION)
1c24e9
@@ -505,7 +511,7 @@ parse_args(int argc, char **argv, int *n
1c24e9
     if (!mode) {
1c24e9
 	/* Defer -k mode setting until we know whether it is a flag or not */
1c24e9
 	if (sudo_settings[ARG_IGNORE_TICKET].value != NULL) {
1c24e9
-	    if (argc == 0 && !(flags & (MODE_SHELL|MODE_LOGIN_SHELL))) {
1c24e9
+	    if (argc == 0 && !ISSET(flags, MODE_SHELL|MODE_LOGIN_SHELL)) {
1c24e9
 		mode = MODE_INVALIDATE;	/* -k by itself */
1c24e9
 		sudo_settings[ARG_IGNORE_TICKET].value = NULL;
1c24e9
 		valid_flags = 0;
1c24e9
@@ -568,23 +574,24 @@ parse_args(int argc, char **argv, int *n
1c24e9
     /*
1c24e9
      * For shell mode we need to rewrite argv
1c24e9
      */
1c24e9
-    if (ISSET(mode, MODE_RUN) && ISSET(flags, MODE_SHELL)) {
1c24e9
+    if (ISSET(flags, MODE_SHELL|MODE_LOGIN_SHELL) && ISSET(mode, MODE_RUN)) {
1c24e9
 	char **av, *cmnd = NULL;
1c24e9
 	int ac = 1;
1c24e9
 
1c24e9
 	if (argc != 0) {
1c24e9
 	    /* shell -c "command" */
1c24e9
 	    char *src, *dst;
1c24e9
-	    size_t cmnd_size = (size_t) (argv[argc - 1] - argv[0]) +
1c24e9
-		strlen(argv[argc - 1]) + 1;
1c24e9
+        size_t size = 0;
1c24e9
 
1c24e9
-	    cmnd = dst = reallocarray(NULL, cmnd_size, 2);
1c24e9
-	    if (cmnd == NULL)
1c24e9
+        for (av = argv; *av != NULL; av++)
1c24e9
+            size += strlen(*av) + 1;
1c24e9
+
1c24e9
+        if (size == 0 || (cmnd = reallocarray(NULL, size, 2)) == NULL)
1c24e9
 		sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1c24e9
 	    if (!gc_add(GC_PTR, cmnd))
1c24e9
 		exit(1);
1c24e9
 
1c24e9
-	    for (av = argv; *av != NULL; av++) {
1c24e9
+        for (dst = cmnd, av = argv; *av != NULL; av++) {
1c24e9
 		for (src = *av; *src != '\0'; src++) {
1c24e9
 		    /* quote potential meta characters */
1c24e9
 		    if (!isalnum((unsigned char)*src) && *src != '_' && *src != '-' && *src != '$')