Skip to contentSkip to conversation

nbd: fix race between timeout and normal completion

PATCH Applied Ming Lei 3 participants 3 messages Aug 30, 2024 – Sep 2, 2024

Subsystems (2)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 nbd: fix race between timeout and normal completion

If request timetout is handled by nbd_requeue_cmd(), normal completion has to be stopped for avoiding to complete this requeued request, other use-after-free can be triggered.

Fix the race by clearing NBD_CMD_INFLIGHT in nbd_requeue_cmd(), meantime make sure that cmd->lock is grabbed for clearing the flag and the requeue.

Cc Josef Bacik <[email protected]>
Cc Yu Kuai <[email protected]>
Fixes 2895f1831e91 ("nbd: don't clear 'NBD_CMD_INFLIGHT' flag if request is not completed")
---
 drivers/block/nbd.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/block/nbd.c +12 -1
index 41a90150b501..69b9851b6798 100644
@@ -181,6 +181,17 @@ static void nbd_requeue_cmd(struct nbd_cmd *cmd)
 {
 	struct request *req = blk_mq_rq_from_pdu(cmd);
 
+	lockdep_assert_held(&cmd->lock);
+
+	/*
+	 * Clear INFLIGHT flag so that this cmd won't be completed in
+	 * normal completion path
+	 *
+	 * INFLIGHT flag will be set when the cmd is queued to nbd next
+	 * time.
+	 */
+	__clear_bit(NBD_CMD_INFLIGHT, &cmd->flags);
+
 	if (!test_and_set_bit(NBD_CMD_REQUEUED, &cmd->flags))
 		blk_mq_requeue_request(req, true);
 }
@@ -488,8 +499,8 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req)
 					nbd_mark_nsock_dead(nbd, nsock, 1);
 				mutex_unlock(&nsock->tx_lock);
 			}
-			mutex_unlock(&cmd->lock);
 			nbd_requeue_cmd(cmd);
+			mutex_unlock(&cmd->lock);
 			nbd_config_put(nbd);
 			return BLK_EH_DONE;
 		}
Signature · 1 line
2.44.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Ming Lei

If request timetout is handled by nbd_requeue_cmd(), normal completion has to be stopped for avoiding to complete this requeued request, other use-after-free can be triggered.

Fix the race by clearing NBD_CMD_INFLIGHT in nbd_requeue_cmd(), meantime make sure that cmd->lock is grabbed for clearing the flag and the requeue.

Quoted 5 lines
Cc Josef Bacik <[email protected]>
Cc Yu Kuai <[email protected]>
Fixes 2895f1831e91 ("nbd: don't clear 'NBD_CMD_INFLIGHT' flag if request is not completed")

---

LGTM

Thanks!

Quoted 36 lines
  drivers/block/nbd.c | 13 ++++++++++++-
  1 file changed, 12 insertions(+), 1 deletion(-)
drivers/block/nbd.c +12 -1
index 41a90150b501..69b9851b6798 100644
@@ -181,6 +181,17 @@ static void nbd_requeue_cmd(struct nbd_cmd *cmd)
  {
  	struct request *req = blk_mq_rq_from_pdu(cmd);
 
+	lockdep_assert_held(&cmd->lock);
+
+	/*
+	 * Clear INFLIGHT flag so that this cmd won't be completed in
+	 * normal completion path
+	 *
+	 * INFLIGHT flag will be set when the cmd is queued to nbd next
+	 * time.
+	 */
+	__clear_bit(NBD_CMD_INFLIGHT, &cmd->flags);
+
  	if (!test_and_set_bit(NBD_CMD_REQUEUED, &cmd->flags))
  		blk_mq_requeue_request(req, true);
  }
@@ -488,8 +499,8 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req)
  					nbd_mark_nsock_dead(nbd, nsock, 1);
  				mutex_unlock(&nsock->tx_lock);
  			}
-			mutex_unlock(&cmd->lock);
  			nbd_requeue_cmd(cmd);
+			mutex_unlock(&cmd->lock);
  			nbd_config_put(nbd);
  			return BLK_EH_DONE;
  		}
maintainer Applied
#2 lore raw
In reply to Ming Lei
Quoted 9 lines · Ming Lei

If request timetout is handled by nbd_requeue_cmd(), normal completion has to be stopped for avoiding to complete this requeued request, other use-after-free can be triggered.

Fix the race by clearing NBD_CMD_INFLIGHT in nbd_requeue_cmd(), meantime make sure that cmd->lock is grabbed for clearing the flag and the requeue.

[...]

Applied, thanks!

[1/1] nbd: fix race between timeout and normal completion

      commit: c9ea57c91f03bcad415e1a20113bdb2077bcf990

Best regards,

Signature · 1 line
Jens Axboe