git: 05f2f4129779 - main - arm64: Reject user-mode faults on kernel addresses

From: Alan Cox <alc_at_FreeBSD.org>
Date: Fri, 27 Nov 2026 05:43:48 UTC
The branch main has been updated by alc:

URL: https://cgit.FreeBSD.org/src/commit/?id=05f2f4129779ccb0857b05ed38ea5b825bae0107

commit 05f2f4129779ccb0857b05ed38ea5b825bae0107
Author:     Alan Cox <alc@FreeBSD.org>
AuthorDate: 2026-11-25 07:36:36 +0000
Commit:     Alan Cox <alc@FreeBSD.org>
CommitDate: 2026-11-27 05:41:49 +0000

    arm64: Reject user-mode faults on kernel addresses
    
    On a CPU without FEAT_E0PD, a user-mode store to a mapped kernel
    address takes a permission fault, which data_abort() passes to
    pmap_fault() with the process's pmap.  pmap_pte() then finds the user
    mapping whose address has the same low-order bits.  If that mapping is
    writable, pmap_fault() marks it dirty, although nothing wrote it, and
    returns KERN_SUCCESS.  The store is retried and faults again, so the
    thread spins instead of receiving SIGSEGV.  Access flag faults, which
    loads also take, have the same flaw when a kernel mapping lacks
    ATTR_AF.
    
    Like amd64's trap_pfault(), send SIGSEGV for a user-mode fault in the
    kernel address space without calling pmap_fault() or vm_fault_trap().
    
    Also, in pmap_fault(), assert that a kernel address comes with the
    kernel pmap and a user address with a user pmap.
    
    Found by:       Claude Code (Opus 5.5)
    Reviewed by:    kib, markj
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D62065
---
 sys/arm64/arm64/pmap.c | 10 ++++++++++
 sys/arm64/arm64/trap.c |  8 ++++++++
 2 files changed, 18 insertions(+)

diff --git a/sys/arm64/arm64/pmap.c b/sys/arm64/arm64/pmap.c
index 8a8b7972105d..4f4c5a886390 100644
--- a/sys/arm64/arm64/pmap.c
+++ b/sys/arm64/arm64/pmap.c
@@ -10076,6 +10076,16 @@ pmap_fault(pmap_t pmap, uint64_t esr, uint64_t far)
 	if (pmap->pm_stage == PM_STAGE2)
 		return (pmap_stage2_fault(pmap, esr, far));
 
+	/*
+	 * pmap_pte() ignores the high-order address bits that distinguish
+	 * between kernel and user virtual addresses.  Consequently, given a
+	 * user pmap and a kernel virtual address, pmap_pte() is not
+	 * guaranteed to fail.  It may return the PTE for the user virtual
+	 * address with the same low-order bits.
+	 */
+	KASSERT(ADDR_IS_KERNEL(far) == (pmap == kernel_pmap),
+	    ("%s: address %#lx is not in pmap %p", __func__, far, pmap));
+
 	/* Data and insn aborts use same encoding for FSC field. */
 	switch (esr & ISS_DATA_DFSC_MASK) {
 	case ISS_DATA_DFSC_AFF_L1:
diff --git a/sys/arm64/arm64/trap.c b/sys/arm64/arm64/trap.c
index e688b9cb76a3..ce42baf3876c 100644
--- a/sys/arm64/arm64/trap.c
+++ b/sys/arm64/arm64/trap.c
@@ -327,6 +327,14 @@ data_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
 
 	fault_va = far;
 	if (lower) {
+		/*
+		 * Don't allow user-mode faults in the kernel address space.
+		 */
+		if (__predict_false(ADDR_IS_KERNEL(far))) {
+			call_trapsignal(td, SIGSEGV, SEGV_MAPERR, (void *)far,
+			    ESR_ELx_EXCEPTION(esr));
+			return;
+		}
 		map = &td->td_proc->p_vmspace->vm_map;
 		if ((td->td_proc->p_md.md_tcr & TCR_TBI0) != 0)
 			fault_va = ADDR_MAKE_CANONICAL(far);