Function `do_pubkey()` can have out-of-bound read issue
A one-byte out-of-bounds heap read occurs in the do_pubkey() function when parsing an RSA public key with a zero-length or all-zero modulus. An attacker could trigger this by supplying a crafted certificate chain, potentially causing a crash.
- Disclosed after 5 days
Summary
A 1-byte out-of-bounds heap read in do_pubkey() in lib/vtls/x509asn1.c. When parsing an RSA public key with a zero-length or all-zero modulus, the loop dereferences a pointer before checking bounds. Requires a non-OpenSSL TLS backend (e.g., Mbed/Gnu). A certificate chain verification can trigger this by sending crafted certificates.
Details
Trigger path:
- Application uses a non-OpenSSL TLS backend (mbedTLS, GnuTLS, Rustls, Schannel)
- A certificate containing an RSA public key with a zero-length modulus: BIT STRING content 00 30 02 02 00
PoC
Minimal C reproduction (with ASan):
// Compile curl's x509asn1.c with -fsanitize=address -O0
// Feed a BIT STRING with content: 00 30 02 02 00
// 00 = unused bits
// 30 02 = SEQUENCE, length 2
// 02 00 = INTEGER, length 0 (zero-length modulus)
uint8_t bitstring[] = {0x00, 0x30, 0x02, 0x02, 0x00};
uint8_t *buf = malloc(sizeof(bitstring)); // exact size, ASan red zone after
memcpy(buf, bitstring, sizeof(bitstring));
struct Curl_asn1Element pubkey = {0};
pubkey.beg = (const char *)buf;
pubkey.end = (const char *)buf + sizeof(bitstring);
pubkey.tag = 0x03;
struct Curl_asn1Element param = {0};
// Calls do_pubkey() -> reads 1 byte past buf -> ASan heap-buffer-overflow
do_pubkey(NULL, 0, "rsaEncryption", ¶m, &pubkey);
ASAN output
==PID==ERROR: AddressSanitizer: heap-buffer-overflow
READ of size 1 at 0x...
#0 ... in do_pubkey lib/vtls/x509asn1.c:1015
Possible fix
for(q = elem.beg; !*q && q < elem.end; q++) ==> for(q = elem.beg; q < elem.end && !*q; q++)
Impact
Summary:
Out-of-bounds read (CWE-125): 1 byte past the parsed ASN.1 element DoS: Crash possible if the overread hits unmapped memory (unlikely with typical allocators)
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the loop condition checks the byte value before verifying the pointer is within bounds, causing a read past the end of the buffer.
Impact
The out-of-bounds read could cause a denial of service (crash) if the overread hits unmapped memory, though this is unlikely with typical allocators.
What the attacker needed
Non-OpenSSL TLS backend; crafted certificate with zero-length modulus
The fix
The loop condition should be reordered to check the pointer against the end before dereferencing it.
How it was found
- Use a non-OpenSSL TLS backend such as mbedTLS, GnuTLS, Rustls, or Schannel.
- Provide a certificate containing an RSA public key with a zero-length modulus (BIT STRING content 00 30 02 02 00).
- Trigger certificate chain verification, which calls do_pubkey() and causes the out-of-bounds read.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When a program reads data from a certificate, it should check that it does not read past the end of the data. Here, a loop checks the data value before checking the boundary, so it can read one byte beyond the allocated memory. This is a small memory safety bug that could crash the program.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.