Skip to contentSkip to conversation

block: fix use-after-free of q->q_usage_counter

PATCH Applied Ming Lei 2 participants 2 messages Dec 15, 2022 Block layer

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 block: fix use-after-free of q->q_usage_counter

For blk-mq, queue release handler is usually called into after blk_mq_freeze_queue_wait() returns. However, q_usage_counter->release() handler may not be started yet at that time, so cause user-after-free.

Fix the issue by moving percpu_ref_exit() into blk_free_queue_rcu() since ->release() is called with rcu read lock held, since it is concluded that the race should be covered in caller per discussion from the two links.

---
 block/blk-core.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
block/blk-core.c +5 -4
index 3866b6c4cd88..9321767470dc 100644
@@ -254,14 +254,15 @@ EXPORT_SYMBOL_GPL(blk_clear_pm_only);
 
 static void blk_free_queue_rcu(struct rcu_head *rcu_head)
 {
-	kmem_cache_free(blk_requestq_cachep,
-			container_of(rcu_head, struct request_queue, rcu_head));
+	struct request_queue *q = container_of(rcu_head,
+			struct request_queue, rcu_head);
+
+	percpu_ref_exit(&q->q_usage_counter);
+	kmem_cache_free(blk_requestq_cachep, q);
 }
 
 static void blk_free_queue(struct request_queue *q)
 {
-	percpu_ref_exit(&q->q_usage_counter);
-
 	if (q->poll_stat)
 		blk_stat_remove_callback(q, q->poll_cb);
 	blk_stat_free_callback(q->poll_cb);
Signature · 1 line
2.31.1
maintainer Applied
#1 lore raw
Quoted 10 lines · Ming Lei

For blk-mq, queue release handler is usually called into after blk_mq_freeze_queue_wait() returns. However, q_usage_counter->release() handler may not be started yet at that time, so cause user-after-free.

Fix the issue by moving percpu_ref_exit() into blk_free_queue_rcu() since ->release() is called with rcu read lock held, since it is concluded that the race should be covered in caller per discussion from the two links.

[...]

Applied, thanks!

[1/1] block: fix use-after-free of q->q_usage_counter

      commit: d36a9ea5e7766961e753ee38d4c331bbe6ef659b

Best regards,

Signature · 1 line
Jens Axboe