Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: Improve verifier log for resource leak on exit

The verifier log when leaking resources on BPF_EXIT may be a bit
confusing, as it's a problem only when finally existing from the main
prog, not from any of the subprogs. Hence, update the verifier error
string and the corresponding selftests matching on it.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Suggested-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20241204030400.208005-6-memxor@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Kumar Kartikeya Dwivedi and committed by
Alexei Starovoitov
cbd8730a c8e2ee1f

+11 -11
+1 -1
kernel/bpf/verifier.c
··· 19102 19102 * match caller reference state when it exits. 19103 19103 */ 19104 19104 err = check_resource_leak(env, exception_exit, !env->cur_state->curframe, 19105 - "BPF_EXIT instruction"); 19105 + "BPF_EXIT instruction in main prog"); 19106 19106 if (err) 19107 19107 return err; 19108 19108
+2 -2
tools/testing/selftests/bpf/progs/exceptions_fail.c
··· 131 131 } 132 132 133 133 SEC("?tc") 134 - __failure __msg("BPF_EXIT instruction cannot be used inside bpf_rcu_read_lock-ed region") 134 + __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_rcu_read_lock-ed region") 135 135 int reject_with_rcu_read_lock(void *ctx) 136 136 { 137 137 bpf_rcu_read_lock(); ··· 147 147 } 148 148 149 149 SEC("?tc") 150 - __failure __msg("BPF_EXIT instruction cannot be used inside bpf_rcu_read_lock-ed region") 150 + __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_rcu_read_lock-ed region") 151 151 int reject_subprog_with_rcu_read_lock(void *ctx) 152 152 { 153 153 bpf_rcu_read_lock();
+7 -7
tools/testing/selftests/bpf/progs/preempt_lock.c
··· 6 6 #include "bpf_experimental.h" 7 7 8 8 SEC("?tc") 9 - __failure __msg("BPF_EXIT instruction cannot be used inside bpf_preempt_disable-ed region") 9 + __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region") 10 10 int preempt_lock_missing_1(struct __sk_buff *ctx) 11 11 { 12 12 bpf_preempt_disable(); ··· 14 14 } 15 15 16 16 SEC("?tc") 17 - __failure __msg("BPF_EXIT instruction cannot be used inside bpf_preempt_disable-ed region") 17 + __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region") 18 18 int preempt_lock_missing_2(struct __sk_buff *ctx) 19 19 { 20 20 bpf_preempt_disable(); ··· 23 23 } 24 24 25 25 SEC("?tc") 26 - __failure __msg("BPF_EXIT instruction cannot be used inside bpf_preempt_disable-ed region") 26 + __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region") 27 27 int preempt_lock_missing_3(struct __sk_buff *ctx) 28 28 { 29 29 bpf_preempt_disable(); ··· 33 33 } 34 34 35 35 SEC("?tc") 36 - __failure __msg("BPF_EXIT instruction cannot be used inside bpf_preempt_disable-ed region") 36 + __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region") 37 37 int preempt_lock_missing_3_minus_2(struct __sk_buff *ctx) 38 38 { 39 39 bpf_preempt_disable(); ··· 55 55 } 56 56 57 57 SEC("?tc") 58 - __failure __msg("BPF_EXIT instruction cannot be used inside bpf_preempt_disable-ed region") 58 + __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region") 59 59 int preempt_lock_missing_1_subprog(struct __sk_buff *ctx) 60 60 { 61 61 preempt_disable(); ··· 63 63 } 64 64 65 65 SEC("?tc") 66 - __failure __msg("BPF_EXIT instruction cannot be used inside bpf_preempt_disable-ed region") 66 + __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region") 67 67 int preempt_lock_missing_2_subprog(struct __sk_buff *ctx) 68 68 { 69 69 preempt_disable(); ··· 72 72 } 73 73 74 74 SEC("?tc") 75 - __failure __msg("BPF_EXIT instruction cannot be used inside bpf_preempt_disable-ed region") 75 + __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region") 76 76 int preempt_lock_missing_2_minus_1_subprog(struct __sk_buff *ctx) 77 77 { 78 78 preempt_disable();
+1 -1
tools/testing/selftests/bpf/progs/verifier_spin_lock.c
··· 187 187 188 188 SEC("cgroup/skb") 189 189 __description("spin_lock: test6 missing unlock") 190 - __failure __msg("BPF_EXIT instruction cannot be used inside bpf_spin_lock-ed region") 190 + __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_spin_lock-ed region") 191 191 __failure_unpriv __msg_unpriv("") 192 192 __naked void spin_lock_test6_missing_unlock(void) 193 193 {