CVE-2024-9287 - CPython `venv` Module and CLI Vulnerability Found, Unquoted Path Names Allow Command Injection on Activation Scripts

A vulnerability (CVE-2024-9287) has been discovered in the CPython venv module and CLI that allows an attacker to inject commands into virtual environment "activation" scripts. This is due to path names provided when creating a virtual environment not being quoted properly. As a result, attacker-controlled virtual environments can execute commands when the virtual environment is activated. Virtual environments that are not created by an attacker or which aren't activated before being used, i.e., ./venv/bin/python, are not affected by this vulnerability.

Code Snippet Showcasing the Vulnerability

The following code snippet demonstrates the creation of a malicious virtual environment that triggers the vulnerability:

import venv

# Create a malicious virtual environment with an injected command
virtual_env_name = 'malicious_venv$(touch /tmp/malicious_file)'
venv.create(virtual_env_name)

When this malicious virtual environment is activated, the injected command will create an empty file at the /tmp/malicious_file location.

1. CVE-2024-9287 Record on MITRE
2. Official CPython Issue Related to this Vulnerability
3. Patch for the Vulnerability

Exploit Details

The vulnerability lies in the creation of the activation script files for virtual environments. When the CPython venv module or CLI creates a virtual environment, activation scripts are created to easily activate the virtual environment and set up the environment variables. It has been discovered that path names are not correctly quoted during this process, allowing for command injection via path names that contain special characters. Below is an example of an activation script with the injected command:

# virtualenvwrapper_activate.sh

function workon() {
    # Injected_command_here
    source ~/Envs/${1}/bin/activate
}

When this script is sourced to activate the virtual environment, the injected command will execute.

Mitigation

To prevent exploitation of this vulnerability, it is advised to ensure that virtual environments are created using trusted sources and not activated before being used. Additionally, a patch for this vulnerability is available, and users should update their CPython versions to incorporate the fix.

Users can verify whether a virtual environment is safe by inspecting the activation scripts for any suspicious commands. Moreover, avoid using virtual environments from untrusted sources. Following these guidelines will reduce the risk of exploitation.

Conclusion

In summary, CVE-2024-9287 is a vulnerability in the CPython venv module and CLI that allows command injection via unquoted path names when creating virtual environments. This vulnerability can be mitigated by using trusted virtual environments, not activating them before use, and updating CPython with the provided patch. By practicing caution and taking appropriate protective measures, users can safeguard their systems from this vulnerability.

Timeline

Published on: 10/22/2024 17:15:06 UTC
Last modified on: 12/06/2024 16:08:08 UTC