From 19f1655fb5ee2283df0bebed54b9c2d208f5f555 Mon Sep 17 00:00:00 2001 From: tb Date: Tue, 9 Jun 2026 12:34:08 +0000 Subject: [PATCH] Avoid freeing a caller-owned buffer in PKCS7_verify() If a PKCS#7 S/MIME message comes with an empty set of digestAlgorithms in the SignedData, PKCS7_verify() would incorrectly free a caller-owned buffer. Fix the freeing logic to avoid this situation. From Igor Ustinov via OpenSSL --- lib/libcrypto/pkcs7/pk7_smime.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/libcrypto/pkcs7/pk7_smime.c b/lib/libcrypto/pkcs7/pk7_smime.c index 9baff7f5252..3806d170824 100644 --- a/lib/libcrypto/pkcs7/pk7_smime.c +++ b/lib/libcrypto/pkcs7/pk7_smime.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pk7_smime.c,v 1.29 2025/12/20 07:22:43 tb Exp $ */ +/* $OpenBSD: pk7_smime.c,v 1.30 2026/06/09 12:34:08 tb Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project. */ @@ -259,7 +259,7 @@ PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, BIO *indata, char buf[4096]; int i, j = 0, k, ret = 0; BIO *p7bio; - BIO *tmpin, *tmpout; + BIO *next, *tmpin, *tmpout; if (!p7) { PKCS7error(PKCS7_R_INVALID_NULL_POINTER); @@ -409,12 +409,12 @@ PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, BIO *indata, ret = 1; -err: - if (tmpin == indata) { - if (indata) - BIO_pop(p7bio); + err: + while (p7bio != NULL && p7bio != indata) { + next = BIO_pop(p7bio); + BIO_free(p7bio); + p7bio = next; } - BIO_free_all(p7bio); sk_X509_free(signers); return ret;