diff mbox series

crypto: ccp - fix memleak in ccp_init_dm_workarea

Message ID 20231123070337.11600-1-dinghao.liu@zju.edu.cn
State Accepted
Commit a1c95dd5bc1d6a5d7a75a376c2107421b7d6240d
Headers show
Series crypto: ccp - fix memleak in ccp_init_dm_workarea | expand

Commit Message

Dinghao Liu Nov. 23, 2023, 7:03 a.m. UTC
When dma_map_single() fails, we should free wa->address to
prevent potential memleak.

Fixes: 63b945091a07 ("crypto: ccp - CCP device driver and interface support")
Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
---
 drivers/crypto/ccp/ccp-ops.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Tom Lendacky Nov. 25, 2023, 3:10 p.m. UTC | #1
On 11/23/23 01:03, Dinghao Liu wrote:
> When dma_map_single() fails, we should free wa->address to
> prevent potential memleak.

You should expand on this a bit more. The ccp_dm_free() function is 
normally called to free that memory, but many of the call spots don't 
expect to have to call ccp_dm_free() on an error return from 
ccp_init_dm_workarea(). Because of that, the memory is freed in 
ccp_init_dm_workarea().

So a more detailed commit message about why this change is needed should 
be provided.

> 
> Fixes: 63b945091a07 ("crypto: ccp - CCP device driver and interface support")
> Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> ---
>   drivers/crypto/ccp/ccp-ops.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/crypto/ccp/ccp-ops.c b/drivers/crypto/ccp/ccp-ops.c
> index aa4e1a500691..45552ae6347e 100644
> --- a/drivers/crypto/ccp/ccp-ops.c
> +++ b/drivers/crypto/ccp/ccp-ops.c
> @@ -179,8 +179,10 @@ 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);

If future changes should result in ccp_dm_free() being called on an error 
returned from ccp_init_dm_workarea(), you should add:

			wa->address = NULL;

Thanks,
Tom

>   			return -ENOMEM;
> +		}
>   
>   		wa->dma.length = len;
>   	}
Dinghao Liu Nov. 27, 2023, 1:45 a.m. UTC | #2
> On 11/23/23 01:03, Dinghao Liu wrote:
> > When dma_map_single() fails, we should free wa->address to
> > prevent potential memleak.
> 
> You should expand on this a bit more. The ccp_dm_free() function is 
> normally called to free that memory, but many of the call spots don't 
> expect to have to call ccp_dm_free() on an error return from 
> ccp_init_dm_workarea(). Because of that, the memory is freed in 
> ccp_init_dm_workarea().
> 
> So a more detailed commit message about why this change is needed should 
> be provided.

Thanks for your suggestion! I will resend a new patch soon.

> 
> > 
> > Fixes: 63b945091a07 ("crypto: ccp - CCP device driver and interface support")
> > Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> > ---
> >   drivers/crypto/ccp/ccp-ops.c | 4 +++-
> >   1 file changed, 3 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/crypto/ccp/ccp-ops.c b/drivers/crypto/ccp/ccp-ops.c
> > index aa4e1a500691..45552ae6347e 100644
> > --- a/drivers/crypto/ccp/ccp-ops.c
> > +++ b/drivers/crypto/ccp/ccp-ops.c
> > @@ -179,8 +179,10 @@ 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);
> 
> If future changes should result in ccp_dm_free() being called on an error 
> returned from ccp_init_dm_workarea(), you should add:
> 
> 			wa->address = NULL;
> 

I will fix this in the new patch, thanks!

Regards,
Dinghao
diff mbox series

Patch

diff --git a/drivers/crypto/ccp/ccp-ops.c b/drivers/crypto/ccp/ccp-ops.c
index aa4e1a500691..45552ae6347e 100644
--- a/drivers/crypto/ccp/ccp-ops.c
+++ b/drivers/crypto/ccp/ccp-ops.c
@@ -179,8 +179,10 @@  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);
 			return -ENOMEM;
+		}
 
 		wa->dma.length = len;
 	}