Loading...
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2017 Facebook
3
4#include "vmlinux.h"
5#include <bpf/bpf_helpers.h>
6#include <bpf/bpf_tracing.h>
7#include <bpf/bpf_core_read.h>
8#include <errno.h>
9#include "bpf_misc.h"
10
11u32 dynamic_sz = 1;
12int kprobe2_res = 0;
13int kretprobe2_res = 0;
14int uprobe_byname_res = 0;
15int uretprobe_byname_res = 0;
16int uprobe_byname2_res = 0;
17int uretprobe_byname2_res = 0;
18int uprobe_byname3_sleepable_res = 0;
19int uprobe_byname3_str_sleepable_res = 0;
20int uprobe_byname3_res = 0;
21int uretprobe_byname3_sleepable_res = 0;
22int uretprobe_byname3_str_sleepable_res = 0;
23int uretprobe_byname3_res = 0;
24void *user_ptr = 0;
25
26int bpf_copy_from_user_str(void *dst, u32, const void *, u64) __weak __ksym;
27
28SEC("ksyscall/nanosleep")
29int BPF_KSYSCALL(handle_kprobe_auto, struct __kernel_timespec *req, struct __kernel_timespec *rem)
30{
31 kprobe2_res = 11;
32 return 0;
33}
34
35SEC("kretsyscall/nanosleep")
36int BPF_KRETPROBE(handle_kretprobe_auto, int ret)
37{
38 kretprobe2_res = 22;
39 return ret;
40}
41
42SEC("uprobe")
43int handle_uprobe_ref_ctr(struct pt_regs *ctx)
44{
45 return 0;
46}
47
48SEC("uretprobe")
49int handle_uretprobe_ref_ctr(struct pt_regs *ctx)
50{
51 return 0;
52}
53
54SEC("uprobe")
55int handle_uprobe_byname(struct pt_regs *ctx)
56{
57 uprobe_byname_res = 5;
58 return 0;
59}
60
61/* use auto-attach format for section definition. */
62SEC("uretprobe//proc/self/exe:trigger_func2")
63int handle_uretprobe_byname(struct pt_regs *ctx)
64{
65 uretprobe_byname_res = 6;
66 return 0;
67}
68
69SEC("uprobe")
70int BPF_UPROBE(handle_uprobe_byname2, const char *pathname, const char *mode)
71{
72 char mode_buf[2] = {};
73
74 /* verify fopen mode */
75 bpf_probe_read_user(mode_buf, sizeof(mode_buf), mode);
76 if (mode_buf[0] == 'r' && mode_buf[1] == 0)
77 uprobe_byname2_res = 7;
78 return 0;
79}
80
81SEC("uretprobe")
82int BPF_URETPROBE(handle_uretprobe_byname2, void *ret)
83{
84 uretprobe_byname2_res = 8;
85 return 0;
86}
87
88static __always_inline bool verify_sleepable_user_copy(void)
89{
90 char data[9];
91
92 bpf_copy_from_user(data, sizeof(data), user_ptr);
93 return bpf_strncmp(data, sizeof(data), "test_data") == 0;
94}
95
96static __always_inline bool verify_sleepable_user_copy_str(void)
97{
98 int ret;
99 char data_long[20];
100 char data_long_pad[20];
101 char data_long_err[20];
102 char data_short[4];
103 char data_short_pad[4];
104
105 ret = bpf_copy_from_user_str(data_short, sizeof(data_short), user_ptr, 0);
106
107 if (bpf_strncmp(data_short, 4, "tes\0") != 0 || ret != 4)
108 return false;
109
110 ret = bpf_copy_from_user_str(data_short_pad, sizeof(data_short_pad), user_ptr, BPF_F_PAD_ZEROS);
111
112 if (bpf_strncmp(data_short, 4, "tes\0") != 0 || ret != 4)
113 return false;
114
115 /* Make sure this passes the verifier */
116 ret = bpf_copy_from_user_str(data_long, dynamic_sz & sizeof(data_long), user_ptr, 0);
117
118 if (ret != 0)
119 return false;
120
121 ret = bpf_copy_from_user_str(data_long, sizeof(data_long), user_ptr, 0);
122
123 if (bpf_strncmp(data_long, 10, "test_data\0") != 0 || ret != 10)
124 return false;
125
126 ret = bpf_copy_from_user_str(data_long_pad, sizeof(data_long_pad), user_ptr, BPF_F_PAD_ZEROS);
127
128 if (bpf_strncmp(data_long_pad, 10, "test_data\0") != 0 || ret != 10 || data_long_pad[19] != '\0')
129 return false;
130
131 ret = bpf_copy_from_user_str(data_long_err, sizeof(data_long_err), (void *)data_long, BPF_F_PAD_ZEROS);
132
133 if (ret > 0 || data_long_err[19] != '\0')
134 return false;
135
136 ret = bpf_copy_from_user_str(data_long, sizeof(data_long), user_ptr, 2);
137
138 if (ret != -EINVAL)
139 return false;
140
141 return true;
142}
143
144SEC("uprobe.s//proc/self/exe:trigger_func3")
145int handle_uprobe_byname3_sleepable(struct pt_regs *ctx)
146{
147 if (verify_sleepable_user_copy())
148 uprobe_byname3_sleepable_res = 9;
149 if (verify_sleepable_user_copy_str())
150 uprobe_byname3_str_sleepable_res = 10;
151 return 0;
152}
153
154/**
155 * same target as the uprobe.s above to force sleepable and non-sleepable
156 * programs in the same bpf_prog_array
157 */
158SEC("uprobe//proc/self/exe:trigger_func3")
159int handle_uprobe_byname3(struct pt_regs *ctx)
160{
161 uprobe_byname3_res = 11;
162 return 0;
163}
164
165SEC("uretprobe.s//proc/self/exe:trigger_func3")
166int handle_uretprobe_byname3_sleepable(struct pt_regs *ctx)
167{
168 if (verify_sleepable_user_copy())
169 uretprobe_byname3_sleepable_res = 12;
170 if (verify_sleepable_user_copy_str())
171 uretprobe_byname3_str_sleepable_res = 13;
172 return 0;
173}
174
175SEC("uretprobe//proc/self/exe:trigger_func3")
176int handle_uretprobe_byname3(struct pt_regs *ctx)
177{
178 uretprobe_byname3_res = 14;
179 return 0;
180}
181
182
183char _license[] SEC("license") = "GPL";
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2017 Facebook
3
4#include "vmlinux.h"
5#include <bpf/bpf_helpers.h>
6#include <bpf/bpf_tracing.h>
7#include <bpf/bpf_core_read.h>
8#include "bpf_misc.h"
9
10int kprobe_res = 0;
11int kprobe2_res = 0;
12int kretprobe_res = 0;
13int kretprobe2_res = 0;
14int uprobe_res = 0;
15int uretprobe_res = 0;
16int uprobe_byname_res = 0;
17int uretprobe_byname_res = 0;
18int uprobe_byname2_res = 0;
19int uretprobe_byname2_res = 0;
20int uprobe_byname3_sleepable_res = 0;
21int uprobe_byname3_res = 0;
22int uretprobe_byname3_sleepable_res = 0;
23int uretprobe_byname3_res = 0;
24void *user_ptr = 0;
25
26SEC("kprobe")
27int handle_kprobe(struct pt_regs *ctx)
28{
29 kprobe_res = 1;
30 return 0;
31}
32
33SEC("ksyscall/nanosleep")
34int BPF_KSYSCALL(handle_kprobe_auto, struct __kernel_timespec *req, struct __kernel_timespec *rem)
35{
36 kprobe2_res = 11;
37 return 0;
38}
39
40/**
41 * This program will be manually made sleepable on the userspace side
42 * and should thus be unattachable.
43 */
44SEC("kprobe/" SYS_PREFIX "sys_nanosleep")
45int handle_kprobe_sleepable(struct pt_regs *ctx)
46{
47 kprobe_res = 2;
48 return 0;
49}
50
51SEC("kretprobe")
52int handle_kretprobe(struct pt_regs *ctx)
53{
54 kretprobe_res = 2;
55 return 0;
56}
57
58SEC("kretsyscall/nanosleep")
59int BPF_KRETPROBE(handle_kretprobe_auto, int ret)
60{
61 kretprobe2_res = 22;
62 return ret;
63}
64
65SEC("uprobe")
66int handle_uprobe(struct pt_regs *ctx)
67{
68 uprobe_res = 3;
69 return 0;
70}
71
72SEC("uretprobe")
73int handle_uretprobe(struct pt_regs *ctx)
74{
75 uretprobe_res = 4;
76 return 0;
77}
78
79SEC("uprobe")
80int handle_uprobe_byname(struct pt_regs *ctx)
81{
82 uprobe_byname_res = 5;
83 return 0;
84}
85
86/* use auto-attach format for section definition. */
87SEC("uretprobe//proc/self/exe:trigger_func2")
88int handle_uretprobe_byname(struct pt_regs *ctx)
89{
90 uretprobe_byname_res = 6;
91 return 0;
92}
93
94SEC("uprobe")
95int handle_uprobe_byname2(struct pt_regs *ctx)
96{
97 unsigned int size = PT_REGS_PARM1(ctx);
98
99 /* verify malloc size */
100 if (size == 1)
101 uprobe_byname2_res = 7;
102 return 0;
103}
104
105SEC("uretprobe")
106int handle_uretprobe_byname2(struct pt_regs *ctx)
107{
108 uretprobe_byname2_res = 8;
109 return 0;
110}
111
112static __always_inline bool verify_sleepable_user_copy(void)
113{
114 char data[9];
115
116 bpf_copy_from_user(data, sizeof(data), user_ptr);
117 return bpf_strncmp(data, sizeof(data), "test_data") == 0;
118}
119
120SEC("uprobe.s//proc/self/exe:trigger_func3")
121int handle_uprobe_byname3_sleepable(struct pt_regs *ctx)
122{
123 if (verify_sleepable_user_copy())
124 uprobe_byname3_sleepable_res = 9;
125 return 0;
126}
127
128/**
129 * same target as the uprobe.s above to force sleepable and non-sleepable
130 * programs in the same bpf_prog_array
131 */
132SEC("uprobe//proc/self/exe:trigger_func3")
133int handle_uprobe_byname3(struct pt_regs *ctx)
134{
135 uprobe_byname3_res = 10;
136 return 0;
137}
138
139SEC("uretprobe.s//proc/self/exe:trigger_func3")
140int handle_uretprobe_byname3_sleepable(struct pt_regs *ctx)
141{
142 if (verify_sleepable_user_copy())
143 uretprobe_byname3_sleepable_res = 11;
144 return 0;
145}
146
147SEC("uretprobe//proc/self/exe:trigger_func3")
148int handle_uretprobe_byname3(struct pt_regs *ctx)
149{
150 uretprobe_byname3_res = 12;
151 return 0;
152}
153
154
155char _license[] SEC("license") = "GPL";