lkml.org 
[lkml]   [2020]   [Jul]   [9]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH v2] bus: fsl-mc: fix invalid free in fsl_mc_device_add
On Thu, Jul 09, 2020 at 05:41:15AM -0700, trix@redhat.com wrote:
> From: Tom Rix <trix@redhat.com>
>
> clang static analysis flags this error
>
> fsl-mc-bus.c:695:2: warning: Attempt to free released memory [unix.Malloc]
> kfree(mc_dev);
> ^~~~~~~~~~~~~
>
> The problem block of code is
>
> mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
> if (!mc_bus)
> return -ENOMEM;
>
> mc_dev = &mc_bus->mc_dev;
>
> mc_bus's structure contains a mc_dev element,
> freeing it later is not appropriate.
>
> So check if mc_bus was allocated before freeing mc_dev
>
> This is a case where checkpatch
>
> WARNING: kfree(NULL) is safe and this check is probably not required
> + if (mc_bus)
> + kfree(mc_bus);
>
> is wrong.
>
> Fixes: a042fbed0290 ("staging: fsl-mc: simplify couple of deallocations")
>
> Signed-off-by: Tom Rix <trix@redhat.com>
> ---
> v1: add a comment to explain freeing uniqueness
>
> drivers/bus/fsl-mc/fsl-mc-bus.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/bus/fsl-mc/fsl-mc-bus.c b/drivers/bus/fsl-mc/fsl-mc-bus.c
> index 40526da5c6a6..df10ed430baa 100644
> --- a/drivers/bus/fsl-mc/fsl-mc-bus.c
> +++ b/drivers/bus/fsl-mc/fsl-mc-bus.c
> @@ -691,8 +691,14 @@ int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
>
> error_cleanup_dev:
> kfree(mc_dev->regions);
> - kfree(mc_bus);
> - kfree(mc_dev);

Close, but that still doesn't help with anyone coming along later, how
about adding:

> + /*
> + * mc_bus allocates a private version of mc_dev
> + * it is not appropriate to free the private version.

* which means we have to check the pointer before freeing it.
* Do not remove this check.

thanks,

greg k-h

\
 
 \ /
  Last update: 2020-07-09 15:38    [W:0.037 / U:0.244 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site