bus: fsl-mc: fix improper free of mc_dev
PATCH 1/1 bus: fsl-mc: fix improper free of mc_dev
From: Tom Rix <[email protected]>
Clang static analysis reports this error
fsl-mc-bus.c:891:2: warning: Attempt to free released memory
kfree(mc_dev);
^~~~~~~~~~~~~In this block of code
if (strcmp(obj_desc->type, "dprc") == 0) {
..
mc_bus = kzalloc(..)
mc_dev = &mc_bus->mc_dev;
mc_dev is not alloc-ed, so it should not be freed. Old handler triggers a false positive from checkpatch, so add a comment and change logic a bit.
--- drivers/bus/fsl-mc/fsl-mc-bus.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/bus/fsl-mc/fsl-mc-bus.c +3 -1
@@ -887,8 +887,10 @@ int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc, error_cleanup_dev:kfree (mc_dev->regions); +/* mc_dev is only allocated when it is not part of mc_bus */ +if (!mc_bus) +kfree (mc_dev);kfree (mc_bus); -kfree (mc_dev);return error; }
Signature · 1 line
2.26.3
Quoted 9 lines · On Tue, Apr 27, 2021 at 11:36 AM <[email protected]> wrote:
From: Tom Rix <[email protected]>
Clang static analysis reports this error
fsl-mc-bus.c:891:2: warning: Attempt to free released memory
kfree(mc_dev); ^~~~~~~~~~~~~
Quoted 6 lines
In this block of code
if (
strcmp(obj_desc->type, "dprc")== 0) {
..
mc_bus= kzalloc(..)
mc_dev=&mc_bus->mc_dev;
Thanks for the patch.
Aren't the allocations for mc_bus and mc_dev mutually exclusive based on that conditional? If so...
Quoted 16 lines
mc_devis not alloc-ed, so it should not be freed. Old handler triggers a false positive from checkpatch, so add a comment and change logic a bit.Fixes a042fbed0290 ("staging: fsl-mc: simplify couple of deallocations")Signed-off-by Tom Rix <[email protected]>--- drivers/bus/fsl-mc/fsl-mc-bus.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)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 891
Quoted 8 lines
drivers/bus/fsl-mc/fsl-mc-bus.c +3
-1@@ -887,8 +887,10 @@ int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc, error_cleanup_dev:kfree (mc_dev->regions); +/* mc_dev is only allocated when it is not part of mc_bus */ +if (!mc_bus) +kfree (mc_dev);kfree (mc_bus); -kfree (mc_dev);
The error handling here seems quite wrong (regardless of your patch). mc_dev->regions is allocated by fsl_mc_device_get_mmio_regions() IIUC. Wouldn't the first goto error_cleanup_dev; taken end up passing an uninitialized pointer to kfree()?
what if strcmp(obj_desc->type, "dprc") == 0 is false? We allocate mc_dev, but then call kfree on mc_bus?
I think it would be safer to locally save the result of strcmp(obj_desc->type, "dprc") == 0, then check that throughout this function, including the error handling at the end, or use multiple labels to unwind the allocations correctly.
drivers/bus/fsl-mc/fsl-mc-bus.c int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
Quoted 5 lines
drivers/bus/fsl-mc/fsl-mc-bus.c +0
-1return error; } --
--
Thanks,
~Nick Desaulniers
Quoted 36 lines · Nick Desaulniers
Quoted 8 lines · On Tue, Apr 27, 2021 at 11:36 AM <[email protected]> wrote:
From: Tom Rix <[email protected]>
Clang static analysis reports this error
fsl-mc-bus.c:891:2: warning: Attempt to free released memory
kfree(mc_dev); ^~~~~~~~~~~~~Quoted 6 lines
In this block of code
if (
strcmp(obj_desc->type, "dprc")== 0) {
..
mc_bus= kzalloc(..)
mc_dev=&mc_bus->mc_dev;Thanks for the patch.
Aren't the allocations for
mc_busandmc_devmutually exclusive based on that conditional? If so...Quoted 16 lines
mc_devis not alloc-ed, so it should not be freed. Old handler triggers a false positive from checkpatch, so add a comment and change logic a bit.Fixes a042fbed0290 ("staging: fsl-mc: simplify couple of deallocations")Signed-off-by Tom Rix <[email protected]>--- drivers/bus/fsl-mc/fsl-mc-bus.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/bus/fsl-mc/fsl-mc-bus.c +0
-0@@ -887,8 +887,10 @@ int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
Quoted 11 lines
drivers/bus/fsl-mc/fsl-mc-bus.c int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc, · line 891
Quoted 7 lines
drivers/bus/fsl-mc/fsl-mc-bus.c +3
-1error_cleanup_dev:kfree (mc_dev->regions); +/* mc_dev is only allocated when it is not part of mc_bus */ +if (!mc_bus) +kfree (mc_dev);kfree (mc_bus); -kfree (mc_dev);The error handling here seems quite wrong (regardless of your patch).
mc_dev->regionsis allocated byfsl_mc_device_get_mmio_regions()IIUC. Wouldn't the firstgoto error_cleanup_dev;taken end up passing an uninitialized pointer tokfree()?
On the first goto, mc_dev->regions, because of the kzalloc, the value would be
mc_bus->mc_dev.regions , should be 0 or
mc_dev->regions, which also should be 0
and kfree handles 0.
Quoted 3 lines
what if
strcmp(obj_desc->type, "dprc") == 0is false? We allocatemc_dev, but then call kfree onmc_bus?
mc_bus is initialized to NULL, which makes the call to kfree safe.
The original handler was
if (mc_bus)
kfree(mc_bus)
else
kfree(mc_dev)
I tried this first, which works, but checkpatch throw a warning for kfree(mc_bus).
This change makes the 'else' with the !mc_bus
Quoted 2 lines
I think it would be safer to locally save the result of
strcmp(obj_desc->type, "dprc") == 0, then check that throughout this
the local mc_bus is only set in this block, so I don't think another local is needed.
Quoted 2 lines
function, including the error handling at the end, or use multiple labels to unwind the allocations correctly.
The goto's could be finer grained because some of the mc_dev->regions are known to be unallocated.
Changing these would not be a fix and it could be argued the simpler, less efficent error handling works as designed.
Tom
Quoted 9 lines
drivers/bus/fsl-mc/fsl-mc-bus.c int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
Quoted 4 lines
drivers/bus/fsl-mc/fsl-mc-bus.c +0
-1return error; } ----
Thanks,
~Nick Desaulniers