blob: afdb7e702a98d8d93b2deb9a4de1f7208f56dab4 [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
Yonghong Song265229d2017-10-23 23:53:08 -070020 * @call: tracepoint event
Alexei Starovoitov25415172015-03-25 12:49:20 -070021 * @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 */
Yonghong Song265229d2017-10-23 23:53:08 -070032unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
Alexei Starovoitov25415172015-03-25 12:49:20 -070033{
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
Yonghong Song265229d2017-10-23 23:53:08 -070052 /*
53 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
54 * to all call sites, we did a bpf_prog_array_valid() there to check
55 * whether call->prog_array is empty or not, which is
56 * a heurisitc to speed up execution.
57 *
58 * If bpf_prog_array_valid() fetched prog_array was
59 * non-NULL, we go into trace_call_bpf() and do the actual
60 * proper rcu_dereference() under RCU lock.
61 * If it turns out that prog_array is NULL then, we bail out.
62 * For the opposite, if the bpf_prog_array_valid() fetched pointer
63 * was NULL, you'll skip the prog_array with the risk of missing
64 * out of events when it was updated in between this and the
65 * rcu_dereference() which is accepted risk.
66 */
67 ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
Alexei Starovoitov25415172015-03-25 12:49:20 -070068
69 out:
70 __this_cpu_dec(bpf_prog_active);
71 preempt_enable();
72
73 return ret;
74}
75EXPORT_SYMBOL_GPL(trace_call_bpf);
76
Daniel Borkmannf3694e02016-09-09 02:45:31 +020077BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr)
Alexei Starovoitov25415172015-03-25 12:49:20 -070078{
Daniel Borkmannf3694e02016-09-09 02:45:31 +020079 int ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -070080
Daniel Borkmann074f5282016-04-13 00:10:52 +020081 ret = probe_kernel_read(dst, unsafe_ptr, size);
82 if (unlikely(ret < 0))
83 memset(dst, 0, size);
84
85 return ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -070086}
87
88static const struct bpf_func_proto bpf_probe_read_proto = {
89 .func = bpf_probe_read,
90 .gpl_only = true,
91 .ret_type = RET_INTEGER,
Daniel Borkmann074f5282016-04-13 00:10:52 +020092 .arg1_type = ARG_PTR_TO_RAW_STACK,
Alexei Starovoitov25415172015-03-25 12:49:20 -070093 .arg2_type = ARG_CONST_STACK_SIZE,
94 .arg3_type = ARG_ANYTHING,
95};
96
Joel Fernandes0a8a3e52019-06-04 17:01:00 -040097BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size, const void *, unsafe_ptr)
98{
99 int ret;
100
101 /*
102 * The strncpy_from_unsafe() call will likely not fill the entire
103 * buffer, but that's okay in this circumstance as we're probing
104 * arbitrary memory anyway similar to bpf_probe_read() and might
105 * as well probe the stack. Thus, memory is explicitly cleared
106 * only in error case, so that improper users ignoring return
107 * code altogether don't copy garbage; otherwise length of string
108 * is returned that can be used for bpf_perf_event_output() et al.
109 */
110 ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
111 if (unlikely(ret < 0))
112 memset(dst, 0, size);
113
114 return ret;
115}
116
117static const struct bpf_func_proto bpf_probe_read_str_proto = {
118 .func = bpf_probe_read_str,
119 .gpl_only = true,
120 .ret_type = RET_INTEGER,
121 .arg1_type = ARG_PTR_TO_RAW_STACK,
122 .arg2_type = ARG_CONST_STACK_SIZE,
123 .arg3_type = ARG_ANYTHING,
124};
125
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200126BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src,
127 u32, size)
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700128{
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700129 /*
130 * Ensure we're in user context which is safe for the helper to
131 * run. This helper has no business in a kthread.
132 *
133 * access_ok() should prevent writing to non-user memory, but in
134 * some situations (nommu, temporary switch, etc) access_ok() does
135 * not provide enough validation, hence the check on KERNEL_DS.
136 */
137
138 if (unlikely(in_interrupt() ||
139 current->flags & (PF_KTHREAD | PF_EXITING)))
140 return -EPERM;
141 if (unlikely(segment_eq(get_fs(), KERNEL_DS)))
142 return -EPERM;
143 if (!access_ok(VERIFY_WRITE, unsafe_ptr, size))
144 return -EPERM;
145
146 return probe_kernel_write(unsafe_ptr, src, size);
147}
148
149static const struct bpf_func_proto bpf_probe_write_user_proto = {
150 .func = bpf_probe_write_user,
151 .gpl_only = true,
152 .ret_type = RET_INTEGER,
153 .arg1_type = ARG_ANYTHING,
154 .arg2_type = ARG_PTR_TO_STACK,
155 .arg3_type = ARG_CONST_STACK_SIZE,
156};
157
158static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
159{
160 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
161 current->comm, task_pid_nr(current));
162
163 return &bpf_probe_write_user_proto;
164}
165
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700166/*
167 * limited trace_printk()
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700168 * only %d %u %x %ld %lu %lx %lld %llu %llx %p %s conversion specifiers allowed
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700169 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200170BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
171 u64, arg2, u64, arg3)
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700172{
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700173 bool str_seen = false;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700174 int mod[3] = {};
175 int fmt_cnt = 0;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700176 u64 unsafe_addr;
177 char buf[64];
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700178 int i;
179
180 /*
181 * bpf_check()->check_func_arg()->check_stack_boundary()
182 * guarantees that fmt points to bpf program stack,
183 * fmt_size bytes of it were initialized and fmt_size > 0
184 */
185 if (fmt[--fmt_size] != 0)
186 return -EINVAL;
187
188 /* check format string for allowed specifiers */
189 for (i = 0; i < fmt_size; i++) {
190 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
191 return -EINVAL;
192
193 if (fmt[i] != '%')
194 continue;
195
196 if (fmt_cnt >= 3)
197 return -EINVAL;
198
199 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
200 i++;
201 if (fmt[i] == 'l') {
202 mod[fmt_cnt]++;
203 i++;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700204 } else if (fmt[i] == 'p' || fmt[i] == 's') {
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700205 mod[fmt_cnt]++;
Martynas Pumputis98547af2018-11-23 17:43:26 +0100206 /* disallow any further format extensions */
207 if (fmt[i + 1] != 0 &&
208 !isspace(fmt[i + 1]) &&
209 !ispunct(fmt[i + 1]))
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700210 return -EINVAL;
211 fmt_cnt++;
Martynas Pumputis98547af2018-11-23 17:43:26 +0100212 if (fmt[i] == 's') {
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700213 if (str_seen)
214 /* allow only one '%s' per fmt string */
215 return -EINVAL;
216 str_seen = true;
217
218 switch (fmt_cnt) {
219 case 1:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200220 unsafe_addr = arg1;
221 arg1 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700222 break;
223 case 2:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200224 unsafe_addr = arg2;
225 arg2 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700226 break;
227 case 3:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200228 unsafe_addr = arg3;
229 arg3 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700230 break;
231 }
232 buf[0] = 0;
233 strncpy_from_unsafe(buf,
234 (void *) (long) unsafe_addr,
235 sizeof(buf));
236 }
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700237 continue;
238 }
239
240 if (fmt[i] == 'l') {
241 mod[fmt_cnt]++;
242 i++;
243 }
244
245 if (fmt[i] != 'd' && fmt[i] != 'u' && fmt[i] != 'x')
246 return -EINVAL;
247 fmt_cnt++;
248 }
249
Daniel Borkmannd6a6b6b2017-08-16 01:45:33 +0200250/* Horrid workaround for getting va_list handling working with different
251 * argument type combinations generically for 32 and 64 bit archs.
252 */
253#define __BPF_TP_EMIT() __BPF_ARG3_TP()
254#define __BPF_TP(...) \
255 __trace_printk(1 /* Fake ip will not be printed. */, \
256 fmt, ##__VA_ARGS__)
257
258#define __BPF_ARG1_TP(...) \
259 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
260 ? __BPF_TP(arg1, ##__VA_ARGS__) \
261 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
262 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
263 : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
264
265#define __BPF_ARG2_TP(...) \
266 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
267 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
268 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
269 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
270 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
271
272#define __BPF_ARG3_TP(...) \
273 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
274 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
275 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
276 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
277 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
278
279 return __BPF_TP_EMIT();
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700280}
281
282static const struct bpf_func_proto bpf_trace_printk_proto = {
283 .func = bpf_trace_printk,
284 .gpl_only = true,
285 .ret_type = RET_INTEGER,
286 .arg1_type = ARG_PTR_TO_STACK,
287 .arg2_type = ARG_CONST_STACK_SIZE,
288};
289
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700290const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
291{
292 /*
293 * this program might be calling bpf_trace_printk,
294 * so allocate per-cpu printk buffers
295 */
296 trace_printk_init_buffers();
297
298 return &bpf_trace_printk_proto;
299}
300
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200301BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
Kaixu Xia35578d72015-08-06 07:02:35 +0000302{
Kaixu Xia35578d72015-08-06 07:02:35 +0000303 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200304 unsigned int cpu = smp_processor_id();
305 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200306 struct bpf_event_entry *ee;
Kaixu Xia35578d72015-08-06 07:02:35 +0000307 struct perf_event *event;
308
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200309 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
310 return -EINVAL;
311 if (index == BPF_F_CURRENT_CPU)
312 index = cpu;
Kaixu Xia35578d72015-08-06 07:02:35 +0000313 if (unlikely(index >= array->map.max_entries))
314 return -E2BIG;
315
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200316 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200317 if (!ee)
Kaixu Xia35578d72015-08-06 07:02:35 +0000318 return -ENOENT;
319
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200320 event = ee->event;
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200321 if (unlikely(event->attr.type != PERF_TYPE_HARDWARE &&
322 event->attr.type != PERF_TYPE_RAW))
323 return -EINVAL;
324
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700325 /* make sure event is local and doesn't have pmu::count */
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200326 if (unlikely(event->oncpu != cpu || event->pmu->count))
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700327 return -EINVAL;
328
Kaixu Xia35578d72015-08-06 07:02:35 +0000329 /*
330 * we don't know if the function is run successfully by the
331 * return value. It can be judged in other places, such as
332 * eBPF programs.
333 */
334 return perf_event_read_local(event);
335}
336
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700337static const struct bpf_func_proto bpf_perf_event_read_proto = {
Kaixu Xia35578d72015-08-06 07:02:35 +0000338 .func = bpf_perf_event_read,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700339 .gpl_only = true,
Kaixu Xia35578d72015-08-06 07:02:35 +0000340 .ret_type = RET_INTEGER,
341 .arg1_type = ARG_CONST_MAP_PTR,
342 .arg2_type = ARG_ANYTHING,
343};
344
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200345static __always_inline u64
346__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
347 u64 flags, struct perf_raw_record *raw)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700348{
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700349 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmannd7931332016-06-28 12:18:24 +0200350 unsigned int cpu = smp_processor_id();
Daniel Borkmann1e337592016-04-18 21:01:23 +0200351 u64 index = flags & BPF_F_INDEX_MASK;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700352 struct perf_sample_data sample_data;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200353 struct bpf_event_entry *ee;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700354 struct perf_event *event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700355
Daniel Borkmann1e337592016-04-18 21:01:23 +0200356 if (index == BPF_F_CURRENT_CPU)
Daniel Borkmannd7931332016-06-28 12:18:24 +0200357 index = cpu;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700358 if (unlikely(index >= array->map.max_entries))
359 return -E2BIG;
360
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200361 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200362 if (!ee)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700363 return -ENOENT;
364
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200365 event = ee->event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700366 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
367 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
368 return -EINVAL;
369
Daniel Borkmannd7931332016-06-28 12:18:24 +0200370 if (unlikely(event->oncpu != cpu))
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700371 return -EOPNOTSUPP;
372
373 perf_sample_data_init(&sample_data, 0, 0);
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200374 sample_data.raw = raw;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700375 perf_event_output(event, &sample_data, regs);
376 return 0;
377}
378
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200379BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
380 u64, flags, void *, data, u64, size)
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200381{
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200382 struct perf_raw_record raw = {
383 .frag = {
384 .size = size,
385 .data = data,
386 },
387 };
388
389 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
390 return -EINVAL;
391
392 return __bpf_perf_event_output(regs, map, flags, &raw);
393}
394
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700395static const struct bpf_func_proto bpf_perf_event_output_proto = {
396 .func = bpf_perf_event_output,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700397 .gpl_only = true,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700398 .ret_type = RET_INTEGER,
399 .arg1_type = ARG_PTR_TO_CTX,
400 .arg2_type = ARG_CONST_MAP_PTR,
401 .arg3_type = ARG_ANYTHING,
402 .arg4_type = ARG_PTR_TO_STACK,
403 .arg5_type = ARG_CONST_STACK_SIZE,
404};
405
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200406static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
407
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200408u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
409 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200410{
411 struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200412 struct perf_raw_frag frag = {
413 .copy = ctx_copy,
414 .size = ctx_size,
415 .data = ctx,
416 };
417 struct perf_raw_record raw = {
418 .frag = {
Andrew Morton183fc152016-07-18 15:50:58 -0700419 {
420 .next = ctx_size ? &frag : NULL,
421 },
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200422 .size = meta_size,
423 .data = meta,
424 },
425 };
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200426
427 perf_fetch_caller_regs(regs);
428
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200429 return __bpf_perf_event_output(regs, map, flags, &raw);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200430}
431
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200432BPF_CALL_0(bpf_get_current_task)
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700433{
434 return (long) current;
435}
436
437static const struct bpf_func_proto bpf_get_current_task_proto = {
438 .func = bpf_get_current_task,
439 .gpl_only = true,
440 .ret_type = RET_INTEGER,
441};
442
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200443BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700444{
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700445 struct bpf_array *array = container_of(map, struct bpf_array, map);
446 struct cgroup *cgrp;
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700447
448 if (unlikely(in_interrupt()))
449 return -EINVAL;
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700450 if (unlikely(idx >= array->map.max_entries))
451 return -E2BIG;
452
453 cgrp = READ_ONCE(array->ptrs[idx]);
454 if (unlikely(!cgrp))
455 return -EAGAIN;
456
457 return task_under_cgroup_hierarchy(current, cgrp);
458}
459
460static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
461 .func = bpf_current_task_under_cgroup,
462 .gpl_only = false,
463 .ret_type = RET_INTEGER,
464 .arg1_type = ARG_CONST_MAP_PTR,
465 .arg2_type = ARG_ANYTHING,
466};
467
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700468static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700469{
470 switch (func_id) {
471 case BPF_FUNC_map_lookup_elem:
472 return &bpf_map_lookup_elem_proto;
473 case BPF_FUNC_map_update_elem:
474 return &bpf_map_update_elem_proto;
475 case BPF_FUNC_map_delete_elem:
476 return &bpf_map_delete_elem_proto;
477 case BPF_FUNC_probe_read:
478 return &bpf_probe_read_proto;
Joel Fernandes0a8a3e52019-06-04 17:01:00 -0400479 case BPF_FUNC_probe_read_str:
480 return &bpf_probe_read_str_proto;
Alexei Starovoitovd9847d32015-03-25 12:49:21 -0700481 case BPF_FUNC_ktime_get_ns:
482 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700483 case BPF_FUNC_tail_call:
484 return &bpf_tail_call_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700485 case BPF_FUNC_get_current_pid_tgid:
486 return &bpf_get_current_pid_tgid_proto;
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700487 case BPF_FUNC_get_current_task:
488 return &bpf_get_current_task_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700489 case BPF_FUNC_get_current_uid_gid:
490 return &bpf_get_current_uid_gid_proto;
491 case BPF_FUNC_get_current_comm:
492 return &bpf_get_current_comm_proto;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700493 case BPF_FUNC_trace_printk:
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700494 return bpf_get_trace_printk_proto();
Alexei Starovoitovab1973d2015-06-12 19:39:14 -0700495 case BPF_FUNC_get_smp_processor_id:
496 return &bpf_get_smp_processor_id_proto;
Kaixu Xia35578d72015-08-06 07:02:35 +0000497 case BPF_FUNC_perf_event_read:
498 return &bpf_perf_event_read_proto;
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700499 case BPF_FUNC_probe_write_user:
500 return bpf_get_probe_write_proto();
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700501 case BPF_FUNC_current_task_under_cgroup:
502 return &bpf_current_task_under_cgroup_proto;
Alexei Starovoitov8937bd82016-08-11 18:17:18 -0700503 case BPF_FUNC_get_prandom_u32:
504 return &bpf_get_prandom_u32_proto;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700505 default:
506 return NULL;
507 }
508}
509
510static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
511{
512 switch (func_id) {
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700513 case BPF_FUNC_perf_event_output:
514 return &bpf_perf_event_output_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800515 case BPF_FUNC_get_stackid:
516 return &bpf_get_stackid_proto;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700517 default:
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700518 return tracing_func_proto(func_id);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700519 }
520}
521
522/* bpf+kprobe programs can access fields of 'struct pt_regs' */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700523static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
524 enum bpf_reg_type *reg_type)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700525{
Alexei Starovoitov25415172015-03-25 12:49:20 -0700526 if (off < 0 || off >= sizeof(struct pt_regs))
527 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700528 if (type != BPF_READ)
529 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700530 if (off % size != 0)
531 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700532 return true;
533}
534
Julia Lawall27dff4e2015-12-11 18:35:59 +0100535static const struct bpf_verifier_ops kprobe_prog_ops = {
Alexei Starovoitov25415172015-03-25 12:49:20 -0700536 .get_func_proto = kprobe_prog_func_proto,
537 .is_valid_access = kprobe_prog_is_valid_access,
538};
539
540static struct bpf_prog_type_list kprobe_tl = {
541 .ops = &kprobe_prog_ops,
542 .type = BPF_PROG_TYPE_KPROBE,
543};
544
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200545BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
546 u64, flags, void *, data, u64, size)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700547{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200548 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
549
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700550 /*
551 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
552 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200553 * from there and call the same bpf_perf_event_output() helper inline.
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700554 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200555 return ____bpf_perf_event_output(regs, map, flags, data, size);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700556}
557
558static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
559 .func = bpf_perf_event_output_tp,
560 .gpl_only = true,
561 .ret_type = RET_INTEGER,
562 .arg1_type = ARG_PTR_TO_CTX,
563 .arg2_type = ARG_CONST_MAP_PTR,
564 .arg3_type = ARG_ANYTHING,
565 .arg4_type = ARG_PTR_TO_STACK,
566 .arg5_type = ARG_CONST_STACK_SIZE,
567};
568
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200569BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
570 u64, flags)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700571{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200572 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700573
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200574 /*
575 * Same comment as in bpf_perf_event_output_tp(), only that this time
576 * the other helper's function body cannot be inlined due to being
577 * external, thus we need to call raw helper function.
578 */
579 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
580 flags, 0, 0);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700581}
582
583static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
584 .func = bpf_get_stackid_tp,
585 .gpl_only = true,
586 .ret_type = RET_INTEGER,
587 .arg1_type = ARG_PTR_TO_CTX,
588 .arg2_type = ARG_CONST_MAP_PTR,
589 .arg3_type = ARG_ANYTHING,
590};
591
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700592static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
593{
594 switch (func_id) {
595 case BPF_FUNC_perf_event_output:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700596 return &bpf_perf_event_output_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700597 case BPF_FUNC_get_stackid:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700598 return &bpf_get_stackid_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700599 default:
600 return tracing_func_proto(func_id);
601 }
602}
603
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700604static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
605 enum bpf_reg_type *reg_type)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700606{
607 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
608 return false;
609 if (type != BPF_READ)
610 return false;
611 if (off % size != 0)
612 return false;
613 return true;
614}
615
616static const struct bpf_verifier_ops tracepoint_prog_ops = {
617 .get_func_proto = tp_prog_func_proto,
618 .is_valid_access = tp_prog_is_valid_access,
619};
620
621static struct bpf_prog_type_list tracepoint_tl = {
622 .ops = &tracepoint_prog_ops,
623 .type = BPF_PROG_TYPE_TRACEPOINT,
624};
625
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700626static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
627 enum bpf_reg_type *reg_type)
628{
629 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
630 return false;
631 if (type != BPF_READ)
632 return false;
633 if (off % size != 0)
634 return false;
635 if (off == offsetof(struct bpf_perf_event_data, sample_period)) {
636 if (size != sizeof(u64))
637 return false;
638 } else {
639 if (size != sizeof(long))
640 return false;
641 }
642 return true;
643}
644
645static u32 pe_prog_convert_ctx_access(enum bpf_access_type type, int dst_reg,
646 int src_reg, int ctx_off,
647 struct bpf_insn *insn_buf,
648 struct bpf_prog *prog)
649{
650 struct bpf_insn *insn = insn_buf;
651
652 switch (ctx_off) {
653 case offsetof(struct bpf_perf_event_data, sample_period):
654 BUILD_BUG_ON(FIELD_SIZEOF(struct perf_sample_data, period) != sizeof(u64));
Daniel Borkmannf035a512016-09-09 02:45:29 +0200655
656 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
657 data), dst_reg, src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700658 offsetof(struct bpf_perf_event_data_kern, data));
659 *insn++ = BPF_LDX_MEM(BPF_DW, dst_reg, dst_reg,
660 offsetof(struct perf_sample_data, period));
661 break;
662 default:
Daniel Borkmannf035a512016-09-09 02:45:29 +0200663 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
664 regs), dst_reg, src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700665 offsetof(struct bpf_perf_event_data_kern, regs));
Daniel Borkmannf035a512016-09-09 02:45:29 +0200666 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), dst_reg, dst_reg, ctx_off);
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700667 break;
668 }
669
670 return insn - insn_buf;
671}
672
673static const struct bpf_verifier_ops perf_event_prog_ops = {
674 .get_func_proto = tp_prog_func_proto,
675 .is_valid_access = pe_prog_is_valid_access,
676 .convert_ctx_access = pe_prog_convert_ctx_access,
677};
678
Yonghong Song265229d2017-10-23 23:53:08 -0700679static DEFINE_MUTEX(bpf_event_mutex);
680
681int perf_event_attach_bpf_prog(struct perf_event *event,
682 struct bpf_prog *prog)
683{
684 struct bpf_prog_array __rcu *old_array;
685 struct bpf_prog_array *new_array;
686 int ret = -EEXIST;
687
688 mutex_lock(&bpf_event_mutex);
689
690 if (event->prog)
691 goto out;
692
693 old_array = rcu_dereference_protected(event->tp_event->prog_array,
694 lockdep_is_held(&bpf_event_mutex));
695 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
696 if (ret < 0)
697 goto out;
698
699 /* set the new array to event->tp_event and set event->prog */
700 event->prog = prog;
701 rcu_assign_pointer(event->tp_event->prog_array, new_array);
702 bpf_prog_array_free(old_array);
703
704out:
705 mutex_unlock(&bpf_event_mutex);
706 return ret;
707}
708
709void perf_event_detach_bpf_prog(struct perf_event *event)
710{
711 struct bpf_prog_array __rcu *old_array;
712 struct bpf_prog_array *new_array;
713 int ret;
714
715 mutex_lock(&bpf_event_mutex);
716
717 if (!event->prog)
718 goto out;
719
720 old_array = rcu_dereference_protected(event->tp_event->prog_array,
721 lockdep_is_held(&bpf_event_mutex));
722
723 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
724 if (ret < 0) {
725 bpf_prog_array_delete_safe(old_array, event->prog);
726 } else {
727 rcu_assign_pointer(event->tp_event->prog_array, new_array);
728 bpf_prog_array_free(old_array);
729 }
730
731 bpf_prog_put(event->prog);
732 event->prog = NULL;
733
734out:
735 mutex_unlock(&bpf_event_mutex);
736}
737
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700738static struct bpf_prog_type_list perf_event_tl = {
739 .ops = &perf_event_prog_ops,
740 .type = BPF_PROG_TYPE_PERF_EVENT,
741};
742
Alexei Starovoitov25415172015-03-25 12:49:20 -0700743static int __init register_kprobe_prog_ops(void)
744{
745 bpf_register_prog_type(&kprobe_tl);
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700746 bpf_register_prog_type(&tracepoint_tl);
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700747 bpf_register_prog_type(&perf_event_tl);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700748 return 0;
749}
750late_initcall(register_kprobe_prog_ops);