mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-11-16 01:05:15 +08:00
[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
This commit is contained in:
1 parent
d3a368d565
commit
f6857f9fb0
1 file changed
+51
-8
@@ -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;
|
||||
|
||||
rt_pci_read_config_u32(pdev, pdev->msix_cap + PCIR_MSIX_TABLE, &table_offset);
|
||||
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));
|
||||
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user