* [PATCH] [v2] crypto: ccp - fix memleak in ccp_init_dm_workarea
@ 2023-11-27 3:47 Dinghao Liu
2023-11-28 16:56 ` Tom Lendacky
2023-12-08 4:04 ` Herbert Xu
0 siblings, 2 replies; 3+ messages in thread
From: Dinghao Liu @ 2023-11-27 3:47 UTC (permalink / raw)
To: dinghao.liu
Cc: Tom Lendacky, John Allen, Herbert Xu, David S. Miller,
linux-crypto, linux-kernel
When dma_map_single() fails, wa->address is supposed to be freed
by the callers of ccp_init_dm_workarea() through ccp_dm_free().
However, many of the call spots don't expect to have to call
ccp_dm_free() on failure of ccp_init_dm_workarea(), which may
lead to a memleak. Let's free wa->address in ccp_init_dm_workarea()
when dma_map_single() fails.
Fixes: 63b945091a07 ("crypto: ccp - CCP device driver and interface support")
Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
---
Changelog:
v2: -Improve the commit message.
-Set wa->address to NULL after kfree() to prevent double-free.
---
drivers/crypto/ccp/ccp-ops.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/drivers/crypto/ccp/ccp-ops.c b/drivers/crypto/ccp/ccp-ops.c
index aa4e1a500691..cb8e99936abb 100644
--- a/drivers/crypto/ccp/ccp-ops.c
+++ b/drivers/crypto/ccp/ccp-ops.c
@@ -179,8 +179,11 @@ static int ccp_init_dm_workarea(struct ccp_dm_workarea *wa,
wa->dma.address = dma_map_single(wa->dev, wa->address, len,
dir);
- if (dma_mapping_error(wa->dev, wa->dma.address))
+ if (dma_mapping_error(wa->dev, wa->dma.address)) {
+ kfree(wa->address);
+ wa->address = NULL;
return -ENOMEM;
+ }
wa->dma.length = len;
}
--
2.17.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] [v2] crypto: ccp - fix memleak in ccp_init_dm_workarea
2023-11-27 3:47 [PATCH] [v2] crypto: ccp - fix memleak in ccp_init_dm_workarea Dinghao Liu
@ 2023-11-28 16:56 ` Tom Lendacky
2023-12-08 4:04 ` Herbert Xu
1 sibling, 0 replies; 3+ messages in thread
From: Tom Lendacky @ 2023-11-28 16:56 UTC (permalink / raw)
To: Dinghao Liu
Cc: John Allen, Herbert Xu, David S. Miller, linux-crypto, linux-kernel
On 11/26/23 21:47, Dinghao Liu wrote:
> When dma_map_single() fails, wa->address is supposed to be freed
> by the callers of ccp_init_dm_workarea() through ccp_dm_free().
> However, many of the call spots don't expect to have to call
> ccp_dm_free() on failure of ccp_init_dm_workarea(), which may
> lead to a memleak. Let's free wa->address in ccp_init_dm_workarea()
> when dma_map_single() fails.
>
> Fixes: 63b945091a07 ("crypto: ccp - CCP device driver and interface support")
> Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
Acked-by: Tom Lendacky <thomas.lendacky@amd.com>
> ---
>
> Changelog:
>
> v2: -Improve the commit message.
> -Set wa->address to NULL after kfree() to prevent double-free.
> ---
> drivers/crypto/ccp/ccp-ops.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/crypto/ccp/ccp-ops.c b/drivers/crypto/ccp/ccp-ops.c
> index aa4e1a500691..cb8e99936abb 100644
> --- a/drivers/crypto/ccp/ccp-ops.c
> +++ b/drivers/crypto/ccp/ccp-ops.c
> @@ -179,8 +179,11 @@ static int ccp_init_dm_workarea(struct ccp_dm_workarea *wa,
>
> wa->dma.address = dma_map_single(wa->dev, wa->address, len,
> dir);
> - if (dma_mapping_error(wa->dev, wa->dma.address))
> + if (dma_mapping_error(wa->dev, wa->dma.address)) {
> + kfree(wa->address);
> + wa->address = NULL;
> return -ENOMEM;
> + }
>
> wa->dma.length = len;
> }
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] [v2] crypto: ccp - fix memleak in ccp_init_dm_workarea
2023-11-27 3:47 [PATCH] [v2] crypto: ccp - fix memleak in ccp_init_dm_workarea Dinghao Liu
2023-11-28 16:56 ` Tom Lendacky
@ 2023-12-08 4:04 ` Herbert Xu
1 sibling, 0 replies; 3+ messages in thread
From: Herbert Xu @ 2023-12-08 4:04 UTC (permalink / raw)
To: Dinghao Liu
Cc: Tom Lendacky, John Allen, David S. Miller, linux-crypto, linux-kernel
On Mon, Nov 27, 2023 at 11:47:10AM +0800, Dinghao Liu wrote:
> When dma_map_single() fails, wa->address is supposed to be freed
> by the callers of ccp_init_dm_workarea() through ccp_dm_free().
> However, many of the call spots don't expect to have to call
> ccp_dm_free() on failure of ccp_init_dm_workarea(), which may
> lead to a memleak. Let's free wa->address in ccp_init_dm_workarea()
> when dma_map_single() fails.
>
> Fixes: 63b945091a07 ("crypto: ccp - CCP device driver and interface support")
> Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> ---
>
> Changelog:
>
> v2: -Improve the commit message.
> -Set wa->address to NULL after kfree() to prevent double-free.
> ---
> drivers/crypto/ccp/ccp-ops.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
Patch applied. Thanks.
--
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-12-08 4:04 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-27 3:47 [PATCH] [v2] crypto: ccp - fix memleak in ccp_init_dm_workarea Dinghao Liu
2023-11-28 16:56 ` Tom Lendacky
2023-12-08 4:04 ` Herbert Xu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®