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

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