Skip to contentSkip to conversation

bpf: copy BPF token from main program to subprograms

PATCH Superseded Eduard Zingerman 2 participants 7 messages Apr 15

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Eduard Zingerman

author maintainer
#0 lore raw

v2 cover bpf: copy BPF token from main program to subprograms

bpf_jit_subprogs() omits aux->token when it creates a struct bpf_prog_aux instances for a subprograms.
This means that for programs loaded via BPF token (i.e., from a non-init user namespace), subprograms fail the bpf_token_capable() check in bpf_prog_kallsyms_add() and don't appear in /proc/kallsyms. Which in-turn makes it impossible to freplace such subprograms.

Changelog:
v1 -> v2:
- target bpf-next tree (fixups.c) instead of bpf tree (verifier.c).

v1: https://lore.kernel.org/bpf/[email protected]/T/
---
Eduard Zingerman (2):
bpf: copy BPF token from main program to subprograms
selftests/bpf: verify kallsyms entries for token-loaded subprograms

 kernel/bpf/fixups.c                                |  1 +
 tools/testing/selftests/bpf/Makefile               |  1 +
 tools/testing/selftests/bpf/prog_tests/token.c     | 81 +++++++++++++++++++++-
 .../selftests/bpf/prog_tests/unpriv_bpf_disabled.c | 21 +-----
 tools/testing/selftests/bpf/progs/token_kallsyms.c | 19 +++++
 tools/testing/selftests/bpf/sysctl_helpers.c       | 26 +++++++
 tools/testing/selftests/bpf/sysctl_helpers.h       |  7 ++
 7 files changed, 134 insertions(+), 22 deletions(-)

---
base-commit: 71b500afd2f7336f5b6c6026f2af546fc079be26
change-id: 20260414-subprog-token-fix-96c29ffa7e12

author maintainer
#1 lore raw

PATCH v2 1/2 bpf: copy BPF token from main program to subprograms

bpf_jit_subprogs() copies various fields from the main program's aux to each subprogram's aux, but omits the BPF token. This causes bpf_prog_kallsyms_add() to fail for subprograms loaded via BPF token, as bpf_token_capable() falls back to capable() in init_user_ns when token is NULL.

Copy prog->aux->token to func[i]->aux->token so that subprograms inherit the same capability delegation as the main program.

Fixes d79a35497547 ("bpf: Consistently use BPF token throughout BPF verifier logic")
---
 kernel/bpf/fixups.c | 1 +
 1 file changed, 1 insertion(+)
kernel/bpf/fixups.c +1 -0
index 67c9b28767e1..dd00a680e4ea 100644
@@ -1110,6 +1110,7 @@ int bpf_jit_subprogs(struct bpf_verifier_env *env)
 		func[i]->aux->exception_cb = env->subprog_info[i].is_exception_cb;
 		func[i]->aux->changes_pkt_data = env->subprog_info[i].changes_pkt_data;
 		func[i]->aux->might_sleep = env->subprog_info[i].might_sleep;
+		func[i]->aux->token = prog->aux->token;
 		if (!i)
 			func[i]->aux->exception_boundary = env->seen_exception;
 
Signature · 1 line
2.53.0
#2 lore raw
Sashiko AI review found 1 potential issue(s): - [High] BPF token capability delegation is ignored for JIT blinding because blinding_requested is evaluated before the token is attached. --

Sashiko AI review found 1 potential issue(s):
- [High] BPF token capability delegation is ignored for JIT blinding because blinding_requested is evaluated before the token is attached. --

commit 6cda306e3c0e6ae78f183769fc6ebeb4369f35c3
Author: Eduard Zingerman <[email protected]>

bpf: copy BPF token from main program to subprograms

This patch fixes an issue where bpf_jit_subprogs() omits copying the BPF token to subprograms' aux data. By copying prog->aux->token, subprograms inherit the same capability delegation as the main program, allowing bpf_prog_kallsyms_add() to succeed for token-loaded subprograms.

kernel/bpf/fixups.c int bpf_jit_subprogs(struct bpf_verifier_env *env) · line 1113

Quoted 8 lines
kernel/bpf/fixups.c +1 -0
@@ -1110,6 +1110,7 @@ int bpf_jit_subprogs(struct bpf_verifier_env *env)
 		func[i]->aux->exception_cb = env->subprog_info[i].is_exception_cb;
 		func[i]->aux->changes_pkt_data = env->subprog_info[i].changes_pkt_data;
 		func[i]->aux->might_sleep = env->subprog_info[i].might_sleep;
+		func[i]->aux->token = prog->aux->token;

While this ensures the token is propagated for bpf_prog_kallsyms_add(), does the token delegation still fail for JIT blinding?

