Skip to contentSkip to conversation

block: don't dereference request after flush insertion

PATCH In review Jens Axboe 3 participants 11 messages Oct 17, 2021 – Oct 18, 2021 Block layer

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

PATCH 1/1 block: don't dereference request after flush insertion

We could have a race here, where the request gets freed before we call into blk_mq_run_hw_queue(). If this happens, we cannot rely on the state of the request.

Grab the hardware context before inserting the flush.

---

block/blk-mq.c +2 -1
index 2197cfbf081f..22b30a89bf3a 100644
@@ -2468,9 +2468,10 @@ void blk_mq_submit_bio(struct bio *bio)
 	}
 
 	if (unlikely(is_flush_fua)) {
+		struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
 		/* Bypass scheduler for flush requests */
 		blk_insert_flush(rq);
-		blk_mq_run_hw_queue(rq->mq_hctx, true);
+		blk_mq_run_hw_queue(hctx, true);
 	} else if (plug && (q->nr_hw_queues == 1 ||
 		   blk_mq_is_shared_tags(rq->mq_hctx->flags) ||
 		   q->mq_ops->commit_rqs || !blk_queue_nonrot(q))) {
Signature · 1 line
Jens Axboe
#1 lore raw
Quoted 15 lines · Jens Axboe

We could have a race here, where the request gets freed before we call into blk_mq_run_hw_queue(). If this happens, we cannot rely on the state of the request.

Grab the hardware context before inserting the flush.

---

block/blk-mq.c +0 -0
index 2197cfbf081f..22b30a89bf3a 100644

block/blk-mq.c void blk_mq_submit_bio(struct bio *bio) · line 2474

Quoted 8 lines
block/blk-mq.c +2 -1
@@ -2468,9 +2468,10 @@ void blk_mq_submit_bio(struct bio *bio)
 	}
 
 	if (unlikely(is_flush_fua)) {
+		struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
 		/* Bypass scheduler for flush requests */
 		blk_insert_flush(rq);
-		blk_mq_run_hw_queue(rq->mq_hctx, true);
+		blk_mq_run_hw_queue(hctx, true);

If the request is freed before running queue, the request queue could be released and the hctx may be freed.

Thanks,
Ming

author maintainer
#2 lore raw
Quoted 18 lines · Ming Lei
Quoted 17 lines · Jens Axboe

We could have a race here, where the request gets freed before we call into blk_mq_run_hw_queue(). If this happens, we cannot rely on the state of the request.

Grab the hardware context before inserting the flush.

---

block/blk-mq.c +0 -0
index 2197cfbf081f..22b30a89bf3a 100644
@@ -2468,9 +2468,10 @@ void blk_mq_submit_bio(struct bio *bio)
 	}
 
Quoted 9 lines

block/blk-mq.c void blk_mq_submit_bio(struct bio *bio) · line 2474

Quoted 6 lines
block/blk-mq.c +2 -1
 	if (unlikely(is_flush_fua)) {
+		struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
 		/* Bypass scheduler for flush requests */
 		blk_insert_flush(rq);
-		blk_mq_run_hw_queue(rq->mq_hctx, true);
+		blk_mq_run_hw_queue(hctx, true);

If the request is freed before running queue, the request queue could be released and the hctx may be freed.

No, we still hold a queue enter ref.

Signature · 1 line
Jens Axboe
#3 lore raw
Quoted 29 lines · Jens Axboe
Quoted 18 lines · Ming Lei
Quoted 17 lines · Jens Axboe

We could have a race here, where the request gets freed before we call into blk_mq_run_hw_queue(). If this happens, we cannot rely on the state of the request.

Grab the hardware context before inserting the flush.

---

block/blk-mq.c +0 -0
index 2197cfbf081f..22b30a89bf3a 100644
@@ -2468,9 +2468,10 @@ void blk_mq_submit_bio(struct bio *bio)
 	}
 
Quoted 9 lines

block/blk-mq.c void blk_mq_submit_bio(struct bio *bio) · line 2474

