xref: /linux/tools/testing/selftests/bpf/progs/tailcall_fail.c (revision c8b90d40d5bba8e6fba457b8a7c10d3c0d467e37)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3 #include <vmlinux.h>
4 #include <bpf/bpf_tracing.h>
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_core_read.h>
7 
8 #include "bpf_misc.h"
9 #include "bpf_experimental.h"
10 
11 extern void bpf_rcu_read_lock(void) __ksym;
12 extern void bpf_rcu_read_unlock(void) __ksym;
13 
14 #define private(name) SEC(".bss." #name) __hidden __attribute__((aligned(8)))
15 
16 private(A) struct bpf_spin_lock lock;
17 
18 struct {
19 	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
20 	__uint(max_entries, 3);
21 	__uint(key_size, sizeof(__u32));
22 	__uint(value_size, sizeof(__u32));
23 } jmp_table SEC(".maps");
24 
25 SEC("?tc")
26 __failure __msg("function calls are not allowed while holding a lock")
27 int reject_tail_call_spin_lock(struct __sk_buff *ctx)
28 {
29 	bpf_spin_lock(&lock);
30 	bpf_tail_call_static(ctx, &jmp_table, 0);
31 	return 0;
32 }
33 
34 SEC("?tc")
35 __failure __msg("tail_call cannot be used inside bpf_rcu_read_lock-ed region")
36 int reject_tail_call_rcu_lock(struct __sk_buff *ctx)
37 {
38 	bpf_rcu_read_lock();
39 	bpf_tail_call_static(ctx, &jmp_table, 0);
40 	bpf_rcu_read_unlock();
41 	return 0;
42 }
43 
44 SEC("?tc")
45 __failure __msg("tail_call cannot be used inside bpf_preempt_disable-ed region")
46 int reject_tail_call_preempt_lock(struct __sk_buff *ctx)
47 {
48 	bpf_guard_preempt();
49 	bpf_tail_call_static(ctx, &jmp_table, 0);
50 	return 0;
51 }
52 
53 SEC("?tc")
54 __failure __msg("tail_call would lead to reference leak")
55 int reject_tail_call_ref(struct __sk_buff *ctx)
56 {
57 	struct foo { int i; } *p;
58 
59 	p = bpf_obj_new(typeof(*p));
60 	bpf_tail_call_static(ctx, &jmp_table, 0);
61 	return 0;
62 }
63 
64 char _license[] SEC("license") = "GPL";
65