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> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 16 | #include <linux/sched/signal.h> |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 17 | #include <linux/vmalloc.h> |
| 18 | #include <linux/mmzone.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 19 | #include <linux/anon_inodes.h> |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 20 | #include <linux/file.h> |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 21 | #include <linux/license.h> |
| 22 | #include <linux/filter.h> |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 23 | #include <linux/version.h> |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 24 | #include <linux/kernel.h> |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 25 | #include <linux/idr.h> |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame^] | 26 | #include <linux/cred.h> |
| 27 | #include <linux/timekeeping.h> |
| 28 | #include <linux/ctype.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 29 | |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 30 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ |
| 31 | (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ |
| 32 | (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ |
| 33 | (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) |
| 34 | #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) |
| 35 | #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map)) |
| 36 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 37 | DEFINE_PER_CPU(int, bpf_prog_active); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 38 | static DEFINE_IDR(prog_idr); |
| 39 | static DEFINE_SPINLOCK(prog_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 40 | static DEFINE_IDR(map_idr); |
| 41 | static DEFINE_SPINLOCK(map_idr_lock); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 42 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 43 | int sysctl_unprivileged_bpf_disabled __read_mostly; |
| 44 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 45 | static const struct bpf_map_ops * const bpf_map_types[] = { |
| 46 | #define BPF_PROG_TYPE(_id, _ops) |
| 47 | #define BPF_MAP_TYPE(_id, _ops) \ |
| 48 | [_id] = &_ops, |
| 49 | #include <linux/bpf_types.h> |
| 50 | #undef BPF_PROG_TYPE |
| 51 | #undef BPF_MAP_TYPE |
| 52 | }; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 53 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 54 | /* |
| 55 | * If we're handed a bigger struct than we know of, ensure all the unknown bits |
| 56 | * are 0 - i.e. new user-space does not rely on any kernel feature extensions |
| 57 | * we don't know about yet. |
| 58 | * |
| 59 | * There is a ToCToU between this function call and the following |
| 60 | * copy_from_user() call. However, this is not a concern since this function is |
| 61 | * meant to be a future-proofing of bits. |
| 62 | */ |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 63 | static int check_uarg_tail_zero(void __user *uaddr, |
| 64 | size_t expected_size, |
| 65 | size_t actual_size) |
| 66 | { |
| 67 | unsigned char __user *addr; |
| 68 | unsigned char __user *end; |
| 69 | unsigned char val; |
| 70 | int err; |
| 71 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 72 | if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ |
| 73 | return -E2BIG; |
| 74 | |
| 75 | if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size))) |
| 76 | return -EFAULT; |
| 77 | |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 78 | if (actual_size <= expected_size) |
| 79 | return 0; |
| 80 | |
| 81 | addr = uaddr + expected_size; |
| 82 | end = uaddr + actual_size; |
| 83 | |
| 84 | for (; addr < end; addr++) { |
| 85 | err = get_user(val, addr); |
| 86 | if (err) |
| 87 | return err; |
| 88 | if (val) |
| 89 | return -E2BIG; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 95 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
| 96 | { |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 97 | struct bpf_map *map; |
| 98 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 99 | if (attr->map_type >= ARRAY_SIZE(bpf_map_types) || |
| 100 | !bpf_map_types[attr->map_type]) |
| 101 | return ERR_PTR(-EINVAL); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 102 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 103 | map = bpf_map_types[attr->map_type]->map_alloc(attr); |
| 104 | if (IS_ERR(map)) |
| 105 | return map; |
| 106 | map->ops = bpf_map_types[attr->map_type]; |
| 107 | map->map_type = attr->map_type; |
| 108 | return map; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 111 | void *bpf_map_area_alloc(size_t size, int numa_node) |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 112 | { |
| 113 | /* We definitely need __GFP_NORETRY, so OOM killer doesn't |
| 114 | * trigger under memory pressure as we really just want to |
| 115 | * fail instead. |
| 116 | */ |
| 117 | const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO; |
| 118 | void *area; |
| 119 | |
| 120 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 121 | area = kmalloc_node(size, GFP_USER | flags, numa_node); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 122 | if (area != NULL) |
| 123 | return area; |
| 124 | } |
| 125 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 126 | return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags, |
| 127 | __builtin_return_address(0)); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void bpf_map_area_free(void *area) |
| 131 | { |
| 132 | kvfree(area); |
| 133 | } |
| 134 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 135 | int bpf_map_precharge_memlock(u32 pages) |
| 136 | { |
| 137 | struct user_struct *user = get_current_user(); |
| 138 | unsigned long memlock_limit, cur; |
| 139 | |
| 140 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 141 | cur = atomic_long_read(&user->locked_vm); |
| 142 | free_uid(user); |
| 143 | if (cur + pages > memlock_limit) |
| 144 | return -EPERM; |
| 145 | return 0; |
| 146 | } |
| 147 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 148 | static int bpf_map_charge_memlock(struct bpf_map *map) |
| 149 | { |
| 150 | struct user_struct *user = get_current_user(); |
| 151 | unsigned long memlock_limit; |
| 152 | |
| 153 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 154 | |
| 155 | atomic_long_add(map->pages, &user->locked_vm); |
| 156 | |
| 157 | if (atomic_long_read(&user->locked_vm) > memlock_limit) { |
| 158 | atomic_long_sub(map->pages, &user->locked_vm); |
| 159 | free_uid(user); |
| 160 | return -EPERM; |
| 161 | } |
| 162 | map->user = user; |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static void bpf_map_uncharge_memlock(struct bpf_map *map) |
| 167 | { |
| 168 | struct user_struct *user = map->user; |
| 169 | |
| 170 | atomic_long_sub(map->pages, &user->locked_vm); |
| 171 | free_uid(user); |
| 172 | } |
| 173 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 174 | static int bpf_map_alloc_id(struct bpf_map *map) |
| 175 | { |
| 176 | int id; |
| 177 | |
| 178 | spin_lock_bh(&map_idr_lock); |
| 179 | id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); |
| 180 | if (id > 0) |
| 181 | map->id = id; |
| 182 | spin_unlock_bh(&map_idr_lock); |
| 183 | |
| 184 | if (WARN_ON_ONCE(!id)) |
| 185 | return -ENOSPC; |
| 186 | |
| 187 | return id > 0 ? 0 : id; |
| 188 | } |
| 189 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 190 | static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock) |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 191 | { |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 192 | unsigned long flags; |
| 193 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 194 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 195 | spin_lock_irqsave(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 196 | else |
| 197 | __acquire(&map_idr_lock); |
| 198 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 199 | idr_remove(&map_idr, map->id); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 200 | |
| 201 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 202 | spin_unlock_irqrestore(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 203 | else |
| 204 | __release(&map_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 207 | /* called from workqueue */ |
| 208 | static void bpf_map_free_deferred(struct work_struct *work) |
| 209 | { |
| 210 | struct bpf_map *map = container_of(work, struct bpf_map, work); |
| 211 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 212 | bpf_map_uncharge_memlock(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 213 | /* implementation dependent freeing */ |
| 214 | map->ops->map_free(map); |
| 215 | } |
| 216 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 217 | static void bpf_map_put_uref(struct bpf_map *map) |
| 218 | { |
| 219 | if (atomic_dec_and_test(&map->usercnt)) { |
| 220 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) |
| 221 | bpf_fd_array_map_clear(map); |
| 222 | } |
| 223 | } |
| 224 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 225 | /* decrement map refcnt and schedule it for freeing via workqueue |
| 226 | * (unrelying map implementation ops->map_free() might sleep) |
| 227 | */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 228 | static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 229 | { |
| 230 | if (atomic_dec_and_test(&map->refcnt)) { |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 231 | /* bpf_map_free_id() must be called first */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 232 | bpf_map_free_id(map, do_idr_lock); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 233 | INIT_WORK(&map->work, bpf_map_free_deferred); |
| 234 | schedule_work(&map->work); |
| 235 | } |
| 236 | } |
| 237 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 238 | void bpf_map_put(struct bpf_map *map) |
| 239 | { |
| 240 | __bpf_map_put(map, true); |
| 241 | } |
| 242 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 243 | void bpf_map_put_with_uref(struct bpf_map *map) |
| 244 | { |
| 245 | bpf_map_put_uref(map); |
| 246 | bpf_map_put(map); |
| 247 | } |
| 248 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 249 | static int bpf_map_release(struct inode *inode, struct file *filp) |
| 250 | { |
Daniel Borkmann | 61d1b6a | 2016-06-15 22:47:12 +0200 | [diff] [blame] | 251 | struct bpf_map *map = filp->private_data; |
| 252 | |
| 253 | if (map->ops->map_release) |
| 254 | map->ops->map_release(map, filp); |
| 255 | |
| 256 | bpf_map_put_with_uref(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 260 | #ifdef CONFIG_PROC_FS |
| 261 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) |
| 262 | { |
| 263 | const struct bpf_map *map = filp->private_data; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 264 | const struct bpf_array *array; |
| 265 | u32 owner_prog_type = 0; |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 266 | u32 owner_jited = 0; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 267 | |
| 268 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { |
| 269 | array = container_of(map, struct bpf_array, map); |
| 270 | owner_prog_type = array->owner_prog_type; |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 271 | owner_jited = array->owner_jited; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 272 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 273 | |
| 274 | seq_printf(m, |
| 275 | "map_type:\t%u\n" |
| 276 | "key_size:\t%u\n" |
| 277 | "value_size:\t%u\n" |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 278 | "max_entries:\t%u\n" |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 279 | "map_flags:\t%#x\n" |
| 280 | "memlock:\t%llu\n", |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 281 | map->map_type, |
| 282 | map->key_size, |
| 283 | map->value_size, |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 284 | map->max_entries, |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 285 | map->map_flags, |
| 286 | map->pages * 1ULL << PAGE_SHIFT); |
| 287 | |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 288 | if (owner_prog_type) { |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 289 | seq_printf(m, "owner_prog_type:\t%u\n", |
| 290 | owner_prog_type); |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 291 | seq_printf(m, "owner_jited:\t%u\n", |
| 292 | owner_jited); |
| 293 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 294 | } |
| 295 | #endif |
| 296 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 297 | static const struct file_operations bpf_map_fops = { |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 298 | #ifdef CONFIG_PROC_FS |
| 299 | .show_fdinfo = bpf_map_show_fdinfo, |
| 300 | #endif |
| 301 | .release = bpf_map_release, |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 302 | }; |
| 303 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 304 | int bpf_map_new_fd(struct bpf_map *map) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 305 | { |
| 306 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, |
| 307 | O_RDWR | O_CLOEXEC); |
| 308 | } |
| 309 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 310 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
| 311 | #define CHECK_ATTR(CMD) \ |
| 312 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ |
| 313 | sizeof(attr->CMD##_LAST_FIELD), 0, \ |
| 314 | sizeof(*attr) - \ |
| 315 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ |
| 316 | sizeof(attr->CMD##_LAST_FIELD)) != NULL |
| 317 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame^] | 318 | /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes. |
| 319 | * Return 0 on success and < 0 on error. |
| 320 | */ |
| 321 | static int bpf_obj_name_cpy(char *dst, const char *src) |
| 322 | { |
| 323 | const char *end = src + BPF_OBJ_NAME_LEN; |
| 324 | |
| 325 | /* Copy all isalnum() and '_' char */ |
| 326 | while (src < end && *src) { |
| 327 | if (!isalnum(*src) && *src != '_') |
| 328 | return -EINVAL; |
| 329 | *dst++ = *src++; |
| 330 | } |
| 331 | |
| 332 | /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */ |
| 333 | if (src == end) |
| 334 | return -EINVAL; |
| 335 | |
| 336 | /* '\0' terminates dst */ |
| 337 | *dst = 0; |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 342 | #define BPF_MAP_CREATE_LAST_FIELD numa_node |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 343 | /* called via syscall */ |
| 344 | static int map_create(union bpf_attr *attr) |
| 345 | { |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 346 | int numa_node = bpf_map_attr_numa_node(attr); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 347 | struct bpf_map *map; |
| 348 | int err; |
| 349 | |
| 350 | err = CHECK_ATTR(BPF_MAP_CREATE); |
| 351 | if (err) |
| 352 | return -EINVAL; |
| 353 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 354 | if (numa_node != NUMA_NO_NODE && |
Eric Dumazet | 96e5ae4 | 2017-09-04 22:41:02 -0700 | [diff] [blame] | 355 | ((unsigned int)numa_node >= nr_node_ids || |
| 356 | !node_online(numa_node))) |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 357 | return -EINVAL; |
| 358 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 359 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ |
| 360 | map = find_and_alloc_map(attr); |
| 361 | if (IS_ERR(map)) |
| 362 | return PTR_ERR(map); |
| 363 | |
| 364 | atomic_set(&map->refcnt, 1); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 365 | atomic_set(&map->usercnt, 1); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 366 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 367 | err = bpf_map_charge_memlock(map); |
| 368 | if (err) |
Daniel Borkmann | 20b2b24 | 2016-11-04 00:56:31 +0100 | [diff] [blame] | 369 | goto free_map_nouncharge; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 370 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 371 | err = bpf_map_alloc_id(map); |
| 372 | if (err) |
| 373 | goto free_map; |
| 374 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 375 | err = bpf_map_new_fd(map); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 376 | if (err < 0) { |
| 377 | /* failed to allocate fd. |
| 378 | * bpf_map_put() is needed because the above |
| 379 | * bpf_map_alloc_id() has published the map |
| 380 | * to the userspace and the userspace may |
| 381 | * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. |
| 382 | */ |
| 383 | bpf_map_put(map); |
| 384 | return err; |
| 385 | } |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 386 | |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 387 | trace_bpf_map_create(map, err); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 388 | return err; |
| 389 | |
| 390 | free_map: |
Daniel Borkmann | 20b2b24 | 2016-11-04 00:56:31 +0100 | [diff] [blame] | 391 | bpf_map_uncharge_memlock(map); |
| 392 | free_map_nouncharge: |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 393 | map->ops->map_free(map); |
| 394 | return err; |
| 395 | } |
| 396 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 397 | /* if error is returned, fd is released. |
| 398 | * On success caller should complete fd access with matching fdput() |
| 399 | */ |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 400 | struct bpf_map *__bpf_map_get(struct fd f) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 401 | { |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 402 | if (!f.file) |
| 403 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 404 | if (f.file->f_op != &bpf_map_fops) { |
| 405 | fdput(f); |
| 406 | return ERR_PTR(-EINVAL); |
| 407 | } |
| 408 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 409 | return f.file->private_data; |
| 410 | } |
| 411 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 412 | /* prog's and map's refcnt limit */ |
| 413 | #define BPF_MAX_REFCNT 32768 |
| 414 | |
| 415 | struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref) |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 416 | { |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 417 | if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) { |
| 418 | atomic_dec(&map->refcnt); |
| 419 | return ERR_PTR(-EBUSY); |
| 420 | } |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 421 | if (uref) |
| 422 | atomic_inc(&map->usercnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 423 | return map; |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 427 | { |
| 428 | struct fd f = fdget(ufd); |
| 429 | struct bpf_map *map; |
| 430 | |
| 431 | map = __bpf_map_get(f); |
| 432 | if (IS_ERR(map)) |
| 433 | return map; |
| 434 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 435 | map = bpf_map_inc(map, true); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 436 | fdput(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 437 | |
| 438 | return map; |
| 439 | } |
| 440 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 441 | /* map_idr_lock should have been held */ |
| 442 | static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, |
| 443 | bool uref) |
| 444 | { |
| 445 | int refold; |
| 446 | |
| 447 | refold = __atomic_add_unless(&map->refcnt, 1, 0); |
| 448 | |
| 449 | if (refold >= BPF_MAX_REFCNT) { |
| 450 | __bpf_map_put(map, false); |
| 451 | return ERR_PTR(-EBUSY); |
| 452 | } |
| 453 | |
| 454 | if (!refold) |
| 455 | return ERR_PTR(-ENOENT); |
| 456 | |
| 457 | if (uref) |
| 458 | atomic_inc(&map->usercnt); |
| 459 | |
| 460 | return map; |
| 461 | } |
| 462 | |
Alexei Starovoitov | b8cdc05 | 2016-03-09 18:56:49 -0800 | [diff] [blame] | 463 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
| 464 | { |
| 465 | return -ENOTSUPP; |
| 466 | } |
| 467 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 468 | /* last field in 'union bpf_attr' used by this command */ |
| 469 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value |
| 470 | |
| 471 | static int map_lookup_elem(union bpf_attr *attr) |
| 472 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 473 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 474 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 475 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 476 | struct bpf_map *map; |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 477 | void *key, *value, *ptr; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 478 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 479 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 480 | int err; |
| 481 | |
| 482 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) |
| 483 | return -EINVAL; |
| 484 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 485 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 486 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 487 | if (IS_ERR(map)) |
| 488 | return PTR_ERR(map); |
| 489 | |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 490 | key = memdup_user(ukey, map->key_size); |
| 491 | if (IS_ERR(key)) { |
| 492 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 493 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 494 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 495 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 496 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 497 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 498 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
| 499 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 500 | else if (IS_FD_MAP(map)) |
| 501 | value_size = sizeof(u32); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 502 | else |
| 503 | value_size = map->value_size; |
| 504 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 505 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 506 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 507 | if (!value) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 508 | goto free_key; |
| 509 | |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 510 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 511 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 512 | err = bpf_percpu_hash_copy(map, key, value); |
| 513 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 514 | err = bpf_percpu_array_copy(map, key, value); |
Alexei Starovoitov | 557c0c6 | 2016-03-07 21:57:17 -0800 | [diff] [blame] | 515 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
| 516 | err = bpf_stackmap_copy(map, key, value); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 517 | } else if (IS_FD_ARRAY(map)) { |
| 518 | err = bpf_fd_array_map_lookup_elem(map, key, value); |
| 519 | } else if (IS_FD_HASH(map)) { |
| 520 | err = bpf_fd_htab_map_lookup_elem(map, key, value); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 521 | } else { |
| 522 | rcu_read_lock(); |
| 523 | ptr = map->ops->map_lookup_elem(map, key); |
| 524 | if (ptr) |
| 525 | memcpy(value, ptr, value_size); |
| 526 | rcu_read_unlock(); |
| 527 | err = ptr ? 0 : -ENOENT; |
| 528 | } |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 529 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 530 | if (err) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 531 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 532 | |
| 533 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 534 | if (copy_to_user(uvalue, value, value_size) != 0) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 535 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 536 | |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 537 | trace_bpf_map_lookup_elem(map, ufd, key, value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 538 | err = 0; |
| 539 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 540 | free_value: |
| 541 | kfree(value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 542 | free_key: |
| 543 | kfree(key); |
| 544 | err_put: |
| 545 | fdput(f); |
| 546 | return err; |
| 547 | } |
| 548 | |
Alexei Starovoitov | 3274f52 | 2014-11-13 17:36:44 -0800 | [diff] [blame] | 549 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 550 | |
| 551 | static int map_update_elem(union bpf_attr *attr) |
| 552 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 553 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 554 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 555 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 556 | struct bpf_map *map; |
| 557 | void *key, *value; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 558 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 559 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 560 | int err; |
| 561 | |
| 562 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) |
| 563 | return -EINVAL; |
| 564 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 565 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 566 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 567 | if (IS_ERR(map)) |
| 568 | return PTR_ERR(map); |
| 569 | |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 570 | key = memdup_user(ukey, map->key_size); |
| 571 | if (IS_ERR(key)) { |
| 572 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 573 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 574 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 575 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 576 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 577 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 578 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
| 579 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
| 580 | else |
| 581 | value_size = map->value_size; |
| 582 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 583 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 584 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 585 | if (!value) |
| 586 | goto free_key; |
| 587 | |
| 588 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 589 | if (copy_from_user(value, uvalue, value_size) != 0) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 590 | goto free_value; |
| 591 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 592 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering from |
| 593 | * inside bpf map update or delete otherwise deadlocks are possible |
| 594 | */ |
| 595 | preempt_disable(); |
| 596 | __this_cpu_inc(bpf_prog_active); |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 597 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 598 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 599 | err = bpf_percpu_hash_update(map, key, value, attr->flags); |
| 600 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 601 | err = bpf_percpu_array_update(map, key, value, attr->flags); |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 602 | } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 603 | map->map_type == BPF_MAP_TYPE_PROG_ARRAY || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 604 | map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || |
| 605 | map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) { |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 606 | rcu_read_lock(); |
| 607 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, |
| 608 | attr->flags); |
| 609 | rcu_read_unlock(); |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 610 | } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
| 611 | rcu_read_lock(); |
| 612 | err = bpf_fd_htab_map_update_elem(map, f.file, key, value, |
| 613 | attr->flags); |
| 614 | rcu_read_unlock(); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 615 | } else { |
| 616 | rcu_read_lock(); |
| 617 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
| 618 | rcu_read_unlock(); |
| 619 | } |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 620 | __this_cpu_dec(bpf_prog_active); |
| 621 | preempt_enable(); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 622 | |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 623 | if (!err) |
| 624 | trace_bpf_map_update_elem(map, ufd, key, value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 625 | free_value: |
| 626 | kfree(value); |
| 627 | free_key: |
| 628 | kfree(key); |
| 629 | err_put: |
| 630 | fdput(f); |
| 631 | return err; |
| 632 | } |
| 633 | |
| 634 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key |
| 635 | |
| 636 | static int map_delete_elem(union bpf_attr *attr) |
| 637 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 638 | void __user *ukey = u64_to_user_ptr(attr->key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 639 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 640 | struct bpf_map *map; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 641 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 642 | void *key; |
| 643 | int err; |
| 644 | |
| 645 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) |
| 646 | return -EINVAL; |
| 647 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 648 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 649 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 650 | if (IS_ERR(map)) |
| 651 | return PTR_ERR(map); |
| 652 | |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 653 | key = memdup_user(ukey, map->key_size); |
| 654 | if (IS_ERR(key)) { |
| 655 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 656 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 657 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 658 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 659 | preempt_disable(); |
| 660 | __this_cpu_inc(bpf_prog_active); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 661 | rcu_read_lock(); |
| 662 | err = map->ops->map_delete_elem(map, key); |
| 663 | rcu_read_unlock(); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 664 | __this_cpu_dec(bpf_prog_active); |
| 665 | preempt_enable(); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 666 | |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 667 | if (!err) |
| 668 | trace_bpf_map_delete_elem(map, ufd, key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 669 | kfree(key); |
| 670 | err_put: |
| 671 | fdput(f); |
| 672 | return err; |
| 673 | } |
| 674 | |
| 675 | /* last field in 'union bpf_attr' used by this command */ |
| 676 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key |
| 677 | |
| 678 | static int map_get_next_key(union bpf_attr *attr) |
| 679 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 680 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 681 | void __user *unext_key = u64_to_user_ptr(attr->next_key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 682 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 683 | struct bpf_map *map; |
| 684 | void *key, *next_key; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 685 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 686 | int err; |
| 687 | |
| 688 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) |
| 689 | return -EINVAL; |
| 690 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 691 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 692 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 693 | if (IS_ERR(map)) |
| 694 | return PTR_ERR(map); |
| 695 | |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 696 | if (ukey) { |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 697 | key = memdup_user(ukey, map->key_size); |
| 698 | if (IS_ERR(key)) { |
| 699 | err = PTR_ERR(key); |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 700 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 701 | } |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 702 | } else { |
| 703 | key = NULL; |
| 704 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 705 | |
| 706 | err = -ENOMEM; |
| 707 | next_key = kmalloc(map->key_size, GFP_USER); |
| 708 | if (!next_key) |
| 709 | goto free_key; |
| 710 | |
| 711 | rcu_read_lock(); |
| 712 | err = map->ops->map_get_next_key(map, key, next_key); |
| 713 | rcu_read_unlock(); |
| 714 | if (err) |
| 715 | goto free_next_key; |
| 716 | |
| 717 | err = -EFAULT; |
| 718 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) |
| 719 | goto free_next_key; |
| 720 | |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 721 | trace_bpf_map_next_key(map, ufd, key, next_key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 722 | err = 0; |
| 723 | |
| 724 | free_next_key: |
| 725 | kfree(next_key); |
| 726 | free_key: |
| 727 | kfree(key); |
| 728 | err_put: |
| 729 | fdput(f); |
| 730 | return err; |
| 731 | } |
| 732 | |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 733 | static const struct bpf_verifier_ops * const bpf_prog_types[] = { |
| 734 | #define BPF_PROG_TYPE(_id, _ops) \ |
| 735 | [_id] = &_ops, |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 736 | #define BPF_MAP_TYPE(_id, _ops) |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 737 | #include <linux/bpf_types.h> |
| 738 | #undef BPF_PROG_TYPE |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 739 | #undef BPF_MAP_TYPE |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 740 | }; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 741 | |
| 742 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
| 743 | { |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 744 | if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type]) |
| 745 | return -EINVAL; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 746 | |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 747 | prog->aux->ops = bpf_prog_types[type]; |
| 748 | prog->type = type; |
| 749 | return 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* drop refcnt on maps used by eBPF program and free auxilary data */ |
| 753 | static void free_used_maps(struct bpf_prog_aux *aux) |
| 754 | { |
| 755 | int i; |
| 756 | |
| 757 | for (i = 0; i < aux->used_map_cnt; i++) |
| 758 | bpf_map_put(aux->used_maps[i]); |
| 759 | |
| 760 | kfree(aux->used_maps); |
| 761 | } |
| 762 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 763 | int __bpf_prog_charge(struct user_struct *user, u32 pages) |
| 764 | { |
| 765 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 766 | unsigned long user_bufs; |
| 767 | |
| 768 | if (user) { |
| 769 | user_bufs = atomic_long_add_return(pages, &user->locked_vm); |
| 770 | if (user_bufs > memlock_limit) { |
| 771 | atomic_long_sub(pages, &user->locked_vm); |
| 772 | return -EPERM; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | void __bpf_prog_uncharge(struct user_struct *user, u32 pages) |
| 780 | { |
| 781 | if (user) |
| 782 | atomic_long_sub(pages, &user->locked_vm); |
| 783 | } |
| 784 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 785 | static int bpf_prog_charge_memlock(struct bpf_prog *prog) |
| 786 | { |
| 787 | struct user_struct *user = get_current_user(); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 788 | int ret; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 789 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 790 | ret = __bpf_prog_charge(user, prog->pages); |
| 791 | if (ret) { |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 792 | free_uid(user); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 793 | return ret; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 794 | } |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 795 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 796 | prog->aux->user = user; |
| 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) |
| 801 | { |
| 802 | struct user_struct *user = prog->aux->user; |
| 803 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 804 | __bpf_prog_uncharge(user, prog->pages); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 805 | free_uid(user); |
| 806 | } |
| 807 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 808 | static int bpf_prog_alloc_id(struct bpf_prog *prog) |
| 809 | { |
| 810 | int id; |
| 811 | |
| 812 | spin_lock_bh(&prog_idr_lock); |
| 813 | id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); |
| 814 | if (id > 0) |
| 815 | prog->aux->id = id; |
| 816 | spin_unlock_bh(&prog_idr_lock); |
| 817 | |
| 818 | /* id is in [1, INT_MAX) */ |
| 819 | if (WARN_ON_ONCE(!id)) |
| 820 | return -ENOSPC; |
| 821 | |
| 822 | return id > 0 ? 0 : id; |
| 823 | } |
| 824 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 825 | static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock) |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 826 | { |
| 827 | /* cBPF to eBPF migrations are currently not in the idr store. */ |
| 828 | if (!prog->aux->id) |
| 829 | return; |
| 830 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 831 | if (do_idr_lock) |
| 832 | spin_lock_bh(&prog_idr_lock); |
| 833 | else |
| 834 | __acquire(&prog_idr_lock); |
| 835 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 836 | idr_remove(&prog_idr, prog->aux->id); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 837 | |
| 838 | if (do_idr_lock) |
| 839 | spin_unlock_bh(&prog_idr_lock); |
| 840 | else |
| 841 | __release(&prog_idr_lock); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 844 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 845 | { |
| 846 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); |
| 847 | |
| 848 | free_used_maps(aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 849 | bpf_prog_uncharge_memlock(aux->prog); |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 850 | bpf_prog_free(aux->prog); |
| 851 | } |
| 852 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 853 | static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 854 | { |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 855 | if (atomic_dec_and_test(&prog->aux->refcnt)) { |
| 856 | trace_bpf_prog_put_rcu(prog); |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 857 | /* bpf_prog_free_id() must be called first */ |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 858 | bpf_prog_free_id(prog, do_idr_lock); |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 859 | bpf_prog_kallsyms_del(prog); |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 860 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 861 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 862 | } |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 863 | |
| 864 | void bpf_prog_put(struct bpf_prog *prog) |
| 865 | { |
| 866 | __bpf_prog_put(prog, true); |
| 867 | } |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 868 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 869 | |
| 870 | static int bpf_prog_release(struct inode *inode, struct file *filp) |
| 871 | { |
| 872 | struct bpf_prog *prog = filp->private_data; |
| 873 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 874 | bpf_prog_put(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 875 | return 0; |
| 876 | } |
| 877 | |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 878 | #ifdef CONFIG_PROC_FS |
| 879 | static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) |
| 880 | { |
| 881 | const struct bpf_prog *prog = filp->private_data; |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 882 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 883 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 884 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 885 | seq_printf(m, |
| 886 | "prog_type:\t%u\n" |
| 887 | "prog_jited:\t%u\n" |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 888 | "prog_tag:\t%s\n" |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 889 | "memlock:\t%llu\n", |
| 890 | prog->type, |
| 891 | prog->jited, |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 892 | prog_tag, |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 893 | prog->pages * 1ULL << PAGE_SHIFT); |
| 894 | } |
| 895 | #endif |
| 896 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 897 | static const struct file_operations bpf_prog_fops = { |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 898 | #ifdef CONFIG_PROC_FS |
| 899 | .show_fdinfo = bpf_prog_show_fdinfo, |
| 900 | #endif |
| 901 | .release = bpf_prog_release, |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 902 | }; |
| 903 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 904 | int bpf_prog_new_fd(struct bpf_prog *prog) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 905 | { |
| 906 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
| 907 | O_RDWR | O_CLOEXEC); |
| 908 | } |
| 909 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 910 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 911 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 912 | if (!f.file) |
| 913 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 914 | if (f.file->f_op != &bpf_prog_fops) { |
| 915 | fdput(f); |
| 916 | return ERR_PTR(-EINVAL); |
| 917 | } |
| 918 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 919 | return f.file->private_data; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 922 | struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i) |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 923 | { |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 924 | if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) { |
| 925 | atomic_sub(i, &prog->aux->refcnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 926 | return ERR_PTR(-EBUSY); |
| 927 | } |
| 928 | return prog; |
| 929 | } |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 930 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
| 931 | |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 932 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
| 933 | { |
| 934 | /* Only to be used for undoing previous bpf_prog_add() in some |
| 935 | * error path. We still know that another entity in our call |
| 936 | * path holds a reference to the program, thus atomic_sub() can |
| 937 | * be safely used in such cases! |
| 938 | */ |
| 939 | WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0); |
| 940 | } |
| 941 | EXPORT_SYMBOL_GPL(bpf_prog_sub); |
| 942 | |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 943 | struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog) |
| 944 | { |
| 945 | return bpf_prog_add(prog, 1); |
| 946 | } |
Daniel Borkmann | 97bc402 | 2016-11-19 01:45:00 +0100 | [diff] [blame] | 947 | EXPORT_SYMBOL_GPL(bpf_prog_inc); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 948 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 949 | /* prog_idr_lock should have been held */ |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 950 | struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 951 | { |
| 952 | int refold; |
| 953 | |
| 954 | refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0); |
| 955 | |
| 956 | if (refold >= BPF_MAX_REFCNT) { |
| 957 | __bpf_prog_put(prog, false); |
| 958 | return ERR_PTR(-EBUSY); |
| 959 | } |
| 960 | |
| 961 | if (!refold) |
| 962 | return ERR_PTR(-ENOENT); |
| 963 | |
| 964 | return prog; |
| 965 | } |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 966 | EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 967 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 968 | 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] | 969 | { |
| 970 | struct fd f = fdget(ufd); |
| 971 | struct bpf_prog *prog; |
| 972 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 973 | prog = ____bpf_prog_get(f); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 974 | if (IS_ERR(prog)) |
| 975 | return prog; |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 976 | if (type && prog->type != *type) { |
| 977 | prog = ERR_PTR(-EINVAL); |
| 978 | goto out; |
| 979 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 980 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 981 | prog = bpf_prog_inc(prog); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 982 | out: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 983 | fdput(f); |
| 984 | return prog; |
| 985 | } |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 986 | |
| 987 | struct bpf_prog *bpf_prog_get(u32 ufd) |
| 988 | { |
| 989 | return __bpf_prog_get(ufd, NULL); |
| 990 | } |
| 991 | |
| 992 | struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type) |
| 993 | { |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 994 | struct bpf_prog *prog = __bpf_prog_get(ufd, &type); |
| 995 | |
| 996 | if (!IS_ERR(prog)) |
| 997 | trace_bpf_prog_get_type(prog); |
| 998 | return prog; |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 999 | } |
| 1000 | EXPORT_SYMBOL_GPL(bpf_prog_get_type); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1001 | |
| 1002 | /* last field in 'union bpf_attr' used by this command */ |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame^] | 1003 | #define BPF_PROG_LOAD_LAST_FIELD prog_name |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1004 | |
| 1005 | static int bpf_prog_load(union bpf_attr *attr) |
| 1006 | { |
| 1007 | enum bpf_prog_type type = attr->prog_type; |
| 1008 | struct bpf_prog *prog; |
| 1009 | int err; |
| 1010 | char license[128]; |
| 1011 | bool is_gpl; |
| 1012 | |
| 1013 | if (CHECK_ATTR(BPF_PROG_LOAD)) |
| 1014 | return -EINVAL; |
| 1015 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1016 | if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT) |
| 1017 | return -EINVAL; |
| 1018 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1019 | /* copy eBPF program license from user space */ |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1020 | if (strncpy_from_user(license, u64_to_user_ptr(attr->license), |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1021 | sizeof(license) - 1) < 0) |
| 1022 | return -EFAULT; |
| 1023 | license[sizeof(license) - 1] = 0; |
| 1024 | |
| 1025 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
| 1026 | is_gpl = license_is_gpl_compatible(license); |
| 1027 | |
Daniel Borkmann | ef0915c | 2016-12-07 01:15:44 +0100 | [diff] [blame] | 1028 | if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS) |
| 1029 | return -E2BIG; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1030 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 1031 | if (type == BPF_PROG_TYPE_KPROBE && |
| 1032 | attr->kern_version != LINUX_VERSION_CODE) |
| 1033 | return -EINVAL; |
| 1034 | |
Chenbo Feng | 80b7d81 | 2017-05-31 18:16:00 -0700 | [diff] [blame] | 1035 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && |
| 1036 | type != BPF_PROG_TYPE_CGROUP_SKB && |
| 1037 | !capable(CAP_SYS_ADMIN)) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1038 | return -EPERM; |
| 1039 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1040 | /* plain bpf_prog allocation */ |
| 1041 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); |
| 1042 | if (!prog) |
| 1043 | return -ENOMEM; |
| 1044 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1045 | err = bpf_prog_charge_memlock(prog); |
| 1046 | if (err) |
| 1047 | goto free_prog_nouncharge; |
| 1048 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1049 | prog->len = attr->insn_cnt; |
| 1050 | |
| 1051 | err = -EFAULT; |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1052 | if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 1053 | bpf_prog_insn_size(prog)) != 0) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1054 | goto free_prog; |
| 1055 | |
| 1056 | prog->orig_prog = NULL; |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1057 | prog->jited = 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1058 | |
| 1059 | atomic_set(&prog->aux->refcnt, 1); |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1060 | prog->gpl_compatible = is_gpl ? 1 : 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1061 | |
| 1062 | /* find program type: socket_filter vs tracing_filter */ |
| 1063 | err = find_prog_type(type, prog); |
| 1064 | if (err < 0) |
| 1065 | goto free_prog; |
| 1066 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame^] | 1067 | prog->aux->load_time = ktime_get_boot_ns(); |
| 1068 | err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name); |
| 1069 | if (err) |
| 1070 | goto free_prog; |
| 1071 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1072 | /* run eBPF verifier */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 1073 | err = bpf_check(&prog, attr); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1074 | if (err < 0) |
| 1075 | goto free_used_maps; |
| 1076 | |
| 1077 | /* eBPF program is ready to be JITed */ |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 1078 | prog = bpf_prog_select_runtime(prog, &err); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1079 | if (err < 0) |
| 1080 | goto free_used_maps; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1081 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1082 | err = bpf_prog_alloc_id(prog); |
| 1083 | if (err) |
| 1084 | goto free_used_maps; |
| 1085 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1086 | err = bpf_prog_new_fd(prog); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1087 | if (err < 0) { |
| 1088 | /* failed to allocate fd. |
| 1089 | * bpf_prog_put() is needed because the above |
| 1090 | * bpf_prog_alloc_id() has published the prog |
| 1091 | * to the userspace and the userspace may |
| 1092 | * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID. |
| 1093 | */ |
| 1094 | bpf_prog_put(prog); |
| 1095 | return err; |
| 1096 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1097 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 1098 | bpf_prog_kallsyms_add(prog); |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1099 | trace_bpf_prog_load(prog, err); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1100 | return err; |
| 1101 | |
| 1102 | free_used_maps: |
| 1103 | free_used_maps(prog->aux); |
| 1104 | free_prog: |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1105 | bpf_prog_uncharge_memlock(prog); |
| 1106 | free_prog_nouncharge: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1107 | bpf_prog_free(prog); |
| 1108 | return err; |
| 1109 | } |
| 1110 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1111 | #define BPF_OBJ_LAST_FIELD bpf_fd |
| 1112 | |
| 1113 | static int bpf_obj_pin(const union bpf_attr *attr) |
| 1114 | { |
| 1115 | if (CHECK_ATTR(BPF_OBJ)) |
| 1116 | return -EINVAL; |
| 1117 | |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1118 | 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] | 1119 | } |
| 1120 | |
| 1121 | static int bpf_obj_get(const union bpf_attr *attr) |
| 1122 | { |
| 1123 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0) |
| 1124 | return -EINVAL; |
| 1125 | |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1126 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname)); |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1127 | } |
| 1128 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1129 | #ifdef CONFIG_CGROUP_BPF |
| 1130 | |
John Fastabend | 464bc0f | 2017-08-28 07:10:04 -0700 | [diff] [blame] | 1131 | #define BPF_PROG_ATTACH_LAST_FIELD attach_flags |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1132 | |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1133 | static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1134 | { |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1135 | struct bpf_prog *prog = NULL; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1136 | int ufd = attr->target_fd; |
| 1137 | struct bpf_map *map; |
| 1138 | struct fd f; |
| 1139 | int err; |
| 1140 | |
| 1141 | f = fdget(ufd); |
| 1142 | map = __bpf_map_get(f); |
| 1143 | if (IS_ERR(map)) |
| 1144 | return PTR_ERR(map); |
| 1145 | |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1146 | if (attach) { |
| 1147 | prog = bpf_prog_get_type(attr->attach_bpf_fd, |
| 1148 | BPF_PROG_TYPE_SK_SKB); |
| 1149 | if (IS_ERR(prog)) { |
| 1150 | fdput(f); |
| 1151 | return PTR_ERR(prog); |
| 1152 | } |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1155 | err = sock_map_prog(map, prog, attr->attach_type); |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1156 | if (err) { |
| 1157 | fdput(f); |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1158 | if (prog) |
| 1159 | bpf_prog_put(prog); |
Dan Carpenter | ae2b27b | 2017-08-18 10:27:02 +0300 | [diff] [blame] | 1160 | return err; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | fdput(f); |
Dan Carpenter | ae2b27b | 2017-08-18 10:27:02 +0300 | [diff] [blame] | 1164 | return 0; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1165 | } |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1166 | |
| 1167 | static int bpf_prog_attach(const union bpf_attr *attr) |
| 1168 | { |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1169 | enum bpf_prog_type ptype; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1170 | struct bpf_prog *prog; |
| 1171 | struct cgroup *cgrp; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1172 | int ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1173 | |
| 1174 | if (!capable(CAP_NET_ADMIN)) |
| 1175 | return -EPERM; |
| 1176 | |
| 1177 | if (CHECK_ATTR(BPF_PROG_ATTACH)) |
| 1178 | return -EINVAL; |
| 1179 | |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1180 | if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE) |
| 1181 | return -EINVAL; |
| 1182 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1183 | switch (attr->attach_type) { |
| 1184 | case BPF_CGROUP_INET_INGRESS: |
| 1185 | case BPF_CGROUP_INET_EGRESS: |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1186 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1187 | break; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1188 | case BPF_CGROUP_INET_SOCK_CREATE: |
| 1189 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
| 1190 | break; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1191 | case BPF_CGROUP_SOCK_OPS: |
| 1192 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
| 1193 | break; |
John Fastabend | 464bc0f | 2017-08-28 07:10:04 -0700 | [diff] [blame] | 1194 | case BPF_SK_SKB_STREAM_PARSER: |
| 1195 | case BPF_SK_SKB_STREAM_VERDICT: |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1196 | return sockmap_get_from_fd(attr, true); |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1197 | default: |
| 1198 | return -EINVAL; |
| 1199 | } |
| 1200 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1201 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
| 1202 | if (IS_ERR(prog)) |
| 1203 | return PTR_ERR(prog); |
| 1204 | |
| 1205 | cgrp = cgroup_get_from_fd(attr->target_fd); |
| 1206 | if (IS_ERR(cgrp)) { |
| 1207 | bpf_prog_put(prog); |
| 1208 | return PTR_ERR(cgrp); |
| 1209 | } |
| 1210 | |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1211 | ret = cgroup_bpf_update(cgrp, prog, attr->attach_type, |
| 1212 | attr->attach_flags & BPF_F_ALLOW_OVERRIDE); |
| 1213 | if (ret) |
| 1214 | bpf_prog_put(prog); |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1215 | cgroup_put(cgrp); |
| 1216 | |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1217 | return ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | #define BPF_PROG_DETACH_LAST_FIELD attach_type |
| 1221 | |
| 1222 | static int bpf_prog_detach(const union bpf_attr *attr) |
| 1223 | { |
| 1224 | struct cgroup *cgrp; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1225 | int ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1226 | |
| 1227 | if (!capable(CAP_NET_ADMIN)) |
| 1228 | return -EPERM; |
| 1229 | |
| 1230 | if (CHECK_ATTR(BPF_PROG_DETACH)) |
| 1231 | return -EINVAL; |
| 1232 | |
| 1233 | switch (attr->attach_type) { |
| 1234 | case BPF_CGROUP_INET_INGRESS: |
| 1235 | case BPF_CGROUP_INET_EGRESS: |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1236 | case BPF_CGROUP_INET_SOCK_CREATE: |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1237 | case BPF_CGROUP_SOCK_OPS: |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1238 | cgrp = cgroup_get_from_fd(attr->target_fd); |
| 1239 | if (IS_ERR(cgrp)) |
| 1240 | return PTR_ERR(cgrp); |
| 1241 | |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1242 | ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false); |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1243 | cgroup_put(cgrp); |
| 1244 | break; |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1245 | case BPF_SK_SKB_STREAM_PARSER: |
| 1246 | case BPF_SK_SKB_STREAM_VERDICT: |
| 1247 | ret = sockmap_get_from_fd(attr, false); |
| 1248 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1249 | default: |
| 1250 | return -EINVAL; |
| 1251 | } |
| 1252 | |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1253 | return ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1254 | } |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1255 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1256 | #endif /* CONFIG_CGROUP_BPF */ |
| 1257 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 1258 | #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration |
| 1259 | |
| 1260 | static int bpf_prog_test_run(const union bpf_attr *attr, |
| 1261 | union bpf_attr __user *uattr) |
| 1262 | { |
| 1263 | struct bpf_prog *prog; |
| 1264 | int ret = -ENOTSUPP; |
| 1265 | |
| 1266 | if (CHECK_ATTR(BPF_PROG_TEST_RUN)) |
| 1267 | return -EINVAL; |
| 1268 | |
| 1269 | prog = bpf_prog_get(attr->test.prog_fd); |
| 1270 | if (IS_ERR(prog)) |
| 1271 | return PTR_ERR(prog); |
| 1272 | |
| 1273 | if (prog->aux->ops->test_run) |
| 1274 | ret = prog->aux->ops->test_run(prog, attr, uattr); |
| 1275 | |
| 1276 | bpf_prog_put(prog); |
| 1277 | return ret; |
| 1278 | } |
| 1279 | |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 1280 | #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id |
| 1281 | |
| 1282 | static int bpf_obj_get_next_id(const union bpf_attr *attr, |
| 1283 | union bpf_attr __user *uattr, |
| 1284 | struct idr *idr, |
| 1285 | spinlock_t *lock) |
| 1286 | { |
| 1287 | u32 next_id = attr->start_id; |
| 1288 | int err = 0; |
| 1289 | |
| 1290 | if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) |
| 1291 | return -EINVAL; |
| 1292 | |
| 1293 | if (!capable(CAP_SYS_ADMIN)) |
| 1294 | return -EPERM; |
| 1295 | |
| 1296 | next_id++; |
| 1297 | spin_lock_bh(lock); |
| 1298 | if (!idr_get_next(idr, &next_id)) |
| 1299 | err = -ENOENT; |
| 1300 | spin_unlock_bh(lock); |
| 1301 | |
| 1302 | if (!err) |
| 1303 | err = put_user(next_id, &uattr->next_id); |
| 1304 | |
| 1305 | return err; |
| 1306 | } |
| 1307 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1308 | #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id |
| 1309 | |
| 1310 | static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) |
| 1311 | { |
| 1312 | struct bpf_prog *prog; |
| 1313 | u32 id = attr->prog_id; |
| 1314 | int fd; |
| 1315 | |
| 1316 | if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) |
| 1317 | return -EINVAL; |
| 1318 | |
| 1319 | if (!capable(CAP_SYS_ADMIN)) |
| 1320 | return -EPERM; |
| 1321 | |
| 1322 | spin_lock_bh(&prog_idr_lock); |
| 1323 | prog = idr_find(&prog_idr, id); |
| 1324 | if (prog) |
| 1325 | prog = bpf_prog_inc_not_zero(prog); |
| 1326 | else |
| 1327 | prog = ERR_PTR(-ENOENT); |
| 1328 | spin_unlock_bh(&prog_idr_lock); |
| 1329 | |
| 1330 | if (IS_ERR(prog)) |
| 1331 | return PTR_ERR(prog); |
| 1332 | |
| 1333 | fd = bpf_prog_new_fd(prog); |
| 1334 | if (fd < 0) |
| 1335 | bpf_prog_put(prog); |
| 1336 | |
| 1337 | return fd; |
| 1338 | } |
| 1339 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1340 | #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD map_id |
| 1341 | |
| 1342 | static int bpf_map_get_fd_by_id(const union bpf_attr *attr) |
| 1343 | { |
| 1344 | struct bpf_map *map; |
| 1345 | u32 id = attr->map_id; |
| 1346 | int fd; |
| 1347 | |
| 1348 | if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID)) |
| 1349 | return -EINVAL; |
| 1350 | |
| 1351 | if (!capable(CAP_SYS_ADMIN)) |
| 1352 | return -EPERM; |
| 1353 | |
| 1354 | spin_lock_bh(&map_idr_lock); |
| 1355 | map = idr_find(&map_idr, id); |
| 1356 | if (map) |
| 1357 | map = bpf_map_inc_not_zero(map, true); |
| 1358 | else |
| 1359 | map = ERR_PTR(-ENOENT); |
| 1360 | spin_unlock_bh(&map_idr_lock); |
| 1361 | |
| 1362 | if (IS_ERR(map)) |
| 1363 | return PTR_ERR(map); |
| 1364 | |
| 1365 | fd = bpf_map_new_fd(map); |
| 1366 | if (fd < 0) |
| 1367 | bpf_map_put(map); |
| 1368 | |
| 1369 | return fd; |
| 1370 | } |
| 1371 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1372 | static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, |
| 1373 | const union bpf_attr *attr, |
| 1374 | union bpf_attr __user *uattr) |
| 1375 | { |
| 1376 | struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 1377 | struct bpf_prog_info info = {}; |
| 1378 | u32 info_len = attr->info.info_len; |
| 1379 | char __user *uinsns; |
| 1380 | u32 ulen; |
| 1381 | int err; |
| 1382 | |
| 1383 | err = check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
| 1384 | if (err) |
| 1385 | return err; |
| 1386 | info_len = min_t(u32, sizeof(info), info_len); |
| 1387 | |
| 1388 | if (copy_from_user(&info, uinfo, info_len)) |
Daniel Borkmann | 89b0968 | 2017-07-27 21:02:46 +0200 | [diff] [blame] | 1389 | return -EFAULT; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1390 | |
| 1391 | info.type = prog->type; |
| 1392 | info.id = prog->aux->id; |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame^] | 1393 | info.load_time = prog->aux->load_time; |
| 1394 | info.created_by_uid = from_kuid_munged(current_user_ns(), |
| 1395 | prog->aux->user->uid); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1396 | |
| 1397 | memcpy(info.tag, prog->tag, sizeof(prog->tag)); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame^] | 1398 | memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); |
| 1399 | |
| 1400 | ulen = info.nr_map_ids; |
| 1401 | info.nr_map_ids = prog->aux->used_map_cnt; |
| 1402 | ulen = min_t(u32, info.nr_map_ids, ulen); |
| 1403 | if (ulen) { |
| 1404 | u32 *user_map_ids = (u32 *)info.map_ids; |
| 1405 | u32 i; |
| 1406 | |
| 1407 | for (i = 0; i < ulen; i++) |
| 1408 | if (put_user(prog->aux->used_maps[i]->id, |
| 1409 | &user_map_ids[i])) |
| 1410 | return -EFAULT; |
| 1411 | } |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1412 | |
| 1413 | if (!capable(CAP_SYS_ADMIN)) { |
| 1414 | info.jited_prog_len = 0; |
| 1415 | info.xlated_prog_len = 0; |
| 1416 | goto done; |
| 1417 | } |
| 1418 | |
| 1419 | ulen = info.jited_prog_len; |
| 1420 | info.jited_prog_len = prog->jited_len; |
| 1421 | if (info.jited_prog_len && ulen) { |
| 1422 | uinsns = u64_to_user_ptr(info.jited_prog_insns); |
| 1423 | ulen = min_t(u32, info.jited_prog_len, ulen); |
| 1424 | if (copy_to_user(uinsns, prog->bpf_func, ulen)) |
| 1425 | return -EFAULT; |
| 1426 | } |
| 1427 | |
| 1428 | ulen = info.xlated_prog_len; |
Daniel Borkmann | 9975a54 | 2017-07-28 17:05:25 +0200 | [diff] [blame] | 1429 | info.xlated_prog_len = bpf_prog_insn_size(prog); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1430 | if (info.xlated_prog_len && ulen) { |
| 1431 | uinsns = u64_to_user_ptr(info.xlated_prog_insns); |
| 1432 | ulen = min_t(u32, info.xlated_prog_len, ulen); |
| 1433 | if (copy_to_user(uinsns, prog->insnsi, ulen)) |
| 1434 | return -EFAULT; |
| 1435 | } |
| 1436 | |
| 1437 | done: |
| 1438 | if (copy_to_user(uinfo, &info, info_len) || |
| 1439 | put_user(info_len, &uattr->info.info_len)) |
| 1440 | return -EFAULT; |
| 1441 | |
| 1442 | return 0; |
| 1443 | } |
| 1444 | |
| 1445 | static int bpf_map_get_info_by_fd(struct bpf_map *map, |
| 1446 | const union bpf_attr *attr, |
| 1447 | union bpf_attr __user *uattr) |
| 1448 | { |
| 1449 | struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 1450 | struct bpf_map_info info = {}; |
| 1451 | u32 info_len = attr->info.info_len; |
| 1452 | int err; |
| 1453 | |
| 1454 | err = check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
| 1455 | if (err) |
| 1456 | return err; |
| 1457 | info_len = min_t(u32, sizeof(info), info_len); |
| 1458 | |
| 1459 | info.type = map->map_type; |
| 1460 | info.id = map->id; |
| 1461 | info.key_size = map->key_size; |
| 1462 | info.value_size = map->value_size; |
| 1463 | info.max_entries = map->max_entries; |
| 1464 | info.map_flags = map->map_flags; |
| 1465 | |
| 1466 | if (copy_to_user(uinfo, &info, info_len) || |
| 1467 | put_user(info_len, &uattr->info.info_len)) |
| 1468 | return -EFAULT; |
| 1469 | |
| 1470 | return 0; |
| 1471 | } |
| 1472 | |
| 1473 | #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info |
| 1474 | |
| 1475 | static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, |
| 1476 | union bpf_attr __user *uattr) |
| 1477 | { |
| 1478 | int ufd = attr->info.bpf_fd; |
| 1479 | struct fd f; |
| 1480 | int err; |
| 1481 | |
| 1482 | if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) |
| 1483 | return -EINVAL; |
| 1484 | |
| 1485 | f = fdget(ufd); |
| 1486 | if (!f.file) |
| 1487 | return -EBADFD; |
| 1488 | |
| 1489 | if (f.file->f_op == &bpf_prog_fops) |
| 1490 | err = bpf_prog_get_info_by_fd(f.file->private_data, attr, |
| 1491 | uattr); |
| 1492 | else if (f.file->f_op == &bpf_map_fops) |
| 1493 | err = bpf_map_get_info_by_fd(f.file->private_data, attr, |
| 1494 | uattr); |
| 1495 | else |
| 1496 | err = -EINVAL; |
| 1497 | |
| 1498 | fdput(f); |
| 1499 | return err; |
| 1500 | } |
| 1501 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 1502 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
| 1503 | { |
| 1504 | union bpf_attr attr = {}; |
| 1505 | int err; |
| 1506 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1507 | if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 1508 | return -EPERM; |
| 1509 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1510 | err = check_uarg_tail_zero(uattr, sizeof(attr), size); |
| 1511 | if (err) |
| 1512 | return err; |
| 1513 | size = min_t(u32, size, sizeof(attr)); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 1514 | |
| 1515 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ |
| 1516 | if (copy_from_user(&attr, uattr, size) != 0) |
| 1517 | return -EFAULT; |
| 1518 | |
| 1519 | switch (cmd) { |
| 1520 | case BPF_MAP_CREATE: |
| 1521 | err = map_create(&attr); |
| 1522 | break; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1523 | case BPF_MAP_LOOKUP_ELEM: |
| 1524 | err = map_lookup_elem(&attr); |
| 1525 | break; |
| 1526 | case BPF_MAP_UPDATE_ELEM: |
| 1527 | err = map_update_elem(&attr); |
| 1528 | break; |
| 1529 | case BPF_MAP_DELETE_ELEM: |
| 1530 | err = map_delete_elem(&attr); |
| 1531 | break; |
| 1532 | case BPF_MAP_GET_NEXT_KEY: |
| 1533 | err = map_get_next_key(&attr); |
| 1534 | break; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1535 | case BPF_PROG_LOAD: |
| 1536 | err = bpf_prog_load(&attr); |
| 1537 | break; |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1538 | case BPF_OBJ_PIN: |
| 1539 | err = bpf_obj_pin(&attr); |
| 1540 | break; |
| 1541 | case BPF_OBJ_GET: |
| 1542 | err = bpf_obj_get(&attr); |
| 1543 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1544 | #ifdef CONFIG_CGROUP_BPF |
| 1545 | case BPF_PROG_ATTACH: |
| 1546 | err = bpf_prog_attach(&attr); |
| 1547 | break; |
| 1548 | case BPF_PROG_DETACH: |
| 1549 | err = bpf_prog_detach(&attr); |
| 1550 | break; |
| 1551 | #endif |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 1552 | case BPF_PROG_TEST_RUN: |
| 1553 | err = bpf_prog_test_run(&attr, uattr); |
| 1554 | break; |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 1555 | case BPF_PROG_GET_NEXT_ID: |
| 1556 | err = bpf_obj_get_next_id(&attr, uattr, |
| 1557 | &prog_idr, &prog_idr_lock); |
| 1558 | break; |
| 1559 | case BPF_MAP_GET_NEXT_ID: |
| 1560 | err = bpf_obj_get_next_id(&attr, uattr, |
| 1561 | &map_idr, &map_idr_lock); |
| 1562 | break; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1563 | case BPF_PROG_GET_FD_BY_ID: |
| 1564 | err = bpf_prog_get_fd_by_id(&attr); |
| 1565 | break; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1566 | case BPF_MAP_GET_FD_BY_ID: |
| 1567 | err = bpf_map_get_fd_by_id(&attr); |
| 1568 | break; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1569 | case BPF_OBJ_GET_INFO_BY_FD: |
| 1570 | err = bpf_obj_get_info_by_fd(&attr, uattr); |
| 1571 | break; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 1572 | default: |
| 1573 | err = -EINVAL; |
| 1574 | break; |
| 1575 | } |
| 1576 | |
| 1577 | return err; |
| 1578 | } |