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