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

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