Skip to contentSkip to conversation

Cleanup ressource leaks in test_aead_speed()

PATCH In review Christian Engelmayer 4 participants 13 messages Apr 21, 2014 – Apr 28, 2014 Crypto API

lore
Share & tools
lore
#0 lore raw

cover Cleanup ressource leaks in test_aead_speed()

This is a cleanup of Coverity ressource leak findings for the quick & dirty crypto testing module crypto/tcrypt.c.

All 3 changesets address function test_aead_speed() that was introduced in 53f52d7a (crypto: tcrypt - Added speed tests for AEAD crypto alogrithms in tcrypt test suite)

The series applies against v3.15-rc2 as well as branch master in tree git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6.git and is compile tested only.

Christian Engelmayer (3):
crypto: Fix potential leak in test_aead_speed() if aad_size is too big
crypto: Fix potential leak in test_aead_speed() if crypto_alloc_aead() fails
crypto: Fix leak of struct aead_request in test_aead_speed()

 crypto/tcrypt.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/3 crypto: Fix potential leak in test_aead_speed() if aad_size is too big

Fix a potential memory leak in the error handling of test_aead_speed(). In case the size check on the associate data length parameter fails, the function goes through the wrong exit label. Reported by Coverity - CID 1163870.

---
 crypto/tcrypt.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
