Blame SOURCES/fix-crypto-memory-leaks.patch

f6c48d
diff --git a/src/crypto/internal/boring/goopenssl.h b/src/crypto/internal/boring/goopenssl.h
f6c48d
index 3585458..ae1607b 100644
f6c48d
--- a/src/crypto/internal/boring/goopenssl.h
f6c48d
+++ b/src/crypto/internal/boring/goopenssl.h
f6c48d
@@ -667,6 +667,7 @@ typedef EVP_PKEY GO_EVP_PKEY;
f6c48d
 DEFINEFUNC(GO_EVP_PKEY *, EVP_PKEY_new, (void), ())
f6c48d
 DEFINEFUNC(void, EVP_PKEY_free, (GO_EVP_PKEY * arg0), (arg0))
f6c48d
 DEFINEFUNC(int, EVP_PKEY_set1_RSA, (GO_EVP_PKEY * arg0, GO_RSA *arg1), (arg0, arg1))
f6c48d
+DEFINEFUNC(int, EVP_PKEY_set1_EC_KEY, (GO_EVP_PKEY * arg0, GO_EC_KEY *arg1), (arg0, arg1))
f6c48d
 DEFINEFUNC(int, EVP_PKEY_verify,
f6c48d
 	(EVP_PKEY_CTX *ctx, const unsigned char *sig, unsigned int siglen, const unsigned char *tbs, size_t tbslen),
f6c48d
 	(ctx, sig, siglen, tbs, tbslen))
f6c48d
diff --git a/src/crypto/internal/boring/openssl_ecdsa_signature.c b/src/crypto/internal/boring/openssl_ecdsa_signature.c
f6c48d
index 4c14cc9..daa1252 100644
f6c48d
--- a/src/crypto/internal/boring/openssl_ecdsa_signature.c
f6c48d
+++ b/src/crypto/internal/boring/openssl_ecdsa_signature.c
f6c48d
@@ -9,19 +9,32 @@
f6c48d
 int
f6c48d
 _goboringcrypto_ECDSA_sign(EVP_MD* md, const uint8_t *msg, size_t msgLen, uint8_t *sig, unsigned int *slen, GO_EC_KEY *eckey)
f6c48d
 {
f6c48d
+    int result;
f6c48d
     EVP_PKEY *key = _goboringcrypto_EVP_PKEY_new();
f6c48d
-    if (!_goboringcrypto_EVP_PKEY_assign_EC_KEY(key, eckey))
f6c48d
-        return 0;
f6c48d
-    return _goboringcrypto_EVP_sign(md, NULL, msg, msgLen, sig, slen, key);
f6c48d
+    if (!_goboringcrypto_EVP_PKEY_set1_EC_KEY(key, eckey)) {
f6c48d
+        result = 0;
f6c48d
+        goto err;
f6c48d
+    }
f6c48d
+    result = _goboringcrypto_EVP_sign(md, NULL, msg, msgLen, sig, slen, key);
f6c48d
+err:
f6c48d
+    _goboringcrypto_EVP_PKEY_free(key);
f6c48d
+    return result;
f6c48d
 }
f6c48d
 
f6c48d
 int
f6c48d
 _goboringcrypto_ECDSA_verify(EVP_MD* md, const uint8_t *msg, size_t msgLen, const uint8_t *sig, unsigned int slen, GO_EC_KEY *eckey)
f6c48d
 {
f6c48d
 
f6c48d
+    int result;
f6c48d
     EVP_PKEY *key = _goboringcrypto_EVP_PKEY_new();
f6c48d
-    if (!_goboringcrypto_EVP_PKEY_assign_EC_KEY(key, eckey))
f6c48d
-        return 0;
f6c48d
+    if (!_goboringcrypto_EVP_PKEY_set1_EC_KEY(key, eckey)) {
f6c48d
+        result = 0;
f6c48d
+        goto err;
f6c48d
+    }
f6c48d
 
f6c48d
-    return _goboringcrypto_EVP_verify(md, NULL, msg, msgLen, sig, slen, key);
f6c48d
+    result = _goboringcrypto_EVP_verify(md, NULL, msg, msgLen, sig, slen, key);
f6c48d
+
f6c48d
+err:
f6c48d
+    _goboringcrypto_EVP_PKEY_free(key);
f6c48d
+    return result;
f6c48d
 }
