CVE-2023-37271: RestrictedPython RestrictedPython Vulnerable To Arbitrary Code Vulnerability

HIGH
Advisory Identifier: GHSA-wqc8-x2pr-7jqh
CVSS 8.4
CWE-913
Published
Advisory Summary

CVE-2023-37271 is a HIGH (CVSS 8.4) restrictedpython vulnerable to arbitrary code vulnerability (CWE-913: Improper Control of Dynamically-Managed Code Resources) affecting RestrictedPython libraries in the PyPI ecosystem. The issue involves restrictedpython vulnerable to arbitrary code execution via stack frame sandbox escape. Remediation is available by upgrading affected packages to version 5.3, 6.1.

CVE-2023-37271 at a Glance

CVE Identifier CVE-2023-37271
GHSA Identifier GHSA-wqc8-x2pr-7jqh
Severity HIGH
CVSS v3.1 Base Score 8.4
CWE Classification CWE-913: Improper Control of Dynamically-Managed Code Resources
Affected Product RestrictedPython
Package Ecosystem PyPI
Published Date
Last Updated

Security Impact

CVSS: 8.4

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:C/C:H/I:H/A:L).

Attack Vector Network (AV:N)
Attack Complexity High (AC:H)
Privileges Required Low (PR:L)
User Interaction None (UI:N)
Scope Changed (S:C)
Confidentiality High (C:H)
Integrity High (I:H)
Availability Low (A:L)

How to Fix CVE-2023-37271

To remediate this vulnerability, upgrade all affected RestrictedPython dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

RestrictedPython fixed: 5.3
pip install RestrictedPython>=5.3
RestrictedPython fixed: 6.1
pip install RestrictedPython>=6.1
restrictedpython fixed: 5.3
pip install restrictedpython>=5.3
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

pip show RestrictedPython

How to Check if You Are Affected

To determine whether your Python environment is affected by CVE-2023-37271, inspect your installed packages using pip:

pip show RestrictedPython

Analyze the terminal output to locate any version of RestrictedPython in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 5.3), your application is affected.

Note: The pip show RestrictedPython command checks currently installed packages in your active Python environment.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-913: Improper Control of Dynamically-Managed Code Resources, where the product does not properly restrict reading from or writing to dynamically-managed code resources such as variables, objects, classes, attributes, functions, or executable instructions or statements. In RestrictedPython, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

### Impact RestrictedPython does not check access to stack frames and their attributes. Stack frames are accessible within at least generators and generator expressions, which are allowed inside RestrictedPython. An attacker with access to a RestrictedPython environment can write code that gets the current stack frame in a generator and then walk the stack all the way beyond the RestrictedPython invocation boundary, thus breaking out of the restricted scope allowing the call of unrestricted Python code and therefore potentially allowing arbitrary code execution in the Python interpreter. All RestrictedPython deployments that allow untrusted users to write Python code in the RestrictedPython environment are at risk. In terms of Zope and Plone, this would mean deployments where the administrator allows untrusted users to create and/or edit objects of type `Script (Python)`, `DTML Method`, `DTML Document` or `Zope Page Template`. This is a non-default configuration and likely to be extremely rare. ### Patches The problem has been fixed in releases 5.3 and 6.1. ### Workarounds There is no workaround available. If you cannot upgrade to the latest release you should ensure the RestrictedPython environment is only available for trusted users. ### References - [RestrictedPython security advisory GHSA-wqc8-x2pr-7jqh](https://github.com/zopefoundation/RestrictedPython/security/advisories/GHSA-wqc8-x2pr-7jqh) ## For more information If you have any questions or comments about this advisory: - Open an issue in the [RestrictedPython issue tracker](https://github.com/zopefoundation/RestrictedPython/issues) - Email us at [security@plone.org](mailto:security@plone.org) ## Credits Thanks for analysing and reporting the go to: - Nakul Choudhary (Quasar0147 on GitHub) - despawningbone on GitHub - Robert Xiao (nneonneo on GitHub)

CVE-2023-37271 Affected RestrictedPython Versions

3 component(s)
Fixed Versions:
5.36.1
Product / Package Ecosystem Affected Versions Fixed Version
RestrictedPython
PyPI
< 5.3
5.3
RestrictedPython
PyPI
≥ 6.0a1.dev0
< 6.1
6.1
restrictedpython
PyPI
< 5.3
5.3

References

Frequently Asked Questions

