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