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