Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mcb: fix error handling in mcb_alloc_bus()

There are two bugs:
1) If ida_simple_get() fails then this code calls put_device(carrier)
but we haven't yet called get_device(carrier) and probably that
leads to a use after free.
2) After device_initialize() then we need to use put_device() to
release the bus. This will free the internal resources tied to the
device and call mcb_free_bus() which will free the rest.

Fixes: 5d9e2ab9fea4 ("mcb: Implement bus->dev.release callback")
Fixes: 18d288198099 ("mcb: Correctly initialize the bus's device")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Johannes Thumshirn <jth@kernel.org>
Link: https://lore.kernel.org/r/32e160cf6864ce77f9d62948338e24db9fd8ead9.1630931319.git.johannes.thumshirn@wdc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Dan Carpenter and committed by
Greg Kroah-Hartman
25a14332 8d753db5

+6 -6
+6 -6
drivers/mcb/mcb-core.c
··· 275 275 276 276 bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL); 277 277 if (bus_nr < 0) { 278 - rc = bus_nr; 279 - goto err_free; 278 + kfree(bus); 279 + return ERR_PTR(bus_nr); 280 280 } 281 281 282 282 bus->bus_nr = bus_nr; ··· 291 291 dev_set_name(&bus->dev, "mcb:%d", bus_nr); 292 292 rc = device_add(&bus->dev); 293 293 if (rc) 294 - goto err_free; 294 + goto err_put; 295 295 296 296 return bus; 297 - err_free: 298 - put_device(carrier); 299 - kfree(bus); 297 + 298 + err_put: 299 + put_device(&bus->dev); 300 300 return ERR_PTR(rc); 301 301 } 302 302 EXPORT_SYMBOL_NS_GPL(mcb_alloc_bus, MCB);