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