The sudoedit command is a tool that allows users to edit files with elevated privileges. It is a popular tool for system administrators and other users who need to make changes to system files.
However, the sudoedit command is also vulnerable to a privilege escalation vulnerability. This vulnerability, known as CVE-2023-22809, allows a local attacker to gain root privileges on a system by adding a line to the sudoers file that grants them permission to run any command as root.
How the vulnerability can be exploited
The CVE-2023-22809 vulnerability is caused by a flaw in the way that sudoedit handles environment variables. When sudoedit is run, it reads the environment variables of the current user. If one of these environment variables contains a "--" argument, sudoedit will add the rest of the arguments to the list of files to edit.
An attacker can exploit this vulnerability by setting an environment variable that contains a "--" argument followed by a line that grants them permission to run any command as root. When sudoedit is run, it will add this line to the sudoers file, giving the attacker root privileges.
Explanation of Exploit Code
The script first checks the version of sudo installed on the system. It verifies if the installed version is within the vulnerable range of 1.8.0 to 1.9.12p1. If the installed version is not vulnerable, it exits with an appropriate message.
The script then checks if the user running the script has the necessary privileges to execute sudoedit or sudo -e as root. It checks the output of sudo -l command for specific strings indicating the user's privileges. If the user does not have the required privileges, it prompts the user to confirm whether they want to proceed anyway.
If the user has the exploitable privileges, the script proceeds and displays a message indicating that the user is exploitable.
The script then prompts the user to add a specific line to the /etc/sudoers file to gain privileges. It provides the line that needs to be added to the file.
After the user confirms, the script attempts to open the /etc/sudoers file using the configured editor. The EDITOR environment variable is set to "vim -- /etc/sudoers" to open the file in vim editor with the -- option to prevent processing command-line arguments. The exploit relies on the user being able to execute sudoedit or sudo -e with the configured editor.
Finally, the script attempts to switch to the root user using sudo su root command.
How to check if your system is vulnerable
To check if your system is vulnerable to the CVE-2023-22809 vulnerability, you can run the following command:
If the output of the command includes a version number that is between 1.8.0 and 1.9.12p1, your system is vulnerable.
How to patch the vulnerability
The vulnerability has been patched in sudo versions 1.9.13 and later. To patch your system, you can download the latest version of sudo from the sudo website: https://www.sudo.ws/
In addition to patching the vulnerability, you can also take the following mitigation strategies to prevent the vulnerability from being exploited:
Do not set environment variables that contain "--" arguments.
Use a restricted sudoers file that only grants users the privileges they need.
Use a privilege escalation detection tool to monitor your system for signs of unauthorized activity.
The CVE-2023-22809 sudoedit privilege escalation vulnerability is a serious security risk. If you are running a vulnerable version of sudo, you should patch your system as soon as possible. You should also take the mitigation strategies outlined above to prevent the vulnerability from being exploited.
For more information on this vulnerability, please see the following resources:
Red Hat Customer Portal: https://access.redhat.com/security/cve/cve-2023-22809