x509: heap-buffer-overflow in X509_STORE_CTX_print_verify_cb on hostname mismatch with openssl cmp -verify_hostname
openssl cmp -verify_hostname (or -verify_email) with a name that does not match the enrolled certificate makes X509_STORE_CTX_print_verify_cb read one byte past a 10-byte heap buffer, because the stored host name is not NUL-terminated.
Files (the command below is run in the directory containing them):
key.pem
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIAICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC
-----END PRIVATE KEY-----
leaf.pem
-----BEGIN CERTIFICATE-----
MIIBPDCB76ADAgECAgECMAUGAytlcDAPMQ0wCwYDVQQDDARyb290MB4XDTIwMDEw
MTAwMDAwMFoXDTQ5MDEwMTAwMDAwMFowDzENMAsGA1UEAwwEbGVhZjAqMAUGAytl
cAMhAIE5dw6ofRdfVqNUZsNMfszLjYqRtO43ol32D1uPybOUo3AwbjAMBgNVHRMB
Af8EAjAAMAsGA1UdDwQEAwIHgDAfBgNVHSMEGDAWgBSa0Z4PFu73FMuQxvGV285m
6UWA+TAwBgNVHREEKTAnggxleGFtcGxlLnRlc3SBEXVzZXJAZXhhbXBsZS50ZXN0
hwTAAAIBMAUGAytlcANBAKdH4t2T0N2n1AqNIZ+Ey28weC+1DNuJgBKOXMkFpqny
MK4Eiodd7UqODXjYHhuQ1RVkKUXahI0jFHQYGw7/4wo=
-----END CERTIFICATE-----
root.pem
-----BEGIN CERTIFICATE-----
MIIBDjCBwaADAgECAgEBMAUGAytlcDAPMQ0wCwYDVQQDDARyb290MB4XDTIwMDEw
MTAwMDAwMFoXDTQ5MDEwMTAwMDAwMFowDzENMAsGA1UEAwwEcm9vdDAqMAUGAytl
cAMhAIqI4910CfGV/VLbLTy6XXLKZwm/HZQSG/N0iAG0D29co0IwQDAPBgNVHRMB
Af8EBTADAQH/MA4GA1UdDwEB/wQEAwIBBjAdBgNVHQ4EFgQUmtGeDxbu9xTLkMbx
ldvOZulFgPkwBQYDK2VwA0EAD0apEyZjp+XudbBtVnl1H3MKePQui/eZLyekqN91
YKJSTC4xZ+Hgy3pVzQG0W89U4O18Ko9r905gjIDS01pxDA==
-----END CERTIFICATE-----
Command (built with ASan):
openssl cmp -config "" -cmd ir -use_mock_srv -ref client -secret pass:shared -srv_ref server -srv_secret pass:shared -newkey key.pem -subject /CN=leaf -rsp_cert leaf.pem -out_trusted root.pem -attime 1893456000 -certout /tmp/enrolled.pem -verify_hostname wrong.test
Output:
==316==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x50200000625a at pc 0x7ffaac250f89 bp 0x7ffc3b065320 sp 0x7ffc3b064a98
READ of size 11 at 0x50200000625a thread T0
#0 0x7ffaac250f88 in printf_common ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors_format.inc:553
#1 0x7ffaac252bd5 in __interceptor_vsnprintf ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:1668
#2 0x55e315a82001 in BIO_vprintf crypto/bio/bio_print.c:59
#3 0x55e315a81e3f in BIO_printf crypto/bio/bio_print.c:25
#4 0x55e315f67d87 in X509_STORE_CTX_print_verify_cb crypto/x509/t_x509.c:478
...
0x50200000625a is located 0 bytes to the right of 10-byte region [0x502000006250,0x50200000625a)
allocated by thread T0 here:
#0 0x7ffaac2a7887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
#1 0x55e315d7996f in CRYPTO_malloc crypto/mem.c:233
#2 0x55e315d7e802 in CRYPTO_memdup crypto/o_str.c:62
#3 0x55e3160182c6 in buffer_from_bytes crypto/x509/x509_vpm.c:39
#4 0x55e3160186af in buffer_copy crypto/x509/x509_vpm.c:83
...
Replacing the last option with -verify_email [email protected] fails the same way (READ of size 19, t_x509.c:483).
I expected no ASan error: X509_VERIFY_PARAM_get0_host() returns a char * that is printed with %s.
Found at commit 6d76833d61fad07a171219d9cb1444bc37f88b8f, which is also the latest upstream commit I checked; both commands fail the same way there. The normal build shows no symptom: it prints certificate verification failed ... error = 62 (hostname mismatch) and Expected hostname(s) = wrong.test, and exits with rc 1 (the expected failure for a mismatching name).
Looks like buffer_copy() in crypto/x509/x509_vpm.c copies the host/email entries with buffer_from_bytes() (a memdup of exactly len bytes), which drops the NUL that buffer_from_string() adds. The copy happens in X509_VERIFY_PARAM_inherit(), reached from X509_STORE_set1_param() in load_trusted (apps/cmp.c:1108).
I can send a patch if that helps.
Found with an LLM-based testing tool; I used Claude to reduce it and look for the cause.
@jdymitarai works on this