[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20200708184524.1809-1-trix@redhat.com>
Date: Wed, 8 Jul 2020 11:45:24 -0700
From: trix@...hat.com
To: stuyoder@...il.com, laurentiu.tudor@....com,
gregkh@...uxfoundation.org
Cc: linux-kernel@...r.kernel.org, Tom Rix <trix@...hat.com>
Subject: [PATCH] bus: fsl-mc: fix invalid free in fsl_mc_device_add
From: Tom Rix <trix@...hat.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@...hat.com>
---
drivers/bus/fsl-mc/fsl-mc-bus.c | 6 ++++--
1 file changed, 4 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..7390e56661a0 100644
--- a/drivers/bus/fsl-mc/fsl-mc-bus.c
+++ b/drivers/bus/fsl-mc/fsl-mc-bus.c
@@ -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;
}
--
2.18.1
Powered by blists - more mailing lists