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