[PATCH v2] iommu/vt-d: Validate table addresses in dmar_fault_dump_ptes()
From: Ibrahim Elshahed
Date: Fri Dec 04 2026 - 13:39:00 EST
dmar_fault_dump_ptes() walks the root entry, context entry, PASID
tables and page tables of a device that has just taken a DMA fault.
It translates each page-table address found in those entries with
phys_to_virt() and dereferences the result, without checking that the
address is backed by memory in the direct map. The walk runs from the
DMAR fault interrupt handler.
If an entry is corrupt and points at an address that has no struct
page, such as one past the end of RAM, phys_to_virt() returns an
unmapped virtual address and the dump faults in the interrupt handler.
A recoverable device fault becomes a panic. Pointing a device's
context entry past the end of RAM and letting the device DMA gives:
DMAR: [DMA Read NO_PASID] Request device [0000:00:03.0] fault addr 0x100000 [fault reason 0x03] Invalid context entry
DMAR: Dump dmar0 table entries for IOVA 0x100000
DMAR: root entry: 0x0000000100e0e001
DMAR: context entry: hi 0x0000000000000502, low 0x0000000400000001
BUG: unable to handle page fault for address: ffff9fb940000000
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
Oops: Oops: 0000 [#1] SMP NOPTI
RIP: 0010:dmar_fault_dump_ptes+0x216/0x263
A struct page alone does not make the address safe either: a reserved
page inside a RAM section has one but no direct-map entry, and reading
it faults the same way.
Reject page-table addresses without a struct page before translating
them, and read each page-table entry with get_kernel_nofault(), so a
bad pointer ends the dump with a message instead of a crash. The root,
context and PASID tables are allocated by the driver and are left as
they are.
Fixes: 914ff7719e8a ("iommu/vt-d: Dump DMAR translation structure when DMA fault occurs")
Co-developed-by: Anselm Busse <abusse@xxxxxxxxxx>
Signed-off-by: Anselm Busse <abusse@xxxxxxxxxx>
Signed-off-by: Ibrahim Elshahed <lshahed@xxxxxxxxx>
---
v1: https://lore.kernel.org/all/20261128121255.66061-1-lshahed@xxxxxxxxx/
Changes in v2:
- Read the page-table entries with get_kernel_nofault(); pfn_valid()
alone lets a reserved page that is not in the direct map through.
- Leave the PASID directory and PASID table lookups as they were.
- Reword the comment above dmar_fault_table_virt().
- Drop Cc: stable.
Tested on iommu/next with a test-only hook that rewrites a device's
context entry before the device DMAs, in QEMU (q35, intel-iommu, legacy
mode) and on an Ice Lake bare-metal host. Without the patch a context
entry pointing past the end of RAM, at the local APIC or at a reserved
page oopses; with it the dump prints "legacy mode page table address is
invalid" or "page table at level 4 is not readable" and the kernel does
not oops.
drivers/iommu/intel/iommu.c | 44 ++++++++++++++++++++++++++++++++-----
1 file changed, 38 insertions(+), 6 deletions(-)
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 5a2e705a2f2e..6a0a9dd05ce9 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -552,15 +552,34 @@ static void free_context_table(struct intel_iommu *iommu)
}
#ifdef CONFIG_DMAR_DEBUG
+/*
+ * A faulting device's page-table entries may hold garbage. Return NULL if
+ * @addr has no struct page.
+ */
+static void *dmar_fault_table_virt(u64 addr)
+{
+ if (!pfn_valid(PHYS_PFN(addr)))
+ return NULL;
+
+ return phys_to_virt(addr);
+}
+
static void pgtable_walk(struct intel_iommu *iommu, unsigned long pfn,
u8 bus, u8 devfn, struct dma_pte *parent, int level)
{
- struct dma_pte *pte;
+ struct dma_pte pte_val, *pte = &pte_val;
int offset;
while (1) {
offset = pfn_level_offset(pfn, level);
- pte = &parent[offset];
+ /*
+ * A table address with a struct page can still lack a direct-map
+ * entry (e.g. a reserved page); read the entry without faulting.
+ */
+ if (get_kernel_nofault(pte_val, &parent[offset])) {
+ pr_info("page table at level %d is not readable\n", level);
+ break;
+ }
pr_info("pte level: %d, pte value: 0x%016llx\n", level, pte->val);
@@ -572,7 +591,12 @@ static void pgtable_walk(struct intel_iommu *iommu, unsigned long pfn,
if (level == 1 || dma_pte_superpage(pte))
break;
- parent = phys_to_virt(dma_pte_addr(pte));
+ parent = dmar_fault_table_virt(dma_pte_addr(pte));
+ if (!parent) {
+ pr_info("page table address is invalid at level %d\n",
+ level - 1);
+ break;
+ }
level--;
}
}
@@ -621,7 +645,11 @@ void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id,
return;
}
level = agaw_to_level(ctx_entry->hi & 7);
- pgtable = phys_to_virt(ctx_entry->lo & VTD_PAGE_MASK);
+ pgtable = dmar_fault_table_virt(ctx_entry->lo & VTD_PAGE_MASK);
+ if (!pgtable) {
+ pr_info("legacy mode page table address is invalid\n");
+ return;
+ }
goto pgtable_walk;
}
@@ -659,10 +687,14 @@ void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id,
if (pasid_pte_get_pgtt(pte) == PASID_ENTRY_PGTT_FL_ONLY) {
level = pte->val[2] & BIT_ULL(2) ? 5 : 4;
- pgtable = phys_to_virt(pte->val[2] & VTD_PAGE_MASK);
+ pgtable = dmar_fault_table_virt(pte->val[2] & VTD_PAGE_MASK);
} else {
level = agaw_to_level((pte->val[0] >> 2) & 0x7);
- pgtable = phys_to_virt(pte->val[0] & VTD_PAGE_MASK);
+ pgtable = dmar_fault_table_virt(pte->val[0] & VTD_PAGE_MASK);
+ }
+ if (!pgtable) {
+ pr_info("scalable mode page table address is invalid\n");
+ return;
}
pgtable_walk:
base-commit: 93425b1e99e00c165b9287575e2e3b43d3ee1282
--
2.47.3