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