f6c48d
diff --git a/src/crypto/internal/boring/openssl_port_rsa.c b/src/crypto/internal/boring/openssl_port_rsa.c
f6c48d
index a8d047d..2e56499 100644
f6c48d
--- a/src/crypto/internal/boring/openssl_port_rsa.c
f6c48d
+++ b/src/crypto/internal/boring/openssl_port_rsa.c
f6c48d
@@ -25,14 +25,13 @@ int _goboringcrypto_RSA_digest_and_sign_pss_mgf1(GO_RSA *rsa, unsigned int *out_
f6c48d
 	EVP_PKEY_CTX *ctx;
f6c48d
 	unsigned int siglen;
f6c48d
 
f6c48d
+	int ret = 0;
f6c48d
 	EVP_PKEY *key = _goboringcrypto_EVP_PKEY_new();
f6c48d
-	if (!_goboringcrypto_EVP_PKEY_assign_RSA(key, rsa))
f6c48d
-		return 0;
f6c48d
+	if (!_goboringcrypto_EVP_PKEY_set1_RSA(key, rsa))
f6c48d
+		goto err;
f6c48d
 	ctx = _goboringcrypto_EVP_PKEY_CTX_new(key, NULL /* no engine */);
f6c48d
 	if (!ctx)
f6c48d
-		return 0;
f6c48d
-
f6c48d
-	int ret = 0;
f6c48d
+		goto err;
f6c48d
 
f6c48d
 	EVP_MD_CTX *mdctx = NULL;
f6c48d
 	if (!(mdctx = _goboringcrypto_EVP_MD_CTX_create()))
f6c48d
@@ -67,6 +66,10 @@ int _goboringcrypto_RSA_digest_and_sign_pss_mgf1(GO_RSA *rsa, unsigned int *out_
f6c48d
 err:
f6c48d
 	if (mdctx)
f6c48d
 		_goboringcrypto_EVP_MD_CTX_free(mdctx);
f6c48d
+	if (ctx)
f6c48d
+		_goboringcrypto_EVP_PKEY_CTX_free(ctx);
f6c48d
+	if (key)
f6c48d
+		_goboringcrypto_EVP_PKEY_free(key);
f6c48d
 
f6c48d
 	return ret;
f6c48d
 }
f6c48d
@@ -78,18 +81,17 @@ int _goboringcrypto_RSA_sign_pss_mgf1(GO_RSA *rsa, unsigned int *out_len, uint8_
f6c48d
 	EVP_PKEY *pkey;
f6c48d
 	size_t siglen;
f6c48d
 
f6c48d
+	int ret = 0;
f6c48d
 	pkey = _goboringcrypto_EVP_PKEY_new();
f6c48d
 	if (!pkey)
f6c48d
-		return 0;
f6c48d
+		goto err;
f6c48d
 
f6c48d
 	if (_goboringcrypto_EVP_PKEY_set1_RSA(pkey, rsa) <= 0)
f6c48d
-		return 0;
f6c48d
-	
f6c48d
+		goto err;
f6c48d
+
f6c48d
 	ctx = _goboringcrypto_EVP_PKEY_CTX_new(pkey, NULL /* no engine */);
f6c48d
 	if (!ctx)
f6c48d
-		return 0;
f6c48d
-
f6c48d
-	int ret = 0;
f6c48d
+		goto err;
f6c48d
 
f6c48d
 	if (_goboringcrypto_EVP_PKEY_sign_init(ctx) <= 0)
f6c48d
 		goto err;
f6c48d
@@ -101,7 +103,7 @@ int _goboringcrypto_RSA_sign_pss_mgf1(GO_RSA *rsa, unsigned int *out_len, uint8_
f6c48d
 		goto err;
f6c48d
 	if (_goboringcrypto_EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, mgf1_md) <= 0)
f6c48d
 		goto err;
f6c48d
-	
f6c48d
+
f6c48d
 	/* Determine buffer length */
f6c48d
 	if (_goboringcrypto_EVP_PKEY_sign(ctx, NULL, &siglen, in, in_len) <= 0)
f6c48d
 		goto err;
f6c48d
@@ -116,7 +118,10 @@ int _goboringcrypto_RSA_sign_pss_mgf1(GO_RSA *rsa, unsigned int *out_len, uint8_
f6c48d
 	ret = 1;
f6c48d
 
f6c48d
 err:
f6c48d
-	_goboringcrypto_EVP_PKEY_CTX_free(ctx);
f6c48d
+	if (ctx)
f6c48d
+		_goboringcrypto_EVP_PKEY_CTX_free(ctx);
f6c48d
+	if (pkey)
f6c48d
+		_goboringcrypto_EVP_PKEY_free(pkey);
f6c48d
 
f6c48d
 	return ret;
f6c48d
 }
f6c48d
@@ -130,14 +135,14 @@ int _goboringcrypto_RSA_verify_pss_mgf1(RSA *rsa, const uint8_t *msg, unsigned i
f6c48d
 
f6c48d
 	pkey = _goboringcrypto_EVP_PKEY_new();
f6c48d
 	if (!pkey)
f6c48d
-		return 0;
f6c48d
+		goto err;
f6c48d
 
f6c48d
 	if (_goboringcrypto_EVP_PKEY_set1_RSA(pkey, rsa) <= 0)
f6c48d
-		return 0;
f6c48d
-	
f6c48d
+		goto err;
f6c48d
+
f6c48d
 	ctx = _goboringcrypto_EVP_PKEY_CTX_new(pkey, NULL /* no engine */);
f6c48d
 	if (!ctx)
f6c48d
-		return 0;
f6c48d
+		goto err;
f6c48d
 
f6c48d
 	if (_goboringcrypto_EVP_PKEY_verify_init(ctx) <= 0)
f6c48d
 		goto err;
f6c48d
@@ -155,25 +160,40 @@ int _goboringcrypto_RSA_verify_pss_mgf1(RSA *rsa, const uint8_t *msg, unsigned i
f6c48d
 	ret = 1;
f6c48d
 
f6c48d
 err:
f6c48d
-	_goboringcrypto_EVP_PKEY_CTX_free(ctx);
f6c48d
+	if (ctx)
f6c48d
+		_goboringcrypto_EVP_PKEY_CTX_free(ctx);
f6c48d
+	if (pkey)
f6c48d
+		_goboringcrypto_EVP_PKEY_free(pkey);
f6c48d
+
f6c48d
 
f6c48d
 	return ret;
f6c48d
 }
f6c48d
 
f6c48d
 int _goboringcrypto_EVP_RSA_sign(EVP_MD *md, const uint8_t *msg, unsigned int msgLen, uint8_t *sig, unsigned int *slen, RSA *rsa)
f6c48d
 {
f6c48d
+	int result;
f6c48d
 	EVP_PKEY *key = _goboringcrypto_EVP_PKEY_new();
f6c48d
-	if (!_goboringcrypto_EVP_PKEY_assign_RSA(key, rsa))
f6c48d
-		return 0;
f6c48d
-	return _goboringcrypto_EVP_sign(md, NULL, msg, msgLen, sig, slen, key);
f6c48d
+	if (!_goboringcrypto_EVP_PKEY_set1_RSA(key, rsa)) {
f6c48d
+		result = 0;
f6c48d
+		goto err;
f6c48d
+	}
f6c48d
+	result = _goboringcrypto_EVP_sign(md, NULL, msg, msgLen, sig, slen, key);
f6c48d
+err:
f6c48d
+	_goboringcrypto_EVP_PKEY_free(key);
f6c48d
+	return result;
f6c48d
 }
f6c48d
 
f6c48d
 int _goboringcrypto_EVP_RSA_verify(EVP_MD *md, const uint8_t *msg, unsigned int msgLen, const uint8_t *sig, unsigned int slen, GO_RSA *rsa)
f6c48d
 {
f6c48d
+	int result;
f6c48d
 	EVP_PKEY *key = _goboringcrypto_EVP_PKEY_new();
f6c48d
-	if (!_goboringcrypto_EVP_PKEY_assign_RSA(key, rsa))
f6c48d
-	{
f6c48d
-		return 0;
f6c48d
+	if (!_goboringcrypto_EVP_PKEY_set1_RSA(key, rsa)) {
f6c48d
+		result = 0;
f6c48d
+		goto err;
f6c48d
 	}
f6c48d
-	 return _goboringcrypto_EVP_verify(md, NULL, msg, msgLen, sig, slen, key);
f6c48d
+	result =  _goboringcrypto_EVP_verify(md, NULL, msg, msgLen, sig, slen, key);
f6c48d
+err:
f6c48d
+	_goboringcrypto_EVP_PKEY_free(key);
f6c48d
+	return result;
f6c48d
+
f6c48d
 }
f6c48d
diff --git a/src/crypto/internal/boring/rsa.go b/src/crypto/internal/boring/rsa.go
f6c48d
index 2eefc27..698c08e 100644
f6c48d
--- a/src/crypto/internal/boring/rsa.go
f6c48d
+++ b/src/crypto/internal/boring/rsa.go
f6c48d
@@ -162,12 +162,23 @@ func setupRSA(withKey func(func(*C.GO_RSA) C.int) C.int,
f6c48d
 			return nil, nil, NewOpenSSLError("EVP_PKEY_set_rsa_oaep_md failed")
f6c48d
 		}
f6c48d
 		// ctx takes ownership of label, so malloc a copy for BoringCrypto to free.
f6c48d
-		clabel := (*C.uint8_t)(C.malloc(C.size_t(len(label))))
f6c48d
-		if clabel == nil {
f6c48d
-			return nil, nil, fail("OPENSSL_malloc")
f6c48d
+		var clabel *C.uint8_t
f6c48d
+		clabel = nil
f6c48d
+		// OpenSSL 1.1.1 does not take ownership of the label if the length is zero.
f6c48d
+		// Depending on the malloc implementation, if clabel is allocated with malloc(0),
f6c48d
+		// metadata for the size-zero allocation is never cleaned up, which is a memory leak.
f6c48d
+		// As such, we must only allocate clabel if the label is of non zero length.
f6c48d
+		if len(label) > 0 {
f6c48d
+			clabel = (*C.uint8_t)(C.malloc(C.size_t(len(label))))
f6c48d
+			if clabel == nil {
f6c48d
+				return nil, nil, fail("OPENSSL_malloc")
f6c48d
+			}
f6c48d
+			copy((*[1 << 30]byte)(unsafe.Pointer(clabel))[:len(label)], label)
f6c48d
 		}
f6c48d
-		copy((*[1 << 30]byte)(unsafe.Pointer(clabel))[:len(label)], label)
f6c48d
-		if C._goboringcrypto_EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, clabel, C.int(len(label))) == 0 {
f6c48d
+		if C._goboringcrypto_EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, clabel, C.int(len(label))) != 1 {
f6c48d
+			if clabel != nil {
f6c48d
+				C.free(unsafe.Pointer(clabel))
f6c48d
+			}
f6c48d
 			return nil, nil, NewOpenSSLError("EVP_PKEY_CTX_set0_rsa_oaep_label failed")
f6c48d
 		}
f6c48d
 	}