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