Quoted 6 lines
block/blk-mq.c +2 -1
 	if (unlikely(is_flush_fua)) {
+		struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
 		/* Bypass scheduler for flush requests */
 		blk_insert_flush(rq);
-		blk_mq_run_hw_queue(rq->mq_hctx, true);
+		blk_mq_run_hw_queue(hctx, true);

If the request is freed before running queue, the request queue could be released and the hctx may be freed.

Quoted 1 line

No, we still hold a queue enter ref.

But that one is released when rq is freed since ac7c5675fa45 ("blk-mq: allow blk_mq_make_request to consume the q_usage_counter reference"), isn't it?

Thanks,
Ming

author maintainer
#4 lore raw
Quoted 30 lines · Ming Lei
Quoted 29 lines · Jens Axboe
Quoted 27 lines · Ming Lei
Quoted 15 lines · Jens Axboe

We could have a race here, where the request gets freed before we call into blk_mq_run_hw_queue(). If this happens, we cannot rely on the state of the request.

Grab the hardware context before inserting the flush.

---

block/blk-mq.c +0 -0
index 2197cfbf081f..22b30a89bf3a 100644

block/blk-mq.c void blk_mq_submit_bio(struct bio *bio) · line 2474

Quoted 8 lines
block/blk-mq.c +2 -1
@@ -2468,9 +2468,10 @@ void blk_mq_submit_bio(struct bio *bio)
 	}
 
 	if (unlikely(is_flush_fua)) {
+		struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
 		/* Bypass scheduler for flush requests */
 		blk_insert_flush(rq);
-		blk_mq_run_hw_queue(rq->mq_hctx, true);
+		blk_mq_run_hw_queue(hctx, true);

If the request is freed before running queue, the request queue could be released and the hctx may be freed.

Quoted 5 lines
Quoted 1 line

No, we still hold a queue enter ref.

But that one is released when rq is freed since ac7c5675fa45 ("blk-mq: allow blk_mq_make_request to consume the q_usage_counter reference"), isn't it?

Yes I think you're right, we need to grab an extra ref in the flush case as we're using it after it may potentially have completed. We could probably make it smarter, but little point for just handling a flush.

commit ea0f672e7cc66e7ec12468ff907de6064656b6e7

Author: Jens Axboe <[email protected]>
Date:   Sat Oct 16 07:34:49 2021 -0600

block: grab extra reference for flush insertion

We could have a race here, where the request gets freed before we call into blk_mq_run_hw_queue(). If this happens, we cannot rely on the state of the request, nor can we rely on the queue still being alive.

Grab an extra queue reference before inserting the flush and then running the queue, to ensure that it is still valid.

block/blk-mq.c +10 -1
index 87dc2debedfb..d28423ccfe2b 100644
@@ -2284,9 +2284,18 @@ blk_qc_t blk_mq_submit_bio(struct bio *bio)
 	}
 
 	if (unlikely(is_flush_fua)) {
+		struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
+
+		/*
+		 * Our queue ref may disappears as soon as the flush is
+		 * inserted, grab an extra one.
+		 */
+		percpu_ref_tryget_live(&q->q_usage_counter);
+
 		/* Bypass scheduler for flush requests */
 		blk_insert_flush(rq);
-		blk_mq_run_hw_queue(rq->mq_hctx, true);
+		blk_mq_run_hw_queue(hctx, true);
+		blk_queue_exit(q);
 	} else if (plug && (q->nr_hw_queues == 1 ||
 		   blk_mq_is_shared_tags(rq->mq_hctx->flags) ||
 		   q->mq_ops->commit_rqs || !blk_queue_nonrot(q))) {
Signature · 1 line
Jens Axboe
#6 lore raw
In reply to Ming Lei
Quoted 30 lines · Ming Lei
Quoted 29 lines · Jens Axboe
Quoted 27 lines · Ming Lei
Quoted 15 lines · Jens Axboe

We could have a race here, where the request gets freed before we call into blk_mq_run_hw_queue(). If this happens, we cannot rely on the state of the request.

Grab the hardware context before inserting the flush.

---

block/blk-mq.c +0 -0
index 2197cfbf081f..22b30a89bf3a 100644

block/blk-mq.c void blk_mq_submit_bio(struct bio *bio) · line 2474

Quoted 8 lines
block/blk-mq.c +2 -1
@@ -2468,9 +2468,10 @@ void blk_mq_submit_bio(struct bio *bio)
 	}
 
 	if (unlikely(is_flush_fua)) {
+		struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
 		/* Bypass scheduler for flush requests */
 		blk_insert_flush(rq);
-		blk_mq_run_hw_queue(rq->mq_hctx, true);
+		blk_mq_run_hw_queue(hctx, true);

If the request is freed before running queue, the request queue could be released and the hctx may be freed.

Quoted 5 lines
Quoted 1 line

No, we still hold a queue enter ref.

But that one is released when rq is freed since ac7c5675fa45 ("blk-mq: allow blk_mq_make_request to consume the q_usage_counter reference"), isn't it?

With commit ac7c5675fa45, any reference to hctx after queuing request could lead to UAF in the code path of blk_mq_submit_bio(). Maybe we need to grab two ref in queue enter, and release one after the bio is submitted.

Thanks,
Ming

author maintainer
#7 lore raw
Quoted 33 lines · Ming Lei
Quoted 32 lines · Ming Lei
Quoted 30 lines · Jens Axboe
Quoted 18 lines · Ming Lei
Quoted 17 lines · Jens Axboe

We could have a race here, where the request gets freed before we call into blk_mq_run_hw_queue(). If this happens, we cannot rely on the state of the request.

Grab the hardware context before inserting the flush.

---

block/blk-mq.c +0 -0
index 2197cfbf081f..22b30a89bf3a 100644
@@ -2468,9 +2468,10 @@ void blk_mq_submit_bio(struct bio *bio)
 	}
 
