Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.130 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796086692; b=ztgMZmiEMkIsHsAohFHcwPajgdxrUM5TgAj8YHWBe8PffROGwUKOUYFoL0zhkALKEKdV JWgROTD0uQW2t4LRpAKY2Uncw23QMjos3hB+3j301FlPqOliQo3IdNsj7HbCakYaz27H8 ha6y8ADkdFIOYq91boiqFxaow2LPgnBJFzlic/jM5czI678n/+/Z+AEbm8LW6mfHSvTjV BJ7bIXfkdMLQ/wUEyu/A1VXkYouCipIyYf5Dd/gbaFONbdBLPWkxyNmEWm+7hUmAxC2K3 FU2tTZv3Q0NgIGKqfgBUYCCKYkv/K/xLOi7jsbuADMWimVRZeFa5mgp26mVFpiwtMwA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796086692; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: X-Developer-Signature:X-Developer-Key:Content-Transfer-Encoding; bh=K0goQIcUKHxDib4reieAvXqSa4uwzbYgiqiwtELvAqo=; b=IrG7xu8G1vvK65fLu5iMRrxKZnqdrxZj5ReWw06mSDIEryBjF5Wn3w5VSHVec6X3MDOP RX+yk4BkmsiThjdz4eWQc/Y4Fzv21l5+w06q1tCqfmg2J2cSKz+QmroR+6f/ZDsLlglpD 7iwEr26NuUlZ8EMXNAzw0GAmM+dQigg624aPPs9ByHKDULMcJybU0WTMimbG+aYSqqPbZ NPzygauZjNueziZCmV/tDvX2cHm+XNHKDy0UW3pKWfilZxlpaaUTVdkS5Yn4D28YqA9g2 DThbkOlAb/Ptb5hIYCkPc/cHFHZ68Sd/wcji4Pyev36D1ZhszPijHgCA4V7z8x3iRIg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=RAP2DVlD; arc=none smtp.remote-ip=74.125.231.130 Received: from mail-oo2-f2.google.com (mail-oo2-f2.google.com [74.125.231.130]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 041E7606A6BA for ; Tue, 1 Dec 2026 00:58:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 041E7606A6BA Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=RAP2DVlD Received: by mail-oo2-f2.google.com with SMTP id 46e09a7af769-849c2965b9eso1988411a34.1 for ; Mon, 30 Nov 2026 16:58:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796086690; x=1796691490; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=K0goQIcUKHxDib4reieAvXqSa4uwzbYgiqiwtELvAqo=; b=RAP2DVlDRhZk8gj09+xKhxTa+4Mh0xz+QNZmzwubxWeflCIAkWBwmxvG5cn4GSsBip IyS5VgFM7vk+RmQOW9ElESQLRKFpza35ep6S5dF4riPUBWI4rFxQeF6ukUJfXEfBJmvR KlaInFyzZRoAwC1aJPA/ItNx5jCPcYNyCVu0oqKIGR00hLcYxQs23TsNBLuvIIASZMkK EuydK+IH8F2aG4vfonbk4arycKkBgJHPpGYPOrZQuHxQmUL54i+yzqFi96NR5ThiEudC +YTSw5zrZk6U+Yljy9mIICTkRIKHXYvmDzLJHtbfJSjDCQdgKUYWOXKaYWR2SEgP4rY7 TQww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796086690; x=1796691490; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=K0goQIcUKHxDib4reieAvXqSa4uwzbYgiqiwtELvAqo=; b=oBm8JSC6b0N5UEE9+fRT2TmE6A1ECEPBv9q2shAfcuT+W3RJo75O6lS65E6hO/yMdc U1fPMjW5iAq7inQs3QqcFZSF8PrCEtuqnBmQXSxbcGCZrLYmDSHcrEyvq/6lxdtSvt7o w22/7C96OJIfr0ZJ+xRiAQN9aO6Gfeq/reEbtIGUQoRcfgpKc1i1OSE8xDdDzSxShh+r iFdC46nHcmGT5omjpnRzLJTeaU+DWS3sjJQn2DNLkiJ+lTVOsXyiR3zR4VA62t1qAs+u aDldfo2M1/LRDYY6quNdrSXbFB1hlSzEQIUBtWzOHy92m58fNifH/RMIwcFI53FR6wyb BeDA== X-Gm-Message-State: APuU4Z/SM0YrkZ7PzKvFu+5/1AGpKOlzfhMDaLUWJfs1TMFI9PkqewwC 491rn1euANzOhsO9pKn5oseJojSPlpCGK86ky+Ybbo1o+xMHBvdZ0aEYUjAzinNAjZ0= X-Gm-Gg: AZFdTZdotJbRtIyMRl6mdyrD66wuzeOcnDFFKv8bQ4CwoxT0+5d5ajCvvw70U9cXliz xAmILLbIaPQGdjtAqKVIUK3WJQmA2+hzBV/TImhMBSGIuu93YnStw9tKrAJqjVonmNdPbCXmfUl lE1VoTrCMvbXVjN7msZPt9Fzhvg7Se2zjl10aS0wdxlCY470X3eNOi+KwzBz43Sz0iyFOvgvye2 3MgLU2Wb+xrr8ST+JSrvWr0gu9r++YF9ewvnqroCdHQOiXeqk7j0EgdwbpfI0ndjtjF/o408r8L WCI4OmhdB64zrIDb0a+6RTG5rOvGhg7PleiJqXwOrm6LNt2TSEXkhEGmLNn4jgBROyDgbSzEmrp NAoZ2TlQQKHcCwczt8Y0U3aW4abrGhWdsorVZ/0DjL/oVe0yzLs1xJ7K8B5yiPiPjsQSgL0NQVu ezYf33aH9G2ixvgl9yABSgdHcQ0sqiKagSWsyOqcIYGWYJO0yN8DksUVgXWws0yNZW2RLmN1gDV fDSkAjyLDP071f4iO9G5jBkZTgYgJhQCr5AxYcHyajAYHF7LSY3L6YhCB6kh/xKqLPjBoPlLABx FabYf/s1PkGN0JaQAb3bM/QL0gNBeET5eDmLhPbq2oNtt5neHjY/Rj9eoqBaAJ3pSx2PELXXLHw FZyfdusMZ7Ti7aUmXUHouMV6gqAGw/boyOmTF X-Received: by 2002:a05:6830:680c:b0:823:e626:2764 with SMTP id 46e09a7af769-863156221c3mr39914907a34.5.1796086689911; Mon, 30 Nov 2026 16:58:09 -0800 (PST) Received: from localhost ([2a03:2880:10ff:72::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-8639825528bsm8779520a34.1.2026.11.30.16.58.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 16:58:08 -0800 (PST) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Emil Tsalapatis , Ihor Solodrai , kkd@meta.com, kernel-team@meta.com, Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Puranjay Mohan , x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v3 1/3] x86/mm: Resolve BPF exception fixups for user address faults under SMAP Date: Tue, 1 Dec 2026 01:58:00 +0100 Message-ID: <477e2c8982a2c1f2d8cfc14dba10255cc6e49363.1796086624.git.memxor@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: References: X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4501; h=from:subject:message-id; bh=W+HsNHutfJbLCJkc8T7oisCByPi4hPQnKNXfP28aR20=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZtPOoFhutj9MH+mX1djP/KkrH57IzR/ssDexRcndmaXJWlK FVztKGVhEONikBVTZCn5v4/J+ETl70DbZdwwc1iZwIZwcQrARG46MjLsn/divsuNGx73DA57Ns9nPH JbdlHeyynbuf4861pqXvx1DiPDBc+Oc5a+9im/RTx5FoZsE3hv2jd/uqcF1zmjp0xdZuFsAA== X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit BPF tracing programs read through pointers that cannot be checked when the program is loaded. For example: SEC("fentry/tcp_retransmit_skb") int BPF_PROG(retrans, struct sock *sk) { struct file *f = sk->sk_socket->file; ... sk is valid, but sk->sk_socket is NULL for an orphaned socket. The verifier marks such a load PROBE_MEM and the JIT gives it an exception table entry of type EX_TYPE_BPF. The load is a single mov, movzx, movsx or movsxd into a register. On a fault, ex_handler_bpf() zeroes the register and skips the instruction. The load is not retried. This does not work for user addresses. With SMAP enabled, do_user_addr_fault() treats a kernel-mode fault on a user address with EFLAGS.AC clear as a kernel bug and oopses without looking at the exception table. So the x86 JIT puts a range check in front of every PROBE_MEM load to keep user addresses, the guard page above them and the vsyscall page away from it. For the second load above, with five-level paging, it emits: leaq 10485776(%rdi), %r11 # 16 - VSYSCALL_ADDR shrq $57, %r11 # zero for user and vsyscall addresses jne load xorl %edi, %edi jmp done load: movq 16(%rdi), %rdi # offsetof(struct socket, file) done: That is 17 or 18 bytes of check for a load of three to eight bytes, and it repeats in software what the CPU already refuses under SMAP. The pointer is only known at run time, so the check cannot be done at load time. Fix up the fault through the exception table when it came from kernel mode, is a read, and the faulting instruction has an EX_TYPE_BPF entry. error_context() uses ex_get_fixup_type() the same way for machine checks. SMAP stays on and the access is not retried, so no user memory is read: the register reads zero, as it does with the range check. The new check only runs for faults that oops today, and every other fault there still oopses. With LASS the access raises #GP instead, which already goes through fixup_exception(). The JIT also uses EX_TYPE_BPF for arena accesses and unwind entries. Arena addresses are kernel addresses, and no instruction starts at the address of an unwind entry, so neither can match a fault on a user address. The addresses a PROBE_MEM load can read stay the same. What changes is who refuses a user address: a compare in software, or the CPU with a fault and a fixup. user, guard, kernel address vsyscall page non-canonical unmapped mapped copy_from_kernel_nofault() compare compare CPU load PROBE_MEM, range check compare CPU CPU load PROBE_MEM, SMAP alone CPU CPU CPU load None of the three filters MMIO. ioremap() space is read in every row, and a BAR mapped into user space is a user address, refused in every row. SMAP refuses user pages, not low addresses. While a temporary mm is loaded (EFI runtime calls, text poking), a low address can have a supervisor mapping. A PROBE_MEM load of it from an interrupt now reads the mapping instead of zero, which is acceptable. This lets the x86 JIT drop the range check when SMAP is enabled. Kernels without SMAP keep the check and never take this path. Suggested-by: Alexei Starovoitov Signed-off-by: Kumar Kartikeya Dwivedi --- arch/x86/mm/fault.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index aa88370ce739..9fe8705eff04 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1263,7 +1263,16 @@ void do_user_addr_fault(struct pt_regs *regs, !(error_code & X86_PF_USER) && !(regs->flags & X86_EFLAGS_AC))) { /* - * No extable entry here. This was a kernel access to an + * The CPU refused the access. If the BPF JIT marked this load + * as one that may fault, skip it and zero the destination. + */ + if (!user_mode(regs) && !(error_code & X86_PF_WRITE) && + ex_get_fixup_type(regs->ip) == EX_TYPE_BPF && + fixup_exception(regs, X86_TRAP_PF, error_code, address)) + return; + + /* + * No other fixups here. This was a kernel access to an * invalid pointer. get_kernel_nofault() will not get here. */ page_fault_oops(regs, error_code, address); -- 2.53.1-Meta