xref: /linux/tools/testing/selftests/bpf/progs/verifier_prevent_map_lookup.c (revision be239684b18e1cdcafcf8c7face4a2f562c745ad)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Converted from tools/testing/selftests/bpf/verifier/prevent_map_lookup.c */
3 
4 #include <linux/bpf.h>
5 #include <bpf/bpf_helpers.h>
6 #include "bpf_misc.h"
7 
8 struct {
9 	__uint(type, BPF_MAP_TYPE_STACK_TRACE);
10 	__uint(max_entries, 1);
11 	__type(key, __u32);
12 	__type(value, __u64);
13 } map_stacktrace SEC(".maps");
14 
15 struct {
16 	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
17 	__uint(max_entries, 8);
18 	__uint(key_size, sizeof(int));
19 	__array(values, void (void));
20 } map_prog2_socket SEC(".maps");
21 
22 SEC("perf_event")
23 __description("prevent map lookup in stack trace")
24 __failure __msg("cannot pass map_type 7 into func bpf_map_lookup_elem")
25 __naked void map_lookup_in_stack_trace(void)
26 {
27 	asm volatile ("					\
28 	r1 = 0;						\
29 	*(u64*)(r10 - 8) = r1;				\
30 	r2 = r10;					\
31 	r2 += -8;					\
32 	r1 = %[map_stacktrace] ll;			\
33 	call %[bpf_map_lookup_elem];			\
34 	exit;						\
35 "	:
36 	: __imm(bpf_map_lookup_elem),
37 	  __imm_addr(map_stacktrace)
38 	: __clobber_all);
39 }
40 
41 SEC("socket")
42 __description("prevent map lookup in prog array")
43 __failure __msg("cannot pass map_type 3 into func bpf_map_lookup_elem")
44 __failure_unpriv
45 __naked void map_lookup_in_prog_array(void)
46 {
47 	asm volatile ("					\
48 	r1 = 0;						\
49 	*(u64*)(r10 - 8) = r1;				\
50 	r2 = r10;					\
51 	r2 += -8;					\
52 	r1 = %[map_prog2_socket] ll;			\
53 	call %[bpf_map_lookup_elem];			\
54 	exit;						\
55 "	:
56 	: __imm(bpf_map_lookup_elem),
57 	  __imm_addr(map_prog2_socket)
58 	: __clobber_all);
59 }
60 
61 char _license[] SEC("license") = "GPL";
62