bpf: introduce TAINT_UNSAFE_BPF for mutating helpers
Subsystems (4)
RFC PATCH v3 1/1 bpf: introduce TAINT_UNSAFE_BPF for mutating helpers
The primary remit of the eBPF verifier is to ensure that eBPF programs can neither crash the kernel nor corrupt memory. Nevertheless, administrative utilities such as "bpftrace --unsafe" permit the loading of programs that employ destructive or mutating helpers, most notably bpf_probe_write_user() and bpf_override_return().
Since commit b28573ebfabe ("bpf: Remove bpf_probe_write_user() warning message"), the kernel no longer issues a warning when an attempt is made to invoke such destructive helpers.
Consequently, this patch introduces a novel kernel taint flag, TAINT_UNSAFE_BPF ("V"). Tainting the kernel establishes a permanent and readily auditable indicator (i.e., /proc/sys/kernel/tainted) to alert maintainers that the kernel's execution flow or user memory may have been compromised by an eBPF program.
---
Changes since v2 [1]:
- Deferred the application of TAINT_UNSAFE_BPF until after the eBPF verifier successfully completes
- Added taints_kernel to struct bpf_prog_aux to track the presence of mutating helpers during static analysis without causing premature side effects
Changes since v1 [2]:
- Moved the taint from run-time execution to load-time verification
- Added "V" flag decoding to tools/debugging/kernel-chktaint (Randy Dunlap)
- Updated the seq command in tainted-kernels.rst to check all 21 bits (Randy Dunlap)
- Fixed a Sphinx "Malformed table" warning by expanding the number column boundaries in tainted-kernels.rst
[1]: https://lore.kernel.org/lkml/[email protected]/
[2]: https://lore.kernel.org/lkml/[email protected]/
--- Documentation/admin-guide/tainted-kernels.rst | 56 ++++++++++--------- include/linux/bpf.h | 1 + include/linux/panic.h | 3 +- kernel/bpf/syscall.c | 7 +++ kernel/bpf/verifier.c | 8 +++ kernel/panic.c | 1 + tools/debugging/kernel-chktaint | 8 +++ 7 files changed, 58 insertions(+), 26 deletions(-)
Documentation/admin-guide/tainted-kernels.rst +31 -25
@@ -74,35 +74,36 @@ a particular type of taint. It's best to leave that to the aforementioned script, but if you need something quick you can use this shell command to check which bits are set:: - $ for i in $(seq 20); do echo $(($i-1)) $(($(cat /proc/sys/kernel/tainted)>>($i-1)&1));done + $ for i in $(seq 21); do echo $(($i-1)) $(($(cat /proc/sys/kernel/tainted)>>($i-1)&1));done Table for decoding tainted state ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -=== === ====== ======================================================== -Bit Log Number Reason that got the kernel tainted -=== === ====== ======================================================== - 0 G/P 1 proprietary module was loaded - 1 _/F 2 module was force loaded - 2 _/S 4 kernel running on an out of specification system - 3 _/R 8 module was force unloaded - 4 _/M 16 processor reported a Machine Check Exception (MCE) - 5 _/B 32 bad page referenced or some unexpected page flags - 6 _/U 64 taint requested by userspace application - 7 _/D 128 kernel died recently, i.e. there was an OOPS or BUG - 8 _/A 256 ACPI table overridden by user - 9 _/W 512 kernel issued warning - 10 _/C 1024 staging driver was loaded - 11 _/I 2048 workaround for bug in platform firmware applied - 12 _/O 4096 externally-built ("out-of-tree") module was loaded - 13 _/E 8192 unsigned module was loaded - 14 _/L 16384 soft lockup occurred - 15 _/K 32768 kernel has been live patched - 16 _/X 65536 auxiliary taint, defined for and used by distros - 17 _/T 131072 kernel was built with the struct randomization plugin - 18 _/N 262144 an in-kernel test has been run - 19 _/J 524288 userspace used a mutating debug operation in fwctl -=== === ====== ======================================================== +=== === ======= ======================================================== +Bit Log Number Reason that got the kernel tainted +=== === ======= ======================================================== + 0 G/P 1 proprietary module was loaded + 1 _/F 2 module was force loaded + 2 _/S 4 kernel running on an out of specification system + 3 _/R 8 module was force unloaded + 4 _/M 16 processor reported a Machine Check Exception (MCE) + 5 _/B 32 bad page referenced or some unexpected page flags + 6 _/U 64 taint requested by userspace application + 7 _/D 128 kernel died recently, i.e. there was an OOPS or BUG + 8 _/A 256 ACPI table overridden by user + 9 _/W 512 kernel issued warning + 10 _/C 1024 staging driver was loaded + 11 _/I 2048 workaround for bug in platform firmware applied + 12 _/O 4096 externally-built ("out-of-tree") module was loaded + 13 _/E 8192 unsigned module was loaded + 14 _/L 16384 soft lockup occurred + 15 _/K 32768 kernel has been live patched + 16 _/X 65536 auxiliary taint, defined for and used by distros + 17 _/T 131072 kernel was built with the struct randomization plugin + 18 _/N 262144 an in-kernel test has been run + 19 _/J 524288 userspace used a mutating debug operation in fwctl + 20 _/V 1048576 an unsafe eBPF program (mutating helper) was loaded +=== === ======= ======================================================== Note: The character ``_`` is representing a blank in this table to make reading easier.
@@ -189,3 +190,8 @@ More detailed explanation for tainting 19) ``J`` if userspace opened /dev/fwctl/* and performed a FWTCL_RPC_DEBUG_WRITE to use the devices debugging features. Device debugging features could cause the device to malfunction in undefined ways. + + 20) ``V`` if an eBPF program utilising unsafe, mutating helpers (such as + bpf_probe_write_user() or bpf_override_return()) was loaded. These helpers + bypass standard eBPF safety guarantees and can alter execution flow or + corrupt memory.
include/linux/bpf.h +1 -0
@@ -1698,6 +1698,7 @@ struct bpf_prog_aux {bool changes_pkt_data;bool might_sleep;bool kprobe_write_ctx; +bool taints_kernel;u64 prog_array_member_cnt;/* counts how many times as member of prog_array */ struct mutex ext_mutex;/* mutex for is_extended and prog_array_member_cnt */ struct bpf_arena *arena;
include/linux/panic.h +2 -1
@@ -88,7 +88,8 @@ static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout)#define TAINT_RANDSTRUCT 17#define TAINT_TEST 18#define TAINT_FWCTL 19 -#define TAINT_FLAGS_COUNT 20 +#define TAINT_UNSAFE_BPF 20 +#define TAINT_FLAGS_COUNT 21#define TAINT_FLAGS_MAX ((1UL << TAINT_FLAGS_COUNT) - 1)struct taint_flag {
kernel/bpf/syscall.c +7 -0
@@ -3083,6 +3083,13 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)if (err <0 ) goto free_used_maps; +/* +* The program has passed the verifier. If it utilises unsafe +* helpers, formally taint the kernel now. +*/ +if (prog->aux->taints_kernel) +add_taint (TAINT_UNSAFE_BPF ,LOCKDEP_STILL_OK ); + err =bpf_prog_mark_insn_arrays_ready (prog);if (err <0 ) goto free_used_maps;
kernel/bpf/verifier.c +8 -0
@@ -10287,6 +10287,14 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insnreturn err; } +/* +* Flag the program if it attempts to use mutating helpers. +* The actual taint is deferred until successful verification. +*/ +if (func_id == BPF_FUNC_probe_write_user || + func_id == BPF_FUNC_override_return) + env->prog->aux->taints_kernel = true; +/* eBPF programs must be GPL compatible to use GPL-ed functions */ if (!env->prog->gpl_compatible && fn->gpl_only) {verbose (env,"cannot call GPL-restricted function from non-GPL compatible program\n" );
kernel/panic.c +1 -0
@@ -825,6 +825,7 @@ const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = {TAINT_FLAG (RANDSTRUCT ,'T' ,' ' ),TAINT_FLAG (TEST ,'N' ,' ' ),TAINT_FLAG (FWCTL ,'J' ,' ' ), + TAINT_FLAG(UNSAFE_BPF, 'V', ' '), };#undef TAINT_FLAG
tools/debugging/kernel-chktaint +8 -0
@@ -211,6 +211,14 @@ else addout "J" echo " * fwctl's mutating debug interface was used (#19)" fi + +T=`expr $T / 2` +if [ `expr $T % 2` -eq 0 ]; then + addout " " +else + addout "V" + echo " * an unsafe eBPF program (mutating helper) was loaded (#20)" +fi echo "Raw taint value as int/string: $taint/'$out'" # report on any tainted loadable modules
Signature · 1 line
2.51.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Premature kernel tainting in bpf_prog_load() before the program is fully loaded and exposed. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Premature kernel tainting in bpf_prog_load() before the program is fully loaded and exposed. --
commit b04c8335f7a6d97d5ae1eb32256b36d77ac00d40
Author: Aaron Tomlin <[email protected]>
bpf: introduce TAINT_UNSAFE_BPF for mutating helpers
This patch introduces a new kernel taint flag (TAINT_UNSAFE_BPF / "V") to indicate when an eBPF program utilizing destructive or mutating helpers is loaded.
Quoted 3 lines
kernel/bpf/syscall.c +0
-0
[ ... ]
Quoted 4 lines
kernel/bpf/syscall.c +0
-0@@ -3083,6 +3083,13 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)if (err <0 ) goto free_used_maps;
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) · line 3095
Quoted 10 lines
kernel/bpf/syscall.c +7
-0+/* +* The program has passed the verifier. If it utilises unsafe +* helpers, formally taint the kernel now. +*/ +if (prog->aux->taints_kernel) +add_taint (TAINT_UNSAFE_BPF ,LOCKDEP_STILL_OK ); + err =bpf_prog_mark_insn_arrays_ready (prog);if (err <0 ) goto free_used_maps;
Is it possible for the kernel to be tainted prematurely here?
If bpf_prog_mark_insn_arrays_ready() or bpf_prog_alloc_id() fail, the BPF program load is aborted, the program is freed, and it is never exposed to user space or executed. Consequently, the kernel's execution flow and memory could not have been compromised by the program.
Should the add_taint() call be deferred until after bpf_prog_alloc_id() completes successfully, which is the point where the program is actually exposed and cannot be gone underneath us?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 25 lines · Aaron Tomlin
struct
taint_flag{kernel/bpf/syscall.c +7
-0@@ -3083,6 +3083,13 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)if (err <0 ) goto free_used_maps; +/* +* The program has passed the verifier. If it utilises unsafe +* helpers, formally taint the kernel now. +*/ +if (prog->aux->taints_kernel) +add_taint (TAINT_UNSAFE_BPF ,LOCKDEP_STILL_OK ); + err =bpf_prog_mark_insn_arrays_ready (prog);if (err <0 ) goto free_used_maps;kernel/bpf/verifier.c +0
-0
kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 10296
Quoted 10 lines
kernel/bpf/verifier.c +7
-0@@ -10287,6 +10287,14 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insnreturn err; } +/* +* Flag the program if it attempts to use mutating helpers. +* The actual taint is deferred until successful verification. +*/ +if (func_id == BPF_FUNC_probe_write_user || + func_id == BPF_FUNC_override_return) + env->prog->aux->taints_kernel = true;
Nack.
Please stop this spam.
We're not doing it. These helpers have been around for a long time. There was no need to taint then. There is no need to taint now.
pw-bot: cr
On Sun, May 03, 2026 at 09:51:49PM +0200, Alexei Starovoitov wrote:
[ ... ]
Quoted 8 lines
kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 10296
Quoted 7 lines
kernel/bpf/verifier.c +7
-0+/* +* Flag the program if it attempts to use mutating helpers. +* The actual taint is deferred until successful verification. +*/ +if (func_id == BPF_FUNC_probe_write_user || + func_id == BPF_FUNC_override_return) + env->prog->aux->taints_kernel = true;
Quoted 5 lines
Nack.
Please stop this spam.
We're not doing it. These helpers have been around for a long time. There was no need to taint then. There is no need to taint now.
Hi Alexei,
Fair enough. I will drop the entire series. Please ignore the v4.
Thank you for your time.
Kind regards,
Signature · 1 line
Aaron Tomlin
Steven Rostedt
maintainerQuoted 5 lines · On Sun, 3 May 2026 21:51:49 +0200 Alexei Starovoitov…
Nack.
Please stop this spam.
We're not doing it. These helpers have been around for a long time. There was no need to taint then. There is no need to taint now.
Hi Alexei,
I'm wondering if there's a way to see what modifications BPF programs are doing to the kernel? I try to make it easy to see what modifications ftrace has done (like the enabled_functions file), because I like to know how my kernel is modified since boot up.
Thus, it would be nice to know if BPF is modifying anything in user space or just what BPF programs are loaded.
Note, I'm agnostic to this change, it just brought up a previous concern of mine when I read it.
Thanks,
-- Steve
Quoted 10 lines · Steven Rostedt
Quoted 5 lines · On Sun, 3 May 2026 21:51:49 +0200 Alexei Starovoitov…
Nack.
Please stop this spam.
We're not doing it. These helpers have been around for a long time. There was no need to taint then. There is no need to taint now.
Quoted 6 lines
Hi Alexei,
I'm wondering if there's a way to see what modifications BPF programs are doing to the kernel? I try to make it easy to see what modifications ftrace has done (like the
enabled_functionsfile), because I like to know how my kernel is modified since boot up.
It's impossible to track all modifications.
See what sched-ext is doing.
What does it modify? Everything.
Steven Rostedt
maintainerQuoted 3 lines · On Wed, 13 May 2026 08:16:07 -0700 Alexei Starovoitov…
It's impossible to track all modifications.
See what sched-ext is doing.
What does it modify? Everything.
What about just having a list of what BPF programs are loaded, what they may be attached to, and what kfuncs they are calling?
-- Steve
Quoted 10 lines · Steven Rostedt
Quoted 3 lines · On Wed, 13 May 2026 08:16:07 -0700 Alexei Starovoitov…
It's impossible to track all modifications.
See what sched-ext is doing.
What does it modify? Everything.What about just having a list of what BPF programs are loaded, what they may be attached to, and what kfuncs they are calling?
Ohh. These have been available forever.
Just bpftool prog, bpftool link, bpftool prog dump xlated
Steven Rostedt
maintainerQuoted 9 lines · On Wed, 13 May 2026 09:35:29 -0700 Alexei Starovoitov…
Quoted 8 lines · Steven Rostedt
Quoted 3 lines · On Wed, 13 May 2026 08:16:07 -0700 Alexei Starovoitov…
It's impossible to track all modifications.
See what sched-ext is doing.
What does it modify? Everything.
Quoted 5 lines
Quoted 2 lines
What about just having a list of what BPF programs are loaded, what they may be attached to, and what kfuncs they are calling?
Ohh. These have been available forever.
Just bpftool prog, bpftool link, bpftool prog dump xlated
Ah thanks. That is useful.
-- Steve
Quoted 9 lines · Alexei Starovoitov
Quoted 8 lines · Steven Rostedt
Quoted 3 lines · On Wed, 13 May 2026 08:16:07 -0700 Alexei Starovoitov…
It's impossible to track all modifications.
See what sched-ext is doing.
What does it modify? Everything.
Quoted 5 lines
Quoted 2 lines
What about just having a list of what BPF programs are loaded, what they may be attached to, and what kfuncs they are calling?
Ohh. These have been available forever.
Just bpftool prog, bpftool link, bpftool prog dump xlated
Hi Alexei,
Thank you for sharing.
Kind regards,
Signature · 1 line
Aaron Tomlin