From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Eduard Zingerman <eddyz87@gmail.com>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
kkd@meta.com, kernel-team@meta.com,
Dave Hansen <dave.hansen@linux.intel.com>,
Andy Lutomirski <luto@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
Borislav Petkov <bp@alien8.de>,
Puranjay Mohan <puranjay@kernel.org>,
x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH bpf-next v2 1/3] x86/mm: Resolve BPF exception fixups for user address faults under SMAP
Date: Sat, 28 Nov 2026 02:28:54 +0100 [thread overview]
Message-ID: <0a30eeab6bea7ddfc0202d1970ab1b7f3a4d580c.1795829324.git.memxor@gmail.com> (raw)
In-Reply-To: <cover.1795829324.git.memxor@gmail.com>
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.
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 <ast@kernel.org>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
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
next prev parent reply other threads:[~2026-11-28 1:29 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-28 1:28 [PATCH bpf-next v2 0/3] bpf, x86: Drop the PROBE_MEM address range check " Kumar Kartikeya Dwivedi
2026-11-28 1:28 ` Kumar Kartikeya Dwivedi [this message]
2026-11-28 1:28 ` [PATCH bpf-next v2 2/3] bpf, x86: Skip " Kumar Kartikeya Dwivedi
2026-11-28 1:28 ` [PATCH bpf-next v2 3/3] selftests/bpf: Test PROBE_MEM loads from invalid addresses Kumar Kartikeya Dwivedi
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=0a30eeab6bea7ddfc0202d1970ab1b7f3a4d580c.1795829324.git.memxor@gmail.com \
--to=memxor@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bp@alien8.de \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=dave.hansen@linux.intel.com \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=kernel-team@meta.com \
--cc=kkd@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=puranjay@kernel.org \
--cc=tglx@kernel.org \
--cc=x86@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
all inboxes | Powered by JetHome®