bpf: Fix interpreter-unsupported insns
Subsystems (3)
cover bpf: Fix interpreter-unsupported insns
Request from Guillaume and Vincent [1][2], backport the following commits to linux-6.12.y with editing the commits manually.
9a6df65d5c6a ("bpf: Introduce jit_required flag and remove bpf_prog_has_kfunc_call()") f1c27922576e ("bpf: Reject programs with inlined helpers if JIT is not available") 34746b5a84ec ("bpf: Disallow interpreter fallback for arena-related insns") 7a0855e73757 ("bpf: Disallow interpreter fallback for BPF_ADDR_PERCPU insn")
As there was a big code refactoring in linux 7.1, the changes in the new file kernel/bpf/fixups.c will be backported to the positions in kernel/bpf/verifier.c.
Patch 1 introduces a new bit 'jit_required' in struct bpf_prog. The other patches set 'jit_required' to fix the issues.
Links:
[1] https://lore.kernel.org/bpf/BESPR02MB12296D62B1B3F33BA21C4F24AF1432@BESPR02MB12296.eurprd02.prod.outlook.com/
[2] https://lore.kernel.org/bpf/MR1P264MB331494502C9D13D8A9EAFB99B7E82@MR1P264MB3314.FRAP264.PROD.OUTLOOK.COM/
Leon Hwang (2):
bpf: Disallow interpreter fallback for arena-related insns
bpf: Disallow interpreter fallback for BPF_ADDR_PERCPU insn
Tiezhu Yang (2):
bpf: Introduce jit_required flag and remove bpf_prog_has_kfunc_call()
bpf: Reject programs with inlined helpers if JIT is not available
include/linux/bpf.h | 22 +++++++++++++++------- kernel/bpf/core.c | 7 ++----- kernel/bpf/verifier.c | 21 ++++++++++++--------- 3 files changed, 29 insertions(+), 21 deletions(-)
--
2.55.0
PATCH 1/4 bpf: Introduce jit_required flag and remove bpf_prog_has_kfunc_call()
From: Tiezhu Yang <[email protected]>
[ Upstream commit 9a6df65d5c6a9947ddab4e563e329720f44b8747 ]
Introduce a 'jit_required' bitfield flag in struct bpf_prog to track whether a BPF program strictly requires the JIT compiler to run. This prevents a dangerous runtime fallback to the interpreter for features that are only implemented in the JIT compiler.
Currently, bpf_prog_has_kfunc_call() is used only for kernel function calls, replace the kfunc-specific helper with the new 'jit_required' flag. This makes it easy to support other JIT-only BPF features, such as inlined helpers.
Adapt the bpf_fixup_call_args() change to fixup_call_args() in kernel/bpf/verifier.c, and the __bpf_prog_select_runtime() change to bpf_prog_select_runtime() in kernel/bpf/core.c.
--- include/linux/bpf.h | 9 ++------- kernel/bpf/core.c | 7 ++----- kernel/bpf/verifier.c | 12 ++++-------- 3 files changed, 8 insertions(+), 20 deletions(-)
include/linux/bpf.h +2 -7
@@ -1585,8 +1585,9 @@ struct bpf_prog_aux {struct bpf_prog {u16 pages;/* Number of allocated pages */ -u16 jited:1 ,/* Is our filter JIT'ed? */ +u32 jited:1 ,/* Is our filter JIT'ed? */ jit_requested:1 ,/* archs need to JIT the prog */ + jit_required:1 ,/* program strictly requires JIT compiler */ gpl_compatible:1 ,/* Is filter GPL compatible? */ cb_access:1 ,/* Is control block accessed? */ dst_needed:1, /* Do we need dst entry? */
@@ -2682,7 +2683,6 @@ const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);void bpf_task_storage_free (struct task_struct *task);void bpf_cgrp_storage_free (struct cgroup *cgroup); -bool bpf_prog_has_kfunc_call (const struct bpf_prog *prog);const struct btf_func_model *bpf_jit_find_kfunc_model (const struct bpf_prog *prog,const struct bpf_insn *insn);
@@ -2951,11 +2951,6 @@ static inline void bpf_task_storage_free(struct task_struct *task) { } -static inline bool bpf_prog_has_kfunc_call (const struct bpf_prog *prog) -{ -return false; -} -static inline const struct btf_func_model *bpf_jit_find_kfunc_model (const struct bpf_prog *prog,const struct bpf_insn *insn)
kernel/bpf/core.c +2 -5
@@ -122,6 +122,7 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag fp->aux = aux; fp->aux->prog = fp; fp->jit_requested =ebpf_jit_enabled (); + fp->jit_required =IS_ENABLED (CONFIG_BPF_JIT_ALWAYS_ON ); fp->blinding_requested =bpf_jit_blinding_enabled (fp);#ifdef CONFIG_CGROUP_BPF aux->cgroup_atype =CGROUP_BPF_ATTACH_TYPE_INVALID ;
@@ -2529,15 +2530,11 @@ struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)/* In case of BPF to BPF calls, verifier did all the prep * work with regards to JITing, etc. */ -bool jit_needed = false; +bool jit_needed = fp->jit_required;if (fp->bpf_func) goto finalize; -if (IS_ENABLED (CONFIG_BPF_JIT_ALWAYS_ON ) || -bpf_prog_has_kfunc_call (fp)) - jit_needed = true; -if (!bpf_prog_select_interpreter (fp)) jit_needed = true;
kernel/bpf/verifier.c +4 -8
@@ -2961,6 +2961,8 @@ static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) prog_aux->kfunc_tab = tab; } + env->prog->jit_required =1 ; +/* func_id == 0 is always invalid, but instead of returning an error, be * conservative and wait until the code elimination pass before returning * error, so that invalid calls that get pruned out can be in BPF programs
@@ -3069,11 +3071,6 @@ static void sort_kfunc_descs_by_imm_off(struct bpf_prog *prog) kfunc_desc_cmp_by_imm_off, NULL); } -bool bpf_prog_has_kfunc_call (const struct bpf_prog *prog) -{ -return !!prog->aux->kfunc_tab; -} -const struct btf_func_model *bpf_jit_find_kfunc_model (const struct bpf_prog *prog,const struct bpf_insn *insn)
@@ -20860,7 +20857,6 @@ static int fixup_call_args(struct bpf_verifier_env *env)#ifndef CONFIG_BPF_JIT_ALWAYS_ON struct bpf_prog *prog = env->prog;struct bpf_insn *insn = prog->insnsi; -bool has_kfunc_call =bpf_prog_has_kfunc_call (prog);int i, depth;#endif int err =0 ;
@@ -20874,8 +20870,8 @@ static int fixup_call_args(struct bpf_verifier_env *env)return err; }#ifndef CONFIG_BPF_JIT_ALWAYS_ON -if (has_kfunc_call) { -verbose (env,"calling kernel functions are not allowed in non-JITed programs\n" ); +if (prog->jit_required) { +verbose (env,"program requires BPF JIT compiler but it is not available\n" );return -EINVAL ; }if (env->subprog_cnt >1 && env->prog->aux->tail_call_reachable) {
Signature · 1 line
2.55.0
PATCH 2/4 bpf: Reject programs with inlined helpers if JIT is not available
In reply to Leon HwangFrom: Tiezhu Yang <[email protected]>
[ Upstream commit f1c27922576edccb99d0257827d09bd05c0304a6 ]
When an architecture (such as LoongArch, ARM64, and RISC-V) implements bpf_jit_inlines_helper_call(), the verifier skips rewriting the helper call offset (insn->imm) in bpf_do_misc_fixups(). This is because the helper is expected to be inlined by the JIT compiler later. Therefore, insn->imm remains as the raw helper enum ID.
However, if JIT is disabled at runtime (net.core.bpf_jit_enable=0) or if JIT compilation fails dynamically (e.g., due to OOM), the program falls back to the BPF interpreter.
When the interpreter executes (__bpf_call_base + insn->imm) with the unpatched raw ID, it jumps into an invalid address space, triggering an instruction alignment fault or a kernel panic.
Although these helpers have valid C implementations in the kernel, the omission of offset rewriting makes runtime interpreter fallback fatal.
Fix this by setting 'prog->jit_required = 1' when helper call rewriting is skipped for JIT inlining. This ensures that such programs are safely rejected if JIT is not available, preventing the runtime kernel panic.
Apply the bpf_do_misc_fixups() change from kernel/bpf/fixups.c to do_misc_fixups() in kernel/bpf/verifier.c.
--- kernel/bpf/verifier.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +3 -1
@@ -21414,8 +21414,10 @@ static int do_misc_fixups(struct bpf_verifier_env *env) }/* Skip inlining the helper call if the JIT does it. */ -if (bpf_jit_inlines_helper_call (insn->imm)) +if (bpf_jit_inlines_helper_call (insn->imm)) { + prog->jit_required =1 ; goto next_insn; + }if (insn->imm == BPF_FUNC_get_route_realm) prog->dst_needed =1 ;
Signature · 1 line
2.55.0
PATCH 3/4 bpf: Disallow interpreter fallback for arena-related insns
In reply to Leon Hwang[ Upstream commit 34746b5a84ec37c0ea2bf6808c65c5ed8790eb51 ]
Since the interpreter does not support the arena-related insns, interpreter fallback should not be allowed for these insns in core.c::__bpf_prog_select_runtime().
Currently, when the interpreter executes the arena ST/LDX/STX insns, it would hit the BUG_ON() in ___bpf_prog_run() at run time.
[ 2.579196] BPF interpreter: unknown opcode a2 (imm: 0x0) [ 2.579998] ------------[ cut here ]------------ [ 2.580652] kernel BUG at kernel/bpf/core.c:2349! [ 2.581314] Oops: invalid opcode: 0000 [#1] SMP PTI
Set jit_required as true when arena map is used in the prog to disallow interpreter fallback for arena-related insns.
Apply the check_map_prog_compatibility() change in resolve_pseudo_ldimm64(), where this branch handles arena maps.
--- kernel/bpf/verifier.c | 1 + 1 file changed, 1 insertion(+)
kernel/bpf/verifier.c +1 -0
@@ -19772,6 +19772,7 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env)return -EOPNOTSUPP ; } env->prog->aux->arena = (void *)map; + env->prog->jit_required = true;if (!bpf_arena_get_user_vm_start (env->prog->aux->arena)) {verbose (env,"arena's user address must be set via map_extra or mmap()\n" );return -EINVAL ;
Signature · 1 line
2.55.0
PATCH 4/4 bpf: Disallow interpreter fallback for BPF_ADDR_PERCPU insn
In reply to Leon Hwang[ Upstream commit 7a0855e73757ee9cf25ba635a1c735018ecba742 ]
The BPF_MOV64_PERCPU_REG insn requires JIT to emit native code to for 'dst_reg = src_reg + <percpu_base_off>'.
However, the interpreter ignores the 'off' at its ALU64_MOV_X label. The 'off' indicates the insn is BPF_MOV64_PERCPU_REG insn. Then, when the interpreter loads memory from the register, it will hit a page fault.
[ 2.545572] BUG: unable to handle page fault for address: ffffffffacaaf034 [ 2.546485] #PF: supervisor read access in kernel mode [ 2.547167] #PF: error_code(0x0000) - not-present page [ 2.547850] PGD 134e63067 P4D 134e63067 PUD 134e64063 PMD 10021c063 PTE 800ffffeca550062 [ 2.548912] Oops: Oops: 0000 [#1] SMP PTI
Set jit_required as true in order to disallow interpreter fallback in core.c::__bpf_prog_select_runtime(), if any BPF_ADDR_PERCPU insn is patched to the prog.
BTW, rename the helper bpf_map_supports_cpu_flags() to bpf_map_is_percpu_map().
Add bpf_map_is_percpu_map() directly because 6.12.y lacks bpf_map_supports_cpu_flags().
Apply the map lookup and bpf_get_smp_processor_id() changes to do_misc_fixups() in kernel/bpf/verifier.c.
Omit the current-task helper hunk because 6.12.y lacks the verifier inlining introduced by commit
eaedea154eb9 ("bpf, x86: inline bpf_get_current_task() for x86_64").
--- include/linux/bpf.h | 13 +++++++++++++ kernel/bpf/verifier.c | 4 ++++ 2 files changed, 17 insertions(+)
include/linux/bpf.h +13 -0
@@ -3504,4 +3504,17 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog) return prog->aux->func_idx != 0; } +static inline bool bpf_map_is_percpu_map (enum bpf_map_type map_type) +{ +switch (map_type) { +case BPF_MAP_TYPE_PERCPU_ARRAY : +case BPF_MAP_TYPE_PERCPU_HASH : +case BPF_MAP_TYPE_LRU_PERCPU_HASH : +case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE : +return true; +default : +return false; + } +} +#endif /* _LINUX_BPF_H */
kernel/bpf/verifier.c +4 -0
@@ -21602,6 +21602,9 @@ static int do_misc_fixups(struct bpf_verifier_env *env)return -EINVAL ; } +if (bpf_map_is_percpu_map (map_ptr->map_type)) + prog->jit_required = true; + new_prog =bpf_patch_insn_data (env, i + delta, insn_buf, cnt);if (!new_prog)
@@ -21706,6 +21709,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env)* way, it's fine to back out this inlining logic */ #ifdef CONFIG_SMP + prog->jit_required = true; insn_buf[0 ] =BPF_MOV32_IMM (BPF_REG_0 , (u32 )(unsigned long )&pcpu_hot.cpu_number); insn_buf[1 ] =BPF_MOV64_PERCPU_REG (BPF_REG_0 ,BPF_REG_0 ); insn_buf[2 ] =BPF_LDX_MEM (BPF_W ,BPF_REG_0 ,BPF_REG_0 ,0 );
Signature · 1 line
2.55.0
cc Guillaume and Vincent.
Guillaume, please test them.
Thanks,
Leon
Quoted 35 lines · Leon Hwang
Request from Guillaume and Vincent [1][2], backport the following commits to linux-6.12.y with editing the commits manually.
9a6df65d5c6a("bpf: Introducejit_requiredflag and removebpf_prog_has_kfunc_call()")f1c27922576e("bpf: Reject programs with inlined helpers if JIT is not available")34746b5a84ec("bpf: Disallow interpreter fallback for arena-related insns")7a0855e73757("bpf: Disallow interpreter fallback forBPF_ADDR_PERCPUinsn")As there was a big code refactoring in linux 7.1, the changes in the new file
kernel/bpf/fixups.cwill be backported to the positions inkernel/bpf/verifier.c.Patch 1 introduces a new bit '
jit_required' in structbpf_prog. The other patches set 'jit_required' to fix the issues.Links:
[1] https://lore.kernel.org/bpf/BESPR02MB12296D62B1B3F33BA21C4F24AF1432@BESPR02MB12296.eurprd02.prod.outlook.com/
[2] https://lore.kernel.org/bpf/MR1P264MB331494502C9D13D8A9EAFB99B7E82@MR1P264MB3314.FRAP264.PROD.OUTLOOK.COM/Leon Hwang (2):
bpf: Disallow interpreter fallback for arena-related insns
bpf: Disallow interpreter fallback forBPF_ADDR_PERCPUinsnTiezhu Yang (2):
bpf: Introducejit_requiredflag and removebpf_prog_has_kfunc_call()
bpf: Reject programs with inlined helpers if JIT is not availableinclude/linux/bpf.h | 22 +++++++++++++++------- kernel/bpf/core.c | 7 ++----- kernel/bpf/verifier.c | 21 ++++++++++++--------- 3 files changed, 29 insertions(+), 21 deletions(-)--
2.55.0
Guillaume Laporte
Tested-byQuoted 1 line · Leon Hwang
cc Guillaume and Vincent.
Thanks a lot for the backports.
Quoted 1 line
Guillaume, please test them.
I applied the 6.12.y and 6.18.y series on top of v6.12.120 and v6.18.66 respectively, and tested them on x86-64 with Debian's config (CONFIG_BPF_JIT_ALWAYS_ON not set). The results are the same on both kernels:
- with net.core.bpf_jit_harden=2, the reproducer's program is now rejected at load time with -ENOTSUPP, as expected, instead of crashing the kernel;
- with net.core.bpf_jit_harden=0, it is JIT compiled and loads as before;
- with net.core.bpf_jit_enable=0, it loads and runs in the interpreter without any issue.
As this thread is about the 6.12.y series, please let me know if I should also reply to the 6.18.y one.
Thanks,
Guillaume Laporte