1 /* 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/bpf_trace.h>
14 #include <linux/syscalls.h>
15 #include <linux/slab.h>
16 #include <linux/sched/signal.h>
17 #include <linux/vmalloc.h>
18 #include <linux/mmzone.h>
19 #include <linux/anon_inodes.h>
20 #include <linux/file.h>
21 #include <linux/license.h>
22 #include <linux/filter.h>
23 #include <linux/version.h>
24 #include <linux/kernel.h>
25 #include <linux/idr.h>
26
27 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
28 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
29 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
30 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
31 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
32 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
33
34 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
35
36 DEFINE_PER_CPU(int, bpf_prog_active);
37 static DEFINE_IDR(prog_idr);
38 static DEFINE_SPINLOCK(prog_idr_lock);
39 static DEFINE_IDR(map_idr);
40 static DEFINE_SPINLOCK(map_idr_lock);
41
42 int sysctl_unprivileged_bpf_disabled __read_mostly;
43
44 static const struct bpf_map_ops * const bpf_map_types[] = {
45 #define BPF_PROG_TYPE(_id, _ops)
46 #define BPF_MAP_TYPE(_id, _ops) \
47 [_id] = &_ops,
48 #include <linux/bpf_types.h>
49 #undef BPF_PROG_TYPE
50 #undef BPF_MAP_TYPE
51 };
52
53 /*
54 * If we're handed a bigger struct than we know of, ensure all the unknown bits
55 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
56 * we don't know about yet.
57 *
58 * There is a ToCToU between this function call and the following
59 * copy_from_user() call. However, this is not a concern since this function is
60 * meant to be a future-proofing of bits.
61 */
check_uarg_tail_zero(void __user * uaddr,size_t expected_size,size_t actual_size)62 static int check_uarg_tail_zero(void __user *uaddr,
63 size_t expected_size,
64 size_t actual_size)
65 {
66 unsigned char __user *addr;
67 unsigned char __user *end;
68 unsigned char val;
69 int err;
70
71 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
72 return -E2BIG;
73
74 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
75 return -EFAULT;
76
77 if (actual_size <= expected_size)
78 return 0;
79
80 addr = uaddr + expected_size;
81 end = uaddr + actual_size;
82
83 for (; addr < end; addr++) {
84 err = get_user(val, addr);
85 if (err)
86 return err;
87 if (val)
88 return -E2BIG;
89 }
90
91 return 0;
92 }
93
find_and_alloc_map(union bpf_attr * attr)94 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
95 {
96 struct bpf_map *map;
97
98 if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
99 !bpf_map_types[attr->map_type])
100 return ERR_PTR(-EINVAL);
101
102 map = bpf_map_types[attr->map_type]->map_alloc(attr);
103 if (IS_ERR(map))
104 return map;
105 map->ops = bpf_map_types[attr->map_type];
106 map->map_type = attr->map_type;
107 return map;
108 }
109
bpf_map_area_alloc(size_t size,int numa_node)110 void *bpf_map_area_alloc(size_t size, int numa_node)
111 {
112 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
113 * trigger under memory pressure as we really just want to
114 * fail instead.
115 */
116 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
117 void *area;
118
119 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
120 area = kmalloc_node(size, GFP_USER | flags, numa_node);
121 if (area != NULL)
122 return area;
123 }
124
125 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
126 __builtin_return_address(0));
127 }
128
bpf_map_area_free(void * area)129 void bpf_map_area_free(void *area)
130 {
131 kvfree(area);
132 }
133
bpf_map_precharge_memlock(u32 pages)134 int bpf_map_precharge_memlock(u32 pages)
135 {
136 struct user_struct *user = get_current_user();
137 unsigned long memlock_limit, cur;
138
139 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
140 cur = atomic_long_read(&user->locked_vm);
141 free_uid(user);
142 if (cur + pages > memlock_limit)
143 return -EPERM;
144 return 0;
145 }
146
bpf_map_charge_memlock(struct bpf_map * map)147 static int bpf_map_charge_memlock(struct bpf_map *map)
148 {
149 struct user_struct *user = get_current_user();
150 unsigned long memlock_limit;
151
152 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
153
154 atomic_long_add(map->pages, &user->locked_vm);
155
156 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
157 atomic_long_sub(map->pages, &user->locked_vm);
158 free_uid(user);
159 return -EPERM;
160 }
161 map->user = user;
162 return 0;
163 }
164
bpf_map_uncharge_memlock(struct bpf_map * map)165 static void bpf_map_uncharge_memlock(struct bpf_map *map)
166 {
167 struct user_struct *user = map->user;
168
169 atomic_long_sub(map->pages, &user->locked_vm);
170 free_uid(user);
171 }
172
bpf_map_alloc_id(struct bpf_map * map)173 static int bpf_map_alloc_id(struct bpf_map *map)
174 {
175 int id;
176
177 spin_lock_bh(&map_idr_lock);
178 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
179 if (id > 0)
180 map->id = id;
181 spin_unlock_bh(&map_idr_lock);
182
183 if (WARN_ON_ONCE(!id))
184 return -ENOSPC;
185
186 return id > 0 ? 0 : id;
187 }
188
bpf_map_free_id(struct bpf_map * map,bool do_idr_lock)189 static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
190 {
191 unsigned long flags;
192
193 if (do_idr_lock)
194 spin_lock_irqsave(&map_idr_lock, flags);
195 else
196 __acquire(&map_idr_lock);
197
198 idr_remove(&map_idr, map->id);
199
200 if (do_idr_lock)
201 spin_unlock_irqrestore(&map_idr_lock, flags);
202 else
203 __release(&map_idr_lock);
204 }
205
206 /* called from workqueue */
bpf_map_free_deferred(struct work_struct * work)207 static void bpf_map_free_deferred(struct work_struct *work)
208 {
209 struct bpf_map *map = container_of(work, struct bpf_map, work);
210
211 bpf_map_uncharge_memlock(map);
212 security_bpf_map_free(map);
213 /* implementation dependent freeing */
214 map->ops->map_free(map);
215 }
216
bpf_map_put_uref(struct bpf_map * map)217 static void bpf_map_put_uref(struct bpf_map *map)
218 {
219 if (atomic_dec_and_test(&map->usercnt)) {
220 if (map->ops->map_release_uref)
221 map->ops->map_release_uref(map);
222 }
223 }
224
225 /* decrement map refcnt and schedule it for freeing via workqueue
226 * (unrelying map implementation ops->map_free() might sleep)
227 */
__bpf_map_put(struct bpf_map * map,bool do_idr_lock)228 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
229 {
230 if (atomic_dec_and_test(&map->refcnt)) {
231 /* bpf_map_free_id() must be called first */
232 bpf_map_free_id(map, do_idr_lock);
233 INIT_WORK(&map->work, bpf_map_free_deferred);
234 schedule_work(&map->work);
235 }
236 }
237
bpf_map_put(struct bpf_map * map)238 void bpf_map_put(struct bpf_map *map)
239 {
240 __bpf_map_put(map, true);
241 }
242
bpf_map_put_with_uref(struct bpf_map * map)243 void bpf_map_put_with_uref(struct bpf_map *map)
244 {
245 bpf_map_put_uref(map);
246 bpf_map_put(map);
247 }
248
bpf_map_release(struct inode * inode,struct file * filp)249 static int bpf_map_release(struct inode *inode, struct file *filp)
250 {
251 struct bpf_map *map = filp->private_data;
252
253 if (map->ops->map_release)
254 map->ops->map_release(map, filp);
255
256 bpf_map_put_with_uref(map);
257 return 0;
258 }
259
260 #ifdef CONFIG_PROC_FS
bpf_map_show_fdinfo(struct seq_file * m,struct file * filp)261 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
262 {
263 const struct bpf_map *map = filp->private_data;
264 const struct bpf_array *array;
265 u32 owner_prog_type = 0;
266 u32 owner_jited = 0;
267
268 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
269 array = container_of(map, struct bpf_array, map);
270 owner_prog_type = array->owner_prog_type;
271 owner_jited = array->owner_jited;
272 }
273
274 seq_printf(m,
275 "map_type:\t%u\n"
276 "key_size:\t%u\n"
277 "value_size:\t%u\n"
278 "max_entries:\t%u\n"
279 "map_flags:\t%#x\n"
280 "memlock:\t%llu\n",
281 map->map_type,
282 map->key_size,
283 map->value_size,
284 map->max_entries,
285 map->map_flags,
286 map->pages * 1ULL << PAGE_SHIFT);
287
288 if (owner_prog_type) {
289 seq_printf(m, "owner_prog_type:\t%u\n",
290 owner_prog_type);
291 seq_printf(m, "owner_jited:\t%u\n",
292 owner_jited);
293 }
294 }
295 #endif
296
bpf_dummy_read(struct file * filp,char __user * buf,size_t siz,loff_t * ppos)297 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
298 loff_t *ppos)
299 {
300 /* We need this handler such that alloc_file() enables
301 * f_mode with FMODE_CAN_READ.
302 */
303 return -EINVAL;
304 }
305
bpf_dummy_write(struct file * filp,const char __user * buf,size_t siz,loff_t * ppos)306 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
307 size_t siz, loff_t *ppos)
308 {
309 /* We need this handler such that alloc_file() enables
310 * f_mode with FMODE_CAN_WRITE.
311 */
312 return -EINVAL;
313 }
314
315 const struct file_operations bpf_map_fops = {
316 #ifdef CONFIG_PROC_FS
317 .show_fdinfo = bpf_map_show_fdinfo,
318 #endif
319 .release = bpf_map_release,
320 .read = bpf_dummy_read,
321 .write = bpf_dummy_write,
322 };
323
bpf_map_new_fd(struct bpf_map * map,int flags)324 int bpf_map_new_fd(struct bpf_map *map, int flags)
325 {
326 int ret;
327
328 ret = security_bpf_map(map, OPEN_FMODE(flags));
329 if (ret < 0)
330 return ret;
331
332 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
333 flags | O_CLOEXEC);
334 }
335
bpf_get_file_flag(int flags)336 int bpf_get_file_flag(int flags)
337 {
338 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
339 return -EINVAL;
340 if (flags & BPF_F_RDONLY)
341 return O_RDONLY;
342 if (flags & BPF_F_WRONLY)
343 return O_WRONLY;
344 return O_RDWR;
345 }
346
347 /* helper macro to check that unused fields 'union bpf_attr' are zero */
348 #define CHECK_ATTR(CMD) \
349 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
350 sizeof(attr->CMD##_LAST_FIELD), 0, \
351 sizeof(*attr) - \
352 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
353 sizeof(attr->CMD##_LAST_FIELD)) != NULL
354
355 #define BPF_MAP_CREATE_LAST_FIELD numa_node
356 /* called via syscall */
map_create(union bpf_attr * attr)357 static int map_create(union bpf_attr *attr)
358 {
359 int numa_node = bpf_map_attr_numa_node(attr);
360 struct bpf_map *map;
361 int f_flags;
362 int err;
363
364 err = CHECK_ATTR(BPF_MAP_CREATE);
365 if (err)
366 return -EINVAL;
367
368 f_flags = bpf_get_file_flag(attr->map_flags);
369 if (f_flags < 0)
370 return f_flags;
371
372 if (numa_node != NUMA_NO_NODE &&
373 ((unsigned int)numa_node >= nr_node_ids ||
374 !node_online(numa_node)))
375 return -EINVAL;
376
377 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
378 map = find_and_alloc_map(attr);
379 if (IS_ERR(map))
380 return PTR_ERR(map);
381
382 atomic_set(&map->refcnt, 1);
383 atomic_set(&map->usercnt, 1);
384
385 err = security_bpf_map_alloc(map);
386 if (err)
387 goto free_map_nouncharge;
388
389 err = bpf_map_charge_memlock(map);
390 if (err)
391 goto free_map_sec;
392
393 err = bpf_map_alloc_id(map);
394 if (err)
395 goto free_map;
396
397 err = bpf_map_new_fd(map, f_flags);
398 if (err < 0) {
399 /* failed to allocate fd.
400 * bpf_map_put_with_uref() is needed because the above
401 * bpf_map_alloc_id() has published the map
402 * to the userspace and the userspace may
403 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
404 */
405 bpf_map_put_with_uref(map);
406 return err;
407 }
408
409 trace_bpf_map_create(map, err);
410 return err;
411
412 free_map:
413 bpf_map_uncharge_memlock(map);
414 free_map_sec:
415 security_bpf_map_free(map);
416 free_map_nouncharge:
417 map->ops->map_free(map);
418 return err;
419 }
420
421 /* if error is returned, fd is released.
422 * On success caller should complete fd access with matching fdput()
423 */
__bpf_map_get(struct fd f)424 struct bpf_map *__bpf_map_get(struct fd f)
425 {
426 if (!f.file)
427 return ERR_PTR(-EBADF);
428 if (f.file->f_op != &bpf_map_fops) {
429 fdput(f);
430 return ERR_PTR(-EINVAL);
431 }
432
433 return f.file->private_data;
434 }
435
436 /* prog's and map's refcnt limit */
437 #define BPF_MAX_REFCNT 32768
438
bpf_map_inc(struct bpf_map * map,bool uref)439 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
440 {
441 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
442 atomic_dec(&map->refcnt);
443 return ERR_PTR(-EBUSY);
444 }
445 if (uref)
446 atomic_inc(&map->usercnt);
447 return map;
448 }
449
bpf_map_get_with_uref(u32 ufd)450 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
451 {
452 struct fd f = fdget(ufd);
453 struct bpf_map *map;
454
455 map = __bpf_map_get(f);
456 if (IS_ERR(map))
457 return map;
458
459 map = bpf_map_inc(map, true);
460 fdput(f);
461
462 return map;
463 }
464
465 /* map_idr_lock should have been held */
bpf_map_inc_not_zero(struct bpf_map * map,bool uref)466 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
467 bool uref)
468 {
469 int refold;
470
471 refold = __atomic_add_unless(&map->refcnt, 1, 0);
472
473 if (refold >= BPF_MAX_REFCNT) {
474 __bpf_map_put(map, false);
475 return ERR_PTR(-EBUSY);
476 }
477
478 if (!refold)
479 return ERR_PTR(-ENOENT);
480
481 if (uref)
482 atomic_inc(&map->usercnt);
483
484 return map;
485 }
486
bpf_stackmap_copy(struct bpf_map * map,void * key,void * value)487 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
488 {
489 return -ENOTSUPP;
490 }
491
492 /* last field in 'union bpf_attr' used by this command */
493 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
494
map_lookup_elem(union bpf_attr * attr)495 static int map_lookup_elem(union bpf_attr *attr)
496 {
497 void __user *ukey = u64_to_user_ptr(attr->key);
498 void __user *uvalue = u64_to_user_ptr(attr->value);
499 int ufd = attr->map_fd;
500 struct bpf_map *map;
501 void *key, *value, *ptr;
502 u32 value_size;
503 struct fd f;
504 int err;
505
506 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
507 return -EINVAL;
508
509 f = fdget(ufd);
510 map = __bpf_map_get(f);
511 if (IS_ERR(map))
512 return PTR_ERR(map);
513
514 if (!(f.file->f_mode & FMODE_CAN_READ)) {
515 err = -EPERM;
516 goto err_put;
517 }
518
519 key = memdup_user(ukey, map->key_size);
520 if (IS_ERR(key)) {
521 err = PTR_ERR(key);
522 goto err_put;
523 }
524
525 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
526 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
527 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
528 value_size = round_up(map->value_size, 8) * num_possible_cpus();
529 else if (IS_FD_MAP(map))
530 value_size = sizeof(u32);
531 else
532 value_size = map->value_size;
533
534 err = -ENOMEM;
535 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
536 if (!value)
537 goto free_key;
538
539 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
540 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
541 err = bpf_percpu_hash_copy(map, key, value);
542 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
543 err = bpf_percpu_array_copy(map, key, value);
544 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
545 err = bpf_stackmap_copy(map, key, value);
546 } else if (IS_FD_ARRAY(map)) {
547 err = bpf_fd_array_map_lookup_elem(map, key, value);
548 } else if (IS_FD_HASH(map)) {
549 err = bpf_fd_htab_map_lookup_elem(map, key, value);
550 } else {
551 rcu_read_lock();
552 if (map->ops->map_lookup_elem_sys_only)
553 ptr = map->ops->map_lookup_elem_sys_only(map, key);
554 else
555 ptr = map->ops->map_lookup_elem(map, key);
556 if (ptr)
557 memcpy(value, ptr, value_size);
558 rcu_read_unlock();
559 err = ptr ? 0 : -ENOENT;
560 }
561
562 if (err)
563 goto free_value;
564
565 err = -EFAULT;
566 if (copy_to_user(uvalue, value, value_size) != 0)
567 goto free_value;
568
569 trace_bpf_map_lookup_elem(map, ufd, key, value);
570 err = 0;
571
572 free_value:
573 kfree(value);
574 free_key:
575 kfree(key);
576 err_put:
577 fdput(f);
578 return err;
579 }
580
maybe_wait_bpf_programs(struct bpf_map * map)581 static void maybe_wait_bpf_programs(struct bpf_map *map)
582 {
583 /* Wait for any running BPF programs to complete so that
584 * userspace, when we return to it, knows that all programs
585 * that could be running use the new map value.
586 */
587 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
588 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
589 synchronize_rcu();
590 }
591
592 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
593
map_update_elem(union bpf_attr * attr)594 static int map_update_elem(union bpf_attr *attr)
595 {
596 void __user *ukey = u64_to_user_ptr(attr->key);
597 void __user *uvalue = u64_to_user_ptr(attr->value);
598 int ufd = attr->map_fd;
599 struct bpf_map *map;
600 void *key, *value;
601 u32 value_size;
602 struct fd f;
603 int err;
604
605 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
606 return -EINVAL;
607
608 f = fdget(ufd);
609 map = __bpf_map_get(f);
610 if (IS_ERR(map))
611 return PTR_ERR(map);
612
613 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
614 err = -EPERM;
615 goto err_put;
616 }
617
618 key = memdup_user(ukey, map->key_size);
619 if (IS_ERR(key)) {
620 err = PTR_ERR(key);
621 goto err_put;
622 }
623
624 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
625 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
626 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
627 value_size = round_up(map->value_size, 8) * num_possible_cpus();
628 else
629 value_size = map->value_size;
630
631 err = -ENOMEM;
632 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
633 if (!value)
634 goto free_key;
635
636 err = -EFAULT;
637 if (copy_from_user(value, uvalue, value_size) != 0)
638 goto free_value;
639
640 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
641 * inside bpf map update or delete otherwise deadlocks are possible
642 */
643 preempt_disable();
644 __this_cpu_inc(bpf_prog_active);
645 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
646 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
647 err = bpf_percpu_hash_update(map, key, value, attr->flags);
648 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
649 err = bpf_percpu_array_update(map, key, value, attr->flags);
650 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
651 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
652 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
653 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
654 rcu_read_lock();
655 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
656 attr->flags);
657 rcu_read_unlock();
658 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
659 rcu_read_lock();
660 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
661 attr->flags);
662 rcu_read_unlock();
663 } else {
664 rcu_read_lock();
665 err = map->ops->map_update_elem(map, key, value, attr->flags);
666 rcu_read_unlock();
667 }
668 __this_cpu_dec(bpf_prog_active);
669 preempt_enable();
670 maybe_wait_bpf_programs(map);
671
672 if (!err)
673 trace_bpf_map_update_elem(map, ufd, key, value);
674 free_value:
675 kfree(value);
676 free_key:
677 kfree(key);
678 err_put:
679 fdput(f);
680 return err;
681 }
682
683 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
684
map_delete_elem(union bpf_attr * attr)685 static int map_delete_elem(union bpf_attr *attr)
686 {
687 void __user *ukey = u64_to_user_ptr(attr->key);
688 int ufd = attr->map_fd;
689 struct bpf_map *map;
690 struct fd f;
691 void *key;
692 int err;
693
694 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
695 return -EINVAL;
696
697 f = fdget(ufd);
698 map = __bpf_map_get(f);
699 if (IS_ERR(map))
700 return PTR_ERR(map);
701
702 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
703 err = -EPERM;
704 goto err_put;
705 }
706
707 key = memdup_user(ukey, map->key_size);
708 if (IS_ERR(key)) {
709 err = PTR_ERR(key);
710 goto err_put;
711 }
712
713 preempt_disable();
714 __this_cpu_inc(bpf_prog_active);
715 rcu_read_lock();
716 err = map->ops->map_delete_elem(map, key);
717 rcu_read_unlock();
718 __this_cpu_dec(bpf_prog_active);
719 preempt_enable();
720 maybe_wait_bpf_programs(map);
721
722 if (!err)
723 trace_bpf_map_delete_elem(map, ufd, key);
724 kfree(key);
725 err_put:
726 fdput(f);
727 return err;
728 }
729
730 /* last field in 'union bpf_attr' used by this command */
731 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
732
map_get_next_key(union bpf_attr * attr)733 static int map_get_next_key(union bpf_attr *attr)
734 {
735 void __user *ukey = u64_to_user_ptr(attr->key);
736 void __user *unext_key = u64_to_user_ptr(attr->next_key);
737 int ufd = attr->map_fd;
738 struct bpf_map *map;
739 void *key, *next_key;
740 struct fd f;
741 int err;
742
743 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
744 return -EINVAL;
745
746 f = fdget(ufd);
747 map = __bpf_map_get(f);
748 if (IS_ERR(map))
749 return PTR_ERR(map);
750
751 if (!(f.file->f_mode & FMODE_CAN_READ)) {
752 err = -EPERM;
753 goto err_put;
754 }
755
756 if (ukey) {
757 key = memdup_user(ukey, map->key_size);
758 if (IS_ERR(key)) {
759 err = PTR_ERR(key);
760 goto err_put;
761 }
762 } else {
763 key = NULL;
764 }
765
766 err = -ENOMEM;
767 next_key = kmalloc(map->key_size, GFP_USER);
768 if (!next_key)
769 goto free_key;
770
771 rcu_read_lock();
772 err = map->ops->map_get_next_key(map, key, next_key);
773 rcu_read_unlock();
774 if (err)
775 goto free_next_key;
776
777 err = -EFAULT;
778 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
779 goto free_next_key;
780
781 trace_bpf_map_next_key(map, ufd, key, next_key);
782 err = 0;
783
784 free_next_key:
785 kfree(next_key);
786 free_key:
787 kfree(key);
788 err_put:
789 fdput(f);
790 return err;
791 }
792
793 static const struct bpf_verifier_ops * const bpf_prog_types[] = {
794 #define BPF_PROG_TYPE(_id, _ops) \
795 [_id] = &_ops,
796 #define BPF_MAP_TYPE(_id, _ops)
797 #include <linux/bpf_types.h>
798 #undef BPF_PROG_TYPE
799 #undef BPF_MAP_TYPE
800 };
801
find_prog_type(enum bpf_prog_type type,struct bpf_prog * prog)802 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
803 {
804 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
805 return -EINVAL;
806
807 prog->aux->ops = bpf_prog_types[type];
808 prog->type = type;
809 return 0;
810 }
811
812 /* drop refcnt on maps used by eBPF program and free auxilary data */
free_used_maps(struct bpf_prog_aux * aux)813 static void free_used_maps(struct bpf_prog_aux *aux)
814 {
815 int i;
816
817 for (i = 0; i < aux->used_map_cnt; i++)
818 bpf_map_put(aux->used_maps[i]);
819
820 kfree(aux->used_maps);
821 }
822
__bpf_prog_charge(struct user_struct * user,u32 pages)823 int __bpf_prog_charge(struct user_struct *user, u32 pages)
824 {
825 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
826 unsigned long user_bufs;
827
828 if (user) {
829 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
830 if (user_bufs > memlock_limit) {
831 atomic_long_sub(pages, &user->locked_vm);
832 return -EPERM;
833 }
834 }
835
836 return 0;
837 }
838
__bpf_prog_uncharge(struct user_struct * user,u32 pages)839 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
840 {
841 if (user)
842 atomic_long_sub(pages, &user->locked_vm);
843 }
844
bpf_prog_charge_memlock(struct bpf_prog * prog)845 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
846 {
847 struct user_struct *user = get_current_user();
848 int ret;
849
850 ret = __bpf_prog_charge(user, prog->pages);
851 if (ret) {
852 free_uid(user);
853 return ret;
854 }
855
856 prog->aux->user = user;
857 return 0;
858 }
859
bpf_prog_uncharge_memlock(struct bpf_prog * prog)860 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
861 {
862 struct user_struct *user = prog->aux->user;
863
864 __bpf_prog_uncharge(user, prog->pages);
865 free_uid(user);
866 }
867
bpf_prog_alloc_id(struct bpf_prog * prog)868 static int bpf_prog_alloc_id(struct bpf_prog *prog)
869 {
870 int id;
871
872 spin_lock_bh(&prog_idr_lock);
873 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
874 if (id > 0)
875 prog->aux->id = id;
876 spin_unlock_bh(&prog_idr_lock);
877
878 /* id is in [1, INT_MAX) */
879 if (WARN_ON_ONCE(!id))
880 return -ENOSPC;
881
882 return id > 0 ? 0 : id;
883 }
884
bpf_prog_free_id(struct bpf_prog * prog,bool do_idr_lock)885 static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
886 {
887 /* cBPF to eBPF migrations are currently not in the idr store. */
888 if (!prog->aux->id)
889 return;
890
891 if (do_idr_lock)
892 spin_lock_bh(&prog_idr_lock);
893 else
894 __acquire(&prog_idr_lock);
895
896 idr_remove(&prog_idr, prog->aux->id);
897
898 if (do_idr_lock)
899 spin_unlock_bh(&prog_idr_lock);
900 else
901 __release(&prog_idr_lock);
902 }
903
__bpf_prog_put_rcu(struct rcu_head * rcu)904 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
905 {
906 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
907
908 free_used_maps(aux);
909 bpf_prog_uncharge_memlock(aux->prog);
910 security_bpf_prog_free(aux);
911 bpf_prog_free(aux->prog);
912 }
913
__bpf_prog_put(struct bpf_prog * prog,bool do_idr_lock)914 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
915 {
916 if (atomic_dec_and_test(&prog->aux->refcnt)) {
917 trace_bpf_prog_put_rcu(prog);
918 /* bpf_prog_free_id() must be called first */
919 bpf_prog_free_id(prog, do_idr_lock);
920 bpf_prog_kallsyms_del(prog);
921 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
922 }
923 }
924
bpf_prog_put(struct bpf_prog * prog)925 void bpf_prog_put(struct bpf_prog *prog)
926 {
927 __bpf_prog_put(prog, true);
928 }
929 EXPORT_SYMBOL_GPL(bpf_prog_put);
930
bpf_prog_release(struct inode * inode,struct file * filp)931 static int bpf_prog_release(struct inode *inode, struct file *filp)
932 {
933 struct bpf_prog *prog = filp->private_data;
934
935 bpf_prog_put(prog);
936 return 0;
937 }
938
939 #ifdef CONFIG_PROC_FS
bpf_prog_show_fdinfo(struct seq_file * m,struct file * filp)940 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
941 {
942 const struct bpf_prog *prog = filp->private_data;
943 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
944
945 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
946 seq_printf(m,
947 "prog_type:\t%u\n"
948 "prog_jited:\t%u\n"
949 "prog_tag:\t%s\n"
950 "memlock:\t%llu\n",
951 prog->type,
952 prog->jited,
953 prog_tag,
954 prog->pages * 1ULL << PAGE_SHIFT);
955 }
956 #endif
957
958 const struct file_operations bpf_prog_fops = {
959 #ifdef CONFIG_PROC_FS
960 .show_fdinfo = bpf_prog_show_fdinfo,
961 #endif
962 .release = bpf_prog_release,
963 .read = bpf_dummy_read,
964 .write = bpf_dummy_write,
965 };
966
bpf_prog_new_fd(struct bpf_prog * prog)967 int bpf_prog_new_fd(struct bpf_prog *prog)
968 {
969 int ret;
970
971 ret = security_bpf_prog(prog);
972 if (ret < 0)
973 return ret;
974
975 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
976 O_RDWR | O_CLOEXEC);
977 }
978
____bpf_prog_get(struct fd f)979 static struct bpf_prog *____bpf_prog_get(struct fd f)
980 {
981 if (!f.file)
982 return ERR_PTR(-EBADF);
983 if (f.file->f_op != &bpf_prog_fops) {
984 fdput(f);
985 return ERR_PTR(-EINVAL);
986 }
987
988 return f.file->private_data;
989 }
990
bpf_prog_add(struct bpf_prog * prog,int i)991 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
992 {
993 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
994 atomic_sub(i, &prog->aux->refcnt);
995 return ERR_PTR(-EBUSY);
996 }
997 return prog;
998 }
999 EXPORT_SYMBOL_GPL(bpf_prog_add);
1000
bpf_prog_sub(struct bpf_prog * prog,int i)1001 void bpf_prog_sub(struct bpf_prog *prog, int i)
1002 {
1003 /* Only to be used for undoing previous bpf_prog_add() in some
1004 * error path. We still know that another entity in our call
1005 * path holds a reference to the program, thus atomic_sub() can
1006 * be safely used in such cases!
1007 */
1008 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1009 }
1010 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1011
bpf_prog_inc(struct bpf_prog * prog)1012 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1013 {
1014 return bpf_prog_add(prog, 1);
1015 }
1016 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1017
1018 /* prog_idr_lock should have been held */
bpf_prog_inc_not_zero(struct bpf_prog * prog)1019 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1020 {
1021 int refold;
1022
1023 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1024
1025 if (refold >= BPF_MAX_REFCNT) {
1026 __bpf_prog_put(prog, false);
1027 return ERR_PTR(-EBUSY);
1028 }
1029
1030 if (!refold)
1031 return ERR_PTR(-ENOENT);
1032
1033 return prog;
1034 }
1035 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1036
__bpf_prog_get(u32 ufd,enum bpf_prog_type * type)1037 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
1038 {
1039 struct fd f = fdget(ufd);
1040 struct bpf_prog *prog;
1041
1042 prog = ____bpf_prog_get(f);
1043 if (IS_ERR(prog))
1044 return prog;
1045 if (type && prog->type != *type) {
1046 prog = ERR_PTR(-EINVAL);
1047 goto out;
1048 }
1049
1050 prog = bpf_prog_inc(prog);
1051 out:
1052 fdput(f);
1053 return prog;
1054 }
1055
bpf_prog_get(u32 ufd)1056 struct bpf_prog *bpf_prog_get(u32 ufd)
1057 {
1058 return __bpf_prog_get(ufd, NULL);
1059 }
1060
bpf_prog_get_type(u32 ufd,enum bpf_prog_type type)1061 struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
1062 {
1063 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
1064
1065 if (!IS_ERR(prog))
1066 trace_bpf_prog_get_type(prog);
1067 return prog;
1068 }
1069 EXPORT_SYMBOL_GPL(bpf_prog_get_type);
1070
1071 /* last field in 'union bpf_attr' used by this command */
1072 #define BPF_PROG_LOAD_LAST_FIELD prog_flags
1073
bpf_prog_load(union bpf_attr * attr)1074 static int bpf_prog_load(union bpf_attr *attr)
1075 {
1076 enum bpf_prog_type type = attr->prog_type;
1077 struct bpf_prog *prog;
1078 int err;
1079 char license[128];
1080 bool is_gpl;
1081
1082 if (CHECK_ATTR(BPF_PROG_LOAD))
1083 return -EINVAL;
1084
1085 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1086 return -EINVAL;
1087
1088 /* copy eBPF program license from user space */
1089 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1090 sizeof(license) - 1) < 0)
1091 return -EFAULT;
1092 license[sizeof(license) - 1] = 0;
1093
1094 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1095 is_gpl = license_is_gpl_compatible(license);
1096
1097 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1098 return -E2BIG;
1099
1100 if (type == BPF_PROG_TYPE_KPROBE &&
1101 attr->kern_version != LINUX_VERSION_CODE)
1102 return -EINVAL;
1103
1104 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1105 type != BPF_PROG_TYPE_CGROUP_SKB &&
1106 !capable(CAP_SYS_ADMIN))
1107 return -EPERM;
1108
1109 /* plain bpf_prog allocation */
1110 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1111 if (!prog)
1112 return -ENOMEM;
1113
1114 err = security_bpf_prog_alloc(prog->aux);
1115 if (err)
1116 goto free_prog_nouncharge;
1117
1118 err = bpf_prog_charge_memlock(prog);
1119 if (err)
1120 goto free_prog_sec;
1121
1122 prog->len = attr->insn_cnt;
1123
1124 err = -EFAULT;
1125 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1126 bpf_prog_insn_size(prog)) != 0)
1127 goto free_prog;
1128
1129 prog->orig_prog = NULL;
1130 prog->jited = 0;
1131
1132 atomic_set(&prog->aux->refcnt, 1);
1133 prog->gpl_compatible = is_gpl ? 1 : 0;
1134
1135 /* find program type: socket_filter vs tracing_filter */
1136 err = find_prog_type(type, prog);
1137 if (err < 0)
1138 goto free_prog;
1139
1140 /* run eBPF verifier */
1141 err = bpf_check(&prog, attr);
1142 if (err < 0)
1143 goto free_used_maps;
1144
1145 /* eBPF program is ready to be JITed */
1146 prog = bpf_prog_select_runtime(prog, &err);
1147 if (err < 0)
1148 goto free_used_maps;
1149
1150 err = bpf_prog_alloc_id(prog);
1151 if (err)
1152 goto free_used_maps;
1153
1154 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
1155 * effectively publicly exposed. However, retrieving via
1156 * bpf_prog_get_fd_by_id() will take another reference,
1157 * therefore it cannot be gone underneath us.
1158 *
1159 * Only for the time /after/ successful bpf_prog_new_fd()
1160 * and before returning to userspace, we might just hold
1161 * one reference and any parallel close on that fd could
1162 * rip everything out. Hence, below notifications must
1163 * happen before bpf_prog_new_fd().
1164 *
1165 * Also, any failure handling from this point onwards must
1166 * be using bpf_prog_put() given the program is exposed.
1167 */
1168 bpf_prog_kallsyms_add(prog);
1169 trace_bpf_prog_load(prog, err);
1170
1171 err = bpf_prog_new_fd(prog);
1172 if (err < 0)
1173 bpf_prog_put(prog);
1174 return err;
1175
1176 free_used_maps:
1177 free_used_maps(prog->aux);
1178 free_prog:
1179 bpf_prog_uncharge_memlock(prog);
1180 free_prog_sec:
1181 security_bpf_prog_free(prog->aux);
1182 free_prog_nouncharge:
1183 bpf_prog_free(prog);
1184 return err;
1185 }
1186
1187 #define BPF_OBJ_LAST_FIELD file_flags
1188
bpf_obj_pin(const union bpf_attr * attr)1189 static int bpf_obj_pin(const union bpf_attr *attr)
1190 {
1191 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1192 return -EINVAL;
1193
1194 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1195 }
1196
bpf_obj_get(const union bpf_attr * attr)1197 static int bpf_obj_get(const union bpf_attr *attr)
1198 {
1199 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1200 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1201 return -EINVAL;
1202
1203 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1204 attr->file_flags);
1205 }
1206
1207 #ifdef CONFIG_CGROUP_BPF
1208
1209 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1210
sockmap_get_from_fd(const union bpf_attr * attr,bool attach)1211 static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
1212 {
1213 struct bpf_prog *prog = NULL;
1214 int ufd = attr->target_fd;
1215 struct bpf_map *map;
1216 struct fd f;
1217 int err;
1218
1219 f = fdget(ufd);
1220 map = __bpf_map_get(f);
1221 if (IS_ERR(map))
1222 return PTR_ERR(map);
1223
1224 if (attach) {
1225 prog = bpf_prog_get_type(attr->attach_bpf_fd,
1226 BPF_PROG_TYPE_SK_SKB);
1227 if (IS_ERR(prog)) {
1228 fdput(f);
1229 return PTR_ERR(prog);
1230 }
1231 }
1232
1233 err = sock_map_prog(map, prog, attr->attach_type);
1234 if (err) {
1235 fdput(f);
1236 if (prog)
1237 bpf_prog_put(prog);
1238 return err;
1239 }
1240
1241 fdput(f);
1242 return 0;
1243 }
1244
bpf_prog_attach(const union bpf_attr * attr)1245 static int bpf_prog_attach(const union bpf_attr *attr)
1246 {
1247 enum bpf_prog_type ptype;
1248 struct bpf_prog *prog;
1249 struct cgroup *cgrp;
1250 int ret;
1251
1252 if (!capable(CAP_NET_ADMIN))
1253 return -EPERM;
1254
1255 if (CHECK_ATTR(BPF_PROG_ATTACH))
1256 return -EINVAL;
1257
1258 if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
1259 return -EINVAL;
1260
1261 switch (attr->attach_type) {
1262 case BPF_CGROUP_INET_INGRESS:
1263 case BPF_CGROUP_INET_EGRESS:
1264 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1265 break;
1266 case BPF_CGROUP_INET_SOCK_CREATE:
1267 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1268 break;
1269 case BPF_CGROUP_SOCK_OPS:
1270 ptype = BPF_PROG_TYPE_SOCK_OPS;
1271 break;
1272 case BPF_SK_SKB_STREAM_PARSER:
1273 case BPF_SK_SKB_STREAM_VERDICT:
1274 return sockmap_get_from_fd(attr, true);
1275 default:
1276 return -EINVAL;
1277 }
1278
1279 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1280 if (IS_ERR(prog))
1281 return PTR_ERR(prog);
1282
1283 cgrp = cgroup_get_from_fd(attr->target_fd);
1284 if (IS_ERR(cgrp)) {
1285 bpf_prog_put(prog);
1286 return PTR_ERR(cgrp);
1287 }
1288
1289 ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
1290 attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
1291 if (ret)
1292 bpf_prog_put(prog);
1293 cgroup_put(cgrp);
1294
1295 return ret;
1296 }
1297
1298 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1299
bpf_prog_detach(const union bpf_attr * attr)1300 static int bpf_prog_detach(const union bpf_attr *attr)
1301 {
1302 struct cgroup *cgrp;
1303 int ret;
1304
1305 if (!capable(CAP_NET_ADMIN))
1306 return -EPERM;
1307
1308 if (CHECK_ATTR(BPF_PROG_DETACH))
1309 return -EINVAL;
1310
1311 switch (attr->attach_type) {
1312 case BPF_CGROUP_INET_INGRESS:
1313 case BPF_CGROUP_INET_EGRESS:
1314 case BPF_CGROUP_INET_SOCK_CREATE:
1315 case BPF_CGROUP_SOCK_OPS:
1316 cgrp = cgroup_get_from_fd(attr->target_fd);
1317 if (IS_ERR(cgrp))
1318 return PTR_ERR(cgrp);
1319
1320 ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
1321 cgroup_put(cgrp);
1322 break;
1323 case BPF_SK_SKB_STREAM_PARSER:
1324 case BPF_SK_SKB_STREAM_VERDICT:
1325 ret = sockmap_get_from_fd(attr, false);
1326 break;
1327 default:
1328 return -EINVAL;
1329 }
1330
1331 return ret;
1332 }
1333
1334 #endif /* CONFIG_CGROUP_BPF */
1335
1336 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1337
bpf_prog_test_run(const union bpf_attr * attr,union bpf_attr __user * uattr)1338 static int bpf_prog_test_run(const union bpf_attr *attr,
1339 union bpf_attr __user *uattr)
1340 {
1341 struct bpf_prog *prog;
1342 int ret = -ENOTSUPP;
1343
1344 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1345 return -EINVAL;
1346
1347 prog = bpf_prog_get(attr->test.prog_fd);
1348 if (IS_ERR(prog))
1349 return PTR_ERR(prog);
1350
1351 if (prog->aux->ops->test_run)
1352 ret = prog->aux->ops->test_run(prog, attr, uattr);
1353
1354 bpf_prog_put(prog);
1355 return ret;
1356 }
1357
1358 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1359
bpf_obj_get_next_id(const union bpf_attr * attr,union bpf_attr __user * uattr,struct idr * idr,spinlock_t * lock)1360 static int bpf_obj_get_next_id(const union bpf_attr *attr,
1361 union bpf_attr __user *uattr,
1362 struct idr *idr,
1363 spinlock_t *lock)
1364 {
1365 u32 next_id = attr->start_id;
1366 int err = 0;
1367
1368 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1369 return -EINVAL;
1370
1371 if (!capable(CAP_SYS_ADMIN))
1372 return -EPERM;
1373
1374 next_id++;
1375 spin_lock_bh(lock);
1376 if (!idr_get_next(idr, &next_id))
1377 err = -ENOENT;
1378 spin_unlock_bh(lock);
1379
1380 if (!err)
1381 err = put_user(next_id, &uattr->next_id);
1382
1383 return err;
1384 }
1385
1386 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1387
bpf_prog_get_fd_by_id(const union bpf_attr * attr)1388 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1389 {
1390 struct bpf_prog *prog;
1391 u32 id = attr->prog_id;
1392 int fd;
1393
1394 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1395 return -EINVAL;
1396
1397 if (!capable(CAP_SYS_ADMIN))
1398 return -EPERM;
1399
1400 spin_lock_bh(&prog_idr_lock);
1401 prog = idr_find(&prog_idr, id);
1402 if (prog)
1403 prog = bpf_prog_inc_not_zero(prog);
1404 else
1405 prog = ERR_PTR(-ENOENT);
1406 spin_unlock_bh(&prog_idr_lock);
1407
1408 if (IS_ERR(prog))
1409 return PTR_ERR(prog);
1410
1411 fd = bpf_prog_new_fd(prog);
1412 if (fd < 0)
1413 bpf_prog_put(prog);
1414
1415 return fd;
1416 }
1417
1418 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
1419
bpf_map_get_fd_by_id(const union bpf_attr * attr)1420 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1421 {
1422 struct bpf_map *map;
1423 u32 id = attr->map_id;
1424 int f_flags;
1425 int fd;
1426
1427 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1428 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
1429 return -EINVAL;
1430
1431 if (!capable(CAP_SYS_ADMIN))
1432 return -EPERM;
1433
1434 f_flags = bpf_get_file_flag(attr->open_flags);
1435 if (f_flags < 0)
1436 return f_flags;
1437
1438 spin_lock_bh(&map_idr_lock);
1439 map = idr_find(&map_idr, id);
1440 if (map)
1441 map = bpf_map_inc_not_zero(map, true);
1442 else
1443 map = ERR_PTR(-ENOENT);
1444 spin_unlock_bh(&map_idr_lock);
1445
1446 if (IS_ERR(map))
1447 return PTR_ERR(map);
1448
1449 fd = bpf_map_new_fd(map, f_flags);
1450 if (fd < 0)
1451 bpf_map_put_with_uref(map);
1452
1453 return fd;
1454 }
1455
bpf_prog_get_info_by_fd(struct bpf_prog * prog,const union bpf_attr * attr,union bpf_attr __user * uattr)1456 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1457 const union bpf_attr *attr,
1458 union bpf_attr __user *uattr)
1459 {
1460 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1461 struct bpf_prog_info info;
1462 u32 info_len = attr->info.info_len;
1463 char __user *uinsns;
1464 u32 ulen;
1465 int err;
1466
1467 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1468 if (err)
1469 return err;
1470 info_len = min_t(u32, sizeof(info), info_len);
1471
1472 memset(&info, 0, sizeof(info));
1473 if (copy_from_user(&info, uinfo, info_len))
1474 return -EFAULT;
1475
1476 info.type = prog->type;
1477 info.id = prog->aux->id;
1478
1479 memcpy(info.tag, prog->tag, sizeof(prog->tag));
1480
1481 if (!capable(CAP_SYS_ADMIN)) {
1482 info.jited_prog_len = 0;
1483 info.xlated_prog_len = 0;
1484 goto done;
1485 }
1486
1487 ulen = info.jited_prog_len;
1488 info.jited_prog_len = prog->jited_len;
1489 if (info.jited_prog_len && ulen) {
1490 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1491 ulen = min_t(u32, info.jited_prog_len, ulen);
1492 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1493 return -EFAULT;
1494 }
1495
1496 ulen = info.xlated_prog_len;
1497 info.xlated_prog_len = bpf_prog_insn_size(prog);
1498 if (info.xlated_prog_len && ulen) {
1499 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1500 ulen = min_t(u32, info.xlated_prog_len, ulen);
1501 if (copy_to_user(uinsns, prog->insnsi, ulen))
1502 return -EFAULT;
1503 }
1504
1505 done:
1506 if (copy_to_user(uinfo, &info, info_len) ||
1507 put_user(info_len, &uattr->info.info_len))
1508 return -EFAULT;
1509
1510 return 0;
1511 }
1512
bpf_map_get_info_by_fd(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)1513 static int bpf_map_get_info_by_fd(struct bpf_map *map,
1514 const union bpf_attr *attr,
1515 union bpf_attr __user *uattr)
1516 {
1517 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1518 struct bpf_map_info info;
1519 u32 info_len = attr->info.info_len;
1520 int err;
1521
1522 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1523 if (err)
1524 return err;
1525 info_len = min_t(u32, sizeof(info), info_len);
1526
1527 memset(&info, 0, sizeof(info));
1528 info.type = map->map_type;
1529 info.id = map->id;
1530 info.key_size = map->key_size;
1531 info.value_size = map->value_size;
1532 info.max_entries = map->max_entries;
1533 info.map_flags = map->map_flags;
1534
1535 if (copy_to_user(uinfo, &info, info_len) ||
1536 put_user(info_len, &uattr->info.info_len))
1537 return -EFAULT;
1538
1539 return 0;
1540 }
1541
1542 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1543
bpf_obj_get_info_by_fd(const union bpf_attr * attr,union bpf_attr __user * uattr)1544 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1545 union bpf_attr __user *uattr)
1546 {
1547 int ufd = attr->info.bpf_fd;
1548 struct fd f;
1549 int err;
1550
1551 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1552 return -EINVAL;
1553
1554 f = fdget(ufd);
1555 if (!f.file)
1556 return -EBADFD;
1557
1558 if (f.file->f_op == &bpf_prog_fops)
1559 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1560 uattr);
1561 else if (f.file->f_op == &bpf_map_fops)
1562 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1563 uattr);
1564 else
1565 err = -EINVAL;
1566
1567 fdput(f);
1568 return err;
1569 }
1570
SYSCALL_DEFINE3(bpf,int,cmd,union bpf_attr __user *,uattr,unsigned int,size)1571 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1572 {
1573 union bpf_attr attr;
1574 int err;
1575
1576 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
1577 return -EPERM;
1578
1579 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1580 if (err)
1581 return err;
1582 size = min_t(u32, size, sizeof(attr));
1583
1584 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1585 memset(&attr, 0, sizeof(attr));
1586 if (copy_from_user(&attr, uattr, size) != 0)
1587 return -EFAULT;
1588
1589 err = security_bpf(cmd, &attr, size);
1590 if (err < 0)
1591 return err;
1592
1593 switch (cmd) {
1594 case BPF_MAP_CREATE:
1595 err = map_create(&attr);
1596 break;
1597 case BPF_MAP_LOOKUP_ELEM:
1598 err = map_lookup_elem(&attr);
1599 break;
1600 case BPF_MAP_UPDATE_ELEM:
1601 err = map_update_elem(&attr);
1602 break;
1603 case BPF_MAP_DELETE_ELEM:
1604 err = map_delete_elem(&attr);
1605 break;
1606 case BPF_MAP_GET_NEXT_KEY:
1607 err = map_get_next_key(&attr);
1608 break;
1609 case BPF_PROG_LOAD:
1610 err = bpf_prog_load(&attr);
1611 break;
1612 case BPF_OBJ_PIN:
1613 err = bpf_obj_pin(&attr);
1614 break;
1615 case BPF_OBJ_GET:
1616 err = bpf_obj_get(&attr);
1617 break;
1618 #ifdef CONFIG_CGROUP_BPF
1619 case BPF_PROG_ATTACH:
1620 err = bpf_prog_attach(&attr);
1621 break;
1622 case BPF_PROG_DETACH:
1623 err = bpf_prog_detach(&attr);
1624 break;
1625 #endif
1626 case BPF_PROG_TEST_RUN:
1627 err = bpf_prog_test_run(&attr, uattr);
1628 break;
1629 case BPF_PROG_GET_NEXT_ID:
1630 err = bpf_obj_get_next_id(&attr, uattr,
1631 &prog_idr, &prog_idr_lock);
1632 break;
1633 case BPF_MAP_GET_NEXT_ID:
1634 err = bpf_obj_get_next_id(&attr, uattr,
1635 &map_idr, &map_idr_lock);
1636 break;
1637 case BPF_PROG_GET_FD_BY_ID:
1638 err = bpf_prog_get_fd_by_id(&attr);
1639 break;
1640 case BPF_MAP_GET_FD_BY_ID:
1641 err = bpf_map_get_fd_by_id(&attr);
1642 break;
1643 case BPF_OBJ_GET_INFO_BY_FD:
1644 err = bpf_obj_get_info_by_fd(&attr, uattr);
1645 break;
1646 default:
1647 err = -EINVAL;
1648 break;
1649 }
1650
1651 return err;
1652 }
1653