Skip to content

Commit 153fcd5

Browse files
Ming Leiaxboe
Ming Lei
authored andcommitted
block: brd: associate with queue until adding disk
brd_free() may be called in failure path on one brd instance which disk isn't added yet, so release handler of gendisk may free the associated request_queue early and causes the following use-after-free[1]. This patch fixes this issue by associating gendisk with request_queue just before adding disk. [1] KASAN: use-after-free Read in del_timer_syncNon-volatile memory driver v1.3 Linux agpgart interface v0.103 [drm] Initialized vgem 1.0.0 20120112 for virtual device on minor 0 usbcore: registered new interface driver udl ================================================================== BUG: KASAN: use-after-free in __lock_acquire+0x36d9/0x4c20 kernel/locking/lockdep.c:3218 Read of size 8 at addr ffff8801d1b6b540 by task swapper/0/1 CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.19.0+ #88 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: __dump_stack lib/dump_stack.c:77 [inline] dump_stack+0x244/0x39d lib/dump_stack.c:113 print_address_description.cold.7+0x9/0x1ff mm/kasan/report.c:256 kasan_report_error mm/kasan/report.c:354 [inline] kasan_report.cold.8+0x242/0x309 mm/kasan/report.c:412 __asan_report_load8_noabort+0x14/0x20 mm/kasan/report.c:433 __lock_acquire+0x36d9/0x4c20 kernel/locking/lockdep.c:3218 lock_acquire+0x1ed/0x520 kernel/locking/lockdep.c:3844 del_timer_sync+0xb7/0x270 kernel/time/timer.c:1283 blk_cleanup_queue+0x413/0x710 block/blk-core.c:809 brd_free+0x5d/0x71 drivers/block/brd.c:422 brd_init+0x2eb/0x393 drivers/block/brd.c:518 do_one_initcall+0x145/0x957 init/main.c:890 do_initcall_level init/main.c:958 [inline] do_initcalls init/main.c:966 [inline] do_basic_setup init/main.c:984 [inline] kernel_init_freeable+0x5c6/0x6b9 init/main.c:1148 kernel_init+0x11/0x1ae init/main.c:1068 ret_from_fork+0x3a/0x50 arch/x86/entry/entry_64.S:350 Reported-by: [email protected] Signed-off-by: Ming Lei <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent c57cdf7 commit 153fcd5

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

drivers/block/brd.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,14 @@ static struct brd_device *brd_alloc(int i)
396396
disk->first_minor = i * max_part;
397397
disk->fops = &brd_fops;
398398
disk->private_data = brd;
399-
disk->queue = brd->brd_queue;
400399
disk->flags = GENHD_FL_EXT_DEVT;
401400
sprintf(disk->disk_name, "ram%d", i);
402401
set_capacity(disk, rd_size * 2);
403-
disk->queue->backing_dev_info->capabilities |= BDI_CAP_SYNCHRONOUS_IO;
402+
brd->brd_queue->backing_dev_info->capabilities |= BDI_CAP_SYNCHRONOUS_IO;
404403

405404
/* Tell the block layer that this is not a rotational device */
406-
blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
407-
blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
405+
blk_queue_flag_set(QUEUE_FLAG_NONROT, brd->brd_queue);
406+
blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, brd->brd_queue);
408407

409408
return brd;
410409

@@ -436,6 +435,7 @@ static struct brd_device *brd_init_one(int i, bool *new)
436435

437436
brd = brd_alloc(i);
438437
if (brd) {
438+
brd->brd_disk->queue = brd->brd_queue;
439439
add_disk(brd->brd_disk);
440440
list_add_tail(&brd->brd_list, &brd_devices);
441441
}
@@ -503,8 +503,14 @@ static int __init brd_init(void)
503503

504504
/* point of no return */
505505

506-
list_for_each_entry(brd, &brd_devices, brd_list)
506+
list_for_each_entry(brd, &brd_devices, brd_list) {
507+
/*
508+
* associate with queue just before adding disk for
509+
* avoiding to mess up failure path
510+
*/
511+
brd->brd_disk->queue = brd->brd_queue;
507512
add_disk(brd->brd_disk);
513+
}
508514

509515
blk_register_region(MKDEV(RAMDISK_MAJOR, 0), 1UL << MINORBITS,
510516
THIS_MODULE, brd_probe, NULL, NULL);

0 commit comments

Comments
 (0)