Skip to content

Commit 52fe2b5

Browse files
Zenghui Yugregkh
Zenghui Yu
authored andcommitted
bcma: Fix memory leak for internally-handled cores
[ Upstream commit b63aed3ff195130fef12e0af590f4838cf0201d8 ] kmemleak reported that dev_name() of internally-handled cores were leaked on driver unbinding. Let's use device_initialize() to take refcounts for them and put_device() to properly free the related stuff. While looking at it, there's another potential issue for those which should be *registered* into driver core. If device_register() failed, we put device once and freed bcma_device structures. In bcma_unregister_cores(), they're treated as unregistered and we hit both UAF and double-free. That smells not good and has also been fixed now. Fixes: ab54bc8 ("bcma: fill core details for every device") Signed-off-by: Zenghui Yu <[email protected]> Signed-off-by: Kalle Valo <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sasha Levin <[email protected]>
1 parent 71d2372 commit 52fe2b5

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

drivers/bcma/main.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ EXPORT_SYMBOL(bcma_core_irq);
236236

237237
void bcma_prepare_core(struct bcma_bus *bus, struct bcma_device *core)
238238
{
239+
device_initialize(&core->dev);
239240
core->dev.release = bcma_release_core_dev;
240241
core->dev.bus = &bcma_bus_type;
241242
dev_set_name(&core->dev, "bcma%d:%d", bus->num, core->core_index);
@@ -299,11 +300,10 @@ static void bcma_register_core(struct bcma_bus *bus, struct bcma_device *core)
299300
{
300301
int err;
301302

302-
err = device_register(&core->dev);
303+
err = device_add(&core->dev);
303304
if (err) {
304305
bcma_err(bus, "Could not register dev for core 0x%03X\n",
305306
core->id.id);
306-
put_device(&core->dev);
307307
return;
308308
}
309309
core->dev_registered = true;
@@ -394,7 +394,7 @@ void bcma_unregister_cores(struct bcma_bus *bus)
394394
/* Now noone uses internally-handled cores, we can free them */
395395
list_for_each_entry_safe(core, tmp, &bus->cores, list) {
396396
list_del(&core->list);
397-
kfree(core);
397+
put_device(&core->dev);
398398
}
399399
}
400400

0 commit comments

Comments
 (0)