bus: fsl-mc: fix invalid free in fsl_mc_device_add
PATCH 1/1 bus: fsl-mc: fix invalid free in fsl_mc_device_add
From: Tom Rix <[email protected]>
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.
--- drivers/bus/fsl-mc/fsl-mc-bus.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/bus/fsl-mc/fsl-mc-bus.c +4 -2
@@ -691,8 +691,10 @@ 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); +if (mc_bus) +kfree (mc_bus); +else +kfree (mc_dev);return error; }
Signature · 1 line
2.18.1
Quoted 41 lines · On Wed, Jul 08, 2020 at 11:45:24AM -0700, [email protected] wrote:
From: Tom Rix <[email protected]>
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_develement,
freeing it later is not appropriate.So check if
mc_buswas allocated before freeingmc_devThis is a case where checkpatch
WARNING: kfree(NULL) is safe and this check is probably not required
drivers/bus/fsl-mc/fsl-mc-bus.c +2
-0+if (mc_bus) +kfree (mc_bus);is wrong.
Fixes a042fbed0290 ("staging: fsl-mc: simplify couple of deallocations")Signed-off-by Tom Rix <[email protected]>--- drivers/bus/fsl-mc/fsl-mc-bus.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)drivers/bus/fsl-mc/fsl-mc-bus.c +0
-0
drivers/bus/fsl-mc/fsl-mc-bus.c int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc, · line 697
Quoted 9 lines
drivers/bus/fsl-mc/fsl-mc-bus.c +4
-2@@ -691,8 +691,10 @@ 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); +if (mc_bus) +kfree (mc_bus); +else +kfree (mc_dev);
You should really put a comment on this saying why you need to do it this way, otherwise someone will come along and "clean it up" again the same way in the future.
thanks,
greg k-h