Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 3 | */ |
| 4 | #include <linux/bpf.h> |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 5 | #include <linux/bpf_trace.h> |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 6 | #include <linux/bpf_lirc.h> |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 7 | #include <linux/btf.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 8 | #include <linux/syscalls.h> |
| 9 | #include <linux/slab.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 10 | #include <linux/sched/signal.h> |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 11 | #include <linux/vmalloc.h> |
| 12 | #include <linux/mmzone.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 13 | #include <linux/anon_inodes.h> |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 14 | #include <linux/fdtable.h> |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 15 | #include <linux/file.h> |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 16 | #include <linux/fs.h> |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 17 | #include <linux/license.h> |
| 18 | #include <linux/filter.h> |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 19 | #include <linux/version.h> |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 21 | #include <linux/idr.h> |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 22 | #include <linux/cred.h> |
| 23 | #include <linux/timekeeping.h> |
| 24 | #include <linux/ctype.h> |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 25 | #include <linux/nospec.h> |
Alexei Starovoitov | ccfe29e | 2019-10-15 20:24:58 -0700 | [diff] [blame] | 26 | #include <uapi/linux/btf.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 27 | |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 28 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ |
| 29 | (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ |
| 30 | (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ |
| 31 | (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) |
| 32 | #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) |
| 33 | #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map)) |
| 34 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 35 | #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) |
| 36 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 37 | DEFINE_PER_CPU(int, bpf_prog_active); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 38 | static DEFINE_IDR(prog_idr); |
| 39 | static DEFINE_SPINLOCK(prog_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 40 | static DEFINE_IDR(map_idr); |
| 41 | static DEFINE_SPINLOCK(map_idr_lock); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 42 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 43 | int sysctl_unprivileged_bpf_disabled __read_mostly; |
| 44 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 45 | static const struct bpf_map_ops * const bpf_map_types[] = { |
Alexei Starovoitov | 91cc1a9 | 2019-11-14 10:57:15 -0800 | [diff] [blame] | 46 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 47 | #define BPF_MAP_TYPE(_id, _ops) \ |
| 48 | [_id] = &_ops, |
| 49 | #include <linux/bpf_types.h> |
| 50 | #undef BPF_PROG_TYPE |
| 51 | #undef BPF_MAP_TYPE |
| 52 | }; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 53 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 54 | /* |
| 55 | * If we're handed a bigger struct than we know of, ensure all the unknown bits |
| 56 | * are 0 - i.e. new user-space does not rely on any kernel feature extensions |
| 57 | * we don't know about yet. |
| 58 | * |
| 59 | * There is a ToCToU between this function call and the following |
| 60 | * copy_from_user() call. However, this is not a concern since this function is |
| 61 | * meant to be a future-proofing of bits. |
| 62 | */ |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 63 | int bpf_check_uarg_tail_zero(void __user *uaddr, |
| 64 | size_t expected_size, |
| 65 | size_t actual_size) |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 66 | { |
| 67 | unsigned char __user *addr; |
| 68 | unsigned char __user *end; |
| 69 | unsigned char val; |
| 70 | int err; |
| 71 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 72 | if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ |
| 73 | return -E2BIG; |
| 74 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 75 | if (unlikely(!access_ok(uaddr, actual_size))) |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 76 | return -EFAULT; |
| 77 | |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 78 | if (actual_size <= expected_size) |
| 79 | return 0; |
| 80 | |
| 81 | addr = uaddr + expected_size; |
| 82 | end = uaddr + actual_size; |
| 83 | |
| 84 | for (; addr < end; addr++) { |
| 85 | err = get_user(val, addr); |
| 86 | if (err) |
| 87 | return err; |
| 88 | if (val) |
| 89 | return -E2BIG; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 95 | const struct bpf_map_ops bpf_map_offload_ops = { |
| 96 | .map_alloc = bpf_map_offload_map_alloc, |
| 97 | .map_free = bpf_map_offload_map_free, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 98 | .map_check_btf = map_check_no_btf, |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 101 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
| 102 | { |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 103 | const struct bpf_map_ops *ops; |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 104 | u32 type = attr->map_type; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 105 | struct bpf_map *map; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 106 | int err; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 107 | |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 108 | if (type >= ARRAY_SIZE(bpf_map_types)) |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 109 | return ERR_PTR(-EINVAL); |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 110 | type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types)); |
| 111 | ops = bpf_map_types[type]; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 112 | if (!ops) |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 113 | return ERR_PTR(-EINVAL); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 114 | |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 115 | if (ops->map_alloc_check) { |
| 116 | err = ops->map_alloc_check(attr); |
| 117 | if (err) |
| 118 | return ERR_PTR(err); |
| 119 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 120 | if (attr->map_ifindex) |
| 121 | ops = &bpf_map_offload_ops; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 122 | map = ops->map_alloc(attr); |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 123 | if (IS_ERR(map)) |
| 124 | return map; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 125 | map->ops = ops; |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 126 | map->map_type = type; |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 127 | return map; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 130 | static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable) |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 131 | { |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 132 | /* We really just want to fail instead of triggering OOM killer |
| 133 | * under memory pressure, therefore we set __GFP_NORETRY to kmalloc, |
| 134 | * which is used for lower order allocation requests. |
| 135 | * |
| 136 | * It has been observed that higher order allocation requests done by |
| 137 | * vmalloc with __GFP_NORETRY being set might fail due to not trying |
| 138 | * to reclaim memory from the page cache, thus we set |
| 139 | * __GFP_RETRY_MAYFAIL to avoid such situations. |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 140 | */ |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 141 | |
| 142 | const gfp_t flags = __GFP_NOWARN | __GFP_ZERO; |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 143 | void *area; |
| 144 | |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 145 | if (size >= SIZE_MAX) |
| 146 | return NULL; |
| 147 | |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 148 | /* kmalloc()'ed memory can't be mmap()'ed */ |
| 149 | if (!mmapable && size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 150 | area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags, |
| 151 | numa_node); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 152 | if (area != NULL) |
| 153 | return area; |
| 154 | } |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 155 | if (mmapable) { |
| 156 | BUG_ON(!PAGE_ALIGNED(size)); |
| 157 | return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL | |
| 158 | __GFP_RETRY_MAYFAIL | flags); |
| 159 | } |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 160 | return __vmalloc_node_flags_caller(size, numa_node, |
| 161 | GFP_KERNEL | __GFP_RETRY_MAYFAIL | |
| 162 | flags, __builtin_return_address(0)); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 165 | void *bpf_map_area_alloc(u64 size, int numa_node) |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 166 | { |
| 167 | return __bpf_map_area_alloc(size, numa_node, false); |
| 168 | } |
| 169 | |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 170 | void *bpf_map_area_mmapable_alloc(u64 size, int numa_node) |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 171 | { |
| 172 | return __bpf_map_area_alloc(size, numa_node, true); |
| 173 | } |
| 174 | |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 175 | void bpf_map_area_free(void *area) |
| 176 | { |
| 177 | kvfree(area); |
| 178 | } |
| 179 | |
Daniel Borkmann | be70bcd | 2019-04-09 23:20:04 +0200 | [diff] [blame] | 180 | static u32 bpf_map_flags_retain_permanent(u32 flags) |
| 181 | { |
| 182 | /* Some map creation flags are not tied to the map object but |
| 183 | * rather to the map fd instead, so they have no meaning upon |
| 184 | * map object inspection since multiple file descriptors with |
| 185 | * different (access) properties can exist here. Thus, given |
| 186 | * this has zero meaning for the map itself, lets clear these |
| 187 | * from here. |
| 188 | */ |
| 189 | return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY); |
| 190 | } |
| 191 | |
Jakub Kicinski | bd47564 | 2018-01-11 20:29:06 -0800 | [diff] [blame] | 192 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) |
| 193 | { |
| 194 | map->map_type = attr->map_type; |
| 195 | map->key_size = attr->key_size; |
| 196 | map->value_size = attr->value_size; |
| 197 | map->max_entries = attr->max_entries; |
Daniel Borkmann | be70bcd | 2019-04-09 23:20:04 +0200 | [diff] [blame] | 198 | map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags); |
Jakub Kicinski | bd47564 | 2018-01-11 20:29:06 -0800 | [diff] [blame] | 199 | map->numa_node = bpf_map_attr_numa_node(attr); |
| 200 | } |
| 201 | |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 202 | static int bpf_charge_memlock(struct user_struct *user, u32 pages) |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 203 | { |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 204 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 205 | |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 206 | if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) { |
| 207 | atomic_long_sub(pages, &user->locked_vm); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 208 | return -EPERM; |
| 209 | } |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 213 | static void bpf_uncharge_memlock(struct user_struct *user, u32 pages) |
| 214 | { |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 215 | if (user) |
| 216 | atomic_long_sub(pages, &user->locked_vm); |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 219 | int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size) |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 220 | { |
Roman Gushchin | c85d691 | 2019-05-29 18:03:59 -0700 | [diff] [blame] | 221 | u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT; |
| 222 | struct user_struct *user; |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 223 | int ret; |
| 224 | |
Roman Gushchin | c85d691 | 2019-05-29 18:03:59 -0700 | [diff] [blame] | 225 | if (size >= U32_MAX - PAGE_SIZE) |
| 226 | return -E2BIG; |
| 227 | |
| 228 | user = get_current_user(); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 229 | ret = bpf_charge_memlock(user, pages); |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 230 | if (ret) { |
| 231 | free_uid(user); |
| 232 | return ret; |
| 233 | } |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 234 | |
| 235 | mem->pages = pages; |
| 236 | mem->user = user; |
| 237 | |
| 238 | return 0; |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 241 | void bpf_map_charge_finish(struct bpf_map_memory *mem) |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 242 | { |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 243 | bpf_uncharge_memlock(mem->user, mem->pages); |
| 244 | free_uid(mem->user); |
| 245 | } |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 246 | |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 247 | void bpf_map_charge_move(struct bpf_map_memory *dst, |
| 248 | struct bpf_map_memory *src) |
| 249 | { |
| 250 | *dst = *src; |
| 251 | |
| 252 | /* Make sure src will not be used for the redundant uncharging. */ |
| 253 | memset(src, 0, sizeof(struct bpf_map_memory)); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 256 | int bpf_map_charge_memlock(struct bpf_map *map, u32 pages) |
| 257 | { |
| 258 | int ret; |
| 259 | |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 260 | ret = bpf_charge_memlock(map->memory.user, pages); |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 261 | if (ret) |
| 262 | return ret; |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 263 | map->memory.pages += pages; |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages) |
| 268 | { |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 269 | bpf_uncharge_memlock(map->memory.user, pages); |
| 270 | map->memory.pages -= pages; |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 273 | static int bpf_map_alloc_id(struct bpf_map *map) |
| 274 | { |
| 275 | int id; |
| 276 | |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 277 | idr_preload(GFP_KERNEL); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 278 | spin_lock_bh(&map_idr_lock); |
| 279 | id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); |
| 280 | if (id > 0) |
| 281 | map->id = id; |
| 282 | spin_unlock_bh(&map_idr_lock); |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 283 | idr_preload_end(); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 284 | |
| 285 | if (WARN_ON_ONCE(!id)) |
| 286 | return -ENOSPC; |
| 287 | |
| 288 | return id > 0 ? 0 : id; |
| 289 | } |
| 290 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 291 | void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock) |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 292 | { |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 293 | unsigned long flags; |
| 294 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 295 | /* Offloaded maps are removed from the IDR store when their device |
| 296 | * disappears - even if someone holds an fd to them they are unusable, |
| 297 | * the memory is gone, all ops will fail; they are simply waiting for |
| 298 | * refcnt to drop to be freed. |
| 299 | */ |
| 300 | if (!map->id) |
| 301 | return; |
| 302 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 303 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 304 | spin_lock_irqsave(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 305 | else |
| 306 | __acquire(&map_idr_lock); |
| 307 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 308 | idr_remove(&map_idr, map->id); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 309 | map->id = 0; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 310 | |
| 311 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 312 | spin_unlock_irqrestore(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 313 | else |
| 314 | __release(&map_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 317 | /* called from workqueue */ |
| 318 | static void bpf_map_free_deferred(struct work_struct *work) |
| 319 | { |
| 320 | struct bpf_map *map = container_of(work, struct bpf_map, work); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 321 | struct bpf_map_memory mem; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 322 | |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 323 | bpf_map_charge_move(&mem, &map->memory); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 324 | security_bpf_map_free(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 325 | /* implementation dependent freeing */ |
| 326 | map->ops->map_free(map); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 327 | bpf_map_charge_finish(&mem); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 330 | static void bpf_map_put_uref(struct bpf_map *map) |
| 331 | { |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 332 | if (atomic64_dec_and_test(&map->usercnt)) { |
John Fastabend | ba6b8de | 2018-04-23 15:39:23 -0700 | [diff] [blame] | 333 | if (map->ops->map_release_uref) |
| 334 | map->ops->map_release_uref(map); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 338 | /* decrement map refcnt and schedule it for freeing via workqueue |
| 339 | * (unrelying map implementation ops->map_free() might sleep) |
| 340 | */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 341 | static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 342 | { |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 343 | if (atomic64_dec_and_test(&map->refcnt)) { |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 344 | /* bpf_map_free_id() must be called first */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 345 | bpf_map_free_id(map, do_idr_lock); |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 346 | btf_put(map->btf); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 347 | INIT_WORK(&map->work, bpf_map_free_deferred); |
| 348 | schedule_work(&map->work); |
| 349 | } |
| 350 | } |
| 351 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 352 | void bpf_map_put(struct bpf_map *map) |
| 353 | { |
| 354 | __bpf_map_put(map, true); |
| 355 | } |
Jakub Kicinski | 630a4d3 | 2018-05-03 18:37:09 -0700 | [diff] [blame] | 356 | EXPORT_SYMBOL_GPL(bpf_map_put); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 357 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 358 | void bpf_map_put_with_uref(struct bpf_map *map) |
| 359 | { |
| 360 | bpf_map_put_uref(map); |
| 361 | bpf_map_put(map); |
| 362 | } |
| 363 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 364 | static int bpf_map_release(struct inode *inode, struct file *filp) |
| 365 | { |
Daniel Borkmann | 61d1b6a | 2016-06-15 22:47:12 +0200 | [diff] [blame] | 366 | struct bpf_map *map = filp->private_data; |
| 367 | |
| 368 | if (map->ops->map_release) |
| 369 | map->ops->map_release(map, filp); |
| 370 | |
| 371 | bpf_map_put_with_uref(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 375 | static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f) |
| 376 | { |
| 377 | fmode_t mode = f.file->f_mode; |
| 378 | |
| 379 | /* Our file permissions may have been overridden by global |
| 380 | * map permissions facing syscall side. |
| 381 | */ |
| 382 | if (READ_ONCE(map->frozen)) |
| 383 | mode &= ~FMODE_CAN_WRITE; |
| 384 | return mode; |
| 385 | } |
| 386 | |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 387 | #ifdef CONFIG_PROC_FS |
| 388 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) |
| 389 | { |
| 390 | const struct bpf_map *map = filp->private_data; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 391 | const struct bpf_array *array; |
Daniel Borkmann | 2beee5f | 2019-11-22 21:07:56 +0100 | [diff] [blame^] | 392 | u32 type = 0, jited = 0; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 393 | |
| 394 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { |
| 395 | array = container_of(map, struct bpf_array, map); |
Daniel Borkmann | 2beee5f | 2019-11-22 21:07:56 +0100 | [diff] [blame^] | 396 | type = array->aux->type; |
| 397 | jited = array->aux->jited; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 398 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 399 | |
| 400 | seq_printf(m, |
| 401 | "map_type:\t%u\n" |
| 402 | "key_size:\t%u\n" |
| 403 | "value_size:\t%u\n" |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 404 | "max_entries:\t%u\n" |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 405 | "map_flags:\t%#x\n" |
Daniel Borkmann | 4316b40 | 2018-06-02 23:06:34 +0200 | [diff] [blame] | 406 | "memlock:\t%llu\n" |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 407 | "map_id:\t%u\n" |
| 408 | "frozen:\t%u\n", |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 409 | map->map_type, |
| 410 | map->key_size, |
| 411 | map->value_size, |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 412 | map->max_entries, |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 413 | map->map_flags, |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 414 | map->memory.pages * 1ULL << PAGE_SHIFT, |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 415 | map->id, |
| 416 | READ_ONCE(map->frozen)); |
Daniel Borkmann | 2beee5f | 2019-11-22 21:07:56 +0100 | [diff] [blame^] | 417 | if (type) { |
| 418 | seq_printf(m, "owner_prog_type:\t%u\n", type); |
| 419 | seq_printf(m, "owner_jited:\t%u\n", jited); |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 420 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 421 | } |
| 422 | #endif |
| 423 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 424 | static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, |
| 425 | loff_t *ppos) |
| 426 | { |
| 427 | /* We need this handler such that alloc_file() enables |
| 428 | * f_mode with FMODE_CAN_READ. |
| 429 | */ |
| 430 | return -EINVAL; |
| 431 | } |
| 432 | |
| 433 | static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, |
| 434 | size_t siz, loff_t *ppos) |
| 435 | { |
| 436 | /* We need this handler such that alloc_file() enables |
| 437 | * f_mode with FMODE_CAN_WRITE. |
| 438 | */ |
| 439 | return -EINVAL; |
| 440 | } |
| 441 | |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 442 | /* called for any extra memory-mapped regions (except initial) */ |
| 443 | static void bpf_map_mmap_open(struct vm_area_struct *vma) |
| 444 | { |
| 445 | struct bpf_map *map = vma->vm_file->private_data; |
| 446 | |
| 447 | bpf_map_inc_with_uref(map); |
| 448 | |
| 449 | if (vma->vm_flags & VM_WRITE) { |
| 450 | mutex_lock(&map->freeze_mutex); |
| 451 | map->writecnt++; |
| 452 | mutex_unlock(&map->freeze_mutex); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /* called for all unmapped memory region (including initial) */ |
| 457 | static void bpf_map_mmap_close(struct vm_area_struct *vma) |
| 458 | { |
| 459 | struct bpf_map *map = vma->vm_file->private_data; |
| 460 | |
| 461 | if (vma->vm_flags & VM_WRITE) { |
| 462 | mutex_lock(&map->freeze_mutex); |
| 463 | map->writecnt--; |
| 464 | mutex_unlock(&map->freeze_mutex); |
| 465 | } |
| 466 | |
| 467 | bpf_map_put_with_uref(map); |
| 468 | } |
| 469 | |
| 470 | static const struct vm_operations_struct bpf_map_default_vmops = { |
| 471 | .open = bpf_map_mmap_open, |
| 472 | .close = bpf_map_mmap_close, |
| 473 | }; |
| 474 | |
| 475 | static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma) |
| 476 | { |
| 477 | struct bpf_map *map = filp->private_data; |
| 478 | int err; |
| 479 | |
| 480 | if (!map->ops->map_mmap || map_value_has_spin_lock(map)) |
| 481 | return -ENOTSUPP; |
| 482 | |
| 483 | if (!(vma->vm_flags & VM_SHARED)) |
| 484 | return -EINVAL; |
| 485 | |
| 486 | mutex_lock(&map->freeze_mutex); |
| 487 | |
| 488 | if ((vma->vm_flags & VM_WRITE) && map->frozen) { |
| 489 | err = -EPERM; |
| 490 | goto out; |
| 491 | } |
| 492 | |
| 493 | /* set default open/close callbacks */ |
| 494 | vma->vm_ops = &bpf_map_default_vmops; |
| 495 | vma->vm_private_data = map; |
| 496 | |
| 497 | err = map->ops->map_mmap(map, vma); |
| 498 | if (err) |
| 499 | goto out; |
| 500 | |
| 501 | bpf_map_inc_with_uref(map); |
| 502 | |
| 503 | if (vma->vm_flags & VM_WRITE) |
| 504 | map->writecnt++; |
| 505 | out: |
| 506 | mutex_unlock(&map->freeze_mutex); |
| 507 | return err; |
| 508 | } |
| 509 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 510 | const struct file_operations bpf_map_fops = { |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 511 | #ifdef CONFIG_PROC_FS |
| 512 | .show_fdinfo = bpf_map_show_fdinfo, |
| 513 | #endif |
| 514 | .release = bpf_map_release, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 515 | .read = bpf_dummy_read, |
| 516 | .write = bpf_dummy_write, |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 517 | .mmap = bpf_map_mmap, |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 518 | }; |
| 519 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 520 | int bpf_map_new_fd(struct bpf_map *map, int flags) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 521 | { |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 522 | int ret; |
| 523 | |
| 524 | ret = security_bpf_map(map, OPEN_FMODE(flags)); |
| 525 | if (ret < 0) |
| 526 | return ret; |
| 527 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 528 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 529 | flags | O_CLOEXEC); |
| 530 | } |
| 531 | |
| 532 | int bpf_get_file_flag(int flags) |
| 533 | { |
| 534 | if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) |
| 535 | return -EINVAL; |
| 536 | if (flags & BPF_F_RDONLY) |
| 537 | return O_RDONLY; |
| 538 | if (flags & BPF_F_WRONLY) |
| 539 | return O_WRONLY; |
| 540 | return O_RDWR; |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 541 | } |
| 542 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 543 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
| 544 | #define CHECK_ATTR(CMD) \ |
| 545 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ |
| 546 | sizeof(attr->CMD##_LAST_FIELD), 0, \ |
| 547 | sizeof(*attr) - \ |
| 548 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ |
| 549 | sizeof(attr->CMD##_LAST_FIELD)) != NULL |
| 550 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 551 | /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes. |
| 552 | * Return 0 on success and < 0 on error. |
| 553 | */ |
| 554 | static int bpf_obj_name_cpy(char *dst, const char *src) |
| 555 | { |
| 556 | const char *end = src + BPF_OBJ_NAME_LEN; |
| 557 | |
Martin KaFai Lau | 473d973 | 2017-10-05 21:52:11 -0700 | [diff] [blame] | 558 | memset(dst, 0, BPF_OBJ_NAME_LEN); |
Daniel Borkmann | 3e0ddc4f | 2019-04-09 23:20:07 +0200 | [diff] [blame] | 559 | /* Copy all isalnum(), '_' and '.' chars. */ |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 560 | while (src < end && *src) { |
Daniel Borkmann | 3e0ddc4f | 2019-04-09 23:20:07 +0200 | [diff] [blame] | 561 | if (!isalnum(*src) && |
| 562 | *src != '_' && *src != '.') |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 563 | return -EINVAL; |
| 564 | *dst++ = *src++; |
| 565 | } |
| 566 | |
| 567 | /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */ |
| 568 | if (src == end) |
| 569 | return -EINVAL; |
| 570 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 574 | int map_check_no_btf(const struct bpf_map *map, |
Roman Gushchin | 1b2b234 | 2018-12-10 15:43:00 -0800 | [diff] [blame] | 575 | const struct btf *btf, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 576 | const struct btf_type *key_type, |
| 577 | const struct btf_type *value_type) |
| 578 | { |
| 579 | return -ENOTSUPP; |
| 580 | } |
| 581 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 582 | static int map_check_btf(struct bpf_map *map, const struct btf *btf, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 583 | u32 btf_key_id, u32 btf_value_id) |
| 584 | { |
| 585 | const struct btf_type *key_type, *value_type; |
| 586 | u32 key_size, value_size; |
| 587 | int ret = 0; |
| 588 | |
Daniel Borkmann | 2824ecb | 2019-04-09 23:20:10 +0200 | [diff] [blame] | 589 | /* Some maps allow key to be unspecified. */ |
| 590 | if (btf_key_id) { |
| 591 | key_type = btf_type_id_size(btf, &btf_key_id, &key_size); |
| 592 | if (!key_type || key_size != map->key_size) |
| 593 | return -EINVAL; |
| 594 | } else { |
| 595 | key_type = btf_type_by_id(btf, 0); |
| 596 | if (!map->ops->map_check_btf) |
| 597 | return -EINVAL; |
| 598 | } |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 599 | |
| 600 | value_type = btf_type_id_size(btf, &btf_value_id, &value_size); |
| 601 | if (!value_type || value_size != map->value_size) |
| 602 | return -EINVAL; |
| 603 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 604 | map->spin_lock_off = btf_find_spin_lock(btf, value_type); |
| 605 | |
| 606 | if (map_value_has_spin_lock(map)) { |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 607 | if (map->map_flags & BPF_F_RDONLY_PROG) |
| 608 | return -EACCES; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 609 | if (map->map_type != BPF_MAP_TYPE_HASH && |
Alexei Starovoitov | e16d2f1 | 2019-01-31 15:40:05 -0800 | [diff] [blame] | 610 | map->map_type != BPF_MAP_TYPE_ARRAY && |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 611 | map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 612 | map->map_type != BPF_MAP_TYPE_SK_STORAGE) |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 613 | return -ENOTSUPP; |
| 614 | if (map->spin_lock_off + sizeof(struct bpf_spin_lock) > |
| 615 | map->value_size) { |
| 616 | WARN_ONCE(1, |
| 617 | "verifier bug spin_lock_off %d value_size %d\n", |
| 618 | map->spin_lock_off, map->value_size); |
| 619 | return -EFAULT; |
| 620 | } |
| 621 | } |
| 622 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 623 | if (map->ops->map_check_btf) |
Roman Gushchin | 1b2b234 | 2018-12-10 15:43:00 -0800 | [diff] [blame] | 624 | ret = map->ops->map_check_btf(map, btf, key_type, value_type); |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 625 | |
| 626 | return ret; |
| 627 | } |
| 628 | |
Martin KaFai Lau | 9b2cf32 | 2018-05-22 14:57:21 -0700 | [diff] [blame] | 629 | #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 630 | /* called via syscall */ |
| 631 | static int map_create(union bpf_attr *attr) |
| 632 | { |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 633 | int numa_node = bpf_map_attr_numa_node(attr); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 634 | struct bpf_map_memory mem; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 635 | struct bpf_map *map; |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 636 | int f_flags; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 637 | int err; |
| 638 | |
| 639 | err = CHECK_ATTR(BPF_MAP_CREATE); |
| 640 | if (err) |
| 641 | return -EINVAL; |
| 642 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 643 | f_flags = bpf_get_file_flag(attr->map_flags); |
| 644 | if (f_flags < 0) |
| 645 | return f_flags; |
| 646 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 647 | if (numa_node != NUMA_NO_NODE && |
Eric Dumazet | 96e5ae4 | 2017-09-04 22:41:02 -0700 | [diff] [blame] | 648 | ((unsigned int)numa_node >= nr_node_ids || |
| 649 | !node_online(numa_node))) |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 650 | return -EINVAL; |
| 651 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 652 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ |
| 653 | map = find_and_alloc_map(attr); |
| 654 | if (IS_ERR(map)) |
| 655 | return PTR_ERR(map); |
| 656 | |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 657 | err = bpf_obj_name_cpy(map->name, attr->map_name); |
| 658 | if (err) |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 659 | goto free_map; |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 660 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 661 | atomic64_set(&map->refcnt, 1); |
| 662 | atomic64_set(&map->usercnt, 1); |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 663 | mutex_init(&map->freeze_mutex); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 664 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 665 | if (attr->btf_key_type_id || attr->btf_value_type_id) { |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 666 | struct btf *btf; |
| 667 | |
Daniel Borkmann | 2824ecb | 2019-04-09 23:20:10 +0200 | [diff] [blame] | 668 | if (!attr->btf_value_type_id) { |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 669 | err = -EINVAL; |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 670 | goto free_map; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | btf = btf_get_by_fd(attr->btf_fd); |
| 674 | if (IS_ERR(btf)) { |
| 675 | err = PTR_ERR(btf); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 676 | goto free_map; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 679 | err = map_check_btf(map, btf, attr->btf_key_type_id, |
| 680 | attr->btf_value_type_id); |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 681 | if (err) { |
| 682 | btf_put(btf); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 683 | goto free_map; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | map->btf = btf; |
Martin KaFai Lau | 9b2cf32 | 2018-05-22 14:57:21 -0700 | [diff] [blame] | 687 | map->btf_key_type_id = attr->btf_key_type_id; |
| 688 | map->btf_value_type_id = attr->btf_value_type_id; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 689 | } else { |
| 690 | map->spin_lock_off = -EINVAL; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 693 | err = security_bpf_map_alloc(map); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 694 | if (err) |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 695 | goto free_map; |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 696 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 697 | err = bpf_map_alloc_id(map); |
| 698 | if (err) |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 699 | goto free_map_sec; |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 700 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 701 | err = bpf_map_new_fd(map, f_flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 702 | if (err < 0) { |
| 703 | /* failed to allocate fd. |
Peng Sun | 352d20d | 2019-02-27 22:36:25 +0800 | [diff] [blame] | 704 | * bpf_map_put_with_uref() is needed because the above |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 705 | * bpf_map_alloc_id() has published the map |
| 706 | * to the userspace and the userspace may |
| 707 | * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. |
| 708 | */ |
Peng Sun | 352d20d | 2019-02-27 22:36:25 +0800 | [diff] [blame] | 709 | bpf_map_put_with_uref(map); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 710 | return err; |
| 711 | } |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 712 | |
| 713 | return err; |
| 714 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 715 | free_map_sec: |
| 716 | security_bpf_map_free(map); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 717 | free_map: |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 718 | btf_put(map->btf); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 719 | bpf_map_charge_move(&mem, &map->memory); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 720 | map->ops->map_free(map); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 721 | bpf_map_charge_finish(&mem); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 722 | return err; |
| 723 | } |
| 724 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 725 | /* if error is returned, fd is released. |
| 726 | * On success caller should complete fd access with matching fdput() |
| 727 | */ |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 728 | struct bpf_map *__bpf_map_get(struct fd f) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 729 | { |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 730 | if (!f.file) |
| 731 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 732 | if (f.file->f_op != &bpf_map_fops) { |
| 733 | fdput(f); |
| 734 | return ERR_PTR(-EINVAL); |
| 735 | } |
| 736 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 737 | return f.file->private_data; |
| 738 | } |
| 739 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 740 | void bpf_map_inc(struct bpf_map *map) |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 741 | { |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 742 | atomic64_inc(&map->refcnt); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 743 | } |
Jakub Kicinski | 630a4d3 | 2018-05-03 18:37:09 -0700 | [diff] [blame] | 744 | EXPORT_SYMBOL_GPL(bpf_map_inc); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 745 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 746 | void bpf_map_inc_with_uref(struct bpf_map *map) |
| 747 | { |
| 748 | atomic64_inc(&map->refcnt); |
| 749 | atomic64_inc(&map->usercnt); |
| 750 | } |
| 751 | EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref); |
| 752 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 753 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 754 | { |
| 755 | struct fd f = fdget(ufd); |
| 756 | struct bpf_map *map; |
| 757 | |
| 758 | map = __bpf_map_get(f); |
| 759 | if (IS_ERR(map)) |
| 760 | return map; |
| 761 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 762 | bpf_map_inc_with_uref(map); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 763 | fdput(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 764 | |
| 765 | return map; |
| 766 | } |
| 767 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 768 | /* map_idr_lock should have been held */ |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 769 | static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref) |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 770 | { |
| 771 | int refold; |
| 772 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 773 | refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 774 | if (!refold) |
| 775 | return ERR_PTR(-ENOENT); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 776 | if (uref) |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 777 | atomic64_inc(&map->usercnt); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 778 | |
| 779 | return map; |
| 780 | } |
| 781 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 782 | struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map) |
Stanislav Fomichev | b0e4701 | 2019-08-14 10:37:48 -0700 | [diff] [blame] | 783 | { |
| 784 | spin_lock_bh(&map_idr_lock); |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 785 | map = __bpf_map_inc_not_zero(map, false); |
Stanislav Fomichev | b0e4701 | 2019-08-14 10:37:48 -0700 | [diff] [blame] | 786 | spin_unlock_bh(&map_idr_lock); |
| 787 | |
| 788 | return map; |
| 789 | } |
| 790 | EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero); |
| 791 | |
Alexei Starovoitov | b8cdc05 | 2016-03-09 18:56:49 -0800 | [diff] [blame] | 792 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
| 793 | { |
| 794 | return -ENOTSUPP; |
| 795 | } |
| 796 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 797 | static void *__bpf_copy_key(void __user *ukey, u64 key_size) |
| 798 | { |
| 799 | if (key_size) |
| 800 | return memdup_user(ukey, key_size); |
| 801 | |
| 802 | if (ukey) |
| 803 | return ERR_PTR(-EINVAL); |
| 804 | |
| 805 | return NULL; |
| 806 | } |
| 807 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 808 | /* last field in 'union bpf_attr' used by this command */ |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 809 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 810 | |
| 811 | static int map_lookup_elem(union bpf_attr *attr) |
| 812 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 813 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 814 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 815 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 816 | struct bpf_map *map; |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 817 | void *key, *value, *ptr; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 818 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 819 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 820 | int err; |
| 821 | |
| 822 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) |
| 823 | return -EINVAL; |
| 824 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 825 | if (attr->flags & ~BPF_F_LOCK) |
| 826 | return -EINVAL; |
| 827 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 828 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 829 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 830 | if (IS_ERR(map)) |
| 831 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 832 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 833 | err = -EPERM; |
| 834 | goto err_put; |
| 835 | } |
| 836 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 837 | if ((attr->flags & BPF_F_LOCK) && |
| 838 | !map_value_has_spin_lock(map)) { |
| 839 | err = -EINVAL; |
| 840 | goto err_put; |
| 841 | } |
| 842 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 843 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 844 | if (IS_ERR(key)) { |
| 845 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 846 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 847 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 848 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 849 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 850 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 851 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || |
| 852 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 853 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 854 | else if (IS_FD_MAP(map)) |
| 855 | value_size = sizeof(u32); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 856 | else |
| 857 | value_size = map->value_size; |
| 858 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 859 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 860 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 861 | if (!value) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 862 | goto free_key; |
| 863 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 864 | if (bpf_map_is_dev_bound(map)) { |
| 865 | err = bpf_map_offload_lookup_elem(map, key, value); |
Martin KaFai Lau | 7c4cd05 | 2019-01-30 18:12:45 -0800 | [diff] [blame] | 866 | goto done; |
| 867 | } |
| 868 | |
| 869 | preempt_disable(); |
| 870 | this_cpu_inc(bpf_prog_active); |
| 871 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 872 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 873 | err = bpf_percpu_hash_copy(map, key, value); |
| 874 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 875 | err = bpf_percpu_array_copy(map, key, value); |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 876 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { |
| 877 | err = bpf_percpu_cgroup_storage_copy(map, key, value); |
Alexei Starovoitov | 557c0c6 | 2016-03-07 21:57:17 -0800 | [diff] [blame] | 878 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
| 879 | err = bpf_stackmap_copy(map, key, value); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 880 | } else if (IS_FD_ARRAY(map)) { |
| 881 | err = bpf_fd_array_map_lookup_elem(map, key, value); |
| 882 | } else if (IS_FD_HASH(map)) { |
| 883 | err = bpf_fd_htab_map_lookup_elem(map, key, value); |
Martin KaFai Lau | 5dc4c4b | 2018-08-08 01:01:24 -0700 | [diff] [blame] | 884 | } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { |
| 885 | err = bpf_fd_reuseport_array_lookup_elem(map, key, value); |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 886 | } else if (map->map_type == BPF_MAP_TYPE_QUEUE || |
| 887 | map->map_type == BPF_MAP_TYPE_STACK) { |
| 888 | err = map->ops->map_peek_elem(map, value); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 889 | } else { |
| 890 | rcu_read_lock(); |
Daniel Borkmann | c611022 | 2019-05-14 01:18:55 +0200 | [diff] [blame] | 891 | if (map->ops->map_lookup_elem_sys_only) |
| 892 | ptr = map->ops->map_lookup_elem_sys_only(map, key); |
| 893 | else |
| 894 | ptr = map->ops->map_lookup_elem(map, key); |
Prashant Bhole | 509db28 | 2018-10-09 10:04:49 +0900 | [diff] [blame] | 895 | if (IS_ERR(ptr)) { |
| 896 | err = PTR_ERR(ptr); |
| 897 | } else if (!ptr) { |
| 898 | err = -ENOENT; |
| 899 | } else { |
| 900 | err = 0; |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 901 | if (attr->flags & BPF_F_LOCK) |
| 902 | /* lock 'ptr' and copy everything but lock */ |
| 903 | copy_map_value_locked(map, value, ptr, true); |
| 904 | else |
| 905 | copy_map_value(map, value, ptr); |
| 906 | /* mask lock, since value wasn't zero inited */ |
| 907 | check_and_init_map_lock(map, value); |
Prashant Bhole | 509db28 | 2018-10-09 10:04:49 +0900 | [diff] [blame] | 908 | } |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 909 | rcu_read_unlock(); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 910 | } |
Martin KaFai Lau | 7c4cd05 | 2019-01-30 18:12:45 -0800 | [diff] [blame] | 911 | this_cpu_dec(bpf_prog_active); |
| 912 | preempt_enable(); |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 913 | |
Martin KaFai Lau | 7c4cd05 | 2019-01-30 18:12:45 -0800 | [diff] [blame] | 914 | done: |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 915 | if (err) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 916 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 917 | |
| 918 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 919 | if (copy_to_user(uvalue, value, value_size) != 0) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 920 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 921 | |
| 922 | err = 0; |
| 923 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 924 | free_value: |
| 925 | kfree(value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 926 | free_key: |
| 927 | kfree(key); |
| 928 | err_put: |
| 929 | fdput(f); |
| 930 | return err; |
| 931 | } |
| 932 | |
Daniel Colascione | 1ae80cf | 2018-10-12 03:54:27 -0700 | [diff] [blame] | 933 | static void maybe_wait_bpf_programs(struct bpf_map *map) |
| 934 | { |
| 935 | /* Wait for any running BPF programs to complete so that |
| 936 | * userspace, when we return to it, knows that all programs |
| 937 | * that could be running use the new map value. |
| 938 | */ |
| 939 | if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS || |
| 940 | map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) |
| 941 | synchronize_rcu(); |
| 942 | } |
| 943 | |
Alexei Starovoitov | 3274f52 | 2014-11-13 17:36:44 -0800 | [diff] [blame] | 944 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 945 | |
| 946 | static int map_update_elem(union bpf_attr *attr) |
| 947 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 948 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 949 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 950 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 951 | struct bpf_map *map; |
| 952 | void *key, *value; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 953 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 954 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 955 | int err; |
| 956 | |
| 957 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) |
| 958 | return -EINVAL; |
| 959 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 960 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 961 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 962 | if (IS_ERR(map)) |
| 963 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 964 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 965 | err = -EPERM; |
| 966 | goto err_put; |
| 967 | } |
| 968 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 969 | if ((attr->flags & BPF_F_LOCK) && |
| 970 | !map_value_has_spin_lock(map)) { |
| 971 | err = -EINVAL; |
| 972 | goto err_put; |
| 973 | } |
| 974 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 975 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 976 | if (IS_ERR(key)) { |
| 977 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 978 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 979 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 980 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 981 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 982 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 983 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || |
| 984 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 985 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
| 986 | else |
| 987 | value_size = map->value_size; |
| 988 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 989 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 990 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 991 | if (!value) |
| 992 | goto free_key; |
| 993 | |
| 994 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 995 | if (copy_from_user(value, uvalue, value_size) != 0) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 996 | goto free_value; |
| 997 | |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 998 | /* Need to create a kthread, thus must support schedule */ |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 999 | if (bpf_map_is_dev_bound(map)) { |
| 1000 | err = bpf_map_offload_update_elem(map, key, value, attr->flags); |
| 1001 | goto out; |
John Fastabend | 99ba2b5 | 2018-07-05 08:50:04 -0700 | [diff] [blame] | 1002 | } else if (map->map_type == BPF_MAP_TYPE_CPUMAP || |
| 1003 | map->map_type == BPF_MAP_TYPE_SOCKHASH || |
| 1004 | map->map_type == BPF_MAP_TYPE_SOCKMAP) { |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 1005 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
| 1006 | goto out; |
| 1007 | } |
| 1008 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 1009 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering from |
| 1010 | * inside bpf map update or delete otherwise deadlocks are possible |
| 1011 | */ |
| 1012 | preempt_disable(); |
| 1013 | __this_cpu_inc(bpf_prog_active); |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 1014 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 1015 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1016 | err = bpf_percpu_hash_update(map, key, value, attr->flags); |
| 1017 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 1018 | err = bpf_percpu_array_update(map, key, value, attr->flags); |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 1019 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { |
| 1020 | err = bpf_percpu_cgroup_storage_update(map, key, value, |
| 1021 | attr->flags); |
Mickaël Salaün | 9c147b5 | 2018-01-26 00:54:02 +0100 | [diff] [blame] | 1022 | } else if (IS_FD_ARRAY(map)) { |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 1023 | rcu_read_lock(); |
| 1024 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, |
| 1025 | attr->flags); |
| 1026 | rcu_read_unlock(); |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 1027 | } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
| 1028 | rcu_read_lock(); |
| 1029 | err = bpf_fd_htab_map_update_elem(map, f.file, key, value, |
| 1030 | attr->flags); |
| 1031 | rcu_read_unlock(); |
Martin KaFai Lau | 5dc4c4b | 2018-08-08 01:01:24 -0700 | [diff] [blame] | 1032 | } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { |
| 1033 | /* rcu_read_lock() is not needed */ |
| 1034 | err = bpf_fd_reuseport_array_update_elem(map, key, value, |
| 1035 | attr->flags); |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 1036 | } else if (map->map_type == BPF_MAP_TYPE_QUEUE || |
| 1037 | map->map_type == BPF_MAP_TYPE_STACK) { |
| 1038 | err = map->ops->map_push_elem(map, value, attr->flags); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1039 | } else { |
| 1040 | rcu_read_lock(); |
| 1041 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
| 1042 | rcu_read_unlock(); |
| 1043 | } |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 1044 | __this_cpu_dec(bpf_prog_active); |
| 1045 | preempt_enable(); |
Daniel Colascione | 1ae80cf | 2018-10-12 03:54:27 -0700 | [diff] [blame] | 1046 | maybe_wait_bpf_programs(map); |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 1047 | out: |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1048 | free_value: |
| 1049 | kfree(value); |
| 1050 | free_key: |
| 1051 | kfree(key); |
| 1052 | err_put: |
| 1053 | fdput(f); |
| 1054 | return err; |
| 1055 | } |
| 1056 | |
| 1057 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key |
| 1058 | |
| 1059 | static int map_delete_elem(union bpf_attr *attr) |
| 1060 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1061 | void __user *ukey = u64_to_user_ptr(attr->key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1062 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1063 | struct bpf_map *map; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1064 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1065 | void *key; |
| 1066 | int err; |
| 1067 | |
| 1068 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) |
| 1069 | return -EINVAL; |
| 1070 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1071 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1072 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1073 | if (IS_ERR(map)) |
| 1074 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1075 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1076 | err = -EPERM; |
| 1077 | goto err_put; |
| 1078 | } |
| 1079 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 1080 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1081 | if (IS_ERR(key)) { |
| 1082 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1083 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1084 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1085 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1086 | if (bpf_map_is_dev_bound(map)) { |
| 1087 | err = bpf_map_offload_delete_elem(map, key); |
| 1088 | goto out; |
| 1089 | } |
| 1090 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 1091 | preempt_disable(); |
| 1092 | __this_cpu_inc(bpf_prog_active); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1093 | rcu_read_lock(); |
| 1094 | err = map->ops->map_delete_elem(map, key); |
| 1095 | rcu_read_unlock(); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 1096 | __this_cpu_dec(bpf_prog_active); |
| 1097 | preempt_enable(); |
Daniel Colascione | 1ae80cf | 2018-10-12 03:54:27 -0700 | [diff] [blame] | 1098 | maybe_wait_bpf_programs(map); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1099 | out: |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1100 | kfree(key); |
| 1101 | err_put: |
| 1102 | fdput(f); |
| 1103 | return err; |
| 1104 | } |
| 1105 | |
| 1106 | /* last field in 'union bpf_attr' used by this command */ |
| 1107 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key |
| 1108 | |
| 1109 | static int map_get_next_key(union bpf_attr *attr) |
| 1110 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1111 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 1112 | void __user *unext_key = u64_to_user_ptr(attr->next_key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1113 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1114 | struct bpf_map *map; |
| 1115 | void *key, *next_key; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1116 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1117 | int err; |
| 1118 | |
| 1119 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) |
| 1120 | return -EINVAL; |
| 1121 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1122 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1123 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1124 | if (IS_ERR(map)) |
| 1125 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1126 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1127 | err = -EPERM; |
| 1128 | goto err_put; |
| 1129 | } |
| 1130 | |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 1131 | if (ukey) { |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 1132 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1133 | if (IS_ERR(key)) { |
| 1134 | err = PTR_ERR(key); |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 1135 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1136 | } |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 1137 | } else { |
| 1138 | key = NULL; |
| 1139 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1140 | |
| 1141 | err = -ENOMEM; |
| 1142 | next_key = kmalloc(map->key_size, GFP_USER); |
| 1143 | if (!next_key) |
| 1144 | goto free_key; |
| 1145 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1146 | if (bpf_map_is_dev_bound(map)) { |
| 1147 | err = bpf_map_offload_get_next_key(map, key, next_key); |
| 1148 | goto out; |
| 1149 | } |
| 1150 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1151 | rcu_read_lock(); |
| 1152 | err = map->ops->map_get_next_key(map, key, next_key); |
| 1153 | rcu_read_unlock(); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1154 | out: |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1155 | if (err) |
| 1156 | goto free_next_key; |
| 1157 | |
| 1158 | err = -EFAULT; |
| 1159 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) |
| 1160 | goto free_next_key; |
| 1161 | |
| 1162 | err = 0; |
| 1163 | |
| 1164 | free_next_key: |
| 1165 | kfree(next_key); |
| 1166 | free_key: |
| 1167 | kfree(key); |
| 1168 | err_put: |
| 1169 | fdput(f); |
| 1170 | return err; |
| 1171 | } |
| 1172 | |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1173 | #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value |
| 1174 | |
| 1175 | static int map_lookup_and_delete_elem(union bpf_attr *attr) |
| 1176 | { |
| 1177 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 1178 | void __user *uvalue = u64_to_user_ptr(attr->value); |
| 1179 | int ufd = attr->map_fd; |
| 1180 | struct bpf_map *map; |
Alexei Starovoitov | 540fefc | 2018-10-19 13:52:38 -0700 | [diff] [blame] | 1181 | void *key, *value; |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1182 | u32 value_size; |
| 1183 | struct fd f; |
| 1184 | int err; |
| 1185 | |
| 1186 | if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM)) |
| 1187 | return -EINVAL; |
| 1188 | |
| 1189 | f = fdget(ufd); |
| 1190 | map = __bpf_map_get(f); |
| 1191 | if (IS_ERR(map)) |
| 1192 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1193 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1194 | err = -EPERM; |
| 1195 | goto err_put; |
| 1196 | } |
| 1197 | |
| 1198 | key = __bpf_copy_key(ukey, map->key_size); |
| 1199 | if (IS_ERR(key)) { |
| 1200 | err = PTR_ERR(key); |
| 1201 | goto err_put; |
| 1202 | } |
| 1203 | |
| 1204 | value_size = map->value_size; |
| 1205 | |
| 1206 | err = -ENOMEM; |
| 1207 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
| 1208 | if (!value) |
| 1209 | goto free_key; |
| 1210 | |
| 1211 | if (map->map_type == BPF_MAP_TYPE_QUEUE || |
| 1212 | map->map_type == BPF_MAP_TYPE_STACK) { |
| 1213 | err = map->ops->map_pop_elem(map, value); |
| 1214 | } else { |
| 1215 | err = -ENOTSUPP; |
| 1216 | } |
| 1217 | |
| 1218 | if (err) |
| 1219 | goto free_value; |
| 1220 | |
| 1221 | if (copy_to_user(uvalue, value, value_size) != 0) |
| 1222 | goto free_value; |
| 1223 | |
| 1224 | err = 0; |
| 1225 | |
| 1226 | free_value: |
| 1227 | kfree(value); |
| 1228 | free_key: |
| 1229 | kfree(key); |
| 1230 | err_put: |
| 1231 | fdput(f); |
| 1232 | return err; |
| 1233 | } |
| 1234 | |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1235 | #define BPF_MAP_FREEZE_LAST_FIELD map_fd |
| 1236 | |
| 1237 | static int map_freeze(const union bpf_attr *attr) |
| 1238 | { |
| 1239 | int err = 0, ufd = attr->map_fd; |
| 1240 | struct bpf_map *map; |
| 1241 | struct fd f; |
| 1242 | |
| 1243 | if (CHECK_ATTR(BPF_MAP_FREEZE)) |
| 1244 | return -EINVAL; |
| 1245 | |
| 1246 | f = fdget(ufd); |
| 1247 | map = __bpf_map_get(f); |
| 1248 | if (IS_ERR(map)) |
| 1249 | return PTR_ERR(map); |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 1250 | |
| 1251 | mutex_lock(&map->freeze_mutex); |
| 1252 | |
| 1253 | if (map->writecnt) { |
| 1254 | err = -EBUSY; |
| 1255 | goto err_put; |
| 1256 | } |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1257 | if (READ_ONCE(map->frozen)) { |
| 1258 | err = -EBUSY; |
| 1259 | goto err_put; |
| 1260 | } |
| 1261 | if (!capable(CAP_SYS_ADMIN)) { |
| 1262 | err = -EPERM; |
| 1263 | goto err_put; |
| 1264 | } |
| 1265 | |
| 1266 | WRITE_ONCE(map->frozen, true); |
| 1267 | err_put: |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 1268 | mutex_unlock(&map->freeze_mutex); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1269 | fdput(f); |
| 1270 | return err; |
| 1271 | } |
| 1272 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 1273 | static const struct bpf_prog_ops * const bpf_prog_types[] = { |
Alexei Starovoitov | 91cc1a9 | 2019-11-14 10:57:15 -0800 | [diff] [blame] | 1274 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 1275 | [_id] = & _name ## _prog_ops, |
| 1276 | #define BPF_MAP_TYPE(_id, _ops) |
| 1277 | #include <linux/bpf_types.h> |
| 1278 | #undef BPF_PROG_TYPE |
| 1279 | #undef BPF_MAP_TYPE |
| 1280 | }; |
| 1281 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1282 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
| 1283 | { |
Daniel Borkmann | d0f1a45 | 2018-05-04 02:13:57 +0200 | [diff] [blame] | 1284 | const struct bpf_prog_ops *ops; |
| 1285 | |
| 1286 | if (type >= ARRAY_SIZE(bpf_prog_types)) |
| 1287 | return -EINVAL; |
| 1288 | type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types)); |
| 1289 | ops = bpf_prog_types[type]; |
| 1290 | if (!ops) |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 1291 | return -EINVAL; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1292 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1293 | if (!bpf_prog_is_dev_bound(prog->aux)) |
Daniel Borkmann | d0f1a45 | 2018-05-04 02:13:57 +0200 | [diff] [blame] | 1294 | prog->aux->ops = ops; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1295 | else |
| 1296 | prog->aux->ops = &bpf_offload_prog_ops; |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 1297 | prog->type = type; |
| 1298 | return 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1301 | int __bpf_prog_charge(struct user_struct *user, u32 pages) |
| 1302 | { |
| 1303 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 1304 | unsigned long user_bufs; |
| 1305 | |
| 1306 | if (user) { |
| 1307 | user_bufs = atomic_long_add_return(pages, &user->locked_vm); |
| 1308 | if (user_bufs > memlock_limit) { |
| 1309 | atomic_long_sub(pages, &user->locked_vm); |
| 1310 | return -EPERM; |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
| 1317 | void __bpf_prog_uncharge(struct user_struct *user, u32 pages) |
| 1318 | { |
| 1319 | if (user) |
| 1320 | atomic_long_sub(pages, &user->locked_vm); |
| 1321 | } |
| 1322 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1323 | static int bpf_prog_charge_memlock(struct bpf_prog *prog) |
| 1324 | { |
| 1325 | struct user_struct *user = get_current_user(); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1326 | int ret; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1327 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1328 | ret = __bpf_prog_charge(user, prog->pages); |
| 1329 | if (ret) { |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1330 | free_uid(user); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1331 | return ret; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1332 | } |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1333 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1334 | prog->aux->user = user; |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
| 1338 | static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) |
| 1339 | { |
| 1340 | struct user_struct *user = prog->aux->user; |
| 1341 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1342 | __bpf_prog_uncharge(user, prog->pages); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1343 | free_uid(user); |
| 1344 | } |
| 1345 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1346 | static int bpf_prog_alloc_id(struct bpf_prog *prog) |
| 1347 | { |
| 1348 | int id; |
| 1349 | |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 1350 | idr_preload(GFP_KERNEL); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1351 | spin_lock_bh(&prog_idr_lock); |
| 1352 | id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); |
| 1353 | if (id > 0) |
| 1354 | prog->aux->id = id; |
| 1355 | spin_unlock_bh(&prog_idr_lock); |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 1356 | idr_preload_end(); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1357 | |
| 1358 | /* id is in [1, INT_MAX) */ |
| 1359 | if (WARN_ON_ONCE(!id)) |
| 1360 | return -ENOSPC; |
| 1361 | |
| 1362 | return id > 0 ? 0 : id; |
| 1363 | } |
| 1364 | |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1365 | void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock) |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1366 | { |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1367 | /* cBPF to eBPF migrations are currently not in the idr store. |
| 1368 | * Offloaded programs are removed from the store when their device |
| 1369 | * disappears - even if someone grabs an fd to them they are unusable, |
| 1370 | * simply waiting for refcnt to drop to be freed. |
| 1371 | */ |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1372 | if (!prog->aux->id) |
| 1373 | return; |
| 1374 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1375 | if (do_idr_lock) |
| 1376 | spin_lock_bh(&prog_idr_lock); |
| 1377 | else |
| 1378 | __acquire(&prog_idr_lock); |
| 1379 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1380 | idr_remove(&prog_idr, prog->aux->id); |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1381 | prog->aux->id = 0; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1382 | |
| 1383 | if (do_idr_lock) |
| 1384 | spin_unlock_bh(&prog_idr_lock); |
| 1385 | else |
| 1386 | __release(&prog_idr_lock); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1389 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 1390 | { |
| 1391 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); |
| 1392 | |
Daniel Borkmann | 3b4d9eb | 2019-10-22 23:30:38 +0200 | [diff] [blame] | 1393 | kvfree(aux->func_info); |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 1394 | kfree(aux->func_info_aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1395 | bpf_prog_uncharge_memlock(aux->prog); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1396 | security_bpf_prog_free(aux); |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 1397 | bpf_prog_free(aux->prog); |
| 1398 | } |
| 1399 | |
Daniel Borkmann | cd7455f | 2019-10-22 15:57:23 +0200 | [diff] [blame] | 1400 | static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred) |
| 1401 | { |
| 1402 | bpf_prog_kallsyms_del_all(prog); |
| 1403 | btf_put(prog->aux->btf); |
Daniel Borkmann | cd7455f | 2019-10-22 15:57:23 +0200 | [diff] [blame] | 1404 | bpf_prog_free_linfo(prog); |
| 1405 | |
| 1406 | if (deferred) |
| 1407 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
| 1408 | else |
| 1409 | __bpf_prog_put_rcu(&prog->aux->rcu); |
| 1410 | } |
| 1411 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1412 | static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1413 | { |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1414 | if (atomic64_dec_and_test(&prog->aux->refcnt)) { |
Song Liu | 6ee52e2 | 2019-01-17 08:15:15 -0800 | [diff] [blame] | 1415 | perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0); |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 1416 | /* bpf_prog_free_id() must be called first */ |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1417 | bpf_prog_free_id(prog, do_idr_lock); |
Daniel Borkmann | cd7455f | 2019-10-22 15:57:23 +0200 | [diff] [blame] | 1418 | __bpf_prog_put_noref(prog, true); |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1419 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1420 | } |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1421 | |
| 1422 | void bpf_prog_put(struct bpf_prog *prog) |
| 1423 | { |
| 1424 | __bpf_prog_put(prog, true); |
| 1425 | } |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 1426 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1427 | |
| 1428 | static int bpf_prog_release(struct inode *inode, struct file *filp) |
| 1429 | { |
| 1430 | struct bpf_prog *prog = filp->private_data; |
| 1431 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1432 | bpf_prog_put(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1433 | return 0; |
| 1434 | } |
| 1435 | |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1436 | static void bpf_prog_get_stats(const struct bpf_prog *prog, |
| 1437 | struct bpf_prog_stats *stats) |
| 1438 | { |
| 1439 | u64 nsecs = 0, cnt = 0; |
| 1440 | int cpu; |
| 1441 | |
| 1442 | for_each_possible_cpu(cpu) { |
| 1443 | const struct bpf_prog_stats *st; |
| 1444 | unsigned int start; |
| 1445 | u64 tnsecs, tcnt; |
| 1446 | |
| 1447 | st = per_cpu_ptr(prog->aux->stats, cpu); |
| 1448 | do { |
| 1449 | start = u64_stats_fetch_begin_irq(&st->syncp); |
| 1450 | tnsecs = st->nsecs; |
| 1451 | tcnt = st->cnt; |
| 1452 | } while (u64_stats_fetch_retry_irq(&st->syncp, start)); |
| 1453 | nsecs += tnsecs; |
| 1454 | cnt += tcnt; |
| 1455 | } |
| 1456 | stats->nsecs = nsecs; |
| 1457 | stats->cnt = cnt; |
| 1458 | } |
| 1459 | |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1460 | #ifdef CONFIG_PROC_FS |
| 1461 | static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) |
| 1462 | { |
| 1463 | const struct bpf_prog *prog = filp->private_data; |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1464 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1465 | struct bpf_prog_stats stats; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1466 | |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1467 | bpf_prog_get_stats(prog, &stats); |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1468 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1469 | seq_printf(m, |
| 1470 | "prog_type:\t%u\n" |
| 1471 | "prog_jited:\t%u\n" |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1472 | "prog_tag:\t%s\n" |
Daniel Borkmann | 4316b40 | 2018-06-02 23:06:34 +0200 | [diff] [blame] | 1473 | "memlock:\t%llu\n" |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1474 | "prog_id:\t%u\n" |
| 1475 | "run_time_ns:\t%llu\n" |
| 1476 | "run_cnt:\t%llu\n", |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1477 | prog->type, |
| 1478 | prog->jited, |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1479 | prog_tag, |
Daniel Borkmann | 4316b40 | 2018-06-02 23:06:34 +0200 | [diff] [blame] | 1480 | prog->pages * 1ULL << PAGE_SHIFT, |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1481 | prog->aux->id, |
| 1482 | stats.nsecs, |
| 1483 | stats.cnt); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1484 | } |
| 1485 | #endif |
| 1486 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1487 | const struct file_operations bpf_prog_fops = { |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1488 | #ifdef CONFIG_PROC_FS |
| 1489 | .show_fdinfo = bpf_prog_show_fdinfo, |
| 1490 | #endif |
| 1491 | .release = bpf_prog_release, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1492 | .read = bpf_dummy_read, |
| 1493 | .write = bpf_dummy_write, |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1494 | }; |
| 1495 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1496 | int bpf_prog_new_fd(struct bpf_prog *prog) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1497 | { |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1498 | int ret; |
| 1499 | |
| 1500 | ret = security_bpf_prog(prog); |
| 1501 | if (ret < 0) |
| 1502 | return ret; |
| 1503 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1504 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
| 1505 | O_RDWR | O_CLOEXEC); |
| 1506 | } |
| 1507 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1508 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1509 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1510 | if (!f.file) |
| 1511 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1512 | if (f.file->f_op != &bpf_prog_fops) { |
| 1513 | fdput(f); |
| 1514 | return ERR_PTR(-EINVAL); |
| 1515 | } |
| 1516 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1517 | return f.file->private_data; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1520 | void bpf_prog_add(struct bpf_prog *prog, int i) |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1521 | { |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1522 | atomic64_add(i, &prog->aux->refcnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1523 | } |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1524 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
| 1525 | |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 1526 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
| 1527 | { |
| 1528 | /* Only to be used for undoing previous bpf_prog_add() in some |
| 1529 | * error path. We still know that another entity in our call |
| 1530 | * path holds a reference to the program, thus atomic_sub() can |
| 1531 | * be safely used in such cases! |
| 1532 | */ |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1533 | WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0); |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 1534 | } |
| 1535 | EXPORT_SYMBOL_GPL(bpf_prog_sub); |
| 1536 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1537 | void bpf_prog_inc(struct bpf_prog *prog) |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1538 | { |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1539 | atomic64_inc(&prog->aux->refcnt); |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1540 | } |
Daniel Borkmann | 97bc402 | 2016-11-19 01:45:00 +0100 | [diff] [blame] | 1541 | EXPORT_SYMBOL_GPL(bpf_prog_inc); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1542 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1543 | /* prog_idr_lock should have been held */ |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1544 | struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1545 | { |
| 1546 | int refold; |
| 1547 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1548 | refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1549 | |
| 1550 | if (!refold) |
| 1551 | return ERR_PTR(-ENOENT); |
| 1552 | |
| 1553 | return prog; |
| 1554 | } |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1555 | EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1556 | |
Al Viro | 040ee69 | 2017-12-02 20:20:38 -0500 | [diff] [blame] | 1557 | bool bpf_prog_get_ok(struct bpf_prog *prog, |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1558 | enum bpf_prog_type *attach_type, bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1559 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1560 | /* not an attachment, just a refcount inc, always allow */ |
| 1561 | if (!attach_type) |
| 1562 | return true; |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1563 | |
| 1564 | if (prog->type != *attach_type) |
| 1565 | return false; |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1566 | if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1567 | return false; |
| 1568 | |
| 1569 | return true; |
| 1570 | } |
| 1571 | |
| 1572 | static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1573 | bool attach_drv) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1574 | { |
| 1575 | struct fd f = fdget(ufd); |
| 1576 | struct bpf_prog *prog; |
| 1577 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1578 | prog = ____bpf_prog_get(f); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1579 | if (IS_ERR(prog)) |
| 1580 | return prog; |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1581 | if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1582 | prog = ERR_PTR(-EINVAL); |
| 1583 | goto out; |
| 1584 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1585 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1586 | bpf_prog_inc(prog); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1587 | out: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1588 | fdput(f); |
| 1589 | return prog; |
| 1590 | } |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1591 | |
| 1592 | struct bpf_prog *bpf_prog_get(u32 ufd) |
| 1593 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1594 | return __bpf_prog_get(ufd, NULL, false); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1595 | } |
| 1596 | |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1597 | struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1598 | bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1599 | { |
Alexei Starovoitov | 4d220ed | 2018-04-28 19:56:37 -0700 | [diff] [blame] | 1600 | return __bpf_prog_get(ufd, &type, attach_drv); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1601 | } |
Jakub Kicinski | 6c8dfe2 | 2017-11-03 13:56:21 -0700 | [diff] [blame] | 1602 | EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1603 | |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1604 | /* Initially all BPF programs could be loaded w/o specifying |
| 1605 | * expected_attach_type. Later for some of them specifying expected_attach_type |
| 1606 | * at load time became required so that program could be validated properly. |
| 1607 | * Programs of types that are allowed to be loaded both w/ and w/o (for |
| 1608 | * backward compatibility) expected_attach_type, should have the default attach |
| 1609 | * type assigned to expected_attach_type for the latter case, so that it can be |
| 1610 | * validated later at attach time. |
| 1611 | * |
| 1612 | * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if |
| 1613 | * prog type requires it but has some attach types that have to be backward |
| 1614 | * compatible. |
| 1615 | */ |
| 1616 | static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) |
| 1617 | { |
| 1618 | switch (attr->prog_type) { |
| 1619 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 1620 | /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't |
| 1621 | * exist so checking for non-zero is the way to go here. |
| 1622 | */ |
| 1623 | if (!attr->expected_attach_type) |
| 1624 | attr->expected_attach_type = |
| 1625 | BPF_CGROUP_INET_SOCK_CREATE; |
| 1626 | break; |
| 1627 | } |
| 1628 | } |
| 1629 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1630 | static int |
Alexei Starovoitov | ccfe29e | 2019-10-15 20:24:58 -0700 | [diff] [blame] | 1631 | bpf_prog_load_check_attach(enum bpf_prog_type prog_type, |
| 1632 | enum bpf_attach_type expected_attach_type, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 1633 | u32 btf_id, u32 prog_fd) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1634 | { |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1635 | switch (prog_type) { |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 1636 | case BPF_PROG_TYPE_TRACING: |
Alexei Starovoitov | c108e3c | 2019-10-17 23:09:33 -0700 | [diff] [blame] | 1637 | if (btf_id > BTF_MAX_TYPE) |
| 1638 | return -EINVAL; |
| 1639 | break; |
| 1640 | default: |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 1641 | if (btf_id || prog_fd) |
Alexei Starovoitov | c108e3c | 2019-10-17 23:09:33 -0700 | [diff] [blame] | 1642 | return -EINVAL; |
| 1643 | break; |
| 1644 | } |
| 1645 | |
| 1646 | switch (prog_type) { |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1647 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 1648 | switch (expected_attach_type) { |
| 1649 | case BPF_CGROUP_INET_SOCK_CREATE: |
| 1650 | case BPF_CGROUP_INET4_POST_BIND: |
| 1651 | case BPF_CGROUP_INET6_POST_BIND: |
| 1652 | return 0; |
| 1653 | default: |
| 1654 | return -EINVAL; |
| 1655 | } |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1656 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 1657 | switch (expected_attach_type) { |
| 1658 | case BPF_CGROUP_INET4_BIND: |
| 1659 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 1660 | case BPF_CGROUP_INET4_CONNECT: |
| 1661 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 1662 | case BPF_CGROUP_UDP4_SENDMSG: |
| 1663 | case BPF_CGROUP_UDP6_SENDMSG: |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 1664 | case BPF_CGROUP_UDP4_RECVMSG: |
| 1665 | case BPF_CGROUP_UDP6_RECVMSG: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1666 | return 0; |
| 1667 | default: |
| 1668 | return -EINVAL; |
| 1669 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 1670 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 1671 | switch (expected_attach_type) { |
| 1672 | case BPF_CGROUP_INET_INGRESS: |
| 1673 | case BPF_CGROUP_INET_EGRESS: |
| 1674 | return 0; |
| 1675 | default: |
| 1676 | return -EINVAL; |
| 1677 | } |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1678 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 1679 | switch (expected_attach_type) { |
| 1680 | case BPF_CGROUP_SETSOCKOPT: |
| 1681 | case BPF_CGROUP_GETSOCKOPT: |
| 1682 | return 0; |
| 1683 | default: |
| 1684 | return -EINVAL; |
| 1685 | } |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1686 | default: |
| 1687 | return 0; |
| 1688 | } |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1689 | } |
| 1690 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1691 | /* last field in 'union bpf_attr' used by this command */ |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 1692 | #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1693 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 1694 | static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1695 | { |
| 1696 | enum bpf_prog_type type = attr->prog_type; |
| 1697 | struct bpf_prog *prog; |
| 1698 | int err; |
| 1699 | char license[128]; |
| 1700 | bool is_gpl; |
| 1701 | |
| 1702 | if (CHECK_ATTR(BPF_PROG_LOAD)) |
| 1703 | return -EINVAL; |
| 1704 | |
Jiong Wang | c240eff | 2019-05-24 23:25:16 +0100 | [diff] [blame] | 1705 | if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | |
| 1706 | BPF_F_ANY_ALIGNMENT | |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 1707 | BPF_F_TEST_STATE_FREQ | |
Jiong Wang | c240eff | 2019-05-24 23:25:16 +0100 | [diff] [blame] | 1708 | BPF_F_TEST_RND_HI32)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1709 | return -EINVAL; |
| 1710 | |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 1711 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && |
| 1712 | (attr->prog_flags & BPF_F_ANY_ALIGNMENT) && |
| 1713 | !capable(CAP_SYS_ADMIN)) |
| 1714 | return -EPERM; |
| 1715 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1716 | /* copy eBPF program license from user space */ |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1717 | if (strncpy_from_user(license, u64_to_user_ptr(attr->license), |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1718 | sizeof(license) - 1) < 0) |
| 1719 | return -EFAULT; |
| 1720 | license[sizeof(license) - 1] = 0; |
| 1721 | |
| 1722 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
| 1723 | is_gpl = license_is_gpl_compatible(license); |
| 1724 | |
Alexei Starovoitov | c04c0d2 | 2019-04-01 21:27:45 -0700 | [diff] [blame] | 1725 | if (attr->insn_cnt == 0 || |
| 1726 | attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) |
Daniel Borkmann | ef0915c | 2016-12-07 01:15:44 +0100 | [diff] [blame] | 1727 | return -E2BIG; |
Chenbo Feng | 80b7d81 | 2017-05-31 18:16:00 -0700 | [diff] [blame] | 1728 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && |
| 1729 | type != BPF_PROG_TYPE_CGROUP_SKB && |
| 1730 | !capable(CAP_SYS_ADMIN)) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1731 | return -EPERM; |
| 1732 | |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1733 | bpf_prog_load_fixup_attach_type(attr); |
Alexei Starovoitov | ccfe29e | 2019-10-15 20:24:58 -0700 | [diff] [blame] | 1734 | if (bpf_prog_load_check_attach(type, attr->expected_attach_type, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 1735 | attr->attach_btf_id, |
| 1736 | attr->attach_prog_fd)) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1737 | return -EINVAL; |
| 1738 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1739 | /* plain bpf_prog allocation */ |
| 1740 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); |
| 1741 | if (!prog) |
| 1742 | return -ENOMEM; |
| 1743 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1744 | prog->expected_attach_type = attr->expected_attach_type; |
Alexei Starovoitov | ccfe29e | 2019-10-15 20:24:58 -0700 | [diff] [blame] | 1745 | prog->aux->attach_btf_id = attr->attach_btf_id; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 1746 | if (attr->attach_prog_fd) { |
| 1747 | struct bpf_prog *tgt_prog; |
| 1748 | |
| 1749 | tgt_prog = bpf_prog_get(attr->attach_prog_fd); |
| 1750 | if (IS_ERR(tgt_prog)) { |
| 1751 | err = PTR_ERR(tgt_prog); |
| 1752 | goto free_prog_nouncharge; |
| 1753 | } |
| 1754 | prog->aux->linked_prog = tgt_prog; |
| 1755 | } |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1756 | |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 1757 | prog->aux->offload_requested = !!attr->prog_ifindex; |
| 1758 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1759 | err = security_bpf_prog_alloc(prog->aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1760 | if (err) |
| 1761 | goto free_prog_nouncharge; |
| 1762 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1763 | err = bpf_prog_charge_memlock(prog); |
| 1764 | if (err) |
| 1765 | goto free_prog_sec; |
| 1766 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1767 | prog->len = attr->insn_cnt; |
| 1768 | |
| 1769 | err = -EFAULT; |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1770 | if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 1771 | bpf_prog_insn_size(prog)) != 0) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1772 | goto free_prog; |
| 1773 | |
| 1774 | prog->orig_prog = NULL; |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1775 | prog->jited = 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1776 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1777 | atomic64_set(&prog->aux->refcnt, 1); |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1778 | prog->gpl_compatible = is_gpl ? 1 : 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1779 | |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 1780 | if (bpf_prog_is_dev_bound(prog->aux)) { |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1781 | err = bpf_prog_offload_init(prog, attr); |
| 1782 | if (err) |
| 1783 | goto free_prog; |
| 1784 | } |
| 1785 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1786 | /* find program type: socket_filter vs tracing_filter */ |
| 1787 | err = find_prog_type(type, prog); |
| 1788 | if (err < 0) |
| 1789 | goto free_prog; |
| 1790 | |
Jason A. Donenfeld | 9285ec4 | 2019-06-21 22:32:48 +0200 | [diff] [blame] | 1791 | prog->aux->load_time = ktime_get_boottime_ns(); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 1792 | err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name); |
| 1793 | if (err) |
| 1794 | goto free_prog; |
| 1795 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1796 | /* run eBPF verifier */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 1797 | err = bpf_check(&prog, attr, uattr); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1798 | if (err < 0) |
| 1799 | goto free_used_maps; |
| 1800 | |
Daniel Borkmann | 9facc33 | 2018-06-15 02:30:48 +0200 | [diff] [blame] | 1801 | prog = bpf_prog_select_runtime(prog, &err); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1802 | if (err < 0) |
| 1803 | goto free_used_maps; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1804 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1805 | err = bpf_prog_alloc_id(prog); |
| 1806 | if (err) |
| 1807 | goto free_used_maps; |
| 1808 | |
Daniel Borkmann | c751798 | 2019-08-23 22:14:23 +0200 | [diff] [blame] | 1809 | /* Upon success of bpf_prog_alloc_id(), the BPF prog is |
| 1810 | * effectively publicly exposed. However, retrieving via |
| 1811 | * bpf_prog_get_fd_by_id() will take another reference, |
| 1812 | * therefore it cannot be gone underneath us. |
| 1813 | * |
| 1814 | * Only for the time /after/ successful bpf_prog_new_fd() |
| 1815 | * and before returning to userspace, we might just hold |
| 1816 | * one reference and any parallel close on that fd could |
| 1817 | * rip everything out. Hence, below notifications must |
| 1818 | * happen before bpf_prog_new_fd(). |
| 1819 | * |
| 1820 | * Also, any failure handling from this point onwards must |
| 1821 | * be using bpf_prog_put() given the program is exposed. |
| 1822 | */ |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 1823 | bpf_prog_kallsyms_add(prog); |
Song Liu | 6ee52e2 | 2019-01-17 08:15:15 -0800 | [diff] [blame] | 1824 | perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); |
Daniel Borkmann | c751798 | 2019-08-23 22:14:23 +0200 | [diff] [blame] | 1825 | |
| 1826 | err = bpf_prog_new_fd(prog); |
| 1827 | if (err < 0) |
| 1828 | bpf_prog_put(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1829 | return err; |
| 1830 | |
| 1831 | free_used_maps: |
Daniel Borkmann | cd7455f | 2019-10-22 15:57:23 +0200 | [diff] [blame] | 1832 | /* In case we have subprogs, we need to wait for a grace |
| 1833 | * period before we can tear down JIT memory since symbols |
| 1834 | * are already exposed under kallsyms. |
| 1835 | */ |
| 1836 | __bpf_prog_put_noref(prog, prog->aux->func_cnt); |
| 1837 | return err; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1838 | free_prog: |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1839 | bpf_prog_uncharge_memlock(prog); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1840 | free_prog_sec: |
| 1841 | security_bpf_prog_free(prog->aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1842 | free_prog_nouncharge: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1843 | bpf_prog_free(prog); |
| 1844 | return err; |
| 1845 | } |
| 1846 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1847 | #define BPF_OBJ_LAST_FIELD file_flags |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1848 | |
| 1849 | static int bpf_obj_pin(const union bpf_attr *attr) |
| 1850 | { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1851 | if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1852 | return -EINVAL; |
| 1853 | |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1854 | return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname)); |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | static int bpf_obj_get(const union bpf_attr *attr) |
| 1858 | { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1859 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || |
| 1860 | attr->file_flags & ~BPF_OBJ_FLAG_MASK) |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1861 | return -EINVAL; |
| 1862 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1863 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), |
| 1864 | attr->file_flags); |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1865 | } |
| 1866 | |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 1867 | static int bpf_tracing_prog_release(struct inode *inode, struct file *filp) |
| 1868 | { |
| 1869 | struct bpf_prog *prog = filp->private_data; |
| 1870 | |
| 1871 | WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog)); |
| 1872 | bpf_prog_put(prog); |
| 1873 | return 0; |
| 1874 | } |
| 1875 | |
| 1876 | static const struct file_operations bpf_tracing_prog_fops = { |
| 1877 | .release = bpf_tracing_prog_release, |
| 1878 | .read = bpf_dummy_read, |
| 1879 | .write = bpf_dummy_write, |
| 1880 | }; |
| 1881 | |
| 1882 | static int bpf_tracing_prog_attach(struct bpf_prog *prog) |
| 1883 | { |
| 1884 | int tr_fd, err; |
| 1885 | |
| 1886 | if (prog->expected_attach_type != BPF_TRACE_FENTRY && |
| 1887 | prog->expected_attach_type != BPF_TRACE_FEXIT) { |
| 1888 | err = -EINVAL; |
| 1889 | goto out_put_prog; |
| 1890 | } |
| 1891 | |
| 1892 | err = bpf_trampoline_link_prog(prog); |
| 1893 | if (err) |
| 1894 | goto out_put_prog; |
| 1895 | |
| 1896 | tr_fd = anon_inode_getfd("bpf-tracing-prog", &bpf_tracing_prog_fops, |
| 1897 | prog, O_CLOEXEC); |
| 1898 | if (tr_fd < 0) { |
| 1899 | WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog)); |
| 1900 | err = tr_fd; |
| 1901 | goto out_put_prog; |
| 1902 | } |
| 1903 | return tr_fd; |
| 1904 | |
| 1905 | out_put_prog: |
| 1906 | bpf_prog_put(prog); |
| 1907 | return err; |
| 1908 | } |
| 1909 | |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1910 | struct bpf_raw_tracepoint { |
| 1911 | struct bpf_raw_event_map *btp; |
| 1912 | struct bpf_prog *prog; |
| 1913 | }; |
| 1914 | |
| 1915 | static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp) |
| 1916 | { |
| 1917 | struct bpf_raw_tracepoint *raw_tp = filp->private_data; |
| 1918 | |
| 1919 | if (raw_tp->prog) { |
| 1920 | bpf_probe_unregister(raw_tp->btp, raw_tp->prog); |
| 1921 | bpf_prog_put(raw_tp->prog); |
| 1922 | } |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 1923 | bpf_put_raw_tracepoint(raw_tp->btp); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1924 | kfree(raw_tp); |
| 1925 | return 0; |
| 1926 | } |
| 1927 | |
| 1928 | static const struct file_operations bpf_raw_tp_fops = { |
| 1929 | .release = bpf_raw_tracepoint_release, |
| 1930 | .read = bpf_dummy_read, |
| 1931 | .write = bpf_dummy_write, |
| 1932 | }; |
| 1933 | |
| 1934 | #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd |
| 1935 | |
| 1936 | static int bpf_raw_tracepoint_open(const union bpf_attr *attr) |
| 1937 | { |
| 1938 | struct bpf_raw_tracepoint *raw_tp; |
| 1939 | struct bpf_raw_event_map *btp; |
| 1940 | struct bpf_prog *prog; |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 1941 | const char *tp_name; |
| 1942 | char buf[128]; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1943 | int tp_fd, err; |
| 1944 | |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 1945 | if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN)) |
| 1946 | return -EINVAL; |
| 1947 | |
| 1948 | prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); |
| 1949 | if (IS_ERR(prog)) |
| 1950 | return PTR_ERR(prog); |
| 1951 | |
| 1952 | if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT && |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 1953 | prog->type != BPF_PROG_TYPE_TRACING && |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 1954 | prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) { |
| 1955 | err = -EINVAL; |
| 1956 | goto out_put_prog; |
| 1957 | } |
| 1958 | |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 1959 | if (prog->type == BPF_PROG_TYPE_TRACING) { |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 1960 | if (attr->raw_tracepoint.name) { |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 1961 | /* The attach point for this category of programs |
| 1962 | * should be specified via btf_id during program load. |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 1963 | */ |
| 1964 | err = -EINVAL; |
| 1965 | goto out_put_prog; |
| 1966 | } |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 1967 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) |
| 1968 | tp_name = prog->aux->attach_func_name; |
| 1969 | else |
| 1970 | return bpf_tracing_prog_attach(prog); |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 1971 | } else { |
| 1972 | if (strncpy_from_user(buf, |
| 1973 | u64_to_user_ptr(attr->raw_tracepoint.name), |
| 1974 | sizeof(buf) - 1) < 0) { |
| 1975 | err = -EFAULT; |
| 1976 | goto out_put_prog; |
| 1977 | } |
| 1978 | buf[sizeof(buf) - 1] = 0; |
| 1979 | tp_name = buf; |
| 1980 | } |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1981 | |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 1982 | btp = bpf_get_raw_tracepoint(tp_name); |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 1983 | if (!btp) { |
| 1984 | err = -ENOENT; |
| 1985 | goto out_put_prog; |
| 1986 | } |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1987 | |
| 1988 | raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER); |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 1989 | if (!raw_tp) { |
| 1990 | err = -ENOMEM; |
| 1991 | goto out_put_btp; |
| 1992 | } |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1993 | raw_tp->btp = btp; |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 1994 | raw_tp->prog = prog; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1995 | |
| 1996 | err = bpf_probe_register(raw_tp->btp, prog); |
| 1997 | if (err) |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 1998 | goto out_free_tp; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1999 | |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2000 | tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp, |
| 2001 | O_CLOEXEC); |
| 2002 | if (tp_fd < 0) { |
| 2003 | bpf_probe_unregister(raw_tp->btp, prog); |
| 2004 | err = tp_fd; |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 2005 | goto out_free_tp; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2006 | } |
| 2007 | return tp_fd; |
| 2008 | |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2009 | out_free_tp: |
| 2010 | kfree(raw_tp); |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 2011 | out_put_btp: |
| 2012 | bpf_put_raw_tracepoint(btp); |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 2013 | out_put_prog: |
| 2014 | bpf_prog_put(prog); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2015 | return err; |
| 2016 | } |
| 2017 | |
Anders Roxell | 3349158 | 2018-04-03 14:09:47 +0200 | [diff] [blame] | 2018 | static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, |
| 2019 | enum bpf_attach_type attach_type) |
| 2020 | { |
| 2021 | switch (prog->type) { |
| 2022 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 2023 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2024 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
Anders Roxell | 3349158 | 2018-04-03 14:09:47 +0200 | [diff] [blame] | 2025 | return attach_type == prog->expected_attach_type ? 0 : -EINVAL; |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 2026 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 2027 | return prog->enforce_expected_attach_type && |
| 2028 | prog->expected_attach_type != attach_type ? |
| 2029 | -EINVAL : 0; |
Anders Roxell | 3349158 | 2018-04-03 14:09:47 +0200 | [diff] [blame] | 2030 | default: |
| 2031 | return 0; |
| 2032 | } |
| 2033 | } |
| 2034 | |
John Fastabend | 464bc0f | 2017-08-28 07:10:04 -0700 | [diff] [blame] | 2035 | #define BPF_PROG_ATTACH_LAST_FIELD attach_flags |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2036 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2037 | #define BPF_F_ATTACH_MASK \ |
| 2038 | (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI) |
| 2039 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2040 | static int bpf_prog_attach(const union bpf_attr *attr) |
| 2041 | { |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 2042 | enum bpf_prog_type ptype; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2043 | struct bpf_prog *prog; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 2044 | int ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2045 | |
| 2046 | if (!capable(CAP_NET_ADMIN)) |
| 2047 | return -EPERM; |
| 2048 | |
| 2049 | if (CHECK_ATTR(BPF_PROG_ATTACH)) |
| 2050 | return -EINVAL; |
| 2051 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2052 | if (attr->attach_flags & ~BPF_F_ATTACH_MASK) |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 2053 | return -EINVAL; |
| 2054 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2055 | switch (attr->attach_type) { |
| 2056 | case BPF_CGROUP_INET_INGRESS: |
| 2057 | case BPF_CGROUP_INET_EGRESS: |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 2058 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2059 | break; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 2060 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 2061 | case BPF_CGROUP_INET4_POST_BIND: |
| 2062 | case BPF_CGROUP_INET6_POST_BIND: |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 2063 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
| 2064 | break; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2065 | case BPF_CGROUP_INET4_BIND: |
| 2066 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 2067 | case BPF_CGROUP_INET4_CONNECT: |
| 2068 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 2069 | case BPF_CGROUP_UDP4_SENDMSG: |
| 2070 | case BPF_CGROUP_UDP6_SENDMSG: |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 2071 | case BPF_CGROUP_UDP4_RECVMSG: |
| 2072 | case BPF_CGROUP_UDP6_RECVMSG: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2073 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
| 2074 | break; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 2075 | case BPF_CGROUP_SOCK_OPS: |
| 2076 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
| 2077 | break; |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 2078 | case BPF_CGROUP_DEVICE: |
| 2079 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; |
| 2080 | break; |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2081 | case BPF_SK_MSG_VERDICT: |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2082 | ptype = BPF_PROG_TYPE_SK_MSG; |
| 2083 | break; |
John Fastabend | 464bc0f | 2017-08-28 07:10:04 -0700 | [diff] [blame] | 2084 | case BPF_SK_SKB_STREAM_PARSER: |
| 2085 | case BPF_SK_SKB_STREAM_VERDICT: |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2086 | ptype = BPF_PROG_TYPE_SK_SKB; |
| 2087 | break; |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 2088 | case BPF_LIRC_MODE2: |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2089 | ptype = BPF_PROG_TYPE_LIRC_MODE2; |
| 2090 | break; |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2091 | case BPF_FLOW_DISSECTOR: |
| 2092 | ptype = BPF_PROG_TYPE_FLOW_DISSECTOR; |
| 2093 | break; |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 2094 | case BPF_CGROUP_SYSCTL: |
| 2095 | ptype = BPF_PROG_TYPE_CGROUP_SYSCTL; |
| 2096 | break; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2097 | case BPF_CGROUP_GETSOCKOPT: |
| 2098 | case BPF_CGROUP_SETSOCKOPT: |
| 2099 | ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT; |
| 2100 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2101 | default: |
| 2102 | return -EINVAL; |
| 2103 | } |
| 2104 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 2105 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
| 2106 | if (IS_ERR(prog)) |
| 2107 | return PTR_ERR(prog); |
| 2108 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2109 | if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { |
| 2110 | bpf_prog_put(prog); |
| 2111 | return -EINVAL; |
| 2112 | } |
| 2113 | |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2114 | switch (ptype) { |
| 2115 | case BPF_PROG_TYPE_SK_SKB: |
| 2116 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 2117 | ret = sock_map_get_from_fd(attr, prog); |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2118 | break; |
| 2119 | case BPF_PROG_TYPE_LIRC_MODE2: |
| 2120 | ret = lirc_prog_attach(attr, prog); |
| 2121 | break; |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2122 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
| 2123 | ret = skb_flow_dissector_bpf_prog_attach(attr, prog); |
| 2124 | break; |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2125 | default: |
| 2126 | ret = cgroup_bpf_prog_attach(attr, ptype, prog); |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 2127 | } |
| 2128 | |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 2129 | if (ret) |
| 2130 | bpf_prog_put(prog); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 2131 | return ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | #define BPF_PROG_DETACH_LAST_FIELD attach_type |
| 2135 | |
| 2136 | static int bpf_prog_detach(const union bpf_attr *attr) |
| 2137 | { |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2138 | enum bpf_prog_type ptype; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2139 | |
| 2140 | if (!capable(CAP_NET_ADMIN)) |
| 2141 | return -EPERM; |
| 2142 | |
| 2143 | if (CHECK_ATTR(BPF_PROG_DETACH)) |
| 2144 | return -EINVAL; |
| 2145 | |
| 2146 | switch (attr->attach_type) { |
| 2147 | case BPF_CGROUP_INET_INGRESS: |
| 2148 | case BPF_CGROUP_INET_EGRESS: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2149 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
| 2150 | break; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 2151 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 2152 | case BPF_CGROUP_INET4_POST_BIND: |
| 2153 | case BPF_CGROUP_INET6_POST_BIND: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2154 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
| 2155 | break; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2156 | case BPF_CGROUP_INET4_BIND: |
| 2157 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 2158 | case BPF_CGROUP_INET4_CONNECT: |
| 2159 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 2160 | case BPF_CGROUP_UDP4_SENDMSG: |
| 2161 | case BPF_CGROUP_UDP6_SENDMSG: |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 2162 | case BPF_CGROUP_UDP4_RECVMSG: |
| 2163 | case BPF_CGROUP_UDP6_RECVMSG: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2164 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
| 2165 | break; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 2166 | case BPF_CGROUP_SOCK_OPS: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2167 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2168 | break; |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 2169 | case BPF_CGROUP_DEVICE: |
| 2170 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; |
| 2171 | break; |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2172 | case BPF_SK_MSG_VERDICT: |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 2173 | return sock_map_get_from_fd(attr, NULL); |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 2174 | case BPF_SK_SKB_STREAM_PARSER: |
| 2175 | case BPF_SK_SKB_STREAM_VERDICT: |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 2176 | return sock_map_get_from_fd(attr, NULL); |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 2177 | case BPF_LIRC_MODE2: |
| 2178 | return lirc_prog_detach(attr); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2179 | case BPF_FLOW_DISSECTOR: |
| 2180 | return skb_flow_dissector_bpf_prog_detach(attr); |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 2181 | case BPF_CGROUP_SYSCTL: |
| 2182 | ptype = BPF_PROG_TYPE_CGROUP_SYSCTL; |
| 2183 | break; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2184 | case BPF_CGROUP_GETSOCKOPT: |
| 2185 | case BPF_CGROUP_SETSOCKOPT: |
| 2186 | ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT; |
| 2187 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2188 | default: |
| 2189 | return -EINVAL; |
| 2190 | } |
| 2191 | |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2192 | return cgroup_bpf_prog_detach(attr, ptype); |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2193 | } |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 2194 | |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2195 | #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt |
| 2196 | |
| 2197 | static int bpf_prog_query(const union bpf_attr *attr, |
| 2198 | union bpf_attr __user *uattr) |
| 2199 | { |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2200 | if (!capable(CAP_NET_ADMIN)) |
| 2201 | return -EPERM; |
| 2202 | if (CHECK_ATTR(BPF_PROG_QUERY)) |
| 2203 | return -EINVAL; |
| 2204 | if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) |
| 2205 | return -EINVAL; |
| 2206 | |
| 2207 | switch (attr->query.attach_type) { |
| 2208 | case BPF_CGROUP_INET_INGRESS: |
| 2209 | case BPF_CGROUP_INET_EGRESS: |
| 2210 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2211 | case BPF_CGROUP_INET4_BIND: |
| 2212 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 2213 | case BPF_CGROUP_INET4_POST_BIND: |
| 2214 | case BPF_CGROUP_INET6_POST_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 2215 | case BPF_CGROUP_INET4_CONNECT: |
| 2216 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 2217 | case BPF_CGROUP_UDP4_SENDMSG: |
| 2218 | case BPF_CGROUP_UDP6_SENDMSG: |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 2219 | case BPF_CGROUP_UDP4_RECVMSG: |
| 2220 | case BPF_CGROUP_UDP6_RECVMSG: |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2221 | case BPF_CGROUP_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 2222 | case BPF_CGROUP_DEVICE: |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 2223 | case BPF_CGROUP_SYSCTL: |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2224 | case BPF_CGROUP_GETSOCKOPT: |
| 2225 | case BPF_CGROUP_SETSOCKOPT: |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2226 | break; |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 2227 | case BPF_LIRC_MODE2: |
| 2228 | return lirc_prog_query(attr, uattr); |
Stanislav Fomichev | 118c8e9 | 2019-04-25 14:37:23 -0700 | [diff] [blame] | 2229 | case BPF_FLOW_DISSECTOR: |
| 2230 | return skb_flow_dissector_prog_query(attr, uattr); |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2231 | default: |
| 2232 | return -EINVAL; |
| 2233 | } |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2234 | |
| 2235 | return cgroup_bpf_prog_query(attr, uattr); |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2236 | } |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2237 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 2238 | #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2239 | |
| 2240 | static int bpf_prog_test_run(const union bpf_attr *attr, |
| 2241 | union bpf_attr __user *uattr) |
| 2242 | { |
| 2243 | struct bpf_prog *prog; |
| 2244 | int ret = -ENOTSUPP; |
| 2245 | |
Alexei Starovoitov | 61f3c96 | 2018-01-17 16:52:02 -0800 | [diff] [blame] | 2246 | if (!capable(CAP_SYS_ADMIN)) |
| 2247 | return -EPERM; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2248 | if (CHECK_ATTR(BPF_PROG_TEST_RUN)) |
| 2249 | return -EINVAL; |
| 2250 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 2251 | if ((attr->test.ctx_size_in && !attr->test.ctx_in) || |
| 2252 | (!attr->test.ctx_size_in && attr->test.ctx_in)) |
| 2253 | return -EINVAL; |
| 2254 | |
| 2255 | if ((attr->test.ctx_size_out && !attr->test.ctx_out) || |
| 2256 | (!attr->test.ctx_size_out && attr->test.ctx_out)) |
| 2257 | return -EINVAL; |
| 2258 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2259 | prog = bpf_prog_get(attr->test.prog_fd); |
| 2260 | if (IS_ERR(prog)) |
| 2261 | return PTR_ERR(prog); |
| 2262 | |
| 2263 | if (prog->aux->ops->test_run) |
| 2264 | ret = prog->aux->ops->test_run(prog, attr, uattr); |
| 2265 | |
| 2266 | bpf_prog_put(prog); |
| 2267 | return ret; |
| 2268 | } |
| 2269 | |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 2270 | #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id |
| 2271 | |
| 2272 | static int bpf_obj_get_next_id(const union bpf_attr *attr, |
| 2273 | union bpf_attr __user *uattr, |
| 2274 | struct idr *idr, |
| 2275 | spinlock_t *lock) |
| 2276 | { |
| 2277 | u32 next_id = attr->start_id; |
| 2278 | int err = 0; |
| 2279 | |
| 2280 | if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) |
| 2281 | return -EINVAL; |
| 2282 | |
| 2283 | if (!capable(CAP_SYS_ADMIN)) |
| 2284 | return -EPERM; |
| 2285 | |
| 2286 | next_id++; |
| 2287 | spin_lock_bh(lock); |
| 2288 | if (!idr_get_next(idr, &next_id)) |
| 2289 | err = -ENOENT; |
| 2290 | spin_unlock_bh(lock); |
| 2291 | |
| 2292 | if (!err) |
| 2293 | err = put_user(next_id, &uattr->next_id); |
| 2294 | |
| 2295 | return err; |
| 2296 | } |
| 2297 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 2298 | #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id |
| 2299 | |
| 2300 | static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) |
| 2301 | { |
| 2302 | struct bpf_prog *prog; |
| 2303 | u32 id = attr->prog_id; |
| 2304 | int fd; |
| 2305 | |
| 2306 | if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) |
| 2307 | return -EINVAL; |
| 2308 | |
| 2309 | if (!capable(CAP_SYS_ADMIN)) |
| 2310 | return -EPERM; |
| 2311 | |
| 2312 | spin_lock_bh(&prog_idr_lock); |
| 2313 | prog = idr_find(&prog_idr, id); |
| 2314 | if (prog) |
| 2315 | prog = bpf_prog_inc_not_zero(prog); |
| 2316 | else |
| 2317 | prog = ERR_PTR(-ENOENT); |
| 2318 | spin_unlock_bh(&prog_idr_lock); |
| 2319 | |
| 2320 | if (IS_ERR(prog)) |
| 2321 | return PTR_ERR(prog); |
| 2322 | |
| 2323 | fd = bpf_prog_new_fd(prog); |
| 2324 | if (fd < 0) |
| 2325 | bpf_prog_put(prog); |
| 2326 | |
| 2327 | return fd; |
| 2328 | } |
| 2329 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2330 | #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2331 | |
| 2332 | static int bpf_map_get_fd_by_id(const union bpf_attr *attr) |
| 2333 | { |
| 2334 | struct bpf_map *map; |
| 2335 | u32 id = attr->map_id; |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2336 | int f_flags; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2337 | int fd; |
| 2338 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2339 | if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || |
| 2340 | attr->open_flags & ~BPF_OBJ_FLAG_MASK) |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2341 | return -EINVAL; |
| 2342 | |
| 2343 | if (!capable(CAP_SYS_ADMIN)) |
| 2344 | return -EPERM; |
| 2345 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2346 | f_flags = bpf_get_file_flag(attr->open_flags); |
| 2347 | if (f_flags < 0) |
| 2348 | return f_flags; |
| 2349 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2350 | spin_lock_bh(&map_idr_lock); |
| 2351 | map = idr_find(&map_idr, id); |
| 2352 | if (map) |
Stanislav Fomichev | b0e4701 | 2019-08-14 10:37:48 -0700 | [diff] [blame] | 2353 | map = __bpf_map_inc_not_zero(map, true); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2354 | else |
| 2355 | map = ERR_PTR(-ENOENT); |
| 2356 | spin_unlock_bh(&map_idr_lock); |
| 2357 | |
| 2358 | if (IS_ERR(map)) |
| 2359 | return PTR_ERR(map); |
| 2360 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2361 | fd = bpf_map_new_fd(map, f_flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2362 | if (fd < 0) |
Peng Sun | 781e628 | 2019-02-26 22:15:37 +0800 | [diff] [blame] | 2363 | bpf_map_put_with_uref(map); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2364 | |
| 2365 | return fd; |
| 2366 | } |
| 2367 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2368 | static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2369 | unsigned long addr, u32 *off, |
| 2370 | u32 *type) |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2371 | { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2372 | const struct bpf_map *map; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2373 | int i; |
| 2374 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2375 | for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) { |
| 2376 | map = prog->aux->used_maps[i]; |
| 2377 | if (map == (void *)addr) { |
| 2378 | *type = BPF_PSEUDO_MAP_FD; |
| 2379 | return map; |
| 2380 | } |
| 2381 | if (!map->ops->map_direct_value_meta) |
| 2382 | continue; |
| 2383 | if (!map->ops->map_direct_value_meta(map, addr, off)) { |
| 2384 | *type = BPF_PSEUDO_MAP_VALUE; |
| 2385 | return map; |
| 2386 | } |
| 2387 | } |
| 2388 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2389 | return NULL; |
| 2390 | } |
| 2391 | |
| 2392 | static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog) |
| 2393 | { |
| 2394 | const struct bpf_map *map; |
| 2395 | struct bpf_insn *insns; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2396 | u32 off, type; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2397 | u64 imm; |
| 2398 | int i; |
| 2399 | |
| 2400 | insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), |
| 2401 | GFP_USER); |
| 2402 | if (!insns) |
| 2403 | return insns; |
| 2404 | |
| 2405 | for (i = 0; i < prog->len; i++) { |
| 2406 | if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) { |
| 2407 | insns[i].code = BPF_JMP | BPF_CALL; |
| 2408 | insns[i].imm = BPF_FUNC_tail_call; |
| 2409 | /* fall-through */ |
| 2410 | } |
| 2411 | if (insns[i].code == (BPF_JMP | BPF_CALL) || |
| 2412 | insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) { |
| 2413 | if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) |
| 2414 | insns[i].code = BPF_JMP | BPF_CALL; |
| 2415 | if (!bpf_dump_raw_ok()) |
| 2416 | insns[i].imm = 0; |
| 2417 | continue; |
| 2418 | } |
| 2419 | |
| 2420 | if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW)) |
| 2421 | continue; |
| 2422 | |
| 2423 | imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2424 | map = bpf_map_from_imm(prog, imm, &off, &type); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2425 | if (map) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2426 | insns[i].src_reg = type; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2427 | insns[i].imm = map->id; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2428 | insns[i + 1].imm = off; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2429 | continue; |
| 2430 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | return insns; |
| 2434 | } |
| 2435 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2436 | static int set_info_rec_size(struct bpf_prog_info *info) |
| 2437 | { |
| 2438 | /* |
| 2439 | * Ensure info.*_rec_size is the same as kernel expected size |
| 2440 | * |
| 2441 | * or |
| 2442 | * |
| 2443 | * Only allow zero *_rec_size if both _rec_size and _cnt are |
| 2444 | * zero. In this case, the kernel will set the expected |
| 2445 | * _rec_size back to the info. |
| 2446 | */ |
| 2447 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2448 | if ((info->nr_func_info || info->func_info_rec_size) && |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2449 | info->func_info_rec_size != sizeof(struct bpf_func_info)) |
| 2450 | return -EINVAL; |
| 2451 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2452 | if ((info->nr_line_info || info->line_info_rec_size) && |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2453 | info->line_info_rec_size != sizeof(struct bpf_line_info)) |
| 2454 | return -EINVAL; |
| 2455 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2456 | if ((info->nr_jited_line_info || info->jited_line_info_rec_size) && |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2457 | info->jited_line_info_rec_size != sizeof(__u64)) |
| 2458 | return -EINVAL; |
| 2459 | |
| 2460 | info->func_info_rec_size = sizeof(struct bpf_func_info); |
| 2461 | info->line_info_rec_size = sizeof(struct bpf_line_info); |
| 2462 | info->jited_line_info_rec_size = sizeof(__u64); |
| 2463 | |
| 2464 | return 0; |
| 2465 | } |
| 2466 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2467 | static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, |
| 2468 | const union bpf_attr *attr, |
| 2469 | union bpf_attr __user *uattr) |
| 2470 | { |
| 2471 | struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 2472 | struct bpf_prog_info info = {}; |
| 2473 | u32 info_len = attr->info.info_len; |
Alexei Starovoitov | 5f8f8b9 | 2019-02-25 14:28:40 -0800 | [diff] [blame] | 2474 | struct bpf_prog_stats stats; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2475 | char __user *uinsns; |
| 2476 | u32 ulen; |
| 2477 | int err; |
| 2478 | |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 2479 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2480 | if (err) |
| 2481 | return err; |
| 2482 | info_len = min_t(u32, sizeof(info), info_len); |
| 2483 | |
| 2484 | if (copy_from_user(&info, uinfo, info_len)) |
Daniel Borkmann | 89b0968 | 2017-07-27 21:02:46 +0200 | [diff] [blame] | 2485 | return -EFAULT; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2486 | |
| 2487 | info.type = prog->type; |
| 2488 | info.id = prog->aux->id; |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 2489 | info.load_time = prog->aux->load_time; |
| 2490 | info.created_by_uid = from_kuid_munged(current_user_ns(), |
| 2491 | prog->aux->user->uid); |
Jiri Olsa | b85fab0 | 2018-04-25 19:41:06 +0200 | [diff] [blame] | 2492 | info.gpl_compatible = prog->gpl_compatible; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2493 | |
| 2494 | memcpy(info.tag, prog->tag, sizeof(prog->tag)); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 2495 | memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); |
| 2496 | |
| 2497 | ulen = info.nr_map_ids; |
| 2498 | info.nr_map_ids = prog->aux->used_map_cnt; |
| 2499 | ulen = min_t(u32, info.nr_map_ids, ulen); |
| 2500 | if (ulen) { |
Martin KaFai Lau | 721e08d | 2017-09-29 10:52:17 -0700 | [diff] [blame] | 2501 | u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 2502 | u32 i; |
| 2503 | |
| 2504 | for (i = 0; i < ulen; i++) |
| 2505 | if (put_user(prog->aux->used_maps[i]->id, |
| 2506 | &user_map_ids[i])) |
| 2507 | return -EFAULT; |
| 2508 | } |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2509 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2510 | err = set_info_rec_size(&info); |
| 2511 | if (err) |
| 2512 | return err; |
Martin KaFai Lau | 7337224 | 2018-12-05 17:35:43 -0800 | [diff] [blame] | 2513 | |
Alexei Starovoitov | 5f8f8b9 | 2019-02-25 14:28:40 -0800 | [diff] [blame] | 2514 | bpf_prog_get_stats(prog, &stats); |
| 2515 | info.run_time_ns = stats.nsecs; |
| 2516 | info.run_cnt = stats.cnt; |
| 2517 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2518 | if (!capable(CAP_SYS_ADMIN)) { |
| 2519 | info.jited_prog_len = 0; |
| 2520 | info.xlated_prog_len = 0; |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2521 | info.nr_jited_ksyms = 0; |
Daniel Borkmann | 28c2fae | 2018-11-02 11:35:46 +0100 | [diff] [blame] | 2522 | info.nr_jited_func_lens = 0; |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2523 | info.nr_func_info = 0; |
| 2524 | info.nr_line_info = 0; |
| 2525 | info.nr_jited_line_info = 0; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2526 | goto done; |
| 2527 | } |
| 2528 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2529 | ulen = info.xlated_prog_len; |
Daniel Borkmann | 9975a54 | 2017-07-28 17:05:25 +0200 | [diff] [blame] | 2530 | info.xlated_prog_len = bpf_prog_insn_size(prog); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2531 | if (info.xlated_prog_len && ulen) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2532 | struct bpf_insn *insns_sanitized; |
| 2533 | bool fault; |
| 2534 | |
| 2535 | if (prog->blinded && !bpf_dump_raw_ok()) { |
| 2536 | info.xlated_prog_insns = 0; |
| 2537 | goto done; |
| 2538 | } |
| 2539 | insns_sanitized = bpf_insn_prepare_dump(prog); |
| 2540 | if (!insns_sanitized) |
| 2541 | return -ENOMEM; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2542 | uinsns = u64_to_user_ptr(info.xlated_prog_insns); |
| 2543 | ulen = min_t(u32, info.xlated_prog_len, ulen); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2544 | fault = copy_to_user(uinsns, insns_sanitized, ulen); |
| 2545 | kfree(insns_sanitized); |
| 2546 | if (fault) |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2547 | return -EFAULT; |
| 2548 | } |
| 2549 | |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 2550 | if (bpf_prog_is_dev_bound(prog->aux)) { |
| 2551 | err = bpf_prog_offload_info_fill(&info, prog); |
| 2552 | if (err) |
| 2553 | return err; |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 2554 | goto done; |
| 2555 | } |
| 2556 | |
| 2557 | /* NOTE: the following code is supposed to be skipped for offload. |
| 2558 | * bpf_prog_offload_info_fill() is the place to fill similar fields |
| 2559 | * for offload. |
| 2560 | */ |
| 2561 | ulen = info.jited_prog_len; |
Sandipan Das | 4d56a76 | 2018-05-24 12:26:51 +0530 | [diff] [blame] | 2562 | if (prog->aux->func_cnt) { |
| 2563 | u32 i; |
| 2564 | |
| 2565 | info.jited_prog_len = 0; |
| 2566 | for (i = 0; i < prog->aux->func_cnt; i++) |
| 2567 | info.jited_prog_len += prog->aux->func[i]->jited_len; |
| 2568 | } else { |
| 2569 | info.jited_prog_len = prog->jited_len; |
| 2570 | } |
| 2571 | |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 2572 | if (info.jited_prog_len && ulen) { |
| 2573 | if (bpf_dump_raw_ok()) { |
| 2574 | uinsns = u64_to_user_ptr(info.jited_prog_insns); |
| 2575 | ulen = min_t(u32, info.jited_prog_len, ulen); |
Sandipan Das | 4d56a76 | 2018-05-24 12:26:51 +0530 | [diff] [blame] | 2576 | |
| 2577 | /* for multi-function programs, copy the JITed |
| 2578 | * instructions for all the functions |
| 2579 | */ |
| 2580 | if (prog->aux->func_cnt) { |
| 2581 | u32 len, free, i; |
| 2582 | u8 *img; |
| 2583 | |
| 2584 | free = ulen; |
| 2585 | for (i = 0; i < prog->aux->func_cnt; i++) { |
| 2586 | len = prog->aux->func[i]->jited_len; |
| 2587 | len = min_t(u32, len, free); |
| 2588 | img = (u8 *) prog->aux->func[i]->bpf_func; |
| 2589 | if (copy_to_user(uinsns, img, len)) |
| 2590 | return -EFAULT; |
| 2591 | uinsns += len; |
| 2592 | free -= len; |
| 2593 | if (!free) |
| 2594 | break; |
| 2595 | } |
| 2596 | } else { |
| 2597 | if (copy_to_user(uinsns, prog->bpf_func, ulen)) |
| 2598 | return -EFAULT; |
| 2599 | } |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 2600 | } else { |
| 2601 | info.jited_prog_insns = 0; |
| 2602 | } |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 2603 | } |
| 2604 | |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2605 | ulen = info.nr_jited_ksyms; |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2606 | info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; |
Song Liu | 7a5725d | 2018-12-10 11:17:50 -0800 | [diff] [blame] | 2607 | if (ulen) { |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2608 | if (bpf_dump_raw_ok()) { |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2609 | unsigned long ksym_addr; |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2610 | u64 __user *user_ksyms; |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2611 | u32 i; |
| 2612 | |
| 2613 | /* copy the address of the kernel symbol |
| 2614 | * corresponding to each function |
| 2615 | */ |
| 2616 | ulen = min_t(u32, info.nr_jited_ksyms, ulen); |
| 2617 | user_ksyms = u64_to_user_ptr(info.jited_ksyms); |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2618 | if (prog->aux->func_cnt) { |
| 2619 | for (i = 0; i < ulen; i++) { |
| 2620 | ksym_addr = (unsigned long) |
| 2621 | prog->aux->func[i]->bpf_func; |
| 2622 | if (put_user((u64) ksym_addr, |
| 2623 | &user_ksyms[i])) |
| 2624 | return -EFAULT; |
| 2625 | } |
| 2626 | } else { |
| 2627 | ksym_addr = (unsigned long) prog->bpf_func; |
| 2628 | if (put_user((u64) ksym_addr, &user_ksyms[0])) |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2629 | return -EFAULT; |
| 2630 | } |
| 2631 | } else { |
| 2632 | info.jited_ksyms = 0; |
| 2633 | } |
| 2634 | } |
| 2635 | |
Sandipan Das | 815581c | 2018-05-24 12:26:52 +0530 | [diff] [blame] | 2636 | ulen = info.nr_jited_func_lens; |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2637 | info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; |
Song Liu | 7a5725d | 2018-12-10 11:17:50 -0800 | [diff] [blame] | 2638 | if (ulen) { |
Sandipan Das | 815581c | 2018-05-24 12:26:52 +0530 | [diff] [blame] | 2639 | if (bpf_dump_raw_ok()) { |
| 2640 | u32 __user *user_lens; |
| 2641 | u32 func_len, i; |
| 2642 | |
| 2643 | /* copy the JITed image lengths for each function */ |
| 2644 | ulen = min_t(u32, info.nr_jited_func_lens, ulen); |
| 2645 | user_lens = u64_to_user_ptr(info.jited_func_lens); |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2646 | if (prog->aux->func_cnt) { |
| 2647 | for (i = 0; i < ulen; i++) { |
| 2648 | func_len = |
| 2649 | prog->aux->func[i]->jited_len; |
| 2650 | if (put_user(func_len, &user_lens[i])) |
| 2651 | return -EFAULT; |
| 2652 | } |
| 2653 | } else { |
| 2654 | func_len = prog->jited_len; |
| 2655 | if (put_user(func_len, &user_lens[0])) |
Sandipan Das | 815581c | 2018-05-24 12:26:52 +0530 | [diff] [blame] | 2656 | return -EFAULT; |
| 2657 | } |
| 2658 | } else { |
| 2659 | info.jited_func_lens = 0; |
| 2660 | } |
| 2661 | } |
| 2662 | |
Martin KaFai Lau | 7337224 | 2018-12-05 17:35:43 -0800 | [diff] [blame] | 2663 | if (prog->aux->btf) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 2664 | info.btf_id = btf_id(prog->aux->btf); |
| 2665 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2666 | ulen = info.nr_func_info; |
| 2667 | info.nr_func_info = prog->aux->func_info_cnt; |
| 2668 | if (info.nr_func_info && ulen) { |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 2669 | char __user *user_finfo; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 2670 | |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 2671 | user_finfo = u64_to_user_ptr(info.func_info); |
| 2672 | ulen = min_t(u32, info.nr_func_info, ulen); |
| 2673 | if (copy_to_user(user_finfo, prog->aux->func_info, |
| 2674 | info.func_info_rec_size * ulen)) |
| 2675 | return -EFAULT; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 2676 | } |
| 2677 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2678 | ulen = info.nr_line_info; |
| 2679 | info.nr_line_info = prog->aux->nr_linfo; |
| 2680 | if (info.nr_line_info && ulen) { |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 2681 | __u8 __user *user_linfo; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2682 | |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 2683 | user_linfo = u64_to_user_ptr(info.line_info); |
| 2684 | ulen = min_t(u32, info.nr_line_info, ulen); |
| 2685 | if (copy_to_user(user_linfo, prog->aux->linfo, |
| 2686 | info.line_info_rec_size * ulen)) |
| 2687 | return -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2688 | } |
| 2689 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2690 | ulen = info.nr_jited_line_info; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2691 | if (prog->aux->jited_linfo) |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2692 | info.nr_jited_line_info = prog->aux->nr_linfo; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2693 | else |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2694 | info.nr_jited_line_info = 0; |
| 2695 | if (info.nr_jited_line_info && ulen) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2696 | if (bpf_dump_raw_ok()) { |
| 2697 | __u64 __user *user_linfo; |
| 2698 | u32 i; |
| 2699 | |
| 2700 | user_linfo = u64_to_user_ptr(info.jited_line_info); |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2701 | ulen = min_t(u32, info.nr_jited_line_info, ulen); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2702 | for (i = 0; i < ulen; i++) { |
| 2703 | if (put_user((__u64)(long)prog->aux->jited_linfo[i], |
| 2704 | &user_linfo[i])) |
| 2705 | return -EFAULT; |
| 2706 | } |
| 2707 | } else { |
| 2708 | info.jited_line_info = 0; |
| 2709 | } |
| 2710 | } |
| 2711 | |
Song Liu | c872bdb | 2018-12-12 09:37:46 -0800 | [diff] [blame] | 2712 | ulen = info.nr_prog_tags; |
| 2713 | info.nr_prog_tags = prog->aux->func_cnt ? : 1; |
| 2714 | if (ulen) { |
| 2715 | __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; |
| 2716 | u32 i; |
| 2717 | |
| 2718 | user_prog_tags = u64_to_user_ptr(info.prog_tags); |
| 2719 | ulen = min_t(u32, info.nr_prog_tags, ulen); |
| 2720 | if (prog->aux->func_cnt) { |
| 2721 | for (i = 0; i < ulen; i++) { |
| 2722 | if (copy_to_user(user_prog_tags[i], |
| 2723 | prog->aux->func[i]->tag, |
| 2724 | BPF_TAG_SIZE)) |
| 2725 | return -EFAULT; |
| 2726 | } |
| 2727 | } else { |
| 2728 | if (copy_to_user(user_prog_tags[0], |
| 2729 | prog->tag, BPF_TAG_SIZE)) |
| 2730 | return -EFAULT; |
| 2731 | } |
| 2732 | } |
| 2733 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2734 | done: |
| 2735 | if (copy_to_user(uinfo, &info, info_len) || |
| 2736 | put_user(info_len, &uattr->info.info_len)) |
| 2737 | return -EFAULT; |
| 2738 | |
| 2739 | return 0; |
| 2740 | } |
| 2741 | |
| 2742 | static int bpf_map_get_info_by_fd(struct bpf_map *map, |
| 2743 | const union bpf_attr *attr, |
| 2744 | union bpf_attr __user *uattr) |
| 2745 | { |
| 2746 | struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 2747 | struct bpf_map_info info = {}; |
| 2748 | u32 info_len = attr->info.info_len; |
| 2749 | int err; |
| 2750 | |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 2751 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2752 | if (err) |
| 2753 | return err; |
| 2754 | info_len = min_t(u32, sizeof(info), info_len); |
| 2755 | |
| 2756 | info.type = map->map_type; |
| 2757 | info.id = map->id; |
| 2758 | info.key_size = map->key_size; |
| 2759 | info.value_size = map->value_size; |
| 2760 | info.max_entries = map->max_entries; |
| 2761 | info.map_flags = map->map_flags; |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 2762 | memcpy(info.name, map->name, sizeof(map->name)); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2763 | |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 2764 | if (map->btf) { |
| 2765 | info.btf_id = btf_id(map->btf); |
Martin KaFai Lau | 9b2cf32 | 2018-05-22 14:57:21 -0700 | [diff] [blame] | 2766 | info.btf_key_type_id = map->btf_key_type_id; |
| 2767 | info.btf_value_type_id = map->btf_value_type_id; |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 2768 | } |
| 2769 | |
Jakub Kicinski | 52775b3 | 2018-01-17 19:13:28 -0800 | [diff] [blame] | 2770 | if (bpf_map_is_dev_bound(map)) { |
| 2771 | err = bpf_map_offload_info_fill(&info, map); |
| 2772 | if (err) |
| 2773 | return err; |
| 2774 | } |
| 2775 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2776 | if (copy_to_user(uinfo, &info, info_len) || |
| 2777 | put_user(info_len, &uattr->info.info_len)) |
| 2778 | return -EFAULT; |
| 2779 | |
| 2780 | return 0; |
| 2781 | } |
| 2782 | |
Martin KaFai Lau | 62dab84 | 2018-05-04 14:49:52 -0700 | [diff] [blame] | 2783 | static int bpf_btf_get_info_by_fd(struct btf *btf, |
| 2784 | const union bpf_attr *attr, |
| 2785 | union bpf_attr __user *uattr) |
| 2786 | { |
| 2787 | struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 2788 | u32 info_len = attr->info.info_len; |
| 2789 | int err; |
| 2790 | |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 2791 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len); |
Martin KaFai Lau | 62dab84 | 2018-05-04 14:49:52 -0700 | [diff] [blame] | 2792 | if (err) |
| 2793 | return err; |
| 2794 | |
| 2795 | return btf_get_info_by_fd(btf, attr, uattr); |
| 2796 | } |
| 2797 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2798 | #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info |
| 2799 | |
| 2800 | static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, |
| 2801 | union bpf_attr __user *uattr) |
| 2802 | { |
| 2803 | int ufd = attr->info.bpf_fd; |
| 2804 | struct fd f; |
| 2805 | int err; |
| 2806 | |
| 2807 | if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) |
| 2808 | return -EINVAL; |
| 2809 | |
| 2810 | f = fdget(ufd); |
| 2811 | if (!f.file) |
| 2812 | return -EBADFD; |
| 2813 | |
| 2814 | if (f.file->f_op == &bpf_prog_fops) |
| 2815 | err = bpf_prog_get_info_by_fd(f.file->private_data, attr, |
| 2816 | uattr); |
| 2817 | else if (f.file->f_op == &bpf_map_fops) |
| 2818 | err = bpf_map_get_info_by_fd(f.file->private_data, attr, |
| 2819 | uattr); |
Martin KaFai Lau | 60197cf | 2018-04-18 15:56:02 -0700 | [diff] [blame] | 2820 | else if (f.file->f_op == &btf_fops) |
Martin KaFai Lau | 62dab84 | 2018-05-04 14:49:52 -0700 | [diff] [blame] | 2821 | err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2822 | else |
| 2823 | err = -EINVAL; |
| 2824 | |
| 2825 | fdput(f); |
| 2826 | return err; |
| 2827 | } |
| 2828 | |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 2829 | #define BPF_BTF_LOAD_LAST_FIELD btf_log_level |
| 2830 | |
| 2831 | static int bpf_btf_load(const union bpf_attr *attr) |
| 2832 | { |
| 2833 | if (CHECK_ATTR(BPF_BTF_LOAD)) |
| 2834 | return -EINVAL; |
| 2835 | |
| 2836 | if (!capable(CAP_SYS_ADMIN)) |
| 2837 | return -EPERM; |
| 2838 | |
| 2839 | return btf_new_fd(attr); |
| 2840 | } |
| 2841 | |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 2842 | #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id |
| 2843 | |
| 2844 | static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) |
| 2845 | { |
| 2846 | if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) |
| 2847 | return -EINVAL; |
| 2848 | |
| 2849 | if (!capable(CAP_SYS_ADMIN)) |
| 2850 | return -EPERM; |
| 2851 | |
| 2852 | return btf_get_fd_by_id(attr->btf_id); |
| 2853 | } |
| 2854 | |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 2855 | static int bpf_task_fd_query_copy(const union bpf_attr *attr, |
| 2856 | union bpf_attr __user *uattr, |
| 2857 | u32 prog_id, u32 fd_type, |
| 2858 | const char *buf, u64 probe_offset, |
| 2859 | u64 probe_addr) |
| 2860 | { |
| 2861 | char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); |
| 2862 | u32 len = buf ? strlen(buf) : 0, input_len; |
| 2863 | int err = 0; |
| 2864 | |
| 2865 | if (put_user(len, &uattr->task_fd_query.buf_len)) |
| 2866 | return -EFAULT; |
| 2867 | input_len = attr->task_fd_query.buf_len; |
| 2868 | if (input_len && ubuf) { |
| 2869 | if (!len) { |
| 2870 | /* nothing to copy, just make ubuf NULL terminated */ |
| 2871 | char zero = '\0'; |
| 2872 | |
| 2873 | if (put_user(zero, ubuf)) |
| 2874 | return -EFAULT; |
| 2875 | } else if (input_len >= len + 1) { |
| 2876 | /* ubuf can hold the string with NULL terminator */ |
| 2877 | if (copy_to_user(ubuf, buf, len + 1)) |
| 2878 | return -EFAULT; |
| 2879 | } else { |
| 2880 | /* ubuf cannot hold the string with NULL terminator, |
| 2881 | * do a partial copy with NULL terminator. |
| 2882 | */ |
| 2883 | char zero = '\0'; |
| 2884 | |
| 2885 | err = -ENOSPC; |
| 2886 | if (copy_to_user(ubuf, buf, input_len - 1)) |
| 2887 | return -EFAULT; |
| 2888 | if (put_user(zero, ubuf + input_len - 1)) |
| 2889 | return -EFAULT; |
| 2890 | } |
| 2891 | } |
| 2892 | |
| 2893 | if (put_user(prog_id, &uattr->task_fd_query.prog_id) || |
| 2894 | put_user(fd_type, &uattr->task_fd_query.fd_type) || |
| 2895 | put_user(probe_offset, &uattr->task_fd_query.probe_offset) || |
| 2896 | put_user(probe_addr, &uattr->task_fd_query.probe_addr)) |
| 2897 | return -EFAULT; |
| 2898 | |
| 2899 | return err; |
| 2900 | } |
| 2901 | |
| 2902 | #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr |
| 2903 | |
| 2904 | static int bpf_task_fd_query(const union bpf_attr *attr, |
| 2905 | union bpf_attr __user *uattr) |
| 2906 | { |
| 2907 | pid_t pid = attr->task_fd_query.pid; |
| 2908 | u32 fd = attr->task_fd_query.fd; |
| 2909 | const struct perf_event *event; |
| 2910 | struct files_struct *files; |
| 2911 | struct task_struct *task; |
| 2912 | struct file *file; |
| 2913 | int err; |
| 2914 | |
| 2915 | if (CHECK_ATTR(BPF_TASK_FD_QUERY)) |
| 2916 | return -EINVAL; |
| 2917 | |
| 2918 | if (!capable(CAP_SYS_ADMIN)) |
| 2919 | return -EPERM; |
| 2920 | |
| 2921 | if (attr->task_fd_query.flags != 0) |
| 2922 | return -EINVAL; |
| 2923 | |
| 2924 | task = get_pid_task(find_vpid(pid), PIDTYPE_PID); |
| 2925 | if (!task) |
| 2926 | return -ENOENT; |
| 2927 | |
| 2928 | files = get_files_struct(task); |
| 2929 | put_task_struct(task); |
| 2930 | if (!files) |
| 2931 | return -ENOENT; |
| 2932 | |
| 2933 | err = 0; |
| 2934 | spin_lock(&files->file_lock); |
| 2935 | file = fcheck_files(files, fd); |
| 2936 | if (!file) |
| 2937 | err = -EBADF; |
| 2938 | else |
| 2939 | get_file(file); |
| 2940 | spin_unlock(&files->file_lock); |
| 2941 | put_files_struct(files); |
| 2942 | |
| 2943 | if (err) |
| 2944 | goto out; |
| 2945 | |
| 2946 | if (file->f_op == &bpf_raw_tp_fops) { |
| 2947 | struct bpf_raw_tracepoint *raw_tp = file->private_data; |
| 2948 | struct bpf_raw_event_map *btp = raw_tp->btp; |
| 2949 | |
| 2950 | err = bpf_task_fd_query_copy(attr, uattr, |
| 2951 | raw_tp->prog->aux->id, |
| 2952 | BPF_FD_TYPE_RAW_TRACEPOINT, |
| 2953 | btp->tp->name, 0, 0); |
| 2954 | goto put_file; |
| 2955 | } |
| 2956 | |
| 2957 | event = perf_get_event(file); |
| 2958 | if (!IS_ERR(event)) { |
| 2959 | u64 probe_offset, probe_addr; |
| 2960 | u32 prog_id, fd_type; |
| 2961 | const char *buf; |
| 2962 | |
| 2963 | err = bpf_get_perf_event_info(event, &prog_id, &fd_type, |
| 2964 | &buf, &probe_offset, |
| 2965 | &probe_addr); |
| 2966 | if (!err) |
| 2967 | err = bpf_task_fd_query_copy(attr, uattr, prog_id, |
| 2968 | fd_type, buf, |
| 2969 | probe_offset, |
| 2970 | probe_addr); |
| 2971 | goto put_file; |
| 2972 | } |
| 2973 | |
| 2974 | err = -ENOTSUPP; |
| 2975 | put_file: |
| 2976 | fput(file); |
| 2977 | out: |
| 2978 | return err; |
| 2979 | } |
| 2980 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2981 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
| 2982 | { |
| 2983 | union bpf_attr attr = {}; |
| 2984 | int err; |
| 2985 | |
Chenbo Feng | 0fa4fe8 | 2018-03-19 17:57:27 -0700 | [diff] [blame] | 2986 | if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN)) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2987 | return -EPERM; |
| 2988 | |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 2989 | err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2990 | if (err) |
| 2991 | return err; |
| 2992 | size = min_t(u32, size, sizeof(attr)); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2993 | |
| 2994 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ |
| 2995 | if (copy_from_user(&attr, uattr, size) != 0) |
| 2996 | return -EFAULT; |
| 2997 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 2998 | err = security_bpf(cmd, &attr, size); |
| 2999 | if (err < 0) |
| 3000 | return err; |
| 3001 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 3002 | switch (cmd) { |
| 3003 | case BPF_MAP_CREATE: |
| 3004 | err = map_create(&attr); |
| 3005 | break; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 3006 | case BPF_MAP_LOOKUP_ELEM: |
| 3007 | err = map_lookup_elem(&attr); |
| 3008 | break; |
| 3009 | case BPF_MAP_UPDATE_ELEM: |
| 3010 | err = map_update_elem(&attr); |
| 3011 | break; |
| 3012 | case BPF_MAP_DELETE_ELEM: |
| 3013 | err = map_delete_elem(&attr); |
| 3014 | break; |
| 3015 | case BPF_MAP_GET_NEXT_KEY: |
| 3016 | err = map_get_next_key(&attr); |
| 3017 | break; |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 3018 | case BPF_MAP_FREEZE: |
| 3019 | err = map_freeze(&attr); |
| 3020 | break; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 3021 | case BPF_PROG_LOAD: |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 3022 | err = bpf_prog_load(&attr, uattr); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 3023 | break; |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 3024 | case BPF_OBJ_PIN: |
| 3025 | err = bpf_obj_pin(&attr); |
| 3026 | break; |
| 3027 | case BPF_OBJ_GET: |
| 3028 | err = bpf_obj_get(&attr); |
| 3029 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 3030 | case BPF_PROG_ATTACH: |
| 3031 | err = bpf_prog_attach(&attr); |
| 3032 | break; |
| 3033 | case BPF_PROG_DETACH: |
| 3034 | err = bpf_prog_detach(&attr); |
| 3035 | break; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 3036 | case BPF_PROG_QUERY: |
| 3037 | err = bpf_prog_query(&attr, uattr); |
| 3038 | break; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 3039 | case BPF_PROG_TEST_RUN: |
| 3040 | err = bpf_prog_test_run(&attr, uattr); |
| 3041 | break; |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 3042 | case BPF_PROG_GET_NEXT_ID: |
| 3043 | err = bpf_obj_get_next_id(&attr, uattr, |
| 3044 | &prog_idr, &prog_idr_lock); |
| 3045 | break; |
| 3046 | case BPF_MAP_GET_NEXT_ID: |
| 3047 | err = bpf_obj_get_next_id(&attr, uattr, |
| 3048 | &map_idr, &map_idr_lock); |
| 3049 | break; |
Quentin Monnet | 1b9ed84 | 2019-08-20 10:31:50 +0100 | [diff] [blame] | 3050 | case BPF_BTF_GET_NEXT_ID: |
| 3051 | err = bpf_obj_get_next_id(&attr, uattr, |
| 3052 | &btf_idr, &btf_idr_lock); |
| 3053 | break; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 3054 | case BPF_PROG_GET_FD_BY_ID: |
| 3055 | err = bpf_prog_get_fd_by_id(&attr); |
| 3056 | break; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 3057 | case BPF_MAP_GET_FD_BY_ID: |
| 3058 | err = bpf_map_get_fd_by_id(&attr); |
| 3059 | break; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3060 | case BPF_OBJ_GET_INFO_BY_FD: |
| 3061 | err = bpf_obj_get_info_by_fd(&attr, uattr); |
| 3062 | break; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 3063 | case BPF_RAW_TRACEPOINT_OPEN: |
| 3064 | err = bpf_raw_tracepoint_open(&attr); |
| 3065 | break; |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 3066 | case BPF_BTF_LOAD: |
| 3067 | err = bpf_btf_load(&attr); |
| 3068 | break; |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 3069 | case BPF_BTF_GET_FD_BY_ID: |
| 3070 | err = bpf_btf_get_fd_by_id(&attr); |
| 3071 | break; |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 3072 | case BPF_TASK_FD_QUERY: |
| 3073 | err = bpf_task_fd_query(&attr, uattr); |
| 3074 | break; |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 3075 | case BPF_MAP_LOOKUP_AND_DELETE_ELEM: |
| 3076 | err = map_lookup_and_delete_elem(&attr); |
| 3077 | break; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 3078 | default: |
| 3079 | err = -EINVAL; |
| 3080 | break; |
| 3081 | } |
| 3082 | |
| 3083 | return err; |
| 3084 | } |