Skip to contentSkip to conversation

nvmet: pci-epf: fix heap overflow for invalid I/O SQES/CQES from the host

PATCH In review Junrui Luo 2 participants 2 messages Apr 24 – Apr 27

Subsystems (3)
lore
Share & tools
lore

Junrui Luo

author
#0 lore raw

PATCH 1/1 nvmet: pci-epf: fix heap overflow for invalid I/O SQES/CQES from the host

nvmet_pci_epf_enable_ctrl() computes ctrl->io_sqes and ctrl->io_cqes from the host-controlled CC.IOSQES/CC.IOCQES fields and only rejects values below sizeof(struct nvme_command) / sizeof(struct nvme_completion). The resulting sizes are used as DMA transfer lengths against the fixed-size iod->cmd (64B) and iod->cqe (16B) buffers.

An oversized IOSQES causes nvmet_pci_epf_transfer() to overflow iod->cmd with host-controlled data, and an oversized IOCQES causes memcpy_toio() to leak adjacent slab memory back to the host.

Change both checks from '<' to '!='.

Fixes 0faa0fe6f90e ("nvmet: New NVMe PCI endpoint function target driver")
---
 drivers/nvme/target/pci-epf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/nvme/target/pci-epf.c +2 -2
index 4e9db96ebfec..4fdd92508609 100644
@@ -1859,14 +1859,14 @@ static int nvmet_pci_epf_enable_ctrl(struct nvmet_pci_epf_ctrl *ctrl)
 	ctrl->mps_mask = ctrl->mps - 1;
 
 	ctrl->io_sqes = 1UL << nvmet_cc_iosqes(ctrl->cc);
-	if (ctrl->io_sqes < sizeof(struct nvme_command)) {
+	if (ctrl->io_sqes != sizeof(struct nvme_command)) {
 		dev_err(ctrl->dev, "Unsupported I/O SQES %zu (need %zu)\n",
 			ctrl->io_sqes, sizeof(struct nvme_command));
 		goto err;
 	}
 
 	ctrl->io_cqes = 1UL << nvmet_cc_iocqes(ctrl->cc);
-	if (ctrl->io_cqes < sizeof(struct nvme_completion)) {
+	if (ctrl->io_cqes != sizeof(struct nvme_completion)) {
 		dev_err(ctrl->dev, "Unsupported I/O CQES %zu (need %zu)\n",
 			ctrl->io_cqes, sizeof(struct nvme_completion));
 		goto err;

---
base-commit: 7aaa8047eafd0bd628065b15757d9b48c5f9c07d
change-id: 20260424-fixes-5ec30cad02cc

Best regards,

Signature · 1 line
Junrui Luo <[email protected]>
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Junrui Luo

nvmet_pci_epf_enable_ctrl() computes ctrl->io_sqes and ctrl->io_cqes from the host-controlled CC.IOSQES/CC.IOCQES fields and only rejects values below sizeof(struct nvme_command) / sizeof(struct nvme_completion). The resulting sizes are used as DMA transfer lengths against the fixed-size iod->cmd (64B) and iod->cqe (16B) buffers.

An oversized IOSQES causes nvmet_pci_epf_transfer() to overflow iod->cmd with host-controlled data, and an oversized IOCQES causes memcpy_toio() to leak adjacent slab memory back to the host.

Quoted 6 lines

Change both checks from '<' to '!='.

Fixes 0faa0fe6f90e ("nvmet: New NVMe PCI endpoint function target driver")

Looks OK.

Signature · 2 lines
Damien Le Moal
Western Digital Research