xref: /linux/tools/testing/selftests/bpf/progs/verifier_kfunc_perfmon.c (revision fbc988f236a60cf50a89da78fc6b7213daa9999c)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <vmlinux.h>
4 #include <bpf/bpf_helpers.h>
5 #include "bpf_misc.h"
6 
7 void *user_ptr;
8 char dynptr_buf[8];
9 u64 kaddr;
10 
11 extern struct kmem_cache *bpf_get_kmem_cache(u64 addr) __ksym;
12 
13 SEC("socket")
14 __success
15 __caps_unpriv(CAP_BPF)
16 __failure_unpriv
17 __msg_unpriv("bpf_rdonly_cast is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")
18 int rdonly_cast_noperfmon(void *ctx)
19 {
20 	char *p = bpf_rdonly_cast(0, 0);
21 
22 	return p[0x7fff];
23 }
24 
25 SEC("socket")
26 __success
27 __caps_unpriv(CAP_BPF)
28 __failure_unpriv
29 __msg_unpriv("bpf_probe_read_kernel_dynptr is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")
30 int probe_read_kernel_dynptr_noperfmon(void *ctx)
31 {
32 	struct bpf_dynptr dptr;
33 
34 	bpf_dynptr_from_mem(dynptr_buf, sizeof(dynptr_buf), 0, &dptr);
35 	bpf_probe_read_kernel_dynptr(&dptr, 0, sizeof(dynptr_buf), user_ptr);
36 	return 0;
37 }
38 
39 SEC("socket")
40 __success
41 __caps_unpriv(CAP_BPF)
42 __failure_unpriv
43 __msg_unpriv("bpf_stream_vprintk is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")
44 int stream_vprintk_noperfmon(void *ctx)
45 {
46 	bpf_stream_printk(BPF_STDOUT, "%pB", (void *)kaddr);
47 	return 0;
48 }
49 
50 SEC("socket")
51 __success
52 __caps_unpriv(CAP_BPF)
53 __failure_unpriv
54 __msg_unpriv("bpf_get_kmem_cache is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")
55 int get_kmem_cache_noperfmon(void *ctx)
56 {
57 	return !!bpf_get_kmem_cache(kaddr);
58 }
59 
60 __weak int subprog_untrusted_read(void *p __arg_untrusted)
61 {
62 	return *(char *)p;
63 }
64 
65 SEC("socket")
66 __success
67 __caps_unpriv(CAP_BPF)
68 __failure_unpriv
69 __msg_unpriv("rdonly_untrusted_mem access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")
70 int arg_untrusted_read_noperfmon(void *ctx)
71 {
72 	return subprog_untrusted_read(0);
73 }
74 
75 char _license[] SEC("license") = "GPL";
76