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/syscalls.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/mmzone.h>
17 #include <linux/anon_inodes.h>
18 #include <linux/file.h>
19 #include <linux/license.h>
20 #include <linux/filter.h>
21 #include <linux/version.h>
22 #include <linux/kernel.h>
24 DEFINE_PER_CPU(int, bpf_prog_active
);
26 int sysctl_unprivileged_bpf_disabled __read_mostly
;
28 static LIST_HEAD(bpf_map_types
);
30 static struct bpf_map
*find_and_alloc_map(union bpf_attr
*attr
)
32 struct bpf_map_type_list
*tl
;
35 list_for_each_entry(tl
, &bpf_map_types
, list_node
) {
36 if (tl
->type
== attr
->map_type
) {
37 map
= tl
->ops
->map_alloc(attr
);
41 map
->map_type
= attr
->map_type
;
45 return ERR_PTR(-EINVAL
);
48 /* boot time registration of different map implementations */
49 void bpf_register_map_type(struct bpf_map_type_list
*tl
)
51 list_add(&tl
->list_node
, &bpf_map_types
);
54 void *bpf_map_area_alloc(size_t size
)
56 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
57 * trigger under memory pressure as we really just want to
60 const gfp_t flags
= __GFP_NOWARN
| __GFP_NORETRY
| __GFP_ZERO
;
63 if (size
<= (PAGE_SIZE
<< PAGE_ALLOC_COSTLY_ORDER
)) {
64 area
= kmalloc(size
, GFP_USER
| flags
);
69 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
| flags
,
73 void bpf_map_area_free(void *area
)
78 int bpf_map_precharge_memlock(u32 pages
)
80 struct user_struct
*user
= get_current_user();
81 unsigned long memlock_limit
, cur
;
83 memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
84 cur
= atomic_long_read(&user
->locked_vm
);
86 if (cur
+ pages
> memlock_limit
)
91 static int bpf_map_charge_memlock(struct bpf_map
*map
)
93 struct user_struct
*user
= get_current_user();
94 unsigned long memlock_limit
;
96 memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
98 atomic_long_add(map
->pages
, &user
->locked_vm
);
100 if (atomic_long_read(&user
->locked_vm
) > memlock_limit
) {
101 atomic_long_sub(map
->pages
, &user
->locked_vm
);
109 static void bpf_map_uncharge_memlock(struct bpf_map
*map
)
111 struct user_struct
*user
= map
->user
;
113 atomic_long_sub(map
->pages
, &user
->locked_vm
);
117 /* called from workqueue */
118 static void bpf_map_free_deferred(struct work_struct
*work
)
120 struct bpf_map
*map
= container_of(work
, struct bpf_map
, work
);
122 bpf_map_uncharge_memlock(map
);
123 /* implementation dependent freeing */
124 map
->ops
->map_free(map
);
127 static void bpf_map_put_uref(struct bpf_map
*map
)
129 if (atomic_dec_and_test(&map
->usercnt
)) {
130 if (map
->map_type
== BPF_MAP_TYPE_PROG_ARRAY
)
131 bpf_fd_array_map_clear(map
);
135 /* decrement map refcnt and schedule it for freeing via workqueue
136 * (unrelying map implementation ops->map_free() might sleep)
138 void bpf_map_put(struct bpf_map
*map
)
140 if (atomic_dec_and_test(&map
->refcnt
)) {
141 INIT_WORK(&map
->work
, bpf_map_free_deferred
);
142 schedule_work(&map
->work
);
146 void bpf_map_put_with_uref(struct bpf_map
*map
)
148 bpf_map_put_uref(map
);
152 static int bpf_map_release(struct inode
*inode
, struct file
*filp
)
154 struct bpf_map
*map
= filp
->private_data
;
156 if (map
->ops
->map_release
)
157 map
->ops
->map_release(map
, filp
);
159 bpf_map_put_with_uref(map
);
163 #ifdef CONFIG_PROC_FS
164 static void bpf_map_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
166 const struct bpf_map
*map
= filp
->private_data
;
167 const struct bpf_array
*array
;
168 u32 owner_prog_type
= 0;
170 if (map
->map_type
== BPF_MAP_TYPE_PROG_ARRAY
) {
171 array
= container_of(map
, struct bpf_array
, map
);
172 owner_prog_type
= array
->owner_prog_type
;
187 map
->pages
* 1ULL << PAGE_SHIFT
);
190 seq_printf(m
, "owner_prog_type:\t%u\n",
195 static const struct file_operations bpf_map_fops
= {
196 #ifdef CONFIG_PROC_FS
197 .show_fdinfo
= bpf_map_show_fdinfo
,
199 .release
= bpf_map_release
,
202 int bpf_map_new_fd(struct bpf_map
*map
)
204 return anon_inode_getfd("bpf-map", &bpf_map_fops
, map
,
208 /* helper macro to check that unused fields 'union bpf_attr' are zero */
209 #define CHECK_ATTR(CMD) \
210 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
211 sizeof(attr->CMD##_LAST_FIELD), 0, \
213 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
214 sizeof(attr->CMD##_LAST_FIELD)) != NULL
216 #define BPF_MAP_CREATE_LAST_FIELD map_flags
217 /* called via syscall */
218 static int map_create(union bpf_attr
*attr
)
223 err
= CHECK_ATTR(BPF_MAP_CREATE
);
227 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
228 map
= find_and_alloc_map(attr
);
232 atomic_set(&map
->refcnt
, 1);
233 atomic_set(&map
->usercnt
, 1);
235 err
= bpf_map_charge_memlock(map
);
237 goto free_map_nouncharge
;
239 err
= bpf_map_new_fd(map
);
241 /* failed to allocate fd */
247 bpf_map_uncharge_memlock(map
);
249 map
->ops
->map_free(map
);
253 /* if error is returned, fd is released.
254 * On success caller should complete fd access with matching fdput()
256 struct bpf_map
*__bpf_map_get(struct fd f
)
259 return ERR_PTR(-EBADF
);
260 if (f
.file
->f_op
!= &bpf_map_fops
) {
262 return ERR_PTR(-EINVAL
);
265 return f
.file
->private_data
;
268 /* prog's and map's refcnt limit */
269 #define BPF_MAX_REFCNT 32768
271 struct bpf_map
*bpf_map_inc(struct bpf_map
*map
, bool uref
)
273 if (atomic_inc_return(&map
->refcnt
) > BPF_MAX_REFCNT
) {
274 atomic_dec(&map
->refcnt
);
275 return ERR_PTR(-EBUSY
);
278 atomic_inc(&map
->usercnt
);
282 struct bpf_map
*bpf_map_get_with_uref(u32 ufd
)
284 struct fd f
= fdget(ufd
);
287 map
= __bpf_map_get(f
);
291 map
= bpf_map_inc(map
, true);
297 int __weak
bpf_stackmap_copy(struct bpf_map
*map
, void *key
, void *value
)
302 /* last field in 'union bpf_attr' used by this command */
303 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
305 static int map_lookup_elem(union bpf_attr
*attr
)
307 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
308 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
309 int ufd
= attr
->map_fd
;
311 void *key
, *value
, *ptr
;
316 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM
))
320 map
= __bpf_map_get(f
);
325 key
= kmalloc(map
->key_size
, GFP_USER
);
330 if (copy_from_user(key
, ukey
, map
->key_size
) != 0)
333 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
334 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
335 map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
)
336 value_size
= round_up(map
->value_size
, 8) * num_possible_cpus();
338 value_size
= map
->value_size
;
341 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
345 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
346 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
) {
347 err
= bpf_percpu_hash_copy(map
, key
, value
);
348 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
) {
349 err
= bpf_percpu_array_copy(map
, key
, value
);
350 } else if (map
->map_type
== BPF_MAP_TYPE_STACK_TRACE
) {
351 err
= bpf_stackmap_copy(map
, key
, value
);
354 ptr
= map
->ops
->map_lookup_elem(map
, key
);
356 memcpy(value
, ptr
, value_size
);
358 err
= ptr
? 0 : -ENOENT
;
365 if (copy_to_user(uvalue
, value
, value_size
) != 0)
379 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
381 static int map_update_elem(union bpf_attr
*attr
)
383 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
384 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
385 int ufd
= attr
->map_fd
;
392 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM
))
396 map
= __bpf_map_get(f
);
401 key
= kmalloc(map
->key_size
, GFP_USER
);
406 if (copy_from_user(key
, ukey
, map
->key_size
) != 0)
409 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
410 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
411 map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
)
412 value_size
= round_up(map
->value_size
, 8) * num_possible_cpus();
414 value_size
= map
->value_size
;
417 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
422 if (copy_from_user(value
, uvalue
, value_size
) != 0)
425 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
426 * inside bpf map update or delete otherwise deadlocks are possible
429 __this_cpu_inc(bpf_prog_active
);
430 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
431 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
) {
432 err
= bpf_percpu_hash_update(map
, key
, value
, attr
->flags
);
433 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
) {
434 err
= bpf_percpu_array_update(map
, key
, value
, attr
->flags
);
435 } else if (map
->map_type
== BPF_MAP_TYPE_PERF_EVENT_ARRAY
||
436 map
->map_type
== BPF_MAP_TYPE_PROG_ARRAY
||
437 map
->map_type
== BPF_MAP_TYPE_CGROUP_ARRAY
) {
439 err
= bpf_fd_array_map_update_elem(map
, f
.file
, key
, value
,
444 err
= map
->ops
->map_update_elem(map
, key
, value
, attr
->flags
);
447 __this_cpu_dec(bpf_prog_active
);
459 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
461 static int map_delete_elem(union bpf_attr
*attr
)
463 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
464 int ufd
= attr
->map_fd
;
470 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM
))
474 map
= __bpf_map_get(f
);
479 key
= kmalloc(map
->key_size
, GFP_USER
);
484 if (copy_from_user(key
, ukey
, map
->key_size
) != 0)
488 __this_cpu_inc(bpf_prog_active
);
490 err
= map
->ops
->map_delete_elem(map
, key
);
492 __this_cpu_dec(bpf_prog_active
);
502 /* last field in 'union bpf_attr' used by this command */
503 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
505 static int map_get_next_key(union bpf_attr
*attr
)
507 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
508 void __user
*unext_key
= u64_to_user_ptr(attr
->next_key
);
509 int ufd
= attr
->map_fd
;
511 void *key
, *next_key
;
515 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY
))
519 map
= __bpf_map_get(f
);
524 key
= kmalloc(map
->key_size
, GFP_USER
);
529 if (copy_from_user(key
, ukey
, map
->key_size
) != 0)
533 next_key
= kmalloc(map
->key_size
, GFP_USER
);
538 err
= map
->ops
->map_get_next_key(map
, key
, next_key
);
544 if (copy_to_user(unext_key
, next_key
, map
->key_size
) != 0)
558 static LIST_HEAD(bpf_prog_types
);
560 static int find_prog_type(enum bpf_prog_type type
, struct bpf_prog
*prog
)
562 struct bpf_prog_type_list
*tl
;
564 list_for_each_entry(tl
, &bpf_prog_types
, list_node
) {
565 if (tl
->type
== type
) {
566 prog
->aux
->ops
= tl
->ops
;
575 void bpf_register_prog_type(struct bpf_prog_type_list
*tl
)
577 list_add(&tl
->list_node
, &bpf_prog_types
);
580 /* fixup insn->imm field of bpf_call instructions:
581 * if (insn->imm == BPF_FUNC_map_lookup_elem)
582 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
583 * else if (insn->imm == BPF_FUNC_map_update_elem)
584 * insn->imm = bpf_map_update_elem - __bpf_call_base;
587 * this function is called after eBPF program passed verification
589 static void fixup_bpf_calls(struct bpf_prog
*prog
)
591 const struct bpf_func_proto
*fn
;
594 for (i
= 0; i
< prog
->len
; i
++) {
595 struct bpf_insn
*insn
= &prog
->insnsi
[i
];
597 if (insn
->code
== (BPF_JMP
| BPF_CALL
)) {
598 /* we reach here when program has bpf_call instructions
599 * and it passed bpf_check(), means that
600 * ops->get_func_proto must have been supplied, check it
602 BUG_ON(!prog
->aux
->ops
->get_func_proto
);
604 if (insn
->imm
== BPF_FUNC_get_route_realm
)
605 prog
->dst_needed
= 1;
606 if (insn
->imm
== BPF_FUNC_get_prandom_u32
)
607 bpf_user_rnd_init_once();
608 if (insn
->imm
== BPF_FUNC_xdp_adjust_head
)
609 prog
->xdp_adjust_head
= 1;
610 if (insn
->imm
== BPF_FUNC_tail_call
) {
611 /* mark bpf_tail_call as different opcode
612 * to avoid conditional branch in
613 * interpeter for every normal call
614 * and to prevent accidental JITing by
615 * JIT compiler that doesn't support
623 fn
= prog
->aux
->ops
->get_func_proto(insn
->imm
);
624 /* all functions that have prototype and verifier allowed
625 * programs to call them, must be real in-kernel functions
628 insn
->imm
= fn
->func
- __bpf_call_base
;
633 /* drop refcnt on maps used by eBPF program and free auxilary data */
634 static void free_used_maps(struct bpf_prog_aux
*aux
)
638 for (i
= 0; i
< aux
->used_map_cnt
; i
++)
639 bpf_map_put(aux
->used_maps
[i
]);
641 kfree(aux
->used_maps
);
644 int __bpf_prog_charge(struct user_struct
*user
, u32 pages
)
646 unsigned long memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
647 unsigned long user_bufs
;
650 user_bufs
= atomic_long_add_return(pages
, &user
->locked_vm
);
651 if (user_bufs
> memlock_limit
) {
652 atomic_long_sub(pages
, &user
->locked_vm
);
660 void __bpf_prog_uncharge(struct user_struct
*user
, u32 pages
)
663 atomic_long_sub(pages
, &user
->locked_vm
);
666 static int bpf_prog_charge_memlock(struct bpf_prog
*prog
)
668 struct user_struct
*user
= get_current_user();
671 ret
= __bpf_prog_charge(user
, prog
->pages
);
677 prog
->aux
->user
= user
;
681 static void bpf_prog_uncharge_memlock(struct bpf_prog
*prog
)
683 struct user_struct
*user
= prog
->aux
->user
;
685 __bpf_prog_uncharge(user
, prog
->pages
);
689 static void __bpf_prog_put_rcu(struct rcu_head
*rcu
)
691 struct bpf_prog_aux
*aux
= container_of(rcu
, struct bpf_prog_aux
, rcu
);
694 bpf_prog_uncharge_memlock(aux
->prog
);
695 bpf_prog_free(aux
->prog
);
698 void bpf_prog_put(struct bpf_prog
*prog
)
700 if (atomic_dec_and_test(&prog
->aux
->refcnt
))
701 call_rcu(&prog
->aux
->rcu
, __bpf_prog_put_rcu
);
703 EXPORT_SYMBOL_GPL(bpf_prog_put
);
705 static int bpf_prog_release(struct inode
*inode
, struct file
*filp
)
707 struct bpf_prog
*prog
= filp
->private_data
;
713 #ifdef CONFIG_PROC_FS
714 static void bpf_prog_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
716 const struct bpf_prog
*prog
= filp
->private_data
;
717 char prog_tag
[sizeof(prog
->tag
) * 2 + 1] = { };
719 bin2hex(prog_tag
, prog
->tag
, sizeof(prog
->tag
));
728 prog
->pages
* 1ULL << PAGE_SHIFT
);
732 static const struct file_operations bpf_prog_fops
= {
733 #ifdef CONFIG_PROC_FS
734 .show_fdinfo
= bpf_prog_show_fdinfo
,
736 .release
= bpf_prog_release
,
739 int bpf_prog_new_fd(struct bpf_prog
*prog
)
741 return anon_inode_getfd("bpf-prog", &bpf_prog_fops
, prog
,
745 static struct bpf_prog
*____bpf_prog_get(struct fd f
)
748 return ERR_PTR(-EBADF
);
749 if (f
.file
->f_op
!= &bpf_prog_fops
) {
751 return ERR_PTR(-EINVAL
);
754 return f
.file
->private_data
;
757 struct bpf_prog
*bpf_prog_add(struct bpf_prog
*prog
, int i
)
759 if (atomic_add_return(i
, &prog
->aux
->refcnt
) > BPF_MAX_REFCNT
) {
760 atomic_sub(i
, &prog
->aux
->refcnt
);
761 return ERR_PTR(-EBUSY
);
765 EXPORT_SYMBOL_GPL(bpf_prog_add
);
767 void bpf_prog_sub(struct bpf_prog
*prog
, int i
)
769 /* Only to be used for undoing previous bpf_prog_add() in some
770 * error path. We still know that another entity in our call
771 * path holds a reference to the program, thus atomic_sub() can
772 * be safely used in such cases!
774 WARN_ON(atomic_sub_return(i
, &prog
->aux
->refcnt
) == 0);
776 EXPORT_SYMBOL_GPL(bpf_prog_sub
);
778 struct bpf_prog
*bpf_prog_inc(struct bpf_prog
*prog
)
780 return bpf_prog_add(prog
, 1);
782 EXPORT_SYMBOL_GPL(bpf_prog_inc
);
784 static struct bpf_prog
*__bpf_prog_get(u32 ufd
, enum bpf_prog_type
*type
)
786 struct fd f
= fdget(ufd
);
787 struct bpf_prog
*prog
;
789 prog
= ____bpf_prog_get(f
);
792 if (type
&& prog
->type
!= *type
) {
793 prog
= ERR_PTR(-EINVAL
);
797 prog
= bpf_prog_inc(prog
);
803 struct bpf_prog
*bpf_prog_get(u32 ufd
)
805 return __bpf_prog_get(ufd
, NULL
);
808 struct bpf_prog
*bpf_prog_get_type(u32 ufd
, enum bpf_prog_type type
)
810 return __bpf_prog_get(ufd
, &type
);
812 EXPORT_SYMBOL_GPL(bpf_prog_get_type
);
814 /* last field in 'union bpf_attr' used by this command */
815 #define BPF_PROG_LOAD_LAST_FIELD kern_version
817 static int bpf_prog_load(union bpf_attr
*attr
)
819 enum bpf_prog_type type
= attr
->prog_type
;
820 struct bpf_prog
*prog
;
825 if (CHECK_ATTR(BPF_PROG_LOAD
))
828 /* copy eBPF program license from user space */
829 if (strncpy_from_user(license
, u64_to_user_ptr(attr
->license
),
830 sizeof(license
) - 1) < 0)
832 license
[sizeof(license
) - 1] = 0;
834 /* eBPF programs must be GPL compatible to use GPL-ed functions */
835 is_gpl
= license_is_gpl_compatible(license
);
837 if (attr
->insn_cnt
== 0 || attr
->insn_cnt
> BPF_MAXINSNS
)
840 if (type
== BPF_PROG_TYPE_KPROBE
&&
841 attr
->kern_version
!= LINUX_VERSION_CODE
)
844 if (type
!= BPF_PROG_TYPE_SOCKET_FILTER
&& !capable(CAP_SYS_ADMIN
))
847 /* plain bpf_prog allocation */
848 prog
= bpf_prog_alloc(bpf_prog_size(attr
->insn_cnt
), GFP_USER
);
852 err
= bpf_prog_charge_memlock(prog
);
854 goto free_prog_nouncharge
;
856 prog
->len
= attr
->insn_cnt
;
859 if (copy_from_user(prog
->insns
, u64_to_user_ptr(attr
->insns
),
860 bpf_prog_insn_size(prog
)) != 0)
863 prog
->orig_prog
= NULL
;
866 atomic_set(&prog
->aux
->refcnt
, 1);
867 prog
->gpl_compatible
= is_gpl
? 1 : 0;
869 /* find program type: socket_filter vs tracing_filter */
870 err
= find_prog_type(type
, prog
);
874 /* run eBPF verifier */
875 err
= bpf_check(&prog
, attr
);
879 /* fixup BPF_CALL->imm field */
880 fixup_bpf_calls(prog
);
882 /* eBPF program is ready to be JITed */
883 prog
= bpf_prog_select_runtime(prog
, &err
);
887 err
= bpf_prog_new_fd(prog
);
889 /* failed to allocate fd */
895 free_used_maps(prog
->aux
);
897 bpf_prog_uncharge_memlock(prog
);
898 free_prog_nouncharge
:
903 #define BPF_OBJ_LAST_FIELD bpf_fd
905 static int bpf_obj_pin(const union bpf_attr
*attr
)
907 if (CHECK_ATTR(BPF_OBJ
))
910 return bpf_obj_pin_user(attr
->bpf_fd
, u64_to_user_ptr(attr
->pathname
));
913 static int bpf_obj_get(const union bpf_attr
*attr
)
915 if (CHECK_ATTR(BPF_OBJ
) || attr
->bpf_fd
!= 0)
918 return bpf_obj_get_user(u64_to_user_ptr(attr
->pathname
));
921 #ifdef CONFIG_CGROUP_BPF
923 #define BPF_PROG_ATTACH_LAST_FIELD attach_type
925 static int bpf_prog_attach(const union bpf_attr
*attr
)
927 struct bpf_prog
*prog
;
929 enum bpf_prog_type ptype
;
931 if (!capable(CAP_NET_ADMIN
))
934 if (CHECK_ATTR(BPF_PROG_ATTACH
))
937 switch (attr
->attach_type
) {
938 case BPF_CGROUP_INET_INGRESS
:
939 case BPF_CGROUP_INET_EGRESS
:
940 ptype
= BPF_PROG_TYPE_CGROUP_SKB
;
942 case BPF_CGROUP_INET_SOCK_CREATE
:
943 ptype
= BPF_PROG_TYPE_CGROUP_SOCK
;
949 prog
= bpf_prog_get_type(attr
->attach_bpf_fd
, ptype
);
951 return PTR_ERR(prog
);
953 cgrp
= cgroup_get_from_fd(attr
->target_fd
);
956 return PTR_ERR(cgrp
);
959 cgroup_bpf_update(cgrp
, prog
, attr
->attach_type
);
965 #define BPF_PROG_DETACH_LAST_FIELD attach_type
967 static int bpf_prog_detach(const union bpf_attr
*attr
)
971 if (!capable(CAP_NET_ADMIN
))
974 if (CHECK_ATTR(BPF_PROG_DETACH
))
977 switch (attr
->attach_type
) {
978 case BPF_CGROUP_INET_INGRESS
:
979 case BPF_CGROUP_INET_EGRESS
:
980 case BPF_CGROUP_INET_SOCK_CREATE
:
981 cgrp
= cgroup_get_from_fd(attr
->target_fd
);
983 return PTR_ERR(cgrp
);
985 cgroup_bpf_update(cgrp
, NULL
, attr
->attach_type
);
995 #endif /* CONFIG_CGROUP_BPF */
997 SYSCALL_DEFINE3(bpf
, int, cmd
, union bpf_attr __user
*, uattr
, unsigned int, size
)
999 union bpf_attr attr
= {};
1002 if (!capable(CAP_SYS_ADMIN
) && sysctl_unprivileged_bpf_disabled
)
1005 if (!access_ok(VERIFY_READ
, uattr
, 1))
1008 if (size
> PAGE_SIZE
) /* silly large */
1011 /* If we're handed a bigger struct than we know of,
1012 * ensure all the unknown bits are 0 - i.e. new
1013 * user-space does not rely on any kernel feature
1014 * extensions we dont know about yet.
1016 if (size
> sizeof(attr
)) {
1017 unsigned char __user
*addr
;
1018 unsigned char __user
*end
;
1021 addr
= (void __user
*)uattr
+ sizeof(attr
);
1022 end
= (void __user
*)uattr
+ size
;
1024 for (; addr
< end
; addr
++) {
1025 err
= get_user(val
, addr
);
1031 size
= sizeof(attr
);
1034 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1035 if (copy_from_user(&attr
, uattr
, size
) != 0)
1039 case BPF_MAP_CREATE
:
1040 err
= map_create(&attr
);
1042 case BPF_MAP_LOOKUP_ELEM
:
1043 err
= map_lookup_elem(&attr
);
1045 case BPF_MAP_UPDATE_ELEM
:
1046 err
= map_update_elem(&attr
);
1048 case BPF_MAP_DELETE_ELEM
:
1049 err
= map_delete_elem(&attr
);
1051 case BPF_MAP_GET_NEXT_KEY
:
1052 err
= map_get_next_key(&attr
);
1055 err
= bpf_prog_load(&attr
);
1058 err
= bpf_obj_pin(&attr
);
1061 err
= bpf_obj_get(&attr
);
1064 #ifdef CONFIG_CGROUP_BPF
1065 case BPF_PROG_ATTACH
:
1066 err
= bpf_prog_attach(&attr
);
1068 case BPF_PROG_DETACH
:
1069 err
= bpf_prog_detach(&attr
);