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