Skip to contentSkip to conversation

nvme-tcp: reject C2HData for a non-read command

PATCH Superseded Chuyf26 4 participants 7 messages Aug 21 – Sep 8 NVMe

lore
Share & tools
lore

Chuyf26

author
#0 lore raw

PATCH 1/1 nvme-tcp: reject C2HData for a non-read command

A malicious or buggy controller can send C2HData PDUs in response to a WRITE command. nvme_tcp_handle_c2h_data() accepts them as long as the request has payload bytes and a current bio, which is true for writes, so the receive path ends up copying the incoming data into the request's iterator. That iterator was initialized from rq_data_dir(rq) in nvme_tcp_init_iter(), i.e. ITER_SOURCE for a write, and _copy_to_iter() refuses to copy into a source iterator: it triggers its WARN_ON_ONCE() and returns 0, the short copy fails with -EFAULT, and the connection is torn down into error recovery. The write never
completes: every reconnect reissues it and a malicious target answers with C2HData again, keeping the controller in a permanent reset/reconnect loop. With panic_on_warn this is an outright crash.

C2HData carries data transferred from the controller to the host and is only valid for commands that read data from the device; write data travels host to controller in H2CData PDUs solicited by R2T. This was reproduced against a target modified to reply to a WRITE with C2HData:
the host logs

    WARNING: CPU: 0 PID: ... at lib/iov_iter.c _copy_to_iter
    Workqueue: nvme_tcp_wq nvme_tcp_io_work [nvme_tcp]

and the controller cycles through reset/reconnect without the write ever completing.

Reject C2HData for any non-read command. This is the symmetric case of commit 6efbc52237fa ("nvme-tcp: fix host memory disclosure on R2T for a read command"), which added the mirror-image direction check to nvme_tcp_handle_r2t().

Fixes 3f2304f8c6d6 ("nvme-tcp: add NVMe over TCP host driver")
Assisted-by abaci:qwen3.8-max
---
 drivers/nvme/host/tcp.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/nvme/host/tcp.c +7 -0
@@ -685,6 +685,13 @@
 	}
 
 	req = blk_mq_rq_to_pdu(rq);
