15b497af4SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 299c55f7dSAlexei Starovoitov /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com 399c55f7dSAlexei Starovoitov */ 499c55f7dSAlexei Starovoitov #include <linux/bpf.h> 5a67edbf4SDaniel Borkmann #include <linux/bpf_trace.h> 6f4364dcfSSean Young #include <linux/bpf_lirc.h> 7f56a653cSMartin KaFai Lau #include <linux/btf.h> 899c55f7dSAlexei Starovoitov #include <linux/syscalls.h> 999c55f7dSAlexei Starovoitov #include <linux/slab.h> 103f07c014SIngo Molnar #include <linux/sched/signal.h> 11d407bd25SDaniel Borkmann #include <linux/vmalloc.h> 12d407bd25SDaniel Borkmann #include <linux/mmzone.h> 1399c55f7dSAlexei Starovoitov #include <linux/anon_inodes.h> 1441bdc4b4SYonghong Song #include <linux/fdtable.h> 15db20fd2bSAlexei Starovoitov #include <linux/file.h> 1641bdc4b4SYonghong Song #include <linux/fs.h> 1709756af4SAlexei Starovoitov #include <linux/license.h> 1809756af4SAlexei Starovoitov #include <linux/filter.h> 192541517cSAlexei Starovoitov #include <linux/version.h> 20535e7b4bSMickaël Salaün #include <linux/kernel.h> 21dc4bb0e2SMartin KaFai Lau #include <linux/idr.h> 22cb4d2b3fSMartin KaFai Lau #include <linux/cred.h> 23cb4d2b3fSMartin KaFai Lau #include <linux/timekeeping.h> 24cb4d2b3fSMartin KaFai Lau #include <linux/ctype.h> 259ef09e35SMark Rutland #include <linux/nospec.h> 26bae141f5SDaniel Borkmann #include <linux/audit.h> 27ccfe29ebSAlexei Starovoitov #include <uapi/linux/btf.h> 289e4e01dfSKP Singh #include <linux/bpf_lsm.h> 29457f4436SAndrii Nakryiko #include <linux/poll.h> 30a3fd7ceeSJakub Sitnicki #include <linux/bpf-netns.h> 3199c55f7dSAlexei Starovoitov 32da765a2fSDaniel Borkmann #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ 3314dc6f04SMartin KaFai Lau (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ 3414dc6f04SMartin KaFai Lau (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) 35da765a2fSDaniel Borkmann #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY) 3614dc6f04SMartin KaFai Lau #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) 37da765a2fSDaniel Borkmann #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \ 38da765a2fSDaniel Borkmann IS_FD_HASH(map)) 3914dc6f04SMartin KaFai Lau 406e71b04aSChenbo Feng #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) 416e71b04aSChenbo Feng 42b121d1e7SAlexei Starovoitov DEFINE_PER_CPU(int, bpf_prog_active); 43dc4bb0e2SMartin KaFai Lau static DEFINE_IDR(prog_idr); 44dc4bb0e2SMartin KaFai Lau static DEFINE_SPINLOCK(prog_idr_lock); 45f3f1c054SMartin KaFai Lau static DEFINE_IDR(map_idr); 46f3f1c054SMartin KaFai Lau static DEFINE_SPINLOCK(map_idr_lock); 47a3b80e10SAndrii Nakryiko static DEFINE_IDR(link_idr); 48a3b80e10SAndrii Nakryiko static DEFINE_SPINLOCK(link_idr_lock); 49b121d1e7SAlexei Starovoitov 501be7f75dSAlexei Starovoitov int sysctl_unprivileged_bpf_disabled __read_mostly; 511be7f75dSAlexei Starovoitov 5240077e0cSJohannes Berg static const struct bpf_map_ops * const bpf_map_types[] = { 5391cc1a99SAlexei Starovoitov #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) 5440077e0cSJohannes Berg #define BPF_MAP_TYPE(_id, _ops) \ 5540077e0cSJohannes Berg [_id] = &_ops, 56f2e10bffSAndrii Nakryiko #define BPF_LINK_TYPE(_id, _name) 5740077e0cSJohannes Berg #include <linux/bpf_types.h> 5840077e0cSJohannes Berg #undef BPF_PROG_TYPE 5940077e0cSJohannes Berg #undef BPF_MAP_TYPE 60f2e10bffSAndrii Nakryiko #undef BPF_LINK_TYPE 6140077e0cSJohannes Berg }; 6299c55f7dSAlexei Starovoitov 63752ba56fSMickaël Salaün /* 64752ba56fSMickaël Salaün * If we're handed a bigger struct than we know of, ensure all the unknown bits 65752ba56fSMickaël Salaün * are 0 - i.e. new user-space does not rely on any kernel feature extensions 66752ba56fSMickaël Salaün * we don't know about yet. 67752ba56fSMickaël Salaün * 68752ba56fSMickaël Salaün * There is a ToCToU between this function call and the following 69752ba56fSMickaël Salaün * copy_from_user() call. However, this is not a concern since this function is 70752ba56fSMickaël Salaün * meant to be a future-proofing of bits. 71752ba56fSMickaël Salaün */ 72dcab51f1SMartin KaFai Lau int bpf_check_uarg_tail_zero(void __user *uaddr, 7358291a74SMickaël Salaün size_t expected_size, 7458291a74SMickaël Salaün size_t actual_size) 7558291a74SMickaël Salaün { 7658291a74SMickaël Salaün unsigned char __user *addr; 7758291a74SMickaël Salaün unsigned char __user *end; 7858291a74SMickaël Salaün unsigned char val; 7958291a74SMickaël Salaün int err; 8058291a74SMickaël Salaün 81752ba56fSMickaël Salaün if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ 82752ba56fSMickaël Salaün return -E2BIG; 83752ba56fSMickaël Salaün 8496d4f267SLinus Torvalds if (unlikely(!access_ok(uaddr, actual_size))) 85752ba56fSMickaël Salaün return -EFAULT; 86752ba56fSMickaël Salaün 8758291a74SMickaël Salaün if (actual_size <= expected_size) 8858291a74SMickaël Salaün return 0; 8958291a74SMickaël Salaün 9058291a74SMickaël Salaün addr = uaddr + expected_size; 9158291a74SMickaël Salaün end = uaddr + actual_size; 9258291a74SMickaël Salaün 9358291a74SMickaël Salaün for (; addr < end; addr++) { 9458291a74SMickaël Salaün err = get_user(val, addr); 9558291a74SMickaël Salaün if (err) 9658291a74SMickaël Salaün return err; 9758291a74SMickaël Salaün if (val) 9858291a74SMickaël Salaün return -E2BIG; 9958291a74SMickaël Salaün } 10058291a74SMickaël Salaün 10158291a74SMickaël Salaün return 0; 10258291a74SMickaël Salaün } 10358291a74SMickaël Salaün 104a3884572SJakub Kicinski const struct bpf_map_ops bpf_map_offload_ops = { 105a3884572SJakub Kicinski .map_alloc = bpf_map_offload_map_alloc, 106a3884572SJakub Kicinski .map_free = bpf_map_offload_map_free, 107e8d2bec0SDaniel Borkmann .map_check_btf = map_check_no_btf, 108a3884572SJakub Kicinski }; 109a3884572SJakub Kicinski 11099c55f7dSAlexei Starovoitov static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) 11199c55f7dSAlexei Starovoitov { 1121110f3a9SJakub Kicinski const struct bpf_map_ops *ops; 1139ef09e35SMark Rutland u32 type = attr->map_type; 11499c55f7dSAlexei Starovoitov struct bpf_map *map; 1151110f3a9SJakub Kicinski int err; 11699c55f7dSAlexei Starovoitov 1179ef09e35SMark Rutland if (type >= ARRAY_SIZE(bpf_map_types)) 1181110f3a9SJakub Kicinski return ERR_PTR(-EINVAL); 1199ef09e35SMark Rutland type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types)); 1209ef09e35SMark Rutland ops = bpf_map_types[type]; 1211110f3a9SJakub Kicinski if (!ops) 12240077e0cSJohannes Berg return ERR_PTR(-EINVAL); 12340077e0cSJohannes Berg 1241110f3a9SJakub Kicinski if (ops->map_alloc_check) { 1251110f3a9SJakub Kicinski err = ops->map_alloc_check(attr); 1261110f3a9SJakub Kicinski if (err) 1271110f3a9SJakub Kicinski return ERR_PTR(err); 1281110f3a9SJakub Kicinski } 129a3884572SJakub Kicinski if (attr->map_ifindex) 130a3884572SJakub Kicinski ops = &bpf_map_offload_ops; 1311110f3a9SJakub Kicinski map = ops->map_alloc(attr); 13299c55f7dSAlexei Starovoitov if (IS_ERR(map)) 13399c55f7dSAlexei Starovoitov return map; 1341110f3a9SJakub Kicinski map->ops = ops; 1359ef09e35SMark Rutland map->map_type = type; 13699c55f7dSAlexei Starovoitov return map; 13799c55f7dSAlexei Starovoitov } 13899c55f7dSAlexei Starovoitov 13915c14a3dSBrian Vazquez static u32 bpf_map_value_size(struct bpf_map *map) 14015c14a3dSBrian Vazquez { 14115c14a3dSBrian Vazquez if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 14215c14a3dSBrian Vazquez map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || 14315c14a3dSBrian Vazquez map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || 14415c14a3dSBrian Vazquez map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) 14515c14a3dSBrian Vazquez return round_up(map->value_size, 8) * num_possible_cpus(); 14615c14a3dSBrian Vazquez else if (IS_FD_MAP(map)) 14715c14a3dSBrian Vazquez return sizeof(u32); 14815c14a3dSBrian Vazquez else 14915c14a3dSBrian Vazquez return map->value_size; 15015c14a3dSBrian Vazquez } 15115c14a3dSBrian Vazquez 15215c14a3dSBrian Vazquez static void maybe_wait_bpf_programs(struct bpf_map *map) 15315c14a3dSBrian Vazquez { 15415c14a3dSBrian Vazquez /* Wait for any running BPF programs to complete so that 15515c14a3dSBrian Vazquez * userspace, when we return to it, knows that all programs 15615c14a3dSBrian Vazquez * that could be running use the new map value. 15715c14a3dSBrian Vazquez */ 15815c14a3dSBrian Vazquez if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS || 15915c14a3dSBrian Vazquez map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) 16015c14a3dSBrian Vazquez synchronize_rcu(); 16115c14a3dSBrian Vazquez } 16215c14a3dSBrian Vazquez 16315c14a3dSBrian Vazquez static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key, 16415c14a3dSBrian Vazquez void *value, __u64 flags) 16515c14a3dSBrian Vazquez { 16615c14a3dSBrian Vazquez int err; 16715c14a3dSBrian Vazquez 16815c14a3dSBrian Vazquez /* Need to create a kthread, thus must support schedule */ 16915c14a3dSBrian Vazquez if (bpf_map_is_dev_bound(map)) { 17015c14a3dSBrian Vazquez return bpf_map_offload_update_elem(map, key, value, flags); 17115c14a3dSBrian Vazquez } else if (map->map_type == BPF_MAP_TYPE_CPUMAP || 17215c14a3dSBrian Vazquez map->map_type == BPF_MAP_TYPE_SOCKHASH || 17315c14a3dSBrian Vazquez map->map_type == BPF_MAP_TYPE_SOCKMAP || 17415c14a3dSBrian Vazquez map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { 17515c14a3dSBrian Vazquez return map->ops->map_update_elem(map, key, value, flags); 17615c14a3dSBrian Vazquez } else if (IS_FD_PROG_ARRAY(map)) { 17715c14a3dSBrian Vazquez return bpf_fd_array_map_update_elem(map, f.file, key, value, 17815c14a3dSBrian Vazquez flags); 17915c14a3dSBrian Vazquez } 18015c14a3dSBrian Vazquez 181b6e5dae1SThomas Gleixner bpf_disable_instrumentation(); 18215c14a3dSBrian Vazquez if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 18315c14a3dSBrian Vazquez map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { 18415c14a3dSBrian Vazquez err = bpf_percpu_hash_update(map, key, value, flags); 18515c14a3dSBrian Vazquez } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { 18615c14a3dSBrian Vazquez err = bpf_percpu_array_update(map, key, value, flags); 18715c14a3dSBrian Vazquez } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { 18815c14a3dSBrian Vazquez err = bpf_percpu_cgroup_storage_update(map, key, value, 18915c14a3dSBrian Vazquez flags); 19015c14a3dSBrian Vazquez } else if (IS_FD_ARRAY(map)) { 19115c14a3dSBrian Vazquez rcu_read_lock(); 19215c14a3dSBrian Vazquez err = bpf_fd_array_map_update_elem(map, f.file, key, value, 19315c14a3dSBrian Vazquez flags); 19415c14a3dSBrian Vazquez rcu_read_unlock(); 19515c14a3dSBrian Vazquez } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { 19615c14a3dSBrian Vazquez rcu_read_lock(); 19715c14a3dSBrian Vazquez err = bpf_fd_htab_map_update_elem(map, f.file, key, value, 19815c14a3dSBrian Vazquez flags); 19915c14a3dSBrian Vazquez rcu_read_unlock(); 20015c14a3dSBrian Vazquez } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { 20115c14a3dSBrian Vazquez /* rcu_read_lock() is not needed */ 20215c14a3dSBrian Vazquez err = bpf_fd_reuseport_array_update_elem(map, key, value, 20315c14a3dSBrian Vazquez flags); 20415c14a3dSBrian Vazquez } else if (map->map_type == BPF_MAP_TYPE_QUEUE || 20515c14a3dSBrian Vazquez map->map_type == BPF_MAP_TYPE_STACK) { 20615c14a3dSBrian Vazquez err = map->ops->map_push_elem(map, value, flags); 20715c14a3dSBrian Vazquez } else { 20815c14a3dSBrian Vazquez rcu_read_lock(); 20915c14a3dSBrian Vazquez err = map->ops->map_update_elem(map, key, value, flags); 21015c14a3dSBrian Vazquez rcu_read_unlock(); 21115c14a3dSBrian Vazquez } 212b6e5dae1SThomas Gleixner bpf_enable_instrumentation(); 21315c14a3dSBrian Vazquez maybe_wait_bpf_programs(map); 21415c14a3dSBrian Vazquez 21515c14a3dSBrian Vazquez return err; 21615c14a3dSBrian Vazquez } 21715c14a3dSBrian Vazquez 21815c14a3dSBrian Vazquez static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value, 21915c14a3dSBrian Vazquez __u64 flags) 22015c14a3dSBrian Vazquez { 22115c14a3dSBrian Vazquez void *ptr; 22215c14a3dSBrian Vazquez int err; 22315c14a3dSBrian Vazquez 224cb4d03abSBrian Vazquez if (bpf_map_is_dev_bound(map)) 225cb4d03abSBrian Vazquez return bpf_map_offload_lookup_elem(map, key, value); 22615c14a3dSBrian Vazquez 227b6e5dae1SThomas Gleixner bpf_disable_instrumentation(); 22815c14a3dSBrian Vazquez if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 22915c14a3dSBrian Vazquez map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { 23015c14a3dSBrian Vazquez err = bpf_percpu_hash_copy(map, key, value); 23115c14a3dSBrian Vazquez } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { 23215c14a3dSBrian Vazquez err = bpf_percpu_array_copy(map, key, value); 23315c14a3dSBrian Vazquez } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { 23415c14a3dSBrian Vazquez err = bpf_percpu_cgroup_storage_copy(map, key, value); 23515c14a3dSBrian Vazquez } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { 23615c14a3dSBrian Vazquez err = bpf_stackmap_copy(map, key, value); 23715c14a3dSBrian Vazquez } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) { 23815c14a3dSBrian Vazquez err = bpf_fd_array_map_lookup_elem(map, key, value); 23915c14a3dSBrian Vazquez } else if (IS_FD_HASH(map)) { 24015c14a3dSBrian Vazquez err = bpf_fd_htab_map_lookup_elem(map, key, value); 24115c14a3dSBrian Vazquez } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { 24215c14a3dSBrian Vazquez err = bpf_fd_reuseport_array_lookup_elem(map, key, value); 24315c14a3dSBrian Vazquez } else if (map->map_type == BPF_MAP_TYPE_QUEUE || 24415c14a3dSBrian Vazquez map->map_type == BPF_MAP_TYPE_STACK) { 24515c14a3dSBrian Vazquez err = map->ops->map_peek_elem(map, value); 24615c14a3dSBrian Vazquez } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { 24715c14a3dSBrian Vazquez /* struct_ops map requires directly updating "value" */ 24815c14a3dSBrian Vazquez err = bpf_struct_ops_map_sys_lookup_elem(map, key, value); 24915c14a3dSBrian Vazquez } else { 25015c14a3dSBrian Vazquez rcu_read_lock(); 25115c14a3dSBrian Vazquez if (map->ops->map_lookup_elem_sys_only) 25215c14a3dSBrian Vazquez ptr = map->ops->map_lookup_elem_sys_only(map, key); 25315c14a3dSBrian Vazquez else 25415c14a3dSBrian Vazquez ptr = map->ops->map_lookup_elem(map, key); 25515c14a3dSBrian Vazquez if (IS_ERR(ptr)) { 25615c14a3dSBrian Vazquez err = PTR_ERR(ptr); 25715c14a3dSBrian Vazquez } else if (!ptr) { 25815c14a3dSBrian Vazquez err = -ENOENT; 25915c14a3dSBrian Vazquez } else { 26015c14a3dSBrian Vazquez err = 0; 26115c14a3dSBrian Vazquez if (flags & BPF_F_LOCK) 26215c14a3dSBrian Vazquez /* lock 'ptr' and copy everything but lock */ 26315c14a3dSBrian Vazquez copy_map_value_locked(map, value, ptr, true); 26415c14a3dSBrian Vazquez else 26515c14a3dSBrian Vazquez copy_map_value(map, value, ptr); 26615c14a3dSBrian Vazquez /* mask lock, since value wasn't zero inited */ 26715c14a3dSBrian Vazquez check_and_init_map_lock(map, value); 26815c14a3dSBrian Vazquez } 26915c14a3dSBrian Vazquez rcu_read_unlock(); 27015c14a3dSBrian Vazquez } 27115c14a3dSBrian Vazquez 272b6e5dae1SThomas Gleixner bpf_enable_instrumentation(); 27315c14a3dSBrian Vazquez maybe_wait_bpf_programs(map); 27415c14a3dSBrian Vazquez 27515c14a3dSBrian Vazquez return err; 27615c14a3dSBrian Vazquez } 27715c14a3dSBrian Vazquez 278196e8ca7SDaniel Borkmann static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable) 279d407bd25SDaniel Borkmann { 280f01a7dbeSMartynas Pumputis /* We really just want to fail instead of triggering OOM killer 281f01a7dbeSMartynas Pumputis * under memory pressure, therefore we set __GFP_NORETRY to kmalloc, 282f01a7dbeSMartynas Pumputis * which is used for lower order allocation requests. 283f01a7dbeSMartynas Pumputis * 284f01a7dbeSMartynas Pumputis * It has been observed that higher order allocation requests done by 285f01a7dbeSMartynas Pumputis * vmalloc with __GFP_NORETRY being set might fail due to not trying 286f01a7dbeSMartynas Pumputis * to reclaim memory from the page cache, thus we set 287f01a7dbeSMartynas Pumputis * __GFP_RETRY_MAYFAIL to avoid such situations. 288d407bd25SDaniel Borkmann */ 289f01a7dbeSMartynas Pumputis 290f01a7dbeSMartynas Pumputis const gfp_t flags = __GFP_NOWARN | __GFP_ZERO; 291d407bd25SDaniel Borkmann void *area; 292d407bd25SDaniel Borkmann 293196e8ca7SDaniel Borkmann if (size >= SIZE_MAX) 294196e8ca7SDaniel Borkmann return NULL; 295196e8ca7SDaniel Borkmann 296fc970227SAndrii Nakryiko /* kmalloc()'ed memory can't be mmap()'ed */ 297fc970227SAndrii Nakryiko if (!mmapable && size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { 298f01a7dbeSMartynas Pumputis area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags, 299f01a7dbeSMartynas Pumputis numa_node); 300d407bd25SDaniel Borkmann if (area != NULL) 301d407bd25SDaniel Borkmann return area; 302d407bd25SDaniel Borkmann } 303fc970227SAndrii Nakryiko if (mmapable) { 304fc970227SAndrii Nakryiko BUG_ON(!PAGE_ALIGNED(size)); 305fc970227SAndrii Nakryiko return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL | 306fc970227SAndrii Nakryiko __GFP_RETRY_MAYFAIL | flags); 307fc970227SAndrii Nakryiko } 308f01a7dbeSMartynas Pumputis return __vmalloc_node_flags_caller(size, numa_node, 309f01a7dbeSMartynas Pumputis GFP_KERNEL | __GFP_RETRY_MAYFAIL | 310f01a7dbeSMartynas Pumputis flags, __builtin_return_address(0)); 311d407bd25SDaniel Borkmann } 312d407bd25SDaniel Borkmann 313196e8ca7SDaniel Borkmann void *bpf_map_area_alloc(u64 size, int numa_node) 314fc970227SAndrii Nakryiko { 315fc970227SAndrii Nakryiko return __bpf_map_area_alloc(size, numa_node, false); 316fc970227SAndrii Nakryiko } 317fc970227SAndrii Nakryiko 318196e8ca7SDaniel Borkmann void *bpf_map_area_mmapable_alloc(u64 size, int numa_node) 319fc970227SAndrii Nakryiko { 320fc970227SAndrii Nakryiko return __bpf_map_area_alloc(size, numa_node, true); 321fc970227SAndrii Nakryiko } 322fc970227SAndrii Nakryiko 323d407bd25SDaniel Borkmann void bpf_map_area_free(void *area) 324d407bd25SDaniel Borkmann { 325d407bd25SDaniel Borkmann kvfree(area); 326d407bd25SDaniel Borkmann } 327d407bd25SDaniel Borkmann 328be70bcd5SDaniel Borkmann static u32 bpf_map_flags_retain_permanent(u32 flags) 329be70bcd5SDaniel Borkmann { 330be70bcd5SDaniel Borkmann /* Some map creation flags are not tied to the map object but 331be70bcd5SDaniel Borkmann * rather to the map fd instead, so they have no meaning upon 332be70bcd5SDaniel Borkmann * map object inspection since multiple file descriptors with 333be70bcd5SDaniel Borkmann * different (access) properties can exist here. Thus, given 334be70bcd5SDaniel Borkmann * this has zero meaning for the map itself, lets clear these 335be70bcd5SDaniel Borkmann * from here. 336be70bcd5SDaniel Borkmann */ 337be70bcd5SDaniel Borkmann return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY); 338be70bcd5SDaniel Borkmann } 339be70bcd5SDaniel Borkmann 340bd475643SJakub Kicinski void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) 341bd475643SJakub Kicinski { 342bd475643SJakub Kicinski map->map_type = attr->map_type; 343bd475643SJakub Kicinski map->key_size = attr->key_size; 344bd475643SJakub Kicinski map->value_size = attr->value_size; 345bd475643SJakub Kicinski map->max_entries = attr->max_entries; 346be70bcd5SDaniel Borkmann map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags); 347bd475643SJakub Kicinski map->numa_node = bpf_map_attr_numa_node(attr); 348bd475643SJakub Kicinski } 349bd475643SJakub Kicinski 3500a4c58f5SRoman Gushchin static int bpf_charge_memlock(struct user_struct *user, u32 pages) 351aaac3ba9SAlexei Starovoitov { 3520a4c58f5SRoman Gushchin unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 353aaac3ba9SAlexei Starovoitov 3540a4c58f5SRoman Gushchin if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) { 3550a4c58f5SRoman Gushchin atomic_long_sub(pages, &user->locked_vm); 356aaac3ba9SAlexei Starovoitov return -EPERM; 357aaac3ba9SAlexei Starovoitov } 358aaac3ba9SAlexei Starovoitov return 0; 359aaac3ba9SAlexei Starovoitov } 360aaac3ba9SAlexei Starovoitov 3610a4c58f5SRoman Gushchin static void bpf_uncharge_memlock(struct user_struct *user, u32 pages) 3620a4c58f5SRoman Gushchin { 363b936ca64SRoman Gushchin if (user) 3640a4c58f5SRoman Gushchin atomic_long_sub(pages, &user->locked_vm); 3650a4c58f5SRoman Gushchin } 3660a4c58f5SRoman Gushchin 367196e8ca7SDaniel Borkmann int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size) 3680a4c58f5SRoman Gushchin { 369c85d6913SRoman Gushchin u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT; 370c85d6913SRoman Gushchin struct user_struct *user; 3710a4c58f5SRoman Gushchin int ret; 3720a4c58f5SRoman Gushchin 373c85d6913SRoman Gushchin if (size >= U32_MAX - PAGE_SIZE) 374c85d6913SRoman Gushchin return -E2BIG; 375c85d6913SRoman Gushchin 376c85d6913SRoman Gushchin user = get_current_user(); 377b936ca64SRoman Gushchin ret = bpf_charge_memlock(user, pages); 3780a4c58f5SRoman Gushchin if (ret) { 3790a4c58f5SRoman Gushchin free_uid(user); 3800a4c58f5SRoman Gushchin return ret; 3810a4c58f5SRoman Gushchin } 382b936ca64SRoman Gushchin 383b936ca64SRoman Gushchin mem->pages = pages; 384b936ca64SRoman Gushchin mem->user = user; 385b936ca64SRoman Gushchin 386b936ca64SRoman Gushchin return 0; 3870a4c58f5SRoman Gushchin } 3880a4c58f5SRoman Gushchin 389b936ca64SRoman Gushchin void bpf_map_charge_finish(struct bpf_map_memory *mem) 390aaac3ba9SAlexei Starovoitov { 391b936ca64SRoman Gushchin bpf_uncharge_memlock(mem->user, mem->pages); 392b936ca64SRoman Gushchin free_uid(mem->user); 393b936ca64SRoman Gushchin } 3943539b96eSRoman Gushchin 395b936ca64SRoman Gushchin void bpf_map_charge_move(struct bpf_map_memory *dst, 396b936ca64SRoman Gushchin struct bpf_map_memory *src) 397b936ca64SRoman Gushchin { 398b936ca64SRoman Gushchin *dst = *src; 399b936ca64SRoman Gushchin 400b936ca64SRoman Gushchin /* Make sure src will not be used for the redundant uncharging. */ 401b936ca64SRoman Gushchin memset(src, 0, sizeof(struct bpf_map_memory)); 402aaac3ba9SAlexei Starovoitov } 403aaac3ba9SAlexei Starovoitov 4040a4c58f5SRoman Gushchin int bpf_map_charge_memlock(struct bpf_map *map, u32 pages) 4050a4c58f5SRoman Gushchin { 4060a4c58f5SRoman Gushchin int ret; 4070a4c58f5SRoman Gushchin 4083539b96eSRoman Gushchin ret = bpf_charge_memlock(map->memory.user, pages); 4090a4c58f5SRoman Gushchin if (ret) 4100a4c58f5SRoman Gushchin return ret; 4113539b96eSRoman Gushchin map->memory.pages += pages; 4120a4c58f5SRoman Gushchin return ret; 4130a4c58f5SRoman Gushchin } 4140a4c58f5SRoman Gushchin 4150a4c58f5SRoman Gushchin void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages) 4160a4c58f5SRoman Gushchin { 4173539b96eSRoman Gushchin bpf_uncharge_memlock(map->memory.user, pages); 4183539b96eSRoman Gushchin map->memory.pages -= pages; 4190a4c58f5SRoman Gushchin } 4200a4c58f5SRoman Gushchin 421f3f1c054SMartin KaFai Lau static int bpf_map_alloc_id(struct bpf_map *map) 422f3f1c054SMartin KaFai Lau { 423f3f1c054SMartin KaFai Lau int id; 424f3f1c054SMartin KaFai Lau 425b76354cdSShaohua Li idr_preload(GFP_KERNEL); 426f3f1c054SMartin KaFai Lau spin_lock_bh(&map_idr_lock); 427f3f1c054SMartin KaFai Lau id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); 428f3f1c054SMartin KaFai Lau if (id > 0) 429f3f1c054SMartin KaFai Lau map->id = id; 430f3f1c054SMartin KaFai Lau spin_unlock_bh(&map_idr_lock); 431b76354cdSShaohua Li idr_preload_end(); 432f3f1c054SMartin KaFai Lau 433f3f1c054SMartin KaFai Lau if (WARN_ON_ONCE(!id)) 434f3f1c054SMartin KaFai Lau return -ENOSPC; 435f3f1c054SMartin KaFai Lau 436f3f1c054SMartin KaFai Lau return id > 0 ? 0 : id; 437f3f1c054SMartin KaFai Lau } 438f3f1c054SMartin KaFai Lau 439a3884572SJakub Kicinski void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock) 440f3f1c054SMartin KaFai Lau { 441930651a7SEric Dumazet unsigned long flags; 442930651a7SEric Dumazet 443a3884572SJakub Kicinski /* Offloaded maps are removed from the IDR store when their device 444a3884572SJakub Kicinski * disappears - even if someone holds an fd to them they are unusable, 445a3884572SJakub Kicinski * the memory is gone, all ops will fail; they are simply waiting for 446a3884572SJakub Kicinski * refcnt to drop to be freed. 447a3884572SJakub Kicinski */ 448a3884572SJakub Kicinski if (!map->id) 449a3884572SJakub Kicinski return; 450a3884572SJakub Kicinski 451bd5f5f4eSMartin KaFai Lau if (do_idr_lock) 452930651a7SEric Dumazet spin_lock_irqsave(&map_idr_lock, flags); 453bd5f5f4eSMartin KaFai Lau else 454bd5f5f4eSMartin KaFai Lau __acquire(&map_idr_lock); 455bd5f5f4eSMartin KaFai Lau 456f3f1c054SMartin KaFai Lau idr_remove(&map_idr, map->id); 457a3884572SJakub Kicinski map->id = 0; 458bd5f5f4eSMartin KaFai Lau 459bd5f5f4eSMartin KaFai Lau if (do_idr_lock) 460930651a7SEric Dumazet spin_unlock_irqrestore(&map_idr_lock, flags); 461bd5f5f4eSMartin KaFai Lau else 462bd5f5f4eSMartin KaFai Lau __release(&map_idr_lock); 463f3f1c054SMartin KaFai Lau } 464f3f1c054SMartin KaFai Lau 46599c55f7dSAlexei Starovoitov /* called from workqueue */ 46699c55f7dSAlexei Starovoitov static void bpf_map_free_deferred(struct work_struct *work) 46799c55f7dSAlexei Starovoitov { 46899c55f7dSAlexei Starovoitov struct bpf_map *map = container_of(work, struct bpf_map, work); 469b936ca64SRoman Gushchin struct bpf_map_memory mem; 47099c55f7dSAlexei Starovoitov 471b936ca64SRoman Gushchin bpf_map_charge_move(&mem, &map->memory); 472afdb09c7SChenbo Feng security_bpf_map_free(map); 47399c55f7dSAlexei Starovoitov /* implementation dependent freeing */ 47499c55f7dSAlexei Starovoitov map->ops->map_free(map); 475b936ca64SRoman Gushchin bpf_map_charge_finish(&mem); 47699c55f7dSAlexei Starovoitov } 47799c55f7dSAlexei Starovoitov 478c9da161cSDaniel Borkmann static void bpf_map_put_uref(struct bpf_map *map) 479c9da161cSDaniel Borkmann { 4801e0bd5a0SAndrii Nakryiko if (atomic64_dec_and_test(&map->usercnt)) { 481ba6b8de4SJohn Fastabend if (map->ops->map_release_uref) 482ba6b8de4SJohn Fastabend map->ops->map_release_uref(map); 483c9da161cSDaniel Borkmann } 484c9da161cSDaniel Borkmann } 485c9da161cSDaniel Borkmann 48699c55f7dSAlexei Starovoitov /* decrement map refcnt and schedule it for freeing via workqueue 48799c55f7dSAlexei Starovoitov * (unrelying map implementation ops->map_free() might sleep) 48899c55f7dSAlexei Starovoitov */ 489bd5f5f4eSMartin KaFai Lau static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock) 49099c55f7dSAlexei Starovoitov { 4911e0bd5a0SAndrii Nakryiko if (atomic64_dec_and_test(&map->refcnt)) { 49234ad5580SMartin KaFai Lau /* bpf_map_free_id() must be called first */ 493bd5f5f4eSMartin KaFai Lau bpf_map_free_id(map, do_idr_lock); 49478958fcaSMartin KaFai Lau btf_put(map->btf); 49599c55f7dSAlexei Starovoitov INIT_WORK(&map->work, bpf_map_free_deferred); 49699c55f7dSAlexei Starovoitov schedule_work(&map->work); 49799c55f7dSAlexei Starovoitov } 49899c55f7dSAlexei Starovoitov } 49999c55f7dSAlexei Starovoitov 500bd5f5f4eSMartin KaFai Lau void bpf_map_put(struct bpf_map *map) 501bd5f5f4eSMartin KaFai Lau { 502bd5f5f4eSMartin KaFai Lau __bpf_map_put(map, true); 503bd5f5f4eSMartin KaFai Lau } 504630a4d38SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_map_put); 505bd5f5f4eSMartin KaFai Lau 506c9da161cSDaniel Borkmann void bpf_map_put_with_uref(struct bpf_map *map) 507c9da161cSDaniel Borkmann { 508c9da161cSDaniel Borkmann bpf_map_put_uref(map); 509c9da161cSDaniel Borkmann bpf_map_put(map); 510c9da161cSDaniel Borkmann } 511c9da161cSDaniel Borkmann 51299c55f7dSAlexei Starovoitov static int bpf_map_release(struct inode *inode, struct file *filp) 51399c55f7dSAlexei Starovoitov { 51461d1b6a4SDaniel Borkmann struct bpf_map *map = filp->private_data; 51561d1b6a4SDaniel Borkmann 51661d1b6a4SDaniel Borkmann if (map->ops->map_release) 51761d1b6a4SDaniel Borkmann map->ops->map_release(map, filp); 51861d1b6a4SDaniel Borkmann 51961d1b6a4SDaniel Borkmann bpf_map_put_with_uref(map); 52099c55f7dSAlexei Starovoitov return 0; 52199c55f7dSAlexei Starovoitov } 52299c55f7dSAlexei Starovoitov 52387df15deSDaniel Borkmann static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f) 52487df15deSDaniel Borkmann { 52587df15deSDaniel Borkmann fmode_t mode = f.file->f_mode; 52687df15deSDaniel Borkmann 52787df15deSDaniel Borkmann /* Our file permissions may have been overridden by global 52887df15deSDaniel Borkmann * map permissions facing syscall side. 52987df15deSDaniel Borkmann */ 53087df15deSDaniel Borkmann if (READ_ONCE(map->frozen)) 53187df15deSDaniel Borkmann mode &= ~FMODE_CAN_WRITE; 53287df15deSDaniel Borkmann return mode; 53387df15deSDaniel Borkmann } 53487df15deSDaniel Borkmann 535f99bf205SDaniel Borkmann #ifdef CONFIG_PROC_FS 536f99bf205SDaniel Borkmann static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) 537f99bf205SDaniel Borkmann { 538f99bf205SDaniel Borkmann const struct bpf_map *map = filp->private_data; 53921116b70SDaniel Borkmann const struct bpf_array *array; 5402beee5f5SDaniel Borkmann u32 type = 0, jited = 0; 54121116b70SDaniel Borkmann 54221116b70SDaniel Borkmann if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { 54321116b70SDaniel Borkmann array = container_of(map, struct bpf_array, map); 5442beee5f5SDaniel Borkmann type = array->aux->type; 5452beee5f5SDaniel Borkmann jited = array->aux->jited; 54621116b70SDaniel Borkmann } 547f99bf205SDaniel Borkmann 548f99bf205SDaniel Borkmann seq_printf(m, 549f99bf205SDaniel Borkmann "map_type:\t%u\n" 550f99bf205SDaniel Borkmann "key_size:\t%u\n" 551f99bf205SDaniel Borkmann "value_size:\t%u\n" 552322cea2fSDaniel Borkmann "max_entries:\t%u\n" 55321116b70SDaniel Borkmann "map_flags:\t%#x\n" 5544316b409SDaniel Borkmann "memlock:\t%llu\n" 55587df15deSDaniel Borkmann "map_id:\t%u\n" 55687df15deSDaniel Borkmann "frozen:\t%u\n", 557f99bf205SDaniel Borkmann map->map_type, 558f99bf205SDaniel Borkmann map->key_size, 559f99bf205SDaniel Borkmann map->value_size, 560322cea2fSDaniel Borkmann map->max_entries, 56121116b70SDaniel Borkmann map->map_flags, 5623539b96eSRoman Gushchin map->memory.pages * 1ULL << PAGE_SHIFT, 56387df15deSDaniel Borkmann map->id, 56487df15deSDaniel Borkmann READ_ONCE(map->frozen)); 5652beee5f5SDaniel Borkmann if (type) { 5662beee5f5SDaniel Borkmann seq_printf(m, "owner_prog_type:\t%u\n", type); 5672beee5f5SDaniel Borkmann seq_printf(m, "owner_jited:\t%u\n", jited); 5689780c0abSDaniel Borkmann } 569f99bf205SDaniel Borkmann } 570f99bf205SDaniel Borkmann #endif 571f99bf205SDaniel Borkmann 5726e71b04aSChenbo Feng static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, 5736e71b04aSChenbo Feng loff_t *ppos) 5746e71b04aSChenbo Feng { 5756e71b04aSChenbo Feng /* We need this handler such that alloc_file() enables 5766e71b04aSChenbo Feng * f_mode with FMODE_CAN_READ. 5776e71b04aSChenbo Feng */ 5786e71b04aSChenbo Feng return -EINVAL; 5796e71b04aSChenbo Feng } 5806e71b04aSChenbo Feng 5816e71b04aSChenbo Feng static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, 5826e71b04aSChenbo Feng size_t siz, loff_t *ppos) 5836e71b04aSChenbo Feng { 5846e71b04aSChenbo Feng /* We need this handler such that alloc_file() enables 5856e71b04aSChenbo Feng * f_mode with FMODE_CAN_WRITE. 5866e71b04aSChenbo Feng */ 5876e71b04aSChenbo Feng return -EINVAL; 5886e71b04aSChenbo Feng } 5896e71b04aSChenbo Feng 590fc970227SAndrii Nakryiko /* called for any extra memory-mapped regions (except initial) */ 591fc970227SAndrii Nakryiko static void bpf_map_mmap_open(struct vm_area_struct *vma) 592fc970227SAndrii Nakryiko { 593fc970227SAndrii Nakryiko struct bpf_map *map = vma->vm_file->private_data; 594fc970227SAndrii Nakryiko 5951f6cb19bSAndrii Nakryiko if (vma->vm_flags & VM_MAYWRITE) { 596fc970227SAndrii Nakryiko mutex_lock(&map->freeze_mutex); 597fc970227SAndrii Nakryiko map->writecnt++; 598fc970227SAndrii Nakryiko mutex_unlock(&map->freeze_mutex); 599fc970227SAndrii Nakryiko } 600fc970227SAndrii Nakryiko } 601fc970227SAndrii Nakryiko 602fc970227SAndrii Nakryiko /* called for all unmapped memory region (including initial) */ 603fc970227SAndrii Nakryiko static void bpf_map_mmap_close(struct vm_area_struct *vma) 604fc970227SAndrii Nakryiko { 605fc970227SAndrii Nakryiko struct bpf_map *map = vma->vm_file->private_data; 606fc970227SAndrii Nakryiko 6071f6cb19bSAndrii Nakryiko if (vma->vm_flags & VM_MAYWRITE) { 608fc970227SAndrii Nakryiko mutex_lock(&map->freeze_mutex); 609fc970227SAndrii Nakryiko map->writecnt--; 610fc970227SAndrii Nakryiko mutex_unlock(&map->freeze_mutex); 611fc970227SAndrii Nakryiko } 612fc970227SAndrii Nakryiko } 613fc970227SAndrii Nakryiko 614fc970227SAndrii Nakryiko static const struct vm_operations_struct bpf_map_default_vmops = { 615fc970227SAndrii Nakryiko .open = bpf_map_mmap_open, 616fc970227SAndrii Nakryiko .close = bpf_map_mmap_close, 617fc970227SAndrii Nakryiko }; 618fc970227SAndrii Nakryiko 619fc970227SAndrii Nakryiko static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma) 620fc970227SAndrii Nakryiko { 621fc970227SAndrii Nakryiko struct bpf_map *map = filp->private_data; 622fc970227SAndrii Nakryiko int err; 623fc970227SAndrii Nakryiko 624fc970227SAndrii Nakryiko if (!map->ops->map_mmap || map_value_has_spin_lock(map)) 625fc970227SAndrii Nakryiko return -ENOTSUPP; 626fc970227SAndrii Nakryiko 627fc970227SAndrii Nakryiko if (!(vma->vm_flags & VM_SHARED)) 628fc970227SAndrii Nakryiko return -EINVAL; 629fc970227SAndrii Nakryiko 630fc970227SAndrii Nakryiko mutex_lock(&map->freeze_mutex); 631fc970227SAndrii Nakryiko 632dfeb376dSAndrii Nakryiko if (vma->vm_flags & VM_WRITE) { 633dfeb376dSAndrii Nakryiko if (map->frozen) { 634fc970227SAndrii Nakryiko err = -EPERM; 635fc970227SAndrii Nakryiko goto out; 636fc970227SAndrii Nakryiko } 637dfeb376dSAndrii Nakryiko /* map is meant to be read-only, so do not allow mapping as 638dfeb376dSAndrii Nakryiko * writable, because it's possible to leak a writable page 639dfeb376dSAndrii Nakryiko * reference and allows user-space to still modify it after 640dfeb376dSAndrii Nakryiko * freezing, while verifier will assume contents do not change 641dfeb376dSAndrii Nakryiko */ 642dfeb376dSAndrii Nakryiko if (map->map_flags & BPF_F_RDONLY_PROG) { 643dfeb376dSAndrii Nakryiko err = -EACCES; 644dfeb376dSAndrii Nakryiko goto out; 645dfeb376dSAndrii Nakryiko } 646dfeb376dSAndrii Nakryiko } 647fc970227SAndrii Nakryiko 648fc970227SAndrii Nakryiko /* set default open/close callbacks */ 649fc970227SAndrii Nakryiko vma->vm_ops = &bpf_map_default_vmops; 650fc970227SAndrii Nakryiko vma->vm_private_data = map; 6511f6cb19bSAndrii Nakryiko vma->vm_flags &= ~VM_MAYEXEC; 6521f6cb19bSAndrii Nakryiko if (!(vma->vm_flags & VM_WRITE)) 6531f6cb19bSAndrii Nakryiko /* disallow re-mapping with PROT_WRITE */ 6541f6cb19bSAndrii Nakryiko vma->vm_flags &= ~VM_MAYWRITE; 655fc970227SAndrii Nakryiko 656fc970227SAndrii Nakryiko err = map->ops->map_mmap(map, vma); 657fc970227SAndrii Nakryiko if (err) 658fc970227SAndrii Nakryiko goto out; 659fc970227SAndrii Nakryiko 6601f6cb19bSAndrii Nakryiko if (vma->vm_flags & VM_MAYWRITE) 661fc970227SAndrii Nakryiko map->writecnt++; 662fc970227SAndrii Nakryiko out: 663fc970227SAndrii Nakryiko mutex_unlock(&map->freeze_mutex); 664fc970227SAndrii Nakryiko return err; 665fc970227SAndrii Nakryiko } 666fc970227SAndrii Nakryiko 667457f4436SAndrii Nakryiko static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts) 668457f4436SAndrii Nakryiko { 669457f4436SAndrii Nakryiko struct bpf_map *map = filp->private_data; 670457f4436SAndrii Nakryiko 671457f4436SAndrii Nakryiko if (map->ops->map_poll) 672457f4436SAndrii Nakryiko return map->ops->map_poll(map, filp, pts); 673457f4436SAndrii Nakryiko 674457f4436SAndrii Nakryiko return EPOLLERR; 675457f4436SAndrii Nakryiko } 676457f4436SAndrii Nakryiko 677f66e448cSChenbo Feng const struct file_operations bpf_map_fops = { 678f99bf205SDaniel Borkmann #ifdef CONFIG_PROC_FS 679f99bf205SDaniel Borkmann .show_fdinfo = bpf_map_show_fdinfo, 680f99bf205SDaniel Borkmann #endif 68199c55f7dSAlexei Starovoitov .release = bpf_map_release, 6826e71b04aSChenbo Feng .read = bpf_dummy_read, 6836e71b04aSChenbo Feng .write = bpf_dummy_write, 684fc970227SAndrii Nakryiko .mmap = bpf_map_mmap, 685457f4436SAndrii Nakryiko .poll = bpf_map_poll, 68699c55f7dSAlexei Starovoitov }; 68799c55f7dSAlexei Starovoitov 6886e71b04aSChenbo Feng int bpf_map_new_fd(struct bpf_map *map, int flags) 689aa79781bSDaniel Borkmann { 690afdb09c7SChenbo Feng int ret; 691afdb09c7SChenbo Feng 692afdb09c7SChenbo Feng ret = security_bpf_map(map, OPEN_FMODE(flags)); 693afdb09c7SChenbo Feng if (ret < 0) 694afdb09c7SChenbo Feng return ret; 695afdb09c7SChenbo Feng 696aa79781bSDaniel Borkmann return anon_inode_getfd("bpf-map", &bpf_map_fops, map, 6976e71b04aSChenbo Feng flags | O_CLOEXEC); 6986e71b04aSChenbo Feng } 6996e71b04aSChenbo Feng 7006e71b04aSChenbo Feng int bpf_get_file_flag(int flags) 7016e71b04aSChenbo Feng { 7026e71b04aSChenbo Feng if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) 7036e71b04aSChenbo Feng return -EINVAL; 7046e71b04aSChenbo Feng if (flags & BPF_F_RDONLY) 7056e71b04aSChenbo Feng return O_RDONLY; 7066e71b04aSChenbo Feng if (flags & BPF_F_WRONLY) 7076e71b04aSChenbo Feng return O_WRONLY; 7086e71b04aSChenbo Feng return O_RDWR; 709aa79781bSDaniel Borkmann } 710aa79781bSDaniel Borkmann 71199c55f7dSAlexei Starovoitov /* helper macro to check that unused fields 'union bpf_attr' are zero */ 71299c55f7dSAlexei Starovoitov #define CHECK_ATTR(CMD) \ 71399c55f7dSAlexei Starovoitov memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ 71499c55f7dSAlexei Starovoitov sizeof(attr->CMD##_LAST_FIELD), 0, \ 71599c55f7dSAlexei Starovoitov sizeof(*attr) - \ 71699c55f7dSAlexei Starovoitov offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ 71799c55f7dSAlexei Starovoitov sizeof(attr->CMD##_LAST_FIELD)) != NULL 71899c55f7dSAlexei Starovoitov 7198e7ae251SMartin KaFai Lau /* dst and src must have at least "size" number of bytes. 7208e7ae251SMartin KaFai Lau * Return strlen on success and < 0 on error. 721cb4d2b3fSMartin KaFai Lau */ 7228e7ae251SMartin KaFai Lau int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size) 723cb4d2b3fSMartin KaFai Lau { 7248e7ae251SMartin KaFai Lau const char *end = src + size; 7258e7ae251SMartin KaFai Lau const char *orig_src = src; 726cb4d2b3fSMartin KaFai Lau 7278e7ae251SMartin KaFai Lau memset(dst, 0, size); 7283e0ddc4fSDaniel Borkmann /* Copy all isalnum(), '_' and '.' chars. */ 729cb4d2b3fSMartin KaFai Lau while (src < end && *src) { 7303e0ddc4fSDaniel Borkmann if (!isalnum(*src) && 7313e0ddc4fSDaniel Borkmann *src != '_' && *src != '.') 732cb4d2b3fSMartin KaFai Lau return -EINVAL; 733cb4d2b3fSMartin KaFai Lau *dst++ = *src++; 734cb4d2b3fSMartin KaFai Lau } 735cb4d2b3fSMartin KaFai Lau 7368e7ae251SMartin KaFai Lau /* No '\0' found in "size" number of bytes */ 737cb4d2b3fSMartin KaFai Lau if (src == end) 738cb4d2b3fSMartin KaFai Lau return -EINVAL; 739cb4d2b3fSMartin KaFai Lau 7408e7ae251SMartin KaFai Lau return src - orig_src; 741cb4d2b3fSMartin KaFai Lau } 742cb4d2b3fSMartin KaFai Lau 743e8d2bec0SDaniel Borkmann int map_check_no_btf(const struct bpf_map *map, 7441b2b234bSRoman Gushchin const struct btf *btf, 745e8d2bec0SDaniel Borkmann const struct btf_type *key_type, 746e8d2bec0SDaniel Borkmann const struct btf_type *value_type) 747e8d2bec0SDaniel Borkmann { 748e8d2bec0SDaniel Borkmann return -ENOTSUPP; 749e8d2bec0SDaniel Borkmann } 750e8d2bec0SDaniel Borkmann 751d83525caSAlexei Starovoitov static int map_check_btf(struct bpf_map *map, const struct btf *btf, 752e8d2bec0SDaniel Borkmann u32 btf_key_id, u32 btf_value_id) 753e8d2bec0SDaniel Borkmann { 754e8d2bec0SDaniel Borkmann const struct btf_type *key_type, *value_type; 755e8d2bec0SDaniel Borkmann u32 key_size, value_size; 756e8d2bec0SDaniel Borkmann int ret = 0; 757e8d2bec0SDaniel Borkmann 7582824ecb7SDaniel Borkmann /* Some maps allow key to be unspecified. */ 7592824ecb7SDaniel Borkmann if (btf_key_id) { 760e8d2bec0SDaniel Borkmann key_type = btf_type_id_size(btf, &btf_key_id, &key_size); 761e8d2bec0SDaniel Borkmann if (!key_type || key_size != map->key_size) 762e8d2bec0SDaniel Borkmann return -EINVAL; 7632824ecb7SDaniel Borkmann } else { 7642824ecb7SDaniel Borkmann key_type = btf_type_by_id(btf, 0); 7652824ecb7SDaniel Borkmann if (!map->ops->map_check_btf) 7662824ecb7SDaniel Borkmann return -EINVAL; 7672824ecb7SDaniel Borkmann } 768e8d2bec0SDaniel Borkmann 769e8d2bec0SDaniel Borkmann value_type = btf_type_id_size(btf, &btf_value_id, &value_size); 770e8d2bec0SDaniel Borkmann if (!value_type || value_size != map->value_size) 771e8d2bec0SDaniel Borkmann return -EINVAL; 772e8d2bec0SDaniel Borkmann 773d83525caSAlexei Starovoitov map->spin_lock_off = btf_find_spin_lock(btf, value_type); 774d83525caSAlexei Starovoitov 775d83525caSAlexei Starovoitov if (map_value_has_spin_lock(map)) { 776591fe988SDaniel Borkmann if (map->map_flags & BPF_F_RDONLY_PROG) 777591fe988SDaniel Borkmann return -EACCES; 778d83525caSAlexei Starovoitov if (map->map_type != BPF_MAP_TYPE_HASH && 779e16d2f1aSAlexei Starovoitov map->map_type != BPF_MAP_TYPE_ARRAY && 7806ac99e8fSMartin KaFai Lau map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && 7816ac99e8fSMartin KaFai Lau map->map_type != BPF_MAP_TYPE_SK_STORAGE) 782d83525caSAlexei Starovoitov return -ENOTSUPP; 783d83525caSAlexei Starovoitov if (map->spin_lock_off + sizeof(struct bpf_spin_lock) > 784d83525caSAlexei Starovoitov map->value_size) { 785d83525caSAlexei Starovoitov WARN_ONCE(1, 786d83525caSAlexei Starovoitov "verifier bug spin_lock_off %d value_size %d\n", 787d83525caSAlexei Starovoitov map->spin_lock_off, map->value_size); 788d83525caSAlexei Starovoitov return -EFAULT; 789d83525caSAlexei Starovoitov } 790d83525caSAlexei Starovoitov } 791d83525caSAlexei Starovoitov 792e8d2bec0SDaniel Borkmann if (map->ops->map_check_btf) 7931b2b234bSRoman Gushchin ret = map->ops->map_check_btf(map, btf, key_type, value_type); 794e8d2bec0SDaniel Borkmann 795e8d2bec0SDaniel Borkmann return ret; 796e8d2bec0SDaniel Borkmann } 797e8d2bec0SDaniel Borkmann 79885d33df3SMartin KaFai Lau #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id 79999c55f7dSAlexei Starovoitov /* called via syscall */ 80099c55f7dSAlexei Starovoitov static int map_create(union bpf_attr *attr) 80199c55f7dSAlexei Starovoitov { 80296eabe7aSMartin KaFai Lau int numa_node = bpf_map_attr_numa_node(attr); 803b936ca64SRoman Gushchin struct bpf_map_memory mem; 80499c55f7dSAlexei Starovoitov struct bpf_map *map; 8056e71b04aSChenbo Feng int f_flags; 80699c55f7dSAlexei Starovoitov int err; 80799c55f7dSAlexei Starovoitov 80899c55f7dSAlexei Starovoitov err = CHECK_ATTR(BPF_MAP_CREATE); 80999c55f7dSAlexei Starovoitov if (err) 81099c55f7dSAlexei Starovoitov return -EINVAL; 81199c55f7dSAlexei Starovoitov 81285d33df3SMartin KaFai Lau if (attr->btf_vmlinux_value_type_id) { 81385d33df3SMartin KaFai Lau if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS || 81485d33df3SMartin KaFai Lau attr->btf_key_type_id || attr->btf_value_type_id) 81585d33df3SMartin KaFai Lau return -EINVAL; 81685d33df3SMartin KaFai Lau } else if (attr->btf_key_type_id && !attr->btf_value_type_id) { 81785d33df3SMartin KaFai Lau return -EINVAL; 81885d33df3SMartin KaFai Lau } 81985d33df3SMartin KaFai Lau 8206e71b04aSChenbo Feng f_flags = bpf_get_file_flag(attr->map_flags); 8216e71b04aSChenbo Feng if (f_flags < 0) 8226e71b04aSChenbo Feng return f_flags; 8236e71b04aSChenbo Feng 82496eabe7aSMartin KaFai Lau if (numa_node != NUMA_NO_NODE && 82596e5ae4eSEric Dumazet ((unsigned int)numa_node >= nr_node_ids || 82696e5ae4eSEric Dumazet !node_online(numa_node))) 82796eabe7aSMartin KaFai Lau return -EINVAL; 82896eabe7aSMartin KaFai Lau 82999c55f7dSAlexei Starovoitov /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ 83099c55f7dSAlexei Starovoitov map = find_and_alloc_map(attr); 83199c55f7dSAlexei Starovoitov if (IS_ERR(map)) 83299c55f7dSAlexei Starovoitov return PTR_ERR(map); 83399c55f7dSAlexei Starovoitov 8348e7ae251SMartin KaFai Lau err = bpf_obj_name_cpy(map->name, attr->map_name, 8358e7ae251SMartin KaFai Lau sizeof(attr->map_name)); 8368e7ae251SMartin KaFai Lau if (err < 0) 837b936ca64SRoman Gushchin goto free_map; 838ad5b177bSMartin KaFai Lau 8391e0bd5a0SAndrii Nakryiko atomic64_set(&map->refcnt, 1); 8401e0bd5a0SAndrii Nakryiko atomic64_set(&map->usercnt, 1); 841fc970227SAndrii Nakryiko mutex_init(&map->freeze_mutex); 84299c55f7dSAlexei Starovoitov 84385d33df3SMartin KaFai Lau map->spin_lock_off = -EINVAL; 84485d33df3SMartin KaFai Lau if (attr->btf_key_type_id || attr->btf_value_type_id || 84585d33df3SMartin KaFai Lau /* Even the map's value is a kernel's struct, 84685d33df3SMartin KaFai Lau * the bpf_prog.o must have BTF to begin with 84785d33df3SMartin KaFai Lau * to figure out the corresponding kernel's 84885d33df3SMartin KaFai Lau * counter part. Thus, attr->btf_fd has 84985d33df3SMartin KaFai Lau * to be valid also. 85085d33df3SMartin KaFai Lau */ 85185d33df3SMartin KaFai Lau attr->btf_vmlinux_value_type_id) { 852a26ca7c9SMartin KaFai Lau struct btf *btf; 853a26ca7c9SMartin KaFai Lau 854a26ca7c9SMartin KaFai Lau btf = btf_get_by_fd(attr->btf_fd); 855a26ca7c9SMartin KaFai Lau if (IS_ERR(btf)) { 856a26ca7c9SMartin KaFai Lau err = PTR_ERR(btf); 857b936ca64SRoman Gushchin goto free_map; 858a26ca7c9SMartin KaFai Lau } 85985d33df3SMartin KaFai Lau map->btf = btf; 860a26ca7c9SMartin KaFai Lau 86185d33df3SMartin KaFai Lau if (attr->btf_value_type_id) { 862e8d2bec0SDaniel Borkmann err = map_check_btf(map, btf, attr->btf_key_type_id, 8639b2cf328SMartin KaFai Lau attr->btf_value_type_id); 86485d33df3SMartin KaFai Lau if (err) 865b936ca64SRoman Gushchin goto free_map; 866a26ca7c9SMartin KaFai Lau } 867a26ca7c9SMartin KaFai Lau 8689b2cf328SMartin KaFai Lau map->btf_key_type_id = attr->btf_key_type_id; 8699b2cf328SMartin KaFai Lau map->btf_value_type_id = attr->btf_value_type_id; 87085d33df3SMartin KaFai Lau map->btf_vmlinux_value_type_id = 87185d33df3SMartin KaFai Lau attr->btf_vmlinux_value_type_id; 872a26ca7c9SMartin KaFai Lau } 873a26ca7c9SMartin KaFai Lau 874afdb09c7SChenbo Feng err = security_bpf_map_alloc(map); 875aaac3ba9SAlexei Starovoitov if (err) 876b936ca64SRoman Gushchin goto free_map; 877afdb09c7SChenbo Feng 878f3f1c054SMartin KaFai Lau err = bpf_map_alloc_id(map); 879f3f1c054SMartin KaFai Lau if (err) 880b936ca64SRoman Gushchin goto free_map_sec; 881f3f1c054SMartin KaFai Lau 8826e71b04aSChenbo Feng err = bpf_map_new_fd(map, f_flags); 883bd5f5f4eSMartin KaFai Lau if (err < 0) { 884bd5f5f4eSMartin KaFai Lau /* failed to allocate fd. 885352d20d6SPeng Sun * bpf_map_put_with_uref() is needed because the above 886bd5f5f4eSMartin KaFai Lau * bpf_map_alloc_id() has published the map 887bd5f5f4eSMartin KaFai Lau * to the userspace and the userspace may 888bd5f5f4eSMartin KaFai Lau * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. 889bd5f5f4eSMartin KaFai Lau */ 890352d20d6SPeng Sun bpf_map_put_with_uref(map); 891bd5f5f4eSMartin KaFai Lau return err; 892bd5f5f4eSMartin KaFai Lau } 89399c55f7dSAlexei Starovoitov 89499c55f7dSAlexei Starovoitov return err; 89599c55f7dSAlexei Starovoitov 896afdb09c7SChenbo Feng free_map_sec: 897afdb09c7SChenbo Feng security_bpf_map_free(map); 898b936ca64SRoman Gushchin free_map: 899a26ca7c9SMartin KaFai Lau btf_put(map->btf); 900b936ca64SRoman Gushchin bpf_map_charge_move(&mem, &map->memory); 90199c55f7dSAlexei Starovoitov map->ops->map_free(map); 902b936ca64SRoman Gushchin bpf_map_charge_finish(&mem); 90399c55f7dSAlexei Starovoitov return err; 90499c55f7dSAlexei Starovoitov } 90599c55f7dSAlexei Starovoitov 906db20fd2bSAlexei Starovoitov /* if error is returned, fd is released. 907db20fd2bSAlexei Starovoitov * On success caller should complete fd access with matching fdput() 908db20fd2bSAlexei Starovoitov */ 909c2101297SDaniel Borkmann struct bpf_map *__bpf_map_get(struct fd f) 910db20fd2bSAlexei Starovoitov { 911db20fd2bSAlexei Starovoitov if (!f.file) 912db20fd2bSAlexei Starovoitov return ERR_PTR(-EBADF); 913db20fd2bSAlexei Starovoitov if (f.file->f_op != &bpf_map_fops) { 914db20fd2bSAlexei Starovoitov fdput(f); 915db20fd2bSAlexei Starovoitov return ERR_PTR(-EINVAL); 916db20fd2bSAlexei Starovoitov } 917db20fd2bSAlexei Starovoitov 918c2101297SDaniel Borkmann return f.file->private_data; 919c2101297SDaniel Borkmann } 920c2101297SDaniel Borkmann 9211e0bd5a0SAndrii Nakryiko void bpf_map_inc(struct bpf_map *map) 922c9da161cSDaniel Borkmann { 9231e0bd5a0SAndrii Nakryiko atomic64_inc(&map->refcnt); 924c9da161cSDaniel Borkmann } 925630a4d38SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_map_inc); 926c9da161cSDaniel Borkmann 9271e0bd5a0SAndrii Nakryiko void bpf_map_inc_with_uref(struct bpf_map *map) 9281e0bd5a0SAndrii Nakryiko { 9291e0bd5a0SAndrii Nakryiko atomic64_inc(&map->refcnt); 9301e0bd5a0SAndrii Nakryiko atomic64_inc(&map->usercnt); 9311e0bd5a0SAndrii Nakryiko } 9321e0bd5a0SAndrii Nakryiko EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref); 9331e0bd5a0SAndrii Nakryiko 9341ed4d924SMartin KaFai Lau struct bpf_map *bpf_map_get(u32 ufd) 9351ed4d924SMartin KaFai Lau { 9361ed4d924SMartin KaFai Lau struct fd f = fdget(ufd); 9371ed4d924SMartin KaFai Lau struct bpf_map *map; 9381ed4d924SMartin KaFai Lau 9391ed4d924SMartin KaFai Lau map = __bpf_map_get(f); 9401ed4d924SMartin KaFai Lau if (IS_ERR(map)) 9411ed4d924SMartin KaFai Lau return map; 9421ed4d924SMartin KaFai Lau 9431ed4d924SMartin KaFai Lau bpf_map_inc(map); 9441ed4d924SMartin KaFai Lau fdput(f); 9451ed4d924SMartin KaFai Lau 9461ed4d924SMartin KaFai Lau return map; 9471ed4d924SMartin KaFai Lau } 9481ed4d924SMartin KaFai Lau 949c9da161cSDaniel Borkmann struct bpf_map *bpf_map_get_with_uref(u32 ufd) 950c2101297SDaniel Borkmann { 951c2101297SDaniel Borkmann struct fd f = fdget(ufd); 952c2101297SDaniel Borkmann struct bpf_map *map; 953c2101297SDaniel Borkmann 954c2101297SDaniel Borkmann map = __bpf_map_get(f); 955c2101297SDaniel Borkmann if (IS_ERR(map)) 956c2101297SDaniel Borkmann return map; 957c2101297SDaniel Borkmann 9581e0bd5a0SAndrii Nakryiko bpf_map_inc_with_uref(map); 959c2101297SDaniel Borkmann fdput(f); 960db20fd2bSAlexei Starovoitov 961db20fd2bSAlexei Starovoitov return map; 962db20fd2bSAlexei Starovoitov } 963db20fd2bSAlexei Starovoitov 964bd5f5f4eSMartin KaFai Lau /* map_idr_lock should have been held */ 9651e0bd5a0SAndrii Nakryiko static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref) 966bd5f5f4eSMartin KaFai Lau { 967bd5f5f4eSMartin KaFai Lau int refold; 968bd5f5f4eSMartin KaFai Lau 9691e0bd5a0SAndrii Nakryiko refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0); 970bd5f5f4eSMartin KaFai Lau if (!refold) 971bd5f5f4eSMartin KaFai Lau return ERR_PTR(-ENOENT); 972bd5f5f4eSMartin KaFai Lau if (uref) 9731e0bd5a0SAndrii Nakryiko atomic64_inc(&map->usercnt); 974bd5f5f4eSMartin KaFai Lau 975bd5f5f4eSMartin KaFai Lau return map; 976bd5f5f4eSMartin KaFai Lau } 977bd5f5f4eSMartin KaFai Lau 9781e0bd5a0SAndrii Nakryiko struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map) 979b0e4701cSStanislav Fomichev { 980b0e4701cSStanislav Fomichev spin_lock_bh(&map_idr_lock); 9811e0bd5a0SAndrii Nakryiko map = __bpf_map_inc_not_zero(map, false); 982b0e4701cSStanislav Fomichev spin_unlock_bh(&map_idr_lock); 983b0e4701cSStanislav Fomichev 984b0e4701cSStanislav Fomichev return map; 985b0e4701cSStanislav Fomichev } 986b0e4701cSStanislav Fomichev EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero); 987b0e4701cSStanislav Fomichev 988b8cdc051SAlexei Starovoitov int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) 989b8cdc051SAlexei Starovoitov { 990b8cdc051SAlexei Starovoitov return -ENOTSUPP; 991b8cdc051SAlexei Starovoitov } 992b8cdc051SAlexei Starovoitov 993c9d29f46SMauricio Vasquez B static void *__bpf_copy_key(void __user *ukey, u64 key_size) 994c9d29f46SMauricio Vasquez B { 995c9d29f46SMauricio Vasquez B if (key_size) 996c9d29f46SMauricio Vasquez B return memdup_user(ukey, key_size); 997c9d29f46SMauricio Vasquez B 998c9d29f46SMauricio Vasquez B if (ukey) 999c9d29f46SMauricio Vasquez B return ERR_PTR(-EINVAL); 1000c9d29f46SMauricio Vasquez B 1001c9d29f46SMauricio Vasquez B return NULL; 1002c9d29f46SMauricio Vasquez B } 1003c9d29f46SMauricio Vasquez B 1004db20fd2bSAlexei Starovoitov /* last field in 'union bpf_attr' used by this command */ 100596049f3aSAlexei Starovoitov #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags 1006db20fd2bSAlexei Starovoitov 1007db20fd2bSAlexei Starovoitov static int map_lookup_elem(union bpf_attr *attr) 1008db20fd2bSAlexei Starovoitov { 1009535e7b4bSMickaël Salaün void __user *ukey = u64_to_user_ptr(attr->key); 1010535e7b4bSMickaël Salaün void __user *uvalue = u64_to_user_ptr(attr->value); 1011db20fd2bSAlexei Starovoitov int ufd = attr->map_fd; 1012db20fd2bSAlexei Starovoitov struct bpf_map *map; 101315c14a3dSBrian Vazquez void *key, *value; 101415a07b33SAlexei Starovoitov u32 value_size; 1015592867bfSDaniel Borkmann struct fd f; 1016db20fd2bSAlexei Starovoitov int err; 1017db20fd2bSAlexei Starovoitov 1018db20fd2bSAlexei Starovoitov if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) 1019db20fd2bSAlexei Starovoitov return -EINVAL; 1020db20fd2bSAlexei Starovoitov 102196049f3aSAlexei Starovoitov if (attr->flags & ~BPF_F_LOCK) 102296049f3aSAlexei Starovoitov return -EINVAL; 102396049f3aSAlexei Starovoitov 1024592867bfSDaniel Borkmann f = fdget(ufd); 1025c2101297SDaniel Borkmann map = __bpf_map_get(f); 1026db20fd2bSAlexei Starovoitov if (IS_ERR(map)) 1027db20fd2bSAlexei Starovoitov return PTR_ERR(map); 102887df15deSDaniel Borkmann if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { 10296e71b04aSChenbo Feng err = -EPERM; 10306e71b04aSChenbo Feng goto err_put; 10316e71b04aSChenbo Feng } 10326e71b04aSChenbo Feng 103396049f3aSAlexei Starovoitov if ((attr->flags & BPF_F_LOCK) && 103496049f3aSAlexei Starovoitov !map_value_has_spin_lock(map)) { 103596049f3aSAlexei Starovoitov err = -EINVAL; 103696049f3aSAlexei Starovoitov goto err_put; 103796049f3aSAlexei Starovoitov } 103896049f3aSAlexei Starovoitov 1039c9d29f46SMauricio Vasquez B key = __bpf_copy_key(ukey, map->key_size); 1040e4448ed8SAl Viro if (IS_ERR(key)) { 1041e4448ed8SAl Viro err = PTR_ERR(key); 1042db20fd2bSAlexei Starovoitov goto err_put; 1043e4448ed8SAl Viro } 1044db20fd2bSAlexei Starovoitov 104515c14a3dSBrian Vazquez value_size = bpf_map_value_size(map); 104615a07b33SAlexei Starovoitov 10478ebe667cSAlexei Starovoitov err = -ENOMEM; 104815a07b33SAlexei Starovoitov value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); 1049db20fd2bSAlexei Starovoitov if (!value) 10508ebe667cSAlexei Starovoitov goto free_key; 10518ebe667cSAlexei Starovoitov 105215c14a3dSBrian Vazquez err = bpf_map_copy_value(map, key, value, attr->flags); 105315a07b33SAlexei Starovoitov if (err) 10548ebe667cSAlexei Starovoitov goto free_value; 1055db20fd2bSAlexei Starovoitov 1056db20fd2bSAlexei Starovoitov err = -EFAULT; 105715a07b33SAlexei Starovoitov if (copy_to_user(uvalue, value, value_size) != 0) 10588ebe667cSAlexei Starovoitov goto free_value; 1059db20fd2bSAlexei Starovoitov 1060db20fd2bSAlexei Starovoitov err = 0; 1061db20fd2bSAlexei Starovoitov 10628ebe667cSAlexei Starovoitov free_value: 10638ebe667cSAlexei Starovoitov kfree(value); 1064db20fd2bSAlexei Starovoitov free_key: 1065db20fd2bSAlexei Starovoitov kfree(key); 1066db20fd2bSAlexei Starovoitov err_put: 1067db20fd2bSAlexei Starovoitov fdput(f); 1068db20fd2bSAlexei Starovoitov return err; 1069db20fd2bSAlexei Starovoitov } 1070db20fd2bSAlexei Starovoitov 10711ae80cf3SDaniel Colascione 10723274f520SAlexei Starovoitov #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags 1073db20fd2bSAlexei Starovoitov 1074db20fd2bSAlexei Starovoitov static int map_update_elem(union bpf_attr *attr) 1075db20fd2bSAlexei Starovoitov { 1076535e7b4bSMickaël Salaün void __user *ukey = u64_to_user_ptr(attr->key); 1077535e7b4bSMickaël Salaün void __user *uvalue = u64_to_user_ptr(attr->value); 1078db20fd2bSAlexei Starovoitov int ufd = attr->map_fd; 1079db20fd2bSAlexei Starovoitov struct bpf_map *map; 1080db20fd2bSAlexei Starovoitov void *key, *value; 108115a07b33SAlexei Starovoitov u32 value_size; 1082592867bfSDaniel Borkmann struct fd f; 1083db20fd2bSAlexei Starovoitov int err; 1084db20fd2bSAlexei Starovoitov 1085db20fd2bSAlexei Starovoitov if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) 1086db20fd2bSAlexei Starovoitov return -EINVAL; 1087db20fd2bSAlexei Starovoitov 1088592867bfSDaniel Borkmann f = fdget(ufd); 1089c2101297SDaniel Borkmann map = __bpf_map_get(f); 1090db20fd2bSAlexei Starovoitov if (IS_ERR(map)) 1091db20fd2bSAlexei Starovoitov return PTR_ERR(map); 109287df15deSDaniel Borkmann if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 10936e71b04aSChenbo Feng err = -EPERM; 10946e71b04aSChenbo Feng goto err_put; 10956e71b04aSChenbo Feng } 10966e71b04aSChenbo Feng 109796049f3aSAlexei Starovoitov if ((attr->flags & BPF_F_LOCK) && 109896049f3aSAlexei Starovoitov !map_value_has_spin_lock(map)) { 109996049f3aSAlexei Starovoitov err = -EINVAL; 110096049f3aSAlexei Starovoitov goto err_put; 110196049f3aSAlexei Starovoitov } 110296049f3aSAlexei Starovoitov 1103c9d29f46SMauricio Vasquez B key = __bpf_copy_key(ukey, map->key_size); 1104e4448ed8SAl Viro if (IS_ERR(key)) { 1105e4448ed8SAl Viro err = PTR_ERR(key); 1106db20fd2bSAlexei Starovoitov goto err_put; 1107e4448ed8SAl Viro } 1108db20fd2bSAlexei Starovoitov 110915a07b33SAlexei Starovoitov if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 11108f844938SMartin KaFai Lau map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || 1111b741f163SRoman Gushchin map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || 1112b741f163SRoman Gushchin map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) 111315a07b33SAlexei Starovoitov value_size = round_up(map->value_size, 8) * num_possible_cpus(); 111415a07b33SAlexei Starovoitov else 111515a07b33SAlexei Starovoitov value_size = map->value_size; 111615a07b33SAlexei Starovoitov 1117db20fd2bSAlexei Starovoitov err = -ENOMEM; 111815a07b33SAlexei Starovoitov value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); 1119db20fd2bSAlexei Starovoitov if (!value) 1120db20fd2bSAlexei Starovoitov goto free_key; 1121db20fd2bSAlexei Starovoitov 1122db20fd2bSAlexei Starovoitov err = -EFAULT; 112315a07b33SAlexei Starovoitov if (copy_from_user(value, uvalue, value_size) != 0) 1124db20fd2bSAlexei Starovoitov goto free_value; 1125db20fd2bSAlexei Starovoitov 112615c14a3dSBrian Vazquez err = bpf_map_update_value(map, f, key, value, attr->flags); 11276710e112SJesper Dangaard Brouer 1128db20fd2bSAlexei Starovoitov free_value: 1129db20fd2bSAlexei Starovoitov kfree(value); 1130db20fd2bSAlexei Starovoitov free_key: 1131db20fd2bSAlexei Starovoitov kfree(key); 1132db20fd2bSAlexei Starovoitov err_put: 1133db20fd2bSAlexei Starovoitov fdput(f); 1134db20fd2bSAlexei Starovoitov return err; 1135db20fd2bSAlexei Starovoitov } 1136db20fd2bSAlexei Starovoitov 1137db20fd2bSAlexei Starovoitov #define BPF_MAP_DELETE_ELEM_LAST_FIELD key 1138db20fd2bSAlexei Starovoitov 1139db20fd2bSAlexei Starovoitov static int map_delete_elem(union bpf_attr *attr) 1140db20fd2bSAlexei Starovoitov { 1141535e7b4bSMickaël Salaün void __user *ukey = u64_to_user_ptr(attr->key); 1142db20fd2bSAlexei Starovoitov int ufd = attr->map_fd; 1143db20fd2bSAlexei Starovoitov struct bpf_map *map; 1144592867bfSDaniel Borkmann struct fd f; 1145db20fd2bSAlexei Starovoitov void *key; 1146db20fd2bSAlexei Starovoitov int err; 1147db20fd2bSAlexei Starovoitov 1148db20fd2bSAlexei Starovoitov if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) 1149db20fd2bSAlexei Starovoitov return -EINVAL; 1150db20fd2bSAlexei Starovoitov 1151592867bfSDaniel Borkmann f = fdget(ufd); 1152c2101297SDaniel Borkmann map = __bpf_map_get(f); 1153db20fd2bSAlexei Starovoitov if (IS_ERR(map)) 1154db20fd2bSAlexei Starovoitov return PTR_ERR(map); 115587df15deSDaniel Borkmann if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 11566e71b04aSChenbo Feng err = -EPERM; 11576e71b04aSChenbo Feng goto err_put; 11586e71b04aSChenbo Feng } 11596e71b04aSChenbo Feng 1160c9d29f46SMauricio Vasquez B key = __bpf_copy_key(ukey, map->key_size); 1161e4448ed8SAl Viro if (IS_ERR(key)) { 1162e4448ed8SAl Viro err = PTR_ERR(key); 1163db20fd2bSAlexei Starovoitov goto err_put; 1164e4448ed8SAl Viro } 1165db20fd2bSAlexei Starovoitov 1166a3884572SJakub Kicinski if (bpf_map_is_dev_bound(map)) { 1167a3884572SJakub Kicinski err = bpf_map_offload_delete_elem(map, key); 1168a3884572SJakub Kicinski goto out; 116985d33df3SMartin KaFai Lau } else if (IS_FD_PROG_ARRAY(map) || 117085d33df3SMartin KaFai Lau map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { 117185d33df3SMartin KaFai Lau /* These maps require sleepable context */ 1172da765a2fSDaniel Borkmann err = map->ops->map_delete_elem(map, key); 1173da765a2fSDaniel Borkmann goto out; 1174a3884572SJakub Kicinski } 1175a3884572SJakub Kicinski 1176b6e5dae1SThomas Gleixner bpf_disable_instrumentation(); 1177db20fd2bSAlexei Starovoitov rcu_read_lock(); 1178db20fd2bSAlexei Starovoitov err = map->ops->map_delete_elem(map, key); 1179db20fd2bSAlexei Starovoitov rcu_read_unlock(); 1180b6e5dae1SThomas Gleixner bpf_enable_instrumentation(); 11811ae80cf3SDaniel Colascione maybe_wait_bpf_programs(map); 1182a3884572SJakub Kicinski out: 1183db20fd2bSAlexei Starovoitov kfree(key); 1184db20fd2bSAlexei Starovoitov err_put: 1185db20fd2bSAlexei Starovoitov fdput(f); 1186db20fd2bSAlexei Starovoitov return err; 1187db20fd2bSAlexei Starovoitov } 1188db20fd2bSAlexei Starovoitov 1189db20fd2bSAlexei Starovoitov /* last field in 'union bpf_attr' used by this command */ 1190db20fd2bSAlexei Starovoitov #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key 1191db20fd2bSAlexei Starovoitov 1192db20fd2bSAlexei Starovoitov static int map_get_next_key(union bpf_attr *attr) 1193db20fd2bSAlexei Starovoitov { 1194535e7b4bSMickaël Salaün void __user *ukey = u64_to_user_ptr(attr->key); 1195535e7b4bSMickaël Salaün void __user *unext_key = u64_to_user_ptr(attr->next_key); 1196db20fd2bSAlexei Starovoitov int ufd = attr->map_fd; 1197db20fd2bSAlexei Starovoitov struct bpf_map *map; 1198db20fd2bSAlexei Starovoitov void *key, *next_key; 1199592867bfSDaniel Borkmann struct fd f; 1200db20fd2bSAlexei Starovoitov int err; 1201db20fd2bSAlexei Starovoitov 1202db20fd2bSAlexei Starovoitov if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) 1203db20fd2bSAlexei Starovoitov return -EINVAL; 1204db20fd2bSAlexei Starovoitov 1205592867bfSDaniel Borkmann f = fdget(ufd); 1206c2101297SDaniel Borkmann map = __bpf_map_get(f); 1207db20fd2bSAlexei Starovoitov if (IS_ERR(map)) 1208db20fd2bSAlexei Starovoitov return PTR_ERR(map); 120987df15deSDaniel Borkmann if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { 12106e71b04aSChenbo Feng err = -EPERM; 12116e71b04aSChenbo Feng goto err_put; 12126e71b04aSChenbo Feng } 12136e71b04aSChenbo Feng 12148fe45924STeng Qin if (ukey) { 1215c9d29f46SMauricio Vasquez B key = __bpf_copy_key(ukey, map->key_size); 1216e4448ed8SAl Viro if (IS_ERR(key)) { 1217e4448ed8SAl Viro err = PTR_ERR(key); 1218db20fd2bSAlexei Starovoitov goto err_put; 1219e4448ed8SAl Viro } 12208fe45924STeng Qin } else { 12218fe45924STeng Qin key = NULL; 12228fe45924STeng Qin } 1223db20fd2bSAlexei Starovoitov 1224db20fd2bSAlexei Starovoitov err = -ENOMEM; 1225db20fd2bSAlexei Starovoitov next_key = kmalloc(map->key_size, GFP_USER); 1226db20fd2bSAlexei Starovoitov if (!next_key) 1227db20fd2bSAlexei Starovoitov goto free_key; 1228db20fd2bSAlexei Starovoitov 1229a3884572SJakub Kicinski if (bpf_map_is_dev_bound(map)) { 1230a3884572SJakub Kicinski err = bpf_map_offload_get_next_key(map, key, next_key); 1231a3884572SJakub Kicinski goto out; 1232a3884572SJakub Kicinski } 1233a3884572SJakub Kicinski 1234db20fd2bSAlexei Starovoitov rcu_read_lock(); 1235db20fd2bSAlexei Starovoitov err = map->ops->map_get_next_key(map, key, next_key); 1236db20fd2bSAlexei Starovoitov rcu_read_unlock(); 1237a3884572SJakub Kicinski out: 1238db20fd2bSAlexei Starovoitov if (err) 1239db20fd2bSAlexei Starovoitov goto free_next_key; 1240db20fd2bSAlexei Starovoitov 1241db20fd2bSAlexei Starovoitov err = -EFAULT; 1242db20fd2bSAlexei Starovoitov if (copy_to_user(unext_key, next_key, map->key_size) != 0) 1243db20fd2bSAlexei Starovoitov goto free_next_key; 1244db20fd2bSAlexei Starovoitov 1245db20fd2bSAlexei Starovoitov err = 0; 1246db20fd2bSAlexei Starovoitov 1247db20fd2bSAlexei Starovoitov free_next_key: 1248db20fd2bSAlexei Starovoitov kfree(next_key); 1249db20fd2bSAlexei Starovoitov free_key: 1250db20fd2bSAlexei Starovoitov kfree(key); 1251db20fd2bSAlexei Starovoitov err_put: 1252db20fd2bSAlexei Starovoitov fdput(f); 1253db20fd2bSAlexei Starovoitov return err; 1254db20fd2bSAlexei Starovoitov } 1255db20fd2bSAlexei Starovoitov 1256aa2e93b8SBrian Vazquez int generic_map_delete_batch(struct bpf_map *map, 1257aa2e93b8SBrian Vazquez const union bpf_attr *attr, 1258aa2e93b8SBrian Vazquez union bpf_attr __user *uattr) 1259aa2e93b8SBrian Vazquez { 1260aa2e93b8SBrian Vazquez void __user *keys = u64_to_user_ptr(attr->batch.keys); 1261aa2e93b8SBrian Vazquez u32 cp, max_count; 1262aa2e93b8SBrian Vazquez int err = 0; 1263aa2e93b8SBrian Vazquez void *key; 1264aa2e93b8SBrian Vazquez 1265aa2e93b8SBrian Vazquez if (attr->batch.elem_flags & ~BPF_F_LOCK) 1266aa2e93b8SBrian Vazquez return -EINVAL; 1267aa2e93b8SBrian Vazquez 1268aa2e93b8SBrian Vazquez if ((attr->batch.elem_flags & BPF_F_LOCK) && 1269aa2e93b8SBrian Vazquez !map_value_has_spin_lock(map)) { 1270aa2e93b8SBrian Vazquez return -EINVAL; 1271aa2e93b8SBrian Vazquez } 1272aa2e93b8SBrian Vazquez 1273aa2e93b8SBrian Vazquez max_count = attr->batch.count; 1274aa2e93b8SBrian Vazquez if (!max_count) 1275aa2e93b8SBrian Vazquez return 0; 1276aa2e93b8SBrian Vazquez 12772e3a94aaSBrian Vazquez key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN); 12782e3a94aaSBrian Vazquez if (!key) 12792e3a94aaSBrian Vazquez return -ENOMEM; 12802e3a94aaSBrian Vazquez 1281aa2e93b8SBrian Vazquez for (cp = 0; cp < max_count; cp++) { 12822e3a94aaSBrian Vazquez err = -EFAULT; 12832e3a94aaSBrian Vazquez if (copy_from_user(key, keys + cp * map->key_size, 12842e3a94aaSBrian Vazquez map->key_size)) 1285aa2e93b8SBrian Vazquez break; 1286aa2e93b8SBrian Vazquez 1287aa2e93b8SBrian Vazquez if (bpf_map_is_dev_bound(map)) { 1288aa2e93b8SBrian Vazquez err = bpf_map_offload_delete_elem(map, key); 1289aa2e93b8SBrian Vazquez break; 1290aa2e93b8SBrian Vazquez } 1291aa2e93b8SBrian Vazquez 1292b6e5dae1SThomas Gleixner bpf_disable_instrumentation(); 1293aa2e93b8SBrian Vazquez rcu_read_lock(); 1294aa2e93b8SBrian Vazquez err = map->ops->map_delete_elem(map, key); 1295aa2e93b8SBrian Vazquez rcu_read_unlock(); 1296b6e5dae1SThomas Gleixner bpf_enable_instrumentation(); 1297aa2e93b8SBrian Vazquez maybe_wait_bpf_programs(map); 1298aa2e93b8SBrian Vazquez if (err) 1299aa2e93b8SBrian Vazquez break; 1300aa2e93b8SBrian Vazquez } 1301aa2e93b8SBrian Vazquez if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) 1302aa2e93b8SBrian Vazquez err = -EFAULT; 13032e3a94aaSBrian Vazquez 13042e3a94aaSBrian Vazquez kfree(key); 1305aa2e93b8SBrian Vazquez return err; 1306aa2e93b8SBrian Vazquez } 1307aa2e93b8SBrian Vazquez 1308aa2e93b8SBrian Vazquez int generic_map_update_batch(struct bpf_map *map, 1309aa2e93b8SBrian Vazquez const union bpf_attr *attr, 1310aa2e93b8SBrian Vazquez union bpf_attr __user *uattr) 1311aa2e93b8SBrian Vazquez { 1312aa2e93b8SBrian Vazquez void __user *values = u64_to_user_ptr(attr->batch.values); 1313aa2e93b8SBrian Vazquez void __user *keys = u64_to_user_ptr(attr->batch.keys); 1314aa2e93b8SBrian Vazquez u32 value_size, cp, max_count; 1315aa2e93b8SBrian Vazquez int ufd = attr->map_fd; 1316aa2e93b8SBrian Vazquez void *key, *value; 1317aa2e93b8SBrian Vazquez struct fd f; 1318aa2e93b8SBrian Vazquez int err = 0; 1319aa2e93b8SBrian Vazquez 1320aa2e93b8SBrian Vazquez f = fdget(ufd); 1321aa2e93b8SBrian Vazquez if (attr->batch.elem_flags & ~BPF_F_LOCK) 1322aa2e93b8SBrian Vazquez return -EINVAL; 1323aa2e93b8SBrian Vazquez 1324aa2e93b8SBrian Vazquez if ((attr->batch.elem_flags & BPF_F_LOCK) && 1325aa2e93b8SBrian Vazquez !map_value_has_spin_lock(map)) { 1326aa2e93b8SBrian Vazquez return -EINVAL; 1327aa2e93b8SBrian Vazquez } 1328aa2e93b8SBrian Vazquez 1329aa2e93b8SBrian Vazquez value_size = bpf_map_value_size(map); 1330aa2e93b8SBrian Vazquez 1331aa2e93b8SBrian Vazquez max_count = attr->batch.count; 1332aa2e93b8SBrian Vazquez if (!max_count) 1333aa2e93b8SBrian Vazquez return 0; 1334aa2e93b8SBrian Vazquez 13352e3a94aaSBrian Vazquez key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN); 13362e3a94aaSBrian Vazquez if (!key) 1337aa2e93b8SBrian Vazquez return -ENOMEM; 1338aa2e93b8SBrian Vazquez 13392e3a94aaSBrian Vazquez value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); 13402e3a94aaSBrian Vazquez if (!value) { 13412e3a94aaSBrian Vazquez kfree(key); 13422e3a94aaSBrian Vazquez return -ENOMEM; 1343aa2e93b8SBrian Vazquez } 13442e3a94aaSBrian Vazquez 13452e3a94aaSBrian Vazquez for (cp = 0; cp < max_count; cp++) { 1346aa2e93b8SBrian Vazquez err = -EFAULT; 13472e3a94aaSBrian Vazquez if (copy_from_user(key, keys + cp * map->key_size, 13482e3a94aaSBrian Vazquez map->key_size) || 13492e3a94aaSBrian Vazquez copy_from_user(value, values + cp * value_size, value_size)) 1350aa2e93b8SBrian Vazquez break; 1351aa2e93b8SBrian Vazquez 1352aa2e93b8SBrian Vazquez err = bpf_map_update_value(map, f, key, value, 1353aa2e93b8SBrian Vazquez attr->batch.elem_flags); 1354aa2e93b8SBrian Vazquez 1355aa2e93b8SBrian Vazquez if (err) 1356aa2e93b8SBrian Vazquez break; 1357aa2e93b8SBrian Vazquez } 1358aa2e93b8SBrian Vazquez 1359aa2e93b8SBrian Vazquez if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) 1360aa2e93b8SBrian Vazquez err = -EFAULT; 1361aa2e93b8SBrian Vazquez 1362aa2e93b8SBrian Vazquez kfree(value); 1363aa2e93b8SBrian Vazquez kfree(key); 1364aa2e93b8SBrian Vazquez return err; 1365aa2e93b8SBrian Vazquez } 1366aa2e93b8SBrian Vazquez 1367cb4d03abSBrian Vazquez #define MAP_LOOKUP_RETRIES 3 1368cb4d03abSBrian Vazquez 1369cb4d03abSBrian Vazquez int generic_map_lookup_batch(struct bpf_map *map, 1370cb4d03abSBrian Vazquez const union bpf_attr *attr, 1371cb4d03abSBrian Vazquez union bpf_attr __user *uattr) 1372cb4d03abSBrian Vazquez { 1373cb4d03abSBrian Vazquez void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch); 1374cb4d03abSBrian Vazquez void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch); 1375cb4d03abSBrian Vazquez void __user *values = u64_to_user_ptr(attr->batch.values); 1376cb4d03abSBrian Vazquez void __user *keys = u64_to_user_ptr(attr->batch.keys); 1377cb4d03abSBrian Vazquez void *buf, *buf_prevkey, *prev_key, *key, *value; 1378cb4d03abSBrian Vazquez int err, retry = MAP_LOOKUP_RETRIES; 1379cb4d03abSBrian Vazquez u32 value_size, cp, max_count; 1380cb4d03abSBrian Vazquez 1381cb4d03abSBrian Vazquez if (attr->batch.elem_flags & ~BPF_F_LOCK) 1382cb4d03abSBrian Vazquez return -EINVAL; 1383cb4d03abSBrian Vazquez 1384cb4d03abSBrian Vazquez if ((attr->batch.elem_flags & BPF_F_LOCK) && 1385cb4d03abSBrian Vazquez !map_value_has_spin_lock(map)) 1386cb4d03abSBrian Vazquez return -EINVAL; 1387cb4d03abSBrian Vazquez 1388cb4d03abSBrian Vazquez value_size = bpf_map_value_size(map); 1389cb4d03abSBrian Vazquez 1390cb4d03abSBrian Vazquez max_count = attr->batch.count; 1391cb4d03abSBrian Vazquez if (!max_count) 1392cb4d03abSBrian Vazquez return 0; 1393cb4d03abSBrian Vazquez 1394cb4d03abSBrian Vazquez if (put_user(0, &uattr->batch.count)) 1395cb4d03abSBrian Vazquez return -EFAULT; 1396cb4d03abSBrian Vazquez 1397cb4d03abSBrian Vazquez buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN); 1398cb4d03abSBrian Vazquez if (!buf_prevkey) 1399cb4d03abSBrian Vazquez return -ENOMEM; 1400cb4d03abSBrian Vazquez 1401cb4d03abSBrian Vazquez buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN); 1402cb4d03abSBrian Vazquez if (!buf) { 1403bb2359f4SDenis Efremov kfree(buf_prevkey); 1404cb4d03abSBrian Vazquez return -ENOMEM; 1405cb4d03abSBrian Vazquez } 1406cb4d03abSBrian Vazquez 1407cb4d03abSBrian Vazquez err = -EFAULT; 1408cb4d03abSBrian Vazquez prev_key = NULL; 1409cb4d03abSBrian Vazquez if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size)) 1410cb4d03abSBrian Vazquez goto free_buf; 1411cb4d03abSBrian Vazquez key = buf; 1412cb4d03abSBrian Vazquez value = key + map->key_size; 1413cb4d03abSBrian Vazquez if (ubatch) 1414cb4d03abSBrian Vazquez prev_key = buf_prevkey; 1415cb4d03abSBrian Vazquez 1416cb4d03abSBrian Vazquez for (cp = 0; cp < max_count;) { 1417cb4d03abSBrian Vazquez rcu_read_lock(); 1418cb4d03abSBrian Vazquez err = map->ops->map_get_next_key(map, prev_key, key); 1419cb4d03abSBrian Vazquez rcu_read_unlock(); 1420cb4d03abSBrian Vazquez if (err) 1421cb4d03abSBrian Vazquez break; 1422cb4d03abSBrian Vazquez err = bpf_map_copy_value(map, key, value, 1423cb4d03abSBrian Vazquez attr->batch.elem_flags); 1424cb4d03abSBrian Vazquez 1425cb4d03abSBrian Vazquez if (err == -ENOENT) { 1426cb4d03abSBrian Vazquez if (retry) { 1427cb4d03abSBrian Vazquez retry--; 1428cb4d03abSBrian Vazquez continue; 1429cb4d03abSBrian Vazquez } 1430cb4d03abSBrian Vazquez err = -EINTR; 1431cb4d03abSBrian Vazquez break; 1432cb4d03abSBrian Vazquez } 1433cb4d03abSBrian Vazquez 1434cb4d03abSBrian Vazquez if (err) 1435cb4d03abSBrian Vazquez goto free_buf; 1436cb4d03abSBrian Vazquez 1437cb4d03abSBrian Vazquez if (copy_to_user(keys + cp * map->key_size, key, 1438cb4d03abSBrian Vazquez map->key_size)) { 1439cb4d03abSBrian Vazquez err = -EFAULT; 1440cb4d03abSBrian Vazquez goto free_buf; 1441cb4d03abSBrian Vazquez } 1442cb4d03abSBrian Vazquez if (copy_to_user(values + cp * value_size, value, value_size)) { 1443cb4d03abSBrian Vazquez err = -EFAULT; 1444cb4d03abSBrian Vazquez goto free_buf; 1445cb4d03abSBrian Vazquez } 1446cb4d03abSBrian Vazquez 1447cb4d03abSBrian Vazquez if (!prev_key) 1448cb4d03abSBrian Vazquez prev_key = buf_prevkey; 1449cb4d03abSBrian Vazquez 1450cb4d03abSBrian Vazquez swap(prev_key, key); 1451cb4d03abSBrian Vazquez retry = MAP_LOOKUP_RETRIES; 1452cb4d03abSBrian Vazquez cp++; 1453cb4d03abSBrian Vazquez } 1454cb4d03abSBrian Vazquez 1455cb4d03abSBrian Vazquez if (err == -EFAULT) 1456cb4d03abSBrian Vazquez goto free_buf; 1457cb4d03abSBrian Vazquez 1458cb4d03abSBrian Vazquez if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) || 1459cb4d03abSBrian Vazquez (cp && copy_to_user(uobatch, prev_key, map->key_size)))) 1460cb4d03abSBrian Vazquez err = -EFAULT; 1461cb4d03abSBrian Vazquez 1462cb4d03abSBrian Vazquez free_buf: 1463cb4d03abSBrian Vazquez kfree(buf_prevkey); 1464cb4d03abSBrian Vazquez kfree(buf); 1465cb4d03abSBrian Vazquez return err; 1466cb4d03abSBrian Vazquez } 1467cb4d03abSBrian Vazquez 1468bd513cd0SMauricio Vasquez B #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value 1469bd513cd0SMauricio Vasquez B 1470bd513cd0SMauricio Vasquez B static int map_lookup_and_delete_elem(union bpf_attr *attr) 1471bd513cd0SMauricio Vasquez B { 1472bd513cd0SMauricio Vasquez B void __user *ukey = u64_to_user_ptr(attr->key); 1473bd513cd0SMauricio Vasquez B void __user *uvalue = u64_to_user_ptr(attr->value); 1474bd513cd0SMauricio Vasquez B int ufd = attr->map_fd; 1475bd513cd0SMauricio Vasquez B struct bpf_map *map; 1476540fefc0SAlexei Starovoitov void *key, *value; 1477bd513cd0SMauricio Vasquez B u32 value_size; 1478bd513cd0SMauricio Vasquez B struct fd f; 1479bd513cd0SMauricio Vasquez B int err; 1480bd513cd0SMauricio Vasquez B 1481bd513cd0SMauricio Vasquez B if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM)) 1482bd513cd0SMauricio Vasquez B return -EINVAL; 1483bd513cd0SMauricio Vasquez B 1484bd513cd0SMauricio Vasquez B f = fdget(ufd); 1485bd513cd0SMauricio Vasquez B map = __bpf_map_get(f); 1486bd513cd0SMauricio Vasquez B if (IS_ERR(map)) 1487bd513cd0SMauricio Vasquez B return PTR_ERR(map); 14881ea0f912SAnton Protopopov if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) || 14891ea0f912SAnton Protopopov !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 1490bd513cd0SMauricio Vasquez B err = -EPERM; 1491bd513cd0SMauricio Vasquez B goto err_put; 1492bd513cd0SMauricio Vasquez B } 1493bd513cd0SMauricio Vasquez B 1494bd513cd0SMauricio Vasquez B key = __bpf_copy_key(ukey, map->key_size); 1495bd513cd0SMauricio Vasquez B if (IS_ERR(key)) { 1496bd513cd0SMauricio Vasquez B err = PTR_ERR(key); 1497bd513cd0SMauricio Vasquez B goto err_put; 1498bd513cd0SMauricio Vasquez B } 1499bd513cd0SMauricio Vasquez B 1500bd513cd0SMauricio Vasquez B value_size = map->value_size; 1501bd513cd0SMauricio Vasquez B 1502bd513cd0SMauricio Vasquez B err = -ENOMEM; 1503bd513cd0SMauricio Vasquez B value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); 1504bd513cd0SMauricio Vasquez B if (!value) 1505bd513cd0SMauricio Vasquez B goto free_key; 1506bd513cd0SMauricio Vasquez B 1507bd513cd0SMauricio Vasquez B if (map->map_type == BPF_MAP_TYPE_QUEUE || 1508bd513cd0SMauricio Vasquez B map->map_type == BPF_MAP_TYPE_STACK) { 1509bd513cd0SMauricio Vasquez B err = map->ops->map_pop_elem(map, value); 1510bd513cd0SMauricio Vasquez B } else { 1511bd513cd0SMauricio Vasquez B err = -ENOTSUPP; 1512bd513cd0SMauricio Vasquez B } 1513bd513cd0SMauricio Vasquez B 1514bd513cd0SMauricio Vasquez B if (err) 1515bd513cd0SMauricio Vasquez B goto free_value; 1516bd513cd0SMauricio Vasquez B 15177f645462SWei Yongjun if (copy_to_user(uvalue, value, value_size) != 0) { 15187f645462SWei Yongjun err = -EFAULT; 1519bd513cd0SMauricio Vasquez B goto free_value; 15207f645462SWei Yongjun } 1521bd513cd0SMauricio Vasquez B 1522bd513cd0SMauricio Vasquez B err = 0; 1523bd513cd0SMauricio Vasquez B 1524bd513cd0SMauricio Vasquez B free_value: 1525bd513cd0SMauricio Vasquez B kfree(value); 1526bd513cd0SMauricio Vasquez B free_key: 1527bd513cd0SMauricio Vasquez B kfree(key); 1528bd513cd0SMauricio Vasquez B err_put: 1529bd513cd0SMauricio Vasquez B fdput(f); 1530bd513cd0SMauricio Vasquez B return err; 1531bd513cd0SMauricio Vasquez B } 1532bd513cd0SMauricio Vasquez B 153387df15deSDaniel Borkmann #define BPF_MAP_FREEZE_LAST_FIELD map_fd 153487df15deSDaniel Borkmann 153587df15deSDaniel Borkmann static int map_freeze(const union bpf_attr *attr) 153687df15deSDaniel Borkmann { 153787df15deSDaniel Borkmann int err = 0, ufd = attr->map_fd; 153887df15deSDaniel Borkmann struct bpf_map *map; 153987df15deSDaniel Borkmann struct fd f; 154087df15deSDaniel Borkmann 154187df15deSDaniel Borkmann if (CHECK_ATTR(BPF_MAP_FREEZE)) 154287df15deSDaniel Borkmann return -EINVAL; 154387df15deSDaniel Borkmann 154487df15deSDaniel Borkmann f = fdget(ufd); 154587df15deSDaniel Borkmann map = __bpf_map_get(f); 154687df15deSDaniel Borkmann if (IS_ERR(map)) 154787df15deSDaniel Borkmann return PTR_ERR(map); 1548fc970227SAndrii Nakryiko 1549849b4d94SMartin KaFai Lau if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { 1550849b4d94SMartin KaFai Lau fdput(f); 1551849b4d94SMartin KaFai Lau return -ENOTSUPP; 1552849b4d94SMartin KaFai Lau } 1553849b4d94SMartin KaFai Lau 1554fc970227SAndrii Nakryiko mutex_lock(&map->freeze_mutex); 1555fc970227SAndrii Nakryiko 1556fc970227SAndrii Nakryiko if (map->writecnt) { 1557fc970227SAndrii Nakryiko err = -EBUSY; 1558fc970227SAndrii Nakryiko goto err_put; 1559fc970227SAndrii Nakryiko } 156087df15deSDaniel Borkmann if (READ_ONCE(map->frozen)) { 156187df15deSDaniel Borkmann err = -EBUSY; 156287df15deSDaniel Borkmann goto err_put; 156387df15deSDaniel Borkmann } 15642c78ee89SAlexei Starovoitov if (!bpf_capable()) { 156587df15deSDaniel Borkmann err = -EPERM; 156687df15deSDaniel Borkmann goto err_put; 156787df15deSDaniel Borkmann } 156887df15deSDaniel Borkmann 156987df15deSDaniel Borkmann WRITE_ONCE(map->frozen, true); 157087df15deSDaniel Borkmann err_put: 1571fc970227SAndrii Nakryiko mutex_unlock(&map->freeze_mutex); 157287df15deSDaniel Borkmann fdput(f); 157387df15deSDaniel Borkmann return err; 157487df15deSDaniel Borkmann } 157587df15deSDaniel Borkmann 15767de16e3aSJakub Kicinski static const struct bpf_prog_ops * const bpf_prog_types[] = { 157791cc1a99SAlexei Starovoitov #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ 15787de16e3aSJakub Kicinski [_id] = & _name ## _prog_ops, 15797de16e3aSJakub Kicinski #define BPF_MAP_TYPE(_id, _ops) 1580f2e10bffSAndrii Nakryiko #define BPF_LINK_TYPE(_id, _name) 15817de16e3aSJakub Kicinski #include <linux/bpf_types.h> 15827de16e3aSJakub Kicinski #undef BPF_PROG_TYPE 15837de16e3aSJakub Kicinski #undef BPF_MAP_TYPE 1584f2e10bffSAndrii Nakryiko #undef BPF_LINK_TYPE 15857de16e3aSJakub Kicinski }; 15867de16e3aSJakub Kicinski 158709756af4SAlexei Starovoitov static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) 158809756af4SAlexei Starovoitov { 1589d0f1a451SDaniel Borkmann const struct bpf_prog_ops *ops; 1590d0f1a451SDaniel Borkmann 1591d0f1a451SDaniel Borkmann if (type >= ARRAY_SIZE(bpf_prog_types)) 1592d0f1a451SDaniel Borkmann return -EINVAL; 1593d0f1a451SDaniel Borkmann type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types)); 1594d0f1a451SDaniel Borkmann ops = bpf_prog_types[type]; 1595d0f1a451SDaniel Borkmann if (!ops) 1596be9370a7SJohannes Berg return -EINVAL; 159709756af4SAlexei Starovoitov 1598ab3f0063SJakub Kicinski if (!bpf_prog_is_dev_bound(prog->aux)) 1599d0f1a451SDaniel Borkmann prog->aux->ops = ops; 1600ab3f0063SJakub Kicinski else 1601ab3f0063SJakub Kicinski prog->aux->ops = &bpf_offload_prog_ops; 160224701eceSDaniel Borkmann prog->type = type; 160309756af4SAlexei Starovoitov return 0; 160409756af4SAlexei Starovoitov } 160509756af4SAlexei Starovoitov 1606bae141f5SDaniel Borkmann enum bpf_audit { 1607bae141f5SDaniel Borkmann BPF_AUDIT_LOAD, 1608bae141f5SDaniel Borkmann BPF_AUDIT_UNLOAD, 1609bae141f5SDaniel Borkmann BPF_AUDIT_MAX, 1610bae141f5SDaniel Borkmann }; 1611bae141f5SDaniel Borkmann 1612bae141f5SDaniel Borkmann static const char * const bpf_audit_str[BPF_AUDIT_MAX] = { 1613bae141f5SDaniel Borkmann [BPF_AUDIT_LOAD] = "LOAD", 1614bae141f5SDaniel Borkmann [BPF_AUDIT_UNLOAD] = "UNLOAD", 1615bae141f5SDaniel Borkmann }; 1616bae141f5SDaniel Borkmann 1617bae141f5SDaniel Borkmann static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op) 1618bae141f5SDaniel Borkmann { 1619bae141f5SDaniel Borkmann struct audit_context *ctx = NULL; 1620bae141f5SDaniel Borkmann struct audit_buffer *ab; 1621bae141f5SDaniel Borkmann 1622bae141f5SDaniel Borkmann if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX)) 1623bae141f5SDaniel Borkmann return; 1624bae141f5SDaniel Borkmann if (audit_enabled == AUDIT_OFF) 1625bae141f5SDaniel Borkmann return; 1626bae141f5SDaniel Borkmann if (op == BPF_AUDIT_LOAD) 1627bae141f5SDaniel Borkmann ctx = audit_context(); 1628bae141f5SDaniel Borkmann ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF); 1629bae141f5SDaniel Borkmann if (unlikely(!ab)) 1630bae141f5SDaniel Borkmann return; 1631bae141f5SDaniel Borkmann audit_log_format(ab, "prog-id=%u op=%s", 1632bae141f5SDaniel Borkmann prog->aux->id, bpf_audit_str[op]); 1633bae141f5SDaniel Borkmann audit_log_end(ab); 1634bae141f5SDaniel Borkmann } 1635bae141f5SDaniel Borkmann 16365ccb071eSDaniel Borkmann int __bpf_prog_charge(struct user_struct *user, u32 pages) 16375ccb071eSDaniel Borkmann { 16385ccb071eSDaniel Borkmann unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 16395ccb071eSDaniel Borkmann unsigned long user_bufs; 16405ccb071eSDaniel Borkmann 16415ccb071eSDaniel Borkmann if (user) { 16425ccb071eSDaniel Borkmann user_bufs = atomic_long_add_return(pages, &user->locked_vm); 16435ccb071eSDaniel Borkmann if (user_bufs > memlock_limit) { 16445ccb071eSDaniel Borkmann atomic_long_sub(pages, &user->locked_vm); 16455ccb071eSDaniel Borkmann return -EPERM; 16465ccb071eSDaniel Borkmann } 16475ccb071eSDaniel Borkmann } 16485ccb071eSDaniel Borkmann 16495ccb071eSDaniel Borkmann return 0; 16505ccb071eSDaniel Borkmann } 16515ccb071eSDaniel Borkmann 16525ccb071eSDaniel Borkmann void __bpf_prog_uncharge(struct user_struct *user, u32 pages) 16535ccb071eSDaniel Borkmann { 16545ccb071eSDaniel Borkmann if (user) 16555ccb071eSDaniel Borkmann atomic_long_sub(pages, &user->locked_vm); 16565ccb071eSDaniel Borkmann } 16575ccb071eSDaniel Borkmann 1658aaac3ba9SAlexei Starovoitov static int bpf_prog_charge_memlock(struct bpf_prog *prog) 1659aaac3ba9SAlexei Starovoitov { 1660aaac3ba9SAlexei Starovoitov struct user_struct *user = get_current_user(); 16615ccb071eSDaniel Borkmann int ret; 1662aaac3ba9SAlexei Starovoitov 16635ccb071eSDaniel Borkmann ret = __bpf_prog_charge(user, prog->pages); 16645ccb071eSDaniel Borkmann if (ret) { 1665aaac3ba9SAlexei Starovoitov free_uid(user); 16665ccb071eSDaniel Borkmann return ret; 1667aaac3ba9SAlexei Starovoitov } 16685ccb071eSDaniel Borkmann 1669aaac3ba9SAlexei Starovoitov prog->aux->user = user; 1670aaac3ba9SAlexei Starovoitov return 0; 1671aaac3ba9SAlexei Starovoitov } 1672aaac3ba9SAlexei Starovoitov 1673aaac3ba9SAlexei Starovoitov static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) 1674aaac3ba9SAlexei Starovoitov { 1675aaac3ba9SAlexei Starovoitov struct user_struct *user = prog->aux->user; 1676aaac3ba9SAlexei Starovoitov 16775ccb071eSDaniel Borkmann __bpf_prog_uncharge(user, prog->pages); 1678aaac3ba9SAlexei Starovoitov free_uid(user); 1679aaac3ba9SAlexei Starovoitov } 1680aaac3ba9SAlexei Starovoitov 1681dc4bb0e2SMartin KaFai Lau static int bpf_prog_alloc_id(struct bpf_prog *prog) 1682dc4bb0e2SMartin KaFai Lau { 1683dc4bb0e2SMartin KaFai Lau int id; 1684dc4bb0e2SMartin KaFai Lau 1685b76354cdSShaohua Li idr_preload(GFP_KERNEL); 1686dc4bb0e2SMartin KaFai Lau spin_lock_bh(&prog_idr_lock); 1687dc4bb0e2SMartin KaFai Lau id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); 1688dc4bb0e2SMartin KaFai Lau if (id > 0) 1689dc4bb0e2SMartin KaFai Lau prog->aux->id = id; 1690dc4bb0e2SMartin KaFai Lau spin_unlock_bh(&prog_idr_lock); 1691b76354cdSShaohua Li idr_preload_end(); 1692dc4bb0e2SMartin KaFai Lau 1693dc4bb0e2SMartin KaFai Lau /* id is in [1, INT_MAX) */ 1694dc4bb0e2SMartin KaFai Lau if (WARN_ON_ONCE(!id)) 1695dc4bb0e2SMartin KaFai Lau return -ENOSPC; 1696dc4bb0e2SMartin KaFai Lau 1697dc4bb0e2SMartin KaFai Lau return id > 0 ? 0 : id; 1698dc4bb0e2SMartin KaFai Lau } 1699dc4bb0e2SMartin KaFai Lau 1700ad8ad79fSJakub Kicinski void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock) 1701dc4bb0e2SMartin KaFai Lau { 1702ad8ad79fSJakub Kicinski /* cBPF to eBPF migrations are currently not in the idr store. 1703ad8ad79fSJakub Kicinski * Offloaded programs are removed from the store when their device 1704ad8ad79fSJakub Kicinski * disappears - even if someone grabs an fd to them they are unusable, 1705ad8ad79fSJakub Kicinski * simply waiting for refcnt to drop to be freed. 1706ad8ad79fSJakub Kicinski */ 1707dc4bb0e2SMartin KaFai Lau if (!prog->aux->id) 1708dc4bb0e2SMartin KaFai Lau return; 1709dc4bb0e2SMartin KaFai Lau 1710b16d9aa4SMartin KaFai Lau if (do_idr_lock) 1711dc4bb0e2SMartin KaFai Lau spin_lock_bh(&prog_idr_lock); 1712b16d9aa4SMartin KaFai Lau else 1713b16d9aa4SMartin KaFai Lau __acquire(&prog_idr_lock); 1714b16d9aa4SMartin KaFai Lau 1715dc4bb0e2SMartin KaFai Lau idr_remove(&prog_idr, prog->aux->id); 1716ad8ad79fSJakub Kicinski prog->aux->id = 0; 1717b16d9aa4SMartin KaFai Lau 1718b16d9aa4SMartin KaFai Lau if (do_idr_lock) 1719dc4bb0e2SMartin KaFai Lau spin_unlock_bh(&prog_idr_lock); 1720b16d9aa4SMartin KaFai Lau else 1721b16d9aa4SMartin KaFai Lau __release(&prog_idr_lock); 1722dc4bb0e2SMartin KaFai Lau } 1723dc4bb0e2SMartin KaFai Lau 17241aacde3dSDaniel Borkmann static void __bpf_prog_put_rcu(struct rcu_head *rcu) 1725abf2e7d6SAlexei Starovoitov { 1726abf2e7d6SAlexei Starovoitov struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); 1727abf2e7d6SAlexei Starovoitov 17283b4d9eb2SDaniel Borkmann kvfree(aux->func_info); 17298c1b6e69SAlexei Starovoitov kfree(aux->func_info_aux); 1730aaac3ba9SAlexei Starovoitov bpf_prog_uncharge_memlock(aux->prog); 1731afdb09c7SChenbo Feng security_bpf_prog_free(aux); 1732abf2e7d6SAlexei Starovoitov bpf_prog_free(aux->prog); 1733abf2e7d6SAlexei Starovoitov } 1734abf2e7d6SAlexei Starovoitov 1735cd7455f1SDaniel Borkmann static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred) 1736cd7455f1SDaniel Borkmann { 1737cd7455f1SDaniel Borkmann bpf_prog_kallsyms_del_all(prog); 1738cd7455f1SDaniel Borkmann btf_put(prog->aux->btf); 1739cd7455f1SDaniel Borkmann bpf_prog_free_linfo(prog); 1740cd7455f1SDaniel Borkmann 1741cd7455f1SDaniel Borkmann if (deferred) 1742cd7455f1SDaniel Borkmann call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); 1743cd7455f1SDaniel Borkmann else 1744cd7455f1SDaniel Borkmann __bpf_prog_put_rcu(&prog->aux->rcu); 1745cd7455f1SDaniel Borkmann } 1746cd7455f1SDaniel Borkmann 1747b16d9aa4SMartin KaFai Lau static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock) 174809756af4SAlexei Starovoitov { 174985192dbfSAndrii Nakryiko if (atomic64_dec_and_test(&prog->aux->refcnt)) { 17506ee52e2aSSong Liu perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0); 1751bae141f5SDaniel Borkmann bpf_audit_prog(prog, BPF_AUDIT_UNLOAD); 175234ad5580SMartin KaFai Lau /* bpf_prog_free_id() must be called first */ 1753b16d9aa4SMartin KaFai Lau bpf_prog_free_id(prog, do_idr_lock); 1754cd7455f1SDaniel Borkmann __bpf_prog_put_noref(prog, true); 175509756af4SAlexei Starovoitov } 1756a67edbf4SDaniel Borkmann } 1757b16d9aa4SMartin KaFai Lau 1758b16d9aa4SMartin KaFai Lau void bpf_prog_put(struct bpf_prog *prog) 1759b16d9aa4SMartin KaFai Lau { 1760b16d9aa4SMartin KaFai Lau __bpf_prog_put(prog, true); 1761b16d9aa4SMartin KaFai Lau } 1762e2e9b654SDaniel Borkmann EXPORT_SYMBOL_GPL(bpf_prog_put); 176309756af4SAlexei Starovoitov 176409756af4SAlexei Starovoitov static int bpf_prog_release(struct inode *inode, struct file *filp) 176509756af4SAlexei Starovoitov { 176609756af4SAlexei Starovoitov struct bpf_prog *prog = filp->private_data; 176709756af4SAlexei Starovoitov 17681aacde3dSDaniel Borkmann bpf_prog_put(prog); 176909756af4SAlexei Starovoitov return 0; 177009756af4SAlexei Starovoitov } 177109756af4SAlexei Starovoitov 1772492ecee8SAlexei Starovoitov static void bpf_prog_get_stats(const struct bpf_prog *prog, 1773492ecee8SAlexei Starovoitov struct bpf_prog_stats *stats) 1774492ecee8SAlexei Starovoitov { 1775492ecee8SAlexei Starovoitov u64 nsecs = 0, cnt = 0; 1776492ecee8SAlexei Starovoitov int cpu; 1777492ecee8SAlexei Starovoitov 1778492ecee8SAlexei Starovoitov for_each_possible_cpu(cpu) { 1779492ecee8SAlexei Starovoitov const struct bpf_prog_stats *st; 1780492ecee8SAlexei Starovoitov unsigned int start; 1781492ecee8SAlexei Starovoitov u64 tnsecs, tcnt; 1782492ecee8SAlexei Starovoitov 1783492ecee8SAlexei Starovoitov st = per_cpu_ptr(prog->aux->stats, cpu); 1784492ecee8SAlexei Starovoitov do { 1785492ecee8SAlexei Starovoitov start = u64_stats_fetch_begin_irq(&st->syncp); 1786492ecee8SAlexei Starovoitov tnsecs = st->nsecs; 1787492ecee8SAlexei Starovoitov tcnt = st->cnt; 1788492ecee8SAlexei Starovoitov } while (u64_stats_fetch_retry_irq(&st->syncp, start)); 1789492ecee8SAlexei Starovoitov nsecs += tnsecs; 1790492ecee8SAlexei Starovoitov cnt += tcnt; 1791492ecee8SAlexei Starovoitov } 1792492ecee8SAlexei Starovoitov stats->nsecs = nsecs; 1793492ecee8SAlexei Starovoitov stats->cnt = cnt; 1794492ecee8SAlexei Starovoitov } 1795492ecee8SAlexei Starovoitov 17967bd509e3SDaniel Borkmann #ifdef CONFIG_PROC_FS 17977bd509e3SDaniel Borkmann static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) 17987bd509e3SDaniel Borkmann { 17997bd509e3SDaniel Borkmann const struct bpf_prog *prog = filp->private_data; 1800f1f7714eSDaniel Borkmann char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; 1801492ecee8SAlexei Starovoitov struct bpf_prog_stats stats; 18027bd509e3SDaniel Borkmann 1803492ecee8SAlexei Starovoitov bpf_prog_get_stats(prog, &stats); 1804f1f7714eSDaniel Borkmann bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); 18057bd509e3SDaniel Borkmann seq_printf(m, 18067bd509e3SDaniel Borkmann "prog_type:\t%u\n" 18077bd509e3SDaniel Borkmann "prog_jited:\t%u\n" 1808f1f7714eSDaniel Borkmann "prog_tag:\t%s\n" 18094316b409SDaniel Borkmann "memlock:\t%llu\n" 1810492ecee8SAlexei Starovoitov "prog_id:\t%u\n" 1811492ecee8SAlexei Starovoitov "run_time_ns:\t%llu\n" 1812492ecee8SAlexei Starovoitov "run_cnt:\t%llu\n", 18137bd509e3SDaniel Borkmann prog->type, 18147bd509e3SDaniel Borkmann prog->jited, 1815f1f7714eSDaniel Borkmann prog_tag, 18164316b409SDaniel Borkmann prog->pages * 1ULL << PAGE_SHIFT, 1817492ecee8SAlexei Starovoitov prog->aux->id, 1818492ecee8SAlexei Starovoitov stats.nsecs, 1819492ecee8SAlexei Starovoitov stats.cnt); 18207bd509e3SDaniel Borkmann } 18217bd509e3SDaniel Borkmann #endif 18227bd509e3SDaniel Borkmann 1823f66e448cSChenbo Feng const struct file_operations bpf_prog_fops = { 18247bd509e3SDaniel Borkmann #ifdef CONFIG_PROC_FS 18257bd509e3SDaniel Borkmann .show_fdinfo = bpf_prog_show_fdinfo, 18267bd509e3SDaniel Borkmann #endif 182709756af4SAlexei Starovoitov .release = bpf_prog_release, 18286e71b04aSChenbo Feng .read = bpf_dummy_read, 18296e71b04aSChenbo Feng .write = bpf_dummy_write, 183009756af4SAlexei Starovoitov }; 183109756af4SAlexei Starovoitov 1832b2197755SDaniel Borkmann int bpf_prog_new_fd(struct bpf_prog *prog) 1833aa79781bSDaniel Borkmann { 1834afdb09c7SChenbo Feng int ret; 1835afdb09c7SChenbo Feng 1836afdb09c7SChenbo Feng ret = security_bpf_prog(prog); 1837afdb09c7SChenbo Feng if (ret < 0) 1838afdb09c7SChenbo Feng return ret; 1839afdb09c7SChenbo Feng 1840aa79781bSDaniel Borkmann return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, 1841aa79781bSDaniel Borkmann O_RDWR | O_CLOEXEC); 1842aa79781bSDaniel Borkmann } 1843aa79781bSDaniel Borkmann 1844113214beSDaniel Borkmann static struct bpf_prog *____bpf_prog_get(struct fd f) 184509756af4SAlexei Starovoitov { 184609756af4SAlexei Starovoitov if (!f.file) 184709756af4SAlexei Starovoitov return ERR_PTR(-EBADF); 184809756af4SAlexei Starovoitov if (f.file->f_op != &bpf_prog_fops) { 184909756af4SAlexei Starovoitov fdput(f); 185009756af4SAlexei Starovoitov return ERR_PTR(-EINVAL); 185109756af4SAlexei Starovoitov } 185209756af4SAlexei Starovoitov 1853c2101297SDaniel Borkmann return f.file->private_data; 185409756af4SAlexei Starovoitov } 185509756af4SAlexei Starovoitov 185685192dbfSAndrii Nakryiko void bpf_prog_add(struct bpf_prog *prog, int i) 185792117d84SAlexei Starovoitov { 185885192dbfSAndrii Nakryiko atomic64_add(i, &prog->aux->refcnt); 185992117d84SAlexei Starovoitov } 186059d3656dSBrenden Blanco EXPORT_SYMBOL_GPL(bpf_prog_add); 186159d3656dSBrenden Blanco 1862c540594fSDaniel Borkmann void bpf_prog_sub(struct bpf_prog *prog, int i) 1863c540594fSDaniel Borkmann { 1864c540594fSDaniel Borkmann /* Only to be used for undoing previous bpf_prog_add() in some 1865c540594fSDaniel Borkmann * error path. We still know that another entity in our call 1866c540594fSDaniel Borkmann * path holds a reference to the program, thus atomic_sub() can 1867c540594fSDaniel Borkmann * be safely used in such cases! 1868c540594fSDaniel Borkmann */ 186985192dbfSAndrii Nakryiko WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0); 1870c540594fSDaniel Borkmann } 1871c540594fSDaniel Borkmann EXPORT_SYMBOL_GPL(bpf_prog_sub); 1872c540594fSDaniel Borkmann 187385192dbfSAndrii Nakryiko void bpf_prog_inc(struct bpf_prog *prog) 187459d3656dSBrenden Blanco { 187585192dbfSAndrii Nakryiko atomic64_inc(&prog->aux->refcnt); 187659d3656dSBrenden Blanco } 187797bc402dSDaniel Borkmann EXPORT_SYMBOL_GPL(bpf_prog_inc); 187892117d84SAlexei Starovoitov 1879b16d9aa4SMartin KaFai Lau /* prog_idr_lock should have been held */ 1880a6f6df69SJohn Fastabend struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) 1881b16d9aa4SMartin KaFai Lau { 1882b16d9aa4SMartin KaFai Lau int refold; 1883b16d9aa4SMartin KaFai Lau 188485192dbfSAndrii Nakryiko refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0); 1885b16d9aa4SMartin KaFai Lau 1886b16d9aa4SMartin KaFai Lau if (!refold) 1887b16d9aa4SMartin KaFai Lau return ERR_PTR(-ENOENT); 1888b16d9aa4SMartin KaFai Lau 1889b16d9aa4SMartin KaFai Lau return prog; 1890b16d9aa4SMartin KaFai Lau } 1891a6f6df69SJohn Fastabend EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); 1892b16d9aa4SMartin KaFai Lau 1893040ee692SAl Viro bool bpf_prog_get_ok(struct bpf_prog *prog, 1894288b3de5SJakub Kicinski enum bpf_prog_type *attach_type, bool attach_drv) 1895248f346fSJakub Kicinski { 1896288b3de5SJakub Kicinski /* not an attachment, just a refcount inc, always allow */ 1897288b3de5SJakub Kicinski if (!attach_type) 1898288b3de5SJakub Kicinski return true; 1899248f346fSJakub Kicinski 1900248f346fSJakub Kicinski if (prog->type != *attach_type) 1901248f346fSJakub Kicinski return false; 1902288b3de5SJakub Kicinski if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) 1903248f346fSJakub Kicinski return false; 1904248f346fSJakub Kicinski 1905248f346fSJakub Kicinski return true; 1906248f346fSJakub Kicinski } 1907248f346fSJakub Kicinski 1908248f346fSJakub Kicinski static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, 1909288b3de5SJakub Kicinski bool attach_drv) 191009756af4SAlexei Starovoitov { 191109756af4SAlexei Starovoitov struct fd f = fdget(ufd); 191209756af4SAlexei Starovoitov struct bpf_prog *prog; 191309756af4SAlexei Starovoitov 1914113214beSDaniel Borkmann prog = ____bpf_prog_get(f); 191509756af4SAlexei Starovoitov if (IS_ERR(prog)) 191609756af4SAlexei Starovoitov return prog; 1917288b3de5SJakub Kicinski if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { 1918113214beSDaniel Borkmann prog = ERR_PTR(-EINVAL); 1919113214beSDaniel Borkmann goto out; 1920113214beSDaniel Borkmann } 192109756af4SAlexei Starovoitov 192285192dbfSAndrii Nakryiko bpf_prog_inc(prog); 1923113214beSDaniel Borkmann out: 192409756af4SAlexei Starovoitov fdput(f); 192509756af4SAlexei Starovoitov return prog; 192609756af4SAlexei Starovoitov } 1927113214beSDaniel Borkmann 1928113214beSDaniel Borkmann struct bpf_prog *bpf_prog_get(u32 ufd) 1929113214beSDaniel Borkmann { 1930288b3de5SJakub Kicinski return __bpf_prog_get(ufd, NULL, false); 1931113214beSDaniel Borkmann } 1932113214beSDaniel Borkmann 1933248f346fSJakub Kicinski struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, 1934288b3de5SJakub Kicinski bool attach_drv) 1935248f346fSJakub Kicinski { 19364d220ed0SAlexei Starovoitov return __bpf_prog_get(ufd, &type, attach_drv); 1937248f346fSJakub Kicinski } 19386c8dfe21SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); 1939248f346fSJakub Kicinski 1940aac3fc32SAndrey Ignatov /* Initially all BPF programs could be loaded w/o specifying 1941aac3fc32SAndrey Ignatov * expected_attach_type. Later for some of them specifying expected_attach_type 1942aac3fc32SAndrey Ignatov * at load time became required so that program could be validated properly. 1943aac3fc32SAndrey Ignatov * Programs of types that are allowed to be loaded both w/ and w/o (for 1944aac3fc32SAndrey Ignatov * backward compatibility) expected_attach_type, should have the default attach 1945aac3fc32SAndrey Ignatov * type assigned to expected_attach_type for the latter case, so that it can be 1946aac3fc32SAndrey Ignatov * validated later at attach time. 1947aac3fc32SAndrey Ignatov * 1948aac3fc32SAndrey Ignatov * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if 1949aac3fc32SAndrey Ignatov * prog type requires it but has some attach types that have to be backward 1950aac3fc32SAndrey Ignatov * compatible. 1951aac3fc32SAndrey Ignatov */ 1952aac3fc32SAndrey Ignatov static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) 1953aac3fc32SAndrey Ignatov { 1954aac3fc32SAndrey Ignatov switch (attr->prog_type) { 1955aac3fc32SAndrey Ignatov case BPF_PROG_TYPE_CGROUP_SOCK: 1956aac3fc32SAndrey Ignatov /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't 1957aac3fc32SAndrey Ignatov * exist so checking for non-zero is the way to go here. 1958aac3fc32SAndrey Ignatov */ 1959aac3fc32SAndrey Ignatov if (!attr->expected_attach_type) 1960aac3fc32SAndrey Ignatov attr->expected_attach_type = 1961aac3fc32SAndrey Ignatov BPF_CGROUP_INET_SOCK_CREATE; 1962aac3fc32SAndrey Ignatov break; 1963aac3fc32SAndrey Ignatov } 1964aac3fc32SAndrey Ignatov } 1965aac3fc32SAndrey Ignatov 19665e43f899SAndrey Ignatov static int 1967ccfe29ebSAlexei Starovoitov bpf_prog_load_check_attach(enum bpf_prog_type prog_type, 1968ccfe29ebSAlexei Starovoitov enum bpf_attach_type expected_attach_type, 19695b92a28aSAlexei Starovoitov u32 btf_id, u32 prog_fd) 19705e43f899SAndrey Ignatov { 197127ae7997SMartin KaFai Lau if (btf_id) { 1972c108e3c1SAlexei Starovoitov if (btf_id > BTF_MAX_TYPE) 1973c108e3c1SAlexei Starovoitov return -EINVAL; 197427ae7997SMartin KaFai Lau 197527ae7997SMartin KaFai Lau switch (prog_type) { 197627ae7997SMartin KaFai Lau case BPF_PROG_TYPE_TRACING: 19779e4e01dfSKP Singh case BPF_PROG_TYPE_LSM: 197827ae7997SMartin KaFai Lau case BPF_PROG_TYPE_STRUCT_OPS: 1979be8704ffSAlexei Starovoitov case BPF_PROG_TYPE_EXT: 1980c108e3c1SAlexei Starovoitov break; 1981c108e3c1SAlexei Starovoitov default: 1982c108e3c1SAlexei Starovoitov return -EINVAL; 1983c108e3c1SAlexei Starovoitov } 198427ae7997SMartin KaFai Lau } 198527ae7997SMartin KaFai Lau 1986be8704ffSAlexei Starovoitov if (prog_fd && prog_type != BPF_PROG_TYPE_TRACING && 1987be8704ffSAlexei Starovoitov prog_type != BPF_PROG_TYPE_EXT) 198827ae7997SMartin KaFai Lau return -EINVAL; 1989c108e3c1SAlexei Starovoitov 1990c108e3c1SAlexei Starovoitov switch (prog_type) { 1991aac3fc32SAndrey Ignatov case BPF_PROG_TYPE_CGROUP_SOCK: 1992aac3fc32SAndrey Ignatov switch (expected_attach_type) { 1993aac3fc32SAndrey Ignatov case BPF_CGROUP_INET_SOCK_CREATE: 1994aac3fc32SAndrey Ignatov case BPF_CGROUP_INET4_POST_BIND: 1995aac3fc32SAndrey Ignatov case BPF_CGROUP_INET6_POST_BIND: 1996aac3fc32SAndrey Ignatov return 0; 1997aac3fc32SAndrey Ignatov default: 1998aac3fc32SAndrey Ignatov return -EINVAL; 1999aac3fc32SAndrey Ignatov } 20004fbac77dSAndrey Ignatov case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 20014fbac77dSAndrey Ignatov switch (expected_attach_type) { 20024fbac77dSAndrey Ignatov case BPF_CGROUP_INET4_BIND: 20034fbac77dSAndrey Ignatov case BPF_CGROUP_INET6_BIND: 2004d74bad4eSAndrey Ignatov case BPF_CGROUP_INET4_CONNECT: 2005d74bad4eSAndrey Ignatov case BPF_CGROUP_INET6_CONNECT: 20061b66d253SDaniel Borkmann case BPF_CGROUP_INET4_GETPEERNAME: 20071b66d253SDaniel Borkmann case BPF_CGROUP_INET6_GETPEERNAME: 20081b66d253SDaniel Borkmann case BPF_CGROUP_INET4_GETSOCKNAME: 20091b66d253SDaniel Borkmann case BPF_CGROUP_INET6_GETSOCKNAME: 20101cedee13SAndrey Ignatov case BPF_CGROUP_UDP4_SENDMSG: 20111cedee13SAndrey Ignatov case BPF_CGROUP_UDP6_SENDMSG: 2012983695faSDaniel Borkmann case BPF_CGROUP_UDP4_RECVMSG: 2013983695faSDaniel Borkmann case BPF_CGROUP_UDP6_RECVMSG: 20145e43f899SAndrey Ignatov return 0; 20154fbac77dSAndrey Ignatov default: 20164fbac77dSAndrey Ignatov return -EINVAL; 20174fbac77dSAndrey Ignatov } 20185cf1e914Sbrakmo case BPF_PROG_TYPE_CGROUP_SKB: 20195cf1e914Sbrakmo switch (expected_attach_type) { 20205cf1e914Sbrakmo case BPF_CGROUP_INET_INGRESS: 20215cf1e914Sbrakmo case BPF_CGROUP_INET_EGRESS: 20225cf1e914Sbrakmo return 0; 20235cf1e914Sbrakmo default: 20245cf1e914Sbrakmo return -EINVAL; 20255cf1e914Sbrakmo } 20260d01da6aSStanislav Fomichev case BPF_PROG_TYPE_CGROUP_SOCKOPT: 20270d01da6aSStanislav Fomichev switch (expected_attach_type) { 20280d01da6aSStanislav Fomichev case BPF_CGROUP_SETSOCKOPT: 20290d01da6aSStanislav Fomichev case BPF_CGROUP_GETSOCKOPT: 20300d01da6aSStanislav Fomichev return 0; 20310d01da6aSStanislav Fomichev default: 20320d01da6aSStanislav Fomichev return -EINVAL; 20330d01da6aSStanislav Fomichev } 2034be8704ffSAlexei Starovoitov case BPF_PROG_TYPE_EXT: 2035be8704ffSAlexei Starovoitov if (expected_attach_type) 2036be8704ffSAlexei Starovoitov return -EINVAL; 2037be8704ffSAlexei Starovoitov /* fallthrough */ 20384fbac77dSAndrey Ignatov default: 20394fbac77dSAndrey Ignatov return 0; 20404fbac77dSAndrey Ignatov } 20415e43f899SAndrey Ignatov } 20425e43f899SAndrey Ignatov 20432c78ee89SAlexei Starovoitov static bool is_net_admin_prog_type(enum bpf_prog_type prog_type) 20442c78ee89SAlexei Starovoitov { 20452c78ee89SAlexei Starovoitov switch (prog_type) { 20462c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_SCHED_CLS: 20472c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_SCHED_ACT: 20482c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_XDP: 20492c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_LWT_IN: 20502c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_LWT_OUT: 20512c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_LWT_XMIT: 20522c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_LWT_SEG6LOCAL: 20532c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_SK_SKB: 20542c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_SK_MSG: 20552c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_LIRC_MODE2: 20562c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_FLOW_DISSECTOR: 20572c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_CGROUP_DEVICE: 20582c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_CGROUP_SOCK: 20592c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 20602c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_CGROUP_SOCKOPT: 20612c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_CGROUP_SYSCTL: 20622c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_SOCK_OPS: 20632c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_EXT: /* extends any prog */ 20642c78ee89SAlexei Starovoitov return true; 20652c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_CGROUP_SKB: 20662c78ee89SAlexei Starovoitov /* always unpriv */ 20672c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_SK_REUSEPORT: 20682c78ee89SAlexei Starovoitov /* equivalent to SOCKET_FILTER. need CAP_BPF only */ 20692c78ee89SAlexei Starovoitov default: 20702c78ee89SAlexei Starovoitov return false; 20712c78ee89SAlexei Starovoitov } 20722c78ee89SAlexei Starovoitov } 20732c78ee89SAlexei Starovoitov 20742c78ee89SAlexei Starovoitov static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) 20752c78ee89SAlexei Starovoitov { 20762c78ee89SAlexei Starovoitov switch (prog_type) { 20772c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_KPROBE: 20782c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_TRACEPOINT: 20792c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_PERF_EVENT: 20802c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_RAW_TRACEPOINT: 20812c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: 20822c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_TRACING: 20832c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_LSM: 20842c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */ 20852c78ee89SAlexei Starovoitov case BPF_PROG_TYPE_EXT: /* extends any prog */ 20862c78ee89SAlexei Starovoitov return true; 20872c78ee89SAlexei Starovoitov default: 20882c78ee89SAlexei Starovoitov return false; 20892c78ee89SAlexei Starovoitov } 20902c78ee89SAlexei Starovoitov } 20912c78ee89SAlexei Starovoitov 209209756af4SAlexei Starovoitov /* last field in 'union bpf_attr' used by this command */ 20935b92a28aSAlexei Starovoitov #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd 209409756af4SAlexei Starovoitov 2095838e9690SYonghong Song static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) 209609756af4SAlexei Starovoitov { 209709756af4SAlexei Starovoitov enum bpf_prog_type type = attr->prog_type; 209809756af4SAlexei Starovoitov struct bpf_prog *prog; 209909756af4SAlexei Starovoitov int err; 210009756af4SAlexei Starovoitov char license[128]; 210109756af4SAlexei Starovoitov bool is_gpl; 210209756af4SAlexei Starovoitov 210309756af4SAlexei Starovoitov if (CHECK_ATTR(BPF_PROG_LOAD)) 210409756af4SAlexei Starovoitov return -EINVAL; 210509756af4SAlexei Starovoitov 2106c240eff6SJiong Wang if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | 2107c240eff6SJiong Wang BPF_F_ANY_ALIGNMENT | 210810d274e8SAlexei Starovoitov BPF_F_TEST_STATE_FREQ | 2109c240eff6SJiong Wang BPF_F_TEST_RND_HI32)) 2110e07b98d9SDavid S. Miller return -EINVAL; 2111e07b98d9SDavid S. Miller 2112e9ee9efcSDavid Miller if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && 2113e9ee9efcSDavid Miller (attr->prog_flags & BPF_F_ANY_ALIGNMENT) && 21142c78ee89SAlexei Starovoitov !bpf_capable()) 2115e9ee9efcSDavid Miller return -EPERM; 2116e9ee9efcSDavid Miller 211709756af4SAlexei Starovoitov /* copy eBPF program license from user space */ 2118535e7b4bSMickaël Salaün if (strncpy_from_user(license, u64_to_user_ptr(attr->license), 211909756af4SAlexei Starovoitov sizeof(license) - 1) < 0) 212009756af4SAlexei Starovoitov return -EFAULT; 212109756af4SAlexei Starovoitov license[sizeof(license) - 1] = 0; 212209756af4SAlexei Starovoitov 212309756af4SAlexei Starovoitov /* eBPF programs must be GPL compatible to use GPL-ed functions */ 212409756af4SAlexei Starovoitov is_gpl = license_is_gpl_compatible(license); 212509756af4SAlexei Starovoitov 2126c04c0d2bSAlexei Starovoitov if (attr->insn_cnt == 0 || 21272c78ee89SAlexei Starovoitov attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) 2128ef0915caSDaniel Borkmann return -E2BIG; 212980b7d819SChenbo Feng if (type != BPF_PROG_TYPE_SOCKET_FILTER && 213080b7d819SChenbo Feng type != BPF_PROG_TYPE_CGROUP_SKB && 21312c78ee89SAlexei Starovoitov !bpf_capable()) 21322c78ee89SAlexei Starovoitov return -EPERM; 21332c78ee89SAlexei Starovoitov 21342c78ee89SAlexei Starovoitov if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN)) 21352c78ee89SAlexei Starovoitov return -EPERM; 21362c78ee89SAlexei Starovoitov if (is_perfmon_prog_type(type) && !perfmon_capable()) 21371be7f75dSAlexei Starovoitov return -EPERM; 21381be7f75dSAlexei Starovoitov 2139aac3fc32SAndrey Ignatov bpf_prog_load_fixup_attach_type(attr); 2140ccfe29ebSAlexei Starovoitov if (bpf_prog_load_check_attach(type, attr->expected_attach_type, 21415b92a28aSAlexei Starovoitov attr->attach_btf_id, 21425b92a28aSAlexei Starovoitov attr->attach_prog_fd)) 21435e43f899SAndrey Ignatov return -EINVAL; 21445e43f899SAndrey Ignatov 214509756af4SAlexei Starovoitov /* plain bpf_prog allocation */ 214609756af4SAlexei Starovoitov prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); 214709756af4SAlexei Starovoitov if (!prog) 214809756af4SAlexei Starovoitov return -ENOMEM; 214909756af4SAlexei Starovoitov 21505e43f899SAndrey Ignatov prog->expected_attach_type = attr->expected_attach_type; 2151ccfe29ebSAlexei Starovoitov prog->aux->attach_btf_id = attr->attach_btf_id; 21525b92a28aSAlexei Starovoitov if (attr->attach_prog_fd) { 21535b92a28aSAlexei Starovoitov struct bpf_prog *tgt_prog; 21545b92a28aSAlexei Starovoitov 21555b92a28aSAlexei Starovoitov tgt_prog = bpf_prog_get(attr->attach_prog_fd); 21565b92a28aSAlexei Starovoitov if (IS_ERR(tgt_prog)) { 21575b92a28aSAlexei Starovoitov err = PTR_ERR(tgt_prog); 21585b92a28aSAlexei Starovoitov goto free_prog_nouncharge; 21595b92a28aSAlexei Starovoitov } 21605b92a28aSAlexei Starovoitov prog->aux->linked_prog = tgt_prog; 21615b92a28aSAlexei Starovoitov } 21625e43f899SAndrey Ignatov 21639a18eedbSJakub Kicinski prog->aux->offload_requested = !!attr->prog_ifindex; 21649a18eedbSJakub Kicinski 2165afdb09c7SChenbo Feng err = security_bpf_prog_alloc(prog->aux); 2166aaac3ba9SAlexei Starovoitov if (err) 2167aaac3ba9SAlexei Starovoitov goto free_prog_nouncharge; 2168aaac3ba9SAlexei Starovoitov 2169afdb09c7SChenbo Feng err = bpf_prog_charge_memlock(prog); 2170afdb09c7SChenbo Feng if (err) 2171afdb09c7SChenbo Feng goto free_prog_sec; 2172afdb09c7SChenbo Feng 217309756af4SAlexei Starovoitov prog->len = attr->insn_cnt; 217409756af4SAlexei Starovoitov 217509756af4SAlexei Starovoitov err = -EFAULT; 2176535e7b4bSMickaël Salaün if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), 2177aafe6ae9SDaniel Borkmann bpf_prog_insn_size(prog)) != 0) 217809756af4SAlexei Starovoitov goto free_prog; 217909756af4SAlexei Starovoitov 218009756af4SAlexei Starovoitov prog->orig_prog = NULL; 2181a91263d5SDaniel Borkmann prog->jited = 0; 218209756af4SAlexei Starovoitov 218385192dbfSAndrii Nakryiko atomic64_set(&prog->aux->refcnt, 1); 2184a91263d5SDaniel Borkmann prog->gpl_compatible = is_gpl ? 1 : 0; 218509756af4SAlexei Starovoitov 21869a18eedbSJakub Kicinski if (bpf_prog_is_dev_bound(prog->aux)) { 2187ab3f0063SJakub Kicinski err = bpf_prog_offload_init(prog, attr); 2188ab3f0063SJakub Kicinski if (err) 2189ab3f0063SJakub Kicinski goto free_prog; 2190ab3f0063SJakub Kicinski } 2191ab3f0063SJakub Kicinski 219209756af4SAlexei Starovoitov /* find program type: socket_filter vs tracing_filter */ 219309756af4SAlexei Starovoitov err = find_prog_type(type, prog); 219409756af4SAlexei Starovoitov if (err < 0) 219509756af4SAlexei Starovoitov goto free_prog; 219609756af4SAlexei Starovoitov 21979285ec4cSJason A. Donenfeld prog->aux->load_time = ktime_get_boottime_ns(); 21988e7ae251SMartin KaFai Lau err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name, 21998e7ae251SMartin KaFai Lau sizeof(attr->prog_name)); 22008e7ae251SMartin KaFai Lau if (err < 0) 2201cb4d2b3fSMartin KaFai Lau goto free_prog; 2202cb4d2b3fSMartin KaFai Lau 220309756af4SAlexei Starovoitov /* run eBPF verifier */ 2204838e9690SYonghong Song err = bpf_check(&prog, attr, uattr); 220509756af4SAlexei Starovoitov if (err < 0) 220609756af4SAlexei Starovoitov goto free_used_maps; 220709756af4SAlexei Starovoitov 2208d1c55ab5SDaniel Borkmann prog = bpf_prog_select_runtime(prog, &err); 220904fd61abSAlexei Starovoitov if (err < 0) 221004fd61abSAlexei Starovoitov goto free_used_maps; 221109756af4SAlexei Starovoitov 2212dc4bb0e2SMartin KaFai Lau err = bpf_prog_alloc_id(prog); 2213dc4bb0e2SMartin KaFai Lau if (err) 2214dc4bb0e2SMartin KaFai Lau goto free_used_maps; 2215dc4bb0e2SMartin KaFai Lau 2216c751798aSDaniel Borkmann /* Upon success of bpf_prog_alloc_id(), the BPF prog is 2217c751798aSDaniel Borkmann * effectively publicly exposed. However, retrieving via 2218c751798aSDaniel Borkmann * bpf_prog_get_fd_by_id() will take another reference, 2219c751798aSDaniel Borkmann * therefore it cannot be gone underneath us. 2220c751798aSDaniel Borkmann * 2221c751798aSDaniel Borkmann * Only for the time /after/ successful bpf_prog_new_fd() 2222c751798aSDaniel Borkmann * and before returning to userspace, we might just hold 2223c751798aSDaniel Borkmann * one reference and any parallel close on that fd could 2224c751798aSDaniel Borkmann * rip everything out. Hence, below notifications must 2225c751798aSDaniel Borkmann * happen before bpf_prog_new_fd(). 2226c751798aSDaniel Borkmann * 2227c751798aSDaniel Borkmann * Also, any failure handling from this point onwards must 2228c751798aSDaniel Borkmann * be using bpf_prog_put() given the program is exposed. 2229b16d9aa4SMartin KaFai Lau */ 223074451e66SDaniel Borkmann bpf_prog_kallsyms_add(prog); 22316ee52e2aSSong Liu perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); 2232bae141f5SDaniel Borkmann bpf_audit_prog(prog, BPF_AUDIT_LOAD); 2233c751798aSDaniel Borkmann 2234c751798aSDaniel Borkmann err = bpf_prog_new_fd(prog); 2235c751798aSDaniel Borkmann if (err < 0) 2236c751798aSDaniel Borkmann bpf_prog_put(prog); 223709756af4SAlexei Starovoitov return err; 223809756af4SAlexei Starovoitov 223909756af4SAlexei Starovoitov free_used_maps: 2240cd7455f1SDaniel Borkmann /* In case we have subprogs, we need to wait for a grace 2241cd7455f1SDaniel Borkmann * period before we can tear down JIT memory since symbols 2242cd7455f1SDaniel Borkmann * are already exposed under kallsyms. 2243cd7455f1SDaniel Borkmann */ 2244cd7455f1SDaniel Borkmann __bpf_prog_put_noref(prog, prog->aux->func_cnt); 2245cd7455f1SDaniel Borkmann return err; 224609756af4SAlexei Starovoitov free_prog: 2247aaac3ba9SAlexei Starovoitov bpf_prog_uncharge_memlock(prog); 2248afdb09c7SChenbo Feng free_prog_sec: 2249afdb09c7SChenbo Feng security_bpf_prog_free(prog->aux); 2250aaac3ba9SAlexei Starovoitov free_prog_nouncharge: 225109756af4SAlexei Starovoitov bpf_prog_free(prog); 225209756af4SAlexei Starovoitov return err; 225309756af4SAlexei Starovoitov } 225409756af4SAlexei Starovoitov 22556e71b04aSChenbo Feng #define BPF_OBJ_LAST_FIELD file_flags 2256b2197755SDaniel Borkmann 2257b2197755SDaniel Borkmann static int bpf_obj_pin(const union bpf_attr *attr) 2258b2197755SDaniel Borkmann { 22596e71b04aSChenbo Feng if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) 2260b2197755SDaniel Borkmann return -EINVAL; 2261b2197755SDaniel Borkmann 2262535e7b4bSMickaël Salaün return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname)); 2263b2197755SDaniel Borkmann } 2264b2197755SDaniel Borkmann 2265b2197755SDaniel Borkmann static int bpf_obj_get(const union bpf_attr *attr) 2266b2197755SDaniel Borkmann { 22676e71b04aSChenbo Feng if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || 22686e71b04aSChenbo Feng attr->file_flags & ~BPF_OBJ_FLAG_MASK) 2269b2197755SDaniel Borkmann return -EINVAL; 2270b2197755SDaniel Borkmann 22716e71b04aSChenbo Feng return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), 22726e71b04aSChenbo Feng attr->file_flags); 2273b2197755SDaniel Borkmann } 2274b2197755SDaniel Borkmann 2275f2e10bffSAndrii Nakryiko void bpf_link_init(struct bpf_link *link, enum bpf_link_type type, 2276a3b80e10SAndrii Nakryiko const struct bpf_link_ops *ops, struct bpf_prog *prog) 227770ed506cSAndrii Nakryiko { 227870ed506cSAndrii Nakryiko atomic64_set(&link->refcnt, 1); 2279f2e10bffSAndrii Nakryiko link->type = type; 2280a3b80e10SAndrii Nakryiko link->id = 0; 228170ed506cSAndrii Nakryiko link->ops = ops; 228270ed506cSAndrii Nakryiko link->prog = prog; 228370ed506cSAndrii Nakryiko } 228470ed506cSAndrii Nakryiko 2285a3b80e10SAndrii Nakryiko static void bpf_link_free_id(int id) 2286a3b80e10SAndrii Nakryiko { 2287a3b80e10SAndrii Nakryiko if (!id) 2288a3b80e10SAndrii Nakryiko return; 2289a3b80e10SAndrii Nakryiko 2290a3b80e10SAndrii Nakryiko spin_lock_bh(&link_idr_lock); 2291a3b80e10SAndrii Nakryiko idr_remove(&link_idr, id); 2292a3b80e10SAndrii Nakryiko spin_unlock_bh(&link_idr_lock); 2293a3b80e10SAndrii Nakryiko } 2294a3b80e10SAndrii Nakryiko 229598868668SAndrii Nakryiko /* Clean up bpf_link and corresponding anon_inode file and FD. After 229698868668SAndrii Nakryiko * anon_inode is created, bpf_link can't be just kfree()'d due to deferred 2297a3b80e10SAndrii Nakryiko * anon_inode's release() call. This helper marksbpf_link as 2298a3b80e10SAndrii Nakryiko * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt 2299a3b80e10SAndrii Nakryiko * is not decremented, it's the responsibility of a calling code that failed 2300a3b80e10SAndrii Nakryiko * to complete bpf_link initialization. 230198868668SAndrii Nakryiko */ 2302a3b80e10SAndrii Nakryiko void bpf_link_cleanup(struct bpf_link_primer *primer) 2303babf3164SAndrii Nakryiko { 2304a3b80e10SAndrii Nakryiko primer->link->prog = NULL; 2305a3b80e10SAndrii Nakryiko bpf_link_free_id(primer->id); 2306a3b80e10SAndrii Nakryiko fput(primer->file); 2307a3b80e10SAndrii Nakryiko put_unused_fd(primer->fd); 2308babf3164SAndrii Nakryiko } 2309babf3164SAndrii Nakryiko 231070ed506cSAndrii Nakryiko void bpf_link_inc(struct bpf_link *link) 231170ed506cSAndrii Nakryiko { 231270ed506cSAndrii Nakryiko atomic64_inc(&link->refcnt); 231370ed506cSAndrii Nakryiko } 231470ed506cSAndrii Nakryiko 231570ed506cSAndrii Nakryiko /* bpf_link_free is guaranteed to be called from process context */ 231670ed506cSAndrii Nakryiko static void bpf_link_free(struct bpf_link *link) 231770ed506cSAndrii Nakryiko { 2318a3b80e10SAndrii Nakryiko bpf_link_free_id(link->id); 2319babf3164SAndrii Nakryiko if (link->prog) { 2320babf3164SAndrii Nakryiko /* detach BPF program, clean up used resources */ 232170ed506cSAndrii Nakryiko link->ops->release(link); 2322babf3164SAndrii Nakryiko bpf_prog_put(link->prog); 2323babf3164SAndrii Nakryiko } 2324babf3164SAndrii Nakryiko /* free bpf_link and its containing memory */ 2325babf3164SAndrii Nakryiko link->ops->dealloc(link); 232670ed506cSAndrii Nakryiko } 232770ed506cSAndrii Nakryiko 232870ed506cSAndrii Nakryiko static void bpf_link_put_deferred(struct work_struct *work) 232970ed506cSAndrii Nakryiko { 233070ed506cSAndrii Nakryiko struct bpf_link *link = container_of(work, struct bpf_link, work); 233170ed506cSAndrii Nakryiko 233270ed506cSAndrii Nakryiko bpf_link_free(link); 233370ed506cSAndrii Nakryiko } 233470ed506cSAndrii Nakryiko 233570ed506cSAndrii Nakryiko /* bpf_link_put can be called from atomic context, but ensures that resources 233670ed506cSAndrii Nakryiko * are freed from process context 233770ed506cSAndrii Nakryiko */ 233870ed506cSAndrii Nakryiko void bpf_link_put(struct bpf_link *link) 233970ed506cSAndrii Nakryiko { 234070ed506cSAndrii Nakryiko if (!atomic64_dec_and_test(&link->refcnt)) 234170ed506cSAndrii Nakryiko return; 234270ed506cSAndrii Nakryiko 234370ed506cSAndrii Nakryiko if (in_atomic()) { 234470ed506cSAndrii Nakryiko INIT_WORK(&link->work, bpf_link_put_deferred); 234570ed506cSAndrii Nakryiko schedule_work(&link->work); 234670ed506cSAndrii Nakryiko } else { 234770ed506cSAndrii Nakryiko bpf_link_free(link); 234870ed506cSAndrii Nakryiko } 234970ed506cSAndrii Nakryiko } 235070ed506cSAndrii Nakryiko 235170ed506cSAndrii Nakryiko static int bpf_link_release(struct inode *inode, struct file *filp) 235270ed506cSAndrii Nakryiko { 235370ed506cSAndrii Nakryiko struct bpf_link *link = filp->private_data; 235470ed506cSAndrii Nakryiko 235570ed506cSAndrii Nakryiko bpf_link_put(link); 2356fec56f58SAlexei Starovoitov return 0; 2357fec56f58SAlexei Starovoitov } 2358fec56f58SAlexei Starovoitov 2359449e14bfSArnd Bergmann #ifdef CONFIG_PROC_FS 2360f2e10bffSAndrii Nakryiko #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) 2361f2e10bffSAndrii Nakryiko #define BPF_MAP_TYPE(_id, _ops) 2362f2e10bffSAndrii Nakryiko #define BPF_LINK_TYPE(_id, _name) [_id] = #_name, 2363f2e10bffSAndrii Nakryiko static const char *bpf_link_type_strs[] = { 2364f2e10bffSAndrii Nakryiko [BPF_LINK_TYPE_UNSPEC] = "<invalid>", 2365f2e10bffSAndrii Nakryiko #include <linux/bpf_types.h> 2366f2e10bffSAndrii Nakryiko }; 2367f2e10bffSAndrii Nakryiko #undef BPF_PROG_TYPE 2368f2e10bffSAndrii Nakryiko #undef BPF_MAP_TYPE 2369f2e10bffSAndrii Nakryiko #undef BPF_LINK_TYPE 237070ed506cSAndrii Nakryiko 237170ed506cSAndrii Nakryiko static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp) 237270ed506cSAndrii Nakryiko { 237370ed506cSAndrii Nakryiko const struct bpf_link *link = filp->private_data; 237470ed506cSAndrii Nakryiko const struct bpf_prog *prog = link->prog; 237570ed506cSAndrii Nakryiko char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; 237670ed506cSAndrii Nakryiko 237770ed506cSAndrii Nakryiko bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); 237870ed506cSAndrii Nakryiko seq_printf(m, 237970ed506cSAndrii Nakryiko "link_type:\t%s\n" 2380a3b80e10SAndrii Nakryiko "link_id:\t%u\n" 238170ed506cSAndrii Nakryiko "prog_tag:\t%s\n" 238270ed506cSAndrii Nakryiko "prog_id:\t%u\n", 2383f2e10bffSAndrii Nakryiko bpf_link_type_strs[link->type], 2384a3b80e10SAndrii Nakryiko link->id, 238570ed506cSAndrii Nakryiko prog_tag, 238670ed506cSAndrii Nakryiko prog->aux->id); 2387f2e10bffSAndrii Nakryiko if (link->ops->show_fdinfo) 2388f2e10bffSAndrii Nakryiko link->ops->show_fdinfo(link, m); 238970ed506cSAndrii Nakryiko } 239070ed506cSAndrii Nakryiko #endif 239170ed506cSAndrii Nakryiko 23926f302bfbSZou Wei static const struct file_operations bpf_link_fops = { 239370ed506cSAndrii Nakryiko #ifdef CONFIG_PROC_FS 239470ed506cSAndrii Nakryiko .show_fdinfo = bpf_link_show_fdinfo, 239570ed506cSAndrii Nakryiko #endif 239670ed506cSAndrii Nakryiko .release = bpf_link_release, 2397fec56f58SAlexei Starovoitov .read = bpf_dummy_read, 2398fec56f58SAlexei Starovoitov .write = bpf_dummy_write, 2399fec56f58SAlexei Starovoitov }; 2400fec56f58SAlexei Starovoitov 2401a3b80e10SAndrii Nakryiko static int bpf_link_alloc_id(struct bpf_link *link) 240270ed506cSAndrii Nakryiko { 2403a3b80e10SAndrii Nakryiko int id; 2404a3b80e10SAndrii Nakryiko 2405a3b80e10SAndrii Nakryiko idr_preload(GFP_KERNEL); 2406a3b80e10SAndrii Nakryiko spin_lock_bh(&link_idr_lock); 2407a3b80e10SAndrii Nakryiko id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC); 2408a3b80e10SAndrii Nakryiko spin_unlock_bh(&link_idr_lock); 2409a3b80e10SAndrii Nakryiko idr_preload_end(); 2410a3b80e10SAndrii Nakryiko 2411a3b80e10SAndrii Nakryiko return id; 241270ed506cSAndrii Nakryiko } 241370ed506cSAndrii Nakryiko 2414a3b80e10SAndrii Nakryiko /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file, 2415a3b80e10SAndrii Nakryiko * reserving unused FD and allocating ID from link_idr. This is to be paired 2416a3b80e10SAndrii Nakryiko * with bpf_link_settle() to install FD and ID and expose bpf_link to 2417a3b80e10SAndrii Nakryiko * user-space, if bpf_link is successfully attached. If not, bpf_link and 2418a3b80e10SAndrii Nakryiko * pre-allocated resources are to be freed with bpf_cleanup() call. All the 2419a3b80e10SAndrii Nakryiko * transient state is passed around in struct bpf_link_primer. 2420a3b80e10SAndrii Nakryiko * This is preferred way to create and initialize bpf_link, especially when 2421a3b80e10SAndrii Nakryiko * there are complicated and expensive operations inbetween creating bpf_link 2422a3b80e10SAndrii Nakryiko * itself and attaching it to BPF hook. By using bpf_link_prime() and 2423a3b80e10SAndrii Nakryiko * bpf_link_settle() kernel code using bpf_link doesn't have to perform 2424a3b80e10SAndrii Nakryiko * expensive (and potentially failing) roll back operations in a rare case 2425a3b80e10SAndrii Nakryiko * that file, FD, or ID can't be allocated. 2426babf3164SAndrii Nakryiko */ 2427a3b80e10SAndrii Nakryiko int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer) 2428babf3164SAndrii Nakryiko { 2429babf3164SAndrii Nakryiko struct file *file; 2430a3b80e10SAndrii Nakryiko int fd, id; 2431babf3164SAndrii Nakryiko 2432babf3164SAndrii Nakryiko fd = get_unused_fd_flags(O_CLOEXEC); 2433babf3164SAndrii Nakryiko if (fd < 0) 2434a3b80e10SAndrii Nakryiko return fd; 2435babf3164SAndrii Nakryiko 2436babf3164SAndrii Nakryiko 2437a3b80e10SAndrii Nakryiko id = bpf_link_alloc_id(link); 2438a3b80e10SAndrii Nakryiko if (id < 0) { 2439a3b80e10SAndrii Nakryiko put_unused_fd(fd); 2440a3b80e10SAndrii Nakryiko return id; 2441a3b80e10SAndrii Nakryiko } 2442a3b80e10SAndrii Nakryiko 2443138c6767SAndrii Nakryiko file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC); 2444138c6767SAndrii Nakryiko if (IS_ERR(file)) { 2445138c6767SAndrii Nakryiko bpf_link_free_id(id); 2446138c6767SAndrii Nakryiko put_unused_fd(fd); 2447138c6767SAndrii Nakryiko return PTR_ERR(file); 2448138c6767SAndrii Nakryiko } 2449138c6767SAndrii Nakryiko 2450a3b80e10SAndrii Nakryiko primer->link = link; 2451a3b80e10SAndrii Nakryiko primer->file = file; 2452a3b80e10SAndrii Nakryiko primer->fd = fd; 2453a3b80e10SAndrii Nakryiko primer->id = id; 2454a3b80e10SAndrii Nakryiko return 0; 2455a3b80e10SAndrii Nakryiko } 2456a3b80e10SAndrii Nakryiko 2457a3b80e10SAndrii Nakryiko int bpf_link_settle(struct bpf_link_primer *primer) 2458a3b80e10SAndrii Nakryiko { 2459a3b80e10SAndrii Nakryiko /* make bpf_link fetchable by ID */ 2460a3b80e10SAndrii Nakryiko spin_lock_bh(&link_idr_lock); 2461a3b80e10SAndrii Nakryiko primer->link->id = primer->id; 2462a3b80e10SAndrii Nakryiko spin_unlock_bh(&link_idr_lock); 2463a3b80e10SAndrii Nakryiko /* make bpf_link fetchable by FD */ 2464a3b80e10SAndrii Nakryiko fd_install(primer->fd, primer->file); 2465a3b80e10SAndrii Nakryiko /* pass through installed FD */ 2466a3b80e10SAndrii Nakryiko return primer->fd; 2467a3b80e10SAndrii Nakryiko } 2468a3b80e10SAndrii Nakryiko 2469a3b80e10SAndrii Nakryiko int bpf_link_new_fd(struct bpf_link *link) 2470a3b80e10SAndrii Nakryiko { 2471a3b80e10SAndrii Nakryiko return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC); 2472babf3164SAndrii Nakryiko } 2473babf3164SAndrii Nakryiko 247470ed506cSAndrii Nakryiko struct bpf_link *bpf_link_get_from_fd(u32 ufd) 247570ed506cSAndrii Nakryiko { 247670ed506cSAndrii Nakryiko struct fd f = fdget(ufd); 247770ed506cSAndrii Nakryiko struct bpf_link *link; 247870ed506cSAndrii Nakryiko 247970ed506cSAndrii Nakryiko if (!f.file) 248070ed506cSAndrii Nakryiko return ERR_PTR(-EBADF); 248170ed506cSAndrii Nakryiko if (f.file->f_op != &bpf_link_fops) { 248270ed506cSAndrii Nakryiko fdput(f); 248370ed506cSAndrii Nakryiko return ERR_PTR(-EINVAL); 248470ed506cSAndrii Nakryiko } 248570ed506cSAndrii Nakryiko 248670ed506cSAndrii Nakryiko link = f.file->private_data; 248770ed506cSAndrii Nakryiko bpf_link_inc(link); 248870ed506cSAndrii Nakryiko fdput(f); 248970ed506cSAndrii Nakryiko 249070ed506cSAndrii Nakryiko return link; 249170ed506cSAndrii Nakryiko } 249270ed506cSAndrii Nakryiko 249370ed506cSAndrii Nakryiko struct bpf_tracing_link { 249470ed506cSAndrii Nakryiko struct bpf_link link; 2495f2e10bffSAndrii Nakryiko enum bpf_attach_type attach_type; 249670ed506cSAndrii Nakryiko }; 249770ed506cSAndrii Nakryiko 249870ed506cSAndrii Nakryiko static void bpf_tracing_link_release(struct bpf_link *link) 249970ed506cSAndrii Nakryiko { 2500babf3164SAndrii Nakryiko WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog)); 2501babf3164SAndrii Nakryiko } 2502babf3164SAndrii Nakryiko 2503babf3164SAndrii Nakryiko static void bpf_tracing_link_dealloc(struct bpf_link *link) 2504babf3164SAndrii Nakryiko { 250570ed506cSAndrii Nakryiko struct bpf_tracing_link *tr_link = 250670ed506cSAndrii Nakryiko container_of(link, struct bpf_tracing_link, link); 250770ed506cSAndrii Nakryiko 250870ed506cSAndrii Nakryiko kfree(tr_link); 250970ed506cSAndrii Nakryiko } 251070ed506cSAndrii Nakryiko 2511f2e10bffSAndrii Nakryiko static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link, 2512f2e10bffSAndrii Nakryiko struct seq_file *seq) 2513f2e10bffSAndrii Nakryiko { 2514f2e10bffSAndrii Nakryiko struct bpf_tracing_link *tr_link = 2515f2e10bffSAndrii Nakryiko container_of(link, struct bpf_tracing_link, link); 2516f2e10bffSAndrii Nakryiko 2517f2e10bffSAndrii Nakryiko seq_printf(seq, 2518f2e10bffSAndrii Nakryiko "attach_type:\t%d\n", 2519f2e10bffSAndrii Nakryiko tr_link->attach_type); 2520f2e10bffSAndrii Nakryiko } 2521f2e10bffSAndrii Nakryiko 2522f2e10bffSAndrii Nakryiko static int bpf_tracing_link_fill_link_info(const struct bpf_link *link, 2523f2e10bffSAndrii Nakryiko struct bpf_link_info *info) 2524f2e10bffSAndrii Nakryiko { 2525f2e10bffSAndrii Nakryiko struct bpf_tracing_link *tr_link = 2526f2e10bffSAndrii Nakryiko container_of(link, struct bpf_tracing_link, link); 2527f2e10bffSAndrii Nakryiko 2528f2e10bffSAndrii Nakryiko info->tracing.attach_type = tr_link->attach_type; 2529f2e10bffSAndrii Nakryiko 2530f2e10bffSAndrii Nakryiko return 0; 2531f2e10bffSAndrii Nakryiko } 2532f2e10bffSAndrii Nakryiko 253370ed506cSAndrii Nakryiko static const struct bpf_link_ops bpf_tracing_link_lops = { 253470ed506cSAndrii Nakryiko .release = bpf_tracing_link_release, 2535babf3164SAndrii Nakryiko .dealloc = bpf_tracing_link_dealloc, 2536f2e10bffSAndrii Nakryiko .show_fdinfo = bpf_tracing_link_show_fdinfo, 2537f2e10bffSAndrii Nakryiko .fill_link_info = bpf_tracing_link_fill_link_info, 253870ed506cSAndrii Nakryiko }; 253970ed506cSAndrii Nakryiko 2540fec56f58SAlexei Starovoitov static int bpf_tracing_prog_attach(struct bpf_prog *prog) 2541fec56f58SAlexei Starovoitov { 2542a3b80e10SAndrii Nakryiko struct bpf_link_primer link_primer; 254370ed506cSAndrii Nakryiko struct bpf_tracing_link *link; 2544a3b80e10SAndrii Nakryiko int err; 2545fec56f58SAlexei Starovoitov 25469e4e01dfSKP Singh switch (prog->type) { 25479e4e01dfSKP Singh case BPF_PROG_TYPE_TRACING: 2548fec56f58SAlexei Starovoitov if (prog->expected_attach_type != BPF_TRACE_FENTRY && 2549be8704ffSAlexei Starovoitov prog->expected_attach_type != BPF_TRACE_FEXIT && 25509e4e01dfSKP Singh prog->expected_attach_type != BPF_MODIFY_RETURN) { 25519e4e01dfSKP Singh err = -EINVAL; 25529e4e01dfSKP Singh goto out_put_prog; 25539e4e01dfSKP Singh } 25549e4e01dfSKP Singh break; 25559e4e01dfSKP Singh case BPF_PROG_TYPE_EXT: 25569e4e01dfSKP Singh if (prog->expected_attach_type != 0) { 25579e4e01dfSKP Singh err = -EINVAL; 25589e4e01dfSKP Singh goto out_put_prog; 25599e4e01dfSKP Singh } 25609e4e01dfSKP Singh break; 25619e4e01dfSKP Singh case BPF_PROG_TYPE_LSM: 25629e4e01dfSKP Singh if (prog->expected_attach_type != BPF_LSM_MAC) { 25639e4e01dfSKP Singh err = -EINVAL; 25649e4e01dfSKP Singh goto out_put_prog; 25659e4e01dfSKP Singh } 25669e4e01dfSKP Singh break; 25679e4e01dfSKP Singh default: 2568fec56f58SAlexei Starovoitov err = -EINVAL; 2569fec56f58SAlexei Starovoitov goto out_put_prog; 2570fec56f58SAlexei Starovoitov } 2571fec56f58SAlexei Starovoitov 257270ed506cSAndrii Nakryiko link = kzalloc(sizeof(*link), GFP_USER); 257370ed506cSAndrii Nakryiko if (!link) { 257470ed506cSAndrii Nakryiko err = -ENOMEM; 2575fec56f58SAlexei Starovoitov goto out_put_prog; 2576fec56f58SAlexei Starovoitov } 2577f2e10bffSAndrii Nakryiko bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING, 2578f2e10bffSAndrii Nakryiko &bpf_tracing_link_lops, prog); 2579f2e10bffSAndrii Nakryiko link->attach_type = prog->expected_attach_type; 2580fec56f58SAlexei Starovoitov 2581a3b80e10SAndrii Nakryiko err = bpf_link_prime(&link->link, &link_primer); 2582a3b80e10SAndrii Nakryiko if (err) { 2583babf3164SAndrii Nakryiko kfree(link); 2584babf3164SAndrii Nakryiko goto out_put_prog; 258570ed506cSAndrii Nakryiko } 2586babf3164SAndrii Nakryiko 2587babf3164SAndrii Nakryiko err = bpf_trampoline_link_prog(prog); 2588babf3164SAndrii Nakryiko if (err) { 2589a3b80e10SAndrii Nakryiko bpf_link_cleanup(&link_primer); 2590babf3164SAndrii Nakryiko goto out_put_prog; 2591babf3164SAndrii Nakryiko } 2592babf3164SAndrii Nakryiko 2593a3b80e10SAndrii Nakryiko return bpf_link_settle(&link_primer); 2594fec56f58SAlexei Starovoitov out_put_prog: 2595fec56f58SAlexei Starovoitov bpf_prog_put(prog); 2596fec56f58SAlexei Starovoitov return err; 2597fec56f58SAlexei Starovoitov } 2598fec56f58SAlexei Starovoitov 259970ed506cSAndrii Nakryiko struct bpf_raw_tp_link { 260070ed506cSAndrii Nakryiko struct bpf_link link; 2601c4f6699dSAlexei Starovoitov struct bpf_raw_event_map *btp; 2602c4f6699dSAlexei Starovoitov }; 2603c4f6699dSAlexei Starovoitov 260470ed506cSAndrii Nakryiko static void bpf_raw_tp_link_release(struct bpf_link *link) 2605c4f6699dSAlexei Starovoitov { 260670ed506cSAndrii Nakryiko struct bpf_raw_tp_link *raw_tp = 260770ed506cSAndrii Nakryiko container_of(link, struct bpf_raw_tp_link, link); 2608c4f6699dSAlexei Starovoitov 260970ed506cSAndrii Nakryiko bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog); 2610a38d1107SMatt Mullins bpf_put_raw_tracepoint(raw_tp->btp); 2611babf3164SAndrii Nakryiko } 2612babf3164SAndrii Nakryiko 2613babf3164SAndrii Nakryiko static void bpf_raw_tp_link_dealloc(struct bpf_link *link) 2614babf3164SAndrii Nakryiko { 2615babf3164SAndrii Nakryiko struct bpf_raw_tp_link *raw_tp = 2616babf3164SAndrii Nakryiko container_of(link, struct bpf_raw_tp_link, link); 2617babf3164SAndrii Nakryiko 2618c4f6699dSAlexei Starovoitov kfree(raw_tp); 2619c4f6699dSAlexei Starovoitov } 2620c4f6699dSAlexei Starovoitov 2621f2e10bffSAndrii Nakryiko static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link, 2622f2e10bffSAndrii Nakryiko struct seq_file *seq) 2623f2e10bffSAndrii Nakryiko { 2624f2e10bffSAndrii Nakryiko struct bpf_raw_tp_link *raw_tp_link = 2625f2e10bffSAndrii Nakryiko container_of(link, struct bpf_raw_tp_link, link); 2626f2e10bffSAndrii Nakryiko 2627f2e10bffSAndrii Nakryiko seq_printf(seq, 2628f2e10bffSAndrii Nakryiko "tp_name:\t%s\n", 2629f2e10bffSAndrii Nakryiko raw_tp_link->btp->tp->name); 2630f2e10bffSAndrii Nakryiko } 2631f2e10bffSAndrii Nakryiko 2632f2e10bffSAndrii Nakryiko static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, 2633f2e10bffSAndrii Nakryiko struct bpf_link_info *info) 2634f2e10bffSAndrii Nakryiko { 2635f2e10bffSAndrii Nakryiko struct bpf_raw_tp_link *raw_tp_link = 2636f2e10bffSAndrii Nakryiko container_of(link, struct bpf_raw_tp_link, link); 2637f2e10bffSAndrii Nakryiko char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name); 2638f2e10bffSAndrii Nakryiko const char *tp_name = raw_tp_link->btp->tp->name; 2639f2e10bffSAndrii Nakryiko u32 ulen = info->raw_tracepoint.tp_name_len; 2640f2e10bffSAndrii Nakryiko size_t tp_len = strlen(tp_name); 2641f2e10bffSAndrii Nakryiko 2642f2e10bffSAndrii Nakryiko if (ulen && !ubuf) 2643f2e10bffSAndrii Nakryiko return -EINVAL; 2644f2e10bffSAndrii Nakryiko 2645f2e10bffSAndrii Nakryiko info->raw_tracepoint.tp_name_len = tp_len + 1; 2646f2e10bffSAndrii Nakryiko 2647f2e10bffSAndrii Nakryiko if (!ubuf) 2648f2e10bffSAndrii Nakryiko return 0; 2649f2e10bffSAndrii Nakryiko 2650f2e10bffSAndrii Nakryiko if (ulen >= tp_len + 1) { 2651f2e10bffSAndrii Nakryiko if (copy_to_user(ubuf, tp_name, tp_len + 1)) 2652f2e10bffSAndrii Nakryiko return -EFAULT; 2653f2e10bffSAndrii Nakryiko } else { 2654f2e10bffSAndrii Nakryiko char zero = '\0'; 2655f2e10bffSAndrii Nakryiko 2656f2e10bffSAndrii Nakryiko if (copy_to_user(ubuf, tp_name, ulen - 1)) 2657f2e10bffSAndrii Nakryiko return -EFAULT; 2658f2e10bffSAndrii Nakryiko if (put_user(zero, ubuf + ulen - 1)) 2659f2e10bffSAndrii Nakryiko return -EFAULT; 2660f2e10bffSAndrii Nakryiko return -ENOSPC; 2661f2e10bffSAndrii Nakryiko } 2662f2e10bffSAndrii Nakryiko 2663f2e10bffSAndrii Nakryiko return 0; 2664f2e10bffSAndrii Nakryiko } 2665f2e10bffSAndrii Nakryiko 2666a3b80e10SAndrii Nakryiko static const struct bpf_link_ops bpf_raw_tp_link_lops = { 266770ed506cSAndrii Nakryiko .release = bpf_raw_tp_link_release, 2668babf3164SAndrii Nakryiko .dealloc = bpf_raw_tp_link_dealloc, 2669f2e10bffSAndrii Nakryiko .show_fdinfo = bpf_raw_tp_link_show_fdinfo, 2670f2e10bffSAndrii Nakryiko .fill_link_info = bpf_raw_tp_link_fill_link_info, 2671c4f6699dSAlexei Starovoitov }; 2672c4f6699dSAlexei Starovoitov 2673c4f6699dSAlexei Starovoitov #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd 2674c4f6699dSAlexei Starovoitov 2675c4f6699dSAlexei Starovoitov static int bpf_raw_tracepoint_open(const union bpf_attr *attr) 2676c4f6699dSAlexei Starovoitov { 2677a3b80e10SAndrii Nakryiko struct bpf_link_primer link_primer; 2678babf3164SAndrii Nakryiko struct bpf_raw_tp_link *link; 2679c4f6699dSAlexei Starovoitov struct bpf_raw_event_map *btp; 2680c4f6699dSAlexei Starovoitov struct bpf_prog *prog; 2681ac4414b5SAlexei Starovoitov const char *tp_name; 2682ac4414b5SAlexei Starovoitov char buf[128]; 2683a3b80e10SAndrii Nakryiko int err; 2684c4f6699dSAlexei Starovoitov 2685ac4414b5SAlexei Starovoitov if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN)) 2686ac4414b5SAlexei Starovoitov return -EINVAL; 2687ac4414b5SAlexei Starovoitov 2688ac4414b5SAlexei Starovoitov prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); 2689ac4414b5SAlexei Starovoitov if (IS_ERR(prog)) 2690ac4414b5SAlexei Starovoitov return PTR_ERR(prog); 2691ac4414b5SAlexei Starovoitov 26929e4e01dfSKP Singh switch (prog->type) { 26939e4e01dfSKP Singh case BPF_PROG_TYPE_TRACING: 26949e4e01dfSKP Singh case BPF_PROG_TYPE_EXT: 26959e4e01dfSKP Singh case BPF_PROG_TYPE_LSM: 2696ac4414b5SAlexei Starovoitov if (attr->raw_tracepoint.name) { 2697fec56f58SAlexei Starovoitov /* The attach point for this category of programs 2698fec56f58SAlexei Starovoitov * should be specified via btf_id during program load. 2699ac4414b5SAlexei Starovoitov */ 2700ac4414b5SAlexei Starovoitov err = -EINVAL; 2701ac4414b5SAlexei Starovoitov goto out_put_prog; 2702ac4414b5SAlexei Starovoitov } 27039e4e01dfSKP Singh if (prog->type == BPF_PROG_TYPE_TRACING && 27049e4e01dfSKP Singh prog->expected_attach_type == BPF_TRACE_RAW_TP) { 270538207291SMartin KaFai Lau tp_name = prog->aux->attach_func_name; 27069e4e01dfSKP Singh break; 27079e4e01dfSKP Singh } 2708fec56f58SAlexei Starovoitov return bpf_tracing_prog_attach(prog); 27099e4e01dfSKP Singh case BPF_PROG_TYPE_RAW_TRACEPOINT: 27109e4e01dfSKP Singh case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: 2711ac4414b5SAlexei Starovoitov if (strncpy_from_user(buf, 2712ac4414b5SAlexei Starovoitov u64_to_user_ptr(attr->raw_tracepoint.name), 2713ac4414b5SAlexei Starovoitov sizeof(buf) - 1) < 0) { 2714ac4414b5SAlexei Starovoitov err = -EFAULT; 2715ac4414b5SAlexei Starovoitov goto out_put_prog; 2716ac4414b5SAlexei Starovoitov } 2717ac4414b5SAlexei Starovoitov buf[sizeof(buf) - 1] = 0; 2718ac4414b5SAlexei Starovoitov tp_name = buf; 27199e4e01dfSKP Singh break; 27209e4e01dfSKP Singh default: 27219e4e01dfSKP Singh err = -EINVAL; 27229e4e01dfSKP Singh goto out_put_prog; 2723ac4414b5SAlexei Starovoitov } 2724c4f6699dSAlexei Starovoitov 2725a38d1107SMatt Mullins btp = bpf_get_raw_tracepoint(tp_name); 2726ac4414b5SAlexei Starovoitov if (!btp) { 2727ac4414b5SAlexei Starovoitov err = -ENOENT; 2728ac4414b5SAlexei Starovoitov goto out_put_prog; 2729ac4414b5SAlexei Starovoitov } 2730c4f6699dSAlexei Starovoitov 2731babf3164SAndrii Nakryiko link = kzalloc(sizeof(*link), GFP_USER); 2732babf3164SAndrii Nakryiko if (!link) { 2733a38d1107SMatt Mullins err = -ENOMEM; 2734a38d1107SMatt Mullins goto out_put_btp; 2735a38d1107SMatt Mullins } 2736f2e10bffSAndrii Nakryiko bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT, 2737f2e10bffSAndrii Nakryiko &bpf_raw_tp_link_lops, prog); 2738babf3164SAndrii Nakryiko link->btp = btp; 2739c4f6699dSAlexei Starovoitov 2740a3b80e10SAndrii Nakryiko err = bpf_link_prime(&link->link, &link_primer); 2741a3b80e10SAndrii Nakryiko if (err) { 2742babf3164SAndrii Nakryiko kfree(link); 2743babf3164SAndrii Nakryiko goto out_put_btp; 2744c4f6699dSAlexei Starovoitov } 2745babf3164SAndrii Nakryiko 2746babf3164SAndrii Nakryiko err = bpf_probe_register(link->btp, prog); 2747babf3164SAndrii Nakryiko if (err) { 2748a3b80e10SAndrii Nakryiko bpf_link_cleanup(&link_primer); 2749babf3164SAndrii Nakryiko goto out_put_btp; 2750babf3164SAndrii Nakryiko } 2751babf3164SAndrii Nakryiko 2752a3b80e10SAndrii Nakryiko return bpf_link_settle(&link_primer); 2753c4f6699dSAlexei Starovoitov 2754a38d1107SMatt Mullins out_put_btp: 2755a38d1107SMatt Mullins bpf_put_raw_tracepoint(btp); 2756ac4414b5SAlexei Starovoitov out_put_prog: 2757ac4414b5SAlexei Starovoitov bpf_prog_put(prog); 2758c4f6699dSAlexei Starovoitov return err; 2759c4f6699dSAlexei Starovoitov } 2760c4f6699dSAlexei Starovoitov 276133491588SAnders Roxell static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, 276233491588SAnders Roxell enum bpf_attach_type attach_type) 276333491588SAnders Roxell { 276433491588SAnders Roxell switch (prog->type) { 276533491588SAnders Roxell case BPF_PROG_TYPE_CGROUP_SOCK: 276633491588SAnders Roxell case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 27670d01da6aSStanislav Fomichev case BPF_PROG_TYPE_CGROUP_SOCKOPT: 276833491588SAnders Roxell return attach_type == prog->expected_attach_type ? 0 : -EINVAL; 27695cf1e914Sbrakmo case BPF_PROG_TYPE_CGROUP_SKB: 27702c78ee89SAlexei Starovoitov if (!capable(CAP_NET_ADMIN)) 27712c78ee89SAlexei Starovoitov /* cg-skb progs can be loaded by unpriv user. 27722c78ee89SAlexei Starovoitov * check permissions at attach time. 27732c78ee89SAlexei Starovoitov */ 27742c78ee89SAlexei Starovoitov return -EPERM; 27755cf1e914Sbrakmo return prog->enforce_expected_attach_type && 27765cf1e914Sbrakmo prog->expected_attach_type != attach_type ? 27775cf1e914Sbrakmo -EINVAL : 0; 277833491588SAnders Roxell default: 277933491588SAnders Roxell return 0; 278033491588SAnders Roxell } 278133491588SAnders Roxell } 278233491588SAnders Roxell 2783e28784e3SAndrii Nakryiko static enum bpf_prog_type 2784e28784e3SAndrii Nakryiko attach_type_to_prog_type(enum bpf_attach_type attach_type) 2785e28784e3SAndrii Nakryiko { 2786e28784e3SAndrii Nakryiko switch (attach_type) { 2787e28784e3SAndrii Nakryiko case BPF_CGROUP_INET_INGRESS: 2788e28784e3SAndrii Nakryiko case BPF_CGROUP_INET_EGRESS: 2789e28784e3SAndrii Nakryiko return BPF_PROG_TYPE_CGROUP_SKB; 2790e28784e3SAndrii Nakryiko break; 2791e28784e3SAndrii Nakryiko case BPF_CGROUP_INET_SOCK_CREATE: 2792e28784e3SAndrii Nakryiko case BPF_CGROUP_INET4_POST_BIND: 2793e28784e3SAndrii Nakryiko case BPF_CGROUP_INET6_POST_BIND: 2794e28784e3SAndrii Nakryiko return BPF_PROG_TYPE_CGROUP_SOCK; 2795e28784e3SAndrii Nakryiko case BPF_CGROUP_INET4_BIND: 2796e28784e3SAndrii Nakryiko case BPF_CGROUP_INET6_BIND: 2797e28784e3SAndrii Nakryiko case BPF_CGROUP_INET4_CONNECT: 2798e28784e3SAndrii Nakryiko case BPF_CGROUP_INET6_CONNECT: 27991b66d253SDaniel Borkmann case BPF_CGROUP_INET4_GETPEERNAME: 28001b66d253SDaniel Borkmann case BPF_CGROUP_INET6_GETPEERNAME: 28011b66d253SDaniel Borkmann case BPF_CGROUP_INET4_GETSOCKNAME: 28021b66d253SDaniel Borkmann case BPF_CGROUP_INET6_GETSOCKNAME: 2803e28784e3SAndrii Nakryiko case BPF_CGROUP_UDP4_SENDMSG: 2804e28784e3SAndrii Nakryiko case BPF_CGROUP_UDP6_SENDMSG: 2805e28784e3SAndrii Nakryiko case BPF_CGROUP_UDP4_RECVMSG: 2806e28784e3SAndrii Nakryiko case BPF_CGROUP_UDP6_RECVMSG: 2807e28784e3SAndrii Nakryiko return BPF_PROG_TYPE_CGROUP_SOCK_ADDR; 2808e28784e3SAndrii Nakryiko case BPF_CGROUP_SOCK_OPS: 2809e28784e3SAndrii Nakryiko return BPF_PROG_TYPE_SOCK_OPS; 2810e28784e3SAndrii Nakryiko case BPF_CGROUP_DEVICE: 2811e28784e3SAndrii Nakryiko return BPF_PROG_TYPE_CGROUP_DEVICE; 2812e28784e3SAndrii Nakryiko case BPF_SK_MSG_VERDICT: 2813e28784e3SAndrii Nakryiko return BPF_PROG_TYPE_SK_MSG; 2814e28784e3SAndrii Nakryiko case BPF_SK_SKB_STREAM_PARSER: 2815e28784e3SAndrii Nakryiko case BPF_SK_SKB_STREAM_VERDICT: 2816e28784e3SAndrii Nakryiko return BPF_PROG_TYPE_SK_SKB; 2817e28784e3SAndrii Nakryiko case BPF_LIRC_MODE2: 2818e28784e3SAndrii Nakryiko return BPF_PROG_TYPE_LIRC_MODE2; 2819e28784e3SAndrii Nakryiko case BPF_FLOW_DISSECTOR: 2820e28784e3SAndrii Nakryiko return BPF_PROG_TYPE_FLOW_DISSECTOR; 2821e28784e3SAndrii Nakryiko case BPF_CGROUP_SYSCTL: 2822e28784e3SAndrii Nakryiko return BPF_PROG_TYPE_CGROUP_SYSCTL; 2823e28784e3SAndrii Nakryiko case BPF_CGROUP_GETSOCKOPT: 2824e28784e3SAndrii Nakryiko case BPF_CGROUP_SETSOCKOPT: 2825e28784e3SAndrii Nakryiko return BPF_PROG_TYPE_CGROUP_SOCKOPT; 2826de4e05caSYonghong Song case BPF_TRACE_ITER: 2827de4e05caSYonghong Song return BPF_PROG_TYPE_TRACING; 2828e28784e3SAndrii Nakryiko default: 2829e28784e3SAndrii Nakryiko return BPF_PROG_TYPE_UNSPEC; 2830e28784e3SAndrii Nakryiko } 2831e28784e3SAndrii Nakryiko } 2832e28784e3SAndrii Nakryiko 28337dd68b32SAndrey Ignatov #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd 2834174a79ffSJohn Fastabend 2835324bda9eSAlexei Starovoitov #define BPF_F_ATTACH_MASK \ 28367dd68b32SAndrey Ignatov (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE) 2837324bda9eSAlexei Starovoitov 2838f4324551SDaniel Mack static int bpf_prog_attach(const union bpf_attr *attr) 2839f4324551SDaniel Mack { 28407f677633SAlexei Starovoitov enum bpf_prog_type ptype; 2841f4324551SDaniel Mack struct bpf_prog *prog; 28427f677633SAlexei Starovoitov int ret; 2843f4324551SDaniel Mack 2844f4324551SDaniel Mack if (CHECK_ATTR(BPF_PROG_ATTACH)) 2845f4324551SDaniel Mack return -EINVAL; 2846f4324551SDaniel Mack 2847324bda9eSAlexei Starovoitov if (attr->attach_flags & ~BPF_F_ATTACH_MASK) 28487f677633SAlexei Starovoitov return -EINVAL; 28497f677633SAlexei Starovoitov 2850e28784e3SAndrii Nakryiko ptype = attach_type_to_prog_type(attr->attach_type); 2851e28784e3SAndrii Nakryiko if (ptype == BPF_PROG_TYPE_UNSPEC) 2852b2cd1257SDavid Ahern return -EINVAL; 2853b2cd1257SDavid Ahern 2854b2cd1257SDavid Ahern prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); 2855f4324551SDaniel Mack if (IS_ERR(prog)) 2856f4324551SDaniel Mack return PTR_ERR(prog); 2857f4324551SDaniel Mack 28585e43f899SAndrey Ignatov if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { 28595e43f899SAndrey Ignatov bpf_prog_put(prog); 28605e43f899SAndrey Ignatov return -EINVAL; 28615e43f899SAndrey Ignatov } 28625e43f899SAndrey Ignatov 2863fdb5c453SSean Young switch (ptype) { 2864fdb5c453SSean Young case BPF_PROG_TYPE_SK_SKB: 2865fdb5c453SSean Young case BPF_PROG_TYPE_SK_MSG: 2866604326b4SDaniel Borkmann ret = sock_map_get_from_fd(attr, prog); 2867fdb5c453SSean Young break; 2868fdb5c453SSean Young case BPF_PROG_TYPE_LIRC_MODE2: 2869fdb5c453SSean Young ret = lirc_prog_attach(attr, prog); 2870fdb5c453SSean Young break; 2871d58e468bSPetar Penkov case BPF_PROG_TYPE_FLOW_DISSECTOR: 2872a3fd7ceeSJakub Sitnicki ret = netns_bpf_prog_attach(attr, prog); 2873d58e468bSPetar Penkov break; 2874e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_DEVICE: 2875e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SKB: 2876e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SOCK: 2877e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2878e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2879e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SYSCTL: 2880e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_SOCK_OPS: 2881fdb5c453SSean Young ret = cgroup_bpf_prog_attach(attr, ptype, prog); 2882e28784e3SAndrii Nakryiko break; 2883e28784e3SAndrii Nakryiko default: 2884e28784e3SAndrii Nakryiko ret = -EINVAL; 2885f4324551SDaniel Mack } 2886f4324551SDaniel Mack 28877f677633SAlexei Starovoitov if (ret) 28887f677633SAlexei Starovoitov bpf_prog_put(prog); 28897f677633SAlexei Starovoitov return ret; 2890f4324551SDaniel Mack } 2891f4324551SDaniel Mack 2892f4324551SDaniel Mack #define BPF_PROG_DETACH_LAST_FIELD attach_type 2893f4324551SDaniel Mack 2894f4324551SDaniel Mack static int bpf_prog_detach(const union bpf_attr *attr) 2895f4324551SDaniel Mack { 2896324bda9eSAlexei Starovoitov enum bpf_prog_type ptype; 2897f4324551SDaniel Mack 2898f4324551SDaniel Mack if (CHECK_ATTR(BPF_PROG_DETACH)) 2899f4324551SDaniel Mack return -EINVAL; 2900f4324551SDaniel Mack 2901e28784e3SAndrii Nakryiko ptype = attach_type_to_prog_type(attr->attach_type); 2902e28784e3SAndrii Nakryiko 2903e28784e3SAndrii Nakryiko switch (ptype) { 2904e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_SK_MSG: 2905e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_SK_SKB: 2906604326b4SDaniel Borkmann return sock_map_get_from_fd(attr, NULL); 2907e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_LIRC_MODE2: 2908f4364dcfSSean Young return lirc_prog_detach(attr); 2909e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_FLOW_DISSECTOR: 29102c78ee89SAlexei Starovoitov if (!capable(CAP_NET_ADMIN)) 29112c78ee89SAlexei Starovoitov return -EPERM; 2912a3fd7ceeSJakub Sitnicki return netns_bpf_prog_detach(attr); 2913e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_DEVICE: 2914e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SKB: 2915e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SOCK: 2916e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 2917e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SOCKOPT: 2918e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SYSCTL: 2919e28784e3SAndrii Nakryiko case BPF_PROG_TYPE_SOCK_OPS: 2920e28784e3SAndrii Nakryiko return cgroup_bpf_prog_detach(attr, ptype); 2921f4324551SDaniel Mack default: 2922f4324551SDaniel Mack return -EINVAL; 2923f4324551SDaniel Mack } 2924f4324551SDaniel Mack } 292540304b2aSLawrence Brakmo 2926468e2f64SAlexei Starovoitov #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt 2927468e2f64SAlexei Starovoitov 2928468e2f64SAlexei Starovoitov static int bpf_prog_query(const union bpf_attr *attr, 2929468e2f64SAlexei Starovoitov union bpf_attr __user *uattr) 2930468e2f64SAlexei Starovoitov { 2931468e2f64SAlexei Starovoitov if (!capable(CAP_NET_ADMIN)) 2932468e2f64SAlexei Starovoitov return -EPERM; 2933468e2f64SAlexei Starovoitov if (CHECK_ATTR(BPF_PROG_QUERY)) 2934468e2f64SAlexei Starovoitov return -EINVAL; 2935468e2f64SAlexei Starovoitov if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) 2936468e2f64SAlexei Starovoitov return -EINVAL; 2937468e2f64SAlexei Starovoitov 2938468e2f64SAlexei Starovoitov switch (attr->query.attach_type) { 2939468e2f64SAlexei Starovoitov case BPF_CGROUP_INET_INGRESS: 2940468e2f64SAlexei Starovoitov case BPF_CGROUP_INET_EGRESS: 2941468e2f64SAlexei Starovoitov case BPF_CGROUP_INET_SOCK_CREATE: 29424fbac77dSAndrey Ignatov case BPF_CGROUP_INET4_BIND: 29434fbac77dSAndrey Ignatov case BPF_CGROUP_INET6_BIND: 2944aac3fc32SAndrey Ignatov case BPF_CGROUP_INET4_POST_BIND: 2945aac3fc32SAndrey Ignatov case BPF_CGROUP_INET6_POST_BIND: 2946d74bad4eSAndrey Ignatov case BPF_CGROUP_INET4_CONNECT: 2947d74bad4eSAndrey Ignatov case BPF_CGROUP_INET6_CONNECT: 29481b66d253SDaniel Borkmann case BPF_CGROUP_INET4_GETPEERNAME: 29491b66d253SDaniel Borkmann case BPF_CGROUP_INET6_GETPEERNAME: 29501b66d253SDaniel Borkmann case BPF_CGROUP_INET4_GETSOCKNAME: 29511b66d253SDaniel Borkmann case BPF_CGROUP_INET6_GETSOCKNAME: 29521cedee13SAndrey Ignatov case BPF_CGROUP_UDP4_SENDMSG: 29531cedee13SAndrey Ignatov case BPF_CGROUP_UDP6_SENDMSG: 2954983695faSDaniel Borkmann case BPF_CGROUP_UDP4_RECVMSG: 2955983695faSDaniel Borkmann case BPF_CGROUP_UDP6_RECVMSG: 2956468e2f64SAlexei Starovoitov case BPF_CGROUP_SOCK_OPS: 2957ebc614f6SRoman Gushchin case BPF_CGROUP_DEVICE: 29587b146cebSAndrey Ignatov case BPF_CGROUP_SYSCTL: 29590d01da6aSStanislav Fomichev case BPF_CGROUP_GETSOCKOPT: 29600d01da6aSStanislav Fomichev case BPF_CGROUP_SETSOCKOPT: 2961e28784e3SAndrii Nakryiko return cgroup_bpf_prog_query(attr, uattr); 2962f4364dcfSSean Young case BPF_LIRC_MODE2: 2963f4364dcfSSean Young return lirc_prog_query(attr, uattr); 2964118c8e9aSStanislav Fomichev case BPF_FLOW_DISSECTOR: 2965a3fd7ceeSJakub Sitnicki return netns_bpf_prog_query(attr, uattr); 2966468e2f64SAlexei Starovoitov default: 2967468e2f64SAlexei Starovoitov return -EINVAL; 2968468e2f64SAlexei Starovoitov } 2969468e2f64SAlexei Starovoitov } 2970f4324551SDaniel Mack 2971b0b9395dSStanislav Fomichev #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out 29721cf1cae9SAlexei Starovoitov 29731cf1cae9SAlexei Starovoitov static int bpf_prog_test_run(const union bpf_attr *attr, 29741cf1cae9SAlexei Starovoitov union bpf_attr __user *uattr) 29751cf1cae9SAlexei Starovoitov { 29761cf1cae9SAlexei Starovoitov struct bpf_prog *prog; 29771cf1cae9SAlexei Starovoitov int ret = -ENOTSUPP; 29781cf1cae9SAlexei Starovoitov 29791cf1cae9SAlexei Starovoitov if (CHECK_ATTR(BPF_PROG_TEST_RUN)) 29801cf1cae9SAlexei Starovoitov return -EINVAL; 29811cf1cae9SAlexei Starovoitov 2982b0b9395dSStanislav Fomichev if ((attr->test.ctx_size_in && !attr->test.ctx_in) || 2983b0b9395dSStanislav Fomichev (!attr->test.ctx_size_in && attr->test.ctx_in)) 2984b0b9395dSStanislav Fomichev return -EINVAL; 2985b0b9395dSStanislav Fomichev 2986b0b9395dSStanislav Fomichev if ((attr->test.ctx_size_out && !attr->test.ctx_out) || 2987b0b9395dSStanislav Fomichev (!attr->test.ctx_size_out && attr->test.ctx_out)) 2988b0b9395dSStanislav Fomichev return -EINVAL; 2989b0b9395dSStanislav Fomichev 29901cf1cae9SAlexei Starovoitov prog = bpf_prog_get(attr->test.prog_fd); 29911cf1cae9SAlexei Starovoitov if (IS_ERR(prog)) 29921cf1cae9SAlexei Starovoitov return PTR_ERR(prog); 29931cf1cae9SAlexei Starovoitov 29941cf1cae9SAlexei Starovoitov if (prog->aux->ops->test_run) 29951cf1cae9SAlexei Starovoitov ret = prog->aux->ops->test_run(prog, attr, uattr); 29961cf1cae9SAlexei Starovoitov 29971cf1cae9SAlexei Starovoitov bpf_prog_put(prog); 29981cf1cae9SAlexei Starovoitov return ret; 29991cf1cae9SAlexei Starovoitov } 30001cf1cae9SAlexei Starovoitov 300134ad5580SMartin KaFai Lau #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id 300234ad5580SMartin KaFai Lau 300334ad5580SMartin KaFai Lau static int bpf_obj_get_next_id(const union bpf_attr *attr, 300434ad5580SMartin KaFai Lau union bpf_attr __user *uattr, 300534ad5580SMartin KaFai Lau struct idr *idr, 300634ad5580SMartin KaFai Lau spinlock_t *lock) 300734ad5580SMartin KaFai Lau { 300834ad5580SMartin KaFai Lau u32 next_id = attr->start_id; 300934ad5580SMartin KaFai Lau int err = 0; 301034ad5580SMartin KaFai Lau 301134ad5580SMartin KaFai Lau if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) 301234ad5580SMartin KaFai Lau return -EINVAL; 301334ad5580SMartin KaFai Lau 301434ad5580SMartin KaFai Lau if (!capable(CAP_SYS_ADMIN)) 301534ad5580SMartin KaFai Lau return -EPERM; 301634ad5580SMartin KaFai Lau 301734ad5580SMartin KaFai Lau next_id++; 301834ad5580SMartin KaFai Lau spin_lock_bh(lock); 301934ad5580SMartin KaFai Lau if (!idr_get_next(idr, &next_id)) 302034ad5580SMartin KaFai Lau err = -ENOENT; 302134ad5580SMartin KaFai Lau spin_unlock_bh(lock); 302234ad5580SMartin KaFai Lau 302334ad5580SMartin KaFai Lau if (!err) 302434ad5580SMartin KaFai Lau err = put_user(next_id, &uattr->next_id); 302534ad5580SMartin KaFai Lau 302634ad5580SMartin KaFai Lau return err; 302734ad5580SMartin KaFai Lau } 302834ad5580SMartin KaFai Lau 30296086d29dSYonghong Song struct bpf_map *bpf_map_get_curr_or_next(u32 *id) 30306086d29dSYonghong Song { 30316086d29dSYonghong Song struct bpf_map *map; 30326086d29dSYonghong Song 30336086d29dSYonghong Song spin_lock_bh(&map_idr_lock); 30346086d29dSYonghong Song again: 30356086d29dSYonghong Song map = idr_get_next(&map_idr, id); 30366086d29dSYonghong Song if (map) { 30376086d29dSYonghong Song map = __bpf_map_inc_not_zero(map, false); 30386086d29dSYonghong Song if (IS_ERR(map)) { 30396086d29dSYonghong Song (*id)++; 30406086d29dSYonghong Song goto again; 30416086d29dSYonghong Song } 30426086d29dSYonghong Song } 30436086d29dSYonghong Song spin_unlock_bh(&map_idr_lock); 30446086d29dSYonghong Song 30456086d29dSYonghong Song return map; 30466086d29dSYonghong Song } 30476086d29dSYonghong Song 3048b16d9aa4SMartin KaFai Lau #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id 3049b16d9aa4SMartin KaFai Lau 30507e6897f9SBjörn Töpel struct bpf_prog *bpf_prog_by_id(u32 id) 30517e6897f9SBjörn Töpel { 30527e6897f9SBjörn Töpel struct bpf_prog *prog; 30537e6897f9SBjörn Töpel 30547e6897f9SBjörn Töpel if (!id) 30557e6897f9SBjörn Töpel return ERR_PTR(-ENOENT); 30567e6897f9SBjörn Töpel 30577e6897f9SBjörn Töpel spin_lock_bh(&prog_idr_lock); 30587e6897f9SBjörn Töpel prog = idr_find(&prog_idr, id); 30597e6897f9SBjörn Töpel if (prog) 30607e6897f9SBjörn Töpel prog = bpf_prog_inc_not_zero(prog); 30617e6897f9SBjörn Töpel else 30627e6897f9SBjörn Töpel prog = ERR_PTR(-ENOENT); 30637e6897f9SBjörn Töpel spin_unlock_bh(&prog_idr_lock); 30647e6897f9SBjörn Töpel return prog; 30657e6897f9SBjörn Töpel } 30667e6897f9SBjörn Töpel 3067b16d9aa4SMartin KaFai Lau static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) 3068b16d9aa4SMartin KaFai Lau { 3069b16d9aa4SMartin KaFai Lau struct bpf_prog *prog; 3070b16d9aa4SMartin KaFai Lau u32 id = attr->prog_id; 3071b16d9aa4SMartin KaFai Lau int fd; 3072b16d9aa4SMartin KaFai Lau 3073b16d9aa4SMartin KaFai Lau if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) 3074b16d9aa4SMartin KaFai Lau return -EINVAL; 3075b16d9aa4SMartin KaFai Lau 3076b16d9aa4SMartin KaFai Lau if (!capable(CAP_SYS_ADMIN)) 3077b16d9aa4SMartin KaFai Lau return -EPERM; 3078b16d9aa4SMartin KaFai Lau 30797e6897f9SBjörn Töpel prog = bpf_prog_by_id(id); 3080b16d9aa4SMartin KaFai Lau if (IS_ERR(prog)) 3081b16d9aa4SMartin KaFai Lau return PTR_ERR(prog); 3082b16d9aa4SMartin KaFai Lau 3083b16d9aa4SMartin KaFai Lau fd = bpf_prog_new_fd(prog); 3084b16d9aa4SMartin KaFai Lau if (fd < 0) 3085b16d9aa4SMartin KaFai Lau bpf_prog_put(prog); 3086b16d9aa4SMartin KaFai Lau 3087b16d9aa4SMartin KaFai Lau return fd; 3088b16d9aa4SMartin KaFai Lau } 3089b16d9aa4SMartin KaFai Lau 30906e71b04aSChenbo Feng #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags 3091bd5f5f4eSMartin KaFai Lau 3092bd5f5f4eSMartin KaFai Lau static int bpf_map_get_fd_by_id(const union bpf_attr *attr) 3093bd5f5f4eSMartin KaFai Lau { 3094bd5f5f4eSMartin KaFai Lau struct bpf_map *map; 3095bd5f5f4eSMartin KaFai Lau u32 id = attr->map_id; 30966e71b04aSChenbo Feng int f_flags; 3097bd5f5f4eSMartin KaFai Lau int fd; 3098bd5f5f4eSMartin KaFai Lau 30996e71b04aSChenbo Feng if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || 31006e71b04aSChenbo Feng attr->open_flags & ~BPF_OBJ_FLAG_MASK) 3101bd5f5f4eSMartin KaFai Lau return -EINVAL; 3102bd5f5f4eSMartin KaFai Lau 3103bd5f5f4eSMartin KaFai Lau if (!capable(CAP_SYS_ADMIN)) 3104bd5f5f4eSMartin KaFai Lau return -EPERM; 3105bd5f5f4eSMartin KaFai Lau 31066e71b04aSChenbo Feng f_flags = bpf_get_file_flag(attr->open_flags); 31076e71b04aSChenbo Feng if (f_flags < 0) 31086e71b04aSChenbo Feng return f_flags; 31096e71b04aSChenbo Feng 3110bd5f5f4eSMartin KaFai Lau spin_lock_bh(&map_idr_lock); 3111bd5f5f4eSMartin KaFai Lau map = idr_find(&map_idr, id); 3112bd5f5f4eSMartin KaFai Lau if (map) 3113b0e4701cSStanislav Fomichev map = __bpf_map_inc_not_zero(map, true); 3114bd5f5f4eSMartin KaFai Lau else 3115bd5f5f4eSMartin KaFai Lau map = ERR_PTR(-ENOENT); 3116bd5f5f4eSMartin KaFai Lau spin_unlock_bh(&map_idr_lock); 3117bd5f5f4eSMartin KaFai Lau 3118bd5f5f4eSMartin KaFai Lau if (IS_ERR(map)) 3119bd5f5f4eSMartin KaFai Lau return PTR_ERR(map); 3120bd5f5f4eSMartin KaFai Lau 31216e71b04aSChenbo Feng fd = bpf_map_new_fd(map, f_flags); 3122bd5f5f4eSMartin KaFai Lau if (fd < 0) 3123781e6282SPeng Sun bpf_map_put_with_uref(map); 3124bd5f5f4eSMartin KaFai Lau 3125bd5f5f4eSMartin KaFai Lau return fd; 3126bd5f5f4eSMartin KaFai Lau } 3127bd5f5f4eSMartin KaFai Lau 31287105e828SDaniel Borkmann static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, 3129d8eca5bbSDaniel Borkmann unsigned long addr, u32 *off, 3130d8eca5bbSDaniel Borkmann u32 *type) 31317105e828SDaniel Borkmann { 3132d8eca5bbSDaniel Borkmann const struct bpf_map *map; 31337105e828SDaniel Borkmann int i; 31347105e828SDaniel Borkmann 3135d8eca5bbSDaniel Borkmann for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) { 3136d8eca5bbSDaniel Borkmann map = prog->aux->used_maps[i]; 3137d8eca5bbSDaniel Borkmann if (map == (void *)addr) { 3138d8eca5bbSDaniel Borkmann *type = BPF_PSEUDO_MAP_FD; 3139d8eca5bbSDaniel Borkmann return map; 3140d8eca5bbSDaniel Borkmann } 3141d8eca5bbSDaniel Borkmann if (!map->ops->map_direct_value_meta) 3142d8eca5bbSDaniel Borkmann continue; 3143d8eca5bbSDaniel Borkmann if (!map->ops->map_direct_value_meta(map, addr, off)) { 3144d8eca5bbSDaniel Borkmann *type = BPF_PSEUDO_MAP_VALUE; 3145d8eca5bbSDaniel Borkmann return map; 3146d8eca5bbSDaniel Borkmann } 3147d8eca5bbSDaniel Borkmann } 3148d8eca5bbSDaniel Borkmann 31497105e828SDaniel Borkmann return NULL; 31507105e828SDaniel Borkmann } 31517105e828SDaniel Borkmann 31527105e828SDaniel Borkmann static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog) 31537105e828SDaniel Borkmann { 31547105e828SDaniel Borkmann const struct bpf_map *map; 31557105e828SDaniel Borkmann struct bpf_insn *insns; 3156d8eca5bbSDaniel Borkmann u32 off, type; 31577105e828SDaniel Borkmann u64 imm; 31587105e828SDaniel Borkmann int i; 31597105e828SDaniel Borkmann 31607105e828SDaniel Borkmann insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), 31617105e828SDaniel Borkmann GFP_USER); 31627105e828SDaniel Borkmann if (!insns) 31637105e828SDaniel Borkmann return insns; 31647105e828SDaniel Borkmann 31657105e828SDaniel Borkmann for (i = 0; i < prog->len; i++) { 31667105e828SDaniel Borkmann if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) { 31677105e828SDaniel Borkmann insns[i].code = BPF_JMP | BPF_CALL; 31687105e828SDaniel Borkmann insns[i].imm = BPF_FUNC_tail_call; 31697105e828SDaniel Borkmann /* fall-through */ 31707105e828SDaniel Borkmann } 31717105e828SDaniel Borkmann if (insns[i].code == (BPF_JMP | BPF_CALL) || 31727105e828SDaniel Borkmann insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) { 31737105e828SDaniel Borkmann if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) 31747105e828SDaniel Borkmann insns[i].code = BPF_JMP | BPF_CALL; 31757105e828SDaniel Borkmann if (!bpf_dump_raw_ok()) 31767105e828SDaniel Borkmann insns[i].imm = 0; 31777105e828SDaniel Borkmann continue; 31787105e828SDaniel Borkmann } 31797105e828SDaniel Borkmann 31807105e828SDaniel Borkmann if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW)) 31817105e828SDaniel Borkmann continue; 31827105e828SDaniel Borkmann 31837105e828SDaniel Borkmann imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; 3184d8eca5bbSDaniel Borkmann map = bpf_map_from_imm(prog, imm, &off, &type); 31857105e828SDaniel Borkmann if (map) { 3186d8eca5bbSDaniel Borkmann insns[i].src_reg = type; 31877105e828SDaniel Borkmann insns[i].imm = map->id; 3188d8eca5bbSDaniel Borkmann insns[i + 1].imm = off; 31897105e828SDaniel Borkmann continue; 31907105e828SDaniel Borkmann } 31917105e828SDaniel Borkmann } 31927105e828SDaniel Borkmann 31937105e828SDaniel Borkmann return insns; 31947105e828SDaniel Borkmann } 31957105e828SDaniel Borkmann 3196c454a46bSMartin KaFai Lau static int set_info_rec_size(struct bpf_prog_info *info) 3197c454a46bSMartin KaFai Lau { 3198c454a46bSMartin KaFai Lau /* 3199c454a46bSMartin KaFai Lau * Ensure info.*_rec_size is the same as kernel expected size 3200c454a46bSMartin KaFai Lau * 3201c454a46bSMartin KaFai Lau * or 3202c454a46bSMartin KaFai Lau * 3203c454a46bSMartin KaFai Lau * Only allow zero *_rec_size if both _rec_size and _cnt are 3204c454a46bSMartin KaFai Lau * zero. In this case, the kernel will set the expected 3205c454a46bSMartin KaFai Lau * _rec_size back to the info. 3206c454a46bSMartin KaFai Lau */ 3207c454a46bSMartin KaFai Lau 320811d8b82dSYonghong Song if ((info->nr_func_info || info->func_info_rec_size) && 3209c454a46bSMartin KaFai Lau info->func_info_rec_size != sizeof(struct bpf_func_info)) 3210c454a46bSMartin KaFai Lau return -EINVAL; 3211c454a46bSMartin KaFai Lau 321211d8b82dSYonghong Song if ((info->nr_line_info || info->line_info_rec_size) && 3213c454a46bSMartin KaFai Lau info->line_info_rec_size != sizeof(struct bpf_line_info)) 3214c454a46bSMartin KaFai Lau return -EINVAL; 3215c454a46bSMartin KaFai Lau 321611d8b82dSYonghong Song if ((info->nr_jited_line_info || info->jited_line_info_rec_size) && 3217c454a46bSMartin KaFai Lau info->jited_line_info_rec_size != sizeof(__u64)) 3218c454a46bSMartin KaFai Lau return -EINVAL; 3219c454a46bSMartin KaFai Lau 3220c454a46bSMartin KaFai Lau info->func_info_rec_size = sizeof(struct bpf_func_info); 3221c454a46bSMartin KaFai Lau info->line_info_rec_size = sizeof(struct bpf_line_info); 3222c454a46bSMartin KaFai Lau info->jited_line_info_rec_size = sizeof(__u64); 3223c454a46bSMartin KaFai Lau 3224c454a46bSMartin KaFai Lau return 0; 3225c454a46bSMartin KaFai Lau } 3226c454a46bSMartin KaFai Lau 32271e270976SMartin KaFai Lau static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, 32281e270976SMartin KaFai Lau const union bpf_attr *attr, 32291e270976SMartin KaFai Lau union bpf_attr __user *uattr) 32301e270976SMartin KaFai Lau { 32311e270976SMartin KaFai Lau struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); 32325c6f2588SGreg Kroah-Hartman struct bpf_prog_info info; 32331e270976SMartin KaFai Lau u32 info_len = attr->info.info_len; 32345f8f8b93SAlexei Starovoitov struct bpf_prog_stats stats; 32351e270976SMartin KaFai Lau char __user *uinsns; 32361e270976SMartin KaFai Lau u32 ulen; 32371e270976SMartin KaFai Lau int err; 32381e270976SMartin KaFai Lau 3239dcab51f1SMartin KaFai Lau err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); 32401e270976SMartin KaFai Lau if (err) 32411e270976SMartin KaFai Lau return err; 32421e270976SMartin KaFai Lau info_len = min_t(u32, sizeof(info), info_len); 32431e270976SMartin KaFai Lau 32445c6f2588SGreg Kroah-Hartman memset(&info, 0, sizeof(info)); 32451e270976SMartin KaFai Lau if (copy_from_user(&info, uinfo, info_len)) 324689b09689SDaniel Borkmann return -EFAULT; 32471e270976SMartin KaFai Lau 32481e270976SMartin KaFai Lau info.type = prog->type; 32491e270976SMartin KaFai Lau info.id = prog->aux->id; 3250cb4d2b3fSMartin KaFai Lau info.load_time = prog->aux->load_time; 3251cb4d2b3fSMartin KaFai Lau info.created_by_uid = from_kuid_munged(current_user_ns(), 3252cb4d2b3fSMartin KaFai Lau prog->aux->user->uid); 3253b85fab0eSJiri Olsa info.gpl_compatible = prog->gpl_compatible; 32541e270976SMartin KaFai Lau 32551e270976SMartin KaFai Lau memcpy(info.tag, prog->tag, sizeof(prog->tag)); 3256cb4d2b3fSMartin KaFai Lau memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); 3257cb4d2b3fSMartin KaFai Lau 3258cb4d2b3fSMartin KaFai Lau ulen = info.nr_map_ids; 3259cb4d2b3fSMartin KaFai Lau info.nr_map_ids = prog->aux->used_map_cnt; 3260cb4d2b3fSMartin KaFai Lau ulen = min_t(u32, info.nr_map_ids, ulen); 3261cb4d2b3fSMartin KaFai Lau if (ulen) { 3262721e08daSMartin KaFai Lau u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); 3263cb4d2b3fSMartin KaFai Lau u32 i; 3264cb4d2b3fSMartin KaFai Lau 3265cb4d2b3fSMartin KaFai Lau for (i = 0; i < ulen; i++) 3266cb4d2b3fSMartin KaFai Lau if (put_user(prog->aux->used_maps[i]->id, 3267cb4d2b3fSMartin KaFai Lau &user_map_ids[i])) 3268cb4d2b3fSMartin KaFai Lau return -EFAULT; 3269cb4d2b3fSMartin KaFai Lau } 32701e270976SMartin KaFai Lau 3271c454a46bSMartin KaFai Lau err = set_info_rec_size(&info); 3272c454a46bSMartin KaFai Lau if (err) 3273c454a46bSMartin KaFai Lau return err; 32747337224fSMartin KaFai Lau 32755f8f8b93SAlexei Starovoitov bpf_prog_get_stats(prog, &stats); 32765f8f8b93SAlexei Starovoitov info.run_time_ns = stats.nsecs; 32775f8f8b93SAlexei Starovoitov info.run_cnt = stats.cnt; 32785f8f8b93SAlexei Starovoitov 32792c78ee89SAlexei Starovoitov if (!bpf_capable()) { 32801e270976SMartin KaFai Lau info.jited_prog_len = 0; 32811e270976SMartin KaFai Lau info.xlated_prog_len = 0; 3282dbecd738SSandipan Das info.nr_jited_ksyms = 0; 328328c2fae7SDaniel Borkmann info.nr_jited_func_lens = 0; 328411d8b82dSYonghong Song info.nr_func_info = 0; 328511d8b82dSYonghong Song info.nr_line_info = 0; 328611d8b82dSYonghong Song info.nr_jited_line_info = 0; 32871e270976SMartin KaFai Lau goto done; 32881e270976SMartin KaFai Lau } 32891e270976SMartin KaFai Lau 32901e270976SMartin KaFai Lau ulen = info.xlated_prog_len; 32919975a54bSDaniel Borkmann info.xlated_prog_len = bpf_prog_insn_size(prog); 32921e270976SMartin KaFai Lau if (info.xlated_prog_len && ulen) { 32937105e828SDaniel Borkmann struct bpf_insn *insns_sanitized; 32947105e828SDaniel Borkmann bool fault; 32957105e828SDaniel Borkmann 32967105e828SDaniel Borkmann if (prog->blinded && !bpf_dump_raw_ok()) { 32977105e828SDaniel Borkmann info.xlated_prog_insns = 0; 32987105e828SDaniel Borkmann goto done; 32997105e828SDaniel Borkmann } 33007105e828SDaniel Borkmann insns_sanitized = bpf_insn_prepare_dump(prog); 33017105e828SDaniel Borkmann if (!insns_sanitized) 33027105e828SDaniel Borkmann return -ENOMEM; 33031e270976SMartin KaFai Lau uinsns = u64_to_user_ptr(info.xlated_prog_insns); 33041e270976SMartin KaFai Lau ulen = min_t(u32, info.xlated_prog_len, ulen); 33057105e828SDaniel Borkmann fault = copy_to_user(uinsns, insns_sanitized, ulen); 33067105e828SDaniel Borkmann kfree(insns_sanitized); 33077105e828SDaniel Borkmann if (fault) 33081e270976SMartin KaFai Lau return -EFAULT; 33091e270976SMartin KaFai Lau } 33101e270976SMartin KaFai Lau 3311675fc275SJakub Kicinski if (bpf_prog_is_dev_bound(prog->aux)) { 3312675fc275SJakub Kicinski err = bpf_prog_offload_info_fill(&info, prog); 3313675fc275SJakub Kicinski if (err) 3314675fc275SJakub Kicinski return err; 3315fcfb126dSJiong Wang goto done; 3316fcfb126dSJiong Wang } 3317fcfb126dSJiong Wang 3318fcfb126dSJiong Wang /* NOTE: the following code is supposed to be skipped for offload. 3319fcfb126dSJiong Wang * bpf_prog_offload_info_fill() is the place to fill similar fields 3320fcfb126dSJiong Wang * for offload. 3321fcfb126dSJiong Wang */ 3322fcfb126dSJiong Wang ulen = info.jited_prog_len; 33234d56a76eSSandipan Das if (prog->aux->func_cnt) { 33244d56a76eSSandipan Das u32 i; 33254d56a76eSSandipan Das 33264d56a76eSSandipan Das info.jited_prog_len = 0; 33274d56a76eSSandipan Das for (i = 0; i < prog->aux->func_cnt; i++) 33284d56a76eSSandipan Das info.jited_prog_len += prog->aux->func[i]->jited_len; 33294d56a76eSSandipan Das } else { 3330fcfb126dSJiong Wang info.jited_prog_len = prog->jited_len; 33314d56a76eSSandipan Das } 33324d56a76eSSandipan Das 3333fcfb126dSJiong Wang if (info.jited_prog_len && ulen) { 3334fcfb126dSJiong Wang if (bpf_dump_raw_ok()) { 3335fcfb126dSJiong Wang uinsns = u64_to_user_ptr(info.jited_prog_insns); 3336fcfb126dSJiong Wang ulen = min_t(u32, info.jited_prog_len, ulen); 33374d56a76eSSandipan Das 33384d56a76eSSandipan Das /* for multi-function programs, copy the JITed 33394d56a76eSSandipan Das * instructions for all the functions 33404d56a76eSSandipan Das */ 33414d56a76eSSandipan Das if (prog->aux->func_cnt) { 33424d56a76eSSandipan Das u32 len, free, i; 33434d56a76eSSandipan Das u8 *img; 33444d56a76eSSandipan Das 33454d56a76eSSandipan Das free = ulen; 33464d56a76eSSandipan Das for (i = 0; i < prog->aux->func_cnt; i++) { 33474d56a76eSSandipan Das len = prog->aux->func[i]->jited_len; 33484d56a76eSSandipan Das len = min_t(u32, len, free); 33494d56a76eSSandipan Das img = (u8 *) prog->aux->func[i]->bpf_func; 33504d56a76eSSandipan Das if (copy_to_user(uinsns, img, len)) 33514d56a76eSSandipan Das return -EFAULT; 33524d56a76eSSandipan Das uinsns += len; 33534d56a76eSSandipan Das free -= len; 33544d56a76eSSandipan Das if (!free) 33554d56a76eSSandipan Das break; 33564d56a76eSSandipan Das } 33574d56a76eSSandipan Das } else { 3358fcfb126dSJiong Wang if (copy_to_user(uinsns, prog->bpf_func, ulen)) 3359fcfb126dSJiong Wang return -EFAULT; 33604d56a76eSSandipan Das } 3361fcfb126dSJiong Wang } else { 3362fcfb126dSJiong Wang info.jited_prog_insns = 0; 3363fcfb126dSJiong Wang } 3364675fc275SJakub Kicinski } 3365675fc275SJakub Kicinski 3366dbecd738SSandipan Das ulen = info.nr_jited_ksyms; 3367ff1889fcSSong Liu info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; 33687a5725ddSSong Liu if (ulen) { 3369dbecd738SSandipan Das if (bpf_dump_raw_ok()) { 3370ff1889fcSSong Liu unsigned long ksym_addr; 3371dbecd738SSandipan Das u64 __user *user_ksyms; 3372dbecd738SSandipan Das u32 i; 3373dbecd738SSandipan Das 3374dbecd738SSandipan Das /* copy the address of the kernel symbol 3375dbecd738SSandipan Das * corresponding to each function 3376dbecd738SSandipan Das */ 3377dbecd738SSandipan Das ulen = min_t(u32, info.nr_jited_ksyms, ulen); 3378dbecd738SSandipan Das user_ksyms = u64_to_user_ptr(info.jited_ksyms); 3379ff1889fcSSong Liu if (prog->aux->func_cnt) { 3380dbecd738SSandipan Das for (i = 0; i < ulen; i++) { 3381ff1889fcSSong Liu ksym_addr = (unsigned long) 3382ff1889fcSSong Liu prog->aux->func[i]->bpf_func; 3383ff1889fcSSong Liu if (put_user((u64) ksym_addr, 3384ff1889fcSSong Liu &user_ksyms[i])) 3385ff1889fcSSong Liu return -EFAULT; 3386ff1889fcSSong Liu } 3387ff1889fcSSong Liu } else { 3388ff1889fcSSong Liu ksym_addr = (unsigned long) prog->bpf_func; 3389ff1889fcSSong Liu if (put_user((u64) ksym_addr, &user_ksyms[0])) 3390dbecd738SSandipan Das return -EFAULT; 3391dbecd738SSandipan Das } 3392dbecd738SSandipan Das } else { 3393dbecd738SSandipan Das info.jited_ksyms = 0; 3394dbecd738SSandipan Das } 3395dbecd738SSandipan Das } 3396dbecd738SSandipan Das 3397815581c1SSandipan Das ulen = info.nr_jited_func_lens; 3398ff1889fcSSong Liu info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; 33997a5725ddSSong Liu if (ulen) { 3400815581c1SSandipan Das if (bpf_dump_raw_ok()) { 3401815581c1SSandipan Das u32 __user *user_lens; 3402815581c1SSandipan Das u32 func_len, i; 3403815581c1SSandipan Das 3404815581c1SSandipan Das /* copy the JITed image lengths for each function */ 3405815581c1SSandipan Das ulen = min_t(u32, info.nr_jited_func_lens, ulen); 3406815581c1SSandipan Das user_lens = u64_to_user_ptr(info.jited_func_lens); 3407ff1889fcSSong Liu if (prog->aux->func_cnt) { 3408815581c1SSandipan Das for (i = 0; i < ulen; i++) { 3409ff1889fcSSong Liu func_len = 3410ff1889fcSSong Liu prog->aux->func[i]->jited_len; 3411815581c1SSandipan Das if (put_user(func_len, &user_lens[i])) 3412815581c1SSandipan Das return -EFAULT; 3413815581c1SSandipan Das } 3414815581c1SSandipan Das } else { 3415ff1889fcSSong Liu func_len = prog->jited_len; 3416ff1889fcSSong Liu if (put_user(func_len, &user_lens[0])) 3417ff1889fcSSong Liu return -EFAULT; 3418ff1889fcSSong Liu } 3419ff1889fcSSong Liu } else { 3420815581c1SSandipan Das info.jited_func_lens = 0; 3421815581c1SSandipan Das } 3422815581c1SSandipan Das } 3423815581c1SSandipan Das 34247337224fSMartin KaFai Lau if (prog->aux->btf) 3425838e9690SYonghong Song info.btf_id = btf_id(prog->aux->btf); 3426838e9690SYonghong Song 342711d8b82dSYonghong Song ulen = info.nr_func_info; 342811d8b82dSYonghong Song info.nr_func_info = prog->aux->func_info_cnt; 342911d8b82dSYonghong Song if (info.nr_func_info && ulen) { 3430838e9690SYonghong Song char __user *user_finfo; 3431838e9690SYonghong Song 3432838e9690SYonghong Song user_finfo = u64_to_user_ptr(info.func_info); 343311d8b82dSYonghong Song ulen = min_t(u32, info.nr_func_info, ulen); 3434ba64e7d8SYonghong Song if (copy_to_user(user_finfo, prog->aux->func_info, 34357337224fSMartin KaFai Lau info.func_info_rec_size * ulen)) 3436838e9690SYonghong Song return -EFAULT; 3437838e9690SYonghong Song } 3438838e9690SYonghong Song 343911d8b82dSYonghong Song ulen = info.nr_line_info; 344011d8b82dSYonghong Song info.nr_line_info = prog->aux->nr_linfo; 344111d8b82dSYonghong Song if (info.nr_line_info && ulen) { 3442c454a46bSMartin KaFai Lau __u8 __user *user_linfo; 3443c454a46bSMartin KaFai Lau 3444c454a46bSMartin KaFai Lau user_linfo = u64_to_user_ptr(info.line_info); 344511d8b82dSYonghong Song ulen = min_t(u32, info.nr_line_info, ulen); 3446c454a46bSMartin KaFai Lau if (copy_to_user(user_linfo, prog->aux->linfo, 3447c454a46bSMartin KaFai Lau info.line_info_rec_size * ulen)) 3448c454a46bSMartin KaFai Lau return -EFAULT; 3449c454a46bSMartin KaFai Lau } 3450c454a46bSMartin KaFai Lau 345111d8b82dSYonghong Song ulen = info.nr_jited_line_info; 3452c454a46bSMartin KaFai Lau if (prog->aux->jited_linfo) 345311d8b82dSYonghong Song info.nr_jited_line_info = prog->aux->nr_linfo; 3454c454a46bSMartin KaFai Lau else 345511d8b82dSYonghong Song info.nr_jited_line_info = 0; 345611d8b82dSYonghong Song if (info.nr_jited_line_info && ulen) { 3457c454a46bSMartin KaFai Lau if (bpf_dump_raw_ok()) { 3458c454a46bSMartin KaFai Lau __u64 __user *user_linfo; 3459c454a46bSMartin KaFai Lau u32 i; 3460c454a46bSMartin KaFai Lau 3461c454a46bSMartin KaFai Lau user_linfo = u64_to_user_ptr(info.jited_line_info); 346211d8b82dSYonghong Song ulen = min_t(u32, info.nr_jited_line_info, ulen); 3463c454a46bSMartin KaFai Lau for (i = 0; i < ulen; i++) { 3464c454a46bSMartin KaFai Lau if (put_user((__u64)(long)prog->aux->jited_linfo[i], 3465c454a46bSMartin KaFai Lau &user_linfo[i])) 3466c454a46bSMartin KaFai Lau return -EFAULT; 3467c454a46bSMartin KaFai Lau } 3468c454a46bSMartin KaFai Lau } else { 3469c454a46bSMartin KaFai Lau info.jited_line_info = 0; 3470c454a46bSMartin KaFai Lau } 3471c454a46bSMartin KaFai Lau } 3472c454a46bSMartin KaFai Lau 3473c872bdb3SSong Liu ulen = info.nr_prog_tags; 3474c872bdb3SSong Liu info.nr_prog_tags = prog->aux->func_cnt ? : 1; 3475c872bdb3SSong Liu if (ulen) { 3476c872bdb3SSong Liu __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; 3477c872bdb3SSong Liu u32 i; 3478c872bdb3SSong Liu 3479c872bdb3SSong Liu user_prog_tags = u64_to_user_ptr(info.prog_tags); 3480c872bdb3SSong Liu ulen = min_t(u32, info.nr_prog_tags, ulen); 3481c872bdb3SSong Liu if (prog->aux->func_cnt) { 3482c872bdb3SSong Liu for (i = 0; i < ulen; i++) { 3483c872bdb3SSong Liu if (copy_to_user(user_prog_tags[i], 3484c872bdb3SSong Liu prog->aux->func[i]->tag, 3485c872bdb3SSong Liu BPF_TAG_SIZE)) 3486c872bdb3SSong Liu return -EFAULT; 3487c872bdb3SSong Liu } 3488c872bdb3SSong Liu } else { 3489c872bdb3SSong Liu if (copy_to_user(user_prog_tags[0], 3490c872bdb3SSong Liu prog->tag, BPF_TAG_SIZE)) 3491c872bdb3SSong Liu return -EFAULT; 3492c872bdb3SSong Liu } 3493c872bdb3SSong Liu } 3494c872bdb3SSong Liu 34951e270976SMartin KaFai Lau done: 34961e270976SMartin KaFai Lau if (copy_to_user(uinfo, &info, info_len) || 34971e270976SMartin KaFai Lau put_user(info_len, &uattr->info.info_len)) 34981e270976SMartin KaFai Lau return -EFAULT; 34991e270976SMartin KaFai Lau 35001e270976SMartin KaFai Lau return 0; 35011e270976SMartin KaFai Lau } 35021e270976SMartin KaFai Lau 35031e270976SMartin KaFai Lau static int bpf_map_get_info_by_fd(struct bpf_map *map, 35041e270976SMartin KaFai Lau const union bpf_attr *attr, 35051e270976SMartin KaFai Lau union bpf_attr __user *uattr) 35061e270976SMartin KaFai Lau { 35071e270976SMartin KaFai Lau struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); 35085c6f2588SGreg Kroah-Hartman struct bpf_map_info info; 35091e270976SMartin KaFai Lau u32 info_len = attr->info.info_len; 35101e270976SMartin KaFai Lau int err; 35111e270976SMartin KaFai Lau 3512dcab51f1SMartin KaFai Lau err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); 35131e270976SMartin KaFai Lau if (err) 35141e270976SMartin KaFai Lau return err; 35151e270976SMartin KaFai Lau info_len = min_t(u32, sizeof(info), info_len); 35161e270976SMartin KaFai Lau 35175c6f2588SGreg Kroah-Hartman memset(&info, 0, sizeof(info)); 35181e270976SMartin KaFai Lau info.type = map->map_type; 35191e270976SMartin KaFai Lau info.id = map->id; 35201e270976SMartin KaFai Lau info.key_size = map->key_size; 35211e270976SMartin KaFai Lau info.value_size = map->value_size; 35221e270976SMartin KaFai Lau info.max_entries = map->max_entries; 35231e270976SMartin KaFai Lau info.map_flags = map->map_flags; 3524ad5b177bSMartin KaFai Lau memcpy(info.name, map->name, sizeof(map->name)); 35251e270976SMartin KaFai Lau 352678958fcaSMartin KaFai Lau if (map->btf) { 352778958fcaSMartin KaFai Lau info.btf_id = btf_id(map->btf); 35289b2cf328SMartin KaFai Lau info.btf_key_type_id = map->btf_key_type_id; 35299b2cf328SMartin KaFai Lau info.btf_value_type_id = map->btf_value_type_id; 353078958fcaSMartin KaFai Lau } 353185d33df3SMartin KaFai Lau info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; 353278958fcaSMartin KaFai Lau 353352775b33SJakub Kicinski if (bpf_map_is_dev_bound(map)) { 353452775b33SJakub Kicinski err = bpf_map_offload_info_fill(&info, map); 353552775b33SJakub Kicinski if (err) 353652775b33SJakub Kicinski return err; 353752775b33SJakub Kicinski } 353852775b33SJakub Kicinski 35391e270976SMartin KaFai Lau if (copy_to_user(uinfo, &info, info_len) || 35401e270976SMartin KaFai Lau put_user(info_len, &uattr->info.info_len)) 35411e270976SMartin KaFai Lau return -EFAULT; 35421e270976SMartin KaFai Lau 35431e270976SMartin KaFai Lau return 0; 35441e270976SMartin KaFai Lau } 35451e270976SMartin KaFai Lau 354662dab84cSMartin KaFai Lau static int bpf_btf_get_info_by_fd(struct btf *btf, 354762dab84cSMartin KaFai Lau const union bpf_attr *attr, 354862dab84cSMartin KaFai Lau union bpf_attr __user *uattr) 354962dab84cSMartin KaFai Lau { 355062dab84cSMartin KaFai Lau struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); 355162dab84cSMartin KaFai Lau u32 info_len = attr->info.info_len; 355262dab84cSMartin KaFai Lau int err; 355362dab84cSMartin KaFai Lau 3554dcab51f1SMartin KaFai Lau err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len); 355562dab84cSMartin KaFai Lau if (err) 355662dab84cSMartin KaFai Lau return err; 355762dab84cSMartin KaFai Lau 355862dab84cSMartin KaFai Lau return btf_get_info_by_fd(btf, attr, uattr); 355962dab84cSMartin KaFai Lau } 356062dab84cSMartin KaFai Lau 3561f2e10bffSAndrii Nakryiko static int bpf_link_get_info_by_fd(struct bpf_link *link, 3562f2e10bffSAndrii Nakryiko const union bpf_attr *attr, 3563f2e10bffSAndrii Nakryiko union bpf_attr __user *uattr) 3564f2e10bffSAndrii Nakryiko { 3565f2e10bffSAndrii Nakryiko struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info); 3566f2e10bffSAndrii Nakryiko struct bpf_link_info info; 3567f2e10bffSAndrii Nakryiko u32 info_len = attr->info.info_len; 3568f2e10bffSAndrii Nakryiko int err; 3569f2e10bffSAndrii Nakryiko 3570f2e10bffSAndrii Nakryiko err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); 3571f2e10bffSAndrii Nakryiko if (err) 3572f2e10bffSAndrii Nakryiko return err; 3573f2e10bffSAndrii Nakryiko info_len = min_t(u32, sizeof(info), info_len); 3574f2e10bffSAndrii Nakryiko 3575f2e10bffSAndrii Nakryiko memset(&info, 0, sizeof(info)); 3576f2e10bffSAndrii Nakryiko if (copy_from_user(&info, uinfo, info_len)) 3577f2e10bffSAndrii Nakryiko return -EFAULT; 3578f2e10bffSAndrii Nakryiko 3579f2e10bffSAndrii Nakryiko info.type = link->type; 3580f2e10bffSAndrii Nakryiko info.id = link->id; 3581f2e10bffSAndrii Nakryiko info.prog_id = link->prog->aux->id; 3582f2e10bffSAndrii Nakryiko 3583f2e10bffSAndrii Nakryiko if (link->ops->fill_link_info) { 3584f2e10bffSAndrii Nakryiko err = link->ops->fill_link_info(link, &info); 3585f2e10bffSAndrii Nakryiko if (err) 3586f2e10bffSAndrii Nakryiko return err; 3587f2e10bffSAndrii Nakryiko } 3588f2e10bffSAndrii Nakryiko 3589f2e10bffSAndrii Nakryiko if (copy_to_user(uinfo, &info, info_len) || 3590f2e10bffSAndrii Nakryiko put_user(info_len, &uattr->info.info_len)) 3591f2e10bffSAndrii Nakryiko return -EFAULT; 3592f2e10bffSAndrii Nakryiko 3593f2e10bffSAndrii Nakryiko return 0; 3594f2e10bffSAndrii Nakryiko } 3595f2e10bffSAndrii Nakryiko 3596f2e10bffSAndrii Nakryiko 35971e270976SMartin KaFai Lau #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info 35981e270976SMartin KaFai Lau 35991e270976SMartin KaFai Lau static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, 36001e270976SMartin KaFai Lau union bpf_attr __user *uattr) 36011e270976SMartin KaFai Lau { 36021e270976SMartin KaFai Lau int ufd = attr->info.bpf_fd; 36031e270976SMartin KaFai Lau struct fd f; 36041e270976SMartin KaFai Lau int err; 36051e270976SMartin KaFai Lau 36061e270976SMartin KaFai Lau if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) 36071e270976SMartin KaFai Lau return -EINVAL; 36081e270976SMartin KaFai Lau 36091e270976SMartin KaFai Lau f = fdget(ufd); 36101e270976SMartin KaFai Lau if (!f.file) 36111e270976SMartin KaFai Lau return -EBADFD; 36121e270976SMartin KaFai Lau 36131e270976SMartin KaFai Lau if (f.file->f_op == &bpf_prog_fops) 36141e270976SMartin KaFai Lau err = bpf_prog_get_info_by_fd(f.file->private_data, attr, 36151e270976SMartin KaFai Lau uattr); 36161e270976SMartin KaFai Lau else if (f.file->f_op == &bpf_map_fops) 36171e270976SMartin KaFai Lau err = bpf_map_get_info_by_fd(f.file->private_data, attr, 36181e270976SMartin KaFai Lau uattr); 361960197cfbSMartin KaFai Lau else if (f.file->f_op == &btf_fops) 362062dab84cSMartin KaFai Lau err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr); 3621f2e10bffSAndrii Nakryiko else if (f.file->f_op == &bpf_link_fops) 3622f2e10bffSAndrii Nakryiko err = bpf_link_get_info_by_fd(f.file->private_data, 3623f2e10bffSAndrii Nakryiko attr, uattr); 36241e270976SMartin KaFai Lau else 36251e270976SMartin KaFai Lau err = -EINVAL; 36261e270976SMartin KaFai Lau 36271e270976SMartin KaFai Lau fdput(f); 36281e270976SMartin KaFai Lau return err; 36291e270976SMartin KaFai Lau } 36301e270976SMartin KaFai Lau 3631f56a653cSMartin KaFai Lau #define BPF_BTF_LOAD_LAST_FIELD btf_log_level 3632f56a653cSMartin KaFai Lau 3633f56a653cSMartin KaFai Lau static int bpf_btf_load(const union bpf_attr *attr) 3634f56a653cSMartin KaFai Lau { 3635f56a653cSMartin KaFai Lau if (CHECK_ATTR(BPF_BTF_LOAD)) 3636f56a653cSMartin KaFai Lau return -EINVAL; 3637f56a653cSMartin KaFai Lau 36382c78ee89SAlexei Starovoitov if (!bpf_capable()) 3639f56a653cSMartin KaFai Lau return -EPERM; 3640f56a653cSMartin KaFai Lau 3641f56a653cSMartin KaFai Lau return btf_new_fd(attr); 3642f56a653cSMartin KaFai Lau } 3643f56a653cSMartin KaFai Lau 364478958fcaSMartin KaFai Lau #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id 364578958fcaSMartin KaFai Lau 364678958fcaSMartin KaFai Lau static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) 364778958fcaSMartin KaFai Lau { 364878958fcaSMartin KaFai Lau if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) 364978958fcaSMartin KaFai Lau return -EINVAL; 365078958fcaSMartin KaFai Lau 365178958fcaSMartin KaFai Lau if (!capable(CAP_SYS_ADMIN)) 365278958fcaSMartin KaFai Lau return -EPERM; 365378958fcaSMartin KaFai Lau 365478958fcaSMartin KaFai Lau return btf_get_fd_by_id(attr->btf_id); 365578958fcaSMartin KaFai Lau } 365678958fcaSMartin KaFai Lau 365741bdc4b4SYonghong Song static int bpf_task_fd_query_copy(const union bpf_attr *attr, 365841bdc4b4SYonghong Song union bpf_attr __user *uattr, 365941bdc4b4SYonghong Song u32 prog_id, u32 fd_type, 366041bdc4b4SYonghong Song const char *buf, u64 probe_offset, 366141bdc4b4SYonghong Song u64 probe_addr) 366241bdc4b4SYonghong Song { 366341bdc4b4SYonghong Song char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); 366441bdc4b4SYonghong Song u32 len = buf ? strlen(buf) : 0, input_len; 366541bdc4b4SYonghong Song int err = 0; 366641bdc4b4SYonghong Song 366741bdc4b4SYonghong Song if (put_user(len, &uattr->task_fd_query.buf_len)) 366841bdc4b4SYonghong Song return -EFAULT; 366941bdc4b4SYonghong Song input_len = attr->task_fd_query.buf_len; 367041bdc4b4SYonghong Song if (input_len && ubuf) { 367141bdc4b4SYonghong Song if (!len) { 367241bdc4b4SYonghong Song /* nothing to copy, just make ubuf NULL terminated */ 367341bdc4b4SYonghong Song char zero = '\0'; 367441bdc4b4SYonghong Song 367541bdc4b4SYonghong Song if (put_user(zero, ubuf)) 367641bdc4b4SYonghong Song return -EFAULT; 367741bdc4b4SYonghong Song } else if (input_len >= len + 1) { 367841bdc4b4SYonghong Song /* ubuf can hold the string with NULL terminator */ 367941bdc4b4SYonghong Song if (copy_to_user(ubuf, buf, len + 1)) 368041bdc4b4SYonghong Song return -EFAULT; 368141bdc4b4SYonghong Song } else { 368241bdc4b4SYonghong Song /* ubuf cannot hold the string with NULL terminator, 368341bdc4b4SYonghong Song * do a partial copy with NULL terminator. 368441bdc4b4SYonghong Song */ 368541bdc4b4SYonghong Song char zero = '\0'; 368641bdc4b4SYonghong Song 368741bdc4b4SYonghong Song err = -ENOSPC; 368841bdc4b4SYonghong Song if (copy_to_user(ubuf, buf, input_len - 1)) 368941bdc4b4SYonghong Song return -EFAULT; 369041bdc4b4SYonghong Song if (put_user(zero, ubuf + input_len - 1)) 369141bdc4b4SYonghong Song return -EFAULT; 369241bdc4b4SYonghong Song } 369341bdc4b4SYonghong Song } 369441bdc4b4SYonghong Song 369541bdc4b4SYonghong Song if (put_user(prog_id, &uattr->task_fd_query.prog_id) || 369641bdc4b4SYonghong Song put_user(fd_type, &uattr->task_fd_query.fd_type) || 369741bdc4b4SYonghong Song put_user(probe_offset, &uattr->task_fd_query.probe_offset) || 369841bdc4b4SYonghong Song put_user(probe_addr, &uattr->task_fd_query.probe_addr)) 369941bdc4b4SYonghong Song return -EFAULT; 370041bdc4b4SYonghong Song 370141bdc4b4SYonghong Song return err; 370241bdc4b4SYonghong Song } 370341bdc4b4SYonghong Song 370441bdc4b4SYonghong Song #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr 370541bdc4b4SYonghong Song 370641bdc4b4SYonghong Song static int bpf_task_fd_query(const union bpf_attr *attr, 370741bdc4b4SYonghong Song union bpf_attr __user *uattr) 370841bdc4b4SYonghong Song { 370941bdc4b4SYonghong Song pid_t pid = attr->task_fd_query.pid; 371041bdc4b4SYonghong Song u32 fd = attr->task_fd_query.fd; 371141bdc4b4SYonghong Song const struct perf_event *event; 371241bdc4b4SYonghong Song struct files_struct *files; 371341bdc4b4SYonghong Song struct task_struct *task; 371441bdc4b4SYonghong Song struct file *file; 371541bdc4b4SYonghong Song int err; 371641bdc4b4SYonghong Song 371741bdc4b4SYonghong Song if (CHECK_ATTR(BPF_TASK_FD_QUERY)) 371841bdc4b4SYonghong Song return -EINVAL; 371941bdc4b4SYonghong Song 372041bdc4b4SYonghong Song if (!capable(CAP_SYS_ADMIN)) 372141bdc4b4SYonghong Song return -EPERM; 372241bdc4b4SYonghong Song 372341bdc4b4SYonghong Song if (attr->task_fd_query.flags != 0) 372441bdc4b4SYonghong Song return -EINVAL; 372541bdc4b4SYonghong Song 372641bdc4b4SYonghong Song task = get_pid_task(find_vpid(pid), PIDTYPE_PID); 372741bdc4b4SYonghong Song if (!task) 372841bdc4b4SYonghong Song return -ENOENT; 372941bdc4b4SYonghong Song 373041bdc4b4SYonghong Song files = get_files_struct(task); 373141bdc4b4SYonghong Song put_task_struct(task); 373241bdc4b4SYonghong Song if (!files) 373341bdc4b4SYonghong Song return -ENOENT; 373441bdc4b4SYonghong Song 373541bdc4b4SYonghong Song err = 0; 373641bdc4b4SYonghong Song spin_lock(&files->file_lock); 373741bdc4b4SYonghong Song file = fcheck_files(files, fd); 373841bdc4b4SYonghong Song if (!file) 373941bdc4b4SYonghong Song err = -EBADF; 374041bdc4b4SYonghong Song else 374141bdc4b4SYonghong Song get_file(file); 374241bdc4b4SYonghong Song spin_unlock(&files->file_lock); 374341bdc4b4SYonghong Song put_files_struct(files); 374441bdc4b4SYonghong Song 374541bdc4b4SYonghong Song if (err) 374641bdc4b4SYonghong Song goto out; 374741bdc4b4SYonghong Song 374870ed506cSAndrii Nakryiko if (file->f_op == &bpf_link_fops) { 374970ed506cSAndrii Nakryiko struct bpf_link *link = file->private_data; 375070ed506cSAndrii Nakryiko 3751a3b80e10SAndrii Nakryiko if (link->ops == &bpf_raw_tp_link_lops) { 375270ed506cSAndrii Nakryiko struct bpf_raw_tp_link *raw_tp = 375370ed506cSAndrii Nakryiko container_of(link, struct bpf_raw_tp_link, link); 375441bdc4b4SYonghong Song struct bpf_raw_event_map *btp = raw_tp->btp; 375541bdc4b4SYonghong Song 375641bdc4b4SYonghong Song err = bpf_task_fd_query_copy(attr, uattr, 375770ed506cSAndrii Nakryiko raw_tp->link.prog->aux->id, 375841bdc4b4SYonghong Song BPF_FD_TYPE_RAW_TRACEPOINT, 375941bdc4b4SYonghong Song btp->tp->name, 0, 0); 376041bdc4b4SYonghong Song goto put_file; 376141bdc4b4SYonghong Song } 376270ed506cSAndrii Nakryiko goto out_not_supp; 376370ed506cSAndrii Nakryiko } 376441bdc4b4SYonghong Song 376541bdc4b4SYonghong Song event = perf_get_event(file); 376641bdc4b4SYonghong Song if (!IS_ERR(event)) { 376741bdc4b4SYonghong Song u64 probe_offset, probe_addr; 376841bdc4b4SYonghong Song u32 prog_id, fd_type; 376941bdc4b4SYonghong Song const char *buf; 377041bdc4b4SYonghong Song 377141bdc4b4SYonghong Song err = bpf_get_perf_event_info(event, &prog_id, &fd_type, 377241bdc4b4SYonghong Song &buf, &probe_offset, 377341bdc4b4SYonghong Song &probe_addr); 377441bdc4b4SYonghong Song if (!err) 377541bdc4b4SYonghong Song err = bpf_task_fd_query_copy(attr, uattr, prog_id, 377641bdc4b4SYonghong Song fd_type, buf, 377741bdc4b4SYonghong Song probe_offset, 377841bdc4b4SYonghong Song probe_addr); 377941bdc4b4SYonghong Song goto put_file; 378041bdc4b4SYonghong Song } 378141bdc4b4SYonghong Song 378270ed506cSAndrii Nakryiko out_not_supp: 378341bdc4b4SYonghong Song err = -ENOTSUPP; 378441bdc4b4SYonghong Song put_file: 378541bdc4b4SYonghong Song fput(file); 378641bdc4b4SYonghong Song out: 378741bdc4b4SYonghong Song return err; 378841bdc4b4SYonghong Song } 378941bdc4b4SYonghong Song 3790cb4d03abSBrian Vazquez #define BPF_MAP_BATCH_LAST_FIELD batch.flags 3791cb4d03abSBrian Vazquez 3792cb4d03abSBrian Vazquez #define BPF_DO_BATCH(fn) \ 3793cb4d03abSBrian Vazquez do { \ 3794cb4d03abSBrian Vazquez if (!fn) { \ 3795cb4d03abSBrian Vazquez err = -ENOTSUPP; \ 3796cb4d03abSBrian Vazquez goto err_put; \ 3797cb4d03abSBrian Vazquez } \ 3798cb4d03abSBrian Vazquez err = fn(map, attr, uattr); \ 3799cb4d03abSBrian Vazquez } while (0) 3800cb4d03abSBrian Vazquez 3801cb4d03abSBrian Vazquez static int bpf_map_do_batch(const union bpf_attr *attr, 3802cb4d03abSBrian Vazquez union bpf_attr __user *uattr, 3803cb4d03abSBrian Vazquez int cmd) 3804cb4d03abSBrian Vazquez { 3805cb4d03abSBrian Vazquez struct bpf_map *map; 3806cb4d03abSBrian Vazquez int err, ufd; 3807cb4d03abSBrian Vazquez struct fd f; 3808cb4d03abSBrian Vazquez 3809cb4d03abSBrian Vazquez if (CHECK_ATTR(BPF_MAP_BATCH)) 3810cb4d03abSBrian Vazquez return -EINVAL; 3811cb4d03abSBrian Vazquez 3812cb4d03abSBrian Vazquez ufd = attr->batch.map_fd; 3813cb4d03abSBrian Vazquez f = fdget(ufd); 3814cb4d03abSBrian Vazquez map = __bpf_map_get(f); 3815cb4d03abSBrian Vazquez if (IS_ERR(map)) 3816cb4d03abSBrian Vazquez return PTR_ERR(map); 3817cb4d03abSBrian Vazquez 381805799638SYonghong Song if ((cmd == BPF_MAP_LOOKUP_BATCH || 381905799638SYonghong Song cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) && 3820cb4d03abSBrian Vazquez !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { 3821cb4d03abSBrian Vazquez err = -EPERM; 3822cb4d03abSBrian Vazquez goto err_put; 3823cb4d03abSBrian Vazquez } 3824cb4d03abSBrian Vazquez 3825cb4d03abSBrian Vazquez if (cmd != BPF_MAP_LOOKUP_BATCH && 3826cb4d03abSBrian Vazquez !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { 3827cb4d03abSBrian Vazquez err = -EPERM; 3828cb4d03abSBrian Vazquez goto err_put; 3829cb4d03abSBrian Vazquez } 3830cb4d03abSBrian Vazquez 3831cb4d03abSBrian Vazquez if (cmd == BPF_MAP_LOOKUP_BATCH) 3832cb4d03abSBrian Vazquez BPF_DO_BATCH(map->ops->map_lookup_batch); 383305799638SYonghong Song else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) 383405799638SYonghong Song BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch); 3835aa2e93b8SBrian Vazquez else if (cmd == BPF_MAP_UPDATE_BATCH) 3836aa2e93b8SBrian Vazquez BPF_DO_BATCH(map->ops->map_update_batch); 3837aa2e93b8SBrian Vazquez else 3838aa2e93b8SBrian Vazquez BPF_DO_BATCH(map->ops->map_delete_batch); 3839cb4d03abSBrian Vazquez 3840cb4d03abSBrian Vazquez err_put: 3841cb4d03abSBrian Vazquez fdput(f); 3842cb4d03abSBrian Vazquez return err; 3843cb4d03abSBrian Vazquez } 3844cb4d03abSBrian Vazquez 3845de4e05caSYonghong Song static int tracing_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 3846de4e05caSYonghong Song { 3847de4e05caSYonghong Song if (attr->link_create.attach_type == BPF_TRACE_ITER && 3848de4e05caSYonghong Song prog->expected_attach_type == BPF_TRACE_ITER) 3849de4e05caSYonghong Song return bpf_iter_link_attach(attr, prog); 3850de4e05caSYonghong Song 3851de4e05caSYonghong Song return -EINVAL; 3852de4e05caSYonghong Song } 3853de4e05caSYonghong Song 3854af6eea57SAndrii Nakryiko #define BPF_LINK_CREATE_LAST_FIELD link_create.flags 3855af6eea57SAndrii Nakryiko static int link_create(union bpf_attr *attr) 3856af6eea57SAndrii Nakryiko { 3857af6eea57SAndrii Nakryiko enum bpf_prog_type ptype; 3858af6eea57SAndrii Nakryiko struct bpf_prog *prog; 3859af6eea57SAndrii Nakryiko int ret; 3860af6eea57SAndrii Nakryiko 3861af6eea57SAndrii Nakryiko if (CHECK_ATTR(BPF_LINK_CREATE)) 3862af6eea57SAndrii Nakryiko return -EINVAL; 3863af6eea57SAndrii Nakryiko 3864af6eea57SAndrii Nakryiko ptype = attach_type_to_prog_type(attr->link_create.attach_type); 3865af6eea57SAndrii Nakryiko if (ptype == BPF_PROG_TYPE_UNSPEC) 3866af6eea57SAndrii Nakryiko return -EINVAL; 3867af6eea57SAndrii Nakryiko 3868af6eea57SAndrii Nakryiko prog = bpf_prog_get_type(attr->link_create.prog_fd, ptype); 3869af6eea57SAndrii Nakryiko if (IS_ERR(prog)) 3870af6eea57SAndrii Nakryiko return PTR_ERR(prog); 3871af6eea57SAndrii Nakryiko 3872af6eea57SAndrii Nakryiko ret = bpf_prog_attach_check_attach_type(prog, 3873af6eea57SAndrii Nakryiko attr->link_create.attach_type); 3874af6eea57SAndrii Nakryiko if (ret) 3875af6eea57SAndrii Nakryiko goto err_out; 3876af6eea57SAndrii Nakryiko 3877af6eea57SAndrii Nakryiko switch (ptype) { 3878af6eea57SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SKB: 3879af6eea57SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SOCK: 3880af6eea57SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: 3881af6eea57SAndrii Nakryiko case BPF_PROG_TYPE_SOCK_OPS: 3882af6eea57SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_DEVICE: 3883af6eea57SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SYSCTL: 3884af6eea57SAndrii Nakryiko case BPF_PROG_TYPE_CGROUP_SOCKOPT: 3885af6eea57SAndrii Nakryiko ret = cgroup_bpf_link_attach(attr, prog); 3886af6eea57SAndrii Nakryiko break; 3887de4e05caSYonghong Song case BPF_PROG_TYPE_TRACING: 3888de4e05caSYonghong Song ret = tracing_bpf_link_attach(attr, prog); 3889de4e05caSYonghong Song break; 3890*7f045a49SJakub Sitnicki case BPF_PROG_TYPE_FLOW_DISSECTOR: 3891*7f045a49SJakub Sitnicki ret = netns_bpf_link_create(attr, prog); 3892*7f045a49SJakub Sitnicki break; 3893af6eea57SAndrii Nakryiko default: 3894af6eea57SAndrii Nakryiko ret = -EINVAL; 3895af6eea57SAndrii Nakryiko } 3896af6eea57SAndrii Nakryiko 3897af6eea57SAndrii Nakryiko err_out: 3898af6eea57SAndrii Nakryiko if (ret < 0) 3899af6eea57SAndrii Nakryiko bpf_prog_put(prog); 3900af6eea57SAndrii Nakryiko return ret; 3901af6eea57SAndrii Nakryiko } 3902af6eea57SAndrii Nakryiko 39030c991ebcSAndrii Nakryiko #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd 39040c991ebcSAndrii Nakryiko 39050c991ebcSAndrii Nakryiko static int link_update(union bpf_attr *attr) 39060c991ebcSAndrii Nakryiko { 39070c991ebcSAndrii Nakryiko struct bpf_prog *old_prog = NULL, *new_prog; 39080c991ebcSAndrii Nakryiko struct bpf_link *link; 39090c991ebcSAndrii Nakryiko u32 flags; 39100c991ebcSAndrii Nakryiko int ret; 39110c991ebcSAndrii Nakryiko 39120c991ebcSAndrii Nakryiko if (CHECK_ATTR(BPF_LINK_UPDATE)) 39130c991ebcSAndrii Nakryiko return -EINVAL; 39140c991ebcSAndrii Nakryiko 39150c991ebcSAndrii Nakryiko flags = attr->link_update.flags; 39160c991ebcSAndrii Nakryiko if (flags & ~BPF_F_REPLACE) 39170c991ebcSAndrii Nakryiko return -EINVAL; 39180c991ebcSAndrii Nakryiko 39190c991ebcSAndrii Nakryiko link = bpf_link_get_from_fd(attr->link_update.link_fd); 39200c991ebcSAndrii Nakryiko if (IS_ERR(link)) 39210c991ebcSAndrii Nakryiko return PTR_ERR(link); 39220c991ebcSAndrii Nakryiko 39230c991ebcSAndrii Nakryiko new_prog = bpf_prog_get(attr->link_update.new_prog_fd); 39244adb7a4aSAndrii Nakryiko if (IS_ERR(new_prog)) { 39254adb7a4aSAndrii Nakryiko ret = PTR_ERR(new_prog); 39264adb7a4aSAndrii Nakryiko goto out_put_link; 39274adb7a4aSAndrii Nakryiko } 39280c991ebcSAndrii Nakryiko 39290c991ebcSAndrii Nakryiko if (flags & BPF_F_REPLACE) { 39300c991ebcSAndrii Nakryiko old_prog = bpf_prog_get(attr->link_update.old_prog_fd); 39310c991ebcSAndrii Nakryiko if (IS_ERR(old_prog)) { 39320c991ebcSAndrii Nakryiko ret = PTR_ERR(old_prog); 39330c991ebcSAndrii Nakryiko old_prog = NULL; 39340c991ebcSAndrii Nakryiko goto out_put_progs; 39350c991ebcSAndrii Nakryiko } 39364adb7a4aSAndrii Nakryiko } else if (attr->link_update.old_prog_fd) { 39374adb7a4aSAndrii Nakryiko ret = -EINVAL; 39384adb7a4aSAndrii Nakryiko goto out_put_progs; 39390c991ebcSAndrii Nakryiko } 39400c991ebcSAndrii Nakryiko 3941f9d04127SAndrii Nakryiko if (link->ops->update_prog) 3942f9d04127SAndrii Nakryiko ret = link->ops->update_prog(link, new_prog, old_prog); 3943f9d04127SAndrii Nakryiko else 3944fe537393SJakub Sitnicki ret = -EINVAL; 39450c991ebcSAndrii Nakryiko 39460c991ebcSAndrii Nakryiko out_put_progs: 39470c991ebcSAndrii Nakryiko if (old_prog) 39480c991ebcSAndrii Nakryiko bpf_prog_put(old_prog); 39490c991ebcSAndrii Nakryiko if (ret) 39500c991ebcSAndrii Nakryiko bpf_prog_put(new_prog); 39514adb7a4aSAndrii Nakryiko out_put_link: 39524adb7a4aSAndrii Nakryiko bpf_link_put(link); 39530c991ebcSAndrii Nakryiko return ret; 39540c991ebcSAndrii Nakryiko } 39550c991ebcSAndrii Nakryiko 39562d602c8cSAndrii Nakryiko static int bpf_link_inc_not_zero(struct bpf_link *link) 39572d602c8cSAndrii Nakryiko { 39582d602c8cSAndrii Nakryiko return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? 0 : -ENOENT; 39592d602c8cSAndrii Nakryiko } 39602d602c8cSAndrii Nakryiko 39612d602c8cSAndrii Nakryiko #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id 39622d602c8cSAndrii Nakryiko 39632d602c8cSAndrii Nakryiko static int bpf_link_get_fd_by_id(const union bpf_attr *attr) 39642d602c8cSAndrii Nakryiko { 39652d602c8cSAndrii Nakryiko struct bpf_link *link; 39662d602c8cSAndrii Nakryiko u32 id = attr->link_id; 39672d602c8cSAndrii Nakryiko int fd, err; 39682d602c8cSAndrii Nakryiko 39692d602c8cSAndrii Nakryiko if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID)) 39702d602c8cSAndrii Nakryiko return -EINVAL; 39712d602c8cSAndrii Nakryiko 39722d602c8cSAndrii Nakryiko if (!capable(CAP_SYS_ADMIN)) 39732d602c8cSAndrii Nakryiko return -EPERM; 39742d602c8cSAndrii Nakryiko 39752d602c8cSAndrii Nakryiko spin_lock_bh(&link_idr_lock); 39762d602c8cSAndrii Nakryiko link = idr_find(&link_idr, id); 39772d602c8cSAndrii Nakryiko /* before link is "settled", ID is 0, pretend it doesn't exist yet */ 39782d602c8cSAndrii Nakryiko if (link) { 39792d602c8cSAndrii Nakryiko if (link->id) 39802d602c8cSAndrii Nakryiko err = bpf_link_inc_not_zero(link); 39812d602c8cSAndrii Nakryiko else 39822d602c8cSAndrii Nakryiko err = -EAGAIN; 39832d602c8cSAndrii Nakryiko } else { 39842d602c8cSAndrii Nakryiko err = -ENOENT; 39852d602c8cSAndrii Nakryiko } 39862d602c8cSAndrii Nakryiko spin_unlock_bh(&link_idr_lock); 39872d602c8cSAndrii Nakryiko 39882d602c8cSAndrii Nakryiko if (err) 39892d602c8cSAndrii Nakryiko return err; 39902d602c8cSAndrii Nakryiko 39912d602c8cSAndrii Nakryiko fd = bpf_link_new_fd(link); 39922d602c8cSAndrii Nakryiko if (fd < 0) 39932d602c8cSAndrii Nakryiko bpf_link_put(link); 39942d602c8cSAndrii Nakryiko 39952d602c8cSAndrii Nakryiko return fd; 39962d602c8cSAndrii Nakryiko } 39972d602c8cSAndrii Nakryiko 3998d46edd67SSong Liu DEFINE_MUTEX(bpf_stats_enabled_mutex); 3999d46edd67SSong Liu 4000d46edd67SSong Liu static int bpf_stats_release(struct inode *inode, struct file *file) 4001d46edd67SSong Liu { 4002d46edd67SSong Liu mutex_lock(&bpf_stats_enabled_mutex); 4003d46edd67SSong Liu static_key_slow_dec(&bpf_stats_enabled_key.key); 4004d46edd67SSong Liu mutex_unlock(&bpf_stats_enabled_mutex); 4005d46edd67SSong Liu return 0; 4006d46edd67SSong Liu } 4007d46edd67SSong Liu 4008d46edd67SSong Liu static const struct file_operations bpf_stats_fops = { 4009d46edd67SSong Liu .release = bpf_stats_release, 4010d46edd67SSong Liu }; 4011d46edd67SSong Liu 4012d46edd67SSong Liu static int bpf_enable_runtime_stats(void) 4013d46edd67SSong Liu { 4014d46edd67SSong Liu int fd; 4015d46edd67SSong Liu 4016d46edd67SSong Liu mutex_lock(&bpf_stats_enabled_mutex); 4017d46edd67SSong Liu 4018d46edd67SSong Liu /* Set a very high limit to avoid overflow */ 4019d46edd67SSong Liu if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) { 4020d46edd67SSong Liu mutex_unlock(&bpf_stats_enabled_mutex); 4021d46edd67SSong Liu return -EBUSY; 4022d46edd67SSong Liu } 4023d46edd67SSong Liu 4024d46edd67SSong Liu fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC); 4025d46edd67SSong Liu if (fd >= 0) 4026d46edd67SSong Liu static_key_slow_inc(&bpf_stats_enabled_key.key); 4027d46edd67SSong Liu 4028d46edd67SSong Liu mutex_unlock(&bpf_stats_enabled_mutex); 4029d46edd67SSong Liu return fd; 4030d46edd67SSong Liu } 4031d46edd67SSong Liu 4032d46edd67SSong Liu #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type 4033d46edd67SSong Liu 4034d46edd67SSong Liu static int bpf_enable_stats(union bpf_attr *attr) 4035d46edd67SSong Liu { 4036d46edd67SSong Liu 4037d46edd67SSong Liu if (CHECK_ATTR(BPF_ENABLE_STATS)) 4038d46edd67SSong Liu return -EINVAL; 4039d46edd67SSong Liu 4040d46edd67SSong Liu if (!capable(CAP_SYS_ADMIN)) 4041d46edd67SSong Liu return -EPERM; 4042d46edd67SSong Liu 4043d46edd67SSong Liu switch (attr->enable_stats.type) { 4044d46edd67SSong Liu case BPF_STATS_RUN_TIME: 4045d46edd67SSong Liu return bpf_enable_runtime_stats(); 4046d46edd67SSong Liu default: 4047d46edd67SSong Liu break; 4048d46edd67SSong Liu } 4049d46edd67SSong Liu return -EINVAL; 4050d46edd67SSong Liu } 4051d46edd67SSong Liu 4052ac51d99bSYonghong Song #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags 4053ac51d99bSYonghong Song 4054ac51d99bSYonghong Song static int bpf_iter_create(union bpf_attr *attr) 4055ac51d99bSYonghong Song { 4056ac51d99bSYonghong Song struct bpf_link *link; 4057ac51d99bSYonghong Song int err; 4058ac51d99bSYonghong Song 4059ac51d99bSYonghong Song if (CHECK_ATTR(BPF_ITER_CREATE)) 4060ac51d99bSYonghong Song return -EINVAL; 4061ac51d99bSYonghong Song 4062ac51d99bSYonghong Song if (attr->iter_create.flags) 4063ac51d99bSYonghong Song return -EINVAL; 4064ac51d99bSYonghong Song 4065ac51d99bSYonghong Song link = bpf_link_get_from_fd(attr->iter_create.link_fd); 4066ac51d99bSYonghong Song if (IS_ERR(link)) 4067ac51d99bSYonghong Song return PTR_ERR(link); 4068ac51d99bSYonghong Song 4069ac51d99bSYonghong Song err = bpf_iter_new_fd(link); 4070ac51d99bSYonghong Song bpf_link_put(link); 4071ac51d99bSYonghong Song 4072ac51d99bSYonghong Song return err; 4073ac51d99bSYonghong Song } 4074ac51d99bSYonghong Song 407599c55f7dSAlexei Starovoitov SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) 407699c55f7dSAlexei Starovoitov { 40778096f229SGreg Kroah-Hartman union bpf_attr attr; 407899c55f7dSAlexei Starovoitov int err; 407999c55f7dSAlexei Starovoitov 40802c78ee89SAlexei Starovoitov if (sysctl_unprivileged_bpf_disabled && !bpf_capable()) 408199c55f7dSAlexei Starovoitov return -EPERM; 408299c55f7dSAlexei Starovoitov 4083dcab51f1SMartin KaFai Lau err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); 408499c55f7dSAlexei Starovoitov if (err) 408599c55f7dSAlexei Starovoitov return err; 40861e270976SMartin KaFai Lau size = min_t(u32, size, sizeof(attr)); 408799c55f7dSAlexei Starovoitov 408899c55f7dSAlexei Starovoitov /* copy attributes from user space, may be less than sizeof(bpf_attr) */ 40898096f229SGreg Kroah-Hartman memset(&attr, 0, sizeof(attr)); 409099c55f7dSAlexei Starovoitov if (copy_from_user(&attr, uattr, size) != 0) 409199c55f7dSAlexei Starovoitov return -EFAULT; 409299c55f7dSAlexei Starovoitov 4093afdb09c7SChenbo Feng err = security_bpf(cmd, &attr, size); 4094afdb09c7SChenbo Feng if (err < 0) 4095afdb09c7SChenbo Feng return err; 4096afdb09c7SChenbo Feng 409799c55f7dSAlexei Starovoitov switch (cmd) { 409899c55f7dSAlexei Starovoitov case BPF_MAP_CREATE: 409999c55f7dSAlexei Starovoitov err = map_create(&attr); 410099c55f7dSAlexei Starovoitov break; 4101db20fd2bSAlexei Starovoitov case BPF_MAP_LOOKUP_ELEM: 4102db20fd2bSAlexei Starovoitov err = map_lookup_elem(&attr); 4103db20fd2bSAlexei Starovoitov break; 4104db20fd2bSAlexei Starovoitov case BPF_MAP_UPDATE_ELEM: 4105db20fd2bSAlexei Starovoitov err = map_update_elem(&attr); 4106db20fd2bSAlexei Starovoitov break; 4107db20fd2bSAlexei Starovoitov case BPF_MAP_DELETE_ELEM: 4108db20fd2bSAlexei Starovoitov err = map_delete_elem(&attr); 4109db20fd2bSAlexei Starovoitov break; 4110db20fd2bSAlexei Starovoitov case BPF_MAP_GET_NEXT_KEY: 4111db20fd2bSAlexei Starovoitov err = map_get_next_key(&attr); 4112db20fd2bSAlexei Starovoitov break; 411387df15deSDaniel Borkmann case BPF_MAP_FREEZE: 411487df15deSDaniel Borkmann err = map_freeze(&attr); 411587df15deSDaniel Borkmann break; 411609756af4SAlexei Starovoitov case BPF_PROG_LOAD: 4117838e9690SYonghong Song err = bpf_prog_load(&attr, uattr); 411809756af4SAlexei Starovoitov break; 4119b2197755SDaniel Borkmann case BPF_OBJ_PIN: 4120b2197755SDaniel Borkmann err = bpf_obj_pin(&attr); 4121b2197755SDaniel Borkmann break; 4122b2197755SDaniel Borkmann case BPF_OBJ_GET: 4123b2197755SDaniel Borkmann err = bpf_obj_get(&attr); 4124b2197755SDaniel Borkmann break; 4125f4324551SDaniel Mack case BPF_PROG_ATTACH: 4126f4324551SDaniel Mack err = bpf_prog_attach(&attr); 4127f4324551SDaniel Mack break; 4128f4324551SDaniel Mack case BPF_PROG_DETACH: 4129f4324551SDaniel Mack err = bpf_prog_detach(&attr); 4130f4324551SDaniel Mack break; 4131468e2f64SAlexei Starovoitov case BPF_PROG_QUERY: 4132468e2f64SAlexei Starovoitov err = bpf_prog_query(&attr, uattr); 4133468e2f64SAlexei Starovoitov break; 41341cf1cae9SAlexei Starovoitov case BPF_PROG_TEST_RUN: 41351cf1cae9SAlexei Starovoitov err = bpf_prog_test_run(&attr, uattr); 41361cf1cae9SAlexei Starovoitov break; 413734ad5580SMartin KaFai Lau case BPF_PROG_GET_NEXT_ID: 413834ad5580SMartin KaFai Lau err = bpf_obj_get_next_id(&attr, uattr, 413934ad5580SMartin KaFai Lau &prog_idr, &prog_idr_lock); 414034ad5580SMartin KaFai Lau break; 414134ad5580SMartin KaFai Lau case BPF_MAP_GET_NEXT_ID: 414234ad5580SMartin KaFai Lau err = bpf_obj_get_next_id(&attr, uattr, 414334ad5580SMartin KaFai Lau &map_idr, &map_idr_lock); 414434ad5580SMartin KaFai Lau break; 41451b9ed84eSQuentin Monnet case BPF_BTF_GET_NEXT_ID: 41461b9ed84eSQuentin Monnet err = bpf_obj_get_next_id(&attr, uattr, 41471b9ed84eSQuentin Monnet &btf_idr, &btf_idr_lock); 41481b9ed84eSQuentin Monnet break; 4149b16d9aa4SMartin KaFai Lau case BPF_PROG_GET_FD_BY_ID: 4150b16d9aa4SMartin KaFai Lau err = bpf_prog_get_fd_by_id(&attr); 4151b16d9aa4SMartin KaFai Lau break; 4152bd5f5f4eSMartin KaFai Lau case BPF_MAP_GET_FD_BY_ID: 4153bd5f5f4eSMartin KaFai Lau err = bpf_map_get_fd_by_id(&attr); 4154bd5f5f4eSMartin KaFai Lau break; 41551e270976SMartin KaFai Lau case BPF_OBJ_GET_INFO_BY_FD: 41561e270976SMartin KaFai Lau err = bpf_obj_get_info_by_fd(&attr, uattr); 41571e270976SMartin KaFai Lau break; 4158c4f6699dSAlexei Starovoitov case BPF_RAW_TRACEPOINT_OPEN: 4159c4f6699dSAlexei Starovoitov err = bpf_raw_tracepoint_open(&attr); 4160c4f6699dSAlexei Starovoitov break; 4161f56a653cSMartin KaFai Lau case BPF_BTF_LOAD: 4162f56a653cSMartin KaFai Lau err = bpf_btf_load(&attr); 4163f56a653cSMartin KaFai Lau break; 416478958fcaSMartin KaFai Lau case BPF_BTF_GET_FD_BY_ID: 416578958fcaSMartin KaFai Lau err = bpf_btf_get_fd_by_id(&attr); 416678958fcaSMartin KaFai Lau break; 416741bdc4b4SYonghong Song case BPF_TASK_FD_QUERY: 416841bdc4b4SYonghong Song err = bpf_task_fd_query(&attr, uattr); 416941bdc4b4SYonghong Song break; 4170bd513cd0SMauricio Vasquez B case BPF_MAP_LOOKUP_AND_DELETE_ELEM: 4171bd513cd0SMauricio Vasquez B err = map_lookup_and_delete_elem(&attr); 4172bd513cd0SMauricio Vasquez B break; 4173cb4d03abSBrian Vazquez case BPF_MAP_LOOKUP_BATCH: 4174cb4d03abSBrian Vazquez err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH); 4175cb4d03abSBrian Vazquez break; 417605799638SYonghong Song case BPF_MAP_LOOKUP_AND_DELETE_BATCH: 417705799638SYonghong Song err = bpf_map_do_batch(&attr, uattr, 417805799638SYonghong Song BPF_MAP_LOOKUP_AND_DELETE_BATCH); 417905799638SYonghong Song break; 4180aa2e93b8SBrian Vazquez case BPF_MAP_UPDATE_BATCH: 4181aa2e93b8SBrian Vazquez err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH); 4182aa2e93b8SBrian Vazquez break; 4183aa2e93b8SBrian Vazquez case BPF_MAP_DELETE_BATCH: 4184aa2e93b8SBrian Vazquez err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH); 4185aa2e93b8SBrian Vazquez break; 4186af6eea57SAndrii Nakryiko case BPF_LINK_CREATE: 4187af6eea57SAndrii Nakryiko err = link_create(&attr); 4188af6eea57SAndrii Nakryiko break; 41890c991ebcSAndrii Nakryiko case BPF_LINK_UPDATE: 41900c991ebcSAndrii Nakryiko err = link_update(&attr); 41910c991ebcSAndrii Nakryiko break; 41922d602c8cSAndrii Nakryiko case BPF_LINK_GET_FD_BY_ID: 41932d602c8cSAndrii Nakryiko err = bpf_link_get_fd_by_id(&attr); 41942d602c8cSAndrii Nakryiko break; 41952d602c8cSAndrii Nakryiko case BPF_LINK_GET_NEXT_ID: 41962d602c8cSAndrii Nakryiko err = bpf_obj_get_next_id(&attr, uattr, 41972d602c8cSAndrii Nakryiko &link_idr, &link_idr_lock); 41982d602c8cSAndrii Nakryiko break; 4199d46edd67SSong Liu case BPF_ENABLE_STATS: 4200d46edd67SSong Liu err = bpf_enable_stats(&attr); 4201d46edd67SSong Liu break; 4202ac51d99bSYonghong Song case BPF_ITER_CREATE: 4203ac51d99bSYonghong Song err = bpf_iter_create(&attr); 4204ac51d99bSYonghong Song break; 420599c55f7dSAlexei Starovoitov default: 420699c55f7dSAlexei Starovoitov err = -EINVAL; 420799c55f7dSAlexei Starovoitov break; 420899c55f7dSAlexei Starovoitov } 420999c55f7dSAlexei Starovoitov 421099c55f7dSAlexei Starovoitov return err; 421199c55f7dSAlexei Starovoitov } 4212