Blame SOURCES/sudo-1.9.12-CVE-2023-22809.patch

ab1841
diff -up ./plugins/sudoers/editor.c.cve ./plugins/sudoers/editor.c
ab1841
--- ./plugins/sudoers/editor.c.cve	2021-01-09 21:12:16.000000000 +0100
ab1841
+++ ./plugins/sudoers/editor.c	2023-01-17 13:57:05.598949058 +0100
ab1841
@@ -126,7 +126,7 @@ resolve_editor(const char *ed, size_t ed
ab1841
     const char *tmp, *cp, *ep = NULL;
ab1841
     const char *edend = ed + edlen;
ab1841
     struct stat user_editor_sb;
ab1841
-    int nargc;
ab1841
+    int nargc = 0;
ab1841
     debug_decl(resolve_editor, SUDOERS_DEBUG_UTIL);
ab1841
 
ab1841
     /*
ab1841
@@ -144,9 +144,7 @@ resolve_editor(const char *ed, size_t ed
ab1841
     /* If we can't find the editor in the user's PATH, give up. */
ab1841
     if (find_path(editor, &editor_path, &user_editor_sb, getenv("PATH"), NULL,
ab1841
 	    0, allowlist) != FOUND) {
ab1841
-	free(editor);
ab1841
-	errno = ENOENT;
ab1841
-	debug_return_str(NULL);
ab1841
+        goto bad;
ab1841
     }
ab1841
 
ab1841
     /* Count rest of arguments and allocate editor argv. */
ab1841
@@ -166,6 +164,18 @@ resolve_editor(const char *ed, size_t ed
ab1841
 	nargv[nargc] = copy_arg(cp, ep - cp);
ab1841
 	if (nargv[nargc] == NULL)
ab1841
 	    goto oom;
ab1841
+
ab1841
+	/*
ab1841
+	 * We use "--" to separate the editor and arguments from the files
ab1841
+	 * to edit.  The editor arguments themselves may not contain "--".
ab1841
+	 */
ab1841
+	if (strcmp(nargv[nargc], "--") == 0) {
ab1841
+	    sudo_warnx(U_("ignoring editor: %.*s"), (int)edlen, ed);
ab1841
+	    sudo_warnx("%s", U_("editor arguments may not contain \"--\""));
ab1841
+	    errno = EINVAL;
ab1841
+	    goto bad;
ab1841
+	}
ab1841
+
ab1841
     }
ab1841
     if (nfiles != 0) {
ab1841
 	nargv[nargc++] = "--";
ab1841
@@ -179,6 +189,7 @@ resolve_editor(const char *ed, size_t ed
ab1841
     debug_return_str(editor_path);
ab1841
 oom:
ab1841
     sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
ab1841
+bad:
ab1841
     free(editor);
ab1841
     free(editor_path);
ab1841
     if (nargv != NULL) {
ab1841
diff -up ./plugins/sudoers/sudoers.c.cve ./plugins/sudoers/sudoers.c
ab1841
--- ./plugins/sudoers/sudoers.c.cve	2023-01-17 13:50:33.718255775 +0100
ab1841
+++ ./plugins/sudoers/sudoers.c	2023-01-17 14:00:53.049710094 +0100
ab1841
@@ -724,21 +724,34 @@ sudoers_policy_main(int argc, char * con
ab1841
 
ab1841
     /* Note: must call audit before uid change. */
ab1841
     if (ISSET(sudo_mode, MODE_EDIT)) {
ab1841
+    const char *env_editor = NULL;
ab1841
 	char **edit_argv;
ab1841
 	int edit_argc;
ab1841
-	const char *env_editor;
ab1841
+
ab1841
 
ab1841
 	free(safe_cmnd);
ab1841
 	safe_cmnd = find_editor(NewArgc - 1, NewArgv + 1, &edit_argc,
ab1841
 	    &edit_argv, NULL, &env_editor, false);
ab1841
 	if (safe_cmnd == NULL) {
ab1841
-	    if (errno != ENOENT)
ab1841
-		goto done;
ab1841
-	    audit_failure(NewArgv, N_("%s: command not found"),
ab1841
-		env_editor ? env_editor : def_editor);
ab1841
-	    sudo_warnx(U_("%s: command not found"),
ab1841
-		env_editor ? env_editor : def_editor);
ab1841
-	    goto bad;
ab1841
+
ab1841
+	    switch (errno) {
ab1841
+	    case ENOENT:
ab1841
+		audit_failure(NewArgv, N_("%s: command not found"),
ab1841
+		    env_editor ? env_editor : def_editor);
ab1841
+		sudo_warnx(U_("%s: command not found"),
ab1841
+		    env_editor ? env_editor : def_editor);
ab1841
+		goto bad;
ab1841
+	    case EINVAL:
ab1841
+		if (def_env_editor && env_editor != NULL) {
ab1841
+		    /* User tried to do something funny with the editor. */
ab1841
+		    log_warningx(SLOG_NO_STDERR|SLOG_AUDIT|SLOG_SEND_MAIL,
ab1841
+			"invalid user-specified editor: %s", env_editor);
ab1841
+		    goto bad;
ab1841
+		}
ab1841
+		FALLTHROUGH;
ab1841
+	    default:
ab1841
+            goto done;
ab1841
+        }
ab1841
 	}
ab1841
 	sudoers_gc_add(GC_VECTOR, edit_argv);
ab1841
 	NewArgv = edit_argv;
ab1841
diff -up ./plugins/sudoers/visudo.c.cve ./plugins/sudoers/visudo.c
ab1841
--- ./plugins/sudoers/visudo.c.cve	2021-01-09 21:12:16.000000000 +0100
ab1841
+++ ./plugins/sudoers/visudo.c	2023-01-17 14:02:01.393135129 +0100
ab1841
@@ -303,7 +303,7 @@ static char *
ab1841
 get_editor(int *editor_argc, char ***editor_argv)
ab1841
 {
ab1841
     char *editor_path = NULL, **allowlist = NULL;
ab1841
-    const char *env_editor;
ab1841
+    const char *env_editor = NULL;
ab1841
     static char *files[] = { "+1", "sudoers" };
ab1841
     unsigned int allowlist_len = 0;
ab1841
     debug_decl(get_editor, SUDOERS_DEBUG_UTIL);
ab1841
@@ -337,7 +337,11 @@ get_editor(int *editor_argc, char ***edi
ab1841
     if (editor_path == NULL) {
ab1841
 	if (def_env_editor && env_editor != NULL) {
ab1841
 	    /* We are honoring $EDITOR so this is a fatal error. */
ab1841
-	    sudo_fatalx(U_("specified editor (%s) doesn't exist"), env_editor);
ab1841
+	    if (errno == ENOENT) {
ab1841
+		sudo_warnx(U_("specified editor (%s) doesn't exist"),
ab1841
+		    env_editor);
ab1841
+	    }
ab1841
+	    exit(EXIT_FAILURE);
ab1841
 	}
ab1841
 	sudo_fatalx(U_("no editor found (editor path = %s)"), def_editor);
ab1841
     }