From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=35.162.73.231 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796409534; b=3WqGiFGMDp3wVRvxGoFZ21DbS4INWuWhzeGHFPyES8LgawP9+PTbMSJMRLj4V+ssGR86 vtPBMghZFgnr+VhWju8m1FDKf8aHHZr0HBHRtiknuCrQVLIxUNRT0oxVxYqCzCVBOXo6O BNQKkQldsRRnag9CAk2hRngoN2BLpYbLO3WZM8RfGnDODNt63MwjQpoS1Ec8RjRl9ovcs 7zI2P1PONHsvPBDcJH7kw9PkBRxzA6w0wFLbmVaYNeusGHdvQc+utEIE5FHeLNee7x1fJ xrsEm8WQQEeDSL0SJmJljuyuamuU3I0G63wmQATNoEUFDqmP3pUhnH/ZbdRuA5pwajA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796409534; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:Received:Received:X-Farcaster-Flow-ID:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy; bh=QAwNlrmdqYrJtX4PaHFqa0ge1h6QcQmSUsuHpLT9W3M=; b=rhnJYvkrwyiR3RTddDpllMGfPVjfUoEvaX0RhC1JiXZ/N+Z2Q9RSUSmTkL24+EjZXl8f koxgsJElN2a476V+Dp/l/+eJZRcX4DUGVZNS1QbUYva0Ofh/Ks2BPafsl9Iwg+QX//JIX MxNbOo61kHUcXvFayGDo1n16TqGqtYvT4d9fj+P50nTdH3402N/7Bp1lPHn9v0j6/SxhY LSUImZDXgOqsgmBXPpxI8+rxkigZGk1IqH8M1AykssHgvFt0JOpT8H9SbkB9S4epFq6Ip TsjCu0wBsJmaFppxLl3z5Gdd1mWpTbF+NJ3zGuFlokyDxSN1xShqRyBKJG6GfBt8Ppg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=amazon.de; spf=pass smtp.mailfrom=amazon.de; dkim=pass header.d=amazon.de header.i=@amazon.de header.a=rsa-sha256 header.s=amazoncorp2 header.b=qnT2R1x7; arc=none smtp.remote-ip=35.162.73.231 Received: from pdx-out-012.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-012.esa.us-west-2.outbound.mail-perimeter.amazon.com [35.162.73.231]) (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 0455C606A32D for ; Fri, 4 Dec 2026 18:38:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0455C606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=amazon.de header.i=@amazon.de header.a=rsa-sha256 header.s=amazoncorp2 header.b=qnT2R1x7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazoncorp2; t=1796409534; x=1827945534; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=QAwNlrmdqYrJtX4PaHFqa0ge1h6QcQmSUsuHpLT9W3M=; b=qnT2R1x77Axz8GaQoVTu1z204b9wVVZi46YcnpdvnoQtDyKUN7PcwRC+ wLwXuJ1ZpkzkSh26aeksc2t3IfAdFsYltp/EVFCfec1ikV7jB/AtXvVRx 0XhBhSgKS+8iW0prRd7mwWxDvVRP+QE4biQGqmOYp6zxnHoJ9MAdnQXYn npC3LGavQh4XCkdeFZyqwcv8upwKGyD0biVECAfBiUArJIQC70HtDuaUE zSzlQDyg4F+M2eKR6vHVBz5KuNOoJuAmAIMZEX5TdZ0TflFaeIBG9HMeE CjwFbJjMoZvLvpMxf5IuibNgMqJbllvHc0Ws3lZHg1vnCH3Hd+8vhKCYh g==; X-CSE-ConnectionGUID: yYamNZ7mQ5CXm8Y6kcr8iA== X-CSE-MsgGUID: jXxeYbWERvOuTm4keADQcQ== X-IronPort-AV: E=Sophos;i="6.29,78,1794873600"; d="scan'208";a="35779108" Received: from ip-10-5-0-115.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.0.115]) by internal-pdx-out-012.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2026 18:38:51 +0000 Received: from EX19MTAUWA002.ant.amazon.com [205.251.233.234:21282] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.37.107:2525] with esmtp (Farcaster) id 8cd3e521-b077-46b9-ad6f-8831b0e93393; Fri, 4 Dec 2026 18:38:51 +0000 (UTC) X-Farcaster-Flow-ID: 8cd3e521-b077-46b9-ad6f-8831b0e93393 Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Fri, 4 Dec 2026 18:38:50 +0000 Received: from dev-dsk-lshahed-1a-d9c3b839.eu-west-1.amazon.com (172.19.123.145) by EX19D001UWA001.ant.amazon.com (10.13.138.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Fri, 4 Dec 2026 18:38:49 +0000 From: Ibrahim Elshahed To: Lu Baolu , David Woodhouse CC: Joerg Roedel , Will Deacon , "Robin Murphy" , , , , Anselm Busse Subject: [PATCH v2] iommu/vt-d: Validate table addresses in dmar_fault_dump_ptes() Date: Fri, 4 Dec 2026 18:38:42 +0000 Message-ID: <20261204183842.3723-1-lshahed@amazon.de> X-Mailer: git-send-email 2.47.3 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: EX19D031UWC003.ant.amazon.com (10.13.139.252) To EX19D001UWA001.ant.amazon.com (10.13.138.214) 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 Signed-off-by: Anselm Busse Signed-off-by: Ibrahim Elshahed --- v1: https://lore.kernel.org/all/20261128121255.66061-1-lshahed@amazon.de/ 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