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

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