Skip to contentSkip to conversation

bus: fsl-mc: fix invalid free in fsl_mc_device_add

PATCH In review [email protected] 3 participants 3 messages Jul 9, 2020 – Jul 10, 2020 QorIQ DPAA2 fsl-mc bus driver

lore
Share & tools
lore
#0 lore raw

PATCH v3 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.

Fixes a042fbed0290 ("staging: fsl-mc: simplify couple of deallocations")

---
v1: add a comment to explain freeing uniqueness
v2: add gregkh's suggestion to comment.

 drivers/bus/fsl-mc/fsl-mc-bus.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/bus/fsl-mc/fsl-mc-bus.c +10 -2
index 40526da5c6a6..839d96d03f0d 100644
@@ -691,8 +691,16 @@ 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);
+	/*
+	 * 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.
+	 */
+	if (mc_bus)
+		kfree(mc_bus);
+	else
+		kfree(mc_dev);
 
 	return error;
 }
Signature · 1 line
2.18.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 65 lines · On Thu, Jul 09, 2020 at 08:31:19AM -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_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

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")

---
v1: add a comment to explain freeing uniqueness
v2: add gregkh's suggestion to comment.

 drivers/bus/fsl-mc/fsl-mc-bus.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/bus/fsl-mc/fsl-mc-bus.c +10 -2
index 40526da5c6a6..839d96d03f0d 100644
@@ -691,8 +691,16 @@ 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);
+	/*
+	 * 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.
+	 */
+	if (mc_bus)
+		kfree(mc_bus);
+	else
+		kfree(mc_dev);
 
 	return error;
 }
Signature · 1 line
2.18.1

Also add a cc: stable to this please when it is applied to a tree, in the proper place, as it fixes a bug.

thanks,

greg k-h

Reviewed-by on v3 1/1
#2 lore raw
In reply to [email protected]
Quoted 53 lines · On 7/9/2020 6:31 PM, [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_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

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")

---
v1: add a comment to explain freeing uniqueness
v2: add gregkh's suggestion to comment.

 drivers/bus/fsl-mc/fsl-mc-bus.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/bus/fsl-mc/fsl-mc-bus.c +3 -2
index 40526da5c6a6..839d96d03f0d 100644
@@ -691,8 +691,16 @@ 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);
+	/*
+	 * mc_bus allocates a private version of mc_dev
+	 * it is not appropriate to free the private version.

drivers/bus/fsl-mc/fsl-mc-bus.c int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc, · line 706

Quoted 10 lines
drivers/bus/fsl-mc/fsl-mc-bus.c +7 -0
+	 * Which means we have to check the pointer before freeing it.
+	 * Do not remove this check.
+	 */
+	if (mc_bus)
+		kfree(mc_bus);
+	else
+		kfree(mc_dev);
 
 	return error;
 }

Thanks, Tom!

---
Best Regards, Laurentiu