[PATCH] crypto: Mark tfm buffer as non leak.

From: Peter Enderborg
Date: Thu Sep 03 2020 - 10:51:55 EST


When running kmemleak on this I got a lot of

unreferenced object 0xfffffff942d4ec00 (size 1024):
comm "init", pid 1, jiffies 4294893619 (age 17475.864s)
hex dump (first 32 bytes):
38 1d cf bd 9e ff ff ff b8 37 8b bd 9e ff ff ff 8........7......
78 38 8b bd 9e ff ff ff 10 00 00 00 00 00 00 00 x8..............
backtrace:
[<00000000c3c55a80>] __kmalloc+0x2cc/0x3b0
[<00000000c599b091>] crypto_create_tfm+0x38/0xf0
[<00000000d4516e51>] crypto_spawn_tfm2+0x58/0xa0
[<000000001bab58aa>] cryptd_skcipher_init_tfm+0x1c/0x40
[<0000000006748df3>] crypto_skcipher_init_tfm+0x158/0x1e0
[<0000000017f3270c>] crypto_create_tfm+0x54/0xf0
[<000000006af1de62>] crypto_alloc_tfm+0x88/0x198
[<000000000d8e8c03>] crypto_alloc_skcipher+0x1c/0x28
[<0000000085448a2a>] cryptd_alloc_skcipher+0x5c/0xb0
[<000000003c48c083>] simd_skcipher_init+0x24/0x68
[<0000000006748df3>] crypto_skcipher_init_tfm+0x158/0x1e0
[<0000000017f3270c>] crypto_create_tfm+0x54/0xf0
[<00000000d4516e51>] crypto_spawn_tfm2+0x58/0xa0
[<00000000b5344705>] crypto_cts_init_tfm+0x1c/0x68
[<0000000006748df3>] crypto_skcipher_init_tfm+0x158/0x1e0
[<0000000017f3270c>] crypto_create_tfm+0x54/0xf0

This is caused by tfm = (struct crypto_tfm *)(mem + tfmsize);
that is keept instead of the allocated buffer in mem.
Reference counting is done on alg.

Signed-off-by: Peter Enderborg <peter.enderborg@xxxxxxxx>
---
crypto/api.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/crypto/api.c b/crypto/api.c
index ed08cbd5b9d3..1a9cb6852a56 100644
--- a/crypto/api.c
+++ b/crypto/api.c
@@ -21,6 +21,7 @@
#include <linux/string.h>
#include <linux/completion.h>
#include "internal.h"
+#include <linux/kmemleak.h>

LIST_HEAD(crypto_alg_list);
EXPORT_SYMBOL_GPL(crypto_alg_list);
@@ -460,7 +461,7 @@ void *crypto_create_tfm_node(struct crypto_alg *alg,

if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
goto cra_init_failed;
-
+ kmemleak_not_leak(mem);
goto out;

cra_init_failed:
--
2.17.1