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 || |
| 295 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
| 296 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
| 297 | else |
| 298 | value_size = map->value_size; |
| 299 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 300 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 301 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 302 | if (!value) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 303 | goto free_key; |
| 304 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 305 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) { |
| 306 | err = bpf_percpu_hash_copy(map, key, value); |
| 307 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 308 | err = bpf_percpu_array_copy(map, key, value); |
Alexei Starovoitov | 557c0c6 | 2016-03-07 21:57:17 -0800 | [diff] [blame] | 309 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
| 310 | err = bpf_stackmap_copy(map, key, value); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 311 | } else { |
| 312 | rcu_read_lock(); |
| 313 | ptr = map->ops->map_lookup_elem(map, key); |
| 314 | if (ptr) |
| 315 | memcpy(value, ptr, value_size); |
| 316 | rcu_read_unlock(); |
| 317 | err = ptr ? 0 : -ENOENT; |
| 318 | } |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 319 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 320 | if (err) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 321 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 322 | |
| 323 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 324 | if (copy_to_user(uvalue, value, value_size) != 0) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 325 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 326 | |
| 327 | err = 0; |
| 328 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 329 | free_value: |
| 330 | kfree(value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 331 | free_key: |
| 332 | kfree(key); |
| 333 | err_put: |
| 334 | fdput(f); |
| 335 | return err; |
| 336 | } |
| 337 | |
Alexei Starovoitov | 3274f52 | 2014-11-13 17:36:44 -0800 | [diff] [blame] | 338 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 339 | |
| 340 | static int map_update_elem(union bpf_attr *attr) |
| 341 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 342 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 343 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 344 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 345 | struct bpf_map *map; |
| 346 | void *key, *value; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 347 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 348 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 349 | int err; |
| 350 | |
| 351 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) |
| 352 | return -EINVAL; |
| 353 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 354 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 355 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 356 | if (IS_ERR(map)) |
| 357 | return PTR_ERR(map); |
| 358 | |
| 359 | err = -ENOMEM; |
| 360 | key = kmalloc(map->key_size, GFP_USER); |
| 361 | if (!key) |
| 362 | goto err_put; |
| 363 | |
| 364 | err = -EFAULT; |
| 365 | if (copy_from_user(key, ukey, map->key_size) != 0) |
| 366 | goto free_key; |
| 367 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 368 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 369 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
| 370 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
| 371 | else |
| 372 | value_size = map->value_size; |
| 373 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 374 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 375 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 376 | if (!value) |
| 377 | goto free_key; |
| 378 | |
| 379 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 380 | if (copy_from_user(value, uvalue, value_size) != 0) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 381 | goto free_value; |
| 382 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 383 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering from |
| 384 | * inside bpf map update or delete otherwise deadlocks are possible |
| 385 | */ |
| 386 | preempt_disable(); |
| 387 | __this_cpu_inc(bpf_prog_active); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 388 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) { |
| 389 | err = bpf_percpu_hash_update(map, key, value, attr->flags); |
| 390 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 391 | err = bpf_percpu_array_update(map, key, value, attr->flags); |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 392 | } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 393 | map->map_type == BPF_MAP_TYPE_PROG_ARRAY || |
| 394 | map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY) { |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 395 | rcu_read_lock(); |
| 396 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, |
| 397 | attr->flags); |
| 398 | rcu_read_unlock(); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 399 | } else { |
| 400 | rcu_read_lock(); |
| 401 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
| 402 | rcu_read_unlock(); |
| 403 | } |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 404 | __this_cpu_dec(bpf_prog_active); |
| 405 | preempt_enable(); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 406 | |
| 407 | free_value: |
| 408 | kfree(value); |
| 409 | free_key: |
| 410 | kfree(key); |
| 411 | err_put: |
| 412 | fdput(f); |
| 413 | return err; |
| 414 | } |
| 415 | |
| 416 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key |
| 417 | |
| 418 | static int map_delete_elem(union bpf_attr *attr) |
| 419 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 420 | void __user *ukey = u64_to_user_ptr(attr->key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 421 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 422 | struct bpf_map *map; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 423 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 424 | void *key; |
| 425 | int err; |
| 426 | |
| 427 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) |
| 428 | return -EINVAL; |
| 429 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 430 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 431 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 432 | if (IS_ERR(map)) |
| 433 | return PTR_ERR(map); |
| 434 | |
| 435 | err = -ENOMEM; |
| 436 | key = kmalloc(map->key_size, GFP_USER); |
| 437 | if (!key) |
| 438 | goto err_put; |
| 439 | |
| 440 | err = -EFAULT; |
| 441 | if (copy_from_user(key, ukey, map->key_size) != 0) |
| 442 | goto free_key; |
| 443 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 444 | preempt_disable(); |
| 445 | __this_cpu_inc(bpf_prog_active); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 446 | rcu_read_lock(); |
| 447 | err = map->ops->map_delete_elem(map, key); |
| 448 | rcu_read_unlock(); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 449 | __this_cpu_dec(bpf_prog_active); |
| 450 | preempt_enable(); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 451 | |
| 452 | free_key: |
| 453 | kfree(key); |
| 454 | err_put: |
| 455 | fdput(f); |
| 456 | return err; |
| 457 | } |
| 458 | |
| 459 | /* last field in 'union bpf_attr' used by this command */ |
| 460 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key |
| 461 | |
| 462 | static int map_get_next_key(union bpf_attr *attr) |
| 463 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 464 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 465 | void __user *unext_key = u64_to_user_ptr(attr->next_key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 466 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 467 | struct bpf_map *map; |
| 468 | void *key, *next_key; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 469 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 470 | int err; |
| 471 | |
| 472 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) |
| 473 | return -EINVAL; |
| 474 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 475 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 476 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 477 | if (IS_ERR(map)) |
| 478 | return PTR_ERR(map); |
| 479 | |
| 480 | err = -ENOMEM; |
| 481 | key = kmalloc(map->key_size, GFP_USER); |
| 482 | if (!key) |
| 483 | goto err_put; |
| 484 | |
| 485 | err = -EFAULT; |
| 486 | if (copy_from_user(key, ukey, map->key_size) != 0) |
| 487 | goto free_key; |
| 488 | |
| 489 | err = -ENOMEM; |
| 490 | next_key = kmalloc(map->key_size, GFP_USER); |
| 491 | if (!next_key) |
| 492 | goto free_key; |
| 493 | |
| 494 | rcu_read_lock(); |
| 495 | err = map->ops->map_get_next_key(map, key, next_key); |
| 496 | rcu_read_unlock(); |
| 497 | if (err) |
| 498 | goto free_next_key; |
| 499 | |
| 500 | err = -EFAULT; |
| 501 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) |
| 502 | goto free_next_key; |
| 503 | |
| 504 | err = 0; |
| 505 | |
| 506 | free_next_key: |
| 507 | kfree(next_key); |
| 508 | free_key: |
| 509 | kfree(key); |
| 510 | err_put: |
| 511 | fdput(f); |
| 512 | return err; |
| 513 | } |
| 514 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 515 | static LIST_HEAD(bpf_prog_types); |
| 516 | |
| 517 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
| 518 | { |
| 519 | struct bpf_prog_type_list *tl; |
| 520 | |
| 521 | list_for_each_entry(tl, &bpf_prog_types, list_node) { |
| 522 | if (tl->type == type) { |
| 523 | prog->aux->ops = tl->ops; |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 524 | prog->type = type; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 525 | return 0; |
| 526 | } |
| 527 | } |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 528 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 529 | return -EINVAL; |
| 530 | } |
| 531 | |
| 532 | void bpf_register_prog_type(struct bpf_prog_type_list *tl) |
| 533 | { |
| 534 | list_add(&tl->list_node, &bpf_prog_types); |
| 535 | } |
| 536 | |
Alexei Starovoitov | 0a542a8 | 2014-09-26 00:17:01 -0700 | [diff] [blame] | 537 | /* fixup insn->imm field of bpf_call instructions: |
| 538 | * if (insn->imm == BPF_FUNC_map_lookup_elem) |
| 539 | * insn->imm = bpf_map_lookup_elem - __bpf_call_base; |
| 540 | * else if (insn->imm == BPF_FUNC_map_update_elem) |
| 541 | * insn->imm = bpf_map_update_elem - __bpf_call_base; |
| 542 | * else ... |
| 543 | * |
| 544 | * this function is called after eBPF program passed verification |
| 545 | */ |
| 546 | static void fixup_bpf_calls(struct bpf_prog *prog) |
| 547 | { |
| 548 | const struct bpf_func_proto *fn; |
| 549 | int i; |
| 550 | |
| 551 | for (i = 0; i < prog->len; i++) { |
| 552 | struct bpf_insn *insn = &prog->insnsi[i]; |
| 553 | |
| 554 | if (insn->code == (BPF_JMP | BPF_CALL)) { |
| 555 | /* we reach here when program has bpf_call instructions |
| 556 | * and it passed bpf_check(), means that |
| 557 | * ops->get_func_proto must have been supplied, check it |
| 558 | */ |
| 559 | BUG_ON(!prog->aux->ops->get_func_proto); |
| 560 | |
Daniel Borkmann | c46646d | 2015-09-30 01:41:51 +0200 | [diff] [blame] | 561 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 562 | prog->dst_needed = 1; |
Daniel Borkmann | 3ad0040 | 2015-10-08 01:20:39 +0200 | [diff] [blame] | 563 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 564 | bpf_user_rnd_init_once(); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 565 | if (insn->imm == BPF_FUNC_tail_call) { |
| 566 | /* mark bpf_tail_call as different opcode |
| 567 | * to avoid conditional branch in |
| 568 | * interpeter for every normal call |
| 569 | * and to prevent accidental JITing by |
| 570 | * JIT compiler that doesn't support |
| 571 | * bpf_tail_call yet |
| 572 | */ |
| 573 | insn->imm = 0; |
| 574 | insn->code |= BPF_X; |
| 575 | continue; |
| 576 | } |
| 577 | |
Alexei Starovoitov | 0a542a8 | 2014-09-26 00:17:01 -0700 | [diff] [blame] | 578 | fn = prog->aux->ops->get_func_proto(insn->imm); |
| 579 | /* all functions that have prototype and verifier allowed |
| 580 | * programs to call them, must be real in-kernel functions |
| 581 | */ |
| 582 | BUG_ON(!fn->func); |
| 583 | insn->imm = fn->func - __bpf_call_base; |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 588 | /* drop refcnt on maps used by eBPF program and free auxilary data */ |
| 589 | static void free_used_maps(struct bpf_prog_aux *aux) |
| 590 | { |
| 591 | int i; |
| 592 | |
| 593 | for (i = 0; i < aux->used_map_cnt; i++) |
| 594 | bpf_map_put(aux->used_maps[i]); |
| 595 | |
| 596 | kfree(aux->used_maps); |
| 597 | } |
| 598 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 599 | static int bpf_prog_charge_memlock(struct bpf_prog *prog) |
| 600 | { |
| 601 | struct user_struct *user = get_current_user(); |
| 602 | unsigned long memlock_limit; |
| 603 | |
| 604 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 605 | |
| 606 | atomic_long_add(prog->pages, &user->locked_vm); |
| 607 | if (atomic_long_read(&user->locked_vm) > memlock_limit) { |
| 608 | atomic_long_sub(prog->pages, &user->locked_vm); |
| 609 | free_uid(user); |
| 610 | return -EPERM; |
| 611 | } |
| 612 | prog->aux->user = user; |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) |
| 617 | { |
| 618 | struct user_struct *user = prog->aux->user; |
| 619 | |
| 620 | atomic_long_sub(prog->pages, &user->locked_vm); |
| 621 | free_uid(user); |
| 622 | } |
| 623 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 624 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 625 | { |
| 626 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); |
| 627 | |
| 628 | free_used_maps(aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 629 | bpf_prog_uncharge_memlock(aux->prog); |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 630 | bpf_prog_free(aux->prog); |
| 631 | } |
| 632 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 633 | void bpf_prog_put(struct bpf_prog *prog) |
| 634 | { |
Daniel Borkmann | e9d8afa | 2015-10-29 14:58:08 +0100 | [diff] [blame] | 635 | if (atomic_dec_and_test(&prog->aux->refcnt)) |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 636 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 637 | } |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 638 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 639 | |
| 640 | static int bpf_prog_release(struct inode *inode, struct file *filp) |
| 641 | { |
| 642 | struct bpf_prog *prog = filp->private_data; |
| 643 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 644 | bpf_prog_put(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | static const struct file_operations bpf_prog_fops = { |
| 649 | .release = bpf_prog_release, |
| 650 | }; |
| 651 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 652 | int bpf_prog_new_fd(struct bpf_prog *prog) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 653 | { |
| 654 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
| 655 | O_RDWR | O_CLOEXEC); |
| 656 | } |
| 657 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 658 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 659 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 660 | if (!f.file) |
| 661 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 662 | if (f.file->f_op != &bpf_prog_fops) { |
| 663 | fdput(f); |
| 664 | return ERR_PTR(-EINVAL); |
| 665 | } |
| 666 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 667 | return f.file->private_data; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 670 | struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i) |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 671 | { |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 672 | if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) { |
| 673 | atomic_sub(i, &prog->aux->refcnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 674 | return ERR_PTR(-EBUSY); |
| 675 | } |
| 676 | return prog; |
| 677 | } |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 678 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
| 679 | |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 680 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
| 681 | { |
| 682 | /* Only to be used for undoing previous bpf_prog_add() in some |
| 683 | * error path. We still know that another entity in our call |
| 684 | * path holds a reference to the program, thus atomic_sub() can |
| 685 | * be safely used in such cases! |
| 686 | */ |
| 687 | WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0); |
| 688 | } |
| 689 | EXPORT_SYMBOL_GPL(bpf_prog_sub); |
| 690 | |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 691 | struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog) |
| 692 | { |
| 693 | return bpf_prog_add(prog, 1); |
| 694 | } |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 695 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 696 | 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] | 697 | { |
| 698 | struct fd f = fdget(ufd); |
| 699 | struct bpf_prog *prog; |
| 700 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 701 | prog = ____bpf_prog_get(f); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 702 | if (IS_ERR(prog)) |
| 703 | return prog; |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 704 | if (type && prog->type != *type) { |
| 705 | prog = ERR_PTR(-EINVAL); |
| 706 | goto out; |
| 707 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 708 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 709 | prog = bpf_prog_inc(prog); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 710 | out: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 711 | fdput(f); |
| 712 | return prog; |
| 713 | } |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 714 | |
| 715 | struct bpf_prog *bpf_prog_get(u32 ufd) |
| 716 | { |
| 717 | return __bpf_prog_get(ufd, NULL); |
| 718 | } |
| 719 | |
| 720 | struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type) |
| 721 | { |
| 722 | return __bpf_prog_get(ufd, &type); |
| 723 | } |
| 724 | EXPORT_SYMBOL_GPL(bpf_prog_get_type); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 725 | |
| 726 | /* last field in 'union bpf_attr' used by this command */ |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 727 | #define BPF_PROG_LOAD_LAST_FIELD kern_version |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 728 | |
| 729 | static int bpf_prog_load(union bpf_attr *attr) |
| 730 | { |
| 731 | enum bpf_prog_type type = attr->prog_type; |
| 732 | struct bpf_prog *prog; |
| 733 | int err; |
| 734 | char license[128]; |
| 735 | bool is_gpl; |
| 736 | |
| 737 | if (CHECK_ATTR(BPF_PROG_LOAD)) |
| 738 | return -EINVAL; |
| 739 | |
| 740 | /* copy eBPF program license from user space */ |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 741 | if (strncpy_from_user(license, u64_to_user_ptr(attr->license), |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 742 | sizeof(license) - 1) < 0) |
| 743 | return -EFAULT; |
| 744 | license[sizeof(license) - 1] = 0; |
| 745 | |
| 746 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
| 747 | is_gpl = license_is_gpl_compatible(license); |
| 748 | |
| 749 | if (attr->insn_cnt >= BPF_MAXINSNS) |
| 750 | return -EINVAL; |
| 751 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 752 | if (type == BPF_PROG_TYPE_KPROBE && |
| 753 | attr->kern_version != LINUX_VERSION_CODE) |
| 754 | return -EINVAL; |
| 755 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 756 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN)) |
| 757 | return -EPERM; |
| 758 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 759 | /* plain bpf_prog allocation */ |
| 760 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); |
| 761 | if (!prog) |
| 762 | return -ENOMEM; |
| 763 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 764 | err = bpf_prog_charge_memlock(prog); |
| 765 | if (err) |
| 766 | goto free_prog_nouncharge; |
| 767 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 768 | prog->len = attr->insn_cnt; |
| 769 | |
| 770 | err = -EFAULT; |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 771 | if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 772 | prog->len * sizeof(struct bpf_insn)) != 0) |
| 773 | goto free_prog; |
| 774 | |
| 775 | prog->orig_prog = NULL; |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 776 | prog->jited = 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 777 | |
| 778 | atomic_set(&prog->aux->refcnt, 1); |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 779 | prog->gpl_compatible = is_gpl ? 1 : 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 780 | |
| 781 | /* find program type: socket_filter vs tracing_filter */ |
| 782 | err = find_prog_type(type, prog); |
| 783 | if (err < 0) |
| 784 | goto free_prog; |
| 785 | |
| 786 | /* run eBPF verifier */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 787 | err = bpf_check(&prog, attr); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 788 | if (err < 0) |
| 789 | goto free_used_maps; |
| 790 | |
Alexei Starovoitov | 0a542a8 | 2014-09-26 00:17:01 -0700 | [diff] [blame] | 791 | /* fixup BPF_CALL->imm field */ |
| 792 | fixup_bpf_calls(prog); |
| 793 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 794 | /* eBPF program is ready to be JITed */ |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 795 | prog = bpf_prog_select_runtime(prog, &err); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 796 | if (err < 0) |
| 797 | goto free_used_maps; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 798 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 799 | err = bpf_prog_new_fd(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 800 | if (err < 0) |
| 801 | /* failed to allocate fd */ |
| 802 | goto free_used_maps; |
| 803 | |
| 804 | return err; |
| 805 | |
| 806 | free_used_maps: |
| 807 | free_used_maps(prog->aux); |
| 808 | free_prog: |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 809 | bpf_prog_uncharge_memlock(prog); |
| 810 | free_prog_nouncharge: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 811 | bpf_prog_free(prog); |
| 812 | return err; |
| 813 | } |
| 814 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 815 | #define BPF_OBJ_LAST_FIELD bpf_fd |
| 816 | |
| 817 | static int bpf_obj_pin(const union bpf_attr *attr) |
| 818 | { |
| 819 | if (CHECK_ATTR(BPF_OBJ)) |
| 820 | return -EINVAL; |
| 821 | |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 822 | 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] | 823 | } |
| 824 | |
| 825 | static int bpf_obj_get(const union bpf_attr *attr) |
| 826 | { |
| 827 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0) |
| 828 | return -EINVAL; |
| 829 | |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 830 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname)); |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 831 | } |
| 832 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 833 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
| 834 | { |
| 835 | union bpf_attr attr = {}; |
| 836 | int err; |
| 837 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 838 | if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 839 | return -EPERM; |
| 840 | |
| 841 | if (!access_ok(VERIFY_READ, uattr, 1)) |
| 842 | return -EFAULT; |
| 843 | |
| 844 | if (size > PAGE_SIZE) /* silly large */ |
| 845 | return -E2BIG; |
| 846 | |
| 847 | /* If we're handed a bigger struct than we know of, |
| 848 | * ensure all the unknown bits are 0 - i.e. new |
| 849 | * user-space does not rely on any kernel feature |
| 850 | * extensions we dont know about yet. |
| 851 | */ |
| 852 | if (size > sizeof(attr)) { |
| 853 | unsigned char __user *addr; |
| 854 | unsigned char __user *end; |
| 855 | unsigned char val; |
| 856 | |
| 857 | addr = (void __user *)uattr + sizeof(attr); |
| 858 | end = (void __user *)uattr + size; |
| 859 | |
| 860 | for (; addr < end; addr++) { |
| 861 | err = get_user(val, addr); |
| 862 | if (err) |
| 863 | return err; |
| 864 | if (val) |
| 865 | return -E2BIG; |
| 866 | } |
| 867 | size = sizeof(attr); |
| 868 | } |
| 869 | |
| 870 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ |
| 871 | if (copy_from_user(&attr, uattr, size) != 0) |
| 872 | return -EFAULT; |
| 873 | |
| 874 | switch (cmd) { |
| 875 | case BPF_MAP_CREATE: |
| 876 | err = map_create(&attr); |
| 877 | break; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 878 | case BPF_MAP_LOOKUP_ELEM: |
| 879 | err = map_lookup_elem(&attr); |
| 880 | break; |
| 881 | case BPF_MAP_UPDATE_ELEM: |
| 882 | err = map_update_elem(&attr); |
| 883 | break; |
| 884 | case BPF_MAP_DELETE_ELEM: |
| 885 | err = map_delete_elem(&attr); |
| 886 | break; |
| 887 | case BPF_MAP_GET_NEXT_KEY: |
| 888 | err = map_get_next_key(&attr); |
| 889 | break; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 890 | case BPF_PROG_LOAD: |
| 891 | err = bpf_prog_load(&attr); |
| 892 | break; |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 893 | case BPF_OBJ_PIN: |
| 894 | err = bpf_obj_pin(&attr); |
| 895 | break; |
| 896 | case BPF_OBJ_GET: |
| 897 | err = bpf_obj_get(&attr); |
| 898 | break; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 899 | default: |
| 900 | err = -EINVAL; |
| 901 | break; |
| 902 | } |
| 903 | |
| 904 | return err; |
| 905 | } |