Looking at bpf_prog_alloc(), it sets:

    fp->blinding_requested = bpf_jit_blinding_enabled(fp);

And bpf_jit_blinding_enabled() calls bpf_token_capable(prog->aux->token, CAP_BPF) to bypass JIT hardening for privileged users.

However, at the time of bpf_prog_alloc(), prog->aux->token is unconditionally NULL, as it gets assigned later in bpf_prog_load(). As a result, bpf_token_capable() falls back to checking the current process's global capabilities.

If an unprivileged user provides a valid BPF token granting CAP_BPF, the check fails, and blinding_requested is set to true. This state is then copied to all subprograms in bpf_jit_subprogs():

    func[i]->blinding_requested = prog->blinding_requested;

Are token holders incorrectly forced into JIT hardening due to this sequence, disabling direct tail-call optimizations?

Quoted 2 lines
kernel/bpf/fixups.c +0 -0
 		if (!i)
 			func[i]->aux->exception_boundary = env->seen_exception;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw
Quoted 37 lines · On Wed, 2026-04-15 at 01:08 +0000, [email protected] wrote:

Sashiko AI review found 1 potential issue(s):
- [High] BPF token capability delegation is ignored for JIT blinding because blinding_requested is evaluated before the token is attached. --

commit 6cda306e3c0e6ae78f183769fc6ebeb4369f35c3
Author: Eduard Zingerman <[email protected]>

bpf: copy BPF token from main program to subprograms

This patch fixes an issue where bpf_jit_subprogs() omits copying the BPF token to subprograms' aux data. By copying prog->aux->token, subprograms inherit the same capability delegation as the main program, allowing bpf_prog_kallsyms_add() to succeed for token-loaded subprograms.

kernel/bpf/fixups.c int bpf_jit_subprogs(struct bpf_verifier_env *env) · line 1113

Quoted 8 lines
kernel/bpf/fixups.c +1 -0
@@ -1110,6 +1110,7 @@ int bpf_jit_subprogs(struct bpf_verifier_env *env)
 		func[i]->aux->exception_cb = env->subprog_info[i].is_exception_cb;
 		func[i]->aux->changes_pkt_data = env->subprog_info[i].changes_pkt_data;
 		func[i]->aux->might_sleep = env->subprog_info[i].might_sleep;
+		func[i]->aux->token = prog->aux->token;

While this ensures the token is propagated for bpf_prog_kallsyms_add(), does the token delegation still fail for JIT blinding?

Looking at bpf_prog_alloc(), it sets:

    fp->blinding_requested = bpf_jit_blinding_enabled(fp);

And bpf_jit_blinding_enabled() calls bpf_token_capable(prog->aux->token, CAP_BPF) to bypass JIT hardening for privileged users.

However, at the time of bpf_prog_alloc(), prog->aux->token is unconditionally NULL, as it gets assigned later in bpf_prog_load(). As a result, bpf_token_capable() falls back to checking the current process's global capabilities.

Quoted 10 lines

If an unprivileged user provides a valid BPF token granting CAP_BPF, the check fails, and blinding_requested is set to true. This state is then copied to all subprograms in bpf_jit_subprogs():

    func[i]->blinding_requested = prog->blinding_requested;

Are token holders incorrectly forced into JIT hardening due to this sequence, disabling direct tail-call optimizations?

Quoted 2 lines
kernel/bpf/fixups.c +0 -0
 		if (!i)
 			func[i]->aux->exception_boundary = env->seen_exception;

I think this analysis is a dud. The token is correctly set for the main program and then is propagated to the subprograms.

author maintainer
#4 lore raw

PATCH v2 2/2 selftests/bpf: verify kallsyms entries for token-loaded subprograms

In reply to Eduard Zingerman

Add a test that loads an XDP program with a global subprogram using a BPF token from a user namespace, then verifies that both the main program and the subprogram appear in /proc/kallsyms.

This exercises the bpf_prog_kallsyms_add() path for subprograms and would have caught the missing aux->token copy in bpf_jit_subprogs().

load_kallsyms_local() filters out kallsyms with zero addresses. For a process with limited capabilities to read kallsym addresses the following sysctl variables have to be set to zero:
- /proc/sys/kernel/perf_event_paranoid
- /proc/sys/kernel/kptr_restrict
Set these variables using sysctl_set() utility function extracted from unpriv_bpf_disabled.c to a separate c/header.

---
 tools/testing/selftests/bpf/Makefile               |  1 +
 tools/testing/selftests/bpf/prog_tests/token.c     | 81 +++++++++++++++++++++-
 .../selftests/bpf/prog_tests/unpriv_bpf_disabled.c | 21 +-----
 tools/testing/selftests/bpf/progs/token_kallsyms.c | 19 +++++
 tools/testing/selftests/bpf/sysctl_helpers.c       | 26 +++++++
 tools/testing/selftests/bpf/sysctl_helpers.h       |  7 ++
 6 files changed, 133 insertions(+), 22 deletions(-)
