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 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 37 | #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) |
| 38 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 39 | DEFINE_PER_CPU(int, bpf_prog_active); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 40 | static DEFINE_IDR(prog_idr); |
| 41 | static DEFINE_SPINLOCK(prog_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 42 | static DEFINE_IDR(map_idr); |
| 43 | static DEFINE_SPINLOCK(map_idr_lock); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 44 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 45 | int sysctl_unprivileged_bpf_disabled __read_mostly; |
| 46 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 47 | static const struct bpf_map_ops * const bpf_map_types[] = { |
| 48 | #define BPF_PROG_TYPE(_id, _ops) |
| 49 | #define BPF_MAP_TYPE(_id, _ops) \ |
| 50 | [_id] = &_ops, |
| 51 | #include <linux/bpf_types.h> |
| 52 | #undef BPF_PROG_TYPE |
| 53 | #undef BPF_MAP_TYPE |
| 54 | }; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 55 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 56 | /* |
| 57 | * If we're handed a bigger struct than we know of, ensure all the unknown bits |
| 58 | * are 0 - i.e. new user-space does not rely on any kernel feature extensions |
| 59 | * we don't know about yet. |
| 60 | * |
| 61 | * There is a ToCToU between this function call and the following |
| 62 | * copy_from_user() call. However, this is not a concern since this function is |
| 63 | * meant to be a future-proofing of bits. |
| 64 | */ |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 65 | static int check_uarg_tail_zero(void __user *uaddr, |
| 66 | size_t expected_size, |
| 67 | size_t actual_size) |
| 68 | { |
| 69 | unsigned char __user *addr; |
| 70 | unsigned char __user *end; |
| 71 | unsigned char val; |
| 72 | int err; |
| 73 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 74 | if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ |
| 75 | return -E2BIG; |
| 76 | |
| 77 | if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size))) |
| 78 | return -EFAULT; |
| 79 | |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 80 | if (actual_size <= expected_size) |
| 81 | return 0; |
| 82 | |
| 83 | addr = uaddr + expected_size; |
| 84 | end = uaddr + actual_size; |
| 85 | |
| 86 | for (; addr < end; addr++) { |
| 87 | err = get_user(val, addr); |
| 88 | if (err) |
| 89 | return err; |
| 90 | if (val) |
| 91 | return -E2BIG; |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 97 | const struct bpf_map_ops bpf_map_offload_ops = { |
| 98 | .map_alloc = bpf_map_offload_map_alloc, |
| 99 | .map_free = bpf_map_offload_map_free, |
| 100 | }; |
| 101 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 102 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
| 103 | { |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 104 | const struct bpf_map_ops *ops; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 105 | struct bpf_map *map; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 106 | int err; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 107 | |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 108 | if (attr->map_type >= ARRAY_SIZE(bpf_map_types)) |
| 109 | return ERR_PTR(-EINVAL); |
| 110 | ops = bpf_map_types[attr->map_type]; |
| 111 | if (!ops) |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 112 | return ERR_PTR(-EINVAL); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 113 | |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 114 | if (ops->map_alloc_check) { |
| 115 | err = ops->map_alloc_check(attr); |
| 116 | if (err) |
| 117 | return ERR_PTR(err); |
| 118 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 119 | if (attr->map_ifindex) |
| 120 | ops = &bpf_map_offload_ops; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 121 | map = ops->map_alloc(attr); |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 122 | if (IS_ERR(map)) |
| 123 | return map; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 124 | map->ops = ops; |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 125 | map->map_type = attr->map_type; |
| 126 | return map; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 129 | void *bpf_map_area_alloc(size_t size, int numa_node) |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 130 | { |
| 131 | /* We definitely need __GFP_NORETRY, so OOM killer doesn't |
| 132 | * trigger under memory pressure as we really just want to |
| 133 | * fail instead. |
| 134 | */ |
| 135 | const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO; |
| 136 | void *area; |
| 137 | |
| 138 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 139 | area = kmalloc_node(size, GFP_USER | flags, numa_node); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 140 | if (area != NULL) |
| 141 | return area; |
| 142 | } |
| 143 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 144 | return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags, |
| 145 | __builtin_return_address(0)); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void bpf_map_area_free(void *area) |
| 149 | { |
| 150 | kvfree(area); |
| 151 | } |
| 152 | |
Jakub Kicinski | bd47564 | 2018-01-11 20:29:06 -0800 | [diff] [blame] | 153 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) |
| 154 | { |
| 155 | map->map_type = attr->map_type; |
| 156 | map->key_size = attr->key_size; |
| 157 | map->value_size = attr->value_size; |
| 158 | map->max_entries = attr->max_entries; |
| 159 | map->map_flags = attr->map_flags; |
| 160 | map->numa_node = bpf_map_attr_numa_node(attr); |
| 161 | } |
| 162 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 163 | int bpf_map_precharge_memlock(u32 pages) |
| 164 | { |
| 165 | struct user_struct *user = get_current_user(); |
| 166 | unsigned long memlock_limit, cur; |
| 167 | |
| 168 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 169 | cur = atomic_long_read(&user->locked_vm); |
| 170 | free_uid(user); |
| 171 | if (cur + pages > memlock_limit) |
| 172 | return -EPERM; |
| 173 | return 0; |
| 174 | } |
| 175 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 176 | static int bpf_map_charge_memlock(struct bpf_map *map) |
| 177 | { |
| 178 | struct user_struct *user = get_current_user(); |
| 179 | unsigned long memlock_limit; |
| 180 | |
| 181 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 182 | |
| 183 | atomic_long_add(map->pages, &user->locked_vm); |
| 184 | |
| 185 | if (atomic_long_read(&user->locked_vm) > memlock_limit) { |
| 186 | atomic_long_sub(map->pages, &user->locked_vm); |
| 187 | free_uid(user); |
| 188 | return -EPERM; |
| 189 | } |
| 190 | map->user = user; |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static void bpf_map_uncharge_memlock(struct bpf_map *map) |
| 195 | { |
| 196 | struct user_struct *user = map->user; |
| 197 | |
| 198 | atomic_long_sub(map->pages, &user->locked_vm); |
| 199 | free_uid(user); |
| 200 | } |
| 201 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 202 | static int bpf_map_alloc_id(struct bpf_map *map) |
| 203 | { |
| 204 | int id; |
| 205 | |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 206 | idr_preload(GFP_KERNEL); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 207 | spin_lock_bh(&map_idr_lock); |
| 208 | id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); |
| 209 | if (id > 0) |
| 210 | map->id = id; |
| 211 | spin_unlock_bh(&map_idr_lock); |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 212 | idr_preload_end(); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 213 | |
| 214 | if (WARN_ON_ONCE(!id)) |
| 215 | return -ENOSPC; |
| 216 | |
| 217 | return id > 0 ? 0 : id; |
| 218 | } |
| 219 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 220 | 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] | 221 | { |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 222 | unsigned long flags; |
| 223 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 224 | /* Offloaded maps are removed from the IDR store when their device |
| 225 | * disappears - even if someone holds an fd to them they are unusable, |
| 226 | * the memory is gone, all ops will fail; they are simply waiting for |
| 227 | * refcnt to drop to be freed. |
| 228 | */ |
| 229 | if (!map->id) |
| 230 | return; |
| 231 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 232 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 233 | spin_lock_irqsave(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 234 | else |
| 235 | __acquire(&map_idr_lock); |
| 236 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 237 | idr_remove(&map_idr, map->id); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 238 | map->id = 0; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 239 | |
| 240 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 241 | spin_unlock_irqrestore(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 242 | else |
| 243 | __release(&map_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 246 | /* called from workqueue */ |
| 247 | static void bpf_map_free_deferred(struct work_struct *work) |
| 248 | { |
| 249 | struct bpf_map *map = container_of(work, struct bpf_map, work); |
| 250 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 251 | bpf_map_uncharge_memlock(map); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 252 | security_bpf_map_free(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 253 | /* implementation dependent freeing */ |
| 254 | map->ops->map_free(map); |
| 255 | } |
| 256 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 257 | static void bpf_map_put_uref(struct bpf_map *map) |
| 258 | { |
| 259 | if (atomic_dec_and_test(&map->usercnt)) { |
| 260 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) |
| 261 | bpf_fd_array_map_clear(map); |
| 262 | } |
| 263 | } |
| 264 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 265 | /* decrement map refcnt and schedule it for freeing via workqueue |
| 266 | * (unrelying map implementation ops->map_free() might sleep) |
| 267 | */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 268 | 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] | 269 | { |
| 270 | if (atomic_dec_and_test(&map->refcnt)) { |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 271 | /* bpf_map_free_id() must be called first */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 272 | bpf_map_free_id(map, do_idr_lock); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 273 | INIT_WORK(&map->work, bpf_map_free_deferred); |
| 274 | schedule_work(&map->work); |
| 275 | } |
| 276 | } |
| 277 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 278 | void bpf_map_put(struct bpf_map *map) |
| 279 | { |
| 280 | __bpf_map_put(map, true); |
| 281 | } |
| 282 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 283 | void bpf_map_put_with_uref(struct bpf_map *map) |
| 284 | { |
| 285 | bpf_map_put_uref(map); |
| 286 | bpf_map_put(map); |
| 287 | } |
| 288 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 289 | static int bpf_map_release(struct inode *inode, struct file *filp) |
| 290 | { |
Daniel Borkmann | 61d1b6a | 2016-06-15 22:47:12 +0200 | [diff] [blame] | 291 | struct bpf_map *map = filp->private_data; |
| 292 | |
| 293 | if (map->ops->map_release) |
| 294 | map->ops->map_release(map, filp); |
| 295 | |
| 296 | bpf_map_put_with_uref(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 300 | #ifdef CONFIG_PROC_FS |
| 301 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) |
| 302 | { |
| 303 | const struct bpf_map *map = filp->private_data; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 304 | const struct bpf_array *array; |
| 305 | u32 owner_prog_type = 0; |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 306 | u32 owner_jited = 0; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 307 | |
| 308 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { |
| 309 | array = container_of(map, struct bpf_array, map); |
| 310 | owner_prog_type = array->owner_prog_type; |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 311 | owner_jited = array->owner_jited; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 312 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 313 | |
| 314 | seq_printf(m, |
| 315 | "map_type:\t%u\n" |
| 316 | "key_size:\t%u\n" |
| 317 | "value_size:\t%u\n" |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 318 | "max_entries:\t%u\n" |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 319 | "map_flags:\t%#x\n" |
| 320 | "memlock:\t%llu\n", |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 321 | map->map_type, |
| 322 | map->key_size, |
| 323 | map->value_size, |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 324 | map->max_entries, |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 325 | map->map_flags, |
| 326 | map->pages * 1ULL << PAGE_SHIFT); |
| 327 | |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 328 | if (owner_prog_type) { |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 329 | seq_printf(m, "owner_prog_type:\t%u\n", |
| 330 | owner_prog_type); |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 331 | seq_printf(m, "owner_jited:\t%u\n", |
| 332 | owner_jited); |
| 333 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 334 | } |
| 335 | #endif |
| 336 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 337 | static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, |
| 338 | loff_t *ppos) |
| 339 | { |
| 340 | /* We need this handler such that alloc_file() enables |
| 341 | * f_mode with FMODE_CAN_READ. |
| 342 | */ |
| 343 | return -EINVAL; |
| 344 | } |
| 345 | |
| 346 | static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, |
| 347 | size_t siz, loff_t *ppos) |
| 348 | { |
| 349 | /* We need this handler such that alloc_file() enables |
| 350 | * f_mode with FMODE_CAN_WRITE. |
| 351 | */ |
| 352 | return -EINVAL; |
| 353 | } |
| 354 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 355 | const struct file_operations bpf_map_fops = { |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 356 | #ifdef CONFIG_PROC_FS |
| 357 | .show_fdinfo = bpf_map_show_fdinfo, |
| 358 | #endif |
| 359 | .release = bpf_map_release, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 360 | .read = bpf_dummy_read, |
| 361 | .write = bpf_dummy_write, |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 362 | }; |
| 363 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 364 | int bpf_map_new_fd(struct bpf_map *map, int flags) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 365 | { |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 366 | int ret; |
| 367 | |
| 368 | ret = security_bpf_map(map, OPEN_FMODE(flags)); |
| 369 | if (ret < 0) |
| 370 | return ret; |
| 371 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 372 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 373 | flags | O_CLOEXEC); |
| 374 | } |
| 375 | |
| 376 | int bpf_get_file_flag(int flags) |
| 377 | { |
| 378 | if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) |
| 379 | return -EINVAL; |
| 380 | if (flags & BPF_F_RDONLY) |
| 381 | return O_RDONLY; |
| 382 | if (flags & BPF_F_WRONLY) |
| 383 | return O_WRONLY; |
| 384 | return O_RDWR; |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 387 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
| 388 | #define CHECK_ATTR(CMD) \ |
| 389 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ |
| 390 | sizeof(attr->CMD##_LAST_FIELD), 0, \ |
| 391 | sizeof(*attr) - \ |
| 392 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ |
| 393 | sizeof(attr->CMD##_LAST_FIELD)) != NULL |
| 394 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 395 | /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes. |
| 396 | * Return 0 on success and < 0 on error. |
| 397 | */ |
| 398 | static int bpf_obj_name_cpy(char *dst, const char *src) |
| 399 | { |
| 400 | const char *end = src + BPF_OBJ_NAME_LEN; |
| 401 | |
Martin KaFai Lau | 473d973 | 2017-10-05 21:52:11 -0700 | [diff] [blame] | 402 | memset(dst, 0, BPF_OBJ_NAME_LEN); |
| 403 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 404 | /* Copy all isalnum() and '_' char */ |
| 405 | while (src < end && *src) { |
| 406 | if (!isalnum(*src) && *src != '_') |
| 407 | return -EINVAL; |
| 408 | *dst++ = *src++; |
| 409 | } |
| 410 | |
| 411 | /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */ |
| 412 | if (src == end) |
| 413 | return -EINVAL; |
| 414 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 418 | #define BPF_MAP_CREATE_LAST_FIELD map_ifindex |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 419 | /* called via syscall */ |
| 420 | static int map_create(union bpf_attr *attr) |
| 421 | { |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 422 | int numa_node = bpf_map_attr_numa_node(attr); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 423 | struct bpf_map *map; |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 424 | int f_flags; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 425 | int err; |
| 426 | |
| 427 | err = CHECK_ATTR(BPF_MAP_CREATE); |
| 428 | if (err) |
| 429 | return -EINVAL; |
| 430 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 431 | f_flags = bpf_get_file_flag(attr->map_flags); |
| 432 | if (f_flags < 0) |
| 433 | return f_flags; |
| 434 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 435 | if (numa_node != NUMA_NO_NODE && |
Eric Dumazet | 96e5ae4 | 2017-09-04 22:41:02 -0700 | [diff] [blame] | 436 | ((unsigned int)numa_node >= nr_node_ids || |
| 437 | !node_online(numa_node))) |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 438 | return -EINVAL; |
| 439 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 440 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ |
| 441 | map = find_and_alloc_map(attr); |
| 442 | if (IS_ERR(map)) |
| 443 | return PTR_ERR(map); |
| 444 | |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 445 | err = bpf_obj_name_cpy(map->name, attr->map_name); |
| 446 | if (err) |
| 447 | goto free_map_nouncharge; |
| 448 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 449 | atomic_set(&map->refcnt, 1); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 450 | atomic_set(&map->usercnt, 1); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 451 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 452 | err = security_bpf_map_alloc(map); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 453 | if (err) |
Daniel Borkmann | 20b2b24 | 2016-11-04 00:56:31 +0100 | [diff] [blame] | 454 | goto free_map_nouncharge; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 455 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 456 | err = bpf_map_charge_memlock(map); |
| 457 | if (err) |
| 458 | goto free_map_sec; |
| 459 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 460 | err = bpf_map_alloc_id(map); |
| 461 | if (err) |
| 462 | goto free_map; |
| 463 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 464 | err = bpf_map_new_fd(map, f_flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 465 | if (err < 0) { |
| 466 | /* failed to allocate fd. |
| 467 | * bpf_map_put() is needed because the above |
| 468 | * bpf_map_alloc_id() has published the map |
| 469 | * to the userspace and the userspace may |
| 470 | * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. |
| 471 | */ |
| 472 | bpf_map_put(map); |
| 473 | return err; |
| 474 | } |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 475 | |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 476 | trace_bpf_map_create(map, err); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 477 | return err; |
| 478 | |
| 479 | free_map: |
Daniel Borkmann | 20b2b24 | 2016-11-04 00:56:31 +0100 | [diff] [blame] | 480 | bpf_map_uncharge_memlock(map); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 481 | free_map_sec: |
| 482 | security_bpf_map_free(map); |
Daniel Borkmann | 20b2b24 | 2016-11-04 00:56:31 +0100 | [diff] [blame] | 483 | free_map_nouncharge: |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 484 | map->ops->map_free(map); |
| 485 | return err; |
| 486 | } |
| 487 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 488 | /* if error is returned, fd is released. |
| 489 | * On success caller should complete fd access with matching fdput() |
| 490 | */ |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 491 | struct bpf_map *__bpf_map_get(struct fd f) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 492 | { |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 493 | if (!f.file) |
| 494 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 495 | if (f.file->f_op != &bpf_map_fops) { |
| 496 | fdput(f); |
| 497 | return ERR_PTR(-EINVAL); |
| 498 | } |
| 499 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 500 | return f.file->private_data; |
| 501 | } |
| 502 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 503 | /* prog's and map's refcnt limit */ |
| 504 | #define BPF_MAX_REFCNT 32768 |
| 505 | |
| 506 | struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref) |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 507 | { |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 508 | if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) { |
| 509 | atomic_dec(&map->refcnt); |
| 510 | return ERR_PTR(-EBUSY); |
| 511 | } |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 512 | if (uref) |
| 513 | atomic_inc(&map->usercnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 514 | return map; |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 518 | { |
| 519 | struct fd f = fdget(ufd); |
| 520 | struct bpf_map *map; |
| 521 | |
| 522 | map = __bpf_map_get(f); |
| 523 | if (IS_ERR(map)) |
| 524 | return map; |
| 525 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 526 | map = bpf_map_inc(map, true); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 527 | fdput(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 528 | |
| 529 | return map; |
| 530 | } |
| 531 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 532 | /* map_idr_lock should have been held */ |
| 533 | static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, |
| 534 | bool uref) |
| 535 | { |
| 536 | int refold; |
| 537 | |
| 538 | refold = __atomic_add_unless(&map->refcnt, 1, 0); |
| 539 | |
| 540 | if (refold >= BPF_MAX_REFCNT) { |
| 541 | __bpf_map_put(map, false); |
| 542 | return ERR_PTR(-EBUSY); |
| 543 | } |
| 544 | |
| 545 | if (!refold) |
| 546 | return ERR_PTR(-ENOENT); |
| 547 | |
| 548 | if (uref) |
| 549 | atomic_inc(&map->usercnt); |
| 550 | |
| 551 | return map; |
| 552 | } |
| 553 | |
Alexei Starovoitov | b8cdc05 | 2016-03-09 18:56:49 -0800 | [diff] [blame] | 554 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
| 555 | { |
| 556 | return -ENOTSUPP; |
| 557 | } |
| 558 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 559 | /* last field in 'union bpf_attr' used by this command */ |
| 560 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value |
| 561 | |
| 562 | static int map_lookup_elem(union bpf_attr *attr) |
| 563 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 564 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 565 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 566 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 567 | struct bpf_map *map; |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 568 | void *key, *value, *ptr; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 569 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 570 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 571 | int err; |
| 572 | |
| 573 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) |
| 574 | return -EINVAL; |
| 575 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 576 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 577 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 578 | if (IS_ERR(map)) |
| 579 | return PTR_ERR(map); |
| 580 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 581 | if (!(f.file->f_mode & FMODE_CAN_READ)) { |
| 582 | err = -EPERM; |
| 583 | goto err_put; |
| 584 | } |
| 585 | |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 586 | key = memdup_user(ukey, map->key_size); |
| 587 | if (IS_ERR(key)) { |
| 588 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 589 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 590 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 591 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 592 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 593 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 594 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
| 595 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 596 | else if (IS_FD_MAP(map)) |
| 597 | value_size = sizeof(u32); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 598 | else |
| 599 | value_size = map->value_size; |
| 600 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 601 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 602 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 603 | if (!value) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 604 | goto free_key; |
| 605 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 606 | if (bpf_map_is_dev_bound(map)) { |
| 607 | err = bpf_map_offload_lookup_elem(map, key, value); |
| 608 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 609 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 610 | err = bpf_percpu_hash_copy(map, key, value); |
| 611 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 612 | err = bpf_percpu_array_copy(map, key, value); |
Alexei Starovoitov | 557c0c6 | 2016-03-07 21:57:17 -0800 | [diff] [blame] | 613 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
| 614 | err = bpf_stackmap_copy(map, key, value); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 615 | } else if (IS_FD_ARRAY(map)) { |
| 616 | err = bpf_fd_array_map_lookup_elem(map, key, value); |
| 617 | } else if (IS_FD_HASH(map)) { |
| 618 | err = bpf_fd_htab_map_lookup_elem(map, key, value); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 619 | } else { |
| 620 | rcu_read_lock(); |
| 621 | ptr = map->ops->map_lookup_elem(map, key); |
| 622 | if (ptr) |
| 623 | memcpy(value, ptr, value_size); |
| 624 | rcu_read_unlock(); |
| 625 | err = ptr ? 0 : -ENOENT; |
| 626 | } |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 627 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 628 | if (err) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 629 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 630 | |
| 631 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 632 | if (copy_to_user(uvalue, value, value_size) != 0) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 633 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 634 | |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 635 | trace_bpf_map_lookup_elem(map, ufd, key, value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 636 | err = 0; |
| 637 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 638 | free_value: |
| 639 | kfree(value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 640 | free_key: |
| 641 | kfree(key); |
| 642 | err_put: |
| 643 | fdput(f); |
| 644 | return err; |
| 645 | } |
| 646 | |
Alexei Starovoitov | 3274f52 | 2014-11-13 17:36:44 -0800 | [diff] [blame] | 647 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 648 | |
| 649 | static int map_update_elem(union bpf_attr *attr) |
| 650 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 651 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 652 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 653 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 654 | struct bpf_map *map; |
| 655 | void *key, *value; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 656 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 657 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 658 | int err; |
| 659 | |
| 660 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) |
| 661 | return -EINVAL; |
| 662 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 663 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 664 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 665 | if (IS_ERR(map)) |
| 666 | return PTR_ERR(map); |
| 667 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 668 | if (!(f.file->f_mode & FMODE_CAN_WRITE)) { |
| 669 | err = -EPERM; |
| 670 | goto err_put; |
| 671 | } |
| 672 | |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 673 | key = memdup_user(ukey, map->key_size); |
| 674 | if (IS_ERR(key)) { |
| 675 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 676 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 677 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 678 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 679 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 680 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 681 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
| 682 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
| 683 | else |
| 684 | value_size = map->value_size; |
| 685 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 686 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 687 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 688 | if (!value) |
| 689 | goto free_key; |
| 690 | |
| 691 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 692 | if (copy_from_user(value, uvalue, value_size) != 0) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 693 | goto free_value; |
| 694 | |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 695 | /* Need to create a kthread, thus must support schedule */ |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 696 | if (bpf_map_is_dev_bound(map)) { |
| 697 | err = bpf_map_offload_update_elem(map, key, value, attr->flags); |
| 698 | goto out; |
| 699 | } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) { |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 700 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
| 701 | goto out; |
| 702 | } |
| 703 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 704 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering from |
| 705 | * inside bpf map update or delete otherwise deadlocks are possible |
| 706 | */ |
| 707 | preempt_disable(); |
| 708 | __this_cpu_inc(bpf_prog_active); |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 709 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 710 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 711 | err = bpf_percpu_hash_update(map, key, value, attr->flags); |
| 712 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 713 | err = bpf_percpu_array_update(map, key, value, attr->flags); |
Mickaël Salaün | 9c147b5 | 2018-01-26 00:54:02 +0100 | [diff] [blame] | 714 | } else if (IS_FD_ARRAY(map)) { |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 715 | rcu_read_lock(); |
| 716 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, |
| 717 | attr->flags); |
| 718 | rcu_read_unlock(); |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 719 | } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
| 720 | rcu_read_lock(); |
| 721 | err = bpf_fd_htab_map_update_elem(map, f.file, key, value, |
| 722 | attr->flags); |
| 723 | rcu_read_unlock(); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 724 | } else { |
| 725 | rcu_read_lock(); |
| 726 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
| 727 | rcu_read_unlock(); |
| 728 | } |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 729 | __this_cpu_dec(bpf_prog_active); |
| 730 | preempt_enable(); |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 731 | out: |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 732 | if (!err) |
| 733 | trace_bpf_map_update_elem(map, ufd, key, value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 734 | free_value: |
| 735 | kfree(value); |
| 736 | free_key: |
| 737 | kfree(key); |
| 738 | err_put: |
| 739 | fdput(f); |
| 740 | return err; |
| 741 | } |
| 742 | |
| 743 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key |
| 744 | |
| 745 | static int map_delete_elem(union bpf_attr *attr) |
| 746 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 747 | void __user *ukey = u64_to_user_ptr(attr->key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 748 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 749 | struct bpf_map *map; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 750 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 751 | void *key; |
| 752 | int err; |
| 753 | |
| 754 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) |
| 755 | return -EINVAL; |
| 756 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 757 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 758 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 759 | if (IS_ERR(map)) |
| 760 | return PTR_ERR(map); |
| 761 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 762 | if (!(f.file->f_mode & FMODE_CAN_WRITE)) { |
| 763 | err = -EPERM; |
| 764 | goto err_put; |
| 765 | } |
| 766 | |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 767 | key = memdup_user(ukey, map->key_size); |
| 768 | if (IS_ERR(key)) { |
| 769 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 770 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 771 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 772 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 773 | if (bpf_map_is_dev_bound(map)) { |
| 774 | err = bpf_map_offload_delete_elem(map, key); |
| 775 | goto out; |
| 776 | } |
| 777 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 778 | preempt_disable(); |
| 779 | __this_cpu_inc(bpf_prog_active); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 780 | rcu_read_lock(); |
| 781 | err = map->ops->map_delete_elem(map, key); |
| 782 | rcu_read_unlock(); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 783 | __this_cpu_dec(bpf_prog_active); |
| 784 | preempt_enable(); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 785 | out: |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 786 | if (!err) |
| 787 | trace_bpf_map_delete_elem(map, ufd, key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 788 | kfree(key); |
| 789 | err_put: |
| 790 | fdput(f); |
| 791 | return err; |
| 792 | } |
| 793 | |
| 794 | /* last field in 'union bpf_attr' used by this command */ |
| 795 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key |
| 796 | |
| 797 | static int map_get_next_key(union bpf_attr *attr) |
| 798 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 799 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 800 | void __user *unext_key = u64_to_user_ptr(attr->next_key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 801 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 802 | struct bpf_map *map; |
| 803 | void *key, *next_key; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 804 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 805 | int err; |
| 806 | |
| 807 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) |
| 808 | return -EINVAL; |
| 809 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 810 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 811 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 812 | if (IS_ERR(map)) |
| 813 | return PTR_ERR(map); |
| 814 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 815 | if (!(f.file->f_mode & FMODE_CAN_READ)) { |
| 816 | err = -EPERM; |
| 817 | goto err_put; |
| 818 | } |
| 819 | |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 820 | if (ukey) { |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 821 | key = memdup_user(ukey, map->key_size); |
| 822 | if (IS_ERR(key)) { |
| 823 | err = PTR_ERR(key); |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 824 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 825 | } |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 826 | } else { |
| 827 | key = NULL; |
| 828 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 829 | |
| 830 | err = -ENOMEM; |
| 831 | next_key = kmalloc(map->key_size, GFP_USER); |
| 832 | if (!next_key) |
| 833 | goto free_key; |
| 834 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 835 | if (bpf_map_is_dev_bound(map)) { |
| 836 | err = bpf_map_offload_get_next_key(map, key, next_key); |
| 837 | goto out; |
| 838 | } |
| 839 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 840 | rcu_read_lock(); |
| 841 | err = map->ops->map_get_next_key(map, key, next_key); |
| 842 | rcu_read_unlock(); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 843 | out: |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 844 | if (err) |
| 845 | goto free_next_key; |
| 846 | |
| 847 | err = -EFAULT; |
| 848 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) |
| 849 | goto free_next_key; |
| 850 | |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 851 | trace_bpf_map_next_key(map, ufd, key, next_key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 852 | err = 0; |
| 853 | |
| 854 | free_next_key: |
| 855 | kfree(next_key); |
| 856 | free_key: |
| 857 | kfree(key); |
| 858 | err_put: |
| 859 | fdput(f); |
| 860 | return err; |
| 861 | } |
| 862 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 863 | static const struct bpf_prog_ops * const bpf_prog_types[] = { |
| 864 | #define BPF_PROG_TYPE(_id, _name) \ |
| 865 | [_id] = & _name ## _prog_ops, |
| 866 | #define BPF_MAP_TYPE(_id, _ops) |
| 867 | #include <linux/bpf_types.h> |
| 868 | #undef BPF_PROG_TYPE |
| 869 | #undef BPF_MAP_TYPE |
| 870 | }; |
| 871 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 872 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
| 873 | { |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 874 | if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type]) |
| 875 | return -EINVAL; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 876 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 877 | if (!bpf_prog_is_dev_bound(prog->aux)) |
| 878 | prog->aux->ops = bpf_prog_types[type]; |
| 879 | else |
| 880 | prog->aux->ops = &bpf_offload_prog_ops; |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 881 | prog->type = type; |
| 882 | return 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /* drop refcnt on maps used by eBPF program and free auxilary data */ |
| 886 | static void free_used_maps(struct bpf_prog_aux *aux) |
| 887 | { |
| 888 | int i; |
| 889 | |
| 890 | for (i = 0; i < aux->used_map_cnt; i++) |
| 891 | bpf_map_put(aux->used_maps[i]); |
| 892 | |
| 893 | kfree(aux->used_maps); |
| 894 | } |
| 895 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 896 | int __bpf_prog_charge(struct user_struct *user, u32 pages) |
| 897 | { |
| 898 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 899 | unsigned long user_bufs; |
| 900 | |
| 901 | if (user) { |
| 902 | user_bufs = atomic_long_add_return(pages, &user->locked_vm); |
| 903 | if (user_bufs > memlock_limit) { |
| 904 | atomic_long_sub(pages, &user->locked_vm); |
| 905 | return -EPERM; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | void __bpf_prog_uncharge(struct user_struct *user, u32 pages) |
| 913 | { |
| 914 | if (user) |
| 915 | atomic_long_sub(pages, &user->locked_vm); |
| 916 | } |
| 917 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 918 | static int bpf_prog_charge_memlock(struct bpf_prog *prog) |
| 919 | { |
| 920 | struct user_struct *user = get_current_user(); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 921 | int ret; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 922 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 923 | ret = __bpf_prog_charge(user, prog->pages); |
| 924 | if (ret) { |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 925 | free_uid(user); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 926 | return ret; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 927 | } |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 928 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 929 | prog->aux->user = user; |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) |
| 934 | { |
| 935 | struct user_struct *user = prog->aux->user; |
| 936 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 937 | __bpf_prog_uncharge(user, prog->pages); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 938 | free_uid(user); |
| 939 | } |
| 940 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 941 | static int bpf_prog_alloc_id(struct bpf_prog *prog) |
| 942 | { |
| 943 | int id; |
| 944 | |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 945 | idr_preload(GFP_KERNEL); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 946 | spin_lock_bh(&prog_idr_lock); |
| 947 | id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); |
| 948 | if (id > 0) |
| 949 | prog->aux->id = id; |
| 950 | spin_unlock_bh(&prog_idr_lock); |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 951 | idr_preload_end(); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 952 | |
| 953 | /* id is in [1, INT_MAX) */ |
| 954 | if (WARN_ON_ONCE(!id)) |
| 955 | return -ENOSPC; |
| 956 | |
| 957 | return id > 0 ? 0 : id; |
| 958 | } |
| 959 | |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 960 | 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] | 961 | { |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 962 | /* cBPF to eBPF migrations are currently not in the idr store. |
| 963 | * Offloaded programs are removed from the store when their device |
| 964 | * disappears - even if someone grabs an fd to them they are unusable, |
| 965 | * simply waiting for refcnt to drop to be freed. |
| 966 | */ |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 967 | if (!prog->aux->id) |
| 968 | return; |
| 969 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 970 | if (do_idr_lock) |
| 971 | spin_lock_bh(&prog_idr_lock); |
| 972 | else |
| 973 | __acquire(&prog_idr_lock); |
| 974 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 975 | idr_remove(&prog_idr, prog->aux->id); |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 976 | prog->aux->id = 0; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 977 | |
| 978 | if (do_idr_lock) |
| 979 | spin_unlock_bh(&prog_idr_lock); |
| 980 | else |
| 981 | __release(&prog_idr_lock); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 982 | } |
| 983 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 984 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 985 | { |
| 986 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); |
| 987 | |
| 988 | free_used_maps(aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 989 | bpf_prog_uncharge_memlock(aux->prog); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 990 | security_bpf_prog_free(aux); |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 991 | bpf_prog_free(aux->prog); |
| 992 | } |
| 993 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 994 | 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] | 995 | { |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 996 | if (atomic_dec_and_test(&prog->aux->refcnt)) { |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 997 | int i; |
| 998 | |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 999 | trace_bpf_prog_put_rcu(prog); |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 1000 | /* bpf_prog_free_id() must be called first */ |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1001 | bpf_prog_free_id(prog, do_idr_lock); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 1002 | |
| 1003 | for (i = 0; i < prog->aux->func_cnt; i++) |
| 1004 | bpf_prog_kallsyms_del(prog->aux->func[i]); |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 1005 | bpf_prog_kallsyms_del(prog); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 1006 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1007 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1008 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1009 | } |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1010 | |
| 1011 | void bpf_prog_put(struct bpf_prog *prog) |
| 1012 | { |
| 1013 | __bpf_prog_put(prog, true); |
| 1014 | } |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 1015 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1016 | |
| 1017 | static int bpf_prog_release(struct inode *inode, struct file *filp) |
| 1018 | { |
| 1019 | struct bpf_prog *prog = filp->private_data; |
| 1020 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1021 | bpf_prog_put(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1022 | return 0; |
| 1023 | } |
| 1024 | |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1025 | #ifdef CONFIG_PROC_FS |
| 1026 | static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) |
| 1027 | { |
| 1028 | const struct bpf_prog *prog = filp->private_data; |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1029 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1030 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1031 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1032 | seq_printf(m, |
| 1033 | "prog_type:\t%u\n" |
| 1034 | "prog_jited:\t%u\n" |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1035 | "prog_tag:\t%s\n" |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1036 | "memlock:\t%llu\n", |
| 1037 | prog->type, |
| 1038 | prog->jited, |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1039 | prog_tag, |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1040 | prog->pages * 1ULL << PAGE_SHIFT); |
| 1041 | } |
| 1042 | #endif |
| 1043 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1044 | const struct file_operations bpf_prog_fops = { |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1045 | #ifdef CONFIG_PROC_FS |
| 1046 | .show_fdinfo = bpf_prog_show_fdinfo, |
| 1047 | #endif |
| 1048 | .release = bpf_prog_release, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1049 | .read = bpf_dummy_read, |
| 1050 | .write = bpf_dummy_write, |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1051 | }; |
| 1052 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1053 | int bpf_prog_new_fd(struct bpf_prog *prog) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1054 | { |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1055 | int ret; |
| 1056 | |
| 1057 | ret = security_bpf_prog(prog); |
| 1058 | if (ret < 0) |
| 1059 | return ret; |
| 1060 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1061 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
| 1062 | O_RDWR | O_CLOEXEC); |
| 1063 | } |
| 1064 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1065 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1066 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1067 | if (!f.file) |
| 1068 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1069 | if (f.file->f_op != &bpf_prog_fops) { |
| 1070 | fdput(f); |
| 1071 | return ERR_PTR(-EINVAL); |
| 1072 | } |
| 1073 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1074 | return f.file->private_data; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1077 | struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i) |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1078 | { |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1079 | if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) { |
| 1080 | atomic_sub(i, &prog->aux->refcnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1081 | return ERR_PTR(-EBUSY); |
| 1082 | } |
| 1083 | return prog; |
| 1084 | } |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1085 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
| 1086 | |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 1087 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
| 1088 | { |
| 1089 | /* Only to be used for undoing previous bpf_prog_add() in some |
| 1090 | * error path. We still know that another entity in our call |
| 1091 | * path holds a reference to the program, thus atomic_sub() can |
| 1092 | * be safely used in such cases! |
| 1093 | */ |
| 1094 | WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0); |
| 1095 | } |
| 1096 | EXPORT_SYMBOL_GPL(bpf_prog_sub); |
| 1097 | |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1098 | struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog) |
| 1099 | { |
| 1100 | return bpf_prog_add(prog, 1); |
| 1101 | } |
Daniel Borkmann | 97bc402 | 2016-11-19 01:45:00 +0100 | [diff] [blame] | 1102 | EXPORT_SYMBOL_GPL(bpf_prog_inc); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1103 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1104 | /* prog_idr_lock should have been held */ |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1105 | 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] | 1106 | { |
| 1107 | int refold; |
| 1108 | |
| 1109 | refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0); |
| 1110 | |
| 1111 | if (refold >= BPF_MAX_REFCNT) { |
| 1112 | __bpf_prog_put(prog, false); |
| 1113 | return ERR_PTR(-EBUSY); |
| 1114 | } |
| 1115 | |
| 1116 | if (!refold) |
| 1117 | return ERR_PTR(-ENOENT); |
| 1118 | |
| 1119 | return prog; |
| 1120 | } |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1121 | EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1122 | |
Al Viro | 040ee69 | 2017-12-02 20:20:38 -0500 | [diff] [blame] | 1123 | bool bpf_prog_get_ok(struct bpf_prog *prog, |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1124 | enum bpf_prog_type *attach_type, bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1125 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1126 | /* not an attachment, just a refcount inc, always allow */ |
| 1127 | if (!attach_type) |
| 1128 | return true; |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1129 | |
| 1130 | if (prog->type != *attach_type) |
| 1131 | return false; |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1132 | if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1133 | return false; |
| 1134 | |
| 1135 | return true; |
| 1136 | } |
| 1137 | |
| 1138 | static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1139 | bool attach_drv) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1140 | { |
| 1141 | struct fd f = fdget(ufd); |
| 1142 | struct bpf_prog *prog; |
| 1143 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1144 | prog = ____bpf_prog_get(f); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1145 | if (IS_ERR(prog)) |
| 1146 | return prog; |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1147 | if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1148 | prog = ERR_PTR(-EINVAL); |
| 1149 | goto out; |
| 1150 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1151 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1152 | prog = bpf_prog_inc(prog); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1153 | out: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1154 | fdput(f); |
| 1155 | return prog; |
| 1156 | } |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1157 | |
| 1158 | struct bpf_prog *bpf_prog_get(u32 ufd) |
| 1159 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1160 | return __bpf_prog_get(ufd, NULL, false); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1161 | } |
| 1162 | |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1163 | struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1164 | bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1165 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1166 | struct bpf_prog *prog = __bpf_prog_get(ufd, &type, attach_drv); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1167 | |
| 1168 | if (!IS_ERR(prog)) |
| 1169 | trace_bpf_prog_get_type(prog); |
| 1170 | return prog; |
| 1171 | } |
Jakub Kicinski | 6c8dfe2 | 2017-11-03 13:56:21 -0700 | [diff] [blame] | 1172 | EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1173 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1174 | static int |
| 1175 | bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type, |
| 1176 | enum bpf_attach_type expected_attach_type) |
| 1177 | { |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1178 | switch (prog_type) { |
| 1179 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 1180 | switch (expected_attach_type) { |
| 1181 | case BPF_CGROUP_INET4_BIND: |
| 1182 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame^] | 1183 | case BPF_CGROUP_INET4_CONNECT: |
| 1184 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1185 | return 0; |
| 1186 | default: |
| 1187 | return -EINVAL; |
| 1188 | } |
| 1189 | default: |
| 1190 | return 0; |
| 1191 | } |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, |
| 1195 | enum bpf_attach_type attach_type) |
| 1196 | { |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1197 | switch (prog->type) { |
| 1198 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 1199 | return attach_type == prog->expected_attach_type ? 0 : -EINVAL; |
| 1200 | default: |
| 1201 | return 0; |
| 1202 | } |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1205 | /* last field in 'union bpf_attr' used by this command */ |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1206 | #define BPF_PROG_LOAD_LAST_FIELD expected_attach_type |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1207 | |
| 1208 | static int bpf_prog_load(union bpf_attr *attr) |
| 1209 | { |
| 1210 | enum bpf_prog_type type = attr->prog_type; |
| 1211 | struct bpf_prog *prog; |
| 1212 | int err; |
| 1213 | char license[128]; |
| 1214 | bool is_gpl; |
| 1215 | |
| 1216 | if (CHECK_ATTR(BPF_PROG_LOAD)) |
| 1217 | return -EINVAL; |
| 1218 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1219 | if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT) |
| 1220 | return -EINVAL; |
| 1221 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1222 | /* copy eBPF program license from user space */ |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1223 | if (strncpy_from_user(license, u64_to_user_ptr(attr->license), |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1224 | sizeof(license) - 1) < 0) |
| 1225 | return -EFAULT; |
| 1226 | license[sizeof(license) - 1] = 0; |
| 1227 | |
| 1228 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
| 1229 | is_gpl = license_is_gpl_compatible(license); |
| 1230 | |
Daniel Borkmann | ef0915c | 2016-12-07 01:15:44 +0100 | [diff] [blame] | 1231 | if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS) |
| 1232 | return -E2BIG; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1233 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 1234 | if (type == BPF_PROG_TYPE_KPROBE && |
| 1235 | attr->kern_version != LINUX_VERSION_CODE) |
| 1236 | return -EINVAL; |
| 1237 | |
Chenbo Feng | 80b7d81 | 2017-05-31 18:16:00 -0700 | [diff] [blame] | 1238 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && |
| 1239 | type != BPF_PROG_TYPE_CGROUP_SKB && |
| 1240 | !capable(CAP_SYS_ADMIN)) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1241 | return -EPERM; |
| 1242 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1243 | if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type)) |
| 1244 | return -EINVAL; |
| 1245 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1246 | /* plain bpf_prog allocation */ |
| 1247 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); |
| 1248 | if (!prog) |
| 1249 | return -ENOMEM; |
| 1250 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1251 | prog->expected_attach_type = attr->expected_attach_type; |
| 1252 | |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 1253 | prog->aux->offload_requested = !!attr->prog_ifindex; |
| 1254 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1255 | err = security_bpf_prog_alloc(prog->aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1256 | if (err) |
| 1257 | goto free_prog_nouncharge; |
| 1258 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1259 | err = bpf_prog_charge_memlock(prog); |
| 1260 | if (err) |
| 1261 | goto free_prog_sec; |
| 1262 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1263 | prog->len = attr->insn_cnt; |
| 1264 | |
| 1265 | err = -EFAULT; |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1266 | if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 1267 | bpf_prog_insn_size(prog)) != 0) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1268 | goto free_prog; |
| 1269 | |
| 1270 | prog->orig_prog = NULL; |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1271 | prog->jited = 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1272 | |
| 1273 | atomic_set(&prog->aux->refcnt, 1); |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1274 | prog->gpl_compatible = is_gpl ? 1 : 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1275 | |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 1276 | if (bpf_prog_is_dev_bound(prog->aux)) { |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1277 | err = bpf_prog_offload_init(prog, attr); |
| 1278 | if (err) |
| 1279 | goto free_prog; |
| 1280 | } |
| 1281 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1282 | /* find program type: socket_filter vs tracing_filter */ |
| 1283 | err = find_prog_type(type, prog); |
| 1284 | if (err < 0) |
| 1285 | goto free_prog; |
| 1286 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 1287 | prog->aux->load_time = ktime_get_boot_ns(); |
| 1288 | err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name); |
| 1289 | if (err) |
| 1290 | goto free_prog; |
| 1291 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1292 | /* run eBPF verifier */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 1293 | err = bpf_check(&prog, attr); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1294 | if (err < 0) |
| 1295 | goto free_used_maps; |
| 1296 | |
| 1297 | /* eBPF program is ready to be JITed */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 1298 | if (!prog->bpf_func) |
| 1299 | prog = bpf_prog_select_runtime(prog, &err); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1300 | if (err < 0) |
| 1301 | goto free_used_maps; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1302 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1303 | err = bpf_prog_alloc_id(prog); |
| 1304 | if (err) |
| 1305 | goto free_used_maps; |
| 1306 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1307 | err = bpf_prog_new_fd(prog); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1308 | if (err < 0) { |
| 1309 | /* failed to allocate fd. |
| 1310 | * bpf_prog_put() is needed because the above |
| 1311 | * bpf_prog_alloc_id() has published the prog |
| 1312 | * to the userspace and the userspace may |
| 1313 | * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID. |
| 1314 | */ |
| 1315 | bpf_prog_put(prog); |
| 1316 | return err; |
| 1317 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1318 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 1319 | bpf_prog_kallsyms_add(prog); |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1320 | trace_bpf_prog_load(prog, err); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1321 | return err; |
| 1322 | |
| 1323 | free_used_maps: |
| 1324 | free_used_maps(prog->aux); |
| 1325 | free_prog: |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1326 | bpf_prog_uncharge_memlock(prog); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1327 | free_prog_sec: |
| 1328 | security_bpf_prog_free(prog->aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1329 | free_prog_nouncharge: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1330 | bpf_prog_free(prog); |
| 1331 | return err; |
| 1332 | } |
| 1333 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1334 | #define BPF_OBJ_LAST_FIELD file_flags |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1335 | |
| 1336 | static int bpf_obj_pin(const union bpf_attr *attr) |
| 1337 | { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1338 | if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1339 | return -EINVAL; |
| 1340 | |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1341 | 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] | 1342 | } |
| 1343 | |
| 1344 | static int bpf_obj_get(const union bpf_attr *attr) |
| 1345 | { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1346 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || |
| 1347 | attr->file_flags & ~BPF_OBJ_FLAG_MASK) |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1348 | return -EINVAL; |
| 1349 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1350 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), |
| 1351 | attr->file_flags); |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1352 | } |
| 1353 | |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1354 | struct bpf_raw_tracepoint { |
| 1355 | struct bpf_raw_event_map *btp; |
| 1356 | struct bpf_prog *prog; |
| 1357 | }; |
| 1358 | |
| 1359 | static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp) |
| 1360 | { |
| 1361 | struct bpf_raw_tracepoint *raw_tp = filp->private_data; |
| 1362 | |
| 1363 | if (raw_tp->prog) { |
| 1364 | bpf_probe_unregister(raw_tp->btp, raw_tp->prog); |
| 1365 | bpf_prog_put(raw_tp->prog); |
| 1366 | } |
| 1367 | kfree(raw_tp); |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | static const struct file_operations bpf_raw_tp_fops = { |
| 1372 | .release = bpf_raw_tracepoint_release, |
| 1373 | .read = bpf_dummy_read, |
| 1374 | .write = bpf_dummy_write, |
| 1375 | }; |
| 1376 | |
| 1377 | #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd |
| 1378 | |
| 1379 | static int bpf_raw_tracepoint_open(const union bpf_attr *attr) |
| 1380 | { |
| 1381 | struct bpf_raw_tracepoint *raw_tp; |
| 1382 | struct bpf_raw_event_map *btp; |
| 1383 | struct bpf_prog *prog; |
| 1384 | char tp_name[128]; |
| 1385 | int tp_fd, err; |
| 1386 | |
| 1387 | if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name), |
| 1388 | sizeof(tp_name) - 1) < 0) |
| 1389 | return -EFAULT; |
| 1390 | tp_name[sizeof(tp_name) - 1] = 0; |
| 1391 | |
| 1392 | btp = bpf_find_raw_tracepoint(tp_name); |
| 1393 | if (!btp) |
| 1394 | return -ENOENT; |
| 1395 | |
| 1396 | raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER); |
| 1397 | if (!raw_tp) |
| 1398 | return -ENOMEM; |
| 1399 | raw_tp->btp = btp; |
| 1400 | |
| 1401 | prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd, |
| 1402 | BPF_PROG_TYPE_RAW_TRACEPOINT); |
| 1403 | if (IS_ERR(prog)) { |
| 1404 | err = PTR_ERR(prog); |
| 1405 | goto out_free_tp; |
| 1406 | } |
| 1407 | |
| 1408 | err = bpf_probe_register(raw_tp->btp, prog); |
| 1409 | if (err) |
| 1410 | goto out_put_prog; |
| 1411 | |
| 1412 | raw_tp->prog = prog; |
| 1413 | tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp, |
| 1414 | O_CLOEXEC); |
| 1415 | if (tp_fd < 0) { |
| 1416 | bpf_probe_unregister(raw_tp->btp, prog); |
| 1417 | err = tp_fd; |
| 1418 | goto out_put_prog; |
| 1419 | } |
| 1420 | return tp_fd; |
| 1421 | |
| 1422 | out_put_prog: |
| 1423 | bpf_prog_put(prog); |
| 1424 | out_free_tp: |
| 1425 | kfree(raw_tp); |
| 1426 | return err; |
| 1427 | } |
| 1428 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1429 | #ifdef CONFIG_CGROUP_BPF |
| 1430 | |
John Fastabend | 464bc0f | 2017-08-28 07:10:04 -0700 | [diff] [blame] | 1431 | #define BPF_PROG_ATTACH_LAST_FIELD attach_flags |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1432 | |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1433 | static int sockmap_get_from_fd(const union bpf_attr *attr, |
| 1434 | int type, bool attach) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1435 | { |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1436 | struct bpf_prog *prog = NULL; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1437 | int ufd = attr->target_fd; |
| 1438 | struct bpf_map *map; |
| 1439 | struct fd f; |
| 1440 | int err; |
| 1441 | |
| 1442 | f = fdget(ufd); |
| 1443 | map = __bpf_map_get(f); |
| 1444 | if (IS_ERR(map)) |
| 1445 | return PTR_ERR(map); |
| 1446 | |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1447 | if (attach) { |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1448 | prog = bpf_prog_get_type(attr->attach_bpf_fd, type); |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1449 | if (IS_ERR(prog)) { |
| 1450 | fdput(f); |
| 1451 | return PTR_ERR(prog); |
| 1452 | } |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1453 | } |
| 1454 | |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1455 | err = sock_map_prog(map, prog, attr->attach_type); |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1456 | if (err) { |
| 1457 | fdput(f); |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1458 | if (prog) |
| 1459 | bpf_prog_put(prog); |
Dan Carpenter | ae2b27b | 2017-08-18 10:27:02 +0300 | [diff] [blame] | 1460 | return err; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | fdput(f); |
Dan Carpenter | ae2b27b | 2017-08-18 10:27:02 +0300 | [diff] [blame] | 1464 | return 0; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1465 | } |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1466 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1467 | #define BPF_F_ATTACH_MASK \ |
| 1468 | (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI) |
| 1469 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1470 | static int bpf_prog_attach(const union bpf_attr *attr) |
| 1471 | { |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1472 | enum bpf_prog_type ptype; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1473 | struct bpf_prog *prog; |
| 1474 | struct cgroup *cgrp; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1475 | int ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1476 | |
| 1477 | if (!capable(CAP_NET_ADMIN)) |
| 1478 | return -EPERM; |
| 1479 | |
| 1480 | if (CHECK_ATTR(BPF_PROG_ATTACH)) |
| 1481 | return -EINVAL; |
| 1482 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1483 | if (attr->attach_flags & ~BPF_F_ATTACH_MASK) |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1484 | return -EINVAL; |
| 1485 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1486 | switch (attr->attach_type) { |
| 1487 | case BPF_CGROUP_INET_INGRESS: |
| 1488 | case BPF_CGROUP_INET_EGRESS: |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1489 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1490 | break; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1491 | case BPF_CGROUP_INET_SOCK_CREATE: |
| 1492 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
| 1493 | break; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1494 | case BPF_CGROUP_INET4_BIND: |
| 1495 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame^] | 1496 | case BPF_CGROUP_INET4_CONNECT: |
| 1497 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1498 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
| 1499 | break; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1500 | case BPF_CGROUP_SOCK_OPS: |
| 1501 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
| 1502 | break; |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1503 | case BPF_CGROUP_DEVICE: |
| 1504 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; |
| 1505 | break; |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1506 | case BPF_SK_MSG_VERDICT: |
| 1507 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true); |
John Fastabend | 464bc0f | 2017-08-28 07:10:04 -0700 | [diff] [blame] | 1508 | case BPF_SK_SKB_STREAM_PARSER: |
| 1509 | case BPF_SK_SKB_STREAM_VERDICT: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1510 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true); |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1511 | default: |
| 1512 | return -EINVAL; |
| 1513 | } |
| 1514 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1515 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
| 1516 | if (IS_ERR(prog)) |
| 1517 | return PTR_ERR(prog); |
| 1518 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1519 | if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { |
| 1520 | bpf_prog_put(prog); |
| 1521 | return -EINVAL; |
| 1522 | } |
| 1523 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1524 | cgrp = cgroup_get_from_fd(attr->target_fd); |
| 1525 | if (IS_ERR(cgrp)) { |
| 1526 | bpf_prog_put(prog); |
| 1527 | return PTR_ERR(cgrp); |
| 1528 | } |
| 1529 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1530 | ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type, |
| 1531 | attr->attach_flags); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1532 | if (ret) |
| 1533 | bpf_prog_put(prog); |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1534 | cgroup_put(cgrp); |
| 1535 | |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1536 | return ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | #define BPF_PROG_DETACH_LAST_FIELD attach_type |
| 1540 | |
| 1541 | static int bpf_prog_detach(const union bpf_attr *attr) |
| 1542 | { |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1543 | enum bpf_prog_type ptype; |
| 1544 | struct bpf_prog *prog; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1545 | struct cgroup *cgrp; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1546 | int ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1547 | |
| 1548 | if (!capable(CAP_NET_ADMIN)) |
| 1549 | return -EPERM; |
| 1550 | |
| 1551 | if (CHECK_ATTR(BPF_PROG_DETACH)) |
| 1552 | return -EINVAL; |
| 1553 | |
| 1554 | switch (attr->attach_type) { |
| 1555 | case BPF_CGROUP_INET_INGRESS: |
| 1556 | case BPF_CGROUP_INET_EGRESS: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1557 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
| 1558 | break; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1559 | case BPF_CGROUP_INET_SOCK_CREATE: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1560 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
| 1561 | break; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1562 | case BPF_CGROUP_INET4_BIND: |
| 1563 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame^] | 1564 | case BPF_CGROUP_INET4_CONNECT: |
| 1565 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1566 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
| 1567 | break; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1568 | case BPF_CGROUP_SOCK_OPS: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1569 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1570 | break; |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1571 | case BPF_CGROUP_DEVICE: |
| 1572 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; |
| 1573 | break; |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1574 | case BPF_SK_MSG_VERDICT: |
| 1575 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false); |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1576 | case BPF_SK_SKB_STREAM_PARSER: |
| 1577 | case BPF_SK_SKB_STREAM_VERDICT: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1578 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false); |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1579 | default: |
| 1580 | return -EINVAL; |
| 1581 | } |
| 1582 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1583 | cgrp = cgroup_get_from_fd(attr->target_fd); |
| 1584 | if (IS_ERR(cgrp)) |
| 1585 | return PTR_ERR(cgrp); |
| 1586 | |
| 1587 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
| 1588 | if (IS_ERR(prog)) |
| 1589 | prog = NULL; |
| 1590 | |
| 1591 | ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0); |
| 1592 | if (prog) |
| 1593 | bpf_prog_put(prog); |
| 1594 | cgroup_put(cgrp); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1595 | return ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1596 | } |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1597 | |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1598 | #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt |
| 1599 | |
| 1600 | static int bpf_prog_query(const union bpf_attr *attr, |
| 1601 | union bpf_attr __user *uattr) |
| 1602 | { |
| 1603 | struct cgroup *cgrp; |
| 1604 | int ret; |
| 1605 | |
| 1606 | if (!capable(CAP_NET_ADMIN)) |
| 1607 | return -EPERM; |
| 1608 | if (CHECK_ATTR(BPF_PROG_QUERY)) |
| 1609 | return -EINVAL; |
| 1610 | if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) |
| 1611 | return -EINVAL; |
| 1612 | |
| 1613 | switch (attr->query.attach_type) { |
| 1614 | case BPF_CGROUP_INET_INGRESS: |
| 1615 | case BPF_CGROUP_INET_EGRESS: |
| 1616 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1617 | case BPF_CGROUP_INET4_BIND: |
| 1618 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame^] | 1619 | case BPF_CGROUP_INET4_CONNECT: |
| 1620 | case BPF_CGROUP_INET6_CONNECT: |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1621 | case BPF_CGROUP_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1622 | case BPF_CGROUP_DEVICE: |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1623 | break; |
| 1624 | default: |
| 1625 | return -EINVAL; |
| 1626 | } |
| 1627 | cgrp = cgroup_get_from_fd(attr->query.target_fd); |
| 1628 | if (IS_ERR(cgrp)) |
| 1629 | return PTR_ERR(cgrp); |
| 1630 | ret = cgroup_bpf_query(cgrp, attr, uattr); |
| 1631 | cgroup_put(cgrp); |
| 1632 | return ret; |
| 1633 | } |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1634 | #endif /* CONFIG_CGROUP_BPF */ |
| 1635 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 1636 | #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration |
| 1637 | |
| 1638 | static int bpf_prog_test_run(const union bpf_attr *attr, |
| 1639 | union bpf_attr __user *uattr) |
| 1640 | { |
| 1641 | struct bpf_prog *prog; |
| 1642 | int ret = -ENOTSUPP; |
| 1643 | |
Alexei Starovoitov | 61f3c96 | 2018-01-17 16:52:02 -0800 | [diff] [blame] | 1644 | if (!capable(CAP_SYS_ADMIN)) |
| 1645 | return -EPERM; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 1646 | if (CHECK_ATTR(BPF_PROG_TEST_RUN)) |
| 1647 | return -EINVAL; |
| 1648 | |
| 1649 | prog = bpf_prog_get(attr->test.prog_fd); |
| 1650 | if (IS_ERR(prog)) |
| 1651 | return PTR_ERR(prog); |
| 1652 | |
| 1653 | if (prog->aux->ops->test_run) |
| 1654 | ret = prog->aux->ops->test_run(prog, attr, uattr); |
| 1655 | |
| 1656 | bpf_prog_put(prog); |
| 1657 | return ret; |
| 1658 | } |
| 1659 | |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 1660 | #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id |
| 1661 | |
| 1662 | static int bpf_obj_get_next_id(const union bpf_attr *attr, |
| 1663 | union bpf_attr __user *uattr, |
| 1664 | struct idr *idr, |
| 1665 | spinlock_t *lock) |
| 1666 | { |
| 1667 | u32 next_id = attr->start_id; |
| 1668 | int err = 0; |
| 1669 | |
| 1670 | if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) |
| 1671 | return -EINVAL; |
| 1672 | |
| 1673 | if (!capable(CAP_SYS_ADMIN)) |
| 1674 | return -EPERM; |
| 1675 | |
| 1676 | next_id++; |
| 1677 | spin_lock_bh(lock); |
| 1678 | if (!idr_get_next(idr, &next_id)) |
| 1679 | err = -ENOENT; |
| 1680 | spin_unlock_bh(lock); |
| 1681 | |
| 1682 | if (!err) |
| 1683 | err = put_user(next_id, &uattr->next_id); |
| 1684 | |
| 1685 | return err; |
| 1686 | } |
| 1687 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1688 | #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id |
| 1689 | |
| 1690 | static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) |
| 1691 | { |
| 1692 | struct bpf_prog *prog; |
| 1693 | u32 id = attr->prog_id; |
| 1694 | int fd; |
| 1695 | |
| 1696 | if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) |
| 1697 | return -EINVAL; |
| 1698 | |
| 1699 | if (!capable(CAP_SYS_ADMIN)) |
| 1700 | return -EPERM; |
| 1701 | |
| 1702 | spin_lock_bh(&prog_idr_lock); |
| 1703 | prog = idr_find(&prog_idr, id); |
| 1704 | if (prog) |
| 1705 | prog = bpf_prog_inc_not_zero(prog); |
| 1706 | else |
| 1707 | prog = ERR_PTR(-ENOENT); |
| 1708 | spin_unlock_bh(&prog_idr_lock); |
| 1709 | |
| 1710 | if (IS_ERR(prog)) |
| 1711 | return PTR_ERR(prog); |
| 1712 | |
| 1713 | fd = bpf_prog_new_fd(prog); |
| 1714 | if (fd < 0) |
| 1715 | bpf_prog_put(prog); |
| 1716 | |
| 1717 | return fd; |
| 1718 | } |
| 1719 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1720 | #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1721 | |
| 1722 | static int bpf_map_get_fd_by_id(const union bpf_attr *attr) |
| 1723 | { |
| 1724 | struct bpf_map *map; |
| 1725 | u32 id = attr->map_id; |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1726 | int f_flags; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1727 | int fd; |
| 1728 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1729 | if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || |
| 1730 | attr->open_flags & ~BPF_OBJ_FLAG_MASK) |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1731 | return -EINVAL; |
| 1732 | |
| 1733 | if (!capable(CAP_SYS_ADMIN)) |
| 1734 | return -EPERM; |
| 1735 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1736 | f_flags = bpf_get_file_flag(attr->open_flags); |
| 1737 | if (f_flags < 0) |
| 1738 | return f_flags; |
| 1739 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1740 | spin_lock_bh(&map_idr_lock); |
| 1741 | map = idr_find(&map_idr, id); |
| 1742 | if (map) |
| 1743 | map = bpf_map_inc_not_zero(map, true); |
| 1744 | else |
| 1745 | map = ERR_PTR(-ENOENT); |
| 1746 | spin_unlock_bh(&map_idr_lock); |
| 1747 | |
| 1748 | if (IS_ERR(map)) |
| 1749 | return PTR_ERR(map); |
| 1750 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1751 | fd = bpf_map_new_fd(map, f_flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1752 | if (fd < 0) |
| 1753 | bpf_map_put(map); |
| 1754 | |
| 1755 | return fd; |
| 1756 | } |
| 1757 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 1758 | static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, |
| 1759 | unsigned long addr) |
| 1760 | { |
| 1761 | int i; |
| 1762 | |
| 1763 | for (i = 0; i < prog->aux->used_map_cnt; i++) |
| 1764 | if (prog->aux->used_maps[i] == (void *)addr) |
| 1765 | return prog->aux->used_maps[i]; |
| 1766 | return NULL; |
| 1767 | } |
| 1768 | |
| 1769 | static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog) |
| 1770 | { |
| 1771 | const struct bpf_map *map; |
| 1772 | struct bpf_insn *insns; |
| 1773 | u64 imm; |
| 1774 | int i; |
| 1775 | |
| 1776 | insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), |
| 1777 | GFP_USER); |
| 1778 | if (!insns) |
| 1779 | return insns; |
| 1780 | |
| 1781 | for (i = 0; i < prog->len; i++) { |
| 1782 | if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) { |
| 1783 | insns[i].code = BPF_JMP | BPF_CALL; |
| 1784 | insns[i].imm = BPF_FUNC_tail_call; |
| 1785 | /* fall-through */ |
| 1786 | } |
| 1787 | if (insns[i].code == (BPF_JMP | BPF_CALL) || |
| 1788 | insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) { |
| 1789 | if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) |
| 1790 | insns[i].code = BPF_JMP | BPF_CALL; |
| 1791 | if (!bpf_dump_raw_ok()) |
| 1792 | insns[i].imm = 0; |
| 1793 | continue; |
| 1794 | } |
| 1795 | |
| 1796 | if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW)) |
| 1797 | continue; |
| 1798 | |
| 1799 | imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; |
| 1800 | map = bpf_map_from_imm(prog, imm); |
| 1801 | if (map) { |
| 1802 | insns[i].src_reg = BPF_PSEUDO_MAP_FD; |
| 1803 | insns[i].imm = map->id; |
| 1804 | insns[i + 1].imm = 0; |
| 1805 | continue; |
| 1806 | } |
| 1807 | |
| 1808 | if (!bpf_dump_raw_ok() && |
| 1809 | imm == (unsigned long)prog->aux) { |
| 1810 | insns[i].imm = 0; |
| 1811 | insns[i + 1].imm = 0; |
| 1812 | continue; |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | return insns; |
| 1817 | } |
| 1818 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1819 | static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, |
| 1820 | const union bpf_attr *attr, |
| 1821 | union bpf_attr __user *uattr) |
| 1822 | { |
| 1823 | struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 1824 | struct bpf_prog_info info = {}; |
| 1825 | u32 info_len = attr->info.info_len; |
| 1826 | char __user *uinsns; |
| 1827 | u32 ulen; |
| 1828 | int err; |
| 1829 | |
| 1830 | err = check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
| 1831 | if (err) |
| 1832 | return err; |
| 1833 | info_len = min_t(u32, sizeof(info), info_len); |
| 1834 | |
| 1835 | if (copy_from_user(&info, uinfo, info_len)) |
Daniel Borkmann | 89b0968 | 2017-07-27 21:02:46 +0200 | [diff] [blame] | 1836 | return -EFAULT; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1837 | |
| 1838 | info.type = prog->type; |
| 1839 | info.id = prog->aux->id; |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 1840 | info.load_time = prog->aux->load_time; |
| 1841 | info.created_by_uid = from_kuid_munged(current_user_ns(), |
| 1842 | prog->aux->user->uid); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1843 | |
| 1844 | memcpy(info.tag, prog->tag, sizeof(prog->tag)); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 1845 | memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); |
| 1846 | |
| 1847 | ulen = info.nr_map_ids; |
| 1848 | info.nr_map_ids = prog->aux->used_map_cnt; |
| 1849 | ulen = min_t(u32, info.nr_map_ids, ulen); |
| 1850 | if (ulen) { |
Martin KaFai Lau | 721e08d | 2017-09-29 10:52:17 -0700 | [diff] [blame] | 1851 | u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 1852 | u32 i; |
| 1853 | |
| 1854 | for (i = 0; i < ulen; i++) |
| 1855 | if (put_user(prog->aux->used_maps[i]->id, |
| 1856 | &user_map_ids[i])) |
| 1857 | return -EFAULT; |
| 1858 | } |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1859 | |
| 1860 | if (!capable(CAP_SYS_ADMIN)) { |
| 1861 | info.jited_prog_len = 0; |
| 1862 | info.xlated_prog_len = 0; |
| 1863 | goto done; |
| 1864 | } |
| 1865 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1866 | ulen = info.xlated_prog_len; |
Daniel Borkmann | 9975a54 | 2017-07-28 17:05:25 +0200 | [diff] [blame] | 1867 | info.xlated_prog_len = bpf_prog_insn_size(prog); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1868 | if (info.xlated_prog_len && ulen) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 1869 | struct bpf_insn *insns_sanitized; |
| 1870 | bool fault; |
| 1871 | |
| 1872 | if (prog->blinded && !bpf_dump_raw_ok()) { |
| 1873 | info.xlated_prog_insns = 0; |
| 1874 | goto done; |
| 1875 | } |
| 1876 | insns_sanitized = bpf_insn_prepare_dump(prog); |
| 1877 | if (!insns_sanitized) |
| 1878 | return -ENOMEM; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1879 | uinsns = u64_to_user_ptr(info.xlated_prog_insns); |
| 1880 | ulen = min_t(u32, info.xlated_prog_len, ulen); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 1881 | fault = copy_to_user(uinsns, insns_sanitized, ulen); |
| 1882 | kfree(insns_sanitized); |
| 1883 | if (fault) |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1884 | return -EFAULT; |
| 1885 | } |
| 1886 | |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 1887 | if (bpf_prog_is_dev_bound(prog->aux)) { |
| 1888 | err = bpf_prog_offload_info_fill(&info, prog); |
| 1889 | if (err) |
| 1890 | return err; |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 1891 | goto done; |
| 1892 | } |
| 1893 | |
| 1894 | /* NOTE: the following code is supposed to be skipped for offload. |
| 1895 | * bpf_prog_offload_info_fill() is the place to fill similar fields |
| 1896 | * for offload. |
| 1897 | */ |
| 1898 | ulen = info.jited_prog_len; |
| 1899 | info.jited_prog_len = prog->jited_len; |
| 1900 | if (info.jited_prog_len && ulen) { |
| 1901 | if (bpf_dump_raw_ok()) { |
| 1902 | uinsns = u64_to_user_ptr(info.jited_prog_insns); |
| 1903 | ulen = min_t(u32, info.jited_prog_len, ulen); |
| 1904 | if (copy_to_user(uinsns, prog->bpf_func, ulen)) |
| 1905 | return -EFAULT; |
| 1906 | } else { |
| 1907 | info.jited_prog_insns = 0; |
| 1908 | } |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 1909 | } |
| 1910 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1911 | done: |
| 1912 | if (copy_to_user(uinfo, &info, info_len) || |
| 1913 | put_user(info_len, &uattr->info.info_len)) |
| 1914 | return -EFAULT; |
| 1915 | |
| 1916 | return 0; |
| 1917 | } |
| 1918 | |
| 1919 | static int bpf_map_get_info_by_fd(struct bpf_map *map, |
| 1920 | const union bpf_attr *attr, |
| 1921 | union bpf_attr __user *uattr) |
| 1922 | { |
| 1923 | struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 1924 | struct bpf_map_info info = {}; |
| 1925 | u32 info_len = attr->info.info_len; |
| 1926 | int err; |
| 1927 | |
| 1928 | err = check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
| 1929 | if (err) |
| 1930 | return err; |
| 1931 | info_len = min_t(u32, sizeof(info), info_len); |
| 1932 | |
| 1933 | info.type = map->map_type; |
| 1934 | info.id = map->id; |
| 1935 | info.key_size = map->key_size; |
| 1936 | info.value_size = map->value_size; |
| 1937 | info.max_entries = map->max_entries; |
| 1938 | info.map_flags = map->map_flags; |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 1939 | memcpy(info.name, map->name, sizeof(map->name)); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1940 | |
Jakub Kicinski | 52775b3 | 2018-01-17 19:13:28 -0800 | [diff] [blame] | 1941 | if (bpf_map_is_dev_bound(map)) { |
| 1942 | err = bpf_map_offload_info_fill(&info, map); |
| 1943 | if (err) |
| 1944 | return err; |
| 1945 | } |
| 1946 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1947 | if (copy_to_user(uinfo, &info, info_len) || |
| 1948 | put_user(info_len, &uattr->info.info_len)) |
| 1949 | return -EFAULT; |
| 1950 | |
| 1951 | return 0; |
| 1952 | } |
| 1953 | |
| 1954 | #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info |
| 1955 | |
| 1956 | static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, |
| 1957 | union bpf_attr __user *uattr) |
| 1958 | { |
| 1959 | int ufd = attr->info.bpf_fd; |
| 1960 | struct fd f; |
| 1961 | int err; |
| 1962 | |
| 1963 | if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) |
| 1964 | return -EINVAL; |
| 1965 | |
| 1966 | f = fdget(ufd); |
| 1967 | if (!f.file) |
| 1968 | return -EBADFD; |
| 1969 | |
| 1970 | if (f.file->f_op == &bpf_prog_fops) |
| 1971 | err = bpf_prog_get_info_by_fd(f.file->private_data, attr, |
| 1972 | uattr); |
| 1973 | else if (f.file->f_op == &bpf_map_fops) |
| 1974 | err = bpf_map_get_info_by_fd(f.file->private_data, attr, |
| 1975 | uattr); |
| 1976 | else |
| 1977 | err = -EINVAL; |
| 1978 | |
| 1979 | fdput(f); |
| 1980 | return err; |
| 1981 | } |
| 1982 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 1983 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
| 1984 | { |
| 1985 | union bpf_attr attr = {}; |
| 1986 | int err; |
| 1987 | |
Chenbo Feng | 0fa4fe8 | 2018-03-19 17:57:27 -0700 | [diff] [blame] | 1988 | if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN)) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 1989 | return -EPERM; |
| 1990 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1991 | err = check_uarg_tail_zero(uattr, sizeof(attr), size); |
| 1992 | if (err) |
| 1993 | return err; |
| 1994 | size = min_t(u32, size, sizeof(attr)); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 1995 | |
| 1996 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ |
| 1997 | if (copy_from_user(&attr, uattr, size) != 0) |
| 1998 | return -EFAULT; |
| 1999 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 2000 | err = security_bpf(cmd, &attr, size); |
| 2001 | if (err < 0) |
| 2002 | return err; |
| 2003 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2004 | switch (cmd) { |
| 2005 | case BPF_MAP_CREATE: |
| 2006 | err = map_create(&attr); |
| 2007 | break; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 2008 | case BPF_MAP_LOOKUP_ELEM: |
| 2009 | err = map_lookup_elem(&attr); |
| 2010 | break; |
| 2011 | case BPF_MAP_UPDATE_ELEM: |
| 2012 | err = map_update_elem(&attr); |
| 2013 | break; |
| 2014 | case BPF_MAP_DELETE_ELEM: |
| 2015 | err = map_delete_elem(&attr); |
| 2016 | break; |
| 2017 | case BPF_MAP_GET_NEXT_KEY: |
| 2018 | err = map_get_next_key(&attr); |
| 2019 | break; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2020 | case BPF_PROG_LOAD: |
| 2021 | err = bpf_prog_load(&attr); |
| 2022 | break; |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 2023 | case BPF_OBJ_PIN: |
| 2024 | err = bpf_obj_pin(&attr); |
| 2025 | break; |
| 2026 | case BPF_OBJ_GET: |
| 2027 | err = bpf_obj_get(&attr); |
| 2028 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2029 | #ifdef CONFIG_CGROUP_BPF |
| 2030 | case BPF_PROG_ATTACH: |
| 2031 | err = bpf_prog_attach(&attr); |
| 2032 | break; |
| 2033 | case BPF_PROG_DETACH: |
| 2034 | err = bpf_prog_detach(&attr); |
| 2035 | break; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2036 | case BPF_PROG_QUERY: |
| 2037 | err = bpf_prog_query(&attr, uattr); |
| 2038 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2039 | #endif |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2040 | case BPF_PROG_TEST_RUN: |
| 2041 | err = bpf_prog_test_run(&attr, uattr); |
| 2042 | break; |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 2043 | case BPF_PROG_GET_NEXT_ID: |
| 2044 | err = bpf_obj_get_next_id(&attr, uattr, |
| 2045 | &prog_idr, &prog_idr_lock); |
| 2046 | break; |
| 2047 | case BPF_MAP_GET_NEXT_ID: |
| 2048 | err = bpf_obj_get_next_id(&attr, uattr, |
| 2049 | &map_idr, &map_idr_lock); |
| 2050 | break; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 2051 | case BPF_PROG_GET_FD_BY_ID: |
| 2052 | err = bpf_prog_get_fd_by_id(&attr); |
| 2053 | break; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2054 | case BPF_MAP_GET_FD_BY_ID: |
| 2055 | err = bpf_map_get_fd_by_id(&attr); |
| 2056 | break; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2057 | case BPF_OBJ_GET_INFO_BY_FD: |
| 2058 | err = bpf_obj_get_info_by_fd(&attr, uattr); |
| 2059 | break; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2060 | case BPF_RAW_TRACEPOINT_OPEN: |
| 2061 | err = bpf_raw_tracepoint_open(&attr); |
| 2062 | break; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2063 | default: |
| 2064 | err = -EINVAL; |
| 2065 | break; |
| 2066 | } |
| 2067 | |
| 2068 | return err; |
| 2069 | } |