From: Yazhou Tang Currently, the BPF instruction set allows bpf-to-bpf call (or internal call, pseudo call) to use a 32-bit imm field to represent the relative jump offset. However, when JIT is disabled or falls back to interpreter, the verifier invokes bpf_patch_call_args() to rewrite the call instruction. In this function, the 32-bit imm is downcasted to s16 and stored in the `off` field. void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth) { stack_depth = max_t(u32, stack_depth, 1); insn->off = (s16) insn->imm; insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] - __bpf_call_base_args; insn->code = BPF_JMP | BPF_CALL_ARGS; } If the original imm exceeds the s16 range (i.e., jump offset > 32KB), this downcast silently truncates the offset, resulting in an incorrect call target. Fix this by explicitly checking the offset boundary in bpf_patch_call_args(). If the offset is out of range, reject the program with -EINVAL and emit a clear verifier log message. Co-developed-by: Tianci Cao Signed-off-by: Tianci Cao Co-developed-by: Shenghao Yuan Signed-off-by: Shenghao Yuan Signed-off-by: Yazhou Tang Reviewed-by: Emil Tsalapatis --- include/linux/bpf.h | 2 +- kernel/bpf/core.c | 7 +++++-- kernel/bpf/verifier.c | 6 +++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 05b34a6355b0..5daea9f82b80 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2916,7 +2916,7 @@ int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size, int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size); #ifndef CONFIG_BPF_JIT_ALWAYS_ON -void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth); +int bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth); #endif struct btf *bpf_get_btf_vmlinux(void); diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 39ddc7eb49b1..471ce73b760e 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2396,13 +2396,16 @@ EVAL4(PROG_NAME_LIST, 416, 448, 480, 512) #undef PROG_NAME_LIST #ifdef CONFIG_BPF_SYSCALL -void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth) +int bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth) { - stack_depth = max_t(u32, stack_depth, 1); + if (insn->imm < S16_MIN || insn->imm > S16_MAX) + return -EINVAL; insn->off = (s16) insn->imm; + stack_depth = max_t(u32, stack_depth, 1); insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] - __bpf_call_base_args; insn->code = BPF_JMP | BPF_CALL_ARGS; + return 0; } #endif #endif diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index df22bfc572e2..702579f56f7d 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -23112,7 +23112,11 @@ static int fixup_call_args(struct bpf_verifier_env *env) depth = get_callee_stack_depth(env, insn, i); if (depth < 0) return depth; - bpf_patch_call_args(insn, depth); + err = bpf_patch_call_args(insn, depth); + if (err) { + verbose(env, "bpf-to-bpf call offset out of range for interpreter\n"); + return err; + } } err = 0; #endif -- 2.53.0 From: Yazhou Tang Add a selftest to verify the verifier and JIT behavior when handling bpf-to-bpf calls with relative jump offsets exceeding the s16 boundary. The test utilizes an inline assembly block with ".rept 200000" to generate a massive dummy subprogram. By placing this padding between the main program and the target subprogram, it forces the verifier to process a bpf-to-bpf call with the imm field exceeding the s16 range. The user-space test driver dynamically adapts to the host's configuration: - When JIT is enabled, it asserts that the program is successfully loaded, JIT-compiled, and executes correctly to return the expected value. - When JIT is disabled, it asserts that the verifier cleanly rejects the program with -EINVAL (or -ENOSPC if the log buffer is truncated), matching the expected "offset out of range" error log. Note: To ensure the test is both effective at load time and efficient at runtime, two specific tricks are employed: 1. Prevent Compiler Optimization: A volatile variable (magic_ret) is used inside target_subprog(). This prevents the compiler from optimizing the function away or aggressively inlining it, ensuring the subprogram and the call instruction are strictly retained in the final BPF bytecode. 2. Prevent Execution Degradation: The massive padding subprogram is conditionally called using a global volatile variable ("int zero", whose value is actually 0). This forces the verifier to statically analyze the branch and calculate the giant jump offset, but allows the runtime to completely bypass the 200,000 dummy instructions, preventing performance degradation during test execution. Co-developed-by: Tianci Cao Signed-off-by: Tianci Cao Co-developed-by: Shenghao Yuan Signed-off-by: Shenghao Yuan Signed-off-by: Yazhou Tang --- .../selftests/bpf/prog_tests/call_large_imm.c | 53 +++++++++++++++++++ .../selftests/bpf/progs/call_large_imm.c | 37 +++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/call_large_imm.c create mode 100644 tools/testing/selftests/bpf/progs/call_large_imm.c diff --git a/tools/testing/selftests/bpf/prog_tests/call_large_imm.c b/tools/testing/selftests/bpf/prog_tests/call_large_imm.c new file mode 100644 index 000000000000..95d51b3cc6b6 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/call_large_imm.c @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include "call_large_imm.skel.h" + +/* + * This test dynamically adapts to the current environment, and will succeed + * whether JIT is enabled or not. + * 1. If JIT is enabled: The test case will successfully load and return + * the expected value. + * 2. If JIT is disabled: It asserts that the verifier cleanly rejects the + * program with -EINVAL (or -ENOSPC) and outputs the expected error log. + */ + +void test_call_large_imm(void) +{ + struct call_large_imm *skel; + int err, prog_fd; + char log_buf[4096]; + + LIBBPF_OPTS(bpf_test_run_opts, opts); + + skel = call_large_imm__open(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + if (!env.jit_enabled) + bpf_program__set_log_buf(skel->progs.call_large_imm_test, + log_buf, sizeof(log_buf)); + + err = call_large_imm__load(skel); + + if (env.jit_enabled) { + if (!ASSERT_OK(err, "load_should_succeed_with_jit")) + goto cleanup; + + prog_fd = bpf_program__fd(skel->progs.call_large_imm_test); + err = bpf_prog_test_run_opts(prog_fd, &opts); + + if (ASSERT_OK(err, "prog_run_success")) + ASSERT_EQ(opts.retval, 3, "prog_retval"); + + } else { + ASSERT_ERR(err, "load_should_fail_in_interpreter"); + ASSERT_TRUE(err == -EINVAL || err == -ENOSPC, "err_should_be_einval_or_enospc"); + + if (!ASSERT_OK_PTR(strstr(log_buf, "bpf-to-bpf call offset out of range for interpreter"), + "check_verifier_log_msg")) { + printf("Actual verifier log:\n%s\n", log_buf); + } + } + +cleanup: + call_large_imm__destroy(skel); diff --git a/tools/testing/selftests/bpf/progs/call_large_imm.c b/tools/testing/selftests/bpf/progs/call_large_imm.c new file mode 100644 index 000000000000..ba6cd40738b7 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/call_large_imm.c @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +/* + * A volatile global variable is used here, so that padding_subprog() + * will not be optimized, and it will not be really executed even if + * it is successfully loaded (when JIT is enabled). + */ +volatile int zero = 0; + +static __attribute__((noinline)) void padding_subprog(void) +{ + asm volatile (" \ + r0 = 0; \ + .rept 200000; \ + r0 += 0; \ + .endr; \ + " ::: "r0"); +} + +static __attribute__((noinline)) int target_subprog(void) +{ + /* A volatile variable is used here to prevent optimization. */ + volatile int magic_ret = 3; + return magic_ret; +} + +SEC("syscall") +int call_large_imm_test(void *ctx) +{ + if (zero) + padding_subprog(); + return target_subprog(); +} + +char LICENSE[] SEC("license") = "GPL"; -- 2.53.0 From: Yazhou Tang In jit_subprogs(), restoring bpf_pseudo_call instructions truncates the 32-bit call_imm into the 16-bit insn->off. For large BPF programs (e.g., offsets > 32767), this silent truncation causes find_subprog() to return an incorrect subprog ID, which is then stored in insn->imm. Consequently, "bpftool prog dump xlated" reads the corrupted insn->imm, fails to resolve the subprogram name, and incorrectly displays a raw kernel pointer. For example, dumping the xlated bytecode of the JIT-loaded selftest introduced in patch 2/3 shows: Before this patch (with call_imm > 32767): 4: (85) call pc+2#bpf_prog_115951674716e036_padding_subprog 5: (85) call pc+3395#0xffffffff814aed4e After this patch: 4: (85) call pc+2#bpf_prog_115951674716e036_padding_subprog 5: (85) call pc+3395#bpf_prog_5dccd3f669bbe4e4_target_subprog This patch fixes the issue by passing the un-truncated 32-bit env->insn_aux_data[i].call_imm directly to find_subprog(). Note that the assignment to insn->off is retained. Although it still truncates for large jumps, it remains correct for the vast majority of BPF programs where the offset fits within s16 bounds. Keeping it ensures backward compatibility for existing user-space tools that rely on insn->off to display the relative offset. A comment is added to explicitly clarify this truncation behavior. Suggested-by: Puranjay Mohan Co-developed-by: Tianci Cao Signed-off-by: Tianci Cao Co-developed-by: Shenghao Yuan Signed-off-by: Shenghao Yuan Signed-off-by: Yazhou Tang --- Hi Puranjay, As you mentioned in the last iteration, this still leaves insn->off wrong but is generally fine. To clarify this for future readers, I have added an inline comment explaining the truncation. I also debated whether to add a warning log when the truncation actually occurs. However, since the program will load and run perfectly fine after this fix, I held off on adding it to avoid unnecessary log spam. Do you think a warning log is worth adding here, or is the code comment sufficient? kernel/bpf/verifier.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 702579f56f7d..2178c3b2ef97 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -23017,8 +23017,12 @@ static int jit_subprogs(struct bpf_verifier_env *env) } if (!bpf_pseudo_call(insn)) continue; + /* + * Note: If the original insn->imm is out of s16 bounds, + * insn->off here will be a truncated value. + */ insn->off = env->insn_aux_data[i].call_imm; - subprog = find_subprog(env, i + insn->off + 1); + subprog = find_subprog(env, i + env->insn_aux_data[i].call_imm + 1); insn->imm = subprog; } -- 2.53.0