Skip to content

Commit b041392

Browse files
bvanasschegregkh
authored andcommitted
blk-mq: Fix a use-after-free
commit 45a9c9d upstream. blk-mq users are allowed to free the memory request_queue.tag_set points at after blk_cleanup_queue() has finished but before blk_release_queue() has started. This can happen e.g. in the SCSI core. The SCSI core namely embeds the tag_set structure in a SCSI host structure. The SCSI host structure is freed by scsi_host_dev_release(). This function is called after blk_cleanup_queue() finished but can be called before blk_release_queue(). This means that it is not safe to access request_queue.tag_set from inside blk_release_queue(). Hence remove the blk_sync_queue() call from blk_release_queue(). This call is not necessary - outstanding requests must have finished before blk_release_queue() is called. Additionally, move the blk_mq_free_queue() call from blk_release_queue() to blk_cleanup_queue() to avoid that struct request_queue.tag_set gets accessed after it has been freed. This patch avoids that the following kernel oops can be triggered when deleting a SCSI host for which scsi-mq was enabled: Call Trace: [<ffffffff8109a7c4>] lock_acquire+0xc4/0x270 [<ffffffff814ce111>] mutex_lock_nested+0x61/0x380 [<ffffffff812575f0>] blk_mq_free_queue+0x30/0x180 [<ffffffff8124d654>] blk_release_queue+0x84/0xd0 [<ffffffff8126c29b>] kobject_cleanup+0x7b/0x1a0 [<ffffffff8126c140>] kobject_put+0x30/0x70 [<ffffffff81245895>] blk_put_queue+0x15/0x20 [<ffffffff8125c409>] disk_release+0x99/0xd0 [<ffffffff8133d056>] device_release+0x36/0xb0 [<ffffffff8126c29b>] kobject_cleanup+0x7b/0x1a0 [<ffffffff8126c140>] kobject_put+0x30/0x70 [<ffffffff8125a78a>] put_disk+0x1a/0x20 [<ffffffff811d4cb5>] __blkdev_put+0x135/0x1b0 [<ffffffff811d56a0>] blkdev_put+0x50/0x160 [<ffffffff81199eb4>] kill_block_super+0x44/0x70 [<ffffffff8119a2a4>] deactivate_locked_super+0x44/0x60 [<ffffffff8119a87e>] deactivate_super+0x4e/0x70 [<ffffffff811b9833>] cleanup_mnt+0x43/0x90 [<ffffffff811b98d2>] __cleanup_mnt+0x12/0x20 [<ffffffff8107252c>] task_work_run+0xac/0xe0 [<ffffffff81002c01>] do_notify_resume+0x61/0xa0 [<ffffffff814d2c58>] int_signal+0x12/0x17 Signed-off-by: Bart Van Assche <bvanassche@acm.org> Cc: Christoph Hellwig <hch@lst.de> Cc: Robert Elliott <elliott@hp.com> Cc: Ming Lei <ming.lei@canonical.com> Cc: Alexander Gordeev <agordeev@redhat.com> Signed-off-by: Jens Axboe <axboe@fb.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 00de3a6 commit b041392

2 files changed

Lines changed: 7 additions & 8 deletions

File tree

‎block/blk-core.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,9 @@ void blk_cleanup_queue(struct request_queue *q)
525525
del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
526526
blk_sync_queue(q);
527527

528+
if (q->mq_ops)
529+
blk_mq_free_queue(q);
530+
528531
spin_lock_irq(lock);
529532
if (q->queue_lock != &q->__queue_lock)
530533
q->queue_lock = &q->__queue_lock;

‎block/blk-sysfs.c‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -492,17 +492,15 @@ static void blk_free_queue_rcu(struct rcu_head *rcu_head)
492492
* Currently, its primary task it to free all the &struct request
493493
* structures that were allocated to the queue and the queue itself.
494494
*
495-
* Caveat:
496-
* Hopefully the low level driver will have finished any
497-
* outstanding requests first...
495+
* Note:
496+
* The low level driver must have finished any outstanding requests first
497+
* via blk_cleanup_queue().
498498
**/
499499
static void blk_release_queue(struct kobject *kobj)
500500
{
501501
struct request_queue *q =
502502
container_of(kobj, struct request_queue, kobj);
503503

504-
blk_sync_queue(q);
505-
506504
blkcg_exit_queue(q);
507505

508506
if (q->elevator) {
@@ -517,9 +515,7 @@ static void blk_release_queue(struct kobject *kobj)
517515
if (q->queue_tags)
518516
__blk_queue_free_tags(q);
519517

520-
if (q->mq_ops)
521-
blk_mq_free_queue(q);
522-
else
518+
if (!q->mq_ops)
523519
blk_free_flush_queue(q->fq);
524520

525521
blk_trace_shutdown(q);

0 commit comments

Comments
 (0)