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

acce34
diff -up ./plugins/sudoers/policy.c.heap-buffer ./plugins/sudoers/policy.c
acce34
--- ./plugins/sudoers/policy.c.heap-buffer	2021-01-20 13:10:29.368619293 +0100
acce34
+++ ./plugins/sudoers/policy.c	2021-01-20 15:56:12.915095745 +0100
acce34
@@ -93,11 +93,12 @@ parse_bool(const char *line, int varlen,
acce34
 int
acce34
 sudoers_policy_deserialize_info(void *v, char **runas_user, char **runas_group)
acce34
 {
acce34
+    const int edit_mask = MODE_EDIT|MODE_IGNORE_TICKET|MODE_NONINTERACTIVE;
acce34
     struct sudoers_policy_open_info *info = v;
acce34
-    char * const *cur;
acce34
     const char *p, *errstr, *groups = NULL;
acce34
     const char *remhost = NULL;
acce34
     bool uid_set = false, gid_set = false;
acce34
+    char * const *cur;
acce34
     int flags = 0;
acce34
     debug_decl(sudoers_policy_deserialize_info, SUDOERS_DEBUG_PLUGIN)
acce34
 
acce34
@@ -326,6 +327,12 @@ sudoers_policy_deserialize_info(void *v,
acce34
 #endif
acce34
     }
acce34
 
acce34
+    /* Sudo front-end should restrict mode flags for sudoedit. */
acce34
+    if (ISSET(flags, MODE_EDIT) && (flags & edit_mask) != flags) {
acce34
+        sudo_warnx(U_("invalid mode flags from sudo front end: 0x%x"), flags);
acce34
+        goto bad;
acce34
+    }
acce34
+
acce34
     user_umask = (mode_t)-1;
acce34
     for (cur = info->user_info; *cur != NULL; cur++) {
acce34
 	if (MATCHES(*cur, "user=")) {
acce34
diff -up ./plugins/sudoers/sudoers.c.heap-buffer ./plugins/sudoers/sudoers.c
acce34
--- ./plugins/sudoers/sudoers.c.heap-buffer	2021-01-20 13:10:29.350619537 +0100
acce34
+++ ./plugins/sudoers/sudoers.c	2021-01-20 13:10:29.369619280 +0100
acce34
@@ -416,7 +416,7 @@ sudoers_policy_main(int argc, char * con
acce34
 
acce34
     /* If run as root with SUDO_USER set, set sudo_user.pw to that user. */
acce34
     /* XXX - causes confusion when root is not listed in sudoers */
acce34
-    if (sudo_mode & (MODE_RUN | MODE_EDIT) && prev_user != NULL) {
acce34
+    if (ISSET(sudo_mode, MODE_RUN|MODE_EDIT) && prev_user != NULL) {
acce34
 	if (user_uid == 0 && strcmp(prev_user, "root") != 0) {
acce34
 	    struct passwd *pw;
acce34
 
acce34
@@ -796,8 +796,8 @@ set_cmnd(void)
acce34
     if (user_cmnd == NULL)
acce34
 	user_cmnd = NewArgv[0];
acce34
 
acce34
-    if (sudo_mode & (MODE_RUN | MODE_EDIT | MODE_CHECK)) {
acce34
-	if (ISSET(sudo_mode, MODE_RUN | MODE_CHECK)) {
acce34
+    if (ISSET(sudo_mode, MODE_RUN|MODE_EDIT|MODE_CHECK)) {
acce34
+	if (!ISSET(sudo_mode, MODE_EDIT)) {
acce34
 	    if (def_secure_path && !user_is_exempt())
acce34
 		path = def_secure_path;
acce34
 	    if (!set_perms(PERM_RUNAS))
acce34
@@ -835,7 +835,8 @@ set_cmnd(void)
acce34
 		sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
acce34
 		debug_return_int(-1);
acce34
 	    }
acce34
-	    if (ISSET(sudo_mode, MODE_SHELL|MODE_LOGIN_SHELL)) {
acce34
+	    if (ISSET(sudo_mode, MODE_SHELL|MODE_LOGIN_SHELL) &&
acce34
+            ISSET(sudo_mode, MODE_RUN)) {
acce34
 		/*
acce34
 		 * When running a command via a shell, the sudo front-end
acce34
 		 * escapes potential meta chars.  We unescape non-spaces
acce34
@@ -843,10 +844,22 @@ set_cmnd(void)
acce34
 		 */
acce34
 		for (to = user_args, av = NewArgv + 1; (from = *av); av++) {
acce34
 		    while (*from) {
acce34
-			if (from[0] == '\\' && !isspace((unsigned char)from[1]))
acce34
+			if (from[0] == '\\' && from[1] != '\0' &&
acce34
+				!isspace((unsigned char)from[1])) {
acce34
 			    from++;
acce34
+			}
acce34
+			if (size - (to - user_args) < 1) {
acce34
+			    sudo_warnx(U_("internal error, %s overflow"),
acce34
+				__func__);
acce34
+			    debug_return_int(NOT_FOUND_ERROR);
acce34
+			}
acce34
 			*to++ = *from++;
acce34
 		    }
acce34
+		    if (size - (to - user_args) < 1) {
acce34
+			sudo_warnx(U_("internal error, %s overflow"),
acce34
+			    __func__);
acce34
+			debug_return_int(NOT_FOUND_ERROR);
acce34
+		    }
acce34
 		    *to++ = ' ';
acce34
 		}
acce34
 		*--to = '\0';
acce34
diff -up ./plugins/sudoers/timestamp.c.heap-buffer ./plugins/sudoers/timestamp.c
acce34
--- ./plugins/sudoers/timestamp.c.heap-buffer	2018-09-02 14:30:08.000000000 +0200
acce34
+++ ./plugins/sudoers/timestamp.c	2021-01-20 13:10:29.369619280 +0100
acce34
@@ -646,8 +646,8 @@ timestamp_lock(void *vcookie, struct pas
acce34
     } else if (entry.type != TS_LOCKEXCL) {
acce34
 	/* Old sudo record, convert it to TS_LOCKEXCL. */
acce34
 	entry.type = TS_LOCKEXCL;
acce34
-	memset((char *)&entry + offsetof(struct timestamp_entry, type), 0,
acce34
-	    nread - offsetof(struct timestamp_entry, type));
acce34
+	memset((char *)&entry + offsetof(struct timestamp_entry, flags), 0,
acce34
+	    nread - offsetof(struct timestamp_entry, flags));
acce34
 	if (ts_write(cookie->fd, cookie->fname, &entry, 0) == -1)
acce34
 	    debug_return_bool(false);
acce34
     }
acce34
diff -up ./src/parse_args.c.heap-buffer ./src/parse_args.c
acce34
--- ./src/parse_args.c.heap-buffer	2018-08-18 16:10:25.000000000 +0200
acce34
+++ ./src/parse_args.c	2021-01-20 16:01:42.500778441 +0100
acce34
@@ -116,7 +116,10 @@ struct environment {
acce34
 /*
acce34
  * Default flags allowed when running a command.
acce34
  */
acce34
-#define DEFAULT_VALID_FLAGS	(MODE_BACKGROUND|MODE_PRESERVE_ENV|MODE_RESET_HOME|MODE_LOGIN_SHELL|MODE_NONINTERACTIVE|MODE_SHELL)
acce34
+#define DEFAULT_VALID_FLAGS	(MODE_BACKGROUND|MODE_PRESERVE_ENV|MODE_RESET_HOME|MODE_LOGIN_SHELL|MODE_NONINTERACTIVE|MODE_PRESERVE_GROUPS|MODE_SHELL)
acce34
+#define EDIT_VALID_FLAGS	MODE_NONINTERACTIVE
acce34
+#define LIST_VALID_FLAGS	(MODE_NONINTERACTIVE|MODE_LONG_LIST)
acce34
+#define VALIDATE_VALID_FLAGS	MODE_NONINTERACTIVE
acce34
 
acce34
 /* Option number for the --host long option due to ambiguity of the -h flag. */
acce34
 #define OPT_HOSTNAME	256
acce34
@@ -260,6 +263,7 @@ parse_args(int argc, char **argv, int *n
acce34
 	progname = "sudoedit";
acce34
 	mode = MODE_EDIT;
acce34
 	sudo_settings[ARG_SUDOEDIT].value = "true";
acce34
+	valid_flags = EDIT_VALID_FLAGS;
acce34
     }
acce34
 
acce34
     /* Load local IP addresses and masks. */
acce34
@@ -345,7 +349,7 @@ parse_args(int argc, char **argv, int *n
acce34
 			usage_excl(1);
acce34
 		    mode = MODE_EDIT;
acce34
 		    sudo_settings[ARG_SUDOEDIT].value = "true";
acce34
-		    valid_flags = MODE_NONINTERACTIVE;
acce34
+		    valid_flags = EDIT_VALID_FLAGS;
acce34
 		    break;
acce34
 		case 'g':
acce34
 		    if (*optarg == '\0')
acce34
@@ -355,6 +359,7 @@ parse_args(int argc, char **argv, int *n
acce34
 		    break;
acce34
 		case 'H':
acce34
 		    sudo_settings[ARG_SET_HOME].value = "true";
acce34
+		    SET(flags, MODE_RESET_HOME);
acce34
 		    break;
acce34
 		case 'h':
acce34
 		    if (optarg == NULL) {
acce34
@@ -404,7 +409,7 @@ parse_args(int argc, char **argv, int *n
acce34
 			    usage_excl(1);
acce34
 		    }
acce34
 		    mode = MODE_LIST;
acce34
-		    valid_flags = MODE_NONINTERACTIVE|MODE_LONG_LIST;
acce34
+		    valid_flags = LIST_VALID_FLAGS;
acce34
 		    break;
acce34
 		case 'n':
acce34
 		    SET(flags, MODE_NONINTERACTIVE);
acce34
@@ -412,6 +417,7 @@ parse_args(int argc, char **argv, int *n
acce34
 		    break;
acce34
 		case 'P':
acce34
 		    sudo_settings[ARG_PRESERVE_GROUPS].value = "true";
acce34
+		    SET(flags, MODE_PRESERVE_GROUPS);
acce34
 		    break;
acce34
 		case 'p':
acce34
 		    /* An empty prompt is allowed. */
acce34
@@ -455,7 +461,7 @@ parse_args(int argc, char **argv, int *n
acce34
 		    if (mode && mode != MODE_VALIDATE)
acce34
 			usage_excl(1);
acce34
 		    mode = MODE_VALIDATE;
acce34
-		    valid_flags = MODE_NONINTERACTIVE;
acce34
+		    valid_flags = VALIDATE_VALID_FLAGS;
acce34
 		    break;
acce34
 		case 'V':
acce34
 		    if (mode && mode != MODE_VERSION)
acce34
@@ -482,7 +488,7 @@ parse_args(int argc, char **argv, int *n
acce34
     if (!mode) {
acce34
 	/* Defer -k mode setting until we know whether it is a flag or not */
acce34
 	if (sudo_settings[ARG_IGNORE_TICKET].value != NULL) {
acce34
-	    if (argc == 0 && !(flags & (MODE_SHELL|MODE_LOGIN_SHELL))) {
acce34
+	    if (argc == 0 && !ISSET(flags, MODE_SHELL|MODE_LOGIN_SHELL)) {
acce34
 		mode = MODE_INVALIDATE;	/* -k by itself */
acce34
 		sudo_settings[ARG_IGNORE_TICKET].value = NULL;
acce34
 		valid_flags = 0;
acce34
@@ -545,23 +551,24 @@ parse_args(int argc, char **argv, int *n
acce34
     /*
acce34
      * For shell mode we need to rewrite argv
acce34
      */
acce34
-    if (ISSET(mode, MODE_RUN) && ISSET(flags, MODE_SHELL)) {
acce34
+    if (ISSET(flags, MODE_SHELL|MODE_LOGIN_SHELL) && ISSET(mode, MODE_RUN)) {
acce34
 	char **av, *cmnd = NULL;
acce34
 	int ac = 1;
acce34
 
acce34
 	if (argc != 0) {
acce34
 	    /* shell -c "command" */
acce34
 	    char *src, *dst;
acce34
-	    size_t cmnd_size = (size_t) (argv[argc - 1] - argv[0]) +
acce34
-		strlen(argv[argc - 1]) + 1;
acce34
+        size_t size = 0;
acce34
 
acce34
-	    cmnd = dst = reallocarray(NULL, cmnd_size, 2);
acce34
-	    if (cmnd == NULL)
acce34
+        for (av = argv; *av != NULL; av++)
acce34
+            size += strlen(*av) + 1;
acce34
+
acce34
+        if (size == 0 || (cmnd = reallocarray(NULL, size, 2)) == NULL)
acce34
 		sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
acce34
 	    if (!gc_add(GC_PTR, cmnd))
acce34
 		exit(1);
acce34
 
acce34
-	    for (av = argv; *av != NULL; av++) {
acce34
+        for (dst = cmnd, av = argv; *av != NULL; av++) {
acce34
 		for (src = *av; *src != '\0'; src++) {
acce34
 		    /* quote potential meta characters */
acce34
 		    if (!isalnum((unsigned char)*src) && *src != '_' && *src != '-' && *src != '$')