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

874fbb
diff -up ./plugins/sudoers/editor.c.other ./plugins/sudoers/editor.c
874fbb
--- ./plugins/sudoers/editor.c.other	2023-01-16 17:37:04.659967300 +0100
874fbb
+++ ./plugins/sudoers/editor.c	2023-01-16 17:40:35.944400376 +0100
874fbb
@@ -39,6 +39,82 @@
874fbb
 #include "sudoers.h"
874fbb
 
874fbb
 /*
874fbb
+ * Non-destructive word-split that handles single and double quotes and
874fbb
+ * escaped white space.  Quotes are only recognized at the start of a word.
874fbb
+ * They are treated as normal characters inside a word.
874fbb
+ */
874fbb
+static const char *
874fbb
+wordsplit(const char *str, const char *endstr, const char **last)
874fbb
+{
874fbb
+    const char *cp;
874fbb
+    debug_decl(wordsplit, SUDO_DEBUG_UTIL);
874fbb
+
874fbb
+    /* If no str specified, use last ptr (if any). */
874fbb
+    if (str == NULL) {
874fbb
+	str = *last;
874fbb
+	/* Consume end quote if present. */
874fbb
+	if (*str == '"' || *str == '\'')
874fbb
+	    str++;
874fbb
+    }
874fbb
+
874fbb
+    /* Skip leading white space characters. */
874fbb
+    while (str < endstr && (*str == ' ' || *str == '\t'))
874fbb
+	str++;
874fbb
+
874fbb
+    /* Empty string? */
874fbb
+    if (str >= endstr) {
874fbb
+	*last = endstr;
874fbb
+	debug_return_ptr(NULL);
874fbb
+    }
874fbb
+
874fbb
+    /* If word is quoted, skip to end quote and return. */
874fbb
+    if (*str == '"' || *str == '\'') {
874fbb
+	const char *endquote = memchr(str + 1, *str, endstr - str);
874fbb
+	if (endquote != NULL) {
874fbb
+	    *last = endquote;
874fbb
+	    debug_return_const_ptr(str + 1);
874fbb
+	}
874fbb
+    }
874fbb
+
874fbb
+    /* Scan str until we encounter white space. */
874fbb
+    for (cp = str; cp < endstr; cp++) {
874fbb
+	if (*cp == '\\') {
874fbb
+	    /* quoted char, do not interpret */
874fbb
+	    cp++;
874fbb
+	    continue;
874fbb
+	}
874fbb
+	if (*cp == ' ' || *cp == '\t') {
874fbb
+	    /* end of word */
874fbb
+	    break;
874fbb
+	}
874fbb
+    }
874fbb
+    *last = cp;
874fbb
+    debug_return_const_ptr(str);
874fbb
+}
874fbb
+
874fbb
+/* Copy len chars from string, collapsing chars escaped with a backslash. */
874fbb
+static char *
874fbb
+copy_arg(const char *src, size_t len)
874fbb
+{
874fbb
+    const char *src_end = src + len;
874fbb
+    char *copy, *dst;
874fbb
+    debug_decl(copy_arg, SUDOERS_DEBUG_UTIL);
874fbb
+
874fbb
+    if ((copy = malloc(len + 1)) != NULL) {
874fbb
+	for (dst = copy; src < src_end; ) {
874fbb
+	    if (*src == '\\') {
874fbb
+		src++;
874fbb
+		continue;
874fbb
+	    }
874fbb
+	    *dst++ = *src++;
874fbb
+	}
874fbb
+	*dst = '\0';
874fbb
+    }
874fbb
+
874fbb
+    debug_return_ptr(copy);
874fbb
+}
874fbb
+
874fbb
+/*
874fbb
  * Search for the specified editor in the user's PATH, checking
874fbb
  * the result against allowlist if non-NULL.  An argument vector
874fbb
  * suitable for execve() is allocated and stored in argv_out.
874fbb
@@ -52,7 +128,7 @@ static char *
874fbb
 resolve_editor(const char *ed, size_t edlen, int nfiles, char **files,
874fbb
     int *argc_out, char ***argv_out, char * const *allowlist)
874fbb
 {
874fbb
-    char **nargv, *editor, *editor_path = NULL;
874fbb
+    char **nargv = NULL, *editor = NULL, *editor_path = NULL;
874fbb
     const char *cp, *ep, *tmp;
874fbb
     const char *edend = ed + edlen;
874fbb
     struct stat user_editor_sb;
874fbb
@@ -64,14 +140,12 @@ resolve_editor(const char *ed, size_t ed
874fbb
      * The EDITOR and VISUAL environment variables may contain command
874fbb
      * line args so look for those and alloc space for them too.
874fbb
      */
