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