Skip to content

Commit a8aec14

Browse files
committed
nvdimm/bus: Fix potential use after free in asynchronous initialization
Dingisoul with KASAN reports a use after free if device_add() fails in nd_async_device_register(). Commit b6eae0f ("libnvdimm: Hold reference on parent while scheduling async init") correctly added a reference on the parent device to be held until asynchronous initialization was complete. However, if device_add() results in an allocation failure the ref count of the device drops to 0 prior to the parent pointer being accessed. Thus resulting in use after free. The bug bot AI correctly identified the fix. Save a reference to the parent pointer to be used to drop the parent reference regardless of the outcome of device_add(). Reported-by: Dingisoul <[email protected]> Closes: http://lore.kernel.org/[email protected] Fixes: b6eae0f ("libnvdimm: Hold reference on parent while scheduling async init") Cc: [email protected] Reviewed-by: Dave Jiang <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Ira Weiny <[email protected]>
1 parent 1f318b9 commit a8aec14

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

drivers/nvdimm/bus.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,14 +486,15 @@ EXPORT_SYMBOL_GPL(nd_synchronize);
486486
static void nd_async_device_register(void *d, async_cookie_t cookie)
487487
{
488488
struct device *dev = d;
489+
struct device *parent = dev->parent;
489490

490491
if (device_add(dev) != 0) {
491492
dev_err(dev, "%s: failed\n", __func__);
492493
put_device(dev);
493494
}
494495
put_device(dev);
495-
if (dev->parent)
496-
put_device(dev->parent);
496+
if (parent)
497+
put_device(parent);
497498
}
498499

499500
static void nd_async_device_unregister(void *d, async_cookie_t cookie)

0 commit comments

Comments
 (0)