nvmet-tcp: add bounds checks in nvmet_tcp_build_pdu_iovec
Subsystems (2)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/1 nvmet-tcp: add bounds checks in nvmet_tcp_build_pdu_iovec
nvmet_tcp_build_pdu_iovec() could walk past cmd->req.sg when a PDU length or offset exceeds sg_cnt and then use bogus sg->length/offset values, leading to _copy_to_iter() GPF/KASAN. Guard sg_idx, remaining entries, and sg->length/offset before building the bvec.
--- drivers/nvme/target/tcp.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
drivers/nvme/target/tcp.c +17 -0
@@ -349,11 +349,14 @@ static void nvmet_tcp_free_cmd_buffers(struct nvmet_tcp_cmd *cmd) cmd->req.sg =NULL ; } +static void nvmet_tcp_fatal_error (struct nvmet_tcp_queue *queue); +static void nvmet_tcp_build_pdu_iovec (struct nvmet_tcp_cmd *cmd) {struct bio_vec *iov = cmd->iov;struct scatterlist *sg;u32 length, offset, sg_offset; +unsigned int sg_remaining;int nr_pages; length = cmd->pdu_len;
@@ -361,9 +364,22 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) offset = cmd->rbytes_done; cmd->sg_idx = offset /PAGE_SIZE ; sg_offset = offset %PAGE_SIZE ; +if (!cmd->req.sg_cnt || cmd->sg_idx >= cmd->req.sg_cnt) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + } sg = &cmd->req.sg[cmd->sg_idx]; + sg_remaining = cmd->req.sg_cnt - cmd->sg_idx;while (length) { +if (!sg_remaining) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + } +if (!sg->length || sg->length <= sg_offset) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + }u32 iov_len =min_t (u32, length, sg->length - sg_offset); bvec_set_page(iov, sg_page(sg), iov_len,
@@ -371,6 +387,7 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) length -= iov_len; sg =sg_next (sg); + sg_remaining--; iov++; sg_offset =0 ; }
Signature · 1 line
2.43.0
[ 104.528480] BUG: unable to handle page fault for address: ffff888002b42660 [ 104.528773] #PF: supervisor write access in kernel mode [ 104.528938] #PF: error_code(0x0003) - permissions violation [ 104.529151] PGD 3a01067 P4D 3a01067 PUD 3a02067 PMD 4f24063 PTE 8000000002b42121 [ 104.529476] Oops: Oops: 0003 [#1] SMP NOPTI [ 104.529817] CPU: 0 UID: 0 PID: 63 Comm: kworker/0:1H Not tainted 6.19.0-rc7 #1 PREEMPT(voluntary [ 104.530090] Hardware name: QEMU Ubuntu 24.04 PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.34 [ 104.530440] Workqueue: nvmet_tcp_wq nvmet_tcp_io_work [ 104.530849] RIP: 0010:_copy_to_iter+0x27e/0x7a0 [ 104.531077] Code: 01 48 2b 05 a4 2a 2f 01 48 c1 f8 06 48 c1 e0 0c 48 03 05 a5 2a 2f 01 48 01 f0 7 [ 104.531620] RSP: 0018:ffffc9000021bba8 EFLAGS: 00000246 [ 104.531805] RAX: ffff888002b42660 RBX: 0000000000000008 RCX: 305c782f706d742f [ 104.532019] RDX: 0000000000000008 RSI: ffff88800515c078 RDI: 00000000000009a0 [ 104.532243] RBP: ffff888005070218 R08: 0000000000000000 R09: ffff888004ed5300 [ 104.532428] R10: 0000000000001000 R11: ffff888003e032e0 R12: ffff88800515c078 [ 104.532615] R13: 0000000000000000 R14: 0000000000000008 R15: 0000000000004080 [ 104.532821] FS: 0000000000000000(0000) GS:ffff8880fa4a4000(0000) knlGS:0000000000000000 [ 104.533048] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 104.533222] CR2: ffff888002b42660 CR3: 0000000004fec000 CR4: 00000000000006f0 [ 104.533483] Call Trace: [ 104.533957] <TASK> [ 104.534195] ? __pfx_simple_copy_to_iter+0x10/0x10 [ 104.534381] __skb_datagram_iter+0x1a7/0x2e0 [ 104.534541] ? __pfx_simple_copy_to_iter+0x10/0x10 [ 104.534671] skb_copy_datagram_iter+0x2f/0x90 [ 104.534811] tcp_recvmsg_locked+0x784/0x9d0 [ 104.534936] tcp_recvmsg+0x81/0x1e0 [ 104.535045] inet_recvmsg+0x50/0x130 [ 104.535166] ? security_socket_recvmsg+0x43/0x60 [ 104.535294] sock_recvmsg+0xa1/0xc0 [ 104.535381] nvmet_tcp_io_work+0xdb/0x720 [ 104.535476] process_one_work+0x15b/0x380 [ 104.535570] worker_thread+0x2a5/0x3c0 [ 104.535653] ? __pfx_worker_thread+0x10/0x10 [ 104.535747] kthread+0xf6/0x1f0 [ 104.535839] ? __pfx_kthread+0x10/0x10 [ 104.535949] ? __pfx_kthread+0x10/0x10 [ 104.536060] ret_from_fork+0x131/0x190 [ 104.536182] ? __pfx_kthread+0x10/0x10 [ 104.536296] ret_from_fork_asm+0x1a/0x30 [ 104.536451] </TASK> [ 104.536545] Modules linked in: [ 104.536764] CR2: ffff888002b42660 [ 104.537046] ---[ end trace 0000000000000000 ]--- [ 104.537291] RIP: 0010:_copy_to_iter+0x27e/0x7a0 [ 104.537412] Code: 01 48 2b 05 a4 2a 2f 01 48 c1 f8 06 48 c1 e0 0c 48 03 05 a5 2a 2f 01 48 01 f0 7 [ 104.537848] RSP: 0018:ffffc9000021bba8 EFLAGS: 00000246 [ 104.538005] RAX: ffff888002b42660 RBX: 0000000000000008 RCX: 305c782f706d742f [ 104.538229] RDX: 0000000000000008 RSI: ffff88800515c078 RDI: 00000000000009a0 [ 104.538416] RBP: ffff888005070218 R08: 0000000000000000 R09: ffff888004ed5300 [ 104.538617] R10: 0000000000001000 R11: ffff888003e032e0 R12: ffff88800515c078 [ 104.538825] R13: 0000000000000000 R14: 0000000000000008 R15: 0000000000004080 [ 104.538999] FS: 0000000000000000(0000) GS:ffff8880fa4a4000(0000) knlGS:0000000000000000 [ 104.539209] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 104.539340] CR2: ffff888002b42660 CR3: 0000000004fec000 CR4: 00000000000006f0 [ 104.539553] note: kworker/0:1H[63] exited with irqs disabled [ 104.540201] kworker/0:1H (63) used greatest stack depth: 12976 bytes left
~ $ uname -a
Linux (none) 6.19.0-rc7 #1 SMP PREEMPT_DYNAMIC Fri Jan 30 09:36:44 KST 2026 x86_64 GNU/Linux
Quoted 67 lines · YunJe Shin
nvmet_tcp_build_pdu_iovec()could walk pastcmd->req.sgwhen a PDU length or offset exceedssg_cntand then use bogus sg->length/offset values, leading to_copy_to_iter()GPF/KASAN. Guardsg_idx, remaining entries, and sg->length/offset before building the bvec.Fixes 872d26a391da ("nvmet-tcp: add NVMe over TCP target driver")Signed-off-by YunJe Shin <[email protected]>Reviewed-by Sagi Grimberg <[email protected]>Reviewed-by Joonkyo Jung <[email protected]>--- drivers/nvme/target/tcp.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/nvme/target/tcp.c +17
-1@@ -349,11 +349,14 @@ static void nvmet_tcp_free_cmd_buffers(struct nvmet_tcp_cmd *cmd) cmd->req.sg =NULL ; } +static void nvmet_tcp_fatal_error (struct nvmet_tcp_queue *queue); +static void nvmet_tcp_build_pdu_iovec (struct nvmet_tcp_cmd *cmd) {struct bio_vec *iov = cmd->iov;struct scatterlist *sg;u32 length, offset, sg_offset; +unsigned int sg_remaining;int nr_pages; length = cmd->pdu_len;@@ -361,9 +364,22 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) offset = cmd->rbytes_done; cmd->sg_idx = offset /PAGE_SIZE ; sg_offset = offset %PAGE_SIZE ; +if (!cmd->req.sg_cnt || cmd->sg_idx >= cmd->req.sg_cnt) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + } sg = &cmd->req.sg[cmd->sg_idx]; + sg_remaining = cmd->req.sg_cnt - cmd->sg_idx;while (length) { +if (!sg_remaining) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + } +if (!sg->length || sg->length <= sg_offset) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + }u32 iov_len =min_t (u32, length, sg->length - sg_offset); bvec_set_page(iov, sg_page(sg), iov_len,@@ -371,6 +387,7 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) length -= iov_len; sg =sg_next (sg); + sg_remaining--; iov++; sg_offset =0 ; } --2.43.0
The Reviewed-by tags mentioned in the earlier thread were obtained through private discussion, which Keith likely wasn't aware of. Could the maintainers please re-review this patch?
Quoted 137 lines · yunje shin
[ 104.528480] BUG: unable to handle page fault for address: ffff888002b42660 [ 104.528773] #PF: supervisor write access in kernel mode [ 104.528938] #PF: error_code(0x0003) - permissions violation [ 104.529151] PGD 3a01067 P4D 3a01067 PUD 3a02067 PMD 4f24063 PTE 8000000002b42121 [ 104.529476] Oops: Oops: 0003 [#1] SMP NOPTI [ 104.529817] CPU: 0 UID: 0 PID: 63 Comm: kworker/0:1H Not tainted 6.19.0-rc7 #1 PREEMPT(voluntary [ 104.530090] Hardware name: QEMU Ubuntu 24.04 PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.34 [ 104.530440] Workqueue: nvmet_tcp_wq nvmet_tcp_io_work [ 104.530849] RIP: 0010:_copy_to_iter+0x27e/0x7a0 [ 104.531077] Code: 01 48 2b 05 a4 2a 2f 01 48 c1 f8 06 48 c1 e0 0c 48 03 05 a5 2a 2f 01 48 01 f0 7 [ 104.531620] RSP: 0018:ffffc9000021bba8 EFLAGS: 00000246 [ 104.531805] RAX: ffff888002b42660 RBX: 0000000000000008 RCX: 305c782f706d742f [ 104.532019] RDX: 0000000000000008 RSI: ffff88800515c078 RDI: 00000000000009a0 [ 104.532243] RBP: ffff888005070218 R08: 0000000000000000 R09: ffff888004ed5300 [ 104.532428] R10: 0000000000001000 R11: ffff888003e032e0 R12: ffff88800515c078 [ 104.532615] R13: 0000000000000000 R14: 0000000000000008 R15: 0000000000004080 [ 104.532821] FS: 0000000000000000(0000) GS:ffff8880fa4a4000(0000) knlGS:0000000000000000 [ 104.533048] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 104.533222] CR2: ffff888002b42660 CR3: 0000000004fec000 CR4: 00000000000006f0 [ 104.533483] Call Trace: [ 104.533957] <TASK> [ 104.534195] ? __pfx_simple_copy_to_iter+0x10/0x10 [ 104.534381] __skb_datagram_iter+0x1a7/0x2e0 [ 104.534541] ? __pfx_simple_copy_to_iter+0x10/0x10 [ 104.534671] skb_copy_datagram_iter+0x2f/0x90 [ 104.534811] tcp_recvmsg_locked+0x784/0x9d0 [ 104.534936] tcp_recvmsg+0x81/0x1e0 [ 104.535045] inet_recvmsg+0x50/0x130 [ 104.535166] ? security_socket_recvmsg+0x43/0x60 [ 104.535294] sock_recvmsg+0xa1/0xc0 [ 104.535381] nvmet_tcp_io_work+0xdb/0x720 [ 104.535476] process_one_work+0x15b/0x380 [ 104.535570] worker_thread+0x2a5/0x3c0 [ 104.535653] ? __pfx_worker_thread+0x10/0x10 [ 104.535747] kthread+0xf6/0x1f0 [ 104.535839] ? __pfx_kthread+0x10/0x10 [ 104.535949] ? __pfx_kthread+0x10/0x10 [ 104.536060] ret_from_fork+0x131/0x190 [ 104.536182] ? __pfx_kthread+0x10/0x10 [ 104.536296] ret_from_fork_asm+0x1a/0x30 [ 104.536451] </TASK> [ 104.536545] Modules linked in: [ 104.536764] CR2: ffff888002b42660 [ 104.537046] ---[ end trace 0000000000000000 ]--- [ 104.537291] RIP: 0010:_copy_to_iter+0x27e/0x7a0 [ 104.537412] Code: 01 48 2b 05 a4 2a 2f 01 48 c1 f8 06 48 c1 e0 0c 48 03 05 a5 2a 2f 01 48 01 f0 7 [ 104.537848] RSP: 0018:ffffc9000021bba8 EFLAGS: 00000246 [ 104.538005] RAX: ffff888002b42660 RBX: 0000000000000008 RCX: 305c782f706d742f [ 104.538229] RDX: 0000000000000008 RSI: ffff88800515c078 RDI: 00000000000009a0 [ 104.538416] RBP: ffff888005070218 R08: 0000000000000000 R09: ffff888004ed5300 [ 104.538617] R10: 0000000000001000 R11: ffff888003e032e0 R12: ffff88800515c078 [ 104.538825] R13: 0000000000000000 R14: 0000000000000008 R15: 0000000000004080 [ 104.538999] FS: 0000000000000000(0000) GS:ffff8880fa4a4000(0000) knlGS:0000000000000000 [ 104.539209] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 104.539340] CR2: ffff888002b42660 CR3: 0000000004fec000 CR4: 00000000000006f0 [ 104.539553] note: kworker/0:1H[63] exited with irqs disabled [ 104.540201] kworker/0:1H (63) used greatest stack depth: 12976 bytes left~ $ uname -a
Linux (none) 6.19.0-rc7 #1 SMPPREEMPT_DYNAMICFri Jan 30 09:36:44 KST 2026x86_64GNU/LinuxQuoted 67 lines · YunJe Shin
nvmet_tcp_build_pdu_iovec()could walk pastcmd->req.sgwhen a PDU length or offset exceedssg_cntand then use bogus sg->length/offset values, leading to_copy_to_iter()GPF/KASAN. Guardsg_idx, remaining entries, and sg->length/offset before building the bvec.Fixes 872d26a391da ("nvmet-tcp: add NVMe over TCP target driver")Signed-off-by YunJe Shin <[email protected]>Reviewed-by Sagi Grimberg <[email protected]>Reviewed-by Joonkyo Jung <[email protected]>--- drivers/nvme/target/tcp.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/nvme/target/tcp.c +17
-1@@ -349,11 +349,14 @@ static void nvmet_tcp_free_cmd_buffers(struct nvmet_tcp_cmd *cmd) cmd->req.sg =NULL ; } +static void nvmet_tcp_fatal_error (struct nvmet_tcp_queue *queue); +static void nvmet_tcp_build_pdu_iovec (struct nvmet_tcp_cmd *cmd) {struct bio_vec *iov = cmd->iov;struct scatterlist *sg;u32 length, offset, sg_offset; +unsigned int sg_remaining;int nr_pages; length = cmd->pdu_len;@@ -361,9 +364,22 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) offset = cmd->rbytes_done; cmd->sg_idx = offset /PAGE_SIZE ; sg_offset = offset %PAGE_SIZE ; +if (!cmd->req.sg_cnt || cmd->sg_idx >= cmd->req.sg_cnt) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + } sg = &cmd->req.sg[cmd->sg_idx]; + sg_remaining = cmd->req.sg_cnt - cmd->sg_idx;while (length) { +if (!sg_remaining) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + } +if (!sg->length || sg->length <= sg_offset) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + }u32 iov_len =min_t (u32, length, sg->length - sg_offset); bvec_set_page(iov, sg_page(sg), iov_len,@@ -371,6 +387,7 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) length -= iov_len; sg =sg_next (sg); + sg_remaining--; iov++; sg_offset =0 ; } --2.43.0
The Reviewed-by tags mentioned in the earlier thread were obtained through private discussion, which kbusch likely wasn't aware of. Could the maintainers please re-review this patch?
Quoted 143 lines · yunje shin
The Reviewed-by tags mentioned in the earlier thread were obtained through private discussion, which Keith likely wasn't aware of. Could the maintainers please re-review this patch?
Quoted 137 lines · yunje shin
[ 104.528480] BUG: unable to handle page fault for address: ffff888002b42660 [ 104.528773] #PF: supervisor write access in kernel mode [ 104.528938] #PF: error_code(0x0003) - permissions violation [ 104.529151] PGD 3a01067 P4D 3a01067 PUD 3a02067 PMD 4f24063 PTE 8000000002b42121 [ 104.529476] Oops: Oops: 0003 [#1] SMP NOPTI [ 104.529817] CPU: 0 UID: 0 PID: 63 Comm: kworker/0:1H Not tainted 6.19.0-rc7 #1 PREEMPT(voluntary [ 104.530090] Hardware name: QEMU Ubuntu 24.04 PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.34 [ 104.530440] Workqueue: nvmet_tcp_wq nvmet_tcp_io_work [ 104.530849] RIP: 0010:_copy_to_iter+0x27e/0x7a0 [ 104.531077] Code: 01 48 2b 05 a4 2a 2f 01 48 c1 f8 06 48 c1 e0 0c 48 03 05 a5 2a 2f 01 48 01 f0 7 [ 104.531620] RSP: 0018:ffffc9000021bba8 EFLAGS: 00000246 [ 104.531805] RAX: ffff888002b42660 RBX: 0000000000000008 RCX: 305c782f706d742f [ 104.532019] RDX: 0000000000000008 RSI: ffff88800515c078 RDI: 00000000000009a0 [ 104.532243] RBP: ffff888005070218 R08: 0000000000000000 R09: ffff888004ed5300 [ 104.532428] R10: 0000000000001000 R11: ffff888003e032e0 R12: ffff88800515c078 [ 104.532615] R13: 0000000000000000 R14: 0000000000000008 R15: 0000000000004080 [ 104.532821] FS: 0000000000000000(0000) GS:ffff8880fa4a4000(0000) knlGS:0000000000000000 [ 104.533048] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 104.533222] CR2: ffff888002b42660 CR3: 0000000004fec000 CR4: 00000000000006f0 [ 104.533483] Call Trace: [ 104.533957] <TASK> [ 104.534195] ? __pfx_simple_copy_to_iter+0x10/0x10 [ 104.534381] __skb_datagram_iter+0x1a7/0x2e0 [ 104.534541] ? __pfx_simple_copy_to_iter+0x10/0x10 [ 104.534671] skb_copy_datagram_iter+0x2f/0x90 [ 104.534811] tcp_recvmsg_locked+0x784/0x9d0 [ 104.534936] tcp_recvmsg+0x81/0x1e0 [ 104.535045] inet_recvmsg+0x50/0x130 [ 104.535166] ? security_socket_recvmsg+0x43/0x60 [ 104.535294] sock_recvmsg+0xa1/0xc0 [ 104.535381] nvmet_tcp_io_work+0xdb/0x720 [ 104.535476] process_one_work+0x15b/0x380 [ 104.535570] worker_thread+0x2a5/0x3c0 [ 104.535653] ? __pfx_worker_thread+0x10/0x10 [ 104.535747] kthread+0xf6/0x1f0 [ 104.535839] ? __pfx_kthread+0x10/0x10 [ 104.535949] ? __pfx_kthread+0x10/0x10 [ 104.536060] ret_from_fork+0x131/0x190 [ 104.536182] ? __pfx_kthread+0x10/0x10 [ 104.536296] ret_from_fork_asm+0x1a/0x30 [ 104.536451] </TASK> [ 104.536545] Modules linked in: [ 104.536764] CR2: ffff888002b42660 [ 104.537046] ---[ end trace 0000000000000000 ]--- [ 104.537291] RIP: 0010:_copy_to_iter+0x27e/0x7a0 [ 104.537412] Code: 01 48 2b 05 a4 2a 2f 01 48 c1 f8 06 48 c1 e0 0c 48 03 05 a5 2a 2f 01 48 01 f0 7 [ 104.537848] RSP: 0018:ffffc9000021bba8 EFLAGS: 00000246 [ 104.538005] RAX: ffff888002b42660 RBX: 0000000000000008 RCX: 305c782f706d742f [ 104.538229] RDX: 0000000000000008 RSI: ffff88800515c078 RDI: 00000000000009a0 [ 104.538416] RBP: ffff888005070218 R08: 0000000000000000 R09: ffff888004ed5300 [ 104.538617] R10: 0000000000001000 R11: ffff888003e032e0 R12: ffff88800515c078 [ 104.538825] R13: 0000000000000000 R14: 0000000000000008 R15: 0000000000004080 [ 104.538999] FS: 0000000000000000(0000) GS:ffff8880fa4a4000(0000) knlGS:0000000000000000 [ 104.539209] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 104.539340] CR2: ffff888002b42660 CR3: 0000000004fec000 CR4: 00000000000006f0 [ 104.539553] note: kworker/0:1H[63] exited with irqs disabled [ 104.540201] kworker/0:1H (63) used greatest stack depth: 12976 bytes left~ $ uname -a
Linux (none) 6.19.0-rc7 #1 SMPPREEMPT_DYNAMICFri Jan 30 09:36:44 KST 2026x86_64GNU/LinuxQuoted 67 lines · YunJe Shin
nvmet_tcp_build_pdu_iovec()could walk pastcmd->req.sgwhen a PDU length or offset exceedssg_cntand then use bogus sg->length/offset values, leading to_copy_to_iter()GPF/KASAN. Guardsg_idx, remaining entries, and sg->length/offset before building the bvec.Fixes 872d26a391da ("nvmet-tcp: add NVMe over TCP target driver")Signed-off-by YunJe Shin <[email protected]>Reviewed-by Sagi Grimberg <[email protected]>Reviewed-by Joonkyo Jung <[email protected]>--- drivers/nvme/target/tcp.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/nvme/target/tcp.c +17
-1@@ -349,11 +349,14 @@ static void nvmet_tcp_free_cmd_buffers(struct nvmet_tcp_cmd *cmd) cmd->req.sg =NULL ; } +static void nvmet_tcp_fatal_error (struct nvmet_tcp_queue *queue); +static void nvmet_tcp_build_pdu_iovec (struct nvmet_tcp_cmd *cmd) {struct bio_vec *iov = cmd->iov;struct scatterlist *sg;u32 length, offset, sg_offset; +unsigned int sg_remaining;int nr_pages; length = cmd->pdu_len;@@ -361,9 +364,22 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) offset = cmd->rbytes_done; cmd->sg_idx = offset /PAGE_SIZE ; sg_offset = offset %PAGE_SIZE ; +if (!cmd->req.sg_cnt || cmd->sg_idx >= cmd->req.sg_cnt) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + } sg = &cmd->req.sg[cmd->sg_idx]; + sg_remaining = cmd->req.sg_cnt - cmd->sg_idx;while (length) { +if (!sg_remaining) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + } +if (!sg->length || sg->length <= sg_offset) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + }u32 iov_len =min_t (u32, length, sg->length - sg_offset); bvec_set_page(iov, sg_page(sg), iov_len,@@ -371,6 +387,7 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) length -= iov_len; sg =sg_next (sg); + sg_remaining--; iov++; sg_offset =0 ; } --2.43.0
Just a gentle ping on this patch.
This fixes a crash/GPF observed in _copy_to_iter() when nvmet_tcp_build_pdu_iovec() walks past cmd->req.sg or uses invalid sg->length/offset, triggered by malformed PDU length/offset. The patch adds bounds checks on sg_idx/remaining entries and validates sg->length/offset before building the bvec.
Also, the Reviewed-by tags in the original posting were obtained via private discussion; I’d appreciate an on-list re-review (or I can resend a v2 without those tags if preferred).
Thanks,
YunJe Shin
Quoted 149 lines · yunje shin
The Reviewed-by tags mentioned in the earlier thread were obtained through private discussion, which kbusch likely wasn't aware of. Could the maintainers please re-review this patch?
Quoted 143 lines · yunje shin
The Reviewed-by tags mentioned in the earlier thread were obtained through private discussion, which Keith likely wasn't aware of. Could the maintainers please re-review this patch?
Quoted 137 lines · yunje shin
[ 104.528480] BUG: unable to handle page fault for address: ffff888002b42660 [ 104.528773] #PF: supervisor write access in kernel mode [ 104.528938] #PF: error_code(0x0003) - permissions violation [ 104.529151] PGD 3a01067 P4D 3a01067 PUD 3a02067 PMD 4f24063 PTE 8000000002b42121 [ 104.529476] Oops: Oops: 0003 [#1] SMP NOPTI [ 104.529817] CPU: 0 UID: 0 PID: 63 Comm: kworker/0:1H Not tainted 6.19.0-rc7 #1 PREEMPT(voluntary [ 104.530090] Hardware name: QEMU Ubuntu 24.04 PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.34 [ 104.530440] Workqueue: nvmet_tcp_wq nvmet_tcp_io_work [ 104.530849] RIP: 0010:_copy_to_iter+0x27e/0x7a0 [ 104.531077] Code: 01 48 2b 05 a4 2a 2f 01 48 c1 f8 06 48 c1 e0 0c 48 03 05 a5 2a 2f 01 48 01 f0 7 [ 104.531620] RSP: 0018:ffffc9000021bba8 EFLAGS: 00000246 [ 104.531805] RAX: ffff888002b42660 RBX: 0000000000000008 RCX: 305c782f706d742f [ 104.532019] RDX: 0000000000000008 RSI: ffff88800515c078 RDI: 00000000000009a0 [ 104.532243] RBP: ffff888005070218 R08: 0000000000000000 R09: ffff888004ed5300 [ 104.532428] R10: 0000000000001000 R11: ffff888003e032e0 R12: ffff88800515c078 [ 104.532615] R13: 0000000000000000 R14: 0000000000000008 R15: 0000000000004080 [ 104.532821] FS: 0000000000000000(0000) GS:ffff8880fa4a4000(0000) knlGS:0000000000000000 [ 104.533048] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 104.533222] CR2: ffff888002b42660 CR3: 0000000004fec000 CR4: 00000000000006f0 [ 104.533483] Call Trace: [ 104.533957] <TASK> [ 104.534195] ? __pfx_simple_copy_to_iter+0x10/0x10 [ 104.534381] __skb_datagram_iter+0x1a7/0x2e0 [ 104.534541] ? __pfx_simple_copy_to_iter+0x10/0x10 [ 104.534671] skb_copy_datagram_iter+0x2f/0x90 [ 104.534811] tcp_recvmsg_locked+0x784/0x9d0 [ 104.534936] tcp_recvmsg+0x81/0x1e0 [ 104.535045] inet_recvmsg+0x50/0x130 [ 104.535166] ? security_socket_recvmsg+0x43/0x60 [ 104.535294] sock_recvmsg+0xa1/0xc0 [ 104.535381] nvmet_tcp_io_work+0xdb/0x720 [ 104.535476] process_one_work+0x15b/0x380 [ 104.535570] worker_thread+0x2a5/0x3c0 [ 104.535653] ? __pfx_worker_thread+0x10/0x10 [ 104.535747] kthread+0xf6/0x1f0 [ 104.535839] ? __pfx_kthread+0x10/0x10 [ 104.535949] ? __pfx_kthread+0x10/0x10 [ 104.536060] ret_from_fork+0x131/0x190 [ 104.536182] ? __pfx_kthread+0x10/0x10 [ 104.536296] ret_from_fork_asm+0x1a/0x30 [ 104.536451] </TASK> [ 104.536545] Modules linked in: [ 104.536764] CR2: ffff888002b42660 [ 104.537046] ---[ end trace 0000000000000000 ]--- [ 104.537291] RIP: 0010:_copy_to_iter+0x27e/0x7a0 [ 104.537412] Code: 01 48 2b 05 a4 2a 2f 01 48 c1 f8 06 48 c1 e0 0c 48 03 05 a5 2a 2f 01 48 01 f0 7 [ 104.537848] RSP: 0018:ffffc9000021bba8 EFLAGS: 00000246 [ 104.538005] RAX: ffff888002b42660 RBX: 0000000000000008 RCX: 305c782f706d742f [ 104.538229] RDX: 0000000000000008 RSI: ffff88800515c078 RDI: 00000000000009a0 [ 104.538416] RBP: ffff888005070218 R08: 0000000000000000 R09: ffff888004ed5300 [ 104.538617] R10: 0000000000001000 R11: ffff888003e032e0 R12: ffff88800515c078 [ 104.538825] R13: 0000000000000000 R14: 0000000000000008 R15: 0000000000004080 [ 104.538999] FS: 0000000000000000(0000) GS:ffff8880fa4a4000(0000) knlGS:0000000000000000 [ 104.539209] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 104.539340] CR2: ffff888002b42660 CR3: 0000000004fec000 CR4: 00000000000006f0 [ 104.539553] note: kworker/0:1H[63] exited with irqs disabled [ 104.540201] kworker/0:1H (63) used greatest stack depth: 12976 bytes left~ $ uname -a
Linux (none) 6.19.0-rc7 #1 SMPPREEMPT_DYNAMICFri Jan 30 09:36:44 KST 2026x86_64GNU/LinuxQuoted 67 lines · YunJe Shin
nvmet_tcp_build_pdu_iovec()could walk pastcmd->req.sgwhen a PDU length or offset exceedssg_cntand then use bogus sg->length/offset values, leading to_copy_to_iter()GPF/KASAN. Guardsg_idx, remaining entries, and sg->length/offset before building the bvec.Fixes 872d26a391da ("nvmet-tcp: add NVMe over TCP target driver")Signed-off-by YunJe Shin <[email protected]>Reviewed-by Sagi Grimberg <[email protected]>Reviewed-by Joonkyo Jung <[email protected]>--- drivers/nvme/target/tcp.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/nvme/target/tcp.c +17
-1@@ -349,11 +349,14 @@ static void nvmet_tcp_free_cmd_buffers(struct nvmet_tcp_cmd *cmd) cmd->req.sg =NULL ; } +static void nvmet_tcp_fatal_error (struct nvmet_tcp_queue *queue); +static void nvmet_tcp_build_pdu_iovec (struct nvmet_tcp_cmd *cmd) {struct bio_vec *iov = cmd->iov;struct scatterlist *sg;u32 length, offset, sg_offset; +unsigned int sg_remaining;int nr_pages; length = cmd->pdu_len;@@ -361,9 +364,22 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) offset = cmd->rbytes_done; cmd->sg_idx = offset /PAGE_SIZE ; sg_offset = offset %PAGE_SIZE ; +if (!cmd->req.sg_cnt || cmd->sg_idx >= cmd->req.sg_cnt) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + } sg = &cmd->req.sg[cmd->sg_idx]; + sg_remaining = cmd->req.sg_cnt - cmd->sg_idx;while (length) { +if (!sg_remaining) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + } +if (!sg->length || sg->length <= sg_offset) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + }u32 iov_len =min_t (u32, length, sg->length - sg_offset); bvec_set_page(iov, sg_page(sg), iov_len,@@ -371,6 +387,7 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) length -= iov_len; sg =sg_next (sg); + sg_remaining--; iov++; sg_offset =0 ; } --2.43.0
Hi everyone,
I have additional crash logs from testing on Kernel 6.19-rc8 that show the same vulnerable code path being hit. The crash is consistently reproducible by sending malformed PDUs.
Test environment:
Hardware/Hypervisor: QEMU (i440FX + PIIX) on Ubuntu 24.04 System Info (uname -a): Linux (none) 6.19.0-rc8 #2 SMP PREEMPT_DYNAMIC Tue Feb 3 13:37:27 KST 2026 x86_64 GNU/Linux
Config: SMP, PREEMPT(voluntary), SLUB_DEBUG enabled
Crash log (6.19-rc8 - General Protection Fault via Controlled OOB Read):
[ 7.626066] Oops: general protection fault, probably for non-canonical address 0xdeadbeefdeadbef8: 0000 [#1] SMP NOPTI [ 7.626954] CPU: 0 UID: 0 PID: 42 Comm: kworker/0:1H Not tainted 6.19.0-rc8 #2 PREEMPT(voluntary) [ 7.627648] Hardware name: QEMU Ubuntu 24.04 PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 7.628389] Workqueue: nvmet_tcp_wq nvmet_tcp_io_work [ 7.628771] RIP: 0010:nvmet_tcp_build_pdu_iovec+0x61/0xc0 [ 7.629160] Code: 48 03 47 30 85 f6 75 22 eb 74 48 8b 50 20 48 8d 48 20 48 89 d0 48 83 e0 fc 83 e2 01 48 0f 44 c1 49 83 c1 10 31 c9 85 f6 74 32 <8b> 50 0c 4c 8b 00 29 ca 39 f2 0f 47 d6 03 48 08 49 83 e0 fc 4d 89 [ 7.630423] RSP: 0018:ffffc90000167cd8 EFLAGS: 00010202 [ 7.630784] RAX: deadbeefdeadbeec RBX: ffff888100349800 RCX: 0000000000000000 [ 7.631256] RDX: 0000000000000001 RSI: 0000000000000008 RDI: ffff888101740000 [ 7.631739] RBP: ffff888101740000 R08: 0000000000000000 R09: ffff88810148b280 [ 7.632221] R10: 0000000000000001 R11: 0000000000000008 R12: ffffc90000167d00 [ 7.632710] R13: ffff888100349918 R14: ffffc90000167dd7 R15: ffff888100349810 [ 7.633190] FS: 0000000000000000(0000) GS:ffff8881b84b1000(0000) knlGS:0000000000000000 [ 7.633779] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 7.634191] CR2: 00000000004c6000 CR3: 0000000100cd6000 CR4: 00000000000006f0 [ 7.634710] Call Trace: [ 7.634906] <TASK> [ 7.635072] nvmet_tcp_try_recv_pdu+0x576/0xa50 [ 7.635401] nvmet_tcp_io_work+0x69/0x720 [ 7.635724] process_one_work+0x15b/0x380 [ 7.636016] worker_thread+0x2a5/0x3c0 [ 7.636634] kthread+0xf6/0x1f0 [ 7.637463] ret_from_fork+0x131/0x190 [ 7.638067] ret_from_fork_asm+0x1a/0x30 [ 7.638336] </TASK>
Analysis: The crash occurs in nvmet_tcp_build_pdu_iovec() during an out-of-bounds read of the scatterlist. In our controlled test, the RAX register correctly reflects the injected pattern 0xdeadbeefdeadbeec, and the kernel attempts to dereference 0xdeadbeefdeadbef8 (calculated from our input), leading to a General Protection Fault.
This confirms that by sending a malformed PDU with a large data_length or data_offset, an attacker can force the kernel to walk past the allocated cmd->req.sg pointers and treat adjacent heap data as a struct scatterlist.
Proposed Mitigation: The proposed patch adds critical bounds checks on sg_idx and sg_remaining within nvmet_tcp_build_pdu_iovec(). These checks ensure the loop terminates correctly even when provided with inconsistent PDU metadata.
I would appreciate any feedback or suggestions on this patch.
Thanks, YunJe Shin
Quoted 166 lines · yunje shin
Just a gentle ping on this patch.
This fixes a crash/GPF observed in
_copy_to_iter()whennvmet_tcp_build_pdu_iovec()walks pastcmd->req.sgor uses invalid sg->length/offset, triggered by malformed PDU length/offset. The patch adds bounds checks on sg_idx/remaining entries and validates sg->length/offset before building the bvec.Also, the Reviewed-by tags in the original posting were obtained via private discussion; I’d appreciate an on-list re-review (or I can resend a v2 without those tags if preferred).
Thanks,
YunJe ShinQuoted 149 lines · yunje shin
The Reviewed-by tags mentioned in the earlier thread were obtained through private discussion, which kbusch likely wasn't aware of. Could the maintainers please re-review this patch?
Quoted 143 lines · yunje shin
The Reviewed-by tags mentioned in the earlier thread were obtained through private discussion, which Keith likely wasn't aware of. Could the maintainers please re-review this patch?
Quoted 137 lines · yunje shin
[ 104.528480] BUG: unable to handle page fault for address: ffff888002b42660 [ 104.528773] #PF: supervisor write access in kernel mode [ 104.528938] #PF: error_code(0x0003) - permissions violation [ 104.529151] PGD 3a01067 P4D 3a01067 PUD 3a02067 PMD 4f24063 PTE 8000000002b42121 [ 104.529476] Oops: Oops: 0003 [#1] SMP NOPTI [ 104.529817] CPU: 0 UID: 0 PID: 63 Comm: kworker/0:1H Not tainted 6.19.0-rc7 #1 PREEMPT(voluntary [ 104.530090] Hardware name: QEMU Ubuntu 24.04 PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.34 [ 104.530440] Workqueue: nvmet_tcp_wq nvmet_tcp_io_work [ 104.530849] RIP: 0010:_copy_to_iter+0x27e/0x7a0 [ 104.531077] Code: 01 48 2b 05 a4 2a 2f 01 48 c1 f8 06 48 c1 e0 0c 48 03 05 a5 2a 2f 01 48 01 f0 7 [ 104.531620] RSP: 0018:ffffc9000021bba8 EFLAGS: 00000246 [ 104.531805] RAX: ffff888002b42660 RBX: 0000000000000008 RCX: 305c782f706d742f [ 104.532019] RDX: 0000000000000008 RSI: ffff88800515c078 RDI: 00000000000009a0 [ 104.532243] RBP: ffff888005070218 R08: 0000000000000000 R09: ffff888004ed5300 [ 104.532428] R10: 0000000000001000 R11: ffff888003e032e0 R12: ffff88800515c078 [ 104.532615] R13: 0000000000000000 R14: 0000000000000008 R15: 0000000000004080 [ 104.532821] FS: 0000000000000000(0000) GS:ffff8880fa4a4000(0000) knlGS:0000000000000000 [ 104.533048] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 104.533222] CR2: ffff888002b42660 CR3: 0000000004fec000 CR4: 00000000000006f0 [ 104.533483] Call Trace: [ 104.533957] <TASK> [ 104.534195] ? __pfx_simple_copy_to_iter+0x10/0x10 [ 104.534381] __skb_datagram_iter+0x1a7/0x2e0 [ 104.534541] ? __pfx_simple_copy_to_iter+0x10/0x10 [ 104.534671] skb_copy_datagram_iter+0x2f/0x90 [ 104.534811] tcp_recvmsg_locked+0x784/0x9d0 [ 104.534936] tcp_recvmsg+0x81/0x1e0 [ 104.535045] inet_recvmsg+0x50/0x130 [ 104.535166] ? security_socket_recvmsg+0x43/0x60 [ 104.535294] sock_recvmsg+0xa1/0xc0 [ 104.535381] nvmet_tcp_io_work+0xdb/0x720 [ 104.535476] process_one_work+0x15b/0x380 [ 104.535570] worker_thread+0x2a5/0x3c0 [ 104.535653] ? __pfx_worker_thread+0x10/0x10 [ 104.535747] kthread+0xf6/0x1f0 [ 104.535839] ? __pfx_kthread+0x10/0x10 [ 104.535949] ? __pfx_kthread+0x10/0x10 [ 104.536060] ret_from_fork+0x131/0x190 [ 104.536182] ? __pfx_kthread+0x10/0x10 [ 104.536296] ret_from_fork_asm+0x1a/0x30 [ 104.536451] </TASK> [ 104.536545] Modules linked in: [ 104.536764] CR2: ffff888002b42660 [ 104.537046] ---[ end trace 0000000000000000 ]--- [ 104.537291] RIP: 0010:_copy_to_iter+0x27e/0x7a0 [ 104.537412] Code: 01 48 2b 05 a4 2a 2f 01 48 c1 f8 06 48 c1 e0 0c 48 03 05 a5 2a 2f 01 48 01 f0 7 [ 104.537848] RSP: 0018:ffffc9000021bba8 EFLAGS: 00000246 [ 104.538005] RAX: ffff888002b42660 RBX: 0000000000000008 RCX: 305c782f706d742f [ 104.538229] RDX: 0000000000000008 RSI: ffff88800515c078 RDI: 00000000000009a0 [ 104.538416] RBP: ffff888005070218 R08: 0000000000000000 R09: ffff888004ed5300 [ 104.538617] R10: 0000000000001000 R11: ffff888003e032e0 R12: ffff88800515c078 [ 104.538825] R13: 0000000000000000 R14: 0000000000000008 R15: 0000000000004080 [ 104.538999] FS: 0000000000000000(0000) GS:ffff8880fa4a4000(0000) knlGS:0000000000000000 [ 104.539209] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 104.539340] CR2: ffff888002b42660 CR3: 0000000004fec000 CR4: 00000000000006f0 [ 104.539553] note: kworker/0:1H[63] exited with irqs disabled [ 104.540201] kworker/0:1H (63) used greatest stack depth: 12976 bytes left~ $ uname -a
Linux (none) 6.19.0-rc7 #1 SMPPREEMPT_DYNAMICFri Jan 30 09:36:44 KST 2026x86_64GNU/LinuxQuoted 67 lines · YunJe Shin
nvmet_tcp_build_pdu_iovec()could walk pastcmd->req.sgwhen a PDU length or offset exceedssg_cntand then use bogus sg->length/offset values, leading to_copy_to_iter()GPF/KASAN. Guardsg_idx, remaining entries, and sg->length/offset before building the bvec.Fixes 872d26a391da ("nvmet-tcp: add NVMe over TCP target driver")Signed-off-by YunJe Shin <[email protected]>Reviewed-by Sagi Grimberg <[email protected]>Reviewed-by Joonkyo Jung <[email protected]>--- drivers/nvme/target/tcp.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/nvme/target/tcp.c +17
-1@@ -349,11 +349,14 @@ static void nvmet_tcp_free_cmd_buffers(struct nvmet_tcp_cmd *cmd) cmd->req.sg =NULL ; } +static void nvmet_tcp_fatal_error (struct nvmet_tcp_queue *queue); +static void nvmet_tcp_build_pdu_iovec (struct nvmet_tcp_cmd *cmd) {struct bio_vec *iov = cmd->iov;struct scatterlist *sg;u32 length, offset, sg_offset; +unsigned int sg_remaining;int nr_pages; length = cmd->pdu_len;@@ -361,9 +364,22 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) offset = cmd->rbytes_done; cmd->sg_idx = offset /PAGE_SIZE ; sg_offset = offset %PAGE_SIZE ; +if (!cmd->req.sg_cnt || cmd->sg_idx >= cmd->req.sg_cnt) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + } sg = &cmd->req.sg[cmd->sg_idx]; + sg_remaining = cmd->req.sg_cnt - cmd->sg_idx;while (length) { +if (!sg_remaining) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + } +if (!sg->length || sg->length <= sg_offset) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + }u32 iov_len =min_t (u32, length, sg->length - sg_offset); bvec_set_page(iov, sg_page(sg), iov_len,@@ -371,6 +387,7 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) length -= iov_len; sg =sg_next (sg); + sg_remaining--; iov++; sg_offset =0 ; } --2.43.0
Quoted 4 lines · YunJe Shin
nvmet_tcp_build_pdu_iovec()could walk pastcmd->req.sgwhen a PDU length or offset exceedssg_cntand then use bogus sg->length/offset values, leading to_copy_to_iter()GPF/KASAN. Guardsg_idx, remaining entries, and sg->length/offset before building the bvec.
Thanks, applied now.
Hi,
Quoted 42 lines · YunJe Shin
nvmet_tcp_build_pdu_iovec()could walk pastcmd->req.sgwhen a PDU length or offset exceedssg_cntand then use bogus sg->length/offset values, leading to_copy_to_iter()GPF/KASAN. Guardsg_idx, remaining entries, and sg->length/offset before building the bvec.Fixes 872d26a391da ("nvmet-tcp: add NVMe over TCP target driver")Signed-off-by YunJe Shin <[email protected]>Reviewed-by Sagi Grimberg <[email protected]>Reviewed-by Joonkyo Jung <[email protected]>--- drivers/nvme/target/tcp.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/nvme/target/tcp.c +8
-0@@ -349,11 +349,14 @@ static void nvmet_tcp_free_cmd_buffers(struct nvmet_tcp_cmd *cmd) cmd->req.sg =NULL ; } +static void nvmet_tcp_fatal_error (struct nvmet_tcp_queue *queue); +static void nvmet_tcp_build_pdu_iovec (struct nvmet_tcp_cmd *cmd) {struct bio_vec *iov = cmd->iov;struct scatterlist *sg;u32 length, offset, sg_offset; +unsigned int sg_remaining;int nr_pages; length = cmd->pdu_len;@@ -361,9 +364,22 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) offset = cmd->rbytes_done; cmd->sg_idx = offset /PAGE_SIZE ; sg_offset = offset %PAGE_SIZE ; +if (!cmd->req.sg_cnt || cmd->sg_idx >= cmd->req.sg_cnt) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + } sg = &cmd->req.sg[cmd->sg_idx]; + sg_remaining = cmd->req.sg_cnt - cmd->sg_idx;
drivers/nvme/target/tcp.c static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) · line 382
Quoted 10 lines
drivers/nvme/target/tcp.c +8
-0while (length) { +if (!sg_remaining) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + } +if (!sg->length || sg->length <= sg_offset) { +nvmet_tcp_fatal_error (cmd->queue); +return ; + }
An experimental AI agent provided the following review feedback.
If we return early here (or in the other bounds checks added below), cmd->recv_msg.msg_iter is left uninitialized because we skip the iov_iter_bvec() call at the end of the function.
Since nvmet_tcp_build_pdu_iovec() returns void, callers are unaware of the failure. For example, in nvmet_tcp_handle_h2c_data_pdu():
nvmet_tcp_build_pdu_iovec(cmd); queue->cmd = cmd; queue->rcv_state = NVMET_TCP_RECV_DATA;
Even though nvmet_tcp_fatal_error() correctly sets rcv_state to NVMET_TCP_RECV_ERR, the caller immediately overwrites it with NVMET_TCP_RECV_DATA.
Does this cause the state machine to proceed and attempt to receive data using the uninitialized cmd->recv_msg.msg_iter? Should this function return an error code so callers can handle the failure?
I do not claim to understand the code, and I did not try to follow the execution sequence. However, I do see that at least one of the callers of nvmet_tcp_build_pdu_iovec() does unconditionally overwrite queue->rcv_state, as indicated above. Please let me know if the AI feedback is reasonable or if it is missing some context.
Thanks,
Guenter
Quoted 13 lines · Guenter Roeck
An experimental AI agent provided the following review feedback.
If we return early here (or in the other bounds checks added below),
cmd->recv_msg.msg_iteris left uninitialized because we skip theiov_iter_bvec()call at the end of the function.Since
nvmet_tcp_build_pdu_iovec()returns void, callers are unaware of the failure. For example, innvmet_tcp_handle_h2c_data_pdu():nvmet_tcp_build_pdu_iovec(cmd); queue->cmd = cmd; queue->rcv_state = NVMET_TCP_RECV_DATA;
Quoted 7 lines
Even though
nvmet_tcp_fatal_error()correctly setsrcv_statetoNVMET_TCP_RECV_ERR, the caller immediately overwrites it withNVMET_TCP_RECV_DATA.Does this cause the state machine to proceed and attempt to receive data using the uninitialized
cmd->recv_msg.msg_iter? Should this function return an error code so callers can handle the failure?
In my opinion this is a valid concern.
I suspect that it's really possible to create a malicious sequence that would make the target crash.
I would change nvmet_tcp_build_pdu_iovec() to return an error code and its caller so they propagate it up to nvmet_tcp_done_recv_pdu()
Something like this:
drivers/nvme/target/tcp.c +25 -22
@@ -351,7 +351,7 @@ static void nvmet_tcp_free_cmd_buffers(struct nvmet_tcp_cmd *cmd)static void nvmet_tcp_fatal_error (struct nvmet_tcp_queue *queue); -static void nvmet_tcp_build_pdu_iovec (struct nvmet_tcp_cmd *cmd) +static int nvmet_tcp_build_pdu_iovec (struct nvmet_tcp_cmd *cmd) {struct bio_vec *iov = cmd->iov;struct scatterlist *sg;
@@ -364,22 +364,19 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd) offset = cmd->rbytes_done; cmd->sg_idx = offset /PAGE_SIZE ; sg_offset = offset %PAGE_SIZE ; -if (!cmd->req.sg_cnt || cmd->sg_idx >= cmd->req.sg_cnt) { -nvmet_tcp_fatal_error (cmd->queue); -return ; - } +if (!cmd->req.sg_cnt || cmd->sg_idx >= cmd->req.sg_cnt) +return -EPROTO ; + sg = &cmd->req.sg[cmd->sg_idx]; sg_remaining = cmd->req.sg_cnt - cmd->sg_idx;while (length) { -if (!sg_remaining) { -nvmet_tcp_fatal_error (cmd->queue); -return ; - } -if (!sg->length || sg->length <= sg_offset) { -nvmet_tcp_fatal_error (cmd->queue); -return ; - } +if (!sg_remaining) +return -EPROTO ; + +if (!sg->length || sg->length <= sg_offset) +return -EPROTO ; +u32 iov_len =min_t (u32, length, sg->length - sg_offset); bvec_set_page(iov, sg_page(sg), iov_len,
@@ -394,6 +391,7 @@ static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd)iov_iter_bvec (&cmd->recv_msg.msg_iter,ITER_DEST , cmd->iov, nr_pages, cmd->pdu_len); +return 0 ; }static void nvmet_tcp_fatal_error (struct nvmet_tcp_queue *queue)
@@ -931,7 +929,7 @@ static int nvmet_tcp_handle_icreq(struct nvmet_tcp_queue *queue)return 0 ; } -static void nvmet_tcp_handle_req_failure (struct nvmet_tcp_queue *queue, +static int nvmet_tcp_handle_req_failure (struct nvmet_tcp_queue *queue,struct nvmet_tcp_cmd *cmd,struct nvmet_req *req) {size_t data_len =le32_to_cpu (req->cmd->common.dptr.sgl.length);
@@ -947,19 +945,19 @@ static void nvmet_tcp_handle_req_failure(struct nvmet_tcp_queue *queue,if (!nvme_is_write (cmd->req.cmd) || !data_len || data_len > cmd->req.port->inline_data_size) {nvmet_prepare_receive_pdu (queue); -return ; +return 0 ; } ret =nvmet_tcp_map_data (cmd);if (unlikely (ret)) {pr_err ("queue %d: failed to map data\n" , queue->idx);nvmet_tcp_fatal_error (queue); -return ; +return ret; } queue->rcv_state =NVMET_TCP_RECV_DATA ; -nvmet_tcp_build_pdu_iovec (cmd); cmd->flags |=NVMET_TCP_F_INIT_FAILED ; +return nvmet_tcp_build_pdu_iovec (cmd); }static int nvmet_tcp_handle_h2c_data_pdu (struct nvmet_tcp_queue *queue)
@@ -1011,7 +1009,8 @@ static int nvmet_tcp_handle_h2c_data_pdu(struct nvmet_tcp_queue *queue) goto err_proto; } cmd->pdu_recv =0 ; -nvmet_tcp_build_pdu_iovec (cmd); +if (nvmet_tcp_build_pdu_iovec (cmd)) + goto err_proto; queue->cmd = cmd; queue->rcv_state =NVMET_TCP_RECV_DATA ;
@@ -1074,8 +1073,10 @@ static int nvmet_tcp_done_recv_pdu(struct nvmet_tcp_queue *queue)le32_to_cpu (req->cmd->common.dptr.sgl.length), le16_to_cpu(req->cqe->status)); -nvmet_tcp_handle_req_failure (queue, queue->cmd, req); -return 0 ; + ret =nvmet_tcp_handle_req_failure (queue, queue->cmd, req); +if (unlikely (ret)) +nvmet_tcp_fatal_error (queue); +return ret; } ret =nvmet_tcp_map_data (queue->cmd);
@@ -1092,8 +1093,10 @@ static int nvmet_tcp_done_recv_pdu(struct nvmet_tcp_queue *queue)if (nvmet_tcp_need_data_in (queue->cmd)) {if (nvmet_tcp_has_inline_data (queue->cmd)) { queue->rcv_state =NVMET_TCP_RECV_DATA ; -nvmet_tcp_build_pdu_iovec (queue->cmd); -return 0 ; + ret =nvmet_tcp_build_pdu_iovec (queue->cmd); +if (unlikely (ret)) +nvmet_tcp_fatal_error (queue); +return ret; }/* send back R2T */ nvmet_tcp_queue_response (&queue->cmd->req);
Maurizio
Quoted 7 lines · Maurizio Lombardi
In my opinion this is a valid concern.
I suspect that it's really possible to create a malicious sequence that would make the target crash.I would change
nvmet_tcp_build_pdu_iovec()to return an error code and its caller so they propagate it up tonvmet_tcp_done_recv_pdu()
Additionally, the current design leaves it somewhat ambiguous as to which function should be responsible for calling nvmet_tcp_fatal_error(). In my opinion, this should be handled by the upper-layer function nvmet_tcp_try_recv(), allowing the lower-level functions to simply return an error code.
I submitted a patch to clean this up exactly one year ago, but it has been forgotten and I only just remembered it.
https://lore.kernel.org/linux-nvme/[email protected]/
I could prepare a patchset to fix all this stuff
Maurizio
Hi,
When I wrote the patch, I followed the existing convention of calling nvmet_tcp_fatal_error() directly. But since the function returns void, having reviewed the AI agent's analysis, the concern seems to have some merit - the caller could overwrite rcv_state with NVMET_TCP_RECV_DATA after the failure, causing the state machine to proceed with an uninitialized cmd->recv_msg.msg_iter.
Maurizio's approach of changing the return type to int and letting callers propagate the error makes sense. This also aligns with his earlier patch [1] to remove redundant nvmet_tcp_fatal_error() calls from lower-level functions.
To confirm this, would the following sequence trigger the issue?
1. Send a crafted H2C Data PDU with an invalid data_offset or length that causes sg_idx >= sg_cnt in nvmet_tcp_build_pdu_iovec()
2. The bounds check triggers -> nvmet_tcp_fatal_error() sets rcv_state = NVMET_TCP_RECV_ERR -> early return (void)
3. Back in nvmet_tcp_handle_h2c_data_pdu(), the caller overwrites rcv_state with NVMET_TCP_RECV_DATA (ignoring the failure)
4. The state machine proceeds to nvmet_tcp_try_recv_data(), which calls recvmsg() using the uninitialized cmd->recv_msg.msg_iter
[1] https://lore.kernel.org/linux-nvme/[email protected]/
Thanks,
Yunje
Quoted 24 lines · Maurizio Lombardi
Quoted 7 lines · Maurizio Lombardi
In my opinion this is a valid concern.
I suspect that it's really possible to create a malicious sequence that would make the target crash.I would change
nvmet_tcp_build_pdu_iovec()to return an error code and its caller so they propagate it up tonvmet_tcp_done_recv_pdu()Additionally, the current design leaves it somewhat ambiguous as to which function should be responsible for calling
nvmet_tcp_fatal_error(). In my opinion, this should be handled by the upper-layer functionnvmet_tcp_try_recv(), allowing the lower-level functions to simply return an error code.I submitted a patch to clean this up exactly one year ago, but it has been forgotten and I only just remembered it.
https://lore.kernel.org/linux-nvme/[email protected]/
I could prepare a patchset to fix all this stuff
Maurizio
Quoted 16 lines · yunje shin
Hi,
When I wrote the patch, I followed the existing convention of calling
nvmet_tcp_fatal_error()directly. But since the function returns void, having reviewed the AI agent's analysis, the concern seems to have some merit - the caller could overwritercv_statewithNVMET_TCP_RECV_DATAafter the failure, causing the state machine to proceed with an uninitializedcmd->recv_msg.msg_iter.Maurizio's approach of changing the return type to int and letting callers propagate the error makes sense. This also aligns with his earlier patch [1] to remove redundant
nvmet_tcp_fatal_error()calls from lower-level functions.To confirm this, would the following sequence trigger the issue?
Quoted 8 lines
1. Send a crafted H2C Data PDU with an invalid
data_offsetor length that causessg_idx>=sg_cntinnvmet_tcp_build_pdu_iovec()
2. The bounds check triggers ->nvmet_tcp_fatal_error()setsrcv_state=NVMET_TCP_RECV_ERR-> early return (void)
3. Back innvmet_tcp_handle_h2c_data_pdu(), the caller overwritesrcv_statewithNVMET_TCP_RECV_DATA(ignoring the failure)
4. The state machine proceeds tonvmet_tcp_try_recv_data(), which callsrecvmsg()using the uninitializedcmd->recv_msg.msg_iter
Yes, this is the sequence that should make the kernel crash.
I say "should" because right now I don't have a reproducer to confirm it 100%.
In any case, the code looks a bit fragile and I am going to submit a patchset to clean it up.
It should be ready very soon.
Maurizio