blob: c22a961d1a42696055df0f10510e087109009f46 [file] [log] [blame]
Alexei Starovoitov25415172015-03-25 12:49:20 -07001/* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
Alexei Starovoitov0515e592016-09-01 18:37:22 -07002 * Copyright (c) 2016 Facebook
Alexei Starovoitov25415172015-03-25 12:49:20 -07003 *
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 Starovoitov0515e592016-09-01 18:37:22 -070012#include <linux/bpf_perf_event.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070013#include <linux/filter.h>
14#include <linux/uaccess.h>
Alexei Starovoitov9c959c82015-03-25 12:49:22 -070015#include <linux/ctype.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070016#include "trace.h"
17
Alexei Starovoitov25415172015-03-25 12:49:20 -070018/**
19 * trace_call_bpf - invoke BPF program
20 * @prog: BPF program
21 * @ctx: opaque context pointer
22 *
23 * kprobe handlers execute BPF programs via this helper.
24 * Can be used from static tracepoints in the future.
25 *
26 * Return: BPF programs always return an integer which is interpreted by
27 * kprobe handler as:
28 * 0 - return from kprobe (event is filtered out)
29 * 1 - store kprobe event into ring buffer
30 * Other values are reserved and currently alias to 1
31 */
32unsigned int trace_call_bpf(struct bpf_prog *prog, void *ctx)
33{
34 unsigned int ret;
35
36 if (in_nmi()) /* not supported yet */
37 return 1;
38
39 preempt_disable();
40
41 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
42 /*
43 * since some bpf program is already running on this cpu,
44 * don't call into another bpf program (same or different)
45 * and don't send kprobe event into ring-buffer,
46 * so return zero here
47 */
48 ret = 0;
49 goto out;
50 }
51
52 rcu_read_lock();
53 ret = BPF_PROG_RUN(prog, ctx);
54 rcu_read_unlock();
55
56 out:
57 __this_cpu_dec(bpf_prog_active);
58 preempt_enable();
59
60 return ret;
61}
62EXPORT_SYMBOL_GPL(trace_call_bpf);
63
Daniel Borkmannf3694e02016-09-09 02:45:31 +020064BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr)
Alexei Starovoitov25415172015-03-25 12:49:20 -070065{
Daniel Borkmannf3694e02016-09-09 02:45:31 +020066 int ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -070067
Daniel Borkmann074f528e2016-04-13 00:10:52 +020068 ret = probe_kernel_read(dst, unsafe_ptr, size);
69 if (unlikely(ret < 0))
70 memset(dst, 0, size);
71
72 return ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -070073}
74
75static const struct bpf_func_proto bpf_probe_read_proto = {
76 .func = bpf_probe_read,
77 .gpl_only = true,
78 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -080079 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
80 .arg2_type = ARG_CONST_SIZE,
Alexei Starovoitov25415172015-03-25 12:49:20 -070081 .arg3_type = ARG_ANYTHING,
82};
83
Daniel Borkmannf3694e02016-09-09 02:45:31 +020084BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src,
85 u32, size)
Sargun Dhillon96ae5222016-07-25 05:54:46 -070086{
Sargun Dhillon96ae5222016-07-25 05:54:46 -070087 /*
88 * Ensure we're in user context which is safe for the helper to
89 * run. This helper has no business in a kthread.
90 *
91 * access_ok() should prevent writing to non-user memory, but in
92 * some situations (nommu, temporary switch, etc) access_ok() does
93 * not provide enough validation, hence the check on KERNEL_DS.
94 */
95
96 if (unlikely(in_interrupt() ||
97 current->flags & (PF_KTHREAD | PF_EXITING)))
98 return -EPERM;
99 if (unlikely(segment_eq(get_fs(), KERNEL_DS)))
100 return -EPERM;
101 if (!access_ok(VERIFY_WRITE, unsafe_ptr, size))
102 return -EPERM;
103
104 return probe_kernel_write(unsafe_ptr, src, size);
105}
106
107static const struct bpf_func_proto bpf_probe_write_user_proto = {
108 .func = bpf_probe_write_user,
109 .gpl_only = true,
110 .ret_type = RET_INTEGER,
111 .arg1_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800112 .arg2_type = ARG_PTR_TO_MEM,
113 .arg3_type = ARG_CONST_SIZE,
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700114};
115
116static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
117{
118 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
119 current->comm, task_pid_nr(current));
120
121 return &bpf_probe_write_user_proto;
122}
123
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700124/*
125 * limited trace_printk()
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700126 * only %d %u %x %ld %lu %lx %lld %llu %llx %p %s conversion specifiers allowed
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700127 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200128BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
129 u64, arg2, u64, arg3)
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700130{
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700131 bool str_seen = false;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700132 int mod[3] = {};
133 int fmt_cnt = 0;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700134 u64 unsafe_addr;
135 char buf[64];
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700136 int i;
137
138 /*
139 * bpf_check()->check_func_arg()->check_stack_boundary()
140 * guarantees that fmt points to bpf program stack,
141 * fmt_size bytes of it were initialized and fmt_size > 0
142 */
143 if (fmt[--fmt_size] != 0)
144 return -EINVAL;
145
146 /* check format string for allowed specifiers */
147 for (i = 0; i < fmt_size; i++) {
148 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
149 return -EINVAL;
150
151 if (fmt[i] != '%')
152 continue;
153
154 if (fmt_cnt >= 3)
155 return -EINVAL;
156
157 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
158 i++;
159 if (fmt[i] == 'l') {
160 mod[fmt_cnt]++;
161 i++;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700162 } else if (fmt[i] == 'p' || fmt[i] == 's') {
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700163 mod[fmt_cnt]++;
164 i++;
165 if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0)
166 return -EINVAL;
167 fmt_cnt++;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700168 if (fmt[i - 1] == 's') {
169 if (str_seen)
170 /* allow only one '%s' per fmt string */
171 return -EINVAL;
172 str_seen = true;
173
174 switch (fmt_cnt) {
175 case 1:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200176 unsafe_addr = arg1;
177 arg1 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700178 break;
179 case 2:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200180 unsafe_addr = arg2;
181 arg2 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700182 break;
183 case 3:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200184 unsafe_addr = arg3;
185 arg3 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700186 break;
187 }
188 buf[0] = 0;
189 strncpy_from_unsafe(buf,
190 (void *) (long) unsafe_addr,
191 sizeof(buf));
192 }
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700193 continue;
194 }
195
196 if (fmt[i] == 'l') {
197 mod[fmt_cnt]++;
198 i++;
199 }
200
201 if (fmt[i] != 'd' && fmt[i] != 'u' && fmt[i] != 'x')
202 return -EINVAL;
203 fmt_cnt++;
204 }
205
206 return __trace_printk(1/* fake ip will not be printed */, fmt,
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200207 mod[0] == 2 ? arg1 : mod[0] == 1 ? (long) arg1 : (u32) arg1,
208 mod[1] == 2 ? arg2 : mod[1] == 1 ? (long) arg2 : (u32) arg2,
209 mod[2] == 2 ? arg3 : mod[2] == 1 ? (long) arg3 : (u32) arg3);
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700210}
211
212static const struct bpf_func_proto bpf_trace_printk_proto = {
213 .func = bpf_trace_printk,
214 .gpl_only = true,
215 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800216 .arg1_type = ARG_PTR_TO_MEM,
217 .arg2_type = ARG_CONST_SIZE,
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700218};
219
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700220const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
221{
222 /*
223 * this program might be calling bpf_trace_printk,
224 * so allocate per-cpu printk buffers
225 */
226 trace_printk_init_buffers();
227
228 return &bpf_trace_printk_proto;
229}
230
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200231BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
Kaixu Xia35578d72015-08-06 07:02:35 +0000232{
Kaixu Xia35578d72015-08-06 07:02:35 +0000233 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200234 unsigned int cpu = smp_processor_id();
235 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200236 struct bpf_event_entry *ee;
Kaixu Xia35578d72015-08-06 07:02:35 +0000237 struct perf_event *event;
238
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200239 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
240 return -EINVAL;
241 if (index == BPF_F_CURRENT_CPU)
242 index = cpu;
Kaixu Xia35578d72015-08-06 07:02:35 +0000243 if (unlikely(index >= array->map.max_entries))
244 return -E2BIG;
245
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200246 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200247 if (!ee)
Kaixu Xia35578d72015-08-06 07:02:35 +0000248 return -ENOENT;
249
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200250 event = ee->event;
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200251 if (unlikely(event->attr.type != PERF_TYPE_HARDWARE &&
252 event->attr.type != PERF_TYPE_RAW))
253 return -EINVAL;
254
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700255 /* make sure event is local and doesn't have pmu::count */
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200256 if (unlikely(event->oncpu != cpu || event->pmu->count))
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700257 return -EINVAL;
258
Kaixu Xia35578d72015-08-06 07:02:35 +0000259 /*
260 * we don't know if the function is run successfully by the
261 * return value. It can be judged in other places, such as
262 * eBPF programs.
263 */
264 return perf_event_read_local(event);
265}
266
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700267static const struct bpf_func_proto bpf_perf_event_read_proto = {
Kaixu Xia35578d72015-08-06 07:02:35 +0000268 .func = bpf_perf_event_read,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700269 .gpl_only = true,
Kaixu Xia35578d72015-08-06 07:02:35 +0000270 .ret_type = RET_INTEGER,
271 .arg1_type = ARG_CONST_MAP_PTR,
272 .arg2_type = ARG_ANYTHING,
273};
274
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200275static __always_inline u64
276__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
277 u64 flags, struct perf_raw_record *raw)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700278{
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700279 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmannd7931332016-06-28 12:18:24 +0200280 unsigned int cpu = smp_processor_id();
Daniel Borkmann1e337592016-04-18 21:01:23 +0200281 u64 index = flags & BPF_F_INDEX_MASK;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700282 struct perf_sample_data sample_data;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200283 struct bpf_event_entry *ee;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700284 struct perf_event *event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700285
Daniel Borkmann1e337592016-04-18 21:01:23 +0200286 if (index == BPF_F_CURRENT_CPU)
Daniel Borkmannd7931332016-06-28 12:18:24 +0200287 index = cpu;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700288 if (unlikely(index >= array->map.max_entries))
289 return -E2BIG;
290
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200291 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200292 if (!ee)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700293 return -ENOENT;
294
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200295 event = ee->event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700296 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
297 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
298 return -EINVAL;
299
Daniel Borkmannd7931332016-06-28 12:18:24 +0200300 if (unlikely(event->oncpu != cpu))
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700301 return -EOPNOTSUPP;
302
303 perf_sample_data_init(&sample_data, 0, 0);
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200304 sample_data.raw = raw;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700305 perf_event_output(event, &sample_data, regs);
306 return 0;
307}
308
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200309BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
310 u64, flags, void *, data, u64, size)
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200311{
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200312 struct perf_raw_record raw = {
313 .frag = {
314 .size = size,
315 .data = data,
316 },
317 };
318
319 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
320 return -EINVAL;
321
322 return __bpf_perf_event_output(regs, map, flags, &raw);
323}
324
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700325static const struct bpf_func_proto bpf_perf_event_output_proto = {
326 .func = bpf_perf_event_output,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700327 .gpl_only = true,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700328 .ret_type = RET_INTEGER,
329 .arg1_type = ARG_PTR_TO_CTX,
330 .arg2_type = ARG_CONST_MAP_PTR,
331 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800332 .arg4_type = ARG_PTR_TO_MEM,
333 .arg5_type = ARG_CONST_SIZE,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700334};
335
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200336static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
337
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200338u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
339 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200340{
341 struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200342 struct perf_raw_frag frag = {
343 .copy = ctx_copy,
344 .size = ctx_size,
345 .data = ctx,
346 };
347 struct perf_raw_record raw = {
348 .frag = {
Andrew Morton183fc152016-07-18 15:50:58 -0700349 {
350 .next = ctx_size ? &frag : NULL,
351 },
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200352 .size = meta_size,
353 .data = meta,
354 },
355 };
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200356
357 perf_fetch_caller_regs(regs);
358
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200359 return __bpf_perf_event_output(regs, map, flags, &raw);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200360}
361
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200362BPF_CALL_0(bpf_get_current_task)
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700363{
364 return (long) current;
365}
366
367static const struct bpf_func_proto bpf_get_current_task_proto = {
368 .func = bpf_get_current_task,
369 .gpl_only = true,
370 .ret_type = RET_INTEGER,
371};
372
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200373BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700374{
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700375 struct bpf_array *array = container_of(map, struct bpf_array, map);
376 struct cgroup *cgrp;
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700377
378 if (unlikely(in_interrupt()))
379 return -EINVAL;
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700380 if (unlikely(idx >= array->map.max_entries))
381 return -E2BIG;
382
383 cgrp = READ_ONCE(array->ptrs[idx]);
384 if (unlikely(!cgrp))
385 return -EAGAIN;
386
387 return task_under_cgroup_hierarchy(current, cgrp);
388}
389
390static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
391 .func = bpf_current_task_under_cgroup,
392 .gpl_only = false,
393 .ret_type = RET_INTEGER,
394 .arg1_type = ARG_CONST_MAP_PTR,
395 .arg2_type = ARG_ANYTHING,
396};
397
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700398static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700399{
400 switch (func_id) {
401 case BPF_FUNC_map_lookup_elem:
402 return &bpf_map_lookup_elem_proto;
403 case BPF_FUNC_map_update_elem:
404 return &bpf_map_update_elem_proto;
405 case BPF_FUNC_map_delete_elem:
406 return &bpf_map_delete_elem_proto;
407 case BPF_FUNC_probe_read:
408 return &bpf_probe_read_proto;
Alexei Starovoitovd9847d32015-03-25 12:49:21 -0700409 case BPF_FUNC_ktime_get_ns:
410 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700411 case BPF_FUNC_tail_call:
412 return &bpf_tail_call_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700413 case BPF_FUNC_get_current_pid_tgid:
414 return &bpf_get_current_pid_tgid_proto;
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700415 case BPF_FUNC_get_current_task:
416 return &bpf_get_current_task_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700417 case BPF_FUNC_get_current_uid_gid:
418 return &bpf_get_current_uid_gid_proto;
419 case BPF_FUNC_get_current_comm:
420 return &bpf_get_current_comm_proto;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700421 case BPF_FUNC_trace_printk:
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700422 return bpf_get_trace_printk_proto();
Alexei Starovoitovab1973d2015-06-12 19:39:14 -0700423 case BPF_FUNC_get_smp_processor_id:
424 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +0200425 case BPF_FUNC_get_numa_node_id:
426 return &bpf_get_numa_node_id_proto;
Kaixu Xia35578d72015-08-06 07:02:35 +0000427 case BPF_FUNC_perf_event_read:
428 return &bpf_perf_event_read_proto;
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700429 case BPF_FUNC_probe_write_user:
430 return bpf_get_probe_write_proto();
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700431 case BPF_FUNC_current_task_under_cgroup:
432 return &bpf_current_task_under_cgroup_proto;
Alexei Starovoitov8937bd82016-08-11 18:17:18 -0700433 case BPF_FUNC_get_prandom_u32:
434 return &bpf_get_prandom_u32_proto;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700435 default:
436 return NULL;
437 }
438}
439
440static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
441{
442 switch (func_id) {
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700443 case BPF_FUNC_perf_event_output:
444 return &bpf_perf_event_output_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800445 case BPF_FUNC_get_stackid:
446 return &bpf_get_stackid_proto;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700447 default:
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700448 return tracing_func_proto(func_id);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700449 }
450}
451
452/* bpf+kprobe programs can access fields of 'struct pt_regs' */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700453static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
454 enum bpf_reg_type *reg_type)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700455{
Alexei Starovoitov25415172015-03-25 12:49:20 -0700456 if (off < 0 || off >= sizeof(struct pt_regs))
457 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700458 if (type != BPF_READ)
459 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700460 if (off % size != 0)
461 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +0100462 /*
463 * Assertion for 32 bit to make sure last 8 byte access
464 * (BPF_DW) to the last 4 byte member is disallowed.
465 */
466 if (off + size > sizeof(struct pt_regs))
467 return false;
468
Alexei Starovoitov25415172015-03-25 12:49:20 -0700469 return true;
470}
471
Julia Lawall27dff4e2015-12-11 18:35:59 +0100472static const struct bpf_verifier_ops kprobe_prog_ops = {
Alexei Starovoitov25415172015-03-25 12:49:20 -0700473 .get_func_proto = kprobe_prog_func_proto,
474 .is_valid_access = kprobe_prog_is_valid_access,
475};
476
477static struct bpf_prog_type_list kprobe_tl = {
478 .ops = &kprobe_prog_ops,
479 .type = BPF_PROG_TYPE_KPROBE,
480};
481
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200482BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
483 u64, flags, void *, data, u64, size)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700484{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200485 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
486
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700487 /*
488 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
489 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200490 * from there and call the same bpf_perf_event_output() helper inline.
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700491 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200492 return ____bpf_perf_event_output(regs, map, flags, data, size);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700493}
494
495static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
496 .func = bpf_perf_event_output_tp,
497 .gpl_only = true,
498 .ret_type = RET_INTEGER,
499 .arg1_type = ARG_PTR_TO_CTX,
500 .arg2_type = ARG_CONST_MAP_PTR,
501 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800502 .arg4_type = ARG_PTR_TO_MEM,
503 .arg5_type = ARG_CONST_SIZE,
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700504};
505
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200506BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
507 u64, flags)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700508{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200509 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700510
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200511 /*
512 * Same comment as in bpf_perf_event_output_tp(), only that this time
513 * the other helper's function body cannot be inlined due to being
514 * external, thus we need to call raw helper function.
515 */
516 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
517 flags, 0, 0);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700518}
519
520static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
521 .func = bpf_get_stackid_tp,
522 .gpl_only = true,
523 .ret_type = RET_INTEGER,
524 .arg1_type = ARG_PTR_TO_CTX,
525 .arg2_type = ARG_CONST_MAP_PTR,
526 .arg3_type = ARG_ANYTHING,
527};
528
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700529static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
530{
531 switch (func_id) {
532 case BPF_FUNC_perf_event_output:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700533 return &bpf_perf_event_output_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700534 case BPF_FUNC_get_stackid:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700535 return &bpf_get_stackid_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700536 default:
537 return tracing_func_proto(func_id);
538 }
539}
540
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700541static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
542 enum bpf_reg_type *reg_type)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700543{
544 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
545 return false;
546 if (type != BPF_READ)
547 return false;
548 if (off % size != 0)
549 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +0100550
551 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700552 return true;
553}
554
555static const struct bpf_verifier_ops tracepoint_prog_ops = {
556 .get_func_proto = tp_prog_func_proto,
557 .is_valid_access = tp_prog_is_valid_access,
558};
559
560static struct bpf_prog_type_list tracepoint_tl = {
561 .ops = &tracepoint_prog_ops,
562 .type = BPF_PROG_TYPE_TRACEPOINT,
563};
564
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700565static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
566 enum bpf_reg_type *reg_type)
567{
568 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
569 return false;
570 if (type != BPF_READ)
571 return false;
572 if (off % size != 0)
573 return false;
574 if (off == offsetof(struct bpf_perf_event_data, sample_period)) {
575 if (size != sizeof(u64))
576 return false;
577 } else {
578 if (size != sizeof(long))
579 return false;
580 }
581 return true;
582}
583
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100584static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
585 const struct bpf_insn *si,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700586 struct bpf_insn *insn_buf,
587 struct bpf_prog *prog)
588{
589 struct bpf_insn *insn = insn_buf;
590
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100591 switch (si->off) {
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700592 case offsetof(struct bpf_perf_event_data, sample_period):
593 BUILD_BUG_ON(FIELD_SIZEOF(struct perf_sample_data, period) != sizeof(u64));
Daniel Borkmannf035a512016-09-09 02:45:29 +0200594
595 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100596 data), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700597 offsetof(struct bpf_perf_event_data_kern, data));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100598 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700599 offsetof(struct perf_sample_data, period));
600 break;
601 default:
Daniel Borkmannf035a512016-09-09 02:45:29 +0200602 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100603 regs), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700604 offsetof(struct bpf_perf_event_data_kern, regs));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100605 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
606 si->off);
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700607 break;
608 }
609
610 return insn - insn_buf;
611}
612
613static const struct bpf_verifier_ops perf_event_prog_ops = {
614 .get_func_proto = tp_prog_func_proto,
615 .is_valid_access = pe_prog_is_valid_access,
616 .convert_ctx_access = pe_prog_convert_ctx_access,
617};
618
619static struct bpf_prog_type_list perf_event_tl = {
620 .ops = &perf_event_prog_ops,
621 .type = BPF_PROG_TYPE_PERF_EVENT,
622};
623
Alexei Starovoitov25415172015-03-25 12:49:20 -0700624static int __init register_kprobe_prog_ops(void)
625{
626 bpf_register_prog_type(&kprobe_tl);
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700627 bpf_register_prog_type(&tracepoint_tl);
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700628 bpf_register_prog_type(&perf_event_tl);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700629 return 0;
630}
631late_initcall(register_kprobe_prog_ops);