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