Skip to contentSkip to conversation

crypto: sun8i-ce - Use kcalloc() instead of kzalloc()

PATCH In review Erick Archer 4 participants 4 messages Jan 21, 2024 – Jan 26, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: sun8i-ce - Use kcalloc() instead of kzalloc()

As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.

So, use the purpose specific kcalloc() function instead of the argument size * count in the kzalloc() function.

---
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c +1 -1
index d358334e5981..ee2a28c906ed 100644
@@ -362,7 +362,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
 		digestsize = SHA512_DIGEST_SIZE;
 
 	/* the padding could be up to two block. */
-	buf = kzalloc(bs * 2, GFP_KERNEL | GFP_DMA);
+	buf = kcalloc(2, bs, GFP_KERNEL | GFP_DMA);
 	if (!buf) {
 		err = -ENOMEM;
 		goto theend;

--
2.25.1

Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Erick Archer

As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.

Quoted 6 lines

So, use the purpose specific kcalloc() function instead of the argument size * count in the kzalloc() function.

Thanks!

Signature · 1 line
Gustavo
Quoted 20 lines
---
  drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c +1 -2
index d358334e5981..ee2a28c906ed 100644
@@ -362,7 +362,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
  		digestsize = SHA512_DIGEST_SIZE;
 
  	/* the padding could be up to two block. */
-	buf = kzalloc(bs * 2, GFP_KERNEL | GFP_DMA);
+	buf = kcalloc(2, bs, GFP_KERNEL | GFP_DMA);
  	if (!buf) {
  		err = -ENOMEM;
  		goto theend;
--

2.25.1

maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Erick Archer

Dne nedelja, 21. januar 2024 ob 16:34:07 CET je Erick Archer napisal(a):

Quoted 8 lines

As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.

Quoted 6 lines

So, use the purpose specific kcalloc() function instead of the argument size * count in the kzalloc() function.

Best regards,
Jernej

maintainer
#3 lore raw
In reply to Erick Archer
Quoted 11 lines · Erick Archer

As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.

So, use the purpose specific kcalloc() function instead of the argument size * count in the kzalloc() function.

Quoted 6 lines
---
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Patch 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