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