nvmet-tcp: reserve connect header digest and align PDUs
Subsystems (3)
PATCH 1/1 nvmet-tcp: reserve connect header digest and align PDUs
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.
--- drivers/nvme/target/tcp.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/nvme/target/tcp.c +10 -5
@@ -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;
Signature · 1 line
2.56.0