* [PATCH] iommu/vt-d: Validate table addresses in dmar_fault_dump_ptes()
@ 2026-11-28 12:12 Ibrahim Elshahed
0 siblings, 0 replies; only message in thread
From: Ibrahim Elshahed @ 2026-11-28 12:12 UTC (permalink / raw)
To: Lu Baolu, David Woodhouse
Cc: Joerg Roedel, Will Deacon, Robin Murphy, iommu, linux-kernel,
nh-open-source, Anselm Busse
From: Anselm Busse <abusse@amazon.com>
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 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
Check each table address with pfn_valid() before dereferencing it and
stop the dump with a message when the check fails. Open-code the PASID
table lookup instead of using get_pasid_table_from_pde() so the
pointer taken from the PASID directory entry gets the same check.
Fixes: 914ff7719e8a ("iommu/vt-d: Dump DMAR translation structure when DMA fault occurs")
Cc: stable@vger.kernel.org
Signed-off-by: Anselm Busse <abusse@amazon.com>
Co-developed-by: Ibrahim Elshahed <lshahed@amazon.de>
Signed-off-by: Ibrahim Elshahed <lshahed@amazon.de>
---
Reproduced with a test-only hook that rewrites a device's context
entry before the device DMAs. In QEMU (q35, intel-iommu, legacy mode)
on iommu/next, a context entry pointing past the end of RAM or at the
local APIC oopses without this patch; with it the dump prints "legacy
mode page table address is invalid" and the system keeps running.
drivers/iommu/intel/iommu.c | 47 +++++++++++++++++++++++++++++++------
1 file changed, 40 insertions(+), 7 deletions(-)
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 5a2e705a2f2e..2f3122b1f4a3 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -552,6 +552,18 @@ static void free_context_table(struct intel_iommu *iommu)
}
#ifdef CONFIG_DMAR_DEBUG
+/*
+ * A faulting device's table entries may hold garbage or point outside
+ * the direct map. Return NULL if @addr cannot be dereferenced.
+ */
+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)
{
@@ -572,7 +584,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 +638,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;
}
@@ -631,7 +652,11 @@ void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id,
}
/* get the pointer to pasid directory entry */
- dir = phys_to_virt(ctx_entry->lo & VTD_PAGE_MASK);
+ dir = dmar_fault_table_virt(ctx_entry->lo & VTD_PAGE_MASK);
+ if (!dir) {
+ pr_info("pasid directory table address is invalid\n");
+ return;
+ }
/* For request-without-pasid, get the pasid from context entry */
if (intel_iommu_sm && pasid == IOMMU_PASID_INVALID)
@@ -642,11 +667,15 @@ void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id,
pr_info("pasid dir entry: 0x%016llx\n", pde->val);
/* get the pointer to the pasid table entry */
- entries = get_pasid_table_from_pde(pde);
- if (!entries) {
+ if (!pasid_pde_is_present(pde)) {
pr_info("pasid table is not present\n");
return;
}
+ entries = dmar_fault_table_virt(READ_ONCE(pde->val) & PDE_PFN_MASK);
+ if (!entries) {
+ pr_info("pasid table address is invalid\n");
+ return;
+ }
index = pasid & PASID_PTE_MASK;
pte = &entries[index];
for (i = 0; i < ARRAY_SIZE(pte->val); i++)
@@ -659,10 +688,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: 1ec7c5bbcabf9a75ba3b0bc0c0e64134a965abe7
--
2.47.3
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-28 12:13 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-28 12:12 [PATCH] iommu/vt-d: Validate table addresses in dmar_fault_dump_ptes() Ibrahim Elshahed
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®