Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 3 | */ |
| 4 | #include <linux/bpf.h> |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 5 | #include <linux/bpf_trace.h> |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 6 | #include <linux/bpf_lirc.h> |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 7 | #include <linux/btf.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 8 | #include <linux/syscalls.h> |
| 9 | #include <linux/slab.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 10 | #include <linux/sched/signal.h> |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 11 | #include <linux/vmalloc.h> |
| 12 | #include <linux/mmzone.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 13 | #include <linux/anon_inodes.h> |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 14 | #include <linux/fdtable.h> |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 15 | #include <linux/file.h> |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 16 | #include <linux/fs.h> |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 17 | #include <linux/license.h> |
| 18 | #include <linux/filter.h> |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 19 | #include <linux/version.h> |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 21 | #include <linux/idr.h> |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 22 | #include <linux/cred.h> |
| 23 | #include <linux/timekeeping.h> |
| 24 | #include <linux/ctype.h> |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 25 | #include <linux/nospec.h> |
Daniel Borkmann | bae141f | 2019-12-06 22:49:34 +0100 | [diff] [blame] | 26 | #include <linux/audit.h> |
Alexei Starovoitov | ccfe29e | 2019-10-15 20:24:58 -0700 | [diff] [blame] | 27 | #include <uapi/linux/btf.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 28 | |
Daniel Borkmann | da765a2 | 2019-11-22 21:07:58 +0100 | [diff] [blame] | 29 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ |
| 30 | (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ |
| 31 | (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) |
| 32 | #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY) |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 33 | #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) |
Daniel Borkmann | da765a2 | 2019-11-22 21:07:58 +0100 | [diff] [blame] | 34 | #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \ |
| 35 | IS_FD_HASH(map)) |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 36 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 37 | #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) |
| 38 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 39 | DEFINE_PER_CPU(int, bpf_prog_active); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 40 | static DEFINE_IDR(prog_idr); |
| 41 | static DEFINE_SPINLOCK(prog_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 42 | static DEFINE_IDR(map_idr); |
| 43 | static DEFINE_SPINLOCK(map_idr_lock); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 44 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 45 | int sysctl_unprivileged_bpf_disabled __read_mostly; |
| 46 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 47 | static const struct bpf_map_ops * const bpf_map_types[] = { |
Alexei Starovoitov | 91cc1a9 | 2019-11-14 10:57:15 -0800 | [diff] [blame] | 48 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 49 | #define BPF_MAP_TYPE(_id, _ops) \ |
| 50 | [_id] = &_ops, |
| 51 | #include <linux/bpf_types.h> |
| 52 | #undef BPF_PROG_TYPE |
| 53 | #undef BPF_MAP_TYPE |
| 54 | }; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 55 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 56 | /* |
| 57 | * If we're handed a bigger struct than we know of, ensure all the unknown bits |
| 58 | * are 0 - i.e. new user-space does not rely on any kernel feature extensions |
| 59 | * we don't know about yet. |
| 60 | * |
| 61 | * There is a ToCToU between this function call and the following |
| 62 | * copy_from_user() call. However, this is not a concern since this function is |
| 63 | * meant to be a future-proofing of bits. |
| 64 | */ |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 65 | int bpf_check_uarg_tail_zero(void __user *uaddr, |
| 66 | size_t expected_size, |
| 67 | size_t actual_size) |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 68 | { |
| 69 | unsigned char __user *addr; |
| 70 | unsigned char __user *end; |
| 71 | unsigned char val; |
| 72 | int err; |
| 73 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 74 | if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ |
| 75 | return -E2BIG; |
| 76 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 77 | if (unlikely(!access_ok(uaddr, actual_size))) |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 78 | return -EFAULT; |
| 79 | |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 80 | if (actual_size <= expected_size) |
| 81 | return 0; |
| 82 | |
| 83 | addr = uaddr + expected_size; |
| 84 | end = uaddr + actual_size; |
| 85 | |
| 86 | for (; addr < end; addr++) { |
| 87 | err = get_user(val, addr); |
| 88 | if (err) |
| 89 | return err; |
| 90 | if (val) |
| 91 | return -E2BIG; |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 97 | const struct bpf_map_ops bpf_map_offload_ops = { |
| 98 | .map_alloc = bpf_map_offload_map_alloc, |
| 99 | .map_free = bpf_map_offload_map_free, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 100 | .map_check_btf = map_check_no_btf, |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 103 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
| 104 | { |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 105 | const struct bpf_map_ops *ops; |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 106 | u32 type = attr->map_type; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 107 | struct bpf_map *map; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 108 | int err; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 109 | |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 110 | if (type >= ARRAY_SIZE(bpf_map_types)) |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 111 | return ERR_PTR(-EINVAL); |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 112 | type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types)); |
| 113 | ops = bpf_map_types[type]; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 114 | if (!ops) |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 115 | return ERR_PTR(-EINVAL); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 116 | |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 117 | if (ops->map_alloc_check) { |
| 118 | err = ops->map_alloc_check(attr); |
| 119 | if (err) |
| 120 | return ERR_PTR(err); |
| 121 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 122 | if (attr->map_ifindex) |
| 123 | ops = &bpf_map_offload_ops; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 124 | map = ops->map_alloc(attr); |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 125 | if (IS_ERR(map)) |
| 126 | return map; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 127 | map->ops = ops; |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 128 | map->map_type = type; |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 129 | return map; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 132 | static u32 bpf_map_value_size(struct bpf_map *map) |
| 133 | { |
| 134 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 135 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
| 136 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || |
| 137 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
| 138 | return round_up(map->value_size, 8) * num_possible_cpus(); |
| 139 | else if (IS_FD_MAP(map)) |
| 140 | return sizeof(u32); |
| 141 | else |
| 142 | return map->value_size; |
| 143 | } |
| 144 | |
| 145 | static void maybe_wait_bpf_programs(struct bpf_map *map) |
| 146 | { |
| 147 | /* Wait for any running BPF programs to complete so that |
| 148 | * userspace, when we return to it, knows that all programs |
| 149 | * that could be running use the new map value. |
| 150 | */ |
| 151 | if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS || |
| 152 | map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) |
| 153 | synchronize_rcu(); |
| 154 | } |
| 155 | |
| 156 | static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key, |
| 157 | void *value, __u64 flags) |
| 158 | { |
| 159 | int err; |
| 160 | |
| 161 | /* Need to create a kthread, thus must support schedule */ |
| 162 | if (bpf_map_is_dev_bound(map)) { |
| 163 | return bpf_map_offload_update_elem(map, key, value, flags); |
| 164 | } else if (map->map_type == BPF_MAP_TYPE_CPUMAP || |
| 165 | map->map_type == BPF_MAP_TYPE_SOCKHASH || |
| 166 | map->map_type == BPF_MAP_TYPE_SOCKMAP || |
| 167 | map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
| 168 | return map->ops->map_update_elem(map, key, value, flags); |
| 169 | } else if (IS_FD_PROG_ARRAY(map)) { |
| 170 | return bpf_fd_array_map_update_elem(map, f.file, key, value, |
| 171 | flags); |
| 172 | } |
| 173 | |
| 174 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering from |
| 175 | * inside bpf map update or delete otherwise deadlocks are possible |
| 176 | */ |
| 177 | preempt_disable(); |
| 178 | __this_cpu_inc(bpf_prog_active); |
| 179 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 180 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
| 181 | err = bpf_percpu_hash_update(map, key, value, flags); |
| 182 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 183 | err = bpf_percpu_array_update(map, key, value, flags); |
| 184 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { |
| 185 | err = bpf_percpu_cgroup_storage_update(map, key, value, |
| 186 | flags); |
| 187 | } else if (IS_FD_ARRAY(map)) { |
| 188 | rcu_read_lock(); |
| 189 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, |
| 190 | flags); |
| 191 | rcu_read_unlock(); |
| 192 | } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
| 193 | rcu_read_lock(); |
| 194 | err = bpf_fd_htab_map_update_elem(map, f.file, key, value, |
| 195 | flags); |
| 196 | rcu_read_unlock(); |
| 197 | } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { |
| 198 | /* rcu_read_lock() is not needed */ |
| 199 | err = bpf_fd_reuseport_array_update_elem(map, key, value, |
| 200 | flags); |
| 201 | } else if (map->map_type == BPF_MAP_TYPE_QUEUE || |
| 202 | map->map_type == BPF_MAP_TYPE_STACK) { |
| 203 | err = map->ops->map_push_elem(map, value, flags); |
| 204 | } else { |
| 205 | rcu_read_lock(); |
| 206 | err = map->ops->map_update_elem(map, key, value, flags); |
| 207 | rcu_read_unlock(); |
| 208 | } |
| 209 | __this_cpu_dec(bpf_prog_active); |
| 210 | preempt_enable(); |
| 211 | maybe_wait_bpf_programs(map); |
| 212 | |
| 213 | return err; |
| 214 | } |
| 215 | |
| 216 | static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value, |
| 217 | __u64 flags) |
| 218 | { |
| 219 | void *ptr; |
| 220 | int err; |
| 221 | |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 222 | if (bpf_map_is_dev_bound(map)) |
| 223 | return bpf_map_offload_lookup_elem(map, key, value); |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 224 | |
| 225 | preempt_disable(); |
| 226 | this_cpu_inc(bpf_prog_active); |
| 227 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 228 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
| 229 | err = bpf_percpu_hash_copy(map, key, value); |
| 230 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 231 | err = bpf_percpu_array_copy(map, key, value); |
| 232 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { |
| 233 | err = bpf_percpu_cgroup_storage_copy(map, key, value); |
| 234 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
| 235 | err = bpf_stackmap_copy(map, key, value); |
| 236 | } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) { |
| 237 | err = bpf_fd_array_map_lookup_elem(map, key, value); |
| 238 | } else if (IS_FD_HASH(map)) { |
| 239 | err = bpf_fd_htab_map_lookup_elem(map, key, value); |
| 240 | } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { |
| 241 | err = bpf_fd_reuseport_array_lookup_elem(map, key, value); |
| 242 | } else if (map->map_type == BPF_MAP_TYPE_QUEUE || |
| 243 | map->map_type == BPF_MAP_TYPE_STACK) { |
| 244 | err = map->ops->map_peek_elem(map, value); |
| 245 | } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
| 246 | /* struct_ops map requires directly updating "value" */ |
| 247 | err = bpf_struct_ops_map_sys_lookup_elem(map, key, value); |
| 248 | } else { |
| 249 | rcu_read_lock(); |
| 250 | if (map->ops->map_lookup_elem_sys_only) |
| 251 | ptr = map->ops->map_lookup_elem_sys_only(map, key); |
| 252 | else |
| 253 | ptr = map->ops->map_lookup_elem(map, key); |
| 254 | if (IS_ERR(ptr)) { |
| 255 | err = PTR_ERR(ptr); |
| 256 | } else if (!ptr) { |
| 257 | err = -ENOENT; |
| 258 | } else { |
| 259 | err = 0; |
| 260 | if (flags & BPF_F_LOCK) |
| 261 | /* lock 'ptr' and copy everything but lock */ |
| 262 | copy_map_value_locked(map, value, ptr, true); |
| 263 | else |
| 264 | copy_map_value(map, value, ptr); |
| 265 | /* mask lock, since value wasn't zero inited */ |
| 266 | check_and_init_map_lock(map, value); |
| 267 | } |
| 268 | rcu_read_unlock(); |
| 269 | } |
| 270 | |
| 271 | this_cpu_dec(bpf_prog_active); |
| 272 | preempt_enable(); |
| 273 | maybe_wait_bpf_programs(map); |
| 274 | |
| 275 | return err; |
| 276 | } |
| 277 | |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 278 | static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable) |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 279 | { |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 280 | /* We really just want to fail instead of triggering OOM killer |
| 281 | * under memory pressure, therefore we set __GFP_NORETRY to kmalloc, |
| 282 | * which is used for lower order allocation requests. |
| 283 | * |
| 284 | * It has been observed that higher order allocation requests done by |
| 285 | * vmalloc with __GFP_NORETRY being set might fail due to not trying |
| 286 | * to reclaim memory from the page cache, thus we set |
| 287 | * __GFP_RETRY_MAYFAIL to avoid such situations. |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 288 | */ |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 289 | |
| 290 | const gfp_t flags = __GFP_NOWARN | __GFP_ZERO; |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 291 | void *area; |
| 292 | |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 293 | if (size >= SIZE_MAX) |
| 294 | return NULL; |
| 295 | |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 296 | /* kmalloc()'ed memory can't be mmap()'ed */ |
| 297 | if (!mmapable && size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 298 | area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags, |
| 299 | numa_node); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 300 | if (area != NULL) |
| 301 | return area; |
| 302 | } |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 303 | if (mmapable) { |
| 304 | BUG_ON(!PAGE_ALIGNED(size)); |
| 305 | return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL | |
| 306 | __GFP_RETRY_MAYFAIL | flags); |
| 307 | } |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 308 | return __vmalloc_node_flags_caller(size, numa_node, |
| 309 | GFP_KERNEL | __GFP_RETRY_MAYFAIL | |
| 310 | flags, __builtin_return_address(0)); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 313 | void *bpf_map_area_alloc(u64 size, int numa_node) |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 314 | { |
| 315 | return __bpf_map_area_alloc(size, numa_node, false); |
| 316 | } |
| 317 | |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 318 | void *bpf_map_area_mmapable_alloc(u64 size, int numa_node) |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 319 | { |
| 320 | return __bpf_map_area_alloc(size, numa_node, true); |
| 321 | } |
| 322 | |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 323 | void bpf_map_area_free(void *area) |
| 324 | { |
| 325 | kvfree(area); |
| 326 | } |
| 327 | |
Daniel Borkmann | be70bcd | 2019-04-09 23:20:04 +0200 | [diff] [blame] | 328 | static u32 bpf_map_flags_retain_permanent(u32 flags) |
| 329 | { |
| 330 | /* Some map creation flags are not tied to the map object but |
| 331 | * rather to the map fd instead, so they have no meaning upon |
| 332 | * map object inspection since multiple file descriptors with |
| 333 | * different (access) properties can exist here. Thus, given |
| 334 | * this has zero meaning for the map itself, lets clear these |
| 335 | * from here. |
| 336 | */ |
| 337 | return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY); |
| 338 | } |
| 339 | |
Jakub Kicinski | bd47564 | 2018-01-11 20:29:06 -0800 | [diff] [blame] | 340 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) |
| 341 | { |
| 342 | map->map_type = attr->map_type; |
| 343 | map->key_size = attr->key_size; |
| 344 | map->value_size = attr->value_size; |
| 345 | map->max_entries = attr->max_entries; |
Daniel Borkmann | be70bcd | 2019-04-09 23:20:04 +0200 | [diff] [blame] | 346 | map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags); |
Jakub Kicinski | bd47564 | 2018-01-11 20:29:06 -0800 | [diff] [blame] | 347 | map->numa_node = bpf_map_attr_numa_node(attr); |
| 348 | } |
| 349 | |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 350 | static int bpf_charge_memlock(struct user_struct *user, u32 pages) |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 351 | { |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 352 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 353 | |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 354 | if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) { |
| 355 | atomic_long_sub(pages, &user->locked_vm); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 356 | return -EPERM; |
| 357 | } |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 361 | static void bpf_uncharge_memlock(struct user_struct *user, u32 pages) |
| 362 | { |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 363 | if (user) |
| 364 | atomic_long_sub(pages, &user->locked_vm); |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 367 | int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size) |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 368 | { |
Roman Gushchin | c85d691 | 2019-05-29 18:03:59 -0700 | [diff] [blame] | 369 | u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT; |
| 370 | struct user_struct *user; |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 371 | int ret; |
| 372 | |
Roman Gushchin | c85d691 | 2019-05-29 18:03:59 -0700 | [diff] [blame] | 373 | if (size >= U32_MAX - PAGE_SIZE) |
| 374 | return -E2BIG; |
| 375 | |
| 376 | user = get_current_user(); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 377 | ret = bpf_charge_memlock(user, pages); |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 378 | if (ret) { |
| 379 | free_uid(user); |
| 380 | return ret; |
| 381 | } |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 382 | |
| 383 | mem->pages = pages; |
| 384 | mem->user = user; |
| 385 | |
| 386 | return 0; |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 389 | void bpf_map_charge_finish(struct bpf_map_memory *mem) |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 390 | { |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 391 | bpf_uncharge_memlock(mem->user, mem->pages); |
| 392 | free_uid(mem->user); |
| 393 | } |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 394 | |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 395 | void bpf_map_charge_move(struct bpf_map_memory *dst, |
| 396 | struct bpf_map_memory *src) |
| 397 | { |
| 398 | *dst = *src; |
| 399 | |
| 400 | /* Make sure src will not be used for the redundant uncharging. */ |
| 401 | memset(src, 0, sizeof(struct bpf_map_memory)); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 404 | int bpf_map_charge_memlock(struct bpf_map *map, u32 pages) |
| 405 | { |
| 406 | int ret; |
| 407 | |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 408 | ret = bpf_charge_memlock(map->memory.user, pages); |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 409 | if (ret) |
| 410 | return ret; |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 411 | map->memory.pages += pages; |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages) |
| 416 | { |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 417 | bpf_uncharge_memlock(map->memory.user, pages); |
| 418 | map->memory.pages -= pages; |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 421 | static int bpf_map_alloc_id(struct bpf_map *map) |
| 422 | { |
| 423 | int id; |
| 424 | |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 425 | idr_preload(GFP_KERNEL); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 426 | spin_lock_bh(&map_idr_lock); |
| 427 | id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); |
| 428 | if (id > 0) |
| 429 | map->id = id; |
| 430 | spin_unlock_bh(&map_idr_lock); |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 431 | idr_preload_end(); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 432 | |
| 433 | if (WARN_ON_ONCE(!id)) |
| 434 | return -ENOSPC; |
| 435 | |
| 436 | return id > 0 ? 0 : id; |
| 437 | } |
| 438 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 439 | 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] | 440 | { |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 441 | unsigned long flags; |
| 442 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 443 | /* Offloaded maps are removed from the IDR store when their device |
| 444 | * disappears - even if someone holds an fd to them they are unusable, |
| 445 | * the memory is gone, all ops will fail; they are simply waiting for |
| 446 | * refcnt to drop to be freed. |
| 447 | */ |
| 448 | if (!map->id) |
| 449 | return; |
| 450 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 451 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 452 | spin_lock_irqsave(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 453 | else |
| 454 | __acquire(&map_idr_lock); |
| 455 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 456 | idr_remove(&map_idr, map->id); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 457 | map->id = 0; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 458 | |
| 459 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 460 | spin_unlock_irqrestore(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 461 | else |
| 462 | __release(&map_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 465 | /* called from workqueue */ |
| 466 | static void bpf_map_free_deferred(struct work_struct *work) |
| 467 | { |
| 468 | struct bpf_map *map = container_of(work, struct bpf_map, work); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 469 | struct bpf_map_memory mem; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 470 | |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 471 | bpf_map_charge_move(&mem, &map->memory); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 472 | security_bpf_map_free(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 473 | /* implementation dependent freeing */ |
| 474 | map->ops->map_free(map); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 475 | bpf_map_charge_finish(&mem); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 478 | static void bpf_map_put_uref(struct bpf_map *map) |
| 479 | { |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 480 | if (atomic64_dec_and_test(&map->usercnt)) { |
John Fastabend | ba6b8de | 2018-04-23 15:39:23 -0700 | [diff] [blame] | 481 | if (map->ops->map_release_uref) |
| 482 | map->ops->map_release_uref(map); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 486 | /* decrement map refcnt and schedule it for freeing via workqueue |
| 487 | * (unrelying map implementation ops->map_free() might sleep) |
| 488 | */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 489 | 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] | 490 | { |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 491 | if (atomic64_dec_and_test(&map->refcnt)) { |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 492 | /* bpf_map_free_id() must be called first */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 493 | bpf_map_free_id(map, do_idr_lock); |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 494 | btf_put(map->btf); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 495 | INIT_WORK(&map->work, bpf_map_free_deferred); |
| 496 | schedule_work(&map->work); |
| 497 | } |
| 498 | } |
| 499 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 500 | void bpf_map_put(struct bpf_map *map) |
| 501 | { |
| 502 | __bpf_map_put(map, true); |
| 503 | } |
Jakub Kicinski | 630a4d3 | 2018-05-03 18:37:09 -0700 | [diff] [blame] | 504 | EXPORT_SYMBOL_GPL(bpf_map_put); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 505 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 506 | void bpf_map_put_with_uref(struct bpf_map *map) |
| 507 | { |
| 508 | bpf_map_put_uref(map); |
| 509 | bpf_map_put(map); |
| 510 | } |
| 511 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 512 | static int bpf_map_release(struct inode *inode, struct file *filp) |
| 513 | { |
Daniel Borkmann | 61d1b6a | 2016-06-15 22:47:12 +0200 | [diff] [blame] | 514 | struct bpf_map *map = filp->private_data; |
| 515 | |
| 516 | if (map->ops->map_release) |
| 517 | map->ops->map_release(map, filp); |
| 518 | |
| 519 | bpf_map_put_with_uref(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 520 | return 0; |
| 521 | } |
| 522 | |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 523 | static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f) |
| 524 | { |
| 525 | fmode_t mode = f.file->f_mode; |
| 526 | |
| 527 | /* Our file permissions may have been overridden by global |
| 528 | * map permissions facing syscall side. |
| 529 | */ |
| 530 | if (READ_ONCE(map->frozen)) |
| 531 | mode &= ~FMODE_CAN_WRITE; |
| 532 | return mode; |
| 533 | } |
| 534 | |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 535 | #ifdef CONFIG_PROC_FS |
| 536 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) |
| 537 | { |
| 538 | const struct bpf_map *map = filp->private_data; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 539 | const struct bpf_array *array; |
Daniel Borkmann | 2beee5f | 2019-11-22 21:07:56 +0100 | [diff] [blame] | 540 | u32 type = 0, jited = 0; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 541 | |
| 542 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { |
| 543 | array = container_of(map, struct bpf_array, map); |
Daniel Borkmann | 2beee5f | 2019-11-22 21:07:56 +0100 | [diff] [blame] | 544 | type = array->aux->type; |
| 545 | jited = array->aux->jited; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 546 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 547 | |
| 548 | seq_printf(m, |
| 549 | "map_type:\t%u\n" |
| 550 | "key_size:\t%u\n" |
| 551 | "value_size:\t%u\n" |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 552 | "max_entries:\t%u\n" |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 553 | "map_flags:\t%#x\n" |
Daniel Borkmann | 4316b40 | 2018-06-02 23:06:34 +0200 | [diff] [blame] | 554 | "memlock:\t%llu\n" |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 555 | "map_id:\t%u\n" |
| 556 | "frozen:\t%u\n", |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 557 | map->map_type, |
| 558 | map->key_size, |
| 559 | map->value_size, |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 560 | map->max_entries, |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 561 | map->map_flags, |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 562 | map->memory.pages * 1ULL << PAGE_SHIFT, |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 563 | map->id, |
| 564 | READ_ONCE(map->frozen)); |
Daniel Borkmann | 2beee5f | 2019-11-22 21:07:56 +0100 | [diff] [blame] | 565 | if (type) { |
| 566 | seq_printf(m, "owner_prog_type:\t%u\n", type); |
| 567 | seq_printf(m, "owner_jited:\t%u\n", jited); |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 568 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 569 | } |
| 570 | #endif |
| 571 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 572 | static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, |
| 573 | loff_t *ppos) |
| 574 | { |
| 575 | /* We need this handler such that alloc_file() enables |
| 576 | * f_mode with FMODE_CAN_READ. |
| 577 | */ |
| 578 | return -EINVAL; |
| 579 | } |
| 580 | |
| 581 | static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, |
| 582 | size_t siz, loff_t *ppos) |
| 583 | { |
| 584 | /* We need this handler such that alloc_file() enables |
| 585 | * f_mode with FMODE_CAN_WRITE. |
| 586 | */ |
| 587 | return -EINVAL; |
| 588 | } |
| 589 | |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 590 | /* called for any extra memory-mapped regions (except initial) */ |
| 591 | static void bpf_map_mmap_open(struct vm_area_struct *vma) |
| 592 | { |
| 593 | struct bpf_map *map = vma->vm_file->private_data; |
| 594 | |
| 595 | bpf_map_inc_with_uref(map); |
| 596 | |
| 597 | if (vma->vm_flags & VM_WRITE) { |
| 598 | mutex_lock(&map->freeze_mutex); |
| 599 | map->writecnt++; |
| 600 | mutex_unlock(&map->freeze_mutex); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /* called for all unmapped memory region (including initial) */ |
| 605 | static void bpf_map_mmap_close(struct vm_area_struct *vma) |
| 606 | { |
| 607 | struct bpf_map *map = vma->vm_file->private_data; |
| 608 | |
| 609 | if (vma->vm_flags & VM_WRITE) { |
| 610 | mutex_lock(&map->freeze_mutex); |
| 611 | map->writecnt--; |
| 612 | mutex_unlock(&map->freeze_mutex); |
| 613 | } |
| 614 | |
| 615 | bpf_map_put_with_uref(map); |
| 616 | } |
| 617 | |
| 618 | static const struct vm_operations_struct bpf_map_default_vmops = { |
| 619 | .open = bpf_map_mmap_open, |
| 620 | .close = bpf_map_mmap_close, |
| 621 | }; |
| 622 | |
| 623 | static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma) |
| 624 | { |
| 625 | struct bpf_map *map = filp->private_data; |
| 626 | int err; |
| 627 | |
| 628 | if (!map->ops->map_mmap || map_value_has_spin_lock(map)) |
| 629 | return -ENOTSUPP; |
| 630 | |
| 631 | if (!(vma->vm_flags & VM_SHARED)) |
| 632 | return -EINVAL; |
| 633 | |
| 634 | mutex_lock(&map->freeze_mutex); |
| 635 | |
| 636 | if ((vma->vm_flags & VM_WRITE) && map->frozen) { |
| 637 | err = -EPERM; |
| 638 | goto out; |
| 639 | } |
| 640 | |
| 641 | /* set default open/close callbacks */ |
| 642 | vma->vm_ops = &bpf_map_default_vmops; |
| 643 | vma->vm_private_data = map; |
| 644 | |
| 645 | err = map->ops->map_mmap(map, vma); |
| 646 | if (err) |
| 647 | goto out; |
| 648 | |
| 649 | bpf_map_inc_with_uref(map); |
| 650 | |
| 651 | if (vma->vm_flags & VM_WRITE) |
| 652 | map->writecnt++; |
| 653 | out: |
| 654 | mutex_unlock(&map->freeze_mutex); |
| 655 | return err; |
| 656 | } |
| 657 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 658 | const struct file_operations bpf_map_fops = { |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 659 | #ifdef CONFIG_PROC_FS |
| 660 | .show_fdinfo = bpf_map_show_fdinfo, |
| 661 | #endif |
| 662 | .release = bpf_map_release, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 663 | .read = bpf_dummy_read, |
| 664 | .write = bpf_dummy_write, |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 665 | .mmap = bpf_map_mmap, |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 666 | }; |
| 667 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 668 | int bpf_map_new_fd(struct bpf_map *map, int flags) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 669 | { |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 670 | int ret; |
| 671 | |
| 672 | ret = security_bpf_map(map, OPEN_FMODE(flags)); |
| 673 | if (ret < 0) |
| 674 | return ret; |
| 675 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 676 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 677 | flags | O_CLOEXEC); |
| 678 | } |
| 679 | |
| 680 | int bpf_get_file_flag(int flags) |
| 681 | { |
| 682 | if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) |
| 683 | return -EINVAL; |
| 684 | if (flags & BPF_F_RDONLY) |
| 685 | return O_RDONLY; |
| 686 | if (flags & BPF_F_WRONLY) |
| 687 | return O_WRONLY; |
| 688 | return O_RDWR; |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 689 | } |
| 690 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 691 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
| 692 | #define CHECK_ATTR(CMD) \ |
| 693 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ |
| 694 | sizeof(attr->CMD##_LAST_FIELD), 0, \ |
| 695 | sizeof(*attr) - \ |
| 696 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ |
| 697 | sizeof(attr->CMD##_LAST_FIELD)) != NULL |
| 698 | |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame^] | 699 | /* dst and src must have at least "size" number of bytes. |
| 700 | * Return strlen on success and < 0 on error. |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 701 | */ |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame^] | 702 | int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size) |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 703 | { |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame^] | 704 | const char *end = src + size; |
| 705 | const char *orig_src = src; |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 706 | |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame^] | 707 | memset(dst, 0, size); |
Daniel Borkmann | 3e0ddc4f | 2019-04-09 23:20:07 +0200 | [diff] [blame] | 708 | /* Copy all isalnum(), '_' and '.' chars. */ |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 709 | while (src < end && *src) { |
Daniel Borkmann | 3e0ddc4f | 2019-04-09 23:20:07 +0200 | [diff] [blame] | 710 | if (!isalnum(*src) && |
| 711 | *src != '_' && *src != '.') |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 712 | return -EINVAL; |
| 713 | *dst++ = *src++; |
| 714 | } |
| 715 | |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame^] | 716 | /* No '\0' found in "size" number of bytes */ |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 717 | if (src == end) |
| 718 | return -EINVAL; |
| 719 | |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame^] | 720 | return src - orig_src; |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 723 | int map_check_no_btf(const struct bpf_map *map, |
Roman Gushchin | 1b2b234 | 2018-12-10 15:43:00 -0800 | [diff] [blame] | 724 | const struct btf *btf, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 725 | const struct btf_type *key_type, |
| 726 | const struct btf_type *value_type) |
| 727 | { |
| 728 | return -ENOTSUPP; |
| 729 | } |
| 730 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 731 | static int map_check_btf(struct bpf_map *map, const struct btf *btf, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 732 | u32 btf_key_id, u32 btf_value_id) |
| 733 | { |
| 734 | const struct btf_type *key_type, *value_type; |
| 735 | u32 key_size, value_size; |
| 736 | int ret = 0; |
| 737 | |
Daniel Borkmann | 2824ecb | 2019-04-09 23:20:10 +0200 | [diff] [blame] | 738 | /* Some maps allow key to be unspecified. */ |
| 739 | if (btf_key_id) { |
| 740 | key_type = btf_type_id_size(btf, &btf_key_id, &key_size); |
| 741 | if (!key_type || key_size != map->key_size) |
| 742 | return -EINVAL; |
| 743 | } else { |
| 744 | key_type = btf_type_by_id(btf, 0); |
| 745 | if (!map->ops->map_check_btf) |
| 746 | return -EINVAL; |
| 747 | } |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 748 | |
| 749 | value_type = btf_type_id_size(btf, &btf_value_id, &value_size); |
| 750 | if (!value_type || value_size != map->value_size) |
| 751 | return -EINVAL; |
| 752 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 753 | map->spin_lock_off = btf_find_spin_lock(btf, value_type); |
| 754 | |
| 755 | if (map_value_has_spin_lock(map)) { |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 756 | if (map->map_flags & BPF_F_RDONLY_PROG) |
| 757 | return -EACCES; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 758 | if (map->map_type != BPF_MAP_TYPE_HASH && |
Alexei Starovoitov | e16d2f1 | 2019-01-31 15:40:05 -0800 | [diff] [blame] | 759 | map->map_type != BPF_MAP_TYPE_ARRAY && |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 760 | map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 761 | map->map_type != BPF_MAP_TYPE_SK_STORAGE) |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 762 | return -ENOTSUPP; |
| 763 | if (map->spin_lock_off + sizeof(struct bpf_spin_lock) > |
| 764 | map->value_size) { |
| 765 | WARN_ONCE(1, |
| 766 | "verifier bug spin_lock_off %d value_size %d\n", |
| 767 | map->spin_lock_off, map->value_size); |
| 768 | return -EFAULT; |
| 769 | } |
| 770 | } |
| 771 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 772 | if (map->ops->map_check_btf) |
Roman Gushchin | 1b2b234 | 2018-12-10 15:43:00 -0800 | [diff] [blame] | 773 | ret = map->ops->map_check_btf(map, btf, key_type, value_type); |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 774 | |
| 775 | return ret; |
| 776 | } |
| 777 | |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 778 | #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 779 | /* called via syscall */ |
| 780 | static int map_create(union bpf_attr *attr) |
| 781 | { |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 782 | int numa_node = bpf_map_attr_numa_node(attr); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 783 | struct bpf_map_memory mem; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 784 | struct bpf_map *map; |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 785 | int f_flags; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 786 | int err; |
| 787 | |
| 788 | err = CHECK_ATTR(BPF_MAP_CREATE); |
| 789 | if (err) |
| 790 | return -EINVAL; |
| 791 | |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 792 | if (attr->btf_vmlinux_value_type_id) { |
| 793 | if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS || |
| 794 | attr->btf_key_type_id || attr->btf_value_type_id) |
| 795 | return -EINVAL; |
| 796 | } else if (attr->btf_key_type_id && !attr->btf_value_type_id) { |
| 797 | return -EINVAL; |
| 798 | } |
| 799 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 800 | f_flags = bpf_get_file_flag(attr->map_flags); |
| 801 | if (f_flags < 0) |
| 802 | return f_flags; |
| 803 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 804 | if (numa_node != NUMA_NO_NODE && |
Eric Dumazet | 96e5ae4 | 2017-09-04 22:41:02 -0700 | [diff] [blame] | 805 | ((unsigned int)numa_node >= nr_node_ids || |
| 806 | !node_online(numa_node))) |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 807 | return -EINVAL; |
| 808 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 809 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ |
| 810 | map = find_and_alloc_map(attr); |
| 811 | if (IS_ERR(map)) |
| 812 | return PTR_ERR(map); |
| 813 | |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame^] | 814 | err = bpf_obj_name_cpy(map->name, attr->map_name, |
| 815 | sizeof(attr->map_name)); |
| 816 | if (err < 0) |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 817 | goto free_map; |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 818 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 819 | atomic64_set(&map->refcnt, 1); |
| 820 | atomic64_set(&map->usercnt, 1); |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 821 | mutex_init(&map->freeze_mutex); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 822 | |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 823 | map->spin_lock_off = -EINVAL; |
| 824 | if (attr->btf_key_type_id || attr->btf_value_type_id || |
| 825 | /* Even the map's value is a kernel's struct, |
| 826 | * the bpf_prog.o must have BTF to begin with |
| 827 | * to figure out the corresponding kernel's |
| 828 | * counter part. Thus, attr->btf_fd has |
| 829 | * to be valid also. |
| 830 | */ |
| 831 | attr->btf_vmlinux_value_type_id) { |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 832 | struct btf *btf; |
| 833 | |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 834 | btf = btf_get_by_fd(attr->btf_fd); |
| 835 | if (IS_ERR(btf)) { |
| 836 | err = PTR_ERR(btf); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 837 | goto free_map; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 838 | } |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 839 | map->btf = btf; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 840 | |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 841 | if (attr->btf_value_type_id) { |
| 842 | err = map_check_btf(map, btf, attr->btf_key_type_id, |
| 843 | attr->btf_value_type_id); |
| 844 | if (err) |
| 845 | goto free_map; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Martin KaFai Lau | 9b2cf32 | 2018-05-22 14:57:21 -0700 | [diff] [blame] | 848 | map->btf_key_type_id = attr->btf_key_type_id; |
| 849 | map->btf_value_type_id = attr->btf_value_type_id; |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 850 | map->btf_vmlinux_value_type_id = |
| 851 | attr->btf_vmlinux_value_type_id; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 854 | err = security_bpf_map_alloc(map); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 855 | if (err) |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 856 | goto free_map; |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 857 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 858 | err = bpf_map_alloc_id(map); |
| 859 | if (err) |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 860 | goto free_map_sec; |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 861 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 862 | err = bpf_map_new_fd(map, f_flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 863 | if (err < 0) { |
| 864 | /* failed to allocate fd. |
Peng Sun | 352d20d | 2019-02-27 22:36:25 +0800 | [diff] [blame] | 865 | * bpf_map_put_with_uref() is needed because the above |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 866 | * bpf_map_alloc_id() has published the map |
| 867 | * to the userspace and the userspace may |
| 868 | * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. |
| 869 | */ |
Peng Sun | 352d20d | 2019-02-27 22:36:25 +0800 | [diff] [blame] | 870 | bpf_map_put_with_uref(map); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 871 | return err; |
| 872 | } |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 873 | |
| 874 | return err; |
| 875 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 876 | free_map_sec: |
| 877 | security_bpf_map_free(map); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 878 | free_map: |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 879 | btf_put(map->btf); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 880 | bpf_map_charge_move(&mem, &map->memory); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 881 | map->ops->map_free(map); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 882 | bpf_map_charge_finish(&mem); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 883 | return err; |
| 884 | } |
| 885 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 886 | /* if error is returned, fd is released. |
| 887 | * On success caller should complete fd access with matching fdput() |
| 888 | */ |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 889 | struct bpf_map *__bpf_map_get(struct fd f) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 890 | { |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 891 | if (!f.file) |
| 892 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 893 | if (f.file->f_op != &bpf_map_fops) { |
| 894 | fdput(f); |
| 895 | return ERR_PTR(-EINVAL); |
| 896 | } |
| 897 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 898 | return f.file->private_data; |
| 899 | } |
| 900 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 901 | void bpf_map_inc(struct bpf_map *map) |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 902 | { |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 903 | atomic64_inc(&map->refcnt); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 904 | } |
Jakub Kicinski | 630a4d3 | 2018-05-03 18:37:09 -0700 | [diff] [blame] | 905 | EXPORT_SYMBOL_GPL(bpf_map_inc); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 906 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 907 | void bpf_map_inc_with_uref(struct bpf_map *map) |
| 908 | { |
| 909 | atomic64_inc(&map->refcnt); |
| 910 | atomic64_inc(&map->usercnt); |
| 911 | } |
| 912 | EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref); |
| 913 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 914 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 915 | { |
| 916 | struct fd f = fdget(ufd); |
| 917 | struct bpf_map *map; |
| 918 | |
| 919 | map = __bpf_map_get(f); |
| 920 | if (IS_ERR(map)) |
| 921 | return map; |
| 922 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 923 | bpf_map_inc_with_uref(map); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 924 | fdput(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 925 | |
| 926 | return map; |
| 927 | } |
| 928 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 929 | /* map_idr_lock should have been held */ |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 930 | static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref) |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 931 | { |
| 932 | int refold; |
| 933 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 934 | refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 935 | if (!refold) |
| 936 | return ERR_PTR(-ENOENT); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 937 | if (uref) |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 938 | atomic64_inc(&map->usercnt); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 939 | |
| 940 | return map; |
| 941 | } |
| 942 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 943 | struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map) |
Stanislav Fomichev | b0e4701 | 2019-08-14 10:37:48 -0700 | [diff] [blame] | 944 | { |
| 945 | spin_lock_bh(&map_idr_lock); |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 946 | map = __bpf_map_inc_not_zero(map, false); |
Stanislav Fomichev | b0e4701 | 2019-08-14 10:37:48 -0700 | [diff] [blame] | 947 | spin_unlock_bh(&map_idr_lock); |
| 948 | |
| 949 | return map; |
| 950 | } |
| 951 | EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero); |
| 952 | |
Alexei Starovoitov | b8cdc05 | 2016-03-09 18:56:49 -0800 | [diff] [blame] | 953 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
| 954 | { |
| 955 | return -ENOTSUPP; |
| 956 | } |
| 957 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 958 | static void *__bpf_copy_key(void __user *ukey, u64 key_size) |
| 959 | { |
| 960 | if (key_size) |
| 961 | return memdup_user(ukey, key_size); |
| 962 | |
| 963 | if (ukey) |
| 964 | return ERR_PTR(-EINVAL); |
| 965 | |
| 966 | return NULL; |
| 967 | } |
| 968 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 969 | /* last field in 'union bpf_attr' used by this command */ |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 970 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 971 | |
| 972 | static int map_lookup_elem(union bpf_attr *attr) |
| 973 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 974 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 975 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 976 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 977 | struct bpf_map *map; |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 978 | void *key, *value; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 979 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 980 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 981 | int err; |
| 982 | |
| 983 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) |
| 984 | return -EINVAL; |
| 985 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 986 | if (attr->flags & ~BPF_F_LOCK) |
| 987 | return -EINVAL; |
| 988 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 989 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 990 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 991 | if (IS_ERR(map)) |
| 992 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 993 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 994 | err = -EPERM; |
| 995 | goto err_put; |
| 996 | } |
| 997 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 998 | if ((attr->flags & BPF_F_LOCK) && |
| 999 | !map_value_has_spin_lock(map)) { |
| 1000 | err = -EINVAL; |
| 1001 | goto err_put; |
| 1002 | } |
| 1003 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 1004 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1005 | if (IS_ERR(key)) { |
| 1006 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1007 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1008 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1009 | |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 1010 | value_size = bpf_map_value_size(map); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1011 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 1012 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1013 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1014 | if (!value) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 1015 | goto free_key; |
| 1016 | |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 1017 | err = bpf_map_copy_value(map, key, value, attr->flags); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1018 | if (err) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 1019 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1020 | |
| 1021 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1022 | if (copy_to_user(uvalue, value, value_size) != 0) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 1023 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1024 | |
| 1025 | err = 0; |
| 1026 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 1027 | free_value: |
| 1028 | kfree(value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1029 | free_key: |
| 1030 | kfree(key); |
| 1031 | err_put: |
| 1032 | fdput(f); |
| 1033 | return err; |
| 1034 | } |
| 1035 | |
Daniel Colascione | 1ae80cf | 2018-10-12 03:54:27 -0700 | [diff] [blame] | 1036 | |
Alexei Starovoitov | 3274f52 | 2014-11-13 17:36:44 -0800 | [diff] [blame] | 1037 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1038 | |
| 1039 | static int map_update_elem(union bpf_attr *attr) |
| 1040 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1041 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 1042 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1043 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1044 | struct bpf_map *map; |
| 1045 | void *key, *value; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1046 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1047 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1048 | int err; |
| 1049 | |
| 1050 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) |
| 1051 | return -EINVAL; |
| 1052 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1053 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1054 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1055 | if (IS_ERR(map)) |
| 1056 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1057 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1058 | err = -EPERM; |
| 1059 | goto err_put; |
| 1060 | } |
| 1061 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 1062 | if ((attr->flags & BPF_F_LOCK) && |
| 1063 | !map_value_has_spin_lock(map)) { |
| 1064 | err = -EINVAL; |
| 1065 | goto err_put; |
| 1066 | } |
| 1067 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 1068 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1069 | if (IS_ERR(key)) { |
| 1070 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1071 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1072 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1073 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1074 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 1075 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 1076 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || |
| 1077 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1078 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
| 1079 | else |
| 1080 | value_size = map->value_size; |
| 1081 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1082 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1083 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1084 | if (!value) |
| 1085 | goto free_key; |
| 1086 | |
| 1087 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1088 | if (copy_from_user(value, uvalue, value_size) != 0) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1089 | goto free_value; |
| 1090 | |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 1091 | err = bpf_map_update_value(map, f, key, value, attr->flags); |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 1092 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1093 | free_value: |
| 1094 | kfree(value); |
| 1095 | free_key: |
| 1096 | kfree(key); |
| 1097 | err_put: |
| 1098 | fdput(f); |
| 1099 | return err; |
| 1100 | } |
| 1101 | |
| 1102 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key |
| 1103 | |
| 1104 | static int map_delete_elem(union bpf_attr *attr) |
| 1105 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1106 | void __user *ukey = u64_to_user_ptr(attr->key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1107 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1108 | struct bpf_map *map; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1109 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1110 | void *key; |
| 1111 | int err; |
| 1112 | |
| 1113 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) |
| 1114 | return -EINVAL; |
| 1115 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1116 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1117 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1118 | if (IS_ERR(map)) |
| 1119 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1120 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1121 | err = -EPERM; |
| 1122 | goto err_put; |
| 1123 | } |
| 1124 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 1125 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1126 | if (IS_ERR(key)) { |
| 1127 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1128 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1129 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1130 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1131 | if (bpf_map_is_dev_bound(map)) { |
| 1132 | err = bpf_map_offload_delete_elem(map, key); |
| 1133 | goto out; |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 1134 | } else if (IS_FD_PROG_ARRAY(map) || |
| 1135 | map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
| 1136 | /* These maps require sleepable context */ |
Daniel Borkmann | da765a2 | 2019-11-22 21:07:58 +0100 | [diff] [blame] | 1137 | err = map->ops->map_delete_elem(map, key); |
| 1138 | goto out; |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1139 | } |
| 1140 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 1141 | preempt_disable(); |
| 1142 | __this_cpu_inc(bpf_prog_active); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1143 | rcu_read_lock(); |
| 1144 | err = map->ops->map_delete_elem(map, key); |
| 1145 | rcu_read_unlock(); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 1146 | __this_cpu_dec(bpf_prog_active); |
| 1147 | preempt_enable(); |
Daniel Colascione | 1ae80cf | 2018-10-12 03:54:27 -0700 | [diff] [blame] | 1148 | maybe_wait_bpf_programs(map); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1149 | out: |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1150 | kfree(key); |
| 1151 | err_put: |
| 1152 | fdput(f); |
| 1153 | return err; |
| 1154 | } |
| 1155 | |
| 1156 | /* last field in 'union bpf_attr' used by this command */ |
| 1157 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key |
| 1158 | |
| 1159 | static int map_get_next_key(union bpf_attr *attr) |
| 1160 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1161 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 1162 | void __user *unext_key = u64_to_user_ptr(attr->next_key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1163 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1164 | struct bpf_map *map; |
| 1165 | void *key, *next_key; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1166 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1167 | int err; |
| 1168 | |
| 1169 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) |
| 1170 | return -EINVAL; |
| 1171 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1172 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1173 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1174 | if (IS_ERR(map)) |
| 1175 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1176 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1177 | err = -EPERM; |
| 1178 | goto err_put; |
| 1179 | } |
| 1180 | |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 1181 | if (ukey) { |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 1182 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1183 | if (IS_ERR(key)) { |
| 1184 | err = PTR_ERR(key); |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 1185 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1186 | } |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 1187 | } else { |
| 1188 | key = NULL; |
| 1189 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1190 | |
| 1191 | err = -ENOMEM; |
| 1192 | next_key = kmalloc(map->key_size, GFP_USER); |
| 1193 | if (!next_key) |
| 1194 | goto free_key; |
| 1195 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1196 | if (bpf_map_is_dev_bound(map)) { |
| 1197 | err = bpf_map_offload_get_next_key(map, key, next_key); |
| 1198 | goto out; |
| 1199 | } |
| 1200 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1201 | rcu_read_lock(); |
| 1202 | err = map->ops->map_get_next_key(map, key, next_key); |
| 1203 | rcu_read_unlock(); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1204 | out: |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1205 | if (err) |
| 1206 | goto free_next_key; |
| 1207 | |
| 1208 | err = -EFAULT; |
| 1209 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) |
| 1210 | goto free_next_key; |
| 1211 | |
| 1212 | err = 0; |
| 1213 | |
| 1214 | free_next_key: |
| 1215 | kfree(next_key); |
| 1216 | free_key: |
| 1217 | kfree(key); |
| 1218 | err_put: |
| 1219 | fdput(f); |
| 1220 | return err; |
| 1221 | } |
| 1222 | |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1223 | int generic_map_delete_batch(struct bpf_map *map, |
| 1224 | const union bpf_attr *attr, |
| 1225 | union bpf_attr __user *uattr) |
| 1226 | { |
| 1227 | void __user *keys = u64_to_user_ptr(attr->batch.keys); |
| 1228 | u32 cp, max_count; |
| 1229 | int err = 0; |
| 1230 | void *key; |
| 1231 | |
| 1232 | if (attr->batch.elem_flags & ~BPF_F_LOCK) |
| 1233 | return -EINVAL; |
| 1234 | |
| 1235 | if ((attr->batch.elem_flags & BPF_F_LOCK) && |
| 1236 | !map_value_has_spin_lock(map)) { |
| 1237 | return -EINVAL; |
| 1238 | } |
| 1239 | |
| 1240 | max_count = attr->batch.count; |
| 1241 | if (!max_count) |
| 1242 | return 0; |
| 1243 | |
Brian Vazquez | 2e3a94a | 2020-01-19 11:40:40 -0800 | [diff] [blame] | 1244 | key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN); |
| 1245 | if (!key) |
| 1246 | return -ENOMEM; |
| 1247 | |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1248 | for (cp = 0; cp < max_count; cp++) { |
Brian Vazquez | 2e3a94a | 2020-01-19 11:40:40 -0800 | [diff] [blame] | 1249 | err = -EFAULT; |
| 1250 | if (copy_from_user(key, keys + cp * map->key_size, |
| 1251 | map->key_size)) |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1252 | break; |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1253 | |
| 1254 | if (bpf_map_is_dev_bound(map)) { |
| 1255 | err = bpf_map_offload_delete_elem(map, key); |
| 1256 | break; |
| 1257 | } |
| 1258 | |
| 1259 | preempt_disable(); |
| 1260 | __this_cpu_inc(bpf_prog_active); |
| 1261 | rcu_read_lock(); |
| 1262 | err = map->ops->map_delete_elem(map, key); |
| 1263 | rcu_read_unlock(); |
| 1264 | __this_cpu_dec(bpf_prog_active); |
| 1265 | preempt_enable(); |
| 1266 | maybe_wait_bpf_programs(map); |
| 1267 | if (err) |
| 1268 | break; |
| 1269 | } |
| 1270 | if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) |
| 1271 | err = -EFAULT; |
Brian Vazquez | 2e3a94a | 2020-01-19 11:40:40 -0800 | [diff] [blame] | 1272 | |
| 1273 | kfree(key); |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1274 | return err; |
| 1275 | } |
| 1276 | |
| 1277 | int generic_map_update_batch(struct bpf_map *map, |
| 1278 | const union bpf_attr *attr, |
| 1279 | union bpf_attr __user *uattr) |
| 1280 | { |
| 1281 | void __user *values = u64_to_user_ptr(attr->batch.values); |
| 1282 | void __user *keys = u64_to_user_ptr(attr->batch.keys); |
| 1283 | u32 value_size, cp, max_count; |
| 1284 | int ufd = attr->map_fd; |
| 1285 | void *key, *value; |
| 1286 | struct fd f; |
| 1287 | int err = 0; |
| 1288 | |
| 1289 | f = fdget(ufd); |
| 1290 | if (attr->batch.elem_flags & ~BPF_F_LOCK) |
| 1291 | return -EINVAL; |
| 1292 | |
| 1293 | if ((attr->batch.elem_flags & BPF_F_LOCK) && |
| 1294 | !map_value_has_spin_lock(map)) { |
| 1295 | return -EINVAL; |
| 1296 | } |
| 1297 | |
| 1298 | value_size = bpf_map_value_size(map); |
| 1299 | |
| 1300 | max_count = attr->batch.count; |
| 1301 | if (!max_count) |
| 1302 | return 0; |
| 1303 | |
Brian Vazquez | 2e3a94a | 2020-01-19 11:40:40 -0800 | [diff] [blame] | 1304 | key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN); |
| 1305 | if (!key) |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1306 | return -ENOMEM; |
| 1307 | |
Brian Vazquez | 2e3a94a | 2020-01-19 11:40:40 -0800 | [diff] [blame] | 1308 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
| 1309 | if (!value) { |
| 1310 | kfree(key); |
| 1311 | return -ENOMEM; |
| 1312 | } |
| 1313 | |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1314 | for (cp = 0; cp < max_count; cp++) { |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1315 | err = -EFAULT; |
Brian Vazquez | 2e3a94a | 2020-01-19 11:40:40 -0800 | [diff] [blame] | 1316 | if (copy_from_user(key, keys + cp * map->key_size, |
| 1317 | map->key_size) || |
| 1318 | copy_from_user(value, values + cp * value_size, value_size)) |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1319 | break; |
| 1320 | |
| 1321 | err = bpf_map_update_value(map, f, key, value, |
| 1322 | attr->batch.elem_flags); |
| 1323 | |
| 1324 | if (err) |
| 1325 | break; |
| 1326 | } |
| 1327 | |
| 1328 | if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) |
| 1329 | err = -EFAULT; |
| 1330 | |
| 1331 | kfree(value); |
| 1332 | kfree(key); |
| 1333 | return err; |
| 1334 | } |
| 1335 | |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 1336 | #define MAP_LOOKUP_RETRIES 3 |
| 1337 | |
| 1338 | int generic_map_lookup_batch(struct bpf_map *map, |
| 1339 | const union bpf_attr *attr, |
| 1340 | union bpf_attr __user *uattr) |
| 1341 | { |
| 1342 | void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch); |
| 1343 | void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch); |
| 1344 | void __user *values = u64_to_user_ptr(attr->batch.values); |
| 1345 | void __user *keys = u64_to_user_ptr(attr->batch.keys); |
| 1346 | void *buf, *buf_prevkey, *prev_key, *key, *value; |
| 1347 | int err, retry = MAP_LOOKUP_RETRIES; |
| 1348 | u32 value_size, cp, max_count; |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 1349 | |
| 1350 | if (attr->batch.elem_flags & ~BPF_F_LOCK) |
| 1351 | return -EINVAL; |
| 1352 | |
| 1353 | if ((attr->batch.elem_flags & BPF_F_LOCK) && |
| 1354 | !map_value_has_spin_lock(map)) |
| 1355 | return -EINVAL; |
| 1356 | |
| 1357 | value_size = bpf_map_value_size(map); |
| 1358 | |
| 1359 | max_count = attr->batch.count; |
| 1360 | if (!max_count) |
| 1361 | return 0; |
| 1362 | |
| 1363 | if (put_user(0, &uattr->batch.count)) |
| 1364 | return -EFAULT; |
| 1365 | |
| 1366 | buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN); |
| 1367 | if (!buf_prevkey) |
| 1368 | return -ENOMEM; |
| 1369 | |
| 1370 | buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN); |
| 1371 | if (!buf) { |
| 1372 | kvfree(buf_prevkey); |
| 1373 | return -ENOMEM; |
| 1374 | } |
| 1375 | |
| 1376 | err = -EFAULT; |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 1377 | prev_key = NULL; |
| 1378 | if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size)) |
| 1379 | goto free_buf; |
| 1380 | key = buf; |
| 1381 | value = key + map->key_size; |
| 1382 | if (ubatch) |
| 1383 | prev_key = buf_prevkey; |
| 1384 | |
| 1385 | for (cp = 0; cp < max_count;) { |
| 1386 | rcu_read_lock(); |
| 1387 | err = map->ops->map_get_next_key(map, prev_key, key); |
| 1388 | rcu_read_unlock(); |
| 1389 | if (err) |
| 1390 | break; |
| 1391 | err = bpf_map_copy_value(map, key, value, |
| 1392 | attr->batch.elem_flags); |
| 1393 | |
| 1394 | if (err == -ENOENT) { |
| 1395 | if (retry) { |
| 1396 | retry--; |
| 1397 | continue; |
| 1398 | } |
| 1399 | err = -EINTR; |
| 1400 | break; |
| 1401 | } |
| 1402 | |
| 1403 | if (err) |
| 1404 | goto free_buf; |
| 1405 | |
| 1406 | if (copy_to_user(keys + cp * map->key_size, key, |
| 1407 | map->key_size)) { |
| 1408 | err = -EFAULT; |
| 1409 | goto free_buf; |
| 1410 | } |
| 1411 | if (copy_to_user(values + cp * value_size, value, value_size)) { |
| 1412 | err = -EFAULT; |
| 1413 | goto free_buf; |
| 1414 | } |
| 1415 | |
| 1416 | if (!prev_key) |
| 1417 | prev_key = buf_prevkey; |
| 1418 | |
| 1419 | swap(prev_key, key); |
| 1420 | retry = MAP_LOOKUP_RETRIES; |
| 1421 | cp++; |
| 1422 | } |
| 1423 | |
| 1424 | if (err == -EFAULT) |
| 1425 | goto free_buf; |
| 1426 | |
| 1427 | if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) || |
| 1428 | (cp && copy_to_user(uobatch, prev_key, map->key_size)))) |
| 1429 | err = -EFAULT; |
| 1430 | |
| 1431 | free_buf: |
| 1432 | kfree(buf_prevkey); |
| 1433 | kfree(buf); |
| 1434 | return err; |
| 1435 | } |
| 1436 | |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1437 | #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value |
| 1438 | |
| 1439 | static int map_lookup_and_delete_elem(union bpf_attr *attr) |
| 1440 | { |
| 1441 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 1442 | void __user *uvalue = u64_to_user_ptr(attr->value); |
| 1443 | int ufd = attr->map_fd; |
| 1444 | struct bpf_map *map; |
Alexei Starovoitov | 540fefc | 2018-10-19 13:52:38 -0700 | [diff] [blame] | 1445 | void *key, *value; |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1446 | u32 value_size; |
| 1447 | struct fd f; |
| 1448 | int err; |
| 1449 | |
| 1450 | if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM)) |
| 1451 | return -EINVAL; |
| 1452 | |
| 1453 | f = fdget(ufd); |
| 1454 | map = __bpf_map_get(f); |
| 1455 | if (IS_ERR(map)) |
| 1456 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1457 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1458 | err = -EPERM; |
| 1459 | goto err_put; |
| 1460 | } |
| 1461 | |
| 1462 | key = __bpf_copy_key(ukey, map->key_size); |
| 1463 | if (IS_ERR(key)) { |
| 1464 | err = PTR_ERR(key); |
| 1465 | goto err_put; |
| 1466 | } |
| 1467 | |
| 1468 | value_size = map->value_size; |
| 1469 | |
| 1470 | err = -ENOMEM; |
| 1471 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
| 1472 | if (!value) |
| 1473 | goto free_key; |
| 1474 | |
| 1475 | if (map->map_type == BPF_MAP_TYPE_QUEUE || |
| 1476 | map->map_type == BPF_MAP_TYPE_STACK) { |
| 1477 | err = map->ops->map_pop_elem(map, value); |
| 1478 | } else { |
| 1479 | err = -ENOTSUPP; |
| 1480 | } |
| 1481 | |
| 1482 | if (err) |
| 1483 | goto free_value; |
| 1484 | |
| 1485 | if (copy_to_user(uvalue, value, value_size) != 0) |
| 1486 | goto free_value; |
| 1487 | |
| 1488 | err = 0; |
| 1489 | |
| 1490 | free_value: |
| 1491 | kfree(value); |
| 1492 | free_key: |
| 1493 | kfree(key); |
| 1494 | err_put: |
| 1495 | fdput(f); |
| 1496 | return err; |
| 1497 | } |
| 1498 | |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1499 | #define BPF_MAP_FREEZE_LAST_FIELD map_fd |
| 1500 | |
| 1501 | static int map_freeze(const union bpf_attr *attr) |
| 1502 | { |
| 1503 | int err = 0, ufd = attr->map_fd; |
| 1504 | struct bpf_map *map; |
| 1505 | struct fd f; |
| 1506 | |
| 1507 | if (CHECK_ATTR(BPF_MAP_FREEZE)) |
| 1508 | return -EINVAL; |
| 1509 | |
| 1510 | f = fdget(ufd); |
| 1511 | map = __bpf_map_get(f); |
| 1512 | if (IS_ERR(map)) |
| 1513 | return PTR_ERR(map); |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 1514 | |
Martin KaFai Lau | 849b4d9 | 2020-03-04 17:34:54 -0800 | [diff] [blame] | 1515 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
| 1516 | fdput(f); |
| 1517 | return -ENOTSUPP; |
| 1518 | } |
| 1519 | |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 1520 | mutex_lock(&map->freeze_mutex); |
| 1521 | |
| 1522 | if (map->writecnt) { |
| 1523 | err = -EBUSY; |
| 1524 | goto err_put; |
| 1525 | } |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1526 | if (READ_ONCE(map->frozen)) { |
| 1527 | err = -EBUSY; |
| 1528 | goto err_put; |
| 1529 | } |
| 1530 | if (!capable(CAP_SYS_ADMIN)) { |
| 1531 | err = -EPERM; |
| 1532 | goto err_put; |
| 1533 | } |
| 1534 | |
| 1535 | WRITE_ONCE(map->frozen, true); |
| 1536 | err_put: |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 1537 | mutex_unlock(&map->freeze_mutex); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1538 | fdput(f); |
| 1539 | return err; |
| 1540 | } |
| 1541 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 1542 | static const struct bpf_prog_ops * const bpf_prog_types[] = { |
Alexei Starovoitov | 91cc1a9 | 2019-11-14 10:57:15 -0800 | [diff] [blame] | 1543 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 1544 | [_id] = & _name ## _prog_ops, |
| 1545 | #define BPF_MAP_TYPE(_id, _ops) |
| 1546 | #include <linux/bpf_types.h> |
| 1547 | #undef BPF_PROG_TYPE |
| 1548 | #undef BPF_MAP_TYPE |
| 1549 | }; |
| 1550 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1551 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
| 1552 | { |
Daniel Borkmann | d0f1a45 | 2018-05-04 02:13:57 +0200 | [diff] [blame] | 1553 | const struct bpf_prog_ops *ops; |
| 1554 | |
| 1555 | if (type >= ARRAY_SIZE(bpf_prog_types)) |
| 1556 | return -EINVAL; |
| 1557 | type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types)); |
| 1558 | ops = bpf_prog_types[type]; |
| 1559 | if (!ops) |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 1560 | return -EINVAL; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1561 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1562 | if (!bpf_prog_is_dev_bound(prog->aux)) |
Daniel Borkmann | d0f1a45 | 2018-05-04 02:13:57 +0200 | [diff] [blame] | 1563 | prog->aux->ops = ops; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1564 | else |
| 1565 | prog->aux->ops = &bpf_offload_prog_ops; |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 1566 | prog->type = type; |
| 1567 | return 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
Daniel Borkmann | bae141f | 2019-12-06 22:49:34 +0100 | [diff] [blame] | 1570 | enum bpf_audit { |
| 1571 | BPF_AUDIT_LOAD, |
| 1572 | BPF_AUDIT_UNLOAD, |
| 1573 | BPF_AUDIT_MAX, |
| 1574 | }; |
| 1575 | |
| 1576 | static const char * const bpf_audit_str[BPF_AUDIT_MAX] = { |
| 1577 | [BPF_AUDIT_LOAD] = "LOAD", |
| 1578 | [BPF_AUDIT_UNLOAD] = "UNLOAD", |
| 1579 | }; |
| 1580 | |
| 1581 | static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op) |
| 1582 | { |
| 1583 | struct audit_context *ctx = NULL; |
| 1584 | struct audit_buffer *ab; |
| 1585 | |
| 1586 | if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX)) |
| 1587 | return; |
| 1588 | if (audit_enabled == AUDIT_OFF) |
| 1589 | return; |
| 1590 | if (op == BPF_AUDIT_LOAD) |
| 1591 | ctx = audit_context(); |
| 1592 | ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF); |
| 1593 | if (unlikely(!ab)) |
| 1594 | return; |
| 1595 | audit_log_format(ab, "prog-id=%u op=%s", |
| 1596 | prog->aux->id, bpf_audit_str[op]); |
| 1597 | audit_log_end(ab); |
| 1598 | } |
| 1599 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1600 | int __bpf_prog_charge(struct user_struct *user, u32 pages) |
| 1601 | { |
| 1602 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 1603 | unsigned long user_bufs; |
| 1604 | |
| 1605 | if (user) { |
| 1606 | user_bufs = atomic_long_add_return(pages, &user->locked_vm); |
| 1607 | if (user_bufs > memlock_limit) { |
| 1608 | atomic_long_sub(pages, &user->locked_vm); |
| 1609 | return -EPERM; |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | return 0; |
| 1614 | } |
| 1615 | |
| 1616 | void __bpf_prog_uncharge(struct user_struct *user, u32 pages) |
| 1617 | { |
| 1618 | if (user) |
| 1619 | atomic_long_sub(pages, &user->locked_vm); |
| 1620 | } |
| 1621 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1622 | static int bpf_prog_charge_memlock(struct bpf_prog *prog) |
| 1623 | { |
| 1624 | struct user_struct *user = get_current_user(); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1625 | int ret; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1626 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1627 | ret = __bpf_prog_charge(user, prog->pages); |
| 1628 | if (ret) { |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1629 | free_uid(user); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1630 | return ret; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1631 | } |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1632 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1633 | prog->aux->user = user; |
| 1634 | return 0; |
| 1635 | } |
| 1636 | |
| 1637 | static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) |
| 1638 | { |
| 1639 | struct user_struct *user = prog->aux->user; |
| 1640 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1641 | __bpf_prog_uncharge(user, prog->pages); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1642 | free_uid(user); |
| 1643 | } |
| 1644 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1645 | static int bpf_prog_alloc_id(struct bpf_prog *prog) |
| 1646 | { |
| 1647 | int id; |
| 1648 | |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 1649 | idr_preload(GFP_KERNEL); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1650 | spin_lock_bh(&prog_idr_lock); |
| 1651 | id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); |
| 1652 | if (id > 0) |
| 1653 | prog->aux->id = id; |
| 1654 | spin_unlock_bh(&prog_idr_lock); |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 1655 | idr_preload_end(); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1656 | |
| 1657 | /* id is in [1, INT_MAX) */ |
| 1658 | if (WARN_ON_ONCE(!id)) |
| 1659 | return -ENOSPC; |
| 1660 | |
| 1661 | return id > 0 ? 0 : id; |
| 1662 | } |
| 1663 | |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1664 | 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] | 1665 | { |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1666 | /* cBPF to eBPF migrations are currently not in the idr store. |
| 1667 | * Offloaded programs are removed from the store when their device |
| 1668 | * disappears - even if someone grabs an fd to them they are unusable, |
| 1669 | * simply waiting for refcnt to drop to be freed. |
| 1670 | */ |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1671 | if (!prog->aux->id) |
| 1672 | return; |
| 1673 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1674 | if (do_idr_lock) |
| 1675 | spin_lock_bh(&prog_idr_lock); |
| 1676 | else |
| 1677 | __acquire(&prog_idr_lock); |
| 1678 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1679 | idr_remove(&prog_idr, prog->aux->id); |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1680 | prog->aux->id = 0; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1681 | |
| 1682 | if (do_idr_lock) |
| 1683 | spin_unlock_bh(&prog_idr_lock); |
| 1684 | else |
| 1685 | __release(&prog_idr_lock); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1686 | } |
| 1687 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1688 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 1689 | { |
| 1690 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); |
| 1691 | |
Daniel Borkmann | 3b4d9eb | 2019-10-22 23:30:38 +0200 | [diff] [blame] | 1692 | kvfree(aux->func_info); |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 1693 | kfree(aux->func_info_aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1694 | bpf_prog_uncharge_memlock(aux->prog); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1695 | security_bpf_prog_free(aux); |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 1696 | bpf_prog_free(aux->prog); |
| 1697 | } |
| 1698 | |
Daniel Borkmann | cd7455f | 2019-10-22 15:57:23 +0200 | [diff] [blame] | 1699 | static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred) |
| 1700 | { |
| 1701 | bpf_prog_kallsyms_del_all(prog); |
| 1702 | btf_put(prog->aux->btf); |
Daniel Borkmann | cd7455f | 2019-10-22 15:57:23 +0200 | [diff] [blame] | 1703 | bpf_prog_free_linfo(prog); |
| 1704 | |
| 1705 | if (deferred) |
| 1706 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
| 1707 | else |
| 1708 | __bpf_prog_put_rcu(&prog->aux->rcu); |
| 1709 | } |
| 1710 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1711 | 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] | 1712 | { |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1713 | if (atomic64_dec_and_test(&prog->aux->refcnt)) { |
Song Liu | 6ee52e2 | 2019-01-17 08:15:15 -0800 | [diff] [blame] | 1714 | perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0); |
Daniel Borkmann | bae141f | 2019-12-06 22:49:34 +0100 | [diff] [blame] | 1715 | bpf_audit_prog(prog, BPF_AUDIT_UNLOAD); |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 1716 | /* bpf_prog_free_id() must be called first */ |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1717 | bpf_prog_free_id(prog, do_idr_lock); |
Daniel Borkmann | cd7455f | 2019-10-22 15:57:23 +0200 | [diff] [blame] | 1718 | __bpf_prog_put_noref(prog, true); |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1719 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1720 | } |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1721 | |
| 1722 | void bpf_prog_put(struct bpf_prog *prog) |
| 1723 | { |
| 1724 | __bpf_prog_put(prog, true); |
| 1725 | } |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 1726 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1727 | |
| 1728 | static int bpf_prog_release(struct inode *inode, struct file *filp) |
| 1729 | { |
| 1730 | struct bpf_prog *prog = filp->private_data; |
| 1731 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1732 | bpf_prog_put(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1733 | return 0; |
| 1734 | } |
| 1735 | |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1736 | static void bpf_prog_get_stats(const struct bpf_prog *prog, |
| 1737 | struct bpf_prog_stats *stats) |
| 1738 | { |
| 1739 | u64 nsecs = 0, cnt = 0; |
| 1740 | int cpu; |
| 1741 | |
| 1742 | for_each_possible_cpu(cpu) { |
| 1743 | const struct bpf_prog_stats *st; |
| 1744 | unsigned int start; |
| 1745 | u64 tnsecs, tcnt; |
| 1746 | |
| 1747 | st = per_cpu_ptr(prog->aux->stats, cpu); |
| 1748 | do { |
| 1749 | start = u64_stats_fetch_begin_irq(&st->syncp); |
| 1750 | tnsecs = st->nsecs; |
| 1751 | tcnt = st->cnt; |
| 1752 | } while (u64_stats_fetch_retry_irq(&st->syncp, start)); |
| 1753 | nsecs += tnsecs; |
| 1754 | cnt += tcnt; |
| 1755 | } |
| 1756 | stats->nsecs = nsecs; |
| 1757 | stats->cnt = cnt; |
| 1758 | } |
| 1759 | |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1760 | #ifdef CONFIG_PROC_FS |
| 1761 | static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) |
| 1762 | { |
| 1763 | const struct bpf_prog *prog = filp->private_data; |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1764 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1765 | struct bpf_prog_stats stats; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1766 | |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1767 | bpf_prog_get_stats(prog, &stats); |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1768 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1769 | seq_printf(m, |
| 1770 | "prog_type:\t%u\n" |
| 1771 | "prog_jited:\t%u\n" |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1772 | "prog_tag:\t%s\n" |
Daniel Borkmann | 4316b40 | 2018-06-02 23:06:34 +0200 | [diff] [blame] | 1773 | "memlock:\t%llu\n" |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1774 | "prog_id:\t%u\n" |
| 1775 | "run_time_ns:\t%llu\n" |
| 1776 | "run_cnt:\t%llu\n", |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1777 | prog->type, |
| 1778 | prog->jited, |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1779 | prog_tag, |
Daniel Borkmann | 4316b40 | 2018-06-02 23:06:34 +0200 | [diff] [blame] | 1780 | prog->pages * 1ULL << PAGE_SHIFT, |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1781 | prog->aux->id, |
| 1782 | stats.nsecs, |
| 1783 | stats.cnt); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1784 | } |
| 1785 | #endif |
| 1786 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1787 | const struct file_operations bpf_prog_fops = { |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1788 | #ifdef CONFIG_PROC_FS |
| 1789 | .show_fdinfo = bpf_prog_show_fdinfo, |
| 1790 | #endif |
| 1791 | .release = bpf_prog_release, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1792 | .read = bpf_dummy_read, |
| 1793 | .write = bpf_dummy_write, |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1794 | }; |
| 1795 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1796 | int bpf_prog_new_fd(struct bpf_prog *prog) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1797 | { |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1798 | int ret; |
| 1799 | |
| 1800 | ret = security_bpf_prog(prog); |
| 1801 | if (ret < 0) |
| 1802 | return ret; |
| 1803 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1804 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
| 1805 | O_RDWR | O_CLOEXEC); |
| 1806 | } |
| 1807 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1808 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1809 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1810 | if (!f.file) |
| 1811 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1812 | if (f.file->f_op != &bpf_prog_fops) { |
| 1813 | fdput(f); |
| 1814 | return ERR_PTR(-EINVAL); |
| 1815 | } |
| 1816 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1817 | return f.file->private_data; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1818 | } |
| 1819 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1820 | void bpf_prog_add(struct bpf_prog *prog, int i) |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1821 | { |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1822 | atomic64_add(i, &prog->aux->refcnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1823 | } |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1824 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
| 1825 | |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 1826 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
| 1827 | { |
| 1828 | /* Only to be used for undoing previous bpf_prog_add() in some |
| 1829 | * error path. We still know that another entity in our call |
| 1830 | * path holds a reference to the program, thus atomic_sub() can |
| 1831 | * be safely used in such cases! |
| 1832 | */ |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1833 | WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0); |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 1834 | } |
| 1835 | EXPORT_SYMBOL_GPL(bpf_prog_sub); |
| 1836 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1837 | void bpf_prog_inc(struct bpf_prog *prog) |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1838 | { |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1839 | atomic64_inc(&prog->aux->refcnt); |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1840 | } |
Daniel Borkmann | 97bc402 | 2016-11-19 01:45:00 +0100 | [diff] [blame] | 1841 | EXPORT_SYMBOL_GPL(bpf_prog_inc); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1842 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1843 | /* prog_idr_lock should have been held */ |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1844 | 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] | 1845 | { |
| 1846 | int refold; |
| 1847 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1848 | refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1849 | |
| 1850 | if (!refold) |
| 1851 | return ERR_PTR(-ENOENT); |
| 1852 | |
| 1853 | return prog; |
| 1854 | } |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1855 | EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1856 | |
Al Viro | 040ee69 | 2017-12-02 20:20:38 -0500 | [diff] [blame] | 1857 | bool bpf_prog_get_ok(struct bpf_prog *prog, |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1858 | enum bpf_prog_type *attach_type, bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1859 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1860 | /* not an attachment, just a refcount inc, always allow */ |
| 1861 | if (!attach_type) |
| 1862 | return true; |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1863 | |
| 1864 | if (prog->type != *attach_type) |
| 1865 | return false; |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1866 | if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1867 | return false; |
| 1868 | |
| 1869 | return true; |
| 1870 | } |
| 1871 | |
| 1872 | 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] | 1873 | bool attach_drv) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1874 | { |
| 1875 | struct fd f = fdget(ufd); |
| 1876 | struct bpf_prog *prog; |
| 1877 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1878 | prog = ____bpf_prog_get(f); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1879 | if (IS_ERR(prog)) |
| 1880 | return prog; |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1881 | if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1882 | prog = ERR_PTR(-EINVAL); |
| 1883 | goto out; |
| 1884 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1885 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1886 | bpf_prog_inc(prog); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1887 | out: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1888 | fdput(f); |
| 1889 | return prog; |
| 1890 | } |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1891 | |
| 1892 | struct bpf_prog *bpf_prog_get(u32 ufd) |
| 1893 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1894 | return __bpf_prog_get(ufd, NULL, false); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1895 | } |
| 1896 | |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1897 | 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] | 1898 | bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1899 | { |
Alexei Starovoitov | 4d220ed | 2018-04-28 19:56:37 -0700 | [diff] [blame] | 1900 | return __bpf_prog_get(ufd, &type, attach_drv); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1901 | } |
Jakub Kicinski | 6c8dfe2 | 2017-11-03 13:56:21 -0700 | [diff] [blame] | 1902 | EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1903 | |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1904 | /* Initially all BPF programs could be loaded w/o specifying |
| 1905 | * expected_attach_type. Later for some of them specifying expected_attach_type |
| 1906 | * at load time became required so that program could be validated properly. |
| 1907 | * Programs of types that are allowed to be loaded both w/ and w/o (for |
| 1908 | * backward compatibility) expected_attach_type, should have the default attach |
| 1909 | * type assigned to expected_attach_type for the latter case, so that it can be |
| 1910 | * validated later at attach time. |
| 1911 | * |
| 1912 | * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if |
| 1913 | * prog type requires it but has some attach types that have to be backward |
| 1914 | * compatible. |
| 1915 | */ |
| 1916 | static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) |
| 1917 | { |
| 1918 | switch (attr->prog_type) { |
| 1919 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 1920 | /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't |
| 1921 | * exist so checking for non-zero is the way to go here. |
| 1922 | */ |
| 1923 | if (!attr->expected_attach_type) |
| 1924 | attr->expected_attach_type = |
| 1925 | BPF_CGROUP_INET_SOCK_CREATE; |
| 1926 | break; |
| 1927 | } |
| 1928 | } |
| 1929 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1930 | static int |
Alexei Starovoitov | ccfe29e | 2019-10-15 20:24:58 -0700 | [diff] [blame] | 1931 | bpf_prog_load_check_attach(enum bpf_prog_type prog_type, |
| 1932 | enum bpf_attach_type expected_attach_type, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 1933 | u32 btf_id, u32 prog_fd) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1934 | { |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 1935 | if (btf_id) { |
Alexei Starovoitov | c108e3c | 2019-10-17 23:09:33 -0700 | [diff] [blame] | 1936 | if (btf_id > BTF_MAX_TYPE) |
| 1937 | return -EINVAL; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 1938 | |
| 1939 | switch (prog_type) { |
| 1940 | case BPF_PROG_TYPE_TRACING: |
| 1941 | case BPF_PROG_TYPE_STRUCT_OPS: |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 1942 | case BPF_PROG_TYPE_EXT: |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 1943 | break; |
| 1944 | default: |
Alexei Starovoitov | c108e3c | 2019-10-17 23:09:33 -0700 | [diff] [blame] | 1945 | return -EINVAL; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 1946 | } |
Alexei Starovoitov | c108e3c | 2019-10-17 23:09:33 -0700 | [diff] [blame] | 1947 | } |
| 1948 | |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 1949 | if (prog_fd && prog_type != BPF_PROG_TYPE_TRACING && |
| 1950 | prog_type != BPF_PROG_TYPE_EXT) |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 1951 | return -EINVAL; |
| 1952 | |
Alexei Starovoitov | c108e3c | 2019-10-17 23:09:33 -0700 | [diff] [blame] | 1953 | switch (prog_type) { |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1954 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 1955 | switch (expected_attach_type) { |
| 1956 | case BPF_CGROUP_INET_SOCK_CREATE: |
| 1957 | case BPF_CGROUP_INET4_POST_BIND: |
| 1958 | case BPF_CGROUP_INET6_POST_BIND: |
| 1959 | return 0; |
| 1960 | default: |
| 1961 | return -EINVAL; |
| 1962 | } |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1963 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 1964 | switch (expected_attach_type) { |
| 1965 | case BPF_CGROUP_INET4_BIND: |
| 1966 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 1967 | case BPF_CGROUP_INET4_CONNECT: |
| 1968 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 1969 | case BPF_CGROUP_UDP4_SENDMSG: |
| 1970 | case BPF_CGROUP_UDP6_SENDMSG: |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 1971 | case BPF_CGROUP_UDP4_RECVMSG: |
| 1972 | case BPF_CGROUP_UDP6_RECVMSG: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1973 | return 0; |
| 1974 | default: |
| 1975 | return -EINVAL; |
| 1976 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 1977 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 1978 | switch (expected_attach_type) { |
| 1979 | case BPF_CGROUP_INET_INGRESS: |
| 1980 | case BPF_CGROUP_INET_EGRESS: |
| 1981 | return 0; |
| 1982 | default: |
| 1983 | return -EINVAL; |
| 1984 | } |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1985 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 1986 | switch (expected_attach_type) { |
| 1987 | case BPF_CGROUP_SETSOCKOPT: |
| 1988 | case BPF_CGROUP_GETSOCKOPT: |
| 1989 | return 0; |
| 1990 | default: |
| 1991 | return -EINVAL; |
| 1992 | } |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 1993 | case BPF_PROG_TYPE_EXT: |
| 1994 | if (expected_attach_type) |
| 1995 | return -EINVAL; |
| 1996 | /* fallthrough */ |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1997 | default: |
| 1998 | return 0; |
| 1999 | } |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2000 | } |
| 2001 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2002 | /* last field in 'union bpf_attr' used by this command */ |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 2003 | #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2004 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 2005 | static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2006 | { |
| 2007 | enum bpf_prog_type type = attr->prog_type; |
| 2008 | struct bpf_prog *prog; |
| 2009 | int err; |
| 2010 | char license[128]; |
| 2011 | bool is_gpl; |
| 2012 | |
| 2013 | if (CHECK_ATTR(BPF_PROG_LOAD)) |
| 2014 | return -EINVAL; |
| 2015 | |
Jiong Wang | c240eff | 2019-05-24 23:25:16 +0100 | [diff] [blame] | 2016 | if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | |
| 2017 | BPF_F_ANY_ALIGNMENT | |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 2018 | BPF_F_TEST_STATE_FREQ | |
Jiong Wang | c240eff | 2019-05-24 23:25:16 +0100 | [diff] [blame] | 2019 | BPF_F_TEST_RND_HI32)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2020 | return -EINVAL; |
| 2021 | |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 2022 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && |
| 2023 | (attr->prog_flags & BPF_F_ANY_ALIGNMENT) && |
| 2024 | !capable(CAP_SYS_ADMIN)) |
| 2025 | return -EPERM; |
| 2026 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2027 | /* copy eBPF program license from user space */ |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 2028 | if (strncpy_from_user(license, u64_to_user_ptr(attr->license), |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2029 | sizeof(license) - 1) < 0) |
| 2030 | return -EFAULT; |
| 2031 | license[sizeof(license) - 1] = 0; |
| 2032 | |
| 2033 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
| 2034 | is_gpl = license_is_gpl_compatible(license); |
| 2035 | |
Alexei Starovoitov | c04c0d2 | 2019-04-01 21:27:45 -0700 | [diff] [blame] | 2036 | if (attr->insn_cnt == 0 || |
| 2037 | attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) |
Daniel Borkmann | ef0915c | 2016-12-07 01:15:44 +0100 | [diff] [blame] | 2038 | return -E2BIG; |
Chenbo Feng | 80b7d81 | 2017-05-31 18:16:00 -0700 | [diff] [blame] | 2039 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && |
| 2040 | type != BPF_PROG_TYPE_CGROUP_SKB && |
| 2041 | !capable(CAP_SYS_ADMIN)) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2042 | return -EPERM; |
| 2043 | |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 2044 | bpf_prog_load_fixup_attach_type(attr); |
Alexei Starovoitov | ccfe29e | 2019-10-15 20:24:58 -0700 | [diff] [blame] | 2045 | if (bpf_prog_load_check_attach(type, attr->expected_attach_type, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 2046 | attr->attach_btf_id, |
| 2047 | attr->attach_prog_fd)) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2048 | return -EINVAL; |
| 2049 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2050 | /* plain bpf_prog allocation */ |
| 2051 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); |
| 2052 | if (!prog) |
| 2053 | return -ENOMEM; |
| 2054 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2055 | prog->expected_attach_type = attr->expected_attach_type; |
Alexei Starovoitov | ccfe29e | 2019-10-15 20:24:58 -0700 | [diff] [blame] | 2056 | prog->aux->attach_btf_id = attr->attach_btf_id; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 2057 | if (attr->attach_prog_fd) { |
| 2058 | struct bpf_prog *tgt_prog; |
| 2059 | |
| 2060 | tgt_prog = bpf_prog_get(attr->attach_prog_fd); |
| 2061 | if (IS_ERR(tgt_prog)) { |
| 2062 | err = PTR_ERR(tgt_prog); |
| 2063 | goto free_prog_nouncharge; |
| 2064 | } |
| 2065 | prog->aux->linked_prog = tgt_prog; |
| 2066 | } |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2067 | |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 2068 | prog->aux->offload_requested = !!attr->prog_ifindex; |
| 2069 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 2070 | err = security_bpf_prog_alloc(prog->aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 2071 | if (err) |
| 2072 | goto free_prog_nouncharge; |
| 2073 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 2074 | err = bpf_prog_charge_memlock(prog); |
| 2075 | if (err) |
| 2076 | goto free_prog_sec; |
| 2077 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2078 | prog->len = attr->insn_cnt; |
| 2079 | |
| 2080 | err = -EFAULT; |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 2081 | if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 2082 | bpf_prog_insn_size(prog)) != 0) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2083 | goto free_prog; |
| 2084 | |
| 2085 | prog->orig_prog = NULL; |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 2086 | prog->jited = 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2087 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 2088 | atomic64_set(&prog->aux->refcnt, 1); |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 2089 | prog->gpl_compatible = is_gpl ? 1 : 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2090 | |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 2091 | if (bpf_prog_is_dev_bound(prog->aux)) { |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 2092 | err = bpf_prog_offload_init(prog, attr); |
| 2093 | if (err) |
| 2094 | goto free_prog; |
| 2095 | } |
| 2096 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2097 | /* find program type: socket_filter vs tracing_filter */ |
| 2098 | err = find_prog_type(type, prog); |
| 2099 | if (err < 0) |
| 2100 | goto free_prog; |
| 2101 | |
Jason A. Donenfeld | 9285ec4 | 2019-06-21 22:32:48 +0200 | [diff] [blame] | 2102 | prog->aux->load_time = ktime_get_boottime_ns(); |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame^] | 2103 | err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name, |
| 2104 | sizeof(attr->prog_name)); |
| 2105 | if (err < 0) |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 2106 | goto free_prog; |
| 2107 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2108 | /* run eBPF verifier */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 2109 | err = bpf_check(&prog, attr, uattr); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2110 | if (err < 0) |
| 2111 | goto free_used_maps; |
| 2112 | |
Daniel Borkmann | 9facc33 | 2018-06-15 02:30:48 +0200 | [diff] [blame] | 2113 | prog = bpf_prog_select_runtime(prog, &err); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2114 | if (err < 0) |
| 2115 | goto free_used_maps; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2116 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 2117 | err = bpf_prog_alloc_id(prog); |
| 2118 | if (err) |
| 2119 | goto free_used_maps; |
| 2120 | |
Daniel Borkmann | c751798 | 2019-08-23 22:14:23 +0200 | [diff] [blame] | 2121 | /* Upon success of bpf_prog_alloc_id(), the BPF prog is |
| 2122 | * effectively publicly exposed. However, retrieving via |
| 2123 | * bpf_prog_get_fd_by_id() will take another reference, |
| 2124 | * therefore it cannot be gone underneath us. |
| 2125 | * |
| 2126 | * Only for the time /after/ successful bpf_prog_new_fd() |
| 2127 | * and before returning to userspace, we might just hold |
| 2128 | * one reference and any parallel close on that fd could |
| 2129 | * rip everything out. Hence, below notifications must |
| 2130 | * happen before bpf_prog_new_fd(). |
| 2131 | * |
| 2132 | * Also, any failure handling from this point onwards must |
| 2133 | * be using bpf_prog_put() given the program is exposed. |
| 2134 | */ |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 2135 | bpf_prog_kallsyms_add(prog); |
Song Liu | 6ee52e2 | 2019-01-17 08:15:15 -0800 | [diff] [blame] | 2136 | perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); |
Daniel Borkmann | bae141f | 2019-12-06 22:49:34 +0100 | [diff] [blame] | 2137 | bpf_audit_prog(prog, BPF_AUDIT_LOAD); |
Daniel Borkmann | c751798 | 2019-08-23 22:14:23 +0200 | [diff] [blame] | 2138 | |
| 2139 | err = bpf_prog_new_fd(prog); |
| 2140 | if (err < 0) |
| 2141 | bpf_prog_put(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2142 | return err; |
| 2143 | |
| 2144 | free_used_maps: |
Daniel Borkmann | cd7455f | 2019-10-22 15:57:23 +0200 | [diff] [blame] | 2145 | /* In case we have subprogs, we need to wait for a grace |
| 2146 | * period before we can tear down JIT memory since symbols |
| 2147 | * are already exposed under kallsyms. |
| 2148 | */ |
| 2149 | __bpf_prog_put_noref(prog, prog->aux->func_cnt); |
| 2150 | return err; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2151 | free_prog: |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 2152 | bpf_prog_uncharge_memlock(prog); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 2153 | free_prog_sec: |
| 2154 | security_bpf_prog_free(prog->aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 2155 | free_prog_nouncharge: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2156 | bpf_prog_free(prog); |
| 2157 | return err; |
| 2158 | } |
| 2159 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2160 | #define BPF_OBJ_LAST_FIELD file_flags |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 2161 | |
| 2162 | static int bpf_obj_pin(const union bpf_attr *attr) |
| 2163 | { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2164 | if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 2165 | return -EINVAL; |
| 2166 | |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 2167 | 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] | 2168 | } |
| 2169 | |
| 2170 | static int bpf_obj_get(const union bpf_attr *attr) |
| 2171 | { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2172 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || |
| 2173 | attr->file_flags & ~BPF_OBJ_FLAG_MASK) |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 2174 | return -EINVAL; |
| 2175 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2176 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), |
| 2177 | attr->file_flags); |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 2178 | } |
| 2179 | |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 2180 | static int bpf_tracing_prog_release(struct inode *inode, struct file *filp) |
| 2181 | { |
| 2182 | struct bpf_prog *prog = filp->private_data; |
| 2183 | |
| 2184 | WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog)); |
| 2185 | bpf_prog_put(prog); |
| 2186 | return 0; |
| 2187 | } |
| 2188 | |
| 2189 | static const struct file_operations bpf_tracing_prog_fops = { |
| 2190 | .release = bpf_tracing_prog_release, |
| 2191 | .read = bpf_dummy_read, |
| 2192 | .write = bpf_dummy_write, |
| 2193 | }; |
| 2194 | |
| 2195 | static int bpf_tracing_prog_attach(struct bpf_prog *prog) |
| 2196 | { |
| 2197 | int tr_fd, err; |
| 2198 | |
| 2199 | if (prog->expected_attach_type != BPF_TRACE_FENTRY && |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 2200 | prog->expected_attach_type != BPF_TRACE_FEXIT && |
| 2201 | prog->type != BPF_PROG_TYPE_EXT) { |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 2202 | err = -EINVAL; |
| 2203 | goto out_put_prog; |
| 2204 | } |
| 2205 | |
| 2206 | err = bpf_trampoline_link_prog(prog); |
| 2207 | if (err) |
| 2208 | goto out_put_prog; |
| 2209 | |
| 2210 | tr_fd = anon_inode_getfd("bpf-tracing-prog", &bpf_tracing_prog_fops, |
| 2211 | prog, O_CLOEXEC); |
| 2212 | if (tr_fd < 0) { |
| 2213 | WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog)); |
| 2214 | err = tr_fd; |
| 2215 | goto out_put_prog; |
| 2216 | } |
| 2217 | return tr_fd; |
| 2218 | |
| 2219 | out_put_prog: |
| 2220 | bpf_prog_put(prog); |
| 2221 | return err; |
| 2222 | } |
| 2223 | |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2224 | struct bpf_raw_tracepoint { |
| 2225 | struct bpf_raw_event_map *btp; |
| 2226 | struct bpf_prog *prog; |
| 2227 | }; |
| 2228 | |
| 2229 | static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp) |
| 2230 | { |
| 2231 | struct bpf_raw_tracepoint *raw_tp = filp->private_data; |
| 2232 | |
| 2233 | if (raw_tp->prog) { |
| 2234 | bpf_probe_unregister(raw_tp->btp, raw_tp->prog); |
| 2235 | bpf_prog_put(raw_tp->prog); |
| 2236 | } |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 2237 | bpf_put_raw_tracepoint(raw_tp->btp); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2238 | kfree(raw_tp); |
| 2239 | return 0; |
| 2240 | } |
| 2241 | |
| 2242 | static const struct file_operations bpf_raw_tp_fops = { |
| 2243 | .release = bpf_raw_tracepoint_release, |
| 2244 | .read = bpf_dummy_read, |
| 2245 | .write = bpf_dummy_write, |
| 2246 | }; |
| 2247 | |
| 2248 | #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd |
| 2249 | |
| 2250 | static int bpf_raw_tracepoint_open(const union bpf_attr *attr) |
| 2251 | { |
| 2252 | struct bpf_raw_tracepoint *raw_tp; |
| 2253 | struct bpf_raw_event_map *btp; |
| 2254 | struct bpf_prog *prog; |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 2255 | const char *tp_name; |
| 2256 | char buf[128]; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2257 | int tp_fd, err; |
| 2258 | |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 2259 | if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN)) |
| 2260 | return -EINVAL; |
| 2261 | |
| 2262 | prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); |
| 2263 | if (IS_ERR(prog)) |
| 2264 | return PTR_ERR(prog); |
| 2265 | |
| 2266 | if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT && |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 2267 | prog->type != BPF_PROG_TYPE_TRACING && |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 2268 | prog->type != BPF_PROG_TYPE_EXT && |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 2269 | prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) { |
| 2270 | err = -EINVAL; |
| 2271 | goto out_put_prog; |
| 2272 | } |
| 2273 | |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 2274 | if (prog->type == BPF_PROG_TYPE_TRACING || |
| 2275 | prog->type == BPF_PROG_TYPE_EXT) { |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 2276 | if (attr->raw_tracepoint.name) { |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 2277 | /* The attach point for this category of programs |
| 2278 | * should be specified via btf_id during program load. |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 2279 | */ |
| 2280 | err = -EINVAL; |
| 2281 | goto out_put_prog; |
| 2282 | } |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 2283 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) |
| 2284 | tp_name = prog->aux->attach_func_name; |
| 2285 | else |
| 2286 | return bpf_tracing_prog_attach(prog); |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 2287 | } else { |
| 2288 | if (strncpy_from_user(buf, |
| 2289 | u64_to_user_ptr(attr->raw_tracepoint.name), |
| 2290 | sizeof(buf) - 1) < 0) { |
| 2291 | err = -EFAULT; |
| 2292 | goto out_put_prog; |
| 2293 | } |
| 2294 | buf[sizeof(buf) - 1] = 0; |
| 2295 | tp_name = buf; |
| 2296 | } |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2297 | |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 2298 | btp = bpf_get_raw_tracepoint(tp_name); |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 2299 | if (!btp) { |
| 2300 | err = -ENOENT; |
| 2301 | goto out_put_prog; |
| 2302 | } |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2303 | |
| 2304 | raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER); |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 2305 | if (!raw_tp) { |
| 2306 | err = -ENOMEM; |
| 2307 | goto out_put_btp; |
| 2308 | } |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2309 | raw_tp->btp = btp; |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 2310 | raw_tp->prog = prog; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2311 | |
| 2312 | err = bpf_probe_register(raw_tp->btp, prog); |
| 2313 | if (err) |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 2314 | goto out_free_tp; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2315 | |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2316 | tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp, |
| 2317 | O_CLOEXEC); |
| 2318 | if (tp_fd < 0) { |
| 2319 | bpf_probe_unregister(raw_tp->btp, prog); |
| 2320 | err = tp_fd; |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 2321 | goto out_free_tp; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2322 | } |
| 2323 | return tp_fd; |
| 2324 | |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2325 | out_free_tp: |
| 2326 | kfree(raw_tp); |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 2327 | out_put_btp: |
| 2328 | bpf_put_raw_tracepoint(btp); |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 2329 | out_put_prog: |
| 2330 | bpf_prog_put(prog); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2331 | return err; |
| 2332 | } |
| 2333 | |
Anders Roxell | 3349158 | 2018-04-03 14:09:47 +0200 | [diff] [blame] | 2334 | static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, |
| 2335 | enum bpf_attach_type attach_type) |
| 2336 | { |
| 2337 | switch (prog->type) { |
| 2338 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 2339 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2340 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
Anders Roxell | 3349158 | 2018-04-03 14:09:47 +0200 | [diff] [blame] | 2341 | return attach_type == prog->expected_attach_type ? 0 : -EINVAL; |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 2342 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 2343 | return prog->enforce_expected_attach_type && |
| 2344 | prog->expected_attach_type != attach_type ? |
| 2345 | -EINVAL : 0; |
Anders Roxell | 3349158 | 2018-04-03 14:09:47 +0200 | [diff] [blame] | 2346 | default: |
| 2347 | return 0; |
| 2348 | } |
| 2349 | } |
| 2350 | |
Andrey Ignatov | 7dd68b3 | 2019-12-18 23:44:35 -0800 | [diff] [blame] | 2351 | #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2352 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2353 | #define BPF_F_ATTACH_MASK \ |
Andrey Ignatov | 7dd68b3 | 2019-12-18 23:44:35 -0800 | [diff] [blame] | 2354 | (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE) |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2355 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2356 | static int bpf_prog_attach(const union bpf_attr *attr) |
| 2357 | { |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 2358 | enum bpf_prog_type ptype; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2359 | struct bpf_prog *prog; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 2360 | int ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2361 | |
| 2362 | if (!capable(CAP_NET_ADMIN)) |
| 2363 | return -EPERM; |
| 2364 | |
| 2365 | if (CHECK_ATTR(BPF_PROG_ATTACH)) |
| 2366 | return -EINVAL; |
| 2367 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2368 | if (attr->attach_flags & ~BPF_F_ATTACH_MASK) |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 2369 | return -EINVAL; |
| 2370 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2371 | switch (attr->attach_type) { |
| 2372 | case BPF_CGROUP_INET_INGRESS: |
| 2373 | case BPF_CGROUP_INET_EGRESS: |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 2374 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2375 | break; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 2376 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 2377 | case BPF_CGROUP_INET4_POST_BIND: |
| 2378 | case BPF_CGROUP_INET6_POST_BIND: |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 2379 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
| 2380 | break; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2381 | case BPF_CGROUP_INET4_BIND: |
| 2382 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 2383 | case BPF_CGROUP_INET4_CONNECT: |
| 2384 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 2385 | case BPF_CGROUP_UDP4_SENDMSG: |
| 2386 | case BPF_CGROUP_UDP6_SENDMSG: |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 2387 | case BPF_CGROUP_UDP4_RECVMSG: |
| 2388 | case BPF_CGROUP_UDP6_RECVMSG: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2389 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
| 2390 | break; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 2391 | case BPF_CGROUP_SOCK_OPS: |
| 2392 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
| 2393 | break; |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 2394 | case BPF_CGROUP_DEVICE: |
| 2395 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; |
| 2396 | break; |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2397 | case BPF_SK_MSG_VERDICT: |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2398 | ptype = BPF_PROG_TYPE_SK_MSG; |
| 2399 | break; |
John Fastabend | 464bc0f | 2017-08-28 07:10:04 -0700 | [diff] [blame] | 2400 | case BPF_SK_SKB_STREAM_PARSER: |
| 2401 | case BPF_SK_SKB_STREAM_VERDICT: |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2402 | ptype = BPF_PROG_TYPE_SK_SKB; |
| 2403 | break; |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 2404 | case BPF_LIRC_MODE2: |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2405 | ptype = BPF_PROG_TYPE_LIRC_MODE2; |
| 2406 | break; |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2407 | case BPF_FLOW_DISSECTOR: |
| 2408 | ptype = BPF_PROG_TYPE_FLOW_DISSECTOR; |
| 2409 | break; |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 2410 | case BPF_CGROUP_SYSCTL: |
| 2411 | ptype = BPF_PROG_TYPE_CGROUP_SYSCTL; |
| 2412 | break; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2413 | case BPF_CGROUP_GETSOCKOPT: |
| 2414 | case BPF_CGROUP_SETSOCKOPT: |
| 2415 | ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT; |
| 2416 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2417 | default: |
| 2418 | return -EINVAL; |
| 2419 | } |
| 2420 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 2421 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
| 2422 | if (IS_ERR(prog)) |
| 2423 | return PTR_ERR(prog); |
| 2424 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2425 | if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { |
| 2426 | bpf_prog_put(prog); |
| 2427 | return -EINVAL; |
| 2428 | } |
| 2429 | |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2430 | switch (ptype) { |
| 2431 | case BPF_PROG_TYPE_SK_SKB: |
| 2432 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 2433 | ret = sock_map_get_from_fd(attr, prog); |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2434 | break; |
| 2435 | case BPF_PROG_TYPE_LIRC_MODE2: |
| 2436 | ret = lirc_prog_attach(attr, prog); |
| 2437 | break; |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2438 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
| 2439 | ret = skb_flow_dissector_bpf_prog_attach(attr, prog); |
| 2440 | break; |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2441 | default: |
| 2442 | ret = cgroup_bpf_prog_attach(attr, ptype, prog); |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 2443 | } |
| 2444 | |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 2445 | if (ret) |
| 2446 | bpf_prog_put(prog); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 2447 | return ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | #define BPF_PROG_DETACH_LAST_FIELD attach_type |
| 2451 | |
| 2452 | static int bpf_prog_detach(const union bpf_attr *attr) |
| 2453 | { |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2454 | enum bpf_prog_type ptype; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2455 | |
| 2456 | if (!capable(CAP_NET_ADMIN)) |
| 2457 | return -EPERM; |
| 2458 | |
| 2459 | if (CHECK_ATTR(BPF_PROG_DETACH)) |
| 2460 | return -EINVAL; |
| 2461 | |
| 2462 | switch (attr->attach_type) { |
| 2463 | case BPF_CGROUP_INET_INGRESS: |
| 2464 | case BPF_CGROUP_INET_EGRESS: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2465 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
| 2466 | break; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 2467 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 2468 | case BPF_CGROUP_INET4_POST_BIND: |
| 2469 | case BPF_CGROUP_INET6_POST_BIND: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2470 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
| 2471 | break; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2472 | case BPF_CGROUP_INET4_BIND: |
| 2473 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 2474 | case BPF_CGROUP_INET4_CONNECT: |
| 2475 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 2476 | case BPF_CGROUP_UDP4_SENDMSG: |
| 2477 | case BPF_CGROUP_UDP6_SENDMSG: |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 2478 | case BPF_CGROUP_UDP4_RECVMSG: |
| 2479 | case BPF_CGROUP_UDP6_RECVMSG: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2480 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
| 2481 | break; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 2482 | case BPF_CGROUP_SOCK_OPS: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2483 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2484 | break; |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 2485 | case BPF_CGROUP_DEVICE: |
| 2486 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; |
| 2487 | break; |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2488 | case BPF_SK_MSG_VERDICT: |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 2489 | return sock_map_get_from_fd(attr, NULL); |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 2490 | case BPF_SK_SKB_STREAM_PARSER: |
| 2491 | case BPF_SK_SKB_STREAM_VERDICT: |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 2492 | return sock_map_get_from_fd(attr, NULL); |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 2493 | case BPF_LIRC_MODE2: |
| 2494 | return lirc_prog_detach(attr); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2495 | case BPF_FLOW_DISSECTOR: |
| 2496 | return skb_flow_dissector_bpf_prog_detach(attr); |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 2497 | case BPF_CGROUP_SYSCTL: |
| 2498 | ptype = BPF_PROG_TYPE_CGROUP_SYSCTL; |
| 2499 | break; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2500 | case BPF_CGROUP_GETSOCKOPT: |
| 2501 | case BPF_CGROUP_SETSOCKOPT: |
| 2502 | ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT; |
| 2503 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2504 | default: |
| 2505 | return -EINVAL; |
| 2506 | } |
| 2507 | |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2508 | return cgroup_bpf_prog_detach(attr, ptype); |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2509 | } |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 2510 | |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2511 | #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt |
| 2512 | |
| 2513 | static int bpf_prog_query(const union bpf_attr *attr, |
| 2514 | union bpf_attr __user *uattr) |
| 2515 | { |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2516 | if (!capable(CAP_NET_ADMIN)) |
| 2517 | return -EPERM; |
| 2518 | if (CHECK_ATTR(BPF_PROG_QUERY)) |
| 2519 | return -EINVAL; |
| 2520 | if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) |
| 2521 | return -EINVAL; |
| 2522 | |
| 2523 | switch (attr->query.attach_type) { |
| 2524 | case BPF_CGROUP_INET_INGRESS: |
| 2525 | case BPF_CGROUP_INET_EGRESS: |
| 2526 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2527 | case BPF_CGROUP_INET4_BIND: |
| 2528 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 2529 | case BPF_CGROUP_INET4_POST_BIND: |
| 2530 | case BPF_CGROUP_INET6_POST_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 2531 | case BPF_CGROUP_INET4_CONNECT: |
| 2532 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 2533 | case BPF_CGROUP_UDP4_SENDMSG: |
| 2534 | case BPF_CGROUP_UDP6_SENDMSG: |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 2535 | case BPF_CGROUP_UDP4_RECVMSG: |
| 2536 | case BPF_CGROUP_UDP6_RECVMSG: |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2537 | case BPF_CGROUP_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 2538 | case BPF_CGROUP_DEVICE: |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 2539 | case BPF_CGROUP_SYSCTL: |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2540 | case BPF_CGROUP_GETSOCKOPT: |
| 2541 | case BPF_CGROUP_SETSOCKOPT: |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2542 | break; |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 2543 | case BPF_LIRC_MODE2: |
| 2544 | return lirc_prog_query(attr, uattr); |
Stanislav Fomichev | 118c8e9 | 2019-04-25 14:37:23 -0700 | [diff] [blame] | 2545 | case BPF_FLOW_DISSECTOR: |
| 2546 | return skb_flow_dissector_prog_query(attr, uattr); |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2547 | default: |
| 2548 | return -EINVAL; |
| 2549 | } |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2550 | |
| 2551 | return cgroup_bpf_prog_query(attr, uattr); |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2552 | } |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2553 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 2554 | #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2555 | |
| 2556 | static int bpf_prog_test_run(const union bpf_attr *attr, |
| 2557 | union bpf_attr __user *uattr) |
| 2558 | { |
| 2559 | struct bpf_prog *prog; |
| 2560 | int ret = -ENOTSUPP; |
| 2561 | |
Alexei Starovoitov | 61f3c96 | 2018-01-17 16:52:02 -0800 | [diff] [blame] | 2562 | if (!capable(CAP_SYS_ADMIN)) |
| 2563 | return -EPERM; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2564 | if (CHECK_ATTR(BPF_PROG_TEST_RUN)) |
| 2565 | return -EINVAL; |
| 2566 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 2567 | if ((attr->test.ctx_size_in && !attr->test.ctx_in) || |
| 2568 | (!attr->test.ctx_size_in && attr->test.ctx_in)) |
| 2569 | return -EINVAL; |
| 2570 | |
| 2571 | if ((attr->test.ctx_size_out && !attr->test.ctx_out) || |
| 2572 | (!attr->test.ctx_size_out && attr->test.ctx_out)) |
| 2573 | return -EINVAL; |
| 2574 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2575 | prog = bpf_prog_get(attr->test.prog_fd); |
| 2576 | if (IS_ERR(prog)) |
| 2577 | return PTR_ERR(prog); |
| 2578 | |
| 2579 | if (prog->aux->ops->test_run) |
| 2580 | ret = prog->aux->ops->test_run(prog, attr, uattr); |
| 2581 | |
| 2582 | bpf_prog_put(prog); |
| 2583 | return ret; |
| 2584 | } |
| 2585 | |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 2586 | #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id |
| 2587 | |
| 2588 | static int bpf_obj_get_next_id(const union bpf_attr *attr, |
| 2589 | union bpf_attr __user *uattr, |
| 2590 | struct idr *idr, |
| 2591 | spinlock_t *lock) |
| 2592 | { |
| 2593 | u32 next_id = attr->start_id; |
| 2594 | int err = 0; |
| 2595 | |
| 2596 | if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) |
| 2597 | return -EINVAL; |
| 2598 | |
| 2599 | if (!capable(CAP_SYS_ADMIN)) |
| 2600 | return -EPERM; |
| 2601 | |
| 2602 | next_id++; |
| 2603 | spin_lock_bh(lock); |
| 2604 | if (!idr_get_next(idr, &next_id)) |
| 2605 | err = -ENOENT; |
| 2606 | spin_unlock_bh(lock); |
| 2607 | |
| 2608 | if (!err) |
| 2609 | err = put_user(next_id, &uattr->next_id); |
| 2610 | |
| 2611 | return err; |
| 2612 | } |
| 2613 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 2614 | #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id |
| 2615 | |
Björn Töpel | 7e6897f | 2019-12-13 18:51:09 +0100 | [diff] [blame] | 2616 | struct bpf_prog *bpf_prog_by_id(u32 id) |
| 2617 | { |
| 2618 | struct bpf_prog *prog; |
| 2619 | |
| 2620 | if (!id) |
| 2621 | return ERR_PTR(-ENOENT); |
| 2622 | |
| 2623 | spin_lock_bh(&prog_idr_lock); |
| 2624 | prog = idr_find(&prog_idr, id); |
| 2625 | if (prog) |
| 2626 | prog = bpf_prog_inc_not_zero(prog); |
| 2627 | else |
| 2628 | prog = ERR_PTR(-ENOENT); |
| 2629 | spin_unlock_bh(&prog_idr_lock); |
| 2630 | return prog; |
| 2631 | } |
| 2632 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 2633 | static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) |
| 2634 | { |
| 2635 | struct bpf_prog *prog; |
| 2636 | u32 id = attr->prog_id; |
| 2637 | int fd; |
| 2638 | |
| 2639 | if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) |
| 2640 | return -EINVAL; |
| 2641 | |
| 2642 | if (!capable(CAP_SYS_ADMIN)) |
| 2643 | return -EPERM; |
| 2644 | |
Björn Töpel | 7e6897f | 2019-12-13 18:51:09 +0100 | [diff] [blame] | 2645 | prog = bpf_prog_by_id(id); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 2646 | if (IS_ERR(prog)) |
| 2647 | return PTR_ERR(prog); |
| 2648 | |
| 2649 | fd = bpf_prog_new_fd(prog); |
| 2650 | if (fd < 0) |
| 2651 | bpf_prog_put(prog); |
| 2652 | |
| 2653 | return fd; |
| 2654 | } |
| 2655 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2656 | #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2657 | |
| 2658 | static int bpf_map_get_fd_by_id(const union bpf_attr *attr) |
| 2659 | { |
| 2660 | struct bpf_map *map; |
| 2661 | u32 id = attr->map_id; |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2662 | int f_flags; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2663 | int fd; |
| 2664 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2665 | if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || |
| 2666 | attr->open_flags & ~BPF_OBJ_FLAG_MASK) |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2667 | return -EINVAL; |
| 2668 | |
| 2669 | if (!capable(CAP_SYS_ADMIN)) |
| 2670 | return -EPERM; |
| 2671 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2672 | f_flags = bpf_get_file_flag(attr->open_flags); |
| 2673 | if (f_flags < 0) |
| 2674 | return f_flags; |
| 2675 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2676 | spin_lock_bh(&map_idr_lock); |
| 2677 | map = idr_find(&map_idr, id); |
| 2678 | if (map) |
Stanislav Fomichev | b0e4701 | 2019-08-14 10:37:48 -0700 | [diff] [blame] | 2679 | map = __bpf_map_inc_not_zero(map, true); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2680 | else |
| 2681 | map = ERR_PTR(-ENOENT); |
| 2682 | spin_unlock_bh(&map_idr_lock); |
| 2683 | |
| 2684 | if (IS_ERR(map)) |
| 2685 | return PTR_ERR(map); |
| 2686 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2687 | fd = bpf_map_new_fd(map, f_flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2688 | if (fd < 0) |
Peng Sun | 781e628 | 2019-02-26 22:15:37 +0800 | [diff] [blame] | 2689 | bpf_map_put_with_uref(map); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2690 | |
| 2691 | return fd; |
| 2692 | } |
| 2693 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2694 | static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2695 | unsigned long addr, u32 *off, |
| 2696 | u32 *type) |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2697 | { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2698 | const struct bpf_map *map; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2699 | int i; |
| 2700 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2701 | for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) { |
| 2702 | map = prog->aux->used_maps[i]; |
| 2703 | if (map == (void *)addr) { |
| 2704 | *type = BPF_PSEUDO_MAP_FD; |
| 2705 | return map; |
| 2706 | } |
| 2707 | if (!map->ops->map_direct_value_meta) |
| 2708 | continue; |
| 2709 | if (!map->ops->map_direct_value_meta(map, addr, off)) { |
| 2710 | *type = BPF_PSEUDO_MAP_VALUE; |
| 2711 | return map; |
| 2712 | } |
| 2713 | } |
| 2714 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2715 | return NULL; |
| 2716 | } |
| 2717 | |
| 2718 | static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog) |
| 2719 | { |
| 2720 | const struct bpf_map *map; |
| 2721 | struct bpf_insn *insns; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2722 | u32 off, type; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2723 | u64 imm; |
| 2724 | int i; |
| 2725 | |
| 2726 | insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), |
| 2727 | GFP_USER); |
| 2728 | if (!insns) |
| 2729 | return insns; |
| 2730 | |
| 2731 | for (i = 0; i < prog->len; i++) { |
| 2732 | if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) { |
| 2733 | insns[i].code = BPF_JMP | BPF_CALL; |
| 2734 | insns[i].imm = BPF_FUNC_tail_call; |
| 2735 | /* fall-through */ |
| 2736 | } |
| 2737 | if (insns[i].code == (BPF_JMP | BPF_CALL) || |
| 2738 | insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) { |
| 2739 | if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) |
| 2740 | insns[i].code = BPF_JMP | BPF_CALL; |
| 2741 | if (!bpf_dump_raw_ok()) |
| 2742 | insns[i].imm = 0; |
| 2743 | continue; |
| 2744 | } |
| 2745 | |
| 2746 | if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW)) |
| 2747 | continue; |
| 2748 | |
| 2749 | imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2750 | map = bpf_map_from_imm(prog, imm, &off, &type); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2751 | if (map) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2752 | insns[i].src_reg = type; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2753 | insns[i].imm = map->id; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2754 | insns[i + 1].imm = off; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2755 | continue; |
| 2756 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | return insns; |
| 2760 | } |
| 2761 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2762 | static int set_info_rec_size(struct bpf_prog_info *info) |
| 2763 | { |
| 2764 | /* |
| 2765 | * Ensure info.*_rec_size is the same as kernel expected size |
| 2766 | * |
| 2767 | * or |
| 2768 | * |
| 2769 | * Only allow zero *_rec_size if both _rec_size and _cnt are |
| 2770 | * zero. In this case, the kernel will set the expected |
| 2771 | * _rec_size back to the info. |
| 2772 | */ |
| 2773 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2774 | if ((info->nr_func_info || info->func_info_rec_size) && |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2775 | info->func_info_rec_size != sizeof(struct bpf_func_info)) |
| 2776 | return -EINVAL; |
| 2777 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2778 | if ((info->nr_line_info || info->line_info_rec_size) && |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2779 | info->line_info_rec_size != sizeof(struct bpf_line_info)) |
| 2780 | return -EINVAL; |
| 2781 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2782 | if ((info->nr_jited_line_info || info->jited_line_info_rec_size) && |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2783 | info->jited_line_info_rec_size != sizeof(__u64)) |
| 2784 | return -EINVAL; |
| 2785 | |
| 2786 | info->func_info_rec_size = sizeof(struct bpf_func_info); |
| 2787 | info->line_info_rec_size = sizeof(struct bpf_line_info); |
| 2788 | info->jited_line_info_rec_size = sizeof(__u64); |
| 2789 | |
| 2790 | return 0; |
| 2791 | } |
| 2792 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2793 | static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, |
| 2794 | const union bpf_attr *attr, |
| 2795 | union bpf_attr __user *uattr) |
| 2796 | { |
| 2797 | struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 2798 | struct bpf_prog_info info = {}; |
| 2799 | u32 info_len = attr->info.info_len; |
Alexei Starovoitov | 5f8f8b9 | 2019-02-25 14:28:40 -0800 | [diff] [blame] | 2800 | struct bpf_prog_stats stats; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2801 | char __user *uinsns; |
| 2802 | u32 ulen; |
| 2803 | int err; |
| 2804 | |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 2805 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2806 | if (err) |
| 2807 | return err; |
| 2808 | info_len = min_t(u32, sizeof(info), info_len); |
| 2809 | |
| 2810 | if (copy_from_user(&info, uinfo, info_len)) |
Daniel Borkmann | 89b0968 | 2017-07-27 21:02:46 +0200 | [diff] [blame] | 2811 | return -EFAULT; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2812 | |
| 2813 | info.type = prog->type; |
| 2814 | info.id = prog->aux->id; |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 2815 | info.load_time = prog->aux->load_time; |
| 2816 | info.created_by_uid = from_kuid_munged(current_user_ns(), |
| 2817 | prog->aux->user->uid); |
Jiri Olsa | b85fab0 | 2018-04-25 19:41:06 +0200 | [diff] [blame] | 2818 | info.gpl_compatible = prog->gpl_compatible; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2819 | |
| 2820 | memcpy(info.tag, prog->tag, sizeof(prog->tag)); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 2821 | memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); |
| 2822 | |
| 2823 | ulen = info.nr_map_ids; |
| 2824 | info.nr_map_ids = prog->aux->used_map_cnt; |
| 2825 | ulen = min_t(u32, info.nr_map_ids, ulen); |
| 2826 | if (ulen) { |
Martin KaFai Lau | 721e08d | 2017-09-29 10:52:17 -0700 | [diff] [blame] | 2827 | 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] | 2828 | u32 i; |
| 2829 | |
| 2830 | for (i = 0; i < ulen; i++) |
| 2831 | if (put_user(prog->aux->used_maps[i]->id, |
| 2832 | &user_map_ids[i])) |
| 2833 | return -EFAULT; |
| 2834 | } |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2835 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2836 | err = set_info_rec_size(&info); |
| 2837 | if (err) |
| 2838 | return err; |
Martin KaFai Lau | 7337224 | 2018-12-05 17:35:43 -0800 | [diff] [blame] | 2839 | |
Alexei Starovoitov | 5f8f8b9 | 2019-02-25 14:28:40 -0800 | [diff] [blame] | 2840 | bpf_prog_get_stats(prog, &stats); |
| 2841 | info.run_time_ns = stats.nsecs; |
| 2842 | info.run_cnt = stats.cnt; |
| 2843 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2844 | if (!capable(CAP_SYS_ADMIN)) { |
| 2845 | info.jited_prog_len = 0; |
| 2846 | info.xlated_prog_len = 0; |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2847 | info.nr_jited_ksyms = 0; |
Daniel Borkmann | 28c2fae | 2018-11-02 11:35:46 +0100 | [diff] [blame] | 2848 | info.nr_jited_func_lens = 0; |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2849 | info.nr_func_info = 0; |
| 2850 | info.nr_line_info = 0; |
| 2851 | info.nr_jited_line_info = 0; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2852 | goto done; |
| 2853 | } |
| 2854 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2855 | ulen = info.xlated_prog_len; |
Daniel Borkmann | 9975a54 | 2017-07-28 17:05:25 +0200 | [diff] [blame] | 2856 | info.xlated_prog_len = bpf_prog_insn_size(prog); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2857 | if (info.xlated_prog_len && ulen) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2858 | struct bpf_insn *insns_sanitized; |
| 2859 | bool fault; |
| 2860 | |
| 2861 | if (prog->blinded && !bpf_dump_raw_ok()) { |
| 2862 | info.xlated_prog_insns = 0; |
| 2863 | goto done; |
| 2864 | } |
| 2865 | insns_sanitized = bpf_insn_prepare_dump(prog); |
| 2866 | if (!insns_sanitized) |
| 2867 | return -ENOMEM; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2868 | uinsns = u64_to_user_ptr(info.xlated_prog_insns); |
| 2869 | ulen = min_t(u32, info.xlated_prog_len, ulen); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2870 | fault = copy_to_user(uinsns, insns_sanitized, ulen); |
| 2871 | kfree(insns_sanitized); |
| 2872 | if (fault) |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2873 | return -EFAULT; |
| 2874 | } |
| 2875 | |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 2876 | if (bpf_prog_is_dev_bound(prog->aux)) { |
| 2877 | err = bpf_prog_offload_info_fill(&info, prog); |
| 2878 | if (err) |
| 2879 | return err; |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 2880 | goto done; |
| 2881 | } |
| 2882 | |
| 2883 | /* NOTE: the following code is supposed to be skipped for offload. |
| 2884 | * bpf_prog_offload_info_fill() is the place to fill similar fields |
| 2885 | * for offload. |
| 2886 | */ |
| 2887 | ulen = info.jited_prog_len; |
Sandipan Das | 4d56a76 | 2018-05-24 12:26:51 +0530 | [diff] [blame] | 2888 | if (prog->aux->func_cnt) { |
| 2889 | u32 i; |
| 2890 | |
| 2891 | info.jited_prog_len = 0; |
| 2892 | for (i = 0; i < prog->aux->func_cnt; i++) |
| 2893 | info.jited_prog_len += prog->aux->func[i]->jited_len; |
| 2894 | } else { |
| 2895 | info.jited_prog_len = prog->jited_len; |
| 2896 | } |
| 2897 | |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 2898 | if (info.jited_prog_len && ulen) { |
| 2899 | if (bpf_dump_raw_ok()) { |
| 2900 | uinsns = u64_to_user_ptr(info.jited_prog_insns); |
| 2901 | ulen = min_t(u32, info.jited_prog_len, ulen); |
Sandipan Das | 4d56a76 | 2018-05-24 12:26:51 +0530 | [diff] [blame] | 2902 | |
| 2903 | /* for multi-function programs, copy the JITed |
| 2904 | * instructions for all the functions |
| 2905 | */ |
| 2906 | if (prog->aux->func_cnt) { |
| 2907 | u32 len, free, i; |
| 2908 | u8 *img; |
| 2909 | |
| 2910 | free = ulen; |
| 2911 | for (i = 0; i < prog->aux->func_cnt; i++) { |
| 2912 | len = prog->aux->func[i]->jited_len; |
| 2913 | len = min_t(u32, len, free); |
| 2914 | img = (u8 *) prog->aux->func[i]->bpf_func; |
| 2915 | if (copy_to_user(uinsns, img, len)) |
| 2916 | return -EFAULT; |
| 2917 | uinsns += len; |
| 2918 | free -= len; |
| 2919 | if (!free) |
| 2920 | break; |
| 2921 | } |
| 2922 | } else { |
| 2923 | if (copy_to_user(uinsns, prog->bpf_func, ulen)) |
| 2924 | return -EFAULT; |
| 2925 | } |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 2926 | } else { |
| 2927 | info.jited_prog_insns = 0; |
| 2928 | } |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 2929 | } |
| 2930 | |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2931 | ulen = info.nr_jited_ksyms; |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2932 | info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; |
Song Liu | 7a5725d | 2018-12-10 11:17:50 -0800 | [diff] [blame] | 2933 | if (ulen) { |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2934 | if (bpf_dump_raw_ok()) { |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2935 | unsigned long ksym_addr; |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2936 | u64 __user *user_ksyms; |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2937 | u32 i; |
| 2938 | |
| 2939 | /* copy the address of the kernel symbol |
| 2940 | * corresponding to each function |
| 2941 | */ |
| 2942 | ulen = min_t(u32, info.nr_jited_ksyms, ulen); |
| 2943 | user_ksyms = u64_to_user_ptr(info.jited_ksyms); |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2944 | if (prog->aux->func_cnt) { |
| 2945 | for (i = 0; i < ulen; i++) { |
| 2946 | ksym_addr = (unsigned long) |
| 2947 | prog->aux->func[i]->bpf_func; |
| 2948 | if (put_user((u64) ksym_addr, |
| 2949 | &user_ksyms[i])) |
| 2950 | return -EFAULT; |
| 2951 | } |
| 2952 | } else { |
| 2953 | ksym_addr = (unsigned long) prog->bpf_func; |
| 2954 | if (put_user((u64) ksym_addr, &user_ksyms[0])) |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2955 | return -EFAULT; |
| 2956 | } |
| 2957 | } else { |
| 2958 | info.jited_ksyms = 0; |
| 2959 | } |
| 2960 | } |
| 2961 | |
Sandipan Das | 815581c | 2018-05-24 12:26:52 +0530 | [diff] [blame] | 2962 | ulen = info.nr_jited_func_lens; |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2963 | info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; |
Song Liu | 7a5725d | 2018-12-10 11:17:50 -0800 | [diff] [blame] | 2964 | if (ulen) { |
Sandipan Das | 815581c | 2018-05-24 12:26:52 +0530 | [diff] [blame] | 2965 | if (bpf_dump_raw_ok()) { |
| 2966 | u32 __user *user_lens; |
| 2967 | u32 func_len, i; |
| 2968 | |
| 2969 | /* copy the JITed image lengths for each function */ |
| 2970 | ulen = min_t(u32, info.nr_jited_func_lens, ulen); |
| 2971 | user_lens = u64_to_user_ptr(info.jited_func_lens); |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2972 | if (prog->aux->func_cnt) { |
| 2973 | for (i = 0; i < ulen; i++) { |
| 2974 | func_len = |
| 2975 | prog->aux->func[i]->jited_len; |
| 2976 | if (put_user(func_len, &user_lens[i])) |
| 2977 | return -EFAULT; |
| 2978 | } |
| 2979 | } else { |
| 2980 | func_len = prog->jited_len; |
| 2981 | if (put_user(func_len, &user_lens[0])) |
Sandipan Das | 815581c | 2018-05-24 12:26:52 +0530 | [diff] [blame] | 2982 | return -EFAULT; |
| 2983 | } |
| 2984 | } else { |
| 2985 | info.jited_func_lens = 0; |
| 2986 | } |
| 2987 | } |
| 2988 | |
Martin KaFai Lau | 7337224 | 2018-12-05 17:35:43 -0800 | [diff] [blame] | 2989 | if (prog->aux->btf) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 2990 | info.btf_id = btf_id(prog->aux->btf); |
| 2991 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2992 | ulen = info.nr_func_info; |
| 2993 | info.nr_func_info = prog->aux->func_info_cnt; |
| 2994 | if (info.nr_func_info && ulen) { |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 2995 | char __user *user_finfo; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 2996 | |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 2997 | user_finfo = u64_to_user_ptr(info.func_info); |
| 2998 | ulen = min_t(u32, info.nr_func_info, ulen); |
| 2999 | if (copy_to_user(user_finfo, prog->aux->func_info, |
| 3000 | info.func_info_rec_size * ulen)) |
| 3001 | return -EFAULT; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 3002 | } |
| 3003 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3004 | ulen = info.nr_line_info; |
| 3005 | info.nr_line_info = prog->aux->nr_linfo; |
| 3006 | if (info.nr_line_info && ulen) { |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 3007 | __u8 __user *user_linfo; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3008 | |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 3009 | user_linfo = u64_to_user_ptr(info.line_info); |
| 3010 | ulen = min_t(u32, info.nr_line_info, ulen); |
| 3011 | if (copy_to_user(user_linfo, prog->aux->linfo, |
| 3012 | info.line_info_rec_size * ulen)) |
| 3013 | return -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3014 | } |
| 3015 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3016 | ulen = info.nr_jited_line_info; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3017 | if (prog->aux->jited_linfo) |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3018 | info.nr_jited_line_info = prog->aux->nr_linfo; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3019 | else |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3020 | info.nr_jited_line_info = 0; |
| 3021 | if (info.nr_jited_line_info && ulen) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3022 | if (bpf_dump_raw_ok()) { |
| 3023 | __u64 __user *user_linfo; |
| 3024 | u32 i; |
| 3025 | |
| 3026 | user_linfo = u64_to_user_ptr(info.jited_line_info); |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3027 | ulen = min_t(u32, info.nr_jited_line_info, ulen); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3028 | for (i = 0; i < ulen; i++) { |
| 3029 | if (put_user((__u64)(long)prog->aux->jited_linfo[i], |
| 3030 | &user_linfo[i])) |
| 3031 | return -EFAULT; |
| 3032 | } |
| 3033 | } else { |
| 3034 | info.jited_line_info = 0; |
| 3035 | } |
| 3036 | } |
| 3037 | |
Song Liu | c872bdb | 2018-12-12 09:37:46 -0800 | [diff] [blame] | 3038 | ulen = info.nr_prog_tags; |
| 3039 | info.nr_prog_tags = prog->aux->func_cnt ? : 1; |
| 3040 | if (ulen) { |
| 3041 | __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; |
| 3042 | u32 i; |
| 3043 | |
| 3044 | user_prog_tags = u64_to_user_ptr(info.prog_tags); |
| 3045 | ulen = min_t(u32, info.nr_prog_tags, ulen); |
| 3046 | if (prog->aux->func_cnt) { |
| 3047 | for (i = 0; i < ulen; i++) { |
| 3048 | if (copy_to_user(user_prog_tags[i], |
| 3049 | prog->aux->func[i]->tag, |
| 3050 | BPF_TAG_SIZE)) |
| 3051 | return -EFAULT; |
| 3052 | } |
| 3053 | } else { |
| 3054 | if (copy_to_user(user_prog_tags[0], |
| 3055 | prog->tag, BPF_TAG_SIZE)) |
| 3056 | return -EFAULT; |
| 3057 | } |
| 3058 | } |
| 3059 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3060 | done: |
| 3061 | if (copy_to_user(uinfo, &info, info_len) || |
| 3062 | put_user(info_len, &uattr->info.info_len)) |
| 3063 | return -EFAULT; |
| 3064 | |
| 3065 | return 0; |
| 3066 | } |
| 3067 | |
| 3068 | static int bpf_map_get_info_by_fd(struct bpf_map *map, |
| 3069 | const union bpf_attr *attr, |
| 3070 | union bpf_attr __user *uattr) |
| 3071 | { |
| 3072 | struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 3073 | struct bpf_map_info info = {}; |
| 3074 | u32 info_len = attr->info.info_len; |
| 3075 | int err; |
| 3076 | |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 3077 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3078 | if (err) |
| 3079 | return err; |
| 3080 | info_len = min_t(u32, sizeof(info), info_len); |
| 3081 | |
| 3082 | info.type = map->map_type; |
| 3083 | info.id = map->id; |
| 3084 | info.key_size = map->key_size; |
| 3085 | info.value_size = map->value_size; |
| 3086 | info.max_entries = map->max_entries; |
| 3087 | info.map_flags = map->map_flags; |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 3088 | memcpy(info.name, map->name, sizeof(map->name)); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3089 | |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 3090 | if (map->btf) { |
| 3091 | info.btf_id = btf_id(map->btf); |
Martin KaFai Lau | 9b2cf32 | 2018-05-22 14:57:21 -0700 | [diff] [blame] | 3092 | info.btf_key_type_id = map->btf_key_type_id; |
| 3093 | info.btf_value_type_id = map->btf_value_type_id; |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 3094 | } |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 3095 | info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 3096 | |
Jakub Kicinski | 52775b3 | 2018-01-17 19:13:28 -0800 | [diff] [blame] | 3097 | if (bpf_map_is_dev_bound(map)) { |
| 3098 | err = bpf_map_offload_info_fill(&info, map); |
| 3099 | if (err) |
| 3100 | return err; |
| 3101 | } |
| 3102 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3103 | if (copy_to_user(uinfo, &info, info_len) || |
| 3104 | put_user(info_len, &uattr->info.info_len)) |
| 3105 | return -EFAULT; |
| 3106 | |
| 3107 | return 0; |
| 3108 | } |
| 3109 | |
Martin KaFai Lau | 62dab84 | 2018-05-04 14:49:52 -0700 | [diff] [blame] | 3110 | static int bpf_btf_get_info_by_fd(struct btf *btf, |
| 3111 | const union bpf_attr *attr, |
| 3112 | union bpf_attr __user *uattr) |
| 3113 | { |
| 3114 | struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 3115 | u32 info_len = attr->info.info_len; |
| 3116 | int err; |
| 3117 | |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 3118 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len); |
Martin KaFai Lau | 62dab84 | 2018-05-04 14:49:52 -0700 | [diff] [blame] | 3119 | if (err) |
| 3120 | return err; |
| 3121 | |
| 3122 | return btf_get_info_by_fd(btf, attr, uattr); |
| 3123 | } |
| 3124 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3125 | #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info |
| 3126 | |
| 3127 | static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, |
| 3128 | union bpf_attr __user *uattr) |
| 3129 | { |
| 3130 | int ufd = attr->info.bpf_fd; |
| 3131 | struct fd f; |
| 3132 | int err; |
| 3133 | |
| 3134 | if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) |
| 3135 | return -EINVAL; |
| 3136 | |
| 3137 | f = fdget(ufd); |
| 3138 | if (!f.file) |
| 3139 | return -EBADFD; |
| 3140 | |
| 3141 | if (f.file->f_op == &bpf_prog_fops) |
| 3142 | err = bpf_prog_get_info_by_fd(f.file->private_data, attr, |
| 3143 | uattr); |
| 3144 | else if (f.file->f_op == &bpf_map_fops) |
| 3145 | err = bpf_map_get_info_by_fd(f.file->private_data, attr, |
| 3146 | uattr); |
Martin KaFai Lau | 60197cf | 2018-04-18 15:56:02 -0700 | [diff] [blame] | 3147 | else if (f.file->f_op == &btf_fops) |
Martin KaFai Lau | 62dab84 | 2018-05-04 14:49:52 -0700 | [diff] [blame] | 3148 | 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] | 3149 | else |
| 3150 | err = -EINVAL; |
| 3151 | |
| 3152 | fdput(f); |
| 3153 | return err; |
| 3154 | } |
| 3155 | |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 3156 | #define BPF_BTF_LOAD_LAST_FIELD btf_log_level |
| 3157 | |
| 3158 | static int bpf_btf_load(const union bpf_attr *attr) |
| 3159 | { |
| 3160 | if (CHECK_ATTR(BPF_BTF_LOAD)) |
| 3161 | return -EINVAL; |
| 3162 | |
| 3163 | if (!capable(CAP_SYS_ADMIN)) |
| 3164 | return -EPERM; |
| 3165 | |
| 3166 | return btf_new_fd(attr); |
| 3167 | } |
| 3168 | |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 3169 | #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id |
| 3170 | |
| 3171 | static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) |
| 3172 | { |
| 3173 | if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) |
| 3174 | return -EINVAL; |
| 3175 | |
| 3176 | if (!capable(CAP_SYS_ADMIN)) |
| 3177 | return -EPERM; |
| 3178 | |
| 3179 | return btf_get_fd_by_id(attr->btf_id); |
| 3180 | } |
| 3181 | |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 3182 | static int bpf_task_fd_query_copy(const union bpf_attr *attr, |
| 3183 | union bpf_attr __user *uattr, |
| 3184 | u32 prog_id, u32 fd_type, |
| 3185 | const char *buf, u64 probe_offset, |
| 3186 | u64 probe_addr) |
| 3187 | { |
| 3188 | char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); |
| 3189 | u32 len = buf ? strlen(buf) : 0, input_len; |
| 3190 | int err = 0; |
| 3191 | |
| 3192 | if (put_user(len, &uattr->task_fd_query.buf_len)) |
| 3193 | return -EFAULT; |
| 3194 | input_len = attr->task_fd_query.buf_len; |
| 3195 | if (input_len && ubuf) { |
| 3196 | if (!len) { |
| 3197 | /* nothing to copy, just make ubuf NULL terminated */ |
| 3198 | char zero = '\0'; |
| 3199 | |
| 3200 | if (put_user(zero, ubuf)) |
| 3201 | return -EFAULT; |
| 3202 | } else if (input_len >= len + 1) { |
| 3203 | /* ubuf can hold the string with NULL terminator */ |
| 3204 | if (copy_to_user(ubuf, buf, len + 1)) |
| 3205 | return -EFAULT; |
| 3206 | } else { |
| 3207 | /* ubuf cannot hold the string with NULL terminator, |
| 3208 | * do a partial copy with NULL terminator. |
| 3209 | */ |
| 3210 | char zero = '\0'; |
| 3211 | |
| 3212 | err = -ENOSPC; |
| 3213 | if (copy_to_user(ubuf, buf, input_len - 1)) |
| 3214 | return -EFAULT; |
| 3215 | if (put_user(zero, ubuf + input_len - 1)) |
| 3216 | return -EFAULT; |
| 3217 | } |
| 3218 | } |
| 3219 | |
| 3220 | if (put_user(prog_id, &uattr->task_fd_query.prog_id) || |
| 3221 | put_user(fd_type, &uattr->task_fd_query.fd_type) || |
| 3222 | put_user(probe_offset, &uattr->task_fd_query.probe_offset) || |
| 3223 | put_user(probe_addr, &uattr->task_fd_query.probe_addr)) |
| 3224 | return -EFAULT; |
| 3225 | |
| 3226 | return err; |
| 3227 | } |
| 3228 | |
| 3229 | #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr |
| 3230 | |
| 3231 | static int bpf_task_fd_query(const union bpf_attr *attr, |
| 3232 | union bpf_attr __user *uattr) |
| 3233 | { |
| 3234 | pid_t pid = attr->task_fd_query.pid; |
| 3235 | u32 fd = attr->task_fd_query.fd; |
| 3236 | const struct perf_event *event; |
| 3237 | struct files_struct *files; |
| 3238 | struct task_struct *task; |
| 3239 | struct file *file; |
| 3240 | int err; |
| 3241 | |
| 3242 | if (CHECK_ATTR(BPF_TASK_FD_QUERY)) |
| 3243 | return -EINVAL; |
| 3244 | |
| 3245 | if (!capable(CAP_SYS_ADMIN)) |
| 3246 | return -EPERM; |
| 3247 | |
| 3248 | if (attr->task_fd_query.flags != 0) |
| 3249 | return -EINVAL; |
| 3250 | |
| 3251 | task = get_pid_task(find_vpid(pid), PIDTYPE_PID); |
| 3252 | if (!task) |
| 3253 | return -ENOENT; |
| 3254 | |
| 3255 | files = get_files_struct(task); |
| 3256 | put_task_struct(task); |
| 3257 | if (!files) |
| 3258 | return -ENOENT; |
| 3259 | |
| 3260 | err = 0; |
| 3261 | spin_lock(&files->file_lock); |
| 3262 | file = fcheck_files(files, fd); |
| 3263 | if (!file) |
| 3264 | err = -EBADF; |
| 3265 | else |
| 3266 | get_file(file); |
| 3267 | spin_unlock(&files->file_lock); |
| 3268 | put_files_struct(files); |
| 3269 | |
| 3270 | if (err) |
| 3271 | goto out; |
| 3272 | |
| 3273 | if (file->f_op == &bpf_raw_tp_fops) { |
| 3274 | struct bpf_raw_tracepoint *raw_tp = file->private_data; |
| 3275 | struct bpf_raw_event_map *btp = raw_tp->btp; |
| 3276 | |
| 3277 | err = bpf_task_fd_query_copy(attr, uattr, |
| 3278 | raw_tp->prog->aux->id, |
| 3279 | BPF_FD_TYPE_RAW_TRACEPOINT, |
| 3280 | btp->tp->name, 0, 0); |
| 3281 | goto put_file; |
| 3282 | } |
| 3283 | |
| 3284 | event = perf_get_event(file); |
| 3285 | if (!IS_ERR(event)) { |
| 3286 | u64 probe_offset, probe_addr; |
| 3287 | u32 prog_id, fd_type; |
| 3288 | const char *buf; |
| 3289 | |
| 3290 | err = bpf_get_perf_event_info(event, &prog_id, &fd_type, |
| 3291 | &buf, &probe_offset, |
| 3292 | &probe_addr); |
| 3293 | if (!err) |
| 3294 | err = bpf_task_fd_query_copy(attr, uattr, prog_id, |
| 3295 | fd_type, buf, |
| 3296 | probe_offset, |
| 3297 | probe_addr); |
| 3298 | goto put_file; |
| 3299 | } |
| 3300 | |
| 3301 | err = -ENOTSUPP; |
| 3302 | put_file: |
| 3303 | fput(file); |
| 3304 | out: |
| 3305 | return err; |
| 3306 | } |
| 3307 | |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 3308 | #define BPF_MAP_BATCH_LAST_FIELD batch.flags |
| 3309 | |
| 3310 | #define BPF_DO_BATCH(fn) \ |
| 3311 | do { \ |
| 3312 | if (!fn) { \ |
| 3313 | err = -ENOTSUPP; \ |
| 3314 | goto err_put; \ |
| 3315 | } \ |
| 3316 | err = fn(map, attr, uattr); \ |
| 3317 | } while (0) |
| 3318 | |
| 3319 | static int bpf_map_do_batch(const union bpf_attr *attr, |
| 3320 | union bpf_attr __user *uattr, |
| 3321 | int cmd) |
| 3322 | { |
| 3323 | struct bpf_map *map; |
| 3324 | int err, ufd; |
| 3325 | struct fd f; |
| 3326 | |
| 3327 | if (CHECK_ATTR(BPF_MAP_BATCH)) |
| 3328 | return -EINVAL; |
| 3329 | |
| 3330 | ufd = attr->batch.map_fd; |
| 3331 | f = fdget(ufd); |
| 3332 | map = __bpf_map_get(f); |
| 3333 | if (IS_ERR(map)) |
| 3334 | return PTR_ERR(map); |
| 3335 | |
Yonghong Song | 0579963 | 2020-01-15 10:43:04 -0800 | [diff] [blame] | 3336 | if ((cmd == BPF_MAP_LOOKUP_BATCH || |
| 3337 | cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) && |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 3338 | !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
| 3339 | err = -EPERM; |
| 3340 | goto err_put; |
| 3341 | } |
| 3342 | |
| 3343 | if (cmd != BPF_MAP_LOOKUP_BATCH && |
| 3344 | !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
| 3345 | err = -EPERM; |
| 3346 | goto err_put; |
| 3347 | } |
| 3348 | |
| 3349 | if (cmd == BPF_MAP_LOOKUP_BATCH) |
| 3350 | BPF_DO_BATCH(map->ops->map_lookup_batch); |
Yonghong Song | 0579963 | 2020-01-15 10:43:04 -0800 | [diff] [blame] | 3351 | else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) |
| 3352 | BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch); |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 3353 | else if (cmd == BPF_MAP_UPDATE_BATCH) |
| 3354 | BPF_DO_BATCH(map->ops->map_update_batch); |
| 3355 | else |
| 3356 | BPF_DO_BATCH(map->ops->map_delete_batch); |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 3357 | |
| 3358 | err_put: |
| 3359 | fdput(f); |
| 3360 | return err; |
| 3361 | } |
| 3362 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 3363 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
| 3364 | { |
| 3365 | union bpf_attr attr = {}; |
| 3366 | int err; |
| 3367 | |
Chenbo Feng | 0fa4fe8 | 2018-03-19 17:57:27 -0700 | [diff] [blame] | 3368 | if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN)) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 3369 | return -EPERM; |
| 3370 | |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 3371 | err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3372 | if (err) |
| 3373 | return err; |
| 3374 | size = min_t(u32, size, sizeof(attr)); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 3375 | |
| 3376 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ |
| 3377 | if (copy_from_user(&attr, uattr, size) != 0) |
| 3378 | return -EFAULT; |
| 3379 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 3380 | err = security_bpf(cmd, &attr, size); |
| 3381 | if (err < 0) |
| 3382 | return err; |
| 3383 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 3384 | switch (cmd) { |
| 3385 | case BPF_MAP_CREATE: |
| 3386 | err = map_create(&attr); |
| 3387 | break; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 3388 | case BPF_MAP_LOOKUP_ELEM: |
| 3389 | err = map_lookup_elem(&attr); |
| 3390 | break; |
| 3391 | case BPF_MAP_UPDATE_ELEM: |
| 3392 | err = map_update_elem(&attr); |
| 3393 | break; |
| 3394 | case BPF_MAP_DELETE_ELEM: |
| 3395 | err = map_delete_elem(&attr); |
| 3396 | break; |
| 3397 | case BPF_MAP_GET_NEXT_KEY: |
| 3398 | err = map_get_next_key(&attr); |
| 3399 | break; |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 3400 | case BPF_MAP_FREEZE: |
| 3401 | err = map_freeze(&attr); |
| 3402 | break; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 3403 | case BPF_PROG_LOAD: |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 3404 | err = bpf_prog_load(&attr, uattr); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 3405 | break; |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 3406 | case BPF_OBJ_PIN: |
| 3407 | err = bpf_obj_pin(&attr); |
| 3408 | break; |
| 3409 | case BPF_OBJ_GET: |
| 3410 | err = bpf_obj_get(&attr); |
| 3411 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 3412 | case BPF_PROG_ATTACH: |
| 3413 | err = bpf_prog_attach(&attr); |
| 3414 | break; |
| 3415 | case BPF_PROG_DETACH: |
| 3416 | err = bpf_prog_detach(&attr); |
| 3417 | break; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 3418 | case BPF_PROG_QUERY: |
| 3419 | err = bpf_prog_query(&attr, uattr); |
| 3420 | break; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 3421 | case BPF_PROG_TEST_RUN: |
| 3422 | err = bpf_prog_test_run(&attr, uattr); |
| 3423 | break; |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 3424 | case BPF_PROG_GET_NEXT_ID: |
| 3425 | err = bpf_obj_get_next_id(&attr, uattr, |
| 3426 | &prog_idr, &prog_idr_lock); |
| 3427 | break; |
| 3428 | case BPF_MAP_GET_NEXT_ID: |
| 3429 | err = bpf_obj_get_next_id(&attr, uattr, |
| 3430 | &map_idr, &map_idr_lock); |
| 3431 | break; |
Quentin Monnet | 1b9ed84 | 2019-08-20 10:31:50 +0100 | [diff] [blame] | 3432 | case BPF_BTF_GET_NEXT_ID: |
| 3433 | err = bpf_obj_get_next_id(&attr, uattr, |
| 3434 | &btf_idr, &btf_idr_lock); |
| 3435 | break; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 3436 | case BPF_PROG_GET_FD_BY_ID: |
| 3437 | err = bpf_prog_get_fd_by_id(&attr); |
| 3438 | break; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 3439 | case BPF_MAP_GET_FD_BY_ID: |
| 3440 | err = bpf_map_get_fd_by_id(&attr); |
| 3441 | break; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3442 | case BPF_OBJ_GET_INFO_BY_FD: |
| 3443 | err = bpf_obj_get_info_by_fd(&attr, uattr); |
| 3444 | break; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 3445 | case BPF_RAW_TRACEPOINT_OPEN: |
| 3446 | err = bpf_raw_tracepoint_open(&attr); |
| 3447 | break; |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 3448 | case BPF_BTF_LOAD: |
| 3449 | err = bpf_btf_load(&attr); |
| 3450 | break; |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 3451 | case BPF_BTF_GET_FD_BY_ID: |
| 3452 | err = bpf_btf_get_fd_by_id(&attr); |
| 3453 | break; |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 3454 | case BPF_TASK_FD_QUERY: |
| 3455 | err = bpf_task_fd_query(&attr, uattr); |
| 3456 | break; |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 3457 | case BPF_MAP_LOOKUP_AND_DELETE_ELEM: |
| 3458 | err = map_lookup_and_delete_elem(&attr); |
| 3459 | break; |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 3460 | case BPF_MAP_LOOKUP_BATCH: |
| 3461 | err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH); |
| 3462 | break; |
Yonghong Song | 0579963 | 2020-01-15 10:43:04 -0800 | [diff] [blame] | 3463 | case BPF_MAP_LOOKUP_AND_DELETE_BATCH: |
| 3464 | err = bpf_map_do_batch(&attr, uattr, |
| 3465 | BPF_MAP_LOOKUP_AND_DELETE_BATCH); |
| 3466 | break; |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 3467 | case BPF_MAP_UPDATE_BATCH: |
| 3468 | err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH); |
| 3469 | break; |
| 3470 | case BPF_MAP_DELETE_BATCH: |
| 3471 | err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH); |
| 3472 | break; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 3473 | default: |
| 3474 | err = -EINVAL; |
| 3475 | break; |
| 3476 | } |
| 3477 | |
| 3478 | return err; |
| 3479 | } |