Skip to contentSkip to conversation

bpf, arm64: Keep BPF_PROBE_MEM loads away from tagged user addresses

PATCH Applied Alexei Starovoitov 3 participants 7 messages Nov 3 – Nov 4

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

cover bpf, arm64: Keep BPF_PROBE_MEM loads away from tagged user addresses

From: Alexei Starovoitov <[email protected]>

BPF CI on arm64 reported "BUG: sleeping function called from invalid context" in subprogs_extable. The prog loads from 1ull << 60. arm64 ignores the top byte of a user address, so this is user address 0, but the guard that the verifier puts around a BPF_PROBE_MEM load compares the whole address with TASK_SIZE and lets it through. The page fault is then handled as a user fault in the context of the prog.

Patch 1 removes the interpreter on x86-64 and arm64 when the JIT is
built, so that a check in the JIT covers all progs.
Patch 2 tests bit 55 of the address in the arm64 JIT. Patch 3 adds a test.

Alexei Starovoitov (3):
bpf: Force BPF_JIT_ALWAYS_ON on x86-64 and arm64
bpf, arm64: Filter user addresses of BPF_PROBE_MEM loads in the JIT selftests/bpf: Test a load from a tagged user address

 Documentation/admin-guide/sysctl/net.rst      |  3 +++
 Documentation/networking/filter.rst           |  3 ++-
 arch/arm64/Kconfig                            |  1 +
 arch/arm64/net/bpf_jit_comp.c                 | 24 +++++++++++++++++++
 arch/x86/Kconfig                              |  1 +
 kernel/bpf/Kconfig                            | 10 +++++++-
 .../bpf/progs/mem_rdonly_untrusted.c          | 16 +++++++++++++
 7 files changed, 56 insertions(+), 2 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/3 bpf: Force BPF_JIT_ALWAYS_ON on x86-64 and arm64

From: Alexei Starovoitov <[email protected]>

The interpreter is a second implementation of the BPF run time. A check that a JIT emits on its own has to be made once more for the interpreter, or it is missing there. BPF CI doesn't see the difference:
the selftests configs of x86-64 and arm64 set CONFIG_BPF_JIT_ALWAYS_ON=y. The interpreter is also a ready-made gadget for speculative execution attacks, which is why this option was added.

Let an arch select ARCH_WANT_BPF_JIT_ALWAYS_ON. It turns CONFIG_BPF_JIT_ALWAYS_ON on and hides its prompt. Select it on x86-64 and arm64.

For a kernel with CONFIG_BPF_SYSCALL=y and CONFIG_BPF_JIT=y that had the option off:
- net.core.bpf_jit_enable is 1 and can't be changed.
- A prog that the JIT fails to compile is refused with -ENOTSUPP. It used to fall back to the interpreter. This includes classic socket and seccomp filters.

A kernel with CONFIG_BPF_JIT=n keeps the interpreter. 32-bit x86 keeps the choice, because its JIT doesn't support all insns.

---
 Documentation/admin-guide/sysctl/net.rst |  3 +++
 Documentation/networking/filter.rst      |  3 ++-
 arch/arm64/Kconfig                       |  1 +
 arch/x86/Kconfig                         |  1 +
 kernel/bpf/Kconfig                       | 10 +++++++++-
 5 files changed, 16 insertions(+), 2 deletions(-)
Documentation/admin-guide/sysctl/net.rst +3 -0
index fe43e8595958..1d408e7e8c0c 100644
@@ -90,6 +90,9 @@ Values:
 	- 1 - enable the JIT
 	- 2 - enable the JIT and ask the compiler to emit traces on kernel log.
 
+With CONFIG_BPF_JIT_ALWAYS_ON the value is 1 and cannot be changed. x86_64 and
+arm64 always enable this option.
+
 bpf_jit_harden
 --------------
 
Documentation/networking/filter.rst +2 -1
index 7f95c3199748..7b90cfc009de 100644
@@ -404,7 +404,8 @@ Example output from dmesg::
     [ 3389.935852] JIT code: 00000040: eb 02 31 c0 c9 c3
 
 When CONFIG_BPF_JIT_ALWAYS_ON is enabled, bpf_jit_enable is permanently set to 1 and
-setting any other value than that will return in failure. This is even the case for
+setting any other value than that will return in failure. x86_64 and arm64 always
+enable it. This is even the case for
 setting bpf_jit_enable to 2, since dumping the final JIT image into the kernel log
 is discouraged and introspection through bpftool (under tools/bpf/bpftool/) is the
 generally recommended approach instead.
