VAITP Dataset

← Back to the dataset

CVE-2022-35861

Pyenv versions 1.2.24 to 2.3.2 allow privilege escalation via a malicious .python-version file, enabling controlled shims execution

  • CVSS 7.8
  • CWE-22 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
  • Authentication, Authorization, and Session Management
  • Local

pyenv 1.2.24 through 2.3.2 allows local users to gain privileges via a .python-version file in the current working directory. An attacker can craft a Python version string in .python-version to execute shims under their control. (Shims are executables that pass a command along to a specific version of pyenv. The version string is used to construct the path to the command, and there is no validation of whether the version specified is a valid version. Thus, relative path traversal can occur.)

CVSS base score
7.8
Published
2022-07-17
OWASP
A05 Security Misconfiguration
Orthogonal defect classification
Function
Code defect classification
Incorrect Functionality
Category
Authentication, Authorization, and Session Management
Subcategory
Privilege Escalation
Accessibility scope
Local
Impact
Privilege Escalation
Affected component
pyenv
Fixed by upgrading
Yes

Solution

Update pyenv to version 2.3.3 or higher.

Vulnerable code sample

import os

def execute_python_version():
    current_directory = os.getcwd()
    version_file_path = os.path.join(current_directory, '.python-version')

    if os.path.exists(version_file_path):
        with open(version_file_path, 'r') as f:
            version = f.read().strip()
            # Directly constructing the path without validation
            pyenv_root = os.path.expanduser("~/.pyenv")
            version_path = os.path.join(pyenv_root, "versions", version)

            # This could lead to execution of arbitrary shims
            if os.path.isfile(version_path):
                os.system(version_path)
            else:
                print(f"Python version {version} not found.")

# Example usage
execute_python_version()

Patched code sample

import os
import re

def validate_python_version(version):
    # Basic validation to ensure the version is a valid format
    # This regex can be adjusted based on the expected version formats
    if re.match(r'^\d+\.\d+(\.\d+)?$', version):
        return True
    return False

def execute_python_version(version):
    if not validate_python_version(version):
        raise ValueError("Invalid Python version specified.")

    # Construct the path safely
    pyenv_root = os.path.expanduser("~/.pyenv")
    version_path = os.path.join(pyenv_root, "versions", version)

    if not os.path.isdir(version_path):
        raise FileNotFoundError(f"Python version {version} not found.")

    # Proceed to execute the shim or the command
    # This is a placeholder for the actual execution logic
    print(f"Executing Python version at: {version_path}")

# Example usage
current_directory = os.getcwd()
version_file_path = os.path.join(current_directory, '.python-version')

if os.path.exists(version_file_path):
    with open(version_file_path, 'r') as f:
        version = f.read().strip()
        execute_python_version(version)

Cite this entry

@misc{vaitp:cve202235861,
  title        = {{Pyenv versions 1.2.24 to 2.3.2 allow privilege escalation via a malicious .python-version file, enabling controlled shims execution}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2022},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2022-35861},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2022-35861/}}
}

Download this entry as JSON Download the whole dataset

Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.

The supreme art of war is to subdue the enemy without fighting.

Sun Tzu – “The Art of War”

:: Shaping the future through research and ingenuity ::

Before you use the VAITP AI models

Please read and accept the following disclaimer:

By choosing "I understand and accept", you acknowledge that you understand and agree to these terms.