nvmet-pci: validate endpoint queue IDs
Subsystems (3)
cover nvmet-pci: validate endpoint queue IDs
A PCI root-complex host can crash an NVMe PCI endpoint target with malformed queue IDs. The endpoint transport allocates its SQ/CQ arrays using ctrl->nr_queues, which is capped by endpoint interrupt capacity, but the common target admin validation only checks queue IDs against subsys->max_qid. A host can therefore submit Create/Delete SQ/CQ commands with qids that pass the common checks yet index past the smaller endpoint transport arrays.
Patch 1 rejects queue IDs outside ctrl->nr_queues before the endpoint SQ/CQ arrays are indexed. Patch 2 adds same-translation-unit KUnit/KASAN
coverage: a valid queue ID that must still be accepted and the out-of-range Create/Delete SQ/CQ cases that must now be rejected.
Reproduced with the KUnit/KASAN test: the stock Create CQ path faults in nvmet_pci_epf_create_cq() after nvmet_check_io_cqid() accepts qid 2 with max_qid 8 and nr_queues 2; patched rejects the malformed cases while the benign control still passes.
Michael Bommarito (2):
nvmet-pci: validate queue IDs against endpoint queues
nvmet-pci: add KUnit coverage for endpoint queue IDs
drivers/nvme/target/Kconfig | 11 +++ drivers/nvme/target/pci-epf.c | 151 ++++++++++++++++++++++++++++++++-- 2 files changed, 157 insertions(+), 5 deletions(-)
--
2.53.0
PATCH 1/2 nvmet-pci: validate queue IDs against endpoint queues
The NVMe PCI endpoint transport allocates SQ/CQ arrays using ctrl->nr_queues, which is capped by endpoint interrupt capacity. Common target admin validation only checks queue IDs against subsys->max_qid, so a root-complex host can submit Create/Delete SQ/CQ commands with qids that pass the common checks but index past the smaller endpoint transport arrays.
Impact: A PCI root-complex host can crash an NVMe PCI endpoint target with malformed queue IDs.
Reject queue IDs that are outside ctrl->nr_queues before indexing the endpoint SQ/CQ arrays.
---
I reproduced this with a same-translation-unit KUnit/KASAN test. The stock Create CQ path faults in nvmet_pci_epf_create_cq() after nvmet_check_io_cqid() accepts qid 2 with max_qid 8 and nr_queues 2. The patched checks reject malformed Create/Delete SQ/CQ cases while the benign control still passes.
drivers/nvme/target/pci-epf.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)
drivers/nvme/target/pci-epf.c +26 -5
@@ -1267,10 +1267,15 @@ static u16 nvmet_pci_epf_create_cq(struct nvmet_ctrl *tctrl, u16 cqid, u16 flags, u16 qsize, u64 pci_addr, u16 vector) { struct nvmet_pci_epf_ctrl *ctrl = tctrl->drvdata; -struct nvmet_pci_epf_queue *cq = &ctrl->cq[cqid]; +struct nvmet_pci_epf_queue *cq;u16 status;int ret; +if (cqid >= ctrl->nr_queues) +return NVME_SC_QID_INVALID |NVME_STATUS_DNR ; + + cq = &ctrl->cq[cqid]; +if (test_bit (NVMET_PCI_EPF_Q_LIVE , &cq->flags))return NVME_SC_QID_INVALID |NVME_STATUS_DNR ;
@@ -1348,7 +1353,12 @@ static u16 nvmet_pci_epf_create_cq(struct nvmet_ctrl *tctrl,static u16 nvmet_pci_epf_delete_cq (struct nvmet_ctrl *tctrl,u16 cqid) {struct nvmet_pci_epf_ctrl *ctrl = tctrl->drvdata; -struct nvmet_pci_epf_queue *cq = &ctrl->cq[cqid]; +struct nvmet_pci_epf_queue *cq; + +if (cqid >= ctrl->nr_queues) +return NVME_SC_QID_INVALID |NVME_STATUS_DNR ; + + cq = &ctrl->cq[cqid];if (!test_and_clear_bit (NVMET_PCI_EPF_Q_LIVE , &cq->flags))return NVME_SC_QID_INVALID |NVME_STATUS_DNR ;
@@ -1367,10 +1377,16 @@ static u16 nvmet_pci_epf_create_sq(struct nvmet_ctrl *tctrl, u16 sqid, u16 cqid, u16 flags, u16 qsize, u64 pci_addr) { struct nvmet_pci_epf_ctrl *ctrl = tctrl->drvdata; -struct nvmet_pci_epf_queue *sq = &ctrl->sq[sqid]; -struct nvmet_pci_epf_queue *cq = &ctrl->cq[cqid]; +struct nvmet_pci_epf_queue *sq; +struct nvmet_pci_epf_queue *cq;u16 status; +if (sqid >= ctrl->nr_queues || cqid >= ctrl->nr_queues) +return NVME_SC_QID_INVALID |NVME_STATUS_DNR ; + + sq = &ctrl->sq[sqid]; + cq = &ctrl->cq[cqid]; +if (test_bit (NVMET_PCI_EPF_Q_LIVE , &sq->flags))return NVME_SC_QID_INVALID |NVME_STATUS_DNR ;
@@ -1419,7 +1435,12 @@ static u16 nvmet_pci_epf_create_sq(struct nvmet_ctrl *tctrl,static u16 nvmet_pci_epf_delete_sq (struct nvmet_ctrl *tctrl,u16 sqid) {struct nvmet_pci_epf_ctrl *ctrl = tctrl->drvdata; -struct nvmet_pci_epf_queue *sq = &ctrl->sq[sqid]; +struct nvmet_pci_epf_queue *sq; + +if (sqid >= ctrl->nr_queues) +return NVME_SC_QID_INVALID |NVME_STATUS_DNR ; + + sq = &ctrl->sq[sqid];if (!test_and_clear_bit (NVMET_PCI_EPF_Q_LIVE , &sq->flags))return NVME_SC_QID_INVALID |NVME_STATUS_DNR ;
Signature · 1 line
2.53.0
Re: [PATCH 1/2] nvmet-pci: validate queue IDs against endpoint queues
Quoted 10 lines · Michael Bommarito
The NVMe PCI endpoint transport allocates SQ/CQ arrays using
ctrl->nr_queues, which is capped by endpoint interrupt capacity. Common target admin validation only checks queue IDs againstsubsys->max_qid, so a root-complex host can submit Create/Delete SQ/CQ commands with qids that pass the common checks but index past the smaller endpoint transport arrays.Impact: A PCI root-complex host can crash an NVMe PCI endpoint target with malformed queue IDs.
Quoted 7 lines
Reject queue IDs that are outside
ctrl->nr_queuesbefore indexing the endpoint SQ/CQ arrays.Fixes 0faa0fe6f90e ("nvmet: New NVMe PCI endpoint function target driver")Assisted-by Codex:gpt-5-5-xhighSigned-off-by Michael Bommarito <[email protected]>
Looks good.
Signature · 2 lines
Damien Le Moal Western Digital Research
PATCH 2/2 nvmet-pci: add KUnit coverage for endpoint queue IDs
In reply to Michael BommaritoAdd KUnit coverage for the PCI endpoint target queue-id boundary. The tests model the case where target-core max_qid is larger than the endpoint transport's ctrl->nr_queues, confirm the common qid check accepts the malformed id, and verify the endpoint callbacks reject out-of-range Create/Delete SQ/CQ requests before indexing transport-private arrays.
This covers the regression fixed by the preceding patch.
--- drivers/nvme/target/Kconfig | 11 ++++ drivers/nvme/target/pci-epf.c | 120 ++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+)
drivers/nvme/target/Kconfig +11 -0
@@ -127,3 +127,14 @@ config NVME_TARGET_PCI_EPF capable PCI controller. If unsure, say N. + +config NVMET_PCI_EPF_KUNIT_TEST + bool "NVMe PCI endpoint target KUnit tests" if !KUNIT_ALL_TESTS + depends on KUNIT + depends on NVME_TARGET_PCI_EPF=y + default KUNIT_ALL_TESTS + help + KUnit tests for the NVMe PCI endpoint target transport. + These tests exercise transport-private queue ID checks for + Create/Delete SQ/CQ commands when target-core max_qid is larger than + the endpoint controller's available queue arrays.
drivers/nvme/target/pci-epf.c +120 -0
129 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [PATCH 2/2] nvmet-pci: add KUnit coverage for endpoint queue IDs
Quoted 10 lines · Michael Bommarito
Add KUnit coverage for the PCI endpoint target queue-id boundary. The tests model the case where target-core
max_qidis larger than the endpoint transport'sctrl->nr_queues, confirm the common qid check accepts the malformed id, and verify the endpoint callbacks reject out-of-range Create/Delete SQ/CQ requests before indexing transport-private arrays.This covers the regression fixed by the preceding patch.
Assisted-by Codex:gpt-5-5-xhighSigned-off-by Michael Bommarito <[email protected]>
I am not very familiar with kunit tests, but looks OK.
Signature · 2 lines
Damien Le Moal Western Digital Research