Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com |
| 3 | * Copyright (c) 2016 Facebook |
| 4 | */ |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/types.h> |
| 7 | #include <linux/slab.h> |
| 8 | #include <linux/bpf.h> |
| 9 | #include <linux/bpf_perf_event.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10 | #include <linux/btf.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11 | #include <linux/filter.h> |
| 12 | #include <linux/uaccess.h> |
| 13 | #include <linux/ctype.h> |
| 14 | #include <linux/kprobes.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 15 | #include <linux/spinlock.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 16 | #include <linux/syscalls.h> |
| 17 | #include <linux/error-injection.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 18 | #include <linux/btf_ids.h> |
| 19 | |
| 20 | #include <uapi/linux/bpf.h> |
| 21 | #include <uapi/linux/btf.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 22 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 23 | #include <asm/tlb.h> |
| 24 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 25 | #include "trace_probe.h" |
| 26 | #include "trace.h" |
| 27 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 28 | #define CREATE_TRACE_POINTS |
| 29 | #include "bpf_trace.h" |
| 30 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 31 | #define bpf_event_rcu_dereference(p) \ |
| 32 | rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex)) |
| 33 | |
| 34 | #ifdef CONFIG_MODULES |
| 35 | struct bpf_trace_module { |
| 36 | struct module *module; |
| 37 | struct list_head list; |
| 38 | }; |
| 39 | |
| 40 | static LIST_HEAD(bpf_trace_modules); |
| 41 | static DEFINE_MUTEX(bpf_module_mutex); |
| 42 | |
| 43 | static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) |
| 44 | { |
| 45 | struct bpf_raw_event_map *btp, *ret = NULL; |
| 46 | struct bpf_trace_module *btm; |
| 47 | unsigned int i; |
| 48 | |
| 49 | mutex_lock(&bpf_module_mutex); |
| 50 | list_for_each_entry(btm, &bpf_trace_modules, list) { |
| 51 | for (i = 0; i < btm->module->num_bpf_raw_events; ++i) { |
| 52 | btp = &btm->module->bpf_raw_events[i]; |
| 53 | if (!strcmp(btp->tp->name, name)) { |
| 54 | if (try_module_get(btm->module)) |
| 55 | ret = btp; |
| 56 | goto out; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | out: |
| 61 | mutex_unlock(&bpf_module_mutex); |
| 62 | return ret; |
| 63 | } |
| 64 | #else |
| 65 | static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) |
| 66 | { |
| 67 | return NULL; |
| 68 | } |
| 69 | #endif /* CONFIG_MODULES */ |
| 70 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 71 | u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); |
| 72 | u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); |
| 73 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 74 | static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size, |
| 75 | u64 flags, const struct btf **btf, |
| 76 | s32 *btf_id); |
| 77 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 78 | /** |
| 79 | * trace_call_bpf - invoke BPF program |
| 80 | * @call: tracepoint event |
| 81 | * @ctx: opaque context pointer |
| 82 | * |
| 83 | * kprobe handlers execute BPF programs via this helper. |
| 84 | * Can be used from static tracepoints in the future. |
| 85 | * |
| 86 | * Return: BPF programs always return an integer which is interpreted by |
| 87 | * kprobe handler as: |
| 88 | * 0 - return from kprobe (event is filtered out) |
| 89 | * 1 - store kprobe event into ring buffer |
| 90 | * Other values are reserved and currently alias to 1 |
| 91 | */ |
| 92 | unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx) |
| 93 | { |
| 94 | unsigned int ret; |
| 95 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 96 | cant_sleep(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 97 | |
| 98 | if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) { |
| 99 | /* |
| 100 | * since some bpf program is already running on this cpu, |
| 101 | * don't call into another bpf program (same or different) |
| 102 | * and don't send kprobe event into ring-buffer, |
| 103 | * so return zero here |
| 104 | */ |
| 105 | ret = 0; |
| 106 | goto out; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock |
| 111 | * to all call sites, we did a bpf_prog_array_valid() there to check |
| 112 | * whether call->prog_array is empty or not, which is |
| 113 | * a heurisitc to speed up execution. |
| 114 | * |
| 115 | * If bpf_prog_array_valid() fetched prog_array was |
| 116 | * non-NULL, we go into trace_call_bpf() and do the actual |
| 117 | * proper rcu_dereference() under RCU lock. |
| 118 | * If it turns out that prog_array is NULL then, we bail out. |
| 119 | * For the opposite, if the bpf_prog_array_valid() fetched pointer |
| 120 | * was NULL, you'll skip the prog_array with the risk of missing |
| 121 | * out of events when it was updated in between this and the |
| 122 | * rcu_dereference() which is accepted risk. |
| 123 | */ |
| 124 | ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN); |
| 125 | |
| 126 | out: |
| 127 | __this_cpu_dec(bpf_prog_active); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 128 | |
| 129 | return ret; |
| 130 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 131 | |
| 132 | #ifdef CONFIG_BPF_KPROBE_OVERRIDE |
| 133 | BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc) |
| 134 | { |
| 135 | regs_set_return_value(regs, rc); |
| 136 | override_function_with_return(regs); |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static const struct bpf_func_proto bpf_override_return_proto = { |
| 141 | .func = bpf_override_return, |
| 142 | .gpl_only = true, |
| 143 | .ret_type = RET_INTEGER, |
| 144 | .arg1_type = ARG_PTR_TO_CTX, |
| 145 | .arg2_type = ARG_ANYTHING, |
| 146 | }; |
| 147 | #endif |
| 148 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 149 | static __always_inline int |
| 150 | bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 151 | { |
| 152 | int ret; |
| 153 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 154 | ret = copy_from_user_nofault(dst, unsafe_ptr, size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 155 | if (unlikely(ret < 0)) |
| 156 | memset(dst, 0, size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 157 | return ret; |
| 158 | } |
| 159 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 160 | BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size, |
| 161 | const void __user *, unsafe_ptr) |
| 162 | { |
| 163 | return bpf_probe_read_user_common(dst, size, unsafe_ptr); |
| 164 | } |
| 165 | |
| 166 | const struct bpf_func_proto bpf_probe_read_user_proto = { |
| 167 | .func = bpf_probe_read_user, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 168 | .gpl_only = true, |
| 169 | .ret_type = RET_INTEGER, |
| 170 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, |
| 171 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, |
| 172 | .arg3_type = ARG_ANYTHING, |
| 173 | }; |
| 174 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 175 | static __always_inline int |
| 176 | bpf_probe_read_user_str_common(void *dst, u32 size, |
| 177 | const void __user *unsafe_ptr) |
| 178 | { |
| 179 | int ret; |
| 180 | |
| 181 | /* |
| 182 | * NB: We rely on strncpy_from_user() not copying junk past the NUL |
| 183 | * terminator into `dst`. |
| 184 | * |
| 185 | * strncpy_from_user() does long-sized strides in the fast path. If the |
| 186 | * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`, |
| 187 | * then there could be junk after the NUL in `dst`. If user takes `dst` |
| 188 | * and keys a hash map with it, then semantically identical strings can |
| 189 | * occupy multiple entries in the map. |
| 190 | */ |
| 191 | ret = strncpy_from_user_nofault(dst, unsafe_ptr, size); |
| 192 | if (unlikely(ret < 0)) |
| 193 | memset(dst, 0, size); |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size, |
| 198 | const void __user *, unsafe_ptr) |
| 199 | { |
| 200 | return bpf_probe_read_user_str_common(dst, size, unsafe_ptr); |
| 201 | } |
| 202 | |
| 203 | const struct bpf_func_proto bpf_probe_read_user_str_proto = { |
| 204 | .func = bpf_probe_read_user_str, |
| 205 | .gpl_only = true, |
| 206 | .ret_type = RET_INTEGER, |
| 207 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, |
| 208 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, |
| 209 | .arg3_type = ARG_ANYTHING, |
| 210 | }; |
| 211 | |
| 212 | static __always_inline int |
| 213 | bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr) |
| 214 | { |
| 215 | int ret; |
| 216 | |
| 217 | ret = copy_from_kernel_nofault(dst, unsafe_ptr, size); |
| 218 | if (unlikely(ret < 0)) |
| 219 | memset(dst, 0, size); |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size, |
| 224 | const void *, unsafe_ptr) |
| 225 | { |
| 226 | return bpf_probe_read_kernel_common(dst, size, unsafe_ptr); |
| 227 | } |
| 228 | |
| 229 | const struct bpf_func_proto bpf_probe_read_kernel_proto = { |
| 230 | .func = bpf_probe_read_kernel, |
| 231 | .gpl_only = true, |
| 232 | .ret_type = RET_INTEGER, |
| 233 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, |
| 234 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, |
| 235 | .arg3_type = ARG_ANYTHING, |
| 236 | }; |
| 237 | |
| 238 | static __always_inline int |
| 239 | bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr) |
| 240 | { |
| 241 | int ret; |
| 242 | |
| 243 | /* |
| 244 | * The strncpy_from_kernel_nofault() call will likely not fill the |
| 245 | * entire buffer, but that's okay in this circumstance as we're probing |
| 246 | * arbitrary memory anyway similar to bpf_probe_read_*() and might |
| 247 | * as well probe the stack. Thus, memory is explicitly cleared |
| 248 | * only in error case, so that improper users ignoring return |
| 249 | * code altogether don't copy garbage; otherwise length of string |
| 250 | * is returned that can be used for bpf_perf_event_output() et al. |
| 251 | */ |
| 252 | ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size); |
| 253 | if (unlikely(ret < 0)) |
| 254 | memset(dst, 0, size); |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size, |
| 259 | const void *, unsafe_ptr) |
| 260 | { |
| 261 | return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr); |
| 262 | } |
| 263 | |
| 264 | const struct bpf_func_proto bpf_probe_read_kernel_str_proto = { |
| 265 | .func = bpf_probe_read_kernel_str, |
| 266 | .gpl_only = true, |
| 267 | .ret_type = RET_INTEGER, |
| 268 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, |
| 269 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, |
| 270 | .arg3_type = ARG_ANYTHING, |
| 271 | }; |
| 272 | |
| 273 | #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE |
| 274 | BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size, |
| 275 | const void *, unsafe_ptr) |
| 276 | { |
| 277 | if ((unsigned long)unsafe_ptr < TASK_SIZE) { |
| 278 | return bpf_probe_read_user_common(dst, size, |
| 279 | (__force void __user *)unsafe_ptr); |
| 280 | } |
| 281 | return bpf_probe_read_kernel_common(dst, size, unsafe_ptr); |
| 282 | } |
| 283 | |
| 284 | static const struct bpf_func_proto bpf_probe_read_compat_proto = { |
| 285 | .func = bpf_probe_read_compat, |
| 286 | .gpl_only = true, |
| 287 | .ret_type = RET_INTEGER, |
| 288 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, |
| 289 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, |
| 290 | .arg3_type = ARG_ANYTHING, |
| 291 | }; |
| 292 | |
| 293 | BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size, |
| 294 | const void *, unsafe_ptr) |
| 295 | { |
| 296 | if ((unsigned long)unsafe_ptr < TASK_SIZE) { |
| 297 | return bpf_probe_read_user_str_common(dst, size, |
| 298 | (__force void __user *)unsafe_ptr); |
| 299 | } |
| 300 | return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr); |
| 301 | } |
| 302 | |
| 303 | static const struct bpf_func_proto bpf_probe_read_compat_str_proto = { |
| 304 | .func = bpf_probe_read_compat_str, |
| 305 | .gpl_only = true, |
| 306 | .ret_type = RET_INTEGER, |
| 307 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, |
| 308 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, |
| 309 | .arg3_type = ARG_ANYTHING, |
| 310 | }; |
| 311 | #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */ |
| 312 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 313 | BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 314 | u32, size) |
| 315 | { |
| 316 | /* |
| 317 | * Ensure we're in user context which is safe for the helper to |
| 318 | * run. This helper has no business in a kthread. |
| 319 | * |
| 320 | * access_ok() should prevent writing to non-user memory, but in |
| 321 | * some situations (nommu, temporary switch, etc) access_ok() does |
| 322 | * not provide enough validation, hence the check on KERNEL_DS. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 323 | * |
| 324 | * nmi_uaccess_okay() ensures the probe is not run in an interim |
| 325 | * state, when the task or mm are switched. This is specifically |
| 326 | * required to prevent the use of temporary mm. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 327 | */ |
| 328 | |
| 329 | if (unlikely(in_interrupt() || |
| 330 | current->flags & (PF_KTHREAD | PF_EXITING))) |
| 331 | return -EPERM; |
| 332 | if (unlikely(uaccess_kernel())) |
| 333 | return -EPERM; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 334 | if (unlikely(!nmi_uaccess_okay())) |
| 335 | return -EPERM; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 336 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 337 | return copy_to_user_nofault(unsafe_ptr, src, size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static const struct bpf_func_proto bpf_probe_write_user_proto = { |
| 341 | .func = bpf_probe_write_user, |
| 342 | .gpl_only = true, |
| 343 | .ret_type = RET_INTEGER, |
| 344 | .arg1_type = ARG_ANYTHING, |
| 345 | .arg2_type = ARG_PTR_TO_MEM, |
| 346 | .arg3_type = ARG_CONST_SIZE, |
| 347 | }; |
| 348 | |
| 349 | static const struct bpf_func_proto *bpf_get_probe_write_proto(void) |
| 350 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 351 | if (!capable(CAP_SYS_ADMIN)) |
| 352 | return NULL; |
| 353 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 354 | pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!", |
| 355 | current->comm, task_pid_nr(current)); |
| 356 | |
| 357 | return &bpf_probe_write_user_proto; |
| 358 | } |
| 359 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 360 | static void bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype, |
| 361 | size_t bufsz) |
| 362 | { |
| 363 | void __user *user_ptr = (__force void __user *)unsafe_ptr; |
| 364 | |
| 365 | buf[0] = 0; |
| 366 | |
| 367 | switch (fmt_ptype) { |
| 368 | case 's': |
| 369 | #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE |
| 370 | if ((unsigned long)unsafe_ptr < TASK_SIZE) { |
| 371 | strncpy_from_user_nofault(buf, user_ptr, bufsz); |
| 372 | break; |
| 373 | } |
| 374 | fallthrough; |
| 375 | #endif |
| 376 | case 'k': |
| 377 | strncpy_from_kernel_nofault(buf, unsafe_ptr, bufsz); |
| 378 | break; |
| 379 | case 'u': |
| 380 | strncpy_from_user_nofault(buf, user_ptr, bufsz); |
| 381 | break; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | static DEFINE_RAW_SPINLOCK(trace_printk_lock); |
| 386 | |
| 387 | #define BPF_TRACE_PRINTK_SIZE 1024 |
| 388 | |
| 389 | static __printf(1, 0) int bpf_do_trace_printk(const char *fmt, ...) |
| 390 | { |
| 391 | static char buf[BPF_TRACE_PRINTK_SIZE]; |
| 392 | unsigned long flags; |
| 393 | va_list ap; |
| 394 | int ret; |
| 395 | |
| 396 | raw_spin_lock_irqsave(&trace_printk_lock, flags); |
| 397 | va_start(ap, fmt); |
| 398 | ret = vsnprintf(buf, sizeof(buf), fmt, ap); |
| 399 | va_end(ap); |
| 400 | /* vsnprintf() will not append null for zero-length strings */ |
| 401 | if (ret == 0) |
| 402 | buf[0] = '\0'; |
| 403 | trace_bpf_trace_printk(buf); |
| 404 | raw_spin_unlock_irqrestore(&trace_printk_lock, flags); |
| 405 | |
| 406 | return ret; |
| 407 | } |
| 408 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 409 | /* |
| 410 | * Only limited trace_printk() conversion specifiers allowed: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 411 | * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %pB %pks %pus %s |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 412 | */ |
| 413 | BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1, |
| 414 | u64, arg2, u64, arg3) |
| 415 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 416 | int i, mod[3] = {}, fmt_cnt = 0; |
| 417 | char buf[64], fmt_ptype; |
| 418 | void *unsafe_ptr = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 419 | bool str_seen = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 420 | |
| 421 | /* |
| 422 | * bpf_check()->check_func_arg()->check_stack_boundary() |
| 423 | * guarantees that fmt points to bpf program stack, |
| 424 | * fmt_size bytes of it were initialized and fmt_size > 0 |
| 425 | */ |
| 426 | if (fmt[--fmt_size] != 0) |
| 427 | return -EINVAL; |
| 428 | |
| 429 | /* check format string for allowed specifiers */ |
| 430 | for (i = 0; i < fmt_size; i++) { |
| 431 | if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) |
| 432 | return -EINVAL; |
| 433 | |
| 434 | if (fmt[i] != '%') |
| 435 | continue; |
| 436 | |
| 437 | if (fmt_cnt >= 3) |
| 438 | return -EINVAL; |
| 439 | |
| 440 | /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */ |
| 441 | i++; |
| 442 | if (fmt[i] == 'l') { |
| 443 | mod[fmt_cnt]++; |
| 444 | i++; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 445 | } else if (fmt[i] == 'p') { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 446 | mod[fmt_cnt]++; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 447 | if ((fmt[i + 1] == 'k' || |
| 448 | fmt[i + 1] == 'u') && |
| 449 | fmt[i + 2] == 's') { |
| 450 | fmt_ptype = fmt[i + 1]; |
| 451 | i += 2; |
| 452 | goto fmt_str; |
| 453 | } |
| 454 | |
| 455 | if (fmt[i + 1] == 'B') { |
| 456 | i++; |
| 457 | goto fmt_next; |
| 458 | } |
| 459 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 460 | /* disallow any further format extensions */ |
| 461 | if (fmt[i + 1] != 0 && |
| 462 | !isspace(fmt[i + 1]) && |
| 463 | !ispunct(fmt[i + 1])) |
| 464 | return -EINVAL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 465 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 466 | goto fmt_next; |
| 467 | } else if (fmt[i] == 's') { |
| 468 | mod[fmt_cnt]++; |
| 469 | fmt_ptype = fmt[i]; |
| 470 | fmt_str: |
| 471 | if (str_seen) |
| 472 | /* allow only one '%s' per fmt string */ |
| 473 | return -EINVAL; |
| 474 | str_seen = true; |
| 475 | |
| 476 | if (fmt[i + 1] != 0 && |
| 477 | !isspace(fmt[i + 1]) && |
| 478 | !ispunct(fmt[i + 1])) |
| 479 | return -EINVAL; |
| 480 | |
| 481 | switch (fmt_cnt) { |
| 482 | case 0: |
| 483 | unsafe_ptr = (void *)(long)arg1; |
| 484 | arg1 = (long)buf; |
| 485 | break; |
| 486 | case 1: |
| 487 | unsafe_ptr = (void *)(long)arg2; |
| 488 | arg2 = (long)buf; |
| 489 | break; |
| 490 | case 2: |
| 491 | unsafe_ptr = (void *)(long)arg3; |
| 492 | arg3 = (long)buf; |
| 493 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 494 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 495 | |
| 496 | bpf_trace_copy_string(buf, unsafe_ptr, fmt_ptype, |
| 497 | sizeof(buf)); |
| 498 | goto fmt_next; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | if (fmt[i] == 'l') { |
| 502 | mod[fmt_cnt]++; |
| 503 | i++; |
| 504 | } |
| 505 | |
| 506 | if (fmt[i] != 'i' && fmt[i] != 'd' && |
| 507 | fmt[i] != 'u' && fmt[i] != 'x') |
| 508 | return -EINVAL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 509 | fmt_next: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 510 | fmt_cnt++; |
| 511 | } |
| 512 | |
| 513 | /* Horrid workaround for getting va_list handling working with different |
| 514 | * argument type combinations generically for 32 and 64 bit archs. |
| 515 | */ |
| 516 | #define __BPF_TP_EMIT() __BPF_ARG3_TP() |
| 517 | #define __BPF_TP(...) \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 518 | bpf_do_trace_printk(fmt, ##__VA_ARGS__) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 519 | |
| 520 | #define __BPF_ARG1_TP(...) \ |
| 521 | ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \ |
| 522 | ? __BPF_TP(arg1, ##__VA_ARGS__) \ |
| 523 | : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \ |
| 524 | ? __BPF_TP((long)arg1, ##__VA_ARGS__) \ |
| 525 | : __BPF_TP((u32)arg1, ##__VA_ARGS__))) |
| 526 | |
| 527 | #define __BPF_ARG2_TP(...) \ |
| 528 | ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \ |
| 529 | ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \ |
| 530 | : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \ |
| 531 | ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \ |
| 532 | : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__))) |
| 533 | |
| 534 | #define __BPF_ARG3_TP(...) \ |
| 535 | ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \ |
| 536 | ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \ |
| 537 | : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \ |
| 538 | ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \ |
| 539 | : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__))) |
| 540 | |
| 541 | return __BPF_TP_EMIT(); |
| 542 | } |
| 543 | |
| 544 | static const struct bpf_func_proto bpf_trace_printk_proto = { |
| 545 | .func = bpf_trace_printk, |
| 546 | .gpl_only = true, |
| 547 | .ret_type = RET_INTEGER, |
| 548 | .arg1_type = ARG_PTR_TO_MEM, |
| 549 | .arg2_type = ARG_CONST_SIZE, |
| 550 | }; |
| 551 | |
| 552 | const struct bpf_func_proto *bpf_get_trace_printk_proto(void) |
| 553 | { |
| 554 | /* |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 555 | * This program might be calling bpf_trace_printk, |
| 556 | * so enable the associated bpf_trace/bpf_trace_printk event. |
| 557 | * Repeat this each time as it is possible a user has |
| 558 | * disabled bpf_trace_printk events. By loading a program |
| 559 | * calling bpf_trace_printk() however the user has expressed |
| 560 | * the intent to see such events. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 561 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 562 | if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1)) |
| 563 | pr_warn_ratelimited("could not enable bpf_trace_printk events"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 564 | |
| 565 | return &bpf_trace_printk_proto; |
| 566 | } |
| 567 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 568 | #define MAX_SEQ_PRINTF_VARARGS 12 |
| 569 | #define MAX_SEQ_PRINTF_MAX_MEMCPY 6 |
| 570 | #define MAX_SEQ_PRINTF_STR_LEN 128 |
| 571 | |
| 572 | struct bpf_seq_printf_buf { |
| 573 | char buf[MAX_SEQ_PRINTF_MAX_MEMCPY][MAX_SEQ_PRINTF_STR_LEN]; |
| 574 | }; |
| 575 | static DEFINE_PER_CPU(struct bpf_seq_printf_buf, bpf_seq_printf_buf); |
| 576 | static DEFINE_PER_CPU(int, bpf_seq_printf_buf_used); |
| 577 | |
| 578 | BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size, |
| 579 | const void *, data, u32, data_len) |
| 580 | { |
| 581 | int err = -EINVAL, fmt_cnt = 0, memcpy_cnt = 0; |
| 582 | int i, buf_used, copy_size, num_args; |
| 583 | u64 params[MAX_SEQ_PRINTF_VARARGS]; |
| 584 | struct bpf_seq_printf_buf *bufs; |
| 585 | const u64 *args = data; |
| 586 | |
| 587 | buf_used = this_cpu_inc_return(bpf_seq_printf_buf_used); |
| 588 | if (WARN_ON_ONCE(buf_used > 1)) { |
| 589 | err = -EBUSY; |
| 590 | goto out; |
| 591 | } |
| 592 | |
| 593 | bufs = this_cpu_ptr(&bpf_seq_printf_buf); |
| 594 | |
| 595 | /* |
| 596 | * bpf_check()->check_func_arg()->check_stack_boundary() |
| 597 | * guarantees that fmt points to bpf program stack, |
| 598 | * fmt_size bytes of it were initialized and fmt_size > 0 |
| 599 | */ |
| 600 | if (fmt[--fmt_size] != 0) |
| 601 | goto out; |
| 602 | |
| 603 | if (data_len & 7) |
| 604 | goto out; |
| 605 | |
| 606 | for (i = 0; i < fmt_size; i++) { |
| 607 | if (fmt[i] == '%') { |
| 608 | if (fmt[i + 1] == '%') |
| 609 | i++; |
| 610 | else if (!data || !data_len) |
| 611 | goto out; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | num_args = data_len / 8; |
| 616 | |
| 617 | /* check format string for allowed specifiers */ |
| 618 | for (i = 0; i < fmt_size; i++) { |
| 619 | /* only printable ascii for now. */ |
| 620 | if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) { |
| 621 | err = -EINVAL; |
| 622 | goto out; |
| 623 | } |
| 624 | |
| 625 | if (fmt[i] != '%') |
| 626 | continue; |
| 627 | |
| 628 | if (fmt[i + 1] == '%') { |
| 629 | i++; |
| 630 | continue; |
| 631 | } |
| 632 | |
| 633 | if (fmt_cnt >= MAX_SEQ_PRINTF_VARARGS) { |
| 634 | err = -E2BIG; |
| 635 | goto out; |
| 636 | } |
| 637 | |
| 638 | if (fmt_cnt >= num_args) { |
| 639 | err = -EINVAL; |
| 640 | goto out; |
| 641 | } |
| 642 | |
| 643 | /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */ |
| 644 | i++; |
| 645 | |
| 646 | /* skip optional "[0 +-][num]" width formating field */ |
| 647 | while (fmt[i] == '0' || fmt[i] == '+' || fmt[i] == '-' || |
| 648 | fmt[i] == ' ') |
| 649 | i++; |
| 650 | if (fmt[i] >= '1' && fmt[i] <= '9') { |
| 651 | i++; |
| 652 | while (fmt[i] >= '0' && fmt[i] <= '9') |
| 653 | i++; |
| 654 | } |
| 655 | |
| 656 | if (fmt[i] == 's') { |
| 657 | void *unsafe_ptr; |
| 658 | |
| 659 | /* try our best to copy */ |
| 660 | if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) { |
| 661 | err = -E2BIG; |
| 662 | goto out; |
| 663 | } |
| 664 | |
| 665 | unsafe_ptr = (void *)(long)args[fmt_cnt]; |
| 666 | err = strncpy_from_kernel_nofault(bufs->buf[memcpy_cnt], |
| 667 | unsafe_ptr, MAX_SEQ_PRINTF_STR_LEN); |
| 668 | if (err < 0) |
| 669 | bufs->buf[memcpy_cnt][0] = '\0'; |
| 670 | params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt]; |
| 671 | |
| 672 | fmt_cnt++; |
| 673 | memcpy_cnt++; |
| 674 | continue; |
| 675 | } |
| 676 | |
| 677 | if (fmt[i] == 'p') { |
| 678 | if (fmt[i + 1] == 0 || |
| 679 | fmt[i + 1] == 'K' || |
| 680 | fmt[i + 1] == 'x' || |
| 681 | fmt[i + 1] == 'B') { |
| 682 | /* just kernel pointers */ |
| 683 | params[fmt_cnt] = args[fmt_cnt]; |
| 684 | fmt_cnt++; |
| 685 | continue; |
| 686 | } |
| 687 | |
| 688 | /* only support "%pI4", "%pi4", "%pI6" and "%pi6". */ |
| 689 | if (fmt[i + 1] != 'i' && fmt[i + 1] != 'I') { |
| 690 | err = -EINVAL; |
| 691 | goto out; |
| 692 | } |
| 693 | if (fmt[i + 2] != '4' && fmt[i + 2] != '6') { |
| 694 | err = -EINVAL; |
| 695 | goto out; |
| 696 | } |
| 697 | |
| 698 | if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) { |
| 699 | err = -E2BIG; |
| 700 | goto out; |
| 701 | } |
| 702 | |
| 703 | |
| 704 | copy_size = (fmt[i + 2] == '4') ? 4 : 16; |
| 705 | |
| 706 | err = copy_from_kernel_nofault(bufs->buf[memcpy_cnt], |
| 707 | (void *) (long) args[fmt_cnt], |
| 708 | copy_size); |
| 709 | if (err < 0) |
| 710 | memset(bufs->buf[memcpy_cnt], 0, copy_size); |
| 711 | params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt]; |
| 712 | |
| 713 | i += 2; |
| 714 | fmt_cnt++; |
| 715 | memcpy_cnt++; |
| 716 | continue; |
| 717 | } |
| 718 | |
| 719 | if (fmt[i] == 'l') { |
| 720 | i++; |
| 721 | if (fmt[i] == 'l') |
| 722 | i++; |
| 723 | } |
| 724 | |
| 725 | if (fmt[i] != 'i' && fmt[i] != 'd' && |
| 726 | fmt[i] != 'u' && fmt[i] != 'x' && |
| 727 | fmt[i] != 'X') { |
| 728 | err = -EINVAL; |
| 729 | goto out; |
| 730 | } |
| 731 | |
| 732 | params[fmt_cnt] = args[fmt_cnt]; |
| 733 | fmt_cnt++; |
| 734 | } |
| 735 | |
| 736 | /* Maximumly we can have MAX_SEQ_PRINTF_VARARGS parameter, just give |
| 737 | * all of them to seq_printf(). |
| 738 | */ |
| 739 | seq_printf(m, fmt, params[0], params[1], params[2], params[3], |
| 740 | params[4], params[5], params[6], params[7], params[8], |
| 741 | params[9], params[10], params[11]); |
| 742 | |
| 743 | err = seq_has_overflowed(m) ? -EOVERFLOW : 0; |
| 744 | out: |
| 745 | this_cpu_dec(bpf_seq_printf_buf_used); |
| 746 | return err; |
| 747 | } |
| 748 | |
| 749 | BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file) |
| 750 | |
| 751 | static const struct bpf_func_proto bpf_seq_printf_proto = { |
| 752 | .func = bpf_seq_printf, |
| 753 | .gpl_only = true, |
| 754 | .ret_type = RET_INTEGER, |
| 755 | .arg1_type = ARG_PTR_TO_BTF_ID, |
| 756 | .arg1_btf_id = &btf_seq_file_ids[0], |
| 757 | .arg2_type = ARG_PTR_TO_MEM, |
| 758 | .arg3_type = ARG_CONST_SIZE, |
| 759 | .arg4_type = ARG_PTR_TO_MEM_OR_NULL, |
| 760 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
| 761 | }; |
| 762 | |
| 763 | BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len) |
| 764 | { |
| 765 | return seq_write(m, data, len) ? -EOVERFLOW : 0; |
| 766 | } |
| 767 | |
| 768 | static const struct bpf_func_proto bpf_seq_write_proto = { |
| 769 | .func = bpf_seq_write, |
| 770 | .gpl_only = true, |
| 771 | .ret_type = RET_INTEGER, |
| 772 | .arg1_type = ARG_PTR_TO_BTF_ID, |
| 773 | .arg1_btf_id = &btf_seq_file_ids[0], |
| 774 | .arg2_type = ARG_PTR_TO_MEM, |
| 775 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, |
| 776 | }; |
| 777 | |
| 778 | BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr, |
| 779 | u32, btf_ptr_size, u64, flags) |
| 780 | { |
| 781 | const struct btf *btf; |
| 782 | s32 btf_id; |
| 783 | int ret; |
| 784 | |
| 785 | ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id); |
| 786 | if (ret) |
| 787 | return ret; |
| 788 | |
| 789 | return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags); |
| 790 | } |
| 791 | |
| 792 | static const struct bpf_func_proto bpf_seq_printf_btf_proto = { |
| 793 | .func = bpf_seq_printf_btf, |
| 794 | .gpl_only = true, |
| 795 | .ret_type = RET_INTEGER, |
| 796 | .arg1_type = ARG_PTR_TO_BTF_ID, |
| 797 | .arg1_btf_id = &btf_seq_file_ids[0], |
| 798 | .arg2_type = ARG_PTR_TO_MEM, |
| 799 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, |
| 800 | .arg4_type = ARG_ANYTHING, |
| 801 | }; |
| 802 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 803 | static __always_inline int |
| 804 | get_map_perf_counter(struct bpf_map *map, u64 flags, |
| 805 | u64 *value, u64 *enabled, u64 *running) |
| 806 | { |
| 807 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 808 | unsigned int cpu = smp_processor_id(); |
| 809 | u64 index = flags & BPF_F_INDEX_MASK; |
| 810 | struct bpf_event_entry *ee; |
| 811 | |
| 812 | if (unlikely(flags & ~(BPF_F_INDEX_MASK))) |
| 813 | return -EINVAL; |
| 814 | if (index == BPF_F_CURRENT_CPU) |
| 815 | index = cpu; |
| 816 | if (unlikely(index >= array->map.max_entries)) |
| 817 | return -E2BIG; |
| 818 | |
| 819 | ee = READ_ONCE(array->ptrs[index]); |
| 820 | if (!ee) |
| 821 | return -ENOENT; |
| 822 | |
| 823 | return perf_event_read_local(ee->event, value, enabled, running); |
| 824 | } |
| 825 | |
| 826 | BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags) |
| 827 | { |
| 828 | u64 value = 0; |
| 829 | int err; |
| 830 | |
| 831 | err = get_map_perf_counter(map, flags, &value, NULL, NULL); |
| 832 | /* |
| 833 | * this api is ugly since we miss [-22..-2] range of valid |
| 834 | * counter values, but that's uapi |
| 835 | */ |
| 836 | if (err) |
| 837 | return err; |
| 838 | return value; |
| 839 | } |
| 840 | |
| 841 | static const struct bpf_func_proto bpf_perf_event_read_proto = { |
| 842 | .func = bpf_perf_event_read, |
| 843 | .gpl_only = true, |
| 844 | .ret_type = RET_INTEGER, |
| 845 | .arg1_type = ARG_CONST_MAP_PTR, |
| 846 | .arg2_type = ARG_ANYTHING, |
| 847 | }; |
| 848 | |
| 849 | BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags, |
| 850 | struct bpf_perf_event_value *, buf, u32, size) |
| 851 | { |
| 852 | int err = -EINVAL; |
| 853 | |
| 854 | if (unlikely(size != sizeof(struct bpf_perf_event_value))) |
| 855 | goto clear; |
| 856 | err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled, |
| 857 | &buf->running); |
| 858 | if (unlikely(err)) |
| 859 | goto clear; |
| 860 | return 0; |
| 861 | clear: |
| 862 | memset(buf, 0, size); |
| 863 | return err; |
| 864 | } |
| 865 | |
| 866 | static const struct bpf_func_proto bpf_perf_event_read_value_proto = { |
| 867 | .func = bpf_perf_event_read_value, |
| 868 | .gpl_only = true, |
| 869 | .ret_type = RET_INTEGER, |
| 870 | .arg1_type = ARG_CONST_MAP_PTR, |
| 871 | .arg2_type = ARG_ANYTHING, |
| 872 | .arg3_type = ARG_PTR_TO_UNINIT_MEM, |
| 873 | .arg4_type = ARG_CONST_SIZE, |
| 874 | }; |
| 875 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 876 | static __always_inline u64 |
| 877 | __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map, |
| 878 | u64 flags, struct perf_sample_data *sd) |
| 879 | { |
| 880 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 881 | unsigned int cpu = smp_processor_id(); |
| 882 | u64 index = flags & BPF_F_INDEX_MASK; |
| 883 | struct bpf_event_entry *ee; |
| 884 | struct perf_event *event; |
| 885 | |
| 886 | if (index == BPF_F_CURRENT_CPU) |
| 887 | index = cpu; |
| 888 | if (unlikely(index >= array->map.max_entries)) |
| 889 | return -E2BIG; |
| 890 | |
| 891 | ee = READ_ONCE(array->ptrs[index]); |
| 892 | if (!ee) |
| 893 | return -ENOENT; |
| 894 | |
| 895 | event = ee->event; |
| 896 | if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE || |
| 897 | event->attr.config != PERF_COUNT_SW_BPF_OUTPUT)) |
| 898 | return -EINVAL; |
| 899 | |
| 900 | if (unlikely(event->oncpu != cpu)) |
| 901 | return -EOPNOTSUPP; |
| 902 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 903 | return perf_event_output(event, sd, regs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 904 | } |
| 905 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 906 | /* |
| 907 | * Support executing tracepoints in normal, irq, and nmi context that each call |
| 908 | * bpf_perf_event_output |
| 909 | */ |
| 910 | struct bpf_trace_sample_data { |
| 911 | struct perf_sample_data sds[3]; |
| 912 | }; |
| 913 | |
| 914 | static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds); |
| 915 | static DEFINE_PER_CPU(int, bpf_trace_nest_level); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 916 | BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map, |
| 917 | u64, flags, void *, data, u64, size) |
| 918 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 919 | struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds); |
| 920 | int nest_level = this_cpu_inc_return(bpf_trace_nest_level); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 921 | struct perf_raw_record raw = { |
| 922 | .frag = { |
| 923 | .size = size, |
| 924 | .data = data, |
| 925 | }, |
| 926 | }; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 927 | struct perf_sample_data *sd; |
| 928 | int err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 929 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 930 | if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) { |
| 931 | err = -EBUSY; |
| 932 | goto out; |
| 933 | } |
| 934 | |
| 935 | sd = &sds->sds[nest_level - 1]; |
| 936 | |
| 937 | if (unlikely(flags & ~(BPF_F_INDEX_MASK))) { |
| 938 | err = -EINVAL; |
| 939 | goto out; |
| 940 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 941 | |
| 942 | perf_sample_data_init(sd, 0, 0); |
| 943 | sd->raw = &raw; |
| 944 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 945 | err = __bpf_perf_event_output(regs, map, flags, sd); |
| 946 | |
| 947 | out: |
| 948 | this_cpu_dec(bpf_trace_nest_level); |
| 949 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | static const struct bpf_func_proto bpf_perf_event_output_proto = { |
| 953 | .func = bpf_perf_event_output, |
| 954 | .gpl_only = true, |
| 955 | .ret_type = RET_INTEGER, |
| 956 | .arg1_type = ARG_PTR_TO_CTX, |
| 957 | .arg2_type = ARG_CONST_MAP_PTR, |
| 958 | .arg3_type = ARG_ANYTHING, |
| 959 | .arg4_type = ARG_PTR_TO_MEM, |
| 960 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
| 961 | }; |
| 962 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 963 | static DEFINE_PER_CPU(int, bpf_event_output_nest_level); |
| 964 | struct bpf_nested_pt_regs { |
| 965 | struct pt_regs regs[3]; |
| 966 | }; |
| 967 | static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs); |
| 968 | static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 969 | |
| 970 | u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, |
| 971 | void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) |
| 972 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 973 | int nest_level = this_cpu_inc_return(bpf_event_output_nest_level); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 974 | struct perf_raw_frag frag = { |
| 975 | .copy = ctx_copy, |
| 976 | .size = ctx_size, |
| 977 | .data = ctx, |
| 978 | }; |
| 979 | struct perf_raw_record raw = { |
| 980 | .frag = { |
| 981 | { |
| 982 | .next = ctx_size ? &frag : NULL, |
| 983 | }, |
| 984 | .size = meta_size, |
| 985 | .data = meta, |
| 986 | }, |
| 987 | }; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 988 | struct perf_sample_data *sd; |
| 989 | struct pt_regs *regs; |
| 990 | u64 ret; |
| 991 | |
| 992 | if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) { |
| 993 | ret = -EBUSY; |
| 994 | goto out; |
| 995 | } |
| 996 | sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]); |
| 997 | regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 998 | |
| 999 | perf_fetch_caller_regs(regs); |
| 1000 | perf_sample_data_init(sd, 0, 0); |
| 1001 | sd->raw = &raw; |
| 1002 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1003 | ret = __bpf_perf_event_output(regs, map, flags, sd); |
| 1004 | out: |
| 1005 | this_cpu_dec(bpf_event_output_nest_level); |
| 1006 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | BPF_CALL_0(bpf_get_current_task) |
| 1010 | { |
| 1011 | return (long) current; |
| 1012 | } |
| 1013 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1014 | const struct bpf_func_proto bpf_get_current_task_proto = { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1015 | .func = bpf_get_current_task, |
| 1016 | .gpl_only = true, |
| 1017 | .ret_type = RET_INTEGER, |
| 1018 | }; |
| 1019 | |
| 1020 | BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx) |
| 1021 | { |
| 1022 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 1023 | struct cgroup *cgrp; |
| 1024 | |
| 1025 | if (unlikely(idx >= array->map.max_entries)) |
| 1026 | return -E2BIG; |
| 1027 | |
| 1028 | cgrp = READ_ONCE(array->ptrs[idx]); |
| 1029 | if (unlikely(!cgrp)) |
| 1030 | return -EAGAIN; |
| 1031 | |
| 1032 | return task_under_cgroup_hierarchy(current, cgrp); |
| 1033 | } |
| 1034 | |
| 1035 | static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = { |
| 1036 | .func = bpf_current_task_under_cgroup, |
| 1037 | .gpl_only = false, |
| 1038 | .ret_type = RET_INTEGER, |
| 1039 | .arg1_type = ARG_CONST_MAP_PTR, |
| 1040 | .arg2_type = ARG_ANYTHING, |
| 1041 | }; |
| 1042 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1043 | struct send_signal_irq_work { |
| 1044 | struct irq_work irq_work; |
| 1045 | struct task_struct *task; |
| 1046 | u32 sig; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1047 | enum pid_type type; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1048 | }; |
| 1049 | |
| 1050 | static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work); |
| 1051 | |
| 1052 | static void do_bpf_send_signal(struct irq_work *entry) |
| 1053 | { |
| 1054 | struct send_signal_irq_work *work; |
| 1055 | |
| 1056 | work = container_of(entry, struct send_signal_irq_work, irq_work); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1057 | group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1060 | static int bpf_send_signal_common(u32 sig, enum pid_type type) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1061 | { |
| 1062 | struct send_signal_irq_work *work = NULL; |
| 1063 | |
| 1064 | /* Similar to bpf_probe_write_user, task needs to be |
| 1065 | * in a sound condition and kernel memory access be |
| 1066 | * permitted in order to send signal to the current |
| 1067 | * task. |
| 1068 | */ |
| 1069 | if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING))) |
| 1070 | return -EPERM; |
| 1071 | if (unlikely(uaccess_kernel())) |
| 1072 | return -EPERM; |
| 1073 | if (unlikely(!nmi_uaccess_okay())) |
| 1074 | return -EPERM; |
| 1075 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1076 | if (irqs_disabled()) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1077 | /* Do an early check on signal validity. Otherwise, |
| 1078 | * the error is lost in deferred irq_work. |
| 1079 | */ |
| 1080 | if (unlikely(!valid_signal(sig))) |
| 1081 | return -EINVAL; |
| 1082 | |
| 1083 | work = this_cpu_ptr(&send_signal_work); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1084 | if (atomic_read(&work->irq_work.flags) & IRQ_WORK_BUSY) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1085 | return -EBUSY; |
| 1086 | |
| 1087 | /* Add the current task, which is the target of sending signal, |
| 1088 | * to the irq_work. The current task may change when queued |
| 1089 | * irq works get executed. |
| 1090 | */ |
| 1091 | work->task = current; |
| 1092 | work->sig = sig; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1093 | work->type = type; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1094 | irq_work_queue(&work->irq_work); |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1098 | return group_send_sig_info(sig, SEND_SIG_PRIV, current, type); |
| 1099 | } |
| 1100 | |
| 1101 | BPF_CALL_1(bpf_send_signal, u32, sig) |
| 1102 | { |
| 1103 | return bpf_send_signal_common(sig, PIDTYPE_TGID); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | static const struct bpf_func_proto bpf_send_signal_proto = { |
| 1107 | .func = bpf_send_signal, |
| 1108 | .gpl_only = false, |
| 1109 | .ret_type = RET_INTEGER, |
| 1110 | .arg1_type = ARG_ANYTHING, |
| 1111 | }; |
| 1112 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1113 | BPF_CALL_1(bpf_send_signal_thread, u32, sig) |
| 1114 | { |
| 1115 | return bpf_send_signal_common(sig, PIDTYPE_PID); |
| 1116 | } |
| 1117 | |
| 1118 | static const struct bpf_func_proto bpf_send_signal_thread_proto = { |
| 1119 | .func = bpf_send_signal_thread, |
| 1120 | .gpl_only = false, |
| 1121 | .ret_type = RET_INTEGER, |
| 1122 | .arg1_type = ARG_ANYTHING, |
| 1123 | }; |
| 1124 | |
| 1125 | BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz) |
| 1126 | { |
| 1127 | long len; |
| 1128 | char *p; |
| 1129 | |
| 1130 | if (!sz) |
| 1131 | return 0; |
| 1132 | |
| 1133 | p = d_path(path, buf, sz); |
| 1134 | if (IS_ERR(p)) { |
| 1135 | len = PTR_ERR(p); |
| 1136 | } else { |
| 1137 | len = buf + sz - p; |
| 1138 | memmove(buf, p, len); |
| 1139 | } |
| 1140 | |
| 1141 | return len; |
| 1142 | } |
| 1143 | |
| 1144 | BTF_SET_START(btf_allowlist_d_path) |
| 1145 | #ifdef CONFIG_SECURITY |
| 1146 | BTF_ID(func, security_file_permission) |
| 1147 | BTF_ID(func, security_inode_getattr) |
| 1148 | BTF_ID(func, security_file_open) |
| 1149 | #endif |
| 1150 | #ifdef CONFIG_SECURITY_PATH |
| 1151 | BTF_ID(func, security_path_truncate) |
| 1152 | #endif |
| 1153 | BTF_ID(func, vfs_truncate) |
| 1154 | BTF_ID(func, vfs_fallocate) |
| 1155 | BTF_ID(func, dentry_open) |
| 1156 | BTF_ID(func, vfs_getattr) |
| 1157 | BTF_ID(func, filp_close) |
| 1158 | BTF_SET_END(btf_allowlist_d_path) |
| 1159 | |
| 1160 | static bool bpf_d_path_allowed(const struct bpf_prog *prog) |
| 1161 | { |
| 1162 | return btf_id_set_contains(&btf_allowlist_d_path, prog->aux->attach_btf_id); |
| 1163 | } |
| 1164 | |
| 1165 | BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path) |
| 1166 | |
| 1167 | static const struct bpf_func_proto bpf_d_path_proto = { |
| 1168 | .func = bpf_d_path, |
| 1169 | .gpl_only = false, |
| 1170 | .ret_type = RET_INTEGER, |
| 1171 | .arg1_type = ARG_PTR_TO_BTF_ID, |
| 1172 | .arg1_btf_id = &bpf_d_path_btf_ids[0], |
| 1173 | .arg2_type = ARG_PTR_TO_MEM, |
| 1174 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, |
| 1175 | .allowed = bpf_d_path_allowed, |
| 1176 | }; |
| 1177 | |
| 1178 | #define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \ |
| 1179 | BTF_F_PTR_RAW | BTF_F_ZERO) |
| 1180 | |
| 1181 | static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size, |
| 1182 | u64 flags, const struct btf **btf, |
| 1183 | s32 *btf_id) |
| 1184 | { |
| 1185 | const struct btf_type *t; |
| 1186 | |
| 1187 | if (unlikely(flags & ~(BTF_F_ALL))) |
| 1188 | return -EINVAL; |
| 1189 | |
| 1190 | if (btf_ptr_size != sizeof(struct btf_ptr)) |
| 1191 | return -EINVAL; |
| 1192 | |
| 1193 | *btf = bpf_get_btf_vmlinux(); |
| 1194 | |
| 1195 | if (IS_ERR_OR_NULL(*btf)) |
| 1196 | return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL; |
| 1197 | |
| 1198 | if (ptr->type_id > 0) |
| 1199 | *btf_id = ptr->type_id; |
| 1200 | else |
| 1201 | return -EINVAL; |
| 1202 | |
| 1203 | if (*btf_id > 0) |
| 1204 | t = btf_type_by_id(*btf, *btf_id); |
| 1205 | if (*btf_id <= 0 || !t) |
| 1206 | return -ENOENT; |
| 1207 | |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
| 1211 | BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr, |
| 1212 | u32, btf_ptr_size, u64, flags) |
| 1213 | { |
| 1214 | const struct btf *btf; |
| 1215 | s32 btf_id; |
| 1216 | int ret; |
| 1217 | |
| 1218 | ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id); |
| 1219 | if (ret) |
| 1220 | return ret; |
| 1221 | |
| 1222 | return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size, |
| 1223 | flags); |
| 1224 | } |
| 1225 | |
| 1226 | const struct bpf_func_proto bpf_snprintf_btf_proto = { |
| 1227 | .func = bpf_snprintf_btf, |
| 1228 | .gpl_only = false, |
| 1229 | .ret_type = RET_INTEGER, |
| 1230 | .arg1_type = ARG_PTR_TO_MEM, |
| 1231 | .arg2_type = ARG_CONST_SIZE, |
| 1232 | .arg3_type = ARG_PTR_TO_MEM, |
| 1233 | .arg4_type = ARG_CONST_SIZE, |
| 1234 | .arg5_type = ARG_ANYTHING, |
| 1235 | }; |
| 1236 | |
| 1237 | const struct bpf_func_proto * |
| 1238 | bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1239 | { |
| 1240 | switch (func_id) { |
| 1241 | case BPF_FUNC_map_lookup_elem: |
| 1242 | return &bpf_map_lookup_elem_proto; |
| 1243 | case BPF_FUNC_map_update_elem: |
| 1244 | return &bpf_map_update_elem_proto; |
| 1245 | case BPF_FUNC_map_delete_elem: |
| 1246 | return &bpf_map_delete_elem_proto; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1247 | case BPF_FUNC_map_push_elem: |
| 1248 | return &bpf_map_push_elem_proto; |
| 1249 | case BPF_FUNC_map_pop_elem: |
| 1250 | return &bpf_map_pop_elem_proto; |
| 1251 | case BPF_FUNC_map_peek_elem: |
| 1252 | return &bpf_map_peek_elem_proto; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1253 | case BPF_FUNC_ktime_get_ns: |
| 1254 | return &bpf_ktime_get_ns_proto; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1255 | case BPF_FUNC_ktime_get_boot_ns: |
| 1256 | return &bpf_ktime_get_boot_ns_proto; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1257 | case BPF_FUNC_tail_call: |
| 1258 | return &bpf_tail_call_proto; |
| 1259 | case BPF_FUNC_get_current_pid_tgid: |
| 1260 | return &bpf_get_current_pid_tgid_proto; |
| 1261 | case BPF_FUNC_get_current_task: |
| 1262 | return &bpf_get_current_task_proto; |
| 1263 | case BPF_FUNC_get_current_uid_gid: |
| 1264 | return &bpf_get_current_uid_gid_proto; |
| 1265 | case BPF_FUNC_get_current_comm: |
| 1266 | return &bpf_get_current_comm_proto; |
| 1267 | case BPF_FUNC_trace_printk: |
| 1268 | return bpf_get_trace_printk_proto(); |
| 1269 | case BPF_FUNC_get_smp_processor_id: |
| 1270 | return &bpf_get_smp_processor_id_proto; |
| 1271 | case BPF_FUNC_get_numa_node_id: |
| 1272 | return &bpf_get_numa_node_id_proto; |
| 1273 | case BPF_FUNC_perf_event_read: |
| 1274 | return &bpf_perf_event_read_proto; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1275 | case BPF_FUNC_current_task_under_cgroup: |
| 1276 | return &bpf_current_task_under_cgroup_proto; |
| 1277 | case BPF_FUNC_get_prandom_u32: |
| 1278 | return &bpf_get_prandom_u32_proto; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1279 | case BPF_FUNC_probe_write_user: |
| 1280 | return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ? |
| 1281 | NULL : bpf_get_probe_write_proto(); |
| 1282 | case BPF_FUNC_probe_read_user: |
| 1283 | return &bpf_probe_read_user_proto; |
| 1284 | case BPF_FUNC_probe_read_kernel: |
| 1285 | return security_locked_down(LOCKDOWN_BPF_READ) < 0 ? |
| 1286 | NULL : &bpf_probe_read_kernel_proto; |
| 1287 | case BPF_FUNC_probe_read_user_str: |
| 1288 | return &bpf_probe_read_user_str_proto; |
| 1289 | case BPF_FUNC_probe_read_kernel_str: |
| 1290 | return security_locked_down(LOCKDOWN_BPF_READ) < 0 ? |
| 1291 | NULL : &bpf_probe_read_kernel_str_proto; |
| 1292 | #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE |
| 1293 | case BPF_FUNC_probe_read: |
| 1294 | return security_locked_down(LOCKDOWN_BPF_READ) < 0 ? |
| 1295 | NULL : &bpf_probe_read_compat_proto; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1296 | case BPF_FUNC_probe_read_str: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1297 | return security_locked_down(LOCKDOWN_BPF_READ) < 0 ? |
| 1298 | NULL : &bpf_probe_read_compat_str_proto; |
| 1299 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1300 | #ifdef CONFIG_CGROUPS |
| 1301 | case BPF_FUNC_get_current_cgroup_id: |
| 1302 | return &bpf_get_current_cgroup_id_proto; |
| 1303 | #endif |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1304 | case BPF_FUNC_send_signal: |
| 1305 | return &bpf_send_signal_proto; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1306 | case BPF_FUNC_send_signal_thread: |
| 1307 | return &bpf_send_signal_thread_proto; |
| 1308 | case BPF_FUNC_perf_event_read_value: |
| 1309 | return &bpf_perf_event_read_value_proto; |
| 1310 | case BPF_FUNC_get_ns_current_pid_tgid: |
| 1311 | return &bpf_get_ns_current_pid_tgid_proto; |
| 1312 | case BPF_FUNC_ringbuf_output: |
| 1313 | return &bpf_ringbuf_output_proto; |
| 1314 | case BPF_FUNC_ringbuf_reserve: |
| 1315 | return &bpf_ringbuf_reserve_proto; |
| 1316 | case BPF_FUNC_ringbuf_submit: |
| 1317 | return &bpf_ringbuf_submit_proto; |
| 1318 | case BPF_FUNC_ringbuf_discard: |
| 1319 | return &bpf_ringbuf_discard_proto; |
| 1320 | case BPF_FUNC_ringbuf_query: |
| 1321 | return &bpf_ringbuf_query_proto; |
| 1322 | case BPF_FUNC_jiffies64: |
| 1323 | return &bpf_jiffies64_proto; |
| 1324 | case BPF_FUNC_get_task_stack: |
| 1325 | return &bpf_get_task_stack_proto; |
| 1326 | case BPF_FUNC_copy_from_user: |
| 1327 | return prog->aux->sleepable ? &bpf_copy_from_user_proto : NULL; |
| 1328 | case BPF_FUNC_snprintf_btf: |
| 1329 | return &bpf_snprintf_btf_proto; |
| 1330 | case BPF_FUNC_per_cpu_ptr: |
| 1331 | return &bpf_per_cpu_ptr_proto; |
| 1332 | case BPF_FUNC_this_cpu_ptr: |
| 1333 | return &bpf_this_cpu_ptr_proto; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1334 | default: |
| 1335 | return NULL; |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | static const struct bpf_func_proto * |
| 1340 | kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
| 1341 | { |
| 1342 | switch (func_id) { |
| 1343 | case BPF_FUNC_perf_event_output: |
| 1344 | return &bpf_perf_event_output_proto; |
| 1345 | case BPF_FUNC_get_stackid: |
| 1346 | return &bpf_get_stackid_proto; |
| 1347 | case BPF_FUNC_get_stack: |
| 1348 | return &bpf_get_stack_proto; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1349 | #ifdef CONFIG_BPF_KPROBE_OVERRIDE |
| 1350 | case BPF_FUNC_override_return: |
| 1351 | return &bpf_override_return_proto; |
| 1352 | #endif |
| 1353 | default: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1354 | return bpf_tracing_func_proto(func_id, prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | /* bpf+kprobe programs can access fields of 'struct pt_regs' */ |
| 1359 | static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type, |
| 1360 | const struct bpf_prog *prog, |
| 1361 | struct bpf_insn_access_aux *info) |
| 1362 | { |
| 1363 | if (off < 0 || off >= sizeof(struct pt_regs)) |
| 1364 | return false; |
| 1365 | if (type != BPF_READ) |
| 1366 | return false; |
| 1367 | if (off % size != 0) |
| 1368 | return false; |
| 1369 | /* |
| 1370 | * Assertion for 32 bit to make sure last 8 byte access |
| 1371 | * (BPF_DW) to the last 4 byte member is disallowed. |
| 1372 | */ |
| 1373 | if (off + size > sizeof(struct pt_regs)) |
| 1374 | return false; |
| 1375 | |
| 1376 | return true; |
| 1377 | } |
| 1378 | |
| 1379 | const struct bpf_verifier_ops kprobe_verifier_ops = { |
| 1380 | .get_func_proto = kprobe_prog_func_proto, |
| 1381 | .is_valid_access = kprobe_prog_is_valid_access, |
| 1382 | }; |
| 1383 | |
| 1384 | const struct bpf_prog_ops kprobe_prog_ops = { |
| 1385 | }; |
| 1386 | |
| 1387 | BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map, |
| 1388 | u64, flags, void *, data, u64, size) |
| 1389 | { |
| 1390 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
| 1391 | |
| 1392 | /* |
| 1393 | * r1 points to perf tracepoint buffer where first 8 bytes are hidden |
| 1394 | * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it |
| 1395 | * from there and call the same bpf_perf_event_output() helper inline. |
| 1396 | */ |
| 1397 | return ____bpf_perf_event_output(regs, map, flags, data, size); |
| 1398 | } |
| 1399 | |
| 1400 | static const struct bpf_func_proto bpf_perf_event_output_proto_tp = { |
| 1401 | .func = bpf_perf_event_output_tp, |
| 1402 | .gpl_only = true, |
| 1403 | .ret_type = RET_INTEGER, |
| 1404 | .arg1_type = ARG_PTR_TO_CTX, |
| 1405 | .arg2_type = ARG_CONST_MAP_PTR, |
| 1406 | .arg3_type = ARG_ANYTHING, |
| 1407 | .arg4_type = ARG_PTR_TO_MEM, |
| 1408 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
| 1409 | }; |
| 1410 | |
| 1411 | BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map, |
| 1412 | u64, flags) |
| 1413 | { |
| 1414 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
| 1415 | |
| 1416 | /* |
| 1417 | * Same comment as in bpf_perf_event_output_tp(), only that this time |
| 1418 | * the other helper's function body cannot be inlined due to being |
| 1419 | * external, thus we need to call raw helper function. |
| 1420 | */ |
| 1421 | return bpf_get_stackid((unsigned long) regs, (unsigned long) map, |
| 1422 | flags, 0, 0); |
| 1423 | } |
| 1424 | |
| 1425 | static const struct bpf_func_proto bpf_get_stackid_proto_tp = { |
| 1426 | .func = bpf_get_stackid_tp, |
| 1427 | .gpl_only = true, |
| 1428 | .ret_type = RET_INTEGER, |
| 1429 | .arg1_type = ARG_PTR_TO_CTX, |
| 1430 | .arg2_type = ARG_CONST_MAP_PTR, |
| 1431 | .arg3_type = ARG_ANYTHING, |
| 1432 | }; |
| 1433 | |
| 1434 | BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size, |
| 1435 | u64, flags) |
| 1436 | { |
| 1437 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
| 1438 | |
| 1439 | return bpf_get_stack((unsigned long) regs, (unsigned long) buf, |
| 1440 | (unsigned long) size, flags, 0); |
| 1441 | } |
| 1442 | |
| 1443 | static const struct bpf_func_proto bpf_get_stack_proto_tp = { |
| 1444 | .func = bpf_get_stack_tp, |
| 1445 | .gpl_only = true, |
| 1446 | .ret_type = RET_INTEGER, |
| 1447 | .arg1_type = ARG_PTR_TO_CTX, |
| 1448 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, |
| 1449 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, |
| 1450 | .arg4_type = ARG_ANYTHING, |
| 1451 | }; |
| 1452 | |
| 1453 | static const struct bpf_func_proto * |
| 1454 | tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
| 1455 | { |
| 1456 | switch (func_id) { |
| 1457 | case BPF_FUNC_perf_event_output: |
| 1458 | return &bpf_perf_event_output_proto_tp; |
| 1459 | case BPF_FUNC_get_stackid: |
| 1460 | return &bpf_get_stackid_proto_tp; |
| 1461 | case BPF_FUNC_get_stack: |
| 1462 | return &bpf_get_stack_proto_tp; |
| 1463 | default: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1464 | return bpf_tracing_func_proto(func_id, prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type, |
| 1469 | const struct bpf_prog *prog, |
| 1470 | struct bpf_insn_access_aux *info) |
| 1471 | { |
| 1472 | if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE) |
| 1473 | return false; |
| 1474 | if (type != BPF_READ) |
| 1475 | return false; |
| 1476 | if (off % size != 0) |
| 1477 | return false; |
| 1478 | |
| 1479 | BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64)); |
| 1480 | return true; |
| 1481 | } |
| 1482 | |
| 1483 | const struct bpf_verifier_ops tracepoint_verifier_ops = { |
| 1484 | .get_func_proto = tp_prog_func_proto, |
| 1485 | .is_valid_access = tp_prog_is_valid_access, |
| 1486 | }; |
| 1487 | |
| 1488 | const struct bpf_prog_ops tracepoint_prog_ops = { |
| 1489 | }; |
| 1490 | |
| 1491 | BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx, |
| 1492 | struct bpf_perf_event_value *, buf, u32, size) |
| 1493 | { |
| 1494 | int err = -EINVAL; |
| 1495 | |
| 1496 | if (unlikely(size != sizeof(struct bpf_perf_event_value))) |
| 1497 | goto clear; |
| 1498 | err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled, |
| 1499 | &buf->running); |
| 1500 | if (unlikely(err)) |
| 1501 | goto clear; |
| 1502 | return 0; |
| 1503 | clear: |
| 1504 | memset(buf, 0, size); |
| 1505 | return err; |
| 1506 | } |
| 1507 | |
| 1508 | static const struct bpf_func_proto bpf_perf_prog_read_value_proto = { |
| 1509 | .func = bpf_perf_prog_read_value, |
| 1510 | .gpl_only = true, |
| 1511 | .ret_type = RET_INTEGER, |
| 1512 | .arg1_type = ARG_PTR_TO_CTX, |
| 1513 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, |
| 1514 | .arg3_type = ARG_CONST_SIZE, |
| 1515 | }; |
| 1516 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1517 | BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx, |
| 1518 | void *, buf, u32, size, u64, flags) |
| 1519 | { |
| 1520 | static const u32 br_entry_size = sizeof(struct perf_branch_entry); |
| 1521 | struct perf_branch_stack *br_stack = ctx->data->br_stack; |
| 1522 | u32 to_copy; |
| 1523 | |
| 1524 | if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE)) |
| 1525 | return -EINVAL; |
| 1526 | |
| 1527 | if (unlikely(!br_stack)) |
| 1528 | return -ENOENT; |
| 1529 | |
| 1530 | if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE) |
| 1531 | return br_stack->nr * br_entry_size; |
| 1532 | |
| 1533 | if (!buf || (size % br_entry_size != 0)) |
| 1534 | return -EINVAL; |
| 1535 | |
| 1536 | to_copy = min_t(u32, br_stack->nr * br_entry_size, size); |
| 1537 | memcpy(buf, br_stack->entries, to_copy); |
| 1538 | |
| 1539 | return to_copy; |
| 1540 | } |
| 1541 | |
| 1542 | static const struct bpf_func_proto bpf_read_branch_records_proto = { |
| 1543 | .func = bpf_read_branch_records, |
| 1544 | .gpl_only = true, |
| 1545 | .ret_type = RET_INTEGER, |
| 1546 | .arg1_type = ARG_PTR_TO_CTX, |
| 1547 | .arg2_type = ARG_PTR_TO_MEM_OR_NULL, |
| 1548 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, |
| 1549 | .arg4_type = ARG_ANYTHING, |
| 1550 | }; |
| 1551 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1552 | static const struct bpf_func_proto * |
| 1553 | pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
| 1554 | { |
| 1555 | switch (func_id) { |
| 1556 | case BPF_FUNC_perf_event_output: |
| 1557 | return &bpf_perf_event_output_proto_tp; |
| 1558 | case BPF_FUNC_get_stackid: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1559 | return &bpf_get_stackid_proto_pe; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1560 | case BPF_FUNC_get_stack: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1561 | return &bpf_get_stack_proto_pe; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1562 | case BPF_FUNC_perf_prog_read_value: |
| 1563 | return &bpf_perf_prog_read_value_proto; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1564 | case BPF_FUNC_read_branch_records: |
| 1565 | return &bpf_read_branch_records_proto; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1566 | default: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1567 | return bpf_tracing_func_proto(func_id, prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | /* |
| 1572 | * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp |
| 1573 | * to avoid potential recursive reuse issue when/if tracepoints are added |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1574 | * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack. |
| 1575 | * |
| 1576 | * Since raw tracepoints run despite bpf_prog_active, support concurrent usage |
| 1577 | * in normal, irq, and nmi context. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1578 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1579 | struct bpf_raw_tp_regs { |
| 1580 | struct pt_regs regs[3]; |
| 1581 | }; |
| 1582 | static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs); |
| 1583 | static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level); |
| 1584 | static struct pt_regs *get_bpf_raw_tp_regs(void) |
| 1585 | { |
| 1586 | struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs); |
| 1587 | int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level); |
| 1588 | |
| 1589 | if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) { |
| 1590 | this_cpu_dec(bpf_raw_tp_nest_level); |
| 1591 | return ERR_PTR(-EBUSY); |
| 1592 | } |
| 1593 | |
| 1594 | return &tp_regs->regs[nest_level - 1]; |
| 1595 | } |
| 1596 | |
| 1597 | static void put_bpf_raw_tp_regs(void) |
| 1598 | { |
| 1599 | this_cpu_dec(bpf_raw_tp_nest_level); |
| 1600 | } |
| 1601 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1602 | BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args, |
| 1603 | struct bpf_map *, map, u64, flags, void *, data, u64, size) |
| 1604 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1605 | struct pt_regs *regs = get_bpf_raw_tp_regs(); |
| 1606 | int ret; |
| 1607 | |
| 1608 | if (IS_ERR(regs)) |
| 1609 | return PTR_ERR(regs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1610 | |
| 1611 | perf_fetch_caller_regs(regs); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1612 | ret = ____bpf_perf_event_output(regs, map, flags, data, size); |
| 1613 | |
| 1614 | put_bpf_raw_tp_regs(); |
| 1615 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = { |
| 1619 | .func = bpf_perf_event_output_raw_tp, |
| 1620 | .gpl_only = true, |
| 1621 | .ret_type = RET_INTEGER, |
| 1622 | .arg1_type = ARG_PTR_TO_CTX, |
| 1623 | .arg2_type = ARG_CONST_MAP_PTR, |
| 1624 | .arg3_type = ARG_ANYTHING, |
| 1625 | .arg4_type = ARG_PTR_TO_MEM, |
| 1626 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
| 1627 | }; |
| 1628 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1629 | extern const struct bpf_func_proto bpf_skb_output_proto; |
| 1630 | extern const struct bpf_func_proto bpf_xdp_output_proto; |
| 1631 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1632 | BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args, |
| 1633 | struct bpf_map *, map, u64, flags) |
| 1634 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1635 | struct pt_regs *regs = get_bpf_raw_tp_regs(); |
| 1636 | int ret; |
| 1637 | |
| 1638 | if (IS_ERR(regs)) |
| 1639 | return PTR_ERR(regs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1640 | |
| 1641 | perf_fetch_caller_regs(regs); |
| 1642 | /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1643 | ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map, |
| 1644 | flags, 0, 0); |
| 1645 | put_bpf_raw_tp_regs(); |
| 1646 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = { |
| 1650 | .func = bpf_get_stackid_raw_tp, |
| 1651 | .gpl_only = true, |
| 1652 | .ret_type = RET_INTEGER, |
| 1653 | .arg1_type = ARG_PTR_TO_CTX, |
| 1654 | .arg2_type = ARG_CONST_MAP_PTR, |
| 1655 | .arg3_type = ARG_ANYTHING, |
| 1656 | }; |
| 1657 | |
| 1658 | BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args, |
| 1659 | void *, buf, u32, size, u64, flags) |
| 1660 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1661 | struct pt_regs *regs = get_bpf_raw_tp_regs(); |
| 1662 | int ret; |
| 1663 | |
| 1664 | if (IS_ERR(regs)) |
| 1665 | return PTR_ERR(regs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1666 | |
| 1667 | perf_fetch_caller_regs(regs); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1668 | ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf, |
| 1669 | (unsigned long) size, flags, 0); |
| 1670 | put_bpf_raw_tp_regs(); |
| 1671 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { |
| 1675 | .func = bpf_get_stack_raw_tp, |
| 1676 | .gpl_only = true, |
| 1677 | .ret_type = RET_INTEGER, |
| 1678 | .arg1_type = ARG_PTR_TO_CTX, |
| 1679 | .arg2_type = ARG_PTR_TO_MEM, |
| 1680 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, |
| 1681 | .arg4_type = ARG_ANYTHING, |
| 1682 | }; |
| 1683 | |
| 1684 | static const struct bpf_func_proto * |
| 1685 | raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
| 1686 | { |
| 1687 | switch (func_id) { |
| 1688 | case BPF_FUNC_perf_event_output: |
| 1689 | return &bpf_perf_event_output_proto_raw_tp; |
| 1690 | case BPF_FUNC_get_stackid: |
| 1691 | return &bpf_get_stackid_proto_raw_tp; |
| 1692 | case BPF_FUNC_get_stack: |
| 1693 | return &bpf_get_stack_proto_raw_tp; |
| 1694 | default: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1695 | return bpf_tracing_func_proto(func_id, prog); |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | const struct bpf_func_proto * |
| 1700 | tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
| 1701 | { |
| 1702 | switch (func_id) { |
| 1703 | #ifdef CONFIG_NET |
| 1704 | case BPF_FUNC_skb_output: |
| 1705 | return &bpf_skb_output_proto; |
| 1706 | case BPF_FUNC_xdp_output: |
| 1707 | return &bpf_xdp_output_proto; |
| 1708 | case BPF_FUNC_skc_to_tcp6_sock: |
| 1709 | return &bpf_skc_to_tcp6_sock_proto; |
| 1710 | case BPF_FUNC_skc_to_tcp_sock: |
| 1711 | return &bpf_skc_to_tcp_sock_proto; |
| 1712 | case BPF_FUNC_skc_to_tcp_timewait_sock: |
| 1713 | return &bpf_skc_to_tcp_timewait_sock_proto; |
| 1714 | case BPF_FUNC_skc_to_tcp_request_sock: |
| 1715 | return &bpf_skc_to_tcp_request_sock_proto; |
| 1716 | case BPF_FUNC_skc_to_udp6_sock: |
| 1717 | return &bpf_skc_to_udp6_sock_proto; |
| 1718 | #endif |
| 1719 | case BPF_FUNC_seq_printf: |
| 1720 | return prog->expected_attach_type == BPF_TRACE_ITER ? |
| 1721 | &bpf_seq_printf_proto : |
| 1722 | NULL; |
| 1723 | case BPF_FUNC_seq_write: |
| 1724 | return prog->expected_attach_type == BPF_TRACE_ITER ? |
| 1725 | &bpf_seq_write_proto : |
| 1726 | NULL; |
| 1727 | case BPF_FUNC_seq_printf_btf: |
| 1728 | return prog->expected_attach_type == BPF_TRACE_ITER ? |
| 1729 | &bpf_seq_printf_btf_proto : |
| 1730 | NULL; |
| 1731 | case BPF_FUNC_d_path: |
| 1732 | return &bpf_d_path_proto; |
| 1733 | default: |
| 1734 | return raw_tp_prog_func_proto(func_id, prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | static bool raw_tp_prog_is_valid_access(int off, int size, |
| 1739 | enum bpf_access_type type, |
| 1740 | const struct bpf_prog *prog, |
| 1741 | struct bpf_insn_access_aux *info) |
| 1742 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1743 | if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1744 | return false; |
| 1745 | if (type != BPF_READ) |
| 1746 | return false; |
| 1747 | if (off % size != 0) |
| 1748 | return false; |
| 1749 | return true; |
| 1750 | } |
| 1751 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1752 | static bool tracing_prog_is_valid_access(int off, int size, |
| 1753 | enum bpf_access_type type, |
| 1754 | const struct bpf_prog *prog, |
| 1755 | struct bpf_insn_access_aux *info) |
| 1756 | { |
| 1757 | if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS) |
| 1758 | return false; |
| 1759 | if (type != BPF_READ) |
| 1760 | return false; |
| 1761 | if (off % size != 0) |
| 1762 | return false; |
| 1763 | return btf_ctx_access(off, size, type, prog, info); |
| 1764 | } |
| 1765 | |
| 1766 | int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog, |
| 1767 | const union bpf_attr *kattr, |
| 1768 | union bpf_attr __user *uattr) |
| 1769 | { |
| 1770 | return -ENOTSUPP; |
| 1771 | } |
| 1772 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1773 | const struct bpf_verifier_ops raw_tracepoint_verifier_ops = { |
| 1774 | .get_func_proto = raw_tp_prog_func_proto, |
| 1775 | .is_valid_access = raw_tp_prog_is_valid_access, |
| 1776 | }; |
| 1777 | |
| 1778 | const struct bpf_prog_ops raw_tracepoint_prog_ops = { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1779 | #ifdef CONFIG_NET |
| 1780 | .test_run = bpf_prog_test_run_raw_tp, |
| 1781 | #endif |
| 1782 | }; |
| 1783 | |
| 1784 | const struct bpf_verifier_ops tracing_verifier_ops = { |
| 1785 | .get_func_proto = tracing_prog_func_proto, |
| 1786 | .is_valid_access = tracing_prog_is_valid_access, |
| 1787 | }; |
| 1788 | |
| 1789 | const struct bpf_prog_ops tracing_prog_ops = { |
| 1790 | .test_run = bpf_prog_test_run_tracing, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1791 | }; |
| 1792 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1793 | static bool raw_tp_writable_prog_is_valid_access(int off, int size, |
| 1794 | enum bpf_access_type type, |
| 1795 | const struct bpf_prog *prog, |
| 1796 | struct bpf_insn_access_aux *info) |
| 1797 | { |
| 1798 | if (off == 0) { |
| 1799 | if (size != sizeof(u64) || type != BPF_READ) |
| 1800 | return false; |
| 1801 | info->reg_type = PTR_TO_TP_BUFFER; |
| 1802 | } |
| 1803 | return raw_tp_prog_is_valid_access(off, size, type, prog, info); |
| 1804 | } |
| 1805 | |
| 1806 | const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = { |
| 1807 | .get_func_proto = raw_tp_prog_func_proto, |
| 1808 | .is_valid_access = raw_tp_writable_prog_is_valid_access, |
| 1809 | }; |
| 1810 | |
| 1811 | const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = { |
| 1812 | }; |
| 1813 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1814 | static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type, |
| 1815 | const struct bpf_prog *prog, |
| 1816 | struct bpf_insn_access_aux *info) |
| 1817 | { |
| 1818 | const int size_u64 = sizeof(u64); |
| 1819 | |
| 1820 | if (off < 0 || off >= sizeof(struct bpf_perf_event_data)) |
| 1821 | return false; |
| 1822 | if (type != BPF_READ) |
| 1823 | return false; |
| 1824 | if (off % size != 0) { |
| 1825 | if (sizeof(unsigned long) != 4) |
| 1826 | return false; |
| 1827 | if (size != 8) |
| 1828 | return false; |
| 1829 | if (off % size != 4) |
| 1830 | return false; |
| 1831 | } |
| 1832 | |
| 1833 | switch (off) { |
| 1834 | case bpf_ctx_range(struct bpf_perf_event_data, sample_period): |
| 1835 | bpf_ctx_record_field_size(info, size_u64); |
| 1836 | if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) |
| 1837 | return false; |
| 1838 | break; |
| 1839 | case bpf_ctx_range(struct bpf_perf_event_data, addr): |
| 1840 | bpf_ctx_record_field_size(info, size_u64); |
| 1841 | if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) |
| 1842 | return false; |
| 1843 | break; |
| 1844 | default: |
| 1845 | if (size != sizeof(long)) |
| 1846 | return false; |
| 1847 | } |
| 1848 | |
| 1849 | return true; |
| 1850 | } |
| 1851 | |
| 1852 | static u32 pe_prog_convert_ctx_access(enum bpf_access_type type, |
| 1853 | const struct bpf_insn *si, |
| 1854 | struct bpf_insn *insn_buf, |
| 1855 | struct bpf_prog *prog, u32 *target_size) |
| 1856 | { |
| 1857 | struct bpf_insn *insn = insn_buf; |
| 1858 | |
| 1859 | switch (si->off) { |
| 1860 | case offsetof(struct bpf_perf_event_data, sample_period): |
| 1861 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, |
| 1862 | data), si->dst_reg, si->src_reg, |
| 1863 | offsetof(struct bpf_perf_event_data_kern, data)); |
| 1864 | *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, |
| 1865 | bpf_target_off(struct perf_sample_data, period, 8, |
| 1866 | target_size)); |
| 1867 | break; |
| 1868 | case offsetof(struct bpf_perf_event_data, addr): |
| 1869 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, |
| 1870 | data), si->dst_reg, si->src_reg, |
| 1871 | offsetof(struct bpf_perf_event_data_kern, data)); |
| 1872 | *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, |
| 1873 | bpf_target_off(struct perf_sample_data, addr, 8, |
| 1874 | target_size)); |
| 1875 | break; |
| 1876 | default: |
| 1877 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, |
| 1878 | regs), si->dst_reg, si->src_reg, |
| 1879 | offsetof(struct bpf_perf_event_data_kern, regs)); |
| 1880 | *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg, |
| 1881 | si->off); |
| 1882 | break; |
| 1883 | } |
| 1884 | |
| 1885 | return insn - insn_buf; |
| 1886 | } |
| 1887 | |
| 1888 | const struct bpf_verifier_ops perf_event_verifier_ops = { |
| 1889 | .get_func_proto = pe_prog_func_proto, |
| 1890 | .is_valid_access = pe_prog_is_valid_access, |
| 1891 | .convert_ctx_access = pe_prog_convert_ctx_access, |
| 1892 | }; |
| 1893 | |
| 1894 | const struct bpf_prog_ops perf_event_prog_ops = { |
| 1895 | }; |
| 1896 | |
| 1897 | static DEFINE_MUTEX(bpf_event_mutex); |
| 1898 | |
| 1899 | #define BPF_TRACE_MAX_PROGS 64 |
| 1900 | |
| 1901 | int perf_event_attach_bpf_prog(struct perf_event *event, |
| 1902 | struct bpf_prog *prog) |
| 1903 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1904 | struct bpf_prog_array *old_array; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1905 | struct bpf_prog_array *new_array; |
| 1906 | int ret = -EEXIST; |
| 1907 | |
| 1908 | /* |
| 1909 | * Kprobe override only works if they are on the function entry, |
| 1910 | * and only if they are on the opt-in list. |
| 1911 | */ |
| 1912 | if (prog->kprobe_override && |
| 1913 | (!trace_kprobe_on_func_entry(event->tp_event) || |
| 1914 | !trace_kprobe_error_injectable(event->tp_event))) |
| 1915 | return -EINVAL; |
| 1916 | |
| 1917 | mutex_lock(&bpf_event_mutex); |
| 1918 | |
| 1919 | if (event->prog) |
| 1920 | goto unlock; |
| 1921 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1922 | old_array = bpf_event_rcu_dereference(event->tp_event->prog_array); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1923 | if (old_array && |
| 1924 | bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) { |
| 1925 | ret = -E2BIG; |
| 1926 | goto unlock; |
| 1927 | } |
| 1928 | |
| 1929 | ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array); |
| 1930 | if (ret < 0) |
| 1931 | goto unlock; |
| 1932 | |
| 1933 | /* set the new array to event->tp_event and set event->prog */ |
| 1934 | event->prog = prog; |
| 1935 | rcu_assign_pointer(event->tp_event->prog_array, new_array); |
| 1936 | bpf_prog_array_free(old_array); |
| 1937 | |
| 1938 | unlock: |
| 1939 | mutex_unlock(&bpf_event_mutex); |
| 1940 | return ret; |
| 1941 | } |
| 1942 | |
| 1943 | void perf_event_detach_bpf_prog(struct perf_event *event) |
| 1944 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1945 | struct bpf_prog_array *old_array; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1946 | struct bpf_prog_array *new_array; |
| 1947 | int ret; |
| 1948 | |
| 1949 | mutex_lock(&bpf_event_mutex); |
| 1950 | |
| 1951 | if (!event->prog) |
| 1952 | goto unlock; |
| 1953 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1954 | old_array = bpf_event_rcu_dereference(event->tp_event->prog_array); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1955 | ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array); |
| 1956 | if (ret == -ENOENT) |
| 1957 | goto unlock; |
| 1958 | if (ret < 0) { |
| 1959 | bpf_prog_array_delete_safe(old_array, event->prog); |
| 1960 | } else { |
| 1961 | rcu_assign_pointer(event->tp_event->prog_array, new_array); |
| 1962 | bpf_prog_array_free(old_array); |
| 1963 | } |
| 1964 | |
| 1965 | bpf_prog_put(event->prog); |
| 1966 | event->prog = NULL; |
| 1967 | |
| 1968 | unlock: |
| 1969 | mutex_unlock(&bpf_event_mutex); |
| 1970 | } |
| 1971 | |
| 1972 | int perf_event_query_prog_array(struct perf_event *event, void __user *info) |
| 1973 | { |
| 1974 | struct perf_event_query_bpf __user *uquery = info; |
| 1975 | struct perf_event_query_bpf query = {}; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1976 | struct bpf_prog_array *progs; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1977 | u32 *ids, prog_cnt, ids_len; |
| 1978 | int ret; |
| 1979 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1980 | if (!perfmon_capable()) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1981 | return -EPERM; |
| 1982 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 1983 | return -EINVAL; |
| 1984 | if (copy_from_user(&query, uquery, sizeof(query))) |
| 1985 | return -EFAULT; |
| 1986 | |
| 1987 | ids_len = query.ids_len; |
| 1988 | if (ids_len > BPF_TRACE_MAX_PROGS) |
| 1989 | return -E2BIG; |
| 1990 | ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN); |
| 1991 | if (!ids) |
| 1992 | return -ENOMEM; |
| 1993 | /* |
| 1994 | * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which |
| 1995 | * is required when user only wants to check for uquery->prog_cnt. |
| 1996 | * There is no need to check for it since the case is handled |
| 1997 | * gracefully in bpf_prog_array_copy_info. |
| 1998 | */ |
| 1999 | |
| 2000 | mutex_lock(&bpf_event_mutex); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2001 | progs = bpf_event_rcu_dereference(event->tp_event->prog_array); |
| 2002 | ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2003 | mutex_unlock(&bpf_event_mutex); |
| 2004 | |
| 2005 | if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) || |
| 2006 | copy_to_user(uquery->ids, ids, ids_len * sizeof(u32))) |
| 2007 | ret = -EFAULT; |
| 2008 | |
| 2009 | kfree(ids); |
| 2010 | return ret; |
| 2011 | } |
| 2012 | |
| 2013 | extern struct bpf_raw_event_map __start__bpf_raw_tp[]; |
| 2014 | extern struct bpf_raw_event_map __stop__bpf_raw_tp[]; |
| 2015 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2016 | struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2017 | { |
| 2018 | struct bpf_raw_event_map *btp = __start__bpf_raw_tp; |
| 2019 | |
| 2020 | for (; btp < __stop__bpf_raw_tp; btp++) { |
| 2021 | if (!strcmp(btp->tp->name, name)) |
| 2022 | return btp; |
| 2023 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2024 | |
| 2025 | return bpf_get_raw_tracepoint_module(name); |
| 2026 | } |
| 2027 | |
| 2028 | void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp) |
| 2029 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2030 | struct module *mod; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2031 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2032 | preempt_disable(); |
| 2033 | mod = __module_address((unsigned long)btp); |
| 2034 | module_put(mod); |
| 2035 | preempt_enable(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | static __always_inline |
| 2039 | void __bpf_trace_run(struct bpf_prog *prog, u64 *args) |
| 2040 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2041 | cant_sleep(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2042 | rcu_read_lock(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2043 | (void) BPF_PROG_RUN(prog, args); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2044 | rcu_read_unlock(); |
| 2045 | } |
| 2046 | |
| 2047 | #define UNPACK(...) __VA_ARGS__ |
| 2048 | #define REPEAT_1(FN, DL, X, ...) FN(X) |
| 2049 | #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__) |
| 2050 | #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__) |
| 2051 | #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__) |
| 2052 | #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__) |
| 2053 | #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__) |
| 2054 | #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__) |
| 2055 | #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__) |
| 2056 | #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__) |
| 2057 | #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__) |
| 2058 | #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__) |
| 2059 | #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__) |
| 2060 | #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__) |
| 2061 | |
| 2062 | #define SARG(X) u64 arg##X |
| 2063 | #define COPY(X) args[X] = arg##X |
| 2064 | |
| 2065 | #define __DL_COM (,) |
| 2066 | #define __DL_SEM (;) |
| 2067 | |
| 2068 | #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 |
| 2069 | |
| 2070 | #define BPF_TRACE_DEFN_x(x) \ |
| 2071 | void bpf_trace_run##x(struct bpf_prog *prog, \ |
| 2072 | REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \ |
| 2073 | { \ |
| 2074 | u64 args[x]; \ |
| 2075 | REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \ |
| 2076 | __bpf_trace_run(prog, args); \ |
| 2077 | } \ |
| 2078 | EXPORT_SYMBOL_GPL(bpf_trace_run##x) |
| 2079 | BPF_TRACE_DEFN_x(1); |
| 2080 | BPF_TRACE_DEFN_x(2); |
| 2081 | BPF_TRACE_DEFN_x(3); |
| 2082 | BPF_TRACE_DEFN_x(4); |
| 2083 | BPF_TRACE_DEFN_x(5); |
| 2084 | BPF_TRACE_DEFN_x(6); |
| 2085 | BPF_TRACE_DEFN_x(7); |
| 2086 | BPF_TRACE_DEFN_x(8); |
| 2087 | BPF_TRACE_DEFN_x(9); |
| 2088 | BPF_TRACE_DEFN_x(10); |
| 2089 | BPF_TRACE_DEFN_x(11); |
| 2090 | BPF_TRACE_DEFN_x(12); |
| 2091 | |
| 2092 | static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog) |
| 2093 | { |
| 2094 | struct tracepoint *tp = btp->tp; |
| 2095 | |
| 2096 | /* |
| 2097 | * check that program doesn't access arguments beyond what's |
| 2098 | * available in this tracepoint |
| 2099 | */ |
| 2100 | if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64)) |
| 2101 | return -EINVAL; |
| 2102 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2103 | if (prog->aux->max_tp_access > btp->writable_size) |
| 2104 | return -EINVAL; |
| 2105 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2106 | return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func, |
| 2107 | prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog) |
| 2111 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2112 | return __bpf_probe_register(btp, prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog) |
| 2116 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2117 | return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
| 2120 | int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id, |
| 2121 | u32 *fd_type, const char **buf, |
| 2122 | u64 *probe_offset, u64 *probe_addr) |
| 2123 | { |
| 2124 | bool is_tracepoint, is_syscall_tp; |
| 2125 | struct bpf_prog *prog; |
| 2126 | int flags, err = 0; |
| 2127 | |
| 2128 | prog = event->prog; |
| 2129 | if (!prog) |
| 2130 | return -ENOENT; |
| 2131 | |
| 2132 | /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */ |
| 2133 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) |
| 2134 | return -EOPNOTSUPP; |
| 2135 | |
| 2136 | *prog_id = prog->aux->id; |
| 2137 | flags = event->tp_event->flags; |
| 2138 | is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT; |
| 2139 | is_syscall_tp = is_syscall_trace_event(event->tp_event); |
| 2140 | |
| 2141 | if (is_tracepoint || is_syscall_tp) { |
| 2142 | *buf = is_tracepoint ? event->tp_event->tp->name |
| 2143 | : event->tp_event->name; |
| 2144 | *fd_type = BPF_FD_TYPE_TRACEPOINT; |
| 2145 | *probe_offset = 0x0; |
| 2146 | *probe_addr = 0x0; |
| 2147 | } else { |
| 2148 | /* kprobe/uprobe */ |
| 2149 | err = -EOPNOTSUPP; |
| 2150 | #ifdef CONFIG_KPROBE_EVENTS |
| 2151 | if (flags & TRACE_EVENT_FL_KPROBE) |
| 2152 | err = bpf_get_kprobe_info(event, fd_type, buf, |
| 2153 | probe_offset, probe_addr, |
| 2154 | event->attr.type == PERF_TYPE_TRACEPOINT); |
| 2155 | #endif |
| 2156 | #ifdef CONFIG_UPROBE_EVENTS |
| 2157 | if (flags & TRACE_EVENT_FL_UPROBE) |
| 2158 | err = bpf_get_uprobe_info(event, fd_type, buf, |
| 2159 | probe_offset, |
| 2160 | event->attr.type == PERF_TYPE_TRACEPOINT); |
| 2161 | #endif |
| 2162 | } |
| 2163 | |
| 2164 | return err; |
| 2165 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2166 | |
| 2167 | static int __init send_signal_irq_work_init(void) |
| 2168 | { |
| 2169 | int cpu; |
| 2170 | struct send_signal_irq_work *work; |
| 2171 | |
| 2172 | for_each_possible_cpu(cpu) { |
| 2173 | work = per_cpu_ptr(&send_signal_work, cpu); |
| 2174 | init_irq_work(&work->irq_work, do_bpf_send_signal); |
| 2175 | } |
| 2176 | return 0; |
| 2177 | } |
| 2178 | |
| 2179 | subsys_initcall(send_signal_irq_work_init); |
| 2180 | |
| 2181 | #ifdef CONFIG_MODULES |
| 2182 | static int bpf_event_notify(struct notifier_block *nb, unsigned long op, |
| 2183 | void *module) |
| 2184 | { |
| 2185 | struct bpf_trace_module *btm, *tmp; |
| 2186 | struct module *mod = module; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2187 | int ret = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2188 | |
| 2189 | if (mod->num_bpf_raw_events == 0 || |
| 2190 | (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING)) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2191 | goto out; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2192 | |
| 2193 | mutex_lock(&bpf_module_mutex); |
| 2194 | |
| 2195 | switch (op) { |
| 2196 | case MODULE_STATE_COMING: |
| 2197 | btm = kzalloc(sizeof(*btm), GFP_KERNEL); |
| 2198 | if (btm) { |
| 2199 | btm->module = module; |
| 2200 | list_add(&btm->list, &bpf_trace_modules); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2201 | } else { |
| 2202 | ret = -ENOMEM; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2203 | } |
| 2204 | break; |
| 2205 | case MODULE_STATE_GOING: |
| 2206 | list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) { |
| 2207 | if (btm->module == module) { |
| 2208 | list_del(&btm->list); |
| 2209 | kfree(btm); |
| 2210 | break; |
| 2211 | } |
| 2212 | } |
| 2213 | break; |
| 2214 | } |
| 2215 | |
| 2216 | mutex_unlock(&bpf_module_mutex); |
| 2217 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2218 | out: |
| 2219 | return notifier_from_errno(ret); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | static struct notifier_block bpf_module_nb = { |
| 2223 | .notifier_call = bpf_event_notify, |
| 2224 | }; |
| 2225 | |
| 2226 | static int __init bpf_event_init(void) |
| 2227 | { |
| 2228 | register_module_notifier(&bpf_module_nb); |
| 2229 | return 0; |
| 2230 | } |
| 2231 | |
| 2232 | fs_initcall(bpf_event_init); |
| 2233 | #endif /* CONFIG_MODULES */ |