From: Alexei Starovoitov The compiler can pass more or fewer arguments to a static subprog than its BTF prototype has parameters. test the subprog result ------------------------------- ---------------------------- ---------- more_than_prototype 1 in BTF, loads the 6th returns 7 more_than_prototype_no_store the same, the caller stores reject nothing less_than_prototype 7 in BTF, reads r1-r5, the returns 15 caller stores nothing stack_arg_write_prototype_slots the same, the caller stores returns 15 the 6th and the 7th r11_no_jit_support a store through r11, the JIT reject has no stack arguments global_more_than_prototype global, 1 in BTF, loads the reject 6th tgt_prog_takes_6th 1 in BTF, loads the 6th, refuse fentry attaches to it more_than_prototype also examines the x86 code of the subprog. The subprog passes no stack arguments to other functions. Thus the JIT must not move rsp in it. less_than_prototype has no insn that uses r11. It has a tail call, which a prog with stack arguments cannot have. Thus it runs only with a JIT that mixes tail calls and subprogs, also where that JIT has no stack arguments. more_than_prototype and stack_arg_write_prototype_slots store through r11. The interpreter has no stack arguments: they load only when the JIT is on. Signed-off-by: Alexei Starovoitov --- .../selftests/bpf/prog_tests/tracing_struct.c | 3 + .../bpf/progs/tracing_struct_int128_tgt.c | 9 +- .../selftests/bpf/progs/verifier_stack_arg.c | 102 ++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/tracing_struct.c b/tools/testing/selftests/bpf/prog_tests/tracing_struct.c index ab74ba077041..a08b480eca73 100644 --- a/tools/testing/selftests/bpf/prog_tests/tracing_struct.c +++ b/tools/testing/selftests/bpf/prog_tests/tracing_struct.c @@ -398,6 +398,9 @@ void test_tracing_struct(void) if (test__start_subtest("tgt_prog_unreliable")) test_tgt_prog_unreliable("unreliable_entry", "callee_unreliable", "unreliable BTF and stack arguments"); + if (test__start_subtest("tgt_prog_takes_6th")) + test_tgt_prog_unreliable("unreliable_entry", "callee_takes_6th", + "unreliable BTF and stack arguments"); if (test__start_subtest("tgt_prog_returns_pair")) test_tgt_prog_unreliable("odd_exit", "callee_takes_r5", "with a >8 byte return value"); diff --git a/tools/testing/selftests/bpf/progs/tracing_struct_int128_tgt.c b/tools/testing/selftests/bpf/progs/tracing_struct_int128_tgt.c index 2e2ddd1b52df..6a8079fe809b 100644 --- a/tools/testing/selftests/bpf/progs/tracing_struct_int128_tgt.c +++ b/tools/testing/selftests/bpf/progs/tracing_struct_int128_tgt.c @@ -69,6 +69,12 @@ static __noinline __u64 callee_unreliable(__u64 *p, __u64 b, __u64 c, __u64 d, return *p + b * 2 + c * 3 + d * 4 + e * 5 + f * 6; } +/* BTF has 'a'. The code also takes the 6th argument, on the stack. */ +static __naked __noinline __used __u64 callee_takes_6th(__u64 a) +{ + asm volatile ("r0 = *(u64 *)(r11 + 8); r0 += r1; exit;"); +} + /* BTF has 'a'. The code also takes r5. It returns in r0 and r2. */ static __naked __noinline __used __int128 callee_takes_r5(__u64 a) { @@ -77,7 +83,8 @@ static __naked __noinline __used __int128 callee_takes_r5(__u64 a) static __naked __noinline __u64 call_takes_more(void) { - asm volatile ("r1 = 7; r5 = 2; call callee_takes_r5; r0 += r2; exit;"); + asm volatile ("r1 = 1; *(u64 *)(r11 - 8) = 6; call callee_takes_6th;" + "r1 = r0; r5 = 2; call callee_takes_r5; r0 += r2; exit;"); } #else diff --git a/tools/testing/selftests/bpf/progs/verifier_stack_arg.c b/tools/testing/selftests/bpf/progs/verifier_stack_arg.c index abf319cb8514..d1fd5a402565 100644 --- a/tools/testing/selftests/bpf/progs/verifier_stack_arg.c +++ b/tools/testing/selftests/bpf/progs/verifier_stack_arg.c @@ -3,6 +3,7 @@ #include #include +#include "../../../include/linux/filter.h" #include "bpf_misc.h" struct { @@ -12,6 +13,40 @@ struct { __type(value, long long); } map_hash_8b SEC(".maps"); +#if defined(__BPF_FEATURE_STACK_ARGUMENT) + +struct { + __uint(type, BPF_MAP_TYPE_PROG_ARRAY); + __uint(max_entries, 1); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u32)); +} jmp_table SEC(".maps"); + +/* BTF has what the source says. The compiler dropped the arguments that are not used. */ +static __naked __noinline __used +long takes_5_says_7(long a, long b, long c, long d, long e, long f, long g) +{ + asm volatile ("r0 = r1; r0 += r2; r0 += r3; r0 += r4; r0 += r5; exit;" ::: __clobber_all); +} + +#define CALL_TAKES_5_SAYS_7(stores) \ + "r1 = 1; r2 = 2; r3 = 3; r4 = 4; r5 = 5;" stores "call takes_5_says_7; exit;" + +/* No insn uses r11: the JIT needs no stack arguments, and a tail call is allowed */ +SEC("tc") +__description("stack_arg: static subprog takes less than its prototype says") +__success __retval(15) +__load_if_JITed() +__arch_x86_64 __arch_arm64 __arch_riscv64 __arch_s390x __arch_loongarch +__naked void less_than_prototype(void) +{ + asm volatile ("r2 = %[jmp_table] ll; r3 = 0; call %[bpf_tail_call];" + CALL_TAKES_5_SAYS_7("") + :: __imm_addr(jmp_table), __imm(bpf_tail_call) : __clobber_all); +} + +#endif + #if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ (defined(__TARGET_ARCH_riscv) && __riscv_xlen == 64)) && \ defined(__BPF_FEATURE_STACK_ARGUMENT) @@ -94,6 +129,52 @@ __naked void stack_arg_read_uninitialized(void) ); } +/* BTF has 'a'. The code also takes the 6th argument. It passes none: rsp stays where it is. */ +static __naked __noinline __used long takes_6th_says_1(long a) +{ + asm volatile ("r0 = *(u64 *)(r11 + 8); r0 += r1; exit;" ::: __clobber_all); +} + +SEC("tc") +__description("stack_arg: static subprog takes more than its prototype says") +__success __retval(7) +__load_if_JITed() +__arch_x86_64 +__jited("func #1") +__jited("...") +__jited(" movq %r9, %rax") +__jited(" addq %rdi, %rax") +__jited(" leave") +__arch_arm64 __arch_riscv64 +__naked void more_than_prototype(void) +{ + asm volatile ("r1 = 1; *(u64 *)(r11 - 8) = 6; call takes_6th_says_1; exit;" + ::: __clobber_all); +} + +SEC("tc") +__description("stack_arg: static subprog takes more than its prototype says, no store") +__failure __msg("callee expects 6 args, stack arg1 is not initialized") +__naked void more_than_prototype_no_store(void) +{ + asm volatile ("r1 = 1; call takes_6th_says_1; exit;" ::: __clobber_all); +} + +/* BTF is the only source for a global subprog: it has no stack arguments. */ +__noinline __used long global_loads_6th(long a) +{ + asm volatile ("%0 = *(u64 *)(r11 + 8);" : "=r"(a)); + return a; +} + +SEC("tc") +__description("stack_arg: global subprog loads the 6th argument") +__failure __msg("invalid read from stack arg off 8 depth 0") +__naked void global_more_than_prototype(void) +{ + asm volatile ("r1 = 1; call global_loads_6th; exit;" ::: __clobber_all); +} + SEC("tc") __description("stack_arg: gap at offset -8, only wrote -16") __failure @@ -405,6 +486,16 @@ __naked void stack_arg_write_unused_slot(void) ); } +SEC("tc") +__description("stack_arg: write the slots of the prototype, the callee loads none") +__success __retval(15) +__load_if_JITed() +__naked void stack_arg_write_prototype_slots(void) +{ + asm volatile (CALL_TAKES_5_SAYS_7("*(u64 *)(r11 - 8) = 6; *(u64 *)(r11 - 16) = 7;") + ::: __clobber_all); +} + SEC("tc") __description("stack_arg: sequential calls reuse slots") __failure @@ -669,6 +760,17 @@ __naked void stack_arg_precision_fallback(void) #else +/* No BTF tells about this store. The insn is what JIT has to deal with. */ +SEC("tc") +__description("stack_arg: r11 where JIT has no stack arguments") +__failure __msg("JIT does not support stack arguments") +__arch_s390x __arch_loongarch +__naked void r11_no_jit_support(void) +{ + asm volatile (".8byte %[st_r11]; r0 = 0; exit;" + :: __imm_insn(st_r11, BPF_ST_MEM(BPF_DW, 11, -8, 1)) : __clobber_all); +} + SEC("socket") __description("stack_arg is not supported by compiler or jit, use a dummy test") __skip("stack_arg is not supported by compiler or jit") -- 2.55.0