Blame SOURCES/gnutls-3.6.16-pkcs7-verify.patch

5b821e
From e5dc27d1a457d1b3abc0582cd133910dff0fc309 Mon Sep 17 00:00:00 2001
5b821e
From: Zoltan Fridrich <zfridric@redhat.com>
5b821e
Date: Fri, 22 Jul 2022 12:00:11 +0200
5b821e
Subject: [PATCH] Fix double free during gnutls_pkcs7_verify
5b821e
5b821e
Signed-off-by: Zoltan Fridrich <zfridric@redhat.com>
5b821e
---
5b821e
 .gitignore                       |   1 +
5b821e
 lib/x509/pkcs7.c                 |   3 +-
5b821e
 tests/Makefile.am                |   3 +-
5b821e
 tests/pkcs7-verify-double-free.c | 215 +++++++++++++++++++++++++++++++
5b821e
 4 files changed, 220 insertions(+), 2 deletions(-)
5b821e
 create mode 100644 tests/pkcs7-verify-double-free.c
5b821e
5b821e
diff --git a/lib/x509/pkcs7.c b/lib/x509/pkcs7.c
5b821e
index 0ff55ba04b..878f867862 100644
5b821e
--- a/lib/x509/pkcs7.c
5b821e
+++ b/lib/x509/pkcs7.c
5b821e
@@ -1318,7 +1318,8 @@ gnutls_x509_crt_t find_signer(gnutls_pkcs7_t pkcs7, gnutls_x509_trust_list_t tl,
5b821e
 				issuer = find_verified_issuer_of(pkcs7, issuer, purpose, vflags);
5b821e
 
5b821e
 				if (issuer != NULL && gnutls_x509_crt_check_issuer(issuer, issuer)) {
5b821e
-					if (prev) gnutls_x509_crt_deinit(prev);
5b821e
+					if (prev && prev != signer)
5b821e
+						gnutls_x509_crt_deinit(prev);
5b821e
 					prev = issuer;
5b821e
 					break;
5b821e
 				}
5b821e
diff --git a/tests/Makefile.am b/tests/Makefile.am
5b821e
index b04cb081b4..0563d3c754 100644
5b821e
--- a/tests/Makefile.am
5b821e
+++ b/tests/Makefile.am
5b821e
@@ -220,7 +220,8 @@ ctests += mini-record-2 simple gnutls_hmac_fast set_pkcs12_cred cert certuniquei
5b821e
 	 sign-verify-newapi sign-verify-deterministic iov aead-cipher-vec \
5b821e
 	 tls13-without-timeout-func buffer status-request-revoked \
5b821e
 	 set_x509_ocsp_multi_cli kdf-api keylog-func \
5b821e
-	 dtls_hello_random_value tls_hello_random_value x509cert-dntypes
5b821e
+	 dtls_hello_random_value tls_hello_random_value x509cert-dntypes \
5b821e
+	 pkcs7-verify-double-free
5b821e
 
5b821e
 if HAVE_SECCOMP_TESTS
5b821e
 ctests += dtls-with-seccomp tls-with-seccomp dtls-client-with-seccomp tls-client-with-seccomp
5b821e
diff --git a/tests/pkcs7-verify-double-free.c b/tests/pkcs7-verify-double-free.c
5b821e
new file mode 100644
5b821e
index 0000000000..fadf307829
5b821e
--- /dev/null
5b821e
+++ b/tests/pkcs7-verify-double-free.c
5b821e
@@ -0,0 +1,215 @@
5b821e
+/*
5b821e
+ * Copyright (C) 2022 Red Hat, Inc.
5b821e
+ *
5b821e
+ * Author: Zoltan Fridrich
5b821e
+ *
5b821e
+ * This file is part of GnuTLS.
5b821e
+ *
5b821e
+ * GnuTLS is free software: you can redistribute it and/or modify it
5b821e
+ * under the terms of the GNU General Public License as published by
5b821e
+ * the Free Software Foundation, either version 3 of the License, or
5b821e
+ * (at your option) any later version.
5b821e
+ *
5b821e
+ * GnuTLS is distributed in the hope that it will be useful, but
5b821e
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
5b821e
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5b821e
+ * General Public License for more details.
5b821e
+ *
5b821e
+ * You should have received a copy of the GNU General Public License
5b821e
+ * along with GnuTLS. If not, see <https://www.gnu.org/licenses/>.
5b821e
+ */
5b821e
+
5b821e
+#ifdef HAVE_CONFIG_H
5b821e
+#include <config.h>
5b821e
+#endif
5b821e
+
5b821e
+#include <stdio.h>
5b821e
+#include <gnutls/pkcs7.h>
5b821e
+#include <gnutls/x509.h>
5b821e
+
5b821e
+#include "utils.h"
5b821e
+
5b821e
+static char rca_pem[] =
5b821e
+	"-----BEGIN CERTIFICATE-----\n"
5b821e
+	"MIIDCjCCAfKgAwIBAgIBATANBgk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3fBbUlfDfTJYGN3JwqX270KchXDElo8gNoDnF7jBMpLFFSEKm\n"
5b821e
+	"MyfbNLX/srh+CEfZaN/OZV4A3MQ0L8vQEp6M4CJhvRLIuMVabZ2coJ0AzystrOMU\n"
5b821e
+	"LirBWjg89RoAjFQ7bTE=\n"
5b821e
+	"-----END CERTIFICATE-----\n";
5b821e
+
5b821e
+static char ca_pem[] =
5b821e
+	"-----BEGIN CERTIFICATE-----\n"
5b821e
+	"MIIDFzCCAf+gAwIBAgIBAjANBgkqhkiG9w0BAQsFADAVMRMwEQYDVQQKDApFeGFt\n"
5b821e
+	"cGxlIENBMCAXDTE3MDcyMTE0NDQzNFoYDzIyMjIwNzIxMTQ0NDM0WjAiMSAwHgYD\n"
5b821e
+	"VQQKDBdFeGFtcGxlIGludGVybWVkaWF0ZSBDQTCCASIwDQYJKoZIhvcNAQEBBQAD\n"
5b821e
+	"ggEPADCCAQoCggEBAKb9ACB8u//sP6MfNU1OsVw68xz3eTPLgKxS0vpqexm6iGVg\n"
5b821e
+	"ug/o9uYRLzqiEukv/eyz9WzHmY7sqlOJjOFdv92+SaNg79Jc51WHPFXgea4/qyfr\n"
5b821e
+	"4y14PGs0SNxm6T44sXurUs7cXydQVUgnq2VCaWFOTUdxXoAWkV8r8GaUoPD/klVz\n"
5b821e
+	"RqxSZVETmX1XBKhsMnnov41kRwVph2C+VfUspsbaUZaz/o/S1/nokhXRACzKsMBr\n"
5b821e
+	"obqiGxbY35uVzsmbAW5ErhQz98AWJL3Bub1fsEMXg6OEMmPH4AtX888dTIYZNw0E\n"
5b821e
+	"bUIESspz1kjJQTtVQDHTprhwz16YiSVeUonlLgMCAwEAAaNjMGEwDwYDVR0TAQH/\n"
5b821e
+	"BAUwAwEB/zAOBgNVHQ8BAf8EBAMCAQYwHQYDVR0OBBYEFPBjxDWjMhjXERirKF9O\n"
5b821e
+	"o/5Cllc5MB8GA1UdIwQYMBaAFIY3julHOYpm4zpdiNpbVQNux7vWMA0GCSqGSIb3\n"
5b821e
+	"DQEBCwUAA4IBAQCTm+vv3hBa6lL5IT+Fw8aTxQ2Ne7mZ5oyazhvXYwwfKNMX3SML\n"
5b821e
+	"W2JdPaL64ZwbxxxYvW401o5Z0CEgru3YFrsqB/hEdl0Uf8UWWJmE1rRa+miTmbjt\n"
5b821e
+	"lrLNCWdrs6CiwvsPITTHg7jevB4KyZYsTSxQFcyr3N3xF+6EmOTC4IkhPPnXYXcp\n"
5b821e
+	"248ih+WOavSYoRvzgB/Dip1WnPYU2mfIV3O8JReRryngA0TzWCLPLUoWR3R4jwtC\n"
5b821e
+	"+1uSLoqaenz3qv3F1WEbke37az9YJuXx/5D8CqFQiZ62TUUtI6fYd8mkMBM4Qfh6\n"
5b821e
+	"NW9XrCkI9wlpL5K9HllhuW0BhKeJkuPpyQ2p\n"
5b821e
+	"-----END CERTIFICATE-----\n";
5b821e
+
5b821e
+static char ee_pem[] =
5b821e
+	"-----BEGIN CERTIFICATE-----\n"
5b821e
+	"MIIDIjCCAgqgAwIBAgIBATANBgkqhkiG9w0BAQsFADAiMSAwHgYDVQQKDBdFeGFt\n"
5b821e
+	"cGxlIGludGVybWVkaWF0ZSBDQTAgFw0yMjA3MjExNDQ1MzdaGA8yMjIyMDcyMTE0\n"
5b821e
+	"NDUzN1owFTETMBEGA1UEAwwKSm9obiBTbWl0aDCCASIwDQYJKoZIhvcNAQEBBQAD\n"
5b821e
+	"ggEPADCCAQoCggEBAMb1uuxppBFY+WVD45iyHUq7DkIJNNOI/JRaybVJfPktWq2E\n"
5b821e
+	"eNe7XhV05KKnqZTbDO2iYqNHqGhZ8pz/IstDRTZP3z/q1vXTG0P9Gx28rEy5TaUY\n"
5b821e
+	"QjtD+ZoFUQm0ORMDBjd8jikqtJ87hKeuOPMH4rzdydotMaPQSm7KLzHBGBr6gg7z\n"
5b821e
+	"g1IxPWkhMyHapoMqqrhjwjzoTY97UIXpZTEoIA+KpEC8f9CciBtL0i1MPBjWozB6\n"
5b821e
+	"Jma9q5iEwZXuRr3cnPYeIPlK2drgDZCMuSFcYiT8ApLw5OhKqY1m2EvfZ2ox2s9R\n"
5b821e
+	"68/HzYdPi3kZwiNEtlBvMlpt5yKBJAflp76d7DkCAwEAAaNuMGwwCwYDVR0PBAQD\n"
5b821e
+	"AgWgMB0GA1UdJQQWMBQGCCsGAQUFBwMCBggrBgEFBQcDBDAdBgNVHQ4EFgQUc+Mi\n"
5b821e
+	"kr8WMCk00SQo+P2iggp/oQkwHwYDVR0jBBgwFoAU8GPENaMyGNcRGKsoX06j/kKW\n"
5b821e
+	"VzkwDQYJKoZIhvcNAQELBQADggEBAKU9+CUR0Jcfybd1+8Aqgh1RH96yQygnVuyt\n"
5b821e
+	"Na9rFz4fM3ij9tGXDHXrkZw8bW1dWLU9quu8zeTxKxc3aiDIw739Alz0tukttDo7\n"
5b821e
+	"dW7YqIb77zsIsWB9p7G9dlxT6ieUy+5IKk69BbeK8KR0vAciAG4KVQxPhuPy/LGX\n"
5b821e
+	"PzqlJIJ4h61s3UOroReHPB1keLZgpORqrvtpClOmABH9TLFRJA/WFg8Q2XYB/p0x\n"
5b821e
+	"l/pWiaoBC+8wK9cDoMUK5yOwXeuCLffCb+UlAD0+z/qxJ2pisE8E9X8rRKRrWI+i\n"
5b821e
+	"G7LtJCEn86EQK8KuRlJxKgj8lClZhoULB0oL4jbblBuNow9WRmM=\n"
5b821e
+	"-----END CERTIFICATE-----\n";
5b821e
+
5b821e
+static char msg_pem[] =
5b821e
+	"-----BEGIN PKCS7-----\n"
5b821e
+	"MIIK2QYJKoZIhvcNAQcCoIIKyjCCCsYCAQExDTALBglghkgBZQMEAgEwCwYJKoZI\n"
5b821e
+	"hvcNAQcBoIIJTzCCAwowggHyoAMCAQICAQEwDQYJKoZIhvcNAQELBQAwFTETMBEG\n"
5b821e
+	"A1UECgwKRXhhbXBsZSBDQTAgFw0xNzA3MjExNDQzMjFaGA8yMjIyMDcyMTE0NDMy\n"
5b821e
+	"MVowFTETMBEGA1UECgwKRXhh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ELBQADggEBAAWe63DcNwmleQ3INFGDJZ/m2I/R/cBa\n"
5b821e
+	"nnrxgR5Ey1ljHdA/x1z1JLTGmGVwqGExs5DNG9Q//Pmc9pZ1yPa8J4Xf8AvFcmkY\n"
5b821e
+	"mWoH1HvW0xu/RF1UN5SAoD2PRQ+Vq4OSPD58IlEu/u4o1wZV7Wl91Cv6VNpiAb63\n"
5b821e
+	"j9PA1YacOpOtcRqG59Vuj9HFm9f30ejHVo2+KJcpo290cR3Zg4fOm8mtjeMdt/QS\n"
5b821e
+	"Atq+RqPAQ7yxqvEEv8zPIZj2kAOQm3mh/yYqBrR68lQUD/dBTP7ApIZkhUK3XK6U\n"
5b821e
+	"nf9JvoF6Fn2+Cnqb//FLBgHSnoeqeQNwDLUXTsD02iYxHzJrhokSY4YxggFQMIIB\n"
5b821e
+	"TAIBATAnMCIxIDAeBgNVBAoMF0V4YW1wbGUgaW50ZXJtZWRpYXRlIENBAgEBMAsG\n"
5b821e
+	"CWCGSAFlAwQCATANBgkqhkiG9w0BAQEFAASCAQATHg6wNsBcs/Ub1GQfKwTpKCk5\n"
5b821e
+	"8QXuNnZ0u7b6mKgrSY2Gf47fpL2aRgaR+BAQncbctu5EH/IL38pWjaGtOhFAj/5q\n"
5b821e
+	"7luVQW11kuyJN3Bd/dtLqawWOwMmAIEigw6X50l5ZHnEVzFfxt+RKTNhk4XWVtbi\n"
5b821e
+	"2iIlITOplW0rnvxYAwCxKL9ocaB7etK8au7ixMxbFp75Ts4iLX8dhlAFdCuFCk8k\n"
5b821e
+	"B8mi9HHuwr3QYRqMPW61hu1wBL3yB8eoZNOwPXb0gkIh6ZvgptxgQzm/cc+Iw9fP\n"
5b821e
+	"QkR0fTM7ElJ5QZmSV98AUbZDHmDvpmcjcUxfSPMc3IoT8T300usRu7QHqKJi\n"
5b821e
+	"-----END PKCS7-----\n";
5b821e
+
5b821e
+const gnutls_datum_t rca_datum = { (void *)rca_pem, sizeof(rca_pem) - 1 };
5b821e
+const gnutls_datum_t ca_datum = { (void *)ca_pem, sizeof(ca_pem) - 1 };
5b821e
+const gnutls_datum_t ee_datum = { (void *)ee_pem, sizeof(ee_pem) - 1 };
5b821e
+const gnutls_datum_t msg_datum = { (void *)msg_pem, sizeof(msg_pem) - 1 };
5b821e
+
5b821e
+static void tls_log_func(int level, const char *str)
5b821e
+{
5b821e
+	fprintf(stderr, "%s |<%d>| %s", "err", level, str);
5b821e
+}
5b821e
+
5b821e
+#define CHECK(X)\
5b821e
+{\
5b821e
+	r = X;\
5b821e
+	if (r < 0)\
5b821e
+		fail("error in %d: %s\n", __LINE__, gnutls_strerror(r));\
5b821e
+}\
5b821e
+
5b821e
+void doit(void)
5b821e
+{
5b821e
+	int r;
5b821e
+	gnutls_x509_crt_t rca_cert = NULL;
5b821e
+	gnutls_x509_crt_t ca_cert = NULL;
5b821e
+	gnutls_x509_crt_t ee_cert = NULL;
5b821e
+	gnutls_x509_trust_list_t tlist = NULL;
5b821e
+	gnutls_pkcs7_t pkcs7 = NULL;
5b821e
+	gnutls_datum_t data = { (unsigned char *)"xxx", 3 };
5b821e
+
5b821e
+	if (debug) {
5b821e
+		gnutls_global_set_log_function(tls_log_func);
5b821e
+		gnutls_global_set_log_level(4711);
5b821e
+	}
5b821e
+
5b821e
+	// Import certificates
5b821e
+	CHECK(gnutls_x509_crt_init(&rca_cert));
5b821e
+	CHECK(gnutls_x509_crt_import(rca_cert, &rca_datum, GNUTLS_X509_FMT_PEM));
5b821e
+	CHECK(gnutls_x509_crt_init(&ca_cert));
5b821e
+	CHECK(gnutls_x509_crt_import(ca_cert, &ca_datum, GNUTLS_X509_FMT_PEM));
5b821e
+	CHECK(gnutls_x509_crt_init(&ee_cert));
5b821e
+	CHECK(gnutls_x509_crt_import(ee_cert, &ee_datum, GNUTLS_X509_FMT_PEM));
5b821e
+
5b821e
+	// Setup trust store
5b821e
+	CHECK(gnutls_x509_trust_list_init(&tlist, 0));
5b821e
+	CHECK(gnutls_x509_trust_list_add_named_crt(tlist, rca_cert, "rca", 3, 0));
5b821e
+	CHECK(gnutls_x509_trust_list_add_named_crt(tlist, ca_cert, "ca", 2, 0));
5b821e
+	CHECK(gnutls_x509_trust_list_add_named_crt(tlist, ee_cert, "ee", 2, 0));
5b821e
+
5b821e
+	// Setup pkcs7 structure
5b821e
+	CHECK(gnutls_pkcs7_init(&pkcs7));
5b821e
+	CHECK(gnutls_pkcs7_import(pkcs7, &msg_datum, GNUTLS_X509_FMT_PEM));
5b821e
+
5b821e
+	// Signature verification
5b821e
+	gnutls_pkcs7_verify(pkcs7, tlist, NULL, 0, 0, &data, 0);
5b821e
+
5b821e
+	gnutls_x509_crt_deinit(rca_cert);
5b821e
+	gnutls_x509_crt_deinit(ca_cert);
5b821e
+	gnutls_x509_crt_deinit(ee_cert);
5b821e
+	gnutls_x509_trust_list_deinit(tlist, 0);
5b821e
+	gnutls_pkcs7_deinit(pkcs7);
5b821e
+}
5b821e
-- 
5b821e
2.37.2
5b821e