Commit | Line | Data |
---|---|---|
179a0cc4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2541517c | 2 | /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com |
0515e599 | 3 | * Copyright (c) 2016 Facebook |
2541517c AS |
4 | */ |
5 | #include <linux/kernel.h> | |
6 | #include <linux/types.h> | |
7 | #include <linux/slab.h> | |
8 | #include <linux/bpf.h> | |
4279adb0 | 9 | #include <linux/bpf_verifier.h> |
0515e599 | 10 | #include <linux/bpf_perf_event.h> |
c4d0bfb4 | 11 | #include <linux/btf.h> |
2541517c AS |
12 | #include <linux/filter.h> |
13 | #include <linux/uaccess.h> | |
9c959c86 | 14 | #include <linux/ctype.h> |
9802d865 | 15 | #include <linux/kprobes.h> |
ac5a72ea | 16 | #include <linux/spinlock.h> |
41bdc4b4 | 17 | #include <linux/syscalls.h> |
540adea3 | 18 | #include <linux/error-injection.h> |
c9a0f3b8 | 19 | #include <linux/btf_ids.h> |
6f100640 | 20 | #include <linux/bpf_lsm.h> |
0dcac272 | 21 | #include <linux/fprobe.h> |
ca74823c JO |
22 | #include <linux/bsearch.h> |
23 | #include <linux/sort.h> | |
f3cf4134 RS |
24 | #include <linux/key.h> |
25 | #include <linux/verification.h> | |
89ae89f5 | 26 | #include <linux/namei.h> |
6f100640 | 27 | |
8e4597c6 | 28 | #include <net/bpf_sk_storage.h> |
9802d865 | 29 | |
c4d0bfb4 AM |
30 | #include <uapi/linux/bpf.h> |
31 | #include <uapi/linux/btf.h> | |
32 | ||
c7b6f29b NA |
33 | #include <asm/tlb.h> |
34 | ||
9802d865 | 35 | #include "trace_probe.h" |
2541517c AS |
36 | #include "trace.h" |
37 | ||
ac5a72ea AM |
38 | #define CREATE_TRACE_POINTS |
39 | #include "bpf_trace.h" | |
40 | ||
e672db03 SF |
41 | #define bpf_event_rcu_dereference(p) \ |
42 | rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex)) | |
43 | ||
8b2efe51 | 44 | #define MAX_UPROBE_MULTI_CNT (1U << 20) |
d6d1e6c1 | 45 | #define MAX_KPROBE_MULTI_CNT (1U << 20) |
8b2efe51 | 46 | |
a38d1107 MM |
47 | #ifdef CONFIG_MODULES |
48 | struct bpf_trace_module { | |
49 | struct module *module; | |
50 | struct list_head list; | |
51 | }; | |
52 | ||
53 | static LIST_HEAD(bpf_trace_modules); | |
54 | static DEFINE_MUTEX(bpf_module_mutex); | |
55 | ||
56 | static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) | |
57 | { | |
58 | struct bpf_raw_event_map *btp, *ret = NULL; | |
59 | struct bpf_trace_module *btm; | |
60 | unsigned int i; | |
61 | ||
62 | mutex_lock(&bpf_module_mutex); | |
63 | list_for_each_entry(btm, &bpf_trace_modules, list) { | |
64 | for (i = 0; i < btm->module->num_bpf_raw_events; ++i) { | |
65 | btp = &btm->module->bpf_raw_events[i]; | |
66 | if (!strcmp(btp->tp->name, name)) { | |
67 | if (try_module_get(btm->module)) | |
68 | ret = btp; | |
69 | goto out; | |
70 | } | |
71 | } | |
72 | } | |
73 | out: | |
74 | mutex_unlock(&bpf_module_mutex); | |
75 | return ret; | |
76 | } | |
77 | #else | |
78 | static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) | |
79 | { | |
80 | return NULL; | |
81 | } | |
82 | #endif /* CONFIG_MODULES */ | |
83 | ||
035226b9 | 84 | u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); |
c195651e | 85 | u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); |
035226b9 | 86 | |
eb411377 AM |
87 | static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size, |
88 | u64 flags, const struct btf **btf, | |
89 | s32 *btf_id); | |
f7098690 JO |
90 | static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx); |
91 | static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx); | |
eb411377 | 92 | |
0b779b61 | 93 | static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx); |
686328d8 | 94 | static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx); |
0b779b61 | 95 | |
2541517c AS |
96 | /** |
97 | * trace_call_bpf - invoke BPF program | |
e87c6bc3 | 98 | * @call: tracepoint event |
2541517c AS |
99 | * @ctx: opaque context pointer |
100 | * | |
101 | * kprobe handlers execute BPF programs via this helper. | |
102 | * Can be used from static tracepoints in the future. | |
103 | * | |
104 | * Return: BPF programs always return an integer which is interpreted by | |
105 | * kprobe handler as: | |
106 | * 0 - return from kprobe (event is filtered out) | |
107 | * 1 - store kprobe event into ring buffer | |
108 | * Other values are reserved and currently alias to 1 | |
109 | */ | |
e87c6bc3 | 110 | unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx) |
2541517c AS |
111 | { |
112 | unsigned int ret; | |
113 | ||
b0a81b94 | 114 | cant_sleep(); |
2541517c AS |
115 | |
116 | if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) { | |
117 | /* | |
118 | * since some bpf program is already running on this cpu, | |
119 | * don't call into another bpf program (same or different) | |
120 | * and don't send kprobe event into ring-buffer, | |
121 | * so return zero here | |
122 | */ | |
dd865789 JO |
123 | rcu_read_lock(); |
124 | bpf_prog_inc_misses_counters(rcu_dereference(call->prog_array)); | |
125 | rcu_read_unlock(); | |
2541517c AS |
126 | ret = 0; |
127 | goto out; | |
128 | } | |
129 | ||
e87c6bc3 YS |
130 | /* |
131 | * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock | |
132 | * to all call sites, we did a bpf_prog_array_valid() there to check | |
133 | * whether call->prog_array is empty or not, which is | |
2b5894cc | 134 | * a heuristic to speed up execution. |
e87c6bc3 YS |
135 | * |
136 | * If bpf_prog_array_valid() fetched prog_array was | |
137 | * non-NULL, we go into trace_call_bpf() and do the actual | |
138 | * proper rcu_dereference() under RCU lock. | |
139 | * If it turns out that prog_array is NULL then, we bail out. | |
140 | * For the opposite, if the bpf_prog_array_valid() fetched pointer | |
141 | * was NULL, you'll skip the prog_array with the risk of missing | |
142 | * out of events when it was updated in between this and the | |
143 | * rcu_dereference() which is accepted risk. | |
144 | */ | |
055eb955 SF |
145 | rcu_read_lock(); |
146 | ret = bpf_prog_run_array(rcu_dereference(call->prog_array), | |
147 | ctx, bpf_prog_run); | |
148 | rcu_read_unlock(); | |
2541517c AS |
149 | |
150 | out: | |
151 | __this_cpu_dec(bpf_prog_active); | |
2541517c AS |
152 | |
153 | return ret; | |
154 | } | |
2541517c | 155 | |
9802d865 JB |
156 | #ifdef CONFIG_BPF_KPROBE_OVERRIDE |
157 | BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc) | |
158 | { | |
9802d865 | 159 | regs_set_return_value(regs, rc); |
540adea3 | 160 | override_function_with_return(regs); |
9802d865 JB |
161 | return 0; |
162 | } | |
163 | ||
164 | static const struct bpf_func_proto bpf_override_return_proto = { | |
165 | .func = bpf_override_return, | |
166 | .gpl_only = true, | |
167 | .ret_type = RET_INTEGER, | |
168 | .arg1_type = ARG_PTR_TO_CTX, | |
169 | .arg2_type = ARG_ANYTHING, | |
170 | }; | |
171 | #endif | |
172 | ||
8d92db5c CH |
173 | static __always_inline int |
174 | bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr) | |
2541517c | 175 | { |
8d92db5c | 176 | int ret; |
2541517c | 177 | |
c0ee37e8 | 178 | ret = copy_from_user_nofault(dst, unsafe_ptr, size); |
6ae08ae3 DB |
179 | if (unlikely(ret < 0)) |
180 | memset(dst, 0, size); | |
6ae08ae3 DB |
181 | return ret; |
182 | } | |
183 | ||
8d92db5c CH |
184 | BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size, |
185 | const void __user *, unsafe_ptr) | |
186 | { | |
187 | return bpf_probe_read_user_common(dst, size, unsafe_ptr); | |
188 | } | |
189 | ||
f470378c | 190 | const struct bpf_func_proto bpf_probe_read_user_proto = { |
6ae08ae3 DB |
191 | .func = bpf_probe_read_user, |
192 | .gpl_only = true, | |
193 | .ret_type = RET_INTEGER, | |
194 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, | |
195 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, | |
196 | .arg3_type = ARG_ANYTHING, | |
197 | }; | |
198 | ||
8d92db5c CH |
199 | static __always_inline int |
200 | bpf_probe_read_user_str_common(void *dst, u32 size, | |
201 | const void __user *unsafe_ptr) | |
6ae08ae3 | 202 | { |
8d92db5c | 203 | int ret; |
6ae08ae3 | 204 | |
6fa6d280 DX |
205 | /* |
206 | * NB: We rely on strncpy_from_user() not copying junk past the NUL | |
207 | * terminator into `dst`. | |
208 | * | |
209 | * strncpy_from_user() does long-sized strides in the fast path. If the | |
210 | * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`, | |
211 | * then there could be junk after the NUL in `dst`. If user takes `dst` | |
212 | * and keys a hash map with it, then semantically identical strings can | |
213 | * occupy multiple entries in the map. | |
214 | */ | |
8d92db5c | 215 | ret = strncpy_from_user_nofault(dst, unsafe_ptr, size); |
6ae08ae3 DB |
216 | if (unlikely(ret < 0)) |
217 | memset(dst, 0, size); | |
6ae08ae3 DB |
218 | return ret; |
219 | } | |
220 | ||
8d92db5c CH |
221 | BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size, |
222 | const void __user *, unsafe_ptr) | |
223 | { | |
224 | return bpf_probe_read_user_str_common(dst, size, unsafe_ptr); | |
225 | } | |
226 | ||
f470378c | 227 | const struct bpf_func_proto bpf_probe_read_user_str_proto = { |
6ae08ae3 DB |
228 | .func = bpf_probe_read_user_str, |
229 | .gpl_only = true, | |
230 | .ret_type = RET_INTEGER, | |
231 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, | |
232 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, | |
233 | .arg3_type = ARG_ANYTHING, | |
234 | }; | |
235 | ||
6ae08ae3 DB |
236 | BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size, |
237 | const void *, unsafe_ptr) | |
238 | { | |
8d92db5c | 239 | return bpf_probe_read_kernel_common(dst, size, unsafe_ptr); |
6ae08ae3 DB |
240 | } |
241 | ||
f470378c | 242 | const struct bpf_func_proto bpf_probe_read_kernel_proto = { |
6ae08ae3 DB |
243 | .func = bpf_probe_read_kernel, |
244 | .gpl_only = true, | |
245 | .ret_type = RET_INTEGER, | |
246 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, | |
247 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, | |
248 | .arg3_type = ARG_ANYTHING, | |
249 | }; | |
250 | ||
6ae08ae3 | 251 | static __always_inline int |
8d92db5c | 252 | bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr) |
6ae08ae3 | 253 | { |
ff40e510 | 254 | int ret; |
8d92db5c | 255 | |
6ae08ae3 | 256 | /* |
8d92db5c CH |
257 | * The strncpy_from_kernel_nofault() call will likely not fill the |
258 | * entire buffer, but that's okay in this circumstance as we're probing | |
6ae08ae3 DB |
259 | * arbitrary memory anyway similar to bpf_probe_read_*() and might |
260 | * as well probe the stack. Thus, memory is explicitly cleared | |
261 | * only in error case, so that improper users ignoring return | |
262 | * code altogether don't copy garbage; otherwise length of string | |
263 | * is returned that can be used for bpf_perf_event_output() et al. | |
264 | */ | |
8d92db5c | 265 | ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size); |
6ae08ae3 | 266 | if (unlikely(ret < 0)) |
ff40e510 | 267 | memset(dst, 0, size); |
074f528e | 268 | return ret; |
2541517c AS |
269 | } |
270 | ||
6ae08ae3 DB |
271 | BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size, |
272 | const void *, unsafe_ptr) | |
273 | { | |
8d92db5c | 274 | return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr); |
6ae08ae3 DB |
275 | } |
276 | ||
f470378c | 277 | const struct bpf_func_proto bpf_probe_read_kernel_str_proto = { |
6ae08ae3 DB |
278 | .func = bpf_probe_read_kernel_str, |
279 | .gpl_only = true, | |
280 | .ret_type = RET_INTEGER, | |
281 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, | |
282 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, | |
283 | .arg3_type = ARG_ANYTHING, | |
284 | }; | |
285 | ||
8d92db5c CH |
286 | #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE |
287 | BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size, | |
288 | const void *, unsafe_ptr) | |
289 | { | |
290 | if ((unsigned long)unsafe_ptr < TASK_SIZE) { | |
291 | return bpf_probe_read_user_common(dst, size, | |
292 | (__force void __user *)unsafe_ptr); | |
293 | } | |
294 | return bpf_probe_read_kernel_common(dst, size, unsafe_ptr); | |
295 | } | |
296 | ||
297 | static const struct bpf_func_proto bpf_probe_read_compat_proto = { | |
298 | .func = bpf_probe_read_compat, | |
299 | .gpl_only = true, | |
300 | .ret_type = RET_INTEGER, | |
301 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, | |
302 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, | |
303 | .arg3_type = ARG_ANYTHING, | |
304 | }; | |
305 | ||
6ae08ae3 DB |
306 | BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size, |
307 | const void *, unsafe_ptr) | |
308 | { | |
8d92db5c CH |
309 | if ((unsigned long)unsafe_ptr < TASK_SIZE) { |
310 | return bpf_probe_read_user_str_common(dst, size, | |
311 | (__force void __user *)unsafe_ptr); | |
312 | } | |
313 | return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr); | |
6ae08ae3 DB |
314 | } |
315 | ||
316 | static const struct bpf_func_proto bpf_probe_read_compat_str_proto = { | |
317 | .func = bpf_probe_read_compat_str, | |
2541517c AS |
318 | .gpl_only = true, |
319 | .ret_type = RET_INTEGER, | |
39f19ebb | 320 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, |
9c019e2b | 321 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, |
2541517c AS |
322 | .arg3_type = ARG_ANYTHING, |
323 | }; | |
8d92db5c | 324 | #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */ |
2541517c | 325 | |
eb1b6688 | 326 | BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src, |
f3694e00 | 327 | u32, size) |
96ae5227 | 328 | { |
96ae5227 SD |
329 | /* |
330 | * Ensure we're in user context which is safe for the helper to | |
331 | * run. This helper has no business in a kthread. | |
332 | * | |
333 | * access_ok() should prevent writing to non-user memory, but in | |
334 | * some situations (nommu, temporary switch, etc) access_ok() does | |
335 | * not provide enough validation, hence the check on KERNEL_DS. | |
c7b6f29b NA |
336 | * |
337 | * nmi_uaccess_okay() ensures the probe is not run in an interim | |
338 | * state, when the task or mm are switched. This is specifically | |
339 | * required to prevent the use of temporary mm. | |
96ae5227 SD |
340 | */ |
341 | ||
342 | if (unlikely(in_interrupt() || | |
343 | current->flags & (PF_KTHREAD | PF_EXITING))) | |
344 | return -EPERM; | |
c7b6f29b NA |
345 | if (unlikely(!nmi_uaccess_okay())) |
346 | return -EPERM; | |
96ae5227 | 347 | |
c0ee37e8 | 348 | return copy_to_user_nofault(unsafe_ptr, src, size); |
96ae5227 SD |
349 | } |
350 | ||
351 | static const struct bpf_func_proto bpf_probe_write_user_proto = { | |
352 | .func = bpf_probe_write_user, | |
353 | .gpl_only = true, | |
354 | .ret_type = RET_INTEGER, | |
355 | .arg1_type = ARG_ANYTHING, | |
216e3cd2 | 356 | .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
39f19ebb | 357 | .arg3_type = ARG_CONST_SIZE, |
96ae5227 SD |
358 | }; |
359 | ||
360 | static const struct bpf_func_proto *bpf_get_probe_write_proto(void) | |
361 | { | |
2c78ee89 AS |
362 | if (!capable(CAP_SYS_ADMIN)) |
363 | return NULL; | |
364 | ||
96ae5227 SD |
365 | pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!", |
366 | current->comm, task_pid_nr(current)); | |
367 | ||
368 | return &bpf_probe_write_user_proto; | |
369 | } | |
370 | ||
d9c9e4db FR |
371 | #define MAX_TRACE_PRINTK_VARARGS 3 |
372 | #define BPF_TRACE_PRINTK_SIZE 1024 | |
ac5a72ea | 373 | |
d9c9e4db FR |
374 | BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1, |
375 | u64, arg2, u64, arg3) | |
ac5a72ea | 376 | { |
d9c9e4db | 377 | u64 args[MAX_TRACE_PRINTK_VARARGS] = { arg1, arg2, arg3 }; |
78aa1cc9 JO |
378 | struct bpf_bprintf_data data = { |
379 | .get_bin_args = true, | |
e2bb9e01 | 380 | .get_buf = true, |
78aa1cc9 | 381 | }; |
ac5a72ea AM |
382 | int ret; |
383 | ||
78aa1cc9 JO |
384 | ret = bpf_bprintf_prepare(fmt, fmt_size, args, |
385 | MAX_TRACE_PRINTK_VARARGS, &data); | |
d9c9e4db FR |
386 | if (ret < 0) |
387 | return ret; | |
388 | ||
e2bb9e01 | 389 | ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args); |
d9c9e4db | 390 | |
e2bb9e01 | 391 | trace_bpf_trace_printk(data.buf); |
ac5a72ea | 392 | |
f19a4050 | 393 | bpf_bprintf_cleanup(&data); |
9c959c86 | 394 | |
d9c9e4db | 395 | return ret; |
9c959c86 AS |
396 | } |
397 | ||
398 | static const struct bpf_func_proto bpf_trace_printk_proto = { | |
399 | .func = bpf_trace_printk, | |
400 | .gpl_only = true, | |
401 | .ret_type = RET_INTEGER, | |
216e3cd2 | 402 | .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
39f19ebb | 403 | .arg2_type = ARG_CONST_SIZE, |
9c959c86 AS |
404 | }; |
405 | ||
10aceb62 | 406 | static void __set_printk_clr_event(void) |
0756ea3e AS |
407 | { |
408 | /* | |
ac5a72ea AM |
409 | * This program might be calling bpf_trace_printk, |
410 | * so enable the associated bpf_trace/bpf_trace_printk event. | |
411 | * Repeat this each time as it is possible a user has | |
412 | * disabled bpf_trace_printk events. By loading a program | |
413 | * calling bpf_trace_printk() however the user has expressed | |
414 | * the intent to see such events. | |
0756ea3e | 415 | */ |
ac5a72ea AM |
416 | if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1)) |
417 | pr_warn_ratelimited("could not enable bpf_trace_printk events"); | |
10aceb62 | 418 | } |
0756ea3e | 419 | |
10aceb62 DM |
420 | const struct bpf_func_proto *bpf_get_trace_printk_proto(void) |
421 | { | |
422 | __set_printk_clr_event(); | |
0756ea3e AS |
423 | return &bpf_trace_printk_proto; |
424 | } | |
425 | ||
78aa1cc9 | 426 | BPF_CALL_4(bpf_trace_vprintk, char *, fmt, u32, fmt_size, const void *, args, |
10aceb62 DM |
427 | u32, data_len) |
428 | { | |
78aa1cc9 JO |
429 | struct bpf_bprintf_data data = { |
430 | .get_bin_args = true, | |
e2bb9e01 | 431 | .get_buf = true, |
78aa1cc9 | 432 | }; |
10aceb62 | 433 | int ret, num_args; |
10aceb62 DM |
434 | |
435 | if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 || | |
78aa1cc9 | 436 | (data_len && !args)) |
10aceb62 DM |
437 | return -EINVAL; |
438 | num_args = data_len / 8; | |
439 | ||
78aa1cc9 | 440 | ret = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data); |
10aceb62 DM |
441 | if (ret < 0) |
442 | return ret; | |
443 | ||
e2bb9e01 | 444 | ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args); |
10aceb62 | 445 | |
e2bb9e01 | 446 | trace_bpf_trace_printk(data.buf); |
10aceb62 | 447 | |
f19a4050 | 448 | bpf_bprintf_cleanup(&data); |
10aceb62 DM |
449 | |
450 | return ret; | |
451 | } | |
452 | ||
453 | static const struct bpf_func_proto bpf_trace_vprintk_proto = { | |
454 | .func = bpf_trace_vprintk, | |
455 | .gpl_only = true, | |
456 | .ret_type = RET_INTEGER, | |
216e3cd2 | 457 | .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
10aceb62 | 458 | .arg2_type = ARG_CONST_SIZE, |
216e3cd2 | 459 | .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY, |
10aceb62 DM |
460 | .arg4_type = ARG_CONST_SIZE_OR_ZERO, |
461 | }; | |
462 | ||
463 | const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void) | |
464 | { | |
465 | __set_printk_clr_event(); | |
466 | return &bpf_trace_vprintk_proto; | |
467 | } | |
468 | ||
492e639f | 469 | BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size, |
78aa1cc9 | 470 | const void *, args, u32, data_len) |
492e639f | 471 | { |
78aa1cc9 JO |
472 | struct bpf_bprintf_data data = { |
473 | .get_bin_args = true, | |
474 | }; | |
d9c9e4db | 475 | int err, num_args; |
492e639f | 476 | |
335ff499 | 477 | if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 || |
78aa1cc9 | 478 | (data_len && !args)) |
d9c9e4db | 479 | return -EINVAL; |
492e639f YS |
480 | num_args = data_len / 8; |
481 | ||
78aa1cc9 | 482 | err = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data); |
d9c9e4db FR |
483 | if (err < 0) |
484 | return err; | |
492e639f | 485 | |
78aa1cc9 | 486 | seq_bprintf(m, fmt, data.bin_args); |
48cac3f4 | 487 | |
f19a4050 | 488 | bpf_bprintf_cleanup(&data); |
d9c9e4db FR |
489 | |
490 | return seq_has_overflowed(m) ? -EOVERFLOW : 0; | |
492e639f YS |
491 | } |
492 | ||
9436ef6e | 493 | BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file) |
c9a0f3b8 | 494 | |
492e639f YS |
495 | static const struct bpf_func_proto bpf_seq_printf_proto = { |
496 | .func = bpf_seq_printf, | |
497 | .gpl_only = true, | |
498 | .ret_type = RET_INTEGER, | |
499 | .arg1_type = ARG_PTR_TO_BTF_ID, | |
9436ef6e | 500 | .arg1_btf_id = &btf_seq_file_ids[0], |
216e3cd2 | 501 | .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
492e639f | 502 | .arg3_type = ARG_CONST_SIZE, |
216e3cd2 | 503 | .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY, |
492e639f | 504 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
492e639f YS |
505 | }; |
506 | ||
507 | BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len) | |
508 | { | |
509 | return seq_write(m, data, len) ? -EOVERFLOW : 0; | |
510 | } | |
511 | ||
492e639f YS |
512 | static const struct bpf_func_proto bpf_seq_write_proto = { |
513 | .func = bpf_seq_write, | |
514 | .gpl_only = true, | |
515 | .ret_type = RET_INTEGER, | |
516 | .arg1_type = ARG_PTR_TO_BTF_ID, | |
9436ef6e | 517 | .arg1_btf_id = &btf_seq_file_ids[0], |
216e3cd2 | 518 | .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
492e639f | 519 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, |
492e639f YS |
520 | }; |
521 | ||
eb411377 AM |
522 | BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr, |
523 | u32, btf_ptr_size, u64, flags) | |
524 | { | |
525 | const struct btf *btf; | |
526 | s32 btf_id; | |
527 | int ret; | |
528 | ||
529 | ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id); | |
530 | if (ret) | |
531 | return ret; | |
532 | ||
533 | return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags); | |
534 | } | |
535 | ||
536 | static const struct bpf_func_proto bpf_seq_printf_btf_proto = { | |
537 | .func = bpf_seq_printf_btf, | |
538 | .gpl_only = true, | |
539 | .ret_type = RET_INTEGER, | |
540 | .arg1_type = ARG_PTR_TO_BTF_ID, | |
541 | .arg1_btf_id = &btf_seq_file_ids[0], | |
216e3cd2 | 542 | .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
492e639f | 543 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, |
eb411377 | 544 | .arg4_type = ARG_ANYTHING, |
492e639f YS |
545 | }; |
546 | ||
908432ca YS |
547 | static __always_inline int |
548 | get_map_perf_counter(struct bpf_map *map, u64 flags, | |
549 | u64 *value, u64 *enabled, u64 *running) | |
35578d79 | 550 | { |
35578d79 | 551 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
6816a7ff DB |
552 | unsigned int cpu = smp_processor_id(); |
553 | u64 index = flags & BPF_F_INDEX_MASK; | |
3b1efb19 | 554 | struct bpf_event_entry *ee; |
35578d79 | 555 | |
6816a7ff DB |
556 | if (unlikely(flags & ~(BPF_F_INDEX_MASK))) |
557 | return -EINVAL; | |
558 | if (index == BPF_F_CURRENT_CPU) | |
559 | index = cpu; | |
35578d79 KX |
560 | if (unlikely(index >= array->map.max_entries)) |
561 | return -E2BIG; | |
562 | ||
3b1efb19 | 563 | ee = READ_ONCE(array->ptrs[index]); |
1ca1cc98 | 564 | if (!ee) |
35578d79 KX |
565 | return -ENOENT; |
566 | ||
908432ca YS |
567 | return perf_event_read_local(ee->event, value, enabled, running); |
568 | } | |
569 | ||
570 | BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags) | |
571 | { | |
572 | u64 value = 0; | |
573 | int err; | |
574 | ||
575 | err = get_map_perf_counter(map, flags, &value, NULL, NULL); | |
35578d79 | 576 | /* |
f91840a3 AS |
577 | * this api is ugly since we miss [-22..-2] range of valid |
578 | * counter values, but that's uapi | |
35578d79 | 579 | */ |
f91840a3 AS |
580 | if (err) |
581 | return err; | |
582 | return value; | |
35578d79 KX |
583 | } |
584 | ||
62544ce8 | 585 | static const struct bpf_func_proto bpf_perf_event_read_proto = { |
35578d79 | 586 | .func = bpf_perf_event_read, |
1075ef59 | 587 | .gpl_only = true, |
35578d79 KX |
588 | .ret_type = RET_INTEGER, |
589 | .arg1_type = ARG_CONST_MAP_PTR, | |
590 | .arg2_type = ARG_ANYTHING, | |
591 | }; | |
592 | ||
908432ca YS |
593 | BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags, |
594 | struct bpf_perf_event_value *, buf, u32, size) | |
595 | { | |
596 | int err = -EINVAL; | |
597 | ||
598 | if (unlikely(size != sizeof(struct bpf_perf_event_value))) | |
599 | goto clear; | |
600 | err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled, | |
601 | &buf->running); | |
602 | if (unlikely(err)) | |
603 | goto clear; | |
604 | return 0; | |
605 | clear: | |
606 | memset(buf, 0, size); | |
607 | return err; | |
608 | } | |
609 | ||
610 | static const struct bpf_func_proto bpf_perf_event_read_value_proto = { | |
611 | .func = bpf_perf_event_read_value, | |
612 | .gpl_only = true, | |
613 | .ret_type = RET_INTEGER, | |
614 | .arg1_type = ARG_CONST_MAP_PTR, | |
615 | .arg2_type = ARG_ANYTHING, | |
616 | .arg3_type = ARG_PTR_TO_UNINIT_MEM, | |
617 | .arg4_type = ARG_CONST_SIZE, | |
618 | }; | |
619 | ||
8e7a3920 DB |
620 | static __always_inline u64 |
621 | __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map, | |
283ca526 | 622 | u64 flags, struct perf_sample_data *sd) |
a43eec30 | 623 | { |
a43eec30 | 624 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
d7931330 | 625 | unsigned int cpu = smp_processor_id(); |
1e33759c | 626 | u64 index = flags & BPF_F_INDEX_MASK; |
3b1efb19 | 627 | struct bpf_event_entry *ee; |
a43eec30 | 628 | struct perf_event *event; |
a43eec30 | 629 | |
1e33759c | 630 | if (index == BPF_F_CURRENT_CPU) |
d7931330 | 631 | index = cpu; |
a43eec30 AS |
632 | if (unlikely(index >= array->map.max_entries)) |
633 | return -E2BIG; | |
634 | ||
3b1efb19 | 635 | ee = READ_ONCE(array->ptrs[index]); |
1ca1cc98 | 636 | if (!ee) |
a43eec30 AS |
637 | return -ENOENT; |
638 | ||
3b1efb19 | 639 | event = ee->event; |
a43eec30 AS |
640 | if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE || |
641 | event->attr.config != PERF_COUNT_SW_BPF_OUTPUT)) | |
642 | return -EINVAL; | |
643 | ||
d7931330 | 644 | if (unlikely(event->oncpu != cpu)) |
a43eec30 AS |
645 | return -EOPNOTSUPP; |
646 | ||
56201969 | 647 | return perf_event_output(event, sd, regs); |
a43eec30 AS |
648 | } |
649 | ||
9594dc3c MM |
650 | /* |
651 | * Support executing tracepoints in normal, irq, and nmi context that each call | |
652 | * bpf_perf_event_output | |
653 | */ | |
654 | struct bpf_trace_sample_data { | |
655 | struct perf_sample_data sds[3]; | |
656 | }; | |
657 | ||
658 | static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds); | |
659 | static DEFINE_PER_CPU(int, bpf_trace_nest_level); | |
f3694e00 DB |
660 | BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map, |
661 | u64, flags, void *, data, u64, size) | |
8e7a3920 | 662 | { |
f2c67a3e | 663 | struct bpf_trace_sample_data *sds; |
8e7a3920 DB |
664 | struct perf_raw_record raw = { |
665 | .frag = { | |
666 | .size = size, | |
667 | .data = data, | |
668 | }, | |
669 | }; | |
9594dc3c | 670 | struct perf_sample_data *sd; |
f2c67a3e JO |
671 | int nest_level, err; |
672 | ||
673 | preempt_disable(); | |
674 | sds = this_cpu_ptr(&bpf_trace_sds); | |
675 | nest_level = this_cpu_inc_return(bpf_trace_nest_level); | |
8e7a3920 | 676 | |
9594dc3c MM |
677 | if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) { |
678 | err = -EBUSY; | |
679 | goto out; | |
680 | } | |
681 | ||
682 | sd = &sds->sds[nest_level - 1]; | |
683 | ||
684 | if (unlikely(flags & ~(BPF_F_INDEX_MASK))) { | |
685 | err = -EINVAL; | |
686 | goto out; | |
687 | } | |
8e7a3920 | 688 | |
283ca526 | 689 | perf_sample_data_init(sd, 0, 0); |
0a9081cf | 690 | perf_sample_save_raw_data(sd, &raw); |
283ca526 | 691 | |
9594dc3c | 692 | err = __bpf_perf_event_output(regs, map, flags, sd); |
9594dc3c MM |
693 | out: |
694 | this_cpu_dec(bpf_trace_nest_level); | |
f2c67a3e | 695 | preempt_enable(); |
9594dc3c | 696 | return err; |
8e7a3920 DB |
697 | } |
698 | ||
a43eec30 AS |
699 | static const struct bpf_func_proto bpf_perf_event_output_proto = { |
700 | .func = bpf_perf_event_output, | |
1075ef59 | 701 | .gpl_only = true, |
a43eec30 AS |
702 | .ret_type = RET_INTEGER, |
703 | .arg1_type = ARG_PTR_TO_CTX, | |
704 | .arg2_type = ARG_CONST_MAP_PTR, | |
705 | .arg3_type = ARG_ANYTHING, | |
216e3cd2 | 706 | .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
a60dd35d | 707 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
a43eec30 AS |
708 | }; |
709 | ||
768fb61f AZ |
710 | static DEFINE_PER_CPU(int, bpf_event_output_nest_level); |
711 | struct bpf_nested_pt_regs { | |
712 | struct pt_regs regs[3]; | |
713 | }; | |
714 | static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs); | |
715 | static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds); | |
bd570ff9 | 716 | |
555c8a86 DB |
717 | u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, |
718 | void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) | |
bd570ff9 | 719 | { |
555c8a86 DB |
720 | struct perf_raw_frag frag = { |
721 | .copy = ctx_copy, | |
722 | .size = ctx_size, | |
723 | .data = ctx, | |
724 | }; | |
725 | struct perf_raw_record raw = { | |
726 | .frag = { | |
183fc153 AM |
727 | { |
728 | .next = ctx_size ? &frag : NULL, | |
729 | }, | |
555c8a86 DB |
730 | .size = meta_size, |
731 | .data = meta, | |
732 | }, | |
733 | }; | |
768fb61f AZ |
734 | struct perf_sample_data *sd; |
735 | struct pt_regs *regs; | |
d62cc390 | 736 | int nest_level; |
768fb61f AZ |
737 | u64 ret; |
738 | ||
d62cc390 JO |
739 | preempt_disable(); |
740 | nest_level = this_cpu_inc_return(bpf_event_output_nest_level); | |
741 | ||
768fb61f AZ |
742 | if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) { |
743 | ret = -EBUSY; | |
744 | goto out; | |
745 | } | |
746 | sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]); | |
747 | regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]); | |
bd570ff9 DB |
748 | |
749 | perf_fetch_caller_regs(regs); | |
283ca526 | 750 | perf_sample_data_init(sd, 0, 0); |
0a9081cf | 751 | perf_sample_save_raw_data(sd, &raw); |
bd570ff9 | 752 | |
768fb61f AZ |
753 | ret = __bpf_perf_event_output(regs, map, flags, sd); |
754 | out: | |
755 | this_cpu_dec(bpf_event_output_nest_level); | |
d62cc390 | 756 | preempt_enable(); |
768fb61f | 757 | return ret; |
bd570ff9 DB |
758 | } |
759 | ||
f3694e00 | 760 | BPF_CALL_0(bpf_get_current_task) |
606274c5 AS |
761 | { |
762 | return (long) current; | |
763 | } | |
764 | ||
f470378c | 765 | const struct bpf_func_proto bpf_get_current_task_proto = { |
606274c5 AS |
766 | .func = bpf_get_current_task, |
767 | .gpl_only = true, | |
768 | .ret_type = RET_INTEGER, | |
769 | }; | |
770 | ||
3ca1032a KS |
771 | BPF_CALL_0(bpf_get_current_task_btf) |
772 | { | |
773 | return (unsigned long) current; | |
774 | } | |
775 | ||
a396eda5 | 776 | const struct bpf_func_proto bpf_get_current_task_btf_proto = { |
3ca1032a KS |
777 | .func = bpf_get_current_task_btf, |
778 | .gpl_only = true, | |
3f00c523 | 779 | .ret_type = RET_PTR_TO_BTF_ID_TRUSTED, |
d19ddb47 | 780 | .ret_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK], |
3ca1032a KS |
781 | }; |
782 | ||
dd6e10fb DX |
783 | BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task) |
784 | { | |
785 | return (unsigned long) task_pt_regs(task); | |
786 | } | |
787 | ||
788 | BTF_ID_LIST(bpf_task_pt_regs_ids) | |
789 | BTF_ID(struct, pt_regs) | |
790 | ||
791 | const struct bpf_func_proto bpf_task_pt_regs_proto = { | |
792 | .func = bpf_task_pt_regs, | |
793 | .gpl_only = true, | |
794 | .arg1_type = ARG_PTR_TO_BTF_ID, | |
d19ddb47 | 795 | .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK], |
dd6e10fb DX |
796 | .ret_type = RET_PTR_TO_BTF_ID, |
797 | .ret_btf_id = &bpf_task_pt_regs_ids[0], | |
798 | }; | |
799 | ||
8b401f9e YS |
800 | struct send_signal_irq_work { |
801 | struct irq_work irq_work; | |
802 | struct task_struct *task; | |
803 | u32 sig; | |
8482941f | 804 | enum pid_type type; |
8b401f9e YS |
805 | }; |
806 | ||
807 | static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work); | |
808 | ||
809 | static void do_bpf_send_signal(struct irq_work *entry) | |
810 | { | |
811 | struct send_signal_irq_work *work; | |
812 | ||
813 | work = container_of(entry, struct send_signal_irq_work, irq_work); | |
8482941f | 814 | group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type); |
bdb7fdb0 | 815 | put_task_struct(work->task); |
8b401f9e YS |
816 | } |
817 | ||
8482941f | 818 | static int bpf_send_signal_common(u32 sig, enum pid_type type) |
8b401f9e YS |
819 | { |
820 | struct send_signal_irq_work *work = NULL; | |
821 | ||
822 | /* Similar to bpf_probe_write_user, task needs to be | |
823 | * in a sound condition and kernel memory access be | |
824 | * permitted in order to send signal to the current | |
825 | * task. | |
826 | */ | |
827 | if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING))) | |
828 | return -EPERM; | |
8b401f9e YS |
829 | if (unlikely(!nmi_uaccess_okay())) |
830 | return -EPERM; | |
a3d81bc1 HS |
831 | /* Task should not be pid=1 to avoid kernel panic. */ |
832 | if (unlikely(is_global_init(current))) | |
833 | return -EPERM; | |
8b401f9e | 834 | |
1bc7896e | 835 | if (irqs_disabled()) { |
e1afb702 YS |
836 | /* Do an early check on signal validity. Otherwise, |
837 | * the error is lost in deferred irq_work. | |
838 | */ | |
839 | if (unlikely(!valid_signal(sig))) | |
840 | return -EINVAL; | |
841 | ||
8b401f9e | 842 | work = this_cpu_ptr(&send_signal_work); |
7a9f50a0 | 843 | if (irq_work_is_busy(&work->irq_work)) |
8b401f9e YS |
844 | return -EBUSY; |
845 | ||
846 | /* Add the current task, which is the target of sending signal, | |
847 | * to the irq_work. The current task may change when queued | |
848 | * irq works get executed. | |
849 | */ | |
bdb7fdb0 | 850 | work->task = get_task_struct(current); |
8b401f9e | 851 | work->sig = sig; |
8482941f | 852 | work->type = type; |
8b401f9e YS |
853 | irq_work_queue(&work->irq_work); |
854 | return 0; | |
855 | } | |
856 | ||
8482941f YS |
857 | return group_send_sig_info(sig, SEND_SIG_PRIV, current, type); |
858 | } | |
859 | ||
860 | BPF_CALL_1(bpf_send_signal, u32, sig) | |
861 | { | |
862 | return bpf_send_signal_common(sig, PIDTYPE_TGID); | |
8b401f9e YS |
863 | } |
864 | ||
865 | static const struct bpf_func_proto bpf_send_signal_proto = { | |
866 | .func = bpf_send_signal, | |
867 | .gpl_only = false, | |
868 | .ret_type = RET_INTEGER, | |
869 | .arg1_type = ARG_ANYTHING, | |
870 | }; | |
871 | ||
8482941f YS |
872 | BPF_CALL_1(bpf_send_signal_thread, u32, sig) |
873 | { | |
874 | return bpf_send_signal_common(sig, PIDTYPE_PID); | |
875 | } | |
876 | ||
877 | static const struct bpf_func_proto bpf_send_signal_thread_proto = { | |
878 | .func = bpf_send_signal_thread, | |
879 | .gpl_only = false, | |
880 | .ret_type = RET_INTEGER, | |
881 | .arg1_type = ARG_ANYTHING, | |
882 | }; | |
883 | ||
6e22ab9d JO |
884 | BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz) |
885 | { | |
f46fab0e | 886 | struct path copy; |
6e22ab9d JO |
887 | long len; |
888 | char *p; | |
889 | ||
890 | if (!sz) | |
891 | return 0; | |
892 | ||
f46fab0e JO |
893 | /* |
894 | * The path pointer is verified as trusted and safe to use, | |
895 | * but let's double check it's valid anyway to workaround | |
896 | * potentially broken verifier. | |
897 | */ | |
898 | len = copy_from_kernel_nofault(©, path, sizeof(*path)); | |
899 | if (len < 0) | |
900 | return len; | |
901 | ||
902 | p = d_path(©, buf, sz); | |
6e22ab9d JO |
903 | if (IS_ERR(p)) { |
904 | len = PTR_ERR(p); | |
905 | } else { | |
906 | len = buf + sz - p; | |
907 | memmove(buf, p, len); | |
908 | } | |
909 | ||
910 | return len; | |
911 | } | |
912 | ||
913 | BTF_SET_START(btf_allowlist_d_path) | |
a8a71796 JO |
914 | #ifdef CONFIG_SECURITY |
915 | BTF_ID(func, security_file_permission) | |
916 | BTF_ID(func, security_inode_getattr) | |
917 | BTF_ID(func, security_file_open) | |
918 | #endif | |
919 | #ifdef CONFIG_SECURITY_PATH | |
920 | BTF_ID(func, security_path_truncate) | |
921 | #endif | |
6e22ab9d JO |
922 | BTF_ID(func, vfs_truncate) |
923 | BTF_ID(func, vfs_fallocate) | |
924 | BTF_ID(func, dentry_open) | |
925 | BTF_ID(func, vfs_getattr) | |
926 | BTF_ID(func, filp_close) | |
927 | BTF_SET_END(btf_allowlist_d_path) | |
928 | ||
929 | static bool bpf_d_path_allowed(const struct bpf_prog *prog) | |
930 | { | |
3d06f34a SL |
931 | if (prog->type == BPF_PROG_TYPE_TRACING && |
932 | prog->expected_attach_type == BPF_TRACE_ITER) | |
933 | return true; | |
934 | ||
6f100640 KS |
935 | if (prog->type == BPF_PROG_TYPE_LSM) |
936 | return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id); | |
937 | ||
938 | return btf_id_set_contains(&btf_allowlist_d_path, | |
939 | prog->aux->attach_btf_id); | |
6e22ab9d JO |
940 | } |
941 | ||
9436ef6e | 942 | BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path) |
6e22ab9d JO |
943 | |
944 | static const struct bpf_func_proto bpf_d_path_proto = { | |
945 | .func = bpf_d_path, | |
946 | .gpl_only = false, | |
947 | .ret_type = RET_INTEGER, | |
948 | .arg1_type = ARG_PTR_TO_BTF_ID, | |
9436ef6e | 949 | .arg1_btf_id = &bpf_d_path_btf_ids[0], |
6e22ab9d JO |
950 | .arg2_type = ARG_PTR_TO_MEM, |
951 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, | |
6e22ab9d JO |
952 | .allowed = bpf_d_path_allowed, |
953 | }; | |
954 | ||
c4d0bfb4 AM |
955 | #define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \ |
956 | BTF_F_PTR_RAW | BTF_F_ZERO) | |
957 | ||
958 | static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size, | |
959 | u64 flags, const struct btf **btf, | |
960 | s32 *btf_id) | |
961 | { | |
962 | const struct btf_type *t; | |
963 | ||
964 | if (unlikely(flags & ~(BTF_F_ALL))) | |
965 | return -EINVAL; | |
966 | ||
967 | if (btf_ptr_size != sizeof(struct btf_ptr)) | |
968 | return -EINVAL; | |
969 | ||
970 | *btf = bpf_get_btf_vmlinux(); | |
971 | ||
972 | if (IS_ERR_OR_NULL(*btf)) | |
abbaa433 | 973 | return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL; |
c4d0bfb4 AM |
974 | |
975 | if (ptr->type_id > 0) | |
976 | *btf_id = ptr->type_id; | |
977 | else | |
978 | return -EINVAL; | |
979 | ||
980 | if (*btf_id > 0) | |
981 | t = btf_type_by_id(*btf, *btf_id); | |
982 | if (*btf_id <= 0 || !t) | |
983 | return -ENOENT; | |
984 | ||
985 | return 0; | |
986 | } | |
987 | ||
988 | BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr, | |
989 | u32, btf_ptr_size, u64, flags) | |
990 | { | |
991 | const struct btf *btf; | |
992 | s32 btf_id; | |
993 | int ret; | |
994 | ||
995 | ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id); | |
996 | if (ret) | |
997 | return ret; | |
998 | ||
999 | return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size, | |
1000 | flags); | |
1001 | } | |
1002 | ||
1003 | const struct bpf_func_proto bpf_snprintf_btf_proto = { | |
1004 | .func = bpf_snprintf_btf, | |
1005 | .gpl_only = false, | |
1006 | .ret_type = RET_INTEGER, | |
1007 | .arg1_type = ARG_PTR_TO_MEM, | |
1008 | .arg2_type = ARG_CONST_SIZE, | |
216e3cd2 | 1009 | .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
c4d0bfb4 AM |
1010 | .arg4_type = ARG_CONST_SIZE, |
1011 | .arg5_type = ARG_ANYTHING, | |
1012 | }; | |
1013 | ||
9b99edca JO |
1014 | BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx) |
1015 | { | |
1016 | /* This helper call is inlined by verifier. */ | |
f92c1e18 | 1017 | return ((u64 *)ctx)[-2]; |
9b99edca JO |
1018 | } |
1019 | ||
1020 | static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = { | |
1021 | .func = bpf_get_func_ip_tracing, | |
1022 | .gpl_only = true, | |
1023 | .ret_type = RET_INTEGER, | |
1024 | .arg1_type = ARG_PTR_TO_CTX, | |
1025 | }; | |
1026 | ||
c09eb2e5 JO |
1027 | #ifdef CONFIG_X86_KERNEL_IBT |
1028 | static unsigned long get_entry_ip(unsigned long fentry_ip) | |
1029 | { | |
1030 | u32 instr; | |
1031 | ||
a8497506 AN |
1032 | /* We want to be extra safe in case entry ip is on the page edge, |
1033 | * but otherwise we need to avoid get_kernel_nofault()'s overhead. | |
1034 | */ | |
1035 | if ((fentry_ip & ~PAGE_MASK) < ENDBR_INSN_SIZE) { | |
1036 | if (get_kernel_nofault(instr, (u32 *)(fentry_ip - ENDBR_INSN_SIZE))) | |
1037 | return fentry_ip; | |
1038 | } else { | |
1039 | instr = *(u32 *)(fentry_ip - ENDBR_INSN_SIZE); | |
1040 | } | |
c09eb2e5 JO |
1041 | if (is_endbr(instr)) |
1042 | fentry_ip -= ENDBR_INSN_SIZE; | |
1043 | return fentry_ip; | |
1044 | } | |
1045 | #else | |
1046 | #define get_entry_ip(fentry_ip) fentry_ip | |
1047 | #endif | |
1048 | ||
9ffd9f3f JO |
1049 | BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs) |
1050 | { | |
a3c485a5 JO |
1051 | struct bpf_trace_run_ctx *run_ctx __maybe_unused; |
1052 | struct kprobe *kp; | |
1053 | ||
1054 | #ifdef CONFIG_UPROBES | |
1055 | run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx); | |
1056 | if (run_ctx->is_uprobe) | |
1057 | return ((struct uprobe_dispatch_data *)current->utask->vaddr)->bp_addr; | |
1058 | #endif | |
1059 | ||
1060 | kp = kprobe_running(); | |
9ffd9f3f | 1061 | |
0e253f7e JO |
1062 | if (!kp || !(kp->flags & KPROBE_FLAG_ON_FUNC_ENTRY)) |
1063 | return 0; | |
1064 | ||
1065 | return get_entry_ip((uintptr_t)kp->addr); | |
9ffd9f3f JO |
1066 | } |
1067 | ||
1068 | static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = { | |
1069 | .func = bpf_get_func_ip_kprobe, | |
1070 | .gpl_only = true, | |
1071 | .ret_type = RET_INTEGER, | |
1072 | .arg1_type = ARG_PTR_TO_CTX, | |
1073 | }; | |
1074 | ||
42a57120 JO |
1075 | BPF_CALL_1(bpf_get_func_ip_kprobe_multi, struct pt_regs *, regs) |
1076 | { | |
f7098690 | 1077 | return bpf_kprobe_multi_entry_ip(current->bpf_ctx); |
42a57120 JO |
1078 | } |
1079 | ||
1080 | static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe_multi = { | |
1081 | .func = bpf_get_func_ip_kprobe_multi, | |
1082 | .gpl_only = false, | |
1083 | .ret_type = RET_INTEGER, | |
1084 | .arg1_type = ARG_PTR_TO_CTX, | |
1085 | }; | |
1086 | ||
ca74823c JO |
1087 | BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi, struct pt_regs *, regs) |
1088 | { | |
f7098690 | 1089 | return bpf_kprobe_multi_cookie(current->bpf_ctx); |
ca74823c JO |
1090 | } |
1091 | ||
1092 | static const struct bpf_func_proto bpf_get_attach_cookie_proto_kmulti = { | |
1093 | .func = bpf_get_attach_cookie_kprobe_multi, | |
1094 | .gpl_only = false, | |
1095 | .ret_type = RET_INTEGER, | |
1096 | .arg1_type = ARG_PTR_TO_CTX, | |
1097 | }; | |
1098 | ||
686328d8 JO |
1099 | BPF_CALL_1(bpf_get_func_ip_uprobe_multi, struct pt_regs *, regs) |
1100 | { | |
1101 | return bpf_uprobe_multi_entry_ip(current->bpf_ctx); | |
1102 | } | |
1103 | ||
1104 | static const struct bpf_func_proto bpf_get_func_ip_proto_uprobe_multi = { | |
1105 | .func = bpf_get_func_ip_uprobe_multi, | |
1106 | .gpl_only = false, | |
1107 | .ret_type = RET_INTEGER, | |
1108 | .arg1_type = ARG_PTR_TO_CTX, | |
1109 | }; | |
1110 | ||
0b779b61 JO |
1111 | BPF_CALL_1(bpf_get_attach_cookie_uprobe_multi, struct pt_regs *, regs) |
1112 | { | |
1113 | return bpf_uprobe_multi_cookie(current->bpf_ctx); | |
1114 | } | |
1115 | ||
1116 | static const struct bpf_func_proto bpf_get_attach_cookie_proto_umulti = { | |
1117 | .func = bpf_get_attach_cookie_uprobe_multi, | |
1118 | .gpl_only = false, | |
1119 | .ret_type = RET_INTEGER, | |
1120 | .arg1_type = ARG_PTR_TO_CTX, | |
1121 | }; | |
1122 | ||
7adfc6c9 AN |
1123 | BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx) |
1124 | { | |
1125 | struct bpf_trace_run_ctx *run_ctx; | |
1126 | ||
1127 | run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx); | |
1128 | return run_ctx->bpf_cookie; | |
1129 | } | |
1130 | ||
1131 | static const struct bpf_func_proto bpf_get_attach_cookie_proto_trace = { | |
1132 | .func = bpf_get_attach_cookie_trace, | |
1133 | .gpl_only = false, | |
1134 | .ret_type = RET_INTEGER, | |
1135 | .arg1_type = ARG_PTR_TO_CTX, | |
1136 | }; | |
1137 | ||
1138 | BPF_CALL_1(bpf_get_attach_cookie_pe, struct bpf_perf_event_data_kern *, ctx) | |
1139 | { | |
1140 | return ctx->event->bpf_cookie; | |
1141 | } | |
1142 | ||
1143 | static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = { | |
1144 | .func = bpf_get_attach_cookie_pe, | |
1145 | .gpl_only = false, | |
1146 | .ret_type = RET_INTEGER, | |
1147 | .arg1_type = ARG_PTR_TO_CTX, | |
1148 | }; | |
1149 | ||
2fcc8241 KFL |
1150 | BPF_CALL_1(bpf_get_attach_cookie_tracing, void *, ctx) |
1151 | { | |
1152 | struct bpf_trace_run_ctx *run_ctx; | |
1153 | ||
1154 | run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx); | |
1155 | return run_ctx->bpf_cookie; | |
1156 | } | |
1157 | ||
1158 | static const struct bpf_func_proto bpf_get_attach_cookie_proto_tracing = { | |
1159 | .func = bpf_get_attach_cookie_tracing, | |
1160 | .gpl_only = false, | |
1161 | .ret_type = RET_INTEGER, | |
1162 | .arg1_type = ARG_PTR_TO_CTX, | |
1163 | }; | |
1164 | ||
856c02db SL |
1165 | BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags) |
1166 | { | |
856c02db SL |
1167 | static const u32 br_entry_size = sizeof(struct perf_branch_entry); |
1168 | u32 entry_cnt = size / br_entry_size; | |
1169 | ||
1170 | entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt); | |
1171 | ||
1172 | if (unlikely(flags)) | |
1173 | return -EINVAL; | |
1174 | ||
1175 | if (!entry_cnt) | |
1176 | return -ENOENT; | |
1177 | ||
1178 | return entry_cnt * br_entry_size; | |
856c02db SL |
1179 | } |
1180 | ||
1181 | static const struct bpf_func_proto bpf_get_branch_snapshot_proto = { | |
1182 | .func = bpf_get_branch_snapshot, | |
1183 | .gpl_only = true, | |
1184 | .ret_type = RET_INTEGER, | |
1185 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, | |
1186 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, | |
1187 | }; | |
1188 | ||
f92c1e18 JO |
1189 | BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value) |
1190 | { | |
1191 | /* This helper call is inlined by verifier. */ | |
1192 | u64 nr_args = ((u64 *)ctx)[-1]; | |
1193 | ||
1194 | if ((u64) n >= nr_args) | |
1195 | return -EINVAL; | |
1196 | *value = ((u64 *)ctx)[n]; | |
1197 | return 0; | |
1198 | } | |
1199 | ||
1200 | static const struct bpf_func_proto bpf_get_func_arg_proto = { | |
1201 | .func = get_func_arg, | |
1202 | .ret_type = RET_INTEGER, | |
1203 | .arg1_type = ARG_PTR_TO_CTX, | |
1204 | .arg2_type = ARG_ANYTHING, | |
32556ce9 DB |
1205 | .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_ALIGNED, |
1206 | .arg3_size = sizeof(u64), | |
f92c1e18 JO |
1207 | }; |
1208 | ||
1209 | BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value) | |
1210 | { | |
1211 | /* This helper call is inlined by verifier. */ | |
1212 | u64 nr_args = ((u64 *)ctx)[-1]; | |
1213 | ||
1214 | *value = ((u64 *)ctx)[nr_args]; | |
1215 | return 0; | |
1216 | } | |
1217 | ||
1218 | static const struct bpf_func_proto bpf_get_func_ret_proto = { | |
1219 | .func = get_func_ret, | |
1220 | .ret_type = RET_INTEGER, | |
1221 | .arg1_type = ARG_PTR_TO_CTX, | |
32556ce9 DB |
1222 | .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_ALIGNED, |
1223 | .arg2_size = sizeof(u64), | |
f92c1e18 JO |
1224 | }; |
1225 | ||
1226 | BPF_CALL_1(get_func_arg_cnt, void *, ctx) | |
1227 | { | |
1228 | /* This helper call is inlined by verifier. */ | |
1229 | return ((u64 *)ctx)[-1]; | |
1230 | } | |
1231 | ||
1232 | static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = { | |
1233 | .func = get_func_arg_cnt, | |
1234 | .ret_type = RET_INTEGER, | |
1235 | .arg1_type = ARG_PTR_TO_CTX, | |
1236 | }; | |
1237 | ||
f3cf4134 | 1238 | #ifdef CONFIG_KEYS |
391145ba | 1239 | __bpf_kfunc_start_defs(); |
f3cf4134 RS |
1240 | |
1241 | /** | |
1242 | * bpf_lookup_user_key - lookup a key by its serial | |
1243 | * @serial: key handle serial number | |
1244 | * @flags: lookup-specific flags | |
1245 | * | |
1246 | * Search a key with a given *serial* and the provided *flags*. | |
1247 | * If found, increment the reference count of the key by one, and | |
1248 | * return it in the bpf_key structure. | |
1249 | * | |
1250 | * The bpf_key structure must be passed to bpf_key_put() when done | |
1251 | * with it, so that the key reference count is decremented and the | |
1252 | * bpf_key structure is freed. | |
1253 | * | |
1254 | * Permission checks are deferred to the time the key is used by | |
1255 | * one of the available key-specific kfuncs. | |
1256 | * | |
1257 | * Set *flags* with KEY_LOOKUP_CREATE, to attempt creating a requested | |
1258 | * special keyring (e.g. session keyring), if it doesn't yet exist. | |
1259 | * Set *flags* with KEY_LOOKUP_PARTIAL, to lookup a key without waiting | |
1260 | * for the key construction, and to retrieve uninstantiated keys (keys | |
1261 | * without data attached to them). | |
1262 | * | |
1263 | * Return: a bpf_key pointer with a valid key pointer if the key is found, a | |
1264 | * NULL pointer otherwise. | |
1265 | */ | |
400031e0 | 1266 | __bpf_kfunc struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags) |
f3cf4134 RS |
1267 | { |
1268 | key_ref_t key_ref; | |
1269 | struct bpf_key *bkey; | |
1270 | ||
1271 | if (flags & ~KEY_LOOKUP_ALL) | |
1272 | return NULL; | |
1273 | ||
1274 | /* | |
1275 | * Permission check is deferred until the key is used, as the | |
1276 | * intent of the caller is unknown here. | |
1277 | */ | |
1278 | key_ref = lookup_user_key(serial, flags, KEY_DEFER_PERM_CHECK); | |
1279 | if (IS_ERR(key_ref)) | |
1280 | return NULL; | |
1281 | ||
1282 | bkey = kmalloc(sizeof(*bkey), GFP_KERNEL); | |
1283 | if (!bkey) { | |
1284 | key_put(key_ref_to_ptr(key_ref)); | |
1285 | return NULL; | |
1286 | } | |
1287 | ||
1288 | bkey->key = key_ref_to_ptr(key_ref); | |
1289 | bkey->has_ref = true; | |
1290 | ||
1291 | return bkey; | |
1292 | } | |
1293 | ||
1294 | /** | |
1295 | * bpf_lookup_system_key - lookup a key by a system-defined ID | |
1296 | * @id: key ID | |
1297 | * | |
1298 | * Obtain a bpf_key structure with a key pointer set to the passed key ID. | |
1299 | * The key pointer is marked as invalid, to prevent bpf_key_put() from | |
1300 | * attempting to decrement the key reference count on that pointer. The key | |
1301 | * pointer set in such way is currently understood only by | |
1302 | * verify_pkcs7_signature(). | |
1303 | * | |
1304 | * Set *id* to one of the values defined in include/linux/verification.h: | |
1305 | * 0 for the primary keyring (immutable keyring of system keys); | |
1306 | * VERIFY_USE_SECONDARY_KEYRING for both the primary and secondary keyring | |
1307 | * (where keys can be added only if they are vouched for by existing keys | |
1308 | * in those keyrings); VERIFY_USE_PLATFORM_KEYRING for the platform | |
1309 | * keyring (primarily used by the integrity subsystem to verify a kexec'ed | |
1310 | * kerned image and, possibly, the initramfs signature). | |
1311 | * | |
1312 | * Return: a bpf_key pointer with an invalid key pointer set from the | |
1313 | * pre-determined ID on success, a NULL pointer otherwise | |
1314 | */ | |
400031e0 | 1315 | __bpf_kfunc struct bpf_key *bpf_lookup_system_key(u64 id) |
f3cf4134 RS |
1316 | { |
1317 | struct bpf_key *bkey; | |
1318 | ||
1319 | if (system_keyring_id_check(id) < 0) | |
1320 | return NULL; | |
1321 | ||
1322 | bkey = kmalloc(sizeof(*bkey), GFP_ATOMIC); | |
1323 | if (!bkey) | |
1324 | return NULL; | |
1325 | ||
1326 | bkey->key = (struct key *)(unsigned long)id; | |
1327 | bkey->has_ref = false; | |
1328 | ||
1329 | return bkey; | |
1330 | } | |
1331 | ||
1332 | /** | |
1333 | * bpf_key_put - decrement key reference count if key is valid and free bpf_key | |
1334 | * @bkey: bpf_key structure | |
1335 | * | |
1336 | * Decrement the reference count of the key inside *bkey*, if the pointer | |
1337 | * is valid, and free *bkey*. | |
1338 | */ | |
400031e0 | 1339 | __bpf_kfunc void bpf_key_put(struct bpf_key *bkey) |
f3cf4134 RS |
1340 | { |
1341 | if (bkey->has_ref) | |
1342 | key_put(bkey->key); | |
1343 | ||
1344 | kfree(bkey); | |
1345 | } | |
1346 | ||
865b0566 RS |
1347 | #ifdef CONFIG_SYSTEM_DATA_VERIFICATION |
1348 | /** | |
1349 | * bpf_verify_pkcs7_signature - verify a PKCS#7 signature | |
cce4c40b DX |
1350 | * @data_p: data to verify |
1351 | * @sig_p: signature of the data | |
865b0566 RS |
1352 | * @trusted_keyring: keyring with keys trusted for signature verification |
1353 | * | |
1354 | * Verify the PKCS#7 signature *sig_ptr* against the supplied *data_ptr* | |
1355 | * with keys in a keyring referenced by *trusted_keyring*. | |
1356 | * | |
1357 | * Return: 0 on success, a negative value on error. | |
1358 | */ | |
cce4c40b DX |
1359 | __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_p, |
1360 | struct bpf_dynptr *sig_p, | |
865b0566 RS |
1361 | struct bpf_key *trusted_keyring) |
1362 | { | |
cce4c40b DX |
1363 | struct bpf_dynptr_kern *data_ptr = (struct bpf_dynptr_kern *)data_p; |
1364 | struct bpf_dynptr_kern *sig_ptr = (struct bpf_dynptr_kern *)sig_p; | |
74523c06 SL |
1365 | const void *data, *sig; |
1366 | u32 data_len, sig_len; | |
865b0566 RS |
1367 | int ret; |
1368 | ||
1369 | if (trusted_keyring->has_ref) { | |
1370 | /* | |
1371 | * Do the permission check deferred in bpf_lookup_user_key(). | |
1372 | * See bpf_lookup_user_key() for more details. | |
1373 | * | |
1374 | * A call to key_task_permission() here would be redundant, as | |
1375 | * it is already done by keyring_search() called by | |
1376 | * find_asymmetric_key(). | |
1377 | */ | |
1378 | ret = key_validate(trusted_keyring->key); | |
1379 | if (ret < 0) | |
1380 | return ret; | |
1381 | } | |
1382 | ||
74523c06 SL |
1383 | data_len = __bpf_dynptr_size(data_ptr); |
1384 | data = __bpf_dynptr_data(data_ptr, data_len); | |
1385 | sig_len = __bpf_dynptr_size(sig_ptr); | |
1386 | sig = __bpf_dynptr_data(sig_ptr, sig_len); | |
1387 | ||
1388 | return verify_pkcs7_signature(data, data_len, sig, sig_len, | |
865b0566 RS |
1389 | trusted_keyring->key, |
1390 | VERIFYING_UNSPECIFIED_SIGNATURE, NULL, | |
1391 | NULL); | |
1392 | } | |
1393 | #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */ | |
1394 | ||
391145ba | 1395 | __bpf_kfunc_end_defs(); |
f3cf4134 | 1396 | |
6f3189f3 | 1397 | BTF_KFUNCS_START(key_sig_kfunc_set) |
f3cf4134 RS |
1398 | BTF_ID_FLAGS(func, bpf_lookup_user_key, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE) |
1399 | BTF_ID_FLAGS(func, bpf_lookup_system_key, KF_ACQUIRE | KF_RET_NULL) | |
1400 | BTF_ID_FLAGS(func, bpf_key_put, KF_RELEASE) | |
865b0566 RS |
1401 | #ifdef CONFIG_SYSTEM_DATA_VERIFICATION |
1402 | BTF_ID_FLAGS(func, bpf_verify_pkcs7_signature, KF_SLEEPABLE) | |
1403 | #endif | |
6f3189f3 | 1404 | BTF_KFUNCS_END(key_sig_kfunc_set) |
f3cf4134 RS |
1405 | |
1406 | static const struct btf_kfunc_id_set bpf_key_sig_kfunc_set = { | |
1407 | .owner = THIS_MODULE, | |
1408 | .set = &key_sig_kfunc_set, | |
1409 | }; | |
1410 | ||
1411 | static int __init bpf_key_sig_kfuncs_init(void) | |
1412 | { | |
1413 | return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, | |
1414 | &bpf_key_sig_kfunc_set); | |
1415 | } | |
1416 | ||
1417 | late_initcall(bpf_key_sig_kfuncs_init); | |
1418 | #endif /* CONFIG_KEYS */ | |
1419 | ||
7adfc6c9 | 1420 | static const struct bpf_func_proto * |
fc611f47 | 1421 | bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
2541517c AS |
1422 | { |
1423 | switch (func_id) { | |
1424 | case BPF_FUNC_map_lookup_elem: | |
1425 | return &bpf_map_lookup_elem_proto; | |
1426 | case BPF_FUNC_map_update_elem: | |
1427 | return &bpf_map_update_elem_proto; | |
1428 | case BPF_FUNC_map_delete_elem: | |
1429 | return &bpf_map_delete_elem_proto; | |
02a8c817 AC |
1430 | case BPF_FUNC_map_push_elem: |
1431 | return &bpf_map_push_elem_proto; | |
1432 | case BPF_FUNC_map_pop_elem: | |
1433 | return &bpf_map_pop_elem_proto; | |
1434 | case BPF_FUNC_map_peek_elem: | |
1435 | return &bpf_map_peek_elem_proto; | |
07343110 FZ |
1436 | case BPF_FUNC_map_lookup_percpu_elem: |
1437 | return &bpf_map_lookup_percpu_elem_proto; | |
d9847d31 AS |
1438 | case BPF_FUNC_ktime_get_ns: |
1439 | return &bpf_ktime_get_ns_proto; | |
71d19214 MÅ» |
1440 | case BPF_FUNC_ktime_get_boot_ns: |
1441 | return &bpf_ktime_get_boot_ns_proto; | |
04fd61ab AS |
1442 | case BPF_FUNC_tail_call: |
1443 | return &bpf_tail_call_proto; | |
606274c5 AS |
1444 | case BPF_FUNC_get_current_task: |
1445 | return &bpf_get_current_task_proto; | |
3ca1032a KS |
1446 | case BPF_FUNC_get_current_task_btf: |
1447 | return &bpf_get_current_task_btf_proto; | |
dd6e10fb DX |
1448 | case BPF_FUNC_task_pt_regs: |
1449 | return &bpf_task_pt_regs_proto; | |
ffeedafb AS |
1450 | case BPF_FUNC_get_current_uid_gid: |
1451 | return &bpf_get_current_uid_gid_proto; | |
1452 | case BPF_FUNC_get_current_comm: | |
1453 | return &bpf_get_current_comm_proto; | |
9c959c86 | 1454 | case BPF_FUNC_trace_printk: |
0756ea3e | 1455 | return bpf_get_trace_printk_proto(); |
ab1973d3 AS |
1456 | case BPF_FUNC_get_smp_processor_id: |
1457 | return &bpf_get_smp_processor_id_proto; | |
2d0e30c3 DB |
1458 | case BPF_FUNC_get_numa_node_id: |
1459 | return &bpf_get_numa_node_id_proto; | |
35578d79 KX |
1460 | case BPF_FUNC_perf_event_read: |
1461 | return &bpf_perf_event_read_proto; | |
8937bd80 AS |
1462 | case BPF_FUNC_get_prandom_u32: |
1463 | return &bpf_get_prandom_u32_proto; | |
51e1bb9e DB |
1464 | case BPF_FUNC_probe_write_user: |
1465 | return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ? | |
1466 | NULL : bpf_get_probe_write_proto(); | |
6ae08ae3 DB |
1467 | case BPF_FUNC_probe_read_user: |
1468 | return &bpf_probe_read_user_proto; | |
1469 | case BPF_FUNC_probe_read_kernel: | |
71330842 | 1470 | return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? |
ff40e510 | 1471 | NULL : &bpf_probe_read_kernel_proto; |
6ae08ae3 DB |
1472 | case BPF_FUNC_probe_read_user_str: |
1473 | return &bpf_probe_read_user_str_proto; | |
1474 | case BPF_FUNC_probe_read_kernel_str: | |
71330842 | 1475 | return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? |
ff40e510 | 1476 | NULL : &bpf_probe_read_kernel_str_proto; |
0ebeea8c DB |
1477 | #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE |
1478 | case BPF_FUNC_probe_read: | |
71330842 | 1479 | return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? |
ff40e510 | 1480 | NULL : &bpf_probe_read_compat_proto; |
a5e8c070 | 1481 | case BPF_FUNC_probe_read_str: |
71330842 | 1482 | return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? |
ff40e510 | 1483 | NULL : &bpf_probe_read_compat_str_proto; |
0ebeea8c | 1484 | #endif |
34ea38ca | 1485 | #ifdef CONFIG_CGROUPS |
c4bcfb38 YS |
1486 | case BPF_FUNC_cgrp_storage_get: |
1487 | return &bpf_cgrp_storage_get_proto; | |
1488 | case BPF_FUNC_cgrp_storage_delete: | |
1489 | return &bpf_cgrp_storage_delete_proto; | |
7f628741 MC |
1490 | case BPF_FUNC_current_task_under_cgroup: |
1491 | return &bpf_current_task_under_cgroup_proto; | |
34ea38ca | 1492 | #endif |
8b401f9e YS |
1493 | case BPF_FUNC_send_signal: |
1494 | return &bpf_send_signal_proto; | |
8482941f YS |
1495 | case BPF_FUNC_send_signal_thread: |
1496 | return &bpf_send_signal_thread_proto; | |
b80b033b SL |
1497 | case BPF_FUNC_perf_event_read_value: |
1498 | return &bpf_perf_event_read_value_proto; | |
457f4436 AN |
1499 | case BPF_FUNC_ringbuf_output: |
1500 | return &bpf_ringbuf_output_proto; | |
1501 | case BPF_FUNC_ringbuf_reserve: | |
1502 | return &bpf_ringbuf_reserve_proto; | |
1503 | case BPF_FUNC_ringbuf_submit: | |
1504 | return &bpf_ringbuf_submit_proto; | |
1505 | case BPF_FUNC_ringbuf_discard: | |
1506 | return &bpf_ringbuf_discard_proto; | |
1507 | case BPF_FUNC_ringbuf_query: | |
1508 | return &bpf_ringbuf_query_proto; | |
72e2b2b6 YS |
1509 | case BPF_FUNC_jiffies64: |
1510 | return &bpf_jiffies64_proto; | |
fa28dcb8 | 1511 | case BPF_FUNC_get_task_stack: |
d4dd9775 AN |
1512 | return prog->sleepable ? &bpf_get_task_stack_sleepable_proto |
1513 | : &bpf_get_task_stack_proto; | |
07be4c4a | 1514 | case BPF_FUNC_copy_from_user: |
01685c5b | 1515 | return &bpf_copy_from_user_proto; |
376040e4 | 1516 | case BPF_FUNC_copy_from_user_task: |
01685c5b | 1517 | return &bpf_copy_from_user_task_proto; |
c4d0bfb4 AM |
1518 | case BPF_FUNC_snprintf_btf: |
1519 | return &bpf_snprintf_btf_proto; | |
b7906b70 | 1520 | case BPF_FUNC_per_cpu_ptr: |
eaa6bcb7 | 1521 | return &bpf_per_cpu_ptr_proto; |
b7906b70 | 1522 | case BPF_FUNC_this_cpu_ptr: |
63d9b80d | 1523 | return &bpf_this_cpu_ptr_proto; |
a10787e6 | 1524 | case BPF_FUNC_task_storage_get: |
4279adb0 MKL |
1525 | if (bpf_prog_check_recur(prog)) |
1526 | return &bpf_task_storage_get_recur_proto; | |
a10787e6 SL |
1527 | return &bpf_task_storage_get_proto; |
1528 | case BPF_FUNC_task_storage_delete: | |
8a7dac37 MKL |
1529 | if (bpf_prog_check_recur(prog)) |
1530 | return &bpf_task_storage_delete_recur_proto; | |
a10787e6 | 1531 | return &bpf_task_storage_delete_proto; |
69c087ba YS |
1532 | case BPF_FUNC_for_each_map_elem: |
1533 | return &bpf_for_each_map_elem_proto; | |
7b15523a FR |
1534 | case BPF_FUNC_snprintf: |
1535 | return &bpf_snprintf_proto; | |
9b99edca JO |
1536 | case BPF_FUNC_get_func_ip: |
1537 | return &bpf_get_func_ip_proto_tracing; | |
856c02db SL |
1538 | case BPF_FUNC_get_branch_snapshot: |
1539 | return &bpf_get_branch_snapshot_proto; | |
7c7e3d31 SL |
1540 | case BPF_FUNC_find_vma: |
1541 | return &bpf_find_vma_proto; | |
10aceb62 DM |
1542 | case BPF_FUNC_trace_vprintk: |
1543 | return bpf_get_trace_vprintk_proto(); | |
9fd82b61 | 1544 | default: |
bbc1d247 | 1545 | return bpf_base_func_proto(func_id, prog); |
9fd82b61 AS |
1546 | } |
1547 | } | |
1548 | ||
535a3692 JO |
1549 | static bool is_kprobe_multi(const struct bpf_prog *prog) |
1550 | { | |
1551 | return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI || | |
1552 | prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION; | |
1553 | } | |
1554 | ||
1555 | static inline bool is_kprobe_session(const struct bpf_prog *prog) | |
1556 | { | |
1557 | return prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION; | |
1558 | } | |
1559 | ||
5e43f899 AI |
1560 | static const struct bpf_func_proto * |
1561 | kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
9fd82b61 AS |
1562 | { |
1563 | switch (func_id) { | |
a43eec30 AS |
1564 | case BPF_FUNC_perf_event_output: |
1565 | return &bpf_perf_event_output_proto; | |
d5a3b1f6 AS |
1566 | case BPF_FUNC_get_stackid: |
1567 | return &bpf_get_stackid_proto; | |
c195651e | 1568 | case BPF_FUNC_get_stack: |
d4dd9775 | 1569 | return prog->sleepable ? &bpf_get_stack_sleepable_proto : &bpf_get_stack_proto; |
9802d865 JB |
1570 | #ifdef CONFIG_BPF_KPROBE_OVERRIDE |
1571 | case BPF_FUNC_override_return: | |
1572 | return &bpf_override_return_proto; | |
1573 | #endif | |
9ffd9f3f | 1574 | case BPF_FUNC_get_func_ip: |
535a3692 | 1575 | if (is_kprobe_multi(prog)) |
686328d8 JO |
1576 | return &bpf_get_func_ip_proto_kprobe_multi; |
1577 | if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI) | |
1578 | return &bpf_get_func_ip_proto_uprobe_multi; | |
1579 | return &bpf_get_func_ip_proto_kprobe; | |
7adfc6c9 | 1580 | case BPF_FUNC_get_attach_cookie: |
535a3692 | 1581 | if (is_kprobe_multi(prog)) |
0b779b61 JO |
1582 | return &bpf_get_attach_cookie_proto_kmulti; |
1583 | if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI) | |
1584 | return &bpf_get_attach_cookie_proto_umulti; | |
1585 | return &bpf_get_attach_cookie_proto_trace; | |
2541517c | 1586 | default: |
fc611f47 | 1587 | return bpf_tracing_func_proto(func_id, prog); |
2541517c AS |
1588 | } |
1589 | } | |
1590 | ||
1591 | /* bpf+kprobe programs can access fields of 'struct pt_regs' */ | |
19de99f7 | 1592 | static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type, |
5e43f899 | 1593 | const struct bpf_prog *prog, |
23994631 | 1594 | struct bpf_insn_access_aux *info) |
2541517c | 1595 | { |
2541517c AS |
1596 | if (off < 0 || off >= sizeof(struct pt_regs)) |
1597 | return false; | |
2541517c AS |
1598 | if (type != BPF_READ) |
1599 | return false; | |
2541517c AS |
1600 | if (off % size != 0) |
1601 | return false; | |
2d071c64 DB |
1602 | /* |
1603 | * Assertion for 32 bit to make sure last 8 byte access | |
1604 | * (BPF_DW) to the last 4 byte member is disallowed. | |
1605 | */ | |
1606 | if (off + size > sizeof(struct pt_regs)) | |
1607 | return false; | |
1608 | ||
2541517c AS |
1609 | return true; |
1610 | } | |
1611 | ||
7de16e3a | 1612 | const struct bpf_verifier_ops kprobe_verifier_ops = { |
2541517c AS |
1613 | .get_func_proto = kprobe_prog_func_proto, |
1614 | .is_valid_access = kprobe_prog_is_valid_access, | |
1615 | }; | |
1616 | ||
7de16e3a JK |
1617 | const struct bpf_prog_ops kprobe_prog_ops = { |
1618 | }; | |
1619 | ||
f3694e00 DB |
1620 | BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map, |
1621 | u64, flags, void *, data, u64, size) | |
9940d67c | 1622 | { |
f3694e00 DB |
1623 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
1624 | ||
9940d67c AS |
1625 | /* |
1626 | * r1 points to perf tracepoint buffer where first 8 bytes are hidden | |
1627 | * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it | |
f3694e00 | 1628 | * from there and call the same bpf_perf_event_output() helper inline. |
9940d67c | 1629 | */ |
f3694e00 | 1630 | return ____bpf_perf_event_output(regs, map, flags, data, size); |
9940d67c AS |
1631 | } |
1632 | ||
1633 | static const struct bpf_func_proto bpf_perf_event_output_proto_tp = { | |
1634 | .func = bpf_perf_event_output_tp, | |
1635 | .gpl_only = true, | |
1636 | .ret_type = RET_INTEGER, | |
1637 | .arg1_type = ARG_PTR_TO_CTX, | |
1638 | .arg2_type = ARG_CONST_MAP_PTR, | |
1639 | .arg3_type = ARG_ANYTHING, | |
216e3cd2 | 1640 | .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
a60dd35d | 1641 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
9940d67c AS |
1642 | }; |
1643 | ||
f3694e00 DB |
1644 | BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map, |
1645 | u64, flags) | |
9940d67c | 1646 | { |
f3694e00 | 1647 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
9940d67c | 1648 | |
f3694e00 DB |
1649 | /* |
1650 | * Same comment as in bpf_perf_event_output_tp(), only that this time | |
1651 | * the other helper's function body cannot be inlined due to being | |
1652 | * external, thus we need to call raw helper function. | |
1653 | */ | |
1654 | return bpf_get_stackid((unsigned long) regs, (unsigned long) map, | |
1655 | flags, 0, 0); | |
9940d67c AS |
1656 | } |
1657 | ||
1658 | static const struct bpf_func_proto bpf_get_stackid_proto_tp = { | |
1659 | .func = bpf_get_stackid_tp, | |
1660 | .gpl_only = true, | |
1661 | .ret_type = RET_INTEGER, | |
1662 | .arg1_type = ARG_PTR_TO_CTX, | |
1663 | .arg2_type = ARG_CONST_MAP_PTR, | |
1664 | .arg3_type = ARG_ANYTHING, | |
1665 | }; | |
1666 | ||
c195651e YS |
1667 | BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size, |
1668 | u64, flags) | |
1669 | { | |
1670 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; | |
1671 | ||
1672 | return bpf_get_stack((unsigned long) regs, (unsigned long) buf, | |
1673 | (unsigned long) size, flags, 0); | |
1674 | } | |
1675 | ||
1676 | static const struct bpf_func_proto bpf_get_stack_proto_tp = { | |
1677 | .func = bpf_get_stack_tp, | |
1678 | .gpl_only = true, | |
1679 | .ret_type = RET_INTEGER, | |
1680 | .arg1_type = ARG_PTR_TO_CTX, | |
1681 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, | |
1682 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, | |
1683 | .arg4_type = ARG_ANYTHING, | |
1684 | }; | |
1685 | ||
5e43f899 AI |
1686 | static const struct bpf_func_proto * |
1687 | tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
f005afed YS |
1688 | { |
1689 | switch (func_id) { | |
1690 | case BPF_FUNC_perf_event_output: | |
1691 | return &bpf_perf_event_output_proto_tp; | |
1692 | case BPF_FUNC_get_stackid: | |
1693 | return &bpf_get_stackid_proto_tp; | |
c195651e YS |
1694 | case BPF_FUNC_get_stack: |
1695 | return &bpf_get_stack_proto_tp; | |
7adfc6c9 AN |
1696 | case BPF_FUNC_get_attach_cookie: |
1697 | return &bpf_get_attach_cookie_proto_trace; | |
f005afed | 1698 | default: |
fc611f47 | 1699 | return bpf_tracing_func_proto(func_id, prog); |
f005afed YS |
1700 | } |
1701 | } | |
1702 | ||
1703 | static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type, | |
5e43f899 | 1704 | const struct bpf_prog *prog, |
f005afed YS |
1705 | struct bpf_insn_access_aux *info) |
1706 | { | |
1707 | if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE) | |
1708 | return false; | |
1709 | if (type != BPF_READ) | |
1710 | return false; | |
1711 | if (off % size != 0) | |
1712 | return false; | |
1713 | ||
1714 | BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64)); | |
1715 | return true; | |
1716 | } | |
1717 | ||
1718 | const struct bpf_verifier_ops tracepoint_verifier_ops = { | |
1719 | .get_func_proto = tp_prog_func_proto, | |
1720 | .is_valid_access = tp_prog_is_valid_access, | |
1721 | }; | |
1722 | ||
1723 | const struct bpf_prog_ops tracepoint_prog_ops = { | |
1724 | }; | |
1725 | ||
1726 | BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx, | |
4bebdc7a YS |
1727 | struct bpf_perf_event_value *, buf, u32, size) |
1728 | { | |
1729 | int err = -EINVAL; | |
1730 | ||
1731 | if (unlikely(size != sizeof(struct bpf_perf_event_value))) | |
1732 | goto clear; | |
1733 | err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled, | |
1734 | &buf->running); | |
1735 | if (unlikely(err)) | |
1736 | goto clear; | |
1737 | return 0; | |
1738 | clear: | |
1739 | memset(buf, 0, size); | |
1740 | return err; | |
1741 | } | |
1742 | ||
f005afed YS |
1743 | static const struct bpf_func_proto bpf_perf_prog_read_value_proto = { |
1744 | .func = bpf_perf_prog_read_value, | |
4bebdc7a YS |
1745 | .gpl_only = true, |
1746 | .ret_type = RET_INTEGER, | |
1747 | .arg1_type = ARG_PTR_TO_CTX, | |
1748 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, | |
1749 | .arg3_type = ARG_CONST_SIZE, | |
1750 | }; | |
1751 | ||
fff7b643 DX |
1752 | BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx, |
1753 | void *, buf, u32, size, u64, flags) | |
1754 | { | |
fff7b643 DX |
1755 | static const u32 br_entry_size = sizeof(struct perf_branch_entry); |
1756 | struct perf_branch_stack *br_stack = ctx->data->br_stack; | |
1757 | u32 to_copy; | |
1758 | ||
1759 | if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE)) | |
1760 | return -EINVAL; | |
1761 | ||
cce6a2d7 JO |
1762 | if (unlikely(!(ctx->data->sample_flags & PERF_SAMPLE_BRANCH_STACK))) |
1763 | return -ENOENT; | |
1764 | ||
fff7b643 | 1765 | if (unlikely(!br_stack)) |
db52f572 | 1766 | return -ENOENT; |
fff7b643 DX |
1767 | |
1768 | if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE) | |
1769 | return br_stack->nr * br_entry_size; | |
1770 | ||
1771 | if (!buf || (size % br_entry_size != 0)) | |
1772 | return -EINVAL; | |
1773 | ||
1774 | to_copy = min_t(u32, br_stack->nr * br_entry_size, size); | |
1775 | memcpy(buf, br_stack->entries, to_copy); | |
1776 | ||
1777 | return to_copy; | |
fff7b643 DX |
1778 | } |
1779 | ||
1780 | static const struct bpf_func_proto bpf_read_branch_records_proto = { | |
1781 | .func = bpf_read_branch_records, | |
1782 | .gpl_only = true, | |
1783 | .ret_type = RET_INTEGER, | |
1784 | .arg1_type = ARG_PTR_TO_CTX, | |
1785 | .arg2_type = ARG_PTR_TO_MEM_OR_NULL, | |
1786 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, | |
1787 | .arg4_type = ARG_ANYTHING, | |
1788 | }; | |
1789 | ||
5e43f899 AI |
1790 | static const struct bpf_func_proto * |
1791 | pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
9fd82b61 AS |
1792 | { |
1793 | switch (func_id) { | |
1794 | case BPF_FUNC_perf_event_output: | |
9940d67c | 1795 | return &bpf_perf_event_output_proto_tp; |
9fd82b61 | 1796 | case BPF_FUNC_get_stackid: |
7b04d6d6 | 1797 | return &bpf_get_stackid_proto_pe; |
c195651e | 1798 | case BPF_FUNC_get_stack: |
7b04d6d6 | 1799 | return &bpf_get_stack_proto_pe; |
4bebdc7a | 1800 | case BPF_FUNC_perf_prog_read_value: |
f005afed | 1801 | return &bpf_perf_prog_read_value_proto; |
fff7b643 DX |
1802 | case BPF_FUNC_read_branch_records: |
1803 | return &bpf_read_branch_records_proto; | |
7adfc6c9 AN |
1804 | case BPF_FUNC_get_attach_cookie: |
1805 | return &bpf_get_attach_cookie_proto_pe; | |
9fd82b61 | 1806 | default: |
fc611f47 | 1807 | return bpf_tracing_func_proto(func_id, prog); |
9fd82b61 AS |
1808 | } |
1809 | } | |
1810 | ||
c4f6699d AS |
1811 | /* |
1812 | * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp | |
1813 | * to avoid potential recursive reuse issue when/if tracepoints are added | |
9594dc3c MM |
1814 | * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack. |
1815 | * | |
1816 | * Since raw tracepoints run despite bpf_prog_active, support concurrent usage | |
1817 | * in normal, irq, and nmi context. | |
c4f6699d | 1818 | */ |
9594dc3c MM |
1819 | struct bpf_raw_tp_regs { |
1820 | struct pt_regs regs[3]; | |
1821 | }; | |
1822 | static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs); | |
1823 | static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level); | |
1824 | static struct pt_regs *get_bpf_raw_tp_regs(void) | |
1825 | { | |
1826 | struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs); | |
1827 | int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level); | |
1828 | ||
1829 | if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) { | |
1830 | this_cpu_dec(bpf_raw_tp_nest_level); | |
1831 | return ERR_PTR(-EBUSY); | |
1832 | } | |
1833 | ||
1834 | return &tp_regs->regs[nest_level - 1]; | |
1835 | } | |
1836 | ||
1837 | static void put_bpf_raw_tp_regs(void) | |
1838 | { | |
1839 | this_cpu_dec(bpf_raw_tp_nest_level); | |
1840 | } | |
1841 | ||
c4f6699d AS |
1842 | BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args, |
1843 | struct bpf_map *, map, u64, flags, void *, data, u64, size) | |
1844 | { | |
9594dc3c MM |
1845 | struct pt_regs *regs = get_bpf_raw_tp_regs(); |
1846 | int ret; | |
1847 | ||
1848 | if (IS_ERR(regs)) | |
1849 | return PTR_ERR(regs); | |
c4f6699d AS |
1850 | |
1851 | perf_fetch_caller_regs(regs); | |
9594dc3c MM |
1852 | ret = ____bpf_perf_event_output(regs, map, flags, data, size); |
1853 | ||
1854 | put_bpf_raw_tp_regs(); | |
1855 | return ret; | |
c4f6699d AS |
1856 | } |
1857 | ||
1858 | static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = { | |
1859 | .func = bpf_perf_event_output_raw_tp, | |
1860 | .gpl_only = true, | |
1861 | .ret_type = RET_INTEGER, | |
1862 | .arg1_type = ARG_PTR_TO_CTX, | |
1863 | .arg2_type = ARG_CONST_MAP_PTR, | |
1864 | .arg3_type = ARG_ANYTHING, | |
216e3cd2 | 1865 | .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
c4f6699d AS |
1866 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
1867 | }; | |
1868 | ||
a7658e1a | 1869 | extern const struct bpf_func_proto bpf_skb_output_proto; |
d831ee84 | 1870 | extern const struct bpf_func_proto bpf_xdp_output_proto; |
d9917302 | 1871 | extern const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto; |
a7658e1a | 1872 | |
c4f6699d AS |
1873 | BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args, |
1874 | struct bpf_map *, map, u64, flags) | |
1875 | { | |
9594dc3c MM |
1876 | struct pt_regs *regs = get_bpf_raw_tp_regs(); |
1877 | int ret; | |
1878 | ||
1879 | if (IS_ERR(regs)) | |
1880 | return PTR_ERR(regs); | |
c4f6699d AS |
1881 | |
1882 | perf_fetch_caller_regs(regs); | |
1883 | /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */ | |
9594dc3c MM |
1884 | ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map, |
1885 | flags, 0, 0); | |
1886 | put_bpf_raw_tp_regs(); | |
1887 | return ret; | |
c4f6699d AS |
1888 | } |
1889 | ||
1890 | static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = { | |
1891 | .func = bpf_get_stackid_raw_tp, | |
1892 | .gpl_only = true, | |
1893 | .ret_type = RET_INTEGER, | |
1894 | .arg1_type = ARG_PTR_TO_CTX, | |
1895 | .arg2_type = ARG_CONST_MAP_PTR, | |
1896 | .arg3_type = ARG_ANYTHING, | |
1897 | }; | |
1898 | ||
c195651e YS |
1899 | BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args, |
1900 | void *, buf, u32, size, u64, flags) | |
1901 | { | |
9594dc3c MM |
1902 | struct pt_regs *regs = get_bpf_raw_tp_regs(); |
1903 | int ret; | |
1904 | ||
1905 | if (IS_ERR(regs)) | |
1906 | return PTR_ERR(regs); | |
c195651e YS |
1907 | |
1908 | perf_fetch_caller_regs(regs); | |
9594dc3c MM |
1909 | ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf, |
1910 | (unsigned long) size, flags, 0); | |
1911 | put_bpf_raw_tp_regs(); | |
1912 | return ret; | |
c195651e YS |
1913 | } |
1914 | ||
1915 | static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { | |
1916 | .func = bpf_get_stack_raw_tp, | |
1917 | .gpl_only = true, | |
1918 | .ret_type = RET_INTEGER, | |
1919 | .arg1_type = ARG_PTR_TO_CTX, | |
216e3cd2 | 1920 | .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
c195651e YS |
1921 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, |
1922 | .arg4_type = ARG_ANYTHING, | |
1923 | }; | |
1924 | ||
5e43f899 AI |
1925 | static const struct bpf_func_proto * |
1926 | raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
c4f6699d AS |
1927 | { |
1928 | switch (func_id) { | |
1929 | case BPF_FUNC_perf_event_output: | |
1930 | return &bpf_perf_event_output_proto_raw_tp; | |
1931 | case BPF_FUNC_get_stackid: | |
1932 | return &bpf_get_stackid_proto_raw_tp; | |
c195651e YS |
1933 | case BPF_FUNC_get_stack: |
1934 | return &bpf_get_stack_proto_raw_tp; | |
68ca5d4e AN |
1935 | case BPF_FUNC_get_attach_cookie: |
1936 | return &bpf_get_attach_cookie_proto_tracing; | |
c4f6699d | 1937 | default: |
fc611f47 | 1938 | return bpf_tracing_func_proto(func_id, prog); |
c4f6699d AS |
1939 | } |
1940 | } | |
1941 | ||
958a3f2d | 1942 | const struct bpf_func_proto * |
f1b9509c AS |
1943 | tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
1944 | { | |
3cee6fb8 MKL |
1945 | const struct bpf_func_proto *fn; |
1946 | ||
f1b9509c AS |
1947 | switch (func_id) { |
1948 | #ifdef CONFIG_NET | |
1949 | case BPF_FUNC_skb_output: | |
1950 | return &bpf_skb_output_proto; | |
d831ee84 EC |
1951 | case BPF_FUNC_xdp_output: |
1952 | return &bpf_xdp_output_proto; | |
af7ec138 YS |
1953 | case BPF_FUNC_skc_to_tcp6_sock: |
1954 | return &bpf_skc_to_tcp6_sock_proto; | |
478cfbdf YS |
1955 | case BPF_FUNC_skc_to_tcp_sock: |
1956 | return &bpf_skc_to_tcp_sock_proto; | |
1957 | case BPF_FUNC_skc_to_tcp_timewait_sock: | |
1958 | return &bpf_skc_to_tcp_timewait_sock_proto; | |
1959 | case BPF_FUNC_skc_to_tcp_request_sock: | |
1960 | return &bpf_skc_to_tcp_request_sock_proto; | |
0d4fad3e YS |
1961 | case BPF_FUNC_skc_to_udp6_sock: |
1962 | return &bpf_skc_to_udp6_sock_proto; | |
9eeb3aa3 HC |
1963 | case BPF_FUNC_skc_to_unix_sock: |
1964 | return &bpf_skc_to_unix_sock_proto; | |
3bc253c2 GT |
1965 | case BPF_FUNC_skc_to_mptcp_sock: |
1966 | return &bpf_skc_to_mptcp_sock_proto; | |
8e4597c6 MKL |
1967 | case BPF_FUNC_sk_storage_get: |
1968 | return &bpf_sk_storage_get_tracing_proto; | |
1969 | case BPF_FUNC_sk_storage_delete: | |
1970 | return &bpf_sk_storage_delete_tracing_proto; | |
b60da495 FR |
1971 | case BPF_FUNC_sock_from_file: |
1972 | return &bpf_sock_from_file_proto; | |
c5dbb89f FR |
1973 | case BPF_FUNC_get_socket_cookie: |
1974 | return &bpf_get_socket_ptr_cookie_proto; | |
d9917302 EC |
1975 | case BPF_FUNC_xdp_get_buff_len: |
1976 | return &bpf_xdp_get_buff_len_trace_proto; | |
f1b9509c | 1977 | #endif |
492e639f YS |
1978 | case BPF_FUNC_seq_printf: |
1979 | return prog->expected_attach_type == BPF_TRACE_ITER ? | |
1980 | &bpf_seq_printf_proto : | |
1981 | NULL; | |
1982 | case BPF_FUNC_seq_write: | |
1983 | return prog->expected_attach_type == BPF_TRACE_ITER ? | |
1984 | &bpf_seq_write_proto : | |
1985 | NULL; | |
eb411377 AM |
1986 | case BPF_FUNC_seq_printf_btf: |
1987 | return prog->expected_attach_type == BPF_TRACE_ITER ? | |
1988 | &bpf_seq_printf_btf_proto : | |
1989 | NULL; | |
6e22ab9d JO |
1990 | case BPF_FUNC_d_path: |
1991 | return &bpf_d_path_proto; | |
f92c1e18 JO |
1992 | case BPF_FUNC_get_func_arg: |
1993 | return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_proto : NULL; | |
1994 | case BPF_FUNC_get_func_ret: | |
1995 | return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL; | |
1996 | case BPF_FUNC_get_func_arg_cnt: | |
1997 | return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL; | |
2fcc8241 | 1998 | case BPF_FUNC_get_attach_cookie: |
68ca5d4e AN |
1999 | if (prog->type == BPF_PROG_TYPE_TRACING && |
2000 | prog->expected_attach_type == BPF_TRACE_RAW_TP) | |
2001 | return &bpf_get_attach_cookie_proto_tracing; | |
2fcc8241 | 2002 | return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto_tracing : NULL; |
f1b9509c | 2003 | default: |
3cee6fb8 MKL |
2004 | fn = raw_tp_prog_func_proto(func_id, prog); |
2005 | if (!fn && prog->expected_attach_type == BPF_TRACE_ITER) | |
2006 | fn = bpf_iter_get_func_proto(func_id, prog); | |
2007 | return fn; | |
f1b9509c AS |
2008 | } |
2009 | } | |
2010 | ||
c4f6699d AS |
2011 | static bool raw_tp_prog_is_valid_access(int off, int size, |
2012 | enum bpf_access_type type, | |
5e43f899 | 2013 | const struct bpf_prog *prog, |
c4f6699d AS |
2014 | struct bpf_insn_access_aux *info) |
2015 | { | |
35346ab6 | 2016 | return bpf_tracing_ctx_access(off, size, type); |
f1b9509c AS |
2017 | } |
2018 | ||
2019 | static bool tracing_prog_is_valid_access(int off, int size, | |
2020 | enum bpf_access_type type, | |
2021 | const struct bpf_prog *prog, | |
2022 | struct bpf_insn_access_aux *info) | |
2023 | { | |
35346ab6 | 2024 | return bpf_tracing_btf_ctx_access(off, size, type, prog, info); |
c4f6699d AS |
2025 | } |
2026 | ||
3e7c67d9 KS |
2027 | int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog, |
2028 | const union bpf_attr *kattr, | |
2029 | union bpf_attr __user *uattr) | |
2030 | { | |
2031 | return -ENOTSUPP; | |
2032 | } | |
2033 | ||
c4f6699d AS |
2034 | const struct bpf_verifier_ops raw_tracepoint_verifier_ops = { |
2035 | .get_func_proto = raw_tp_prog_func_proto, | |
2036 | .is_valid_access = raw_tp_prog_is_valid_access, | |
2037 | }; | |
2038 | ||
2039 | const struct bpf_prog_ops raw_tracepoint_prog_ops = { | |
ebfb4d40 | 2040 | #ifdef CONFIG_NET |
1b4d60ec | 2041 | .test_run = bpf_prog_test_run_raw_tp, |
ebfb4d40 | 2042 | #endif |
c4f6699d AS |
2043 | }; |
2044 | ||
f1b9509c AS |
2045 | const struct bpf_verifier_ops tracing_verifier_ops = { |
2046 | .get_func_proto = tracing_prog_func_proto, | |
2047 | .is_valid_access = tracing_prog_is_valid_access, | |
2048 | }; | |
2049 | ||
2050 | const struct bpf_prog_ops tracing_prog_ops = { | |
da00d2f1 | 2051 | .test_run = bpf_prog_test_run_tracing, |
f1b9509c AS |
2052 | }; |
2053 | ||
9df1c28b MM |
2054 | static bool raw_tp_writable_prog_is_valid_access(int off, int size, |
2055 | enum bpf_access_type type, | |
2056 | const struct bpf_prog *prog, | |
2057 | struct bpf_insn_access_aux *info) | |
2058 | { | |
2059 | if (off == 0) { | |
2060 | if (size != sizeof(u64) || type != BPF_READ) | |
2061 | return false; | |
2062 | info->reg_type = PTR_TO_TP_BUFFER; | |
2063 | } | |
2064 | return raw_tp_prog_is_valid_access(off, size, type, prog, info); | |
2065 | } | |
2066 | ||
2067 | const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = { | |
2068 | .get_func_proto = raw_tp_prog_func_proto, | |
2069 | .is_valid_access = raw_tp_writable_prog_is_valid_access, | |
2070 | }; | |
2071 | ||
2072 | const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = { | |
2073 | }; | |
2074 | ||
0515e599 | 2075 | static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type, |
5e43f899 | 2076 | const struct bpf_prog *prog, |
23994631 | 2077 | struct bpf_insn_access_aux *info) |
0515e599 | 2078 | { |
95da0cdb | 2079 | const int size_u64 = sizeof(u64); |
31fd8581 | 2080 | |
0515e599 AS |
2081 | if (off < 0 || off >= sizeof(struct bpf_perf_event_data)) |
2082 | return false; | |
2083 | if (type != BPF_READ) | |
2084 | return false; | |
bc23105c DB |
2085 | if (off % size != 0) { |
2086 | if (sizeof(unsigned long) != 4) | |
2087 | return false; | |
2088 | if (size != 8) | |
2089 | return false; | |
2090 | if (off % size != 4) | |
2091 | return false; | |
2092 | } | |
31fd8581 | 2093 | |
f96da094 DB |
2094 | switch (off) { |
2095 | case bpf_ctx_range(struct bpf_perf_event_data, sample_period): | |
95da0cdb TQ |
2096 | bpf_ctx_record_field_size(info, size_u64); |
2097 | if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) | |
2098 | return false; | |
2099 | break; | |
2100 | case bpf_ctx_range(struct bpf_perf_event_data, addr): | |
2101 | bpf_ctx_record_field_size(info, size_u64); | |
2102 | if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) | |
23994631 | 2103 | return false; |
f96da094 DB |
2104 | break; |
2105 | default: | |
0515e599 AS |
2106 | if (size != sizeof(long)) |
2107 | return false; | |
2108 | } | |
f96da094 | 2109 | |
0515e599 AS |
2110 | return true; |
2111 | } | |
2112 | ||
6b8cc1d1 DB |
2113 | static u32 pe_prog_convert_ctx_access(enum bpf_access_type type, |
2114 | const struct bpf_insn *si, | |
0515e599 | 2115 | struct bpf_insn *insn_buf, |
f96da094 | 2116 | struct bpf_prog *prog, u32 *target_size) |
0515e599 AS |
2117 | { |
2118 | struct bpf_insn *insn = insn_buf; | |
2119 | ||
6b8cc1d1 | 2120 | switch (si->off) { |
0515e599 | 2121 | case offsetof(struct bpf_perf_event_data, sample_period): |
f035a515 | 2122 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, |
6b8cc1d1 | 2123 | data), si->dst_reg, si->src_reg, |
0515e599 | 2124 | offsetof(struct bpf_perf_event_data_kern, data)); |
6b8cc1d1 | 2125 | *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, |
f96da094 DB |
2126 | bpf_target_off(struct perf_sample_data, period, 8, |
2127 | target_size)); | |
0515e599 | 2128 | break; |
95da0cdb TQ |
2129 | case offsetof(struct bpf_perf_event_data, addr): |
2130 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, | |
2131 | data), si->dst_reg, si->src_reg, | |
2132 | offsetof(struct bpf_perf_event_data_kern, data)); | |
2133 | *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, | |
2134 | bpf_target_off(struct perf_sample_data, addr, 8, | |
2135 | target_size)); | |
2136 | break; | |
0515e599 | 2137 | default: |
f035a515 | 2138 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, |
6b8cc1d1 | 2139 | regs), si->dst_reg, si->src_reg, |
0515e599 | 2140 | offsetof(struct bpf_perf_event_data_kern, regs)); |
6b8cc1d1 DB |
2141 | *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg, |
2142 | si->off); | |
0515e599 AS |
2143 | break; |
2144 | } | |
2145 | ||
2146 | return insn - insn_buf; | |
2147 | } | |
2148 | ||
7de16e3a | 2149 | const struct bpf_verifier_ops perf_event_verifier_ops = { |
f005afed | 2150 | .get_func_proto = pe_prog_func_proto, |
0515e599 AS |
2151 | .is_valid_access = pe_prog_is_valid_access, |
2152 | .convert_ctx_access = pe_prog_convert_ctx_access, | |
2153 | }; | |
7de16e3a JK |
2154 | |
2155 | const struct bpf_prog_ops perf_event_prog_ops = { | |
2156 | }; | |
e87c6bc3 YS |
2157 | |
2158 | static DEFINE_MUTEX(bpf_event_mutex); | |
2159 | ||
c8c088ba YS |
2160 | #define BPF_TRACE_MAX_PROGS 64 |
2161 | ||
e87c6bc3 | 2162 | int perf_event_attach_bpf_prog(struct perf_event *event, |
82e6b1ee AN |
2163 | struct bpf_prog *prog, |
2164 | u64 bpf_cookie) | |
e87c6bc3 | 2165 | { |
e672db03 | 2166 | struct bpf_prog_array *old_array; |
e87c6bc3 YS |
2167 | struct bpf_prog_array *new_array; |
2168 | int ret = -EEXIST; | |
2169 | ||
9802d865 | 2170 | /* |
b4da3340 MH |
2171 | * Kprobe override only works if they are on the function entry, |
2172 | * and only if they are on the opt-in list. | |
9802d865 JB |
2173 | */ |
2174 | if (prog->kprobe_override && | |
b4da3340 | 2175 | (!trace_kprobe_on_func_entry(event->tp_event) || |
9802d865 JB |
2176 | !trace_kprobe_error_injectable(event->tp_event))) |
2177 | return -EINVAL; | |
2178 | ||
e87c6bc3 YS |
2179 | mutex_lock(&bpf_event_mutex); |
2180 | ||
2181 | if (event->prog) | |
07c41a29 | 2182 | goto unlock; |
e87c6bc3 | 2183 | |
e672db03 | 2184 | old_array = bpf_event_rcu_dereference(event->tp_event->prog_array); |
c8c088ba YS |
2185 | if (old_array && |
2186 | bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) { | |
2187 | ret = -E2BIG; | |
2188 | goto unlock; | |
2189 | } | |
2190 | ||
82e6b1ee | 2191 | ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array); |
e87c6bc3 | 2192 | if (ret < 0) |
07c41a29 | 2193 | goto unlock; |
e87c6bc3 YS |
2194 | |
2195 | /* set the new array to event->tp_event and set event->prog */ | |
2196 | event->prog = prog; | |
82e6b1ee | 2197 | event->bpf_cookie = bpf_cookie; |
e87c6bc3 | 2198 | rcu_assign_pointer(event->tp_event->prog_array, new_array); |
8c7dcb84 | 2199 | bpf_prog_array_free_sleepable(old_array); |
e87c6bc3 | 2200 | |
07c41a29 | 2201 | unlock: |
e87c6bc3 YS |
2202 | mutex_unlock(&bpf_event_mutex); |
2203 | return ret; | |
2204 | } | |
2205 | ||
2206 | void perf_event_detach_bpf_prog(struct perf_event *event) | |
2207 | { | |
e672db03 | 2208 | struct bpf_prog_array *old_array; |
e87c6bc3 YS |
2209 | struct bpf_prog_array *new_array; |
2210 | int ret; | |
2211 | ||
2212 | mutex_lock(&bpf_event_mutex); | |
2213 | ||
2214 | if (!event->prog) | |
07c41a29 | 2215 | goto unlock; |
e87c6bc3 | 2216 | |
e672db03 | 2217 | old_array = bpf_event_rcu_dereference(event->tp_event->prog_array); |
82e6b1ee | 2218 | ret = bpf_prog_array_copy(old_array, event->prog, NULL, 0, &new_array); |
170a7e3e SY |
2219 | if (ret == -ENOENT) |
2220 | goto unlock; | |
e87c6bc3 YS |
2221 | if (ret < 0) { |
2222 | bpf_prog_array_delete_safe(old_array, event->prog); | |
2223 | } else { | |
2224 | rcu_assign_pointer(event->tp_event->prog_array, new_array); | |
8c7dcb84 | 2225 | bpf_prog_array_free_sleepable(old_array); |
e87c6bc3 YS |
2226 | } |
2227 | ||
2228 | bpf_prog_put(event->prog); | |
2229 | event->prog = NULL; | |
2230 | ||
07c41a29 | 2231 | unlock: |
e87c6bc3 YS |
2232 | mutex_unlock(&bpf_event_mutex); |
2233 | } | |
f371b304 | 2234 | |
f4e2298e | 2235 | int perf_event_query_prog_array(struct perf_event *event, void __user *info) |
f371b304 YS |
2236 | { |
2237 | struct perf_event_query_bpf __user *uquery = info; | |
2238 | struct perf_event_query_bpf query = {}; | |
e672db03 | 2239 | struct bpf_prog_array *progs; |
3a38bb98 | 2240 | u32 *ids, prog_cnt, ids_len; |
f371b304 YS |
2241 | int ret; |
2242 | ||
031258da | 2243 | if (!perfmon_capable()) |
f371b304 YS |
2244 | return -EPERM; |
2245 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
2246 | return -EINVAL; | |
2247 | if (copy_from_user(&query, uquery, sizeof(query))) | |
2248 | return -EFAULT; | |
3a38bb98 YS |
2249 | |
2250 | ids_len = query.ids_len; | |
2251 | if (ids_len > BPF_TRACE_MAX_PROGS) | |
9c481b90 | 2252 | return -E2BIG; |
3a38bb98 YS |
2253 | ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN); |
2254 | if (!ids) | |
2255 | return -ENOMEM; | |
2256 | /* | |
2257 | * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which | |
2258 | * is required when user only wants to check for uquery->prog_cnt. | |
2259 | * There is no need to check for it since the case is handled | |
2260 | * gracefully in bpf_prog_array_copy_info. | |
2261 | */ | |
f371b304 YS |
2262 | |
2263 | mutex_lock(&bpf_event_mutex); | |
e672db03 SF |
2264 | progs = bpf_event_rcu_dereference(event->tp_event->prog_array); |
2265 | ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt); | |
f371b304 YS |
2266 | mutex_unlock(&bpf_event_mutex); |
2267 | ||
3a38bb98 YS |
2268 | if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) || |
2269 | copy_to_user(uquery->ids, ids, ids_len * sizeof(u32))) | |
2270 | ret = -EFAULT; | |
2271 | ||
2272 | kfree(ids); | |
f371b304 YS |
2273 | return ret; |
2274 | } | |
c4f6699d AS |
2275 | |
2276 | extern struct bpf_raw_event_map __start__bpf_raw_tp[]; | |
2277 | extern struct bpf_raw_event_map __stop__bpf_raw_tp[]; | |
2278 | ||
a38d1107 | 2279 | struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name) |
c4f6699d AS |
2280 | { |
2281 | struct bpf_raw_event_map *btp = __start__bpf_raw_tp; | |
2282 | ||
2283 | for (; btp < __stop__bpf_raw_tp; btp++) { | |
2284 | if (!strcmp(btp->tp->name, name)) | |
2285 | return btp; | |
2286 | } | |
a38d1107 MM |
2287 | |
2288 | return bpf_get_raw_tracepoint_module(name); | |
2289 | } | |
2290 | ||
2291 | void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp) | |
2292 | { | |
12cc126d | 2293 | struct module *mod; |
a38d1107 | 2294 | |
12cc126d AN |
2295 | preempt_disable(); |
2296 | mod = __module_address((unsigned long)btp); | |
2297 | module_put(mod); | |
2298 | preempt_enable(); | |
c4f6699d AS |
2299 | } |
2300 | ||
2301 | static __always_inline | |
d4dfc570 | 2302 | void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) |
c4f6699d | 2303 | { |
d4dfc570 | 2304 | struct bpf_prog *prog = link->link.prog; |
68ca5d4e AN |
2305 | struct bpf_run_ctx *old_run_ctx; |
2306 | struct bpf_trace_run_ctx run_ctx; | |
d4dfc570 | 2307 | |
f03efe49 | 2308 | cant_sleep(); |
05b24ff9 JO |
2309 | if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) { |
2310 | bpf_prog_inc_misses_counter(prog); | |
2311 | goto out; | |
2312 | } | |
68ca5d4e AN |
2313 | |
2314 | run_ctx.bpf_cookie = link->cookie; | |
2315 | old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); | |
2316 | ||
c4f6699d | 2317 | rcu_read_lock(); |
fb7dd8bc | 2318 | (void) bpf_prog_run(prog, args); |
c4f6699d | 2319 | rcu_read_unlock(); |
68ca5d4e AN |
2320 | |
2321 | bpf_reset_run_ctx(old_run_ctx); | |
05b24ff9 JO |
2322 | out: |
2323 | this_cpu_dec(*(prog->active)); | |
c4f6699d AS |
2324 | } |
2325 | ||
2326 | #define UNPACK(...) __VA_ARGS__ | |
2327 | #define REPEAT_1(FN, DL, X, ...) FN(X) | |
2328 | #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__) | |
2329 | #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__) | |
2330 | #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__) | |
2331 | #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__) | |
2332 | #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__) | |
2333 | #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__) | |
2334 | #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__) | |
2335 | #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__) | |
2336 | #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__) | |
2337 | #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__) | |
2338 | #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__) | |
2339 | #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__) | |
2340 | ||
2341 | #define SARG(X) u64 arg##X | |
2342 | #define COPY(X) args[X] = arg##X | |
2343 | ||
2344 | #define __DL_COM (,) | |
2345 | #define __DL_SEM (;) | |
2346 | ||
2347 | #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 | |
2348 | ||
2349 | #define BPF_TRACE_DEFN_x(x) \ | |
d4dfc570 | 2350 | void bpf_trace_run##x(struct bpf_raw_tp_link *link, \ |
c4f6699d AS |
2351 | REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \ |
2352 | { \ | |
2353 | u64 args[x]; \ | |
2354 | REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \ | |
d4dfc570 | 2355 | __bpf_trace_run(link, args); \ |
c4f6699d AS |
2356 | } \ |
2357 | EXPORT_SYMBOL_GPL(bpf_trace_run##x) | |
2358 | BPF_TRACE_DEFN_x(1); | |
2359 | BPF_TRACE_DEFN_x(2); | |
2360 | BPF_TRACE_DEFN_x(3); | |
2361 | BPF_TRACE_DEFN_x(4); | |
2362 | BPF_TRACE_DEFN_x(5); | |
2363 | BPF_TRACE_DEFN_x(6); | |
2364 | BPF_TRACE_DEFN_x(7); | |
2365 | BPF_TRACE_DEFN_x(8); | |
2366 | BPF_TRACE_DEFN_x(9); | |
2367 | BPF_TRACE_DEFN_x(10); | |
2368 | BPF_TRACE_DEFN_x(11); | |
2369 | BPF_TRACE_DEFN_x(12); | |
2370 | ||
d4dfc570 | 2371 | int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link) |
c4f6699d AS |
2372 | { |
2373 | struct tracepoint *tp = btp->tp; | |
d4dfc570 | 2374 | struct bpf_prog *prog = link->link.prog; |
c4f6699d AS |
2375 | |
2376 | /* | |
2377 | * check that program doesn't access arguments beyond what's | |
2378 | * available in this tracepoint | |
2379 | */ | |
2380 | if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64)) | |
2381 | return -EINVAL; | |
2382 | ||
9df1c28b MM |
2383 | if (prog->aux->max_tp_access > btp->writable_size) |
2384 | return -EINVAL; | |
2385 | ||
d4dfc570 | 2386 | return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func, link); |
c4f6699d AS |
2387 | } |
2388 | ||
d4dfc570 | 2389 | int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link) |
c4f6699d | 2390 | { |
d4dfc570 | 2391 | return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, link); |
c4f6699d | 2392 | } |
41bdc4b4 YS |
2393 | |
2394 | int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id, | |
2395 | u32 *fd_type, const char **buf, | |
3acf8ace JO |
2396 | u64 *probe_offset, u64 *probe_addr, |
2397 | unsigned long *missed) | |
41bdc4b4 YS |
2398 | { |
2399 | bool is_tracepoint, is_syscall_tp; | |
2400 | struct bpf_prog *prog; | |
2401 | int flags, err = 0; | |
2402 | ||
2403 | prog = event->prog; | |
2404 | if (!prog) | |
2405 | return -ENOENT; | |
2406 | ||
2407 | /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */ | |
2408 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) | |
2409 | return -EOPNOTSUPP; | |
2410 | ||
2411 | *prog_id = prog->aux->id; | |
2412 | flags = event->tp_event->flags; | |
2413 | is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT; | |
2414 | is_syscall_tp = is_syscall_trace_event(event->tp_event); | |
2415 | ||
2416 | if (is_tracepoint || is_syscall_tp) { | |
2417 | *buf = is_tracepoint ? event->tp_event->tp->name | |
2418 | : event->tp_event->name; | |
1b715e1b YS |
2419 | /* We allow NULL pointer for tracepoint */ |
2420 | if (fd_type) | |
2421 | *fd_type = BPF_FD_TYPE_TRACEPOINT; | |
2422 | if (probe_offset) | |
2423 | *probe_offset = 0x0; | |
2424 | if (probe_addr) | |
2425 | *probe_addr = 0x0; | |
41bdc4b4 YS |
2426 | } else { |
2427 | /* kprobe/uprobe */ | |
2428 | err = -EOPNOTSUPP; | |
2429 | #ifdef CONFIG_KPROBE_EVENTS | |
2430 | if (flags & TRACE_EVENT_FL_KPROBE) | |
2431 | err = bpf_get_kprobe_info(event, fd_type, buf, | |
3acf8ace | 2432 | probe_offset, probe_addr, missed, |
41bdc4b4 YS |
2433 | event->attr.type == PERF_TYPE_TRACEPOINT); |
2434 | #endif | |
2435 | #ifdef CONFIG_UPROBE_EVENTS | |
2436 | if (flags & TRACE_EVENT_FL_UPROBE) | |
2437 | err = bpf_get_uprobe_info(event, fd_type, buf, | |
5125e757 | 2438 | probe_offset, probe_addr, |
41bdc4b4 YS |
2439 | event->attr.type == PERF_TYPE_TRACEPOINT); |
2440 | #endif | |
2441 | } | |
2442 | ||
2443 | return err; | |
2444 | } | |
a38d1107 | 2445 | |
9db1ff0a YS |
2446 | static int __init send_signal_irq_work_init(void) |
2447 | { | |
2448 | int cpu; | |
2449 | struct send_signal_irq_work *work; | |
2450 | ||
2451 | for_each_possible_cpu(cpu) { | |
2452 | work = per_cpu_ptr(&send_signal_work, cpu); | |
2453 | init_irq_work(&work->irq_work, do_bpf_send_signal); | |
2454 | } | |
2455 | return 0; | |
2456 | } | |
2457 | ||
2458 | subsys_initcall(send_signal_irq_work_init); | |
2459 | ||
a38d1107 | 2460 | #ifdef CONFIG_MODULES |
390e99cf SF |
2461 | static int bpf_event_notify(struct notifier_block *nb, unsigned long op, |
2462 | void *module) | |
a38d1107 MM |
2463 | { |
2464 | struct bpf_trace_module *btm, *tmp; | |
2465 | struct module *mod = module; | |
0340a6b7 | 2466 | int ret = 0; |
a38d1107 MM |
2467 | |
2468 | if (mod->num_bpf_raw_events == 0 || | |
2469 | (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING)) | |
0340a6b7 | 2470 | goto out; |
a38d1107 MM |
2471 | |
2472 | mutex_lock(&bpf_module_mutex); | |
2473 | ||
2474 | switch (op) { | |
2475 | case MODULE_STATE_COMING: | |
2476 | btm = kzalloc(sizeof(*btm), GFP_KERNEL); | |
2477 | if (btm) { | |
2478 | btm->module = module; | |
2479 | list_add(&btm->list, &bpf_trace_modules); | |
0340a6b7 PZ |
2480 | } else { |
2481 | ret = -ENOMEM; | |
a38d1107 MM |
2482 | } |
2483 | break; | |
2484 | case MODULE_STATE_GOING: | |
2485 | list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) { | |
2486 | if (btm->module == module) { | |
2487 | list_del(&btm->list); | |
2488 | kfree(btm); | |
2489 | break; | |
2490 | } | |
2491 | } | |
2492 | break; | |
2493 | } | |
2494 | ||
2495 | mutex_unlock(&bpf_module_mutex); | |
2496 | ||
0340a6b7 PZ |
2497 | out: |
2498 | return notifier_from_errno(ret); | |
a38d1107 MM |
2499 | } |
2500 | ||
2501 | static struct notifier_block bpf_module_nb = { | |
2502 | .notifier_call = bpf_event_notify, | |
2503 | }; | |
2504 | ||
390e99cf | 2505 | static int __init bpf_event_init(void) |
a38d1107 MM |
2506 | { |
2507 | register_module_notifier(&bpf_module_nb); | |
2508 | return 0; | |
2509 | } | |
2510 | ||
2511 | fs_initcall(bpf_event_init); | |
2512 | #endif /* CONFIG_MODULES */ | |
0dcac272 | 2513 | |
adf46d88 JO |
2514 | struct bpf_session_run_ctx { |
2515 | struct bpf_run_ctx run_ctx; | |
2516 | bool is_return; | |
5c919ace | 2517 | void *data; |
adf46d88 JO |
2518 | }; |
2519 | ||
0dcac272 JO |
2520 | #ifdef CONFIG_FPROBE |
2521 | struct bpf_kprobe_multi_link { | |
2522 | struct bpf_link link; | |
2523 | struct fprobe fp; | |
2524 | unsigned long *addrs; | |
ca74823c JO |
2525 | u64 *cookies; |
2526 | u32 cnt; | |
e22061b2 JO |
2527 | u32 mods_cnt; |
2528 | struct module **mods; | |
7ac8d0d2 | 2529 | u32 flags; |
0dcac272 JO |
2530 | }; |
2531 | ||
f7098690 | 2532 | struct bpf_kprobe_multi_run_ctx { |
adf46d88 | 2533 | struct bpf_session_run_ctx session_ctx; |
f7098690 JO |
2534 | struct bpf_kprobe_multi_link *link; |
2535 | unsigned long entry_ip; | |
2536 | }; | |
2537 | ||
0236fec5 JO |
2538 | struct user_syms { |
2539 | const char **syms; | |
2540 | char *buf; | |
2541 | }; | |
2542 | ||
2543 | static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt) | |
2544 | { | |
2545 | unsigned long __user usymbol; | |
2546 | const char **syms = NULL; | |
2547 | char *buf = NULL, *p; | |
2548 | int err = -ENOMEM; | |
2549 | unsigned int i; | |
2550 | ||
fd58f7df | 2551 | syms = kvmalloc_array(cnt, sizeof(*syms), GFP_KERNEL); |
0236fec5 JO |
2552 | if (!syms) |
2553 | goto error; | |
2554 | ||
fd58f7df | 2555 | buf = kvmalloc_array(cnt, KSYM_NAME_LEN, GFP_KERNEL); |
0236fec5 JO |
2556 | if (!buf) |
2557 | goto error; | |
2558 | ||
2559 | for (p = buf, i = 0; i < cnt; i++) { | |
2560 | if (__get_user(usymbol, usyms + i)) { | |
2561 | err = -EFAULT; | |
2562 | goto error; | |
2563 | } | |
2564 | err = strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LEN); | |
2565 | if (err == KSYM_NAME_LEN) | |
2566 | err = -E2BIG; | |
2567 | if (err < 0) | |
2568 | goto error; | |
2569 | syms[i] = p; | |
2570 | p += err + 1; | |
2571 | } | |
2572 | ||
2573 | us->syms = syms; | |
2574 | us->buf = buf; | |
2575 | return 0; | |
2576 | ||
2577 | error: | |
2578 | if (err) { | |
2579 | kvfree(syms); | |
2580 | kvfree(buf); | |
2581 | } | |
2582 | return err; | |
2583 | } | |
2584 | ||
e22061b2 JO |
2585 | static void kprobe_multi_put_modules(struct module **mods, u32 cnt) |
2586 | { | |
2587 | u32 i; | |
2588 | ||
2589 | for (i = 0; i < cnt; i++) | |
2590 | module_put(mods[i]); | |
2591 | } | |
2592 | ||
0236fec5 JO |
2593 | static void free_user_syms(struct user_syms *us) |
2594 | { | |
2595 | kvfree(us->syms); | |
2596 | kvfree(us->buf); | |
2597 | } | |
2598 | ||
0dcac272 JO |
2599 | static void bpf_kprobe_multi_link_release(struct bpf_link *link) |
2600 | { | |
2601 | struct bpf_kprobe_multi_link *kmulti_link; | |
2602 | ||
2603 | kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); | |
2604 | unregister_fprobe(&kmulti_link->fp); | |
e22061b2 | 2605 | kprobe_multi_put_modules(kmulti_link->mods, kmulti_link->mods_cnt); |
0dcac272 JO |
2606 | } |
2607 | ||
2608 | static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link) | |
2609 | { | |
2610 | struct bpf_kprobe_multi_link *kmulti_link; | |
2611 | ||
2612 | kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); | |
2613 | kvfree(kmulti_link->addrs); | |
ca74823c | 2614 | kvfree(kmulti_link->cookies); |
e22061b2 | 2615 | kfree(kmulti_link->mods); |
0dcac272 JO |
2616 | kfree(kmulti_link); |
2617 | } | |
2618 | ||
7ac8d0d2 YS |
2619 | static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link, |
2620 | struct bpf_link_info *info) | |
2621 | { | |
9fd112b1 | 2622 | u64 __user *ucookies = u64_to_user_ptr(info->kprobe_multi.cookies); |
7ac8d0d2 YS |
2623 | u64 __user *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs); |
2624 | struct bpf_kprobe_multi_link *kmulti_link; | |
2625 | u32 ucount = info->kprobe_multi.count; | |
2626 | int err = 0, i; | |
2627 | ||
2628 | if (!uaddrs ^ !ucount) | |
2629 | return -EINVAL; | |
9fd112b1 JO |
2630 | if (ucookies && !ucount) |
2631 | return -EINVAL; | |
7ac8d0d2 YS |
2632 | |
2633 | kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); | |
2634 | info->kprobe_multi.count = kmulti_link->cnt; | |
2635 | info->kprobe_multi.flags = kmulti_link->flags; | |
e2b2cd59 | 2636 | info->kprobe_multi.missed = kmulti_link->fp.nmissed; |
7ac8d0d2 YS |
2637 | |
2638 | if (!uaddrs) | |
2639 | return 0; | |
2640 | if (ucount < kmulti_link->cnt) | |
2641 | err = -ENOSPC; | |
2642 | else | |
2643 | ucount = kmulti_link->cnt; | |
2644 | ||
9fd112b1 JO |
2645 | if (ucookies) { |
2646 | if (kmulti_link->cookies) { | |
2647 | if (copy_to_user(ucookies, kmulti_link->cookies, ucount * sizeof(u64))) | |
2648 | return -EFAULT; | |
2649 | } else { | |
2650 | for (i = 0; i < ucount; i++) { | |
2651 | if (put_user(0, ucookies + i)) | |
2652 | return -EFAULT; | |
2653 | } | |
2654 | } | |
2655 | } | |
2656 | ||
7ac8d0d2 YS |
2657 | if (kallsyms_show_value(current_cred())) { |
2658 | if (copy_to_user(uaddrs, kmulti_link->addrs, ucount * sizeof(u64))) | |
2659 | return -EFAULT; | |
2660 | } else { | |
2661 | for (i = 0; i < ucount; i++) { | |
2662 | if (put_user(0, uaddrs + i)) | |
2663 | return -EFAULT; | |
2664 | } | |
2665 | } | |
2666 | return err; | |
2667 | } | |
2668 | ||
0dcac272 JO |
2669 | static const struct bpf_link_ops bpf_kprobe_multi_link_lops = { |
2670 | .release = bpf_kprobe_multi_link_release, | |
1a80dbcb | 2671 | .dealloc_deferred = bpf_kprobe_multi_link_dealloc, |
7ac8d0d2 | 2672 | .fill_link_info = bpf_kprobe_multi_link_fill_link_info, |
0dcac272 JO |
2673 | }; |
2674 | ||
ca74823c JO |
2675 | static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv) |
2676 | { | |
2677 | const struct bpf_kprobe_multi_link *link = priv; | |
2678 | unsigned long *addr_a = a, *addr_b = b; | |
2679 | u64 *cookie_a, *cookie_b; | |
ca74823c JO |
2680 | |
2681 | cookie_a = link->cookies + (addr_a - link->addrs); | |
2682 | cookie_b = link->cookies + (addr_b - link->addrs); | |
2683 | ||
2684 | /* swap addr_a/addr_b and cookie_a/cookie_b values */ | |
11e17ae4 JC |
2685 | swap(*addr_a, *addr_b); |
2686 | swap(*cookie_a, *cookie_b); | |
ca74823c JO |
2687 | } |
2688 | ||
1a1b0716 | 2689 | static int bpf_kprobe_multi_addrs_cmp(const void *a, const void *b) |
ca74823c JO |
2690 | { |
2691 | const unsigned long *addr_a = a, *addr_b = b; | |
2692 | ||
2693 | if (*addr_a == *addr_b) | |
2694 | return 0; | |
2695 | return *addr_a < *addr_b ? -1 : 1; | |
2696 | } | |
2697 | ||
2698 | static int bpf_kprobe_multi_cookie_cmp(const void *a, const void *b, const void *priv) | |
2699 | { | |
1a1b0716 | 2700 | return bpf_kprobe_multi_addrs_cmp(a, b); |
ca74823c JO |
2701 | } |
2702 | ||
f7098690 | 2703 | static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx) |
ca74823c | 2704 | { |
f7098690 | 2705 | struct bpf_kprobe_multi_run_ctx *run_ctx; |
ca74823c | 2706 | struct bpf_kprobe_multi_link *link; |
f7098690 | 2707 | u64 *cookie, entry_ip; |
ca74823c | 2708 | unsigned long *addr; |
ca74823c JO |
2709 | |
2710 | if (WARN_ON_ONCE(!ctx)) | |
2711 | return 0; | |
adf46d88 JO |
2712 | run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, |
2713 | session_ctx.run_ctx); | |
f7098690 | 2714 | link = run_ctx->link; |
ca74823c JO |
2715 | if (!link->cookies) |
2716 | return 0; | |
f7098690 JO |
2717 | entry_ip = run_ctx->entry_ip; |
2718 | addr = bsearch(&entry_ip, link->addrs, link->cnt, sizeof(entry_ip), | |
1a1b0716 | 2719 | bpf_kprobe_multi_addrs_cmp); |
ca74823c JO |
2720 | if (!addr) |
2721 | return 0; | |
2722 | cookie = link->cookies + (addr - link->addrs); | |
2723 | return *cookie; | |
2724 | } | |
2725 | ||
f7098690 JO |
2726 | static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx) |
2727 | { | |
2728 | struct bpf_kprobe_multi_run_ctx *run_ctx; | |
2729 | ||
adf46d88 JO |
2730 | run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, |
2731 | session_ctx.run_ctx); | |
f7098690 JO |
2732 | return run_ctx->entry_ip; |
2733 | } | |
2734 | ||
0dcac272 JO |
2735 | static int |
2736 | kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, | |
adf46d88 | 2737 | unsigned long entry_ip, struct pt_regs *regs, |
5c919ace | 2738 | bool is_return, void *data) |
0dcac272 | 2739 | { |
f7098690 | 2740 | struct bpf_kprobe_multi_run_ctx run_ctx = { |
adf46d88 JO |
2741 | .session_ctx = { |
2742 | .is_return = is_return, | |
5c919ace | 2743 | .data = data, |
adf46d88 | 2744 | }, |
f7098690 JO |
2745 | .link = link, |
2746 | .entry_ip = entry_ip, | |
2747 | }; | |
ca74823c | 2748 | struct bpf_run_ctx *old_run_ctx; |
0dcac272 JO |
2749 | int err; |
2750 | ||
2751 | if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) { | |
f915fcb3 | 2752 | bpf_prog_inc_misses_counter(link->link.prog); |
0dcac272 JO |
2753 | err = 0; |
2754 | goto out; | |
2755 | } | |
2756 | ||
2757 | migrate_disable(); | |
2758 | rcu_read_lock(); | |
adf46d88 | 2759 | old_run_ctx = bpf_set_run_ctx(&run_ctx.session_ctx.run_ctx); |
0dcac272 | 2760 | err = bpf_prog_run(link->link.prog, regs); |
ca74823c | 2761 | bpf_reset_run_ctx(old_run_ctx); |
0dcac272 JO |
2762 | rcu_read_unlock(); |
2763 | migrate_enable(); | |
2764 | ||
2765 | out: | |
2766 | __this_cpu_dec(bpf_prog_active); | |
2767 | return err; | |
2768 | } | |
2769 | ||
39d95420 | 2770 | static int |
c09eb2e5 | 2771 | kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip, |
cb16330d MHG |
2772 | unsigned long ret_ip, struct pt_regs *regs, |
2773 | void *data) | |
0dcac272 | 2774 | { |
0dcac272 | 2775 | struct bpf_kprobe_multi_link *link; |
535a3692 | 2776 | int err; |
0dcac272 | 2777 | |
39d95420 | 2778 | link = container_of(fp, struct bpf_kprobe_multi_link, fp); |
5c919ace | 2779 | err = kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs, false, data); |
535a3692 | 2780 | return is_kprobe_session(link->link.prog) ? err : 0; |
39d95420 MHG |
2781 | } |
2782 | ||
2783 | static void | |
2784 | kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip, | |
cb16330d MHG |
2785 | unsigned long ret_ip, struct pt_regs *regs, |
2786 | void *data) | |
0dcac272 | 2787 | { |
0dcac272 JO |
2788 | struct bpf_kprobe_multi_link *link; |
2789 | ||
0dcac272 | 2790 | link = container_of(fp, struct bpf_kprobe_multi_link, fp); |
5c919ace | 2791 | kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs, true, data); |
0dcac272 JO |
2792 | } |
2793 | ||
eb5fb032 | 2794 | static int symbols_cmp_r(const void *a, const void *b, const void *priv) |
0dcac272 | 2795 | { |
0236fec5 JO |
2796 | const char **str_a = (const char **) a; |
2797 | const char **str_b = (const char **) b; | |
0dcac272 | 2798 | |
0236fec5 | 2799 | return strcmp(*str_a, *str_b); |
0dcac272 JO |
2800 | } |
2801 | ||
eb5fb032 JO |
2802 | struct multi_symbols_sort { |
2803 | const char **funcs; | |
2804 | u64 *cookies; | |
2805 | }; | |
2806 | ||
2807 | static void symbols_swap_r(void *a, void *b, int size, const void *priv) | |
2808 | { | |
2809 | const struct multi_symbols_sort *data = priv; | |
2810 | const char **name_a = a, **name_b = b; | |
2811 | ||
2812 | swap(*name_a, *name_b); | |
2813 | ||
2814 | /* If defined, swap also related cookies. */ | |
2815 | if (data->cookies) { | |
2816 | u64 *cookie_a, *cookie_b; | |
2817 | ||
2818 | cookie_a = data->cookies + (name_a - data->funcs); | |
2819 | cookie_b = data->cookies + (name_b - data->funcs); | |
2820 | swap(*cookie_a, *cookie_b); | |
2821 | } | |
2822 | } | |
2823 | ||
6a5f2d6e | 2824 | struct modules_array { |
e22061b2 JO |
2825 | struct module **mods; |
2826 | int mods_cnt; | |
2827 | int mods_cap; | |
2828 | }; | |
2829 | ||
6a5f2d6e | 2830 | static int add_module(struct modules_array *arr, struct module *mod) |
e22061b2 | 2831 | { |
e22061b2 JO |
2832 | struct module **mods; |
2833 | ||
6a5f2d6e JO |
2834 | if (arr->mods_cnt == arr->mods_cap) { |
2835 | arr->mods_cap = max(16, arr->mods_cap * 3 / 2); | |
2836 | mods = krealloc_array(arr->mods, arr->mods_cap, sizeof(*mods), GFP_KERNEL); | |
e22061b2 JO |
2837 | if (!mods) |
2838 | return -ENOMEM; | |
6a5f2d6e | 2839 | arr->mods = mods; |
e22061b2 JO |
2840 | } |
2841 | ||
6a5f2d6e JO |
2842 | arr->mods[arr->mods_cnt] = mod; |
2843 | arr->mods_cnt++; | |
e22061b2 JO |
2844 | return 0; |
2845 | } | |
2846 | ||
6a5f2d6e JO |
2847 | static bool has_module(struct modules_array *arr, struct module *mod) |
2848 | { | |
2849 | int i; | |
2850 | ||
2851 | for (i = arr->mods_cnt - 1; i >= 0; i--) { | |
2852 | if (arr->mods[i] == mod) | |
2853 | return true; | |
2854 | } | |
2855 | return false; | |
2856 | } | |
2857 | ||
e22061b2 JO |
2858 | static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u32 addrs_cnt) |
2859 | { | |
6a5f2d6e JO |
2860 | struct modules_array arr = {}; |
2861 | u32 i, err = 0; | |
2862 | ||
2863 | for (i = 0; i < addrs_cnt; i++) { | |
2864 | struct module *mod; | |
2865 | ||
2866 | preempt_disable(); | |
2867 | mod = __module_address(addrs[i]); | |
2868 | /* Either no module or we it's already stored */ | |
2869 | if (!mod || has_module(&arr, mod)) { | |
2870 | preempt_enable(); | |
2871 | continue; | |
2872 | } | |
2873 | if (!try_module_get(mod)) | |
2874 | err = -EINVAL; | |
2875 | preempt_enable(); | |
2876 | if (err) | |
2877 | break; | |
2878 | err = add_module(&arr, mod); | |
2879 | if (err) { | |
2880 | module_put(mod); | |
2881 | break; | |
2882 | } | |
2883 | } | |
e22061b2 JO |
2884 | |
2885 | /* We return either err < 0 in case of error, ... */ | |
e22061b2 | 2886 | if (err) { |
6a5f2d6e JO |
2887 | kprobe_multi_put_modules(arr.mods, arr.mods_cnt); |
2888 | kfree(arr.mods); | |
e22061b2 JO |
2889 | return err; |
2890 | } | |
2891 | ||
2892 | /* or number of modules found if everything is ok. */ | |
6a5f2d6e JO |
2893 | *mods = arr.mods; |
2894 | return arr.mods_cnt; | |
e22061b2 JO |
2895 | } |
2896 | ||
41bc46c1 JO |
2897 | static int addrs_check_error_injection_list(unsigned long *addrs, u32 cnt) |
2898 | { | |
2899 | u32 i; | |
2900 | ||
2901 | for (i = 0; i < cnt; i++) { | |
2902 | if (!within_error_injection_list(addrs[i])) | |
2903 | return -EINVAL; | |
2904 | } | |
2905 | return 0; | |
2906 | } | |
2907 | ||
0dcac272 JO |
2908 | int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) |
2909 | { | |
2910 | struct bpf_kprobe_multi_link *link = NULL; | |
2911 | struct bpf_link_primer link_primer; | |
ca74823c | 2912 | void __user *ucookies; |
0dcac272 JO |
2913 | unsigned long *addrs; |
2914 | u32 flags, cnt, size; | |
2915 | void __user *uaddrs; | |
ca74823c | 2916 | u64 *cookies = NULL; |
0dcac272 JO |
2917 | void __user *usyms; |
2918 | int err; | |
2919 | ||
2920 | /* no support for 32bit archs yet */ | |
2921 | if (sizeof(u64) != sizeof(void *)) | |
2922 | return -EOPNOTSUPP; | |
2923 | ||
535a3692 | 2924 | if (!is_kprobe_multi(prog)) |
0dcac272 JO |
2925 | return -EINVAL; |
2926 | ||
2927 | flags = attr->link_create.kprobe_multi.flags; | |
2928 | if (flags & ~BPF_F_KPROBE_MULTI_RETURN) | |
2929 | return -EINVAL; | |
2930 | ||
2931 | uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs); | |
2932 | usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms); | |
2933 | if (!!uaddrs == !!usyms) | |
2934 | return -EINVAL; | |
2935 | ||
2936 | cnt = attr->link_create.kprobe_multi.cnt; | |
2937 | if (!cnt) | |
2938 | return -EINVAL; | |
d6d1e6c1 HT |
2939 | if (cnt > MAX_KPROBE_MULTI_CNT) |
2940 | return -E2BIG; | |
0dcac272 JO |
2941 | |
2942 | size = cnt * sizeof(*addrs); | |
fd58f7df | 2943 | addrs = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL); |
0dcac272 JO |
2944 | if (!addrs) |
2945 | return -ENOMEM; | |
2946 | ||
eb5fb032 JO |
2947 | ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies); |
2948 | if (ucookies) { | |
2949 | cookies = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL); | |
2950 | if (!cookies) { | |
2951 | err = -ENOMEM; | |
2952 | goto error; | |
2953 | } | |
2954 | if (copy_from_user(cookies, ucookies, size)) { | |
2955 | err = -EFAULT; | |
2956 | goto error; | |
2957 | } | |
2958 | } | |
2959 | ||
0dcac272 JO |
2960 | if (uaddrs) { |
2961 | if (copy_from_user(addrs, uaddrs, size)) { | |
2962 | err = -EFAULT; | |
2963 | goto error; | |
2964 | } | |
2965 | } else { | |
eb5fb032 JO |
2966 | struct multi_symbols_sort data = { |
2967 | .cookies = cookies, | |
2968 | }; | |
0236fec5 JO |
2969 | struct user_syms us; |
2970 | ||
2971 | err = copy_user_syms(&us, usyms, cnt); | |
2972 | if (err) | |
2973 | goto error; | |
2974 | ||
eb5fb032 JO |
2975 | if (cookies) |
2976 | data.funcs = us.syms; | |
2977 | ||
2978 | sort_r(us.syms, cnt, sizeof(*us.syms), symbols_cmp_r, | |
2979 | symbols_swap_r, &data); | |
2980 | ||
0236fec5 JO |
2981 | err = ftrace_lookup_symbols(us.syms, cnt, addrs); |
2982 | free_user_syms(&us); | |
0dcac272 JO |
2983 | if (err) |
2984 | goto error; | |
2985 | } | |
2986 | ||
41bc46c1 JO |
2987 | if (prog->kprobe_override && addrs_check_error_injection_list(addrs, cnt)) { |
2988 | err = -EINVAL; | |
2989 | goto error; | |
2990 | } | |
2991 | ||
0dcac272 JO |
2992 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
2993 | if (!link) { | |
2994 | err = -ENOMEM; | |
2995 | goto error; | |
2996 | } | |
2997 | ||
2998 | bpf_link_init(&link->link, BPF_LINK_TYPE_KPROBE_MULTI, | |
2999 | &bpf_kprobe_multi_link_lops, prog); | |
3000 | ||
3001 | err = bpf_link_prime(&link->link, &link_primer); | |
3002 | if (err) | |
3003 | goto error; | |
3004 | ||
535a3692 | 3005 | if (!(flags & BPF_F_KPROBE_MULTI_RETURN)) |
0dcac272 | 3006 | link->fp.entry_handler = kprobe_multi_link_handler; |
535a3692 JO |
3007 | if ((flags & BPF_F_KPROBE_MULTI_RETURN) || is_kprobe_session(prog)) |
3008 | link->fp.exit_handler = kprobe_multi_link_exit_handler; | |
5c919ace JO |
3009 | if (is_kprobe_session(prog)) |
3010 | link->fp.entry_data_size = sizeof(u64); | |
0dcac272 JO |
3011 | |
3012 | link->addrs = addrs; | |
ca74823c JO |
3013 | link->cookies = cookies; |
3014 | link->cnt = cnt; | |
7ac8d0d2 | 3015 | link->flags = flags; |
ca74823c JO |
3016 | |
3017 | if (cookies) { | |
3018 | /* | |
3019 | * Sorting addresses will trigger sorting cookies as well | |
3020 | * (check bpf_kprobe_multi_cookie_swap). This way we can | |
3021 | * find cookie based on the address in bpf_get_attach_cookie | |
3022 | * helper. | |
3023 | */ | |
3024 | sort_r(addrs, cnt, sizeof(*addrs), | |
3025 | bpf_kprobe_multi_cookie_cmp, | |
3026 | bpf_kprobe_multi_cookie_swap, | |
3027 | link); | |
e22061b2 JO |
3028 | } |
3029 | ||
3030 | err = get_modules_for_addrs(&link->mods, addrs, cnt); | |
3031 | if (err < 0) { | |
3032 | bpf_link_cleanup(&link_primer); | |
3033 | return err; | |
ca74823c | 3034 | } |
e22061b2 | 3035 | link->mods_cnt = err; |
0dcac272 JO |
3036 | |
3037 | err = register_fprobe_ips(&link->fp, addrs, cnt); | |
3038 | if (err) { | |
e22061b2 | 3039 | kprobe_multi_put_modules(link->mods, link->mods_cnt); |
0dcac272 JO |
3040 | bpf_link_cleanup(&link_primer); |
3041 | return err; | |
3042 | } | |
3043 | ||
3044 | return bpf_link_settle(&link_primer); | |
3045 | ||
3046 | error: | |
3047 | kfree(link); | |
3048 | kvfree(addrs); | |
ca74823c | 3049 | kvfree(cookies); |
0dcac272 JO |
3050 | return err; |
3051 | } | |
3052 | #else /* !CONFIG_FPROBE */ | |
3053 | int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) | |
3054 | { | |
3055 | return -EOPNOTSUPP; | |
3056 | } | |
f7098690 JO |
3057 | static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx) |
3058 | { | |
3059 | return 0; | |
3060 | } | |
3061 | static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx) | |
ca74823c JO |
3062 | { |
3063 | return 0; | |
3064 | } | |
0dcac272 | 3065 | #endif |
89ae89f5 JO |
3066 | |
3067 | #ifdef CONFIG_UPROBES | |
3068 | struct bpf_uprobe_multi_link; | |
3069 | ||
3070 | struct bpf_uprobe { | |
3071 | struct bpf_uprobe_multi_link *link; | |
3072 | loff_t offset; | |
4930b7f5 | 3073 | unsigned long ref_ctr_offset; |
0b779b61 | 3074 | u64 cookie; |
3c83a9ad | 3075 | struct uprobe *uprobe; |
89ae89f5 JO |
3076 | struct uprobe_consumer consumer; |
3077 | }; | |
3078 | ||
3079 | struct bpf_uprobe_multi_link { | |
3080 | struct path path; | |
3081 | struct bpf_link link; | |
3082 | u32 cnt; | |
e56fdbfb | 3083 | u32 flags; |
89ae89f5 | 3084 | struct bpf_uprobe *uprobes; |
b733eead | 3085 | struct task_struct *task; |
89ae89f5 JO |
3086 | }; |
3087 | ||
3088 | struct bpf_uprobe_multi_run_ctx { | |
3089 | struct bpf_run_ctx run_ctx; | |
3090 | unsigned long entry_ip; | |
0b779b61 | 3091 | struct bpf_uprobe *uprobe; |
89ae89f5 JO |
3092 | }; |
3093 | ||
3c83a9ad | 3094 | static void bpf_uprobe_unregister(struct bpf_uprobe *uprobes, u32 cnt) |
89ae89f5 JO |
3095 | { |
3096 | u32 i; | |
3097 | ||
3c83a9ad | 3098 | for (i = 0; i < cnt; i++) |
04b01625 PZ |
3099 | uprobe_unregister_nosync(uprobes[i].uprobe, &uprobes[i].consumer); |
3100 | ||
3101 | if (cnt) | |
3102 | uprobe_unregister_sync(); | |
89ae89f5 JO |
3103 | } |
3104 | ||
3105 | static void bpf_uprobe_multi_link_release(struct bpf_link *link) | |
3106 | { | |
3107 | struct bpf_uprobe_multi_link *umulti_link; | |
3108 | ||
3109 | umulti_link = container_of(link, struct bpf_uprobe_multi_link, link); | |
3c83a9ad | 3110 | bpf_uprobe_unregister(umulti_link->uprobes, umulti_link->cnt); |
e9c856ca AN |
3111 | if (umulti_link->task) |
3112 | put_task_struct(umulti_link->task); | |
3113 | path_put(&umulti_link->path); | |
89ae89f5 JO |
3114 | } |
3115 | ||
3116 | static void bpf_uprobe_multi_link_dealloc(struct bpf_link *link) | |
3117 | { | |
3118 | struct bpf_uprobe_multi_link *umulti_link; | |
3119 | ||
3120 | umulti_link = container_of(link, struct bpf_uprobe_multi_link, link); | |
89ae89f5 JO |
3121 | kvfree(umulti_link->uprobes); |
3122 | kfree(umulti_link); | |
3123 | } | |
3124 | ||
e56fdbfb JO |
3125 | static int bpf_uprobe_multi_link_fill_link_info(const struct bpf_link *link, |
3126 | struct bpf_link_info *info) | |
3127 | { | |
3128 | u64 __user *uref_ctr_offsets = u64_to_user_ptr(info->uprobe_multi.ref_ctr_offsets); | |
3129 | u64 __user *ucookies = u64_to_user_ptr(info->uprobe_multi.cookies); | |
3130 | u64 __user *uoffsets = u64_to_user_ptr(info->uprobe_multi.offsets); | |
3131 | u64 __user *upath = u64_to_user_ptr(info->uprobe_multi.path); | |
3132 | u32 upath_size = info->uprobe_multi.path_size; | |
3133 | struct bpf_uprobe_multi_link *umulti_link; | |
3134 | u32 ucount = info->uprobe_multi.count; | |
3135 | int err = 0, i; | |
3136 | long left; | |
3137 | ||
3138 | if (!upath ^ !upath_size) | |
3139 | return -EINVAL; | |
3140 | ||
3141 | if ((uoffsets || uref_ctr_offsets || ucookies) && !ucount) | |
3142 | return -EINVAL; | |
3143 | ||
3144 | umulti_link = container_of(link, struct bpf_uprobe_multi_link, link); | |
3145 | info->uprobe_multi.count = umulti_link->cnt; | |
3146 | info->uprobe_multi.flags = umulti_link->flags; | |
3147 | info->uprobe_multi.pid = umulti_link->task ? | |
3148 | task_pid_nr_ns(umulti_link->task, task_active_pid_ns(current)) : 0; | |
3149 | ||
3150 | if (upath) { | |
3151 | char *p, *buf; | |
3152 | ||
3153 | upath_size = min_t(u32, upath_size, PATH_MAX); | |
3154 | ||
3155 | buf = kmalloc(upath_size, GFP_KERNEL); | |
3156 | if (!buf) | |
3157 | return -ENOMEM; | |
3158 | p = d_path(&umulti_link->path, buf, upath_size); | |
3159 | if (IS_ERR(p)) { | |
3160 | kfree(buf); | |
3161 | return PTR_ERR(p); | |
3162 | } | |
3163 | upath_size = buf + upath_size - p; | |
3164 | left = copy_to_user(upath, p, upath_size); | |
3165 | kfree(buf); | |
3166 | if (left) | |
3167 | return -EFAULT; | |
3168 | info->uprobe_multi.path_size = upath_size; | |
3169 | } | |
3170 | ||
3171 | if (!uoffsets && !ucookies && !uref_ctr_offsets) | |
3172 | return 0; | |
3173 | ||
3174 | if (ucount < umulti_link->cnt) | |
3175 | err = -ENOSPC; | |
3176 | else | |
3177 | ucount = umulti_link->cnt; | |
3178 | ||
3179 | for (i = 0; i < ucount; i++) { | |
3180 | if (uoffsets && | |
3181 | put_user(umulti_link->uprobes[i].offset, uoffsets + i)) | |
3182 | return -EFAULT; | |
3183 | if (uref_ctr_offsets && | |
3184 | put_user(umulti_link->uprobes[i].ref_ctr_offset, uref_ctr_offsets + i)) | |
3185 | return -EFAULT; | |
3186 | if (ucookies && | |
3187 | put_user(umulti_link->uprobes[i].cookie, ucookies + i)) | |
3188 | return -EFAULT; | |
3189 | } | |
3190 | ||
3191 | return err; | |
3192 | } | |
3193 | ||
89ae89f5 JO |
3194 | static const struct bpf_link_ops bpf_uprobe_multi_link_lops = { |
3195 | .release = bpf_uprobe_multi_link_release, | |
1a80dbcb | 3196 | .dealloc_deferred = bpf_uprobe_multi_link_dealloc, |
e56fdbfb | 3197 | .fill_link_info = bpf_uprobe_multi_link_fill_link_info, |
89ae89f5 JO |
3198 | }; |
3199 | ||
3200 | static int uprobe_prog_run(struct bpf_uprobe *uprobe, | |
3201 | unsigned long entry_ip, | |
3202 | struct pt_regs *regs) | |
3203 | { | |
3204 | struct bpf_uprobe_multi_link *link = uprobe->link; | |
3205 | struct bpf_uprobe_multi_run_ctx run_ctx = { | |
3206 | .entry_ip = entry_ip, | |
0b779b61 | 3207 | .uprobe = uprobe, |
89ae89f5 JO |
3208 | }; |
3209 | struct bpf_prog *prog = link->link.prog; | |
66c84731 | 3210 | bool sleepable = prog->sleepable; |
89ae89f5 JO |
3211 | struct bpf_run_ctx *old_run_ctx; |
3212 | int err = 0; | |
3213 | ||
900f362e | 3214 | if (link->task && !same_thread_group(current, link->task)) |
b733eead JO |
3215 | return 0; |
3216 | ||
89ae89f5 JO |
3217 | if (sleepable) |
3218 | rcu_read_lock_trace(); | |
3219 | else | |
3220 | rcu_read_lock(); | |
3221 | ||
3222 | migrate_disable(); | |
3223 | ||
3224 | old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); | |
3225 | err = bpf_prog_run(link->link.prog, regs); | |
3226 | bpf_reset_run_ctx(old_run_ctx); | |
3227 | ||
3228 | migrate_enable(); | |
3229 | ||
3230 | if (sleepable) | |
3231 | rcu_read_unlock_trace(); | |
3232 | else | |
3233 | rcu_read_unlock(); | |
3234 | return err; | |
3235 | } | |
3236 | ||
b733eead | 3237 | static bool |
59da880a | 3238 | uprobe_multi_link_filter(struct uprobe_consumer *con, struct mm_struct *mm) |
b733eead JO |
3239 | { |
3240 | struct bpf_uprobe *uprobe; | |
3241 | ||
3242 | uprobe = container_of(con, struct bpf_uprobe, consumer); | |
3243 | return uprobe->link->task->mm == mm; | |
3244 | } | |
3245 | ||
89ae89f5 JO |
3246 | static int |
3247 | uprobe_multi_link_handler(struct uprobe_consumer *con, struct pt_regs *regs) | |
3248 | { | |
3249 | struct bpf_uprobe *uprobe; | |
3250 | ||
3251 | uprobe = container_of(con, struct bpf_uprobe, consumer); | |
3252 | return uprobe_prog_run(uprobe, instruction_pointer(regs), regs); | |
3253 | } | |
3254 | ||
3255 | static int | |
3256 | uprobe_multi_link_ret_handler(struct uprobe_consumer *con, unsigned long func, struct pt_regs *regs) | |
3257 | { | |
3258 | struct bpf_uprobe *uprobe; | |
3259 | ||
3260 | uprobe = container_of(con, struct bpf_uprobe, consumer); | |
3261 | return uprobe_prog_run(uprobe, func, regs); | |
3262 | } | |
3263 | ||
686328d8 JO |
3264 | static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx) |
3265 | { | |
3266 | struct bpf_uprobe_multi_run_ctx *run_ctx; | |
3267 | ||
3268 | run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, run_ctx); | |
3269 | return run_ctx->entry_ip; | |
3270 | } | |
3271 | ||
0b779b61 JO |
3272 | static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx) |
3273 | { | |
3274 | struct bpf_uprobe_multi_run_ctx *run_ctx; | |
3275 | ||
3276 | run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, run_ctx); | |
3277 | return run_ctx->uprobe->cookie; | |
3278 | } | |
3279 | ||
89ae89f5 JO |
3280 | int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) |
3281 | { | |
3282 | struct bpf_uprobe_multi_link *link = NULL; | |
3283 | unsigned long __user *uref_ctr_offsets; | |
89ae89f5 JO |
3284 | struct bpf_link_primer link_primer; |
3285 | struct bpf_uprobe *uprobes = NULL; | |
b733eead | 3286 | struct task_struct *task = NULL; |
89ae89f5 | 3287 | unsigned long __user *uoffsets; |
0b779b61 | 3288 | u64 __user *ucookies; |
89ae89f5 JO |
3289 | void __user *upath; |
3290 | u32 flags, cnt, i; | |
3291 | struct path path; | |
3292 | char *name; | |
b733eead | 3293 | pid_t pid; |
89ae89f5 JO |
3294 | int err; |
3295 | ||
3296 | /* no support for 32bit archs yet */ | |
3297 | if (sizeof(u64) != sizeof(void *)) | |
3298 | return -EOPNOTSUPP; | |
3299 | ||
3300 | if (prog->expected_attach_type != BPF_TRACE_UPROBE_MULTI) | |
3301 | return -EINVAL; | |
3302 | ||
3303 | flags = attr->link_create.uprobe_multi.flags; | |
3304 | if (flags & ~BPF_F_UPROBE_MULTI_RETURN) | |
3305 | return -EINVAL; | |
3306 | ||
3307 | /* | |
3308 | * path, offsets and cnt are mandatory, | |
0b779b61 | 3309 | * ref_ctr_offsets and cookies are optional |
89ae89f5 JO |
3310 | */ |
3311 | upath = u64_to_user_ptr(attr->link_create.uprobe_multi.path); | |
3312 | uoffsets = u64_to_user_ptr(attr->link_create.uprobe_multi.offsets); | |
3313 | cnt = attr->link_create.uprobe_multi.cnt; | |
46ba0e49 | 3314 | pid = attr->link_create.uprobe_multi.pid; |
89ae89f5 | 3315 | |
46ba0e49 | 3316 | if (!upath || !uoffsets || !cnt || pid < 0) |
89ae89f5 | 3317 | return -EINVAL; |
8b2efe51 HT |
3318 | if (cnt > MAX_UPROBE_MULTI_CNT) |
3319 | return -E2BIG; | |
89ae89f5 JO |
3320 | |
3321 | uref_ctr_offsets = u64_to_user_ptr(attr->link_create.uprobe_multi.ref_ctr_offsets); | |
0b779b61 | 3322 | ucookies = u64_to_user_ptr(attr->link_create.uprobe_multi.cookies); |
89ae89f5 JO |
3323 | |
3324 | name = strndup_user(upath, PATH_MAX); | |
3325 | if (IS_ERR(name)) { | |
3326 | err = PTR_ERR(name); | |
3327 | return err; | |
3328 | } | |
3329 | ||
3330 | err = kern_path(name, LOOKUP_FOLLOW, &path); | |
3331 | kfree(name); | |
3332 | if (err) | |
3333 | return err; | |
3334 | ||
3335 | if (!d_is_reg(path.dentry)) { | |
3336 | err = -EBADF; | |
3337 | goto error_path_put; | |
3338 | } | |
3339 | ||
b733eead | 3340 | if (pid) { |
46ba0e49 | 3341 | task = get_pid_task(find_vpid(pid), PIDTYPE_TGID); |
57eb5e1c JO |
3342 | if (!task) { |
3343 | err = -ESRCH; | |
b733eead | 3344 | goto error_path_put; |
57eb5e1c | 3345 | } |
b733eead JO |
3346 | } |
3347 | ||
89ae89f5 JO |
3348 | err = -ENOMEM; |
3349 | ||
3350 | link = kzalloc(sizeof(*link), GFP_KERNEL); | |
3351 | uprobes = kvcalloc(cnt, sizeof(*uprobes), GFP_KERNEL); | |
3352 | ||
3353 | if (!uprobes || !link) | |
3354 | goto error_free; | |
3355 | ||
89ae89f5 | 3356 | for (i = 0; i < cnt; i++) { |
3983c002 | 3357 | if (__get_user(uprobes[i].offset, uoffsets + i)) { |
0b779b61 JO |
3358 | err = -EFAULT; |
3359 | goto error_free; | |
3360 | } | |
3983c002 JO |
3361 | if (uprobes[i].offset < 0) { |
3362 | err = -EINVAL; | |
3363 | goto error_free; | |
3364 | } | |
4930b7f5 | 3365 | if (uref_ctr_offsets && __get_user(uprobes[i].ref_ctr_offset, uref_ctr_offsets + i)) { |
89ae89f5 JO |
3366 | err = -EFAULT; |
3367 | goto error_free; | |
3368 | } | |
3983c002 | 3369 | if (ucookies && __get_user(uprobes[i].cookie, ucookies + i)) { |
89ae89f5 JO |
3370 | err = -EFAULT; |
3371 | goto error_free; | |
3372 | } | |
3373 | ||
3374 | uprobes[i].link = link; | |
3375 | ||
3376 | if (flags & BPF_F_UPROBE_MULTI_RETURN) | |
3377 | uprobes[i].consumer.ret_handler = uprobe_multi_link_ret_handler; | |
3378 | else | |
3379 | uprobes[i].consumer.handler = uprobe_multi_link_handler; | |
b733eead JO |
3380 | |
3381 | if (pid) | |
3382 | uprobes[i].consumer.filter = uprobe_multi_link_filter; | |
89ae89f5 JO |
3383 | } |
3384 | ||
3385 | link->cnt = cnt; | |
3386 | link->uprobes = uprobes; | |
3387 | link->path = path; | |
b733eead | 3388 | link->task = task; |
e56fdbfb | 3389 | link->flags = flags; |
89ae89f5 JO |
3390 | |
3391 | bpf_link_init(&link->link, BPF_LINK_TYPE_UPROBE_MULTI, | |
3392 | &bpf_uprobe_multi_link_lops, prog); | |
3393 | ||
3394 | for (i = 0; i < cnt; i++) { | |
3c83a9ad ON |
3395 | uprobes[i].uprobe = uprobe_register(d_real_inode(link->path.dentry), |
3396 | uprobes[i].offset, | |
3397 | uprobes[i].ref_ctr_offset, | |
3398 | &uprobes[i].consumer); | |
3399 | if (IS_ERR(uprobes[i].uprobe)) { | |
3400 | err = PTR_ERR(uprobes[i].uprobe); | |
5fe6e308 ON |
3401 | link->cnt = i; |
3402 | goto error_unregister; | |
89ae89f5 JO |
3403 | } |
3404 | } | |
3405 | ||
3406 | err = bpf_link_prime(&link->link, &link_primer); | |
3407 | if (err) | |
5fe6e308 | 3408 | goto error_unregister; |
89ae89f5 | 3409 | |
89ae89f5 JO |
3410 | return bpf_link_settle(&link_primer); |
3411 | ||
5fe6e308 ON |
3412 | error_unregister: |
3413 | bpf_uprobe_unregister(uprobes, link->cnt); | |
3414 | ||
89ae89f5 | 3415 | error_free: |
89ae89f5 JO |
3416 | kvfree(uprobes); |
3417 | kfree(link); | |
b733eead JO |
3418 | if (task) |
3419 | put_task_struct(task); | |
89ae89f5 JO |
3420 | error_path_put: |
3421 | path_put(&path); | |
3422 | return err; | |
3423 | } | |
3424 | #else /* !CONFIG_UPROBES */ | |
3425 | int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) | |
3426 | { | |
3427 | return -EOPNOTSUPP; | |
3428 | } | |
0b779b61 JO |
3429 | static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx) |
3430 | { | |
3431 | return 0; | |
3432 | } | |
686328d8 JO |
3433 | static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx) |
3434 | { | |
3435 | return 0; | |
3436 | } | |
89ae89f5 | 3437 | #endif /* CONFIG_UPROBES */ |
adf46d88 | 3438 | |
adf46d88 JO |
3439 | __bpf_kfunc_start_defs(); |
3440 | ||
3441 | __bpf_kfunc bool bpf_session_is_return(void) | |
3442 | { | |
3443 | struct bpf_session_run_ctx *session_ctx; | |
3444 | ||
3445 | session_ctx = container_of(current->bpf_ctx, struct bpf_session_run_ctx, run_ctx); | |
3446 | return session_ctx->is_return; | |
3447 | } | |
3448 | ||
717d6313 | 3449 | __bpf_kfunc __u64 *bpf_session_cookie(void) |
5c919ace JO |
3450 | { |
3451 | struct bpf_session_run_ctx *session_ctx; | |
3452 | ||
3453 | session_ctx = container_of(current->bpf_ctx, struct bpf_session_run_ctx, run_ctx); | |
3454 | return session_ctx->data; | |
3455 | } | |
3456 | ||
adf46d88 JO |
3457 | __bpf_kfunc_end_defs(); |
3458 | ||
3459 | BTF_KFUNCS_START(kprobe_multi_kfunc_set_ids) | |
3460 | BTF_ID_FLAGS(func, bpf_session_is_return) | |
5c919ace | 3461 | BTF_ID_FLAGS(func, bpf_session_cookie) |
adf46d88 JO |
3462 | BTF_KFUNCS_END(kprobe_multi_kfunc_set_ids) |
3463 | ||
3464 | static int bpf_kprobe_multi_filter(const struct bpf_prog *prog, u32 kfunc_id) | |
3465 | { | |
3466 | if (!btf_id_set8_contains(&kprobe_multi_kfunc_set_ids, kfunc_id)) | |
3467 | return 0; | |
3468 | ||
3469 | if (!is_kprobe_session(prog)) | |
3470 | return -EACCES; | |
3471 | ||
3472 | return 0; | |
3473 | } | |
3474 | ||
3475 | static const struct btf_kfunc_id_set bpf_kprobe_multi_kfunc_set = { | |
3476 | .owner = THIS_MODULE, | |
3477 | .set = &kprobe_multi_kfunc_set_ids, | |
3478 | .filter = bpf_kprobe_multi_filter, | |
3479 | }; | |
3480 | ||
3481 | static int __init bpf_kprobe_multi_kfuncs_init(void) | |
3482 | { | |
3483 | return register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_kprobe_multi_kfunc_set); | |
3484 | } | |
3485 | ||
3486 | late_initcall(bpf_kprobe_multi_kfuncs_init); |