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> |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame^] | 14 | #include <linux/btf.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 15 | #include <linux/syscalls.h> |
| 16 | #include <linux/slab.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 17 | #include <linux/sched/signal.h> |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 18 | #include <linux/vmalloc.h> |
| 19 | #include <linux/mmzone.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 20 | #include <linux/anon_inodes.h> |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 21 | #include <linux/file.h> |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 22 | #include <linux/license.h> |
| 23 | #include <linux/filter.h> |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 24 | #include <linux/version.h> |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 25 | #include <linux/kernel.h> |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 26 | #include <linux/idr.h> |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 27 | #include <linux/cred.h> |
| 28 | #include <linux/timekeeping.h> |
| 29 | #include <linux/ctype.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 30 | |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 31 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ |
| 32 | (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ |
| 33 | (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ |
| 34 | (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) |
| 35 | #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) |
| 36 | #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map)) |
| 37 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 38 | #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) |
| 39 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 40 | DEFINE_PER_CPU(int, bpf_prog_active); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 41 | static DEFINE_IDR(prog_idr); |
| 42 | static DEFINE_SPINLOCK(prog_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 43 | static DEFINE_IDR(map_idr); |
| 44 | static DEFINE_SPINLOCK(map_idr_lock); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 45 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 46 | int sysctl_unprivileged_bpf_disabled __read_mostly; |
| 47 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 48 | static const struct bpf_map_ops * const bpf_map_types[] = { |
| 49 | #define BPF_PROG_TYPE(_id, _ops) |
| 50 | #define BPF_MAP_TYPE(_id, _ops) \ |
| 51 | [_id] = &_ops, |
| 52 | #include <linux/bpf_types.h> |
| 53 | #undef BPF_PROG_TYPE |
| 54 | #undef BPF_MAP_TYPE |
| 55 | }; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 56 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 57 | /* |
| 58 | * If we're handed a bigger struct than we know of, ensure all the unknown bits |
| 59 | * are 0 - i.e. new user-space does not rely on any kernel feature extensions |
| 60 | * we don't know about yet. |
| 61 | * |
| 62 | * There is a ToCToU between this function call and the following |
| 63 | * copy_from_user() call. However, this is not a concern since this function is |
| 64 | * meant to be a future-proofing of bits. |
| 65 | */ |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 66 | static int check_uarg_tail_zero(void __user *uaddr, |
| 67 | size_t expected_size, |
| 68 | size_t actual_size) |
| 69 | { |
| 70 | unsigned char __user *addr; |
| 71 | unsigned char __user *end; |
| 72 | unsigned char val; |
| 73 | int err; |
| 74 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 75 | if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ |
| 76 | return -E2BIG; |
| 77 | |
| 78 | if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size))) |
| 79 | return -EFAULT; |
| 80 | |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 81 | if (actual_size <= expected_size) |
| 82 | return 0; |
| 83 | |
| 84 | addr = uaddr + expected_size; |
| 85 | end = uaddr + actual_size; |
| 86 | |
| 87 | for (; addr < end; addr++) { |
| 88 | err = get_user(val, addr); |
| 89 | if (err) |
| 90 | return err; |
| 91 | if (val) |
| 92 | return -E2BIG; |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 98 | const struct bpf_map_ops bpf_map_offload_ops = { |
| 99 | .map_alloc = bpf_map_offload_map_alloc, |
| 100 | .map_free = bpf_map_offload_map_free, |
| 101 | }; |
| 102 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 103 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
| 104 | { |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 105 | const struct bpf_map_ops *ops; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 106 | struct bpf_map *map; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 107 | int err; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 108 | |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 109 | if (attr->map_type >= ARRAY_SIZE(bpf_map_types)) |
| 110 | return ERR_PTR(-EINVAL); |
| 111 | ops = bpf_map_types[attr->map_type]; |
| 112 | if (!ops) |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 113 | return ERR_PTR(-EINVAL); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 114 | |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 115 | if (ops->map_alloc_check) { |
| 116 | err = ops->map_alloc_check(attr); |
| 117 | if (err) |
| 118 | return ERR_PTR(err); |
| 119 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 120 | if (attr->map_ifindex) |
| 121 | ops = &bpf_map_offload_ops; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 122 | map = ops->map_alloc(attr); |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 123 | if (IS_ERR(map)) |
| 124 | return map; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 125 | map->ops = ops; |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 126 | map->map_type = attr->map_type; |
| 127 | return map; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 130 | void *bpf_map_area_alloc(size_t size, int numa_node) |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 131 | { |
| 132 | /* We definitely need __GFP_NORETRY, so OOM killer doesn't |
| 133 | * trigger under memory pressure as we really just want to |
| 134 | * fail instead. |
| 135 | */ |
| 136 | const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO; |
| 137 | void *area; |
| 138 | |
| 139 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 140 | area = kmalloc_node(size, GFP_USER | flags, numa_node); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 141 | if (area != NULL) |
| 142 | return area; |
| 143 | } |
| 144 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 145 | return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags, |
| 146 | __builtin_return_address(0)); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void bpf_map_area_free(void *area) |
| 150 | { |
| 151 | kvfree(area); |
| 152 | } |
| 153 | |
Jakub Kicinski | bd47564 | 2018-01-11 20:29:06 -0800 | [diff] [blame] | 154 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) |
| 155 | { |
| 156 | map->map_type = attr->map_type; |
| 157 | map->key_size = attr->key_size; |
| 158 | map->value_size = attr->value_size; |
| 159 | map->max_entries = attr->max_entries; |
| 160 | map->map_flags = attr->map_flags; |
| 161 | map->numa_node = bpf_map_attr_numa_node(attr); |
| 162 | } |
| 163 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 164 | int bpf_map_precharge_memlock(u32 pages) |
| 165 | { |
| 166 | struct user_struct *user = get_current_user(); |
| 167 | unsigned long memlock_limit, cur; |
| 168 | |
| 169 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 170 | cur = atomic_long_read(&user->locked_vm); |
| 171 | free_uid(user); |
| 172 | if (cur + pages > memlock_limit) |
| 173 | return -EPERM; |
| 174 | return 0; |
| 175 | } |
| 176 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 177 | static int bpf_map_charge_memlock(struct bpf_map *map) |
| 178 | { |
| 179 | struct user_struct *user = get_current_user(); |
| 180 | unsigned long memlock_limit; |
| 181 | |
| 182 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 183 | |
| 184 | atomic_long_add(map->pages, &user->locked_vm); |
| 185 | |
| 186 | if (atomic_long_read(&user->locked_vm) > memlock_limit) { |
| 187 | atomic_long_sub(map->pages, &user->locked_vm); |
| 188 | free_uid(user); |
| 189 | return -EPERM; |
| 190 | } |
| 191 | map->user = user; |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static void bpf_map_uncharge_memlock(struct bpf_map *map) |
| 196 | { |
| 197 | struct user_struct *user = map->user; |
| 198 | |
| 199 | atomic_long_sub(map->pages, &user->locked_vm); |
| 200 | free_uid(user); |
| 201 | } |
| 202 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 203 | static int bpf_map_alloc_id(struct bpf_map *map) |
| 204 | { |
| 205 | int id; |
| 206 | |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 207 | idr_preload(GFP_KERNEL); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 208 | spin_lock_bh(&map_idr_lock); |
| 209 | id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); |
| 210 | if (id > 0) |
| 211 | map->id = id; |
| 212 | spin_unlock_bh(&map_idr_lock); |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 213 | idr_preload_end(); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 214 | |
| 215 | if (WARN_ON_ONCE(!id)) |
| 216 | return -ENOSPC; |
| 217 | |
| 218 | return id > 0 ? 0 : id; |
| 219 | } |
| 220 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 221 | 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] | 222 | { |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 223 | unsigned long flags; |
| 224 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 225 | /* Offloaded maps are removed from the IDR store when their device |
| 226 | * disappears - even if someone holds an fd to them they are unusable, |
| 227 | * the memory is gone, all ops will fail; they are simply waiting for |
| 228 | * refcnt to drop to be freed. |
| 229 | */ |
| 230 | if (!map->id) |
| 231 | return; |
| 232 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 233 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 234 | spin_lock_irqsave(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 235 | else |
| 236 | __acquire(&map_idr_lock); |
| 237 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 238 | idr_remove(&map_idr, map->id); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 239 | map->id = 0; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 240 | |
| 241 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 242 | spin_unlock_irqrestore(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 243 | else |
| 244 | __release(&map_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 247 | /* called from workqueue */ |
| 248 | static void bpf_map_free_deferred(struct work_struct *work) |
| 249 | { |
| 250 | struct bpf_map *map = container_of(work, struct bpf_map, work); |
| 251 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 252 | bpf_map_uncharge_memlock(map); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 253 | security_bpf_map_free(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 254 | /* implementation dependent freeing */ |
| 255 | map->ops->map_free(map); |
| 256 | } |
| 257 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 258 | static void bpf_map_put_uref(struct bpf_map *map) |
| 259 | { |
| 260 | if (atomic_dec_and_test(&map->usercnt)) { |
| 261 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) |
| 262 | bpf_fd_array_map_clear(map); |
| 263 | } |
| 264 | } |
| 265 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 266 | /* decrement map refcnt and schedule it for freeing via workqueue |
| 267 | * (unrelying map implementation ops->map_free() might sleep) |
| 268 | */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 269 | 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] | 270 | { |
| 271 | if (atomic_dec_and_test(&map->refcnt)) { |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 272 | /* bpf_map_free_id() must be called first */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 273 | bpf_map_free_id(map, do_idr_lock); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 274 | INIT_WORK(&map->work, bpf_map_free_deferred); |
| 275 | schedule_work(&map->work); |
| 276 | } |
| 277 | } |
| 278 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 279 | void bpf_map_put(struct bpf_map *map) |
| 280 | { |
| 281 | __bpf_map_put(map, true); |
| 282 | } |
| 283 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 284 | void bpf_map_put_with_uref(struct bpf_map *map) |
| 285 | { |
| 286 | bpf_map_put_uref(map); |
| 287 | bpf_map_put(map); |
| 288 | } |
| 289 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 290 | static int bpf_map_release(struct inode *inode, struct file *filp) |
| 291 | { |
Daniel Borkmann | 61d1b6a | 2016-06-15 22:47:12 +0200 | [diff] [blame] | 292 | struct bpf_map *map = filp->private_data; |
| 293 | |
| 294 | if (map->ops->map_release) |
| 295 | map->ops->map_release(map, filp); |
| 296 | |
| 297 | bpf_map_put_with_uref(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 301 | #ifdef CONFIG_PROC_FS |
| 302 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) |
| 303 | { |
| 304 | const struct bpf_map *map = filp->private_data; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 305 | const struct bpf_array *array; |
| 306 | u32 owner_prog_type = 0; |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 307 | u32 owner_jited = 0; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 308 | |
| 309 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { |
| 310 | array = container_of(map, struct bpf_array, map); |
| 311 | owner_prog_type = array->owner_prog_type; |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 312 | owner_jited = array->owner_jited; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 313 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 314 | |
| 315 | seq_printf(m, |
| 316 | "map_type:\t%u\n" |
| 317 | "key_size:\t%u\n" |
| 318 | "value_size:\t%u\n" |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 319 | "max_entries:\t%u\n" |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 320 | "map_flags:\t%#x\n" |
| 321 | "memlock:\t%llu\n", |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 322 | map->map_type, |
| 323 | map->key_size, |
| 324 | map->value_size, |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 325 | map->max_entries, |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 326 | map->map_flags, |
| 327 | map->pages * 1ULL << PAGE_SHIFT); |
| 328 | |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 329 | if (owner_prog_type) { |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 330 | seq_printf(m, "owner_prog_type:\t%u\n", |
| 331 | owner_prog_type); |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 332 | seq_printf(m, "owner_jited:\t%u\n", |
| 333 | owner_jited); |
| 334 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 335 | } |
| 336 | #endif |
| 337 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 338 | static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, |
| 339 | loff_t *ppos) |
| 340 | { |
| 341 | /* We need this handler such that alloc_file() enables |
| 342 | * f_mode with FMODE_CAN_READ. |
| 343 | */ |
| 344 | return -EINVAL; |
| 345 | } |
| 346 | |
| 347 | static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, |
| 348 | size_t siz, loff_t *ppos) |
| 349 | { |
| 350 | /* We need this handler such that alloc_file() enables |
| 351 | * f_mode with FMODE_CAN_WRITE. |
| 352 | */ |
| 353 | return -EINVAL; |
| 354 | } |
| 355 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 356 | const struct file_operations bpf_map_fops = { |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 357 | #ifdef CONFIG_PROC_FS |
| 358 | .show_fdinfo = bpf_map_show_fdinfo, |
| 359 | #endif |
| 360 | .release = bpf_map_release, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 361 | .read = bpf_dummy_read, |
| 362 | .write = bpf_dummy_write, |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 363 | }; |
| 364 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 365 | int bpf_map_new_fd(struct bpf_map *map, int flags) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 366 | { |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 367 | int ret; |
| 368 | |
| 369 | ret = security_bpf_map(map, OPEN_FMODE(flags)); |
| 370 | if (ret < 0) |
| 371 | return ret; |
| 372 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 373 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 374 | flags | O_CLOEXEC); |
| 375 | } |
| 376 | |
| 377 | int bpf_get_file_flag(int flags) |
| 378 | { |
| 379 | if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) |
| 380 | return -EINVAL; |
| 381 | if (flags & BPF_F_RDONLY) |
| 382 | return O_RDONLY; |
| 383 | if (flags & BPF_F_WRONLY) |
| 384 | return O_WRONLY; |
| 385 | return O_RDWR; |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 386 | } |
| 387 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 388 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
| 389 | #define CHECK_ATTR(CMD) \ |
| 390 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ |
| 391 | sizeof(attr->CMD##_LAST_FIELD), 0, \ |
| 392 | sizeof(*attr) - \ |
| 393 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ |
| 394 | sizeof(attr->CMD##_LAST_FIELD)) != NULL |
| 395 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 396 | /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes. |
| 397 | * Return 0 on success and < 0 on error. |
| 398 | */ |
| 399 | static int bpf_obj_name_cpy(char *dst, const char *src) |
| 400 | { |
| 401 | const char *end = src + BPF_OBJ_NAME_LEN; |
| 402 | |
Martin KaFai Lau | 473d973 | 2017-10-05 21:52:11 -0700 | [diff] [blame] | 403 | memset(dst, 0, BPF_OBJ_NAME_LEN); |
| 404 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 405 | /* Copy all isalnum() and '_' char */ |
| 406 | while (src < end && *src) { |
| 407 | if (!isalnum(*src) && *src != '_') |
| 408 | return -EINVAL; |
| 409 | *dst++ = *src++; |
| 410 | } |
| 411 | |
| 412 | /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */ |
| 413 | if (src == end) |
| 414 | return -EINVAL; |
| 415 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 416 | return 0; |
| 417 | } |
| 418 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 419 | #define BPF_MAP_CREATE_LAST_FIELD map_ifindex |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 420 | /* called via syscall */ |
| 421 | static int map_create(union bpf_attr *attr) |
| 422 | { |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 423 | int numa_node = bpf_map_attr_numa_node(attr); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 424 | struct bpf_map *map; |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 425 | int f_flags; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 426 | int err; |
| 427 | |
| 428 | err = CHECK_ATTR(BPF_MAP_CREATE); |
| 429 | if (err) |
| 430 | return -EINVAL; |
| 431 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 432 | f_flags = bpf_get_file_flag(attr->map_flags); |
| 433 | if (f_flags < 0) |
| 434 | return f_flags; |
| 435 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 436 | if (numa_node != NUMA_NO_NODE && |
Eric Dumazet | 96e5ae4 | 2017-09-04 22:41:02 -0700 | [diff] [blame] | 437 | ((unsigned int)numa_node >= nr_node_ids || |
| 438 | !node_online(numa_node))) |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 439 | return -EINVAL; |
| 440 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 441 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ |
| 442 | map = find_and_alloc_map(attr); |
| 443 | if (IS_ERR(map)) |
| 444 | return PTR_ERR(map); |
| 445 | |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 446 | err = bpf_obj_name_cpy(map->name, attr->map_name); |
| 447 | if (err) |
| 448 | goto free_map_nouncharge; |
| 449 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 450 | atomic_set(&map->refcnt, 1); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 451 | atomic_set(&map->usercnt, 1); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 452 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 453 | err = security_bpf_map_alloc(map); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 454 | if (err) |
Daniel Borkmann | 20b2b24 | 2016-11-04 00:56:31 +0100 | [diff] [blame] | 455 | goto free_map_nouncharge; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 456 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 457 | err = bpf_map_charge_memlock(map); |
| 458 | if (err) |
| 459 | goto free_map_sec; |
| 460 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 461 | err = bpf_map_alloc_id(map); |
| 462 | if (err) |
| 463 | goto free_map; |
| 464 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 465 | err = bpf_map_new_fd(map, f_flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 466 | if (err < 0) { |
| 467 | /* failed to allocate fd. |
| 468 | * bpf_map_put() is needed because the above |
| 469 | * bpf_map_alloc_id() has published the map |
| 470 | * to the userspace and the userspace may |
| 471 | * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. |
| 472 | */ |
| 473 | bpf_map_put(map); |
| 474 | return err; |
| 475 | } |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 476 | |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 477 | trace_bpf_map_create(map, err); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 478 | return err; |
| 479 | |
| 480 | free_map: |
Daniel Borkmann | 20b2b24 | 2016-11-04 00:56:31 +0100 | [diff] [blame] | 481 | bpf_map_uncharge_memlock(map); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 482 | free_map_sec: |
| 483 | security_bpf_map_free(map); |
Daniel Borkmann | 20b2b24 | 2016-11-04 00:56:31 +0100 | [diff] [blame] | 484 | free_map_nouncharge: |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 485 | map->ops->map_free(map); |
| 486 | return err; |
| 487 | } |
| 488 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 489 | /* if error is returned, fd is released. |
| 490 | * On success caller should complete fd access with matching fdput() |
| 491 | */ |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 492 | struct bpf_map *__bpf_map_get(struct fd f) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 493 | { |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 494 | if (!f.file) |
| 495 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 496 | if (f.file->f_op != &bpf_map_fops) { |
| 497 | fdput(f); |
| 498 | return ERR_PTR(-EINVAL); |
| 499 | } |
| 500 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 501 | return f.file->private_data; |
| 502 | } |
| 503 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 504 | /* prog's and map's refcnt limit */ |
| 505 | #define BPF_MAX_REFCNT 32768 |
| 506 | |
| 507 | struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref) |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 508 | { |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 509 | if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) { |
| 510 | atomic_dec(&map->refcnt); |
| 511 | return ERR_PTR(-EBUSY); |
| 512 | } |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 513 | if (uref) |
| 514 | atomic_inc(&map->usercnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 515 | return map; |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 519 | { |
| 520 | struct fd f = fdget(ufd); |
| 521 | struct bpf_map *map; |
| 522 | |
| 523 | map = __bpf_map_get(f); |
| 524 | if (IS_ERR(map)) |
| 525 | return map; |
| 526 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 527 | map = bpf_map_inc(map, true); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 528 | fdput(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 529 | |
| 530 | return map; |
| 531 | } |
| 532 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 533 | /* map_idr_lock should have been held */ |
| 534 | static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, |
| 535 | bool uref) |
| 536 | { |
| 537 | int refold; |
| 538 | |
| 539 | refold = __atomic_add_unless(&map->refcnt, 1, 0); |
| 540 | |
| 541 | if (refold >= BPF_MAX_REFCNT) { |
| 542 | __bpf_map_put(map, false); |
| 543 | return ERR_PTR(-EBUSY); |
| 544 | } |
| 545 | |
| 546 | if (!refold) |
| 547 | return ERR_PTR(-ENOENT); |
| 548 | |
| 549 | if (uref) |
| 550 | atomic_inc(&map->usercnt); |
| 551 | |
| 552 | return map; |
| 553 | } |
| 554 | |
Alexei Starovoitov | b8cdc05 | 2016-03-09 18:56:49 -0800 | [diff] [blame] | 555 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
| 556 | { |
| 557 | return -ENOTSUPP; |
| 558 | } |
| 559 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 560 | /* last field in 'union bpf_attr' used by this command */ |
| 561 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value |
| 562 | |
| 563 | static int map_lookup_elem(union bpf_attr *attr) |
| 564 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 565 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 566 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 567 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 568 | struct bpf_map *map; |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 569 | void *key, *value, *ptr; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 570 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 571 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 572 | int err; |
| 573 | |
| 574 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) |
| 575 | return -EINVAL; |
| 576 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 577 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 578 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 579 | if (IS_ERR(map)) |
| 580 | return PTR_ERR(map); |
| 581 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 582 | if (!(f.file->f_mode & FMODE_CAN_READ)) { |
| 583 | err = -EPERM; |
| 584 | goto err_put; |
| 585 | } |
| 586 | |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 587 | key = memdup_user(ukey, map->key_size); |
| 588 | if (IS_ERR(key)) { |
| 589 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 590 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 591 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 592 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 593 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 594 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 595 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
| 596 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 597 | else if (IS_FD_MAP(map)) |
| 598 | value_size = sizeof(u32); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 599 | else |
| 600 | value_size = map->value_size; |
| 601 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 602 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 603 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 604 | if (!value) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 605 | goto free_key; |
| 606 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 607 | if (bpf_map_is_dev_bound(map)) { |
| 608 | err = bpf_map_offload_lookup_elem(map, key, value); |
| 609 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 610 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 611 | err = bpf_percpu_hash_copy(map, key, value); |
| 612 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 613 | err = bpf_percpu_array_copy(map, key, value); |
Alexei Starovoitov | 557c0c6 | 2016-03-07 21:57:17 -0800 | [diff] [blame] | 614 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
| 615 | err = bpf_stackmap_copy(map, key, value); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 616 | } else if (IS_FD_ARRAY(map)) { |
| 617 | err = bpf_fd_array_map_lookup_elem(map, key, value); |
| 618 | } else if (IS_FD_HASH(map)) { |
| 619 | err = bpf_fd_htab_map_lookup_elem(map, key, value); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 620 | } else { |
| 621 | rcu_read_lock(); |
| 622 | ptr = map->ops->map_lookup_elem(map, key); |
| 623 | if (ptr) |
| 624 | memcpy(value, ptr, value_size); |
| 625 | rcu_read_unlock(); |
| 626 | err = ptr ? 0 : -ENOENT; |
| 627 | } |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 628 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 629 | if (err) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 630 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 631 | |
| 632 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 633 | if (copy_to_user(uvalue, value, value_size) != 0) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 634 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 635 | |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 636 | trace_bpf_map_lookup_elem(map, ufd, key, value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 637 | err = 0; |
| 638 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 639 | free_value: |
| 640 | kfree(value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 641 | free_key: |
| 642 | kfree(key); |
| 643 | err_put: |
| 644 | fdput(f); |
| 645 | return err; |
| 646 | } |
| 647 | |
Alexei Starovoitov | 3274f52 | 2014-11-13 17:36:44 -0800 | [diff] [blame] | 648 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 649 | |
| 650 | static int map_update_elem(union bpf_attr *attr) |
| 651 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 652 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 653 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 654 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 655 | struct bpf_map *map; |
| 656 | void *key, *value; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 657 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 658 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 659 | int err; |
| 660 | |
| 661 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) |
| 662 | return -EINVAL; |
| 663 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 664 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 665 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 666 | if (IS_ERR(map)) |
| 667 | return PTR_ERR(map); |
| 668 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 669 | if (!(f.file->f_mode & FMODE_CAN_WRITE)) { |
| 670 | err = -EPERM; |
| 671 | goto err_put; |
| 672 | } |
| 673 | |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 674 | key = memdup_user(ukey, map->key_size); |
| 675 | if (IS_ERR(key)) { |
| 676 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 677 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 678 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 679 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 680 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 681 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 682 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
| 683 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
| 684 | else |
| 685 | value_size = map->value_size; |
| 686 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 687 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 688 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 689 | if (!value) |
| 690 | goto free_key; |
| 691 | |
| 692 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 693 | if (copy_from_user(value, uvalue, value_size) != 0) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 694 | goto free_value; |
| 695 | |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 696 | /* Need to create a kthread, thus must support schedule */ |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 697 | if (bpf_map_is_dev_bound(map)) { |
| 698 | err = bpf_map_offload_update_elem(map, key, value, attr->flags); |
| 699 | goto out; |
| 700 | } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) { |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 701 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
| 702 | goto out; |
| 703 | } |
| 704 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 705 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering from |
| 706 | * inside bpf map update or delete otherwise deadlocks are possible |
| 707 | */ |
| 708 | preempt_disable(); |
| 709 | __this_cpu_inc(bpf_prog_active); |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 710 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 711 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 712 | err = bpf_percpu_hash_update(map, key, value, attr->flags); |
| 713 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 714 | err = bpf_percpu_array_update(map, key, value, attr->flags); |
Mickaël Salaün | 9c147b5 | 2018-01-26 00:54:02 +0100 | [diff] [blame] | 715 | } else if (IS_FD_ARRAY(map)) { |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 716 | rcu_read_lock(); |
| 717 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, |
| 718 | attr->flags); |
| 719 | rcu_read_unlock(); |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 720 | } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
| 721 | rcu_read_lock(); |
| 722 | err = bpf_fd_htab_map_update_elem(map, f.file, key, value, |
| 723 | attr->flags); |
| 724 | rcu_read_unlock(); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 725 | } else { |
| 726 | rcu_read_lock(); |
| 727 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
| 728 | rcu_read_unlock(); |
| 729 | } |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 730 | __this_cpu_dec(bpf_prog_active); |
| 731 | preempt_enable(); |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 732 | out: |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 733 | if (!err) |
| 734 | trace_bpf_map_update_elem(map, ufd, key, value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 735 | free_value: |
| 736 | kfree(value); |
| 737 | free_key: |
| 738 | kfree(key); |
| 739 | err_put: |
| 740 | fdput(f); |
| 741 | return err; |
| 742 | } |
| 743 | |
| 744 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key |
| 745 | |
| 746 | static int map_delete_elem(union bpf_attr *attr) |
| 747 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 748 | void __user *ukey = u64_to_user_ptr(attr->key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 749 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 750 | struct bpf_map *map; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 751 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 752 | void *key; |
| 753 | int err; |
| 754 | |
| 755 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) |
| 756 | return -EINVAL; |
| 757 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 758 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 759 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 760 | if (IS_ERR(map)) |
| 761 | return PTR_ERR(map); |
| 762 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 763 | if (!(f.file->f_mode & FMODE_CAN_WRITE)) { |
| 764 | err = -EPERM; |
| 765 | goto err_put; |
| 766 | } |
| 767 | |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 768 | key = memdup_user(ukey, map->key_size); |
| 769 | if (IS_ERR(key)) { |
| 770 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 771 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 772 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 773 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 774 | if (bpf_map_is_dev_bound(map)) { |
| 775 | err = bpf_map_offload_delete_elem(map, key); |
| 776 | goto out; |
| 777 | } |
| 778 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 779 | preempt_disable(); |
| 780 | __this_cpu_inc(bpf_prog_active); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 781 | rcu_read_lock(); |
| 782 | err = map->ops->map_delete_elem(map, key); |
| 783 | rcu_read_unlock(); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 784 | __this_cpu_dec(bpf_prog_active); |
| 785 | preempt_enable(); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 786 | out: |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 787 | if (!err) |
| 788 | trace_bpf_map_delete_elem(map, ufd, key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 789 | kfree(key); |
| 790 | err_put: |
| 791 | fdput(f); |
| 792 | return err; |
| 793 | } |
| 794 | |
| 795 | /* last field in 'union bpf_attr' used by this command */ |
| 796 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key |
| 797 | |
| 798 | static int map_get_next_key(union bpf_attr *attr) |
| 799 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 800 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 801 | void __user *unext_key = u64_to_user_ptr(attr->next_key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 802 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 803 | struct bpf_map *map; |
| 804 | void *key, *next_key; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 805 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 806 | int err; |
| 807 | |
| 808 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) |
| 809 | return -EINVAL; |
| 810 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 811 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 812 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 813 | if (IS_ERR(map)) |
| 814 | return PTR_ERR(map); |
| 815 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 816 | if (!(f.file->f_mode & FMODE_CAN_READ)) { |
| 817 | err = -EPERM; |
| 818 | goto err_put; |
| 819 | } |
| 820 | |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 821 | if (ukey) { |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 822 | key = memdup_user(ukey, map->key_size); |
| 823 | if (IS_ERR(key)) { |
| 824 | err = PTR_ERR(key); |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 825 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 826 | } |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 827 | } else { |
| 828 | key = NULL; |
| 829 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 830 | |
| 831 | err = -ENOMEM; |
| 832 | next_key = kmalloc(map->key_size, GFP_USER); |
| 833 | if (!next_key) |
| 834 | goto free_key; |
| 835 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 836 | if (bpf_map_is_dev_bound(map)) { |
| 837 | err = bpf_map_offload_get_next_key(map, key, next_key); |
| 838 | goto out; |
| 839 | } |
| 840 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 841 | rcu_read_lock(); |
| 842 | err = map->ops->map_get_next_key(map, key, next_key); |
| 843 | rcu_read_unlock(); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 844 | out: |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 845 | if (err) |
| 846 | goto free_next_key; |
| 847 | |
| 848 | err = -EFAULT; |
| 849 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) |
| 850 | goto free_next_key; |
| 851 | |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 852 | trace_bpf_map_next_key(map, ufd, key, next_key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 853 | err = 0; |
| 854 | |
| 855 | free_next_key: |
| 856 | kfree(next_key); |
| 857 | free_key: |
| 858 | kfree(key); |
| 859 | err_put: |
| 860 | fdput(f); |
| 861 | return err; |
| 862 | } |
| 863 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 864 | static const struct bpf_prog_ops * const bpf_prog_types[] = { |
| 865 | #define BPF_PROG_TYPE(_id, _name) \ |
| 866 | [_id] = & _name ## _prog_ops, |
| 867 | #define BPF_MAP_TYPE(_id, _ops) |
| 868 | #include <linux/bpf_types.h> |
| 869 | #undef BPF_PROG_TYPE |
| 870 | #undef BPF_MAP_TYPE |
| 871 | }; |
| 872 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 873 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
| 874 | { |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 875 | if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type]) |
| 876 | return -EINVAL; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 877 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 878 | if (!bpf_prog_is_dev_bound(prog->aux)) |
| 879 | prog->aux->ops = bpf_prog_types[type]; |
| 880 | else |
| 881 | prog->aux->ops = &bpf_offload_prog_ops; |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 882 | prog->type = type; |
| 883 | return 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /* drop refcnt on maps used by eBPF program and free auxilary data */ |
| 887 | static void free_used_maps(struct bpf_prog_aux *aux) |
| 888 | { |
| 889 | int i; |
| 890 | |
| 891 | for (i = 0; i < aux->used_map_cnt; i++) |
| 892 | bpf_map_put(aux->used_maps[i]); |
| 893 | |
| 894 | kfree(aux->used_maps); |
| 895 | } |
| 896 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 897 | int __bpf_prog_charge(struct user_struct *user, u32 pages) |
| 898 | { |
| 899 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 900 | unsigned long user_bufs; |
| 901 | |
| 902 | if (user) { |
| 903 | user_bufs = atomic_long_add_return(pages, &user->locked_vm); |
| 904 | if (user_bufs > memlock_limit) { |
| 905 | atomic_long_sub(pages, &user->locked_vm); |
| 906 | return -EPERM; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | void __bpf_prog_uncharge(struct user_struct *user, u32 pages) |
| 914 | { |
| 915 | if (user) |
| 916 | atomic_long_sub(pages, &user->locked_vm); |
| 917 | } |
| 918 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 919 | static int bpf_prog_charge_memlock(struct bpf_prog *prog) |
| 920 | { |
| 921 | struct user_struct *user = get_current_user(); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 922 | int ret; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 923 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 924 | ret = __bpf_prog_charge(user, prog->pages); |
| 925 | if (ret) { |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 926 | free_uid(user); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 927 | return ret; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 928 | } |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 929 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 930 | prog->aux->user = user; |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) |
| 935 | { |
| 936 | struct user_struct *user = prog->aux->user; |
| 937 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 938 | __bpf_prog_uncharge(user, prog->pages); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 939 | free_uid(user); |
| 940 | } |
| 941 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 942 | static int bpf_prog_alloc_id(struct bpf_prog *prog) |
| 943 | { |
| 944 | int id; |
| 945 | |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 946 | idr_preload(GFP_KERNEL); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 947 | spin_lock_bh(&prog_idr_lock); |
| 948 | id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); |
| 949 | if (id > 0) |
| 950 | prog->aux->id = id; |
| 951 | spin_unlock_bh(&prog_idr_lock); |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 952 | idr_preload_end(); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 953 | |
| 954 | /* id is in [1, INT_MAX) */ |
| 955 | if (WARN_ON_ONCE(!id)) |
| 956 | return -ENOSPC; |
| 957 | |
| 958 | return id > 0 ? 0 : id; |
| 959 | } |
| 960 | |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 961 | 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] | 962 | { |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 963 | /* cBPF to eBPF migrations are currently not in the idr store. |
| 964 | * Offloaded programs are removed from the store when their device |
| 965 | * disappears - even if someone grabs an fd to them they are unusable, |
| 966 | * simply waiting for refcnt to drop to be freed. |
| 967 | */ |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 968 | if (!prog->aux->id) |
| 969 | return; |
| 970 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 971 | if (do_idr_lock) |
| 972 | spin_lock_bh(&prog_idr_lock); |
| 973 | else |
| 974 | __acquire(&prog_idr_lock); |
| 975 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 976 | idr_remove(&prog_idr, prog->aux->id); |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 977 | prog->aux->id = 0; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 978 | |
| 979 | if (do_idr_lock) |
| 980 | spin_unlock_bh(&prog_idr_lock); |
| 981 | else |
| 982 | __release(&prog_idr_lock); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 985 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 986 | { |
| 987 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); |
| 988 | |
| 989 | free_used_maps(aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 990 | bpf_prog_uncharge_memlock(aux->prog); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 991 | security_bpf_prog_free(aux); |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 992 | bpf_prog_free(aux->prog); |
| 993 | } |
| 994 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 995 | 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] | 996 | { |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 997 | if (atomic_dec_and_test(&prog->aux->refcnt)) { |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 998 | int i; |
| 999 | |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1000 | trace_bpf_prog_put_rcu(prog); |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 1001 | /* bpf_prog_free_id() must be called first */ |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1002 | bpf_prog_free_id(prog, do_idr_lock); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 1003 | |
| 1004 | for (i = 0; i < prog->aux->func_cnt; i++) |
| 1005 | bpf_prog_kallsyms_del(prog->aux->func[i]); |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 1006 | bpf_prog_kallsyms_del(prog); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 1007 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1008 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1009 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1010 | } |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1011 | |
| 1012 | void bpf_prog_put(struct bpf_prog *prog) |
| 1013 | { |
| 1014 | __bpf_prog_put(prog, true); |
| 1015 | } |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 1016 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1017 | |
| 1018 | static int bpf_prog_release(struct inode *inode, struct file *filp) |
| 1019 | { |
| 1020 | struct bpf_prog *prog = filp->private_data; |
| 1021 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1022 | bpf_prog_put(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1023 | return 0; |
| 1024 | } |
| 1025 | |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1026 | #ifdef CONFIG_PROC_FS |
| 1027 | static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) |
| 1028 | { |
| 1029 | const struct bpf_prog *prog = filp->private_data; |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1030 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1031 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1032 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1033 | seq_printf(m, |
| 1034 | "prog_type:\t%u\n" |
| 1035 | "prog_jited:\t%u\n" |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1036 | "prog_tag:\t%s\n" |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1037 | "memlock:\t%llu\n", |
| 1038 | prog->type, |
| 1039 | prog->jited, |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1040 | prog_tag, |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1041 | prog->pages * 1ULL << PAGE_SHIFT); |
| 1042 | } |
| 1043 | #endif |
| 1044 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1045 | const struct file_operations bpf_prog_fops = { |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1046 | #ifdef CONFIG_PROC_FS |
| 1047 | .show_fdinfo = bpf_prog_show_fdinfo, |
| 1048 | #endif |
| 1049 | .release = bpf_prog_release, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1050 | .read = bpf_dummy_read, |
| 1051 | .write = bpf_dummy_write, |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1052 | }; |
| 1053 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1054 | int bpf_prog_new_fd(struct bpf_prog *prog) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1055 | { |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1056 | int ret; |
| 1057 | |
| 1058 | ret = security_bpf_prog(prog); |
| 1059 | if (ret < 0) |
| 1060 | return ret; |
| 1061 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1062 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
| 1063 | O_RDWR | O_CLOEXEC); |
| 1064 | } |
| 1065 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1066 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1067 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1068 | if (!f.file) |
| 1069 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1070 | if (f.file->f_op != &bpf_prog_fops) { |
| 1071 | fdput(f); |
| 1072 | return ERR_PTR(-EINVAL); |
| 1073 | } |
| 1074 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1075 | return f.file->private_data; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1078 | struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i) |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1079 | { |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1080 | if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) { |
| 1081 | atomic_sub(i, &prog->aux->refcnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1082 | return ERR_PTR(-EBUSY); |
| 1083 | } |
| 1084 | return prog; |
| 1085 | } |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1086 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
| 1087 | |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 1088 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
| 1089 | { |
| 1090 | /* Only to be used for undoing previous bpf_prog_add() in some |
| 1091 | * error path. We still know that another entity in our call |
| 1092 | * path holds a reference to the program, thus atomic_sub() can |
| 1093 | * be safely used in such cases! |
| 1094 | */ |
| 1095 | WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0); |
| 1096 | } |
| 1097 | EXPORT_SYMBOL_GPL(bpf_prog_sub); |
| 1098 | |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1099 | struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog) |
| 1100 | { |
| 1101 | return bpf_prog_add(prog, 1); |
| 1102 | } |
Daniel Borkmann | 97bc402 | 2016-11-19 01:45:00 +0100 | [diff] [blame] | 1103 | EXPORT_SYMBOL_GPL(bpf_prog_inc); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1104 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1105 | /* prog_idr_lock should have been held */ |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1106 | 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] | 1107 | { |
| 1108 | int refold; |
| 1109 | |
| 1110 | refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0); |
| 1111 | |
| 1112 | if (refold >= BPF_MAX_REFCNT) { |
| 1113 | __bpf_prog_put(prog, false); |
| 1114 | return ERR_PTR(-EBUSY); |
| 1115 | } |
| 1116 | |
| 1117 | if (!refold) |
| 1118 | return ERR_PTR(-ENOENT); |
| 1119 | |
| 1120 | return prog; |
| 1121 | } |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1122 | EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1123 | |
Al Viro | 040ee69 | 2017-12-02 20:20:38 -0500 | [diff] [blame] | 1124 | bool bpf_prog_get_ok(struct bpf_prog *prog, |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1125 | enum bpf_prog_type *attach_type, bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1126 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1127 | /* not an attachment, just a refcount inc, always allow */ |
| 1128 | if (!attach_type) |
| 1129 | return true; |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1130 | |
| 1131 | if (prog->type != *attach_type) |
| 1132 | return false; |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1133 | if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1134 | return false; |
| 1135 | |
| 1136 | return true; |
| 1137 | } |
| 1138 | |
| 1139 | 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] | 1140 | bool attach_drv) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1141 | { |
| 1142 | struct fd f = fdget(ufd); |
| 1143 | struct bpf_prog *prog; |
| 1144 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1145 | prog = ____bpf_prog_get(f); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1146 | if (IS_ERR(prog)) |
| 1147 | return prog; |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1148 | if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1149 | prog = ERR_PTR(-EINVAL); |
| 1150 | goto out; |
| 1151 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1152 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1153 | prog = bpf_prog_inc(prog); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1154 | out: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1155 | fdput(f); |
| 1156 | return prog; |
| 1157 | } |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1158 | |
| 1159 | struct bpf_prog *bpf_prog_get(u32 ufd) |
| 1160 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1161 | return __bpf_prog_get(ufd, NULL, false); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1162 | } |
| 1163 | |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1164 | 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] | 1165 | bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1166 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1167 | struct bpf_prog *prog = __bpf_prog_get(ufd, &type, attach_drv); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1168 | |
| 1169 | if (!IS_ERR(prog)) |
| 1170 | trace_bpf_prog_get_type(prog); |
| 1171 | return prog; |
| 1172 | } |
Jakub Kicinski | 6c8dfe2 | 2017-11-03 13:56:21 -0700 | [diff] [blame] | 1173 | EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1174 | |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1175 | /* Initially all BPF programs could be loaded w/o specifying |
| 1176 | * expected_attach_type. Later for some of them specifying expected_attach_type |
| 1177 | * at load time became required so that program could be validated properly. |
| 1178 | * Programs of types that are allowed to be loaded both w/ and w/o (for |
| 1179 | * backward compatibility) expected_attach_type, should have the default attach |
| 1180 | * type assigned to expected_attach_type for the latter case, so that it can be |
| 1181 | * validated later at attach time. |
| 1182 | * |
| 1183 | * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if |
| 1184 | * prog type requires it but has some attach types that have to be backward |
| 1185 | * compatible. |
| 1186 | */ |
| 1187 | static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) |
| 1188 | { |
| 1189 | switch (attr->prog_type) { |
| 1190 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 1191 | /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't |
| 1192 | * exist so checking for non-zero is the way to go here. |
| 1193 | */ |
| 1194 | if (!attr->expected_attach_type) |
| 1195 | attr->expected_attach_type = |
| 1196 | BPF_CGROUP_INET_SOCK_CREATE; |
| 1197 | break; |
| 1198 | } |
| 1199 | } |
| 1200 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1201 | static int |
| 1202 | bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type, |
| 1203 | enum bpf_attach_type expected_attach_type) |
| 1204 | { |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1205 | switch (prog_type) { |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1206 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 1207 | switch (expected_attach_type) { |
| 1208 | case BPF_CGROUP_INET_SOCK_CREATE: |
| 1209 | case BPF_CGROUP_INET4_POST_BIND: |
| 1210 | case BPF_CGROUP_INET6_POST_BIND: |
| 1211 | return 0; |
| 1212 | default: |
| 1213 | return -EINVAL; |
| 1214 | } |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1215 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 1216 | switch (expected_attach_type) { |
| 1217 | case BPF_CGROUP_INET4_BIND: |
| 1218 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 1219 | case BPF_CGROUP_INET4_CONNECT: |
| 1220 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1221 | return 0; |
| 1222 | default: |
| 1223 | return -EINVAL; |
| 1224 | } |
| 1225 | default: |
| 1226 | return 0; |
| 1227 | } |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1230 | /* last field in 'union bpf_attr' used by this command */ |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1231 | #define BPF_PROG_LOAD_LAST_FIELD expected_attach_type |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1232 | |
| 1233 | static int bpf_prog_load(union bpf_attr *attr) |
| 1234 | { |
| 1235 | enum bpf_prog_type type = attr->prog_type; |
| 1236 | struct bpf_prog *prog; |
| 1237 | int err; |
| 1238 | char license[128]; |
| 1239 | bool is_gpl; |
| 1240 | |
| 1241 | if (CHECK_ATTR(BPF_PROG_LOAD)) |
| 1242 | return -EINVAL; |
| 1243 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1244 | if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT) |
| 1245 | return -EINVAL; |
| 1246 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1247 | /* copy eBPF program license from user space */ |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1248 | if (strncpy_from_user(license, u64_to_user_ptr(attr->license), |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1249 | sizeof(license) - 1) < 0) |
| 1250 | return -EFAULT; |
| 1251 | license[sizeof(license) - 1] = 0; |
| 1252 | |
| 1253 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
| 1254 | is_gpl = license_is_gpl_compatible(license); |
| 1255 | |
Daniel Borkmann | ef0915c | 2016-12-07 01:15:44 +0100 | [diff] [blame] | 1256 | if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS) |
| 1257 | return -E2BIG; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1258 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 1259 | if (type == BPF_PROG_TYPE_KPROBE && |
| 1260 | attr->kern_version != LINUX_VERSION_CODE) |
| 1261 | return -EINVAL; |
| 1262 | |
Chenbo Feng | 80b7d81 | 2017-05-31 18:16:00 -0700 | [diff] [blame] | 1263 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && |
| 1264 | type != BPF_PROG_TYPE_CGROUP_SKB && |
| 1265 | !capable(CAP_SYS_ADMIN)) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1266 | return -EPERM; |
| 1267 | |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1268 | bpf_prog_load_fixup_attach_type(attr); |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1269 | if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type)) |
| 1270 | return -EINVAL; |
| 1271 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1272 | /* plain bpf_prog allocation */ |
| 1273 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); |
| 1274 | if (!prog) |
| 1275 | return -ENOMEM; |
| 1276 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1277 | prog->expected_attach_type = attr->expected_attach_type; |
| 1278 | |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 1279 | prog->aux->offload_requested = !!attr->prog_ifindex; |
| 1280 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1281 | err = security_bpf_prog_alloc(prog->aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1282 | if (err) |
| 1283 | goto free_prog_nouncharge; |
| 1284 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1285 | err = bpf_prog_charge_memlock(prog); |
| 1286 | if (err) |
| 1287 | goto free_prog_sec; |
| 1288 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1289 | prog->len = attr->insn_cnt; |
| 1290 | |
| 1291 | err = -EFAULT; |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1292 | if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 1293 | bpf_prog_insn_size(prog)) != 0) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1294 | goto free_prog; |
| 1295 | |
| 1296 | prog->orig_prog = NULL; |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1297 | prog->jited = 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1298 | |
| 1299 | atomic_set(&prog->aux->refcnt, 1); |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1300 | prog->gpl_compatible = is_gpl ? 1 : 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1301 | |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 1302 | if (bpf_prog_is_dev_bound(prog->aux)) { |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1303 | err = bpf_prog_offload_init(prog, attr); |
| 1304 | if (err) |
| 1305 | goto free_prog; |
| 1306 | } |
| 1307 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1308 | /* find program type: socket_filter vs tracing_filter */ |
| 1309 | err = find_prog_type(type, prog); |
| 1310 | if (err < 0) |
| 1311 | goto free_prog; |
| 1312 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 1313 | prog->aux->load_time = ktime_get_boot_ns(); |
| 1314 | err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name); |
| 1315 | if (err) |
| 1316 | goto free_prog; |
| 1317 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1318 | /* run eBPF verifier */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 1319 | err = bpf_check(&prog, attr); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1320 | if (err < 0) |
| 1321 | goto free_used_maps; |
| 1322 | |
| 1323 | /* eBPF program is ready to be JITed */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 1324 | if (!prog->bpf_func) |
| 1325 | prog = bpf_prog_select_runtime(prog, &err); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1326 | if (err < 0) |
| 1327 | goto free_used_maps; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1328 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1329 | err = bpf_prog_alloc_id(prog); |
| 1330 | if (err) |
| 1331 | goto free_used_maps; |
| 1332 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1333 | err = bpf_prog_new_fd(prog); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1334 | if (err < 0) { |
| 1335 | /* failed to allocate fd. |
| 1336 | * bpf_prog_put() is needed because the above |
| 1337 | * bpf_prog_alloc_id() has published the prog |
| 1338 | * to the userspace and the userspace may |
| 1339 | * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID. |
| 1340 | */ |
| 1341 | bpf_prog_put(prog); |
| 1342 | return err; |
| 1343 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1344 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 1345 | bpf_prog_kallsyms_add(prog); |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1346 | trace_bpf_prog_load(prog, err); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1347 | return err; |
| 1348 | |
| 1349 | free_used_maps: |
| 1350 | free_used_maps(prog->aux); |
| 1351 | free_prog: |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1352 | bpf_prog_uncharge_memlock(prog); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1353 | free_prog_sec: |
| 1354 | security_bpf_prog_free(prog->aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1355 | free_prog_nouncharge: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1356 | bpf_prog_free(prog); |
| 1357 | return err; |
| 1358 | } |
| 1359 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1360 | #define BPF_OBJ_LAST_FIELD file_flags |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1361 | |
| 1362 | static int bpf_obj_pin(const union bpf_attr *attr) |
| 1363 | { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1364 | if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1365 | return -EINVAL; |
| 1366 | |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1367 | 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] | 1368 | } |
| 1369 | |
| 1370 | static int bpf_obj_get(const union bpf_attr *attr) |
| 1371 | { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1372 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || |
| 1373 | attr->file_flags & ~BPF_OBJ_FLAG_MASK) |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1374 | return -EINVAL; |
| 1375 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1376 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), |
| 1377 | attr->file_flags); |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1378 | } |
| 1379 | |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1380 | struct bpf_raw_tracepoint { |
| 1381 | struct bpf_raw_event_map *btp; |
| 1382 | struct bpf_prog *prog; |
| 1383 | }; |
| 1384 | |
| 1385 | static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp) |
| 1386 | { |
| 1387 | struct bpf_raw_tracepoint *raw_tp = filp->private_data; |
| 1388 | |
| 1389 | if (raw_tp->prog) { |
| 1390 | bpf_probe_unregister(raw_tp->btp, raw_tp->prog); |
| 1391 | bpf_prog_put(raw_tp->prog); |
| 1392 | } |
| 1393 | kfree(raw_tp); |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
| 1397 | static const struct file_operations bpf_raw_tp_fops = { |
| 1398 | .release = bpf_raw_tracepoint_release, |
| 1399 | .read = bpf_dummy_read, |
| 1400 | .write = bpf_dummy_write, |
| 1401 | }; |
| 1402 | |
| 1403 | #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd |
| 1404 | |
| 1405 | static int bpf_raw_tracepoint_open(const union bpf_attr *attr) |
| 1406 | { |
| 1407 | struct bpf_raw_tracepoint *raw_tp; |
| 1408 | struct bpf_raw_event_map *btp; |
| 1409 | struct bpf_prog *prog; |
| 1410 | char tp_name[128]; |
| 1411 | int tp_fd, err; |
| 1412 | |
| 1413 | if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name), |
| 1414 | sizeof(tp_name) - 1) < 0) |
| 1415 | return -EFAULT; |
| 1416 | tp_name[sizeof(tp_name) - 1] = 0; |
| 1417 | |
| 1418 | btp = bpf_find_raw_tracepoint(tp_name); |
| 1419 | if (!btp) |
| 1420 | return -ENOENT; |
| 1421 | |
| 1422 | raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER); |
| 1423 | if (!raw_tp) |
| 1424 | return -ENOMEM; |
| 1425 | raw_tp->btp = btp; |
| 1426 | |
| 1427 | prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd, |
| 1428 | BPF_PROG_TYPE_RAW_TRACEPOINT); |
| 1429 | if (IS_ERR(prog)) { |
| 1430 | err = PTR_ERR(prog); |
| 1431 | goto out_free_tp; |
| 1432 | } |
| 1433 | |
| 1434 | err = bpf_probe_register(raw_tp->btp, prog); |
| 1435 | if (err) |
| 1436 | goto out_put_prog; |
| 1437 | |
| 1438 | raw_tp->prog = prog; |
| 1439 | tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp, |
| 1440 | O_CLOEXEC); |
| 1441 | if (tp_fd < 0) { |
| 1442 | bpf_probe_unregister(raw_tp->btp, prog); |
| 1443 | err = tp_fd; |
| 1444 | goto out_put_prog; |
| 1445 | } |
| 1446 | return tp_fd; |
| 1447 | |
| 1448 | out_put_prog: |
| 1449 | bpf_prog_put(prog); |
| 1450 | out_free_tp: |
| 1451 | kfree(raw_tp); |
| 1452 | return err; |
| 1453 | } |
| 1454 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1455 | #ifdef CONFIG_CGROUP_BPF |
| 1456 | |
Anders Roxell | 3349158 | 2018-04-03 14:09:47 +0200 | [diff] [blame] | 1457 | static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, |
| 1458 | enum bpf_attach_type attach_type) |
| 1459 | { |
| 1460 | switch (prog->type) { |
| 1461 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 1462 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 1463 | return attach_type == prog->expected_attach_type ? 0 : -EINVAL; |
| 1464 | default: |
| 1465 | return 0; |
| 1466 | } |
| 1467 | } |
| 1468 | |
John Fastabend | 464bc0f | 2017-08-28 07:10:04 -0700 | [diff] [blame] | 1469 | #define BPF_PROG_ATTACH_LAST_FIELD attach_flags |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1470 | |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1471 | static int sockmap_get_from_fd(const union bpf_attr *attr, |
| 1472 | int type, bool attach) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1473 | { |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1474 | struct bpf_prog *prog = NULL; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1475 | int ufd = attr->target_fd; |
| 1476 | struct bpf_map *map; |
| 1477 | struct fd f; |
| 1478 | int err; |
| 1479 | |
| 1480 | f = fdget(ufd); |
| 1481 | map = __bpf_map_get(f); |
| 1482 | if (IS_ERR(map)) |
| 1483 | return PTR_ERR(map); |
| 1484 | |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1485 | if (attach) { |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1486 | prog = bpf_prog_get_type(attr->attach_bpf_fd, type); |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1487 | if (IS_ERR(prog)) { |
| 1488 | fdput(f); |
| 1489 | return PTR_ERR(prog); |
| 1490 | } |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1493 | err = sock_map_prog(map, prog, attr->attach_type); |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1494 | if (err) { |
| 1495 | fdput(f); |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1496 | if (prog) |
| 1497 | bpf_prog_put(prog); |
Dan Carpenter | ae2b27b | 2017-08-18 10:27:02 +0300 | [diff] [blame] | 1498 | return err; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | fdput(f); |
Dan Carpenter | ae2b27b | 2017-08-18 10:27:02 +0300 | [diff] [blame] | 1502 | return 0; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1503 | } |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1504 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1505 | #define BPF_F_ATTACH_MASK \ |
| 1506 | (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI) |
| 1507 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1508 | static int bpf_prog_attach(const union bpf_attr *attr) |
| 1509 | { |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1510 | enum bpf_prog_type ptype; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1511 | struct bpf_prog *prog; |
| 1512 | struct cgroup *cgrp; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1513 | int ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1514 | |
| 1515 | if (!capable(CAP_NET_ADMIN)) |
| 1516 | return -EPERM; |
| 1517 | |
| 1518 | if (CHECK_ATTR(BPF_PROG_ATTACH)) |
| 1519 | return -EINVAL; |
| 1520 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1521 | if (attr->attach_flags & ~BPF_F_ATTACH_MASK) |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1522 | return -EINVAL; |
| 1523 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1524 | switch (attr->attach_type) { |
| 1525 | case BPF_CGROUP_INET_INGRESS: |
| 1526 | case BPF_CGROUP_INET_EGRESS: |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1527 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1528 | break; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1529 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1530 | case BPF_CGROUP_INET4_POST_BIND: |
| 1531 | case BPF_CGROUP_INET6_POST_BIND: |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1532 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
| 1533 | break; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1534 | case BPF_CGROUP_INET4_BIND: |
| 1535 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 1536 | case BPF_CGROUP_INET4_CONNECT: |
| 1537 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1538 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
| 1539 | break; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1540 | case BPF_CGROUP_SOCK_OPS: |
| 1541 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
| 1542 | break; |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1543 | case BPF_CGROUP_DEVICE: |
| 1544 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; |
| 1545 | break; |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1546 | case BPF_SK_MSG_VERDICT: |
| 1547 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true); |
John Fastabend | 464bc0f | 2017-08-28 07:10:04 -0700 | [diff] [blame] | 1548 | case BPF_SK_SKB_STREAM_PARSER: |
| 1549 | case BPF_SK_SKB_STREAM_VERDICT: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1550 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true); |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1551 | default: |
| 1552 | return -EINVAL; |
| 1553 | } |
| 1554 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1555 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
| 1556 | if (IS_ERR(prog)) |
| 1557 | return PTR_ERR(prog); |
| 1558 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1559 | if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { |
| 1560 | bpf_prog_put(prog); |
| 1561 | return -EINVAL; |
| 1562 | } |
| 1563 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1564 | cgrp = cgroup_get_from_fd(attr->target_fd); |
| 1565 | if (IS_ERR(cgrp)) { |
| 1566 | bpf_prog_put(prog); |
| 1567 | return PTR_ERR(cgrp); |
| 1568 | } |
| 1569 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1570 | ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type, |
| 1571 | attr->attach_flags); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1572 | if (ret) |
| 1573 | bpf_prog_put(prog); |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1574 | cgroup_put(cgrp); |
| 1575 | |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1576 | return ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | #define BPF_PROG_DETACH_LAST_FIELD attach_type |
| 1580 | |
| 1581 | static int bpf_prog_detach(const union bpf_attr *attr) |
| 1582 | { |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1583 | enum bpf_prog_type ptype; |
| 1584 | struct bpf_prog *prog; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1585 | struct cgroup *cgrp; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1586 | int ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1587 | |
| 1588 | if (!capable(CAP_NET_ADMIN)) |
| 1589 | return -EPERM; |
| 1590 | |
| 1591 | if (CHECK_ATTR(BPF_PROG_DETACH)) |
| 1592 | return -EINVAL; |
| 1593 | |
| 1594 | switch (attr->attach_type) { |
| 1595 | case BPF_CGROUP_INET_INGRESS: |
| 1596 | case BPF_CGROUP_INET_EGRESS: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1597 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
| 1598 | break; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1599 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1600 | case BPF_CGROUP_INET4_POST_BIND: |
| 1601 | case BPF_CGROUP_INET6_POST_BIND: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1602 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
| 1603 | break; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1604 | case BPF_CGROUP_INET4_BIND: |
| 1605 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 1606 | case BPF_CGROUP_INET4_CONNECT: |
| 1607 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1608 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
| 1609 | break; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1610 | case BPF_CGROUP_SOCK_OPS: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1611 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1612 | break; |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1613 | case BPF_CGROUP_DEVICE: |
| 1614 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; |
| 1615 | break; |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1616 | case BPF_SK_MSG_VERDICT: |
| 1617 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false); |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1618 | case BPF_SK_SKB_STREAM_PARSER: |
| 1619 | case BPF_SK_SKB_STREAM_VERDICT: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1620 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false); |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1621 | default: |
| 1622 | return -EINVAL; |
| 1623 | } |
| 1624 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1625 | cgrp = cgroup_get_from_fd(attr->target_fd); |
| 1626 | if (IS_ERR(cgrp)) |
| 1627 | return PTR_ERR(cgrp); |
| 1628 | |
| 1629 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
| 1630 | if (IS_ERR(prog)) |
| 1631 | prog = NULL; |
| 1632 | |
| 1633 | ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0); |
| 1634 | if (prog) |
| 1635 | bpf_prog_put(prog); |
| 1636 | cgroup_put(cgrp); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1637 | return ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1638 | } |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1639 | |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1640 | #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt |
| 1641 | |
| 1642 | static int bpf_prog_query(const union bpf_attr *attr, |
| 1643 | union bpf_attr __user *uattr) |
| 1644 | { |
| 1645 | struct cgroup *cgrp; |
| 1646 | int ret; |
| 1647 | |
| 1648 | if (!capable(CAP_NET_ADMIN)) |
| 1649 | return -EPERM; |
| 1650 | if (CHECK_ATTR(BPF_PROG_QUERY)) |
| 1651 | return -EINVAL; |
| 1652 | if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) |
| 1653 | return -EINVAL; |
| 1654 | |
| 1655 | switch (attr->query.attach_type) { |
| 1656 | case BPF_CGROUP_INET_INGRESS: |
| 1657 | case BPF_CGROUP_INET_EGRESS: |
| 1658 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1659 | case BPF_CGROUP_INET4_BIND: |
| 1660 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1661 | case BPF_CGROUP_INET4_POST_BIND: |
| 1662 | case BPF_CGROUP_INET6_POST_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 1663 | case BPF_CGROUP_INET4_CONNECT: |
| 1664 | case BPF_CGROUP_INET6_CONNECT: |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1665 | case BPF_CGROUP_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1666 | case BPF_CGROUP_DEVICE: |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1667 | break; |
| 1668 | default: |
| 1669 | return -EINVAL; |
| 1670 | } |
| 1671 | cgrp = cgroup_get_from_fd(attr->query.target_fd); |
| 1672 | if (IS_ERR(cgrp)) |
| 1673 | return PTR_ERR(cgrp); |
| 1674 | ret = cgroup_bpf_query(cgrp, attr, uattr); |
| 1675 | cgroup_put(cgrp); |
| 1676 | return ret; |
| 1677 | } |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1678 | #endif /* CONFIG_CGROUP_BPF */ |
| 1679 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 1680 | #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration |
| 1681 | |
| 1682 | static int bpf_prog_test_run(const union bpf_attr *attr, |
| 1683 | union bpf_attr __user *uattr) |
| 1684 | { |
| 1685 | struct bpf_prog *prog; |
| 1686 | int ret = -ENOTSUPP; |
| 1687 | |
Alexei Starovoitov | 61f3c96 | 2018-01-17 16:52:02 -0800 | [diff] [blame] | 1688 | if (!capable(CAP_SYS_ADMIN)) |
| 1689 | return -EPERM; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 1690 | if (CHECK_ATTR(BPF_PROG_TEST_RUN)) |
| 1691 | return -EINVAL; |
| 1692 | |
| 1693 | prog = bpf_prog_get(attr->test.prog_fd); |
| 1694 | if (IS_ERR(prog)) |
| 1695 | return PTR_ERR(prog); |
| 1696 | |
| 1697 | if (prog->aux->ops->test_run) |
| 1698 | ret = prog->aux->ops->test_run(prog, attr, uattr); |
| 1699 | |
| 1700 | bpf_prog_put(prog); |
| 1701 | return ret; |
| 1702 | } |
| 1703 | |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 1704 | #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id |
| 1705 | |
| 1706 | static int bpf_obj_get_next_id(const union bpf_attr *attr, |
| 1707 | union bpf_attr __user *uattr, |
| 1708 | struct idr *idr, |
| 1709 | spinlock_t *lock) |
| 1710 | { |
| 1711 | u32 next_id = attr->start_id; |
| 1712 | int err = 0; |
| 1713 | |
| 1714 | if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) |
| 1715 | return -EINVAL; |
| 1716 | |
| 1717 | if (!capable(CAP_SYS_ADMIN)) |
| 1718 | return -EPERM; |
| 1719 | |
| 1720 | next_id++; |
| 1721 | spin_lock_bh(lock); |
| 1722 | if (!idr_get_next(idr, &next_id)) |
| 1723 | err = -ENOENT; |
| 1724 | spin_unlock_bh(lock); |
| 1725 | |
| 1726 | if (!err) |
| 1727 | err = put_user(next_id, &uattr->next_id); |
| 1728 | |
| 1729 | return err; |
| 1730 | } |
| 1731 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1732 | #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id |
| 1733 | |
| 1734 | static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) |
| 1735 | { |
| 1736 | struct bpf_prog *prog; |
| 1737 | u32 id = attr->prog_id; |
| 1738 | int fd; |
| 1739 | |
| 1740 | if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) |
| 1741 | return -EINVAL; |
| 1742 | |
| 1743 | if (!capable(CAP_SYS_ADMIN)) |
| 1744 | return -EPERM; |
| 1745 | |
| 1746 | spin_lock_bh(&prog_idr_lock); |
| 1747 | prog = idr_find(&prog_idr, id); |
| 1748 | if (prog) |
| 1749 | prog = bpf_prog_inc_not_zero(prog); |
| 1750 | else |
| 1751 | prog = ERR_PTR(-ENOENT); |
| 1752 | spin_unlock_bh(&prog_idr_lock); |
| 1753 | |
| 1754 | if (IS_ERR(prog)) |
| 1755 | return PTR_ERR(prog); |
| 1756 | |
| 1757 | fd = bpf_prog_new_fd(prog); |
| 1758 | if (fd < 0) |
| 1759 | bpf_prog_put(prog); |
| 1760 | |
| 1761 | return fd; |
| 1762 | } |
| 1763 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1764 | #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1765 | |
| 1766 | static int bpf_map_get_fd_by_id(const union bpf_attr *attr) |
| 1767 | { |
| 1768 | struct bpf_map *map; |
| 1769 | u32 id = attr->map_id; |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1770 | int f_flags; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1771 | int fd; |
| 1772 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1773 | if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || |
| 1774 | attr->open_flags & ~BPF_OBJ_FLAG_MASK) |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1775 | return -EINVAL; |
| 1776 | |
| 1777 | if (!capable(CAP_SYS_ADMIN)) |
| 1778 | return -EPERM; |
| 1779 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1780 | f_flags = bpf_get_file_flag(attr->open_flags); |
| 1781 | if (f_flags < 0) |
| 1782 | return f_flags; |
| 1783 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1784 | spin_lock_bh(&map_idr_lock); |
| 1785 | map = idr_find(&map_idr, id); |
| 1786 | if (map) |
| 1787 | map = bpf_map_inc_not_zero(map, true); |
| 1788 | else |
| 1789 | map = ERR_PTR(-ENOENT); |
| 1790 | spin_unlock_bh(&map_idr_lock); |
| 1791 | |
| 1792 | if (IS_ERR(map)) |
| 1793 | return PTR_ERR(map); |
| 1794 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1795 | fd = bpf_map_new_fd(map, f_flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1796 | if (fd < 0) |
| 1797 | bpf_map_put(map); |
| 1798 | |
| 1799 | return fd; |
| 1800 | } |
| 1801 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 1802 | static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, |
| 1803 | unsigned long addr) |
| 1804 | { |
| 1805 | int i; |
| 1806 | |
| 1807 | for (i = 0; i < prog->aux->used_map_cnt; i++) |
| 1808 | if (prog->aux->used_maps[i] == (void *)addr) |
| 1809 | return prog->aux->used_maps[i]; |
| 1810 | return NULL; |
| 1811 | } |
| 1812 | |
| 1813 | static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog) |
| 1814 | { |
| 1815 | const struct bpf_map *map; |
| 1816 | struct bpf_insn *insns; |
| 1817 | u64 imm; |
| 1818 | int i; |
| 1819 | |
| 1820 | insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), |
| 1821 | GFP_USER); |
| 1822 | if (!insns) |
| 1823 | return insns; |
| 1824 | |
| 1825 | for (i = 0; i < prog->len; i++) { |
| 1826 | if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) { |
| 1827 | insns[i].code = BPF_JMP | BPF_CALL; |
| 1828 | insns[i].imm = BPF_FUNC_tail_call; |
| 1829 | /* fall-through */ |
| 1830 | } |
| 1831 | if (insns[i].code == (BPF_JMP | BPF_CALL) || |
| 1832 | insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) { |
| 1833 | if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) |
| 1834 | insns[i].code = BPF_JMP | BPF_CALL; |
| 1835 | if (!bpf_dump_raw_ok()) |
| 1836 | insns[i].imm = 0; |
| 1837 | continue; |
| 1838 | } |
| 1839 | |
| 1840 | if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW)) |
| 1841 | continue; |
| 1842 | |
| 1843 | imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; |
| 1844 | map = bpf_map_from_imm(prog, imm); |
| 1845 | if (map) { |
| 1846 | insns[i].src_reg = BPF_PSEUDO_MAP_FD; |
| 1847 | insns[i].imm = map->id; |
| 1848 | insns[i + 1].imm = 0; |
| 1849 | continue; |
| 1850 | } |
| 1851 | |
| 1852 | if (!bpf_dump_raw_ok() && |
| 1853 | imm == (unsigned long)prog->aux) { |
| 1854 | insns[i].imm = 0; |
| 1855 | insns[i + 1].imm = 0; |
| 1856 | continue; |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | return insns; |
| 1861 | } |
| 1862 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1863 | static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, |
| 1864 | const union bpf_attr *attr, |
| 1865 | union bpf_attr __user *uattr) |
| 1866 | { |
| 1867 | struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 1868 | struct bpf_prog_info info = {}; |
| 1869 | u32 info_len = attr->info.info_len; |
| 1870 | char __user *uinsns; |
| 1871 | u32 ulen; |
| 1872 | int err; |
| 1873 | |
| 1874 | err = check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
| 1875 | if (err) |
| 1876 | return err; |
| 1877 | info_len = min_t(u32, sizeof(info), info_len); |
| 1878 | |
| 1879 | if (copy_from_user(&info, uinfo, info_len)) |
Daniel Borkmann | 89b0968 | 2017-07-27 21:02:46 +0200 | [diff] [blame] | 1880 | return -EFAULT; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1881 | |
| 1882 | info.type = prog->type; |
| 1883 | info.id = prog->aux->id; |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 1884 | info.load_time = prog->aux->load_time; |
| 1885 | info.created_by_uid = from_kuid_munged(current_user_ns(), |
| 1886 | prog->aux->user->uid); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1887 | |
| 1888 | memcpy(info.tag, prog->tag, sizeof(prog->tag)); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 1889 | memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); |
| 1890 | |
| 1891 | ulen = info.nr_map_ids; |
| 1892 | info.nr_map_ids = prog->aux->used_map_cnt; |
| 1893 | ulen = min_t(u32, info.nr_map_ids, ulen); |
| 1894 | if (ulen) { |
Martin KaFai Lau | 721e08d | 2017-09-29 10:52:17 -0700 | [diff] [blame] | 1895 | 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] | 1896 | u32 i; |
| 1897 | |
| 1898 | for (i = 0; i < ulen; i++) |
| 1899 | if (put_user(prog->aux->used_maps[i]->id, |
| 1900 | &user_map_ids[i])) |
| 1901 | return -EFAULT; |
| 1902 | } |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1903 | |
| 1904 | if (!capable(CAP_SYS_ADMIN)) { |
| 1905 | info.jited_prog_len = 0; |
| 1906 | info.xlated_prog_len = 0; |
| 1907 | goto done; |
| 1908 | } |
| 1909 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1910 | ulen = info.xlated_prog_len; |
Daniel Borkmann | 9975a54 | 2017-07-28 17:05:25 +0200 | [diff] [blame] | 1911 | info.xlated_prog_len = bpf_prog_insn_size(prog); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1912 | if (info.xlated_prog_len && ulen) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 1913 | struct bpf_insn *insns_sanitized; |
| 1914 | bool fault; |
| 1915 | |
| 1916 | if (prog->blinded && !bpf_dump_raw_ok()) { |
| 1917 | info.xlated_prog_insns = 0; |
| 1918 | goto done; |
| 1919 | } |
| 1920 | insns_sanitized = bpf_insn_prepare_dump(prog); |
| 1921 | if (!insns_sanitized) |
| 1922 | return -ENOMEM; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1923 | uinsns = u64_to_user_ptr(info.xlated_prog_insns); |
| 1924 | ulen = min_t(u32, info.xlated_prog_len, ulen); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 1925 | fault = copy_to_user(uinsns, insns_sanitized, ulen); |
| 1926 | kfree(insns_sanitized); |
| 1927 | if (fault) |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1928 | return -EFAULT; |
| 1929 | } |
| 1930 | |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 1931 | if (bpf_prog_is_dev_bound(prog->aux)) { |
| 1932 | err = bpf_prog_offload_info_fill(&info, prog); |
| 1933 | if (err) |
| 1934 | return err; |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 1935 | goto done; |
| 1936 | } |
| 1937 | |
| 1938 | /* NOTE: the following code is supposed to be skipped for offload. |
| 1939 | * bpf_prog_offload_info_fill() is the place to fill similar fields |
| 1940 | * for offload. |
| 1941 | */ |
| 1942 | ulen = info.jited_prog_len; |
| 1943 | info.jited_prog_len = prog->jited_len; |
| 1944 | if (info.jited_prog_len && ulen) { |
| 1945 | if (bpf_dump_raw_ok()) { |
| 1946 | uinsns = u64_to_user_ptr(info.jited_prog_insns); |
| 1947 | ulen = min_t(u32, info.jited_prog_len, ulen); |
| 1948 | if (copy_to_user(uinsns, prog->bpf_func, ulen)) |
| 1949 | return -EFAULT; |
| 1950 | } else { |
| 1951 | info.jited_prog_insns = 0; |
| 1952 | } |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 1953 | } |
| 1954 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1955 | done: |
| 1956 | if (copy_to_user(uinfo, &info, info_len) || |
| 1957 | put_user(info_len, &uattr->info.info_len)) |
| 1958 | return -EFAULT; |
| 1959 | |
| 1960 | return 0; |
| 1961 | } |
| 1962 | |
| 1963 | static int bpf_map_get_info_by_fd(struct bpf_map *map, |
| 1964 | const union bpf_attr *attr, |
| 1965 | union bpf_attr __user *uattr) |
| 1966 | { |
| 1967 | struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 1968 | struct bpf_map_info info = {}; |
| 1969 | u32 info_len = attr->info.info_len; |
| 1970 | int err; |
| 1971 | |
| 1972 | err = check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
| 1973 | if (err) |
| 1974 | return err; |
| 1975 | info_len = min_t(u32, sizeof(info), info_len); |
| 1976 | |
| 1977 | info.type = map->map_type; |
| 1978 | info.id = map->id; |
| 1979 | info.key_size = map->key_size; |
| 1980 | info.value_size = map->value_size; |
| 1981 | info.max_entries = map->max_entries; |
| 1982 | info.map_flags = map->map_flags; |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 1983 | memcpy(info.name, map->name, sizeof(map->name)); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1984 | |
Jakub Kicinski | 52775b3 | 2018-01-17 19:13:28 -0800 | [diff] [blame] | 1985 | if (bpf_map_is_dev_bound(map)) { |
| 1986 | err = bpf_map_offload_info_fill(&info, map); |
| 1987 | if (err) |
| 1988 | return err; |
| 1989 | } |
| 1990 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1991 | if (copy_to_user(uinfo, &info, info_len) || |
| 1992 | put_user(info_len, &uattr->info.info_len)) |
| 1993 | return -EFAULT; |
| 1994 | |
| 1995 | return 0; |
| 1996 | } |
| 1997 | |
| 1998 | #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info |
| 1999 | |
| 2000 | static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, |
| 2001 | union bpf_attr __user *uattr) |
| 2002 | { |
| 2003 | int ufd = attr->info.bpf_fd; |
| 2004 | struct fd f; |
| 2005 | int err; |
| 2006 | |
| 2007 | if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) |
| 2008 | return -EINVAL; |
| 2009 | |
| 2010 | f = fdget(ufd); |
| 2011 | if (!f.file) |
| 2012 | return -EBADFD; |
| 2013 | |
| 2014 | if (f.file->f_op == &bpf_prog_fops) |
| 2015 | err = bpf_prog_get_info_by_fd(f.file->private_data, attr, |
| 2016 | uattr); |
| 2017 | else if (f.file->f_op == &bpf_map_fops) |
| 2018 | err = bpf_map_get_info_by_fd(f.file->private_data, attr, |
| 2019 | uattr); |
| 2020 | else |
| 2021 | err = -EINVAL; |
| 2022 | |
| 2023 | fdput(f); |
| 2024 | return err; |
| 2025 | } |
| 2026 | |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame^] | 2027 | #define BPF_BTF_LOAD_LAST_FIELD btf_log_level |
| 2028 | |
| 2029 | static int bpf_btf_load(const union bpf_attr *attr) |
| 2030 | { |
| 2031 | if (CHECK_ATTR(BPF_BTF_LOAD)) |
| 2032 | return -EINVAL; |
| 2033 | |
| 2034 | if (!capable(CAP_SYS_ADMIN)) |
| 2035 | return -EPERM; |
| 2036 | |
| 2037 | return btf_new_fd(attr); |
| 2038 | } |
| 2039 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2040 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
| 2041 | { |
| 2042 | union bpf_attr attr = {}; |
| 2043 | int err; |
| 2044 | |
Chenbo Feng | 0fa4fe8 | 2018-03-19 17:57:27 -0700 | [diff] [blame] | 2045 | if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN)) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2046 | return -EPERM; |
| 2047 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2048 | err = check_uarg_tail_zero(uattr, sizeof(attr), size); |
| 2049 | if (err) |
| 2050 | return err; |
| 2051 | size = min_t(u32, size, sizeof(attr)); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2052 | |
| 2053 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ |
| 2054 | if (copy_from_user(&attr, uattr, size) != 0) |
| 2055 | return -EFAULT; |
| 2056 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 2057 | err = security_bpf(cmd, &attr, size); |
| 2058 | if (err < 0) |
| 2059 | return err; |
| 2060 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2061 | switch (cmd) { |
| 2062 | case BPF_MAP_CREATE: |
| 2063 | err = map_create(&attr); |
| 2064 | break; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 2065 | case BPF_MAP_LOOKUP_ELEM: |
| 2066 | err = map_lookup_elem(&attr); |
| 2067 | break; |
| 2068 | case BPF_MAP_UPDATE_ELEM: |
| 2069 | err = map_update_elem(&attr); |
| 2070 | break; |
| 2071 | case BPF_MAP_DELETE_ELEM: |
| 2072 | err = map_delete_elem(&attr); |
| 2073 | break; |
| 2074 | case BPF_MAP_GET_NEXT_KEY: |
| 2075 | err = map_get_next_key(&attr); |
| 2076 | break; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2077 | case BPF_PROG_LOAD: |
| 2078 | err = bpf_prog_load(&attr); |
| 2079 | break; |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 2080 | case BPF_OBJ_PIN: |
| 2081 | err = bpf_obj_pin(&attr); |
| 2082 | break; |
| 2083 | case BPF_OBJ_GET: |
| 2084 | err = bpf_obj_get(&attr); |
| 2085 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2086 | #ifdef CONFIG_CGROUP_BPF |
| 2087 | case BPF_PROG_ATTACH: |
| 2088 | err = bpf_prog_attach(&attr); |
| 2089 | break; |
| 2090 | case BPF_PROG_DETACH: |
| 2091 | err = bpf_prog_detach(&attr); |
| 2092 | break; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2093 | case BPF_PROG_QUERY: |
| 2094 | err = bpf_prog_query(&attr, uattr); |
| 2095 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2096 | #endif |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2097 | case BPF_PROG_TEST_RUN: |
| 2098 | err = bpf_prog_test_run(&attr, uattr); |
| 2099 | break; |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 2100 | case BPF_PROG_GET_NEXT_ID: |
| 2101 | err = bpf_obj_get_next_id(&attr, uattr, |
| 2102 | &prog_idr, &prog_idr_lock); |
| 2103 | break; |
| 2104 | case BPF_MAP_GET_NEXT_ID: |
| 2105 | err = bpf_obj_get_next_id(&attr, uattr, |
| 2106 | &map_idr, &map_idr_lock); |
| 2107 | break; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 2108 | case BPF_PROG_GET_FD_BY_ID: |
| 2109 | err = bpf_prog_get_fd_by_id(&attr); |
| 2110 | break; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2111 | case BPF_MAP_GET_FD_BY_ID: |
| 2112 | err = bpf_map_get_fd_by_id(&attr); |
| 2113 | break; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2114 | case BPF_OBJ_GET_INFO_BY_FD: |
| 2115 | err = bpf_obj_get_info_by_fd(&attr, uattr); |
| 2116 | break; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2117 | case BPF_RAW_TRACEPOINT_OPEN: |
| 2118 | err = bpf_raw_tracepoint_open(&attr); |
| 2119 | break; |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame^] | 2120 | case BPF_BTF_LOAD: |
| 2121 | err = bpf_btf_load(&attr); |
| 2122 | break; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2123 | default: |
| 2124 | err = -EINVAL; |
| 2125 | break; |
| 2126 | } |
| 2127 | |
| 2128 | return err; |
| 2129 | } |