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