bpf: remove tracepoints from bpf core
[linux-2.6-block.git] / kernel / bpf / syscall.c
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/btf.h>
15 #include <linux/syscalls.h>
16 #include <linux/slab.h>
17 #include <linux/sched/signal.h>
18 #include <linux/vmalloc.h>
19 #include <linux/mmzone.h>
20 #include <linux/anon_inodes.h>
21 #include <linux/file.h>
22 #include <linux/license.h>
23 #include <linux/filter.h>
24 #include <linux/version.h>
25 #include <linux/kernel.h>
26 #include <linux/idr.h>
27 #include <linux/cred.h>
28 #include <linux/timekeeping.h>
29 #include <linux/ctype.h>
30 #include <linux/btf.h>
31
32 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
33                            (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
34                            (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
35                            (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
36 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
37 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
38
39 #define BPF_OBJ_FLAG_MASK   (BPF_F_RDONLY | BPF_F_WRONLY)
40
41 DEFINE_PER_CPU(int, bpf_prog_active);
42 static DEFINE_IDR(prog_idr);
43 static DEFINE_SPINLOCK(prog_idr_lock);
44 static DEFINE_IDR(map_idr);
45 static DEFINE_SPINLOCK(map_idr_lock);
46
47 int sysctl_unprivileged_bpf_disabled __read_mostly;
48
49 static const struct bpf_map_ops * const bpf_map_types[] = {
50 #define BPF_PROG_TYPE(_id, _ops)
51 #define BPF_MAP_TYPE(_id, _ops) \
52         [_id] = &_ops,
53 #include <linux/bpf_types.h>
54 #undef BPF_PROG_TYPE
55 #undef BPF_MAP_TYPE
56 };
57
58 /*
59  * If we're handed a bigger struct than we know of, ensure all the unknown bits
60  * are 0 - i.e. new user-space does not rely on any kernel feature extensions
61  * we don't know about yet.
62  *
63  * There is a ToCToU between this function call and the following
64  * copy_from_user() call. However, this is not a concern since this function is
65  * meant to be a future-proofing of bits.
66  */
67 static int check_uarg_tail_zero(void __user *uaddr,
68                                 size_t expected_size,
69                                 size_t actual_size)
70 {
71         unsigned char __user *addr;
72         unsigned char __user *end;
73         unsigned char val;
74         int err;
75
76         if (unlikely(actual_size > PAGE_SIZE))  /* silly large */
77                 return -E2BIG;
78
79         if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
80                 return -EFAULT;
81
82         if (actual_size <= expected_size)
83                 return 0;
84
85         addr = uaddr + expected_size;
86         end  = uaddr + actual_size;
87
88         for (; addr < end; addr++) {
89                 err = get_user(val, addr);
90                 if (err)
91                         return err;
92                 if (val)
93                         return -E2BIG;
94         }
95
96         return 0;
97 }
98
99 const struct bpf_map_ops bpf_map_offload_ops = {
100         .map_alloc = bpf_map_offload_map_alloc,
101         .map_free = bpf_map_offload_map_free,
102 };
103
104 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
105 {
106         const struct bpf_map_ops *ops;
107         struct bpf_map *map;
108         int err;
109
110         if (attr->map_type >= ARRAY_SIZE(bpf_map_types))
111                 return ERR_PTR(-EINVAL);
112         ops = bpf_map_types[attr->map_type];
113         if (!ops)
114                 return ERR_PTR(-EINVAL);
115
116         if (ops->map_alloc_check) {
117                 err = ops->map_alloc_check(attr);
118                 if (err)
119                         return ERR_PTR(err);
120         }
121         if (attr->map_ifindex)
122                 ops = &bpf_map_offload_ops;
123         map = ops->map_alloc(attr);
124         if (IS_ERR(map))
125                 return map;
126         map->ops = ops;
127         map->map_type = attr->map_type;
128         return map;
129 }
130
131 void *bpf_map_area_alloc(size_t size, int numa_node)
132 {
133         /* We definitely need __GFP_NORETRY, so OOM killer doesn't
134          * trigger under memory pressure as we really just want to
135          * fail instead.
136          */
137         const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
138         void *area;
139
140         if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
141                 area = kmalloc_node(size, GFP_USER | flags, numa_node);
142                 if (area != NULL)
143                         return area;
144         }
145
146         return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
147                                            __builtin_return_address(0));
148 }
149
150 void bpf_map_area_free(void *area)
151 {
152         kvfree(area);
153 }
154
155 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
156 {
157         map->map_type = attr->map_type;
158         map->key_size = attr->key_size;
159         map->value_size = attr->value_size;
160         map->max_entries = attr->max_entries;
161         map->map_flags = attr->map_flags;
162         map->numa_node = bpf_map_attr_numa_node(attr);
163 }
164
165 int bpf_map_precharge_memlock(u32 pages)
166 {
167         struct user_struct *user = get_current_user();
168         unsigned long memlock_limit, cur;
169
170         memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
171         cur = atomic_long_read(&user->locked_vm);
172         free_uid(user);
173         if (cur + pages > memlock_limit)
174                 return -EPERM;
175         return 0;
176 }
177
178 static int bpf_map_charge_memlock(struct bpf_map *map)
179 {
180         struct user_struct *user = get_current_user();
181         unsigned long memlock_limit;
182
183         memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
184
185         atomic_long_add(map->pages, &user->locked_vm);
186
187         if (atomic_long_read(&user->locked_vm) > memlock_limit) {
188                 atomic_long_sub(map->pages, &user->locked_vm);
189                 free_uid(user);
190                 return -EPERM;
191         }
192         map->user = user;
193         return 0;
194 }
195
196 static void bpf_map_uncharge_memlock(struct bpf_map *map)
197 {
198         struct user_struct *user = map->user;
199
200         atomic_long_sub(map->pages, &user->locked_vm);
201         free_uid(user);
202 }
203
204 static int bpf_map_alloc_id(struct bpf_map *map)
205 {
206         int id;
207
208         idr_preload(GFP_KERNEL);
209         spin_lock_bh(&map_idr_lock);
210         id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
211         if (id > 0)
212                 map->id = id;
213         spin_unlock_bh(&map_idr_lock);
214         idr_preload_end();
215
216         if (WARN_ON_ONCE(!id))
217                 return -ENOSPC;
218
219         return id > 0 ? 0 : id;
220 }
221
222 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
223 {
224         unsigned long flags;
225
226         /* Offloaded maps are removed from the IDR store when their device
227          * disappears - even if someone holds an fd to them they are unusable,
228          * the memory is gone, all ops will fail; they are simply waiting for
229          * refcnt to drop to be freed.
230          */
231         if (!map->id)
232                 return;
233
234         if (do_idr_lock)
235                 spin_lock_irqsave(&map_idr_lock, flags);
236         else
237                 __acquire(&map_idr_lock);
238
239         idr_remove(&map_idr, map->id);
240         map->id = 0;
241
242         if (do_idr_lock)
243                 spin_unlock_irqrestore(&map_idr_lock, flags);
244         else
245                 __release(&map_idr_lock);
246 }
247
248 /* called from workqueue */
249 static void bpf_map_free_deferred(struct work_struct *work)
250 {
251         struct bpf_map *map = container_of(work, struct bpf_map, work);
252
253         bpf_map_uncharge_memlock(map);
254         security_bpf_map_free(map);
255         btf_put(map->btf);
256         /* implementation dependent freeing */
257         map->ops->map_free(map);
258 }
259
260 static void bpf_map_put_uref(struct bpf_map *map)
261 {
262         if (atomic_dec_and_test(&map->usercnt)) {
263                 if (map->ops->map_release_uref)
264                         map->ops->map_release_uref(map);
265         }
266 }
267
268 /* decrement map refcnt and schedule it for freeing via workqueue
269  * (unrelying map implementation ops->map_free() might sleep)
270  */
271 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
272 {
273         if (atomic_dec_and_test(&map->refcnt)) {
274                 /* bpf_map_free_id() must be called first */
275                 bpf_map_free_id(map, do_idr_lock);
276                 INIT_WORK(&map->work, bpf_map_free_deferred);
277                 schedule_work(&map->work);
278         }
279 }
280
281 void bpf_map_put(struct bpf_map *map)
282 {
283         __bpf_map_put(map, true);
284 }
285
286 void bpf_map_put_with_uref(struct bpf_map *map)
287 {
288         bpf_map_put_uref(map);
289         bpf_map_put(map);
290 }
291
292 static int bpf_map_release(struct inode *inode, struct file *filp)
293 {
294         struct bpf_map *map = filp->private_data;
295
296         if (map->ops->map_release)
297                 map->ops->map_release(map, filp);
298
299         bpf_map_put_with_uref(map);
300         return 0;
301 }
302
303 #ifdef CONFIG_PROC_FS
304 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
305 {
306         const struct bpf_map *map = filp->private_data;
307         const struct bpf_array *array;
308         u32 owner_prog_type = 0;
309         u32 owner_jited = 0;
310
311         if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
312                 array = container_of(map, struct bpf_array, map);
313                 owner_prog_type = array->owner_prog_type;
314                 owner_jited = array->owner_jited;
315         }
316
317         seq_printf(m,
318                    "map_type:\t%u\n"
319                    "key_size:\t%u\n"
320                    "value_size:\t%u\n"
321                    "max_entries:\t%u\n"
322                    "map_flags:\t%#x\n"
323                    "memlock:\t%llu\n",
324                    map->map_type,
325                    map->key_size,
326                    map->value_size,
327                    map->max_entries,
328                    map->map_flags,
329                    map->pages * 1ULL << PAGE_SHIFT);
330
331         if (owner_prog_type) {
332                 seq_printf(m, "owner_prog_type:\t%u\n",
333                            owner_prog_type);
334                 seq_printf(m, "owner_jited:\t%u\n",
335                            owner_jited);
336         }
337 }
338 #endif
339
340 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
341                               loff_t *ppos)
342 {
343         /* We need this handler such that alloc_file() enables
344          * f_mode with FMODE_CAN_READ.
345          */
346         return -EINVAL;
347 }
348
349 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
350                                size_t siz, loff_t *ppos)
351 {
352         /* We need this handler such that alloc_file() enables
353          * f_mode with FMODE_CAN_WRITE.
354          */
355         return -EINVAL;
356 }
357
358 const struct file_operations bpf_map_fops = {
359 #ifdef CONFIG_PROC_FS
360         .show_fdinfo    = bpf_map_show_fdinfo,
361 #endif
362         .release        = bpf_map_release,
363         .read           = bpf_dummy_read,
364         .write          = bpf_dummy_write,
365 };
366
367 int bpf_map_new_fd(struct bpf_map *map, int flags)
368 {
369         int ret;
370
371         ret = security_bpf_map(map, OPEN_FMODE(flags));
372         if (ret < 0)
373                 return ret;
374
375         return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
376                                 flags | O_CLOEXEC);
377 }
378
379 int bpf_get_file_flag(int flags)
380 {
381         if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
382                 return -EINVAL;
383         if (flags & BPF_F_RDONLY)
384                 return O_RDONLY;
385         if (flags & BPF_F_WRONLY)
386                 return O_WRONLY;
387         return O_RDWR;
388 }
389
390 /* helper macro to check that unused fields 'union bpf_attr' are zero */
391 #define CHECK_ATTR(CMD) \
392         memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
393                    sizeof(attr->CMD##_LAST_FIELD), 0, \
394                    sizeof(*attr) - \
395                    offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
396                    sizeof(attr->CMD##_LAST_FIELD)) != NULL
397
398 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
399  * Return 0 on success and < 0 on error.
400  */
401 static int bpf_obj_name_cpy(char *dst, const char *src)
402 {
403         const char *end = src + BPF_OBJ_NAME_LEN;
404
405         memset(dst, 0, BPF_OBJ_NAME_LEN);
406
407         /* Copy all isalnum() and '_' char */
408         while (src < end && *src) {
409                 if (!isalnum(*src) && *src != '_')
410                         return -EINVAL;
411                 *dst++ = *src++;
412         }
413
414         /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
415         if (src == end)
416                 return -EINVAL;
417
418         return 0;
419 }
420
421 #define BPF_MAP_CREATE_LAST_FIELD btf_value_id
422 /* called via syscall */
423 static int map_create(union bpf_attr *attr)
424 {
425         int numa_node = bpf_map_attr_numa_node(attr);
426         struct bpf_map *map;
427         int f_flags;
428         int err;
429
430         err = CHECK_ATTR(BPF_MAP_CREATE);
431         if (err)
432                 return -EINVAL;
433
434         f_flags = bpf_get_file_flag(attr->map_flags);
435         if (f_flags < 0)
436                 return f_flags;
437
438         if (numa_node != NUMA_NO_NODE &&
439             ((unsigned int)numa_node >= nr_node_ids ||
440              !node_online(numa_node)))
441                 return -EINVAL;
442
443         /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
444         map = find_and_alloc_map(attr);
445         if (IS_ERR(map))
446                 return PTR_ERR(map);
447
448         err = bpf_obj_name_cpy(map->name, attr->map_name);
449         if (err)
450                 goto free_map_nouncharge;
451
452         atomic_set(&map->refcnt, 1);
453         atomic_set(&map->usercnt, 1);
454
455         if (bpf_map_support_seq_show(map) &&
456             (attr->btf_key_id || attr->btf_value_id)) {
457                 struct btf *btf;
458
459                 if (!attr->btf_key_id || !attr->btf_value_id) {
460                         err = -EINVAL;
461                         goto free_map_nouncharge;
462                 }
463
464                 btf = btf_get_by_fd(attr->btf_fd);
465                 if (IS_ERR(btf)) {
466                         err = PTR_ERR(btf);
467                         goto free_map_nouncharge;
468                 }
469
470                 err = map->ops->map_check_btf(map, btf, attr->btf_key_id,
471                                               attr->btf_value_id);
472                 if (err) {
473                         btf_put(btf);
474                         goto free_map_nouncharge;
475                 }
476
477                 map->btf = btf;
478                 map->btf_key_id = attr->btf_key_id;
479                 map->btf_value_id = attr->btf_value_id;
480         }
481
482         err = security_bpf_map_alloc(map);
483         if (err)
484                 goto free_map_nouncharge;
485
486         err = bpf_map_charge_memlock(map);
487         if (err)
488                 goto free_map_sec;
489
490         err = bpf_map_alloc_id(map);
491         if (err)
492                 goto free_map;
493
494         err = bpf_map_new_fd(map, f_flags);
495         if (err < 0) {
496                 /* failed to allocate fd.
497                  * bpf_map_put() is needed because the above
498                  * bpf_map_alloc_id() has published the map
499                  * to the userspace and the userspace may
500                  * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
501                  */
502                 bpf_map_put(map);
503                 return err;
504         }
505
506         return err;
507
508 free_map:
509         bpf_map_uncharge_memlock(map);
510 free_map_sec:
511         security_bpf_map_free(map);
512 free_map_nouncharge:
513         btf_put(map->btf);
514         map->ops->map_free(map);
515         return err;
516 }
517
518 /* if error is returned, fd is released.
519  * On success caller should complete fd access with matching fdput()
520  */
521 struct bpf_map *__bpf_map_get(struct fd f)
522 {
523         if (!f.file)
524                 return ERR_PTR(-EBADF);
525         if (f.file->f_op != &bpf_map_fops) {
526                 fdput(f);
527                 return ERR_PTR(-EINVAL);
528         }
529
530         return f.file->private_data;
531 }
532
533 /* prog's and map's refcnt limit */
534 #define BPF_MAX_REFCNT 32768
535
536 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
537 {
538         if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
539                 atomic_dec(&map->refcnt);
540                 return ERR_PTR(-EBUSY);
541         }
542         if (uref)
543                 atomic_inc(&map->usercnt);
544         return map;
545 }
546
547 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
548 {
549         struct fd f = fdget(ufd);
550         struct bpf_map *map;
551
552         map = __bpf_map_get(f);
553         if (IS_ERR(map))
554                 return map;
555
556         map = bpf_map_inc(map, true);
557         fdput(f);
558
559         return map;
560 }
561
562 /* map_idr_lock should have been held */
563 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
564                                             bool uref)
565 {
566         int refold;
567
568         refold = __atomic_add_unless(&map->refcnt, 1, 0);
569
570         if (refold >= BPF_MAX_REFCNT) {
571                 __bpf_map_put(map, false);
572                 return ERR_PTR(-EBUSY);
573         }
574
575         if (!refold)
576                 return ERR_PTR(-ENOENT);
577
578         if (uref)
579                 atomic_inc(&map->usercnt);
580
581         return map;
582 }
583
584 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
585 {
586         return -ENOTSUPP;
587 }
588
589 /* last field in 'union bpf_attr' used by this command */
590 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
591
592 static int map_lookup_elem(union bpf_attr *attr)
593 {
594         void __user *ukey = u64_to_user_ptr(attr->key);
595         void __user *uvalue = u64_to_user_ptr(attr->value);
596         int ufd = attr->map_fd;
597         struct bpf_map *map;
598         void *key, *value, *ptr;
599         u32 value_size;
600         struct fd f;
601         int err;
602
603         if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
604                 return -EINVAL;
605
606         f = fdget(ufd);
607         map = __bpf_map_get(f);
608         if (IS_ERR(map))
609                 return PTR_ERR(map);
610
611         if (!(f.file->f_mode & FMODE_CAN_READ)) {
612                 err = -EPERM;
613                 goto err_put;
614         }
615
616         key = memdup_user(ukey, map->key_size);
617         if (IS_ERR(key)) {
618                 err = PTR_ERR(key);
619                 goto err_put;
620         }
621
622         if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
623             map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
624             map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
625                 value_size = round_up(map->value_size, 8) * num_possible_cpus();
626         else if (IS_FD_MAP(map))
627                 value_size = sizeof(u32);
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         if (bpf_map_is_dev_bound(map)) {
637                 err = bpf_map_offload_lookup_elem(map, key, value);
638         } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
639                    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
640                 err = bpf_percpu_hash_copy(map, key, value);
641         } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
642                 err = bpf_percpu_array_copy(map, key, value);
643         } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
644                 err = bpf_stackmap_copy(map, key, value);
645         } else if (IS_FD_ARRAY(map)) {
646                 err = bpf_fd_array_map_lookup_elem(map, key, value);
647         } else if (IS_FD_HASH(map)) {
648                 err = bpf_fd_htab_map_lookup_elem(map, key, value);
649         } else {
650                 rcu_read_lock();
651                 ptr = map->ops->map_lookup_elem(map, key);
652                 if (ptr)
653                         memcpy(value, ptr, value_size);
654                 rcu_read_unlock();
655                 err = ptr ? 0 : -ENOENT;
656         }
657
658         if (err)
659                 goto free_value;
660
661         err = -EFAULT;
662         if (copy_to_user(uvalue, value, value_size) != 0)
663                 goto free_value;
664
665         err = 0;
666
667 free_value:
668         kfree(value);
669 free_key:
670         kfree(key);
671 err_put:
672         fdput(f);
673         return err;
674 }
675
676 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
677
678 static int map_update_elem(union bpf_attr *attr)
679 {
680         void __user *ukey = u64_to_user_ptr(attr->key);
681         void __user *uvalue = u64_to_user_ptr(attr->value);
682         int ufd = attr->map_fd;
683         struct bpf_map *map;
684         void *key, *value;
685         u32 value_size;
686         struct fd f;
687         int err;
688
689         if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
690                 return -EINVAL;
691
692         f = fdget(ufd);
693         map = __bpf_map_get(f);
694         if (IS_ERR(map))
695                 return PTR_ERR(map);
696
697         if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
698                 err = -EPERM;
699                 goto err_put;
700         }
701
702         key = memdup_user(ukey, map->key_size);
703         if (IS_ERR(key)) {
704                 err = PTR_ERR(key);
705                 goto err_put;
706         }
707
708         if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
709             map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
710             map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
711                 value_size = round_up(map->value_size, 8) * num_possible_cpus();
712         else
713                 value_size = map->value_size;
714
715         err = -ENOMEM;
716         value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
717         if (!value)
718                 goto free_key;
719
720         err = -EFAULT;
721         if (copy_from_user(value, uvalue, value_size) != 0)
722                 goto free_value;
723
724         /* Need to create a kthread, thus must support schedule */
725         if (bpf_map_is_dev_bound(map)) {
726                 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
727                 goto out;
728         } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
729                 err = map->ops->map_update_elem(map, key, value, attr->flags);
730                 goto out;
731         }
732
733         /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
734          * inside bpf map update or delete otherwise deadlocks are possible
735          */
736         preempt_disable();
737         __this_cpu_inc(bpf_prog_active);
738         if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
739             map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
740                 err = bpf_percpu_hash_update(map, key, value, attr->flags);
741         } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
742                 err = bpf_percpu_array_update(map, key, value, attr->flags);
743         } else if (IS_FD_ARRAY(map)) {
744                 rcu_read_lock();
745                 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
746                                                    attr->flags);
747                 rcu_read_unlock();
748         } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
749                 rcu_read_lock();
750                 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
751                                                   attr->flags);
752                 rcu_read_unlock();
753         } else {
754                 rcu_read_lock();
755                 err = map->ops->map_update_elem(map, key, value, attr->flags);
756                 rcu_read_unlock();
757         }
758         __this_cpu_dec(bpf_prog_active);
759         preempt_enable();
760 out:
761 free_value:
762         kfree(value);
763 free_key:
764         kfree(key);
765 err_put:
766         fdput(f);
767         return err;
768 }
769
770 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
771
772 static int map_delete_elem(union bpf_attr *attr)
773 {
774         void __user *ukey = u64_to_user_ptr(attr->key);
775         int ufd = attr->map_fd;
776         struct bpf_map *map;
777         struct fd f;
778         void *key;
779         int err;
780
781         if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
782                 return -EINVAL;
783
784         f = fdget(ufd);
785         map = __bpf_map_get(f);
786         if (IS_ERR(map))
787                 return PTR_ERR(map);
788
789         if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
790                 err = -EPERM;
791                 goto err_put;
792         }
793
794         key = memdup_user(ukey, map->key_size);
795         if (IS_ERR(key)) {
796                 err = PTR_ERR(key);
797                 goto err_put;
798         }
799
800         if (bpf_map_is_dev_bound(map)) {
801                 err = bpf_map_offload_delete_elem(map, key);
802                 goto out;
803         }
804
805         preempt_disable();
806         __this_cpu_inc(bpf_prog_active);
807         rcu_read_lock();
808         err = map->ops->map_delete_elem(map, key);
809         rcu_read_unlock();
810         __this_cpu_dec(bpf_prog_active);
811         preempt_enable();
812 out:
813         kfree(key);
814 err_put:
815         fdput(f);
816         return err;
817 }
818
819 /* last field in 'union bpf_attr' used by this command */
820 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
821
822 static int map_get_next_key(union bpf_attr *attr)
823 {
824         void __user *ukey = u64_to_user_ptr(attr->key);
825         void __user *unext_key = u64_to_user_ptr(attr->next_key);
826         int ufd = attr->map_fd;
827         struct bpf_map *map;
828         void *key, *next_key;
829         struct fd f;
830         int err;
831
832         if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
833                 return -EINVAL;
834
835         f = fdget(ufd);
836         map = __bpf_map_get(f);
837         if (IS_ERR(map))
838                 return PTR_ERR(map);
839
840         if (!(f.file->f_mode & FMODE_CAN_READ)) {
841                 err = -EPERM;
842                 goto err_put;
843         }
844
845         if (ukey) {
846                 key = memdup_user(ukey, map->key_size);
847                 if (IS_ERR(key)) {
848                         err = PTR_ERR(key);
849                         goto err_put;
850                 }
851         } else {
852                 key = NULL;
853         }
854
855         err = -ENOMEM;
856         next_key = kmalloc(map->key_size, GFP_USER);
857         if (!next_key)
858                 goto free_key;
859
860         if (bpf_map_is_dev_bound(map)) {
861                 err = bpf_map_offload_get_next_key(map, key, next_key);
862                 goto out;
863         }
864
865         rcu_read_lock();
866         err = map->ops->map_get_next_key(map, key, next_key);
867         rcu_read_unlock();
868 out:
869         if (err)
870                 goto free_next_key;
871
872         err = -EFAULT;
873         if (copy_to_user(unext_key, next_key, map->key_size) != 0)
874                 goto free_next_key;
875
876         err = 0;
877
878 free_next_key:
879         kfree(next_key);
880 free_key:
881         kfree(key);
882 err_put:
883         fdput(f);
884         return err;
885 }
886
887 static const struct bpf_prog_ops * const bpf_prog_types[] = {
888 #define BPF_PROG_TYPE(_id, _name) \
889         [_id] = & _name ## _prog_ops,
890 #define BPF_MAP_TYPE(_id, _ops)
891 #include <linux/bpf_types.h>
892 #undef BPF_PROG_TYPE
893 #undef BPF_MAP_TYPE
894 };
895
896 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
897 {
898         if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
899                 return -EINVAL;
900
901         if (!bpf_prog_is_dev_bound(prog->aux))
902                 prog->aux->ops = bpf_prog_types[type];
903         else
904                 prog->aux->ops = &bpf_offload_prog_ops;
905         prog->type = type;
906         return 0;
907 }
908
909 /* drop refcnt on maps used by eBPF program and free auxilary data */
910 static void free_used_maps(struct bpf_prog_aux *aux)
911 {
912         int i;
913
914         for (i = 0; i < aux->used_map_cnt; i++)
915                 bpf_map_put(aux->used_maps[i]);
916
917         kfree(aux->used_maps);
918 }
919
920 int __bpf_prog_charge(struct user_struct *user, u32 pages)
921 {
922         unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
923         unsigned long user_bufs;
924
925         if (user) {
926                 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
927                 if (user_bufs > memlock_limit) {
928                         atomic_long_sub(pages, &user->locked_vm);
929                         return -EPERM;
930                 }
931         }
932
933         return 0;
934 }
935
936 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
937 {
938         if (user)
939                 atomic_long_sub(pages, &user->locked_vm);
940 }
941
942 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
943 {
944         struct user_struct *user = get_current_user();
945         int ret;
946
947         ret = __bpf_prog_charge(user, prog->pages);
948         if (ret) {
949                 free_uid(user);
950                 return ret;
951         }
952
953         prog->aux->user = user;
954         return 0;
955 }
956
957 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
958 {
959         struct user_struct *user = prog->aux->user;
960
961         __bpf_prog_uncharge(user, prog->pages);
962         free_uid(user);
963 }
964
965 static int bpf_prog_alloc_id(struct bpf_prog *prog)
966 {
967         int id;
968
969         idr_preload(GFP_KERNEL);
970         spin_lock_bh(&prog_idr_lock);
971         id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
972         if (id > 0)
973                 prog->aux->id = id;
974         spin_unlock_bh(&prog_idr_lock);
975         idr_preload_end();
976
977         /* id is in [1, INT_MAX) */
978         if (WARN_ON_ONCE(!id))
979                 return -ENOSPC;
980
981         return id > 0 ? 0 : id;
982 }
983
984 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
985 {
986         /* cBPF to eBPF migrations are currently not in the idr store.
987          * Offloaded programs are removed from the store when their device
988          * disappears - even if someone grabs an fd to them they are unusable,
989          * simply waiting for refcnt to drop to be freed.
990          */
991         if (!prog->aux->id)
992                 return;
993
994         if (do_idr_lock)
995                 spin_lock_bh(&prog_idr_lock);
996         else
997                 __acquire(&prog_idr_lock);
998
999         idr_remove(&prog_idr, prog->aux->id);
1000         prog->aux->id = 0;
1001
1002         if (do_idr_lock)
1003                 spin_unlock_bh(&prog_idr_lock);
1004         else
1005                 __release(&prog_idr_lock);
1006 }
1007
1008 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1009 {
1010         struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1011
1012         free_used_maps(aux);
1013         bpf_prog_uncharge_memlock(aux->prog);
1014         security_bpf_prog_free(aux);
1015         bpf_prog_free(aux->prog);
1016 }
1017
1018 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1019 {
1020         if (atomic_dec_and_test(&prog->aux->refcnt)) {
1021                 int i;
1022
1023                 /* bpf_prog_free_id() must be called first */
1024                 bpf_prog_free_id(prog, do_idr_lock);
1025
1026                 for (i = 0; i < prog->aux->func_cnt; i++)
1027                         bpf_prog_kallsyms_del(prog->aux->func[i]);
1028                 bpf_prog_kallsyms_del(prog);
1029
1030                 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1031         }
1032 }
1033
1034 void bpf_prog_put(struct bpf_prog *prog)
1035 {
1036         __bpf_prog_put(prog, true);
1037 }
1038 EXPORT_SYMBOL_GPL(bpf_prog_put);
1039
1040 static int bpf_prog_release(struct inode *inode, struct file *filp)
1041 {
1042         struct bpf_prog *prog = filp->private_data;
1043
1044         bpf_prog_put(prog);
1045         return 0;
1046 }
1047
1048 #ifdef CONFIG_PROC_FS
1049 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1050 {
1051         const struct bpf_prog *prog = filp->private_data;
1052         char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1053
1054         bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1055         seq_printf(m,
1056                    "prog_type:\t%u\n"
1057                    "prog_jited:\t%u\n"
1058                    "prog_tag:\t%s\n"
1059                    "memlock:\t%llu\n",
1060                    prog->type,
1061                    prog->jited,
1062                    prog_tag,
1063                    prog->pages * 1ULL << PAGE_SHIFT);
1064 }
1065 #endif
1066
1067 const struct file_operations bpf_prog_fops = {
1068 #ifdef CONFIG_PROC_FS
1069         .show_fdinfo    = bpf_prog_show_fdinfo,
1070 #endif
1071         .release        = bpf_prog_release,
1072         .read           = bpf_dummy_read,
1073         .write          = bpf_dummy_write,
1074 };
1075
1076 int bpf_prog_new_fd(struct bpf_prog *prog)
1077 {
1078         int ret;
1079
1080         ret = security_bpf_prog(prog);
1081         if (ret < 0)
1082                 return ret;
1083
1084         return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1085                                 O_RDWR | O_CLOEXEC);
1086 }
1087
1088 static struct bpf_prog *____bpf_prog_get(struct fd f)
1089 {
1090         if (!f.file)
1091                 return ERR_PTR(-EBADF);
1092         if (f.file->f_op != &bpf_prog_fops) {
1093                 fdput(f);
1094                 return ERR_PTR(-EINVAL);
1095         }
1096
1097         return f.file->private_data;
1098 }
1099
1100 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
1101 {
1102         if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1103                 atomic_sub(i, &prog->aux->refcnt);
1104                 return ERR_PTR(-EBUSY);
1105         }
1106         return prog;
1107 }
1108 EXPORT_SYMBOL_GPL(bpf_prog_add);
1109
1110 void bpf_prog_sub(struct bpf_prog *prog, int i)
1111 {
1112         /* Only to be used for undoing previous bpf_prog_add() in some
1113          * error path. We still know that another entity in our call
1114          * path holds a reference to the program, thus atomic_sub() can
1115          * be safely used in such cases!
1116          */
1117         WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1118 }
1119 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1120
1121 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1122 {
1123         return bpf_prog_add(prog, 1);
1124 }
1125 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1126
1127 /* prog_idr_lock should have been held */
1128 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1129 {
1130         int refold;
1131
1132         refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1133
1134         if (refold >= BPF_MAX_REFCNT) {
1135                 __bpf_prog_put(prog, false);
1136                 return ERR_PTR(-EBUSY);
1137         }
1138
1139         if (!refold)
1140                 return ERR_PTR(-ENOENT);
1141
1142         return prog;
1143 }
1144 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1145
1146 bool bpf_prog_get_ok(struct bpf_prog *prog,
1147                             enum bpf_prog_type *attach_type, bool attach_drv)
1148 {
1149         /* not an attachment, just a refcount inc, always allow */
1150         if (!attach_type)
1151                 return true;
1152
1153         if (prog->type != *attach_type)
1154                 return false;
1155         if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1156                 return false;
1157
1158         return true;
1159 }
1160
1161 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1162                                        bool attach_drv)
1163 {
1164         struct fd f = fdget(ufd);
1165         struct bpf_prog *prog;
1166
1167         prog = ____bpf_prog_get(f);
1168         if (IS_ERR(prog))
1169                 return prog;
1170         if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1171                 prog = ERR_PTR(-EINVAL);
1172                 goto out;
1173         }
1174
1175         prog = bpf_prog_inc(prog);
1176 out:
1177         fdput(f);
1178         return prog;
1179 }
1180
1181 struct bpf_prog *bpf_prog_get(u32 ufd)
1182 {
1183         return __bpf_prog_get(ufd, NULL, false);
1184 }
1185
1186 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1187                                        bool attach_drv)
1188 {
1189         return __bpf_prog_get(ufd, &type, attach_drv);
1190 }
1191 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1192
1193 /* Initially all BPF programs could be loaded w/o specifying
1194  * expected_attach_type. Later for some of them specifying expected_attach_type
1195  * at load time became required so that program could be validated properly.
1196  * Programs of types that are allowed to be loaded both w/ and w/o (for
1197  * backward compatibility) expected_attach_type, should have the default attach
1198  * type assigned to expected_attach_type for the latter case, so that it can be
1199  * validated later at attach time.
1200  *
1201  * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1202  * prog type requires it but has some attach types that have to be backward
1203  * compatible.
1204  */
1205 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1206 {
1207         switch (attr->prog_type) {
1208         case BPF_PROG_TYPE_CGROUP_SOCK:
1209                 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1210                  * exist so checking for non-zero is the way to go here.
1211                  */
1212                 if (!attr->expected_attach_type)
1213                         attr->expected_attach_type =
1214                                 BPF_CGROUP_INET_SOCK_CREATE;
1215                 break;
1216         }
1217 }
1218
1219 static int
1220 bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1221                                 enum bpf_attach_type expected_attach_type)
1222 {
1223         switch (prog_type) {
1224         case BPF_PROG_TYPE_CGROUP_SOCK:
1225                 switch (expected_attach_type) {
1226                 case BPF_CGROUP_INET_SOCK_CREATE:
1227                 case BPF_CGROUP_INET4_POST_BIND:
1228                 case BPF_CGROUP_INET6_POST_BIND:
1229                         return 0;
1230                 default:
1231                         return -EINVAL;
1232                 }
1233         case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1234                 switch (expected_attach_type) {
1235                 case BPF_CGROUP_INET4_BIND:
1236                 case BPF_CGROUP_INET6_BIND:
1237                 case BPF_CGROUP_INET4_CONNECT:
1238                 case BPF_CGROUP_INET6_CONNECT:
1239                         return 0;
1240                 default:
1241                         return -EINVAL;
1242                 }
1243         default:
1244                 return 0;
1245         }
1246 }
1247
1248 /* last field in 'union bpf_attr' used by this command */
1249 #define BPF_PROG_LOAD_LAST_FIELD expected_attach_type
1250
1251 static int bpf_prog_load(union bpf_attr *attr)
1252 {
1253         enum bpf_prog_type type = attr->prog_type;
1254         struct bpf_prog *prog;
1255         int err;
1256         char license[128];
1257         bool is_gpl;
1258
1259         if (CHECK_ATTR(BPF_PROG_LOAD))
1260                 return -EINVAL;
1261
1262         if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1263                 return -EINVAL;
1264
1265         /* copy eBPF program license from user space */
1266         if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1267                               sizeof(license) - 1) < 0)
1268                 return -EFAULT;
1269         license[sizeof(license) - 1] = 0;
1270
1271         /* eBPF programs must be GPL compatible to use GPL-ed functions */
1272         is_gpl = license_is_gpl_compatible(license);
1273
1274         if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1275                 return -E2BIG;
1276
1277         if (type == BPF_PROG_TYPE_KPROBE &&
1278             attr->kern_version != LINUX_VERSION_CODE)
1279                 return -EINVAL;
1280
1281         if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1282             type != BPF_PROG_TYPE_CGROUP_SKB &&
1283             !capable(CAP_SYS_ADMIN))
1284                 return -EPERM;
1285
1286         bpf_prog_load_fixup_attach_type(attr);
1287         if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1288                 return -EINVAL;
1289
1290         /* plain bpf_prog allocation */
1291         prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1292         if (!prog)
1293                 return -ENOMEM;
1294
1295         prog->expected_attach_type = attr->expected_attach_type;
1296
1297         prog->aux->offload_requested = !!attr->prog_ifindex;
1298
1299         err = security_bpf_prog_alloc(prog->aux);
1300         if (err)
1301                 goto free_prog_nouncharge;
1302
1303         err = bpf_prog_charge_memlock(prog);
1304         if (err)
1305                 goto free_prog_sec;
1306
1307         prog->len = attr->insn_cnt;
1308
1309         err = -EFAULT;
1310         if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1311                            bpf_prog_insn_size(prog)) != 0)
1312                 goto free_prog;
1313
1314         prog->orig_prog = NULL;
1315         prog->jited = 0;
1316
1317         atomic_set(&prog->aux->refcnt, 1);
1318         prog->gpl_compatible = is_gpl ? 1 : 0;
1319
1320         if (bpf_prog_is_dev_bound(prog->aux)) {
1321                 err = bpf_prog_offload_init(prog, attr);
1322                 if (err)
1323                         goto free_prog;
1324         }
1325
1326         /* find program type: socket_filter vs tracing_filter */
1327         err = find_prog_type(type, prog);
1328         if (err < 0)
1329                 goto free_prog;
1330
1331         prog->aux->load_time = ktime_get_boot_ns();
1332         err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1333         if (err)
1334                 goto free_prog;
1335
1336         /* run eBPF verifier */
1337         err = bpf_check(&prog, attr);
1338         if (err < 0)
1339                 goto free_used_maps;
1340
1341         /* eBPF program is ready to be JITed */
1342         if (!prog->bpf_func)
1343                 prog = bpf_prog_select_runtime(prog, &err);
1344         if (err < 0)
1345                 goto free_used_maps;
1346
1347         err = bpf_prog_alloc_id(prog);
1348         if (err)
1349                 goto free_used_maps;
1350
1351         err = bpf_prog_new_fd(prog);
1352         if (err < 0) {
1353                 /* failed to allocate fd.
1354                  * bpf_prog_put() is needed because the above
1355                  * bpf_prog_alloc_id() has published the prog
1356                  * to the userspace and the userspace may
1357                  * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1358                  */
1359                 bpf_prog_put(prog);
1360                 return err;
1361         }
1362
1363         bpf_prog_kallsyms_add(prog);
1364         return err;
1365
1366 free_used_maps:
1367         free_used_maps(prog->aux);
1368 free_prog:
1369         bpf_prog_uncharge_memlock(prog);
1370 free_prog_sec:
1371         security_bpf_prog_free(prog->aux);
1372 free_prog_nouncharge:
1373         bpf_prog_free(prog);
1374         return err;
1375 }
1376
1377 #define BPF_OBJ_LAST_FIELD file_flags
1378
1379 static int bpf_obj_pin(const union bpf_attr *attr)
1380 {
1381         if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1382                 return -EINVAL;
1383
1384         return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1385 }
1386
1387 static int bpf_obj_get(const union bpf_attr *attr)
1388 {
1389         if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1390             attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1391                 return -EINVAL;
1392
1393         return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1394                                 attr->file_flags);
1395 }
1396
1397 struct bpf_raw_tracepoint {
1398         struct bpf_raw_event_map *btp;
1399         struct bpf_prog *prog;
1400 };
1401
1402 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1403 {
1404         struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1405
1406         if (raw_tp->prog) {
1407                 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1408                 bpf_prog_put(raw_tp->prog);
1409         }
1410         kfree(raw_tp);
1411         return 0;
1412 }
1413
1414 static const struct file_operations bpf_raw_tp_fops = {
1415         .release        = bpf_raw_tracepoint_release,
1416         .read           = bpf_dummy_read,
1417         .write          = bpf_dummy_write,
1418 };
1419
1420 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1421
1422 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1423 {
1424         struct bpf_raw_tracepoint *raw_tp;
1425         struct bpf_raw_event_map *btp;
1426         struct bpf_prog *prog;
1427         char tp_name[128];
1428         int tp_fd, err;
1429
1430         if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1431                               sizeof(tp_name) - 1) < 0)
1432                 return -EFAULT;
1433         tp_name[sizeof(tp_name) - 1] = 0;
1434
1435         btp = bpf_find_raw_tracepoint(tp_name);
1436         if (!btp)
1437                 return -ENOENT;
1438
1439         raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1440         if (!raw_tp)
1441                 return -ENOMEM;
1442         raw_tp->btp = btp;
1443
1444         prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1445                                  BPF_PROG_TYPE_RAW_TRACEPOINT);
1446         if (IS_ERR(prog)) {
1447                 err = PTR_ERR(prog);
1448                 goto out_free_tp;
1449         }
1450
1451         err = bpf_probe_register(raw_tp->btp, prog);
1452         if (err)
1453                 goto out_put_prog;
1454
1455         raw_tp->prog = prog;
1456         tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1457                                  O_CLOEXEC);
1458         if (tp_fd < 0) {
1459                 bpf_probe_unregister(raw_tp->btp, prog);
1460                 err = tp_fd;
1461                 goto out_put_prog;
1462         }
1463         return tp_fd;
1464
1465 out_put_prog:
1466         bpf_prog_put(prog);
1467 out_free_tp:
1468         kfree(raw_tp);
1469         return err;
1470 }
1471
1472 #ifdef CONFIG_CGROUP_BPF
1473
1474 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1475                                              enum bpf_attach_type attach_type)
1476 {
1477         switch (prog->type) {
1478         case BPF_PROG_TYPE_CGROUP_SOCK:
1479         case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1480                 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1481         default:
1482                 return 0;
1483         }
1484 }
1485
1486 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1487
1488 static int sockmap_get_from_fd(const union bpf_attr *attr,
1489                                int type, bool attach)
1490 {
1491         struct bpf_prog *prog = NULL;
1492         int ufd = attr->target_fd;
1493         struct bpf_map *map;
1494         struct fd f;
1495         int err;
1496
1497         f = fdget(ufd);
1498         map = __bpf_map_get(f);
1499         if (IS_ERR(map))
1500                 return PTR_ERR(map);
1501
1502         if (attach) {
1503                 prog = bpf_prog_get_type(attr->attach_bpf_fd, type);
1504                 if (IS_ERR(prog)) {
1505                         fdput(f);
1506                         return PTR_ERR(prog);
1507                 }
1508         }
1509
1510         err = sock_map_prog(map, prog, attr->attach_type);
1511         if (err) {
1512                 fdput(f);
1513                 if (prog)
1514                         bpf_prog_put(prog);
1515                 return err;
1516         }
1517
1518         fdput(f);
1519         return 0;
1520 }
1521
1522 #define BPF_F_ATTACH_MASK \
1523         (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1524
1525 static int bpf_prog_attach(const union bpf_attr *attr)
1526 {
1527         enum bpf_prog_type ptype;
1528         struct bpf_prog *prog;
1529         struct cgroup *cgrp;
1530         int ret;
1531
1532         if (!capable(CAP_NET_ADMIN))
1533                 return -EPERM;
1534
1535         if (CHECK_ATTR(BPF_PROG_ATTACH))
1536                 return -EINVAL;
1537
1538         if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
1539                 return -EINVAL;
1540
1541         switch (attr->attach_type) {
1542         case BPF_CGROUP_INET_INGRESS:
1543         case BPF_CGROUP_INET_EGRESS:
1544                 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1545                 break;
1546         case BPF_CGROUP_INET_SOCK_CREATE:
1547         case BPF_CGROUP_INET4_POST_BIND:
1548         case BPF_CGROUP_INET6_POST_BIND:
1549                 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1550                 break;
1551         case BPF_CGROUP_INET4_BIND:
1552         case BPF_CGROUP_INET6_BIND:
1553         case BPF_CGROUP_INET4_CONNECT:
1554         case BPF_CGROUP_INET6_CONNECT:
1555                 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1556                 break;
1557         case BPF_CGROUP_SOCK_OPS:
1558                 ptype = BPF_PROG_TYPE_SOCK_OPS;
1559                 break;
1560         case BPF_CGROUP_DEVICE:
1561                 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1562                 break;
1563         case BPF_SK_MSG_VERDICT:
1564                 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true);
1565         case BPF_SK_SKB_STREAM_PARSER:
1566         case BPF_SK_SKB_STREAM_VERDICT:
1567                 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true);
1568         default:
1569                 return -EINVAL;
1570         }
1571
1572         prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1573         if (IS_ERR(prog))
1574                 return PTR_ERR(prog);
1575
1576         if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1577                 bpf_prog_put(prog);
1578                 return -EINVAL;
1579         }
1580
1581         cgrp = cgroup_get_from_fd(attr->target_fd);
1582         if (IS_ERR(cgrp)) {
1583                 bpf_prog_put(prog);
1584                 return PTR_ERR(cgrp);
1585         }
1586
1587         ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1588                                 attr->attach_flags);
1589         if (ret)
1590                 bpf_prog_put(prog);
1591         cgroup_put(cgrp);
1592
1593         return ret;
1594 }
1595
1596 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1597
1598 static int bpf_prog_detach(const union bpf_attr *attr)
1599 {
1600         enum bpf_prog_type ptype;
1601         struct bpf_prog *prog;
1602         struct cgroup *cgrp;
1603         int ret;
1604
1605         if (!capable(CAP_NET_ADMIN))
1606                 return -EPERM;
1607
1608         if (CHECK_ATTR(BPF_PROG_DETACH))
1609                 return -EINVAL;
1610
1611         switch (attr->attach_type) {
1612         case BPF_CGROUP_INET_INGRESS:
1613         case BPF_CGROUP_INET_EGRESS:
1614                 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1615                 break;
1616         case BPF_CGROUP_INET_SOCK_CREATE:
1617         case BPF_CGROUP_INET4_POST_BIND:
1618         case BPF_CGROUP_INET6_POST_BIND:
1619                 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1620                 break;
1621         case BPF_CGROUP_INET4_BIND:
1622         case BPF_CGROUP_INET6_BIND:
1623         case BPF_CGROUP_INET4_CONNECT:
1624         case BPF_CGROUP_INET6_CONNECT:
1625                 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1626                 break;
1627         case BPF_CGROUP_SOCK_OPS:
1628                 ptype = BPF_PROG_TYPE_SOCK_OPS;
1629                 break;
1630         case BPF_CGROUP_DEVICE:
1631                 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1632                 break;
1633         case BPF_SK_MSG_VERDICT:
1634                 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false);
1635         case BPF_SK_SKB_STREAM_PARSER:
1636         case BPF_SK_SKB_STREAM_VERDICT:
1637                 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false);
1638         default:
1639                 return -EINVAL;
1640         }
1641
1642         cgrp = cgroup_get_from_fd(attr->target_fd);
1643         if (IS_ERR(cgrp))
1644                 return PTR_ERR(cgrp);
1645
1646         prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1647         if (IS_ERR(prog))
1648                 prog = NULL;
1649
1650         ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1651         if (prog)
1652                 bpf_prog_put(prog);
1653         cgroup_put(cgrp);
1654         return ret;
1655 }
1656
1657 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1658
1659 static int bpf_prog_query(const union bpf_attr *attr,
1660                           union bpf_attr __user *uattr)
1661 {
1662         struct cgroup *cgrp;
1663         int ret;
1664
1665         if (!capable(CAP_NET_ADMIN))
1666                 return -EPERM;
1667         if (CHECK_ATTR(BPF_PROG_QUERY))
1668                 return -EINVAL;
1669         if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1670                 return -EINVAL;
1671
1672         switch (attr->query.attach_type) {
1673         case BPF_CGROUP_INET_INGRESS:
1674         case BPF_CGROUP_INET_EGRESS:
1675         case BPF_CGROUP_INET_SOCK_CREATE:
1676         case BPF_CGROUP_INET4_BIND:
1677         case BPF_CGROUP_INET6_BIND:
1678         case BPF_CGROUP_INET4_POST_BIND:
1679         case BPF_CGROUP_INET6_POST_BIND:
1680         case BPF_CGROUP_INET4_CONNECT:
1681         case BPF_CGROUP_INET6_CONNECT:
1682         case BPF_CGROUP_SOCK_OPS:
1683         case BPF_CGROUP_DEVICE:
1684                 break;
1685         default:
1686                 return -EINVAL;
1687         }
1688         cgrp = cgroup_get_from_fd(attr->query.target_fd);
1689         if (IS_ERR(cgrp))
1690                 return PTR_ERR(cgrp);
1691         ret = cgroup_bpf_query(cgrp, attr, uattr);
1692         cgroup_put(cgrp);
1693         return ret;
1694 }
1695 #endif /* CONFIG_CGROUP_BPF */
1696
1697 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1698
1699 static int bpf_prog_test_run(const union bpf_attr *attr,
1700                              union bpf_attr __user *uattr)
1701 {
1702         struct bpf_prog *prog;
1703         int ret = -ENOTSUPP;
1704
1705         if (!capable(CAP_SYS_ADMIN))
1706                 return -EPERM;
1707         if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1708                 return -EINVAL;
1709
1710         prog = bpf_prog_get(attr->test.prog_fd);
1711         if (IS_ERR(prog))
1712                 return PTR_ERR(prog);
1713
1714         if (prog->aux->ops->test_run)
1715                 ret = prog->aux->ops->test_run(prog, attr, uattr);
1716
1717         bpf_prog_put(prog);
1718         return ret;
1719 }
1720
1721 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1722
1723 static int bpf_obj_get_next_id(const union bpf_attr *attr,
1724                                union bpf_attr __user *uattr,
1725                                struct idr *idr,
1726                                spinlock_t *lock)
1727 {
1728         u32 next_id = attr->start_id;
1729         int err = 0;
1730
1731         if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1732                 return -EINVAL;
1733
1734         if (!capable(CAP_SYS_ADMIN))
1735                 return -EPERM;
1736
1737         next_id++;
1738         spin_lock_bh(lock);
1739         if (!idr_get_next(idr, &next_id))
1740                 err = -ENOENT;
1741         spin_unlock_bh(lock);
1742
1743         if (!err)
1744                 err = put_user(next_id, &uattr->next_id);
1745
1746         return err;
1747 }
1748
1749 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1750
1751 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1752 {
1753         struct bpf_prog *prog;
1754         u32 id = attr->prog_id;
1755         int fd;
1756
1757         if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1758                 return -EINVAL;
1759
1760         if (!capable(CAP_SYS_ADMIN))
1761                 return -EPERM;
1762
1763         spin_lock_bh(&prog_idr_lock);
1764         prog = idr_find(&prog_idr, id);
1765         if (prog)
1766                 prog = bpf_prog_inc_not_zero(prog);
1767         else
1768                 prog = ERR_PTR(-ENOENT);
1769         spin_unlock_bh(&prog_idr_lock);
1770
1771         if (IS_ERR(prog))
1772                 return PTR_ERR(prog);
1773
1774         fd = bpf_prog_new_fd(prog);
1775         if (fd < 0)
1776                 bpf_prog_put(prog);
1777
1778         return fd;
1779 }
1780
1781 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
1782
1783 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1784 {
1785         struct bpf_map *map;
1786         u32 id = attr->map_id;
1787         int f_flags;
1788         int fd;
1789
1790         if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1791             attr->open_flags & ~BPF_OBJ_FLAG_MASK)
1792                 return -EINVAL;
1793
1794         if (!capable(CAP_SYS_ADMIN))
1795                 return -EPERM;
1796
1797         f_flags = bpf_get_file_flag(attr->open_flags);
1798         if (f_flags < 0)
1799                 return f_flags;
1800
1801         spin_lock_bh(&map_idr_lock);
1802         map = idr_find(&map_idr, id);
1803         if (map)
1804                 map = bpf_map_inc_not_zero(map, true);
1805         else
1806                 map = ERR_PTR(-ENOENT);
1807         spin_unlock_bh(&map_idr_lock);
1808
1809         if (IS_ERR(map))
1810                 return PTR_ERR(map);
1811
1812         fd = bpf_map_new_fd(map, f_flags);
1813         if (fd < 0)
1814                 bpf_map_put(map);
1815
1816         return fd;
1817 }
1818
1819 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1820                                               unsigned long addr)
1821 {
1822         int i;
1823
1824         for (i = 0; i < prog->aux->used_map_cnt; i++)
1825                 if (prog->aux->used_maps[i] == (void *)addr)
1826                         return prog->aux->used_maps[i];
1827         return NULL;
1828 }
1829
1830 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1831 {
1832         const struct bpf_map *map;
1833         struct bpf_insn *insns;
1834         u64 imm;
1835         int i;
1836
1837         insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1838                         GFP_USER);
1839         if (!insns)
1840                 return insns;
1841
1842         for (i = 0; i < prog->len; i++) {
1843                 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1844                         insns[i].code = BPF_JMP | BPF_CALL;
1845                         insns[i].imm = BPF_FUNC_tail_call;
1846                         /* fall-through */
1847                 }
1848                 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1849                     insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1850                         if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1851                                 insns[i].code = BPF_JMP | BPF_CALL;
1852                         if (!bpf_dump_raw_ok())
1853                                 insns[i].imm = 0;
1854                         continue;
1855                 }
1856
1857                 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1858                         continue;
1859
1860                 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1861                 map = bpf_map_from_imm(prog, imm);
1862                 if (map) {
1863                         insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1864                         insns[i].imm = map->id;
1865                         insns[i + 1].imm = 0;
1866                         continue;
1867                 }
1868
1869                 if (!bpf_dump_raw_ok() &&
1870                     imm == (unsigned long)prog->aux) {
1871                         insns[i].imm = 0;
1872                         insns[i + 1].imm = 0;
1873                         continue;
1874                 }
1875         }
1876
1877         return insns;
1878 }
1879
1880 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1881                                    const union bpf_attr *attr,
1882                                    union bpf_attr __user *uattr)
1883 {
1884         struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1885         struct bpf_prog_info info = {};
1886         u32 info_len = attr->info.info_len;
1887         char __user *uinsns;
1888         u32 ulen;
1889         int err;
1890
1891         err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1892         if (err)
1893                 return err;
1894         info_len = min_t(u32, sizeof(info), info_len);
1895
1896         if (copy_from_user(&info, uinfo, info_len))
1897                 return -EFAULT;
1898
1899         info.type = prog->type;
1900         info.id = prog->aux->id;
1901         info.load_time = prog->aux->load_time;
1902         info.created_by_uid = from_kuid_munged(current_user_ns(),
1903                                                prog->aux->user->uid);
1904         info.gpl_compatible = prog->gpl_compatible;
1905
1906         memcpy(info.tag, prog->tag, sizeof(prog->tag));
1907         memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1908
1909         ulen = info.nr_map_ids;
1910         info.nr_map_ids = prog->aux->used_map_cnt;
1911         ulen = min_t(u32, info.nr_map_ids, ulen);
1912         if (ulen) {
1913                 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
1914                 u32 i;
1915
1916                 for (i = 0; i < ulen; i++)
1917                         if (put_user(prog->aux->used_maps[i]->id,
1918                                      &user_map_ids[i]))
1919                                 return -EFAULT;
1920         }
1921
1922         if (!capable(CAP_SYS_ADMIN)) {
1923                 info.jited_prog_len = 0;
1924                 info.xlated_prog_len = 0;
1925                 goto done;
1926         }
1927
1928         ulen = info.xlated_prog_len;
1929         info.xlated_prog_len = bpf_prog_insn_size(prog);
1930         if (info.xlated_prog_len && ulen) {
1931                 struct bpf_insn *insns_sanitized;
1932                 bool fault;
1933
1934                 if (prog->blinded && !bpf_dump_raw_ok()) {
1935                         info.xlated_prog_insns = 0;
1936                         goto done;
1937                 }
1938                 insns_sanitized = bpf_insn_prepare_dump(prog);
1939                 if (!insns_sanitized)
1940                         return -ENOMEM;
1941                 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1942                 ulen = min_t(u32, info.xlated_prog_len, ulen);
1943                 fault = copy_to_user(uinsns, insns_sanitized, ulen);
1944                 kfree(insns_sanitized);
1945                 if (fault)
1946                         return -EFAULT;
1947         }
1948
1949         if (bpf_prog_is_dev_bound(prog->aux)) {
1950                 err = bpf_prog_offload_info_fill(&info, prog);
1951                 if (err)
1952                         return err;
1953                 goto done;
1954         }
1955
1956         /* NOTE: the following code is supposed to be skipped for offload.
1957          * bpf_prog_offload_info_fill() is the place to fill similar fields
1958          * for offload.
1959          */
1960         ulen = info.jited_prog_len;
1961         info.jited_prog_len = prog->jited_len;
1962         if (info.jited_prog_len && ulen) {
1963                 if (bpf_dump_raw_ok()) {
1964                         uinsns = u64_to_user_ptr(info.jited_prog_insns);
1965                         ulen = min_t(u32, info.jited_prog_len, ulen);
1966                         if (copy_to_user(uinsns, prog->bpf_func, ulen))
1967                                 return -EFAULT;
1968                 } else {
1969                         info.jited_prog_insns = 0;
1970                 }
1971         }
1972
1973 done:
1974         if (copy_to_user(uinfo, &info, info_len) ||
1975             put_user(info_len, &uattr->info.info_len))
1976                 return -EFAULT;
1977
1978         return 0;
1979 }
1980
1981 static int bpf_map_get_info_by_fd(struct bpf_map *map,
1982                                   const union bpf_attr *attr,
1983                                   union bpf_attr __user *uattr)
1984 {
1985         struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1986         struct bpf_map_info info = {};
1987         u32 info_len = attr->info.info_len;
1988         int err;
1989
1990         err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1991         if (err)
1992                 return err;
1993         info_len = min_t(u32, sizeof(info), info_len);
1994
1995         info.type = map->map_type;
1996         info.id = map->id;
1997         info.key_size = map->key_size;
1998         info.value_size = map->value_size;
1999         info.max_entries = map->max_entries;
2000         info.map_flags = map->map_flags;
2001         memcpy(info.name, map->name, sizeof(map->name));
2002
2003         if (bpf_map_is_dev_bound(map)) {
2004                 err = bpf_map_offload_info_fill(&info, map);
2005                 if (err)
2006                         return err;
2007         }
2008
2009         if (copy_to_user(uinfo, &info, info_len) ||
2010             put_user(info_len, &uattr->info.info_len))
2011                 return -EFAULT;
2012
2013         return 0;
2014 }
2015
2016 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2017
2018 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2019                                   union bpf_attr __user *uattr)
2020 {
2021         int ufd = attr->info.bpf_fd;
2022         struct fd f;
2023         int err;
2024
2025         if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2026                 return -EINVAL;
2027
2028         f = fdget(ufd);
2029         if (!f.file)
2030                 return -EBADFD;
2031
2032         if (f.file->f_op == &bpf_prog_fops)
2033                 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2034                                               uattr);
2035         else if (f.file->f_op == &bpf_map_fops)
2036                 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2037                                              uattr);
2038         else if (f.file->f_op == &btf_fops)
2039                 err = btf_get_info_by_fd(f.file->private_data, attr, uattr);
2040         else
2041                 err = -EINVAL;
2042
2043         fdput(f);
2044         return err;
2045 }
2046
2047 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2048
2049 static int bpf_btf_load(const union bpf_attr *attr)
2050 {
2051         if (CHECK_ATTR(BPF_BTF_LOAD))
2052                 return -EINVAL;
2053
2054         if (!capable(CAP_SYS_ADMIN))
2055                 return -EPERM;
2056
2057         return btf_new_fd(attr);
2058 }
2059
2060 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2061 {
2062         union bpf_attr attr = {};
2063         int err;
2064
2065         if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
2066                 return -EPERM;
2067
2068         err = check_uarg_tail_zero(uattr, sizeof(attr), size);
2069         if (err)
2070                 return err;
2071         size = min_t(u32, size, sizeof(attr));
2072
2073         /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2074         if (copy_from_user(&attr, uattr, size) != 0)
2075                 return -EFAULT;
2076
2077         err = security_bpf(cmd, &attr, size);
2078         if (err < 0)
2079                 return err;
2080
2081         switch (cmd) {
2082         case BPF_MAP_CREATE:
2083                 err = map_create(&attr);
2084                 break;
2085         case BPF_MAP_LOOKUP_ELEM:
2086                 err = map_lookup_elem(&attr);
2087                 break;
2088         case BPF_MAP_UPDATE_ELEM:
2089                 err = map_update_elem(&attr);
2090                 break;
2091         case BPF_MAP_DELETE_ELEM:
2092                 err = map_delete_elem(&attr);
2093                 break;
2094         case BPF_MAP_GET_NEXT_KEY:
2095                 err = map_get_next_key(&attr);
2096                 break;
2097         case BPF_PROG_LOAD:
2098                 err = bpf_prog_load(&attr);
2099                 break;
2100         case BPF_OBJ_PIN:
2101                 err = bpf_obj_pin(&attr);
2102                 break;
2103         case BPF_OBJ_GET:
2104                 err = bpf_obj_get(&attr);
2105                 break;
2106 #ifdef CONFIG_CGROUP_BPF
2107         case BPF_PROG_ATTACH:
2108                 err = bpf_prog_attach(&attr);
2109                 break;
2110         case BPF_PROG_DETACH:
2111                 err = bpf_prog_detach(&attr);
2112                 break;
2113         case BPF_PROG_QUERY:
2114                 err = bpf_prog_query(&attr, uattr);
2115                 break;
2116 #endif
2117         case BPF_PROG_TEST_RUN:
2118                 err = bpf_prog_test_run(&attr, uattr);
2119                 break;
2120         case BPF_PROG_GET_NEXT_ID:
2121                 err = bpf_obj_get_next_id(&attr, uattr,
2122                                           &prog_idr, &prog_idr_lock);
2123                 break;
2124         case BPF_MAP_GET_NEXT_ID:
2125                 err = bpf_obj_get_next_id(&attr, uattr,
2126                                           &map_idr, &map_idr_lock);
2127                 break;
2128         case BPF_PROG_GET_FD_BY_ID:
2129                 err = bpf_prog_get_fd_by_id(&attr);
2130                 break;
2131         case BPF_MAP_GET_FD_BY_ID:
2132                 err = bpf_map_get_fd_by_id(&attr);
2133                 break;
2134         case BPF_OBJ_GET_INFO_BY_FD:
2135                 err = bpf_obj_get_info_by_fd(&attr, uattr);
2136                 break;
2137         case BPF_RAW_TRACEPOINT_OPEN:
2138                 err = bpf_raw_tracepoint_open(&attr);
2139                 break;
2140         case BPF_BTF_LOAD:
2141                 err = bpf_btf_load(&attr);
2142                 break;
2143         default:
2144                 err = -EINVAL;
2145                 break;
2146         }
2147
2148         return err;
2149 }