blob: 4266ffde07caed42a2c704355665a4f5f7948007 [file] [log] [blame]
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -08001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 */
12#include <linux/bpf.h>
13#include <linux/rcupdate.h>
Daniel Borkmann03e69b52015-03-14 02:27:16 +010014#include <linux/random.h>
Daniel Borkmannc04167c2015-03-14 02:27:17 +010015#include <linux/smp.h>
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +020016#include <linux/topology.h>
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +020017#include <linux/ktime.h>
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070018#include <linux/sched.h>
19#include <linux/uidgid.h>
Daniel Borkmannf3694e02016-09-09 02:45:31 +020020#include <linux/filter.h>
Andrey Ignatovd7a4cb92019-03-18 17:55:26 -070021#include <linux/ctype.h>
22
23#include "../../lib/kstrtox.h"
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080024
25/* If kernel subsystem is allowing eBPF programs to call this function,
26 * inside its own verifier_ops->get_func_proto() callback it should return
27 * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
28 *
29 * Different map implementations will rely on rcu in map methods
30 * lookup/update/delete, therefore eBPF programs must run under rcu lock
31 * if program is allowed to access maps, so check rcu_read_lock_held in
32 * all three functions.
33 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +020034BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key)
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080035{
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080036 WARN_ON_ONCE(!rcu_read_lock_held());
Daniel Borkmannf3694e02016-09-09 02:45:31 +020037 return (unsigned long) map->ops->map_lookup_elem(map, key);
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080038}
39
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +010040const struct bpf_func_proto bpf_map_lookup_elem_proto = {
Daniel Borkmann3324b582015-05-29 23:23:07 +020041 .func = bpf_map_lookup_elem,
42 .gpl_only = false,
Daniel Borkmann36bbef52016-09-20 00:26:13 +020043 .pkt_access = true,
Daniel Borkmann3324b582015-05-29 23:23:07 +020044 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
45 .arg1_type = ARG_CONST_MAP_PTR,
46 .arg2_type = ARG_PTR_TO_MAP_KEY,
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080047};
48
Daniel Borkmannf3694e02016-09-09 02:45:31 +020049BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
50 void *, value, u64, flags)
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080051{
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080052 WARN_ON_ONCE(!rcu_read_lock_held());
Daniel Borkmannf3694e02016-09-09 02:45:31 +020053 return map->ops->map_update_elem(map, key, value, flags);
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080054}
55
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +010056const struct bpf_func_proto bpf_map_update_elem_proto = {
Daniel Borkmann3324b582015-05-29 23:23:07 +020057 .func = bpf_map_update_elem,
58 .gpl_only = false,
Daniel Borkmann36bbef52016-09-20 00:26:13 +020059 .pkt_access = true,
Daniel Borkmann3324b582015-05-29 23:23:07 +020060 .ret_type = RET_INTEGER,
61 .arg1_type = ARG_CONST_MAP_PTR,
62 .arg2_type = ARG_PTR_TO_MAP_KEY,
63 .arg3_type = ARG_PTR_TO_MAP_VALUE,
64 .arg4_type = ARG_ANYTHING,
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080065};
66
Daniel Borkmannf3694e02016-09-09 02:45:31 +020067BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key)
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080068{
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080069 WARN_ON_ONCE(!rcu_read_lock_held());
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080070 return map->ops->map_delete_elem(map, key);
71}
72
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +010073const struct bpf_func_proto bpf_map_delete_elem_proto = {
Daniel Borkmann3324b582015-05-29 23:23:07 +020074 .func = bpf_map_delete_elem,
75 .gpl_only = false,
Daniel Borkmann36bbef52016-09-20 00:26:13 +020076 .pkt_access = true,
Daniel Borkmann3324b582015-05-29 23:23:07 +020077 .ret_type = RET_INTEGER,
78 .arg1_type = ARG_CONST_MAP_PTR,
79 .arg2_type = ARG_PTR_TO_MAP_KEY,
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080080};
Daniel Borkmann03e69b52015-03-14 02:27:16 +010081
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +020082BPF_CALL_3(bpf_map_push_elem, struct bpf_map *, map, void *, value, u64, flags)
83{
84 return map->ops->map_push_elem(map, value, flags);
85}
86
87const struct bpf_func_proto bpf_map_push_elem_proto = {
88 .func = bpf_map_push_elem,
89 .gpl_only = false,
90 .pkt_access = true,
91 .ret_type = RET_INTEGER,
92 .arg1_type = ARG_CONST_MAP_PTR,
93 .arg2_type = ARG_PTR_TO_MAP_VALUE,
94 .arg3_type = ARG_ANYTHING,
95};
96
97BPF_CALL_2(bpf_map_pop_elem, struct bpf_map *, map, void *, value)
98{
99 return map->ops->map_pop_elem(map, value);
100}
101
102const struct bpf_func_proto bpf_map_pop_elem_proto = {
103 .func = bpf_map_pop_elem,
104 .gpl_only = false,
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200105 .ret_type = RET_INTEGER,
106 .arg1_type = ARG_CONST_MAP_PTR,
107 .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE,
108};
109
110BPF_CALL_2(bpf_map_peek_elem, struct bpf_map *, map, void *, value)
111{
112 return map->ops->map_peek_elem(map, value);
113}
114
115const struct bpf_func_proto bpf_map_peek_elem_proto = {
116 .func = bpf_map_pop_elem,
117 .gpl_only = false,
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200118 .ret_type = RET_INTEGER,
119 .arg1_type = ARG_CONST_MAP_PTR,
120 .arg2_type = ARG_PTR_TO_UNINIT_MAP_VALUE,
121};
122
Daniel Borkmann03e69b52015-03-14 02:27:16 +0100123const struct bpf_func_proto bpf_get_prandom_u32_proto = {
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200124 .func = bpf_user_rnd_u32,
Daniel Borkmann03e69b52015-03-14 02:27:16 +0100125 .gpl_only = false,
126 .ret_type = RET_INTEGER,
127};
Daniel Borkmannc04167c2015-03-14 02:27:17 +0100128
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200129BPF_CALL_0(bpf_get_smp_processor_id)
Daniel Borkmannc04167c2015-03-14 02:27:17 +0100130{
Daniel Borkmann80b48c42016-06-28 12:18:26 +0200131 return smp_processor_id();
Daniel Borkmannc04167c2015-03-14 02:27:17 +0100132}
133
134const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
135 .func = bpf_get_smp_processor_id,
136 .gpl_only = false,
137 .ret_type = RET_INTEGER,
138};
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +0200139
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +0200140BPF_CALL_0(bpf_get_numa_node_id)
141{
142 return numa_node_id();
143}
144
145const struct bpf_func_proto bpf_get_numa_node_id_proto = {
146 .func = bpf_get_numa_node_id,
147 .gpl_only = false,
148 .ret_type = RET_INTEGER,
149};
150
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200151BPF_CALL_0(bpf_ktime_get_ns)
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +0200152{
153 /* NMI safe access to clock monotonic */
154 return ktime_get_mono_fast_ns();
155}
156
157const struct bpf_func_proto bpf_ktime_get_ns_proto = {
158 .func = bpf_ktime_get_ns,
159 .gpl_only = true,
160 .ret_type = RET_INTEGER,
161};
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700162
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200163BPF_CALL_0(bpf_get_current_pid_tgid)
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700164{
165 struct task_struct *task = current;
166
Daniel Borkmann6088b582016-09-09 02:45:28 +0200167 if (unlikely(!task))
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700168 return -EINVAL;
169
170 return (u64) task->tgid << 32 | task->pid;
171}
172
173const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
174 .func = bpf_get_current_pid_tgid,
175 .gpl_only = false,
176 .ret_type = RET_INTEGER,
177};
178
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200179BPF_CALL_0(bpf_get_current_uid_gid)
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700180{
181 struct task_struct *task = current;
182 kuid_t uid;
183 kgid_t gid;
184
Daniel Borkmann6088b582016-09-09 02:45:28 +0200185 if (unlikely(!task))
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700186 return -EINVAL;
187
188 current_uid_gid(&uid, &gid);
189 return (u64) from_kgid(&init_user_ns, gid) << 32 |
Daniel Borkmann6088b582016-09-09 02:45:28 +0200190 from_kuid(&init_user_ns, uid);
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700191}
192
193const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
194 .func = bpf_get_current_uid_gid,
195 .gpl_only = false,
196 .ret_type = RET_INTEGER,
197};
198
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200199BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700200{
201 struct task_struct *task = current;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700202
Daniel Borkmann074f528e2016-04-13 00:10:52 +0200203 if (unlikely(!task))
204 goto err_clear;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700205
Daniel Borkmann074f528e2016-04-13 00:10:52 +0200206 strncpy(buf, task->comm, size);
207
208 /* Verifier guarantees that size > 0. For task->comm exceeding
209 * size, guarantee that buf is %NUL-terminated. Unconditionally
210 * done here to save the size test.
211 */
212 buf[size - 1] = 0;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700213 return 0;
Daniel Borkmann074f528e2016-04-13 00:10:52 +0200214err_clear:
215 memset(buf, 0, size);
216 return -EINVAL;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700217}
218
219const struct bpf_func_proto bpf_get_current_comm_proto = {
220 .func = bpf_get_current_comm,
221 .gpl_only = false,
222 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800223 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
224 .arg2_type = ARG_CONST_SIZE,
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700225};
Yonghong Songbf6fa2c82018-06-03 15:59:41 -0700226
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800227#if defined(CONFIG_QUEUED_SPINLOCKS) || defined(CONFIG_BPF_ARCH_SPINLOCK)
228
229static inline void __bpf_spin_lock(struct bpf_spin_lock *lock)
230{
231 arch_spinlock_t *l = (void *)lock;
232 union {
233 __u32 val;
234 arch_spinlock_t lock;
235 } u = { .lock = __ARCH_SPIN_LOCK_UNLOCKED };
236
237 compiletime_assert(u.val == 0, "__ARCH_SPIN_LOCK_UNLOCKED not 0");
238 BUILD_BUG_ON(sizeof(*l) != sizeof(__u32));
239 BUILD_BUG_ON(sizeof(*lock) != sizeof(__u32));
240 arch_spin_lock(l);
241}
242
243static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock)
244{
245 arch_spinlock_t *l = (void *)lock;
246
247 arch_spin_unlock(l);
248}
249
250#else
251
252static inline void __bpf_spin_lock(struct bpf_spin_lock *lock)
253{
254 atomic_t *l = (void *)lock;
255
256 BUILD_BUG_ON(sizeof(*l) != sizeof(*lock));
257 do {
258 atomic_cond_read_relaxed(l, !VAL);
259 } while (atomic_xchg(l, 1));
260}
261
262static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock)
263{
264 atomic_t *l = (void *)lock;
265
266 atomic_set_release(l, 0);
267}
268
269#endif
270
271static DEFINE_PER_CPU(unsigned long, irqsave_flags);
272
273notrace BPF_CALL_1(bpf_spin_lock, struct bpf_spin_lock *, lock)
274{
275 unsigned long flags;
276
277 local_irq_save(flags);
278 __bpf_spin_lock(lock);
279 __this_cpu_write(irqsave_flags, flags);
280 return 0;
281}
282
283const struct bpf_func_proto bpf_spin_lock_proto = {
284 .func = bpf_spin_lock,
285 .gpl_only = false,
286 .ret_type = RET_VOID,
287 .arg1_type = ARG_PTR_TO_SPIN_LOCK,
288};
289
290notrace BPF_CALL_1(bpf_spin_unlock, struct bpf_spin_lock *, lock)
291{
292 unsigned long flags;
293
294 flags = __this_cpu_read(irqsave_flags);
295 __bpf_spin_unlock(lock);
296 local_irq_restore(flags);
297 return 0;
298}
299
300const struct bpf_func_proto bpf_spin_unlock_proto = {
301 .func = bpf_spin_unlock,
302 .gpl_only = false,
303 .ret_type = RET_VOID,
304 .arg1_type = ARG_PTR_TO_SPIN_LOCK,
305};
306
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800307void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
308 bool lock_src)
309{
310 struct bpf_spin_lock *lock;
311
312 if (lock_src)
313 lock = src + map->spin_lock_off;
314 else
315 lock = dst + map->spin_lock_off;
316 preempt_disable();
317 ____bpf_spin_lock(lock);
318 copy_map_value(map, dst, src);
319 ____bpf_spin_unlock(lock);
320 preempt_enable();
321}
322
Yonghong Songbf6fa2c82018-06-03 15:59:41 -0700323#ifdef CONFIG_CGROUPS
324BPF_CALL_0(bpf_get_current_cgroup_id)
325{
326 struct cgroup *cgrp = task_dfl_cgroup(current);
327
328 return cgrp->kn->id.id;
329}
330
331const struct bpf_func_proto bpf_get_current_cgroup_id_proto = {
332 .func = bpf_get_current_cgroup_id,
333 .gpl_only = false,
334 .ret_type = RET_INTEGER,
335};
Roman Gushchincd339432018-08-02 14:27:24 -0700336
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000337#ifdef CONFIG_CGROUP_BPF
Roman Gushchinf294b372018-09-28 14:45:40 +0000338DECLARE_PER_CPU(struct bpf_cgroup_storage*,
339 bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);
Roman Gushchincd339432018-08-02 14:27:24 -0700340
341BPF_CALL_2(bpf_get_local_storage, struct bpf_map *, map, u64, flags)
342{
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000343 /* flags argument is not used now,
344 * but provides an ability to extend the API.
345 * verifier checks that its value is correct.
Roman Gushchincd339432018-08-02 14:27:24 -0700346 */
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000347 enum bpf_cgroup_storage_type stype = cgroup_storage_type(map);
Roman Gushchinf294b372018-09-28 14:45:40 +0000348 struct bpf_cgroup_storage *storage;
Roman Gushchinb741f162018-09-28 14:45:43 +0000349 void *ptr;
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000350
Roman Gushchinf294b372018-09-28 14:45:40 +0000351 storage = this_cpu_read(bpf_cgroup_storage[stype]);
352
Roman Gushchinb741f162018-09-28 14:45:43 +0000353 if (stype == BPF_CGROUP_STORAGE_SHARED)
354 ptr = &READ_ONCE(storage->buf)->data[0];
355 else
356 ptr = this_cpu_ptr(storage->percpu_buf);
357
358 return (unsigned long)ptr;
Roman Gushchincd339432018-08-02 14:27:24 -0700359}
360
361const struct bpf_func_proto bpf_get_local_storage_proto = {
362 .func = bpf_get_local_storage,
363 .gpl_only = false,
364 .ret_type = RET_PTR_TO_MAP_VALUE,
365 .arg1_type = ARG_CONST_MAP_PTR,
366 .arg2_type = ARG_ANYTHING,
367};
Yonghong Songbf6fa2c82018-06-03 15:59:41 -0700368#endif
Andrey Ignatovd7a4cb92019-03-18 17:55:26 -0700369
370#define BPF_STRTOX_BASE_MASK 0x1F
371
372static int __bpf_strtoull(const char *buf, size_t buf_len, u64 flags,
373 unsigned long long *res, bool *is_negative)
374{
375 unsigned int base = flags & BPF_STRTOX_BASE_MASK;
376 const char *cur_buf = buf;
377 size_t cur_len = buf_len;
378 unsigned int consumed;
379 size_t val_len;
380 char str[64];
381
382 if (!buf || !buf_len || !res || !is_negative)
383 return -EINVAL;
384
385 if (base != 0 && base != 8 && base != 10 && base != 16)
386 return -EINVAL;
387
388 if (flags & ~BPF_STRTOX_BASE_MASK)
389 return -EINVAL;
390
391 while (cur_buf < buf + buf_len && isspace(*cur_buf))
392 ++cur_buf;
393
394 *is_negative = (cur_buf < buf + buf_len && *cur_buf == '-');
395 if (*is_negative)
396 ++cur_buf;
397
398 consumed = cur_buf - buf;
399 cur_len -= consumed;
400 if (!cur_len)
401 return -EINVAL;
402
403 cur_len = min(cur_len, sizeof(str) - 1);
404 memcpy(str, cur_buf, cur_len);
405 str[cur_len] = '\0';
406 cur_buf = str;
407
408 cur_buf = _parse_integer_fixup_radix(cur_buf, &base);
409 val_len = _parse_integer(cur_buf, base, res);
410
411 if (val_len & KSTRTOX_OVERFLOW)
412 return -ERANGE;
413
414 if (val_len == 0)
415 return -EINVAL;
416
417 cur_buf += val_len;
418 consumed += cur_buf - str;
419
420 return consumed;
421}
422
423static int __bpf_strtoll(const char *buf, size_t buf_len, u64 flags,
424 long long *res)
425{
426 unsigned long long _res;
427 bool is_negative;
428 int err;
429
430 err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
431 if (err < 0)
432 return err;
433 if (is_negative) {
434 if ((long long)-_res > 0)
435 return -ERANGE;
436 *res = -_res;
437 } else {
438 if ((long long)_res < 0)
439 return -ERANGE;
440 *res = _res;
441 }
442 return err;
443}
444
445BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags,
446 long *, res)
447{
448 long long _res;
449 int err;
450
451 err = __bpf_strtoll(buf, buf_len, flags, &_res);
452 if (err < 0)
453 return err;
454 if (_res != (long)_res)
455 return -ERANGE;
456 *res = _res;
457 return err;
458}
459
460const struct bpf_func_proto bpf_strtol_proto = {
461 .func = bpf_strtol,
462 .gpl_only = false,
463 .ret_type = RET_INTEGER,
464 .arg1_type = ARG_PTR_TO_MEM,
465 .arg2_type = ARG_CONST_SIZE,
466 .arg3_type = ARG_ANYTHING,
467 .arg4_type = ARG_PTR_TO_LONG,
468};
469
470BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,
471 unsigned long *, res)
472{
473 unsigned long long _res;
474 bool is_negative;
475 int err;
476
477 err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
478 if (err < 0)
479 return err;
480 if (is_negative)
481 return -EINVAL;
482 if (_res != (unsigned long)_res)
483 return -ERANGE;
484 *res = _res;
485 return err;
486}
487
488const struct bpf_func_proto bpf_strtoul_proto = {
489 .func = bpf_strtoul,
490 .gpl_only = false,
491 .ret_type = RET_INTEGER,
492 .arg1_type = ARG_PTR_TO_MEM,
493 .arg2_type = ARG_CONST_SIZE,
494 .arg3_type = ARG_ANYTHING,
495 .arg4_type = ARG_PTR_TO_LONG,
496};
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000497#endif