Skip to contentSkip to conversation

crypto: stm32: Check for NULL return of kmalloc_array()

PATCH In review QintaoShen 2 participants 2 messages Mar 24, 2022 – Mar 25, 2022

Subsystems (2)
lore
Share & tools
lore

QintaoShen

author
#0 lore raw

PATCH 1/1 crypto: stm32: Check for NULL return of kmalloc_array()

As the potential failure of memory allocation, kmalloc_array() may return NULL and lead to NULL pointer dereference because of the usage of 'rctx->hw_context'.

Therefore, it is better to check the return value of kmalloc_array().

---
 drivers/crypto/stm32/stm32-hash.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/crypto/stm32/stm32-hash.c +2 -1
index d33006d..f1a1a13 100644
@@ -970,7 +970,8 @@ static int stm32_hash_export(struct ahash_request *req, void *out)
 	rctx->hw_context = kmalloc_array(3 + HASH_CSR_REGISTER_NUMBER,
 					 sizeof(u32),
 					 GFP_KERNEL);
-
+	if (!rctx->hw_context)
+		return -ENOMEM;
 	preg = rctx->hw_context;
 
 	*preg++ = stm32_hash_read(hdev, HASH_IMR);
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 15 lines · QintaoShen

As the potential failure of memory allocation, kmalloc_array() may return NULL and lead to NULL pointer dereference because of the usage of 'rctx->hw_context'.

Therefore, it is better to check the return value of kmalloc_array().

---
drivers/crypto/stm32/stm32-hash.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
drivers/crypto/stm32/stm32-hash.c +0 -0
index d33006d..f1a1a13 100644

drivers/crypto/stm32/stm32-hash.c static int stm32_hash_export(struct ahash_request *req, void *out) · line 974

Quoted 6 lines
drivers/crypto/stm32/stm32-hash.c +2 -1
@@ -970,7 +970,8 @@ static int stm32_hash_export(struct ahash_request *req, void *out)
       rctx->hw_context = kmalloc_array(3 + HASH_CSR_REGISTER_NUMBER,
                                        sizeof(u32),
                                        GFP_KERNEL);
-
+       if (!rctx->hw_context)
+               return -ENOMEM;

This patch introduces a runtime PM imbalance. Please fix.

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