arch/arm64/Kconfig +1 -0
index 1075bc661833..8cb2e2609eaa 100644
@@ -88,6 +88,7 @@ config ARM64
 	select ARCH_SUPPORTS_SCHED_CLUSTER
 	select ARCH_SUPPORTS_SCHED_MC
 	select ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
+	select ARCH_WANT_BPF_JIT_ALWAYS_ON
 	select ARCH_WANT_COMPAT_IPC_PARSE_VERSION if COMPAT
 	select ARCH_WANT_DEFAULT_BPF_JIT
 	select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT
arch/x86/Kconfig +1 -0
index 58dce0b66179..695a77bb261a 100644
@@ -140,6 +140,7 @@ config X86
 	select ARCH_USE_QUEUED_SPINLOCKS
 	select ARCH_USE_SYM_ANNOTATIONS
 	select ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
+	select ARCH_WANT_BPF_JIT_ALWAYS_ON	if X86_64
 	select ARCH_WANT_DEFAULT_BPF_JIT	if X86_64
 	select ARCH_WANTS_CLOCKSOURCE_READ_INLINE	if X86_64
 	select ARCH_WANTS_DYNAMIC_TASK_STRUCT
kernel/bpf/Kconfig +9 -1
index 98493e32db2a..2b7bbb9bd5f8 100644
@@ -26,6 +26,11 @@ config HAVE_EBPF_JIT_KASAN
 config ARCH_WANT_DEFAULT_BPF_JIT
 	bool
 
+# Used by archs to tell that kernels that were compiled with BPF JIT support
+# have no BPF interpreter.
+config ARCH_WANT_BPF_JIT_ALWAYS_ON
+	bool
+
 menu "BPF subsystem"
 
 config BPF_SYSCALL
@@ -60,7 +65,8 @@ config BPF_JIT
 	  /proc/sys/net/core/bpf_jit_kallsyms (optional)
 
 config BPF_JIT_ALWAYS_ON
-	bool "Permanently enable BPF JIT and remove BPF interpreter"
+	bool "Permanently enable BPF JIT and remove BPF interpreter" if !ARCH_WANT_BPF_JIT_ALWAYS_ON
+	default ARCH_WANT_BPF_JIT_ALWAYS_ON
 	depends on BPF_SYSCALL && HAVE_EBPF_JIT && BPF_JIT
 	help
 	  Enables BPF JIT and removes BPF interpreter to avoid speculative
@@ -70,6 +76,8 @@ config BPF_JIT_ALWAYS_ON
 	  is permanently set to 1 and setting any other value than that will
 	  return failure.
 
+	  x86-64 and arm64 always enable this option.
+
 config BPF_JIT_DEFAULT_ON
 	def_bool ARCH_WANT_DEFAULT_BPF_JIT || BPF_JIT_ALWAYS_ON
 	depends on HAVE_EBPF_JIT && BPF_JIT
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH bpf-next 1/3] bpf: Force BPF_JIT_ALWAYS_ON on x86-64 and arm64

TL;DR: - Docs/Kconfig text says x86_64 and arm64 "always" enable JIT_ALWAYS_ON, but it still depends on BPF_SYSCALL - Nit: new sentence in filter.rst breaks the "This is even the case" flow
Quoted 1 line

bpf: Force BPF_JIT_ALWAYS_ON on x86-64 and arm64

TL;DR:
- Docs/Kconfig text says x86_64 and arm64 "always" enable JIT_ALWAYS_ON, but it still depends on BPF_SYSCALL
- Nit: new sentence in filter.rst breaks the "This is even the case" flow

Documentation/admin-guide/sysctl/net.rst Values: · line 94

Quoted 9 lines
Documentation/admin-guide/sysctl/net.rst +2 -0
@@ -90,6 +90,9 @@ Values:
 	- 1 - enable the JIT
 	- 2 - enable the JIT and ask the compiler to emit traces on kernel log.
 
+With CONFIG_BPF_JIT_ALWAYS_ON the value is 1 and cannot be changed. x86_64 and
+arm64 always enable this option.

Is "always enable this option" accurate here?

BPF_JIT_ALWAYS_ON still has "depends on BPF_SYSCALL && HAVE_EBPF_JIT && BPF_JIT", so the new "default ARCH_WANT_BPF_JIT_ALWAYS_ON" is limited by those dependencies. An x86-64 or arm64 kernel with BPF_JIT=y but BPF_SYSCALL=n keeps BPF_JIT_ALWAYS_ON=n and the interpreter, and bpf_jit_enable (registered under CONFIG_BPF_JIT) can still be set to 0, 1 or 2.

