* [PATCH bpf-next v3 1/3] x86/mm: Resolve BPF exception fixups for user address faults under SMAP
2026-12-01 0:57 [PATCH bpf-next v3 0/3] bpf, x86: Drop the PROBE_MEM address range check under SMAP Kumar Kartikeya Dwivedi
@ 2026-12-01 0:58 ` Kumar Kartikeya Dwivedi
2026-12-01 0:58 ` [PATCH bpf-next v3 2/3] bpf, x86: Skip the PROBE_MEM address range check " Kumar Kartikeya Dwivedi
2026-12-01 0:58 ` [PATCH bpf-next v3 3/3] selftests/bpf: Test PROBE_MEM loads from invalid addresses Kumar Kartikeya Dwivedi
2 siblings, 0 replies; 5+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-12-01 0:58 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, Emil Tsalapatis, Ihor Solodrai, kkd,
kernel-team, Dave Hansen, Andy Lutomirski, Peter Zijlstra,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Puranjay Mohan,
x86, linux-kernel
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 <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
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH bpf-next v3 2/3] bpf, x86: Skip the PROBE_MEM address range check under SMAP
2026-12-01 0:57 [PATCH bpf-next v3 0/3] bpf, x86: Drop the PROBE_MEM address range check under SMAP Kumar Kartikeya Dwivedi
2026-12-01 0:58 ` [PATCH bpf-next v3 1/3] x86/mm: Resolve BPF exception fixups for user address faults " Kumar Kartikeya Dwivedi
@ 2026-12-01 0:58 ` Kumar Kartikeya Dwivedi
2026-12-01 0:58 ` [PATCH bpf-next v3 3/3] selftests/bpf: Test PROBE_MEM loads from invalid addresses Kumar Kartikeya Dwivedi
2 siblings, 0 replies; 5+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-12-01 0:58 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, Emil Tsalapatis, Ihor Solodrai, kkd,
kernel-team, Dave Hansen, Andy Lutomirski, Peter Zijlstra,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Puranjay Mohan,
x86, linux-kernel
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. A fault on a user or vsyscall address used to oops under SMAP instead
of using the load's exception table entry. The check is five instructions
(lea, shr, jne, xor, jmp) and 17 or 18 bytes, for a load of three to eight
bytes.
do_user_addr_fault() now fixes up such a fault when the faulting
instruction has an EX_TYPE_BPF exception table entry. With LASS the access
raises #GP, which exc_general_protection() has always fixed up. So emit the
bare load when SMAP is enabled. The feature cannot change after boot, so it
is checked at JIT time. Kernels without SMAP, including those booted with
clearcpuid=smap, keep the check. bpf_arch_uaddress_limit() still returns 0
on x86, so the verifier adds no check of its own.
Programs loaded without CAP_BPF keep the check too. The fault handler finds
a program's exception table through the BPF ksym tree, and
bpf_prog_kallsyms_add() leaves such programs out of it. They can still have
PROBE_MEM loads: with kernel.unprivileged_bpf_disabled=0, a socket filter
loaded with only CAP_PERFMON can call bpf_skc_to_tcp_sock() and walk
pointers from the result. bpf_prog_load() now records whether the loader
had CAP_BPF, and the new bpf_prog_kallsyms_allowed() gives that answer to
both the JIT and bpf_prog_kallsyms_add().
JITed code size from veristat, on one kernel booted with and without
clearcpuid=smap, over the BPF selftests and 474 production objects from
Meta:
programs with PROBE_MEM reduction per program
mean median max
BPF selftests 5486 100 20.2% 19.9% 58.2%
Meta production programs 1942 262 3.5% 0.8% 51.4%
No program grows; dump_tcp6 of the selftests shrinks from 3142 to 2129
bytes. Most programs are unchanged, because loads through trusted pointers
and the probe_read helpers do not use PROBE_MEM.
flex_array_far_join in verifier_btf_array_access matched the lea of the
check to see that a far load got a fault handler. Under SMAP such a load is
now the bare load, so drop that expectation. flex_array_far_load still runs
a far load on an unmapped address.
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
arch/x86/net/bpf_jit_comp.c | 33 +++++++++++++++----
include/linux/bpf.h | 1 +
include/linux/filter.h | 1 +
kernel/bpf/core.c | 16 +++++++--
kernel/bpf/syscall.c | 1 +
.../bpf/progs/verifier_btf_array_access.c | 3 +-
6 files changed, 45 insertions(+), 10 deletions(-)
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 2b2a81e02cfc..daea13bcce5f 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -299,6 +299,9 @@ struct jit_context {
*/
int tail_call_direct_label;
int tail_call_indirect_label;
+
+ /* Emit the address range check in front of PROBE_MEM loads */
+ bool probe_mem_check;
};
/* Maximum number of bytes emitted while JITing one eBPF insn */
@@ -2573,6 +2576,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
const s32 imm32 = insn->imm;
u32 dst_reg = insn->dst_reg;
u32 src_reg = insn->src_reg;
+ bool probe_mem, bounds_check;
bool accesses_stack_only;
u8 b3 = 0;
u8 *start_of_ldx;
@@ -3117,6 +3121,9 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
case BPF_LDX | BPF_PROBE_MEMSX | BPF_B:
case BPF_LDX | BPF_PROBE_MEMSX | BPF_H:
case BPF_LDX | BPF_PROBE_MEMSX | BPF_W:
+ probe_mem = BPF_MODE(insn->code) == BPF_PROBE_MEM ||
+ BPF_MODE(insn->code) == BPF_PROBE_MEMSX;
+ bounds_check = probe_mem && ctx->probe_mem_check;
insn_off = insn->off;
if (src_reg == BPF_REG_PARAMS) {
if (insn_off == 8 && !arena_sp) {
@@ -3134,8 +3141,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
*/
}
- if (BPF_MODE(insn->code) == BPF_PROBE_MEM ||
- BPF_MODE(insn->code) == BPF_PROBE_MEMSX) {
+ if (bounds_check) {
/* Conservatively check that src_reg + insn->off is a kernel address:
* src_reg + insn->off > TASK_SIZE_MAX + PAGE_SIZE
* and
@@ -3176,6 +3182,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
/* populate jmp_offset for JNE above to jump to start_of_ldx */
start_of_ldx = prog;
end_of_jmp[-1] = start_of_ldx - end_of_jmp;
+ } else if (probe_mem) {
+ start_of_ldx = prog;
} else if (!accesses_stack_only) {
err = emit_kasan_check(env, &prog, src_reg,
insn_off,
@@ -3189,14 +3197,14 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
emit_ldsx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn_off);
else
emit_ldx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn_off);
- if (BPF_MODE(insn->code) == BPF_PROBE_MEM ||
- BPF_MODE(insn->code) == BPF_PROBE_MEMSX) {
+ if (probe_mem) {
struct exception_table_entry *ex;
u8 *_insn = image + proglen + (start_of_ldx - temp);
s64 delta;
/* populate jmp_offset for JMP above */
- start_of_ldx[-1] = prog - start_of_ldx;
+ if (bounds_check)
+ start_of_ldx[-1] = prog - start_of_ldx;
if (!bpf_prog->aux->extable)
break;
@@ -4893,6 +4901,15 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
addrs[i] = proglen;
}
ctx.cleanup_addr = proglen;
+ /*
+ * With SMAP enabled, a PROBE_MEM load of a user address faults (#PF, or
+ * #GP with LASS) and the fault handler resolves the exception table
+ * entry of the program, as for an unmapped kernel address. That lookup
+ * only finds programs in the BPF ksym tree, so programs loaded without
+ * CAP_BPF keep the address range check, as do kernels without SMAP.
+ */
+ ctx.probe_mem_check = !cpu_feature_enabled(X86_FEATURE_SMAP) ||
+ !bpf_prog_kallsyms_allowed(prog);
skip_init_addrs:
/*
@@ -5235,7 +5252,11 @@ bool bpf_jit_supports_ptr_xchg(void)
return true;
}
-/* x86-64 JIT emits its own code to filter user addresses so return 0 here */
+/*
+ * The x86-64 JIT keeps user addresses away from PROBE_MEM loads itself, with
+ * its own range check or by leaving the access to SMAP and the fault handler
+ * (see bpf_int_jit_compile()), so return 0 here.
+ */
u64 bpf_arch_uaddress_limit(void)
{
return 0;
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 4f9c2bf64643..db55849b7383 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1936,6 +1936,7 @@ struct bpf_prog_aux {
enum bpf_attach_type saved_dst_attach_type;
bool verifier_zext; /* Zero extensions has been inserted by verifier. */
bool dev_bound; /* Program is bound to the netdev. */
+ bool bpf_capable; /* Program was loaded with CAP_BPF. */
bool offload_requested; /* Program is bound and offloaded to the netdev. */
bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
bool attach_tracing_prog; /* true if tracing another tracing program */
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 06f128632d5c..40df54efb817 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -1686,6 +1686,7 @@ int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
char *sym);
struct bpf_prog *bpf_prog_ksym_find(unsigned long addr);
+bool bpf_prog_kallsyms_allowed(const struct bpf_prog *fp);
void bpf_prog_kallsyms_add(struct bpf_prog *fp);
void bpf_prog_kallsyms_del(struct bpf_prog *fp);
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index facb0d6d2e8d..f515c46a01ca 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -953,10 +953,22 @@ static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
return fp->jited && !bpf_prog_was_classic(fp);
}
+/*
+ * Programs loaded without CAP_BPF are kept out of the BPF ksym tree, so
+ * search_bpf_extables() does not find their exception tables. A JIT that
+ * relies on the exception table to recover from a faulting PROBE_MEM load needs
+ * this answer at JIT time, before bpf_prog_kallsyms_add() runs. Both get the
+ * answer that bpf_prog_load() recorded, so they cannot disagree, and the JIT
+ * makes no capability check of its own.
+ */
+bool bpf_prog_kallsyms_allowed(const struct bpf_prog *fp)
+{
+ return fp->aux->main_prog_aux->bpf_capable;
+}
+
void bpf_prog_kallsyms_add(struct bpf_prog *fp)
{
- if (!bpf_prog_kallsyms_candidate(fp) ||
- !bpf_token_capable(fp->aux->token, CAP_BPF))
+ if (!bpf_prog_kallsyms_candidate(fp) || !bpf_prog_kallsyms_allowed(fp))
return;
bpf_prog_ksym_set_addr(fp);
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 198438b95849..539ac76a28cb 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3104,6 +3104,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at
/* move token into prog->aux, reuse taken refcnt */
prog->aux->token = token;
token = NULL;
+ prog->aux->bpf_capable = bpf_cap;
prog->aux->user = get_current_user();
prog->len = attr->insn_cnt;
diff --git a/tools/testing/selftests/bpf/progs/verifier_btf_array_access.c b/tools/testing/selftests/bpf/progs/verifier_btf_array_access.c
index 2d48abed9ad9..da8e6e958b9c 100644
--- a/tools/testing/selftests/bpf/progs/verifier_btf_array_access.c
+++ b/tools/testing/selftests/bpf/progs/verifier_btf_array_access.c
@@ -569,8 +569,7 @@ int flex_array_far_load(void *ctx)
/* The join has the offsets from the start of the array to 1 << 40. */
SEC("tp_btf/exit_mmap")
-__success __arch_x86_64
-__jited(" leaq 0xa00000(%rbx), %r11")
+__success
__naked void flex_array_far_join(void)
{
asm volatile (" \
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH bpf-next v3 3/3] selftests/bpf: Test PROBE_MEM loads from invalid addresses
2026-12-01 0:57 [PATCH bpf-next v3 0/3] bpf, x86: Drop the PROBE_MEM address range check under SMAP Kumar Kartikeya Dwivedi
2026-12-01 0:58 ` [PATCH bpf-next v3 1/3] x86/mm: Resolve BPF exception fixups for user address faults " Kumar Kartikeya Dwivedi
2026-12-01 0:58 ` [PATCH bpf-next v3 2/3] bpf, x86: Skip the PROBE_MEM address range check " Kumar Kartikeya Dwivedi
@ 2026-12-01 0:58 ` Kumar Kartikeya Dwivedi
2026-12-01 1:16 ` sashiko-bot
2 siblings, 1 reply; 5+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-12-01 0:58 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, Emil Tsalapatis, Ihor Solodrai, kkd,
kernel-team, Dave Hansen, Andy Lutomirski, Peter Zijlstra,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Puranjay Mohan,
x86, linux-kernel
Add a test that performs PROBE_MEM loads of three sizes through a
bpf_core_cast() pointer whose value is chosen by userspace: NULL, a low
user address, the last user page and the guard page of both the four and
five level paging layouts, a non-canonical address and, on x86-64, the
vsyscall page and an offset into it. Each load must read zero and the
kernel must survive. A load through the current task pointer checks that
the same loads read real values when the address is valid. NULL and the low
user address are left out on s390x, which has its lowcore there.
The test passes on kernels that reject the addresses with the JIT's range
check and on kernels that rely on the fault handler. With only the JIT
change applied, the first subtest oopses.
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
.../bpf/prog_tests/probe_mem_fault.c | 77 +++++++++++++++++++
.../selftests/bpf/progs/probe_mem_fault.c | 41 ++++++++++
2 files changed, 118 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/probe_mem_fault.c
create mode 100644 tools/testing/selftests/bpf/progs/probe_mem_fault.c
diff --git a/tools/testing/selftests/bpf/prog_tests/probe_mem_fault.c b/tools/testing/selftests/bpf/prog_tests/probe_mem_fault.c
new file mode 100644
index 000000000000..9b89864b9ac2
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/probe_mem_fault.c
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <test_progs.h>
+#include "probe_mem_fault.skel.h"
+
+#if defined(__x86_64__)
+#include <asm/vsyscall.h>
+#endif
+
+/*
+ * Addresses a PROBE_MEM load has to survive. Either the JIT's address check
+ * or the fault handler must turn each load into a zero result. The user and
+ * guard page addresses are those of x86-64 with four and five paging levels;
+ * on other kernels they are merely unmapped.
+ */
+static const struct {
+ const char *name;
+ __u64 addr;
+} bad_addrs[] = {
+#if !defined(__s390x__)
+ /* s390x has its lowcore at these addresses, so the loads read it. */
+ { "null", 0 },
+ { "low_user", 4096 },
+#endif
+ { "last_user_page_4l", (1ULL << 47) - 2 * 4096 },
+ { "guard_page_4l", (1ULL << 47) - 4096 },
+ { "last_user_page_5l", (1ULL << 56) - 2 * 4096 },
+ { "guard_page_5l", (1ULL << 56) - 4096 },
+ { "non_canonical", 1ULL << 63 },
+#if defined(__x86_64__)
+ { "vsyscall", VSYSCALL_ADDR },
+ { "vsyscall_tail", VSYSCALL_ADDR + 0x800 },
+#endif
+};
+
+static void trigger(struct probe_mem_fault *skel, int *runs)
+{
+ skel->bss->val_dw = ~0ULL;
+ skel->bss->val_w = ~0U;
+ skel->bss->val_b = ~0;
+ usleep(1);
+ ASSERT_EQ(skel->bss->runs, ++*runs, "runs");
+}
+
+void test_probe_mem_fault(void)
+{
+ struct probe_mem_fault *skel;
+ int runs = 0, i;
+
+ skel = probe_mem_fault__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+
+ skel->bss->target_pid = getpid();
+ if (!ASSERT_OK(probe_mem_fault__attach(skel), "attach"))
+ goto out;
+
+ /* A valid kernel address is read for real. */
+ skel->bss->use_current_task = true;
+ trigger(skel, &runs);
+ ASSERT_EQ(skel->bss->val_w, getpid(), "pid");
+ ASSERT_NEQ(skel->bss->val_dw, 0, "start_time");
+ ASSERT_NEQ(skel->bss->val_b, 0, "comm");
+
+ skel->bss->use_current_task = false;
+ for (i = 0; i < ARRAY_SIZE(bad_addrs); i++) {
+ if (!test__start_subtest(bad_addrs[i].name))
+ continue;
+ skel->bss->addr = bad_addrs[i].addr;
+ trigger(skel, &runs);
+ ASSERT_EQ(skel->bss->val_dw, 0, "start_time");
+ ASSERT_EQ(skel->bss->val_w, 0, "pid");
+ ASSERT_EQ(skel->bss->val_b, 0, "comm");
+ }
+out:
+ probe_mem_fault__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/probe_mem_fault.c b/tools/testing/selftests/bpf/progs/probe_mem_fault.c
new file mode 100644
index 000000000000..f95db455722e
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/probe_mem_fault.c
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+int target_pid;
+bool use_current_task;
+__u64 addr;
+int runs;
+__u64 val_dw;
+__u32 val_w;
+__u8 val_b;
+
+SEC("fentry/" SYS_PREFIX "sys_nanosleep")
+int probe_mem_fault(void *ctx)
+{
+ struct task_struct *task;
+ __u64 p = addr;
+
+ if ((bpf_get_current_pid_tgid() >> 32) != target_pid)
+ return 0;
+
+ if (use_current_task)
+ p = (__u64)bpf_get_current_task_btf();
+ /*
+ * bpf_core_cast() yields an untrusted pointer, so every load through it
+ * is a PROBE_MEM load. Whatever the address is, the load must either
+ * read the field or produce zero; the kernel must not oops.
+ */
+ task = bpf_core_cast((void *)p, struct task_struct);
+ val_dw = task->start_time;
+ val_w = task->pid;
+ val_b = task->comm[0];
+ runs++;
+ return 0;
+}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH bpf-next v3 3/3] selftests/bpf: Test PROBE_MEM loads from invalid addresses
2026-12-01 0:58 ` [PATCH bpf-next v3 3/3] selftests/bpf: Test PROBE_MEM loads from invalid addresses Kumar Kartikeya Dwivedi
@ 2026-12-01 1:16 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-01 1:16 UTC (permalink / raw)
To: Kumar Kartikeya Dwivedi, bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] selftests/bpf: probe_mem_fault uses LoongArch64 DMW0 base as
non-canonical
--
commit 86780e4477a0aeef39bfb4a6c844fecd37572420
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
selftests/bpf: Test PROBE_MEM loads from invalid addresses
This commit adds a BPF selftest that checks PROBE_MEM loads of three sizes
from various invalid addresses and verifies that each load produces zero
without oopsing the kernel.
> diff --git a/tools/testing/selftests/bpf/prog_tests/probe_mem_fault.c b/tools/testing/selftests/bpf/prog_tests/probe_mem_fault.c
> new file mode 100644
> index 0000000000000..9b89864b9ac24
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/probe_mem_fault.c
> @@ -0,0 +1,77 @@
[ ... ]
> +static const struct {
> + const char *name;
> + __u64 addr;
> +} bad_addrs[] = {
> +#if !defined(__s390x__)
> + /* s390x has its lowcore at these addresses, so the loads read it. */
> + { "null", 0 },
> + { "low_user", 4096 },
> +#endif
> + { "last_user_page_4l", (1ULL << 47) - 2 * 4096 },
> + { "guard_page_4l", (1ULL << 47) - 4096 },
> + { "last_user_page_5l", (1ULL << 56) - 2 * 4096 },
> + { "guard_page_5l", (1ULL << 56) - 4096 },
> + { "non_canonical", 1ULL << 63 },
[Severity: Medium]
Is 1ULL << 63 guaranteed to fault or be rejected in bad_addrs[] on 64-bit
LoongArch?
On LoongArch64, do_misc_fixups() only zeroes addresses whose upper 32 bits
are at or below TASK_SIZE >> 32, and 0x8000000000000000 is CSR_DMW0_BASE
(UNCACHE_BASE), the base of Direct Mapping Window 0 enabled in kernel mode:
arch/loongarch/include/asm/loongarch.h:
#define CSR_DMW0_PLV0 _CONST64_(1 << 0)
#define CSR_DMW0_VSEG _CONST64_(0x8000)
#define CSR_DMW0_BASE (CSR_DMW0_VSEG << DMW_PABITS)
#define CSR_DMW0_INIT (CSR_DMW0_BASE | CSR_DMW0_PLV0)
Instead of raising an exception, the PROBE_MEM loads access physical address
0x0 directly with uncached attributes, which fails the zero assertions in
test_probe_mem_fault() if low physical memory contains non-zero data or
triggers an uncached MMIO read if physical address 0x0 is not backed by RAM.
Using an address outside the direct mapping windows such as 1ULL << 60 (as in
bpf_testmod_return_ptr()) faults on LoongArch64 while remaining non-canonical
on x86-64.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796086624.git.memxor@gmail.com?part=3
^ permalink raw reply [flat|nested] 5+ messages in thread