Quoted 9 lines

block/blk-mq.c void blk_mq_submit_bio(struct bio *bio) · line 2474

Quoted 6 lines
block/blk-mq.c +2 -1
 	if (unlikely(is_flush_fua)) {
+		struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
 		/* Bypass scheduler for flush requests */
 		blk_insert_flush(rq);
-		blk_mq_run_hw_queue(rq->mq_hctx, true);
+		blk_mq_run_hw_queue(hctx, true);

If the request is freed before running queue, the request queue could be released and the hctx may be freed.

No, we still hold a queue enter ref.

Quoted 7 lines
Quoted 3 lines

But that one is released when rq is freed since ac7c5675fa45 ("blk-mq: allow blk_mq_make_request to consume the q_usage_counter reference"), isn't it?

With commit ac7c5675fa45, any reference to hctx after queuing request could lead to UAF in the code path of blk_mq_submit_bio(). Maybe we need to grab two ref in queue enter, and release one after the bio is submitted.

I'd rather audit and see if there are any, because extra get+put isn't exactly free.

Signature · 1 line
Jens Axboe
#8 lore raw
Quoted 38 lines · Jens Axboe
Quoted 37 lines · Ming Lei
Quoted 35 lines · Ming Lei
Quoted 29 lines · Jens Axboe
Quoted 18 lines · Ming Lei
Quoted 17 lines · Jens Axboe

We could have a race here, where the request gets freed before we call into blk_mq_run_hw_queue(). If this happens, we cannot rely on the state of the request.

Grab the hardware context before inserting the flush.

---

block/blk-mq.c +0 -0
index 2197cfbf081f..22b30a89bf3a 100644
@@ -2468,9 +2468,10 @@ void blk_mq_submit_bio(struct bio *bio)
 	}
 
Quoted 9 lines

block/blk-mq.c void blk_mq_submit_bio(struct bio *bio) · line 2474

Quoted 6 lines
block/blk-mq.c +2 -1
 	if (unlikely(is_flush_fua)) {
+		struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
 		/* Bypass scheduler for flush requests */
 		blk_insert_flush(rq);
-		blk_mq_run_hw_queue(rq->mq_hctx, true);
+		blk_mq_run_hw_queue(hctx, true);

If the request is freed before running queue, the request queue could be released and the hctx may be freed.

Quoted 1 line

No, we still hold a queue enter ref.

But that one is released when rq is freed since ac7c5675fa45 ("blk-mq: allow blk_mq_make_request to consume the q_usage_counter reference"), isn't it?

Quoted 6 lines
Quoted 3 lines

With commit ac7c5675fa45, any reference to hctx after queuing request could lead to UAF in the code path of blk_mq_submit_bio(). Maybe we need to grab two ref in queue enter, and release one after the bio is submitted.

I'd rather audit and see if there are any, because extra get+put isn't exactly free.

Only direct issue needn't that, looks others do need grab one extra ref if the request has to be queued somewhere before dispatch.

Thanks,
Ming

Reviewed-by on v1 1/1
#9 lore raw
In reply to Jens Axboe
Quoted 16 lines · Jens Axboe

We could have a race here, where the request gets freed before we call into blk_mq_run_hw_queue(). If this happens, we cannot rely on the state of the request.

Grab the hardware context before inserting the flush.

---

block/blk-mq.c +0 -0
index 2197cfbf081f..22b30a89bf3a 100644
@@ -2468,9 +2468,10 @@ void blk_mq_submit_bio(struct bio *bio)
 	}

