rnbd-clt: fix use-after-free on device sysfs creation failure
Subsystems (3)
PATCH 1/1 rnbd-clt: fix use-after-free on device sysfs creation failure
rnbd_clt_add_dev_kobj() initializes dev->kobj and adds it below the gendisk kobject after rnbd_clt_map_device() has returned a live rnbd_clt_dev to rnbd_clt_map_device_store().
If kobject_init_and_add() fails, the helper currently calls kobject_put() on the embedded kobject. That put can drop the initial kobject reference and run rnbd_dev_release(), freeing dev. The caller then continues through the normal error path and calls rnbd_clt_unmap_device(dev, true, NULL), dereferencing the freed device.
Leave the initialized kobject alive until the normal device teardown has completed and rnbd_clt_put_dev() drops the final device reference. Since kobject_init_and_add() failure can leave the kobject initialized but not present in sysfs, only call kobject_del() from rnbd_destroy_sysfs() when state_in_sysfs is set.
--- drivers/block/rnbd/rnbd-clt-sysfs.c | 2 +- drivers/block/rnbd/rnbd-clt.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/block/rnbd/rnbd-clt-sysfs.c +1 -1
@@ -498,7 +498,7 @@ static int rnbd_clt_add_dev_kobj(struct rnbd_clt_dev *dev)if (ret) {rnbd_clt_err (dev,"Failed to create device sysfs dir, err: %d\n" , ret); -kobject_put (&dev->kobj); +return ret; }kobject_uevent (gd_kobj,KOBJ_ONLINE );
drivers/block/rnbd/rnbd-clt.c +1 -1
@@ -1658,7 +1658,7 @@ static void rnbd_destroy_sysfs(struct rnbd_clt_dev *dev, const struct attribute *sysfs_self) {rnbd_clt_remove_dev_symlink (dev); -if (dev->kobj.state_initialized) { +if (dev->kobj.state_in_sysfs) {if (sysfs_self)/* To avoid deadlock firstly remove itself */ sysfs_remove_file_self (&dev->kobj, sysfs_self);
Signature · 1 line
2.43.0
Quoted 17 lines · Shuangpeng Bai
rnbd_clt_add_dev_kobj()initializesdev->kobjand adds it below the gendisk kobject afterrnbd_clt_map_device()has returned a livernbd_clt_devtornbd_clt_map_device_store().If
kobject_init_and_add()fails, the helper currently callskobject_put()on the embedded kobject. That put can drop the initial kobject reference and runrnbd_dev_release(), freeing dev. The caller then continues through the normal error path and callsrnbd_clt_unmap_device(dev, true, NULL), dereferencing the freed device.Leave the initialized kobject alive until the normal device teardown has completed and
rnbd_clt_put_dev()drops the final device reference. Sincekobject_init_and_add()failure can leave the kobject initialized but not present in sysfs, only callkobject_del()fromrnbd_destroy_sysfs()whenstate_in_sysfsis set.
Quoted 3 lines
Fixes 581cf833cac4 ("block: rnbd: add .release to rnbd_dev_ktype")Signed-off-by Shuangpeng Bai <[email protected]>
LGTM!
Quoted 34 lines
--- drivers/block/rnbd/rnbd-clt-sysfs.c | 2 +- drivers/block/rnbd/rnbd-clt.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)drivers/block/rnbd/rnbd-clt-sysfs.c +1
-1@@ -498,7 +498,7 @@ static int rnbd_clt_add_dev_kobj(struct rnbd_clt_dev *dev)if (ret) {rnbd_clt_err (dev,"Failed to create device sysfs dir, err: %d\n" , ret); -kobject_put (&dev->kobj); +return ret; }kobject_uevent (gd_kobj,KOBJ_ONLINE );drivers/block/rnbd/rnbd-clt.c +1
-2@@ -1658,7 +1658,7 @@ static void rnbd_destroy_sysfs(struct rnbd_clt_dev *dev, const struct attribute *sysfs_self) {rnbd_clt_remove_dev_symlink (dev); -if (dev->kobj.state_initialized) { +if (dev->kobj.state_in_sysfs) {if (sysfs_self)/* To avoid deadlock firstly remove itself */ sysfs_remove_file_self (&dev->kobj, sysfs_self); --2.43.0
Signature · 2 lines
Regards -Haris