summaryrefslogtreecommitdiff
path: root/components/drivers
diff options
context:
space:
mode:
authorBernard Xiong <[email protected]>2026-11-05 10:47:23 +0800
committerGitHub <[email protected]>2026-11-05 10:47:23 +0800
commitf6857f9fb0f8e1a7e6064ed3078e21587d497ea5 (patch)
treec8306fa60ae103076d15f547f6bc3559f80a1109 /components/drivers
parentd3a368d565d189593234e6c0e8d174cdbf75e7a0 (diff)
[DM/PCI] Validate MSI-X table BIR and offset against BAR (#11986)
* [DM/PCI] Validate MSI-X table BIR and offset against BAR BIR and the table offset in the MSI-X capability are controlled by the device. msix_table_remap() used BIR directly as an index into pdev->resource[] and never checked the table against the BAR size, so a malicious device could make the kernel map and write to an arbitrary physical address during probe. Reject out-of-range BIR values, unassigned or zero-sized BARs, and tables that do not fit completely inside the assigned BAR. Also verify that the requested vector count does not exceed the table size read from the MSI-X control register. Fixes #11985 * [DM/PCI] Fix cppcheck warnings in MSI-X code - Restore the default IRQ only when an MSI descriptor exists, avoiding a potential NULL pointer dereference in rt_pci_msi_shutdown - Initialize the error code in msix_setup_msi_descs so that an empty vector list returns a defined value
Diffstat (limited to 'components/drivers')
-rw-r--r--components/drivers/pci/msi/msi.c59
1 files changed, 51 insertions, 8 deletions
diff --git a/components/drivers/pci/msi/msi.c b/components/drivers/pci/msi/msi.c
index 5d20b52d40..8ed5a2dc1a 100644
--- a/components/drivers/pci/msi/msi.c
+++ b/components/drivers/pci/msi/msi.c
@@ -191,10 +191,11 @@ void rt_pci_msi_shutdown(struct rt_pci_device *pdev)
if ((desc = rt_pci_msi_first_desc(pdev)))
{
msi_unmask(&desc->msi, msi_multi_mask(&desc->msi), pdev);
+
+ /* Restore pdev->irq to its default pin-assertion IRQ */
+ pdev->irq = desc->msi.default_irq;
}
- /* Restore pdev->irq to its default pin-assertion IRQ */
- pdev->irq = desc->msi.default_irq;
pdev->msi_enabled = RT_FALSE;
}
@@ -738,27 +739,57 @@ static void *msix_table_remap(struct rt_pci_device *pdev, rt_size_t entries_nr)
{
rt_uint8_t bir;
rt_uint32_t table_offset;
- rt_ubase_t table_base_phys;
+ rt_uint64_t table_base_phys, table_bytes;
+ struct rt_pci_bus_resource *res;
+
+ if (rt_pci_read_config_u32(pdev, pdev->msix_cap + PCIR_MSIX_TABLE, &table_offset))
+ {
+ LOG_E("%s: Read MSI-X table offset fail", rt_dm_dev_get_name(&pdev->parent));
- rt_pci_read_config_u32(pdev, pdev->msix_cap + PCIR_MSIX_TABLE, &table_offset);
+ return RT_NULL;
+ }
+
+ /* The BIR field is device-controlled, never use it as a raw array index */
bir = (rt_uint8_t)(table_offset & PCIM_MSIX_BIR_MASK);
- if (pdev->resource[bir].flags & PCI_BUS_REGION_F_NONE)
+ if (bir >= RT_ARRAY_SIZE(pdev->resource))
+ {
+ LOG_E("%s: MSI-X table BIR %d is out of range", rt_dm_dev_get_name(&pdev->parent), bir);
+
+ return RT_NULL;
+ }
+
+ res = &pdev->resource[bir];
+
+ if (res->flags & PCI_BUS_REGION_F_NONE || res->size == 0)
{
LOG_E("%s: BAR[bir = %d] is invalid", rt_dm_dev_get_name(&pdev->parent), bir);
return RT_NULL;
}
- table_base_phys = pdev->resource[bir].base + (table_offset & ~PCIM_MSIX_BIR_MASK);
+ table_offset &= PCIM_MSIX_TABLE_OFFSET;
+ table_bytes = (rt_uint64_t)entries_nr * PCIM_MSIX_ENTRY_SIZE;
- return rt_ioremap((void *)table_base_phys, entries_nr * PCIM_MSIX_ENTRY_SIZE);
+ /* The whole MSI-X table must be contained in the assigned BAR */
+ if (table_offset > res->size || table_bytes > res->size - table_offset)
+ {
+ LOG_E("%s: MSI-X table(offset = 0x%x, size = %d) exceeds BAR[bir = %d] size 0x%lx",
+ rt_dm_dev_get_name(&pdev->parent), table_offset, (int)table_bytes,
+ bir, (unsigned long)res->size);
+
+ return RT_NULL;
+ }
+
+ table_base_phys = (rt_uint64_t)res->base + table_offset;
+
+ return rt_ioremap((void *)(rt_ubase_t)table_base_phys, table_bytes);
}
static rt_err_t msix_setup_msi_descs(struct rt_pci_device *pdev,
void *table_base, struct rt_pci_msix_entry *entries, int nvec)
{
- rt_err_t err;
+ rt_err_t err = RT_EOK;
struct rt_pci_msi_desc desc;
rt_memset(&desc, 0, sizeof(desc));
@@ -810,6 +841,18 @@ static rt_ssize_t msix_capability_init(struct rt_pci_device *pdev,
rt_pci_read_config_u16(pdev, pdev->msix_cap + PCIR_MSIX_CTRL, &msgctl);
/* Request & Map MSI-X table region */
table_size = rt_pci_msix_table_size(msgctl);
+
+ /* A device may report different table sizes on repeated reads, make sure
+ * the requested vectors can be covered by the table mapped below */
+ if ((rt_size_t)nvec > table_size)
+ {
+ LOG_E("%s: Requested %d vectors exceed MSI-X table size %d",
+ rt_dm_dev_get_name(&pdev->parent), nvec, (int)table_size);
+
+ err = -RT_EINVAL;
+ goto _out_disbale_msix;
+ }
+
table_base = msix_table_remap(pdev, table_size);
if (!table_base)