The commit message states the condition ("For a kernel with CONFIG_BPF_SYSCALL=y and CONFIG_BPF_JIT=y"), but this text, the similar sentence in Documentation/networking/filter.rst, and the new help text in kernel/bpf/Kconfig ("x86-64 and arm64 always enable this option.") leave it out. Would something like "x86_64 and arm64 enable this option whenever CONFIG_BPF_SYSCALL is set" be more accurate?

[ ... ]

Documentation/networking/filter.rst Example output from dmesg:: · line 411

Quoted 13 lines
Documentation/networking/filter.rst +2 -1
@@ -404,7 +404,8 @@ Example output from dmesg::
     [ 3389.935852] JIT code: 00000040: eb 02 31 c0 c9 c3
 
 When CONFIG_BPF_JIT_ALWAYS_ON is enabled, bpf_jit_enable is permanently set to 1 and
-setting any other value than that will return in failure. This is even the case for
+setting any other value than that will return in failure. x86_64 and arm64 always
+enable it. This is even the case for
 setting bpf_jit_enable to 2, since dumping the final JIT image into the kernel log
 is discouraged and introspection through bpftool (under tools/bpf/bpftool/) is the
 generally recommended approach instead.

This isn't a bug, but the new sentence now sits between "will return in failure." and "This is even the case for setting bpf_jit_enable to 2".

"This" used to refer to the failed write, so the paragraph now reads as if x86_64 and arm64 always enabling it is "even the case for setting bpf_jit_enable to 2". Could the arch sentence move to the end of the paragraph, after "...is the generally recommended approach instead."?

The line "enable it. This is even the case for" is also not reflowed.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/41741570364

#3 lore raw

Re: [PATCH bpf-next 1/3] bpf: Force BPF_JIT_ALWAYS_ON on x86-64 and arm64

Quoted 4 lines · On Tue, Nov 03, 2026 at 04:11 PM [email protected] <[email protected]>…

TL;DR:
- Docs/Kconfig text says x86_64 and arm64 "always" enable JIT_ALWAYS_ON, but it still depends on BPF_SYSCALL
- Nit: new sentence in filter.rst breaks the "This is even the case" flow

Not worth a respin.
The commit log has the exact condition. A kernel with BPF_SYSCALL=n has classic filters only. They have no BPF_PROBE_MEM loads, so patch 2 doesn't depend on it.

#4 lore raw

PATCH 2/3 bpf, arm64: Filter user addresses of BPF_PROBE_MEM loads in the JIT

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

A BPF_PROBE_MEM load must not touch user memory. On arm64 the verifier skips the load when the upper 32 bits of the address are not above those of TASK_SIZE. But the CPU ignores the top byte of a user address: with a non-zero top byte and bit 55 clear it passes this guard.

do_page_fault() finds an exception table entry for the load, takes it for a uaccess routine and handles a user fault in the prog's context:

- It sleeps on a contended mmap_lock in the RCU read-side critical section of the prog. BPF CI hits it in subprogs_extable, where bpf_testmod_return_ptr() returns 1ull << 60:

  BUG: sleeping function called from invalid context at kernel/locking/rwsem.c:1598
   lock_mm_and_find_vma
   do_page_fault
   bpf_prog_c3033363d3d961d2_handle_fexit_ret_subprogs3

- When the page is mapped, PAN refuses the load and handle_mm_fault() has nothing to fix. The load faults forever and the task can't be killed.

Bit 55 of an address selects the page table: TTBR0 for user, TTBR1 for kernel. Test this bit in the JIT and read zero when it is clear. Return 0 from bpf_arch_uaddress_limit() to turn off the guard of the verifier, like x86 does. No prog is left without both: a kernel with bpf(2) and the arm64 JIT has no interpreter.

A load from a kernel address now runs 3 extra insns instead of 6, and 2 instead of 5 when the offset is zero:

     before                          after
     mov   x9, x8                    add   x11, x8, #8
     add   x9, x9, #8                tst   x11, #0x80000000000000
     lsr   x9, x9, #32               b.ne  1f
     cmp   x9, #16, lsl #12          mov   x20, #0
     b.ls  1f                        b     2f
     ldr   x20, [x8, #8]          1: ldr   x20, [x11]
     b     2f                     2:
  1: mov   x20, #0

2:

Fixes 66e13b615a0c ("bpf: verifier: prevent userspace memory access")
---
 arch/arm64/net/bpf_jit_comp.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
arch/arm64/net/bpf_jit_comp.c +24 -0
index e99f105b7f61..b1d0840d26ba 100644
@@ -1966,6 +1966,24 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
 			src_adj = src;
 			off_adj = off;
 		}
