Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016 Facebook |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of version 2 of the GNU General Public |
| 6 | * License as published by the Free Software Foundation. |
| 7 | */ |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/bpf.h> |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 12 | #include <linux/bpf_perf_event.h> |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 13 | #include <linux/filter.h> |
| 14 | #include <linux/uaccess.h> |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 15 | #include <linux/ctype.h> |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 16 | #include <linux/kprobes.h> |
Masami Hiramatsu | 540adea | 2018-01-13 02:55:03 +0900 | [diff] [blame] | 17 | #include <linux/error-injection.h> |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 18 | |
| 19 | #include "trace_probe.h" |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 20 | #include "trace.h" |
| 21 | |
Gianluca Borello | 035226b | 2017-10-26 01:47:42 +0000 | [diff] [blame] | 22 | 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^] | 23 | 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] | 24 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 25 | /** |
| 26 | * trace_call_bpf - invoke BPF program |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 27 | * @call: tracepoint event |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 28 | * @ctx: opaque context pointer |
| 29 | * |
| 30 | * kprobe handlers execute BPF programs via this helper. |
| 31 | * Can be used from static tracepoints in the future. |
| 32 | * |
| 33 | * Return: BPF programs always return an integer which is interpreted by |
| 34 | * kprobe handler as: |
| 35 | * 0 - return from kprobe (event is filtered out) |
| 36 | * 1 - store kprobe event into ring buffer |
| 37 | * Other values are reserved and currently alias to 1 |
| 38 | */ |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 39 | unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx) |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 40 | { |
| 41 | unsigned int ret; |
| 42 | |
| 43 | if (in_nmi()) /* not supported yet */ |
| 44 | return 1; |
| 45 | |
| 46 | preempt_disable(); |
| 47 | |
| 48 | if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) { |
| 49 | /* |
| 50 | * since some bpf program is already running on this cpu, |
| 51 | * don't call into another bpf program (same or different) |
| 52 | * and don't send kprobe event into ring-buffer, |
| 53 | * so return zero here |
| 54 | */ |
| 55 | ret = 0; |
| 56 | goto out; |
| 57 | } |
| 58 | |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 59 | /* |
| 60 | * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock |
| 61 | * to all call sites, we did a bpf_prog_array_valid() there to check |
| 62 | * whether call->prog_array is empty or not, which is |
| 63 | * a heurisitc to speed up execution. |
| 64 | * |
| 65 | * If bpf_prog_array_valid() fetched prog_array was |
| 66 | * non-NULL, we go into trace_call_bpf() and do the actual |
| 67 | * proper rcu_dereference() under RCU lock. |
| 68 | * If it turns out that prog_array is NULL then, we bail out. |
| 69 | * For the opposite, if the bpf_prog_array_valid() fetched pointer |
| 70 | * was NULL, you'll skip the prog_array with the risk of missing |
| 71 | * out of events when it was updated in between this and the |
| 72 | * rcu_dereference() which is accepted risk. |
| 73 | */ |
| 74 | 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] | 75 | |
| 76 | out: |
| 77 | __this_cpu_dec(bpf_prog_active); |
| 78 | preempt_enable(); |
| 79 | |
| 80 | return ret; |
| 81 | } |
| 82 | EXPORT_SYMBOL_GPL(trace_call_bpf); |
| 83 | |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 84 | #ifdef CONFIG_BPF_KPROBE_OVERRIDE |
| 85 | BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc) |
| 86 | { |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 87 | regs_set_return_value(regs, rc); |
Masami Hiramatsu | 540adea | 2018-01-13 02:55:03 +0900 | [diff] [blame] | 88 | override_function_with_return(regs); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static const struct bpf_func_proto bpf_override_return_proto = { |
| 93 | .func = bpf_override_return, |
| 94 | .gpl_only = true, |
| 95 | .ret_type = RET_INTEGER, |
| 96 | .arg1_type = ARG_PTR_TO_CTX, |
| 97 | .arg2_type = ARG_ANYTHING, |
| 98 | }; |
| 99 | #endif |
| 100 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 101 | BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr) |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 102 | { |
Gianluca Borello | eb33f2c | 2017-11-22 18:32:54 +0000 | [diff] [blame] | 103 | int ret; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 104 | |
Daniel Borkmann | 074f528e | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 105 | ret = probe_kernel_read(dst, unsafe_ptr, size); |
| 106 | if (unlikely(ret < 0)) |
| 107 | memset(dst, 0, size); |
| 108 | |
| 109 | return ret; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static const struct bpf_func_proto bpf_probe_read_proto = { |
| 113 | .func = bpf_probe_read, |
| 114 | .gpl_only = true, |
| 115 | .ret_type = RET_INTEGER, |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 116 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, |
Yonghong Song | 9c019e2 | 2017-11-12 14:49:10 -0800 | [diff] [blame] | 117 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 118 | .arg3_type = ARG_ANYTHING, |
| 119 | }; |
| 120 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 121 | BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src, |
| 122 | u32, size) |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 123 | { |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 124 | /* |
| 125 | * Ensure we're in user context which is safe for the helper to |
| 126 | * run. This helper has no business in a kthread. |
| 127 | * |
| 128 | * access_ok() should prevent writing to non-user memory, but in |
| 129 | * some situations (nommu, temporary switch, etc) access_ok() does |
| 130 | * not provide enough validation, hence the check on KERNEL_DS. |
| 131 | */ |
| 132 | |
| 133 | if (unlikely(in_interrupt() || |
| 134 | current->flags & (PF_KTHREAD | PF_EXITING))) |
| 135 | return -EPERM; |
Al Viro | db68ce1 | 2017-03-20 21:08:07 -0400 | [diff] [blame] | 136 | if (unlikely(uaccess_kernel())) |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 137 | return -EPERM; |
| 138 | if (!access_ok(VERIFY_WRITE, unsafe_ptr, size)) |
| 139 | return -EPERM; |
| 140 | |
| 141 | return probe_kernel_write(unsafe_ptr, src, size); |
| 142 | } |
| 143 | |
| 144 | static const struct bpf_func_proto bpf_probe_write_user_proto = { |
| 145 | .func = bpf_probe_write_user, |
| 146 | .gpl_only = true, |
| 147 | .ret_type = RET_INTEGER, |
| 148 | .arg1_type = ARG_ANYTHING, |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 149 | .arg2_type = ARG_PTR_TO_MEM, |
| 150 | .arg3_type = ARG_CONST_SIZE, |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | static const struct bpf_func_proto *bpf_get_probe_write_proto(void) |
| 154 | { |
| 155 | pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!", |
| 156 | current->comm, task_pid_nr(current)); |
| 157 | |
| 158 | return &bpf_probe_write_user_proto; |
| 159 | } |
| 160 | |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 161 | /* |
John Fastabend | 7bda4b4 | 2017-07-02 02:13:29 +0200 | [diff] [blame] | 162 | * Only limited trace_printk() conversion specifiers allowed: |
| 163 | * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 164 | */ |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 165 | BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1, |
| 166 | u64, arg2, u64, arg3) |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 167 | { |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 168 | bool str_seen = false; |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 169 | int mod[3] = {}; |
| 170 | int fmt_cnt = 0; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 171 | u64 unsafe_addr; |
| 172 | char buf[64]; |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 173 | int i; |
| 174 | |
| 175 | /* |
| 176 | * bpf_check()->check_func_arg()->check_stack_boundary() |
| 177 | * guarantees that fmt points to bpf program stack, |
| 178 | * fmt_size bytes of it were initialized and fmt_size > 0 |
| 179 | */ |
| 180 | if (fmt[--fmt_size] != 0) |
| 181 | return -EINVAL; |
| 182 | |
| 183 | /* check format string for allowed specifiers */ |
| 184 | for (i = 0; i < fmt_size; i++) { |
| 185 | if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) |
| 186 | return -EINVAL; |
| 187 | |
| 188 | if (fmt[i] != '%') |
| 189 | continue; |
| 190 | |
| 191 | if (fmt_cnt >= 3) |
| 192 | return -EINVAL; |
| 193 | |
| 194 | /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */ |
| 195 | i++; |
| 196 | if (fmt[i] == 'l') { |
| 197 | mod[fmt_cnt]++; |
| 198 | i++; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 199 | } else if (fmt[i] == 'p' || fmt[i] == 's') { |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 200 | mod[fmt_cnt]++; |
| 201 | i++; |
| 202 | if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0) |
| 203 | return -EINVAL; |
| 204 | fmt_cnt++; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 205 | if (fmt[i - 1] == 's') { |
| 206 | if (str_seen) |
| 207 | /* allow only one '%s' per fmt string */ |
| 208 | return -EINVAL; |
| 209 | str_seen = true; |
| 210 | |
| 211 | switch (fmt_cnt) { |
| 212 | case 1: |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 213 | unsafe_addr = arg1; |
| 214 | arg1 = (long) buf; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 215 | break; |
| 216 | case 2: |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 217 | unsafe_addr = arg2; |
| 218 | arg2 = (long) buf; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 219 | break; |
| 220 | case 3: |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 221 | unsafe_addr = arg3; |
| 222 | arg3 = (long) buf; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 223 | break; |
| 224 | } |
| 225 | buf[0] = 0; |
| 226 | strncpy_from_unsafe(buf, |
| 227 | (void *) (long) unsafe_addr, |
| 228 | sizeof(buf)); |
| 229 | } |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 230 | continue; |
| 231 | } |
| 232 | |
| 233 | if (fmt[i] == 'l') { |
| 234 | mod[fmt_cnt]++; |
| 235 | i++; |
| 236 | } |
| 237 | |
John Fastabend | 7bda4b4 | 2017-07-02 02:13:29 +0200 | [diff] [blame] | 238 | if (fmt[i] != 'i' && fmt[i] != 'd' && |
| 239 | fmt[i] != 'u' && fmt[i] != 'x') |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 240 | return -EINVAL; |
| 241 | fmt_cnt++; |
| 242 | } |
| 243 | |
Daniel Borkmann | 88a5c69 | 2017-08-16 01:45:33 +0200 | [diff] [blame] | 244 | /* Horrid workaround for getting va_list handling working with different |
| 245 | * argument type combinations generically for 32 and 64 bit archs. |
| 246 | */ |
| 247 | #define __BPF_TP_EMIT() __BPF_ARG3_TP() |
| 248 | #define __BPF_TP(...) \ |
Yonghong Song | eefa864a | 2018-01-17 09:19:32 -0800 | [diff] [blame] | 249 | __trace_printk(0 /* Fake ip */, \ |
Daniel Borkmann | 88a5c69 | 2017-08-16 01:45:33 +0200 | [diff] [blame] | 250 | fmt, ##__VA_ARGS__) |
| 251 | |
| 252 | #define __BPF_ARG1_TP(...) \ |
| 253 | ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \ |
| 254 | ? __BPF_TP(arg1, ##__VA_ARGS__) \ |
| 255 | : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \ |
| 256 | ? __BPF_TP((long)arg1, ##__VA_ARGS__) \ |
| 257 | : __BPF_TP((u32)arg1, ##__VA_ARGS__))) |
| 258 | |
| 259 | #define __BPF_ARG2_TP(...) \ |
| 260 | ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \ |
| 261 | ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \ |
| 262 | : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \ |
| 263 | ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \ |
| 264 | : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__))) |
| 265 | |
| 266 | #define __BPF_ARG3_TP(...) \ |
| 267 | ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \ |
| 268 | ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \ |
| 269 | : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \ |
| 270 | ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \ |
| 271 | : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__))) |
| 272 | |
| 273 | return __BPF_TP_EMIT(); |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static const struct bpf_func_proto bpf_trace_printk_proto = { |
| 277 | .func = bpf_trace_printk, |
| 278 | .gpl_only = true, |
| 279 | .ret_type = RET_INTEGER, |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 280 | .arg1_type = ARG_PTR_TO_MEM, |
| 281 | .arg2_type = ARG_CONST_SIZE, |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 282 | }; |
| 283 | |
Alexei Starovoitov | 0756ea3 | 2015-06-12 19:39:13 -0700 | [diff] [blame] | 284 | const struct bpf_func_proto *bpf_get_trace_printk_proto(void) |
| 285 | { |
| 286 | /* |
| 287 | * this program might be calling bpf_trace_printk, |
| 288 | * so allocate per-cpu printk buffers |
| 289 | */ |
| 290 | trace_printk_init_buffers(); |
| 291 | |
| 292 | return &bpf_trace_printk_proto; |
| 293 | } |
| 294 | |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 295 | static __always_inline int |
| 296 | get_map_perf_counter(struct bpf_map *map, u64 flags, |
| 297 | u64 *value, u64 *enabled, u64 *running) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 298 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 299 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Daniel Borkmann | 6816a7f | 2016-06-28 12:18:25 +0200 | [diff] [blame] | 300 | unsigned int cpu = smp_processor_id(); |
| 301 | u64 index = flags & BPF_F_INDEX_MASK; |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 302 | struct bpf_event_entry *ee; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 303 | |
Daniel Borkmann | 6816a7f | 2016-06-28 12:18:25 +0200 | [diff] [blame] | 304 | if (unlikely(flags & ~(BPF_F_INDEX_MASK))) |
| 305 | return -EINVAL; |
| 306 | if (index == BPF_F_CURRENT_CPU) |
| 307 | index = cpu; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 308 | if (unlikely(index >= array->map.max_entries)) |
| 309 | return -E2BIG; |
| 310 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 311 | ee = READ_ONCE(array->ptrs[index]); |
Daniel Borkmann | 1ca1cc9 | 2016-06-28 12:18:23 +0200 | [diff] [blame] | 312 | if (!ee) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 313 | return -ENOENT; |
| 314 | |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 315 | return perf_event_read_local(ee->event, value, enabled, running); |
| 316 | } |
| 317 | |
| 318 | BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags) |
| 319 | { |
| 320 | u64 value = 0; |
| 321 | int err; |
| 322 | |
| 323 | err = get_map_perf_counter(map, flags, &value, NULL, NULL); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 324 | /* |
Alexei Starovoitov | f91840a | 2017-06-02 21:03:52 -0700 | [diff] [blame] | 325 | * this api is ugly since we miss [-22..-2] range of valid |
| 326 | * counter values, but that's uapi |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 327 | */ |
Alexei Starovoitov | f91840a | 2017-06-02 21:03:52 -0700 | [diff] [blame] | 328 | if (err) |
| 329 | return err; |
| 330 | return value; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Alexei Starovoitov | 62544ce | 2015-10-22 17:10:14 -0700 | [diff] [blame] | 333 | static const struct bpf_func_proto bpf_perf_event_read_proto = { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 334 | .func = bpf_perf_event_read, |
Alexei Starovoitov | 1075ef5 | 2015-10-23 14:58:19 -0700 | [diff] [blame] | 335 | .gpl_only = true, |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 336 | .ret_type = RET_INTEGER, |
| 337 | .arg1_type = ARG_CONST_MAP_PTR, |
| 338 | .arg2_type = ARG_ANYTHING, |
| 339 | }; |
| 340 | |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 341 | BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags, |
| 342 | struct bpf_perf_event_value *, buf, u32, size) |
| 343 | { |
| 344 | int err = -EINVAL; |
| 345 | |
| 346 | if (unlikely(size != sizeof(struct bpf_perf_event_value))) |
| 347 | goto clear; |
| 348 | err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled, |
| 349 | &buf->running); |
| 350 | if (unlikely(err)) |
| 351 | goto clear; |
| 352 | return 0; |
| 353 | clear: |
| 354 | memset(buf, 0, size); |
| 355 | return err; |
| 356 | } |
| 357 | |
| 358 | static const struct bpf_func_proto bpf_perf_event_read_value_proto = { |
| 359 | .func = bpf_perf_event_read_value, |
| 360 | .gpl_only = true, |
| 361 | .ret_type = RET_INTEGER, |
| 362 | .arg1_type = ARG_CONST_MAP_PTR, |
| 363 | .arg2_type = ARG_ANYTHING, |
| 364 | .arg3_type = ARG_PTR_TO_UNINIT_MEM, |
| 365 | .arg4_type = ARG_CONST_SIZE, |
| 366 | }; |
| 367 | |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 368 | static DEFINE_PER_CPU(struct perf_sample_data, bpf_trace_sd); |
Daniel Borkmann | 20b9d7a | 2017-06-11 00:50:40 +0200 | [diff] [blame] | 369 | |
Daniel Borkmann | 8e7a392 | 2016-07-14 18:08:04 +0200 | [diff] [blame] | 370 | static __always_inline u64 |
| 371 | __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map, |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 372 | u64 flags, struct perf_sample_data *sd) |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 373 | { |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 374 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Daniel Borkmann | d793133 | 2016-06-28 12:18:24 +0200 | [diff] [blame] | 375 | unsigned int cpu = smp_processor_id(); |
Daniel Borkmann | 1e33759 | 2016-04-18 21:01:23 +0200 | [diff] [blame] | 376 | u64 index = flags & BPF_F_INDEX_MASK; |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 377 | struct bpf_event_entry *ee; |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 378 | struct perf_event *event; |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 379 | |
Daniel Borkmann | 1e33759 | 2016-04-18 21:01:23 +0200 | [diff] [blame] | 380 | if (index == BPF_F_CURRENT_CPU) |
Daniel Borkmann | d793133 | 2016-06-28 12:18:24 +0200 | [diff] [blame] | 381 | index = cpu; |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 382 | if (unlikely(index >= array->map.max_entries)) |
| 383 | return -E2BIG; |
| 384 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 385 | ee = READ_ONCE(array->ptrs[index]); |
Daniel Borkmann | 1ca1cc9 | 2016-06-28 12:18:23 +0200 | [diff] [blame] | 386 | if (!ee) |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 387 | return -ENOENT; |
| 388 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 389 | event = ee->event; |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 390 | if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE || |
| 391 | event->attr.config != PERF_COUNT_SW_BPF_OUTPUT)) |
| 392 | return -EINVAL; |
| 393 | |
Daniel Borkmann | d793133 | 2016-06-28 12:18:24 +0200 | [diff] [blame] | 394 | if (unlikely(event->oncpu != cpu)) |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 395 | return -EOPNOTSUPP; |
| 396 | |
Daniel Borkmann | 20b9d7a | 2017-06-11 00:50:40 +0200 | [diff] [blame] | 397 | perf_event_output(event, sd, regs); |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 398 | return 0; |
| 399 | } |
| 400 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 401 | BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map, |
| 402 | u64, flags, void *, data, u64, size) |
Daniel Borkmann | 8e7a392 | 2016-07-14 18:08:04 +0200 | [diff] [blame] | 403 | { |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 404 | struct perf_sample_data *sd = this_cpu_ptr(&bpf_trace_sd); |
Daniel Borkmann | 8e7a392 | 2016-07-14 18:08:04 +0200 | [diff] [blame] | 405 | struct perf_raw_record raw = { |
| 406 | .frag = { |
| 407 | .size = size, |
| 408 | .data = data, |
| 409 | }, |
| 410 | }; |
| 411 | |
| 412 | if (unlikely(flags & ~(BPF_F_INDEX_MASK))) |
| 413 | return -EINVAL; |
| 414 | |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 415 | perf_sample_data_init(sd, 0, 0); |
| 416 | sd->raw = &raw; |
| 417 | |
| 418 | return __bpf_perf_event_output(regs, map, flags, sd); |
Daniel Borkmann | 8e7a392 | 2016-07-14 18:08:04 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 421 | static const struct bpf_func_proto bpf_perf_event_output_proto = { |
| 422 | .func = bpf_perf_event_output, |
Alexei Starovoitov | 1075ef5 | 2015-10-23 14:58:19 -0700 | [diff] [blame] | 423 | .gpl_only = true, |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 424 | .ret_type = RET_INTEGER, |
| 425 | .arg1_type = ARG_PTR_TO_CTX, |
| 426 | .arg2_type = ARG_CONST_MAP_PTR, |
| 427 | .arg3_type = ARG_ANYTHING, |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 428 | .arg4_type = ARG_PTR_TO_MEM, |
Gianluca Borello | a60dd35 | 2017-11-22 18:32:56 +0000 | [diff] [blame] | 429 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 430 | }; |
| 431 | |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 432 | static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs); |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 433 | static DEFINE_PER_CPU(struct perf_sample_data, bpf_misc_sd); |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 434 | |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 435 | u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, |
| 436 | void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 437 | { |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 438 | struct perf_sample_data *sd = this_cpu_ptr(&bpf_misc_sd); |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 439 | struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs); |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 440 | struct perf_raw_frag frag = { |
| 441 | .copy = ctx_copy, |
| 442 | .size = ctx_size, |
| 443 | .data = ctx, |
| 444 | }; |
| 445 | struct perf_raw_record raw = { |
| 446 | .frag = { |
Andrew Morton | 183fc15 | 2016-07-18 15:50:58 -0700 | [diff] [blame] | 447 | { |
| 448 | .next = ctx_size ? &frag : NULL, |
| 449 | }, |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 450 | .size = meta_size, |
| 451 | .data = meta, |
| 452 | }, |
| 453 | }; |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 454 | |
| 455 | perf_fetch_caller_regs(regs); |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 456 | perf_sample_data_init(sd, 0, 0); |
| 457 | sd->raw = &raw; |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 458 | |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 459 | return __bpf_perf_event_output(regs, map, flags, sd); |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 462 | BPF_CALL_0(bpf_get_current_task) |
Alexei Starovoitov | 606274c | 2016-07-06 22:38:36 -0700 | [diff] [blame] | 463 | { |
| 464 | return (long) current; |
| 465 | } |
| 466 | |
| 467 | static const struct bpf_func_proto bpf_get_current_task_proto = { |
| 468 | .func = bpf_get_current_task, |
| 469 | .gpl_only = true, |
| 470 | .ret_type = RET_INTEGER, |
| 471 | }; |
| 472 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 473 | 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] | 474 | { |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 475 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 476 | struct cgroup *cgrp; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 477 | |
| 478 | if (unlikely(in_interrupt())) |
| 479 | return -EINVAL; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 480 | if (unlikely(idx >= array->map.max_entries)) |
| 481 | return -E2BIG; |
| 482 | |
| 483 | cgrp = READ_ONCE(array->ptrs[idx]); |
| 484 | if (unlikely(!cgrp)) |
| 485 | return -EAGAIN; |
| 486 | |
| 487 | return task_under_cgroup_hierarchy(current, cgrp); |
| 488 | } |
| 489 | |
| 490 | static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = { |
| 491 | .func = bpf_current_task_under_cgroup, |
| 492 | .gpl_only = false, |
| 493 | .ret_type = RET_INTEGER, |
| 494 | .arg1_type = ARG_CONST_MAP_PTR, |
| 495 | .arg2_type = ARG_ANYTHING, |
| 496 | }; |
| 497 | |
Gianluca Borello | a5e8c07 | 2017-01-18 17:55:49 +0000 | [diff] [blame] | 498 | BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size, |
| 499 | const void *, unsafe_ptr) |
| 500 | { |
| 501 | int ret; |
| 502 | |
| 503 | /* |
| 504 | * The strncpy_from_unsafe() call will likely not fill the entire |
| 505 | * buffer, but that's okay in this circumstance as we're probing |
| 506 | * arbitrary memory anyway similar to bpf_probe_read() and might |
| 507 | * as well probe the stack. Thus, memory is explicitly cleared |
| 508 | * only in error case, so that improper users ignoring return |
| 509 | * code altogether don't copy garbage; otherwise length of string |
| 510 | * is returned that can be used for bpf_perf_event_output() et al. |
| 511 | */ |
| 512 | ret = strncpy_from_unsafe(dst, unsafe_ptr, size); |
| 513 | if (unlikely(ret < 0)) |
| 514 | memset(dst, 0, size); |
| 515 | |
| 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | static const struct bpf_func_proto bpf_probe_read_str_proto = { |
| 520 | .func = bpf_probe_read_str, |
| 521 | .gpl_only = true, |
| 522 | .ret_type = RET_INTEGER, |
| 523 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, |
Gianluca Borello | 5c4e120 | 2017-11-22 18:32:55 +0000 | [diff] [blame] | 524 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, |
Gianluca Borello | a5e8c07 | 2017-01-18 17:55:49 +0000 | [diff] [blame] | 525 | .arg3_type = ARG_ANYTHING, |
| 526 | }; |
| 527 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 528 | static const struct bpf_func_proto * |
| 529 | 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] | 530 | { |
| 531 | switch (func_id) { |
| 532 | case BPF_FUNC_map_lookup_elem: |
| 533 | return &bpf_map_lookup_elem_proto; |
| 534 | case BPF_FUNC_map_update_elem: |
| 535 | return &bpf_map_update_elem_proto; |
| 536 | case BPF_FUNC_map_delete_elem: |
| 537 | return &bpf_map_delete_elem_proto; |
| 538 | case BPF_FUNC_probe_read: |
| 539 | return &bpf_probe_read_proto; |
Alexei Starovoitov | d9847d3 | 2015-03-25 12:49:21 -0700 | [diff] [blame] | 540 | case BPF_FUNC_ktime_get_ns: |
| 541 | return &bpf_ktime_get_ns_proto; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 542 | case BPF_FUNC_tail_call: |
| 543 | return &bpf_tail_call_proto; |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 544 | case BPF_FUNC_get_current_pid_tgid: |
| 545 | return &bpf_get_current_pid_tgid_proto; |
Alexei Starovoitov | 606274c | 2016-07-06 22:38:36 -0700 | [diff] [blame] | 546 | case BPF_FUNC_get_current_task: |
| 547 | return &bpf_get_current_task_proto; |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 548 | case BPF_FUNC_get_current_uid_gid: |
| 549 | return &bpf_get_current_uid_gid_proto; |
| 550 | case BPF_FUNC_get_current_comm: |
| 551 | return &bpf_get_current_comm_proto; |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 552 | case BPF_FUNC_trace_printk: |
Alexei Starovoitov | 0756ea3 | 2015-06-12 19:39:13 -0700 | [diff] [blame] | 553 | return bpf_get_trace_printk_proto(); |
Alexei Starovoitov | ab1973d | 2015-06-12 19:39:14 -0700 | [diff] [blame] | 554 | case BPF_FUNC_get_smp_processor_id: |
| 555 | return &bpf_get_smp_processor_id_proto; |
Daniel Borkmann | 2d0e30c | 2016-10-21 12:46:33 +0200 | [diff] [blame] | 556 | case BPF_FUNC_get_numa_node_id: |
| 557 | return &bpf_get_numa_node_id_proto; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 558 | case BPF_FUNC_perf_event_read: |
| 559 | return &bpf_perf_event_read_proto; |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 560 | case BPF_FUNC_probe_write_user: |
| 561 | return bpf_get_probe_write_proto(); |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 562 | case BPF_FUNC_current_task_under_cgroup: |
| 563 | return &bpf_current_task_under_cgroup_proto; |
Alexei Starovoitov | 8937bd8 | 2016-08-11 18:17:18 -0700 | [diff] [blame] | 564 | case BPF_FUNC_get_prandom_u32: |
| 565 | return &bpf_get_prandom_u32_proto; |
Gianluca Borello | a5e8c07 | 2017-01-18 17:55:49 +0000 | [diff] [blame] | 566 | case BPF_FUNC_probe_read_str: |
| 567 | return &bpf_probe_read_str_proto; |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 568 | default: |
| 569 | return NULL; |
| 570 | } |
| 571 | } |
| 572 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 573 | static const struct bpf_func_proto * |
| 574 | 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] | 575 | { |
| 576 | switch (func_id) { |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 577 | case BPF_FUNC_perf_event_output: |
| 578 | return &bpf_perf_event_output_proto; |
Alexei Starovoitov | d5a3b1f | 2016-02-17 19:58:58 -0800 | [diff] [blame] | 579 | case BPF_FUNC_get_stackid: |
| 580 | return &bpf_get_stackid_proto; |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame^] | 581 | case BPF_FUNC_get_stack: |
| 582 | return &bpf_get_stack_proto; |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 583 | case BPF_FUNC_perf_event_read_value: |
| 584 | return &bpf_perf_event_read_value_proto; |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 585 | #ifdef CONFIG_BPF_KPROBE_OVERRIDE |
| 586 | case BPF_FUNC_override_return: |
| 587 | return &bpf_override_return_proto; |
| 588 | #endif |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 589 | default: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 590 | return tracing_func_proto(func_id, prog); |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
| 594 | /* bpf+kprobe programs can access fields of 'struct pt_regs' */ |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 595 | 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] | 596 | const struct bpf_prog *prog, |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 597 | struct bpf_insn_access_aux *info) |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 598 | { |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 599 | if (off < 0 || off >= sizeof(struct pt_regs)) |
| 600 | return false; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 601 | if (type != BPF_READ) |
| 602 | return false; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 603 | if (off % size != 0) |
| 604 | return false; |
Daniel Borkmann | 2d071c6 | 2017-01-15 01:34:25 +0100 | [diff] [blame] | 605 | /* |
| 606 | * Assertion for 32 bit to make sure last 8 byte access |
| 607 | * (BPF_DW) to the last 4 byte member is disallowed. |
| 608 | */ |
| 609 | if (off + size > sizeof(struct pt_regs)) |
| 610 | return false; |
| 611 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 612 | return true; |
| 613 | } |
| 614 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 615 | const struct bpf_verifier_ops kprobe_verifier_ops = { |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 616 | .get_func_proto = kprobe_prog_func_proto, |
| 617 | .is_valid_access = kprobe_prog_is_valid_access, |
| 618 | }; |
| 619 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 620 | const struct bpf_prog_ops kprobe_prog_ops = { |
| 621 | }; |
| 622 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 623 | BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map, |
| 624 | u64, flags, void *, data, u64, size) |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 625 | { |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 626 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
| 627 | |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 628 | /* |
| 629 | * r1 points to perf tracepoint buffer where first 8 bytes are hidden |
| 630 | * 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] | 631 | * from there and call the same bpf_perf_event_output() helper inline. |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 632 | */ |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 633 | return ____bpf_perf_event_output(regs, map, flags, data, size); |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | static const struct bpf_func_proto bpf_perf_event_output_proto_tp = { |
| 637 | .func = bpf_perf_event_output_tp, |
| 638 | .gpl_only = true, |
| 639 | .ret_type = RET_INTEGER, |
| 640 | .arg1_type = ARG_PTR_TO_CTX, |
| 641 | .arg2_type = ARG_CONST_MAP_PTR, |
| 642 | .arg3_type = ARG_ANYTHING, |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 643 | .arg4_type = ARG_PTR_TO_MEM, |
Gianluca Borello | a60dd35 | 2017-11-22 18:32:56 +0000 | [diff] [blame] | 644 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 645 | }; |
| 646 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 647 | BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map, |
| 648 | u64, flags) |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 649 | { |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 650 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 651 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 652 | /* |
| 653 | * Same comment as in bpf_perf_event_output_tp(), only that this time |
| 654 | * the other helper's function body cannot be inlined due to being |
| 655 | * external, thus we need to call raw helper function. |
| 656 | */ |
| 657 | return bpf_get_stackid((unsigned long) regs, (unsigned long) map, |
| 658 | flags, 0, 0); |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | static const struct bpf_func_proto bpf_get_stackid_proto_tp = { |
| 662 | .func = bpf_get_stackid_tp, |
| 663 | .gpl_only = true, |
| 664 | .ret_type = RET_INTEGER, |
| 665 | .arg1_type = ARG_PTR_TO_CTX, |
| 666 | .arg2_type = ARG_CONST_MAP_PTR, |
| 667 | .arg3_type = ARG_ANYTHING, |
| 668 | }; |
| 669 | |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame^] | 670 | BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size, |
| 671 | u64, flags) |
| 672 | { |
| 673 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
| 674 | |
| 675 | return bpf_get_stack((unsigned long) regs, (unsigned long) buf, |
| 676 | (unsigned long) size, flags, 0); |
| 677 | } |
| 678 | |
| 679 | static const struct bpf_func_proto bpf_get_stack_proto_tp = { |
| 680 | .func = bpf_get_stack_tp, |
| 681 | .gpl_only = true, |
| 682 | .ret_type = RET_INTEGER, |
| 683 | .arg1_type = ARG_PTR_TO_CTX, |
| 684 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, |
| 685 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, |
| 686 | .arg4_type = ARG_ANYTHING, |
| 687 | }; |
| 688 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 689 | static const struct bpf_func_proto * |
| 690 | 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] | 691 | { |
| 692 | switch (func_id) { |
| 693 | case BPF_FUNC_perf_event_output: |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 694 | return &bpf_perf_event_output_proto_tp; |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 695 | case BPF_FUNC_get_stackid: |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 696 | return &bpf_get_stackid_proto_tp; |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame^] | 697 | case BPF_FUNC_get_stack: |
| 698 | return &bpf_get_stack_proto_tp; |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 699 | default: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 700 | return tracing_func_proto(func_id, prog); |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 704 | 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] | 705 | const struct bpf_prog *prog, |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 706 | struct bpf_insn_access_aux *info) |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 707 | { |
| 708 | if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE) |
| 709 | return false; |
| 710 | if (type != BPF_READ) |
| 711 | return false; |
| 712 | if (off % size != 0) |
| 713 | return false; |
Daniel Borkmann | 2d071c6 | 2017-01-15 01:34:25 +0100 | [diff] [blame] | 714 | |
| 715 | BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64)); |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 716 | return true; |
| 717 | } |
| 718 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 719 | const struct bpf_verifier_ops tracepoint_verifier_ops = { |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 720 | .get_func_proto = tp_prog_func_proto, |
| 721 | .is_valid_access = tp_prog_is_valid_access, |
| 722 | }; |
| 723 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 724 | const struct bpf_prog_ops tracepoint_prog_ops = { |
| 725 | }; |
| 726 | |
Yonghong Song | f005afe | 2018-03-20 11:19:17 -0700 | [diff] [blame] | 727 | BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx, |
| 728 | struct bpf_perf_event_value *, buf, u32, size) |
| 729 | { |
| 730 | int err = -EINVAL; |
| 731 | |
| 732 | if (unlikely(size != sizeof(struct bpf_perf_event_value))) |
| 733 | goto clear; |
| 734 | err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled, |
| 735 | &buf->running); |
| 736 | if (unlikely(err)) |
| 737 | goto clear; |
| 738 | return 0; |
| 739 | clear: |
| 740 | memset(buf, 0, size); |
| 741 | return err; |
| 742 | } |
| 743 | |
| 744 | static const struct bpf_func_proto bpf_perf_prog_read_value_proto = { |
| 745 | .func = bpf_perf_prog_read_value, |
| 746 | .gpl_only = true, |
| 747 | .ret_type = RET_INTEGER, |
| 748 | .arg1_type = ARG_PTR_TO_CTX, |
| 749 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, |
| 750 | .arg3_type = ARG_CONST_SIZE, |
| 751 | }; |
| 752 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 753 | static const struct bpf_func_proto * |
| 754 | 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] | 755 | { |
| 756 | switch (func_id) { |
| 757 | case BPF_FUNC_perf_event_output: |
| 758 | return &bpf_perf_event_output_proto_tp; |
| 759 | case BPF_FUNC_get_stackid: |
| 760 | return &bpf_get_stackid_proto_tp; |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame^] | 761 | case BPF_FUNC_get_stack: |
| 762 | return &bpf_get_stack_proto_tp; |
Yonghong Song | f005afe | 2018-03-20 11:19:17 -0700 | [diff] [blame] | 763 | case BPF_FUNC_perf_prog_read_value: |
| 764 | return &bpf_perf_prog_read_value_proto; |
| 765 | default: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 766 | return tracing_func_proto(func_id, prog); |
Yonghong Song | f005afe | 2018-03-20 11:19:17 -0700 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 770 | /* |
| 771 | * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp |
| 772 | * to avoid potential recursive reuse issue when/if tracepoints are added |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame^] | 773 | * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 774 | */ |
| 775 | static DEFINE_PER_CPU(struct pt_regs, bpf_raw_tp_regs); |
| 776 | BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args, |
| 777 | struct bpf_map *, map, u64, flags, void *, data, u64, size) |
| 778 | { |
| 779 | struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs); |
| 780 | |
| 781 | perf_fetch_caller_regs(regs); |
| 782 | return ____bpf_perf_event_output(regs, map, flags, data, size); |
| 783 | } |
| 784 | |
| 785 | static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = { |
| 786 | .func = bpf_perf_event_output_raw_tp, |
| 787 | .gpl_only = true, |
| 788 | .ret_type = RET_INTEGER, |
| 789 | .arg1_type = ARG_PTR_TO_CTX, |
| 790 | .arg2_type = ARG_CONST_MAP_PTR, |
| 791 | .arg3_type = ARG_ANYTHING, |
| 792 | .arg4_type = ARG_PTR_TO_MEM, |
| 793 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
| 794 | }; |
| 795 | |
| 796 | BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args, |
| 797 | struct bpf_map *, map, u64, flags) |
| 798 | { |
| 799 | struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs); |
| 800 | |
| 801 | perf_fetch_caller_regs(regs); |
| 802 | /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */ |
| 803 | return bpf_get_stackid((unsigned long) regs, (unsigned long) map, |
| 804 | flags, 0, 0); |
| 805 | } |
| 806 | |
| 807 | static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = { |
| 808 | .func = bpf_get_stackid_raw_tp, |
| 809 | .gpl_only = true, |
| 810 | .ret_type = RET_INTEGER, |
| 811 | .arg1_type = ARG_PTR_TO_CTX, |
| 812 | .arg2_type = ARG_CONST_MAP_PTR, |
| 813 | .arg3_type = ARG_ANYTHING, |
| 814 | }; |
| 815 | |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame^] | 816 | BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args, |
| 817 | void *, buf, u32, size, u64, flags) |
| 818 | { |
| 819 | struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs); |
| 820 | |
| 821 | perf_fetch_caller_regs(regs); |
| 822 | return bpf_get_stack((unsigned long) regs, (unsigned long) buf, |
| 823 | (unsigned long) size, flags, 0); |
| 824 | } |
| 825 | |
| 826 | static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { |
| 827 | .func = bpf_get_stack_raw_tp, |
| 828 | .gpl_only = true, |
| 829 | .ret_type = RET_INTEGER, |
| 830 | .arg1_type = ARG_PTR_TO_CTX, |
| 831 | .arg2_type = ARG_PTR_TO_MEM, |
| 832 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, |
| 833 | .arg4_type = ARG_ANYTHING, |
| 834 | }; |
| 835 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 836 | static const struct bpf_func_proto * |
| 837 | 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] | 838 | { |
| 839 | switch (func_id) { |
| 840 | case BPF_FUNC_perf_event_output: |
| 841 | return &bpf_perf_event_output_proto_raw_tp; |
| 842 | case BPF_FUNC_get_stackid: |
| 843 | return &bpf_get_stackid_proto_raw_tp; |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame^] | 844 | case BPF_FUNC_get_stack: |
| 845 | return &bpf_get_stack_proto_raw_tp; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 846 | default: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 847 | return tracing_func_proto(func_id, prog); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
| 851 | static bool raw_tp_prog_is_valid_access(int off, int size, |
| 852 | enum bpf_access_type type, |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 853 | const struct bpf_prog *prog, |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 854 | struct bpf_insn_access_aux *info) |
| 855 | { |
| 856 | /* largest tracepoint in the kernel has 12 args */ |
| 857 | if (off < 0 || off >= sizeof(__u64) * 12) |
| 858 | return false; |
| 859 | if (type != BPF_READ) |
| 860 | return false; |
| 861 | if (off % size != 0) |
| 862 | return false; |
| 863 | return true; |
| 864 | } |
| 865 | |
| 866 | const struct bpf_verifier_ops raw_tracepoint_verifier_ops = { |
| 867 | .get_func_proto = raw_tp_prog_func_proto, |
| 868 | .is_valid_access = raw_tp_prog_is_valid_access, |
| 869 | }; |
| 870 | |
| 871 | const struct bpf_prog_ops raw_tracepoint_prog_ops = { |
| 872 | }; |
| 873 | |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 874 | 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] | 875 | const struct bpf_prog *prog, |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 876 | struct bpf_insn_access_aux *info) |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 877 | { |
Teng Qin | 95da0cd | 2018-03-06 10:55:01 -0800 | [diff] [blame] | 878 | const int size_u64 = sizeof(u64); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 879 | |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 880 | if (off < 0 || off >= sizeof(struct bpf_perf_event_data)) |
| 881 | return false; |
| 882 | if (type != BPF_READ) |
| 883 | return false; |
| 884 | if (off % size != 0) |
| 885 | return false; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 886 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 887 | switch (off) { |
| 888 | case bpf_ctx_range(struct bpf_perf_event_data, sample_period): |
Teng Qin | 95da0cd | 2018-03-06 10:55:01 -0800 | [diff] [blame] | 889 | bpf_ctx_record_field_size(info, size_u64); |
| 890 | if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) |
| 891 | return false; |
| 892 | break; |
| 893 | case bpf_ctx_range(struct bpf_perf_event_data, addr): |
| 894 | bpf_ctx_record_field_size(info, size_u64); |
| 895 | if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 896 | return false; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 897 | break; |
| 898 | default: |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 899 | if (size != sizeof(long)) |
| 900 | return false; |
| 901 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 902 | |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 903 | return true; |
| 904 | } |
| 905 | |
Daniel Borkmann | 6b8cc1d | 2017-01-12 11:51:32 +0100 | [diff] [blame] | 906 | static u32 pe_prog_convert_ctx_access(enum bpf_access_type type, |
| 907 | const struct bpf_insn *si, |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 908 | struct bpf_insn *insn_buf, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 909 | struct bpf_prog *prog, u32 *target_size) |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 910 | { |
| 911 | struct bpf_insn *insn = insn_buf; |
| 912 | |
Daniel Borkmann | 6b8cc1d | 2017-01-12 11:51:32 +0100 | [diff] [blame] | 913 | switch (si->off) { |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 914 | case offsetof(struct bpf_perf_event_data, sample_period): |
Daniel Borkmann | f035a51 | 2016-09-09 02:45:29 +0200 | [diff] [blame] | 915 | *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] | 916 | data), si->dst_reg, si->src_reg, |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 917 | offsetof(struct bpf_perf_event_data_kern, data)); |
Daniel Borkmann | 6b8cc1d | 2017-01-12 11:51:32 +0100 | [diff] [blame] | 918 | *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 919 | bpf_target_off(struct perf_sample_data, period, 8, |
| 920 | target_size)); |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 921 | break; |
Teng Qin | 95da0cd | 2018-03-06 10:55:01 -0800 | [diff] [blame] | 922 | case offsetof(struct bpf_perf_event_data, addr): |
| 923 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, |
| 924 | data), si->dst_reg, si->src_reg, |
| 925 | offsetof(struct bpf_perf_event_data_kern, data)); |
| 926 | *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, |
| 927 | bpf_target_off(struct perf_sample_data, addr, 8, |
| 928 | target_size)); |
| 929 | break; |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 930 | default: |
Daniel Borkmann | f035a51 | 2016-09-09 02:45:29 +0200 | [diff] [blame] | 931 | *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] | 932 | regs), si->dst_reg, si->src_reg, |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 933 | offsetof(struct bpf_perf_event_data_kern, regs)); |
Daniel Borkmann | 6b8cc1d | 2017-01-12 11:51:32 +0100 | [diff] [blame] | 934 | *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg, |
| 935 | si->off); |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 936 | break; |
| 937 | } |
| 938 | |
| 939 | return insn - insn_buf; |
| 940 | } |
| 941 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 942 | const struct bpf_verifier_ops perf_event_verifier_ops = { |
Yonghong Song | f005afe | 2018-03-20 11:19:17 -0700 | [diff] [blame] | 943 | .get_func_proto = pe_prog_func_proto, |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 944 | .is_valid_access = pe_prog_is_valid_access, |
| 945 | .convert_ctx_access = pe_prog_convert_ctx_access, |
| 946 | }; |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 947 | |
| 948 | const struct bpf_prog_ops perf_event_prog_ops = { |
| 949 | }; |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 950 | |
| 951 | static DEFINE_MUTEX(bpf_event_mutex); |
| 952 | |
Yonghong Song | c8c088b | 2017-11-30 13:47:54 -0800 | [diff] [blame] | 953 | #define BPF_TRACE_MAX_PROGS 64 |
| 954 | |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 955 | int perf_event_attach_bpf_prog(struct perf_event *event, |
| 956 | struct bpf_prog *prog) |
| 957 | { |
| 958 | struct bpf_prog_array __rcu *old_array; |
| 959 | struct bpf_prog_array *new_array; |
| 960 | int ret = -EEXIST; |
| 961 | |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 962 | /* |
Masami Hiramatsu | b4da334 | 2018-01-13 02:54:04 +0900 | [diff] [blame] | 963 | * Kprobe override only works if they are on the function entry, |
| 964 | * and only if they are on the opt-in list. |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 965 | */ |
| 966 | if (prog->kprobe_override && |
Masami Hiramatsu | b4da334 | 2018-01-13 02:54:04 +0900 | [diff] [blame] | 967 | (!trace_kprobe_on_func_entry(event->tp_event) || |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 968 | !trace_kprobe_error_injectable(event->tp_event))) |
| 969 | return -EINVAL; |
| 970 | |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 971 | mutex_lock(&bpf_event_mutex); |
| 972 | |
| 973 | if (event->prog) |
Yonghong Song | 07c41a2 | 2017-10-30 13:50:22 -0700 | [diff] [blame] | 974 | goto unlock; |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 975 | |
Yonghong Song | 07c41a2 | 2017-10-30 13:50:22 -0700 | [diff] [blame] | 976 | old_array = event->tp_event->prog_array; |
Yonghong Song | c8c088b | 2017-11-30 13:47:54 -0800 | [diff] [blame] | 977 | if (old_array && |
| 978 | bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) { |
| 979 | ret = -E2BIG; |
| 980 | goto unlock; |
| 981 | } |
| 982 | |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 983 | ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array); |
| 984 | if (ret < 0) |
Yonghong Song | 07c41a2 | 2017-10-30 13:50:22 -0700 | [diff] [blame] | 985 | goto unlock; |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 986 | |
| 987 | /* set the new array to event->tp_event and set event->prog */ |
| 988 | event->prog = prog; |
| 989 | rcu_assign_pointer(event->tp_event->prog_array, new_array); |
| 990 | bpf_prog_array_free(old_array); |
| 991 | |
Yonghong Song | 07c41a2 | 2017-10-30 13:50:22 -0700 | [diff] [blame] | 992 | unlock: |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 993 | mutex_unlock(&bpf_event_mutex); |
| 994 | return ret; |
| 995 | } |
| 996 | |
| 997 | void perf_event_detach_bpf_prog(struct perf_event *event) |
| 998 | { |
| 999 | struct bpf_prog_array __rcu *old_array; |
| 1000 | struct bpf_prog_array *new_array; |
| 1001 | int ret; |
| 1002 | |
| 1003 | mutex_lock(&bpf_event_mutex); |
| 1004 | |
| 1005 | if (!event->prog) |
Yonghong Song | 07c41a2 | 2017-10-30 13:50:22 -0700 | [diff] [blame] | 1006 | goto unlock; |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1007 | |
Yonghong Song | 07c41a2 | 2017-10-30 13:50:22 -0700 | [diff] [blame] | 1008 | old_array = event->tp_event->prog_array; |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1009 | ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array); |
| 1010 | if (ret < 0) { |
| 1011 | bpf_prog_array_delete_safe(old_array, event->prog); |
| 1012 | } else { |
| 1013 | rcu_assign_pointer(event->tp_event->prog_array, new_array); |
| 1014 | bpf_prog_array_free(old_array); |
| 1015 | } |
| 1016 | |
| 1017 | bpf_prog_put(event->prog); |
| 1018 | event->prog = NULL; |
| 1019 | |
Yonghong Song | 07c41a2 | 2017-10-30 13:50:22 -0700 | [diff] [blame] | 1020 | unlock: |
Yonghong Song | e87c6bc38 | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1021 | mutex_unlock(&bpf_event_mutex); |
| 1022 | } |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1023 | |
Yonghong Song | f4e2298 | 2017-12-13 10:35:37 -0800 | [diff] [blame] | 1024 | 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] | 1025 | { |
| 1026 | struct perf_event_query_bpf __user *uquery = info; |
| 1027 | struct perf_event_query_bpf query = {}; |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1028 | u32 *ids, prog_cnt, ids_len; |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1029 | int ret; |
| 1030 | |
| 1031 | if (!capable(CAP_SYS_ADMIN)) |
| 1032 | return -EPERM; |
| 1033 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 1034 | return -EINVAL; |
| 1035 | if (copy_from_user(&query, uquery, sizeof(query))) |
| 1036 | return -EFAULT; |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1037 | |
| 1038 | ids_len = query.ids_len; |
| 1039 | if (ids_len > BPF_TRACE_MAX_PROGS) |
Daniel Borkmann | 9c481b9 | 2018-02-14 15:31:00 +0100 | [diff] [blame] | 1040 | return -E2BIG; |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1041 | ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN); |
| 1042 | if (!ids) |
| 1043 | return -ENOMEM; |
| 1044 | /* |
| 1045 | * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which |
| 1046 | * is required when user only wants to check for uquery->prog_cnt. |
| 1047 | * There is no need to check for it since the case is handled |
| 1048 | * gracefully in bpf_prog_array_copy_info. |
| 1049 | */ |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1050 | |
| 1051 | mutex_lock(&bpf_event_mutex); |
| 1052 | ret = bpf_prog_array_copy_info(event->tp_event->prog_array, |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1053 | ids, |
| 1054 | ids_len, |
| 1055 | &prog_cnt); |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1056 | mutex_unlock(&bpf_event_mutex); |
| 1057 | |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1058 | if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) || |
| 1059 | copy_to_user(uquery->ids, ids, ids_len * sizeof(u32))) |
| 1060 | ret = -EFAULT; |
| 1061 | |
| 1062 | kfree(ids); |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1063 | return ret; |
| 1064 | } |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1065 | |
| 1066 | extern struct bpf_raw_event_map __start__bpf_raw_tp[]; |
| 1067 | extern struct bpf_raw_event_map __stop__bpf_raw_tp[]; |
| 1068 | |
| 1069 | struct bpf_raw_event_map *bpf_find_raw_tracepoint(const char *name) |
| 1070 | { |
| 1071 | struct bpf_raw_event_map *btp = __start__bpf_raw_tp; |
| 1072 | |
| 1073 | for (; btp < __stop__bpf_raw_tp; btp++) { |
| 1074 | if (!strcmp(btp->tp->name, name)) |
| 1075 | return btp; |
| 1076 | } |
| 1077 | return NULL; |
| 1078 | } |
| 1079 | |
| 1080 | static __always_inline |
| 1081 | void __bpf_trace_run(struct bpf_prog *prog, u64 *args) |
| 1082 | { |
| 1083 | rcu_read_lock(); |
| 1084 | preempt_disable(); |
| 1085 | (void) BPF_PROG_RUN(prog, args); |
| 1086 | preempt_enable(); |
| 1087 | rcu_read_unlock(); |
| 1088 | } |
| 1089 | |
| 1090 | #define UNPACK(...) __VA_ARGS__ |
| 1091 | #define REPEAT_1(FN, DL, X, ...) FN(X) |
| 1092 | #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__) |
| 1093 | #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__) |
| 1094 | #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__) |
| 1095 | #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__) |
| 1096 | #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__) |
| 1097 | #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__) |
| 1098 | #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__) |
| 1099 | #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__) |
| 1100 | #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__) |
| 1101 | #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__) |
| 1102 | #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__) |
| 1103 | #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__) |
| 1104 | |
| 1105 | #define SARG(X) u64 arg##X |
| 1106 | #define COPY(X) args[X] = arg##X |
| 1107 | |
| 1108 | #define __DL_COM (,) |
| 1109 | #define __DL_SEM (;) |
| 1110 | |
| 1111 | #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 |
| 1112 | |
| 1113 | #define BPF_TRACE_DEFN_x(x) \ |
| 1114 | void bpf_trace_run##x(struct bpf_prog *prog, \ |
| 1115 | REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \ |
| 1116 | { \ |
| 1117 | u64 args[x]; \ |
| 1118 | REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \ |
| 1119 | __bpf_trace_run(prog, args); \ |
| 1120 | } \ |
| 1121 | EXPORT_SYMBOL_GPL(bpf_trace_run##x) |
| 1122 | BPF_TRACE_DEFN_x(1); |
| 1123 | BPF_TRACE_DEFN_x(2); |
| 1124 | BPF_TRACE_DEFN_x(3); |
| 1125 | BPF_TRACE_DEFN_x(4); |
| 1126 | BPF_TRACE_DEFN_x(5); |
| 1127 | BPF_TRACE_DEFN_x(6); |
| 1128 | BPF_TRACE_DEFN_x(7); |
| 1129 | BPF_TRACE_DEFN_x(8); |
| 1130 | BPF_TRACE_DEFN_x(9); |
| 1131 | BPF_TRACE_DEFN_x(10); |
| 1132 | BPF_TRACE_DEFN_x(11); |
| 1133 | BPF_TRACE_DEFN_x(12); |
| 1134 | |
| 1135 | static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog) |
| 1136 | { |
| 1137 | struct tracepoint *tp = btp->tp; |
| 1138 | |
| 1139 | /* |
| 1140 | * check that program doesn't access arguments beyond what's |
| 1141 | * available in this tracepoint |
| 1142 | */ |
| 1143 | if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64)) |
| 1144 | return -EINVAL; |
| 1145 | |
| 1146 | return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog); |
| 1147 | } |
| 1148 | |
| 1149 | int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog) |
| 1150 | { |
| 1151 | int err; |
| 1152 | |
| 1153 | mutex_lock(&bpf_event_mutex); |
| 1154 | err = __bpf_probe_register(btp, prog); |
| 1155 | mutex_unlock(&bpf_event_mutex); |
| 1156 | return err; |
| 1157 | } |
| 1158 | |
| 1159 | int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog) |
| 1160 | { |
| 1161 | int err; |
| 1162 | |
| 1163 | mutex_lock(&bpf_event_mutex); |
| 1164 | err = tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog); |
| 1165 | mutex_unlock(&bpf_event_mutex); |
| 1166 | return err; |
| 1167 | } |