Re: [PATCH bpf-next v1 1/3] x86/mm: Resolve BPF exception fixups for user address faults under SMAP
From: Kumar Kartikeya Dwivedi
Date: Mon Nov 23 2026 - 12:24:20 EST
On Mon, 23 Nov 2026 at 16:36, Dave Hansen <dave.hansen@xxxxxxxxx> wrote:
>
> On 10/9/26 08:23, Kumar Kartikeya Dwivedi wrote:
> > SEC("fentry/tcp_retransmit_skb") <- attaches to the entry of tcp_retransmit_skb()
> > int BPF_PROG(retrans, struct sock *sk)
> > {
> > struct file *f = sk->sk_socket->file;
> >
> > This program was written for doing a TCP related investigation. The first load
> > of sk is fine, but sk_socket can be NULL (orphaned socket) or stale, so the
> > second load becomes a mov with an extable entry. Below is the JITed sequence.
>
> In every other part of the kernel, dereferencing a NULL pointer would be
> called a bug. Isn't this just plain buggy code?
>
> I assume the BPF program wants to tolerate these situations because it
> can't fully participate in the synchronization mechanisms that the data
> structure actually needs. So, it makes what (in the normal kernel) is a
> buggy access.
It would be equivalent of doing copy_from_kernel_nofault() in the
normal kernel.
We allow privileged programs to do the equivalent of
copy_from_kernel_nofault() on such pointers, for reads only (the
verifier rejects stores). It is just done by instrumenting the
program's loads, so that the call doesn't need to be written
explicitly for every dereference.
The BPF users know that such reads are best effort, and they use them
with such expectations. Annotating all of the kernel is not scalable
to determine where it would be safe to allow, so privileged users use
their judgement on attaching in the right place and using this
mechanism to do safe reads of kernel data structures.
>
> The rest of the story is trying to fix that up.
>
> But I don't quite know how this can be done "safely" in a way that the
> fixups end up with good data or NULL. If SMAP isn't in play, can't this
> just go reading garbage from userspace?
It doesn't guarantee good data. The guarantee is the same as
copy_from_kernel_nofault(), i.e. the read won't crash the kernel. A stale
pointer to memory that is still mapped reads whatever is there now.
Programs that need valid data use trusted pointers the verifier can prove valid
(refcounted or RCU protected), and loads through those have no extable
entry.
This contract is well understood by BPF users. If they need to read
valid data they will use trusted pointers. If not, they rely on this
mechanism and attach to the right place to ensure the data they read
is valid. In practice it is heavily used by tracing programs
collecting statistics and observability data.
These loads never read user memory, with or without SMAP. Without SMAP
nothing changes: the JIT keeps the bounds check in front of the load as today,
which zeroes the destination register for user addresses and the vsyscall
page without doing the load. Patch 2 only drops the check when
X86_FEATURE_SMAP is enabled, where the CPU blocks the load, and patch 1
lets that fault be fixed up through the same extable entry, as we already
do for kernel address faults.
>
> Also, can you talk a bit about the code generation on the BPF side here?
> Are the instructions covered by the exception table handler all pure MOV's?
That is correct. Each extable entry covers a single load into a
register. mov for 4 and 8 byte loads, movzx for 1 and 2 byte loads, and
movsx/movsxd for the sign-extending ones. On a fault, ex_handler_bpf()
zeroes the destination register and skips the instruction.