What is CVE-2023-37271?
CVE-2023-37271 is a security vulnerability in RestrictedPython involving RestrictedPython vulnerable to arbitrary code execution via stack frame sandbox escape under CWE-913: Improper Control of Dynamically-Managed Code Resources.
Which RestrictedPython versions are affected?
Affected versions include releases prior to 5.3, 6.1 across affected packages: RestrictedPython, restrictedpython.
What version fixes CVE-2023-37271?
Upgrading to 5.3, 6.1 (or the latest patched release for your respective branch) resolves the vulnerability.
Is RestrictedPython affected?
Yes, RestrictedPython is affected in releases prior to 5.3, 6.1. Upgrade your dependency to 5.3, 6.1 or later.
Is restrictedpython affected?
Yes, restrictedpython is affected in releases prior to 5.3. Upgrade your dependency to 5.3 or later.
What is the CVSS score of CVE-2023-37271?
CVE-2023-37271 has a CVSS v3.1 base score of 8.4 (HIGH) with vector string CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:C/C:H/I:H/A:L.
What CWE is associated with CVE-2023-37271?
CVE-2023-37271 is classified under CWE-913: Improper Control of Dynamically-Managed Code Resources (The product does not properly restrict reading from or writing to dynamically-managed code resources such as variables, objects, classes, attributes, functions, or executable instructions or statements.).
How can I check PyPI dependencies for CVE-2023-37271?
Run the dependency tree command: 'pip show RestrictedPython'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-wqc8-x2pr-7jqh",
  "modified" : "2023-07-11T19:40:20Z",
  "published" : "2023-07-10T21:53:22Z",
  "aliases" : [ "CVE-2023-37271" ],
  "summary" : "RestrictedPython vulnerable to arbitrary code execution via stack frame sandbox escape",
  "details" : "### Impact\n\nRestrictedPython does not check access to stack frames and their attributes. Stack frames are accessible within at least generators and generator expressions, which are allowed inside RestrictedPython. An attacker with access to a RestrictedPython environment can write code that gets the current stack frame in a generator and then walk the stack all the way beyond the RestrictedPython invocation boundary, thus breaking out of the restricted scope allowing the call of unrestricted Python code and therefore potentially allowing arbitrary code execution in the Python interpreter.\n\nAll RestrictedPython deployments that allow untrusted users to write Python code in the RestrictedPython environment are at risk. In terms of Zope and Plone, this would mean deployments where the administrator allows untrusted users to create and/or edit objects of type `Script (Python)`, `DTML Method`, `DTML Document` or `Zope Page Template`. This is a non-default configuration and likely to be extremely rare.\n\n### Patches\n\nThe problem has been fixed in releases 5.3 and 6.1.\n\n### Workarounds\n\nThere is no workaround available. If you cannot upgrade to the latest release you should ensure the RestrictedPython environment is only available for trusted users.\n\n### References\n\n- [RestrictedPython security advisory GHSA-wqc8-x2pr-7jqh](https://github.com/zopefoundation/RestrictedPython/security/advisories/GHSA-wqc8-x2pr-7jqh)\n\n## For more information\n\nIf you have any questions or comments about this advisory:\n\n- Open an issue in the [RestrictedPython issue tracker](https://github.com/zopefoundation/RestrictedPython/issues)\n- Email us at [security@plone.org](mailto:security@plone.org)\n\n## Credits\n\nThanks for analysing and reporting the go to:\n- Nakul Choudhary (Quasar0147 on GitHub)\n- despawningbone on GitHub\n- Robert Xiao (nneonneo on GitHub)",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:C/C:H/I:H/A:L"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "RestrictedPython"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "5.3"
      } ]
    } ]
  }, {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "RestrictedPython"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "6.0a1.dev0"
      }, {
        "fixed" : "6.1"
      } ]
    } ]
  }, {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "restrictedpython"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "5.3"
      } ]
    } ]
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/zopefoundation/RestrictedPython/security/advisories/GHSA-wqc8-x2pr-7jqh"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2023-37271"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/zopefoundation/RestrictedPython/commit/c8eca66ae49081f0016d2e1f094c3d72095ef531"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/pypa/advisory-database/tree/main/vulns/restrictedpython/PYSEC-2023-118.yaml"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/zopefoundation/RestrictedPython"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-913" ],
    "severity" : "HIGH",
    "github_reviewed" : true,
    "github_reviewed_at" : "2023-07-10T21:53:22Z",
    "nvd_published_at" : "2023-07-11T18:15:20Z"
  }
}