block/blk-mq.c void blk_mq_submit_bio(struct bio *bio) · line 2477

Quoted 10 lines
block/blk-mq.c +2 -1
 
 	if (unlikely(is_flush_fua)) {
+		struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
 		/* Bypass scheduler for flush requests */
 		blk_insert_flush(rq);
-		blk_mq_run_hw_queue(rq->mq_hctx, true);
+		blk_mq_run_hw_queue(hctx, true);
 	} else if (plug && (q->nr_hw_queues == 1 ||
 		   blk_mq_is_shared_tags(rq->mq_hctx->flags) ||
 		   q->mq_ops->commit_rqs || !blk_queue_nonrot(q))) {

From report in [1], no device close & queue release is involved, and request freeing could be much easier to trigger than queue release, so looks fine:

Fixes f328476e373a ("blk-mq: cleanup blk_mq_submit_bio")

[1] https://lore.kernel.org/linux-block/[email protected]/T/#t

thanks,
Ming

author maintainer
#10 lore raw
Quoted 32 lines · Ming Lei
Quoted 16 lines · Jens Axboe

We could have a race here, where the request gets freed before we call into blk_mq_run_hw_queue(). If this happens, we cannot rely on the state of the request.

Grab the hardware context before inserting the flush.

---

block/blk-mq.c +0 -0
index 2197cfbf081f..22b30a89bf3a 100644
@@ -2468,9 +2468,10 @@ void blk_mq_submit_bio(struct bio *bio)
 	}

block/blk-mq.c void blk_mq_submit_bio(struct bio *bio) · line 2477

Quoted 10 lines
block/blk-mq.c +2 -1
 
 	if (unlikely(is_flush_fua)) {
+		struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
 		/* Bypass scheduler for flush requests */
 		blk_insert_flush(rq);
-		blk_mq_run_hw_queue(rq->mq_hctx, true);
+		blk_mq_run_hw_queue(hctx, true);
 	} else if (plug && (q->nr_hw_queues == 1 ||
 		   blk_mq_is_shared_tags(rq->mq_hctx->flags) ||
 		   q->mq_ops->commit_rqs || !blk_queue_nonrot(q))) {

From report in [1], no device close & queue release is involved, and request freeing could be much easier to trigger than queue release, so looks fine:

Quoted 3 lines
Fixes f328476e373a ("blk-mq: cleanup blk_mq_submit_bio")

Wasn't in the one I sent out, but I do have the fixes as well. Thanks, I'll add your reviewed-by.

Signature · 1 line
Jens Axboe
#5 lore raw
In reply to Jens Axboe

I think this can be done much simpler. The only place in blk_insert_flush that actually needs to run the queue is the case where no flushes are needed, as all the others are handled via the flush state machine and the requeue list. So something like this should work:

block/blk-flush.c +1 -1
index 4201728bf3a5a..1fce6d16e6d3a 100644
@@ -421,7 +421,7 @@ void blk_insert_flush(struct request *rq)
 	 */
 	if ((policy & REQ_FSEQ_DATA) &&
 	    !(policy & (REQ_FSEQ_PREFLUSH | REQ_FSEQ_POSTFLUSH))) {
-		blk_mq_request_bypass_insert(rq, false, false);
+		blk_mq_request_bypass_insert(rq, false, true);
 		return;
 	}
 
block/blk-mq.c +0 -1
index f296edff47246..89a142b61f456 100644
@@ -2314,7 +2314,6 @@ void blk_mq_submit_bio(struct bio *bio)
 	if (unlikely(is_flush_fua)) {
 		/* Bypass scheduler for flush requests */
 		blk_insert_flush(rq);
-		blk_mq_run_hw_queue(rq->mq_hctx, true);
 	} else if (plug && (q->nr_hw_queues == 1 ||
 		   blk_mq_is_shared_tags(rq->mq_hctx->flags) ||
 		   q->mq_ops->commit_rqs || !blk_queue_nonrot(q))) {