Skip to content

Commit 259bf92

Browse files
Yang Erkungregkh
Yang Erkun
authored andcommitted
brd: defer automatic disk creation until module initialization succeeds
[ Upstream commit 826cc42 ] My colleague Wupeng found the following problems during fault injection: BUG: unable to handle page fault for address: fffffbfff809d073 PGD 6e648067 P4D 123ec8067 PUD 123ec4067 PMD 100e38067 PTE 0 Oops: Oops: 0000 [#1] PREEMPT SMP KASAN NOPTI CPU: 5 UID: 0 PID: 755 Comm: modprobe Not tainted 6.12.0-rc3+ #17 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.1-2.fc37 04/01/2014 RIP: 0010:__asan_load8+0x4c/0xa0 ... Call Trace: <TASK> blkdev_put_whole+0x41/0x70 bdev_release+0x1a3/0x250 blkdev_release+0x11/0x20 __fput+0x1d7/0x4a0 task_work_run+0xfc/0x180 syscall_exit_to_user_mode+0x1de/0x1f0 do_syscall_64+0x6b/0x170 entry_SYSCALL_64_after_hwframe+0x76/0x7e loop_init() is calling loop_add() after __register_blkdev() succeeds and is ignoring disk_add() failure from loop_add(), for loop_add() failure is not fatal and successfully created disks are already visible to bdev_open(). brd_init() is currently calling brd_alloc() before __register_blkdev() succeeds and is releasing successfully created disks when brd_init() returns an error. This can cause UAF for the latter two case: case 1: T1: modprobe brd brd_init brd_alloc(0) // success add_disk disk_scan_partitions bdev_file_open_by_dev // alloc file fput // won't free until back to userspace brd_alloc(1) // failed since mem alloc error inject // error path for modprobe will release code segment // back to userspace __fput blkdev_release bdev_release blkdev_put_whole bdev->bd_disk->fops->release // fops is freed now, UAF! case 2: T1: T2: modprobe brd brd_init brd_alloc(0) // success open(/dev/ram0) brd_alloc(1) // fail // error path for modprobe close(/dev/ram0) ... /* UAF! */ bdev->bd_disk->fops->release Fix this problem by following what loop_init() does. Besides, reintroduce brd_devices_mutex to help serialize modifications to brd_list. Fixes: 7f9b348 ("brd: convert to blk_alloc_disk/blk_cleanup_disk") Reported-by: Wupeng Ma <[email protected]> Signed-off-by: Yang Erkun <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 2185802 commit 259bf92

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

drivers/block/brd.c

+44-22
Original file line numberDiff line numberDiff line change
@@ -368,23 +368,50 @@ __setup("ramdisk_size=", ramdisk_size);
368368
* (should share code eventually).
369369
*/
370370
static LIST_HEAD(brd_devices);
371+
static DEFINE_MUTEX(brd_devices_mutex);
371372
static struct dentry *brd_debugfs_dir;
372373

374+
static struct brd_device *brd_find_or_alloc_device(int i)
375+
{
376+
struct brd_device *brd;
377+
378+
mutex_lock(&brd_devices_mutex);
379+
list_for_each_entry(brd, &brd_devices, brd_list) {
380+
if (brd->brd_number == i) {
381+
mutex_unlock(&brd_devices_mutex);
382+
return ERR_PTR(-EEXIST);
383+
}
384+
}
385+
386+
brd = kzalloc(sizeof(*brd), GFP_KERNEL);
387+
if (!brd) {
388+
mutex_unlock(&brd_devices_mutex);
389+
return ERR_PTR(-ENOMEM);
390+
}
391+
brd->brd_number = i;
392+
list_add_tail(&brd->brd_list, &brd_devices);
393+
mutex_unlock(&brd_devices_mutex);
394+
return brd;
395+
}
396+
397+
static void brd_free_device(struct brd_device *brd)
398+
{
399+
mutex_lock(&brd_devices_mutex);
400+
list_del(&brd->brd_list);
401+
mutex_unlock(&brd_devices_mutex);
402+
kfree(brd);
403+
}
404+
373405
static int brd_alloc(int i)
374406
{
375407
struct brd_device *brd;
376408
struct gendisk *disk;
377409
char buf[DISK_NAME_LEN];
378410
int err = -ENOMEM;
379411

380-
list_for_each_entry(brd, &brd_devices, brd_list)
381-
if (brd->brd_number == i)
382-
return -EEXIST;
383-
brd = kzalloc(sizeof(*brd), GFP_KERNEL);
384-
if (!brd)
385-
return -ENOMEM;
386-
brd->brd_number = i;
387-
list_add_tail(&brd->brd_list, &brd_devices);
412+
brd = brd_find_or_alloc_device(i);
413+
if (IS_ERR(brd))
414+
return PTR_ERR(brd);
388415

389416
spin_lock_init(&brd->brd_lock);
390417
INIT_RADIX_TREE(&brd->brd_pages, GFP_ATOMIC);
@@ -428,8 +455,7 @@ static int brd_alloc(int i)
428455
out_cleanup_disk:
429456
put_disk(disk);
430457
out_free_dev:
431-
list_del(&brd->brd_list);
432-
kfree(brd);
458+
brd_free_device(brd);
433459
return err;
434460
}
435461

@@ -448,8 +474,7 @@ static void brd_cleanup(void)
448474
del_gendisk(brd->brd_disk);
449475
put_disk(brd->brd_disk);
450476
brd_free_pages(brd);
451-
list_del(&brd->brd_list);
452-
kfree(brd);
477+
brd_free_device(brd);
453478
}
454479
}
455480

@@ -476,16 +501,6 @@ static int __init brd_init(void)
476501
{
477502
int err, i;
478503

479-
brd_check_and_reset_par();
480-
481-
brd_debugfs_dir = debugfs_create_dir("ramdisk_pages", NULL);
482-
483-
for (i = 0; i < rd_nr; i++) {
484-
err = brd_alloc(i);
485-
if (err)
486-
goto out_free;
487-
}
488-
489504
/*
490505
* brd module now has a feature to instantiate underlying device
491506
* structure on-demand, provided that there is an access dev node.
@@ -501,11 +516,18 @@ static int __init brd_init(void)
501516
* dynamically.
502517
*/
503518

519+
brd_check_and_reset_par();
520+
521+
brd_debugfs_dir = debugfs_create_dir("ramdisk_pages", NULL);
522+
504523
if (__register_blkdev(RAMDISK_MAJOR, "ramdisk", brd_probe)) {
505524
err = -EIO;
506525
goto out_free;
507526
}
508527

528+
for (i = 0; i < rd_nr; i++)
529+
brd_alloc(i);
530+
509531
pr_info("brd: module loaded\n");
510532
return 0;
511533

0 commit comments

Comments
 (0)