From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.17 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796095320; b=fIac2EQpyHq5aGdf4Mw40I7Zu3ajeu2IKh+6btva9ynTkbt/YGHHHIPHWk/EyB2dkWNE Bm8UztXMEmppd/0vvI9F6Bv0SoMyZaOkZdZevY4PPWNDTnAqBTXblcjqYyM3E/8cKG+fY wKuM1hVs/KOMLxAZZJH0DTMnd+MK7SlkwIAU45cHB0OhAnuVsLaTMM13DUvnTGBnJMOdP zgcWDlm36dDlZznDp1b3QqXZXnaVM/Cp2Szgy1UFnxqEXCxAOPxWHFN6xFeCy4nz6a5x2 qAv9VEeD37z7rh+Ws3dUbax+OgOb7BFnNPv6jtSsew342HdDzqSVbTyLi5oPn5boLCg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796095320; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Message-ID:Date: MIME-Version:User-Agent:Cc:Subject:To:References:Content-Language: From:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=hPd8ssrnl+ANRH3jVUd2MKAD9Kix12Vf6ubbOUiN0/A=; b=W2Ul5ViwJDeKQSnfdAYWyvhRshrUDmZnMTxS0WlqlHF78C84m2XvE1w/EICwn/b0KUh2 DYY/mLxKSAjlbz+ZaUKyQ6iItTtvYiHB3qAMh2B1mthliyXkRHDNZBu4UZcPbBOw+mHgx VaHI23AnLkT+ekEsTyyBls6QcCISJlu1TMFe9d38TDfIKaC6zNwuz+llQ3w3F1Qfhyy94 mbjHfgqOAfCMYWsi9i24ow5LHMuT9vbBvZSLv6PYtXK5K2v0cmBw7+EfzGG4nD3Th2OKu pdHz3GD5S25S+pMBYDs7JpE+1Wph6jdfHYv+GiTivTV3SulD29IxvexeT5MWuL5SWyg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=IxMCtENZ; arc=none smtp.remote-ip=198.175.65.17 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62A86606A31A for ; Tue, 1 Dec 2026 03:21:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 62A86606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=IxMCtENZ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796095320; x=1827631320; h=message-id:date:mime-version:cc:subject:to:references: from:in-reply-to:content-transfer-encoding; bh=hPd8ssrnl+ANRH3jVUd2MKAD9Kix12Vf6ubbOUiN0/A=; b=IxMCtENZY4PXY5gErc1wNNXaGHlWp+hmzCsUvEy6Uyli5JS/OyAcPWxn Baezoa9Gh860OqoDt9RsKhahhycUAmrEEl4hDEpYZrYJJ9sCptp0MwdLT vd9v9iPqLpcPRRTJY1+RVv0UU5rpo6v5CxZfXtPxaJ0VOOXIqk8R/pzYJ boY3gjF48+Ows0Woz1hJ6xNhThc7Ey4kPVoM7sRzifV3TgwDCF003eccs yjlHzXUbX2bP8Prq+2gzHaONiZIOAvMMQKtZ6i+V57Qgy76LLH6zgL7x4 8KQw1lamQxu7XRYTtGt+IqWUzJbxG0nWiams8WWHA0PQq++GPAa5Ti1EK g==; X-CSE-ConnectionGUID: SS4LP6//SWi3BQlXQTrVoQ== X-CSE-MsgGUID: ZdTl/6hIQKWHRwFDHh9x5Q== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4954095" X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="4954095" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by orvoesa109.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 19:21:59 -0800 X-CSE-ConnectionGUID: C7EABsitSDazzLtWgIjZ3w== X-CSE-MsgGUID: 9At7cOBmTcu0H/94DQobsg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="13429828" Received: from unknown (HELO [10.238.1.144]) ([10.238.1.144]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 19:21:56 -0800 Message-ID: <65120975-6d15-4e86-878a-a575260752bd@linux.intel.com> Date: Tue, 1 Dec 2026 11:21:54 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Cc: baolu.lu@linux.intel.com, Joerg Roedel , Will Deacon , Robin Murphy , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, nh-open-source@amazon.com, Anselm Busse Subject: Re: [PATCH] iommu/vt-d: Validate table addresses in dmar_fault_dump_ptes() To: Ibrahim Elshahed , David Woodhouse References: <20261128121255.66061-1-lshahed@amazon.de> Content-Language: en-US From: Baolu Lu In-Reply-To: <20261128121255.66061-1-lshahed@amazon.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 11/28/2026 8:12 PM, Ibrahim Elshahed wrote: > From: Anselm Busse > > 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 > Co-developed-by: Ibrahim Elshahed > Signed-off-by: Ibrahim Elshahed > --- > 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