+	if (unlikely(rq_data_dir(rq) != READ)) {
+		dev_err(queue->ctrl->ctrl.device,
+			"req %d unexpected c2hdata for a non-read command\n",
+			rq->tag);
+		return -EPROTO;
+	}
+
 	if (!blk_rq_payload_bytes(rq) || !req->curr_bio || !req->data_len) {
 		dev_err(queue->ctrl->ctrl.device,
 			"queue %d tag %#x unexpected data\n",
Signature · 1 line
2.43.5
maintainer
#3 lore raw
In reply to Chuyf26
Quoted 43 lines · On 21/08/2026 12:28, Chuyf26 wrote:

A malicious or buggy controller can send C2HData PDUs in response to a WRITE command. nvme_tcp_handle_c2h_data() accepts them as long as the request has payload bytes and a current bio, which is true for writes, so the receive path ends up copying the incoming data into the request's iterator. That iterator was initialized from rq_data_dir(rq) in nvme_tcp_init_iter(), i.e. ITER_SOURCE for a write, and _copy_to_iter() refuses to copy into a source iterator: it triggers its WARN_ON_ONCE() and returns 0, the short copy fails with -EFAULT, and the connection is torn down into error recovery. The write never
completes: every reconnect reissues it and a malicious target answers with C2HData again, keeping the controller in a permanent reset/reconnect loop. With panic_on_warn this is an outright crash.

C2HData carries data transferred from the controller to the host and is only valid for commands that read data from the device; write data travels host to controller in H2CData PDUs solicited by R2T. This was reproduced against a target modified to reply to a WRITE with C2HData:
the host logs

     WARNING: CPU: 0 PID: ... at lib/iov_iter.c _copy_to_iter
     Workqueue: nvme_tcp_wq nvme_tcp_io_work [nvme_tcp]

and the controller cycles through reset/reconnect without the write ever completing.

Reject C2HData for any non-read command. This is the symmetric case of commit 6efbc52237fa ("nvme-tcp: fix host memory disclosure on R2T for a read command"), which added the mirror-image direction check to nvme_tcp_handle_r2t().

Fixes 3f2304f8c6d6 ("nvme-tcp: add NVMe over TCP host driver")
Assisted-by abaci:qwen3.8-max
---
  drivers/nvme/host/tcp.c | 7 +++++++
  1 file changed, 7 insertions(+)

drivers/nvme/host/tcp.c line 694

Quoted 10 lines
drivers/nvme/host/tcp.c +7 -0
@@ -685,6 +685,13 @@
  	}
 
  	req = blk_mq_rq_to_pdu(rq);
+	if (unlikely(rq_data_dir(rq) != READ)) {
+		dev_err(queue->ctrl->ctrl.device,
+			"req %d unexpected c2hdata for a non-read command\n",
+			rq->tag);
+		return -EPROTO;
+	}
+

It'd be better to check this before the req assignment.

Other than that, this looks good.

author
#4 lore raw

PATCH v2 1/1 nvme-tcp: reject C2HData for a non-read command

A malicious or buggy controller can send C2HData PDUs in response to a WRITE command. nvme_tcp_handle_c2h_data() accepts them as long as the request has payload bytes and a current bio, which is true for writes, so the receive path ends up copying the incoming data into the request's iterator. That iterator was initialized from rq_data_dir(rq) in nvme_tcp_init_iter(), i.e. ITER_SOURCE for a write, and _copy_to_iter() refuses to copy into a source iterator: it triggers its WARN_ON_ONCE() and returns 0, the short copy fails with -EFAULT, and the connection is torn down into error recovery. The write never
completes: every reconnect reissues it and a malicious target answers with C2HData again, keeping the controller in a permanent reset/reconnect loop. With panic_on_warn this is an outright crash.

C2HData carries data transferred from the controller to the host and is only valid for commands that read data from the device; write data travels host to controller in H2CData PDUs solicited by R2T. This was reproduced against a target modified to reply to a WRITE with C2HData:
the host logs

    WARNING: CPU: 0 PID: ... at lib/iov_iter.c _copy_to_iter
    Workqueue: nvme_tcp_wq nvme_tcp_io_work [nvme_tcp]

and the controller cycles through reset/reconnect without the write ever completing.

Reject C2HData for any non-read command. This is the symmetric case of commit 6efbc52237fa ("nvme-tcp: fix host memory disclosure on R2T for a read command"), which added the mirror-image direction check to nvme_tcp_handle_r2t().

Fixes 3f2304f8c6d6 ("nvme-tcp: add NVMe over TCP host driver")
Assisted-by abaci:qwen3.8-max

---
v2: move the direction check before the req assignment (Sagi).
Keep -EPROTO (Sagi: "EPROTO is more appropriate").

 drivers/nvme/host/tcp.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/nvme/host/tcp.c +7 -0
index 5fda966..60d7c37 100644
@@ -684,6 +684,13 @@ static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue,
 		return -ENOENT;
 	}
 
+	if (unlikely(rq_data_dir(rq) != READ)) {
+		dev_err(queue->ctrl->ctrl.device,
+			"req %d unexpected c2hdata for a non-read command\n",
+			rq->tag);
+		return -EPROTO;
+	}
+
 	req = blk_mq_rq_to_pdu(rq);
 	if (!blk_rq_payload_bytes(rq) || !req->curr_bio || !req->data_len) {
 		dev_err(queue->ctrl->ctrl.device,
Signature · 1 line
2.43.5
maintainer
#6 lore raw
In reply to Yifei Chu
Quoted 19 lines · Yifei Chu

A malicious or buggy controller can send C2HData PDUs in response to a WRITE command. nvme_tcp_handle_c2h_data() accepts them as long as the request has payload bytes and a current bio, which is true for writes, so the receive path ends up copying the incoming data into the request's iterator. That iterator was initialized from rq_data_dir(rq) in nvme_tcp_init_iter(), i.e. ITER_SOURCE for a write, and _copy_to_iter() refuses to copy into a source iterator: it triggers its WARN_ON_ONCE() and returns 0, the short copy fails with -EFAULT, and the connection is torn down into error recovery. The write never
completes: every reconnect reissues it and a malicious target answers with C2HData again, keeping the controller in a permanent reset/reconnect loop. With panic_on_warn this is an outright crash.

C2HData carries data transferred from the controller to the host and is only valid for commands that read data from the device; write data travels host to controller in H2CData PDUs solicited by R2T. This was reproduced against a target modified to reply to a WRITE with C2HData:
the host logs

Quoted 5 lines
    WARNING: CPU: 0 PID: ... at lib/iov_iter.c _copy_to_iter
    Workqueue: nvme_tcp_wq nvme_tcp_io_work [nvme_tcp]

and the controller cycles through reset/reconnect without the write ever completing.

This is already closed:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit?id=f83af377c148f6ad94b41c0e8313f12adf45e1c1