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> |
| 13 | #include <linux/syscalls.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/anon_inodes.h> |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 16 | #include <linux/file.h> |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 17 | #include <linux/license.h> |
| 18 | #include <linux/filter.h> |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 19 | #include <linux/version.h> |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 21 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 22 | DEFINE_PER_CPU(int, bpf_prog_active); |
| 23 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 24 | int sysctl_unprivileged_bpf_disabled __read_mostly; |
| 25 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 26 | static LIST_HEAD(bpf_map_types); |
| 27 | |
| 28 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
| 29 | { |
| 30 | struct bpf_map_type_list *tl; |
| 31 | struct bpf_map *map; |
| 32 | |
| 33 | list_for_each_entry(tl, &bpf_map_types, list_node) { |
| 34 | if (tl->type == attr->map_type) { |
| 35 | map = tl->ops->map_alloc(attr); |
| 36 | if (IS_ERR(map)) |
| 37 | return map; |
| 38 | map->ops = tl->ops; |
| 39 | map->map_type = attr->map_type; |
| 40 | return map; |
| 41 | } |
| 42 | } |
| 43 | return ERR_PTR(-EINVAL); |
| 44 | } |
| 45 | |
| 46 | /* boot time registration of different map implementations */ |
| 47 | void bpf_register_map_type(struct bpf_map_type_list *tl) |
| 48 | { |
| 49 | list_add(&tl->list_node, &bpf_map_types); |
| 50 | } |
| 51 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 52 | int bpf_map_precharge_memlock(u32 pages) |
| 53 | { |
| 54 | struct user_struct *user = get_current_user(); |
| 55 | unsigned long memlock_limit, cur; |
| 56 | |
| 57 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 58 | cur = atomic_long_read(&user->locked_vm); |
| 59 | free_uid(user); |
| 60 | if (cur + pages > memlock_limit) |
| 61 | return -EPERM; |
| 62 | return 0; |
| 63 | } |
| 64 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 65 | static int bpf_map_charge_memlock(struct bpf_map *map) |
| 66 | { |
| 67 | struct user_struct *user = get_current_user(); |
| 68 | unsigned long memlock_limit; |
| 69 | |
| 70 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 71 | |
| 72 | atomic_long_add(map->pages, &user->locked_vm); |
| 73 | |
| 74 | if (atomic_long_read(&user->locked_vm) > memlock_limit) { |
| 75 | atomic_long_sub(map->pages, &user->locked_vm); |
| 76 | free_uid(user); |
| 77 | return -EPERM; |
| 78 | } |
| 79 | map->user = user; |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static void bpf_map_uncharge_memlock(struct bpf_map *map) |
| 84 | { |
| 85 | struct user_struct *user = map->user; |
| 86 | |
| 87 | atomic_long_sub(map->pages, &user->locked_vm); |
| 88 | free_uid(user); |
| 89 | } |
| 90 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 91 | /* called from workqueue */ |
| 92 | static void bpf_map_free_deferred(struct work_struct *work) |
| 93 | { |
| 94 | struct bpf_map *map = container_of(work, struct bpf_map, work); |
| 95 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 96 | bpf_map_uncharge_memlock(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 97 | /* implementation dependent freeing */ |
| 98 | map->ops->map_free(map); |
| 99 | } |
| 100 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 101 | static void bpf_map_put_uref(struct bpf_map *map) |
| 102 | { |
| 103 | if (atomic_dec_and_test(&map->usercnt)) { |
| 104 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) |
| 105 | bpf_fd_array_map_clear(map); |
| 106 | } |
| 107 | } |
| 108 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 109 | /* decrement map refcnt and schedule it for freeing via workqueue |
| 110 | * (unrelying map implementation ops->map_free() might sleep) |
| 111 | */ |
| 112 | void bpf_map_put(struct bpf_map *map) |
| 113 | { |
| 114 | if (atomic_dec_and_test(&map->refcnt)) { |
| 115 | INIT_WORK(&map->work, bpf_map_free_deferred); |
| 116 | schedule_work(&map->work); |
| 117 | } |
| 118 | } |
| 119 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 120 | void bpf_map_put_with_uref(struct bpf_map *map) |
| 121 | { |
| 122 | bpf_map_put_uref(map); |
| 123 | bpf_map_put(map); |
| 124 | } |
| 125 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 126 | static int bpf_map_release(struct inode *inode, struct file *filp) |
| 127 | { |
Daniel Borkmann | 61d1b6a | 2016-06-15 22:47:12 +0200 | [diff] [blame] | 128 | struct bpf_map *map = filp->private_data; |
| 129 | |
| 130 | if (map->ops->map_release) |
| 131 | map->ops->map_release(map, filp); |
| 132 | |
| 133 | bpf_map_put_with_uref(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 137 | #ifdef CONFIG_PROC_FS |
| 138 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) |
| 139 | { |
| 140 | const struct bpf_map *map = filp->private_data; |
| 141 | |
| 142 | seq_printf(m, |
| 143 | "map_type:\t%u\n" |
| 144 | "key_size:\t%u\n" |
| 145 | "value_size:\t%u\n" |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 146 | "max_entries:\t%u\n" |
| 147 | "map_flags:\t%#x\n", |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 148 | map->map_type, |
| 149 | map->key_size, |
| 150 | map->value_size, |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 151 | map->max_entries, |
| 152 | map->map_flags); |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 153 | } |
| 154 | #endif |
| 155 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 156 | static const struct file_operations bpf_map_fops = { |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 157 | #ifdef CONFIG_PROC_FS |
| 158 | .show_fdinfo = bpf_map_show_fdinfo, |
| 159 | #endif |
| 160 | .release = bpf_map_release, |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 163 | int bpf_map_new_fd(struct bpf_map *map) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 164 | { |
| 165 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, |
| 166 | O_RDWR | O_CLOEXEC); |
| 167 | } |
| 168 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 169 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
| 170 | #define CHECK_ATTR(CMD) \ |
| 171 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ |
| 172 | sizeof(attr->CMD##_LAST_FIELD), 0, \ |
| 173 | sizeof(*attr) - \ |
| 174 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ |
| 175 | sizeof(attr->CMD##_LAST_FIELD)) != NULL |
| 176 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 177 | #define BPF_MAP_CREATE_LAST_FIELD map_flags |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 178 | /* called via syscall */ |
| 179 | static int map_create(union bpf_attr *attr) |
| 180 | { |
| 181 | struct bpf_map *map; |
| 182 | int err; |
| 183 | |
| 184 | err = CHECK_ATTR(BPF_MAP_CREATE); |
| 185 | if (err) |
| 186 | return -EINVAL; |
| 187 | |
| 188 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ |
| 189 | map = find_and_alloc_map(attr); |
| 190 | if (IS_ERR(map)) |
| 191 | return PTR_ERR(map); |
| 192 | |
| 193 | atomic_set(&map->refcnt, 1); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 194 | atomic_set(&map->usercnt, 1); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 195 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 196 | err = bpf_map_charge_memlock(map); |
| 197 | if (err) |
Daniel Borkmann | 20b2b24 | 2016-11-04 00:56:31 +0100 | [diff] [blame] | 198 | goto free_map_nouncharge; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 199 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 200 | err = bpf_map_new_fd(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 201 | if (err < 0) |
| 202 | /* failed to allocate fd */ |
| 203 | goto free_map; |
| 204 | |
| 205 | return err; |
| 206 | |
| 207 | free_map: |
Daniel Borkmann | 20b2b24 | 2016-11-04 00:56:31 +0100 | [diff] [blame] | 208 | bpf_map_uncharge_memlock(map); |
| 209 | free_map_nouncharge: |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 210 | map->ops->map_free(map); |
| 211 | return err; |
| 212 | } |
| 213 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 214 | /* if error is returned, fd is released. |
| 215 | * On success caller should complete fd access with matching fdput() |
| 216 | */ |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 217 | struct bpf_map *__bpf_map_get(struct fd f) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 218 | { |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 219 | if (!f.file) |
| 220 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 221 | if (f.file->f_op != &bpf_map_fops) { |
| 222 | fdput(f); |
| 223 | return ERR_PTR(-EINVAL); |
| 224 | } |
| 225 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 226 | return f.file->private_data; |
| 227 | } |
| 228 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 229 | /* prog's and map's refcnt limit */ |
| 230 | #define BPF_MAX_REFCNT 32768 |
| 231 | |
| 232 | struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref) |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 233 | { |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 234 | if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) { |
| 235 | atomic_dec(&map->refcnt); |
| 236 | return ERR_PTR(-EBUSY); |
| 237 | } |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 238 | if (uref) |
| 239 | atomic_inc(&map->usercnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 240 | return map; |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 244 | { |
| 245 | struct fd f = fdget(ufd); |
| 246 | struct bpf_map *map; |
| 247 | |
| 248 | map = __bpf_map_get(f); |
| 249 | if (IS_ERR(map)) |
| 250 | return map; |
| 251 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 252 | map = bpf_map_inc(map, true); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 253 | fdput(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 254 | |
| 255 | return map; |
| 256 | } |
| 257 | |
Alexei Starovoitov | b8cdc05 | 2016-03-09 18:56:49 -0800 | [diff] [blame] | 258 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
| 259 | { |
| 260 | return -ENOTSUPP; |
| 261 | } |
| 262 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 263 | /* last field in 'union bpf_attr' used by this command */ |
| 264 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value |
| 265 | |
| 266 | static int map_lookup_elem(union bpf_attr *attr) |
| 267 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 268 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 269 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 270 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 271 | struct bpf_map *map; |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 272 | void *key, *value, *ptr; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 273 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 274 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 275 | int err; |
| 276 | |
| 277 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) |
| 278 | return -EINVAL; |
| 279 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 280 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 281 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 282 | if (IS_ERR(map)) |
| 283 | return PTR_ERR(map); |
| 284 | |
| 285 | err = -ENOMEM; |
| 286 | key = kmalloc(map->key_size, GFP_USER); |
| 287 | if (!key) |
| 288 | goto err_put; |
| 289 | |
| 290 | err = -EFAULT; |
| 291 | if (copy_from_user(key, ukey, map->key_size) != 0) |
| 292 | goto free_key; |
| 293 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 294 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame^] | 295 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 296 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
| 297 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
| 298 | else |
| 299 | value_size = map->value_size; |
| 300 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 301 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 302 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 303 | if (!value) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 304 | goto free_key; |
| 305 | |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame^] | 306 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 307 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 308 | err = bpf_percpu_hash_copy(map, key, value); |
| 309 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 310 | err = bpf_percpu_array_copy(map, key, value); |
Alexei Starovoitov | 557c0c6 | 2016-03-07 21:57:17 -0800 | [diff] [blame] | 311 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
| 312 | err = bpf_stackmap_copy(map, key, value); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 313 | } else { |
| 314 | rcu_read_lock(); |
| 315 | ptr = map->ops->map_lookup_elem(map, key); |
| 316 | if (ptr) |
| 317 | memcpy(value, ptr, value_size); |
| 318 | rcu_read_unlock(); |
| 319 | err = ptr ? 0 : -ENOENT; |
| 320 | } |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 321 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 322 | if (err) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 323 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 324 | |
| 325 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 326 | if (copy_to_user(uvalue, value, value_size) != 0) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 327 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 328 | |
| 329 | err = 0; |
| 330 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 331 | free_value: |
| 332 | kfree(value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 333 | free_key: |
| 334 | kfree(key); |
| 335 | err_put: |
| 336 | fdput(f); |
| 337 | return err; |
| 338 | } |
| 339 | |
Alexei Starovoitov | 3274f52 | 2014-11-13 17:36:44 -0800 | [diff] [blame] | 340 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 341 | |
| 342 | static int map_update_elem(union bpf_attr *attr) |
| 343 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 344 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 345 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 346 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 347 | struct bpf_map *map; |
| 348 | void *key, *value; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 349 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 350 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 351 | int err; |
| 352 | |
| 353 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) |
| 354 | return -EINVAL; |
| 355 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 356 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 357 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 358 | if (IS_ERR(map)) |
| 359 | return PTR_ERR(map); |
| 360 | |
| 361 | err = -ENOMEM; |
| 362 | key = kmalloc(map->key_size, GFP_USER); |
| 363 | if (!key) |
| 364 | goto err_put; |
| 365 | |
| 366 | err = -EFAULT; |
| 367 | if (copy_from_user(key, ukey, map->key_size) != 0) |
| 368 | goto free_key; |
| 369 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 370 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame^] | 371 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 372 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
| 373 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
| 374 | else |
| 375 | value_size = map->value_size; |
| 376 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 377 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 378 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 379 | if (!value) |
| 380 | goto free_key; |
| 381 | |
| 382 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 383 | if (copy_from_user(value, uvalue, value_size) != 0) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 384 | goto free_value; |
| 385 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 386 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering from |
| 387 | * inside bpf map update or delete otherwise deadlocks are possible |
| 388 | */ |
| 389 | preempt_disable(); |
| 390 | __this_cpu_inc(bpf_prog_active); |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame^] | 391 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 392 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 393 | err = bpf_percpu_hash_update(map, key, value, attr->flags); |
| 394 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 395 | err = bpf_percpu_array_update(map, key, value, attr->flags); |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 396 | } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 397 | map->map_type == BPF_MAP_TYPE_PROG_ARRAY || |
| 398 | map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY) { |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 399 | rcu_read_lock(); |
| 400 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, |
| 401 | attr->flags); |
| 402 | rcu_read_unlock(); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 403 | } else { |
| 404 | rcu_read_lock(); |
| 405 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
| 406 | rcu_read_unlock(); |
| 407 | } |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 408 | __this_cpu_dec(bpf_prog_active); |
| 409 | preempt_enable(); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 410 | |
| 411 | free_value: |
| 412 | kfree(value); |
| 413 | free_key: |
| 414 | kfree(key); |
| 415 | err_put: |
| 416 | fdput(f); |
| 417 | return err; |
| 418 | } |
| 419 | |
| 420 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key |
| 421 | |
| 422 | static int map_delete_elem(union bpf_attr *attr) |
| 423 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 424 | void __user *ukey = u64_to_user_ptr(attr->key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 425 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 426 | struct bpf_map *map; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 427 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 428 | void *key; |
| 429 | int err; |
| 430 | |
| 431 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) |
| 432 | return -EINVAL; |
| 433 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 434 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 435 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 436 | if (IS_ERR(map)) |
| 437 | return PTR_ERR(map); |
| 438 | |
| 439 | err = -ENOMEM; |
| 440 | key = kmalloc(map->key_size, GFP_USER); |
| 441 | if (!key) |
| 442 | goto err_put; |
| 443 | |
| 444 | err = -EFAULT; |
| 445 | if (copy_from_user(key, ukey, map->key_size) != 0) |
| 446 | goto free_key; |
| 447 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 448 | preempt_disable(); |
| 449 | __this_cpu_inc(bpf_prog_active); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 450 | rcu_read_lock(); |
| 451 | err = map->ops->map_delete_elem(map, key); |
| 452 | rcu_read_unlock(); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 453 | __this_cpu_dec(bpf_prog_active); |
| 454 | preempt_enable(); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 455 | |
| 456 | free_key: |
| 457 | kfree(key); |
| 458 | err_put: |
| 459 | fdput(f); |
| 460 | return err; |
| 461 | } |
| 462 | |
| 463 | /* last field in 'union bpf_attr' used by this command */ |
| 464 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key |
| 465 | |
| 466 | static int map_get_next_key(union bpf_attr *attr) |
| 467 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 468 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 469 | void __user *unext_key = u64_to_user_ptr(attr->next_key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 470 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 471 | struct bpf_map *map; |
| 472 | void *key, *next_key; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 473 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 474 | int err; |
| 475 | |
| 476 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) |
| 477 | return -EINVAL; |
| 478 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 479 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 480 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 481 | if (IS_ERR(map)) |
| 482 | return PTR_ERR(map); |
| 483 | |
| 484 | err = -ENOMEM; |
| 485 | key = kmalloc(map->key_size, GFP_USER); |
| 486 | if (!key) |
| 487 | goto err_put; |
| 488 | |
| 489 | err = -EFAULT; |
| 490 | if (copy_from_user(key, ukey, map->key_size) != 0) |
| 491 | goto free_key; |
| 492 | |
| 493 | err = -ENOMEM; |
| 494 | next_key = kmalloc(map->key_size, GFP_USER); |
| 495 | if (!next_key) |
| 496 | goto free_key; |
| 497 | |
| 498 | rcu_read_lock(); |
| 499 | err = map->ops->map_get_next_key(map, key, next_key); |
| 500 | rcu_read_unlock(); |
| 501 | if (err) |
| 502 | goto free_next_key; |
| 503 | |
| 504 | err = -EFAULT; |
| 505 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) |
| 506 | goto free_next_key; |
| 507 | |
| 508 | err = 0; |
| 509 | |
| 510 | free_next_key: |
| 511 | kfree(next_key); |
| 512 | free_key: |
| 513 | kfree(key); |
| 514 | err_put: |
| 515 | fdput(f); |
| 516 | return err; |
| 517 | } |
| 518 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 519 | static LIST_HEAD(bpf_prog_types); |
| 520 | |
| 521 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
| 522 | { |
| 523 | struct bpf_prog_type_list *tl; |
| 524 | |
| 525 | list_for_each_entry(tl, &bpf_prog_types, list_node) { |
| 526 | if (tl->type == type) { |
| 527 | prog->aux->ops = tl->ops; |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 528 | prog->type = type; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 529 | return 0; |
| 530 | } |
| 531 | } |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 532 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 533 | return -EINVAL; |
| 534 | } |
| 535 | |
| 536 | void bpf_register_prog_type(struct bpf_prog_type_list *tl) |
| 537 | { |
| 538 | list_add(&tl->list_node, &bpf_prog_types); |
| 539 | } |
| 540 | |
Alexei Starovoitov | 0a542a8 | 2014-09-26 00:17:01 -0700 | [diff] [blame] | 541 | /* fixup insn->imm field of bpf_call instructions: |
| 542 | * if (insn->imm == BPF_FUNC_map_lookup_elem) |
| 543 | * insn->imm = bpf_map_lookup_elem - __bpf_call_base; |
| 544 | * else if (insn->imm == BPF_FUNC_map_update_elem) |
| 545 | * insn->imm = bpf_map_update_elem - __bpf_call_base; |
| 546 | * else ... |
| 547 | * |
| 548 | * this function is called after eBPF program passed verification |
| 549 | */ |
| 550 | static void fixup_bpf_calls(struct bpf_prog *prog) |
| 551 | { |
| 552 | const struct bpf_func_proto *fn; |
| 553 | int i; |
| 554 | |
| 555 | for (i = 0; i < prog->len; i++) { |
| 556 | struct bpf_insn *insn = &prog->insnsi[i]; |
| 557 | |
| 558 | if (insn->code == (BPF_JMP | BPF_CALL)) { |
| 559 | /* we reach here when program has bpf_call instructions |
| 560 | * and it passed bpf_check(), means that |
| 561 | * ops->get_func_proto must have been supplied, check it |
| 562 | */ |
| 563 | BUG_ON(!prog->aux->ops->get_func_proto); |
| 564 | |
Daniel Borkmann | c46646d | 2015-09-30 01:41:51 +0200 | [diff] [blame] | 565 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 566 | prog->dst_needed = 1; |
Daniel Borkmann | 3ad0040 | 2015-10-08 01:20:39 +0200 | [diff] [blame] | 567 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 568 | bpf_user_rnd_init_once(); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 569 | if (insn->imm == BPF_FUNC_tail_call) { |
| 570 | /* mark bpf_tail_call as different opcode |
| 571 | * to avoid conditional branch in |
| 572 | * interpeter for every normal call |
| 573 | * and to prevent accidental JITing by |
| 574 | * JIT compiler that doesn't support |
| 575 | * bpf_tail_call yet |
| 576 | */ |
| 577 | insn->imm = 0; |
| 578 | insn->code |= BPF_X; |
| 579 | continue; |
| 580 | } |
| 581 | |
Alexei Starovoitov | 0a542a8 | 2014-09-26 00:17:01 -0700 | [diff] [blame] | 582 | fn = prog->aux->ops->get_func_proto(insn->imm); |
| 583 | /* all functions that have prototype and verifier allowed |
| 584 | * programs to call them, must be real in-kernel functions |
| 585 | */ |
| 586 | BUG_ON(!fn->func); |
| 587 | insn->imm = fn->func - __bpf_call_base; |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 592 | /* drop refcnt on maps used by eBPF program and free auxilary data */ |
| 593 | static void free_used_maps(struct bpf_prog_aux *aux) |
| 594 | { |
| 595 | int i; |
| 596 | |
| 597 | for (i = 0; i < aux->used_map_cnt; i++) |
| 598 | bpf_map_put(aux->used_maps[i]); |
| 599 | |
| 600 | kfree(aux->used_maps); |
| 601 | } |
| 602 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 603 | static int bpf_prog_charge_memlock(struct bpf_prog *prog) |
| 604 | { |
| 605 | struct user_struct *user = get_current_user(); |
| 606 | unsigned long memlock_limit; |
| 607 | |
| 608 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 609 | |
| 610 | atomic_long_add(prog->pages, &user->locked_vm); |
| 611 | if (atomic_long_read(&user->locked_vm) > memlock_limit) { |
| 612 | atomic_long_sub(prog->pages, &user->locked_vm); |
| 613 | free_uid(user); |
| 614 | return -EPERM; |
| 615 | } |
| 616 | prog->aux->user = user; |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) |
| 621 | { |
| 622 | struct user_struct *user = prog->aux->user; |
| 623 | |
| 624 | atomic_long_sub(prog->pages, &user->locked_vm); |
| 625 | free_uid(user); |
| 626 | } |
| 627 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 628 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 629 | { |
| 630 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); |
| 631 | |
| 632 | free_used_maps(aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 633 | bpf_prog_uncharge_memlock(aux->prog); |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 634 | bpf_prog_free(aux->prog); |
| 635 | } |
| 636 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 637 | void bpf_prog_put(struct bpf_prog *prog) |
| 638 | { |
Daniel Borkmann | e9d8afa | 2015-10-29 14:58:08 +0100 | [diff] [blame] | 639 | if (atomic_dec_and_test(&prog->aux->refcnt)) |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 640 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 641 | } |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 642 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 643 | |
| 644 | static int bpf_prog_release(struct inode *inode, struct file *filp) |
| 645 | { |
| 646 | struct bpf_prog *prog = filp->private_data; |
| 647 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 648 | bpf_prog_put(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | static const struct file_operations bpf_prog_fops = { |
| 653 | .release = bpf_prog_release, |
| 654 | }; |
| 655 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 656 | int bpf_prog_new_fd(struct bpf_prog *prog) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 657 | { |
| 658 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
| 659 | O_RDWR | O_CLOEXEC); |
| 660 | } |
| 661 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 662 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 663 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 664 | if (!f.file) |
| 665 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 666 | if (f.file->f_op != &bpf_prog_fops) { |
| 667 | fdput(f); |
| 668 | return ERR_PTR(-EINVAL); |
| 669 | } |
| 670 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 671 | return f.file->private_data; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 674 | struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i) |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 675 | { |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 676 | if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) { |
| 677 | atomic_sub(i, &prog->aux->refcnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 678 | return ERR_PTR(-EBUSY); |
| 679 | } |
| 680 | return prog; |
| 681 | } |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 682 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
| 683 | |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 684 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
| 685 | { |
| 686 | /* Only to be used for undoing previous bpf_prog_add() in some |
| 687 | * error path. We still know that another entity in our call |
| 688 | * path holds a reference to the program, thus atomic_sub() can |
| 689 | * be safely used in such cases! |
| 690 | */ |
| 691 | WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0); |
| 692 | } |
| 693 | EXPORT_SYMBOL_GPL(bpf_prog_sub); |
| 694 | |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 695 | struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog) |
| 696 | { |
| 697 | return bpf_prog_add(prog, 1); |
| 698 | } |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 699 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 700 | static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 701 | { |
| 702 | struct fd f = fdget(ufd); |
| 703 | struct bpf_prog *prog; |
| 704 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 705 | prog = ____bpf_prog_get(f); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 706 | if (IS_ERR(prog)) |
| 707 | return prog; |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 708 | if (type && prog->type != *type) { |
| 709 | prog = ERR_PTR(-EINVAL); |
| 710 | goto out; |
| 711 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 712 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 713 | prog = bpf_prog_inc(prog); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 714 | out: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 715 | fdput(f); |
| 716 | return prog; |
| 717 | } |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 718 | |
| 719 | struct bpf_prog *bpf_prog_get(u32 ufd) |
| 720 | { |
| 721 | return __bpf_prog_get(ufd, NULL); |
| 722 | } |
| 723 | |
| 724 | struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type) |
| 725 | { |
| 726 | return __bpf_prog_get(ufd, &type); |
| 727 | } |
| 728 | EXPORT_SYMBOL_GPL(bpf_prog_get_type); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 729 | |
| 730 | /* last field in 'union bpf_attr' used by this command */ |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 731 | #define BPF_PROG_LOAD_LAST_FIELD kern_version |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 732 | |
| 733 | static int bpf_prog_load(union bpf_attr *attr) |
| 734 | { |
| 735 | enum bpf_prog_type type = attr->prog_type; |
| 736 | struct bpf_prog *prog; |
| 737 | int err; |
| 738 | char license[128]; |
| 739 | bool is_gpl; |
| 740 | |
| 741 | if (CHECK_ATTR(BPF_PROG_LOAD)) |
| 742 | return -EINVAL; |
| 743 | |
| 744 | /* copy eBPF program license from user space */ |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 745 | if (strncpy_from_user(license, u64_to_user_ptr(attr->license), |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 746 | sizeof(license) - 1) < 0) |
| 747 | return -EFAULT; |
| 748 | license[sizeof(license) - 1] = 0; |
| 749 | |
| 750 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
| 751 | is_gpl = license_is_gpl_compatible(license); |
| 752 | |
| 753 | if (attr->insn_cnt >= BPF_MAXINSNS) |
| 754 | return -EINVAL; |
| 755 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 756 | if (type == BPF_PROG_TYPE_KPROBE && |
| 757 | attr->kern_version != LINUX_VERSION_CODE) |
| 758 | return -EINVAL; |
| 759 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 760 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN)) |
| 761 | return -EPERM; |
| 762 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 763 | /* plain bpf_prog allocation */ |
| 764 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); |
| 765 | if (!prog) |
| 766 | return -ENOMEM; |
| 767 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 768 | err = bpf_prog_charge_memlock(prog); |
| 769 | if (err) |
| 770 | goto free_prog_nouncharge; |
| 771 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 772 | prog->len = attr->insn_cnt; |
| 773 | |
| 774 | err = -EFAULT; |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 775 | if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 776 | prog->len * sizeof(struct bpf_insn)) != 0) |
| 777 | goto free_prog; |
| 778 | |
| 779 | prog->orig_prog = NULL; |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 780 | prog->jited = 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 781 | |
| 782 | atomic_set(&prog->aux->refcnt, 1); |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 783 | prog->gpl_compatible = is_gpl ? 1 : 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 784 | |
| 785 | /* find program type: socket_filter vs tracing_filter */ |
| 786 | err = find_prog_type(type, prog); |
| 787 | if (err < 0) |
| 788 | goto free_prog; |
| 789 | |
| 790 | /* run eBPF verifier */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 791 | err = bpf_check(&prog, attr); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 792 | if (err < 0) |
| 793 | goto free_used_maps; |
| 794 | |
Alexei Starovoitov | 0a542a8 | 2014-09-26 00:17:01 -0700 | [diff] [blame] | 795 | /* fixup BPF_CALL->imm field */ |
| 796 | fixup_bpf_calls(prog); |
| 797 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 798 | /* eBPF program is ready to be JITed */ |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 799 | prog = bpf_prog_select_runtime(prog, &err); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 800 | if (err < 0) |
| 801 | goto free_used_maps; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 802 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 803 | err = bpf_prog_new_fd(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 804 | if (err < 0) |
| 805 | /* failed to allocate fd */ |
| 806 | goto free_used_maps; |
| 807 | |
| 808 | return err; |
| 809 | |
| 810 | free_used_maps: |
| 811 | free_used_maps(prog->aux); |
| 812 | free_prog: |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 813 | bpf_prog_uncharge_memlock(prog); |
| 814 | free_prog_nouncharge: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 815 | bpf_prog_free(prog); |
| 816 | return err; |
| 817 | } |
| 818 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 819 | #define BPF_OBJ_LAST_FIELD bpf_fd |
| 820 | |
| 821 | static int bpf_obj_pin(const union bpf_attr *attr) |
| 822 | { |
| 823 | if (CHECK_ATTR(BPF_OBJ)) |
| 824 | return -EINVAL; |
| 825 | |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 826 | 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] | 827 | } |
| 828 | |
| 829 | static int bpf_obj_get(const union bpf_attr *attr) |
| 830 | { |
| 831 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0) |
| 832 | return -EINVAL; |
| 833 | |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 834 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname)); |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 835 | } |
| 836 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 837 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
| 838 | { |
| 839 | union bpf_attr attr = {}; |
| 840 | int err; |
| 841 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 842 | if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 843 | return -EPERM; |
| 844 | |
| 845 | if (!access_ok(VERIFY_READ, uattr, 1)) |
| 846 | return -EFAULT; |
| 847 | |
| 848 | if (size > PAGE_SIZE) /* silly large */ |
| 849 | return -E2BIG; |
| 850 | |
| 851 | /* If we're handed a bigger struct than we know of, |
| 852 | * ensure all the unknown bits are 0 - i.e. new |
| 853 | * user-space does not rely on any kernel feature |
| 854 | * extensions we dont know about yet. |
| 855 | */ |
| 856 | if (size > sizeof(attr)) { |
| 857 | unsigned char __user *addr; |
| 858 | unsigned char __user *end; |
| 859 | unsigned char val; |
| 860 | |
| 861 | addr = (void __user *)uattr + sizeof(attr); |
| 862 | end = (void __user *)uattr + size; |
| 863 | |
| 864 | for (; addr < end; addr++) { |
| 865 | err = get_user(val, addr); |
| 866 | if (err) |
| 867 | return err; |
| 868 | if (val) |
| 869 | return -E2BIG; |
| 870 | } |
| 871 | size = sizeof(attr); |
| 872 | } |
| 873 | |
| 874 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ |
| 875 | if (copy_from_user(&attr, uattr, size) != 0) |
| 876 | return -EFAULT; |
| 877 | |
| 878 | switch (cmd) { |
| 879 | case BPF_MAP_CREATE: |
| 880 | err = map_create(&attr); |
| 881 | break; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 882 | case BPF_MAP_LOOKUP_ELEM: |
| 883 | err = map_lookup_elem(&attr); |
| 884 | break; |
| 885 | case BPF_MAP_UPDATE_ELEM: |
| 886 | err = map_update_elem(&attr); |
| 887 | break; |
| 888 | case BPF_MAP_DELETE_ELEM: |
| 889 | err = map_delete_elem(&attr); |
| 890 | break; |
| 891 | case BPF_MAP_GET_NEXT_KEY: |
| 892 | err = map_get_next_key(&attr); |
| 893 | break; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 894 | case BPF_PROG_LOAD: |
| 895 | err = bpf_prog_load(&attr); |
| 896 | break; |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 897 | case BPF_OBJ_PIN: |
| 898 | err = bpf_obj_pin(&attr); |
| 899 | break; |
| 900 | case BPF_OBJ_GET: |
| 901 | err = bpf_obj_get(&attr); |
| 902 | break; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 903 | default: |
| 904 | err = -EINVAL; |
| 905 | break; |
| 906 | } |
| 907 | |
| 908 | return err; |
| 909 | } |