Skip to content

Commit 88cf30c

Browse files
Yang Erkunvijay-suman
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]> (cherry picked from commit 41219c147df8bbd6591f59af5d695fb6c9a1cbff) Signed-off-by: Vijayendra Suman <[email protected]>
1 parent 5f81278 commit 88cf30c

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
@@ -362,23 +362,50 @@ __setup("ramdisk_size=", ramdisk_size);
362362
* (should share code eventually).
363363
*/
364364
static LIST_HEAD(brd_devices);
365+
static DEFINE_MUTEX(brd_devices_mutex);
365366
static struct dentry *brd_debugfs_dir;
366367

368+
static struct brd_device *brd_find_or_alloc_device(int i)
369+
{
370+
struct brd_device *brd;
371+
372+
mutex_lock(&brd_devices_mutex);
373+
list_for_each_entry(brd, &brd_devices, brd_list) {
374+
if (brd->brd_number == i) {
375+
mutex_unlock(&brd_devices_mutex);
376+
return ERR_PTR(-EEXIST);
377+
}
378+
}
379+
380+
brd = kzalloc(sizeof(*brd), GFP_KERNEL);
381+
if (!brd) {
382+
mutex_unlock(&brd_devices_mutex);
383+
return ERR_PTR(-ENOMEM);
384+
}
385+
brd->brd_number = i;
386+
list_add_tail(&brd->brd_list, &brd_devices);
387+
mutex_unlock(&brd_devices_mutex);
388+
return brd;
389+
}
390+
391+
static void brd_free_device(struct brd_device *brd)
392+
{
393+
mutex_lock(&brd_devices_mutex);
394+
list_del(&brd->brd_list);
395+
mutex_unlock(&brd_devices_mutex);
396+
kfree(brd);
397+
}
398+
367399
static int brd_alloc(int i)
368400
{
369401
struct brd_device *brd;
370402
struct gendisk *disk;
371403
char buf[DISK_NAME_LEN];
372404
int err = -ENOMEM;
373405

374-
list_for_each_entry(brd, &brd_devices, brd_list)
375-
if (brd->brd_number == i)
376-
return -EEXIST;
377-
brd = kzalloc(sizeof(*brd), GFP_KERNEL);
378-
if (!brd)
379-
return -ENOMEM;
380-
brd->brd_number = i;
381-
list_add_tail(&brd->brd_list, &brd_devices);
406+
brd = brd_find_or_alloc_device(i);
407+
if (IS_ERR(brd))
408+
return PTR_ERR(brd);
382409

383410
spin_lock_init(&brd->brd_lock);
384411
INIT_RADIX_TREE(&brd->brd_pages, GFP_ATOMIC);
@@ -423,8 +450,7 @@ static int brd_alloc(int i)
423450
out_cleanup_disk:
424451
blk_cleanup_disk(disk);
425452
out_free_dev:
426-
list_del(&brd->brd_list);
427-
kfree(brd);
453+
brd_free_device(brd);
428454
return err;
429455
}
430456

@@ -443,8 +469,7 @@ static void brd_cleanup(void)
443469
del_gendisk(brd->brd_disk);
444470
blk_cleanup_disk(brd->brd_disk);
445471
brd_free_pages(brd);
446-
list_del(&brd->brd_list);
447-
kfree(brd);
472+
brd_free_device(brd);
448473
}
449474
}
450475

@@ -471,16 +496,6 @@ static int __init brd_init(void)
471496
{
472497
int err, i;
473498

474-
brd_check_and_reset_par();
475-
476-
brd_debugfs_dir = debugfs_create_dir("ramdisk_pages", NULL);
477-
478-
for (i = 0; i < rd_nr; i++) {
479-
err = brd_alloc(i);
480-
if (err)
481-
goto out_free;
482-
}
483-
484499
/*
485500
* brd module now has a feature to instantiate underlying device
486501
* structure on-demand, provided that there is an access dev node.
@@ -496,11 +511,18 @@ static int __init brd_init(void)
496511
* dynamically.
497512
*/
498513

514+
brd_check_and_reset_par();
515+
516+
brd_debugfs_dir = debugfs_create_dir("ramdisk_pages", NULL);
517+
499518
if (__register_blkdev(RAMDISK_MAJOR, "ramdisk", brd_probe)) {
500519
err = -EIO;
501520
goto out_free;
502521
}
503522

523+
for (i = 0; i < rd_nr; i++)
524+
brd_alloc(i);
525+
504526
pr_info("brd: module loaded\n");
505527
return 0;
506528

0 commit comments

Comments
 (0)