VAITP Dataset

← Back to the dataset

CVE-2021-32807

Remote code execution in Zope via AccessControl v4 and v5

  • CVSS 7.2
  • CWE-1321
  • Design Defects
  • Remote

The module `AccessControl` defines security policies for Python code used in restricted code within Zope applications. Restricted code is any code that resides in Zope's object database, such as the contents of `Script (Python)` objects. The policies defined in `AccessControl` severely restrict access to Python modules and only exempt a few that are deemed safe, such as Python's `string` module. However, full access to the `string` module also allows access to the class `Formatter`, which can be overridden and extended within `Script (Python)` in a way that provides access to other unsafe Python libraries. Those unsafe Python libraries can be used for remote code execution. By default, you need to have the admin-level Zope "Manager" role to add or edit `Script (Python)` objects through the web. Only sites that allow untrusted users to add/edit these scripts through the web – which would be a very unusual configuration to begin with – are at risk. The problem has been fixed in AccessControl 4.3 and 5.2. Only AccessControl versions 4 and 5 are vulnerable, and only on Python 3, not Python 2.7. As a workaround, a site administrator can restrict adding/editing `Script (Python)` objects through the web using the standard Zope user/role permission mechanisms. Untrusted users should not be assigned the Zope Manager role and adding/editing these scripts through the web should be restricted to trusted users only. This is the default configuration in Zope.

CVSS base score
7.2
Published
2021-07-30
OWASP
A05 Security Misconfiguration
Orthogonal defect classification
Function
Code defect classification
Incorrect Functionality
Category
Design Defects
Subcategory
Poorly Designed Access Controls
Accessibility scope
Remote
Impact
Arbitrary Code Execution
Affected component
AccessControl
Fixed by upgrading
Yes

Solution

Update AccessControl to version 4.3 or 5.2 or restrict adding/editing Script (Python) objects through the web using Zope user/role permissions.

Vulnerable code sample

from AccessControl import getSecurityManager
from string import Formatter

class UnsafeFormatter(Formatter):
    def get_value(self, key, args, kwargs):
        # This method could be overridden to access unsafe libraries
        if key == 'os':
            import os  # This is an unsafe operation
            return os
        return super().get_value(key, args, kwargs)

# Example of a vulnerable script that allows untrusted users to add/edit scripts
def vulnerable_script(user_input):
    formatter = UnsafeFormatter()
    return formatter.format(user_input)

# Simulating adding a script by an untrusted user
def add_script(script_code):
    if getSecurityManager().checkPermission('Add script', None):
        exec(script_code)  # This could allow execution of arbitrary code
    else:
        raise Exception("Unauthorized: You do not have permission to add scripts.")

Patched code sample

from AccessControl import getSecurityManager
from AccessControl import ClassSecurityInfo
from AccessControl.Permissions import add_script
from AccessControl.Permissions import change_script

class SecureFormatter:
    security = ClassSecurityInfo()

    @security.protected(add_script)
    def add_script(self, script_name, script_code):
        # Only allow trusted users to add scripts
        if getSecurityManager().checkPermission('Add script', self):
            # Logic to add the script safely
            pass
        else:
            raise Unauthorized("You do not have permission to add scripts.")

    @security.protected(change_script)
    def change_script(self, script_name, new_script_code):
        # Only allow trusted users to change scripts
        if getSecurityManager().checkPermission('Change script', self):
            # Logic to change the script safely
            pass
        else:
            raise Unauthorized("You do not have permission to change scripts.")

Published fix

Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.

Cite this entry

@misc{vaitp:cve202132807,
  title        = {{Remote code execution in Zope via AccessControl v4 and v5}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2021},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2021-32807},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2021-32807/}}
}

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.