+		if (BPF_MODE(insn->code) == BPF_PROBE_MEM ||
+		    BPF_MODE(insn->code) == BPF_PROBE_MEMSX) {
+			/*
+			 * Read zero instead of user memory. Bit 55 of an address
+			 * selects the user or the kernel page table. The bits
+			 * above it don't tell: the CPU ignores the top byte of
+			 * a user address.
+			 */
+			if (off) {
+				emit_a64_add_i(1, tmp2, src, tmp, off, ctx);
+				src_adj = tmp2;
+				off_adj = 0;
+			}
+			emit(A64_TST_I(1, src_adj, BIT(55)), ctx);
+			emit(A64_B_(A64_COND_NE, 3), ctx);
+			emit(A64_MOVZ(1, dst, 0, 0), ctx);
+			emit(A64_B(2), ctx);
+		}
 		sign_extend = (BPF_MODE(insn->code) == BPF_MEMSX ||
 				BPF_MODE(insn->code) == BPF_PROBE_MEMSX ||
 				 BPF_MODE(insn->code) == BPF_PROBE_MEM32SX);
@@ -3503,6 +3521,12 @@ bool bpf_jit_supports_ptr_xchg(void)
 	return true;
 }
 
+/* The JIT emits its own code to filter user addresses */
+u64 bpf_arch_uaddress_limit(void)
+{
+	return 0;
+}
+
 bool bpf_jit_supports_exceptions(void)
 {
 	/* We unwind through both kernel frames starting from within bpf_throw
Signature · 1 line
2.55.0
#5 lore raw

PATCH 3/3 selftests/bpf: Test a load from a tagged user address

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

arm64 ignores the top byte of a user address. Check that a BPF_PROBE_MEM load from a mapped user address with a tag in the top byte reads zero.

Use a syscall prog. It runs in process context with page faults enabled, so the fault handler doesn't fix up the load before it looks at the address.

---
 .../selftests/bpf/progs/mem_rdonly_untrusted.c   | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c +16 -0
index 23019023511a..b5cf85e2a19a 100644
@@ -51,6 +51,22 @@ int ldx_is_ok_bad_addr(void *ctx)
 	return p[0x7fff];
 }
 
+SEC("syscall")
+__success
+__retval(0)
+int ldx_is_ok_tagged_user_addr(void *ctx)
+{
+	struct task_struct *task = bpf_get_current_task_btf();
+	char *p;
+
+	if (!bpf_core_enum_value_exists(enum bpf_features, BPF_FEAT_RDONLY_CAST_TO_VOID))
+		return 42;
+
+	/* arm64 ignores the top byte of a user address */
+	p = bpf_rdonly_cast((void *)(task->mm->arg_start | 1ul << 60), 0);
+	return *p;
+}
+
 SEC("socket")
 __success
 __retval(1)
Signature · 1 line
2.55.0
#6 lore raw
In reply to Alexei Starovoitov
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 10 lines · On Tue, 3 Nov 2026 23:19:28 +0000 you wrote:

From: Alexei Starovoitov <[email protected]>

BPF CI on arm64 reported "BUG: sleeping function called from invalid context" in subprogs_extable. The prog loads from 1ull << 60. arm64 ignores the top byte of a user address, so this is user address 0, but the guard that the verifier puts around a BPF_PROBE_MEM load compares the whole address with TASK_SIZE and lets it through. The page fault is then handled as a user fault in the context of the prog.

[...]

Here is the summary with links:
- [bpf-next,1/3] bpf: Force BPF_JIT_ALWAYS_ON on x86-64 and arm64

    https://git.kernel.org/bpf/bpf-next/c/8db2aca155a4
  - [bpf-next,2/3] bpf, arm64: Filter user addresses of BPF_PROBE_MEM loads in the JIT
    https://git.kernel.org/bpf/bpf-next/c/8d8794555932
  - [bpf-next,3/3] selftests/bpf: Test a load from a tagged user address
    https://git.kernel.org/bpf/bpf-next/c/f68a11f141ce

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html