Skip to contentSkip to conversation

bus: fsl-mc: Fix two double free problems in fsl_mc_device_add()

PATCH In review Su Hui 2 participants 4 messages Nov 15, 2024

Subsystems (2)
lore
Share & tools
lore

Su Hui

author
#0 lore raw

v2 cover bus: fsl-mc: Fix two double free problems in fsl_mc_device_add()

This patchset fixes two double free problems in fsl_mc_device_add(). One is reported by clang static checker, another is reported by Dan when reviewing the code.

ps: There is only patch 1 in v1 patch, patch 2 has no v1 version.

v1: https://lore.kernel.org/all/[email protected]/

Su Hui (2):
bus: fsl-mc: Fix the double free in fsl_mc_device_add()
bus: fsl-mc: using put_device() when add_device() failed in

    fsl_mc_device_add()
 drivers/bus/fsl-mc/fsl-mc-bus.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
Signature · 1 line
2.30.2
author
#1 lore raw

PATCH v2 1/2 bus: fsl-mc: Fix the double free in fsl_mc_device_add()

Clang static checker(scan-build) warning: drivers/bus/fsl-mc/fsl-mc-bus.c: line 909, column 2 Attempt to free released memory.

When 'obj_desc->type' == "dprc" and begin to free 'mc_bus' and 'mc_dev', there is a double free problem because of 'mc_dev = &mc_bus->mc_dev'. Add a judgment to fix this problem.

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

---
v2:
- using is_fsl_mc_bus_dprc().
https://lore.kernel.org/all/[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 +4 -2
index 930d8a3ba722..319a081a29ef 100644
@@ -905,8 +905,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 (is_fsl_mc_bus_dprc(mc_dev))
+		kfree(mc_bus);
+	else
+		kfree(mc_dev);
 
 	return error;
 }
Signature · 1 line
2.30.2
author
#2 lore raw

PATCH v2 2/2 bus: fsl-mc: using put_device() when add_device() failed in fsl_mc_device_add()

In reply to Su Hui

When device_add(&mc_dev->dev) failed, calling put_device() to let 'mc_dev->dev.release' free the resource rather than goto label error_cleanup_dev. Otherwise, it can cause double free problem.

Fixes 95b3523b723e ("staging: fsl-mc: add device release callback")
---
 drivers/bus/fsl-mc/fsl-mc-bus.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/bus/fsl-mc/fsl-mc-bus.c +2 -1
index 319a081a29ef..09f8772fed8f 100644
@@ -895,7 +895,8 @@ int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
 		dev_err(parent_dev,
 			"device_add() failed for device %s: %d\n",
 			dev_name(&mc_dev->dev), error);
-		goto error_cleanup_dev;
+		put_device(&mc_dev->dev);
+		return error;
 	}
 
 	dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
Signature · 1 line
2.30.2
Reviewed-by on the v2 series
#3 lore raw
In reply to Su Hui
Quoted 4 lines · Su Hui

This patchset fixes two double free problems in fsl_mc_device_add(). One is reported by clang static checker, another is reported by Dan when reviewing the code.

Quoted 8 lines

ps: There is only patch 1 in v1 patch, patch 2 has no v1 version.

v1: https://lore.kernel.org/all/[email protected]/

Su Hui (2):
bus: fsl-mc: Fix the double free in fsl_mc_device_add()
bus: fsl-mc: using put_device() when add_device() failed in

    fsl_mc_device_add()

Thanks!

regards,
dan carpenter