tools/testing/selftests/bpf/Makefile +1 -0
index f75c4f52c028..da81a1853cfd 100644
@@ -750,6 +750,7 @@ TRUNNER_EXTRA_SOURCES := test_progs.c		\
 			 btf_helpers.c		\
 			 cap_helpers.c		\
 			 unpriv_helpers.c 	\
+			 sysctl_helpers.c	\
 			 netlink_helpers.c	\
 			 jit_disasm_helpers.c	\
 			 io_helpers.c		\
tools/testing/selftests/bpf/prog_tests/token.c +79 -2

107 lines not on this page · raw message

tools/testing/selftests/bpf/prog_tests/unpriv_bpf_disabled.c +1 -20
index 472f4f9fa95f..64404602b9ab 100644
@@ -8,6 +8,7 @@
 
 #include "cap_helpers.h"
 #include "bpf_util.h"
+#include "sysctl_helpers.h"
 
 /* Using CAP_LAST_CAP is risky here, since it can get pulled in from
  * an old /usr/include/linux/capability.h and be < CAP_BPF; as a result
@@ -36,26 +37,6 @@ static void process_perfbuf(void *ctx, int cpu, void *data, __u32 len)
 		got_perfbuf_val = *(__u32 *)data;
 }
 
-static int sysctl_set(const char *sysctl_path, char *old_val, const char *new_val)
-{
-	int ret = 0;
-	FILE *fp;
-
-	fp = fopen(sysctl_path, "r+");
-	if (!fp)
-		return -errno;
-	if (old_val && fscanf(fp, "%s", old_val) <= 0) {
-		ret = -ENOENT;
-	} else if (!old_val || strcmp(old_val, new_val) != 0) {
-		fseek(fp, 0, SEEK_SET);
-		if (fprintf(fp, "%s", new_val) < 0)
-			ret = -errno;
-	}
-	fclose(fp);
-
-	return ret;
-}
-
 static void test_unpriv_bpf_disabled_positive(struct test_unpriv_bpf_disabled *skel,
 					      __u32 prog_id, int prog_fd, int perf_fd,
 					      char **map_paths, int *map_fds)
tools/testing/selftests/bpf/progs/token_kallsyms.c new file +19 -0
new file mode 100644
index 000000000000..c9f9344f3eb2
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+char _license[] SEC("license") = "GPL";
+
+__weak
+int token_ksym_subprog(void)
+{
+	return 0;
+}
+
+SEC("xdp")
+int xdp_main(struct xdp_md *xdp)
+{
+	return token_ksym_subprog();
+}
tools/testing/selftests/bpf/sysctl_helpers.c new file +26 -0
new file mode 100644
index 000000000000..e58c23adaa3e
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+
+#include "sysctl_helpers.h"
+
+int sysctl_set(const char *sysctl_path, char *old_val, const char *new_val)
+{
+	int ret = 0;
+	FILE *fp;
+
+	fp = fopen(sysctl_path, "r+");
+	if (!fp)
+		return -errno;
+	if (old_val && fscanf(fp, "%s", old_val) <= 0) {
+		ret = -ENOENT;
+	} else if (!old_val || strcmp(old_val, new_val) != 0) {
+		fseek(fp, 0, SEEK_SET);
+		if (fprintf(fp, "%s", new_val) < 0)
+			ret = -errno;
+	}
+	fclose(fp);
+
+	return ret;
+}
tools/testing/selftests/bpf/sysctl_helpers.h new file +7 -0
new file mode 100644
index 000000000000..28d7e209df72
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __SYSCTL_HELPERS_H
+#define __SYSCTL_HELPERS_H
+
+int sysctl_set(const char *sysctl_path, char *old_val, const char *new_val);
+
+#endif
Signature · 1 line
2.53.0
#5 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: verify kallsyms entries for token-loaded subprograms

Sashiko AI review found 1 potential issue(s): - [Medium] The test modifies global sysctls (`perf_event_paranoid` and `kptr_restrict`) while running in parallel, causing race conditions and potential…

Sashiko AI review found 1 potential issue(s):
- [Medium] The test modifies global sysctls (perf_event_paranoid and kptr_restrict) while running in parallel, causing race conditions and potential host state leakage. --

commit 9e0a167112286dfb5b44d5686732b8297533a10b
Author: Eduard Zingerman <[email protected]>

selftests/bpf: verify kallsyms entries for token-loaded subprograms

This commit adds a test to verify that both the main program and its global subprogram appear in /proc/kallsyms when loaded using a user namespace BPF token. It temporarily modifies global sysctl variables to allow reading kallsym addresses.

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/token.c +0 -0
index b81dde283052e..89da527ed9304 100644

[ ... ]

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/token.c +1 -0
@@ -1194,4 +1252,23 @@ void test_token(void)
 
 		subtest_userns(&opts, userns_bpf_token_info);
 	}
+	if (test__start_subtest("obj_priv_prog_kallsyms")) {

tools/testing/selftests/bpf/prog_tests/token.c void test_token(void) · line 1265

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/token.c +10 -0
+		char perf_paranoid_orig[32] = {};
+		char kptr_restrict_orig[32] = {};
+		struct bpffs_opts opts = {
+			.cmds = bit(BPF_BTF_LOAD) | bit(BPF_PROG_LOAD),
+			.progs = bit(BPF_PROG_TYPE_XDP),
+			.attachs = ~0ULL,
+		};
+
+		sysctl_set("/proc/sys/kernel/perf_event_paranoid", perf_paranoid_orig, "0");
+		sysctl_set("/proc/sys/kernel/kptr_restrict", kptr_restrict_orig, "0");

The BPF selftest framework executes test_token() concurrently with other tests because it lacks the serial_test_ prefix.

If another parallel test modifies or relies on these sysctls simultaneously, can this create a race condition where the parallel test reads the already-modified "0" as its original state?

This could result in the host permanently retaining the lowered security settings once the tests complete.

Should this test be renamed to serial_test_token() to prevent concurrent modification of global system state?

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/token.c +8 -0
+
+		subtest_userns(&opts, userns_obj_priv_prog_kallsyms);
+
+		if (perf_paranoid_orig[0])
+			sysctl_set("/proc/sys/kernel/perf_event_paranoid", NULL, perf_paranoid_orig);
+		if (kptr_restrict_orig[0])
+			sysctl_set("/proc/sys/kernel/kptr_restrict", NULL, kptr_restrict_orig);
+	}
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#6 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: verify kallsyms entries for token-loaded subprograms

Quoted 44 lines · On Wed, 2026-04-15 at 01:21 +0000, [email protected] wrote:

Sashiko AI review found 1 potential issue(s):
- [Medium] The test modifies global sysctls (perf_event_paranoid and kptr_restrict) while running in parallel, causing race conditions and potential host state leakage. --

commit 9e0a167112286dfb5b44d5686732b8297533a10b
Author: Eduard Zingerman <[email protected]>

selftests/bpf: verify kallsyms entries for token-loaded subprograms

This commit adds a test to verify that both the main program and its global subprogram appear in /proc/kallsyms when loaded using a user namespace BPF token. It temporarily modifies global sysctl variables to allow reading kallsym addresses.

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/token.c +0 -0
index b81dde283052e..89da527ed9304 100644

[ ... ]

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/token.c +1 -0
@@ -1194,4 +1252,23 @@ void test_token(void)
 
 		subtest_userns(&opts, userns_bpf_token_info);
 	}
+	if (test__start_subtest("obj_priv_prog_kallsyms")) {

tools/testing/selftests/bpf/prog_tests/token.c void test_token(void) · line 1265

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/token.c +10 -0
+		char perf_paranoid_orig[32] = {};
+		char kptr_restrict_orig[32] = {};
+		struct bpffs_opts opts = {
+			.cmds = bit(BPF_BTF_LOAD) | bit(BPF_PROG_LOAD),
+			.progs = bit(BPF_PROG_TYPE_XDP),
+			.attachs = ~0ULL,
+		};
+
+		sysctl_set("/proc/sys/kernel/perf_event_paranoid", perf_paranoid_orig, "0");
+		sysctl_set("/proc/sys/kernel/kptr_restrict", kptr_restrict_orig, "0");

The BPF selftest framework executes test_token() concurrently with other tests because it lacks the serial_test_ prefix.

If another parallel test modifies or relies on these sysctls simultaneously, can this create a race condition where the parallel test reads the already-modified "0" as its original state?

Quoted 5 lines

This could result in the host permanently retaining the lowered security settings once the tests complete.

Should this test be renamed to serial_test_token() to prevent concurrent modification of global system state?

I don't think that we have tests that rely on these parameters atm, but suggestion makes sense.

Quoted 10 lines
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/token.c +8 -0
+
+		subtest_userns(&opts, userns_obj_priv_prog_kallsyms);
+
+		if (perf_paranoid_orig[0])
+			sysctl_set("/proc/sys/kernel/perf_event_paranoid", NULL, perf_paranoid_orig);
+		if (kptr_restrict_orig[0])
+			sysctl_set("/proc/sys/kernel/kptr_restrict", NULL, kptr_restrict_orig);
+	}
 }