Skip to content

Commit

Permalink
block: fix error unwinding in device_add_disk
Browse files Browse the repository at this point in the history
One device_add is called disk->ev will be freed by disk_release, so we
should free it twice.  Fix this by allocating disk->ev after device_add
so that the extra local unwinding can be removed entirely.

Based on an earlier patch from Tetsuo Handa.

Reported-by: syzbot <[email protected]>
Tested-by: syzbot <[email protected]>
Fixes: 83cbce9 ("block: add error handling for device_add_disk / add_disk")
Signed-off-by: Christoph Hellwig <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
Christoph Hellwig authored and axboe committed Dec 21, 2021
1 parent 37e11c3 commit 99d8690
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions block/genhd.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,6 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
disk->first_minor = ret;
}

ret = disk_alloc_events(disk);
if (ret)
goto out_free_ext_minor;

/* delay uevents, until we scanned partition table */
dev_set_uevent_suppress(ddev, 1);

Expand All @@ -456,7 +452,12 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
ddev->devt = MKDEV(disk->major, disk->first_minor);
ret = device_add(ddev);
if (ret)
goto out_disk_release_events;
goto out_free_ext_minor;

ret = disk_alloc_events(disk);
if (ret)
goto out_device_del;

if (!sysfs_deprecated) {
ret = sysfs_create_link(block_depr, &ddev->kobj,
kobject_name(&ddev->kobj));
Expand Down Expand Up @@ -538,8 +539,6 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
sysfs_remove_link(block_depr, dev_name(ddev));
out_device_del:
device_del(ddev);
out_disk_release_events:
disk_release_events(disk);
out_free_ext_minor:
if (disk->major == BLOCK_EXT_MAJOR)
blk_free_ext_minor(disk->first_minor);
Expand Down

0 comments on commit 99d8690

Please sign in to comment.