2023-01-19 17:45:13

by Alexander Shishkin

[permalink] [raw]
Subject: [PATCH 0/2] PCI/MSI: Hardening fixes

Hi,

Here are 2 hardening fixes around MSIX table size/offset handling,
aiming to prevent a malicious device or VMM from triggering bugs by
supplying bogus values.

Alexander Shishkin (2):
PCI/MSI: Cache the MSIX table size
PCI/MSI: Validate device supplied MSI table offset and size

drivers/pci/msi/api.c | 7 ++++++-
drivers/pci/msi/msi.c | 12 +++++++++---
include/linux/pci.h | 1 +
3 files changed, 16 insertions(+), 4 deletions(-)

--
2.39.0


2023-01-19 18:02:30

by Alexander Shishkin

[permalink] [raw]
Subject: [PATCH 2/2] PCI/MSI: Validate device supplied MSI table offset and size

Currently, the MSI table offset supplied by device's config space is
passed directly into ioremap() without validation, allowing, for
example, a malicious VMM to trick the OS into exposing its private
memory.

Correct this by making sure the table with the given number of vectors
fits into its BIR starting at the provided table offset.

Signed-off-by: Alexander Shishkin <[email protected]>
Reported-by: Elena Reshetova <[email protected]>
Reviewed-by: Mika Westerberg <[email protected]>
Reviewed-by: Ilpo Järvinen <[email protected]>
Cc: [email protected]
---
drivers/pci/msi/msi.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/msi/msi.c b/drivers/pci/msi/msi.c
index d50cd45119f1..e93e633cb6a3 100644
--- a/drivers/pci/msi/msi.c
+++ b/drivers/pci/msi/msi.c
@@ -552,7 +552,8 @@ static void __iomem *msix_map_region(struct pci_dev *dev,
unsigned int nr_entries)
{
resource_size_t phys_addr;
- u32 table_offset;
+ u32 table_offset, table_size;
+ resource_size_t bir_size;
unsigned long flags;
u8 bir;

@@ -563,10 +564,15 @@ static void __iomem *msix_map_region(struct pci_dev *dev,
if (!flags || (flags & IORESOURCE_UNSET))
return NULL;

+ bir_size = pci_resource_len(dev, bir);
+ table_size = nr_entries * PCI_MSIX_ENTRY_SIZE;
table_offset &= PCI_MSIX_TABLE_OFFSET;
+ if (bir_size < table_size || table_offset > bir_size - table_size)
+ return NULL;
+
phys_addr = pci_resource_start(dev, bir) + table_offset;

- return ioremap(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
+ return ioremap(phys_addr, table_size);
}

/**
--
2.39.0