Skip to content

Commit b425e50

Browse files
Bart Van Asscheaxboe
authored andcommitted
block: Avoid that blk_exit_rl() triggers a use-after-free
Since the introduction of .init_rq_fn() and .exit_rq_fn() it is essential that the memory allocated for struct request_queue stays around until all blk_exit_rl() calls have finished. Hence make blk_init_rl() take a reference on struct request_queue. This patch fixes the following crash: general protection fault: 0000 [#2] SMP CPU: 3 PID: 28 Comm: ksoftirqd/3 Tainted: G D 4.12.0-rc2-dbg+ #2 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.0.0-prebuilt.qemu-project.org 04/01/2014 task: ffff88013a108040 task.stack: ffffc9000071c000 RIP: 0010:free_request_size+0x1a/0x30 RSP: 0018:ffffc9000071fd38 EFLAGS: 00010202 RAX: 6b6b6b6b6b6b6b6b RBX: ffff880067362a88 RCX: 0000000000000003 RDX: ffff880067464178 RSI: ffff880067362a88 RDI: ffff880135ea4418 RBP: ffffc9000071fd40 R08: 0000000000000000 R09: 0000000100180009 R10: ffffc9000071fd38 R11: ffffffff81110800 R12: ffff88006752d3d8 R13: ffff88006752d3d8 R14: ffff88013a108040 R15: 000000000000000a FS: 0000000000000000(0000) GS:ffff88013fd80000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007fa8ec1edb00 CR3: 0000000138ee8000 CR4: 00000000001406e0 Call Trace: mempool_destroy.part.10+0x21/0x40 mempool_destroy+0xe/0x10 blk_exit_rl+0x12/0x20 blkg_free+0x4d/0xa0 __blkg_release_rcu+0x59/0x170 rcu_process_callbacks+0x260/0x4e0 __do_softirq+0x116/0x250 smpboot_thread_fn+0x123/0x1e0 kthread+0x109/0x140 ret_from_fork+0x31/0x40 Fixes: commit e9c787e ("scsi: allocate scsi_cmnd structures as part of struct request") Signed-off-by: Bart Van Assche <bart.vanassche@sandisk.com> Acked-by: Tejun Heo <tj@kernel.org> Reviewed-by: Hannes Reinecke <hare@suse.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Cc: Jan Kara <jack@suse.cz> Cc: <stable@vger.kernel.org> # v4.11+ Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent 5be6b75 commit b425e50

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

block/blk-cgroup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static void blkg_free(struct blkcg_gq *blkg)
7474
blkcg_policy[i]->pd_free_fn(blkg->pd[i]);
7575

7676
if (blkg->blkcg != &blkcg_root)
77-
blk_exit_rl(&blkg->rl);
77+
blk_exit_rl(blkg->q, &blkg->rl);
7878

7979
blkg_rwstat_exit(&blkg->stat_ios);
8080
blkg_rwstat_exit(&blkg->stat_bytes);

block/blk-core.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,13 +648,19 @@ int blk_init_rl(struct request_list *rl, struct request_queue *q,
648648
if (!rl->rq_pool)
649649
return -ENOMEM;
650650

651+
if (rl != &q->root_rl)
652+
WARN_ON_ONCE(!blk_get_queue(q));
653+
651654
return 0;
652655
}
653656

654-
void blk_exit_rl(struct request_list *rl)
657+
void blk_exit_rl(struct request_queue *q, struct request_list *rl)
655658
{
656-
if (rl->rq_pool)
659+
if (rl->rq_pool) {
657660
mempool_destroy(rl->rq_pool);
661+
if (rl != &q->root_rl)
662+
blk_put_queue(q);
663+
}
658664
}
659665

660666
struct request_queue *blk_alloc_queue(gfp_t gfp_mask)

block/blk-sysfs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ static void blk_release_queue(struct kobject *kobj)
809809

810810
blk_free_queue_stats(q->stats);
811811

812-
blk_exit_rl(&q->root_rl);
812+
blk_exit_rl(q, &q->root_rl);
813813

814814
if (q->queue_tags)
815815
__blk_queue_free_tags(q);

block/blk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void blk_free_flush_queue(struct blk_flush_queue *q);
5959

6060
int blk_init_rl(struct request_list *rl, struct request_queue *q,
6161
gfp_t gfp_mask);
62-
void blk_exit_rl(struct request_list *rl);
62+
void blk_exit_rl(struct request_queue *q, struct request_list *rl);
6363
void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
6464
struct bio *bio);
6565
void blk_queue_bypass_start(struct request_queue *q);

0 commit comments

Comments
 (0)