mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Weiming Shi <bestswngs@gmail.com>
To: Christoph Hellwig <hch@lst.de>, Sagi Grimberg <sagi@grimberg.me>,
	Chaitanya Kulkarni <kch@nvidia.com>
Cc: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org,
	Xiang Mei <xmei5@asu.edu>, Weiming Shi <bestswngs@gmail.com>,
	co+018500376d4b835e@bugs.sh, stable@vger.kernel.org
Subject: [PATCH] nvmet-tcp: reserve connect header digest and align PDUs
Date: Wed, 25 Nov 2026 08:04:20 -0800	[thread overview]
Message-ID: <20261125160418.1518007-3-bestswngs@gmail.com> (raw)

With CONFIG_NVME_TARGET and CONFIG_NVME_TARGET_TCP enabled and a listening
TCP target port configured by an administrator via configfs,
nvmet_tcp_alloc_queue() calls nvmet_tcp_alloc_cmd(queue, &queue->connect)
on accept before any socket read. At that point queue->hdr_digest is
false and nvmet_tcp_hdgst_len() returns 0, so the response, data, and
R2T PDU buffers on queue->connect are allocated at 24 bytes each.

When a remote peer enables NVME_TCP_HDR_DIGEST_ENABLE in ICReq,
nvmet_tcp_handle_icreq() sets queue->hdr_digest to true without resizing
queue->connect. Subsequent commands handled on queue->connect, including
Connect itself, then write 4 digest bytes past the 24-byte allocation in
nvmet_setup_response_pdu() or nvmet_setup_r2t_pdu(), and
nvmet_try_send_response() or nvmet_try_send_r2t() sends 28 bytes.
Because nvmet_tcp_alloc_cmd() carves PDUs from queue->pf_cache using
page_frag_alloc(), those 4 bytes overwrite the next fragment without
triggering KASAN. Replacing those allocations with kmalloc() exposes the
overflow:

  BUG: KASAN: slab-out-of-bounds in nvmet_setup_response_pdu (./include/linux/unaligned.h:38 drivers/nvme/target/tcp.c:341 drivers/nvme/target/tcp.c:604)
  Write of size 4 at addr ffff8880230f2818 by task kworker/1:0H/26
  Workqueue: nvmet_tcp_wq nvmet_tcp_io_work
  Call Trace:
   kasan_report (mm/kasan/report.c:595)
   nvmet_setup_response_pdu (./include/linux/unaligned.h:38 drivers/nvme/target/tcp.c:341 drivers/nvme/target/tcp.c:604)
   nvmet_tcp_io_work (drivers/nvme/target/tcp.c:641 drivers/nvme/target/tcp.c:866 drivers/nvme/target/tcp.c:914 drivers/nvme/target/tcp.c:1519)
   process_one_work (kernel/workqueue.c:3495)
   worker_thread (kernel/workqueue.c:3578 kernel/workqueue.c:3659)
  Allocated by task 51:
   __kmalloc_noprof (./include/linux/kasan.h:267 mm/slub.c:5531 mm/slub.c:5556)
   nvmet_tcp_alloc_cmd (./include/linux/slab.h:995 drivers/nvme/target/tcp.c:1549)
   nvmet_tcp_accept_work (drivers/nvme/target/tcp.c:2023 drivers/nvme/target/tcp.c:2105)
  The buggy address is located 0 bytes to the right of
   allocated 24-byte region [ffff8880230f2800, ffff8880230f2818)

Reserve NVME_TCP_DIGEST_LENGTH for queue->connect and keep using
nvmet_tcp_hdgst_len(queue) for queue->cmds, which are allocated after
ICReq. Drop the unused digest space from c->cmd_pdu, which only receives
copied commands via memcpy() and never holds a header digest. Round
every page_frag_alloc() size up with ALIGN(..., 8) so 28-byte PDUs do
not misalign following rsp_pdu->cqe or cmd_pdu->cmd allocations that
contain 64-bit fields. Using page_frag_alloc() with aligned sizes avoids
page_frag_alloc_align(), whose fourth argument changed from align_mask
to align in v6.9 and which did not exist before v5.12.

Fixes: 872d26a391da ("nvmet-tcp: add NVMe over TCP target driver")
Reported-by: <co+018500376d4b835e@bugs.sh>
Assisted-by: LLM
Cc: stable@vger.kernel.org
Signed-off-by: Weiming Shi <bestswngs@gmail.com>
---
 drivers/nvme/target/tcp.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/drivers/nvme/target/tcp.c b/drivers/nvme/target/tcp.c
index 8a2e0e056131..cfc0e462d81f 100644
--- a/drivers/nvme/target/tcp.c
+++ b/drivers/nvme/target/tcp.c
@@ -1532,30 +1532,35 @@ static void nvmet_tcp_io_work(struct work_struct *w)
 static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue,
 		struct nvmet_tcp_cmd *c)
 {
-	u8 hdgst = nvmet_tcp_hdgst_len(queue);
+	u8 hdgst = (c == &queue->connect) ?
+		NVME_TCP_DIGEST_LENGTH : nvmet_tcp_hdgst_len(queue);
 
 	c->queue = queue;
 	c->req.port = queue->nport;
 
 	c->cmd_pdu = page_frag_alloc(&queue->pf_cache,
-			sizeof(*c->cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
+			ALIGN(sizeof(*c->cmd_pdu), 8),
+			GFP_KERNEL | __GFP_ZERO);
 	if (!c->cmd_pdu)
 		return -ENOMEM;
 	c->req.cmd = &c->cmd_pdu->cmd;
 
 	c->rsp_pdu = page_frag_alloc(&queue->pf_cache,
-			sizeof(*c->rsp_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
+			ALIGN(sizeof(*c->rsp_pdu) + hdgst, 8),
+			GFP_KERNEL | __GFP_ZERO);
 	if (!c->rsp_pdu)
 		goto out_free_cmd;
 	c->req.cqe = &c->rsp_pdu->cqe;
 
 	c->data_pdu = page_frag_alloc(&queue->pf_cache,
-			sizeof(*c->data_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
+			ALIGN(sizeof(*c->data_pdu) + hdgst, 8),
+			GFP_KERNEL | __GFP_ZERO);
 	if (!c->data_pdu)
 		goto out_free_rsp;
 
 	c->r2t_pdu = page_frag_alloc(&queue->pf_cache,
-			sizeof(*c->r2t_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
+			ALIGN(sizeof(*c->r2t_pdu) + hdgst, 8),
+			GFP_KERNEL | __GFP_ZERO);
 	if (!c->r2t_pdu)
 		goto out_free_data;
 
-- 
2.56.0


                 reply	other threads:[~2026-11-25 16:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125160418.1518007-3-bestswngs@gmail.com \
    --to=bestswngs@gmail.com \
    --cc=co+018500376d4b835e@bugs.sh \
    --cc=hch@lst.de \
    --cc=kch@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sagi@grimberg.me \
    --cc=stable@vger.kernel.org \
    --cc=xmei5@asu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®