Skip to contentSkip to conversation

rnbd-clt: fix use-after-free on device sysfs creation failure

PATCH In review Shuangpeng Bai 2 participants 2 messages Aug 2 – Nov 20

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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.

Fixes 581cf833cac4 ("block: rnbd: add .release to rnbd_dev_ktype")
---
 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
index eb485e4c12e2..9f5d30659f3d 100644
@@ -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
index 4d6725a0035e..5fe5c7e259b7 100644
@@ -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
Acked-by Md Haris Iqbal on v1 1/1
#1 lore raw
Quoted 17 lines · Shuangpeng Bai

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.

Quoted 3 lines
Fixes 581cf833cac4 ("block: rnbd: add .release to rnbd_dev_ktype")

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
index eb485e4c12e2..9f5d30659f3d 100644
@@ -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
index 4d6725a0035e..5fe5c7e259b7 100644
@@ -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