crypto/tcrypt.c +6 -8
index 870be7b..1856d7f 100644
@@ -282,6 +282,11 @@ static void test_aead_speed(const char *algo, int enc, unsigned int sec,
 	unsigned int *b_size;
 	unsigned int iv_len;
 
+	if (aad_size >= PAGE_SIZE) {
+		pr_err("associate data length (%u) too big\n", aad_size);
+		return;
+	}
+
 	if (enc == ENCRYPT)
 		e = "encryption";
 	else
@@ -323,14 +328,7 @@ static void test_aead_speed(const char *algo, int enc, unsigned int sec,
 		b_size = aead_sizes;
 		do {
 			assoc = axbuf[0];
-
-			if (aad_size < PAGE_SIZE)
-				memset(assoc, 0xff, aad_size);
-			else {
-				pr_err("associate data length (%u) too big\n",
-					aad_size);
-				goto out_nosg;
-			}
+			memset(assoc, 0xff, aad_size);
 			sg_init_one(&asg[0], assoc, aad_size);
 
 			if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH 1/3] crypto: Fix potential leak in test_aead_speed() if aad_size is too big

Quoted 15 lines · Christian Engelmayer

Fix a potential memory leak in the error handling of test_aead_speed(). In case the size check on the associate data length parameter fails, the function goes through the wrong exit label. Reported by Coverity - CID 1163870.

---
 crypto/tcrypt.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
crypto/tcrypt.c +0 -0
index 870be7b..1856d7f 100644

crypto/tcrypt.c static void test_aead_speed(const char *algo, int enc, · line 285

Quoted 4 lines
crypto/tcrypt.c +1 -0
@@ -282,6 +282,11 @@ static void test_aead_speed(const char *algo, int enc,
 unsigned int sec, unsigned int *b_size;
 	unsigned int iv_len;
 
+	if (aad_size >= PAGE_SIZE) {

On an unrelated note ... Won't if (aad_size > PAGE_SIZE) be sufficient here?

Cheers!

#3 lore raw

Re: [PATCH 1/3] crypto: Fix potential leak in test_aead_speed() if aad_size is too big

Quoted 4 lines · Marek Vasut

crypto/tcrypt.c static void test_aead_speed(const char *algo, int enc, unsigned int sec, · line 285

Quoted 1 line · Christian Engelmayer
crypto/tcrypt.c +1 -0
+	if (aad_size >= PAGE_SIZE) {

On an unrelated note ... Won't if (aad_size > PAGE_SIZE) be sufficient here?

From what I have seen how the buffers are allocated via __get_free_page() I thought so too. However, as it previously read

	if (aad_size < PAGE_SIZE)
		memset(assoc, 0xff, aad_size);
	else {

my intention was simply to make the modification so that the bug is addressed without introducing an additional change.

Regards,
Christian

#4 lore raw

Re: [PATCH 1/3] crypto: Fix potential leak in test_aead_speed() if aad_size is too big

Quoted 10 lines · Christian Engelmayer
Quoted 5 lines · Marek Vasut

crypto/tcrypt.c static void test_aead_speed(const char *algo, int enc, unsigned int sec, · line 285

Quoted 1 line · Christian Engelmayer
crypto/tcrypt.c +1 -0
+	if (aad_size >= PAGE_SIZE) {

On an unrelated note ... Won't if (aad_size > PAGE_SIZE) be sufficient here?

From what I have seen how the buffers are allocated via __get_free_page() I thought so too. However, as it previously read

Quoted 6 lines
	if (aad_size < PAGE_SIZE)
		memset(assoc, 0xff, aad_size);
	else {

my intention was simply to make the modification so that the bug is addressed without introducing an additional change.

I fully agree with you. I was just curious about the comparison here.

Best regards,
Marek Vasut

#5 lore raw

Re: [PATCH 1/3] crypto: Fix potential leak in test_aead_speed() if aad_size is too big

In reply to Christian Engelmayer
Quoted 5 lines · Christian Engelmayer

Fix a potential memory leak in the error handling of test_aead_speed(). In case the size check on the associate data length parameter fails, the function goes through the wrong exit label. Reported by Coverity - CID 1163870.

Acked.

Tim

Quoted 36 lines
---
 crypto/tcrypt.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
crypto/tcrypt.c +6 -8
index 870be7b..1856d7f 100644
@@ -282,6 +282,11 @@ static void test_aead_speed(const char *algo, int enc, unsigned int sec,
 	unsigned int *b_size;
 	unsigned int iv_len;
 
+	if (aad_size >= PAGE_SIZE) {
+		pr_err("associate data length (%u) too big\n", aad_size);
+		return;
+	}
+
 	if (enc == ENCRYPT)
 		e = "encryption";
 	else
@@ -323,14 +328,7 @@ static void test_aead_speed(const char *algo, int enc, unsigned int sec,
 		b_size = aead_sizes;
 		do {
 			assoc = axbuf[0];
-
-			if (aad_size < PAGE_SIZE)
-				memset(assoc, 0xff, aad_size);
-			else {
-				pr_err("associate data length (%u) too big\n",
-					aad_size);
-				goto out_nosg;
-			}
+			memset(assoc, 0xff, aad_size);
 			sg_init_one(&asg[0], assoc, aad_size);
 
 			if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
#6 lore raw

PATCH 2/3 crypto: Fix potential leak in test_aead_speed() if crypto_alloc_aead() fails

In reply to Christian Engelmayer

Fix a potential memory leak in the error handling of test_aead_speed(). In case crypto_alloc_aead() fails, the function returns without going through the centralized cleanup path. Reported by Coverity - CID 1163870.

---
 crypto/tcrypt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
crypto/tcrypt.c +2 -1
index 1856d7f..1849155 100644
@@ -313,7 +313,7 @@ static void test_aead_speed(const char *algo, int enc, unsigned int sec,
 	if (IS_ERR(tfm)) {
 		pr_err("alg: aead: Failed to load transform for %s: %ld\n", algo,
 		       PTR_ERR(tfm));
-		return;
+		goto out_notfm;
 	}
 
 	req = aead_request_alloc(tfm, GFP_KERNEL);
@@ -391,6 +391,7 @@ static void test_aead_speed(const char *algo, int enc, unsigned int sec,
 
 out:
 	crypto_free_aead(tfm);
+out_notfm:
 	kfree(sg);
 out_nosg:
 	testmgr_free_buf(xoutbuf);
Signature · 1 line
1.9.1
Reviewed-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] crypto: Fix potential leak in test_aead_speed() if crypto_alloc_aead() fails

Quoted 5 lines · Christian Engelmayer

Fix a potential memory leak in the error handling of test_aead_speed(). In case crypto_alloc_aead() fails, the function returns without going through the centralized cleanup path. Reported by Coverity - CID 1163870.

Looks OK to me, thanks.

Best regards,
Marek Vasut

#8 lore raw

Re: [PATCH 2/3] crypto: Fix potential leak in test_aead_speed() if crypto_alloc_aead() fails

In reply to Christian Engelmayer
Quoted 5 lines · Christian Engelmayer

Fix a potential memory leak in the error handling of test_aead_speed(). In case crypto_alloc_aead() fails, the function returns without going through the centralized cleanup path. Reported by Coverity - CID 1163870.

Acked.

Tim

Quoted 25 lines
---
 crypto/tcrypt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
crypto/tcrypt.c +2 -1
index 1856d7f..1849155 100644
@@ -313,7 +313,7 @@ static void test_aead_speed(const char *algo, int enc, unsigned int sec,
 	if (IS_ERR(tfm)) {
 		pr_err("alg: aead: Failed to load transform for %s: %ld\n", algo,
 		       PTR_ERR(tfm));
-		return;
+		goto out_notfm;
 	}
 
 	req = aead_request_alloc(tfm, GFP_KERNEL);
@@ -391,6 +391,7 @@ static void test_aead_speed(const char *algo, int enc, unsigned int sec,
 
 out:
 	crypto_free_aead(tfm);
+out_notfm:
 	kfree(sg);
 out_nosg:
 	testmgr_free_buf(xoutbuf);
#9 lore raw

PATCH 3/3 crypto: Fix leak of struct aead_request in test_aead_speed()

In reply to Christian Engelmayer

Fix leakage of memory for struct aead_request that is allocated via aead_request_alloc() but not released via aead_request_free(). Reported by Coverity - CID 1163869.

---
 crypto/tcrypt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
crypto/tcrypt.c +3 -1
index 1849155..09c93ff2 100644
@@ -320,7 +320,7 @@ static void test_aead_speed(const char *algo, int enc, unsigned int sec,
 	if (!req) {
 		pr_err("alg: aead: Failed to allocate request for %s\n",
 		       algo);
-		goto out;
+		goto out_noreq;
 	}
 
 	i = 0;
@@ -390,6 +390,8 @@ static void test_aead_speed(const char *algo, int enc, unsigned int sec,
 	} while (*keysize);
 
 out:
+	aead_request_free(req);
+out_noreq:
 	crypto_free_aead(tfm);
 out_notfm:
 	kfree(sg);
Signature · 1 line
1.9.1
Reviewed-by on v1 3/3
#10 lore raw

Re: [PATCH 3/3] crypto: Fix leak of struct aead_request in test_aead_speed()

Quoted 5 lines · Christian Engelmayer

Fix leakage of memory for struct aead_request that is allocated via aead_request_alloc() but not released via aead_request_free(). Reported by Coverity - CID 1163869.

Best regards,
Marek Vasut

#11 lore raw

Re: [PATCH 3/3] crypto: Fix leak of struct aead_request in test_aead_speed()

In reply to Christian Engelmayer
Quoted 5 lines · Christian Engelmayer

Fix leakage of memory for struct aead_request that is allocated via aead_request_alloc() but not released via aead_request_free(). Reported by Coverity - CID 1163869.

Acked. Thanks for fixing the leak.

Tim

Quoted 26 lines
---
 crypto/tcrypt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
crypto/tcrypt.c +3 -1
index 1849155..09c93ff2 100644
@@ -320,7 +320,7 @@ static void test_aead_speed(const char *algo, int enc, unsigned int sec,
 	if (!req) {
 		pr_err("alg: aead: Failed to allocate request for %s\n",
 		       algo);
-		goto out;
+		goto out_noreq;
 	}
 
 	i = 0;
@@ -390,6 +390,8 @@ static void test_aead_speed(const char *algo, int enc, unsigned int sec,
 	} while (*keysize);
 
 out:
+	aead_request_free(req);
+out_noreq:
 	crypto_free_aead(tfm);
 out_notfm:
 	kfree(sg);
maintainer
#12 lore raw
In reply to Christian Engelmayer
Quoted 6 lines · Christian Engelmayer

This is a cleanup of Coverity ressource leak findings for the quick & dirty crypto testing module crypto/tcrypt.c.

All 3 changesets address function test_aead_speed() that was introduced in 53f52d7a (crypto: tcrypt - Added speed tests for AEAD crypto alogrithms in tcrypt test suite)

All applied. Thanks!

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt