Эх сурвалжийг харах

crypto: testmgr - Allocate only the required output size for hash tests

There are some hashes (e.g. sha224) that have some internal trickery
to make sure that only the correct number of output bytes are
generated.  If something goes wrong, they could potentially overrun
the output buffer.

Make the test more robust by allocating only enough space for the
correct output size so that memory debugging will catch the error if
the output is overrun.

Tested by intentionally breaking sha224 to output all 256
internally-generated bits while running on KASAN.

Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Andrew Lutomirski 8 жил өмнө
parent
commit
e93acd6f67

+ 5 - 4
crypto/testmgr.c

@@ -265,6 +265,7 @@ static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
 		       const int align_offset)
 		       const int align_offset)
 {
 {
 	const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
 	const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
+	size_t digest_size = crypto_ahash_digestsize(tfm);
 	unsigned int i, j, k, temp;
 	unsigned int i, j, k, temp;
 	struct scatterlist sg[8];
 	struct scatterlist sg[8];
 	char *result;
 	char *result;
@@ -275,7 +276,7 @@ static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
 	char *xbuf[XBUFSIZE];
 	char *xbuf[XBUFSIZE];
 	int ret = -ENOMEM;
 	int ret = -ENOMEM;
 
 
-	result = kmalloc(MAX_DIGEST_SIZE, GFP_KERNEL);
+	result = kmalloc(digest_size, GFP_KERNEL);
 	if (!result)
 	if (!result)
 		return ret;
 		return ret;
 	key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
 	key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
@@ -305,7 +306,7 @@ static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
 			goto out;
 			goto out;
 
 
 		j++;
 		j++;
-		memset(result, 0, MAX_DIGEST_SIZE);
+		memset(result, 0, digest_size);
 
 
 		hash_buff = xbuf[0];
 		hash_buff = xbuf[0];
 		hash_buff += align_offset;
 		hash_buff += align_offset;
@@ -380,7 +381,7 @@ static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
 			continue;
 			continue;
 
 
 		j++;
 		j++;
-		memset(result, 0, MAX_DIGEST_SIZE);
+		memset(result, 0, digest_size);
 
 
 		temp = 0;
 		temp = 0;
 		sg_init_table(sg, template[i].np);
 		sg_init_table(sg, template[i].np);
@@ -458,7 +459,7 @@ static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
 			continue;
 			continue;
 
 
 		j++;
 		j++;
-		memset(result, 0, MAX_DIGEST_SIZE);
+		memset(result, 0, digest_size);
 
 
 		ret = -EINVAL;
 		ret = -EINVAL;
 		hash_buff = xbuf[0];
 		hash_buff = xbuf[0];