nvmet: fix stale data exposure and ZNS validation
Subsystems (4)
cover nvmet: fix stale data exposure and ZNS validation
A connected host can currently receive stale target memory through three nvmet paths.
The FC transport sends a read buffer even when the command completes with an error. Some errors occur before the command handler initializes that buffer, so the host receives data left in the allocation.
Zone Management Receive writes only the Number of Zones field and the descriptors returned by the backend, while the transports send the entire requested buffer. Reserved header bytes and unused descriptor slots can therefore retain stale data. The handler also does not verify that the transport data length agrees with NUMD, allowing a host to increase the amount returned beyond the size described by the command.
The PCI endpoint transport allocates each read segment at the length of the host's SGL data descriptor and sends every segment in full, even when the descriptors describe more than the command transfers. The excess reaches the host uninitialized, on any successful read.
Patch 1 makes the FC transport send read data only for successful commands, matching the TCP and RDMA transports. Patch 2 validates the Zone Management Receive transfer length and zeroes the report before filling it. Patch 3 rejects NUMD values whose byte count wraps the u32 length calculation instead of accepting the wrapped request.
Patch 4 fixes a related PCI endpoint bug that patch 5 depends on. The transport reports every error it detects itself as a successful completion, because nvmet_pci_epf_complete_iod() reloads the status from a CQE that only the nvmet core fills in, so even a write whose data could not be fetched is reported as written. Patch 5 then makes the transport reject SGLs whose data descriptors do not add up to the command's data length.
Patches 1-3 were tested over the loop, tcp, rdma (rdma_rxe) and fc (fcloop) transports, with a zoned null_blk namespace for the Zone Management Receive fixes. On the unpatched kernel, a failed Identify over fc returns data, zone reports carry stale bytes and accept a transfer length that disagrees with NUMD, and a wrapping NUMD is accepted. With patches 1-3 applied, none of these occur on any of the four transports. Patches 4 and 5 are build-tested only: no PCI endpoint hardware was available.
A tested reproducer for patches 1-3 is available privately on request.
Andrea Parri (5):
nvmet-fc: only send read data for successful commands
nvmet: zns: don't leak stale memory in zone management receive
nvmet: zns: reject a NUMD whose byte count wraps
nvmet: pci-epf: report transport errors in command completions
nvmet: pci-epf: require SGLs to match the command data length
drivers/nvme/target/fc.c | 8 ++++---- drivers/nvme/target/pci-epf.c | 30 +++++++++++++++++++++++++++--- drivers/nvme/target/zns.c | 17 +++++++++++++++-- 3 files changed, 46 insertions(+), 9 deletions(-)
base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
Signature · 1 line
2.53.0
PATCH 1/5 nvmet-fc: only send read data for successful commands
When a read command completes with an error, __nvmet_fc_fcp_nvme_cmd_done() still pushes the whole data buffer to the host before the response. nvmet_fc_alloc_tgt_pgs() allocates that buffer with sgl_alloc() and no __GFP_ZERO, and a command rejected by nvmet_check_transfer_len() leaves it untouched, so the host receives stale target memory.
The buffer is as large as the data length in the command IU, so the amount of stale memory returned is up to the host.
The tcp and rdma targets send data only when cqe->status is zero (see nvmet_tcp_need_data_out() and nvmet_rdma_need_data_out()), so no nvmet command can rely on returning data with an error status. Do the same for fc. The ERSP then reports a transferred length of fod->offset, which is 0, matching what nvme_fc_fcpio_done() checks on the host.
Tested over nvme-fcloop: a failed read command no longer transfers data and still completes with its error status, and successful reads are unaffected.
--- drivers/nvme/target/fc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/nvme/target/fc.c +4 -4
@@ -2484,11 +2484,11 @@ __nvmet_fc_fcp_nvme_cmd_done(struct nvmet_fc_tgtport *tgtport, }else {/* -* try to push the data even if the SQE status is non-zero. -* There may be a status where data still was intended to -* be moved +* A failed command may not have filled its read buffer, so +* only push data for success, as the tcp and rdma targets do. */ -if ((fod->io_dir ==NVMET_FCP_READ ) && (fod->data_sg_cnt)) { +if (fod->io_dir ==NVMET_FCP_READ && fod->data_sg_cnt && + !cqe->status) {/* push the data over before sending rsp */ nvmet_fc_transfer_fcp_data (tgtport, fod,NVMET_FCOP_READDATA );
Signature · 1 line
2.53.0
PATCH 2/5 nvmet: zns: don't leak stale memory in zone management receive
In reply to Andrea ParriA Zone Management Receive report returns the full buffer the host asked for, but nvmet_bdev_zone_zmgmt_recv_work() writes only the 8-byte Number of Zones field and one 64-byte descriptor for each matching zone that fits. The 56 reserved header bytes and every descriptor slot past the last match keep whatever the buffer held before. A 4096-byte report covering a single zone leaves 4024 bytes unwritten.
The tcp, rdma and fc transports allocate that buffer with sgl_alloc() and pci-epf with kmalloc(), none with __GFP_ZERO, and on success each sends the whole buffer to the host (nvmet_setup_c2h_data_pdu() for tcp), so the host receives old kernel memory from the target.
nvmet_bdev_execute_zone_mgmt_recv() also never checks the transport data length against NUMD, so on transports where the host sets that length, the buffer sent back can be much larger than the report NUMD describes. pci-epf derives its data length from NUMD, so the length check below does not apply there, but the zeroing still covers the report.
Reject a data length that does not match NUMD with nvmet_check_transfer_len(), as nvmet_bdev_execute_zone_append() does, and zero the report with nvmet_zero_sgl() before filling it. Zeroing NUMD bytes is only enough because of the length check, so the two go together.
Allocating with __GFP_ZERO in the transports would also close the leak, but it adds a memset to every read command to fix one handler, so the transports are left alone.
Tested over the loop, tcp, rdma and fc transports with a zoned null_blk namespace: before this patch, zone reports carried stale bytes and a data length larger than NUMD was accepted; with it, reports are fully initialized and such a length fails with Data SGL Length Invalid.
---
pci-epf has a separate, pre-existing problem with SGLs that describe more than the command's data length. Patch 5/5 fixes it.
drivers/nvme/target/zns.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/nvme/target/zns.c +10 -0
@@ -282,6 +282,11 @@ static void nvmet_bdev_zone_zmgmt_recv_work(struct work_struct *w)if (status) goto out; +/* Only the zone count and matching descriptors are written below. */ + status =nvmet_zero_sgl (req,0 , out_bufsize); +if (status) + goto out; +if (!req_slba_nr_zones) { status =NVME_SC_SUCCESS ; goto out;
@@ -317,6 +322,11 @@ static void nvmet_bdev_zone_zmgmt_recv_work(struct work_struct *w)void nvmet_bdev_execute_zone_mgmt_recv (struct nvmet_req *req) { +u32 out_bufsize = (le32_to_cpu (req->cmd->zmr.numd) +1 ) <<2 ; + +if (!nvmet_check_transfer_len (req, out_bufsize)) +return ; +INIT_WORK (&req->z.zmgmt_work, nvmet_bdev_zone_zmgmt_recv_work);queue_work (zbd_wq, &req->z.zmgmt_work); }
Signature · 1 line
2.53.0
Re: [PATCH 2/5] nvmet: zns: don't leak stale memory in zone management receive
Quoted 4 lines · Andrea Parri
Tested over the loop, tcp, rdma and fc transports with a zoned
null_blknamespace: before this patch, zone reports carried stale bytes and a data length larger than NUMD was accepted; with it, reports are fully initialized and such a length fails with Data SGL Length Invalid.
Can you wire up the reproduce in blktests?
The change itself looks good:
Re: [PATCH 2/5] nvmet: zns: don't leak stale memory in zone management receive
Quoted 7 lines · Christoph Hellwig
Quoted 4 lines · Andrea Parri
Tested over the loop, tcp, rdma and fc transports with a zoned
null_blknamespace: before this patch, zone reports carried stale bytes and a data length larger than NUMD was accepted; with it, reports are fully initialized and such a length fails with Data SGL Length Invalid.Can you wire up the reproduce in blktests?
Sure. Following Documentation/process/security-bugs.rst I had only offered the reproducers on request; since you'd like them in blktests, I'll post them to linux-block shortly, unless you'd rather have them off-list until the fixes are merged. One test covers this patch, and two more cover the fc and NUMD patches of this series.
Thanks for the review,
Andrea
Christoph Hellwig
maintainerRe: [PATCH 2/5] nvmet: zns: don't leak stale memory in zone management receive
Quoted 7 lines · Andrea Parri
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Andrea Parri
Tested over the loop, tcp, rdma and fc transports with a zoned
null_blknamespace: before this patch, zone reports carried stale bytes and a data length larger than NUMD was accepted; with it, reports are fully initialized and such a length fails with Data SGL Length Invalid.
Quoted 7 lines
Quoted 1 line
Can you wire up the reproduce in blktests?
Sure. Following
Documentation/process/security-bugs.rstI had only offered the reproducers on request; since you'd like them in blktests, I'll post them to linux-block shortly, unless you'd rather have them off-list until the fixes are merged. One test covers this patch, and two more cover the fc and NUMD patches of this series.
I would not consider anything relating nvme except for userspace API a security issue. The protocols are clearly not designed for and/or advertised for untrusted networks.
Just to make it clear, that's not to say we don't want these fixed, but they are considered robustness fixes and not security issues.
Re: [PATCH 2/5] nvmet: zns: don't leak stale memory in zone management receive
Quoted 2 lines · Christoph Hellwig
Just to make it clear, that's not to say we don't want these fixed, but they are considered robustness fixes and not security issues.
Thanks for clarifying. The tests are now posted:
https://lore.kernel.org/r/[email protected]
Andrea
Re: [PATCH 2/5] nvmet: zns: don't leak stale memory in zone management receive
In reply to Andrea ParriQuoted 11 lines · Andrea Parri
A Zone Management Receive report returns the full buffer the host asked for, but
nvmet_bdev_zone_zmgmt_recv_work()writes only the 8-byte Number of Zones field and one 64-byte descriptor for each matching zone that fits. The 56 reserved header bytes and every descriptor slot past the last match keep whatever the buffer held before. A 4096-byte report covering a single zone leaves 4024 bytes unwritten.The tcp, rdma and fc transports allocate that buffer with
sgl_alloc()and pci-epf withkmalloc(), none with__GFP_ZERO, and on success each sends the whole buffer to the host (nvmet_setup_c2h_data_pdu()for tcp), so the host receives old kernel memory from the target.
Thanks, applied to nvme-7.4.
This one looks good on its own independent of the patch series, so I've applied this one now and will wait for the comments on the remaining patches to settle before considering the rest.
PATCH 3/5 nvmet: zns: reject a NUMD whose byte count wraps
In reply to Andrea ParriZone Management Receive computes its buffer size as (numd + 1) << 2 in u32, in nvmet_bdev_validate_zone_mgmt_recv(), nvmet_bdev_zone_zmgmt_recv_work() and nvmet_bdev_execute_zone_mgmt_recv(). NUMD is a 32-bit dword count, so any NUMD from 0x3fffffff up wraps.
The data length check in nvmet_bdev_execute_zone_mgmt_recv() sees the same wrapped value, so such a command can pass it. A data length that does not match the wrapped size is already rejected by that check, with Data SGL Length Invalid. Every site agrees on the wrapped size, so the target zeroes and fills only that many bytes and nothing leaks, but it answers a command whose NUMD asks for 4 GiB or more.
Reject a NUMD from U32_MAX / 4 up with Invalid Field, next to the existing check for a NUMD smaller than the report header. Computing the size in size_t instead would still wrap on 32-bit kernels, where nvmet_check_transfer_len() and req->transfer_len are 32 bits wide.
nvmet_io_cmd_transfer_len() in core.c uses the same expression and is left alone here.
Tested over the loop, tcp, rdma and fc transports with a zoned null_blk namespace: a wrapping NUMD, sent with a data length that matches the wrapped size, was accepted before this patch and now fails with Invalid Field, while ordinary NUMD values still succeed.
--- drivers/nvme/target/zns.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/nvme/target/zns.c +5 -2
@@ -150,14 +150,17 @@ void nvmet_execute_identify_ns_zns(struct nvmet_req *req)static u16 nvmet_bdev_validate_zone_mgmt_recv (struct nvmet_req *req) {sector_t sect =nvmet_lba_to_sect (req->ns, req->cmd->zmr.slba); -u32 out_bufsize = (le32_to_cpu (req->cmd->zmr.numd) +1 ) <<2 ; +u32 numd =le32_to_cpu (req->cmd->zmr.numd); +u32 out_bufsize = (numd +1 ) <<2 ;if (sect >=get_capacity (req->ns->bdev->bd_disk)) { req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd , slba);return NVME_SC_LBA_RANGE |NVME_STATUS_DNR ; } -if (out_bufsize <sizeof (struct nvme_zone_report )) { +/* From U32_MAX / 4 up, the byte count wraps and no longer matches NUMD. */ +if (numd >=U32_MAX /4 || + out_bufsize <sizeof (struct nvme_zone_report )) { req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd , numd);return NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ; }
Signature · 1 line
2.53.0
Christoph Hellwig
maintainerRe: [PATCH 3/5] nvmet: zns: reject a NUMD whose byte count wraps
Quoted 42 lines · Andrea Parri
Zone Management Receive computes its buffer size as (numd + 1) << 2 in u32, in
nvmet_bdev_validate_zone_mgmt_recv(),nvmet_bdev_zone_zmgmt_recv_work()andnvmet_bdev_execute_zone_mgmt_recv(). NUMD is a 32-bit dword count, so any NUMD from0x3fffffffup wraps.The data length check in
nvmet_bdev_execute_zone_mgmt_recv()sees the same wrapped value, so such a command can pass it. A data length that does not match the wrapped size is already rejected by that check, with Data SGL Length Invalid. Every site agrees on the wrapped size, so the target zeroes and fills only that many bytes and nothing leaks, but it answers a command whose NUMD asks for 4 GiB or more.Reject a NUMD from
U32_MAX/ 4 up with Invalid Field, next to the existing check for a NUMD smaller than the report header. Computing the size insize_tinstead would still wrap on 32-bit kernels, wherenvmet_check_transfer_len()andreq->transfer_lenare 32 bits wide.
nvmet_io_cmd_transfer_len()incore.cuses the same expression and is left alone here.Tested over the loop, tcp, rdma and fc transports with a zoned
null_blknamespace: a wrapping NUMD, sent with a data length that matches the wrapped size, was accepted before this patch and now fails with Invalid Field, while ordinary NUMD values still succeed.Fixes aaf2e048af27 ("nvmet: add ZBD over ZNS backend support")Assisted-by LLMSigned-off-by Andrea Parri <[email protected]>--- drivers/nvme/target/zns.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)drivers/nvme/target/zns.c +0
-1@@ -150,14 +150,17 @@ void nvmet_execute_identify_ns_zns(struct nvmet_req *req)static u16 nvmet_bdev_validate_zone_mgmt_recv (struct nvmet_req *req) {sector_t sect =nvmet_lba_to_sect (req->ns, req->cmd->zmr.slba); -u32 out_bufsize = (le32_to_cpu (req->cmd->zmr.numd) +1 ) <<2 ;
drivers/nvme/target/zns.c void nvmet_execute_identify_ns_zns(struct nvmet_req *req) · line 161
Quoted 10 lines
drivers/nvme/target/zns.c +3
-1+u32 numd =le32_to_cpu (req->cmd->zmr.numd); +u32 out_bufsize = (numd +1 ) <<2 ;if (sect >=get_capacity (req->ns->bdev->bd_disk)) { req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd , slba);return NVME_SC_LBA_RANGE |NVME_STATUS_DNR ; } -if (out_bufsize <sizeof (struct nvme_zone_report )) { +/* From U32_MAX / 4 up, the byte count wraps and no longer matches NUMD. */
Overly long line here.
drivers/nvme/target/zns.c void nvmet_execute_identify_ns_zns(struct nvmet_req *req) · line 163
Quoted 2 lines
drivers/nvme/target/zns.c +2
-0+if (numd >=U32_MAX /4 || + out_bufsize <sizeof (struct nvme_zone_report )) {
I guess to make various sanitizers happy we probably should only calculate out_bufsize after bounds checking numd?
Otherwise this looks good.
Re: [PATCH 3/5] nvmet: zns: reject a NUMD whose byte count wraps
Quoted 9 lines · Christoph Hellwig
drivers/nvme/target/zns.c void nvmet_execute_identify_ns_zns(struct nvmet_req *req) · line 161
Quoted 1 line
drivers/nvme/target/zns.c +1
-0+/* From U32_MAX / 4 up, the byte count wraps and no longer matches NUMD. */ Overly long line here.
drivers/nvme/target/zns.c void nvmet_execute_identify_ns_zns(struct nvmet_req *req) · line 163
Quoted 2 lines
drivers/nvme/target/zns.c +2
-0+if (numd >=U32_MAX /4 || + out_bufsize <sizeof (struct nvme_zone_report )) {I guess to make various sanitizers happy we probably should only calculate
out_bufsizeafter bounds checking numd?
Agreed. For v2 I plan to move the check to the start of nvmet_bdev_execute_zone_mgmt_recv(), before the handler or its work item computes (numd + 1) << 2, and to make nvmet_io_cmd_transfer_len() return 0 for such a NUMD, so pci-epf doesn't compute it either. I'll also keep the comments within 80 columns.
Thanks,
Andrea
PATCH 4/5 nvmet: pci-epf: report transport errors in command completions
In reply to Andrea ParriWhen the PCI endpoint target fails a command itself, the host receives a successful completion. This happens for a data length above MDTS, malformed PRPs or SGLs, a buffer allocation failure, an SQ that is not live, and a failed DMA of the command data. A write whose data cannot be fetched from the host is thus reported as written, though nothing was executed.
Each of these paths sets iod->status and calls nvmet_pci_epf_complete_iod(), which then reloads iod->status from iod->cqe.status. The nvmet core only fills that field when it completes the request, which these paths never do, so it is still zero and nvmet_pci_epf_cq_work() posts a success status to the host.
Drop the reload. Every path already sets iod->status before completion:nvmet_pci_epf_queue_response() copies it from the request's CQE, and the transport error paths set it themselves.
Found by code inspection. Build-tested only (x86_64, W=1): no PCI endpoint hardware was available to run it.
--- drivers/nvme/target/pci-epf.c | 1 - 1 file changed, 1 deletion(-)
drivers/nvme/target/pci-epf.c +0 -1
@@ -1152,7 +1152,6 @@ static void nvmet_pci_epf_complete_iod(struct nvmet_pci_epf_iod *iod)unsigned long flags;/* Print an error message for failed commands, except AENs. */ - iod->status =le16_to_cpu (iod->cqe.status) >>1 ;if (iod->status && iod->cmd.common.opcode != nvme_admin_async_event) dev_err(iod->ctrl->dev, "CQ[%d]: Command %s (0x%x) status 0x%0x\n",
Signature · 1 line
2.53.0
Christoph Hellwig
maintainerRe: [PATCH 4/5] nvmet: pci-epf: report transport errors in command completions
I think this looks good. Damien/Niklas, can you test this on hardware?
Quoted 42 lines · Andrea Parri
When the PCI endpoint target fails a command itself, the host receives a successful completion. This happens for a data length above MDTS, malformed PRPs or SGLs, a buffer allocation failure, an SQ that is not live, and a failed DMA of the command data. A write whose data cannot be fetched from the host is thus reported as written, though nothing was executed.
Each of these paths sets
iod->statusand callsnvmet_pci_epf_complete_iod(), which then reloadsiod->statusfromiod->cqe.status. The nvmet core only fills that field when it completes the request, which these paths never do, so it is still zero andnvmet_pci_epf_cq_work()posts a success status to the host.Drop the reload. Every path already sets
iod->statusbefore completion:nvmet_pci_epf_queue_response()copies it from the request's CQE, and the transport error paths set it themselves.Found by code inspection. Build-tested only (
x86_64, W=1): no PCI endpoint hardware was available to run it.Fixes 0faa0fe6f90e ("nvmet: New NVMe PCI endpoint function target driver")Assisted-by LLMSigned-off-by Andrea Parri <[email protected]>--- drivers/nvme/target/pci-epf.c | 1 - 1 file changed, 1 deletion(-)drivers/nvme/target/pci-epf.c +0
-1@@ -1152,7 +1152,6 @@ static void nvmet_pci_epf_complete_iod(struct nvmet_pci_epf_iod *iod)unsigned long flags;/* Print an error message for failed commands, except AENs. */ - iod->status =le16_to_cpu (iod->cqe.status) >>1 ;if (iod->status && iod->cmd.common.opcode != nvme_admin_async_event) dev_err(iod->ctrl->dev, "CQ[%d]: Command %s (0x%x) status 0x%0x\n",Signature · 1 line
2.53.0
---end quoted text---
Re: [PATCH 4/5] nvmet: pci-epf: report transport errors in command completions
Quoted 1 line · Christoph Hellwig
I think this looks good. Damien/Niklas, can you test this on hardware?
I do not have the hardware available right now. Niklas ? Can you test this?
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH 4/5] nvmet: pci-epf: report transport errors in command completions
Quoted 4 lines · Damien Le Moal
Quoted 1 line · Christoph Hellwig
I think this looks good. Damien/Niklas, can you test this on hardware?
I do not have the hardware available right now. Niklas ? Can you test this?
Thanks, Damien. I don't have PCI endpoint hardware either, so this and the SGL patch are build-tested only. I'll keep Niklas on Cc: for v2.
Thanks,
Andrea
Niklas Cassel
Tested-byRe: [PATCH 4/5] nvmet: pci-epf: report transport errors in command completions
In reply to Damien Le MoalQuoted 4 lines · Damien Le Moal
Quoted 1 line · Christoph Hellwig
I think this looks good. Damien/Niklas, can you test this on hardware?
I do not have the hardware available right now. Niklas ? Can you test this?
I understand that these changes are not supposed to affect performance, but posting it for completeness:
Before patches:
# ./fio-test.sh /dev/nvme0n1 Running on /dev/nvme0n1... Rnd read, 4KB, QD=1, 1 job : IOPS=3088, BW=12.1MiB/s (12.7MB/s) Rnd read, 4KB, QD=32, 1 job : IOPS=45.4k, BW=177MiB/s (186MB/s) Rnd read, 4KB, QD=32, 4 jobs: IOPS=47.8k, BW=187MiB/s (196MB/s) Rnd read, 128KB, QD=1, 1 job : IOPS=1803, BW=225MiB/s (236MB/s) Rnd read, 128KB, QD=32, 1 job : IOPS=9078, BW=1135MiB/s (1190MB/s) Rnd read, 128KB, QD=32, 4 jobs: IOPS=10.0k, BW=1253MiB/s (1314MB/s) Rnd read, 512KB, QD=1, 1 job : IOPS=1447, BW=724MiB/s (759MB/s) Rnd read, 512KB, QD=32, 1 job : IOPS=2415, BW=1208MiB/s (1267MB/s) Rnd read, 512KB, QD=32, 4 jobs: IOPS=2686, BW=1343MiB/s (1409MB/s) Rnd write, 4KB, QD=1, 1 job : IOPS=3046, BW=11.9MiB/s (12.5MB/s) Rnd write, 4KB, QD=32, 1 job : IOPS=22.9k, BW=89.3MiB/s (93.6MB/s) Rnd write, 4KB, QD=32, 4 jobs: IOPS=22.9k, BW=89.6MiB/s (94.0MB/s) Rnd write, 128KB, QD=1, 1 job : IOPS=1555, BW=194MiB/s (204MB/s) Rnd write, 128KB, QD=32, 1 job : IOPS=6183, BW=773MiB/s (810MB/s) Rnd write, 128KB, QD=32, 4 jobs: IOPS=6903, BW=863MiB/s (905MB/s) Seq read, 128KB, QD=1, 1 job : IOPS=1672, BW=209MiB/s (219MB/s) Seq read, 128KB, QD=32, 1 job : IOPS=9033, BW=1129MiB/s (1184MB/s) Seq read, 512KB, QD=1, 1 job : IOPS=1580, BW=790MiB/s (829MB/s) Seq read, 512KB, QD=32, 1 job : IOPS=2536, BW=1268MiB/s (1330MB/s) Seq read, 1MB, QD=32, 1 job : IOPS=1297, BW=1297MiB/s (1360MB/s) Seq write, 128KB, QD=1, 1 job : IOPS=1547, BW=193MiB/s (203MB/s) Seq write, 128KB, QD=32, 1 job : IOPS=6563, BW=820MiB/s (860MB/s) Seq write, 512KB, QD=1, 1 job : IOPS=893, BW=447MiB/s (469MB/s) Seq write, 512KB, QD=32, 1 job : IOPS=1667, BW=834MiB/s (874MB/s) Seq write, 1MB, QD=32, 1 job : IOPS=762, BW=763MiB/s (800MB/s) Rnd rdwr, 4K..1MB, QD=8, 4 jobs: IOPS=788, BW=396MiB/s (415MB/s) IOPS=811, BW=408MiB/s (427MB/s)
After patches:
# ./fio-test.sh /dev/nvme0n1 Running on /dev/nvme0n1... Rnd read, 4KB, QD=1, 1 job : IOPS=2958, BW=11.6MiB/s (12.1MB/s) Rnd read, 4KB, QD=32, 1 job : IOPS=44.5k, BW=174MiB/s (182MB/s) Rnd read, 4KB, QD=32, 4 jobs: IOPS=47.4k, BW=185MiB/s (194MB/s) Rnd read, 128KB, QD=1, 1 job : IOPS=1741, BW=218MiB/s (228MB/s) Rnd read, 128KB, QD=32, 1 job : IOPS=9327, BW=1166MiB/s (1223MB/s) Rnd read, 128KB, QD=32, 4 jobs: IOPS=10.2k, BW=1280MiB/s (1342MB/s) Rnd read, 512KB, QD=1, 1 job : IOPS=1444, BW=722MiB/s (758MB/s) Rnd read, 512KB, QD=32, 1 job : IOPS=2505, BW=1253MiB/s (1313MB/s) Rnd read, 512KB, QD=32, 4 jobs: IOPS=2516, BW=1258MiB/s (1319MB/s) Rnd write, 4KB, QD=1, 1 job : IOPS=3322, BW=13.0MiB/s (13.6MB/s) Rnd write, 4KB, QD=32, 1 job : IOPS=22.8k, BW=89.1MiB/s (93.5MB/s) Rnd write, 4KB, QD=32, 4 jobs: IOPS=22.9k, BW=89.4MiB/s (93.7MB/s) Rnd write, 128KB, QD=1, 1 job : IOPS=1529, BW=191MiB/s (201MB/s) Rnd write, 128KB, QD=32, 1 job : IOPS=6369, BW=796MiB/s (835MB/s) Rnd write, 128KB, QD=32, 4 jobs: IOPS=6449, BW=806MiB/s (845MB/s) Seq read, 128KB, QD=1, 1 job : IOPS=1690, BW=211MiB/s (222MB/s) Seq read, 128KB, QD=32, 1 job : IOPS=9262, BW=1158MiB/s (1214MB/s) Seq read, 512KB, QD=1, 1 job : IOPS=1428, BW=714MiB/s (749MB/s) Seq read, 512KB, QD=32, 1 job : IOPS=2652, BW=1326MiB/s (1391MB/s) Seq read, 1MB, QD=32, 1 job : IOPS=1301, BW=1302MiB/s (1365MB/s) Seq write, 128KB, QD=1, 1 job : IOPS=1531, BW=191MiB/s (201MB/s) Seq write, 128KB, QD=32, 1 job : IOPS=6144, BW=768MiB/s (805MB/s) Seq write, 512KB, QD=1, 1 job : IOPS=924, BW=462MiB/s (485MB/s) Seq write, 512KB, QD=32, 1 job : IOPS=1468, BW=734MiB/s (770MB/s) Seq write, 1MB, QD=32, 1 job : IOPS=749, BW=749MiB/s (786MB/s) Rnd rdwr, 4K..1MB, QD=8, 4 jobs: IOPS=786, BW=394MiB/s (414MB/s) IOPS=809, BW=407MiB/s (426MB/s)
Also used ftrace on the EP side to see that SGLs were actually being used:
kworker/u32:2-75 [005] ..... 300.152187: nvmet_pci_epf_iod_parse_sgls <-nvmet_pci_epf_exec_iod_work kworker/u32:2-75 [005] ..... 300.152560: nvmet_pci_epf_iod_parse_sgls <-nvmet_pci_epf_exec_iod_work kworker/u32:2-75 [005] ..... 300.153126: nvmet_pci_epf_iod_parse_sgls <-nvmet_pci_epf_exec_iod_work kworker/u32:2-75 [005] ..... 300.153702: nvmet_pci_epf_iod_parse_sgls <-nvmet_pci_epf_exec_iod_work kworker/u32:2-75 [005] ..... 300.154273: nvmet_pci_epf_iod_parse_sgls <-nvmet_pci_epf_exec_iod_work kworker/u32:2-75 [005] ..... 300.154807: nvmet_pci_epf_iod_parse_sgls <-nvmet_pci_epf_exec_iod_work kworker/u32:2-75 [005] ..... 300.155445: nvmet_pci_epf_iod_parse_sgls <-nvmet_pci_epf_exec_iod_work kworker/u32:2-75 [005] ..... 300.156263: nvmet_pci_epf_iod_parse_sgls <-nvmet_pci_epf_exec_iod_work kworker/u32:2-75 [005] ..... 300.156676: nvmet_pci_epf_iod_parse_sgls <-nvmet_pci_epf_exec_iod_work kworker/u32:2-75 [005] ..... 300.157340: nvmet_pci_epf_iod_parse_sgls <-nvmet_pci_epf_exec_iod_work kworker/u32:2-75 [005] ..... 300.158096: nvmet_pci_epf_iod_parse_sgls <-nvmet_pci_epf_exec_iod_work
I can not see anything wrong, thus:
PATCH 5/5 nvmet: pci-epf: require SGLs to match the command data length
In reply to Andrea ParriThe PCI endpoint target sends the host every byte a read command's SGL describes, even when the SGL describes more than the command transfers. The excess is kmalloc() memory the command never wrote, so the host receives stale target memory. Any successful read can do this, including Identify.
nvmet_pci_epf_alloc_iod_data_buf() allocates each segment at the length of its SGL data descriptor, but sets req->transfer_len from iod->data_len, the length the command implies. Once the command completes, nvmet_pci_epf_transfer_iod_data() sends every segment in full. Nothing compares the descriptor lengths with iod->data_len, and the MDTS check only bounds iod->data_len.
Fail the command with Data SGL Length Invalid when the data descriptors do not add up to iod->data_len, before any segment is allocated. The tcp and rdma transports take the data length from the SGL itself, so there most commands already reject a mismatch in nvmet_check_transfer_len(). PRP lists are built from iod->data_len and are unaffected.
Found by code inspection. Build-tested only (x86_64, W=1): no PCI endpoint hardware was available to run it.
---
For stable, this depends on patch 4/5: without it, the new Data SGL Length Invalid error is still reported to the host as a success.
drivers/nvme/target/pci-epf.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-)
drivers/nvme/target/pci-epf.c +27 -2
@@ -1073,10 +1073,22 @@ static int nvmet_pci_epf_iod_parse_sgl_segments(struct nvmet_pci_epf_ctrl *ctrl,return 0 ; } +static u64 nvmet_pci_epf_iod_data_segs_len (struct nvmet_pci_epf_iod *iod) +{ +u64 len =0 ; +int i; + +for (i =0 ; i < iod->nr_data_segs; i++) + len += iod->data_segs[i].length; + +return len; +} +static int nvmet_pci_epf_iod_parse_sgls (struct nvmet_pci_epf_iod *iod) {struct nvmet_pci_epf_ctrl *ctrl = iod->ctrl;struct nvme_sgl_desc *sgl = &iod->cmd.common.dptr.sgl; +int ret;if (sgl->type == (NVME_SGL_FMT_DATA_DESC <<4 )) {/* Single data descriptor case. */
@@ -1084,10 +1096,23 @@ static int nvmet_pci_epf_iod_parse_sgls(struct nvmet_pci_epf_iod *iod) iod->data_segs = &iod->data_seg; iod->data_seg.pci_addr =le64_to_cpu (sgl->addr); iod->data_seg.length =le32_to_cpu (sgl->length); -return 0 ; + }else { + ret =nvmet_pci_epf_iod_parse_sgl_segments (ctrl, iod); +if (ret) +return ret; } -return nvmet_pci_epf_iod_parse_sgl_segments (ctrl, iod); +/* +* The command executes on data_len bytes, but each segment is +* allocated and transferred in full, so they must describe exactly +* data_len bytes. +*/ +if (nvmet_pci_epf_iod_data_segs_len (iod) != iod->data_len) { + iod->status =NVME_SC_SGL_INVALID_DATA |NVME_STATUS_DNR ; +return -EINVAL ; + } + +return 0 ; }static int nvmet_pci_epf_alloc_iod_data_buf (struct nvmet_pci_epf_iod *iod)
Signature · 1 line
2.53.0
Christoph Hellwig
maintainerRe: [PATCH 5/5] nvmet: pci-epf: require SGLs to match the command data length
Quoted 20 lines · Andrea Parri
The PCI endpoint target sends the host every byte a read command's SGL describes, even when the SGL describes more than the command transfers. The excess is
kmalloc()memory the command never wrote, so the host receives stale target memory. Any successful read can do this, including Identify.
nvmet_pci_epf_alloc_iod_data_buf()allocates each segment at the length of its SGL data descriptor, but setsreq->transfer_lenfromiod->data_len, the length the command implies. Once the command completes,nvmet_pci_epf_transfer_iod_data()sends every segment in full. Nothing compares the descriptor lengths withiod->data_len, and the MDTS check only boundsiod->data_len.Fail the command with Data SGL Length Invalid when the data descriptors do not add up to
iod->data_len, before any segment is allocated. The tcp and rdma transports take the data length from the SGL itself, so there most commands already reject a mismatch innvmet_check_transfer_len(). PRP lists are built fromiod->data_lenand are unaffected.
Quoted 2 lines
Found by code inspection. Build-tested only (
x86_64, W=1): no PCI endpoint hardware was available to run it.
Looks good, although again having this validated on hardware would be really useful.
Niklas Cassel
Tested-byRe: [PATCH 5/5] nvmet: pci-epf: require SGLs to match the command data length
Quoted 21 lines · Christoph Hellwig
Quoted 20 lines · Andrea Parri
The PCI endpoint target sends the host every byte a read command's SGL describes, even when the SGL describes more than the command transfers. The excess is
kmalloc()memory the command never wrote, so the host receives stale target memory. Any successful read can do this, including Identify.
nvmet_pci_epf_alloc_iod_data_buf()allocates each segment at the length of its SGL data descriptor, but setsreq->transfer_lenfromiod->data_len, the length the command implies. Once the command completes,nvmet_pci_epf_transfer_iod_data()sends every segment in full. Nothing compares the descriptor lengths withiod->data_len, and the MDTS check only boundsiod->data_len.Fail the command with Data SGL Length Invalid when the data descriptors do not add up to
iod->data_len, before any segment is allocated. The tcp and rdma transports take the data length from the SGL itself, so there most commands already reject a mismatch innvmet_check_transfer_len(). PRP lists are built fromiod->data_lenand are unaffected.
Quoted 6 lines
Quoted 2 lines
Found by code inspection. Build-tested only (
x86_64, W=1): no PCI endpoint hardware was available to run it.Looks good, although again having this validated on hardware would be really useful.