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