blob: dc498b605d5dd36137eaba7bd0ee93da72a36c33 [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;
Al Virodb68ce12017-03-20 21:08:07 -040099 if (unlikely(uaccess_kernel()))
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700100 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/*
John Fastabend7bda4b42017-07-02 02:13:29 +0200125 * Only limited trace_printk() conversion specifiers allowed:
126 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
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
John Fastabend7bda4b42017-07-02 02:13:29 +0200201 if (fmt[i] != 'i' && fmt[i] != 'd' &&
202 fmt[i] != 'u' && fmt[i] != 'x')
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700203 return -EINVAL;
204 fmt_cnt++;
205 }
206
Daniel Borkmann88a5c692017-08-16 01:45:33 +0200207/* Horrid workaround for getting va_list handling working with different
208 * argument type combinations generically for 32 and 64 bit archs.
209 */
210#define __BPF_TP_EMIT() __BPF_ARG3_TP()
211#define __BPF_TP(...) \
212 __trace_printk(1 /* Fake ip will not be printed. */, \
213 fmt, ##__VA_ARGS__)
214
215#define __BPF_ARG1_TP(...) \
216 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
217 ? __BPF_TP(arg1, ##__VA_ARGS__) \
218 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
219 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
220 : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
221
222#define __BPF_ARG2_TP(...) \
223 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
224 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
225 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
226 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
227 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
228
229#define __BPF_ARG3_TP(...) \
230 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
231 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
232 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
233 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
234 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
235
236 return __BPF_TP_EMIT();
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700237}
238
239static const struct bpf_func_proto bpf_trace_printk_proto = {
240 .func = bpf_trace_printk,
241 .gpl_only = true,
242 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800243 .arg1_type = ARG_PTR_TO_MEM,
244 .arg2_type = ARG_CONST_SIZE,
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700245};
246
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700247const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
248{
249 /*
250 * this program might be calling bpf_trace_printk,
251 * so allocate per-cpu printk buffers
252 */
253 trace_printk_init_buffers();
254
255 return &bpf_trace_printk_proto;
256}
257
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200258BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
Kaixu Xia35578d72015-08-06 07:02:35 +0000259{
Kaixu Xia35578d72015-08-06 07:02:35 +0000260 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200261 unsigned int cpu = smp_processor_id();
262 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200263 struct bpf_event_entry *ee;
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700264 u64 value = 0;
265 int err;
Kaixu Xia35578d72015-08-06 07:02:35 +0000266
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200267 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
268 return -EINVAL;
269 if (index == BPF_F_CURRENT_CPU)
270 index = cpu;
Kaixu Xia35578d72015-08-06 07:02:35 +0000271 if (unlikely(index >= array->map.max_entries))
272 return -E2BIG;
273
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200274 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200275 if (!ee)
Kaixu Xia35578d72015-08-06 07:02:35 +0000276 return -ENOENT;
277
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700278 err = perf_event_read_local(ee->event, &value);
Kaixu Xia35578d72015-08-06 07:02:35 +0000279 /*
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700280 * this api is ugly since we miss [-22..-2] range of valid
281 * counter values, but that's uapi
Kaixu Xia35578d72015-08-06 07:02:35 +0000282 */
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700283 if (err)
284 return err;
285 return value;
Kaixu Xia35578d72015-08-06 07:02:35 +0000286}
287
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700288static const struct bpf_func_proto bpf_perf_event_read_proto = {
Kaixu Xia35578d72015-08-06 07:02:35 +0000289 .func = bpf_perf_event_read,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700290 .gpl_only = true,
Kaixu Xia35578d72015-08-06 07:02:35 +0000291 .ret_type = RET_INTEGER,
292 .arg1_type = ARG_CONST_MAP_PTR,
293 .arg2_type = ARG_ANYTHING,
294};
295
Daniel Borkmann20b9d7a2017-06-11 00:50:40 +0200296static DEFINE_PER_CPU(struct perf_sample_data, bpf_sd);
297
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200298static __always_inline u64
299__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
300 u64 flags, struct perf_raw_record *raw)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700301{
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700302 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmann20b9d7a2017-06-11 00:50:40 +0200303 struct perf_sample_data *sd = this_cpu_ptr(&bpf_sd);
Daniel Borkmannd7931332016-06-28 12:18:24 +0200304 unsigned int cpu = smp_processor_id();
Daniel Borkmann1e337592016-04-18 21:01:23 +0200305 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200306 struct bpf_event_entry *ee;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700307 struct perf_event *event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700308
Daniel Borkmann1e337592016-04-18 21:01:23 +0200309 if (index == BPF_F_CURRENT_CPU)
Daniel Borkmannd7931332016-06-28 12:18:24 +0200310 index = cpu;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700311 if (unlikely(index >= array->map.max_entries))
312 return -E2BIG;
313
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200314 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200315 if (!ee)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700316 return -ENOENT;
317
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200318 event = ee->event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700319 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
320 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
321 return -EINVAL;
322
Daniel Borkmannd7931332016-06-28 12:18:24 +0200323 if (unlikely(event->oncpu != cpu))
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700324 return -EOPNOTSUPP;
325
Daniel Borkmann20b9d7a2017-06-11 00:50:40 +0200326 perf_sample_data_init(sd, 0, 0);
327 sd->raw = raw;
328 perf_event_output(event, sd, regs);
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700329 return 0;
330}
331
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200332BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
333 u64, flags, void *, data, u64, size)
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200334{
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200335 struct perf_raw_record raw = {
336 .frag = {
337 .size = size,
338 .data = data,
339 },
340 };
341
342 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
343 return -EINVAL;
344
345 return __bpf_perf_event_output(regs, map, flags, &raw);
346}
347
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700348static const struct bpf_func_proto bpf_perf_event_output_proto = {
349 .func = bpf_perf_event_output,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700350 .gpl_only = true,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700351 .ret_type = RET_INTEGER,
352 .arg1_type = ARG_PTR_TO_CTX,
353 .arg2_type = ARG_CONST_MAP_PTR,
354 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800355 .arg4_type = ARG_PTR_TO_MEM,
356 .arg5_type = ARG_CONST_SIZE,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700357};
358
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200359static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
360
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200361u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
362 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200363{
364 struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200365 struct perf_raw_frag frag = {
366 .copy = ctx_copy,
367 .size = ctx_size,
368 .data = ctx,
369 };
370 struct perf_raw_record raw = {
371 .frag = {
Andrew Morton183fc152016-07-18 15:50:58 -0700372 {
373 .next = ctx_size ? &frag : NULL,
374 },
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200375 .size = meta_size,
376 .data = meta,
377 },
378 };
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200379
380 perf_fetch_caller_regs(regs);
381
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200382 return __bpf_perf_event_output(regs, map, flags, &raw);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200383}
384
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200385BPF_CALL_0(bpf_get_current_task)
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700386{
387 return (long) current;
388}
389
390static const struct bpf_func_proto bpf_get_current_task_proto = {
391 .func = bpf_get_current_task,
392 .gpl_only = true,
393 .ret_type = RET_INTEGER,
394};
395
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200396BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700397{
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700398 struct bpf_array *array = container_of(map, struct bpf_array, map);
399 struct cgroup *cgrp;
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700400
401 if (unlikely(in_interrupt()))
402 return -EINVAL;
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700403 if (unlikely(idx >= array->map.max_entries))
404 return -E2BIG;
405
406 cgrp = READ_ONCE(array->ptrs[idx]);
407 if (unlikely(!cgrp))
408 return -EAGAIN;
409
410 return task_under_cgroup_hierarchy(current, cgrp);
411}
412
413static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
414 .func = bpf_current_task_under_cgroup,
415 .gpl_only = false,
416 .ret_type = RET_INTEGER,
417 .arg1_type = ARG_CONST_MAP_PTR,
418 .arg2_type = ARG_ANYTHING,
419};
420
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000421BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size,
422 const void *, unsafe_ptr)
423{
424 int ret;
425
426 /*
427 * The strncpy_from_unsafe() call will likely not fill the entire
428 * buffer, but that's okay in this circumstance as we're probing
429 * arbitrary memory anyway similar to bpf_probe_read() and might
430 * as well probe the stack. Thus, memory is explicitly cleared
431 * only in error case, so that improper users ignoring return
432 * code altogether don't copy garbage; otherwise length of string
433 * is returned that can be used for bpf_perf_event_output() et al.
434 */
435 ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
436 if (unlikely(ret < 0))
437 memset(dst, 0, size);
438
439 return ret;
440}
441
442static const struct bpf_func_proto bpf_probe_read_str_proto = {
443 .func = bpf_probe_read_str,
444 .gpl_only = true,
445 .ret_type = RET_INTEGER,
446 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
447 .arg2_type = ARG_CONST_SIZE,
448 .arg3_type = ARG_ANYTHING,
449};
450
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700451static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700452{
453 switch (func_id) {
454 case BPF_FUNC_map_lookup_elem:
455 return &bpf_map_lookup_elem_proto;
456 case BPF_FUNC_map_update_elem:
457 return &bpf_map_update_elem_proto;
458 case BPF_FUNC_map_delete_elem:
459 return &bpf_map_delete_elem_proto;
460 case BPF_FUNC_probe_read:
461 return &bpf_probe_read_proto;
Alexei Starovoitovd9847d32015-03-25 12:49:21 -0700462 case BPF_FUNC_ktime_get_ns:
463 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700464 case BPF_FUNC_tail_call:
465 return &bpf_tail_call_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700466 case BPF_FUNC_get_current_pid_tgid:
467 return &bpf_get_current_pid_tgid_proto;
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700468 case BPF_FUNC_get_current_task:
469 return &bpf_get_current_task_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700470 case BPF_FUNC_get_current_uid_gid:
471 return &bpf_get_current_uid_gid_proto;
472 case BPF_FUNC_get_current_comm:
473 return &bpf_get_current_comm_proto;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700474 case BPF_FUNC_trace_printk:
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700475 return bpf_get_trace_printk_proto();
Alexei Starovoitovab1973d2015-06-12 19:39:14 -0700476 case BPF_FUNC_get_smp_processor_id:
477 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +0200478 case BPF_FUNC_get_numa_node_id:
479 return &bpf_get_numa_node_id_proto;
Kaixu Xia35578d72015-08-06 07:02:35 +0000480 case BPF_FUNC_perf_event_read:
481 return &bpf_perf_event_read_proto;
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700482 case BPF_FUNC_probe_write_user:
483 return bpf_get_probe_write_proto();
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700484 case BPF_FUNC_current_task_under_cgroup:
485 return &bpf_current_task_under_cgroup_proto;
Alexei Starovoitov8937bd82016-08-11 18:17:18 -0700486 case BPF_FUNC_get_prandom_u32:
487 return &bpf_get_prandom_u32_proto;
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000488 case BPF_FUNC_probe_read_str:
489 return &bpf_probe_read_str_proto;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700490 default:
491 return NULL;
492 }
493}
494
495static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
496{
497 switch (func_id) {
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700498 case BPF_FUNC_perf_event_output:
499 return &bpf_perf_event_output_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800500 case BPF_FUNC_get_stackid:
501 return &bpf_get_stackid_proto;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700502 default:
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700503 return tracing_func_proto(func_id);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700504 }
505}
506
507/* bpf+kprobe programs can access fields of 'struct pt_regs' */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700508static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Yonghong Song23994632017-06-22 15:07:39 -0700509 struct bpf_insn_access_aux *info)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700510{
Alexei Starovoitov25415172015-03-25 12:49:20 -0700511 if (off < 0 || off >= sizeof(struct pt_regs))
512 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700513 if (type != BPF_READ)
514 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700515 if (off % size != 0)
516 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +0100517 /*
518 * Assertion for 32 bit to make sure last 8 byte access
519 * (BPF_DW) to the last 4 byte member is disallowed.
520 */
521 if (off + size > sizeof(struct pt_regs))
522 return false;
523
Alexei Starovoitov25415172015-03-25 12:49:20 -0700524 return true;
525}
526
Johannes Bergbe9370a2017-04-11 15:34:57 +0200527const struct bpf_verifier_ops kprobe_prog_ops = {
Alexei Starovoitov25415172015-03-25 12:49:20 -0700528 .get_func_proto = kprobe_prog_func_proto,
529 .is_valid_access = kprobe_prog_is_valid_access,
530};
531
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200532BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
533 u64, flags, void *, data, u64, size)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700534{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200535 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
536
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700537 /*
538 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
539 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200540 * from there and call the same bpf_perf_event_output() helper inline.
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700541 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200542 return ____bpf_perf_event_output(regs, map, flags, data, size);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700543}
544
545static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
546 .func = bpf_perf_event_output_tp,
547 .gpl_only = true,
548 .ret_type = RET_INTEGER,
549 .arg1_type = ARG_PTR_TO_CTX,
550 .arg2_type = ARG_CONST_MAP_PTR,
551 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800552 .arg4_type = ARG_PTR_TO_MEM,
553 .arg5_type = ARG_CONST_SIZE,
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700554};
555
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200556BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
557 u64, flags)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700558{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200559 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700560
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200561 /*
562 * Same comment as in bpf_perf_event_output_tp(), only that this time
563 * the other helper's function body cannot be inlined due to being
564 * external, thus we need to call raw helper function.
565 */
566 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
567 flags, 0, 0);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700568}
569
570static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
571 .func = bpf_get_stackid_tp,
572 .gpl_only = true,
573 .ret_type = RET_INTEGER,
574 .arg1_type = ARG_PTR_TO_CTX,
575 .arg2_type = ARG_CONST_MAP_PTR,
576 .arg3_type = ARG_ANYTHING,
577};
578
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700579static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
580{
581 switch (func_id) {
582 case BPF_FUNC_perf_event_output:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700583 return &bpf_perf_event_output_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700584 case BPF_FUNC_get_stackid:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700585 return &bpf_get_stackid_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700586 default:
587 return tracing_func_proto(func_id);
588 }
589}
590
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700591static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Yonghong Song23994632017-06-22 15:07:39 -0700592 struct bpf_insn_access_aux *info)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700593{
594 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
595 return false;
596 if (type != BPF_READ)
597 return false;
598 if (off % size != 0)
599 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +0100600
601 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700602 return true;
603}
604
Johannes Bergbe9370a2017-04-11 15:34:57 +0200605const struct bpf_verifier_ops tracepoint_prog_ops = {
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700606 .get_func_proto = tp_prog_func_proto,
607 .is_valid_access = tp_prog_is_valid_access,
608};
609
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700610static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Yonghong Song23994632017-06-22 15:07:39 -0700611 struct bpf_insn_access_aux *info)
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700612{
Daniel Borkmannf96da092017-07-02 02:13:27 +0200613 const int size_sp = FIELD_SIZEOF(struct bpf_perf_event_data,
614 sample_period);
Yonghong Song31fd8582017-06-13 15:52:13 -0700615
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700616 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
617 return false;
618 if (type != BPF_READ)
619 return false;
620 if (off % size != 0)
621 return false;
Yonghong Song31fd8582017-06-13 15:52:13 -0700622
Daniel Borkmannf96da092017-07-02 02:13:27 +0200623 switch (off) {
624 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
625 bpf_ctx_record_field_size(info, size_sp);
626 if (!bpf_ctx_narrow_access_ok(off, size, size_sp))
Yonghong Song23994632017-06-22 15:07:39 -0700627 return false;
Daniel Borkmannf96da092017-07-02 02:13:27 +0200628 break;
629 default:
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700630 if (size != sizeof(long))
631 return false;
632 }
Daniel Borkmannf96da092017-07-02 02:13:27 +0200633
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700634 return true;
635}
636
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100637static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
638 const struct bpf_insn *si,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700639 struct bpf_insn *insn_buf,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200640 struct bpf_prog *prog, u32 *target_size)
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700641{
642 struct bpf_insn *insn = insn_buf;
643
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100644 switch (si->off) {
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700645 case offsetof(struct bpf_perf_event_data, sample_period):
Daniel Borkmannf035a512016-09-09 02:45:29 +0200646 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100647 data), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700648 offsetof(struct bpf_perf_event_data_kern, data));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100649 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200650 bpf_target_off(struct perf_sample_data, period, 8,
651 target_size));
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700652 break;
653 default:
Daniel Borkmannf035a512016-09-09 02:45:29 +0200654 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100655 regs), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700656 offsetof(struct bpf_perf_event_data_kern, regs));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100657 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
658 si->off);
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700659 break;
660 }
661
662 return insn - insn_buf;
663}
664
Johannes Bergbe9370a2017-04-11 15:34:57 +0200665const struct bpf_verifier_ops perf_event_prog_ops = {
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700666 .get_func_proto = tp_prog_func_proto,
667 .is_valid_access = pe_prog_is_valid_access,
668 .convert_ctx_access = pe_prog_convert_ctx_access,
669};