Blame SOURCES/openssl-pkcs11-0.4.10-fix-memory-leak.patch

45dc05
From 66ebbaac74a1f6f1960ea1049eb8e75ebbdf9782 Mon Sep 17 00:00:00 2001
45dc05
From: Michal Trojnara <Michal.Trojnara@stunnel.org>
45dc05
Date: Fri, 28 Feb 2020 05:42:47 +0100
45dc05
Subject: [PATCH] Revert "fix use-after-free on PKCS11_pkey_meths."
45dc05
45dc05
This reverts commit e64496a198d4d2eb0310a22dc21be8b81367d319.
45dc05
45dc05
Upstream-Status: Backport [https://github.com/OpenSC/libp11/commit/66ebbaac74a1f6f1960ea1049eb8e75ebbdf9782]
45dc05
---
45dc05
 src/p11_pkey.c | 10 ++++++----
45dc05
 1 file changed, 6 insertions(+), 4 deletions(-)
45dc05
45dc05
diff --git a/src/p11_pkey.c b/src/p11_pkey.c
45dc05
index 8df45abd..4ed98f65 100644
45dc05
--- a/src/p11_pkey.c
45dc05
+++ b/src/p11_pkey.c
45dc05
@@ -673,8 +673,8 @@ int PKCS11_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
45dc05
 		EVP_PKEY_EC,
45dc05
 		0
45dc05
 	};
45dc05
-	EVP_PKEY_METHOD *pkey_method_rsa = NULL;
45dc05
-	EVP_PKEY_METHOD *pkey_method_ec = NULL;
45dc05
+	static EVP_PKEY_METHOD *pkey_method_rsa = NULL;
45dc05
+	static EVP_PKEY_METHOD *pkey_method_ec = NULL;
45dc05
 
45dc05
 	(void)e; /* squash the unused parameter warning */
45dc05
 	/* all PKCS#11 engines currently share the same pkey_meths */
45dc05
@@ -687,14 +687,16 @@ int PKCS11_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
45dc05
 	/* get the EVP_PKEY_METHOD */
45dc05
 	switch (nid) {
45dc05
 	case EVP_PKEY_RSA:
45dc05
-		pkey_method_rsa = pkcs11_pkey_method_rsa();
45dc05
+		if (!pkey_method_rsa)
45dc05
+			pkey_method_rsa = pkcs11_pkey_method_rsa();
45dc05
 		if (pkey_method_rsa == NULL)
45dc05
 			return 0;
45dc05
 		*pmeth = pkey_method_rsa;
45dc05
 		return 1; /* success */
45dc05
 #ifndef OPENSSL_NO_EC
45dc05
 	case EVP_PKEY_EC:
45dc05
-		pkey_method_ec = pkcs11_pkey_method_ec();
45dc05
+		if (!pkey_method_ec)
45dc05
+			pkey_method_ec = pkcs11_pkey_method_ec();
45dc05
 		if (pkey_method_ec == NULL)
45dc05
 			return 0;
45dc05
 		*pmeth = pkey_method_ec;
45dc05
45dc05
From 5aa56b4ac45655aab20bd49bb918e649875b0f4d Mon Sep 17 00:00:00 2001
45dc05
From: Michal Trojnara <Michal.Trojnara@stunnel.org>
45dc05
Date: Fri, 28 Feb 2020 07:09:42 +0100
45dc05
Subject: [PATCH] Disable EVP_PKEY_FLAG_DYNAMIC
45dc05
45dc05
Fixes #328
45dc05
45dc05
Upstream-Status: Backport [https://github.com/OpenSC/libp11/commit/5aa56b4ac45655aab20bd49bb918e649875b0f4d]
45dc05
---
45dc05
 src/p11_pkey.c | 14 +++++++++++++-
45dc05
 1 file changed, 13 insertions(+), 1 deletion(-)
45dc05
45dc05
diff --git a/src/p11_pkey.c b/src/p11_pkey.c
45dc05
index 4ed98f65..4e0956bf 100644
45dc05
--- a/src/p11_pkey.c
45dc05
+++ b/src/p11_pkey.c
45dc05
@@ -36,7 +36,6 @@ static int (*orig_pkey_ec_sign) (EVP_PKEY_CTX *ctx,
45dc05
 	const unsigned char *tbs, size_t tbslen);
45dc05
 #endif /* OPENSSL_NO_EC */
45dc05
 
45dc05
-#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
45dc05
 struct evp_pkey_method_st {
45dc05
 	int pkey_id;
45dc05
 	int flags;
45dc05
@@ -75,6 +74,9 @@ struct evp_pkey_method_st {
45dc05
 	int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
45dc05
 	int (*ctrl_str) (EVP_PKEY_CTX *ctx, const char *type, const char *value);
45dc05
 } /* EVP_PKEY_METHOD */ ;
45dc05
+
45dc05
+#if OPENSSL_VERSION_NUMBER >= 0x10000000L
45dc05
+#define EVP_PKEY_FLAG_DYNAMIC 1
45dc05
 #endif
45dc05
 
45dc05
 #if OPENSSL_VERSION_NUMBER < 0x10002000L || defined(LIBRESSL_VERSION_NUMBER)
45dc05
@@ -516,6 +518,11 @@ static EVP_PKEY_METHOD *pkcs11_pkey_method_rsa()
45dc05
 	new_meth = EVP_PKEY_meth_new(EVP_PKEY_RSA,
45dc05
 		EVP_PKEY_FLAG_AUTOARGLEN);
45dc05
 
45dc05
+#ifdef EVP_PKEY_FLAG_DYNAMIC
45dc05
+	/* do not allow OpenSSL to free this object */
45dc05
+	new_meth->flags &= ~EVP_PKEY_FLAG_DYNAMIC;
45dc05
+#endif
45dc05
+
45dc05
 	EVP_PKEY_meth_copy(new_meth, orig_meth);
45dc05
 
45dc05
 	EVP_PKEY_meth_set_sign(new_meth,
45dc05
@@ -655,6 +662,11 @@ static EVP_PKEY_METHOD *pkcs11_pkey_method_ec()
45dc05
 	new_meth = EVP_PKEY_meth_new(EVP_PKEY_EC,
45dc05
 		EVP_PKEY_FLAG_AUTOARGLEN);
45dc05
 
45dc05
+#ifdef EVP_PKEY_FLAG_DYNAMIC
45dc05
+	/* do not allow OpenSSL to free this object */
45dc05
+	new_meth->flags &= ~EVP_PKEY_FLAG_DYNAMIC;
45dc05
+#endif
45dc05
+
45dc05
 	EVP_PKEY_meth_copy(new_meth, orig_meth);
45dc05
 
45dc05
 	EVP_PKEY_meth_set_sign(new_meth,