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