The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Baolu Lu <baolu.lu@linux.intel.com>
To: Ibrahim Elshahed <lshahed@amazon.de>,
	David Woodhouse <dwmw2@infradead.org>
Cc: baolu.lu@linux.intel.com, Joerg Roedel <joro@8bytes.org>,
	Will Deacon <will@kernel.org>,
	Robin Murphy <robin.murphy@arm.com>,
	iommu@lists.linux.dev, linux-kernel@vger.kernel.org,
	nh-open-source@amazon.com, Anselm Busse <abusse@amazon.com>
Subject: Re: [PATCH] iommu/vt-d: Validate table addresses in dmar_fault_dump_ptes()
Date: Tue, 1 Dec 2026 11:21:54 +0800	[thread overview]
Message-ID: <65120975-6d15-4e86-878a-a575260752bd@linux.intel.com> (raw)
In-Reply-To: <20261128121255.66061-1-lshahed@amazon.de>

On 11/28/2026 8:12 PM, Ibrahim Elshahed wrote:
> 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

This code is only built with CONFIG_DMAR_DEBUG, which is selected only
by INTEL_IOMMU_DEBUGFS, and that option is documented as not intended
for production. The issue was also only reproduced with a test-only
hook. So I would like to drop the cc-stable tag.

> 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;

pfn_valid() only tells you that the PFN has a struct page. It doesn't
guarantee that phys_to_virt() is safe to dereference it.

If the goal is to never fault while dumping, copy_from_kernel_nofault()
or get_kernel_nofault() would be more robust. Otherwise, please reword
the comment to say that addresses without a backing struct page are
rejected.

> +
> +	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;
>   	}

Root/context/pasid tables are configured during iommu initialization and
remain static throughout the system's lifecycle. Dereferencing addresses
from these entries is safe. Otherwise, the driver is already broken,
that's a more serious bug.

> +	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

Thanks,
baolu


      reply	other threads:[~2026-12-01  3:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 12:12 [PATCH] iommu/vt-d: Validate table addresses in dmar_fault_dump_ptes() Ibrahim Elshahed
2026-12-01  3:21 ` Baolu Lu [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=65120975-6d15-4e86-878a-a575260752bd@linux.intel.com \
    --to=baolu.lu@linux.intel.com \
    --cc=abusse@amazon.com \
    --cc=dwmw2@infradead.org \
    --cc=iommu@lists.linux.dev \
    --cc=joro@8bytes.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lshahed@amazon.de \
    --cc=nh-open-source@amazon.com \
    --cc=robin.murphy@arm.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox