123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683// SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ #include <linux/bpf.h> #include <bpf/bpf_helpers.h> #include "bpf_misc.h" struct { __uint(type, BPF_MAP_TYPE_HASH); __uint(max_entries, 1); __type(key, long long); __type(value, long long); } map_hash_8b SEC(".maps"); #if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ (defined(__TARGET_ARCH_riscv) && __riscv_xlen == 64)) && \ defined(__BPF_FEATURE_STACK_ARGUMENT) __noinline __used static int subprog_6args(int a, int b, int c, int d, int e, int f) { return a + b + c + d + e + f; } __noinline __used static int subprog_7args(int a, int b, int c, int d, int e, int f, int g) { return a + b + c + d + e + f + g; } __noinline __used static long subprog_deref_arg6(long a, long b, long c, long d, long e, long *f) { return *f; } SEC("tc") __description("stack_arg: subprog with 6 args") __success __retval(21) __naked void stack_arg_6args(void) { asm volatile ( "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "*(u64 *)(r11 - 8) = 6;" "call subprog_6args;" "exit;" ::: __clobber_all ); } SEC("tc") __description("stack_arg: two subprogs with >5 args") __success __retval(90) __naked void stack_arg_two_subprogs(void) { asm volatile ( "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "*(u64 *)(r11 - 8) = 10;" "call subprog_6args;" "r6 = r0;" "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "*(u64 *)(r11 - 16) = 30;" "*(u64 *)(r11 - 8) = 20;" "call subprog_7args;" "r0 += r6;" "exit;" ::: __clobber_all ); } SEC("tc") __description("stack_arg: read from uninitialized stack arg slot") __failure __msg("invalid read from stack arg off 8 depth 0") __naked void stack_arg_read_uninitialized(void) { asm volatile ( "r0 = *(u64 *)(r11 + 8);" "r0 = 0;" "exit;" ::: __clobber_all ); } SEC("tc") __description("stack_arg: gap at offset -8, only wrote -16") __failure __msg("callee expects 7 args, stack arg1 is not initialized") __naked void stack_arg_gap_at_minus8(void) { asm volatile ( "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "*(u64 *)(r11 - 16) = 30;" "call subprog_7args;" "exit;" ::: __clobber_all ); } SEC("tc") __description("stack_arg: pruning with different stack arg types") __failure __log_level(2) __flag(BPF_F_TEST_STATE_FREQ) __msg("arg JOIN insn 8 -> 10 r1: _ + fp0-8 => fp0-8|fp0+0") __msg("arg JOIN insn 8 -> 10 sa0: _ + fp0-8 => fp0-8|fp0+0") __msg("R{{[0-9]}} invalid mem access 'scalar'") __naked void stack_arg_pruning_type_mismatch(void) { asm volatile ( "call %[bpf_get_prandom_u32];" "r6 = r0;" /* local = 0 on program stack */ "r7 = 0;" "*(u64 *)(r10 - 8) = r7;" /* Branch based on random value */ "if r6 s> 3 goto l0_%=;" /* Path 1: store stack pointer to outgoing arg6 */ "r1 = r10;" "r1 += -8;" "*(u64 *)(r11 - 8) = r1;" "goto l1_%=;" "l0_%=:" /* Path 2: store scalar to outgoing arg6 */ "*(u64 *)(r11 - 8) = 42;" "l1_%=:" /* Call subprog that dereferences arg6 */ "r1 = r6;" "r2 = 0;" "r3 = 0;" "r4 = 0;" "r5 = 0;" "call subprog_deref_arg6;" "exit;" :: __imm(bpf_get_prandom_u32) : __clobber_all ); } SEC("tc") __description("stack_arg: release_reference invalidates stack arg slot") __failure __msg("callee expects 6 args, stack arg1 is not initialized") __naked void stack_arg_release_ref(void) { asm volatile ( "r6 = r1;" /* struct bpf_sock_tuple tuple = {} */ "r2 = 0;" "*(u32 *)(r10 - 8) = r2;" "*(u64 *)(r10 - 16) = r2;" "*(u64 *)(r10 - 24) = r2;" "*(u64 *)(r10 - 32) = r2;" "*(u64 *)(r10 - 40) = r2;" "*(u64 *)(r10 - 48) = r2;" /* sk = bpf_sk_lookup_tcp(ctx, &tuple, sizeof(tuple), 0, 0) */ "r1 = r6;" "r2 = r10;" "r2 += -48;" "r3 = %[sizeof_bpf_sock_tuple];" "r4 = 0;" "r5 = 0;" "call %[bpf_sk_lookup_tcp];" /* r0 = sk (PTR_TO_SOCK_OR_NULL) */ "if r0 == 0 goto l0_%=;" /* Store sock ref to outgoing arg6 slot */ "*(u64 *)(r11 - 8) = r0;" /* Release the reference — invalidates the stack arg slot */ "r1 = r0;" "call %[bpf_sk_release];" /* Call subprog that dereferences arg6 — should fail */ "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "call subprog_deref_arg6;" "l0_%=:" "r0 = 0;" "exit;" : : __imm(bpf_sk_lookup_tcp), __imm(bpf_sk_release), __imm_const(sizeof_bpf_sock_tuple, sizeof(struct bpf_sock_tuple)) : __clobber_all ); } SEC("tc") __description("stack_arg: pkt pointer in stack arg slot invalidated after pull_data") __failure __msg("callee expects 6 args, stack arg1 is not initialized") __naked void stack_arg_stale_pkt_ptr(void) { asm volatile ( "r6 = r1;" "r7 = *(u32 *)(r6 + %[__sk_buff_data]);" "r8 = *(u32 *)(r6 + %[__sk_buff_data_end]);" /* check pkt has at least 1 byte */ "r0 = r7;" "r0 += 8;" "if r0 > r8 goto l0_%=;" /* Store valid pkt pointer to outgoing arg6 slot */ "*(u64 *)(r11 - 8) = r7;" /* bpf_skb_pull_data invalidates all pkt pointers */ "r1 = r6;" "r2 = 0;" "call %[bpf_skb_pull_data];" /* Call subprog that dereferences arg6 — should fail */ "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "call subprog_deref_arg6;" "l0_%=:" "r0 = 0;" "exit;" : : __imm(bpf_skb_pull_data), __imm_const(__sk_buff_data, offsetof(struct __sk_buff, data)), __imm_const(__sk_buff_data_end, offsetof(struct __sk_buff, data_end)) : __clobber_all ); } SEC("tc") __description("stack_arg: null propagation rejects deref on null branch") __failure __msg("R{{[0-9]}} invalid mem access 'scalar'") __naked void stack_arg_null_propagation_fail(void) { asm volatile ( "r1 = 0;" "*(u64 *)(r10 - 8) = r1;" /* r0 = bpf_map_lookup_elem(&map_hash_8b, &key) */ "r2 = r10;" "r2 += -8;" "r1 = %[map_hash_8b] ll;" "call %[bpf_map_lookup_elem];" /* Store PTR_TO_MAP_VALUE_OR_NULL to outgoing arg6 slot */ "*(u64 *)(r11 - 8) = r0;" /* null check on r0 */ "if r0 != 0 goto l0_%=;" /* * On null branch, outgoing slot is SCALAR(0). * Call subprog that dereferences arg6 — should fail. */ "r1 = 0;" "r2 = 0;" "r3 = 0;" "r4 = 0;" "r5 = 0;" "call subprog_deref_arg6;" "l0_%=:" "r0 = 0;" "exit;" : : __imm(bpf_map_lookup_elem), __imm_addr(map_hash_8b) : __clobber_all ); } SEC("tc") __description("stack_arg: missing store on one branch") __failure __msg("callee expects 7 args, stack arg1 is not initialized") __naked void stack_arg_missing_store_one_branch(void) { asm volatile ( "call %[bpf_get_prandom_u32];" "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" /* Write arg7 (r11-16) before branch */ "*(u64 *)(r11 - 16) = 20;" "if r0 > 0 goto l0_%=;" /* Path 1: write arg6 and call */ "*(u64 *)(r11 - 8) = 10;" "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "call subprog_7args;" "goto l1_%=;" "l0_%=:" /* Path 2: missing arg6 store, call should fail */ "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "call subprog_7args;" "l1_%=:" "r0 = 0;" "exit;" :: __imm(bpf_get_prandom_u32) : __clobber_all ); } SEC("tc") __description("stack_arg: share a store for both branches") __success __retval(0) __naked void stack_arg_shared_store(void) { asm volatile ( "call %[bpf_get_prandom_u32];" "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" /* Write arg7 (r11-16) before branch */ "*(u64 *)(r11 - 16) = 20;" "if r0 > 0 goto l0_%=;" /* Path 1: write arg6 and call */ "*(u64 *)(r11 - 8) = 10;" "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "call subprog_7args;" "goto l1_%=;" "l0_%=:" /* Path 2: also write arg6 and call */ "*(u64 *)(r11 - 8) = 30;" "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "call subprog_7args;" "l1_%=:" "r0 = 0;" "exit;" :: __imm(bpf_get_prandom_u32) : __clobber_all ); } SEC("tc") __description("stack_arg: write beyond max outgoing depth") __failure __msg("stack arg write offset -80 exceeds max 7 stack args") __naked void stack_arg_write_beyond_max(void) { asm volatile ( "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" /* Write to offset -80, way beyond any callee's needs */ "*(u64 *)(r11 - 80) = 99;" "*(u64 *)(r11 - 16) = 20;" "*(u64 *)(r11 - 8) = 10;" "call subprog_7args;" "r0 = 0;" "exit;" ::: __clobber_all ); } SEC("tc") __description("stack_arg: write unused stack arg slot") __failure __msg("func#0 writes 5 stack arg slots, but calls only require 2") __naked void stack_arg_write_unused_slot(void) { asm volatile ( "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" /* Write to offset -40, unused for the callee */ "*(u64 *)(r11 - 40) = 99;" "*(u64 *)(r11 - 16) = 20;" "*(u64 *)(r11 - 8) = 10;" "call subprog_7args;" "r0 = 0;" "exit;" ::: __clobber_all ); } SEC("tc") __description("stack_arg: sequential calls reuse slots") __failure __msg("callee expects 7 args, stack arg1 is not initialized") __naked void stack_arg_sequential_calls(void) { asm volatile ( "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "*(u64 *)(r11 - 8) = 6;" "*(u64 *)(r11 - 16) = 7;" "call subprog_7args;" "r6 = r0;" "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "call subprog_7args;" "r0 += r6;" "exit;" ::: __clobber_all ); } /* * A JIT passes the first stack arguments in registers. may_goto is not a call: they must be * the same after it, also when it looks at the clock. That is once in 65535 iterations. */ __naked __noinline __used static long loops_then_reads_8args(long a, long b, long c, long d, long e, long f, long g, long h) { asm volatile ( "r1 = 0;" "1:" "may_goto 2f;" "r1 += 1;" "if r1 < 70000 goto 1b;" "2:" "r0 = *(u64 *)(r11 + 8);" "r1 = *(u64 *)(r11 + 16);" "r0 += r1;" "r1 = *(u64 *)(r11 + 24);" "r0 += r1;" "exit;" ); } SEC("tc") __description("stack_arg: may_goto looks at the clock, then the callee reads its args") __success __retval(876) __naked void stack_arg_may_goto_in_callee(void) { asm volatile ( "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "*(u64 *)(r11 - 24) = 800;" "*(u64 *)(r11 - 16) = 70;" "*(u64 *)(r11 - 8) = 6;" "call loops_then_reads_8args;" "exit;" ::: __clobber_all ); } __naked __noinline __used static long spins_then_reads_8args(long a, long b, long c, long d, long e, long f, long g, long h) { asm volatile ( "1:" "may_goto 2f;" "goto 1b;" "2:" "r0 = *(u64 *)(r11 + 8);" "r1 = *(u64 *)(r11 + 16);" "r0 += r1;" "r1 = *(u64 *)(r11 + 24);" "r0 += r1;" "exit;" ); } SEC("tc") __description("stack_arg: the time of may_goto is up, then the callee reads its args") __success __retval(876) __naked void stack_arg_may_goto_timeout_in_callee(void) { asm volatile ( "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "*(u64 *)(r11 - 24) = 800;" "*(u64 *)(r11 - 16) = 70;" "*(u64 *)(r11 - 8) = 6;" "call spins_then_reads_8args;" "exit;" ::: __clobber_all ); } __naked __noinline __used static long reads_8args(long a, long b, long c, long d, long e, long f, long g, long h) { asm volatile ( "r0 = *(u64 *)(r11 + 8);" "r1 = *(u64 *)(r11 + 16);" "r0 += r1;" "r1 = *(u64 *)(r11 + 24);" "r0 += r1;" "exit;" ); } SEC("tc") __description("stack_arg: may_goto looks at the clock between the stores of the args and the call") __success __retval(876) __naked void stack_arg_may_goto_in_caller(void) { asm volatile ( "*(u64 *)(r11 - 24) = 800;" "*(u64 *)(r11 - 16) = 70;" "*(u64 *)(r11 - 8) = 6;" "r6 = 0;" "1:" "may_goto 2f;" "r6 += 1;" "if r6 < 70000 goto 1b;" "2:" "r1 = 1;" "r2 = 2;" "r3 = 3;" "r4 = 4;" "r5 = 5;" "call reads_8args;" "exit;" ::: __clobber_all ); } /* arg6 is an offset into the value of map_hash_8b. 0-7 are in bounds. */ __naked __noinline __used static long arg6_is_map_value_off(long a, long b, long c, long d, long e, long off) { asm volatile ( "r6 = *(u64 *)(r11 + 8);" "r1 = 0;" "*(u64 *)(r10 - 8) = r1;" "r2 = r10;" "r2 += -8;" "r1 = %[map_hash_8b] ll;" "call %[bpf_map_lookup_elem];" "if r0 == 0 goto l0_%=;" "r0 += r6;" "r0 = *(u8 *)(r0 + 0);" "l0_%=:" "exit;" : : __imm(bpf_map_lookup_elem), __imm_addr(map_hash_8b) : __clobber_all ); } SEC("tc") __description("stack_arg: a pruned state propagates precision of arg6") __failure __msg("invalid access to map value") __flag(BPF_F_TEST_STATE_FREQ) __naked void stack_arg_precision_pruned_state(void) { asm volatile ( "call %[bpf_get_prandom_u32];" "if r0 != 0 goto l0_%=;" /* Path 1: arg6 = 0. It makes arg6 precise at l3. */ "*(u64 *)(r11 - 8) = 0;" "goto l3_%=;" "l0_%=:" "if r0 == 1 goto l1_%=;" /* Path 2: arg6 = r6 = 0. Pruned at l3. It has to make r6 precise at l2. */ "r6 = 0;" "goto l2_%=;" "l1_%=:" /* Path 3: arg6 = r6 = 8. Out of bounds. */ "r6 = 8;" "l2_%=:" "*(u64 *)(r11 - 8) = r6;" "l3_%=:" "r1 = 0;" "r2 = 0;" "r3 = 0;" "r4 = 0;" "r5 = 0;" "call arg6_is_map_value_off;" "exit;" :: __imm(bpf_get_prandom_u32) : __clobber_all ); } /* The same, but backtracking of r1 sees a store through r1 and marks all scalars precise. */ __naked __noinline __used static long arg6_is_map_value_off_fallback(long a, long b, long c, long d, long e, long off) { asm volatile ( "r6 = *(u64 *)(r11 + 8);" "r1 = 0;" "*(u64 *)(r10 - 8) = r1;" "r2 = r10;" "r2 += -8;" "r1 = %[map_hash_8b] ll;" "call %[bpf_map_lookup_elem];" "if r0 == 0 goto l0_%=;" "r1 = r0;" "*(u8 *)(r1 + 0) = 0;" "r1 -= r0;" "r1 &= 0;" "r1 += r6;" "r0 += r1;" "r0 = *(u8 *)(r0 + 0);" "l0_%=:" "exit;" : : __imm(bpf_map_lookup_elem), __imm_addr(map_hash_8b) : __clobber_all ); } SEC("tc") __description("stack_arg: forcing all scalars precise covers arg6") __failure __msg("invalid access to map value") __flag(BPF_F_TEST_STATE_FREQ) __naked void stack_arg_precision_fallback(void) { asm volatile ( "call %[bpf_get_prandom_u32];" "if r0 != 0 goto l0_%=;" "*(u64 *)(r11 - 8) = 0;" "goto l1_%=;" "l0_%=:" /* Out of bounds. Must not be pruned at l1. */ "*(u64 *)(r11 - 8) = 8;" "l1_%=:" "r1 = 0;" "r2 = 0;" "r3 = 0;" "r4 = 0;" "r5 = 0;" "call arg6_is_map_value_off_fallback;" "exit;" :: __imm(bpf_get_prandom_u32) : __clobber_all ); } #else 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") __success int dummy_test(void) { return 0; } #endif char _license[] SEC("license") = "GPL";