Skip to contentSkip to conversation

HID: amd_sfh: Validate PCI BAR size before mapping

RFC PATCH syzbot 2 participants 2 messages Aug 26

Subsystems (2)
lore
Share & tools
lore

syzbot

author syzbot
#0 lore raw

The amd_sfh driver maps PCI BAR 2 using pcim_iomap_regions() and subsequently accesses MMIO registers at offsets up to 0x10958 (e.g., AMD_P2C_MSG3 at 0x1068C). However, the driver never validates that the BAR size is large enough to cover these accesses. If the driver is bound to a device with a smaller BAR 2, this leads to an out-of-bounds memory access and a page fault during the probe function.

For example, a page fault can occur when reading from privdata->mmio + AMD_P2C_MSG3 in mp2_select_ops():

  BUG: unable to handle page fault for address: ffffc9000390368c
  PGD 100000067 P4D 100000067 PUD 1012c1067 PMD 105b64067 PTE 0
  Oops: Oops: 0000 [#1] SMP KASAN NOPTI
  RIP: 0010:readl arch/x86/include/asm/io.h:59 [inline]
  RIP: 0010:mp2_select_ops drivers/hid/amd-sfh-hid/amd_sfh_pcie.c:282
  [inline]
  RIP: 0010:amd_mp2_pci_probe+0x337/0x5f0
  drivers/hid/amd-sfh-hid/amd_sfh_pcie.c:487
  Call Trace:
   <TASK>
   local_pci_probe drivers/pci/pci-driver.c:332 [inline]
   pci_call_probe drivers/pci/pci-driver.c:394 [inline]
   __pci_device_probe drivers/pci/pci-driver.c:455 [inline]
   pci_device_probe+0x431/0xc90 drivers/pci/pci-driver.c:489

Fix this by verifying that the length of BAR 2 is at least 128KB before attempting to map it. Since the maximum accessed offset is 0x10958, and PCI BAR sizes are powers of 2, any legitimate hardware will have a BAR size of at least 128KB.

Fixes 4f567b9f8141 ("SFH: PCIe driver to add support of AMD sensor fusion hub")
Assisted-by Gemini:gemini-3.7-flash Gemini:gemini-3.1-pro-preview syzbot
Reported-by [email protected]

To: "Basavaraj Natikar" <[email protected]>
To: "Benjamin Tissoires" <[email protected]>
To: "Jiri Kosina" <[email protected]>
To: <[email protected]>
To: "Sandeep Singh" <[email protected]>
Cc: <[email protected]>
Cc: "Mario Limonciello (AMD)" <[email protected]>

---
v2:
- Define AMD_SFH_MIN_BAR_SIZE macro instead of hardcoded 128 * 1024
- Use AMD_SFH_MIN_BAR_SIZE in amd_sfh1_1_init()

v1:
https://lore.kernel.org/all/[email protected]/T/
---

drivers/hid/amd-sfh-hid/amd_sfh_common.h +2 -0
index 78f830c13..c0c324565 100644
@@ -17,6 +17,8 @@
 #define PCI_DEVICE_ID_AMD_MP2		0x15E4
 #define PCI_DEVICE_ID_AMD_MP2_1_1	0x164A
 
+#define AMD_SFH_MIN_BAR_SIZE		(128 * 1024)
+
 #define AMD_C2P_MSG(regno) (0x10500 + ((regno) * 4))
 #define AMD_P2C_MSG(regno) (0x10680 + ((regno) * 4))
 
drivers/hid/amd-sfh-hid/amd_sfh_pcie.c +5 -0
index 4b81cebdc..d870585a0 100644
@@ -451,6 +451,11 @@ static int amd_mp2_pci_probe(struct pci_dev *pdev, const struct pci_device_id *i
 	if (rc)
 		return rc;
 
+	if (pci_resource_len(pdev, 2) < AMD_SFH_MIN_BAR_SIZE) {
+		dev_err(&pdev->dev, "BAR 2 is too small\n");
+		return -EINVAL;
+	}
+
 	rc = pcim_iomap_regions(pdev, BIT(2), DRIVER_NAME);
 	if (rc)
 		return rc;
drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_init.c +2 -2
index b0bab2a1d..0afc596b5 100644
@@ -388,12 +388,12 @@ int amd_sfh1_1_init(struct amd_mp2_dev *mp2)
 	int rc;
 
 	phy_base <<= 21;
-	if (!devm_request_mem_region(dev, phy_base, 128 * 1024, "amd_sfh")) {
+	if (!devm_request_mem_region(dev, phy_base, AMD_SFH_MIN_BAR_SIZE, "amd_sfh")) {
 		dev_dbg(dev, "can't reserve mmio registers\n");
 		return -ENOMEM;
 	}
 
-	mp2->vsbase = devm_ioremap(dev, phy_base, 128 * 1024);
+	mp2->vsbase = devm_ioremap(dev, phy_base, AMD_SFH_MIN_BAR_SIZE);
 	if (!mp2->vsbase) {
 		dev_dbg(dev, "failed to remap vsbase\n");
 		return -ENOMEM;

base-commit: db2ddb87143519e20a95aa36c60b36107b736a58

Signature · 9 lines
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).

See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at [email protected].
#1 lore raw
Quoted 69 lines · syzbot

The amd_sfh driver maps PCI BAR 2 using pcim_iomap_regions() and subsequently accesses MMIO registers at offsets up to 0x10958 (e.g., AMD_P2C_MSG3 at 0x1068C). However, the driver never validates that the BAR size is large enough to cover these accesses. If the driver is bound to a device with a smaller BAR 2, this leads to an out-of-bounds memory access and a page fault during the probe function.

For example, a page fault can occur when reading from privdata->mmio + AMD_P2C_MSG3 in mp2_select_ops():

  BUG: unable to handle page fault for address: ffffc9000390368c
  PGD 100000067 P4D 100000067 PUD 1012c1067 PMD 105b64067 PTE 0
  Oops: Oops: 0000 [#1] SMP KASAN NOPTI
  RIP: 0010:readl arch/x86/include/asm/io.h:59 [inline]
  RIP: 0010:mp2_select_ops drivers/hid/amd-sfh-hid/amd_sfh_pcie.c:282
  [inline]
  RIP: 0010:amd_mp2_pci_probe+0x337/0x5f0
  drivers/hid/amd-sfh-hid/amd_sfh_pcie.c:487
  Call Trace:
   <TASK>
   local_pci_probe drivers/pci/pci-driver.c:332 [inline]
   pci_call_probe drivers/pci/pci-driver.c:394 [inline]
   __pci_device_probe drivers/pci/pci-driver.c:455 [inline]
   pci_device_probe+0x431/0xc90 drivers/pci/pci-driver.c:489

Fix this by verifying that the length of BAR 2 is at least 128KB before attempting to map it. Since the maximum accessed offset is 0x10958, and PCI BAR sizes are powers of 2, any legitimate hardware will have a BAR size of at least 128KB.

Fixes 4f567b9f8141 ("SFH: PCIe driver to add support of AMD sensor fusion hub")
Assisted-by Gemini:gemini-3.7-flash Gemini:gemini-3.1-pro-preview syzbot
Reported-by [email protected]

To: "Basavaraj Natikar" <[email protected]>
To: "Benjamin Tissoires" <[email protected]>
To: "Jiri Kosina" <[email protected]>
To: <[email protected]>
To: "Sandeep Singh" <[email protected]>
Cc: <[email protected]>
Cc: "Mario Limonciello (AMD)" <[email protected]>

---
v2:
- Define AMD_SFH_MIN_BAR_SIZE macro instead of hardcoded 128 * 1024
- Use AMD_SFH_MIN_BAR_SIZE in amd_sfh1_1_init()

v1:
https://lore.kernel.org/all/[email protected]/T/
---

drivers/hid/amd-sfh-hid/amd_sfh_common.h +2 -0
index 78f830c13..c0c324565 100644
@@ -17,6 +17,8 @@
 #define PCI_DEVICE_ID_AMD_MP2		0x15E4
 #define PCI_DEVICE_ID_AMD_MP2_1_1	0x164A
 
+#define AMD_SFH_MIN_BAR_SIZE		(128 * 1024)
+
 #define AMD_C2P_MSG(regno) (0x10500 + ((regno) * 4))
 #define AMD_P2C_MSG(regno) (0x10680 + ((regno) * 4))
 
drivers/hid/amd-sfh-hid/amd_sfh_pcie.c +0 -0
index 4b81cebdc..d870585a0 100644

drivers/hid/amd-sfh-hid/amd_sfh_pcie.c static int amd_mp2_pci_probe(struct pci_dev *pdev, const struct pci_device_id *i · line 452

Quoted 2 lines
drivers/hid/amd-sfh-hid/amd_sfh_pcie.c +0 -0
@@ -451,6 +451,11 @@ static int amd_mp2_pci_probe(struct pci_dev *pdev, const struct pci_device_id *i
 	if (rc)
 		return rc;

Can you also add check for IORESOURCE_MEM using pci_resource_flags() here? Just to be safe.

Quoted 13 lines
drivers/hid/amd-sfh-hid/amd_sfh_pcie.c +5 -0
+	if (pci_resource_len(pdev, 2) < AMD_SFH_MIN_BAR_SIZE) {
+		dev_err(&pdev->dev, "BAR 2 is too small\n");
+		return -EINVAL;
+	}
+
 	rc = pcim_iomap_regions(pdev, BIT(2), DRIVER_NAME);
 	if (rc)
 		return rc;
drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_init.c +0 -0
index b0bab2a1d..0afc596b5 100644

drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_init.c int amd_sfh1_1_init(struct amd_mp2_dev *mp2) · line 391

Quoted 5 lines
drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_init.c +1 -1
@@ -388,12 +388,12 @@ int amd_sfh1_1_init(struct amd_mp2_dev *mp2)
 	int rc;
 
 	phy_base <<= 21;
-	if (!devm_request_mem_region(dev, phy_base, 128 * 1024, "amd_sfh")) {
+	if (!devm_request_mem_region(dev, phy_base, AMD_SFH_MIN_BAR_SIZE, "amd_sfh")) {

Hm this isn't the BAR2, so using here AMD_SFH_MIN_BAR_SIZE is misleading. Do you agree? Maybe leave it as it is?

drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_init.c int amd_sfh1_1_init(struct amd_mp2_dev *mp2) · line 396

Quoted 6 lines
drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_init.c +1 -1
 		dev_dbg(dev, "can't reserve mmio registers\n");
 		return -ENOMEM;
 	}
 
-	mp2->vsbase = devm_ioremap(dev, phy_base, 128 * 1024);
+	mp2->vsbase = devm_ioremap(dev, phy_base, AMD_SFH_MIN_BAR_SIZE);

ditto

Quoted 6 lines
drivers/hid/amd-sfh-hid/sfh1_1/amd_sfh_init.c +0 -0
 	if (!mp2->vsbase) {
 		dev_dbg(dev, "failed to remap vsbase\n");
 		return -ENOMEM;

base-commit: db2ddb87143519e20a95aa36c60b36107b736a58

Signature · 1 line
Slawomir Stepien