2022-07-04 01:10:36

by Jianglei Nie

[permalink] [raw]
Subject: [PATCH] ima/evm: Fix potential memory leak in ima_init_crypto()

ima_init_crypto() allocates a memory chunk for "ima_algo_array" with
kcalloc(). When some errors occur, the function jumps to "out_array"
and releases the "ima_algo_array[i].tfm". But "ima_algo_array" is
not released, which will lead to a memory leak.

We can release the ima_algo_array with kfree() when some errors occur
to fix the memory leak.

Signed-off-by: Jianglei Nie <[email protected]>
---
security/integrity/ima/ima_crypto.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/security/integrity/ima/ima_crypto.c b/security/integrity/ima/ima_crypto.c
index a7206cc1d7d1..64499056648a 100644
--- a/security/integrity/ima/ima_crypto.c
+++ b/security/integrity/ima/ima_crypto.c
@@ -205,6 +205,7 @@ int __init ima_init_crypto(void)

crypto_free_shash(ima_algo_array[i].tfm);
}
+ kfree(ima_algo_array);
out:
crypto_free_shash(ima_shash_tfm);
return rc;
--
2.25.1


2022-07-07 04:13:30

by Mimi Zohar

[permalink] [raw]
Subject: Re: [PATCH] ima/evm: Fix potential memory leak in ima_init_crypto()

Hi Jianglei,

Thank you for the patch.

On Mon, 2022-07-04 at 08:59 +0800, Jianglei Nie wrote:
> ima_init_crypto() allocates a memory chunk for "ima_algo_array" with
> kcalloc(). When some errors occur, the function jumps to "out_array"
> and releases the "ima_algo_array[i].tfm". But "ima_algo_array" is
> not released, which will lead to a memory leak.

There's too much low level code details in the above paragraph. The
patch description should be written from a higher level perspective.
Refer to the original commit 6d94809af6b0 ("ima: Allocate and
initialize tfm for each PCR bank") for an example.
>
> We can release the ima_algo_array with kfree() when some errors occur
> to fix the memory leak.

Please re-word the above sentence in the imperative mode. Refer to
Documentation/process/submitting-patches.rst for an example.

thanks,

Mimi
>
> Signed-off-by: Jianglei Nie <[email protected]>