Skip to contentSkip to conversation

PCI: quirks: Fix out-of-bounds MMIO read in nvme_disable_and_flr()

PATCH Superseded Mohamad Raizudeen 3 participants 4 messages Sep 3 PCI

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 PCI: quirks: Fix out-of-bounds MMIO read in nvme_disable_and_flr()

In nvme_disable_and_flr(), the PCI bar is mapped using NVME_REG_CC + sizeof(cfg) which is (0x14 + 4 = 0x18 bytes)

However, the function later reads the controller status from NVME_REG_CSTS - offset 0x1C, which is outside the mapped 0x18 byte boundary. Reading past the mapped MMIO region is undefined behavior and can return invalid data.

Fix this by increasing the mapping size to include NVME_REG_CSTS.

Fixes ffb0863426eb9 ("PCI: Disable Samsung SM961/PM961 NVMe before FLR")

---
Changes in v2:
- Changed the commit message to avoid overstating the risk as pointed out by Alex Williamson, to be more accurate about the actual behavior.

 drivers/pci/quirks.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/quirks.c +1 -1
index b09f27f7846f..ed03892cc960 100644
@@ -4090,7 +4090,7 @@ static int nvme_disable_and_flr(struct pci_dev *dev, bool probe)
 	if (probe)
 		return 0;
 
-	bar = pci_iomap(dev, 0, NVME_REG_CC + sizeof(cfg));
+	bar = pci_iomap(dev, 0, NVME_REG_CSTS + sizeof(cfg));
 	if (!bar)
 		return -ENOTTY;
 
Signature · 1 line
2.53.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

In nvme_disable_and_flr(), the PCI bar is mapped using NVME_REG_CC + sizeof(cfg) which is (0x14 + 4 = 0x18 bytes)

However, the function later reads the controller status from NVME_REG_CSTS - offset 0x1C, which is outside the mapped 0x18 byte boundary. Reading past the mapped MMIO region is undefined behavior and can return invalid data.

Quoted 4 lines

Fix this by increasing the mapping size to include NVME_REG_CSTS.

Fixes ffb0863426eb9 ("PCI: Disable Samsung SM961/PM961 NVMe before FLR")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Mohamad Raizudeen
Quoted 7 lines · On Thu, 3 Sep 2026 09:30:49 +0530 Mohamad Raizudeen…

In nvme_disable_and_flr(), the PCI bar is mapped using NVME_REG_CC + sizeof(cfg) which is (0x14 + 4 = 0x18 bytes)

However, the function later reads the controller status from NVME_REG_CSTS - offset 0x1C, which is outside the mapped 0x18 byte boundary. Reading past the mapped MMIO region is undefined behavior and can return invalid data.

"Undefined behavior" still seems an overstatement. The code violates the API contract, it should be fixed, but it's effectively harmless afaict.

Quoted 18 lines

Fix this by increasing the mapping size to include NVME_REG_CSTS.

Fixes ffb0863426eb9 ("PCI: Disable Samsung SM961/PM961 NVMe before

FLR") Signed-off-by: Mohamad Raizudeen <[email protected]>
---
Changes in v2:
- Changed the commit message to avoid overstating the risk as pointed out by Alex Williamson, to be more accurate about the actual
behavior.

 drivers/pci/quirks.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/quirks.c +0 -0
index b09f27f7846f..ed03892cc960 100644

drivers/pci/quirks.c static int nvme_disable_and_flr(struct pci_dev · line 4096

Quoted 8 lines
drivers/pci/quirks.c +1 -1
@@ -4090,7 +4090,7 @@ static int nvme_disable_and_flr(struct pci_dev
 *dev, bool probe) if (probe)
 		return 0;
 
-	bar = pci_iomap(dev, 0, NVME_REG_CC + sizeof(cfg));
+	bar = pci_iomap(dev, 0, NVME_REG_CSTS + sizeof(cfg));
 	if (!bar)
 		return -ENOTTY;
 

The cfg variable is no longer related to the mapping size now, it happens to be the same size, but this should be sizeof(u32) to avoid confusion. Thanks,

Alex

#3 lore raw
Quoted 46 lines · Alex Williamson
Quoted 7 lines · On Thu, 3 Sep 2026 09:30:49 +0530 Mohamad Raizudeen…

In nvme_disable_and_flr(), the PCI bar is mapped using NVME_REG_CC + sizeof(cfg) which is (0x14 + 4 = 0x18 bytes)

However, the function later reads the controller status from NVME_REG_CSTS - offset 0x1C, which is outside the mapped 0x18 byte boundary. Reading past the mapped MMIO region is undefined behavior and can return invalid data.

"Undefined behavior" still seems an overstatement. The code violates the API contract, it should be fixed, but it's effectively harmless afaict.

Quoted 18 lines

Fix this by increasing the mapping size to include NVME_REG_CSTS.

Fixes ffb0863426eb9 ("PCI: Disable Samsung SM961/PM961 NVMe before

FLR") Signed-off-by: Mohamad Raizudeen <[email protected]>
---
Changes in v2:
- Changed the commit message to avoid overstating the risk as pointed out by Alex Williamson, to be more accurate about the actual
behavior.

 drivers/pci/quirks.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/quirks.c +0 -0
index b09f27f7846f..ed03892cc960 100644

drivers/pci/quirks.c static int nvme_disable_and_flr(struct pci_dev · line 4096

Quoted 8 lines
drivers/pci/quirks.c +1 -1
@@ -4090,7 +4090,7 @@ static int nvme_disable_and_flr(struct pci_dev
 *dev, bool probe) if (probe)
 		return 0;
 
-	bar = pci_iomap(dev, 0, NVME_REG_CC + sizeof(cfg));
+	bar = pci_iomap(dev, 0, NVME_REG_CSTS + sizeof(cfg));
 	if (!bar)
 		return -ENOTTY;
 

The cfg variable is no longer related to the mapping size now, it happens to be the same size, but this should be sizeof(u32) to avoid confusion. Thanks,

Quoted 1 line

Alex

Hi Alex,

I will make that change and update the commit message for v3.

Thanks,
Mohamad Raizudeen