1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
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.
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.
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 #include <linux/cred.h>
27 #include <linux/timekeeping.h>
28 #include <linux/ctype.h>
30 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
31 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
32 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
33 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
34 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
35 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
37 DEFINE_PER_CPU(int, bpf_prog_active
);
38 static DEFINE_IDR(prog_idr
);
39 static DEFINE_SPINLOCK(prog_idr_lock
);
40 static DEFINE_IDR(map_idr
);
41 static DEFINE_SPINLOCK(map_idr_lock
);
43 int sysctl_unprivileged_bpf_disabled __read_mostly
;
45 static const struct bpf_map_ops
* const bpf_map_types
[] = {
46 #define BPF_PROG_TYPE(_id, _ops)
47 #define BPF_MAP_TYPE(_id, _ops) \
49 #include <linux/bpf_types.h>
55 * If we're handed a bigger struct than we know of, ensure all the unknown bits
56 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
57 * we don't know about yet.
59 * There is a ToCToU between this function call and the following
60 * copy_from_user() call. However, this is not a concern since this function is
61 * meant to be a future-proofing of bits.
63 static int check_uarg_tail_zero(void __user
*uaddr
,
67 unsigned char __user
*addr
;
68 unsigned char __user
*end
;
72 if (unlikely(actual_size
> PAGE_SIZE
)) /* silly large */
75 if (unlikely(!access_ok(VERIFY_READ
, uaddr
, actual_size
)))
78 if (actual_size
<= expected_size
)
81 addr
= uaddr
+ expected_size
;
82 end
= uaddr
+ actual_size
;
84 for (; addr
< end
; addr
++) {
85 err
= get_user(val
, addr
);
95 static struct bpf_map
*find_and_alloc_map(union bpf_attr
*attr
)
99 if (attr
->map_type
>= ARRAY_SIZE(bpf_map_types
) ||
100 !bpf_map_types
[attr
->map_type
])
101 return ERR_PTR(-EINVAL
);
103 map
= bpf_map_types
[attr
->map_type
]->map_alloc(attr
);
106 map
->ops
= bpf_map_types
[attr
->map_type
];
107 map
->map_type
= attr
->map_type
;
111 void *bpf_map_area_alloc(size_t size
, int numa_node
)
113 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
114 * trigger under memory pressure as we really just want to
117 const gfp_t flags
= __GFP_NOWARN
| __GFP_NORETRY
| __GFP_ZERO
;
120 if (size
<= (PAGE_SIZE
<< PAGE_ALLOC_COSTLY_ORDER
)) {
121 area
= kmalloc_node(size
, GFP_USER
| flags
, numa_node
);
126 return __vmalloc_node_flags_caller(size
, numa_node
, GFP_KERNEL
| flags
,
127 __builtin_return_address(0));
130 void bpf_map_area_free(void *area
)
135 int bpf_map_precharge_memlock(u32 pages
)
137 struct user_struct
*user
= get_current_user();
138 unsigned long memlock_limit
, cur
;
140 memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
141 cur
= atomic_long_read(&user
->locked_vm
);
143 if (cur
+ pages
> memlock_limit
)
148 static int bpf_map_charge_memlock(struct bpf_map
*map
)
150 struct user_struct
*user
= get_current_user();
151 unsigned long memlock_limit
;
153 memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
155 atomic_long_add(map
->pages
, &user
->locked_vm
);
157 if (atomic_long_read(&user
->locked_vm
) > memlock_limit
) {
158 atomic_long_sub(map
->pages
, &user
->locked_vm
);
166 static void bpf_map_uncharge_memlock(struct bpf_map
*map
)
168 struct user_struct
*user
= map
->user
;
170 atomic_long_sub(map
->pages
, &user
->locked_vm
);
174 static int bpf_map_alloc_id(struct bpf_map
*map
)
178 spin_lock_bh(&map_idr_lock
);
179 id
= idr_alloc_cyclic(&map_idr
, map
, 1, INT_MAX
, GFP_ATOMIC
);
182 spin_unlock_bh(&map_idr_lock
);
184 if (WARN_ON_ONCE(!id
))
187 return id
> 0 ? 0 : id
;
190 static void bpf_map_free_id(struct bpf_map
*map
, bool do_idr_lock
)
195 spin_lock_irqsave(&map_idr_lock
, flags
);
197 __acquire(&map_idr_lock
);
199 idr_remove(&map_idr
, map
->id
);
202 spin_unlock_irqrestore(&map_idr_lock
, flags
);
204 __release(&map_idr_lock
);
207 /* called from workqueue */
208 static void bpf_map_free_deferred(struct work_struct
*work
)
210 struct bpf_map
*map
= container_of(work
, struct bpf_map
, work
);
212 bpf_map_uncharge_memlock(map
);
213 /* implementation dependent freeing */
214 map
->ops
->map_free(map
);
217 static void bpf_map_put_uref(struct bpf_map
*map
)
219 if (atomic_dec_and_test(&map
->usercnt
)) {
220 if (map
->map_type
== BPF_MAP_TYPE_PROG_ARRAY
)
221 bpf_fd_array_map_clear(map
);
225 /* decrement map refcnt and schedule it for freeing via workqueue
226 * (unrelying map implementation ops->map_free() might sleep)
228 static void __bpf_map_put(struct bpf_map
*map
, bool do_idr_lock
)
230 if (atomic_dec_and_test(&map
->refcnt
)) {
231 /* bpf_map_free_id() must be called first */
232 bpf_map_free_id(map
, do_idr_lock
);
233 INIT_WORK(&map
->work
, bpf_map_free_deferred
);
234 schedule_work(&map
->work
);
238 void bpf_map_put(struct bpf_map
*map
)
240 __bpf_map_put(map
, true);
243 void bpf_map_put_with_uref(struct bpf_map
*map
)
245 bpf_map_put_uref(map
);
249 static int bpf_map_release(struct inode
*inode
, struct file
*filp
)
251 struct bpf_map
*map
= filp
->private_data
;
253 if (map
->ops
->map_release
)
254 map
->ops
->map_release(map
, filp
);
256 bpf_map_put_with_uref(map
);
260 #ifdef CONFIG_PROC_FS
261 static void bpf_map_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
263 const struct bpf_map
*map
= filp
->private_data
;
264 const struct bpf_array
*array
;
265 u32 owner_prog_type
= 0;
268 if (map
->map_type
== BPF_MAP_TYPE_PROG_ARRAY
) {
269 array
= container_of(map
, struct bpf_array
, map
);
270 owner_prog_type
= array
->owner_prog_type
;
271 owner_jited
= array
->owner_jited
;
286 map
->pages
* 1ULL << PAGE_SHIFT
);
288 if (owner_prog_type
) {
289 seq_printf(m
, "owner_prog_type:\t%u\n",
291 seq_printf(m
, "owner_jited:\t%u\n",
297 static const struct file_operations bpf_map_fops
= {
298 #ifdef CONFIG_PROC_FS
299 .show_fdinfo
= bpf_map_show_fdinfo
,
301 .release
= bpf_map_release
,
304 int bpf_map_new_fd(struct bpf_map
*map
)
306 return anon_inode_getfd("bpf-map", &bpf_map_fops
, map
,
310 /* helper macro to check that unused fields 'union bpf_attr' are zero */
311 #define CHECK_ATTR(CMD) \
312 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
313 sizeof(attr->CMD##_LAST_FIELD), 0, \
315 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
316 sizeof(attr->CMD##_LAST_FIELD)) != NULL
318 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
319 * Return 0 on success and < 0 on error.
321 static int bpf_obj_name_cpy(char *dst
, const char *src
)
323 const char *end
= src
+ BPF_OBJ_NAME_LEN
;
325 memset(dst
, 0, BPF_OBJ_NAME_LEN
);
327 /* Copy all isalnum() and '_' char */
328 while (src
< end
&& *src
) {
329 if (!isalnum(*src
) && *src
!= '_')
334 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
341 #define BPF_MAP_CREATE_LAST_FIELD map_name
342 /* called via syscall */
343 static int map_create(union bpf_attr
*attr
)
345 int numa_node
= bpf_map_attr_numa_node(attr
);
349 err
= CHECK_ATTR(BPF_MAP_CREATE
);
353 if (numa_node
!= NUMA_NO_NODE
&&
354 ((unsigned int)numa_node
>= nr_node_ids
||
355 !node_online(numa_node
)))
358 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
359 map
= find_and_alloc_map(attr
);
363 err
= bpf_obj_name_cpy(map
->name
, attr
->map_name
);
365 goto free_map_nouncharge
;
367 atomic_set(&map
->refcnt
, 1);
368 atomic_set(&map
->usercnt
, 1);
370 err
= bpf_map_charge_memlock(map
);
372 goto free_map_nouncharge
;
374 err
= bpf_map_alloc_id(map
);
378 err
= bpf_map_new_fd(map
);
380 /* failed to allocate fd.
381 * bpf_map_put() is needed because the above
382 * bpf_map_alloc_id() has published the map
383 * to the userspace and the userspace may
384 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
390 trace_bpf_map_create(map
, err
);
394 bpf_map_uncharge_memlock(map
);
396 map
->ops
->map_free(map
);
400 /* if error is returned, fd is released.
401 * On success caller should complete fd access with matching fdput()
403 struct bpf_map
*__bpf_map_get(struct fd f
)
406 return ERR_PTR(-EBADF
);
407 if (f
.file
->f_op
!= &bpf_map_fops
) {
409 return ERR_PTR(-EINVAL
);
412 return f
.file
->private_data
;
415 /* prog's and map's refcnt limit */
416 #define BPF_MAX_REFCNT 32768
418 struct bpf_map
*bpf_map_inc(struct bpf_map
*map
, bool uref
)
420 if (atomic_inc_return(&map
->refcnt
) > BPF_MAX_REFCNT
) {
421 atomic_dec(&map
->refcnt
);
422 return ERR_PTR(-EBUSY
);
425 atomic_inc(&map
->usercnt
);
429 struct bpf_map
*bpf_map_get_with_uref(u32 ufd
)
431 struct fd f
= fdget(ufd
);
434 map
= __bpf_map_get(f
);
438 map
= bpf_map_inc(map
, true);
444 /* map_idr_lock should have been held */
445 static struct bpf_map
*bpf_map_inc_not_zero(struct bpf_map
*map
,
450 refold
= __atomic_add_unless(&map
->refcnt
, 1, 0);
452 if (refold
>= BPF_MAX_REFCNT
) {
453 __bpf_map_put(map
, false);
454 return ERR_PTR(-EBUSY
);
458 return ERR_PTR(-ENOENT
);
461 atomic_inc(&map
->usercnt
);
466 int __weak
bpf_stackmap_copy(struct bpf_map
*map
, void *key
, void *value
)
471 /* last field in 'union bpf_attr' used by this command */
472 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
474 static int map_lookup_elem(union bpf_attr
*attr
)
476 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
477 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
478 int ufd
= attr
->map_fd
;
480 void *key
, *value
, *ptr
;
485 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM
))
489 map
= __bpf_map_get(f
);
493 key
= memdup_user(ukey
, map
->key_size
);
499 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
500 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
501 map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
)
502 value_size
= round_up(map
->value_size
, 8) * num_possible_cpus();
503 else if (IS_FD_MAP(map
))
504 value_size
= sizeof(u32
);
506 value_size
= map
->value_size
;
509 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
513 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
514 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
) {
515 err
= bpf_percpu_hash_copy(map
, key
, value
);
516 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
) {
517 err
= bpf_percpu_array_copy(map
, key
, value
);
518 } else if (map
->map_type
== BPF_MAP_TYPE_STACK_TRACE
) {
519 err
= bpf_stackmap_copy(map
, key
, value
);
520 } else if (IS_FD_ARRAY(map
)) {
521 err
= bpf_fd_array_map_lookup_elem(map
, key
, value
);
522 } else if (IS_FD_HASH(map
)) {
523 err
= bpf_fd_htab_map_lookup_elem(map
, key
, value
);
526 ptr
= map
->ops
->map_lookup_elem(map
, key
);
528 memcpy(value
, ptr
, value_size
);
530 err
= ptr
? 0 : -ENOENT
;
537 if (copy_to_user(uvalue
, value
, value_size
) != 0)
540 trace_bpf_map_lookup_elem(map
, ufd
, key
, value
);
552 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
554 static int map_update_elem(union bpf_attr
*attr
)
556 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
557 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
558 int ufd
= attr
->map_fd
;
565 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM
))
569 map
= __bpf_map_get(f
);
573 key
= memdup_user(ukey
, map
->key_size
);
579 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
580 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
581 map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
)
582 value_size
= round_up(map
->value_size
, 8) * num_possible_cpus();
584 value_size
= map
->value_size
;
587 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
592 if (copy_from_user(value
, uvalue
, value_size
) != 0)
595 /* Need to create a kthread, thus must support schedule */
596 if (map
->map_type
== BPF_MAP_TYPE_CPUMAP
) {
597 err
= map
->ops
->map_update_elem(map
, key
, value
, attr
->flags
);
601 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
602 * inside bpf map update or delete otherwise deadlocks are possible
605 __this_cpu_inc(bpf_prog_active
);
606 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
607 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
) {
608 err
= bpf_percpu_hash_update(map
, key
, value
, attr
->flags
);
609 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
) {
610 err
= bpf_percpu_array_update(map
, key
, value
, attr
->flags
);
611 } else if (map
->map_type
== BPF_MAP_TYPE_PERF_EVENT_ARRAY
||
612 map
->map_type
== BPF_MAP_TYPE_PROG_ARRAY
||
613 map
->map_type
== BPF_MAP_TYPE_CGROUP_ARRAY
||
614 map
->map_type
== BPF_MAP_TYPE_ARRAY_OF_MAPS
) {
616 err
= bpf_fd_array_map_update_elem(map
, f
.file
, key
, value
,
619 } else if (map
->map_type
== BPF_MAP_TYPE_HASH_OF_MAPS
) {
621 err
= bpf_fd_htab_map_update_elem(map
, f
.file
, key
, value
,
626 err
= map
->ops
->map_update_elem(map
, key
, value
, attr
->flags
);
629 __this_cpu_dec(bpf_prog_active
);
633 trace_bpf_map_update_elem(map
, ufd
, key
, value
);
643 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
645 static int map_delete_elem(union bpf_attr
*attr
)
647 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
648 int ufd
= attr
->map_fd
;
654 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM
))
658 map
= __bpf_map_get(f
);
662 key
= memdup_user(ukey
, map
->key_size
);
669 __this_cpu_inc(bpf_prog_active
);
671 err
= map
->ops
->map_delete_elem(map
, key
);
673 __this_cpu_dec(bpf_prog_active
);
677 trace_bpf_map_delete_elem(map
, ufd
, key
);
684 /* last field in 'union bpf_attr' used by this command */
685 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
687 static int map_get_next_key(union bpf_attr
*attr
)
689 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
690 void __user
*unext_key
= u64_to_user_ptr(attr
->next_key
);
691 int ufd
= attr
->map_fd
;
693 void *key
, *next_key
;
697 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY
))
701 map
= __bpf_map_get(f
);
706 key
= memdup_user(ukey
, map
->key_size
);
716 next_key
= kmalloc(map
->key_size
, GFP_USER
);
721 err
= map
->ops
->map_get_next_key(map
, key
, next_key
);
727 if (copy_to_user(unext_key
, next_key
, map
->key_size
) != 0)
730 trace_bpf_map_next_key(map
, ufd
, key
, next_key
);
742 static const struct bpf_prog_ops
* const bpf_prog_types
[] = {
743 #define BPF_PROG_TYPE(_id, _name) \
744 [_id] = & _name ## _prog_ops,
745 #define BPF_MAP_TYPE(_id, _ops)
746 #include <linux/bpf_types.h>
751 static const struct bpf_verifier_ops
* const bpf_verifier_ops
[] = {
752 #define BPF_PROG_TYPE(_id, _name) \
753 [_id] = & _name ## _verifier_ops,
754 #define BPF_MAP_TYPE(_id, _ops)
755 #include <linux/bpf_types.h>
760 static int find_prog_type(enum bpf_prog_type type
, struct bpf_prog
*prog
)
762 if (type
>= ARRAY_SIZE(bpf_prog_types
) || !bpf_prog_types
[type
])
765 prog
->aux
->ops
= bpf_prog_types
[type
];
766 prog
->aux
->vops
= bpf_verifier_ops
[type
];
771 /* drop refcnt on maps used by eBPF program and free auxilary data */
772 static void free_used_maps(struct bpf_prog_aux
*aux
)
776 for (i
= 0; i
< aux
->used_map_cnt
; i
++)
777 bpf_map_put(aux
->used_maps
[i
]);
779 kfree(aux
->used_maps
);
782 int __bpf_prog_charge(struct user_struct
*user
, u32 pages
)
784 unsigned long memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
785 unsigned long user_bufs
;
788 user_bufs
= atomic_long_add_return(pages
, &user
->locked_vm
);
789 if (user_bufs
> memlock_limit
) {
790 atomic_long_sub(pages
, &user
->locked_vm
);
798 void __bpf_prog_uncharge(struct user_struct
*user
, u32 pages
)
801 atomic_long_sub(pages
, &user
->locked_vm
);
804 static int bpf_prog_charge_memlock(struct bpf_prog
*prog
)
806 struct user_struct
*user
= get_current_user();
809 ret
= __bpf_prog_charge(user
, prog
->pages
);
815 prog
->aux
->user
= user
;
819 static void bpf_prog_uncharge_memlock(struct bpf_prog
*prog
)
821 struct user_struct
*user
= prog
->aux
->user
;
823 __bpf_prog_uncharge(user
, prog
->pages
);
827 static int bpf_prog_alloc_id(struct bpf_prog
*prog
)
831 spin_lock_bh(&prog_idr_lock
);
832 id
= idr_alloc_cyclic(&prog_idr
, prog
, 1, INT_MAX
, GFP_ATOMIC
);
835 spin_unlock_bh(&prog_idr_lock
);
837 /* id is in [1, INT_MAX) */
838 if (WARN_ON_ONCE(!id
))
841 return id
> 0 ? 0 : id
;
844 static void bpf_prog_free_id(struct bpf_prog
*prog
, bool do_idr_lock
)
846 /* cBPF to eBPF migrations are currently not in the idr store. */
851 spin_lock_bh(&prog_idr_lock
);
853 __acquire(&prog_idr_lock
);
855 idr_remove(&prog_idr
, prog
->aux
->id
);
858 spin_unlock_bh(&prog_idr_lock
);
860 __release(&prog_idr_lock
);
863 static void __bpf_prog_put_rcu(struct rcu_head
*rcu
)
865 struct bpf_prog_aux
*aux
= container_of(rcu
, struct bpf_prog_aux
, rcu
);
868 bpf_prog_uncharge_memlock(aux
->prog
);
869 bpf_prog_free(aux
->prog
);
872 static void __bpf_prog_put(struct bpf_prog
*prog
, bool do_idr_lock
)
874 if (atomic_dec_and_test(&prog
->aux
->refcnt
)) {
875 trace_bpf_prog_put_rcu(prog
);
876 /* bpf_prog_free_id() must be called first */
877 bpf_prog_free_id(prog
, do_idr_lock
);
878 bpf_prog_kallsyms_del(prog
);
879 call_rcu(&prog
->aux
->rcu
, __bpf_prog_put_rcu
);
883 void bpf_prog_put(struct bpf_prog
*prog
)
885 __bpf_prog_put(prog
, true);
887 EXPORT_SYMBOL_GPL(bpf_prog_put
);
889 static int bpf_prog_release(struct inode
*inode
, struct file
*filp
)
891 struct bpf_prog
*prog
= filp
->private_data
;
897 #ifdef CONFIG_PROC_FS
898 static void bpf_prog_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
900 const struct bpf_prog
*prog
= filp
->private_data
;
901 char prog_tag
[sizeof(prog
->tag
) * 2 + 1] = { };
903 bin2hex(prog_tag
, prog
->tag
, sizeof(prog
->tag
));
912 prog
->pages
* 1ULL << PAGE_SHIFT
);
916 static const struct file_operations bpf_prog_fops
= {
917 #ifdef CONFIG_PROC_FS
918 .show_fdinfo
= bpf_prog_show_fdinfo
,
920 .release
= bpf_prog_release
,
923 int bpf_prog_new_fd(struct bpf_prog
*prog
)
925 return anon_inode_getfd("bpf-prog", &bpf_prog_fops
, prog
,
929 static struct bpf_prog
*____bpf_prog_get(struct fd f
)
932 return ERR_PTR(-EBADF
);
933 if (f
.file
->f_op
!= &bpf_prog_fops
) {
935 return ERR_PTR(-EINVAL
);
938 return f
.file
->private_data
;
941 struct bpf_prog
*bpf_prog_add(struct bpf_prog
*prog
, int i
)
943 if (atomic_add_return(i
, &prog
->aux
->refcnt
) > BPF_MAX_REFCNT
) {
944 atomic_sub(i
, &prog
->aux
->refcnt
);
945 return ERR_PTR(-EBUSY
);
949 EXPORT_SYMBOL_GPL(bpf_prog_add
);
951 void bpf_prog_sub(struct bpf_prog
*prog
, int i
)
953 /* Only to be used for undoing previous bpf_prog_add() in some
954 * error path. We still know that another entity in our call
955 * path holds a reference to the program, thus atomic_sub() can
956 * be safely used in such cases!
958 WARN_ON(atomic_sub_return(i
, &prog
->aux
->refcnt
) == 0);
960 EXPORT_SYMBOL_GPL(bpf_prog_sub
);
962 struct bpf_prog
*bpf_prog_inc(struct bpf_prog
*prog
)
964 return bpf_prog_add(prog
, 1);
966 EXPORT_SYMBOL_GPL(bpf_prog_inc
);
968 /* prog_idr_lock should have been held */
969 struct bpf_prog
*bpf_prog_inc_not_zero(struct bpf_prog
*prog
)
973 refold
= __atomic_add_unless(&prog
->aux
->refcnt
, 1, 0);
975 if (refold
>= BPF_MAX_REFCNT
) {
976 __bpf_prog_put(prog
, false);
977 return ERR_PTR(-EBUSY
);
981 return ERR_PTR(-ENOENT
);
985 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero
);
987 static struct bpf_prog
*__bpf_prog_get(u32 ufd
, enum bpf_prog_type
*type
)
989 struct fd f
= fdget(ufd
);
990 struct bpf_prog
*prog
;
992 prog
= ____bpf_prog_get(f
);
995 if (type
&& prog
->type
!= *type
) {
996 prog
= ERR_PTR(-EINVAL
);
1000 prog
= bpf_prog_inc(prog
);
1006 struct bpf_prog
*bpf_prog_get(u32 ufd
)
1008 return __bpf_prog_get(ufd
, NULL
);
1011 struct bpf_prog
*bpf_prog_get_type(u32 ufd
, enum bpf_prog_type type
)
1013 struct bpf_prog
*prog
= __bpf_prog_get(ufd
, &type
);
1016 trace_bpf_prog_get_type(prog
);
1019 EXPORT_SYMBOL_GPL(bpf_prog_get_type
);
1021 /* last field in 'union bpf_attr' used by this command */
1022 #define BPF_PROG_LOAD_LAST_FIELD prog_name
1024 static int bpf_prog_load(union bpf_attr
*attr
)
1026 enum bpf_prog_type type
= attr
->prog_type
;
1027 struct bpf_prog
*prog
;
1032 if (CHECK_ATTR(BPF_PROG_LOAD
))
1035 if (attr
->prog_flags
& ~BPF_F_STRICT_ALIGNMENT
)
1038 /* copy eBPF program license from user space */
1039 if (strncpy_from_user(license
, u64_to_user_ptr(attr
->license
),
1040 sizeof(license
) - 1) < 0)
1042 license
[sizeof(license
) - 1] = 0;
1044 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1045 is_gpl
= license_is_gpl_compatible(license
);
1047 if (attr
->insn_cnt
== 0 || attr
->insn_cnt
> BPF_MAXINSNS
)
1050 if (type
== BPF_PROG_TYPE_KPROBE
&&
1051 attr
->kern_version
!= LINUX_VERSION_CODE
)
1054 if (type
!= BPF_PROG_TYPE_SOCKET_FILTER
&&
1055 type
!= BPF_PROG_TYPE_CGROUP_SKB
&&
1056 !capable(CAP_SYS_ADMIN
))
1059 /* plain bpf_prog allocation */
1060 prog
= bpf_prog_alloc(bpf_prog_size(attr
->insn_cnt
), GFP_USER
);
1064 err
= bpf_prog_charge_memlock(prog
);
1066 goto free_prog_nouncharge
;
1068 prog
->len
= attr
->insn_cnt
;
1071 if (copy_from_user(prog
->insns
, u64_to_user_ptr(attr
->insns
),
1072 bpf_prog_insn_size(prog
)) != 0)
1075 prog
->orig_prog
= NULL
;
1078 atomic_set(&prog
->aux
->refcnt
, 1);
1079 prog
->gpl_compatible
= is_gpl
? 1 : 0;
1081 /* find program type: socket_filter vs tracing_filter */
1082 err
= find_prog_type(type
, prog
);
1086 prog
->aux
->load_time
= ktime_get_boot_ns();
1087 err
= bpf_obj_name_cpy(prog
->aux
->name
, attr
->prog_name
);
1091 /* run eBPF verifier */
1092 err
= bpf_check(&prog
, attr
);
1094 goto free_used_maps
;
1096 /* eBPF program is ready to be JITed */
1097 prog
= bpf_prog_select_runtime(prog
, &err
);
1099 goto free_used_maps
;
1101 err
= bpf_prog_alloc_id(prog
);
1103 goto free_used_maps
;
1105 err
= bpf_prog_new_fd(prog
);
1107 /* failed to allocate fd.
1108 * bpf_prog_put() is needed because the above
1109 * bpf_prog_alloc_id() has published the prog
1110 * to the userspace and the userspace may
1111 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1117 bpf_prog_kallsyms_add(prog
);
1118 trace_bpf_prog_load(prog
, err
);
1122 free_used_maps(prog
->aux
);
1124 bpf_prog_uncharge_memlock(prog
);
1125 free_prog_nouncharge
:
1126 bpf_prog_free(prog
);
1130 #define BPF_OBJ_LAST_FIELD bpf_fd
1132 static int bpf_obj_pin(const union bpf_attr
*attr
)
1134 if (CHECK_ATTR(BPF_OBJ
))
1137 return bpf_obj_pin_user(attr
->bpf_fd
, u64_to_user_ptr(attr
->pathname
));
1140 static int bpf_obj_get(const union bpf_attr
*attr
)
1142 if (CHECK_ATTR(BPF_OBJ
) || attr
->bpf_fd
!= 0)
1145 return bpf_obj_get_user(u64_to_user_ptr(attr
->pathname
));
1148 #ifdef CONFIG_CGROUP_BPF
1150 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1152 static int sockmap_get_from_fd(const union bpf_attr
*attr
, bool attach
)
1154 struct bpf_prog
*prog
= NULL
;
1155 int ufd
= attr
->target_fd
;
1156 struct bpf_map
*map
;
1161 map
= __bpf_map_get(f
);
1163 return PTR_ERR(map
);
1166 prog
= bpf_prog_get_type(attr
->attach_bpf_fd
,
1167 BPF_PROG_TYPE_SK_SKB
);
1170 return PTR_ERR(prog
);
1174 err
= sock_map_prog(map
, prog
, attr
->attach_type
);
1186 #define BPF_F_ATTACH_MASK \
1187 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1189 static int bpf_prog_attach(const union bpf_attr
*attr
)
1191 enum bpf_prog_type ptype
;
1192 struct bpf_prog
*prog
;
1193 struct cgroup
*cgrp
;
1196 if (!capable(CAP_NET_ADMIN
))
1199 if (CHECK_ATTR(BPF_PROG_ATTACH
))
1202 if (attr
->attach_flags
& ~BPF_F_ATTACH_MASK
)
1205 switch (attr
->attach_type
) {
1206 case BPF_CGROUP_INET_INGRESS
:
1207 case BPF_CGROUP_INET_EGRESS
:
1208 ptype
= BPF_PROG_TYPE_CGROUP_SKB
;
1210 case BPF_CGROUP_INET_SOCK_CREATE
:
1211 ptype
= BPF_PROG_TYPE_CGROUP_SOCK
;
1213 case BPF_CGROUP_SOCK_OPS
:
1214 ptype
= BPF_PROG_TYPE_SOCK_OPS
;
1216 case BPF_SK_SKB_STREAM_PARSER
:
1217 case BPF_SK_SKB_STREAM_VERDICT
:
1218 return sockmap_get_from_fd(attr
, true);
1223 prog
= bpf_prog_get_type(attr
->attach_bpf_fd
, ptype
);
1225 return PTR_ERR(prog
);
1227 cgrp
= cgroup_get_from_fd(attr
->target_fd
);
1230 return PTR_ERR(cgrp
);
1233 ret
= cgroup_bpf_attach(cgrp
, prog
, attr
->attach_type
,
1234 attr
->attach_flags
);
1242 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1244 static int bpf_prog_detach(const union bpf_attr
*attr
)
1246 enum bpf_prog_type ptype
;
1247 struct bpf_prog
*prog
;
1248 struct cgroup
*cgrp
;
1251 if (!capable(CAP_NET_ADMIN
))
1254 if (CHECK_ATTR(BPF_PROG_DETACH
))
1257 switch (attr
->attach_type
) {
1258 case BPF_CGROUP_INET_INGRESS
:
1259 case BPF_CGROUP_INET_EGRESS
:
1260 ptype
= BPF_PROG_TYPE_CGROUP_SKB
;
1262 case BPF_CGROUP_INET_SOCK_CREATE
:
1263 ptype
= BPF_PROG_TYPE_CGROUP_SOCK
;
1265 case BPF_CGROUP_SOCK_OPS
:
1266 ptype
= BPF_PROG_TYPE_SOCK_OPS
;
1268 case BPF_SK_SKB_STREAM_PARSER
:
1269 case BPF_SK_SKB_STREAM_VERDICT
:
1270 return sockmap_get_from_fd(attr
, false);
1275 cgrp
= cgroup_get_from_fd(attr
->target_fd
);
1277 return PTR_ERR(cgrp
);
1279 prog
= bpf_prog_get_type(attr
->attach_bpf_fd
, ptype
);
1283 ret
= cgroup_bpf_detach(cgrp
, prog
, attr
->attach_type
, 0);
1290 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1292 static int bpf_prog_query(const union bpf_attr
*attr
,
1293 union bpf_attr __user
*uattr
)
1295 struct cgroup
*cgrp
;
1298 if (!capable(CAP_NET_ADMIN
))
1300 if (CHECK_ATTR(BPF_PROG_QUERY
))
1302 if (attr
->query
.query_flags
& ~BPF_F_QUERY_EFFECTIVE
)
1305 switch (attr
->query
.attach_type
) {
1306 case BPF_CGROUP_INET_INGRESS
:
1307 case BPF_CGROUP_INET_EGRESS
:
1308 case BPF_CGROUP_INET_SOCK_CREATE
:
1309 case BPF_CGROUP_SOCK_OPS
:
1314 cgrp
= cgroup_get_from_fd(attr
->query
.target_fd
);
1316 return PTR_ERR(cgrp
);
1317 ret
= cgroup_bpf_query(cgrp
, attr
, uattr
);
1321 #endif /* CONFIG_CGROUP_BPF */
1323 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1325 static int bpf_prog_test_run(const union bpf_attr
*attr
,
1326 union bpf_attr __user
*uattr
)
1328 struct bpf_prog
*prog
;
1329 int ret
= -ENOTSUPP
;
1331 if (CHECK_ATTR(BPF_PROG_TEST_RUN
))
1334 prog
= bpf_prog_get(attr
->test
.prog_fd
);
1336 return PTR_ERR(prog
);
1338 if (prog
->aux
->ops
->test_run
)
1339 ret
= prog
->aux
->ops
->test_run(prog
, attr
, uattr
);
1345 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1347 static int bpf_obj_get_next_id(const union bpf_attr
*attr
,
1348 union bpf_attr __user
*uattr
,
1352 u32 next_id
= attr
->start_id
;
1355 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID
) || next_id
>= INT_MAX
)
1358 if (!capable(CAP_SYS_ADMIN
))
1363 if (!idr_get_next(idr
, &next_id
))
1365 spin_unlock_bh(lock
);
1368 err
= put_user(next_id
, &uattr
->next_id
);
1373 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1375 static int bpf_prog_get_fd_by_id(const union bpf_attr
*attr
)
1377 struct bpf_prog
*prog
;
1378 u32 id
= attr
->prog_id
;
1381 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID
))
1384 if (!capable(CAP_SYS_ADMIN
))
1387 spin_lock_bh(&prog_idr_lock
);
1388 prog
= idr_find(&prog_idr
, id
);
1390 prog
= bpf_prog_inc_not_zero(prog
);
1392 prog
= ERR_PTR(-ENOENT
);
1393 spin_unlock_bh(&prog_idr_lock
);
1396 return PTR_ERR(prog
);
1398 fd
= bpf_prog_new_fd(prog
);
1405 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD map_id
1407 static int bpf_map_get_fd_by_id(const union bpf_attr
*attr
)
1409 struct bpf_map
*map
;
1410 u32 id
= attr
->map_id
;
1413 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID
))
1416 if (!capable(CAP_SYS_ADMIN
))
1419 spin_lock_bh(&map_idr_lock
);
1420 map
= idr_find(&map_idr
, id
);
1422 map
= bpf_map_inc_not_zero(map
, true);
1424 map
= ERR_PTR(-ENOENT
);
1425 spin_unlock_bh(&map_idr_lock
);
1428 return PTR_ERR(map
);
1430 fd
= bpf_map_new_fd(map
);
1437 static int bpf_prog_get_info_by_fd(struct bpf_prog
*prog
,
1438 const union bpf_attr
*attr
,
1439 union bpf_attr __user
*uattr
)
1441 struct bpf_prog_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
1442 struct bpf_prog_info info
= {};
1443 u32 info_len
= attr
->info
.info_len
;
1444 char __user
*uinsns
;
1448 err
= check_uarg_tail_zero(uinfo
, sizeof(info
), info_len
);
1451 info_len
= min_t(u32
, sizeof(info
), info_len
);
1453 if (copy_from_user(&info
, uinfo
, info_len
))
1456 info
.type
= prog
->type
;
1457 info
.id
= prog
->aux
->id
;
1458 info
.load_time
= prog
->aux
->load_time
;
1459 info
.created_by_uid
= from_kuid_munged(current_user_ns(),
1460 prog
->aux
->user
->uid
);
1462 memcpy(info
.tag
, prog
->tag
, sizeof(prog
->tag
));
1463 memcpy(info
.name
, prog
->aux
->name
, sizeof(prog
->aux
->name
));
1465 ulen
= info
.nr_map_ids
;
1466 info
.nr_map_ids
= prog
->aux
->used_map_cnt
;
1467 ulen
= min_t(u32
, info
.nr_map_ids
, ulen
);
1469 u32 __user
*user_map_ids
= u64_to_user_ptr(info
.map_ids
);
1472 for (i
= 0; i
< ulen
; i
++)
1473 if (put_user(prog
->aux
->used_maps
[i
]->id
,
1478 if (!capable(CAP_SYS_ADMIN
)) {
1479 info
.jited_prog_len
= 0;
1480 info
.xlated_prog_len
= 0;
1484 ulen
= info
.jited_prog_len
;
1485 info
.jited_prog_len
= prog
->jited_len
;
1486 if (info
.jited_prog_len
&& ulen
) {
1487 uinsns
= u64_to_user_ptr(info
.jited_prog_insns
);
1488 ulen
= min_t(u32
, info
.jited_prog_len
, ulen
);
1489 if (copy_to_user(uinsns
, prog
->bpf_func
, ulen
))
1493 ulen
= info
.xlated_prog_len
;
1494 info
.xlated_prog_len
= bpf_prog_insn_size(prog
);
1495 if (info
.xlated_prog_len
&& ulen
) {
1496 uinsns
= u64_to_user_ptr(info
.xlated_prog_insns
);
1497 ulen
= min_t(u32
, info
.xlated_prog_len
, ulen
);
1498 if (copy_to_user(uinsns
, prog
->insnsi
, ulen
))
1503 if (copy_to_user(uinfo
, &info
, info_len
) ||
1504 put_user(info_len
, &uattr
->info
.info_len
))
1510 static int bpf_map_get_info_by_fd(struct bpf_map
*map
,
1511 const union bpf_attr
*attr
,
1512 union bpf_attr __user
*uattr
)
1514 struct bpf_map_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
1515 struct bpf_map_info info
= {};
1516 u32 info_len
= attr
->info
.info_len
;
1519 err
= check_uarg_tail_zero(uinfo
, sizeof(info
), info_len
);
1522 info_len
= min_t(u32
, sizeof(info
), info_len
);
1524 info
.type
= map
->map_type
;
1526 info
.key_size
= map
->key_size
;
1527 info
.value_size
= map
->value_size
;
1528 info
.max_entries
= map
->max_entries
;
1529 info
.map_flags
= map
->map_flags
;
1530 memcpy(info
.name
, map
->name
, sizeof(map
->name
));
1532 if (copy_to_user(uinfo
, &info
, info_len
) ||
1533 put_user(info_len
, &uattr
->info
.info_len
))
1539 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1541 static int bpf_obj_get_info_by_fd(const union bpf_attr
*attr
,
1542 union bpf_attr __user
*uattr
)
1544 int ufd
= attr
->info
.bpf_fd
;
1548 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD
))
1555 if (f
.file
->f_op
== &bpf_prog_fops
)
1556 err
= bpf_prog_get_info_by_fd(f
.file
->private_data
, attr
,
1558 else if (f
.file
->f_op
== &bpf_map_fops
)
1559 err
= bpf_map_get_info_by_fd(f
.file
->private_data
, attr
,
1568 SYSCALL_DEFINE3(bpf
, int, cmd
, union bpf_attr __user
*, uattr
, unsigned int, size
)
1570 union bpf_attr attr
= {};
1573 if (!capable(CAP_SYS_ADMIN
) && sysctl_unprivileged_bpf_disabled
)
1576 err
= check_uarg_tail_zero(uattr
, sizeof(attr
), size
);
1579 size
= min_t(u32
, size
, sizeof(attr
));
1581 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1582 if (copy_from_user(&attr
, uattr
, size
) != 0)
1586 case BPF_MAP_CREATE
:
1587 err
= map_create(&attr
);
1589 case BPF_MAP_LOOKUP_ELEM
:
1590 err
= map_lookup_elem(&attr
);
1592 case BPF_MAP_UPDATE_ELEM
:
1593 err
= map_update_elem(&attr
);
1595 case BPF_MAP_DELETE_ELEM
:
1596 err
= map_delete_elem(&attr
);
1598 case BPF_MAP_GET_NEXT_KEY
:
1599 err
= map_get_next_key(&attr
);
1602 err
= bpf_prog_load(&attr
);
1605 err
= bpf_obj_pin(&attr
);
1608 err
= bpf_obj_get(&attr
);
1610 #ifdef CONFIG_CGROUP_BPF
1611 case BPF_PROG_ATTACH
:
1612 err
= bpf_prog_attach(&attr
);
1614 case BPF_PROG_DETACH
:
1615 err
= bpf_prog_detach(&attr
);
1617 case BPF_PROG_QUERY
:
1618 err
= bpf_prog_query(&attr
, uattr
);
1621 case BPF_PROG_TEST_RUN
:
1622 err
= bpf_prog_test_run(&attr
, uattr
);
1624 case BPF_PROG_GET_NEXT_ID
:
1625 err
= bpf_obj_get_next_id(&attr
, uattr
,
1626 &prog_idr
, &prog_idr_lock
);
1628 case BPF_MAP_GET_NEXT_ID
:
1629 err
= bpf_obj_get_next_id(&attr
, uattr
,
1630 &map_idr
, &map_idr_lock
);
1632 case BPF_PROG_GET_FD_BY_ID
:
1633 err
= bpf_prog_get_fd_by_id(&attr
);
1635 case BPF_MAP_GET_FD_BY_ID
:
1636 err
= bpf_map_get_fd_by_id(&attr
);
1638 case BPF_OBJ_GET_INFO_BY_FD
:
1639 err
= bpf_obj_get_info_by_fd(&attr
, uattr
);