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