Skip to content

Commit 7f21735

Browse files
Yang Yingliangaxboe
authored andcommitted
rbd: fix possible memory leak in rbd_sysfs_init()
If device_register() returns error in rbd_sysfs_init(), name of kobject which is allocated in dev_set_name() called in device_add() is leaked. As comment of device_add() says, it should call put_device() to drop the reference count that was set in device_initialize() when it fails, so the name can be freed in kobject_cleanup(). Fault injection test can trigger this problem: unreferenced object 0xffff88810173aa78 (size 8): comm "modprobe", pid 247, jiffies 4294714278 (age 31.789s) hex dump (first 8 bytes): 72 62 64 00 81 88 ff ff rbd..... backtrace: [<00000000f58fae56>] __kmalloc_node_track_caller+0x44/0x1b0 [<00000000bdd44fe7>] kstrdup+0x3a/0x70 [<00000000f7844d0b>] kstrdup_const+0x63/0x80 [<000000001b0a0eeb>] kvasprintf_const+0x10b/0x190 [<00000000a47bd894>] kobject_set_name_vargs+0x56/0x150 [<00000000d5edbf18>] dev_set_name+0xab/0xe0 [<00000000f5153e80>] device_add+0x106/0x1f20 Fixes: dfc5606 ("rbd: replace the rbd sysfs interface") Signed-off-by: Yang Yingliang <[email protected]> Reviewed-by: Alex Elder <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 02341a0 commit 7f21735

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

drivers/block/rbd.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7222,8 +7222,10 @@ static int __init rbd_sysfs_init(void)
72227222
int ret;
72237223

72247224
ret = device_register(&rbd_root_dev);
7225-
if (ret < 0)
7225+
if (ret < 0) {
7226+
put_device(&rbd_root_dev);
72267227
return ret;
7228+
}
72277229

72287230
ret = bus_register(&rbd_bus_type);
72297231
if (ret < 0)

0 commit comments

Comments
 (0)