874fbb
-    cp = sudo_strsplit(ed, edend, " \t", &ep);
874fbb
+    cp = wordsplit(ed, edend, &ep);
874fbb
     if (cp == NULL)
874fbb
 	debug_return_str(NULL);
874fbb
-    editor = strndup(cp, (size_t)(ep - cp));
874fbb
-    if (editor == NULL) {
874fbb
-	sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
874fbb
-	debug_return_str(NULL);
874fbb
-    }
874fbb
+    editor = copy_arg(cp, ep - cp);
874fbb
+    if (editor == NULL)
874fbb
+        goto oom;
874fbb
 
874fbb
     /* If we can't find the editor in the user's PATH, give up. */
874fbb
     if (find_path(editor, &editor_path, &user_editor_sb, getenv("PATH"), 0, allowlist) != FOUND) {
874fbb
@@ -81,30 +155,22 @@ resolve_editor(const char *ed, size_t ed
874fbb
     }
874fbb
 
874fbb
     /* Count rest of arguments and allocate editor argv. */
874fbb
-    for (nargc = 1, tmp = ep; sudo_strsplit(NULL, edend, " \t", &tmp) != NULL; )
874fbb
+    for (nargc = 1, tmp = ep; wordsplit(NULL, edend, &tmp) != NULL; )
874fbb
 	nargc++;
874fbb
     if (nfiles != 0)
874fbb
 	nargc += nfiles + 1;
874fbb
     nargv = reallocarray(NULL, nargc + 1, sizeof(char *));
874fbb
-    if (nargv == NULL) {
874fbb
-	sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
874fbb
-	free(editor);
874fbb
-	free(editor_path);
874fbb
-	debug_return_str(NULL);
874fbb
-    }
874fbb
+    if (nargv == NULL)
874fbb
+	goto oom;
874fbb
 
874fbb
     /* Fill in editor argv (assumes files[] is NULL-terminated). */
874fbb
     nargv[0] = editor;
874fbb
-    for (nargc = 1; (cp = sudo_strsplit(NULL, edend, " \t", &ep)) != NULL; nargc++) {
874fbb
-	nargv[nargc] = strndup(cp, (size_t)(ep - cp));
874fbb
-	if (nargv[nargc] == NULL) {
874fbb
-	    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
874fbb
-	    free(editor_path);
874fbb
-	    while (nargc--)
874fbb
-		free(nargv[nargc]);
874fbb
-	    free(nargv);
874fbb
-	    debug_return_str(NULL);
874fbb
-	}
874fbb
+    editor = NULL;
874fbb
+    for (nargc = 1; (cp = wordsplit(NULL, edend, &ep)) != NULL; nargc++) {
874fbb
+	/* Copy string, collapsing chars escaped with a backslash. */
874fbb
+	nargv[nargc] = copy_arg(cp, ep - cp);
874fbb
+	if (nargv[nargc] == NULL)
874fbb
+	    goto oom;
874fbb
     }
874fbb
     if (nfiles != 0) {
874fbb
 	nargv[nargc++] = "--";
874fbb
@@ -116,6 +182,16 @@ resolve_editor(const char *ed, size_t ed
874fbb
     *argc_out = nargc;
874fbb
     *argv_out = nargv;
874fbb
     debug_return_str(editor_path);
874fbb
+oom:
874fbb
+    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
874fbb
+    free(editor);
874fbb
+    free(editor_path);
874fbb
+    if (nargv != NULL) {
874fbb
+	while (nargc--)
874fbb
+	    free(nargv[nargc]);
874fbb
+	free(nargv);
874fbb
+    }
874fbb
+    debug_return_str(NULL);
874fbb
 }
874fbb
 
874fbb
 /*