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> |
Jakub Kicinski | aef2fed | 2021-12-15 18:55:37 -0800 | [diff] [blame] | 5 | #include <linux/bpf-cgroup.h> |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 6 | #include <linux/bpf_trace.h> |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 7 | #include <linux/bpf_lirc.h> |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 8 | #include <linux/bpf_verifier.h> |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 9 | #include <linux/btf.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 10 | #include <linux/syscalls.h> |
| 11 | #include <linux/slab.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 12 | #include <linux/sched/signal.h> |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 13 | #include <linux/vmalloc.h> |
| 14 | #include <linux/mmzone.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 15 | #include <linux/anon_inodes.h> |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 16 | #include <linux/fdtable.h> |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 17 | #include <linux/file.h> |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 18 | #include <linux/fs.h> |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 19 | #include <linux/license.h> |
| 20 | #include <linux/filter.h> |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 21 | #include <linux/kernel.h> |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 22 | #include <linux/idr.h> |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 23 | #include <linux/cred.h> |
| 24 | #include <linux/timekeeping.h> |
| 25 | #include <linux/ctype.h> |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 26 | #include <linux/nospec.h> |
Daniel Borkmann | bae141f | 2019-12-06 22:49:34 +0100 | [diff] [blame] | 27 | #include <linux/audit.h> |
Alexei Starovoitov | ccfe29e | 2019-10-15 20:24:58 -0700 | [diff] [blame] | 28 | #include <uapi/linux/btf.h> |
Mike Rapoport | ca5999f | 2020-06-08 21:32:38 -0700 | [diff] [blame] | 29 | #include <linux/pgtable.h> |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 30 | #include <linux/bpf_lsm.h> |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 31 | #include <linux/poll.h> |
Jakub Sitnicki | a3fd7ce | 2020-05-31 10:28:36 +0200 | [diff] [blame] | 32 | #include <linux/bpf-netns.h> |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 33 | #include <linux/rcupdate_trace.h> |
Roman Gushchin | 48edc1f | 2020-12-01 13:58:32 -0800 | [diff] [blame] | 34 | #include <linux/memcontrol.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 35 | |
Daniel Borkmann | da765a2 | 2019-11-22 21:07:58 +0100 | [diff] [blame] | 36 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ |
| 37 | (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ |
| 38 | (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) |
| 39 | #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY) |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 40 | #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] | 41 | #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \ |
| 42 | IS_FD_HASH(map)) |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 43 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 44 | #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) |
| 45 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 46 | DEFINE_PER_CPU(int, bpf_prog_active); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 47 | static DEFINE_IDR(prog_idr); |
| 48 | static DEFINE_SPINLOCK(prog_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 49 | static DEFINE_IDR(map_idr); |
| 50 | static DEFINE_SPINLOCK(map_idr_lock); |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 51 | static DEFINE_IDR(link_idr); |
| 52 | static DEFINE_SPINLOCK(link_idr_lock); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 53 | |
Daniel Borkmann | 08389d8 | 2021-05-11 22:35:17 +0200 | [diff] [blame] | 54 | int sysctl_unprivileged_bpf_disabled __read_mostly = |
| 55 | IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 56 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 57 | static const struct bpf_map_ops * const bpf_map_types[] = { |
Alexei Starovoitov | 91cc1a9 | 2019-11-14 10:57:15 -0800 | [diff] [blame] | 58 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 59 | #define BPF_MAP_TYPE(_id, _ops) \ |
| 60 | [_id] = &_ops, |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 61 | #define BPF_LINK_TYPE(_id, _name) |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 62 | #include <linux/bpf_types.h> |
| 63 | #undef BPF_PROG_TYPE |
| 64 | #undef BPF_MAP_TYPE |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 65 | #undef BPF_LINK_TYPE |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 66 | }; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 67 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 68 | /* |
| 69 | * If we're handed a bigger struct than we know of, ensure all the unknown bits |
| 70 | * are 0 - i.e. new user-space does not rely on any kernel feature extensions |
| 71 | * we don't know about yet. |
| 72 | * |
| 73 | * There is a ToCToU between this function call and the following |
| 74 | * copy_from_user() call. However, this is not a concern since this function is |
| 75 | * meant to be a future-proofing of bits. |
| 76 | */ |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 77 | int bpf_check_uarg_tail_zero(bpfptr_t uaddr, |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 78 | size_t expected_size, |
| 79 | size_t actual_size) |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 80 | { |
Al Viro | b7e4b65 | 2020-05-08 00:16:31 -0400 | [diff] [blame] | 81 | int res; |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 82 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 83 | if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ |
| 84 | return -E2BIG; |
| 85 | |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 86 | if (actual_size <= expected_size) |
| 87 | return 0; |
| 88 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 89 | if (uaddr.is_kernel) |
| 90 | res = memchr_inv(uaddr.kernel + expected_size, 0, |
| 91 | actual_size - expected_size) == NULL; |
| 92 | else |
| 93 | res = check_zeroed_user(uaddr.user + expected_size, |
| 94 | actual_size - expected_size); |
Al Viro | b7e4b65 | 2020-05-08 00:16:31 -0400 | [diff] [blame] | 95 | if (res < 0) |
| 96 | return res; |
| 97 | return res ? 0 : -E2BIG; |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 100 | const struct bpf_map_ops bpf_map_offload_ops = { |
Martin KaFai Lau | f4d0525 | 2020-08-27 18:18:06 -0700 | [diff] [blame] | 101 | .map_meta_equal = bpf_map_meta_equal, |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 102 | .map_alloc = bpf_map_offload_map_alloc, |
| 103 | .map_free = bpf_map_offload_map_free, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 104 | .map_check_btf = map_check_no_btf, |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 107 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
| 108 | { |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 109 | const struct bpf_map_ops *ops; |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 110 | u32 type = attr->map_type; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 111 | struct bpf_map *map; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 112 | int err; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 113 | |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 114 | if (type >= ARRAY_SIZE(bpf_map_types)) |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 115 | return ERR_PTR(-EINVAL); |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 116 | type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types)); |
| 117 | ops = bpf_map_types[type]; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 118 | if (!ops) |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 119 | return ERR_PTR(-EINVAL); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 120 | |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 121 | if (ops->map_alloc_check) { |
| 122 | err = ops->map_alloc_check(attr); |
| 123 | if (err) |
| 124 | return ERR_PTR(err); |
| 125 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 126 | if (attr->map_ifindex) |
| 127 | ops = &bpf_map_offload_ops; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 128 | map = ops->map_alloc(attr); |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 129 | if (IS_ERR(map)) |
| 130 | return map; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 131 | map->ops = ops; |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 132 | map->map_type = type; |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 133 | return map; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 136 | static void bpf_map_write_active_inc(struct bpf_map *map) |
| 137 | { |
| 138 | atomic64_inc(&map->writecnt); |
| 139 | } |
| 140 | |
| 141 | static void bpf_map_write_active_dec(struct bpf_map *map) |
| 142 | { |
| 143 | atomic64_dec(&map->writecnt); |
| 144 | } |
| 145 | |
| 146 | bool bpf_map_write_active(const struct bpf_map *map) |
| 147 | { |
| 148 | return atomic64_read(&map->writecnt) != 0; |
| 149 | } |
| 150 | |
Roman Gushchin | 80ee81e | 2020-12-01 13:58:58 -0800 | [diff] [blame] | 151 | static u32 bpf_map_value_size(const struct bpf_map *map) |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 152 | { |
| 153 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 154 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
| 155 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || |
| 156 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
| 157 | return round_up(map->value_size, 8) * num_possible_cpus(); |
| 158 | else if (IS_FD_MAP(map)) |
| 159 | return sizeof(u32); |
| 160 | else |
| 161 | return map->value_size; |
| 162 | } |
| 163 | |
| 164 | static void maybe_wait_bpf_programs(struct bpf_map *map) |
| 165 | { |
| 166 | /* Wait for any running BPF programs to complete so that |
| 167 | * userspace, when we return to it, knows that all programs |
| 168 | * that could be running use the new map value. |
| 169 | */ |
| 170 | if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS || |
| 171 | map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) |
| 172 | synchronize_rcu(); |
| 173 | } |
| 174 | |
| 175 | static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key, |
| 176 | void *value, __u64 flags) |
| 177 | { |
| 178 | int err; |
| 179 | |
| 180 | /* Need to create a kthread, thus must support schedule */ |
| 181 | if (bpf_map_is_dev_bound(map)) { |
| 182 | return bpf_map_offload_update_elem(map, key, value, flags); |
| 183 | } else if (map->map_type == BPF_MAP_TYPE_CPUMAP || |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 184 | map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
| 185 | return map->ops->map_update_elem(map, key, value, flags); |
Lorenz Bauer | 13b79d3 | 2020-08-21 11:29:45 +0100 | [diff] [blame] | 186 | } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH || |
| 187 | map->map_type == BPF_MAP_TYPE_SOCKMAP) { |
| 188 | return sock_map_update_elem_sys(map, key, value, flags); |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 189 | } else if (IS_FD_PROG_ARRAY(map)) { |
| 190 | return bpf_fd_array_map_update_elem(map, f.file, key, value, |
| 191 | flags); |
| 192 | } |
| 193 | |
Thomas Gleixner | b6e5dae | 2020-02-24 15:01:49 +0100 | [diff] [blame] | 194 | bpf_disable_instrumentation(); |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 195 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 196 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
| 197 | err = bpf_percpu_hash_update(map, key, value, flags); |
| 198 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 199 | err = bpf_percpu_array_update(map, key, value, flags); |
| 200 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { |
| 201 | err = bpf_percpu_cgroup_storage_update(map, key, value, |
| 202 | flags); |
| 203 | } else if (IS_FD_ARRAY(map)) { |
| 204 | rcu_read_lock(); |
| 205 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, |
| 206 | flags); |
| 207 | rcu_read_unlock(); |
| 208 | } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
| 209 | rcu_read_lock(); |
| 210 | err = bpf_fd_htab_map_update_elem(map, f.file, key, value, |
| 211 | flags); |
| 212 | rcu_read_unlock(); |
| 213 | } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { |
| 214 | /* rcu_read_lock() is not needed */ |
| 215 | err = bpf_fd_reuseport_array_update_elem(map, key, value, |
| 216 | flags); |
| 217 | } else if (map->map_type == BPF_MAP_TYPE_QUEUE || |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 218 | map->map_type == BPF_MAP_TYPE_STACK || |
| 219 | map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) { |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 220 | err = map->ops->map_push_elem(map, value, flags); |
| 221 | } else { |
| 222 | rcu_read_lock(); |
| 223 | err = map->ops->map_update_elem(map, key, value, flags); |
| 224 | rcu_read_unlock(); |
| 225 | } |
Thomas Gleixner | b6e5dae | 2020-02-24 15:01:49 +0100 | [diff] [blame] | 226 | bpf_enable_instrumentation(); |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 227 | maybe_wait_bpf_programs(map); |
| 228 | |
| 229 | return err; |
| 230 | } |
| 231 | |
| 232 | static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value, |
| 233 | __u64 flags) |
| 234 | { |
| 235 | void *ptr; |
| 236 | int err; |
| 237 | |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 238 | if (bpf_map_is_dev_bound(map)) |
| 239 | return bpf_map_offload_lookup_elem(map, key, value); |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 240 | |
Thomas Gleixner | b6e5dae | 2020-02-24 15:01:49 +0100 | [diff] [blame] | 241 | bpf_disable_instrumentation(); |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 242 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 243 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
| 244 | err = bpf_percpu_hash_copy(map, key, value); |
| 245 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 246 | err = bpf_percpu_array_copy(map, key, value); |
| 247 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { |
| 248 | err = bpf_percpu_cgroup_storage_copy(map, key, value); |
| 249 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
| 250 | err = bpf_stackmap_copy(map, key, value); |
| 251 | } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) { |
| 252 | err = bpf_fd_array_map_lookup_elem(map, key, value); |
| 253 | } else if (IS_FD_HASH(map)) { |
| 254 | err = bpf_fd_htab_map_lookup_elem(map, key, value); |
| 255 | } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { |
| 256 | err = bpf_fd_reuseport_array_lookup_elem(map, key, value); |
| 257 | } else if (map->map_type == BPF_MAP_TYPE_QUEUE || |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 258 | map->map_type == BPF_MAP_TYPE_STACK || |
| 259 | map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) { |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 260 | err = map->ops->map_peek_elem(map, value); |
| 261 | } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
| 262 | /* struct_ops map requires directly updating "value" */ |
| 263 | err = bpf_struct_ops_map_sys_lookup_elem(map, key, value); |
| 264 | } else { |
| 265 | rcu_read_lock(); |
| 266 | if (map->ops->map_lookup_elem_sys_only) |
| 267 | ptr = map->ops->map_lookup_elem_sys_only(map, key); |
| 268 | else |
| 269 | ptr = map->ops->map_lookup_elem(map, key); |
| 270 | if (IS_ERR(ptr)) { |
| 271 | err = PTR_ERR(ptr); |
| 272 | } else if (!ptr) { |
| 273 | err = -ENOENT; |
| 274 | } else { |
| 275 | err = 0; |
| 276 | if (flags & BPF_F_LOCK) |
| 277 | /* lock 'ptr' and copy everything but lock */ |
| 278 | copy_map_value_locked(map, value, ptr, true); |
| 279 | else |
| 280 | copy_map_value(map, value, ptr); |
Alexei Starovoitov | 6813466 | 2021-07-14 17:54:10 -0700 | [diff] [blame] | 281 | /* mask lock and timer, since value wasn't zero inited */ |
| 282 | check_and_init_map_value(map, value); |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 283 | } |
| 284 | rcu_read_unlock(); |
| 285 | } |
| 286 | |
Thomas Gleixner | b6e5dae | 2020-02-24 15:01:49 +0100 | [diff] [blame] | 287 | bpf_enable_instrumentation(); |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 288 | maybe_wait_bpf_programs(map); |
| 289 | |
| 290 | return err; |
| 291 | } |
| 292 | |
Roman Gushchin | d5299b6 | 2020-12-01 13:58:33 -0800 | [diff] [blame] | 293 | /* Please, do not use this function outside from the map creation path |
| 294 | * (e.g. in map update path) without taking care of setting the active |
| 295 | * memory cgroup (see at bpf_map_kmalloc_node() for example). |
| 296 | */ |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 297 | 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] | 298 | { |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 299 | /* We really just want to fail instead of triggering OOM killer |
| 300 | * under memory pressure, therefore we set __GFP_NORETRY to kmalloc, |
| 301 | * which is used for lower order allocation requests. |
| 302 | * |
| 303 | * It has been observed that higher order allocation requests done by |
| 304 | * vmalloc with __GFP_NORETRY being set might fail due to not trying |
| 305 | * to reclaim memory from the page cache, thus we set |
| 306 | * __GFP_RETRY_MAYFAIL to avoid such situations. |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 307 | */ |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 308 | |
Roman Gushchin | d5299b6 | 2020-12-01 13:58:33 -0800 | [diff] [blame] | 309 | const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT; |
Christoph Hellwig | 041de93 | 2020-06-01 21:52:02 -0700 | [diff] [blame] | 310 | unsigned int flags = 0; |
| 311 | unsigned long align = 1; |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 312 | void *area; |
| 313 | |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 314 | if (size >= SIZE_MAX) |
| 315 | return NULL; |
| 316 | |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 317 | /* kmalloc()'ed memory can't be mmap()'ed */ |
Christoph Hellwig | 041de93 | 2020-06-01 21:52:02 -0700 | [diff] [blame] | 318 | if (mmapable) { |
| 319 | BUG_ON(!PAGE_ALIGNED(size)); |
| 320 | align = SHMLBA; |
| 321 | flags = VM_USERMAP; |
| 322 | } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { |
| 323 | area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY, |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 324 | numa_node); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 325 | if (area != NULL) |
| 326 | return area; |
| 327 | } |
Christoph Hellwig | 041de93 | 2020-06-01 21:52:02 -0700 | [diff] [blame] | 328 | |
| 329 | return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, |
| 330 | gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL, |
| 331 | flags, numa_node, __builtin_return_address(0)); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 334 | void *bpf_map_area_alloc(u64 size, int numa_node) |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 335 | { |
| 336 | return __bpf_map_area_alloc(size, numa_node, false); |
| 337 | } |
| 338 | |
Daniel Borkmann | 196e8ca | 2019-11-20 23:04:44 +0100 | [diff] [blame] | 339 | void *bpf_map_area_mmapable_alloc(u64 size, int numa_node) |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 340 | { |
| 341 | return __bpf_map_area_alloc(size, numa_node, true); |
| 342 | } |
| 343 | |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 344 | void bpf_map_area_free(void *area) |
| 345 | { |
| 346 | kvfree(area); |
| 347 | } |
| 348 | |
Daniel Borkmann | be70bcd | 2019-04-09 23:20:04 +0200 | [diff] [blame] | 349 | static u32 bpf_map_flags_retain_permanent(u32 flags) |
| 350 | { |
| 351 | /* Some map creation flags are not tied to the map object but |
| 352 | * rather to the map fd instead, so they have no meaning upon |
| 353 | * map object inspection since multiple file descriptors with |
| 354 | * different (access) properties can exist here. Thus, given |
| 355 | * this has zero meaning for the map itself, lets clear these |
| 356 | * from here. |
| 357 | */ |
| 358 | return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY); |
| 359 | } |
| 360 | |
Jakub Kicinski | bd47564 | 2018-01-11 20:29:06 -0800 | [diff] [blame] | 361 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) |
| 362 | { |
| 363 | map->map_type = attr->map_type; |
| 364 | map->key_size = attr->key_size; |
| 365 | map->value_size = attr->value_size; |
| 366 | map->max_entries = attr->max_entries; |
Daniel Borkmann | be70bcd | 2019-04-09 23:20:04 +0200 | [diff] [blame] | 367 | map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags); |
Jakub Kicinski | bd47564 | 2018-01-11 20:29:06 -0800 | [diff] [blame] | 368 | map->numa_node = bpf_map_attr_numa_node(attr); |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 369 | map->map_extra = attr->map_extra; |
Jakub Kicinski | bd47564 | 2018-01-11 20:29:06 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 372 | static int bpf_map_alloc_id(struct bpf_map *map) |
| 373 | { |
| 374 | int id; |
| 375 | |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 376 | idr_preload(GFP_KERNEL); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 377 | spin_lock_bh(&map_idr_lock); |
| 378 | id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); |
| 379 | if (id > 0) |
| 380 | map->id = id; |
| 381 | spin_unlock_bh(&map_idr_lock); |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 382 | idr_preload_end(); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 383 | |
| 384 | if (WARN_ON_ONCE(!id)) |
| 385 | return -ENOSPC; |
| 386 | |
| 387 | return id > 0 ? 0 : id; |
| 388 | } |
| 389 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 390 | 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] | 391 | { |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 392 | unsigned long flags; |
| 393 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 394 | /* Offloaded maps are removed from the IDR store when their device |
| 395 | * disappears - even if someone holds an fd to them they are unusable, |
| 396 | * the memory is gone, all ops will fail; they are simply waiting for |
| 397 | * refcnt to drop to be freed. |
| 398 | */ |
| 399 | if (!map->id) |
| 400 | return; |
| 401 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 402 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 403 | spin_lock_irqsave(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 404 | else |
| 405 | __acquire(&map_idr_lock); |
| 406 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 407 | idr_remove(&map_idr, map->id); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 408 | map->id = 0; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 409 | |
| 410 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 411 | spin_unlock_irqrestore(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 412 | else |
| 413 | __release(&map_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Roman Gushchin | 48edc1f | 2020-12-01 13:58:32 -0800 | [diff] [blame] | 416 | #ifdef CONFIG_MEMCG_KMEM |
| 417 | static void bpf_map_save_memcg(struct bpf_map *map) |
| 418 | { |
| 419 | map->memcg = get_mem_cgroup_from_mm(current->mm); |
| 420 | } |
| 421 | |
| 422 | static void bpf_map_release_memcg(struct bpf_map *map) |
| 423 | { |
| 424 | mem_cgroup_put(map->memcg); |
| 425 | } |
| 426 | |
| 427 | void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags, |
| 428 | int node) |
| 429 | { |
| 430 | struct mem_cgroup *old_memcg; |
| 431 | void *ptr; |
| 432 | |
| 433 | old_memcg = set_active_memcg(map->memcg); |
| 434 | ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node); |
| 435 | set_active_memcg(old_memcg); |
| 436 | |
| 437 | return ptr; |
| 438 | } |
| 439 | |
| 440 | void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags) |
| 441 | { |
| 442 | struct mem_cgroup *old_memcg; |
| 443 | void *ptr; |
| 444 | |
| 445 | old_memcg = set_active_memcg(map->memcg); |
| 446 | ptr = kzalloc(size, flags | __GFP_ACCOUNT); |
| 447 | set_active_memcg(old_memcg); |
| 448 | |
| 449 | return ptr; |
| 450 | } |
| 451 | |
| 452 | void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, |
| 453 | size_t align, gfp_t flags) |
| 454 | { |
| 455 | struct mem_cgroup *old_memcg; |
| 456 | void __percpu *ptr; |
| 457 | |
| 458 | old_memcg = set_active_memcg(map->memcg); |
| 459 | ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT); |
| 460 | set_active_memcg(old_memcg); |
| 461 | |
| 462 | return ptr; |
| 463 | } |
| 464 | |
| 465 | #else |
| 466 | static void bpf_map_save_memcg(struct bpf_map *map) |
| 467 | { |
| 468 | } |
| 469 | |
| 470 | static void bpf_map_release_memcg(struct bpf_map *map) |
| 471 | { |
| 472 | } |
| 473 | #endif |
| 474 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 475 | /* called from workqueue */ |
| 476 | static void bpf_map_free_deferred(struct work_struct *work) |
| 477 | { |
| 478 | struct bpf_map *map = container_of(work, struct bpf_map, work); |
| 479 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 480 | security_bpf_map_free(map); |
Roman Gushchin | 48edc1f | 2020-12-01 13:58:32 -0800 | [diff] [blame] | 481 | bpf_map_release_memcg(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 482 | /* implementation dependent freeing */ |
| 483 | map->ops->map_free(map); |
| 484 | } |
| 485 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 486 | static void bpf_map_put_uref(struct bpf_map *map) |
| 487 | { |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 488 | if (atomic64_dec_and_test(&map->usercnt)) { |
John Fastabend | ba6b8de | 2018-04-23 15:39:23 -0700 | [diff] [blame] | 489 | if (map->ops->map_release_uref) |
| 490 | map->ops->map_release_uref(map); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 494 | /* decrement map refcnt and schedule it for freeing via workqueue |
| 495 | * (unrelying map implementation ops->map_free() might sleep) |
| 496 | */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 497 | 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] | 498 | { |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 499 | if (atomic64_dec_and_test(&map->refcnt)) { |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 500 | /* bpf_map_free_id() must be called first */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 501 | bpf_map_free_id(map, do_idr_lock); |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 502 | btf_put(map->btf); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 503 | INIT_WORK(&map->work, bpf_map_free_deferred); |
| 504 | schedule_work(&map->work); |
| 505 | } |
| 506 | } |
| 507 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 508 | void bpf_map_put(struct bpf_map *map) |
| 509 | { |
| 510 | __bpf_map_put(map, true); |
| 511 | } |
Jakub Kicinski | 630a4d3 | 2018-05-03 18:37:09 -0700 | [diff] [blame] | 512 | EXPORT_SYMBOL_GPL(bpf_map_put); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 513 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 514 | void bpf_map_put_with_uref(struct bpf_map *map) |
| 515 | { |
| 516 | bpf_map_put_uref(map); |
| 517 | bpf_map_put(map); |
| 518 | } |
| 519 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 520 | static int bpf_map_release(struct inode *inode, struct file *filp) |
| 521 | { |
Daniel Borkmann | 61d1b6a | 2016-06-15 22:47:12 +0200 | [diff] [blame] | 522 | struct bpf_map *map = filp->private_data; |
| 523 | |
| 524 | if (map->ops->map_release) |
| 525 | map->ops->map_release(map, filp); |
| 526 | |
| 527 | bpf_map_put_with_uref(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 528 | return 0; |
| 529 | } |
| 530 | |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 531 | static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f) |
| 532 | { |
| 533 | fmode_t mode = f.file->f_mode; |
| 534 | |
| 535 | /* Our file permissions may have been overridden by global |
| 536 | * map permissions facing syscall side. |
| 537 | */ |
| 538 | if (READ_ONCE(map->frozen)) |
| 539 | mode &= ~FMODE_CAN_WRITE; |
| 540 | return mode; |
| 541 | } |
| 542 | |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 543 | #ifdef CONFIG_PROC_FS |
Roman Gushchin | 80ee81e | 2020-12-01 13:58:58 -0800 | [diff] [blame] | 544 | /* Provides an approximation of the map's memory footprint. |
| 545 | * Used only to provide a backward compatibility and display |
| 546 | * a reasonable "memlock" info. |
| 547 | */ |
| 548 | static unsigned long bpf_map_memory_footprint(const struct bpf_map *map) |
| 549 | { |
| 550 | unsigned long size; |
| 551 | |
| 552 | size = round_up(map->key_size + bpf_map_value_size(map), 8); |
| 553 | |
| 554 | return round_up(map->max_entries * size, PAGE_SIZE); |
| 555 | } |
| 556 | |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 557 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) |
| 558 | { |
| 559 | const struct bpf_map *map = filp->private_data; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 560 | const struct bpf_array *array; |
Daniel Borkmann | 2beee5f | 2019-11-22 21:07:56 +0100 | [diff] [blame] | 561 | u32 type = 0, jited = 0; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 562 | |
| 563 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { |
| 564 | array = container_of(map, struct bpf_array, map); |
Toke Høiland-Jørgensen | 54713c8 | 2021-10-26 13:00:19 +0200 | [diff] [blame] | 565 | spin_lock(&array->aux->owner.lock); |
| 566 | type = array->aux->owner.type; |
| 567 | jited = array->aux->owner.jited; |
| 568 | spin_unlock(&array->aux->owner.lock); |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 569 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 570 | |
| 571 | seq_printf(m, |
| 572 | "map_type:\t%u\n" |
| 573 | "key_size:\t%u\n" |
| 574 | "value_size:\t%u\n" |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 575 | "max_entries:\t%u\n" |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 576 | "map_flags:\t%#x\n" |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 577 | "map_extra:\t%#llx\n" |
Roman Gushchin | 80ee81e | 2020-12-01 13:58:58 -0800 | [diff] [blame] | 578 | "memlock:\t%lu\n" |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 579 | "map_id:\t%u\n" |
| 580 | "frozen:\t%u\n", |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 581 | map->map_type, |
| 582 | map->key_size, |
| 583 | map->value_size, |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 584 | map->max_entries, |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 585 | map->map_flags, |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 586 | (unsigned long long)map->map_extra, |
Roman Gushchin | 80ee81e | 2020-12-01 13:58:58 -0800 | [diff] [blame] | 587 | bpf_map_memory_footprint(map), |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 588 | map->id, |
| 589 | READ_ONCE(map->frozen)); |
Daniel Borkmann | 2beee5f | 2019-11-22 21:07:56 +0100 | [diff] [blame] | 590 | if (type) { |
| 591 | seq_printf(m, "owner_prog_type:\t%u\n", type); |
| 592 | seq_printf(m, "owner_jited:\t%u\n", jited); |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 593 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 594 | } |
| 595 | #endif |
| 596 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 597 | static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, |
| 598 | loff_t *ppos) |
| 599 | { |
| 600 | /* We need this handler such that alloc_file() enables |
| 601 | * f_mode with FMODE_CAN_READ. |
| 602 | */ |
| 603 | return -EINVAL; |
| 604 | } |
| 605 | |
| 606 | static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, |
| 607 | size_t siz, loff_t *ppos) |
| 608 | { |
| 609 | /* We need this handler such that alloc_file() enables |
| 610 | * f_mode with FMODE_CAN_WRITE. |
| 611 | */ |
| 612 | return -EINVAL; |
| 613 | } |
| 614 | |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 615 | /* called for any extra memory-mapped regions (except initial) */ |
| 616 | static void bpf_map_mmap_open(struct vm_area_struct *vma) |
| 617 | { |
| 618 | struct bpf_map *map = vma->vm_file->private_data; |
| 619 | |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 620 | if (vma->vm_flags & VM_MAYWRITE) |
| 621 | bpf_map_write_active_inc(map); |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* called for all unmapped memory region (including initial) */ |
| 625 | static void bpf_map_mmap_close(struct vm_area_struct *vma) |
| 626 | { |
| 627 | struct bpf_map *map = vma->vm_file->private_data; |
| 628 | |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 629 | if (vma->vm_flags & VM_MAYWRITE) |
| 630 | bpf_map_write_active_dec(map); |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | static const struct vm_operations_struct bpf_map_default_vmops = { |
| 634 | .open = bpf_map_mmap_open, |
| 635 | .close = bpf_map_mmap_close, |
| 636 | }; |
| 637 | |
| 638 | static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma) |
| 639 | { |
| 640 | struct bpf_map *map = filp->private_data; |
| 641 | int err; |
| 642 | |
Alexei Starovoitov | 6813466 | 2021-07-14 17:54:10 -0700 | [diff] [blame] | 643 | if (!map->ops->map_mmap || map_value_has_spin_lock(map) || |
| 644 | map_value_has_timer(map)) |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 645 | return -ENOTSUPP; |
| 646 | |
| 647 | if (!(vma->vm_flags & VM_SHARED)) |
| 648 | return -EINVAL; |
| 649 | |
| 650 | mutex_lock(&map->freeze_mutex); |
| 651 | |
Andrii Nakryiko | dfeb376 | 2020-05-18 22:38:24 -0700 | [diff] [blame] | 652 | if (vma->vm_flags & VM_WRITE) { |
| 653 | if (map->frozen) { |
| 654 | err = -EPERM; |
| 655 | goto out; |
| 656 | } |
| 657 | /* map is meant to be read-only, so do not allow mapping as |
| 658 | * writable, because it's possible to leak a writable page |
| 659 | * reference and allows user-space to still modify it after |
| 660 | * freezing, while verifier will assume contents do not change |
| 661 | */ |
| 662 | if (map->map_flags & BPF_F_RDONLY_PROG) { |
| 663 | err = -EACCES; |
| 664 | goto out; |
| 665 | } |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /* set default open/close callbacks */ |
| 669 | vma->vm_ops = &bpf_map_default_vmops; |
| 670 | vma->vm_private_data = map; |
Andrii Nakryiko | 1f6cb19 | 2020-04-10 13:26:12 -0700 | [diff] [blame] | 671 | vma->vm_flags &= ~VM_MAYEXEC; |
| 672 | if (!(vma->vm_flags & VM_WRITE)) |
| 673 | /* disallow re-mapping with PROT_WRITE */ |
| 674 | vma->vm_flags &= ~VM_MAYWRITE; |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 675 | |
| 676 | err = map->ops->map_mmap(map, vma); |
| 677 | if (err) |
| 678 | goto out; |
| 679 | |
Andrii Nakryiko | 1f6cb19 | 2020-04-10 13:26:12 -0700 | [diff] [blame] | 680 | if (vma->vm_flags & VM_MAYWRITE) |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 681 | bpf_map_write_active_inc(map); |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 682 | out: |
| 683 | mutex_unlock(&map->freeze_mutex); |
| 684 | return err; |
| 685 | } |
| 686 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 687 | static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts) |
| 688 | { |
| 689 | struct bpf_map *map = filp->private_data; |
| 690 | |
| 691 | if (map->ops->map_poll) |
| 692 | return map->ops->map_poll(map, filp, pts); |
| 693 | |
| 694 | return EPOLLERR; |
| 695 | } |
| 696 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 697 | const struct file_operations bpf_map_fops = { |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 698 | #ifdef CONFIG_PROC_FS |
| 699 | .show_fdinfo = bpf_map_show_fdinfo, |
| 700 | #endif |
| 701 | .release = bpf_map_release, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 702 | .read = bpf_dummy_read, |
| 703 | .write = bpf_dummy_write, |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 704 | .mmap = bpf_map_mmap, |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 705 | .poll = bpf_map_poll, |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 706 | }; |
| 707 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 708 | int bpf_map_new_fd(struct bpf_map *map, int flags) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 709 | { |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 710 | int ret; |
| 711 | |
| 712 | ret = security_bpf_map(map, OPEN_FMODE(flags)); |
| 713 | if (ret < 0) |
| 714 | return ret; |
| 715 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 716 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 717 | flags | O_CLOEXEC); |
| 718 | } |
| 719 | |
| 720 | int bpf_get_file_flag(int flags) |
| 721 | { |
| 722 | if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) |
| 723 | return -EINVAL; |
| 724 | if (flags & BPF_F_RDONLY) |
| 725 | return O_RDONLY; |
| 726 | if (flags & BPF_F_WRONLY) |
| 727 | return O_WRONLY; |
| 728 | return O_RDWR; |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 729 | } |
| 730 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 731 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
| 732 | #define CHECK_ATTR(CMD) \ |
| 733 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ |
| 734 | sizeof(attr->CMD##_LAST_FIELD), 0, \ |
| 735 | sizeof(*attr) - \ |
| 736 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ |
| 737 | sizeof(attr->CMD##_LAST_FIELD)) != NULL |
| 738 | |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame] | 739 | /* dst and src must have at least "size" number of bytes. |
| 740 | * Return strlen on success and < 0 on error. |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 741 | */ |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame] | 742 | 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] | 743 | { |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame] | 744 | const char *end = src + size; |
| 745 | const char *orig_src = src; |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 746 | |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame] | 747 | memset(dst, 0, size); |
Daniel Borkmann | 3e0ddc4f | 2019-04-09 23:20:07 +0200 | [diff] [blame] | 748 | /* Copy all isalnum(), '_' and '.' chars. */ |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 749 | while (src < end && *src) { |
Daniel Borkmann | 3e0ddc4f | 2019-04-09 23:20:07 +0200 | [diff] [blame] | 750 | if (!isalnum(*src) && |
| 751 | *src != '_' && *src != '.') |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 752 | return -EINVAL; |
| 753 | *dst++ = *src++; |
| 754 | } |
| 755 | |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame] | 756 | /* No '\0' found in "size" number of bytes */ |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 757 | if (src == end) |
| 758 | return -EINVAL; |
| 759 | |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame] | 760 | return src - orig_src; |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 763 | int map_check_no_btf(const struct bpf_map *map, |
Roman Gushchin | 1b2b234 | 2018-12-10 15:43:00 -0800 | [diff] [blame] | 764 | const struct btf *btf, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 765 | const struct btf_type *key_type, |
| 766 | const struct btf_type *value_type) |
| 767 | { |
| 768 | return -ENOTSUPP; |
| 769 | } |
| 770 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 771 | 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] | 772 | u32 btf_key_id, u32 btf_value_id) |
| 773 | { |
| 774 | const struct btf_type *key_type, *value_type; |
| 775 | u32 key_size, value_size; |
| 776 | int ret = 0; |
| 777 | |
Daniel Borkmann | 2824ecb | 2019-04-09 23:20:10 +0200 | [diff] [blame] | 778 | /* Some maps allow key to be unspecified. */ |
| 779 | if (btf_key_id) { |
| 780 | key_type = btf_type_id_size(btf, &btf_key_id, &key_size); |
| 781 | if (!key_type || key_size != map->key_size) |
| 782 | return -EINVAL; |
| 783 | } else { |
| 784 | key_type = btf_type_by_id(btf, 0); |
| 785 | if (!map->ops->map_check_btf) |
| 786 | return -EINVAL; |
| 787 | } |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 788 | |
| 789 | value_type = btf_type_id_size(btf, &btf_value_id, &value_size); |
| 790 | if (!value_type || value_size != map->value_size) |
| 791 | return -EINVAL; |
| 792 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 793 | map->spin_lock_off = btf_find_spin_lock(btf, value_type); |
| 794 | |
| 795 | if (map_value_has_spin_lock(map)) { |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 796 | if (map->map_flags & BPF_F_RDONLY_PROG) |
| 797 | return -EACCES; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 798 | if (map->map_type != BPF_MAP_TYPE_HASH && |
Alexei Starovoitov | e16d2f1 | 2019-01-31 15:40:05 -0800 | [diff] [blame] | 799 | map->map_type != BPF_MAP_TYPE_ARRAY && |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 800 | map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
KP Singh | 8ea6368 | 2020-08-25 20:29:17 +0200 | [diff] [blame] | 801 | map->map_type != BPF_MAP_TYPE_SK_STORAGE && |
KP Singh | 4cf1bc1 | 2020-11-06 10:37:40 +0000 | [diff] [blame] | 802 | map->map_type != BPF_MAP_TYPE_INODE_STORAGE && |
| 803 | map->map_type != BPF_MAP_TYPE_TASK_STORAGE) |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 804 | return -ENOTSUPP; |
| 805 | if (map->spin_lock_off + sizeof(struct bpf_spin_lock) > |
| 806 | map->value_size) { |
| 807 | WARN_ONCE(1, |
| 808 | "verifier bug spin_lock_off %d value_size %d\n", |
| 809 | map->spin_lock_off, map->value_size); |
| 810 | return -EFAULT; |
| 811 | } |
| 812 | } |
| 813 | |
Alexei Starovoitov | 6813466 | 2021-07-14 17:54:10 -0700 | [diff] [blame] | 814 | map->timer_off = btf_find_timer(btf, value_type); |
| 815 | if (map_value_has_timer(map)) { |
| 816 | if (map->map_flags & BPF_F_RDONLY_PROG) |
| 817 | return -EACCES; |
| 818 | if (map->map_type != BPF_MAP_TYPE_HASH && |
| 819 | map->map_type != BPF_MAP_TYPE_LRU_HASH && |
| 820 | map->map_type != BPF_MAP_TYPE_ARRAY) |
| 821 | return -EOPNOTSUPP; |
| 822 | } |
| 823 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 824 | if (map->ops->map_check_btf) |
Roman Gushchin | 1b2b234 | 2018-12-10 15:43:00 -0800 | [diff] [blame] | 825 | ret = map->ops->map_check_btf(map, btf, key_type, value_type); |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 826 | |
| 827 | return ret; |
| 828 | } |
| 829 | |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 830 | #define BPF_MAP_CREATE_LAST_FIELD map_extra |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 831 | /* called via syscall */ |
| 832 | static int map_create(union bpf_attr *attr) |
| 833 | { |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 834 | int numa_node = bpf_map_attr_numa_node(attr); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 835 | struct bpf_map *map; |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 836 | int f_flags; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 837 | int err; |
| 838 | |
| 839 | err = CHECK_ATTR(BPF_MAP_CREATE); |
| 840 | if (err) |
| 841 | return -EINVAL; |
| 842 | |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 843 | if (attr->btf_vmlinux_value_type_id) { |
| 844 | if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS || |
| 845 | attr->btf_key_type_id || attr->btf_value_type_id) |
| 846 | return -EINVAL; |
| 847 | } else if (attr->btf_key_type_id && !attr->btf_value_type_id) { |
| 848 | return -EINVAL; |
| 849 | } |
| 850 | |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 851 | if (attr->map_type != BPF_MAP_TYPE_BLOOM_FILTER && |
| 852 | attr->map_extra != 0) |
| 853 | return -EINVAL; |
| 854 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 855 | f_flags = bpf_get_file_flag(attr->map_flags); |
| 856 | if (f_flags < 0) |
| 857 | return f_flags; |
| 858 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 859 | if (numa_node != NUMA_NO_NODE && |
Eric Dumazet | 96e5ae4 | 2017-09-04 22:41:02 -0700 | [diff] [blame] | 860 | ((unsigned int)numa_node >= nr_node_ids || |
| 861 | !node_online(numa_node))) |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 862 | return -EINVAL; |
| 863 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 864 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ |
| 865 | map = find_and_alloc_map(attr); |
| 866 | if (IS_ERR(map)) |
| 867 | return PTR_ERR(map); |
| 868 | |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame] | 869 | err = bpf_obj_name_cpy(map->name, attr->map_name, |
| 870 | sizeof(attr->map_name)); |
| 871 | if (err < 0) |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 872 | goto free_map; |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 873 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 874 | atomic64_set(&map->refcnt, 1); |
| 875 | atomic64_set(&map->usercnt, 1); |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 876 | mutex_init(&map->freeze_mutex); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 877 | |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 878 | map->spin_lock_off = -EINVAL; |
Alexei Starovoitov | 6813466 | 2021-07-14 17:54:10 -0700 | [diff] [blame] | 879 | map->timer_off = -EINVAL; |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 880 | if (attr->btf_key_type_id || attr->btf_value_type_id || |
| 881 | /* Even the map's value is a kernel's struct, |
| 882 | * the bpf_prog.o must have BTF to begin with |
| 883 | * to figure out the corresponding kernel's |
| 884 | * counter part. Thus, attr->btf_fd has |
| 885 | * to be valid also. |
| 886 | */ |
| 887 | attr->btf_vmlinux_value_type_id) { |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 888 | struct btf *btf; |
| 889 | |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 890 | btf = btf_get_by_fd(attr->btf_fd); |
| 891 | if (IS_ERR(btf)) { |
| 892 | err = PTR_ERR(btf); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 893 | goto free_map; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 894 | } |
Alexei Starovoitov | 350a5c4 | 2021-03-07 14:52:48 -0800 | [diff] [blame] | 895 | if (btf_is_kernel(btf)) { |
| 896 | btf_put(btf); |
| 897 | err = -EACCES; |
| 898 | goto free_map; |
| 899 | } |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 900 | map->btf = btf; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 901 | |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 902 | if (attr->btf_value_type_id) { |
| 903 | err = map_check_btf(map, btf, attr->btf_key_type_id, |
| 904 | attr->btf_value_type_id); |
| 905 | if (err) |
| 906 | goto free_map; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Martin KaFai Lau | 9b2cf32 | 2018-05-22 14:57:21 -0700 | [diff] [blame] | 909 | map->btf_key_type_id = attr->btf_key_type_id; |
| 910 | map->btf_value_type_id = attr->btf_value_type_id; |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 911 | map->btf_vmlinux_value_type_id = |
| 912 | attr->btf_vmlinux_value_type_id; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 915 | err = security_bpf_map_alloc(map); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 916 | if (err) |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 917 | goto free_map; |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 918 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 919 | err = bpf_map_alloc_id(map); |
| 920 | if (err) |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 921 | goto free_map_sec; |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 922 | |
Roman Gushchin | 48edc1f | 2020-12-01 13:58:32 -0800 | [diff] [blame] | 923 | bpf_map_save_memcg(map); |
| 924 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 925 | err = bpf_map_new_fd(map, f_flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 926 | if (err < 0) { |
| 927 | /* failed to allocate fd. |
Peng Sun | 352d20d | 2019-02-27 22:36:25 +0800 | [diff] [blame] | 928 | * bpf_map_put_with_uref() is needed because the above |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 929 | * bpf_map_alloc_id() has published the map |
| 930 | * to the userspace and the userspace may |
| 931 | * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. |
| 932 | */ |
Peng Sun | 352d20d | 2019-02-27 22:36:25 +0800 | [diff] [blame] | 933 | bpf_map_put_with_uref(map); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 934 | return err; |
| 935 | } |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 936 | |
| 937 | return err; |
| 938 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 939 | free_map_sec: |
| 940 | security_bpf_map_free(map); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 941 | free_map: |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 942 | btf_put(map->btf); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 943 | map->ops->map_free(map); |
| 944 | return err; |
| 945 | } |
| 946 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 947 | /* if error is returned, fd is released. |
| 948 | * On success caller should complete fd access with matching fdput() |
| 949 | */ |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 950 | struct bpf_map *__bpf_map_get(struct fd f) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 951 | { |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 952 | if (!f.file) |
| 953 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 954 | if (f.file->f_op != &bpf_map_fops) { |
| 955 | fdput(f); |
| 956 | return ERR_PTR(-EINVAL); |
| 957 | } |
| 958 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 959 | return f.file->private_data; |
| 960 | } |
| 961 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 962 | void bpf_map_inc(struct bpf_map *map) |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 963 | { |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 964 | atomic64_inc(&map->refcnt); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 965 | } |
Jakub Kicinski | 630a4d3 | 2018-05-03 18:37:09 -0700 | [diff] [blame] | 966 | EXPORT_SYMBOL_GPL(bpf_map_inc); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 967 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 968 | void bpf_map_inc_with_uref(struct bpf_map *map) |
| 969 | { |
| 970 | atomic64_inc(&map->refcnt); |
| 971 | atomic64_inc(&map->usercnt); |
| 972 | } |
| 973 | EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref); |
| 974 | |
Martin KaFai Lau | 1ed4d92 | 2020-02-25 15:04:21 -0800 | [diff] [blame] | 975 | struct bpf_map *bpf_map_get(u32 ufd) |
| 976 | { |
| 977 | struct fd f = fdget(ufd); |
| 978 | struct bpf_map *map; |
| 979 | |
| 980 | map = __bpf_map_get(f); |
| 981 | if (IS_ERR(map)) |
| 982 | return map; |
| 983 | |
| 984 | bpf_map_inc(map); |
| 985 | fdput(f); |
| 986 | |
| 987 | return map; |
| 988 | } |
| 989 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 990 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 991 | { |
| 992 | struct fd f = fdget(ufd); |
| 993 | struct bpf_map *map; |
| 994 | |
| 995 | map = __bpf_map_get(f); |
| 996 | if (IS_ERR(map)) |
| 997 | return map; |
| 998 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 999 | bpf_map_inc_with_uref(map); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1000 | fdput(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1001 | |
| 1002 | return map; |
| 1003 | } |
| 1004 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1005 | /* map_idr_lock should have been held */ |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 1006 | 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] | 1007 | { |
| 1008 | int refold; |
| 1009 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 1010 | refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1011 | if (!refold) |
| 1012 | return ERR_PTR(-ENOENT); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1013 | if (uref) |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 1014 | atomic64_inc(&map->usercnt); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1015 | |
| 1016 | return map; |
| 1017 | } |
| 1018 | |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 1019 | struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map) |
Stanislav Fomichev | b0e4701 | 2019-08-14 10:37:48 -0700 | [diff] [blame] | 1020 | { |
| 1021 | spin_lock_bh(&map_idr_lock); |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 1022 | map = __bpf_map_inc_not_zero(map, false); |
Stanislav Fomichev | b0e4701 | 2019-08-14 10:37:48 -0700 | [diff] [blame] | 1023 | spin_unlock_bh(&map_idr_lock); |
| 1024 | |
| 1025 | return map; |
| 1026 | } |
| 1027 | EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero); |
| 1028 | |
Alexei Starovoitov | b8cdc05 | 2016-03-09 18:56:49 -0800 | [diff] [blame] | 1029 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
| 1030 | { |
| 1031 | return -ENOTSUPP; |
| 1032 | } |
| 1033 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 1034 | static void *__bpf_copy_key(void __user *ukey, u64 key_size) |
| 1035 | { |
| 1036 | if (key_size) |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1037 | return vmemdup_user(ukey, key_size); |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 1038 | |
| 1039 | if (ukey) |
| 1040 | return ERR_PTR(-EINVAL); |
| 1041 | |
| 1042 | return NULL; |
| 1043 | } |
| 1044 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 1045 | static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size) |
| 1046 | { |
| 1047 | if (key_size) |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1048 | return kvmemdup_bpfptr(ukey, key_size); |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 1049 | |
| 1050 | if (!bpfptr_is_null(ukey)) |
| 1051 | return ERR_PTR(-EINVAL); |
| 1052 | |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1056 | /* last field in 'union bpf_attr' used by this command */ |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 1057 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1058 | |
| 1059 | static int map_lookup_elem(union bpf_attr *attr) |
| 1060 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1061 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 1062 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1063 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1064 | struct bpf_map *map; |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 1065 | void *key, *value; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1066 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1067 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1068 | int err; |
| 1069 | |
| 1070 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) |
| 1071 | return -EINVAL; |
| 1072 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 1073 | if (attr->flags & ~BPF_F_LOCK) |
| 1074 | return -EINVAL; |
| 1075 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1076 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1077 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1078 | if (IS_ERR(map)) |
| 1079 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1080 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1081 | err = -EPERM; |
| 1082 | goto err_put; |
| 1083 | } |
| 1084 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 1085 | if ((attr->flags & BPF_F_LOCK) && |
| 1086 | !map_value_has_spin_lock(map)) { |
| 1087 | err = -EINVAL; |
| 1088 | goto err_put; |
| 1089 | } |
| 1090 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 1091 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1092 | if (IS_ERR(key)) { |
| 1093 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1094 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1095 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1096 | |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 1097 | value_size = bpf_map_value_size(map); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1098 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 1099 | err = -ENOMEM; |
Stanislav Fomichev | f0dce1d | 2021-08-18 16:52:15 -0700 | [diff] [blame] | 1100 | value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1101 | if (!value) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 1102 | goto free_key; |
| 1103 | |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 1104 | if (map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) { |
| 1105 | if (copy_from_user(value, uvalue, value_size)) |
| 1106 | err = -EFAULT; |
| 1107 | else |
| 1108 | err = bpf_map_copy_value(map, key, value, attr->flags); |
| 1109 | goto free_value; |
| 1110 | } |
| 1111 | |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 1112 | err = bpf_map_copy_value(map, key, value, attr->flags); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1113 | if (err) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 1114 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1115 | |
| 1116 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1117 | if (copy_to_user(uvalue, value, value_size) != 0) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 1118 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1119 | |
| 1120 | err = 0; |
| 1121 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 1122 | free_value: |
Stanislav Fomichev | f0dce1d | 2021-08-18 16:52:15 -0700 | [diff] [blame] | 1123 | kvfree(value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1124 | free_key: |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1125 | kvfree(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1126 | err_put: |
| 1127 | fdput(f); |
| 1128 | return err; |
| 1129 | } |
| 1130 | |
Daniel Colascione | 1ae80cf | 2018-10-12 03:54:27 -0700 | [diff] [blame] | 1131 | |
Alexei Starovoitov | 3274f52 | 2014-11-13 17:36:44 -0800 | [diff] [blame] | 1132 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1133 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 1134 | static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1135 | { |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 1136 | bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel); |
| 1137 | bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1138 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1139 | struct bpf_map *map; |
| 1140 | void *key, *value; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1141 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1142 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1143 | int err; |
| 1144 | |
| 1145 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) |
| 1146 | return -EINVAL; |
| 1147 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1148 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1149 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1150 | if (IS_ERR(map)) |
| 1151 | return PTR_ERR(map); |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 1152 | bpf_map_write_active_inc(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1153 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1154 | err = -EPERM; |
| 1155 | goto err_put; |
| 1156 | } |
| 1157 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 1158 | if ((attr->flags & BPF_F_LOCK) && |
| 1159 | !map_value_has_spin_lock(map)) { |
| 1160 | err = -EINVAL; |
| 1161 | goto err_put; |
| 1162 | } |
| 1163 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 1164 | key = ___bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1165 | if (IS_ERR(key)) { |
| 1166 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1167 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1168 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1169 | |
Stanislav Fomichev | f0dce1d | 2021-08-18 16:52:15 -0700 | [diff] [blame] | 1170 | value_size = bpf_map_value_size(map); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 1171 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1172 | err = -ENOMEM; |
Stanislav Fomichev | f0dce1d | 2021-08-18 16:52:15 -0700 | [diff] [blame] | 1173 | value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1174 | if (!value) |
| 1175 | goto free_key; |
| 1176 | |
| 1177 | err = -EFAULT; |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 1178 | if (copy_from_bpfptr(value, uvalue, value_size) != 0) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1179 | goto free_value; |
| 1180 | |
Brian Vazquez | 15c14a3 | 2020-01-15 10:43:00 -0800 | [diff] [blame] | 1181 | err = bpf_map_update_value(map, f, key, value, attr->flags); |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 1182 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1183 | free_value: |
Stanislav Fomichev | f0dce1d | 2021-08-18 16:52:15 -0700 | [diff] [blame] | 1184 | kvfree(value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1185 | free_key: |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1186 | kvfree(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1187 | err_put: |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 1188 | bpf_map_write_active_dec(map); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1189 | fdput(f); |
| 1190 | return err; |
| 1191 | } |
| 1192 | |
| 1193 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key |
| 1194 | |
| 1195 | static int map_delete_elem(union bpf_attr *attr) |
| 1196 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1197 | void __user *ukey = u64_to_user_ptr(attr->key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1198 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1199 | struct bpf_map *map; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1200 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1201 | void *key; |
| 1202 | int err; |
| 1203 | |
| 1204 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) |
| 1205 | return -EINVAL; |
| 1206 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1207 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1208 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1209 | if (IS_ERR(map)) |
| 1210 | return PTR_ERR(map); |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 1211 | bpf_map_write_active_inc(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1212 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1213 | err = -EPERM; |
| 1214 | goto err_put; |
| 1215 | } |
| 1216 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 1217 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1218 | if (IS_ERR(key)) { |
| 1219 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1220 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1221 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1222 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1223 | if (bpf_map_is_dev_bound(map)) { |
| 1224 | err = bpf_map_offload_delete_elem(map, key); |
| 1225 | goto out; |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 1226 | } else if (IS_FD_PROG_ARRAY(map) || |
| 1227 | map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
| 1228 | /* These maps require sleepable context */ |
Daniel Borkmann | da765a2 | 2019-11-22 21:07:58 +0100 | [diff] [blame] | 1229 | err = map->ops->map_delete_elem(map, key); |
| 1230 | goto out; |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1231 | } |
| 1232 | |
Thomas Gleixner | b6e5dae | 2020-02-24 15:01:49 +0100 | [diff] [blame] | 1233 | bpf_disable_instrumentation(); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1234 | rcu_read_lock(); |
| 1235 | err = map->ops->map_delete_elem(map, key); |
| 1236 | rcu_read_unlock(); |
Thomas Gleixner | b6e5dae | 2020-02-24 15:01:49 +0100 | [diff] [blame] | 1237 | bpf_enable_instrumentation(); |
Daniel Colascione | 1ae80cf | 2018-10-12 03:54:27 -0700 | [diff] [blame] | 1238 | maybe_wait_bpf_programs(map); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1239 | out: |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1240 | kvfree(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1241 | err_put: |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 1242 | bpf_map_write_active_dec(map); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1243 | fdput(f); |
| 1244 | return err; |
| 1245 | } |
| 1246 | |
| 1247 | /* last field in 'union bpf_attr' used by this command */ |
| 1248 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key |
| 1249 | |
| 1250 | static int map_get_next_key(union bpf_attr *attr) |
| 1251 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1252 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 1253 | void __user *unext_key = u64_to_user_ptr(attr->next_key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1254 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1255 | struct bpf_map *map; |
| 1256 | void *key, *next_key; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1257 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1258 | int err; |
| 1259 | |
| 1260 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) |
| 1261 | return -EINVAL; |
| 1262 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1263 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1264 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1265 | if (IS_ERR(map)) |
| 1266 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1267 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1268 | err = -EPERM; |
| 1269 | goto err_put; |
| 1270 | } |
| 1271 | |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 1272 | if (ukey) { |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 1273 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1274 | if (IS_ERR(key)) { |
| 1275 | err = PTR_ERR(key); |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 1276 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1277 | } |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 1278 | } else { |
| 1279 | key = NULL; |
| 1280 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1281 | |
| 1282 | err = -ENOMEM; |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1283 | next_key = kvmalloc(map->key_size, GFP_USER); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1284 | if (!next_key) |
| 1285 | goto free_key; |
| 1286 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1287 | if (bpf_map_is_dev_bound(map)) { |
| 1288 | err = bpf_map_offload_get_next_key(map, key, next_key); |
| 1289 | goto out; |
| 1290 | } |
| 1291 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1292 | rcu_read_lock(); |
| 1293 | err = map->ops->map_get_next_key(map, key, next_key); |
| 1294 | rcu_read_unlock(); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1295 | out: |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1296 | if (err) |
| 1297 | goto free_next_key; |
| 1298 | |
| 1299 | err = -EFAULT; |
| 1300 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) |
| 1301 | goto free_next_key; |
| 1302 | |
| 1303 | err = 0; |
| 1304 | |
| 1305 | free_next_key: |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1306 | kvfree(next_key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1307 | free_key: |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1308 | kvfree(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1309 | err_put: |
| 1310 | fdput(f); |
| 1311 | return err; |
| 1312 | } |
| 1313 | |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1314 | int generic_map_delete_batch(struct bpf_map *map, |
| 1315 | const union bpf_attr *attr, |
| 1316 | union bpf_attr __user *uattr) |
| 1317 | { |
| 1318 | void __user *keys = u64_to_user_ptr(attr->batch.keys); |
| 1319 | u32 cp, max_count; |
| 1320 | int err = 0; |
| 1321 | void *key; |
| 1322 | |
| 1323 | if (attr->batch.elem_flags & ~BPF_F_LOCK) |
| 1324 | return -EINVAL; |
| 1325 | |
| 1326 | if ((attr->batch.elem_flags & BPF_F_LOCK) && |
| 1327 | !map_value_has_spin_lock(map)) { |
| 1328 | return -EINVAL; |
| 1329 | } |
| 1330 | |
| 1331 | max_count = attr->batch.count; |
| 1332 | if (!max_count) |
| 1333 | return 0; |
| 1334 | |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1335 | key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN); |
Brian Vazquez | 2e3a94a | 2020-01-19 11:40:40 -0800 | [diff] [blame] | 1336 | if (!key) |
| 1337 | return -ENOMEM; |
| 1338 | |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1339 | for (cp = 0; cp < max_count; cp++) { |
Brian Vazquez | 2e3a94a | 2020-01-19 11:40:40 -0800 | [diff] [blame] | 1340 | err = -EFAULT; |
| 1341 | if (copy_from_user(key, keys + cp * map->key_size, |
| 1342 | map->key_size)) |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1343 | break; |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1344 | |
| 1345 | if (bpf_map_is_dev_bound(map)) { |
| 1346 | err = bpf_map_offload_delete_elem(map, key); |
| 1347 | break; |
| 1348 | } |
| 1349 | |
Thomas Gleixner | b6e5dae | 2020-02-24 15:01:49 +0100 | [diff] [blame] | 1350 | bpf_disable_instrumentation(); |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1351 | rcu_read_lock(); |
| 1352 | err = map->ops->map_delete_elem(map, key); |
| 1353 | rcu_read_unlock(); |
Thomas Gleixner | b6e5dae | 2020-02-24 15:01:49 +0100 | [diff] [blame] | 1354 | bpf_enable_instrumentation(); |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1355 | maybe_wait_bpf_programs(map); |
| 1356 | if (err) |
| 1357 | break; |
| 1358 | } |
| 1359 | if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) |
| 1360 | err = -EFAULT; |
Brian Vazquez | 2e3a94a | 2020-01-19 11:40:40 -0800 | [diff] [blame] | 1361 | |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1362 | kvfree(key); |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1363 | return err; |
| 1364 | } |
| 1365 | |
| 1366 | int generic_map_update_batch(struct bpf_map *map, |
| 1367 | const union bpf_attr *attr, |
| 1368 | union bpf_attr __user *uattr) |
| 1369 | { |
| 1370 | void __user *values = u64_to_user_ptr(attr->batch.values); |
| 1371 | void __user *keys = u64_to_user_ptr(attr->batch.keys); |
| 1372 | u32 value_size, cp, max_count; |
Xu Kuohai | fda7a38 | 2021-10-19 03:29:34 +0000 | [diff] [blame] | 1373 | int ufd = attr->batch.map_fd; |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1374 | void *key, *value; |
| 1375 | struct fd f; |
| 1376 | int err = 0; |
| 1377 | |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1378 | if (attr->batch.elem_flags & ~BPF_F_LOCK) |
| 1379 | return -EINVAL; |
| 1380 | |
| 1381 | if ((attr->batch.elem_flags & BPF_F_LOCK) && |
| 1382 | !map_value_has_spin_lock(map)) { |
| 1383 | return -EINVAL; |
| 1384 | } |
| 1385 | |
| 1386 | value_size = bpf_map_value_size(map); |
| 1387 | |
| 1388 | max_count = attr->batch.count; |
| 1389 | if (!max_count) |
| 1390 | return 0; |
| 1391 | |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1392 | key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN); |
Brian Vazquez | 2e3a94a | 2020-01-19 11:40:40 -0800 | [diff] [blame] | 1393 | if (!key) |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1394 | return -ENOMEM; |
| 1395 | |
Stanislav Fomichev | f0dce1d | 2021-08-18 16:52:15 -0700 | [diff] [blame] | 1396 | value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Brian Vazquez | 2e3a94a | 2020-01-19 11:40:40 -0800 | [diff] [blame] | 1397 | if (!value) { |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1398 | kvfree(key); |
Brian Vazquez | 2e3a94a | 2020-01-19 11:40:40 -0800 | [diff] [blame] | 1399 | return -ENOMEM; |
| 1400 | } |
| 1401 | |
Xu Kuohai | fda7a38 | 2021-10-19 03:29:34 +0000 | [diff] [blame] | 1402 | f = fdget(ufd); /* bpf_map_do_batch() guarantees ufd is valid */ |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1403 | for (cp = 0; cp < max_count; cp++) { |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1404 | err = -EFAULT; |
Brian Vazquez | 2e3a94a | 2020-01-19 11:40:40 -0800 | [diff] [blame] | 1405 | if (copy_from_user(key, keys + cp * map->key_size, |
| 1406 | map->key_size) || |
| 1407 | copy_from_user(value, values + cp * value_size, value_size)) |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1408 | break; |
| 1409 | |
| 1410 | err = bpf_map_update_value(map, f, key, value, |
| 1411 | attr->batch.elem_flags); |
| 1412 | |
| 1413 | if (err) |
| 1414 | break; |
| 1415 | } |
| 1416 | |
| 1417 | if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) |
| 1418 | err = -EFAULT; |
| 1419 | |
Stanislav Fomichev | f0dce1d | 2021-08-18 16:52:15 -0700 | [diff] [blame] | 1420 | kvfree(value); |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1421 | kvfree(key); |
Xu Kuohai | fda7a38 | 2021-10-19 03:29:34 +0000 | [diff] [blame] | 1422 | fdput(f); |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 1423 | return err; |
| 1424 | } |
| 1425 | |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 1426 | #define MAP_LOOKUP_RETRIES 3 |
| 1427 | |
| 1428 | int generic_map_lookup_batch(struct bpf_map *map, |
| 1429 | const union bpf_attr *attr, |
| 1430 | union bpf_attr __user *uattr) |
| 1431 | { |
| 1432 | void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch); |
| 1433 | void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch); |
| 1434 | void __user *values = u64_to_user_ptr(attr->batch.values); |
| 1435 | void __user *keys = u64_to_user_ptr(attr->batch.keys); |
| 1436 | void *buf, *buf_prevkey, *prev_key, *key, *value; |
| 1437 | int err, retry = MAP_LOOKUP_RETRIES; |
| 1438 | u32 value_size, cp, max_count; |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 1439 | |
| 1440 | if (attr->batch.elem_flags & ~BPF_F_LOCK) |
| 1441 | return -EINVAL; |
| 1442 | |
| 1443 | if ((attr->batch.elem_flags & BPF_F_LOCK) && |
| 1444 | !map_value_has_spin_lock(map)) |
| 1445 | return -EINVAL; |
| 1446 | |
| 1447 | value_size = bpf_map_value_size(map); |
| 1448 | |
| 1449 | max_count = attr->batch.count; |
| 1450 | if (!max_count) |
| 1451 | return 0; |
| 1452 | |
| 1453 | if (put_user(0, &uattr->batch.count)) |
| 1454 | return -EFAULT; |
| 1455 | |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1456 | buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN); |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 1457 | if (!buf_prevkey) |
| 1458 | return -ENOMEM; |
| 1459 | |
Stanislav Fomichev | f0dce1d | 2021-08-18 16:52:15 -0700 | [diff] [blame] | 1460 | buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN); |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 1461 | if (!buf) { |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1462 | kvfree(buf_prevkey); |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 1463 | return -ENOMEM; |
| 1464 | } |
| 1465 | |
| 1466 | err = -EFAULT; |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 1467 | prev_key = NULL; |
| 1468 | if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size)) |
| 1469 | goto free_buf; |
| 1470 | key = buf; |
| 1471 | value = key + map->key_size; |
| 1472 | if (ubatch) |
| 1473 | prev_key = buf_prevkey; |
| 1474 | |
| 1475 | for (cp = 0; cp < max_count;) { |
| 1476 | rcu_read_lock(); |
| 1477 | err = map->ops->map_get_next_key(map, prev_key, key); |
| 1478 | rcu_read_unlock(); |
| 1479 | if (err) |
| 1480 | break; |
| 1481 | err = bpf_map_copy_value(map, key, value, |
| 1482 | attr->batch.elem_flags); |
| 1483 | |
| 1484 | if (err == -ENOENT) { |
| 1485 | if (retry) { |
| 1486 | retry--; |
| 1487 | continue; |
| 1488 | } |
| 1489 | err = -EINTR; |
| 1490 | break; |
| 1491 | } |
| 1492 | |
| 1493 | if (err) |
| 1494 | goto free_buf; |
| 1495 | |
| 1496 | if (copy_to_user(keys + cp * map->key_size, key, |
| 1497 | map->key_size)) { |
| 1498 | err = -EFAULT; |
| 1499 | goto free_buf; |
| 1500 | } |
| 1501 | if (copy_to_user(values + cp * value_size, value, value_size)) { |
| 1502 | err = -EFAULT; |
| 1503 | goto free_buf; |
| 1504 | } |
| 1505 | |
| 1506 | if (!prev_key) |
| 1507 | prev_key = buf_prevkey; |
| 1508 | |
| 1509 | swap(prev_key, key); |
| 1510 | retry = MAP_LOOKUP_RETRIES; |
| 1511 | cp++; |
| 1512 | } |
| 1513 | |
| 1514 | if (err == -EFAULT) |
| 1515 | goto free_buf; |
| 1516 | |
| 1517 | if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) || |
| 1518 | (cp && copy_to_user(uobatch, prev_key, map->key_size)))) |
| 1519 | err = -EFAULT; |
| 1520 | |
| 1521 | free_buf: |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1522 | kvfree(buf_prevkey); |
Stanislav Fomichev | f0dce1d | 2021-08-18 16:52:15 -0700 | [diff] [blame] | 1523 | kvfree(buf); |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 1524 | return err; |
| 1525 | } |
| 1526 | |
Denis Salopek | 3e87f19 | 2021-05-11 23:00:04 +0200 | [diff] [blame] | 1527 | #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1528 | |
| 1529 | static int map_lookup_and_delete_elem(union bpf_attr *attr) |
| 1530 | { |
| 1531 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 1532 | void __user *uvalue = u64_to_user_ptr(attr->value); |
| 1533 | int ufd = attr->map_fd; |
| 1534 | struct bpf_map *map; |
Alexei Starovoitov | 540fefc | 2018-10-19 13:52:38 -0700 | [diff] [blame] | 1535 | void *key, *value; |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1536 | u32 value_size; |
| 1537 | struct fd f; |
| 1538 | int err; |
| 1539 | |
| 1540 | if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM)) |
| 1541 | return -EINVAL; |
| 1542 | |
Denis Salopek | 3e87f19 | 2021-05-11 23:00:04 +0200 | [diff] [blame] | 1543 | if (attr->flags & ~BPF_F_LOCK) |
| 1544 | return -EINVAL; |
| 1545 | |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1546 | f = fdget(ufd); |
| 1547 | map = __bpf_map_get(f); |
| 1548 | if (IS_ERR(map)) |
| 1549 | return PTR_ERR(map); |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 1550 | bpf_map_write_active_inc(map); |
Anton Protopopov | 1ea0f91 | 2020-05-27 18:56:59 +0000 | [diff] [blame] | 1551 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) || |
| 1552 | !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1553 | err = -EPERM; |
| 1554 | goto err_put; |
| 1555 | } |
| 1556 | |
Denis Salopek | 3e87f19 | 2021-05-11 23:00:04 +0200 | [diff] [blame] | 1557 | if (attr->flags && |
| 1558 | (map->map_type == BPF_MAP_TYPE_QUEUE || |
| 1559 | map->map_type == BPF_MAP_TYPE_STACK)) { |
| 1560 | err = -EINVAL; |
| 1561 | goto err_put; |
| 1562 | } |
| 1563 | |
| 1564 | if ((attr->flags & BPF_F_LOCK) && |
| 1565 | !map_value_has_spin_lock(map)) { |
| 1566 | err = -EINVAL; |
| 1567 | goto err_put; |
| 1568 | } |
| 1569 | |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1570 | key = __bpf_copy_key(ukey, map->key_size); |
| 1571 | if (IS_ERR(key)) { |
| 1572 | err = PTR_ERR(key); |
| 1573 | goto err_put; |
| 1574 | } |
| 1575 | |
Denis Salopek | 3e87f19 | 2021-05-11 23:00:04 +0200 | [diff] [blame] | 1576 | value_size = bpf_map_value_size(map); |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1577 | |
| 1578 | err = -ENOMEM; |
Stanislav Fomichev | f0dce1d | 2021-08-18 16:52:15 -0700 | [diff] [blame] | 1579 | value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1580 | if (!value) |
| 1581 | goto free_key; |
| 1582 | |
Denis Salopek | 3e87f19 | 2021-05-11 23:00:04 +0200 | [diff] [blame] | 1583 | err = -ENOTSUPP; |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1584 | if (map->map_type == BPF_MAP_TYPE_QUEUE || |
| 1585 | map->map_type == BPF_MAP_TYPE_STACK) { |
| 1586 | err = map->ops->map_pop_elem(map, value); |
Denis Salopek | 3e87f19 | 2021-05-11 23:00:04 +0200 | [diff] [blame] | 1587 | } else if (map->map_type == BPF_MAP_TYPE_HASH || |
| 1588 | map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 1589 | map->map_type == BPF_MAP_TYPE_LRU_HASH || |
| 1590 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
| 1591 | if (!bpf_map_is_dev_bound(map)) { |
| 1592 | bpf_disable_instrumentation(); |
| 1593 | rcu_read_lock(); |
| 1594 | err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags); |
| 1595 | rcu_read_unlock(); |
| 1596 | bpf_enable_instrumentation(); |
| 1597 | } |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | if (err) |
| 1601 | goto free_value; |
| 1602 | |
Wei Yongjun | 7f64546 | 2020-04-30 08:18:51 +0000 | [diff] [blame] | 1603 | if (copy_to_user(uvalue, value, value_size) != 0) { |
| 1604 | err = -EFAULT; |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1605 | goto free_value; |
Wei Yongjun | 7f64546 | 2020-04-30 08:18:51 +0000 | [diff] [blame] | 1606 | } |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1607 | |
| 1608 | err = 0; |
| 1609 | |
| 1610 | free_value: |
Stanislav Fomichev | f0dce1d | 2021-08-18 16:52:15 -0700 | [diff] [blame] | 1611 | kvfree(value); |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1612 | free_key: |
Stanislav Fomichev | 44779a4b | 2021-08-18 16:52:16 -0700 | [diff] [blame] | 1613 | kvfree(key); |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1614 | err_put: |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 1615 | bpf_map_write_active_dec(map); |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1616 | fdput(f); |
| 1617 | return err; |
| 1618 | } |
| 1619 | |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1620 | #define BPF_MAP_FREEZE_LAST_FIELD map_fd |
| 1621 | |
| 1622 | static int map_freeze(const union bpf_attr *attr) |
| 1623 | { |
| 1624 | int err = 0, ufd = attr->map_fd; |
| 1625 | struct bpf_map *map; |
| 1626 | struct fd f; |
| 1627 | |
| 1628 | if (CHECK_ATTR(BPF_MAP_FREEZE)) |
| 1629 | return -EINVAL; |
| 1630 | |
| 1631 | f = fdget(ufd); |
| 1632 | map = __bpf_map_get(f); |
| 1633 | if (IS_ERR(map)) |
| 1634 | return PTR_ERR(map); |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 1635 | |
Alexei Starovoitov | 6813466 | 2021-07-14 17:54:10 -0700 | [diff] [blame] | 1636 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS || |
| 1637 | map_value_has_timer(map)) { |
Martin KaFai Lau | 849b4d9 | 2020-03-04 17:34:54 -0800 | [diff] [blame] | 1638 | fdput(f); |
| 1639 | return -ENOTSUPP; |
| 1640 | } |
| 1641 | |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 1642 | mutex_lock(&map->freeze_mutex); |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 1643 | if (bpf_map_write_active(map)) { |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 1644 | err = -EBUSY; |
| 1645 | goto err_put; |
| 1646 | } |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1647 | if (READ_ONCE(map->frozen)) { |
| 1648 | err = -EBUSY; |
| 1649 | goto err_put; |
| 1650 | } |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 1651 | if (!bpf_capable()) { |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1652 | err = -EPERM; |
| 1653 | goto err_put; |
| 1654 | } |
| 1655 | |
| 1656 | WRITE_ONCE(map->frozen, true); |
| 1657 | err_put: |
Andrii Nakryiko | fc97022 | 2019-11-17 09:28:04 -0800 | [diff] [blame] | 1658 | mutex_unlock(&map->freeze_mutex); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1659 | fdput(f); |
| 1660 | return err; |
| 1661 | } |
| 1662 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 1663 | static const struct bpf_prog_ops * const bpf_prog_types[] = { |
Alexei Starovoitov | 91cc1a9 | 2019-11-14 10:57:15 -0800 | [diff] [blame] | 1664 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 1665 | [_id] = & _name ## _prog_ops, |
| 1666 | #define BPF_MAP_TYPE(_id, _ops) |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 1667 | #define BPF_LINK_TYPE(_id, _name) |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 1668 | #include <linux/bpf_types.h> |
| 1669 | #undef BPF_PROG_TYPE |
| 1670 | #undef BPF_MAP_TYPE |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 1671 | #undef BPF_LINK_TYPE |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 1672 | }; |
| 1673 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1674 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
| 1675 | { |
Daniel Borkmann | d0f1a45 | 2018-05-04 02:13:57 +0200 | [diff] [blame] | 1676 | const struct bpf_prog_ops *ops; |
| 1677 | |
| 1678 | if (type >= ARRAY_SIZE(bpf_prog_types)) |
| 1679 | return -EINVAL; |
| 1680 | type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types)); |
| 1681 | ops = bpf_prog_types[type]; |
| 1682 | if (!ops) |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 1683 | return -EINVAL; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1684 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1685 | if (!bpf_prog_is_dev_bound(prog->aux)) |
Daniel Borkmann | d0f1a45 | 2018-05-04 02:13:57 +0200 | [diff] [blame] | 1686 | prog->aux->ops = ops; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1687 | else |
| 1688 | prog->aux->ops = &bpf_offload_prog_ops; |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 1689 | prog->type = type; |
| 1690 | return 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1691 | } |
| 1692 | |
Daniel Borkmann | bae141f | 2019-12-06 22:49:34 +0100 | [diff] [blame] | 1693 | enum bpf_audit { |
| 1694 | BPF_AUDIT_LOAD, |
| 1695 | BPF_AUDIT_UNLOAD, |
| 1696 | BPF_AUDIT_MAX, |
| 1697 | }; |
| 1698 | |
| 1699 | static const char * const bpf_audit_str[BPF_AUDIT_MAX] = { |
| 1700 | [BPF_AUDIT_LOAD] = "LOAD", |
| 1701 | [BPF_AUDIT_UNLOAD] = "UNLOAD", |
| 1702 | }; |
| 1703 | |
| 1704 | static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op) |
| 1705 | { |
| 1706 | struct audit_context *ctx = NULL; |
| 1707 | struct audit_buffer *ab; |
| 1708 | |
| 1709 | if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX)) |
| 1710 | return; |
| 1711 | if (audit_enabled == AUDIT_OFF) |
| 1712 | return; |
| 1713 | if (op == BPF_AUDIT_LOAD) |
| 1714 | ctx = audit_context(); |
| 1715 | ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF); |
| 1716 | if (unlikely(!ab)) |
| 1717 | return; |
| 1718 | audit_log_format(ab, "prog-id=%u op=%s", |
| 1719 | prog->aux->id, bpf_audit_str[op]); |
| 1720 | audit_log_end(ab); |
| 1721 | } |
| 1722 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1723 | static int bpf_prog_alloc_id(struct bpf_prog *prog) |
| 1724 | { |
| 1725 | int id; |
| 1726 | |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 1727 | idr_preload(GFP_KERNEL); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1728 | spin_lock_bh(&prog_idr_lock); |
| 1729 | id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); |
| 1730 | if (id > 0) |
| 1731 | prog->aux->id = id; |
| 1732 | spin_unlock_bh(&prog_idr_lock); |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 1733 | idr_preload_end(); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1734 | |
| 1735 | /* id is in [1, INT_MAX) */ |
| 1736 | if (WARN_ON_ONCE(!id)) |
| 1737 | return -ENOSPC; |
| 1738 | |
| 1739 | return id > 0 ? 0 : id; |
| 1740 | } |
| 1741 | |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1742 | 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] | 1743 | { |
Alexei Starovoitov | d809e13 | 2021-07-14 17:54:07 -0700 | [diff] [blame] | 1744 | unsigned long flags; |
| 1745 | |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1746 | /* cBPF to eBPF migrations are currently not in the idr store. |
| 1747 | * Offloaded programs are removed from the store when their device |
| 1748 | * disappears - even if someone grabs an fd to them they are unusable, |
| 1749 | * simply waiting for refcnt to drop to be freed. |
| 1750 | */ |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1751 | if (!prog->aux->id) |
| 1752 | return; |
| 1753 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1754 | if (do_idr_lock) |
Alexei Starovoitov | d809e13 | 2021-07-14 17:54:07 -0700 | [diff] [blame] | 1755 | spin_lock_irqsave(&prog_idr_lock, flags); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1756 | else |
| 1757 | __acquire(&prog_idr_lock); |
| 1758 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1759 | idr_remove(&prog_idr, prog->aux->id); |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1760 | prog->aux->id = 0; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1761 | |
| 1762 | if (do_idr_lock) |
Alexei Starovoitov | d809e13 | 2021-07-14 17:54:07 -0700 | [diff] [blame] | 1763 | spin_unlock_irqrestore(&prog_idr_lock, flags); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1764 | else |
| 1765 | __release(&prog_idr_lock); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1766 | } |
| 1767 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1768 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 1769 | { |
| 1770 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); |
| 1771 | |
Daniel Borkmann | 3b4d9eb | 2019-10-22 23:30:38 +0200 | [diff] [blame] | 1772 | kvfree(aux->func_info); |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 1773 | kfree(aux->func_info_aux); |
Roman Gushchin | 3ac1f01 | 2020-12-01 13:58:59 -0800 | [diff] [blame] | 1774 | free_uid(aux->user); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1775 | security_bpf_prog_free(aux); |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 1776 | bpf_prog_free(aux->prog); |
| 1777 | } |
| 1778 | |
Daniel Borkmann | cd7455f | 2019-10-22 15:57:23 +0200 | [diff] [blame] | 1779 | static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred) |
| 1780 | { |
| 1781 | bpf_prog_kallsyms_del_all(prog); |
| 1782 | btf_put(prog->aux->btf); |
Martin KaFai Lau | e16301f | 2021-03-24 18:51:30 -0700 | [diff] [blame] | 1783 | kvfree(prog->aux->jited_linfo); |
| 1784 | kvfree(prog->aux->linfo); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1785 | kfree(prog->aux->kfunc_tab); |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 1786 | if (prog->aux->attach_btf) |
| 1787 | btf_put(prog->aux->attach_btf); |
Daniel Borkmann | cd7455f | 2019-10-22 15:57:23 +0200 | [diff] [blame] | 1788 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 1789 | if (deferred) { |
| 1790 | if (prog->aux->sleepable) |
| 1791 | call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu); |
| 1792 | else |
| 1793 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
| 1794 | } else { |
Daniel Borkmann | cd7455f | 2019-10-22 15:57:23 +0200 | [diff] [blame] | 1795 | __bpf_prog_put_rcu(&prog->aux->rcu); |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 1796 | } |
Daniel Borkmann | cd7455f | 2019-10-22 15:57:23 +0200 | [diff] [blame] | 1797 | } |
| 1798 | |
Alexei Starovoitov | d809e13 | 2021-07-14 17:54:07 -0700 | [diff] [blame] | 1799 | static void bpf_prog_put_deferred(struct work_struct *work) |
| 1800 | { |
| 1801 | struct bpf_prog_aux *aux; |
| 1802 | struct bpf_prog *prog; |
| 1803 | |
| 1804 | aux = container_of(work, struct bpf_prog_aux, work); |
| 1805 | prog = aux->prog; |
| 1806 | perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0); |
| 1807 | bpf_audit_prog(prog, BPF_AUDIT_UNLOAD); |
| 1808 | __bpf_prog_put_noref(prog, true); |
| 1809 | } |
| 1810 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1811 | 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] | 1812 | { |
Alexei Starovoitov | d809e13 | 2021-07-14 17:54:07 -0700 | [diff] [blame] | 1813 | struct bpf_prog_aux *aux = prog->aux; |
| 1814 | |
| 1815 | if (atomic64_dec_and_test(&aux->refcnt)) { |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 1816 | /* bpf_prog_free_id() must be called first */ |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1817 | bpf_prog_free_id(prog, do_idr_lock); |
Alexei Starovoitov | d809e13 | 2021-07-14 17:54:07 -0700 | [diff] [blame] | 1818 | |
| 1819 | if (in_irq() || irqs_disabled()) { |
| 1820 | INIT_WORK(&aux->work, bpf_prog_put_deferred); |
| 1821 | schedule_work(&aux->work); |
| 1822 | } else { |
| 1823 | bpf_prog_put_deferred(&aux->work); |
| 1824 | } |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1825 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1826 | } |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1827 | |
| 1828 | void bpf_prog_put(struct bpf_prog *prog) |
| 1829 | { |
| 1830 | __bpf_prog_put(prog, true); |
| 1831 | } |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 1832 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1833 | |
| 1834 | static int bpf_prog_release(struct inode *inode, struct file *filp) |
| 1835 | { |
| 1836 | struct bpf_prog *prog = filp->private_data; |
| 1837 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1838 | bpf_prog_put(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1839 | return 0; |
| 1840 | } |
| 1841 | |
Eric Dumazet | 61a0aba | 2021-10-26 14:41:33 -0700 | [diff] [blame] | 1842 | struct bpf_prog_kstats { |
| 1843 | u64 nsecs; |
| 1844 | u64 cnt; |
| 1845 | u64 misses; |
| 1846 | }; |
| 1847 | |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1848 | static void bpf_prog_get_stats(const struct bpf_prog *prog, |
Eric Dumazet | 61a0aba | 2021-10-26 14:41:33 -0700 | [diff] [blame] | 1849 | struct bpf_prog_kstats *stats) |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1850 | { |
Alexei Starovoitov | 9ed9e9b | 2021-02-09 19:36:31 -0800 | [diff] [blame] | 1851 | u64 nsecs = 0, cnt = 0, misses = 0; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1852 | int cpu; |
| 1853 | |
| 1854 | for_each_possible_cpu(cpu) { |
| 1855 | const struct bpf_prog_stats *st; |
| 1856 | unsigned int start; |
Alexei Starovoitov | 9ed9e9b | 2021-02-09 19:36:31 -0800 | [diff] [blame] | 1857 | u64 tnsecs, tcnt, tmisses; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1858 | |
Alexei Starovoitov | 700d479 | 2021-02-09 19:36:26 -0800 | [diff] [blame] | 1859 | st = per_cpu_ptr(prog->stats, cpu); |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1860 | do { |
| 1861 | start = u64_stats_fetch_begin_irq(&st->syncp); |
Eric Dumazet | 61a0aba | 2021-10-26 14:41:33 -0700 | [diff] [blame] | 1862 | tnsecs = u64_stats_read(&st->nsecs); |
| 1863 | tcnt = u64_stats_read(&st->cnt); |
| 1864 | tmisses = u64_stats_read(&st->misses); |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1865 | } while (u64_stats_fetch_retry_irq(&st->syncp, start)); |
| 1866 | nsecs += tnsecs; |
| 1867 | cnt += tcnt; |
Alexei Starovoitov | 9ed9e9b | 2021-02-09 19:36:31 -0800 | [diff] [blame] | 1868 | misses += tmisses; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1869 | } |
| 1870 | stats->nsecs = nsecs; |
| 1871 | stats->cnt = cnt; |
Alexei Starovoitov | 9ed9e9b | 2021-02-09 19:36:31 -0800 | [diff] [blame] | 1872 | stats->misses = misses; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1873 | } |
| 1874 | |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1875 | #ifdef CONFIG_PROC_FS |
| 1876 | static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) |
| 1877 | { |
| 1878 | const struct bpf_prog *prog = filp->private_data; |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1879 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
Eric Dumazet | 61a0aba | 2021-10-26 14:41:33 -0700 | [diff] [blame] | 1880 | struct bpf_prog_kstats stats; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1881 | |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1882 | bpf_prog_get_stats(prog, &stats); |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1883 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1884 | seq_printf(m, |
| 1885 | "prog_type:\t%u\n" |
| 1886 | "prog_jited:\t%u\n" |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1887 | "prog_tag:\t%s\n" |
Daniel Borkmann | 4316b40 | 2018-06-02 23:06:34 +0200 | [diff] [blame] | 1888 | "memlock:\t%llu\n" |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1889 | "prog_id:\t%u\n" |
| 1890 | "run_time_ns:\t%llu\n" |
Alexei Starovoitov | 9ed9e9b | 2021-02-09 19:36:31 -0800 | [diff] [blame] | 1891 | "run_cnt:\t%llu\n" |
Dave Marchevsky | aba64c7 | 2021-10-20 00:48:17 -0700 | [diff] [blame] | 1892 | "recursion_misses:\t%llu\n" |
| 1893 | "verified_insns:\t%u\n", |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1894 | prog->type, |
| 1895 | prog->jited, |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1896 | prog_tag, |
Daniel Borkmann | 4316b40 | 2018-06-02 23:06:34 +0200 | [diff] [blame] | 1897 | prog->pages * 1ULL << PAGE_SHIFT, |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1898 | prog->aux->id, |
| 1899 | stats.nsecs, |
Alexei Starovoitov | 9ed9e9b | 2021-02-09 19:36:31 -0800 | [diff] [blame] | 1900 | stats.cnt, |
Dave Marchevsky | aba64c7 | 2021-10-20 00:48:17 -0700 | [diff] [blame] | 1901 | stats.misses, |
| 1902 | prog->aux->verified_insns); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1903 | } |
| 1904 | #endif |
| 1905 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1906 | const struct file_operations bpf_prog_fops = { |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1907 | #ifdef CONFIG_PROC_FS |
| 1908 | .show_fdinfo = bpf_prog_show_fdinfo, |
| 1909 | #endif |
| 1910 | .release = bpf_prog_release, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1911 | .read = bpf_dummy_read, |
| 1912 | .write = bpf_dummy_write, |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1913 | }; |
| 1914 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1915 | int bpf_prog_new_fd(struct bpf_prog *prog) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1916 | { |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1917 | int ret; |
| 1918 | |
| 1919 | ret = security_bpf_prog(prog); |
| 1920 | if (ret < 0) |
| 1921 | return ret; |
| 1922 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1923 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
| 1924 | O_RDWR | O_CLOEXEC); |
| 1925 | } |
| 1926 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1927 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1928 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1929 | if (!f.file) |
| 1930 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1931 | if (f.file->f_op != &bpf_prog_fops) { |
| 1932 | fdput(f); |
| 1933 | return ERR_PTR(-EINVAL); |
| 1934 | } |
| 1935 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1936 | return f.file->private_data; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1937 | } |
| 1938 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1939 | void bpf_prog_add(struct bpf_prog *prog, int i) |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1940 | { |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1941 | atomic64_add(i, &prog->aux->refcnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1942 | } |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1943 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
| 1944 | |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 1945 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
| 1946 | { |
| 1947 | /* Only to be used for undoing previous bpf_prog_add() in some |
| 1948 | * error path. We still know that another entity in our call |
| 1949 | * path holds a reference to the program, thus atomic_sub() can |
| 1950 | * be safely used in such cases! |
| 1951 | */ |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1952 | WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0); |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 1953 | } |
| 1954 | EXPORT_SYMBOL_GPL(bpf_prog_sub); |
| 1955 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1956 | void bpf_prog_inc(struct bpf_prog *prog) |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1957 | { |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1958 | atomic64_inc(&prog->aux->refcnt); |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1959 | } |
Daniel Borkmann | 97bc402 | 2016-11-19 01:45:00 +0100 | [diff] [blame] | 1960 | EXPORT_SYMBOL_GPL(bpf_prog_inc); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1961 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1962 | /* prog_idr_lock should have been held */ |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1963 | 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] | 1964 | { |
| 1965 | int refold; |
| 1966 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 1967 | refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1968 | |
| 1969 | if (!refold) |
| 1970 | return ERR_PTR(-ENOENT); |
| 1971 | |
| 1972 | return prog; |
| 1973 | } |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1974 | EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1975 | |
Al Viro | 040ee69 | 2017-12-02 20:20:38 -0500 | [diff] [blame] | 1976 | bool bpf_prog_get_ok(struct bpf_prog *prog, |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1977 | enum bpf_prog_type *attach_type, bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1978 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1979 | /* not an attachment, just a refcount inc, always allow */ |
| 1980 | if (!attach_type) |
| 1981 | return true; |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1982 | |
| 1983 | if (prog->type != *attach_type) |
| 1984 | return false; |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1985 | if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1986 | return false; |
| 1987 | |
| 1988 | return true; |
| 1989 | } |
| 1990 | |
| 1991 | 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] | 1992 | bool attach_drv) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1993 | { |
| 1994 | struct fd f = fdget(ufd); |
| 1995 | struct bpf_prog *prog; |
| 1996 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1997 | prog = ____bpf_prog_get(f); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1998 | if (IS_ERR(prog)) |
| 1999 | return prog; |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 2000 | if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 2001 | prog = ERR_PTR(-EINVAL); |
| 2002 | goto out; |
| 2003 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2004 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 2005 | bpf_prog_inc(prog); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 2006 | out: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2007 | fdput(f); |
| 2008 | return prog; |
| 2009 | } |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 2010 | |
| 2011 | struct bpf_prog *bpf_prog_get(u32 ufd) |
| 2012 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 2013 | return __bpf_prog_get(ufd, NULL, false); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 2014 | } |
| 2015 | |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 2016 | 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] | 2017 | bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 2018 | { |
Alexei Starovoitov | 4d220ed | 2018-04-28 19:56:37 -0700 | [diff] [blame] | 2019 | return __bpf_prog_get(ufd, &type, attach_drv); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 2020 | } |
Jakub Kicinski | 6c8dfe2 | 2017-11-03 13:56:21 -0700 | [diff] [blame] | 2021 | EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 2022 | |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 2023 | /* Initially all BPF programs could be loaded w/o specifying |
| 2024 | * expected_attach_type. Later for some of them specifying expected_attach_type |
| 2025 | * at load time became required so that program could be validated properly. |
| 2026 | * Programs of types that are allowed to be loaded both w/ and w/o (for |
| 2027 | * backward compatibility) expected_attach_type, should have the default attach |
| 2028 | * type assigned to expected_attach_type for the latter case, so that it can be |
| 2029 | * validated later at attach time. |
| 2030 | * |
| 2031 | * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if |
| 2032 | * prog type requires it but has some attach types that have to be backward |
| 2033 | * compatible. |
| 2034 | */ |
| 2035 | static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) |
| 2036 | { |
| 2037 | switch (attr->prog_type) { |
| 2038 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 2039 | /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't |
| 2040 | * exist so checking for non-zero is the way to go here. |
| 2041 | */ |
| 2042 | if (!attr->expected_attach_type) |
| 2043 | attr->expected_attach_type = |
| 2044 | BPF_CGROUP_INET_SOCK_CREATE; |
| 2045 | break; |
Kuniyuki Iwashima | d5e4dda | 2021-06-12 21:32:22 +0900 | [diff] [blame] | 2046 | case BPF_PROG_TYPE_SK_REUSEPORT: |
| 2047 | if (!attr->expected_attach_type) |
| 2048 | attr->expected_attach_type = |
| 2049 | BPF_SK_REUSEPORT_SELECT; |
| 2050 | break; |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 2051 | } |
| 2052 | } |
| 2053 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2054 | static int |
Alexei Starovoitov | ccfe29e | 2019-10-15 20:24:58 -0700 | [diff] [blame] | 2055 | bpf_prog_load_check_attach(enum bpf_prog_type prog_type, |
| 2056 | enum bpf_attach_type expected_attach_type, |
Andrii Nakryiko | 290248a | 2020-12-03 12:46:30 -0800 | [diff] [blame] | 2057 | struct btf *attach_btf, u32 btf_id, |
| 2058 | struct bpf_prog *dst_prog) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2059 | { |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 2060 | if (btf_id) { |
Alexei Starovoitov | c108e3c | 2019-10-17 23:09:33 -0700 | [diff] [blame] | 2061 | if (btf_id > BTF_MAX_TYPE) |
| 2062 | return -EINVAL; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 2063 | |
Andrii Nakryiko | 290248a | 2020-12-03 12:46:30 -0800 | [diff] [blame] | 2064 | if (!attach_btf && !dst_prog) |
| 2065 | return -EINVAL; |
| 2066 | |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 2067 | switch (prog_type) { |
| 2068 | case BPF_PROG_TYPE_TRACING: |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 2069 | case BPF_PROG_TYPE_LSM: |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 2070 | case BPF_PROG_TYPE_STRUCT_OPS: |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 2071 | case BPF_PROG_TYPE_EXT: |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 2072 | break; |
| 2073 | default: |
Alexei Starovoitov | c108e3c | 2019-10-17 23:09:33 -0700 | [diff] [blame] | 2074 | return -EINVAL; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 2075 | } |
Alexei Starovoitov | c108e3c | 2019-10-17 23:09:33 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
Andrii Nakryiko | 290248a | 2020-12-03 12:46:30 -0800 | [diff] [blame] | 2078 | if (attach_btf && (!btf_id || dst_prog)) |
| 2079 | return -EINVAL; |
| 2080 | |
| 2081 | if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING && |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 2082 | prog_type != BPF_PROG_TYPE_EXT) |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 2083 | return -EINVAL; |
| 2084 | |
Alexei Starovoitov | c108e3c | 2019-10-17 23:09:33 -0700 | [diff] [blame] | 2085 | switch (prog_type) { |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 2086 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 2087 | switch (expected_attach_type) { |
| 2088 | case BPF_CGROUP_INET_SOCK_CREATE: |
Stanislav Fomichev | f583674 | 2020-07-06 16:01:25 -0700 | [diff] [blame] | 2089 | case BPF_CGROUP_INET_SOCK_RELEASE: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 2090 | case BPF_CGROUP_INET4_POST_BIND: |
| 2091 | case BPF_CGROUP_INET6_POST_BIND: |
| 2092 | return 0; |
| 2093 | default: |
| 2094 | return -EINVAL; |
| 2095 | } |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2096 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 2097 | switch (expected_attach_type) { |
| 2098 | case BPF_CGROUP_INET4_BIND: |
| 2099 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 2100 | case BPF_CGROUP_INET4_CONNECT: |
| 2101 | case BPF_CGROUP_INET6_CONNECT: |
Daniel Borkmann | 1b66d25 | 2020-05-19 00:45:45 +0200 | [diff] [blame] | 2102 | case BPF_CGROUP_INET4_GETPEERNAME: |
| 2103 | case BPF_CGROUP_INET6_GETPEERNAME: |
| 2104 | case BPF_CGROUP_INET4_GETSOCKNAME: |
| 2105 | case BPF_CGROUP_INET6_GETSOCKNAME: |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 2106 | case BPF_CGROUP_UDP4_SENDMSG: |
| 2107 | case BPF_CGROUP_UDP6_SENDMSG: |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 2108 | case BPF_CGROUP_UDP4_RECVMSG: |
| 2109 | case BPF_CGROUP_UDP6_RECVMSG: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2110 | return 0; |
| 2111 | default: |
| 2112 | return -EINVAL; |
| 2113 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 2114 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 2115 | switch (expected_attach_type) { |
| 2116 | case BPF_CGROUP_INET_INGRESS: |
| 2117 | case BPF_CGROUP_INET_EGRESS: |
| 2118 | return 0; |
| 2119 | default: |
| 2120 | return -EINVAL; |
| 2121 | } |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2122 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 2123 | switch (expected_attach_type) { |
| 2124 | case BPF_CGROUP_SETSOCKOPT: |
| 2125 | case BPF_CGROUP_GETSOCKOPT: |
| 2126 | return 0; |
| 2127 | default: |
| 2128 | return -EINVAL; |
| 2129 | } |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 2130 | case BPF_PROG_TYPE_SK_LOOKUP: |
| 2131 | if (expected_attach_type == BPF_SK_LOOKUP) |
| 2132 | return 0; |
| 2133 | return -EINVAL; |
Kuniyuki Iwashima | d5e4dda | 2021-06-12 21:32:22 +0900 | [diff] [blame] | 2134 | case BPF_PROG_TYPE_SK_REUSEPORT: |
| 2135 | switch (expected_attach_type) { |
| 2136 | case BPF_SK_REUSEPORT_SELECT: |
| 2137 | case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE: |
| 2138 | return 0; |
| 2139 | default: |
| 2140 | return -EINVAL; |
| 2141 | } |
Alexei Starovoitov | 79a7f8b | 2021-05-13 17:36:03 -0700 | [diff] [blame] | 2142 | case BPF_PROG_TYPE_SYSCALL: |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 2143 | case BPF_PROG_TYPE_EXT: |
| 2144 | if (expected_attach_type) |
| 2145 | return -EINVAL; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2146 | fallthrough; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2147 | default: |
| 2148 | return 0; |
| 2149 | } |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2150 | } |
| 2151 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2152 | static bool is_net_admin_prog_type(enum bpf_prog_type prog_type) |
| 2153 | { |
| 2154 | switch (prog_type) { |
| 2155 | case BPF_PROG_TYPE_SCHED_CLS: |
| 2156 | case BPF_PROG_TYPE_SCHED_ACT: |
| 2157 | case BPF_PROG_TYPE_XDP: |
| 2158 | case BPF_PROG_TYPE_LWT_IN: |
| 2159 | case BPF_PROG_TYPE_LWT_OUT: |
| 2160 | case BPF_PROG_TYPE_LWT_XMIT: |
| 2161 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
| 2162 | case BPF_PROG_TYPE_SK_SKB: |
| 2163 | case BPF_PROG_TYPE_SK_MSG: |
| 2164 | case BPF_PROG_TYPE_LIRC_MODE2: |
| 2165 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
| 2166 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
| 2167 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 2168 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 2169 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 2170 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
| 2171 | case BPF_PROG_TYPE_SOCK_OPS: |
| 2172 | case BPF_PROG_TYPE_EXT: /* extends any prog */ |
| 2173 | return true; |
| 2174 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 2175 | /* always unpriv */ |
| 2176 | case BPF_PROG_TYPE_SK_REUSEPORT: |
| 2177 | /* equivalent to SOCKET_FILTER. need CAP_BPF only */ |
| 2178 | default: |
| 2179 | return false; |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) |
| 2184 | { |
| 2185 | switch (prog_type) { |
| 2186 | case BPF_PROG_TYPE_KPROBE: |
| 2187 | case BPF_PROG_TYPE_TRACEPOINT: |
| 2188 | case BPF_PROG_TYPE_PERF_EVENT: |
| 2189 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 2190 | case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: |
| 2191 | case BPF_PROG_TYPE_TRACING: |
| 2192 | case BPF_PROG_TYPE_LSM: |
| 2193 | case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */ |
| 2194 | case BPF_PROG_TYPE_EXT: /* extends any prog */ |
| 2195 | return true; |
| 2196 | default: |
| 2197 | return false; |
| 2198 | } |
| 2199 | } |
| 2200 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2201 | /* last field in 'union bpf_attr' used by this command */ |
Alexei Starovoitov | fbd94c7 | 2021-12-01 10:10:28 -0800 | [diff] [blame] | 2202 | #define BPF_PROG_LOAD_LAST_FIELD core_relo_rec_size |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2203 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 2204 | static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2205 | { |
| 2206 | enum bpf_prog_type type = attr->prog_type; |
Andrii Nakryiko | 290248a | 2020-12-03 12:46:30 -0800 | [diff] [blame] | 2207 | struct bpf_prog *prog, *dst_prog = NULL; |
| 2208 | struct btf *attach_btf = NULL; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2209 | int err; |
| 2210 | char license[128]; |
| 2211 | bool is_gpl; |
| 2212 | |
| 2213 | if (CHECK_ATTR(BPF_PROG_LOAD)) |
| 2214 | return -EINVAL; |
| 2215 | |
Jiong Wang | c240eff | 2019-05-24 23:25:16 +0100 | [diff] [blame] | 2216 | if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | |
| 2217 | BPF_F_ANY_ALIGNMENT | |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 2218 | BPF_F_TEST_STATE_FREQ | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 2219 | BPF_F_SLEEPABLE | |
Jiong Wang | c240eff | 2019-05-24 23:25:16 +0100 | [diff] [blame] | 2220 | BPF_F_TEST_RND_HI32)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2221 | return -EINVAL; |
| 2222 | |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 2223 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && |
| 2224 | (attr->prog_flags & BPF_F_ANY_ALIGNMENT) && |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2225 | !bpf_capable()) |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 2226 | return -EPERM; |
| 2227 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2228 | /* copy eBPF program license from user space */ |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 2229 | if (strncpy_from_bpfptr(license, |
| 2230 | make_bpfptr(attr->license, uattr.is_kernel), |
| 2231 | sizeof(license) - 1) < 0) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2232 | return -EFAULT; |
| 2233 | license[sizeof(license) - 1] = 0; |
| 2234 | |
| 2235 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
| 2236 | is_gpl = license_is_gpl_compatible(license); |
| 2237 | |
Alexei Starovoitov | c04c0d2 | 2019-04-01 21:27:45 -0700 | [diff] [blame] | 2238 | if (attr->insn_cnt == 0 || |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2239 | attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) |
Daniel Borkmann | ef0915c | 2016-12-07 01:15:44 +0100 | [diff] [blame] | 2240 | return -E2BIG; |
Chenbo Feng | 80b7d81 | 2017-05-31 18:16:00 -0700 | [diff] [blame] | 2241 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && |
| 2242 | type != BPF_PROG_TYPE_CGROUP_SKB && |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2243 | !bpf_capable()) |
| 2244 | return -EPERM; |
| 2245 | |
Maciej Żenczykowski | b338cb9 | 2020-06-20 14:26:16 -0700 | [diff] [blame] | 2246 | if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN)) |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2247 | return -EPERM; |
| 2248 | if (is_perfmon_prog_type(type) && !perfmon_capable()) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2249 | return -EPERM; |
| 2250 | |
Andrii Nakryiko | 290248a | 2020-12-03 12:46:30 -0800 | [diff] [blame] | 2251 | /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog |
| 2252 | * or btf, we need to check which one it is |
| 2253 | */ |
| 2254 | if (attr->attach_prog_fd) { |
| 2255 | dst_prog = bpf_prog_get(attr->attach_prog_fd); |
| 2256 | if (IS_ERR(dst_prog)) { |
| 2257 | dst_prog = NULL; |
| 2258 | attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd); |
| 2259 | if (IS_ERR(attach_btf)) |
| 2260 | return -EINVAL; |
| 2261 | if (!btf_is_kernel(attach_btf)) { |
Andrii Nakryiko | 8bdd8e2 | 2020-12-07 22:43:26 -0800 | [diff] [blame] | 2262 | /* attaching through specifying bpf_prog's BTF |
| 2263 | * objects directly might be supported eventually |
| 2264 | */ |
Andrii Nakryiko | 290248a | 2020-12-03 12:46:30 -0800 | [diff] [blame] | 2265 | btf_put(attach_btf); |
Andrii Nakryiko | 8bdd8e2 | 2020-12-07 22:43:26 -0800 | [diff] [blame] | 2266 | return -ENOTSUPP; |
Andrii Nakryiko | 290248a | 2020-12-03 12:46:30 -0800 | [diff] [blame] | 2267 | } |
| 2268 | } |
| 2269 | } else if (attr->attach_btf_id) { |
| 2270 | /* fall back to vmlinux BTF, if BTF type ID is specified */ |
| 2271 | attach_btf = bpf_get_btf_vmlinux(); |
| 2272 | if (IS_ERR(attach_btf)) |
| 2273 | return PTR_ERR(attach_btf); |
| 2274 | if (!attach_btf) |
| 2275 | return -EINVAL; |
| 2276 | btf_get(attach_btf); |
| 2277 | } |
| 2278 | |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 2279 | bpf_prog_load_fixup_attach_type(attr); |
Alexei Starovoitov | ccfe29e | 2019-10-15 20:24:58 -0700 | [diff] [blame] | 2280 | if (bpf_prog_load_check_attach(type, attr->expected_attach_type, |
Andrii Nakryiko | 290248a | 2020-12-03 12:46:30 -0800 | [diff] [blame] | 2281 | attach_btf, attr->attach_btf_id, |
| 2282 | dst_prog)) { |
| 2283 | if (dst_prog) |
| 2284 | bpf_prog_put(dst_prog); |
| 2285 | if (attach_btf) |
| 2286 | btf_put(attach_btf); |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2287 | return -EINVAL; |
Andrii Nakryiko | 290248a | 2020-12-03 12:46:30 -0800 | [diff] [blame] | 2288 | } |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2289 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2290 | /* plain bpf_prog allocation */ |
| 2291 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); |
Andrii Nakryiko | 290248a | 2020-12-03 12:46:30 -0800 | [diff] [blame] | 2292 | if (!prog) { |
| 2293 | if (dst_prog) |
| 2294 | bpf_prog_put(dst_prog); |
| 2295 | if (attach_btf) |
| 2296 | btf_put(attach_btf); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2297 | return -ENOMEM; |
Andrii Nakryiko | 290248a | 2020-12-03 12:46:30 -0800 | [diff] [blame] | 2298 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2299 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2300 | prog->expected_attach_type = attr->expected_attach_type; |
Andrii Nakryiko | 290248a | 2020-12-03 12:46:30 -0800 | [diff] [blame] | 2301 | prog->aux->attach_btf = attach_btf; |
Alexei Starovoitov | ccfe29e | 2019-10-15 20:24:58 -0700 | [diff] [blame] | 2302 | prog->aux->attach_btf_id = attr->attach_btf_id; |
Andrii Nakryiko | 290248a | 2020-12-03 12:46:30 -0800 | [diff] [blame] | 2303 | prog->aux->dst_prog = dst_prog; |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 2304 | prog->aux->offload_requested = !!attr->prog_ifindex; |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 2305 | prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE; |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 2306 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 2307 | err = security_bpf_prog_alloc(prog->aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 2308 | if (err) |
Roman Gushchin | 3ac1f01 | 2020-12-01 13:58:59 -0800 | [diff] [blame] | 2309 | goto free_prog; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 2310 | |
Roman Gushchin | 3ac1f01 | 2020-12-01 13:58:59 -0800 | [diff] [blame] | 2311 | prog->aux->user = get_current_user(); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2312 | prog->len = attr->insn_cnt; |
| 2313 | |
| 2314 | err = -EFAULT; |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 2315 | if (copy_from_bpfptr(prog->insns, |
| 2316 | make_bpfptr(attr->insns, uattr.is_kernel), |
| 2317 | bpf_prog_insn_size(prog)) != 0) |
Roman Gushchin | 3ac1f01 | 2020-12-01 13:58:59 -0800 | [diff] [blame] | 2318 | goto free_prog_sec; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2319 | |
| 2320 | prog->orig_prog = NULL; |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 2321 | prog->jited = 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2322 | |
Andrii Nakryiko | 85192db | 2019-11-17 09:28:03 -0800 | [diff] [blame] | 2323 | atomic64_set(&prog->aux->refcnt, 1); |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 2324 | prog->gpl_compatible = is_gpl ? 1 : 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2325 | |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 2326 | if (bpf_prog_is_dev_bound(prog->aux)) { |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 2327 | err = bpf_prog_offload_init(prog, attr); |
| 2328 | if (err) |
Roman Gushchin | 3ac1f01 | 2020-12-01 13:58:59 -0800 | [diff] [blame] | 2329 | goto free_prog_sec; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 2330 | } |
| 2331 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2332 | /* find program type: socket_filter vs tracing_filter */ |
| 2333 | err = find_prog_type(type, prog); |
| 2334 | if (err < 0) |
Roman Gushchin | 3ac1f01 | 2020-12-01 13:58:59 -0800 | [diff] [blame] | 2335 | goto free_prog_sec; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2336 | |
Jason A. Donenfeld | 9285ec4 | 2019-06-21 22:32:48 +0200 | [diff] [blame] | 2337 | prog->aux->load_time = ktime_get_boottime_ns(); |
Martin KaFai Lau | 8e7ae25 | 2020-03-13 18:02:09 -0700 | [diff] [blame] | 2338 | err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name, |
| 2339 | sizeof(attr->prog_name)); |
| 2340 | if (err < 0) |
Roman Gushchin | 3ac1f01 | 2020-12-01 13:58:59 -0800 | [diff] [blame] | 2341 | goto free_prog_sec; |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 2342 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2343 | /* run eBPF verifier */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 2344 | err = bpf_check(&prog, attr, uattr); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2345 | if (err < 0) |
| 2346 | goto free_used_maps; |
| 2347 | |
Daniel Borkmann | 9facc33 | 2018-06-15 02:30:48 +0200 | [diff] [blame] | 2348 | prog = bpf_prog_select_runtime(prog, &err); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2349 | if (err < 0) |
| 2350 | goto free_used_maps; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2351 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 2352 | err = bpf_prog_alloc_id(prog); |
| 2353 | if (err) |
| 2354 | goto free_used_maps; |
| 2355 | |
Daniel Borkmann | c751798 | 2019-08-23 22:14:23 +0200 | [diff] [blame] | 2356 | /* Upon success of bpf_prog_alloc_id(), the BPF prog is |
| 2357 | * effectively publicly exposed. However, retrieving via |
| 2358 | * bpf_prog_get_fd_by_id() will take another reference, |
| 2359 | * therefore it cannot be gone underneath us. |
| 2360 | * |
| 2361 | * Only for the time /after/ successful bpf_prog_new_fd() |
| 2362 | * and before returning to userspace, we might just hold |
| 2363 | * one reference and any parallel close on that fd could |
| 2364 | * rip everything out. Hence, below notifications must |
| 2365 | * happen before bpf_prog_new_fd(). |
| 2366 | * |
| 2367 | * Also, any failure handling from this point onwards must |
| 2368 | * be using bpf_prog_put() given the program is exposed. |
| 2369 | */ |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 2370 | bpf_prog_kallsyms_add(prog); |
Song Liu | 6ee52e2 | 2019-01-17 08:15:15 -0800 | [diff] [blame] | 2371 | perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); |
Daniel Borkmann | bae141f | 2019-12-06 22:49:34 +0100 | [diff] [blame] | 2372 | bpf_audit_prog(prog, BPF_AUDIT_LOAD); |
Daniel Borkmann | c751798 | 2019-08-23 22:14:23 +0200 | [diff] [blame] | 2373 | |
| 2374 | err = bpf_prog_new_fd(prog); |
| 2375 | if (err < 0) |
| 2376 | bpf_prog_put(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2377 | return err; |
| 2378 | |
| 2379 | free_used_maps: |
Daniel Borkmann | cd7455f | 2019-10-22 15:57:23 +0200 | [diff] [blame] | 2380 | /* In case we have subprogs, we need to wait for a grace |
| 2381 | * period before we can tear down JIT memory since symbols |
| 2382 | * are already exposed under kallsyms. |
| 2383 | */ |
| 2384 | __bpf_prog_put_noref(prog, prog->aux->func_cnt); |
| 2385 | return err; |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 2386 | free_prog_sec: |
Roman Gushchin | 3ac1f01 | 2020-12-01 13:58:59 -0800 | [diff] [blame] | 2387 | free_uid(prog->aux->user); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 2388 | security_bpf_prog_free(prog->aux); |
Roman Gushchin | 3ac1f01 | 2020-12-01 13:58:59 -0800 | [diff] [blame] | 2389 | free_prog: |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 2390 | if (prog->aux->attach_btf) |
| 2391 | btf_put(prog->aux->attach_btf); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2392 | bpf_prog_free(prog); |
| 2393 | return err; |
| 2394 | } |
| 2395 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2396 | #define BPF_OBJ_LAST_FIELD file_flags |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 2397 | |
| 2398 | static int bpf_obj_pin(const union bpf_attr *attr) |
| 2399 | { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2400 | if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 2401 | return -EINVAL; |
| 2402 | |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 2403 | 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] | 2404 | } |
| 2405 | |
| 2406 | static int bpf_obj_get(const union bpf_attr *attr) |
| 2407 | { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2408 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || |
| 2409 | attr->file_flags & ~BPF_OBJ_FLAG_MASK) |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 2410 | return -EINVAL; |
| 2411 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2412 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), |
| 2413 | attr->file_flags); |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 2414 | } |
| 2415 | |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 2416 | void bpf_link_init(struct bpf_link *link, enum bpf_link_type type, |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2417 | const struct bpf_link_ops *ops, struct bpf_prog *prog) |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2418 | { |
| 2419 | atomic64_set(&link->refcnt, 1); |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 2420 | link->type = type; |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2421 | link->id = 0; |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2422 | link->ops = ops; |
| 2423 | link->prog = prog; |
| 2424 | } |
| 2425 | |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2426 | static void bpf_link_free_id(int id) |
| 2427 | { |
| 2428 | if (!id) |
| 2429 | return; |
| 2430 | |
| 2431 | spin_lock_bh(&link_idr_lock); |
| 2432 | idr_remove(&link_idr, id); |
| 2433 | spin_unlock_bh(&link_idr_lock); |
| 2434 | } |
| 2435 | |
Andrii Nakryiko | 9886866 | 2020-03-12 17:21:28 -0700 | [diff] [blame] | 2436 | /* Clean up bpf_link and corresponding anon_inode file and FD. After |
| 2437 | * 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] | 2438 | * anon_inode's release() call. This helper marksbpf_link as |
| 2439 | * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt |
| 2440 | * is not decremented, it's the responsibility of a calling code that failed |
| 2441 | * to complete bpf_link initialization. |
Andrii Nakryiko | 9886866 | 2020-03-12 17:21:28 -0700 | [diff] [blame] | 2442 | */ |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2443 | void bpf_link_cleanup(struct bpf_link_primer *primer) |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2444 | { |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2445 | primer->link->prog = NULL; |
| 2446 | bpf_link_free_id(primer->id); |
| 2447 | fput(primer->file); |
| 2448 | put_unused_fd(primer->fd); |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2451 | void bpf_link_inc(struct bpf_link *link) |
| 2452 | { |
| 2453 | atomic64_inc(&link->refcnt); |
| 2454 | } |
| 2455 | |
| 2456 | /* bpf_link_free is guaranteed to be called from process context */ |
| 2457 | static void bpf_link_free(struct bpf_link *link) |
| 2458 | { |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2459 | bpf_link_free_id(link->id); |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2460 | if (link->prog) { |
| 2461 | /* detach BPF program, clean up used resources */ |
| 2462 | link->ops->release(link); |
| 2463 | bpf_prog_put(link->prog); |
| 2464 | } |
| 2465 | /* free bpf_link and its containing memory */ |
| 2466 | link->ops->dealloc(link); |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | static void bpf_link_put_deferred(struct work_struct *work) |
| 2470 | { |
| 2471 | struct bpf_link *link = container_of(work, struct bpf_link, work); |
| 2472 | |
| 2473 | bpf_link_free(link); |
| 2474 | } |
| 2475 | |
| 2476 | /* bpf_link_put can be called from atomic context, but ensures that resources |
| 2477 | * are freed from process context |
| 2478 | */ |
| 2479 | void bpf_link_put(struct bpf_link *link) |
| 2480 | { |
| 2481 | if (!atomic64_dec_and_test(&link->refcnt)) |
| 2482 | return; |
| 2483 | |
Alexei Starovoitov | f00f2f7 | 2020-09-23 19:10:38 -0700 | [diff] [blame] | 2484 | if (in_atomic()) { |
| 2485 | INIT_WORK(&link->work, bpf_link_put_deferred); |
| 2486 | schedule_work(&link->work); |
| 2487 | } else { |
| 2488 | bpf_link_free(link); |
| 2489 | } |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | static int bpf_link_release(struct inode *inode, struct file *filp) |
| 2493 | { |
| 2494 | struct bpf_link *link = filp->private_data; |
| 2495 | |
| 2496 | bpf_link_put(link); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 2497 | return 0; |
| 2498 | } |
| 2499 | |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2500 | #ifdef CONFIG_PROC_FS |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 2501 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) |
| 2502 | #define BPF_MAP_TYPE(_id, _ops) |
| 2503 | #define BPF_LINK_TYPE(_id, _name) [_id] = #_name, |
| 2504 | static const char *bpf_link_type_strs[] = { |
| 2505 | [BPF_LINK_TYPE_UNSPEC] = "<invalid>", |
| 2506 | #include <linux/bpf_types.h> |
| 2507 | }; |
| 2508 | #undef BPF_PROG_TYPE |
| 2509 | #undef BPF_MAP_TYPE |
| 2510 | #undef BPF_LINK_TYPE |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2511 | |
| 2512 | static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp) |
| 2513 | { |
| 2514 | const struct bpf_link *link = filp->private_data; |
| 2515 | const struct bpf_prog *prog = link->prog; |
| 2516 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2517 | |
| 2518 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
| 2519 | seq_printf(m, |
| 2520 | "link_type:\t%s\n" |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2521 | "link_id:\t%u\n" |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2522 | "prog_tag:\t%s\n" |
| 2523 | "prog_id:\t%u\n", |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 2524 | bpf_link_type_strs[link->type], |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2525 | link->id, |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2526 | prog_tag, |
| 2527 | prog->aux->id); |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 2528 | if (link->ops->show_fdinfo) |
| 2529 | link->ops->show_fdinfo(link, m); |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2530 | } |
| 2531 | #endif |
| 2532 | |
Zou Wei | 6f302bf | 2020-04-23 10:32:40 +0800 | [diff] [blame] | 2533 | static const struct file_operations bpf_link_fops = { |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2534 | #ifdef CONFIG_PROC_FS |
| 2535 | .show_fdinfo = bpf_link_show_fdinfo, |
| 2536 | #endif |
| 2537 | .release = bpf_link_release, |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 2538 | .read = bpf_dummy_read, |
| 2539 | .write = bpf_dummy_write, |
| 2540 | }; |
| 2541 | |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2542 | static int bpf_link_alloc_id(struct bpf_link *link) |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2543 | { |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2544 | int id; |
| 2545 | |
| 2546 | idr_preload(GFP_KERNEL); |
| 2547 | spin_lock_bh(&link_idr_lock); |
| 2548 | id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC); |
| 2549 | spin_unlock_bh(&link_idr_lock); |
| 2550 | idr_preload_end(); |
| 2551 | |
| 2552 | return id; |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2553 | } |
| 2554 | |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2555 | /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file, |
| 2556 | * reserving unused FD and allocating ID from link_idr. This is to be paired |
| 2557 | * with bpf_link_settle() to install FD and ID and expose bpf_link to |
| 2558 | * user-space, if bpf_link is successfully attached. If not, bpf_link and |
| 2559 | * pre-allocated resources are to be freed with bpf_cleanup() call. All the |
| 2560 | * transient state is passed around in struct bpf_link_primer. |
| 2561 | * This is preferred way to create and initialize bpf_link, especially when |
| 2562 | * there are complicated and expensive operations inbetween creating bpf_link |
| 2563 | * itself and attaching it to BPF hook. By using bpf_link_prime() and |
| 2564 | * bpf_link_settle() kernel code using bpf_link doesn't have to perform |
| 2565 | * expensive (and potentially failing) roll back operations in a rare case |
| 2566 | * that file, FD, or ID can't be allocated. |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2567 | */ |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2568 | 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] | 2569 | { |
| 2570 | struct file *file; |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2571 | int fd, id; |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2572 | |
| 2573 | fd = get_unused_fd_flags(O_CLOEXEC); |
| 2574 | if (fd < 0) |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2575 | return fd; |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2576 | |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2577 | |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2578 | id = bpf_link_alloc_id(link); |
| 2579 | if (id < 0) { |
| 2580 | put_unused_fd(fd); |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2581 | return id; |
| 2582 | } |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2583 | |
| 2584 | file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC); |
| 2585 | if (IS_ERR(file)) { |
Andrii Nakryiko | 138c676 | 2020-05-01 11:56:22 -0700 | [diff] [blame] | 2586 | bpf_link_free_id(id); |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2587 | put_unused_fd(fd); |
Andrii Nakryiko | 138c676 | 2020-05-01 11:56:22 -0700 | [diff] [blame] | 2588 | return PTR_ERR(file); |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2589 | } |
| 2590 | |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2591 | primer->link = link; |
| 2592 | primer->file = file; |
| 2593 | primer->fd = fd; |
| 2594 | primer->id = id; |
| 2595 | return 0; |
| 2596 | } |
| 2597 | |
| 2598 | int bpf_link_settle(struct bpf_link_primer *primer) |
| 2599 | { |
| 2600 | /* make bpf_link fetchable by ID */ |
| 2601 | spin_lock_bh(&link_idr_lock); |
| 2602 | primer->link->id = primer->id; |
| 2603 | spin_unlock_bh(&link_idr_lock); |
| 2604 | /* make bpf_link fetchable by FD */ |
| 2605 | fd_install(primer->fd, primer->file); |
| 2606 | /* pass through installed FD */ |
| 2607 | return primer->fd; |
| 2608 | } |
| 2609 | |
| 2610 | int bpf_link_new_fd(struct bpf_link *link) |
| 2611 | { |
| 2612 | return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC); |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2613 | } |
| 2614 | |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2615 | struct bpf_link *bpf_link_get_from_fd(u32 ufd) |
| 2616 | { |
| 2617 | struct fd f = fdget(ufd); |
| 2618 | struct bpf_link *link; |
| 2619 | |
| 2620 | if (!f.file) |
| 2621 | return ERR_PTR(-EBADF); |
| 2622 | if (f.file->f_op != &bpf_link_fops) { |
| 2623 | fdput(f); |
| 2624 | return ERR_PTR(-EINVAL); |
| 2625 | } |
| 2626 | |
| 2627 | link = f.file->private_data; |
| 2628 | bpf_link_inc(link); |
| 2629 | fdput(f); |
| 2630 | |
| 2631 | return link; |
| 2632 | } |
| 2633 | |
| 2634 | struct bpf_tracing_link { |
| 2635 | struct bpf_link link; |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 2636 | enum bpf_attach_type attach_type; |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 2637 | struct bpf_trampoline *trampoline; |
| 2638 | struct bpf_prog *tgt_prog; |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2639 | }; |
| 2640 | |
| 2641 | static void bpf_tracing_link_release(struct bpf_link *link) |
| 2642 | { |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 2643 | struct bpf_tracing_link *tr_link = |
| 2644 | container_of(link, struct bpf_tracing_link, link); |
| 2645 | |
| 2646 | WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog, |
| 2647 | tr_link->trampoline)); |
| 2648 | |
| 2649 | bpf_trampoline_put(tr_link->trampoline); |
| 2650 | |
| 2651 | /* tgt_prog is NULL if target is a kernel function */ |
| 2652 | if (tr_link->tgt_prog) |
| 2653 | bpf_prog_put(tr_link->tgt_prog); |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | static void bpf_tracing_link_dealloc(struct bpf_link *link) |
| 2657 | { |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2658 | struct bpf_tracing_link *tr_link = |
| 2659 | container_of(link, struct bpf_tracing_link, link); |
| 2660 | |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2661 | kfree(tr_link); |
| 2662 | } |
| 2663 | |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 2664 | static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link, |
| 2665 | struct seq_file *seq) |
| 2666 | { |
| 2667 | struct bpf_tracing_link *tr_link = |
| 2668 | container_of(link, struct bpf_tracing_link, link); |
| 2669 | |
| 2670 | seq_printf(seq, |
| 2671 | "attach_type:\t%d\n", |
| 2672 | tr_link->attach_type); |
| 2673 | } |
| 2674 | |
| 2675 | static int bpf_tracing_link_fill_link_info(const struct bpf_link *link, |
| 2676 | struct bpf_link_info *info) |
| 2677 | { |
| 2678 | struct bpf_tracing_link *tr_link = |
| 2679 | container_of(link, struct bpf_tracing_link, link); |
| 2680 | |
| 2681 | info->tracing.attach_type = tr_link->attach_type; |
Toke Høiland-Jørgensen | 441e8c6 | 2021-04-13 11:16:06 +0200 | [diff] [blame] | 2682 | bpf_trampoline_unpack_key(tr_link->trampoline->key, |
| 2683 | &info->tracing.target_obj_id, |
| 2684 | &info->tracing.target_btf_id); |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 2685 | |
| 2686 | return 0; |
| 2687 | } |
| 2688 | |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2689 | static const struct bpf_link_ops bpf_tracing_link_lops = { |
| 2690 | .release = bpf_tracing_link_release, |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2691 | .dealloc = bpf_tracing_link_dealloc, |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 2692 | .show_fdinfo = bpf_tracing_link_show_fdinfo, |
| 2693 | .fill_link_info = bpf_tracing_link_fill_link_info, |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2694 | }; |
| 2695 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 2696 | static int bpf_tracing_prog_attach(struct bpf_prog *prog, |
| 2697 | int tgt_prog_fd, |
| 2698 | u32 btf_id) |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 2699 | { |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2700 | struct bpf_link_primer link_primer; |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 2701 | struct bpf_prog *tgt_prog = NULL; |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 2702 | struct bpf_trampoline *tr = NULL; |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2703 | struct bpf_tracing_link *link; |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 2704 | u64 key = 0; |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2705 | int err; |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 2706 | |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 2707 | switch (prog->type) { |
| 2708 | case BPF_PROG_TYPE_TRACING: |
| 2709 | if (prog->expected_attach_type != BPF_TRACE_FENTRY && |
| 2710 | prog->expected_attach_type != BPF_TRACE_FEXIT && |
| 2711 | prog->expected_attach_type != BPF_MODIFY_RETURN) { |
| 2712 | err = -EINVAL; |
| 2713 | goto out_put_prog; |
| 2714 | } |
| 2715 | break; |
| 2716 | case BPF_PROG_TYPE_EXT: |
| 2717 | if (prog->expected_attach_type != 0) { |
| 2718 | err = -EINVAL; |
| 2719 | goto out_put_prog; |
| 2720 | } |
| 2721 | break; |
| 2722 | case BPF_PROG_TYPE_LSM: |
| 2723 | if (prog->expected_attach_type != BPF_LSM_MAC) { |
| 2724 | err = -EINVAL; |
| 2725 | goto out_put_prog; |
| 2726 | } |
| 2727 | break; |
| 2728 | default: |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 2729 | err = -EINVAL; |
| 2730 | goto out_put_prog; |
| 2731 | } |
| 2732 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 2733 | if (!!tgt_prog_fd != !!btf_id) { |
| 2734 | err = -EINVAL; |
| 2735 | goto out_put_prog; |
| 2736 | } |
| 2737 | |
| 2738 | if (tgt_prog_fd) { |
| 2739 | /* For now we only allow new targets for BPF_PROG_TYPE_EXT */ |
| 2740 | if (prog->type != BPF_PROG_TYPE_EXT) { |
| 2741 | err = -EINVAL; |
| 2742 | goto out_put_prog; |
| 2743 | } |
| 2744 | |
| 2745 | tgt_prog = bpf_prog_get(tgt_prog_fd); |
| 2746 | if (IS_ERR(tgt_prog)) { |
| 2747 | err = PTR_ERR(tgt_prog); |
| 2748 | tgt_prog = NULL; |
| 2749 | goto out_put_prog; |
| 2750 | } |
| 2751 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 2752 | key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id); |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 2753 | } |
| 2754 | |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2755 | link = kzalloc(sizeof(*link), GFP_USER); |
| 2756 | if (!link) { |
| 2757 | err = -ENOMEM; |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 2758 | goto out_put_prog; |
| 2759 | } |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 2760 | bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING, |
| 2761 | &bpf_tracing_link_lops, prog); |
| 2762 | link->attach_type = prog->expected_attach_type; |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 2763 | |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 2764 | mutex_lock(&prog->aux->dst_mutex); |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2765 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 2766 | /* There are a few possible cases here: |
| 2767 | * |
| 2768 | * - if prog->aux->dst_trampoline is set, the program was just loaded |
| 2769 | * and not yet attached to anything, so we can use the values stored |
| 2770 | * in prog->aux |
| 2771 | * |
| 2772 | * - if prog->aux->dst_trampoline is NULL, the program has already been |
| 2773 | * attached to a target and its initial target was cleared (below) |
| 2774 | * |
| 2775 | * - if tgt_prog != NULL, the caller specified tgt_prog_fd + |
| 2776 | * target_btf_id using the link_create API. |
| 2777 | * |
| 2778 | * - if tgt_prog == NULL when this function was called using the old |
Jiri Olsa | f3a9507 | 2021-04-14 21:51:41 +0200 | [diff] [blame] | 2779 | * raw_tracepoint_open API, and we need a target from prog->aux |
| 2780 | * |
| 2781 | * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program |
| 2782 | * was detached and is going for re-attachment. |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 2783 | */ |
| 2784 | if (!prog->aux->dst_trampoline && !tgt_prog) { |
Jiri Olsa | f3a9507 | 2021-04-14 21:51:41 +0200 | [diff] [blame] | 2785 | /* |
| 2786 | * Allow re-attach for TRACING and LSM programs. If it's |
| 2787 | * currently linked, bpf_trampoline_link_prog will fail. |
| 2788 | * EXT programs need to specify tgt_prog_fd, so they |
| 2789 | * re-attach in separate code path. |
| 2790 | */ |
| 2791 | if (prog->type != BPF_PROG_TYPE_TRACING && |
| 2792 | prog->type != BPF_PROG_TYPE_LSM) { |
| 2793 | err = -EINVAL; |
| 2794 | goto out_unlock; |
| 2795 | } |
| 2796 | btf_id = prog->aux->attach_btf_id; |
| 2797 | key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id); |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 2798 | } |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 2799 | |
| 2800 | if (!prog->aux->dst_trampoline || |
| 2801 | (key && key != prog->aux->dst_trampoline->key)) { |
| 2802 | /* If there is no saved target, or the specified target is |
| 2803 | * different from the destination specified at load time, we |
| 2804 | * need a new trampoline and a check for compatibility |
| 2805 | */ |
| 2806 | struct bpf_attach_target_info tgt_info = {}; |
| 2807 | |
| 2808 | err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id, |
| 2809 | &tgt_info); |
| 2810 | if (err) |
| 2811 | goto out_unlock; |
| 2812 | |
| 2813 | tr = bpf_trampoline_get(key, &tgt_info); |
| 2814 | if (!tr) { |
| 2815 | err = -ENOMEM; |
| 2816 | goto out_unlock; |
| 2817 | } |
| 2818 | } else { |
| 2819 | /* The caller didn't specify a target, or the target was the |
| 2820 | * same as the destination supplied during program load. This |
| 2821 | * means we can reuse the trampoline and reference from program |
| 2822 | * load time, and there is no need to allocate a new one. This |
| 2823 | * can only happen once for any program, as the saved values in |
| 2824 | * prog->aux are cleared below. |
| 2825 | */ |
| 2826 | tr = prog->aux->dst_trampoline; |
| 2827 | tgt_prog = prog->aux->dst_prog; |
| 2828 | } |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 2829 | |
| 2830 | err = bpf_link_prime(&link->link, &link_primer); |
| 2831 | if (err) |
| 2832 | goto out_unlock; |
| 2833 | |
| 2834 | err = bpf_trampoline_link_prog(prog, tr); |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2835 | if (err) { |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2836 | bpf_link_cleanup(&link_primer); |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 2837 | link = NULL; |
| 2838 | goto out_unlock; |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2839 | } |
| 2840 | |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 2841 | link->tgt_prog = tgt_prog; |
| 2842 | link->trampoline = tr; |
| 2843 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 2844 | /* Always clear the trampoline and target prog from prog->aux to make |
| 2845 | * sure the original attach destination is not kept alive after a |
| 2846 | * program is (re-)attached to another target. |
| 2847 | */ |
| 2848 | if (prog->aux->dst_prog && |
| 2849 | (tgt_prog_fd || tr != prog->aux->dst_trampoline)) |
| 2850 | /* got extra prog ref from syscall, or attaching to different prog */ |
| 2851 | bpf_prog_put(prog->aux->dst_prog); |
| 2852 | if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline) |
| 2853 | /* we allocated a new trampoline, so free the old one */ |
| 2854 | bpf_trampoline_put(prog->aux->dst_trampoline); |
| 2855 | |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 2856 | prog->aux->dst_prog = NULL; |
| 2857 | prog->aux->dst_trampoline = NULL; |
| 2858 | mutex_unlock(&prog->aux->dst_mutex); |
| 2859 | |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2860 | return bpf_link_settle(&link_primer); |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 2861 | out_unlock: |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 2862 | if (tr && tr != prog->aux->dst_trampoline) |
| 2863 | bpf_trampoline_put(tr); |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 2864 | mutex_unlock(&prog->aux->dst_mutex); |
| 2865 | kfree(link); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 2866 | out_put_prog: |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 2867 | if (tgt_prog_fd && tgt_prog) |
| 2868 | bpf_prog_put(tgt_prog); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 2869 | return err; |
| 2870 | } |
| 2871 | |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2872 | struct bpf_raw_tp_link { |
| 2873 | struct bpf_link link; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2874 | struct bpf_raw_event_map *btp; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2875 | }; |
| 2876 | |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2877 | static void bpf_raw_tp_link_release(struct bpf_link *link) |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2878 | { |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2879 | struct bpf_raw_tp_link *raw_tp = |
| 2880 | container_of(link, struct bpf_raw_tp_link, link); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2881 | |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2882 | bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog); |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 2883 | bpf_put_raw_tracepoint(raw_tp->btp); |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2884 | } |
| 2885 | |
| 2886 | static void bpf_raw_tp_link_dealloc(struct bpf_link *link) |
| 2887 | { |
| 2888 | struct bpf_raw_tp_link *raw_tp = |
| 2889 | container_of(link, struct bpf_raw_tp_link, link); |
| 2890 | |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2891 | kfree(raw_tp); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2892 | } |
| 2893 | |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 2894 | static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link, |
| 2895 | struct seq_file *seq) |
| 2896 | { |
| 2897 | struct bpf_raw_tp_link *raw_tp_link = |
| 2898 | container_of(link, struct bpf_raw_tp_link, link); |
| 2899 | |
| 2900 | seq_printf(seq, |
| 2901 | "tp_name:\t%s\n", |
| 2902 | raw_tp_link->btp->tp->name); |
| 2903 | } |
| 2904 | |
| 2905 | static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, |
| 2906 | struct bpf_link_info *info) |
| 2907 | { |
| 2908 | struct bpf_raw_tp_link *raw_tp_link = |
| 2909 | container_of(link, struct bpf_raw_tp_link, link); |
| 2910 | char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name); |
| 2911 | const char *tp_name = raw_tp_link->btp->tp->name; |
| 2912 | u32 ulen = info->raw_tracepoint.tp_name_len; |
| 2913 | size_t tp_len = strlen(tp_name); |
| 2914 | |
Yonghong Song | b474959 | 2020-08-21 12:10:54 -0700 | [diff] [blame] | 2915 | if (!ulen ^ !ubuf) |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 2916 | return -EINVAL; |
| 2917 | |
| 2918 | info->raw_tracepoint.tp_name_len = tp_len + 1; |
| 2919 | |
| 2920 | if (!ubuf) |
| 2921 | return 0; |
| 2922 | |
| 2923 | if (ulen >= tp_len + 1) { |
| 2924 | if (copy_to_user(ubuf, tp_name, tp_len + 1)) |
| 2925 | return -EFAULT; |
| 2926 | } else { |
| 2927 | char zero = '\0'; |
| 2928 | |
| 2929 | if (copy_to_user(ubuf, tp_name, ulen - 1)) |
| 2930 | return -EFAULT; |
| 2931 | if (put_user(zero, ubuf + ulen - 1)) |
| 2932 | return -EFAULT; |
| 2933 | return -ENOSPC; |
| 2934 | } |
| 2935 | |
| 2936 | return 0; |
| 2937 | } |
| 2938 | |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 2939 | static const struct bpf_link_ops bpf_raw_tp_link_lops = { |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 2940 | .release = bpf_raw_tp_link_release, |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 2941 | .dealloc = bpf_raw_tp_link_dealloc, |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 2942 | .show_fdinfo = bpf_raw_tp_link_show_fdinfo, |
| 2943 | .fill_link_info = bpf_raw_tp_link_fill_link_info, |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2944 | }; |
| 2945 | |
Andrii Nakryiko | b89fbfb | 2021-08-15 00:05:57 -0700 | [diff] [blame] | 2946 | #ifdef CONFIG_PERF_EVENTS |
| 2947 | struct bpf_perf_link { |
| 2948 | struct bpf_link link; |
| 2949 | struct file *perf_file; |
| 2950 | }; |
| 2951 | |
| 2952 | static void bpf_perf_link_release(struct bpf_link *link) |
| 2953 | { |
| 2954 | struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); |
| 2955 | struct perf_event *event = perf_link->perf_file->private_data; |
| 2956 | |
| 2957 | perf_event_free_bpf_prog(event); |
| 2958 | fput(perf_link->perf_file); |
| 2959 | } |
| 2960 | |
| 2961 | static void bpf_perf_link_dealloc(struct bpf_link *link) |
| 2962 | { |
| 2963 | struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); |
| 2964 | |
| 2965 | kfree(perf_link); |
| 2966 | } |
| 2967 | |
| 2968 | static const struct bpf_link_ops bpf_perf_link_lops = { |
| 2969 | .release = bpf_perf_link_release, |
| 2970 | .dealloc = bpf_perf_link_dealloc, |
| 2971 | }; |
| 2972 | |
| 2973 | static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) |
| 2974 | { |
| 2975 | struct bpf_link_primer link_primer; |
| 2976 | struct bpf_perf_link *link; |
| 2977 | struct perf_event *event; |
| 2978 | struct file *perf_file; |
| 2979 | int err; |
| 2980 | |
| 2981 | if (attr->link_create.flags) |
| 2982 | return -EINVAL; |
| 2983 | |
| 2984 | perf_file = perf_event_get(attr->link_create.target_fd); |
| 2985 | if (IS_ERR(perf_file)) |
| 2986 | return PTR_ERR(perf_file); |
| 2987 | |
| 2988 | link = kzalloc(sizeof(*link), GFP_USER); |
| 2989 | if (!link) { |
| 2990 | err = -ENOMEM; |
| 2991 | goto out_put_file; |
| 2992 | } |
| 2993 | bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog); |
| 2994 | link->perf_file = perf_file; |
| 2995 | |
| 2996 | err = bpf_link_prime(&link->link, &link_primer); |
| 2997 | if (err) { |
| 2998 | kfree(link); |
| 2999 | goto out_put_file; |
| 3000 | } |
| 3001 | |
| 3002 | event = perf_file->private_data; |
Andrii Nakryiko | 82e6b1e | 2021-08-15 00:05:58 -0700 | [diff] [blame] | 3003 | err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie); |
Andrii Nakryiko | b89fbfb | 2021-08-15 00:05:57 -0700 | [diff] [blame] | 3004 | if (err) { |
| 3005 | bpf_link_cleanup(&link_primer); |
| 3006 | goto out_put_file; |
| 3007 | } |
| 3008 | /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */ |
| 3009 | bpf_prog_inc(prog); |
| 3010 | |
| 3011 | return bpf_link_settle(&link_primer); |
| 3012 | |
| 3013 | out_put_file: |
| 3014 | fput(perf_file); |
| 3015 | return err; |
| 3016 | } |
| 3017 | #endif /* CONFIG_PERF_EVENTS */ |
| 3018 | |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 3019 | #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd |
| 3020 | |
| 3021 | static int bpf_raw_tracepoint_open(const union bpf_attr *attr) |
| 3022 | { |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 3023 | struct bpf_link_primer link_primer; |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 3024 | struct bpf_raw_tp_link *link; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 3025 | struct bpf_raw_event_map *btp; |
| 3026 | struct bpf_prog *prog; |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 3027 | const char *tp_name; |
| 3028 | char buf[128]; |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 3029 | int err; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 3030 | |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 3031 | if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN)) |
| 3032 | return -EINVAL; |
| 3033 | |
| 3034 | prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); |
| 3035 | if (IS_ERR(prog)) |
| 3036 | return PTR_ERR(prog); |
| 3037 | |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 3038 | switch (prog->type) { |
| 3039 | case BPF_PROG_TYPE_TRACING: |
| 3040 | case BPF_PROG_TYPE_EXT: |
| 3041 | case BPF_PROG_TYPE_LSM: |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 3042 | if (attr->raw_tracepoint.name) { |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 3043 | /* The attach point for this category of programs |
| 3044 | * should be specified via btf_id during program load. |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 3045 | */ |
| 3046 | err = -EINVAL; |
| 3047 | goto out_put_prog; |
| 3048 | } |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 3049 | if (prog->type == BPF_PROG_TYPE_TRACING && |
| 3050 | prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 3051 | tp_name = prog->aux->attach_func_name; |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 3052 | break; |
| 3053 | } |
Jiri Olsa | 5541075 | 2021-01-11 20:16:50 +0100 | [diff] [blame] | 3054 | err = bpf_tracing_prog_attach(prog, 0, 0); |
| 3055 | if (err >= 0) |
| 3056 | return err; |
| 3057 | goto out_put_prog; |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 3058 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 3059 | case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 3060 | if (strncpy_from_user(buf, |
| 3061 | u64_to_user_ptr(attr->raw_tracepoint.name), |
| 3062 | sizeof(buf) - 1) < 0) { |
| 3063 | err = -EFAULT; |
| 3064 | goto out_put_prog; |
| 3065 | } |
| 3066 | buf[sizeof(buf) - 1] = 0; |
| 3067 | tp_name = buf; |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 3068 | break; |
| 3069 | default: |
| 3070 | err = -EINVAL; |
| 3071 | goto out_put_prog; |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 3072 | } |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 3073 | |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 3074 | btp = bpf_get_raw_tracepoint(tp_name); |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 3075 | if (!btp) { |
| 3076 | err = -ENOENT; |
| 3077 | goto out_put_prog; |
| 3078 | } |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 3079 | |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 3080 | link = kzalloc(sizeof(*link), GFP_USER); |
| 3081 | if (!link) { |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 3082 | err = -ENOMEM; |
| 3083 | goto out_put_btp; |
| 3084 | } |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 3085 | bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT, |
| 3086 | &bpf_raw_tp_link_lops, prog); |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 3087 | link->btp = btp; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 3088 | |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 3089 | err = bpf_link_prime(&link->link, &link_primer); |
| 3090 | if (err) { |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 3091 | kfree(link); |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 3092 | goto out_put_btp; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 3093 | } |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 3094 | |
| 3095 | err = bpf_probe_register(link->btp, prog); |
| 3096 | if (err) { |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 3097 | bpf_link_cleanup(&link_primer); |
Andrii Nakryiko | babf316 | 2020-03-09 16:10:51 -0700 | [diff] [blame] | 3098 | goto out_put_btp; |
| 3099 | } |
| 3100 | |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 3101 | return bpf_link_settle(&link_primer); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 3102 | |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 3103 | out_put_btp: |
| 3104 | bpf_put_raw_tracepoint(btp); |
Alexei Starovoitov | ac4414b | 2019-10-15 20:25:01 -0700 | [diff] [blame] | 3105 | out_put_prog: |
| 3106 | bpf_prog_put(prog); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 3107 | return err; |
| 3108 | } |
| 3109 | |
Anders Roxell | 3349158 | 2018-04-03 14:09:47 +0200 | [diff] [blame] | 3110 | static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, |
| 3111 | enum bpf_attach_type attach_type) |
| 3112 | { |
| 3113 | switch (prog->type) { |
| 3114 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 3115 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 3116 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 3117 | case BPF_PROG_TYPE_SK_LOOKUP: |
Anders Roxell | 3349158 | 2018-04-03 14:09:47 +0200 | [diff] [blame] | 3118 | return attach_type == prog->expected_attach_type ? 0 : -EINVAL; |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 3119 | case BPF_PROG_TYPE_CGROUP_SKB: |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 3120 | if (!capable(CAP_NET_ADMIN)) |
| 3121 | /* cg-skb progs can be loaded by unpriv user. |
| 3122 | * check permissions at attach time. |
| 3123 | */ |
| 3124 | return -EPERM; |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 3125 | return prog->enforce_expected_attach_type && |
| 3126 | prog->expected_attach_type != attach_type ? |
| 3127 | -EINVAL : 0; |
Anders Roxell | 3349158 | 2018-04-03 14:09:47 +0200 | [diff] [blame] | 3128 | default: |
| 3129 | return 0; |
| 3130 | } |
| 3131 | } |
| 3132 | |
Andrii Nakryiko | e28784e | 2020-03-24 23:57:42 -0700 | [diff] [blame] | 3133 | static enum bpf_prog_type |
| 3134 | attach_type_to_prog_type(enum bpf_attach_type attach_type) |
| 3135 | { |
| 3136 | switch (attach_type) { |
| 3137 | case BPF_CGROUP_INET_INGRESS: |
| 3138 | case BPF_CGROUP_INET_EGRESS: |
| 3139 | return BPF_PROG_TYPE_CGROUP_SKB; |
Andrii Nakryiko | e28784e | 2020-03-24 23:57:42 -0700 | [diff] [blame] | 3140 | case BPF_CGROUP_INET_SOCK_CREATE: |
Stanislav Fomichev | f583674 | 2020-07-06 16:01:25 -0700 | [diff] [blame] | 3141 | case BPF_CGROUP_INET_SOCK_RELEASE: |
Andrii Nakryiko | e28784e | 2020-03-24 23:57:42 -0700 | [diff] [blame] | 3142 | case BPF_CGROUP_INET4_POST_BIND: |
| 3143 | case BPF_CGROUP_INET6_POST_BIND: |
| 3144 | return BPF_PROG_TYPE_CGROUP_SOCK; |
| 3145 | case BPF_CGROUP_INET4_BIND: |
| 3146 | case BPF_CGROUP_INET6_BIND: |
| 3147 | case BPF_CGROUP_INET4_CONNECT: |
| 3148 | case BPF_CGROUP_INET6_CONNECT: |
Daniel Borkmann | 1b66d25 | 2020-05-19 00:45:45 +0200 | [diff] [blame] | 3149 | case BPF_CGROUP_INET4_GETPEERNAME: |
| 3150 | case BPF_CGROUP_INET6_GETPEERNAME: |
| 3151 | case BPF_CGROUP_INET4_GETSOCKNAME: |
| 3152 | case BPF_CGROUP_INET6_GETSOCKNAME: |
Andrii Nakryiko | e28784e | 2020-03-24 23:57:42 -0700 | [diff] [blame] | 3153 | case BPF_CGROUP_UDP4_SENDMSG: |
| 3154 | case BPF_CGROUP_UDP6_SENDMSG: |
| 3155 | case BPF_CGROUP_UDP4_RECVMSG: |
| 3156 | case BPF_CGROUP_UDP6_RECVMSG: |
| 3157 | return BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
| 3158 | case BPF_CGROUP_SOCK_OPS: |
| 3159 | return BPF_PROG_TYPE_SOCK_OPS; |
| 3160 | case BPF_CGROUP_DEVICE: |
| 3161 | return BPF_PROG_TYPE_CGROUP_DEVICE; |
| 3162 | case BPF_SK_MSG_VERDICT: |
| 3163 | return BPF_PROG_TYPE_SK_MSG; |
| 3164 | case BPF_SK_SKB_STREAM_PARSER: |
| 3165 | case BPF_SK_SKB_STREAM_VERDICT: |
Cong Wang | a7ba455 | 2021-03-30 19:32:30 -0700 | [diff] [blame] | 3166 | case BPF_SK_SKB_VERDICT: |
Andrii Nakryiko | e28784e | 2020-03-24 23:57:42 -0700 | [diff] [blame] | 3167 | return BPF_PROG_TYPE_SK_SKB; |
| 3168 | case BPF_LIRC_MODE2: |
| 3169 | return BPF_PROG_TYPE_LIRC_MODE2; |
| 3170 | case BPF_FLOW_DISSECTOR: |
| 3171 | return BPF_PROG_TYPE_FLOW_DISSECTOR; |
| 3172 | case BPF_CGROUP_SYSCTL: |
| 3173 | return BPF_PROG_TYPE_CGROUP_SYSCTL; |
| 3174 | case BPF_CGROUP_GETSOCKOPT: |
| 3175 | case BPF_CGROUP_SETSOCKOPT: |
| 3176 | return BPF_PROG_TYPE_CGROUP_SOCKOPT; |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 3177 | case BPF_TRACE_ITER: |
| 3178 | return BPF_PROG_TYPE_TRACING; |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 3179 | case BPF_SK_LOOKUP: |
| 3180 | return BPF_PROG_TYPE_SK_LOOKUP; |
Andrii Nakryiko | aa8d3a7 | 2020-07-21 23:45:57 -0700 | [diff] [blame] | 3181 | case BPF_XDP: |
| 3182 | return BPF_PROG_TYPE_XDP; |
Andrii Nakryiko | e28784e | 2020-03-24 23:57:42 -0700 | [diff] [blame] | 3183 | default: |
| 3184 | return BPF_PROG_TYPE_UNSPEC; |
| 3185 | } |
| 3186 | } |
| 3187 | |
Andrey Ignatov | 7dd68b3 | 2019-12-18 23:44:35 -0800 | [diff] [blame] | 3188 | #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 3189 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 3190 | #define BPF_F_ATTACH_MASK \ |
Andrey Ignatov | 7dd68b3 | 2019-12-18 23:44:35 -0800 | [diff] [blame] | 3191 | (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE) |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 3192 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 3193 | static int bpf_prog_attach(const union bpf_attr *attr) |
| 3194 | { |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 3195 | enum bpf_prog_type ptype; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 3196 | struct bpf_prog *prog; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 3197 | int ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 3198 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 3199 | if (CHECK_ATTR(BPF_PROG_ATTACH)) |
| 3200 | return -EINVAL; |
| 3201 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 3202 | if (attr->attach_flags & ~BPF_F_ATTACH_MASK) |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 3203 | return -EINVAL; |
| 3204 | |
Andrii Nakryiko | e28784e | 2020-03-24 23:57:42 -0700 | [diff] [blame] | 3205 | ptype = attach_type_to_prog_type(attr->attach_type); |
| 3206 | if (ptype == BPF_PROG_TYPE_UNSPEC) |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 3207 | return -EINVAL; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 3208 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 3209 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
| 3210 | if (IS_ERR(prog)) |
| 3211 | return PTR_ERR(prog); |
| 3212 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 3213 | if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { |
| 3214 | bpf_prog_put(prog); |
| 3215 | return -EINVAL; |
| 3216 | } |
| 3217 | |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 3218 | switch (ptype) { |
| 3219 | case BPF_PROG_TYPE_SK_SKB: |
| 3220 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 3221 | ret = sock_map_get_from_fd(attr, prog); |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 3222 | break; |
| 3223 | case BPF_PROG_TYPE_LIRC_MODE2: |
| 3224 | ret = lirc_prog_attach(attr, prog); |
| 3225 | break; |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 3226 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Jakub Sitnicki | a3fd7ce | 2020-05-31 10:28:36 +0200 | [diff] [blame] | 3227 | ret = netns_bpf_prog_attach(attr, prog); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 3228 | break; |
Andrii Nakryiko | e28784e | 2020-03-24 23:57:42 -0700 | [diff] [blame] | 3229 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
| 3230 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 3231 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 3232 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 3233 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 3234 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
| 3235 | case BPF_PROG_TYPE_SOCK_OPS: |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 3236 | ret = cgroup_bpf_prog_attach(attr, ptype, prog); |
Andrii Nakryiko | e28784e | 2020-03-24 23:57:42 -0700 | [diff] [blame] | 3237 | break; |
| 3238 | default: |
| 3239 | ret = -EINVAL; |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 3240 | } |
| 3241 | |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 3242 | if (ret) |
| 3243 | bpf_prog_put(prog); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 3244 | return ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | #define BPF_PROG_DETACH_LAST_FIELD attach_type |
| 3248 | |
| 3249 | static int bpf_prog_detach(const union bpf_attr *attr) |
| 3250 | { |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 3251 | enum bpf_prog_type ptype; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 3252 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 3253 | if (CHECK_ATTR(BPF_PROG_DETACH)) |
| 3254 | return -EINVAL; |
| 3255 | |
Andrii Nakryiko | e28784e | 2020-03-24 23:57:42 -0700 | [diff] [blame] | 3256 | ptype = attach_type_to_prog_type(attr->attach_type); |
| 3257 | |
| 3258 | switch (ptype) { |
| 3259 | case BPF_PROG_TYPE_SK_MSG: |
| 3260 | case BPF_PROG_TYPE_SK_SKB: |
Lorenz Bauer | bb0de31 | 2020-06-29 10:56:28 +0100 | [diff] [blame] | 3261 | return sock_map_prog_detach(attr, ptype); |
Andrii Nakryiko | e28784e | 2020-03-24 23:57:42 -0700 | [diff] [blame] | 3262 | case BPF_PROG_TYPE_LIRC_MODE2: |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 3263 | return lirc_prog_detach(attr); |
Andrii Nakryiko | e28784e | 2020-03-24 23:57:42 -0700 | [diff] [blame] | 3264 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Lorenz Bauer | 4ac2add | 2020-06-29 10:56:26 +0100 | [diff] [blame] | 3265 | return netns_bpf_prog_detach(attr, ptype); |
Andrii Nakryiko | e28784e | 2020-03-24 23:57:42 -0700 | [diff] [blame] | 3266 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
| 3267 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 3268 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 3269 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 3270 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 3271 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
| 3272 | case BPF_PROG_TYPE_SOCK_OPS: |
| 3273 | return cgroup_bpf_prog_detach(attr, ptype); |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 3274 | default: |
| 3275 | return -EINVAL; |
| 3276 | } |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 3277 | } |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 3278 | |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 3279 | #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt |
| 3280 | |
| 3281 | static int bpf_prog_query(const union bpf_attr *attr, |
| 3282 | union bpf_attr __user *uattr) |
| 3283 | { |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 3284 | if (!capable(CAP_NET_ADMIN)) |
| 3285 | return -EPERM; |
| 3286 | if (CHECK_ATTR(BPF_PROG_QUERY)) |
| 3287 | return -EINVAL; |
| 3288 | if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) |
| 3289 | return -EINVAL; |
| 3290 | |
| 3291 | switch (attr->query.attach_type) { |
| 3292 | case BPF_CGROUP_INET_INGRESS: |
| 3293 | case BPF_CGROUP_INET_EGRESS: |
| 3294 | case BPF_CGROUP_INET_SOCK_CREATE: |
Stanislav Fomichev | f583674 | 2020-07-06 16:01:25 -0700 | [diff] [blame] | 3295 | case BPF_CGROUP_INET_SOCK_RELEASE: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 3296 | case BPF_CGROUP_INET4_BIND: |
| 3297 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 3298 | case BPF_CGROUP_INET4_POST_BIND: |
| 3299 | case BPF_CGROUP_INET6_POST_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 3300 | case BPF_CGROUP_INET4_CONNECT: |
| 3301 | case BPF_CGROUP_INET6_CONNECT: |
Daniel Borkmann | 1b66d25 | 2020-05-19 00:45:45 +0200 | [diff] [blame] | 3302 | case BPF_CGROUP_INET4_GETPEERNAME: |
| 3303 | case BPF_CGROUP_INET6_GETPEERNAME: |
| 3304 | case BPF_CGROUP_INET4_GETSOCKNAME: |
| 3305 | case BPF_CGROUP_INET6_GETSOCKNAME: |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 3306 | case BPF_CGROUP_UDP4_SENDMSG: |
| 3307 | case BPF_CGROUP_UDP6_SENDMSG: |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 3308 | case BPF_CGROUP_UDP4_RECVMSG: |
| 3309 | case BPF_CGROUP_UDP6_RECVMSG: |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 3310 | case BPF_CGROUP_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 3311 | case BPF_CGROUP_DEVICE: |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 3312 | case BPF_CGROUP_SYSCTL: |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 3313 | case BPF_CGROUP_GETSOCKOPT: |
| 3314 | case BPF_CGROUP_SETSOCKOPT: |
Andrii Nakryiko | e28784e | 2020-03-24 23:57:42 -0700 | [diff] [blame] | 3315 | return cgroup_bpf_prog_query(attr, uattr); |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 3316 | case BPF_LIRC_MODE2: |
| 3317 | return lirc_prog_query(attr, uattr); |
Stanislav Fomichev | 118c8e9 | 2019-04-25 14:37:23 -0700 | [diff] [blame] | 3318 | case BPF_FLOW_DISSECTOR: |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 3319 | case BPF_SK_LOOKUP: |
Jakub Sitnicki | a3fd7ce | 2020-05-31 10:28:36 +0200 | [diff] [blame] | 3320 | return netns_bpf_prog_query(attr, uattr); |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 3321 | default: |
| 3322 | return -EINVAL; |
| 3323 | } |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 3324 | } |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 3325 | |
Song Liu | 1b4d60e | 2020-09-25 13:54:29 -0700 | [diff] [blame] | 3326 | #define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 3327 | |
| 3328 | static int bpf_prog_test_run(const union bpf_attr *attr, |
| 3329 | union bpf_attr __user *uattr) |
| 3330 | { |
| 3331 | struct bpf_prog *prog; |
| 3332 | int ret = -ENOTSUPP; |
| 3333 | |
| 3334 | if (CHECK_ATTR(BPF_PROG_TEST_RUN)) |
| 3335 | return -EINVAL; |
| 3336 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 3337 | if ((attr->test.ctx_size_in && !attr->test.ctx_in) || |
| 3338 | (!attr->test.ctx_size_in && attr->test.ctx_in)) |
| 3339 | return -EINVAL; |
| 3340 | |
| 3341 | if ((attr->test.ctx_size_out && !attr->test.ctx_out) || |
| 3342 | (!attr->test.ctx_size_out && attr->test.ctx_out)) |
| 3343 | return -EINVAL; |
| 3344 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 3345 | prog = bpf_prog_get(attr->test.prog_fd); |
| 3346 | if (IS_ERR(prog)) |
| 3347 | return PTR_ERR(prog); |
| 3348 | |
| 3349 | if (prog->aux->ops->test_run) |
| 3350 | ret = prog->aux->ops->test_run(prog, attr, uattr); |
| 3351 | |
| 3352 | bpf_prog_put(prog); |
| 3353 | return ret; |
| 3354 | } |
| 3355 | |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 3356 | #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id |
| 3357 | |
| 3358 | static int bpf_obj_get_next_id(const union bpf_attr *attr, |
| 3359 | union bpf_attr __user *uattr, |
| 3360 | struct idr *idr, |
| 3361 | spinlock_t *lock) |
| 3362 | { |
| 3363 | u32 next_id = attr->start_id; |
| 3364 | int err = 0; |
| 3365 | |
| 3366 | if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) |
| 3367 | return -EINVAL; |
| 3368 | |
| 3369 | if (!capable(CAP_SYS_ADMIN)) |
| 3370 | return -EPERM; |
| 3371 | |
| 3372 | next_id++; |
| 3373 | spin_lock_bh(lock); |
| 3374 | if (!idr_get_next(idr, &next_id)) |
| 3375 | err = -ENOENT; |
| 3376 | spin_unlock_bh(lock); |
| 3377 | |
| 3378 | if (!err) |
| 3379 | err = put_user(next_id, &uattr->next_id); |
| 3380 | |
| 3381 | return err; |
| 3382 | } |
| 3383 | |
Yonghong Song | 6086d29 | 2020-05-09 10:59:09 -0700 | [diff] [blame] | 3384 | struct bpf_map *bpf_map_get_curr_or_next(u32 *id) |
| 3385 | { |
| 3386 | struct bpf_map *map; |
| 3387 | |
| 3388 | spin_lock_bh(&map_idr_lock); |
| 3389 | again: |
| 3390 | map = idr_get_next(&map_idr, id); |
| 3391 | if (map) { |
| 3392 | map = __bpf_map_inc_not_zero(map, false); |
| 3393 | if (IS_ERR(map)) { |
| 3394 | (*id)++; |
| 3395 | goto again; |
| 3396 | } |
| 3397 | } |
| 3398 | spin_unlock_bh(&map_idr_lock); |
| 3399 | |
| 3400 | return map; |
| 3401 | } |
| 3402 | |
Alexei Starovoitov | a228a64 | 2020-07-01 18:10:18 -0700 | [diff] [blame] | 3403 | struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id) |
| 3404 | { |
| 3405 | struct bpf_prog *prog; |
| 3406 | |
| 3407 | spin_lock_bh(&prog_idr_lock); |
| 3408 | again: |
| 3409 | prog = idr_get_next(&prog_idr, id); |
| 3410 | if (prog) { |
| 3411 | prog = bpf_prog_inc_not_zero(prog); |
| 3412 | if (IS_ERR(prog)) { |
| 3413 | (*id)++; |
| 3414 | goto again; |
| 3415 | } |
| 3416 | } |
| 3417 | spin_unlock_bh(&prog_idr_lock); |
| 3418 | |
| 3419 | return prog; |
| 3420 | } |
| 3421 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 3422 | #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id |
| 3423 | |
Björn Töpel | 7e6897f | 2019-12-13 18:51:09 +0100 | [diff] [blame] | 3424 | struct bpf_prog *bpf_prog_by_id(u32 id) |
| 3425 | { |
| 3426 | struct bpf_prog *prog; |
| 3427 | |
| 3428 | if (!id) |
| 3429 | return ERR_PTR(-ENOENT); |
| 3430 | |
| 3431 | spin_lock_bh(&prog_idr_lock); |
| 3432 | prog = idr_find(&prog_idr, id); |
| 3433 | if (prog) |
| 3434 | prog = bpf_prog_inc_not_zero(prog); |
| 3435 | else |
| 3436 | prog = ERR_PTR(-ENOENT); |
| 3437 | spin_unlock_bh(&prog_idr_lock); |
| 3438 | return prog; |
| 3439 | } |
| 3440 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 3441 | static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) |
| 3442 | { |
| 3443 | struct bpf_prog *prog; |
| 3444 | u32 id = attr->prog_id; |
| 3445 | int fd; |
| 3446 | |
| 3447 | if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) |
| 3448 | return -EINVAL; |
| 3449 | |
| 3450 | if (!capable(CAP_SYS_ADMIN)) |
| 3451 | return -EPERM; |
| 3452 | |
Björn Töpel | 7e6897f | 2019-12-13 18:51:09 +0100 | [diff] [blame] | 3453 | prog = bpf_prog_by_id(id); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 3454 | if (IS_ERR(prog)) |
| 3455 | return PTR_ERR(prog); |
| 3456 | |
| 3457 | fd = bpf_prog_new_fd(prog); |
| 3458 | if (fd < 0) |
| 3459 | bpf_prog_put(prog); |
| 3460 | |
| 3461 | return fd; |
| 3462 | } |
| 3463 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 3464 | #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 3465 | |
| 3466 | static int bpf_map_get_fd_by_id(const union bpf_attr *attr) |
| 3467 | { |
| 3468 | struct bpf_map *map; |
| 3469 | u32 id = attr->map_id; |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 3470 | int f_flags; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 3471 | int fd; |
| 3472 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 3473 | if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || |
| 3474 | attr->open_flags & ~BPF_OBJ_FLAG_MASK) |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 3475 | return -EINVAL; |
| 3476 | |
| 3477 | if (!capable(CAP_SYS_ADMIN)) |
| 3478 | return -EPERM; |
| 3479 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 3480 | f_flags = bpf_get_file_flag(attr->open_flags); |
| 3481 | if (f_flags < 0) |
| 3482 | return f_flags; |
| 3483 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 3484 | spin_lock_bh(&map_idr_lock); |
| 3485 | map = idr_find(&map_idr, id); |
| 3486 | if (map) |
Stanislav Fomichev | b0e4701 | 2019-08-14 10:37:48 -0700 | [diff] [blame] | 3487 | map = __bpf_map_inc_not_zero(map, true); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 3488 | else |
| 3489 | map = ERR_PTR(-ENOENT); |
| 3490 | spin_unlock_bh(&map_idr_lock); |
| 3491 | |
| 3492 | if (IS_ERR(map)) |
| 3493 | return PTR_ERR(map); |
| 3494 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 3495 | fd = bpf_map_new_fd(map, f_flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 3496 | if (fd < 0) |
Peng Sun | 781e628 | 2019-02-26 22:15:37 +0800 | [diff] [blame] | 3497 | bpf_map_put_with_uref(map); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 3498 | |
| 3499 | return fd; |
| 3500 | } |
| 3501 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3502 | 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] | 3503 | unsigned long addr, u32 *off, |
| 3504 | u32 *type) |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3505 | { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 3506 | const struct bpf_map *map; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3507 | int i; |
| 3508 | |
YiFei Zhu | 984fe94 | 2020-09-15 16:45:39 -0700 | [diff] [blame] | 3509 | mutex_lock(&prog->aux->used_maps_mutex); |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 3510 | for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) { |
| 3511 | map = prog->aux->used_maps[i]; |
| 3512 | if (map == (void *)addr) { |
| 3513 | *type = BPF_PSEUDO_MAP_FD; |
YiFei Zhu | 984fe94 | 2020-09-15 16:45:39 -0700 | [diff] [blame] | 3514 | goto out; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 3515 | } |
| 3516 | if (!map->ops->map_direct_value_meta) |
| 3517 | continue; |
| 3518 | if (!map->ops->map_direct_value_meta(map, addr, off)) { |
| 3519 | *type = BPF_PSEUDO_MAP_VALUE; |
YiFei Zhu | 984fe94 | 2020-09-15 16:45:39 -0700 | [diff] [blame] | 3520 | goto out; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 3521 | } |
| 3522 | } |
YiFei Zhu | 984fe94 | 2020-09-15 16:45:39 -0700 | [diff] [blame] | 3523 | map = NULL; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 3524 | |
YiFei Zhu | 984fe94 | 2020-09-15 16:45:39 -0700 | [diff] [blame] | 3525 | out: |
| 3526 | mutex_unlock(&prog->aux->used_maps_mutex); |
| 3527 | return map; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3528 | } |
| 3529 | |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 3530 | static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog, |
| 3531 | const struct cred *f_cred) |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3532 | { |
| 3533 | const struct bpf_map *map; |
| 3534 | struct bpf_insn *insns; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 3535 | u32 off, type; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3536 | u64 imm; |
Andrii Nakryiko | 29fcb05 | 2020-06-12 17:21:15 -0700 | [diff] [blame] | 3537 | u8 code; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3538 | int i; |
| 3539 | |
| 3540 | insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), |
| 3541 | GFP_USER); |
| 3542 | if (!insns) |
| 3543 | return insns; |
| 3544 | |
| 3545 | for (i = 0; i < prog->len; i++) { |
Andrii Nakryiko | 29fcb05 | 2020-06-12 17:21:15 -0700 | [diff] [blame] | 3546 | code = insns[i].code; |
| 3547 | |
| 3548 | if (code == (BPF_JMP | BPF_TAIL_CALL)) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3549 | insns[i].code = BPF_JMP | BPF_CALL; |
| 3550 | insns[i].imm = BPF_FUNC_tail_call; |
| 3551 | /* fall-through */ |
| 3552 | } |
Andrii Nakryiko | 29fcb05 | 2020-06-12 17:21:15 -0700 | [diff] [blame] | 3553 | if (code == (BPF_JMP | BPF_CALL) || |
| 3554 | code == (BPF_JMP | BPF_CALL_ARGS)) { |
| 3555 | if (code == (BPF_JMP | BPF_CALL_ARGS)) |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3556 | insns[i].code = BPF_JMP | BPF_CALL; |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 3557 | if (!bpf_dump_raw_ok(f_cred)) |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3558 | insns[i].imm = 0; |
| 3559 | continue; |
| 3560 | } |
Andrii Nakryiko | 29fcb05 | 2020-06-12 17:21:15 -0700 | [diff] [blame] | 3561 | if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) { |
| 3562 | insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM; |
| 3563 | continue; |
| 3564 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3565 | |
Andrii Nakryiko | 29fcb05 | 2020-06-12 17:21:15 -0700 | [diff] [blame] | 3566 | if (code != (BPF_LD | BPF_IMM | BPF_DW)) |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3567 | continue; |
| 3568 | |
| 3569 | imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 3570 | map = bpf_map_from_imm(prog, imm, &off, &type); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3571 | if (map) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 3572 | insns[i].src_reg = type; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3573 | insns[i].imm = map->id; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 3574 | insns[i + 1].imm = off; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3575 | continue; |
| 3576 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3577 | } |
| 3578 | |
| 3579 | return insns; |
| 3580 | } |
| 3581 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3582 | static int set_info_rec_size(struct bpf_prog_info *info) |
| 3583 | { |
| 3584 | /* |
| 3585 | * Ensure info.*_rec_size is the same as kernel expected size |
| 3586 | * |
| 3587 | * or |
| 3588 | * |
| 3589 | * Only allow zero *_rec_size if both _rec_size and _cnt are |
| 3590 | * zero. In this case, the kernel will set the expected |
| 3591 | * _rec_size back to the info. |
| 3592 | */ |
| 3593 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3594 | if ((info->nr_func_info || info->func_info_rec_size) && |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3595 | info->func_info_rec_size != sizeof(struct bpf_func_info)) |
| 3596 | return -EINVAL; |
| 3597 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3598 | if ((info->nr_line_info || info->line_info_rec_size) && |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3599 | info->line_info_rec_size != sizeof(struct bpf_line_info)) |
| 3600 | return -EINVAL; |
| 3601 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3602 | 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] | 3603 | info->jited_line_info_rec_size != sizeof(__u64)) |
| 3604 | return -EINVAL; |
| 3605 | |
| 3606 | info->func_info_rec_size = sizeof(struct bpf_func_info); |
| 3607 | info->line_info_rec_size = sizeof(struct bpf_line_info); |
| 3608 | info->jited_line_info_rec_size = sizeof(__u64); |
| 3609 | |
| 3610 | return 0; |
| 3611 | } |
| 3612 | |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 3613 | static int bpf_prog_get_info_by_fd(struct file *file, |
| 3614 | struct bpf_prog *prog, |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3615 | const union bpf_attr *attr, |
| 3616 | union bpf_attr __user *uattr) |
| 3617 | { |
| 3618 | 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] | 3619 | struct bpf_prog_info info; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3620 | u32 info_len = attr->info.info_len; |
Eric Dumazet | 61a0aba | 2021-10-26 14:41:33 -0700 | [diff] [blame] | 3621 | struct bpf_prog_kstats stats; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3622 | char __user *uinsns; |
| 3623 | u32 ulen; |
| 3624 | int err; |
| 3625 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 3626 | err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3627 | if (err) |
| 3628 | return err; |
| 3629 | info_len = min_t(u32, sizeof(info), info_len); |
| 3630 | |
Greg Kroah-Hartman | 5c6f258 | 2020-03-20 17:22:58 +0100 | [diff] [blame] | 3631 | memset(&info, 0, sizeof(info)); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3632 | if (copy_from_user(&info, uinfo, info_len)) |
Daniel Borkmann | 89b0968 | 2017-07-27 21:02:46 +0200 | [diff] [blame] | 3633 | return -EFAULT; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3634 | |
| 3635 | info.type = prog->type; |
| 3636 | info.id = prog->aux->id; |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 3637 | info.load_time = prog->aux->load_time; |
| 3638 | info.created_by_uid = from_kuid_munged(current_user_ns(), |
| 3639 | prog->aux->user->uid); |
Jiri Olsa | b85fab0 | 2018-04-25 19:41:06 +0200 | [diff] [blame] | 3640 | info.gpl_compatible = prog->gpl_compatible; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3641 | |
| 3642 | memcpy(info.tag, prog->tag, sizeof(prog->tag)); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 3643 | memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); |
| 3644 | |
YiFei Zhu | 984fe94 | 2020-09-15 16:45:39 -0700 | [diff] [blame] | 3645 | mutex_lock(&prog->aux->used_maps_mutex); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 3646 | ulen = info.nr_map_ids; |
| 3647 | info.nr_map_ids = prog->aux->used_map_cnt; |
| 3648 | ulen = min_t(u32, info.nr_map_ids, ulen); |
| 3649 | if (ulen) { |
Martin KaFai Lau | 721e08d | 2017-09-29 10:52:17 -0700 | [diff] [blame] | 3650 | 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] | 3651 | u32 i; |
| 3652 | |
| 3653 | for (i = 0; i < ulen; i++) |
| 3654 | if (put_user(prog->aux->used_maps[i]->id, |
YiFei Zhu | 984fe94 | 2020-09-15 16:45:39 -0700 | [diff] [blame] | 3655 | &user_map_ids[i])) { |
| 3656 | mutex_unlock(&prog->aux->used_maps_mutex); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 3657 | return -EFAULT; |
YiFei Zhu | 984fe94 | 2020-09-15 16:45:39 -0700 | [diff] [blame] | 3658 | } |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 3659 | } |
YiFei Zhu | 984fe94 | 2020-09-15 16:45:39 -0700 | [diff] [blame] | 3660 | mutex_unlock(&prog->aux->used_maps_mutex); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3661 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3662 | err = set_info_rec_size(&info); |
| 3663 | if (err) |
| 3664 | return err; |
Martin KaFai Lau | 7337224 | 2018-12-05 17:35:43 -0800 | [diff] [blame] | 3665 | |
Alexei Starovoitov | 5f8f8b9 | 2019-02-25 14:28:40 -0800 | [diff] [blame] | 3666 | bpf_prog_get_stats(prog, &stats); |
| 3667 | info.run_time_ns = stats.nsecs; |
| 3668 | info.run_cnt = stats.cnt; |
Alexei Starovoitov | 9ed9e9b | 2021-02-09 19:36:31 -0800 | [diff] [blame] | 3669 | info.recursion_misses = stats.misses; |
Alexei Starovoitov | 5f8f8b9 | 2019-02-25 14:28:40 -0800 | [diff] [blame] | 3670 | |
Dave Marchevsky | aba64c7 | 2021-10-20 00:48:17 -0700 | [diff] [blame] | 3671 | info.verified_insns = prog->aux->verified_insns; |
| 3672 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 3673 | if (!bpf_capable()) { |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3674 | info.jited_prog_len = 0; |
| 3675 | info.xlated_prog_len = 0; |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 3676 | info.nr_jited_ksyms = 0; |
Daniel Borkmann | 28c2fae | 2018-11-02 11:35:46 +0100 | [diff] [blame] | 3677 | info.nr_jited_func_lens = 0; |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3678 | info.nr_func_info = 0; |
| 3679 | info.nr_line_info = 0; |
| 3680 | info.nr_jited_line_info = 0; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3681 | goto done; |
| 3682 | } |
| 3683 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3684 | ulen = info.xlated_prog_len; |
Daniel Borkmann | 9975a54 | 2017-07-28 17:05:25 +0200 | [diff] [blame] | 3685 | info.xlated_prog_len = bpf_prog_insn_size(prog); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3686 | if (info.xlated_prog_len && ulen) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3687 | struct bpf_insn *insns_sanitized; |
| 3688 | bool fault; |
| 3689 | |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 3690 | if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3691 | info.xlated_prog_insns = 0; |
| 3692 | goto done; |
| 3693 | } |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 3694 | insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3695 | if (!insns_sanitized) |
| 3696 | return -ENOMEM; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3697 | uinsns = u64_to_user_ptr(info.xlated_prog_insns); |
| 3698 | ulen = min_t(u32, info.xlated_prog_len, ulen); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 3699 | fault = copy_to_user(uinsns, insns_sanitized, ulen); |
| 3700 | kfree(insns_sanitized); |
| 3701 | if (fault) |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3702 | return -EFAULT; |
| 3703 | } |
| 3704 | |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 3705 | if (bpf_prog_is_dev_bound(prog->aux)) { |
| 3706 | err = bpf_prog_offload_info_fill(&info, prog); |
| 3707 | if (err) |
| 3708 | return err; |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 3709 | goto done; |
| 3710 | } |
| 3711 | |
| 3712 | /* NOTE: the following code is supposed to be skipped for offload. |
| 3713 | * bpf_prog_offload_info_fill() is the place to fill similar fields |
| 3714 | * for offload. |
| 3715 | */ |
| 3716 | ulen = info.jited_prog_len; |
Sandipan Das | 4d56a76 | 2018-05-24 12:26:51 +0530 | [diff] [blame] | 3717 | if (prog->aux->func_cnt) { |
| 3718 | u32 i; |
| 3719 | |
| 3720 | info.jited_prog_len = 0; |
| 3721 | for (i = 0; i < prog->aux->func_cnt; i++) |
| 3722 | info.jited_prog_len += prog->aux->func[i]->jited_len; |
| 3723 | } else { |
| 3724 | info.jited_prog_len = prog->jited_len; |
| 3725 | } |
| 3726 | |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 3727 | if (info.jited_prog_len && ulen) { |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 3728 | if (bpf_dump_raw_ok(file->f_cred)) { |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 3729 | uinsns = u64_to_user_ptr(info.jited_prog_insns); |
| 3730 | ulen = min_t(u32, info.jited_prog_len, ulen); |
Sandipan Das | 4d56a76 | 2018-05-24 12:26:51 +0530 | [diff] [blame] | 3731 | |
| 3732 | /* for multi-function programs, copy the JITed |
| 3733 | * instructions for all the functions |
| 3734 | */ |
| 3735 | if (prog->aux->func_cnt) { |
| 3736 | u32 len, free, i; |
| 3737 | u8 *img; |
| 3738 | |
| 3739 | free = ulen; |
| 3740 | for (i = 0; i < prog->aux->func_cnt; i++) { |
| 3741 | len = prog->aux->func[i]->jited_len; |
| 3742 | len = min_t(u32, len, free); |
| 3743 | img = (u8 *) prog->aux->func[i]->bpf_func; |
| 3744 | if (copy_to_user(uinsns, img, len)) |
| 3745 | return -EFAULT; |
| 3746 | uinsns += len; |
| 3747 | free -= len; |
| 3748 | if (!free) |
| 3749 | break; |
| 3750 | } |
| 3751 | } else { |
| 3752 | if (copy_to_user(uinsns, prog->bpf_func, ulen)) |
| 3753 | return -EFAULT; |
| 3754 | } |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 3755 | } else { |
| 3756 | info.jited_prog_insns = 0; |
| 3757 | } |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 3758 | } |
| 3759 | |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 3760 | ulen = info.nr_jited_ksyms; |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 3761 | info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; |
Song Liu | 7a5725d | 2018-12-10 11:17:50 -0800 | [diff] [blame] | 3762 | if (ulen) { |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 3763 | if (bpf_dump_raw_ok(file->f_cred)) { |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 3764 | unsigned long ksym_addr; |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 3765 | u64 __user *user_ksyms; |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 3766 | u32 i; |
| 3767 | |
| 3768 | /* copy the address of the kernel symbol |
| 3769 | * corresponding to each function |
| 3770 | */ |
| 3771 | ulen = min_t(u32, info.nr_jited_ksyms, ulen); |
| 3772 | user_ksyms = u64_to_user_ptr(info.jited_ksyms); |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 3773 | if (prog->aux->func_cnt) { |
| 3774 | for (i = 0; i < ulen; i++) { |
| 3775 | ksym_addr = (unsigned long) |
| 3776 | prog->aux->func[i]->bpf_func; |
| 3777 | if (put_user((u64) ksym_addr, |
| 3778 | &user_ksyms[i])) |
| 3779 | return -EFAULT; |
| 3780 | } |
| 3781 | } else { |
| 3782 | ksym_addr = (unsigned long) prog->bpf_func; |
| 3783 | if (put_user((u64) ksym_addr, &user_ksyms[0])) |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 3784 | return -EFAULT; |
| 3785 | } |
| 3786 | } else { |
| 3787 | info.jited_ksyms = 0; |
| 3788 | } |
| 3789 | } |
| 3790 | |
Sandipan Das | 815581c | 2018-05-24 12:26:52 +0530 | [diff] [blame] | 3791 | ulen = info.nr_jited_func_lens; |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 3792 | info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; |
Song Liu | 7a5725d | 2018-12-10 11:17:50 -0800 | [diff] [blame] | 3793 | if (ulen) { |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 3794 | if (bpf_dump_raw_ok(file->f_cred)) { |
Sandipan Das | 815581c | 2018-05-24 12:26:52 +0530 | [diff] [blame] | 3795 | u32 __user *user_lens; |
| 3796 | u32 func_len, i; |
| 3797 | |
| 3798 | /* copy the JITed image lengths for each function */ |
| 3799 | ulen = min_t(u32, info.nr_jited_func_lens, ulen); |
| 3800 | user_lens = u64_to_user_ptr(info.jited_func_lens); |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 3801 | if (prog->aux->func_cnt) { |
| 3802 | for (i = 0; i < ulen; i++) { |
| 3803 | func_len = |
| 3804 | prog->aux->func[i]->jited_len; |
| 3805 | if (put_user(func_len, &user_lens[i])) |
| 3806 | return -EFAULT; |
| 3807 | } |
| 3808 | } else { |
| 3809 | func_len = prog->jited_len; |
| 3810 | if (put_user(func_len, &user_lens[0])) |
Sandipan Das | 815581c | 2018-05-24 12:26:52 +0530 | [diff] [blame] | 3811 | return -EFAULT; |
| 3812 | } |
| 3813 | } else { |
| 3814 | info.jited_func_lens = 0; |
| 3815 | } |
| 3816 | } |
| 3817 | |
Martin KaFai Lau | 7337224 | 2018-12-05 17:35:43 -0800 | [diff] [blame] | 3818 | if (prog->aux->btf) |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3819 | info.btf_id = btf_obj_id(prog->aux->btf); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 3820 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3821 | ulen = info.nr_func_info; |
| 3822 | info.nr_func_info = prog->aux->func_info_cnt; |
| 3823 | if (info.nr_func_info && ulen) { |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 3824 | char __user *user_finfo; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 3825 | |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 3826 | user_finfo = u64_to_user_ptr(info.func_info); |
| 3827 | ulen = min_t(u32, info.nr_func_info, ulen); |
| 3828 | if (copy_to_user(user_finfo, prog->aux->func_info, |
| 3829 | info.func_info_rec_size * ulen)) |
| 3830 | return -EFAULT; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 3831 | } |
| 3832 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3833 | ulen = info.nr_line_info; |
| 3834 | info.nr_line_info = prog->aux->nr_linfo; |
| 3835 | if (info.nr_line_info && ulen) { |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 3836 | __u8 __user *user_linfo; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3837 | |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 3838 | user_linfo = u64_to_user_ptr(info.line_info); |
| 3839 | ulen = min_t(u32, info.nr_line_info, ulen); |
| 3840 | if (copy_to_user(user_linfo, prog->aux->linfo, |
| 3841 | info.line_info_rec_size * ulen)) |
| 3842 | return -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3843 | } |
| 3844 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3845 | ulen = info.nr_jited_line_info; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3846 | if (prog->aux->jited_linfo) |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3847 | info.nr_jited_line_info = prog->aux->nr_linfo; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3848 | else |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3849 | info.nr_jited_line_info = 0; |
| 3850 | if (info.nr_jited_line_info && ulen) { |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 3851 | if (bpf_dump_raw_ok(file->f_cred)) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3852 | __u64 __user *user_linfo; |
| 3853 | u32 i; |
| 3854 | |
| 3855 | user_linfo = u64_to_user_ptr(info.jited_line_info); |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 3856 | ulen = min_t(u32, info.nr_jited_line_info, ulen); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 3857 | for (i = 0; i < ulen; i++) { |
| 3858 | if (put_user((__u64)(long)prog->aux->jited_linfo[i], |
| 3859 | &user_linfo[i])) |
| 3860 | return -EFAULT; |
| 3861 | } |
| 3862 | } else { |
| 3863 | info.jited_line_info = 0; |
| 3864 | } |
| 3865 | } |
| 3866 | |
Song Liu | c872bdb | 2018-12-12 09:37:46 -0800 | [diff] [blame] | 3867 | ulen = info.nr_prog_tags; |
| 3868 | info.nr_prog_tags = prog->aux->func_cnt ? : 1; |
| 3869 | if (ulen) { |
| 3870 | __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; |
| 3871 | u32 i; |
| 3872 | |
| 3873 | user_prog_tags = u64_to_user_ptr(info.prog_tags); |
| 3874 | ulen = min_t(u32, info.nr_prog_tags, ulen); |
| 3875 | if (prog->aux->func_cnt) { |
| 3876 | for (i = 0; i < ulen; i++) { |
| 3877 | if (copy_to_user(user_prog_tags[i], |
| 3878 | prog->aux->func[i]->tag, |
| 3879 | BPF_TAG_SIZE)) |
| 3880 | return -EFAULT; |
| 3881 | } |
| 3882 | } else { |
| 3883 | if (copy_to_user(user_prog_tags[0], |
| 3884 | prog->tag, BPF_TAG_SIZE)) |
| 3885 | return -EFAULT; |
| 3886 | } |
| 3887 | } |
| 3888 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3889 | done: |
| 3890 | if (copy_to_user(uinfo, &info, info_len) || |
| 3891 | put_user(info_len, &uattr->info.info_len)) |
| 3892 | return -EFAULT; |
| 3893 | |
| 3894 | return 0; |
| 3895 | } |
| 3896 | |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 3897 | static int bpf_map_get_info_by_fd(struct file *file, |
| 3898 | struct bpf_map *map, |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3899 | const union bpf_attr *attr, |
| 3900 | union bpf_attr __user *uattr) |
| 3901 | { |
| 3902 | 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] | 3903 | struct bpf_map_info info; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3904 | u32 info_len = attr->info.info_len; |
| 3905 | int err; |
| 3906 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 3907 | err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3908 | if (err) |
| 3909 | return err; |
| 3910 | info_len = min_t(u32, sizeof(info), info_len); |
| 3911 | |
Greg Kroah-Hartman | 5c6f258 | 2020-03-20 17:22:58 +0100 | [diff] [blame] | 3912 | memset(&info, 0, sizeof(info)); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3913 | info.type = map->map_type; |
| 3914 | info.id = map->id; |
| 3915 | info.key_size = map->key_size; |
| 3916 | info.value_size = map->value_size; |
| 3917 | info.max_entries = map->max_entries; |
| 3918 | info.map_flags = map->map_flags; |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 3919 | info.map_extra = map->map_extra; |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 3920 | memcpy(info.name, map->name, sizeof(map->name)); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3921 | |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 3922 | if (map->btf) { |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3923 | info.btf_id = btf_obj_id(map->btf); |
Martin KaFai Lau | 9b2cf32 | 2018-05-22 14:57:21 -0700 | [diff] [blame] | 3924 | info.btf_key_type_id = map->btf_key_type_id; |
| 3925 | info.btf_value_type_id = map->btf_value_type_id; |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 3926 | } |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 3927 | 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] | 3928 | |
Jakub Kicinski | 52775b3 | 2018-01-17 19:13:28 -0800 | [diff] [blame] | 3929 | if (bpf_map_is_dev_bound(map)) { |
| 3930 | err = bpf_map_offload_info_fill(&info, map); |
| 3931 | if (err) |
| 3932 | return err; |
| 3933 | } |
| 3934 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3935 | if (copy_to_user(uinfo, &info, info_len) || |
| 3936 | put_user(info_len, &uattr->info.info_len)) |
| 3937 | return -EFAULT; |
| 3938 | |
| 3939 | return 0; |
| 3940 | } |
| 3941 | |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 3942 | static int bpf_btf_get_info_by_fd(struct file *file, |
| 3943 | struct btf *btf, |
Martin KaFai Lau | 62dab84 | 2018-05-04 14:49:52 -0700 | [diff] [blame] | 3944 | const union bpf_attr *attr, |
| 3945 | union bpf_attr __user *uattr) |
| 3946 | { |
| 3947 | struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 3948 | u32 info_len = attr->info.info_len; |
| 3949 | int err; |
| 3950 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 3951 | err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len); |
Martin KaFai Lau | 62dab84 | 2018-05-04 14:49:52 -0700 | [diff] [blame] | 3952 | if (err) |
| 3953 | return err; |
| 3954 | |
| 3955 | return btf_get_info_by_fd(btf, attr, uattr); |
| 3956 | } |
| 3957 | |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 3958 | static int bpf_link_get_info_by_fd(struct file *file, |
| 3959 | struct bpf_link *link, |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 3960 | const union bpf_attr *attr, |
| 3961 | union bpf_attr __user *uattr) |
| 3962 | { |
| 3963 | struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 3964 | struct bpf_link_info info; |
| 3965 | u32 info_len = attr->info.info_len; |
| 3966 | int err; |
| 3967 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 3968 | err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 3969 | if (err) |
| 3970 | return err; |
| 3971 | info_len = min_t(u32, sizeof(info), info_len); |
| 3972 | |
| 3973 | memset(&info, 0, sizeof(info)); |
| 3974 | if (copy_from_user(&info, uinfo, info_len)) |
| 3975 | return -EFAULT; |
| 3976 | |
| 3977 | info.type = link->type; |
| 3978 | info.id = link->id; |
| 3979 | info.prog_id = link->prog->aux->id; |
| 3980 | |
| 3981 | if (link->ops->fill_link_info) { |
| 3982 | err = link->ops->fill_link_info(link, &info); |
| 3983 | if (err) |
| 3984 | return err; |
| 3985 | } |
| 3986 | |
| 3987 | if (copy_to_user(uinfo, &info, info_len) || |
| 3988 | put_user(info_len, &uattr->info.info_len)) |
| 3989 | return -EFAULT; |
| 3990 | |
| 3991 | return 0; |
| 3992 | } |
| 3993 | |
| 3994 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 3995 | #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info |
| 3996 | |
| 3997 | static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, |
| 3998 | union bpf_attr __user *uattr) |
| 3999 | { |
| 4000 | int ufd = attr->info.bpf_fd; |
| 4001 | struct fd f; |
| 4002 | int err; |
| 4003 | |
| 4004 | if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) |
| 4005 | return -EINVAL; |
| 4006 | |
| 4007 | f = fdget(ufd); |
| 4008 | if (!f.file) |
| 4009 | return -EBADFD; |
| 4010 | |
| 4011 | if (f.file->f_op == &bpf_prog_fops) |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 4012 | err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr, |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 4013 | uattr); |
| 4014 | else if (f.file->f_op == &bpf_map_fops) |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 4015 | err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr, |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 4016 | uattr); |
Martin KaFai Lau | 60197cf | 2018-04-18 15:56:02 -0700 | [diff] [blame] | 4017 | else if (f.file->f_op == &btf_fops) |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 4018 | err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr); |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 4019 | else if (f.file->f_op == &bpf_link_fops) |
Kees Cook | 6396026 | 2020-07-02 15:45:23 -0700 | [diff] [blame] | 4020 | err = bpf_link_get_info_by_fd(f.file, f.file->private_data, |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 4021 | attr, uattr); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 4022 | else |
| 4023 | err = -EINVAL; |
| 4024 | |
| 4025 | fdput(f); |
| 4026 | return err; |
| 4027 | } |
| 4028 | |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 4029 | #define BPF_BTF_LOAD_LAST_FIELD btf_log_level |
| 4030 | |
Alexei Starovoitov | c571bd7 | 2021-05-13 17:36:08 -0700 | [diff] [blame] | 4031 | static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr) |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 4032 | { |
| 4033 | if (CHECK_ATTR(BPF_BTF_LOAD)) |
| 4034 | return -EINVAL; |
| 4035 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 4036 | if (!bpf_capable()) |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 4037 | return -EPERM; |
| 4038 | |
Alexei Starovoitov | c571bd7 | 2021-05-13 17:36:08 -0700 | [diff] [blame] | 4039 | return btf_new_fd(attr, uattr); |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 4040 | } |
| 4041 | |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 4042 | #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id |
| 4043 | |
| 4044 | static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) |
| 4045 | { |
| 4046 | if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) |
| 4047 | return -EINVAL; |
| 4048 | |
| 4049 | if (!capable(CAP_SYS_ADMIN)) |
| 4050 | return -EPERM; |
| 4051 | |
| 4052 | return btf_get_fd_by_id(attr->btf_id); |
| 4053 | } |
| 4054 | |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 4055 | static int bpf_task_fd_query_copy(const union bpf_attr *attr, |
| 4056 | union bpf_attr __user *uattr, |
| 4057 | u32 prog_id, u32 fd_type, |
| 4058 | const char *buf, u64 probe_offset, |
| 4059 | u64 probe_addr) |
| 4060 | { |
| 4061 | char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); |
| 4062 | u32 len = buf ? strlen(buf) : 0, input_len; |
| 4063 | int err = 0; |
| 4064 | |
| 4065 | if (put_user(len, &uattr->task_fd_query.buf_len)) |
| 4066 | return -EFAULT; |
| 4067 | input_len = attr->task_fd_query.buf_len; |
| 4068 | if (input_len && ubuf) { |
| 4069 | if (!len) { |
| 4070 | /* nothing to copy, just make ubuf NULL terminated */ |
| 4071 | char zero = '\0'; |
| 4072 | |
| 4073 | if (put_user(zero, ubuf)) |
| 4074 | return -EFAULT; |
| 4075 | } else if (input_len >= len + 1) { |
| 4076 | /* ubuf can hold the string with NULL terminator */ |
| 4077 | if (copy_to_user(ubuf, buf, len + 1)) |
| 4078 | return -EFAULT; |
| 4079 | } else { |
| 4080 | /* ubuf cannot hold the string with NULL terminator, |
| 4081 | * do a partial copy with NULL terminator. |
| 4082 | */ |
| 4083 | char zero = '\0'; |
| 4084 | |
| 4085 | err = -ENOSPC; |
| 4086 | if (copy_to_user(ubuf, buf, input_len - 1)) |
| 4087 | return -EFAULT; |
| 4088 | if (put_user(zero, ubuf + input_len - 1)) |
| 4089 | return -EFAULT; |
| 4090 | } |
| 4091 | } |
| 4092 | |
| 4093 | if (put_user(prog_id, &uattr->task_fd_query.prog_id) || |
| 4094 | put_user(fd_type, &uattr->task_fd_query.fd_type) || |
| 4095 | put_user(probe_offset, &uattr->task_fd_query.probe_offset) || |
| 4096 | put_user(probe_addr, &uattr->task_fd_query.probe_addr)) |
| 4097 | return -EFAULT; |
| 4098 | |
| 4099 | return err; |
| 4100 | } |
| 4101 | |
| 4102 | #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr |
| 4103 | |
| 4104 | static int bpf_task_fd_query(const union bpf_attr *attr, |
| 4105 | union bpf_attr __user *uattr) |
| 4106 | { |
| 4107 | pid_t pid = attr->task_fd_query.pid; |
| 4108 | u32 fd = attr->task_fd_query.fd; |
| 4109 | const struct perf_event *event; |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 4110 | struct task_struct *task; |
| 4111 | struct file *file; |
| 4112 | int err; |
| 4113 | |
| 4114 | if (CHECK_ATTR(BPF_TASK_FD_QUERY)) |
| 4115 | return -EINVAL; |
| 4116 | |
| 4117 | if (!capable(CAP_SYS_ADMIN)) |
| 4118 | return -EPERM; |
| 4119 | |
| 4120 | if (attr->task_fd_query.flags != 0) |
| 4121 | return -EINVAL; |
| 4122 | |
| 4123 | task = get_pid_task(find_vpid(pid), PIDTYPE_PID); |
| 4124 | if (!task) |
| 4125 | return -ENOENT; |
| 4126 | |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 4127 | err = 0; |
Eric W. Biederman | b48845a | 2020-11-20 17:14:22 -0600 | [diff] [blame] | 4128 | file = fget_task(task, fd); |
| 4129 | put_task_struct(task); |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 4130 | if (!file) |
Eric W. Biederman | b48845a | 2020-11-20 17:14:22 -0600 | [diff] [blame] | 4131 | return -EBADF; |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 4132 | |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 4133 | if (file->f_op == &bpf_link_fops) { |
| 4134 | struct bpf_link *link = file->private_data; |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 4135 | |
Andrii Nakryiko | a3b80e1 | 2020-04-28 17:16:06 -0700 | [diff] [blame] | 4136 | if (link->ops == &bpf_raw_tp_link_lops) { |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 4137 | struct bpf_raw_tp_link *raw_tp = |
| 4138 | container_of(link, struct bpf_raw_tp_link, link); |
| 4139 | struct bpf_raw_event_map *btp = raw_tp->btp; |
| 4140 | |
| 4141 | err = bpf_task_fd_query_copy(attr, uattr, |
| 4142 | raw_tp->link.prog->aux->id, |
| 4143 | BPF_FD_TYPE_RAW_TRACEPOINT, |
| 4144 | btp->tp->name, 0, 0); |
| 4145 | goto put_file; |
| 4146 | } |
| 4147 | goto out_not_supp; |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 4148 | } |
| 4149 | |
| 4150 | event = perf_get_event(file); |
| 4151 | if (!IS_ERR(event)) { |
| 4152 | u64 probe_offset, probe_addr; |
| 4153 | u32 prog_id, fd_type; |
| 4154 | const char *buf; |
| 4155 | |
| 4156 | err = bpf_get_perf_event_info(event, &prog_id, &fd_type, |
| 4157 | &buf, &probe_offset, |
| 4158 | &probe_addr); |
| 4159 | if (!err) |
| 4160 | err = bpf_task_fd_query_copy(attr, uattr, prog_id, |
| 4161 | fd_type, buf, |
| 4162 | probe_offset, |
| 4163 | probe_addr); |
| 4164 | goto put_file; |
| 4165 | } |
| 4166 | |
Andrii Nakryiko | 70ed506 | 2020-03-02 20:31:57 -0800 | [diff] [blame] | 4167 | out_not_supp: |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 4168 | err = -ENOTSUPP; |
| 4169 | put_file: |
| 4170 | fput(file); |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 4171 | return err; |
| 4172 | } |
| 4173 | |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 4174 | #define BPF_MAP_BATCH_LAST_FIELD batch.flags |
| 4175 | |
| 4176 | #define BPF_DO_BATCH(fn) \ |
| 4177 | do { \ |
| 4178 | if (!fn) { \ |
| 4179 | err = -ENOTSUPP; \ |
| 4180 | goto err_put; \ |
| 4181 | } \ |
| 4182 | err = fn(map, attr, uattr); \ |
| 4183 | } while (0) |
| 4184 | |
| 4185 | static int bpf_map_do_batch(const union bpf_attr *attr, |
| 4186 | union bpf_attr __user *uattr, |
| 4187 | int cmd) |
| 4188 | { |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 4189 | bool has_read = cmd == BPF_MAP_LOOKUP_BATCH || |
| 4190 | cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH; |
| 4191 | bool has_write = cmd != BPF_MAP_LOOKUP_BATCH; |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 4192 | struct bpf_map *map; |
| 4193 | int err, ufd; |
| 4194 | struct fd f; |
| 4195 | |
| 4196 | if (CHECK_ATTR(BPF_MAP_BATCH)) |
| 4197 | return -EINVAL; |
| 4198 | |
| 4199 | ufd = attr->batch.map_fd; |
| 4200 | f = fdget(ufd); |
| 4201 | map = __bpf_map_get(f); |
| 4202 | if (IS_ERR(map)) |
| 4203 | return PTR_ERR(map); |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 4204 | if (has_write) |
| 4205 | bpf_map_write_active_inc(map); |
| 4206 | if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 4207 | err = -EPERM; |
| 4208 | goto err_put; |
| 4209 | } |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 4210 | if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 4211 | err = -EPERM; |
| 4212 | goto err_put; |
| 4213 | } |
| 4214 | |
| 4215 | if (cmd == BPF_MAP_LOOKUP_BATCH) |
| 4216 | BPF_DO_BATCH(map->ops->map_lookup_batch); |
Yonghong Song | 0579963 | 2020-01-15 10:43:04 -0800 | [diff] [blame] | 4217 | else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) |
| 4218 | BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch); |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 4219 | else if (cmd == BPF_MAP_UPDATE_BATCH) |
| 4220 | BPF_DO_BATCH(map->ops->map_update_batch); |
| 4221 | else |
| 4222 | BPF_DO_BATCH(map->ops->map_delete_batch); |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 4223 | err_put: |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 4224 | if (has_write) |
| 4225 | bpf_map_write_active_dec(map); |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 4226 | fdput(f); |
| 4227 | return err; |
| 4228 | } |
| 4229 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4230 | static int tracing_bpf_link_attach(const union bpf_attr *attr, bpfptr_t uattr, |
| 4231 | struct bpf_prog *prog) |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 4232 | { |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 4233 | if (attr->link_create.attach_type != prog->expected_attach_type) |
| 4234 | return -EINVAL; |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 4235 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 4236 | if (prog->expected_attach_type == BPF_TRACE_ITER) |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4237 | return bpf_iter_link_attach(attr, uattr, prog); |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 4238 | else if (prog->type == BPF_PROG_TYPE_EXT) |
| 4239 | return bpf_tracing_prog_attach(prog, |
| 4240 | attr->link_create.target_fd, |
| 4241 | attr->link_create.target_btf_id); |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 4242 | return -EINVAL; |
| 4243 | } |
| 4244 | |
Yonghong Song | 5e7b302 | 2020-08-04 22:50:56 -0700 | [diff] [blame] | 4245 | #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4246 | static int link_create(union bpf_attr *attr, bpfptr_t uattr) |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 4247 | { |
| 4248 | enum bpf_prog_type ptype; |
| 4249 | struct bpf_prog *prog; |
| 4250 | int ret; |
| 4251 | |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 4252 | if (CHECK_ATTR(BPF_LINK_CREATE)) |
| 4253 | return -EINVAL; |
| 4254 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 4255 | prog = bpf_prog_get(attr->link_create.prog_fd); |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 4256 | if (IS_ERR(prog)) |
| 4257 | return PTR_ERR(prog); |
| 4258 | |
| 4259 | ret = bpf_prog_attach_check_attach_type(prog, |
| 4260 | attr->link_create.attach_type); |
| 4261 | if (ret) |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 4262 | goto out; |
| 4263 | |
Andrii Nakryiko | b89fbfb | 2021-08-15 00:05:57 -0700 | [diff] [blame] | 4264 | switch (prog->type) { |
| 4265 | case BPF_PROG_TYPE_EXT: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4266 | ret = tracing_bpf_link_attach(attr, uattr, prog); |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 4267 | goto out; |
Andrii Nakryiko | b89fbfb | 2021-08-15 00:05:57 -0700 | [diff] [blame] | 4268 | case BPF_PROG_TYPE_PERF_EVENT: |
| 4269 | case BPF_PROG_TYPE_KPROBE: |
| 4270 | case BPF_PROG_TYPE_TRACEPOINT: |
| 4271 | if (attr->link_create.attach_type != BPF_PERF_EVENT) { |
| 4272 | ret = -EINVAL; |
| 4273 | goto out; |
| 4274 | } |
| 4275 | ptype = prog->type; |
| 4276 | break; |
| 4277 | default: |
| 4278 | ptype = attach_type_to_prog_type(attr->link_create.attach_type); |
| 4279 | if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) { |
| 4280 | ret = -EINVAL; |
| 4281 | goto out; |
| 4282 | } |
| 4283 | break; |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 4284 | } |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 4285 | |
| 4286 | switch (ptype) { |
| 4287 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 4288 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 4289 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 4290 | case BPF_PROG_TYPE_SOCK_OPS: |
| 4291 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
| 4292 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
| 4293 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 4294 | ret = cgroup_bpf_link_attach(attr, prog); |
| 4295 | break; |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 4296 | case BPF_PROG_TYPE_TRACING: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4297 | ret = tracing_bpf_link_attach(attr, uattr, prog); |
Yonghong Song | de4e05c | 2020-05-09 10:59:01 -0700 | [diff] [blame] | 4298 | break; |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 4299 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 4300 | case BPF_PROG_TYPE_SK_LOOKUP: |
Jakub Sitnicki | 7f045a4 | 2020-05-31 10:28:38 +0200 | [diff] [blame] | 4301 | ret = netns_bpf_link_create(attr, prog); |
| 4302 | break; |
Andrii Nakryiko | 310ad79 | 2020-07-28 12:05:27 -0700 | [diff] [blame] | 4303 | #ifdef CONFIG_NET |
Andrii Nakryiko | aa8d3a7 | 2020-07-21 23:45:57 -0700 | [diff] [blame] | 4304 | case BPF_PROG_TYPE_XDP: |
| 4305 | ret = bpf_xdp_link_attach(attr, prog); |
| 4306 | break; |
Andrii Nakryiko | 310ad79 | 2020-07-28 12:05:27 -0700 | [diff] [blame] | 4307 | #endif |
Andrii Nakryiko | b89fbfb | 2021-08-15 00:05:57 -0700 | [diff] [blame] | 4308 | #ifdef CONFIG_PERF_EVENTS |
| 4309 | case BPF_PROG_TYPE_PERF_EVENT: |
| 4310 | case BPF_PROG_TYPE_TRACEPOINT: |
| 4311 | case BPF_PROG_TYPE_KPROBE: |
| 4312 | ret = bpf_perf_link_attach(attr, prog); |
| 4313 | break; |
| 4314 | #endif |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 4315 | default: |
| 4316 | ret = -EINVAL; |
| 4317 | } |
| 4318 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 4319 | out: |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 4320 | if (ret < 0) |
| 4321 | bpf_prog_put(prog); |
| 4322 | return ret; |
| 4323 | } |
| 4324 | |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 4325 | #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd |
| 4326 | |
| 4327 | static int link_update(union bpf_attr *attr) |
| 4328 | { |
| 4329 | struct bpf_prog *old_prog = NULL, *new_prog; |
| 4330 | struct bpf_link *link; |
| 4331 | u32 flags; |
| 4332 | int ret; |
| 4333 | |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 4334 | if (CHECK_ATTR(BPF_LINK_UPDATE)) |
| 4335 | return -EINVAL; |
| 4336 | |
| 4337 | flags = attr->link_update.flags; |
| 4338 | if (flags & ~BPF_F_REPLACE) |
| 4339 | return -EINVAL; |
| 4340 | |
| 4341 | link = bpf_link_get_from_fd(attr->link_update.link_fd); |
| 4342 | if (IS_ERR(link)) |
| 4343 | return PTR_ERR(link); |
| 4344 | |
| 4345 | new_prog = bpf_prog_get(attr->link_update.new_prog_fd); |
Andrii Nakryiko | 4adb7a4 | 2020-04-23 22:20:44 -0700 | [diff] [blame] | 4346 | if (IS_ERR(new_prog)) { |
| 4347 | ret = PTR_ERR(new_prog); |
| 4348 | goto out_put_link; |
| 4349 | } |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 4350 | |
| 4351 | if (flags & BPF_F_REPLACE) { |
| 4352 | old_prog = bpf_prog_get(attr->link_update.old_prog_fd); |
| 4353 | if (IS_ERR(old_prog)) { |
| 4354 | ret = PTR_ERR(old_prog); |
| 4355 | old_prog = NULL; |
| 4356 | goto out_put_progs; |
| 4357 | } |
Andrii Nakryiko | 4adb7a4 | 2020-04-23 22:20:44 -0700 | [diff] [blame] | 4358 | } else if (attr->link_update.old_prog_fd) { |
| 4359 | ret = -EINVAL; |
| 4360 | goto out_put_progs; |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 4361 | } |
| 4362 | |
Andrii Nakryiko | f9d0412 | 2020-04-28 17:16:05 -0700 | [diff] [blame] | 4363 | if (link->ops->update_prog) |
| 4364 | ret = link->ops->update_prog(link, new_prog, old_prog); |
| 4365 | else |
Jakub Sitnicki | fe53739 | 2020-05-25 14:29:28 +0200 | [diff] [blame] | 4366 | ret = -EINVAL; |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 4367 | |
| 4368 | out_put_progs: |
| 4369 | if (old_prog) |
| 4370 | bpf_prog_put(old_prog); |
| 4371 | if (ret) |
| 4372 | bpf_prog_put(new_prog); |
Andrii Nakryiko | 4adb7a4 | 2020-04-23 22:20:44 -0700 | [diff] [blame] | 4373 | out_put_link: |
| 4374 | bpf_link_put(link); |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 4375 | return ret; |
| 4376 | } |
| 4377 | |
Andrii Nakryiko | 73b11c2a | 2020-07-31 11:28:26 -0700 | [diff] [blame] | 4378 | #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd |
| 4379 | |
| 4380 | static int link_detach(union bpf_attr *attr) |
| 4381 | { |
| 4382 | struct bpf_link *link; |
| 4383 | int ret; |
| 4384 | |
| 4385 | if (CHECK_ATTR(BPF_LINK_DETACH)) |
| 4386 | return -EINVAL; |
| 4387 | |
| 4388 | link = bpf_link_get_from_fd(attr->link_detach.link_fd); |
| 4389 | if (IS_ERR(link)) |
| 4390 | return PTR_ERR(link); |
| 4391 | |
| 4392 | if (link->ops->detach) |
| 4393 | ret = link->ops->detach(link); |
| 4394 | else |
| 4395 | ret = -EOPNOTSUPP; |
| 4396 | |
| 4397 | bpf_link_put(link); |
| 4398 | return ret; |
| 4399 | } |
| 4400 | |
Alexei Starovoitov | 005142b | 2020-08-18 21:27:56 -0700 | [diff] [blame] | 4401 | static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link) |
Andrii Nakryiko | 2d602c8 | 2020-04-28 17:16:07 -0700 | [diff] [blame] | 4402 | { |
Alexei Starovoitov | 005142b | 2020-08-18 21:27:56 -0700 | [diff] [blame] | 4403 | return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT); |
| 4404 | } |
| 4405 | |
| 4406 | struct bpf_link *bpf_link_by_id(u32 id) |
| 4407 | { |
| 4408 | struct bpf_link *link; |
| 4409 | |
| 4410 | if (!id) |
| 4411 | return ERR_PTR(-ENOENT); |
| 4412 | |
| 4413 | spin_lock_bh(&link_idr_lock); |
| 4414 | /* before link is "settled", ID is 0, pretend it doesn't exist yet */ |
| 4415 | link = idr_find(&link_idr, id); |
| 4416 | if (link) { |
| 4417 | if (link->id) |
| 4418 | link = bpf_link_inc_not_zero(link); |
| 4419 | else |
| 4420 | link = ERR_PTR(-EAGAIN); |
| 4421 | } else { |
| 4422 | link = ERR_PTR(-ENOENT); |
| 4423 | } |
| 4424 | spin_unlock_bh(&link_idr_lock); |
| 4425 | return link; |
Andrii Nakryiko | 2d602c8 | 2020-04-28 17:16:07 -0700 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id |
| 4429 | |
| 4430 | static int bpf_link_get_fd_by_id(const union bpf_attr *attr) |
| 4431 | { |
| 4432 | struct bpf_link *link; |
| 4433 | u32 id = attr->link_id; |
Alexei Starovoitov | 005142b | 2020-08-18 21:27:56 -0700 | [diff] [blame] | 4434 | int fd; |
Andrii Nakryiko | 2d602c8 | 2020-04-28 17:16:07 -0700 | [diff] [blame] | 4435 | |
| 4436 | if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID)) |
| 4437 | return -EINVAL; |
| 4438 | |
| 4439 | if (!capable(CAP_SYS_ADMIN)) |
| 4440 | return -EPERM; |
| 4441 | |
Alexei Starovoitov | 005142b | 2020-08-18 21:27:56 -0700 | [diff] [blame] | 4442 | link = bpf_link_by_id(id); |
| 4443 | if (IS_ERR(link)) |
| 4444 | return PTR_ERR(link); |
Andrii Nakryiko | 2d602c8 | 2020-04-28 17:16:07 -0700 | [diff] [blame] | 4445 | |
| 4446 | fd = bpf_link_new_fd(link); |
| 4447 | if (fd < 0) |
| 4448 | bpf_link_put(link); |
| 4449 | |
| 4450 | return fd; |
| 4451 | } |
| 4452 | |
Song Liu | d46edd6 | 2020-04-30 00:15:04 -0700 | [diff] [blame] | 4453 | DEFINE_MUTEX(bpf_stats_enabled_mutex); |
| 4454 | |
| 4455 | static int bpf_stats_release(struct inode *inode, struct file *file) |
| 4456 | { |
| 4457 | mutex_lock(&bpf_stats_enabled_mutex); |
| 4458 | static_key_slow_dec(&bpf_stats_enabled_key.key); |
| 4459 | mutex_unlock(&bpf_stats_enabled_mutex); |
| 4460 | return 0; |
| 4461 | } |
| 4462 | |
| 4463 | static const struct file_operations bpf_stats_fops = { |
| 4464 | .release = bpf_stats_release, |
| 4465 | }; |
| 4466 | |
| 4467 | static int bpf_enable_runtime_stats(void) |
| 4468 | { |
| 4469 | int fd; |
| 4470 | |
| 4471 | mutex_lock(&bpf_stats_enabled_mutex); |
| 4472 | |
| 4473 | /* Set a very high limit to avoid overflow */ |
| 4474 | if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) { |
| 4475 | mutex_unlock(&bpf_stats_enabled_mutex); |
| 4476 | return -EBUSY; |
| 4477 | } |
| 4478 | |
| 4479 | fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC); |
| 4480 | if (fd >= 0) |
| 4481 | static_key_slow_inc(&bpf_stats_enabled_key.key); |
| 4482 | |
| 4483 | mutex_unlock(&bpf_stats_enabled_mutex); |
| 4484 | return fd; |
| 4485 | } |
| 4486 | |
| 4487 | #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type |
| 4488 | |
| 4489 | static int bpf_enable_stats(union bpf_attr *attr) |
| 4490 | { |
| 4491 | |
| 4492 | if (CHECK_ATTR(BPF_ENABLE_STATS)) |
| 4493 | return -EINVAL; |
| 4494 | |
| 4495 | if (!capable(CAP_SYS_ADMIN)) |
| 4496 | return -EPERM; |
| 4497 | |
| 4498 | switch (attr->enable_stats.type) { |
| 4499 | case BPF_STATS_RUN_TIME: |
| 4500 | return bpf_enable_runtime_stats(); |
| 4501 | default: |
| 4502 | break; |
| 4503 | } |
| 4504 | return -EINVAL; |
| 4505 | } |
| 4506 | |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 4507 | #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags |
| 4508 | |
| 4509 | static int bpf_iter_create(union bpf_attr *attr) |
| 4510 | { |
| 4511 | struct bpf_link *link; |
| 4512 | int err; |
| 4513 | |
| 4514 | if (CHECK_ATTR(BPF_ITER_CREATE)) |
| 4515 | return -EINVAL; |
| 4516 | |
| 4517 | if (attr->iter_create.flags) |
| 4518 | return -EINVAL; |
| 4519 | |
| 4520 | link = bpf_link_get_from_fd(attr->iter_create.link_fd); |
| 4521 | if (IS_ERR(link)) |
| 4522 | return PTR_ERR(link); |
| 4523 | |
| 4524 | err = bpf_iter_new_fd(link); |
| 4525 | bpf_link_put(link); |
| 4526 | |
| 4527 | return err; |
| 4528 | } |
| 4529 | |
YiFei Zhu | ef15314 | 2020-09-15 16:45:40 -0700 | [diff] [blame] | 4530 | #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags |
| 4531 | |
| 4532 | static int bpf_prog_bind_map(union bpf_attr *attr) |
| 4533 | { |
| 4534 | struct bpf_prog *prog; |
| 4535 | struct bpf_map *map; |
| 4536 | struct bpf_map **used_maps_old, **used_maps_new; |
| 4537 | int i, ret = 0; |
| 4538 | |
| 4539 | if (CHECK_ATTR(BPF_PROG_BIND_MAP)) |
| 4540 | return -EINVAL; |
| 4541 | |
| 4542 | if (attr->prog_bind_map.flags) |
| 4543 | return -EINVAL; |
| 4544 | |
| 4545 | prog = bpf_prog_get(attr->prog_bind_map.prog_fd); |
| 4546 | if (IS_ERR(prog)) |
| 4547 | return PTR_ERR(prog); |
| 4548 | |
| 4549 | map = bpf_map_get(attr->prog_bind_map.map_fd); |
| 4550 | if (IS_ERR(map)) { |
| 4551 | ret = PTR_ERR(map); |
| 4552 | goto out_prog_put; |
| 4553 | } |
| 4554 | |
| 4555 | mutex_lock(&prog->aux->used_maps_mutex); |
| 4556 | |
| 4557 | used_maps_old = prog->aux->used_maps; |
| 4558 | |
| 4559 | for (i = 0; i < prog->aux->used_map_cnt; i++) |
Stanislav Fomichev | 1028ae4 | 2020-10-02 17:25:44 -0700 | [diff] [blame] | 4560 | if (used_maps_old[i] == map) { |
| 4561 | bpf_map_put(map); |
YiFei Zhu | ef15314 | 2020-09-15 16:45:40 -0700 | [diff] [blame] | 4562 | goto out_unlock; |
Stanislav Fomichev | 1028ae4 | 2020-10-02 17:25:44 -0700 | [diff] [blame] | 4563 | } |
YiFei Zhu | ef15314 | 2020-09-15 16:45:40 -0700 | [diff] [blame] | 4564 | |
| 4565 | used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1, |
| 4566 | sizeof(used_maps_new[0]), |
| 4567 | GFP_KERNEL); |
| 4568 | if (!used_maps_new) { |
| 4569 | ret = -ENOMEM; |
| 4570 | goto out_unlock; |
| 4571 | } |
| 4572 | |
| 4573 | memcpy(used_maps_new, used_maps_old, |
| 4574 | sizeof(used_maps_old[0]) * prog->aux->used_map_cnt); |
| 4575 | used_maps_new[prog->aux->used_map_cnt] = map; |
| 4576 | |
| 4577 | prog->aux->used_map_cnt++; |
| 4578 | prog->aux->used_maps = used_maps_new; |
| 4579 | |
| 4580 | kfree(used_maps_old); |
| 4581 | |
| 4582 | out_unlock: |
| 4583 | mutex_unlock(&prog->aux->used_maps_mutex); |
| 4584 | |
| 4585 | if (ret) |
| 4586 | bpf_map_put(map); |
| 4587 | out_prog_put: |
| 4588 | bpf_prog_put(prog); |
| 4589 | return ret; |
| 4590 | } |
| 4591 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4592 | static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 4593 | { |
Greg Kroah-Hartman | 8096f22 | 2020-03-20 10:48:13 +0100 | [diff] [blame] | 4594 | union bpf_attr attr; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 4595 | int err; |
| 4596 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 4597 | if (sysctl_unprivileged_bpf_disabled && !bpf_capable()) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 4598 | return -EPERM; |
| 4599 | |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 4600 | err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 4601 | if (err) |
| 4602 | return err; |
| 4603 | size = min_t(u32, size, sizeof(attr)); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 4604 | |
| 4605 | /* 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] | 4606 | memset(&attr, 0, sizeof(attr)); |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4607 | if (copy_from_bpfptr(&attr, uattr, size) != 0) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 4608 | return -EFAULT; |
| 4609 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 4610 | err = security_bpf(cmd, &attr, size); |
| 4611 | if (err < 0) |
| 4612 | return err; |
| 4613 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 4614 | switch (cmd) { |
| 4615 | case BPF_MAP_CREATE: |
| 4616 | err = map_create(&attr); |
| 4617 | break; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 4618 | case BPF_MAP_LOOKUP_ELEM: |
| 4619 | err = map_lookup_elem(&attr); |
| 4620 | break; |
| 4621 | case BPF_MAP_UPDATE_ELEM: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4622 | err = map_update_elem(&attr, uattr); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 4623 | break; |
| 4624 | case BPF_MAP_DELETE_ELEM: |
| 4625 | err = map_delete_elem(&attr); |
| 4626 | break; |
| 4627 | case BPF_MAP_GET_NEXT_KEY: |
| 4628 | err = map_get_next_key(&attr); |
| 4629 | break; |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 4630 | case BPF_MAP_FREEZE: |
| 4631 | err = map_freeze(&attr); |
| 4632 | break; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 4633 | case BPF_PROG_LOAD: |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4634 | err = bpf_prog_load(&attr, uattr); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 4635 | break; |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 4636 | case BPF_OBJ_PIN: |
| 4637 | err = bpf_obj_pin(&attr); |
| 4638 | break; |
| 4639 | case BPF_OBJ_GET: |
| 4640 | err = bpf_obj_get(&attr); |
| 4641 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 4642 | case BPF_PROG_ATTACH: |
| 4643 | err = bpf_prog_attach(&attr); |
| 4644 | break; |
| 4645 | case BPF_PROG_DETACH: |
| 4646 | err = bpf_prog_detach(&attr); |
| 4647 | break; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 4648 | case BPF_PROG_QUERY: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4649 | err = bpf_prog_query(&attr, uattr.user); |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 4650 | break; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 4651 | case BPF_PROG_TEST_RUN: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4652 | err = bpf_prog_test_run(&attr, uattr.user); |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 4653 | break; |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 4654 | case BPF_PROG_GET_NEXT_ID: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4655 | err = bpf_obj_get_next_id(&attr, uattr.user, |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 4656 | &prog_idr, &prog_idr_lock); |
| 4657 | break; |
| 4658 | case BPF_MAP_GET_NEXT_ID: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4659 | err = bpf_obj_get_next_id(&attr, uattr.user, |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 4660 | &map_idr, &map_idr_lock); |
| 4661 | break; |
Quentin Monnet | 1b9ed84 | 2019-08-20 10:31:50 +0100 | [diff] [blame] | 4662 | case BPF_BTF_GET_NEXT_ID: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4663 | err = bpf_obj_get_next_id(&attr, uattr.user, |
Quentin Monnet | 1b9ed84 | 2019-08-20 10:31:50 +0100 | [diff] [blame] | 4664 | &btf_idr, &btf_idr_lock); |
| 4665 | break; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 4666 | case BPF_PROG_GET_FD_BY_ID: |
| 4667 | err = bpf_prog_get_fd_by_id(&attr); |
| 4668 | break; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 4669 | case BPF_MAP_GET_FD_BY_ID: |
| 4670 | err = bpf_map_get_fd_by_id(&attr); |
| 4671 | break; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 4672 | case BPF_OBJ_GET_INFO_BY_FD: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4673 | err = bpf_obj_get_info_by_fd(&attr, uattr.user); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 4674 | break; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 4675 | case BPF_RAW_TRACEPOINT_OPEN: |
| 4676 | err = bpf_raw_tracepoint_open(&attr); |
| 4677 | break; |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 4678 | case BPF_BTF_LOAD: |
Alexei Starovoitov | c571bd7 | 2021-05-13 17:36:08 -0700 | [diff] [blame] | 4679 | err = bpf_btf_load(&attr, uattr); |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 4680 | break; |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 4681 | case BPF_BTF_GET_FD_BY_ID: |
| 4682 | err = bpf_btf_get_fd_by_id(&attr); |
| 4683 | break; |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 4684 | case BPF_TASK_FD_QUERY: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4685 | err = bpf_task_fd_query(&attr, uattr.user); |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 4686 | break; |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 4687 | case BPF_MAP_LOOKUP_AND_DELETE_ELEM: |
| 4688 | err = map_lookup_and_delete_elem(&attr); |
| 4689 | break; |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 4690 | case BPF_MAP_LOOKUP_BATCH: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4691 | err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH); |
Brian Vazquez | cb4d03a | 2020-01-15 10:43:01 -0800 | [diff] [blame] | 4692 | break; |
Yonghong Song | 0579963 | 2020-01-15 10:43:04 -0800 | [diff] [blame] | 4693 | case BPF_MAP_LOOKUP_AND_DELETE_BATCH: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4694 | err = bpf_map_do_batch(&attr, uattr.user, |
Yonghong Song | 0579963 | 2020-01-15 10:43:04 -0800 | [diff] [blame] | 4695 | BPF_MAP_LOOKUP_AND_DELETE_BATCH); |
| 4696 | break; |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 4697 | case BPF_MAP_UPDATE_BATCH: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4698 | err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH); |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 4699 | break; |
| 4700 | case BPF_MAP_DELETE_BATCH: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4701 | err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH); |
Brian Vazquez | aa2e93b | 2020-01-15 10:43:02 -0800 | [diff] [blame] | 4702 | break; |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 4703 | case BPF_LINK_CREATE: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4704 | err = link_create(&attr, uattr); |
Andrii Nakryiko | af6eea5 | 2020-03-29 19:59:58 -0700 | [diff] [blame] | 4705 | break; |
Andrii Nakryiko | 0c991eb | 2020-03-29 19:59:59 -0700 | [diff] [blame] | 4706 | case BPF_LINK_UPDATE: |
| 4707 | err = link_update(&attr); |
| 4708 | break; |
Andrii Nakryiko | 2d602c8 | 2020-04-28 17:16:07 -0700 | [diff] [blame] | 4709 | case BPF_LINK_GET_FD_BY_ID: |
| 4710 | err = bpf_link_get_fd_by_id(&attr); |
| 4711 | break; |
| 4712 | case BPF_LINK_GET_NEXT_ID: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4713 | err = bpf_obj_get_next_id(&attr, uattr.user, |
Andrii Nakryiko | 2d602c8 | 2020-04-28 17:16:07 -0700 | [diff] [blame] | 4714 | &link_idr, &link_idr_lock); |
| 4715 | break; |
Song Liu | d46edd6 | 2020-04-30 00:15:04 -0700 | [diff] [blame] | 4716 | case BPF_ENABLE_STATS: |
| 4717 | err = bpf_enable_stats(&attr); |
| 4718 | break; |
Yonghong Song | ac51d99 | 2020-05-09 10:59:05 -0700 | [diff] [blame] | 4719 | case BPF_ITER_CREATE: |
| 4720 | err = bpf_iter_create(&attr); |
| 4721 | break; |
Andrii Nakryiko | 73b11c2a | 2020-07-31 11:28:26 -0700 | [diff] [blame] | 4722 | case BPF_LINK_DETACH: |
| 4723 | err = link_detach(&attr); |
| 4724 | break; |
YiFei Zhu | ef15314 | 2020-09-15 16:45:40 -0700 | [diff] [blame] | 4725 | case BPF_PROG_BIND_MAP: |
| 4726 | err = bpf_prog_bind_map(&attr); |
| 4727 | break; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 4728 | default: |
| 4729 | err = -EINVAL; |
| 4730 | break; |
| 4731 | } |
| 4732 | |
| 4733 | return err; |
| 4734 | } |
Alexei Starovoitov | 79a7f8b | 2021-05-13 17:36:03 -0700 | [diff] [blame] | 4735 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4736 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
| 4737 | { |
| 4738 | return __sys_bpf(cmd, USER_BPFPTR(uattr), size); |
| 4739 | } |
| 4740 | |
Alexei Starovoitov | 79a7f8b | 2021-05-13 17:36:03 -0700 | [diff] [blame] | 4741 | static bool syscall_prog_is_valid_access(int off, int size, |
| 4742 | enum bpf_access_type type, |
| 4743 | const struct bpf_prog *prog, |
| 4744 | struct bpf_insn_access_aux *info) |
| 4745 | { |
| 4746 | if (off < 0 || off >= U16_MAX) |
| 4747 | return false; |
| 4748 | if (off % size != 0) |
| 4749 | return false; |
| 4750 | return true; |
| 4751 | } |
| 4752 | |
| 4753 | BPF_CALL_3(bpf_sys_bpf, int, cmd, void *, attr, u32, attr_size) |
| 4754 | { |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4755 | switch (cmd) { |
| 4756 | case BPF_MAP_CREATE: |
| 4757 | case BPF_MAP_UPDATE_ELEM: |
| 4758 | case BPF_MAP_FREEZE: |
| 4759 | case BPF_PROG_LOAD: |
Alexei Starovoitov | c571bd7 | 2021-05-13 17:36:08 -0700 | [diff] [blame] | 4760 | case BPF_BTF_LOAD: |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 4761 | break; |
| 4762 | /* case BPF_PROG_TEST_RUN: |
| 4763 | * is not part of this list to prevent recursive test_run |
| 4764 | */ |
| 4765 | default: |
| 4766 | return -EINVAL; |
| 4767 | } |
| 4768 | return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size); |
Alexei Starovoitov | 79a7f8b | 2021-05-13 17:36:03 -0700 | [diff] [blame] | 4769 | } |
| 4770 | |
Pu Lehui | 3a2daa7 | 2021-05-19 14:41:16 +0800 | [diff] [blame] | 4771 | static const struct bpf_func_proto bpf_sys_bpf_proto = { |
Alexei Starovoitov | 79a7f8b | 2021-05-13 17:36:03 -0700 | [diff] [blame] | 4772 | .func = bpf_sys_bpf, |
| 4773 | .gpl_only = false, |
| 4774 | .ret_type = RET_INTEGER, |
| 4775 | .arg1_type = ARG_ANYTHING, |
Hao Luo | 216e3cd | 2021-12-16 16:31:51 -0800 | [diff] [blame] | 4776 | .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, |
Alexei Starovoitov | 79a7f8b | 2021-05-13 17:36:03 -0700 | [diff] [blame] | 4777 | .arg3_type = ARG_CONST_SIZE, |
| 4778 | }; |
| 4779 | |
| 4780 | const struct bpf_func_proto * __weak |
| 4781 | tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
| 4782 | { |
| 4783 | return bpf_base_func_proto(func_id); |
| 4784 | } |
| 4785 | |
Alexei Starovoitov | 3abea08 | 2021-05-13 17:36:12 -0700 | [diff] [blame] | 4786 | BPF_CALL_1(bpf_sys_close, u32, fd) |
| 4787 | { |
| 4788 | /* When bpf program calls this helper there should not be |
| 4789 | * an fdget() without matching completed fdput(). |
| 4790 | * This helper is allowed in the following callchain only: |
| 4791 | * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close |
| 4792 | */ |
| 4793 | return close_fd(fd); |
| 4794 | } |
| 4795 | |
Pu Lehui | 3a2daa7 | 2021-05-19 14:41:16 +0800 | [diff] [blame] | 4796 | static const struct bpf_func_proto bpf_sys_close_proto = { |
Alexei Starovoitov | 3abea08 | 2021-05-13 17:36:12 -0700 | [diff] [blame] | 4797 | .func = bpf_sys_close, |
| 4798 | .gpl_only = false, |
| 4799 | .ret_type = RET_INTEGER, |
| 4800 | .arg1_type = ARG_ANYTHING, |
| 4801 | }; |
| 4802 | |
Kumar Kartikeya Dwivedi | d6aef08 | 2021-10-28 12:04:54 +0530 | [diff] [blame] | 4803 | BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res) |
| 4804 | { |
| 4805 | if (flags) |
| 4806 | return -EINVAL; |
| 4807 | |
| 4808 | if (name_sz <= 1 || name[name_sz - 1]) |
| 4809 | return -EINVAL; |
| 4810 | |
| 4811 | if (!bpf_dump_raw_ok(current_cred())) |
| 4812 | return -EPERM; |
| 4813 | |
| 4814 | *res = kallsyms_lookup_name(name); |
| 4815 | return *res ? 0 : -ENOENT; |
| 4816 | } |
| 4817 | |
| 4818 | const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = { |
| 4819 | .func = bpf_kallsyms_lookup_name, |
| 4820 | .gpl_only = false, |
| 4821 | .ret_type = RET_INTEGER, |
| 4822 | .arg1_type = ARG_PTR_TO_MEM, |
Kumar Kartikeya Dwivedi | d4efb17 | 2021-11-23 05:27:31 +0530 | [diff] [blame] | 4823 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, |
Kumar Kartikeya Dwivedi | d6aef08 | 2021-10-28 12:04:54 +0530 | [diff] [blame] | 4824 | .arg3_type = ARG_ANYTHING, |
| 4825 | .arg4_type = ARG_PTR_TO_LONG, |
| 4826 | }; |
| 4827 | |
Alexei Starovoitov | 79a7f8b | 2021-05-13 17:36:03 -0700 | [diff] [blame] | 4828 | static const struct bpf_func_proto * |
| 4829 | syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
| 4830 | { |
| 4831 | switch (func_id) { |
| 4832 | case BPF_FUNC_sys_bpf: |
| 4833 | return &bpf_sys_bpf_proto; |
Alexei Starovoitov | 3d78417 | 2021-05-13 17:36:11 -0700 | [diff] [blame] | 4834 | case BPF_FUNC_btf_find_by_name_kind: |
| 4835 | return &bpf_btf_find_by_name_kind_proto; |
Alexei Starovoitov | 3abea08 | 2021-05-13 17:36:12 -0700 | [diff] [blame] | 4836 | case BPF_FUNC_sys_close: |
| 4837 | return &bpf_sys_close_proto; |
Kumar Kartikeya Dwivedi | d6aef08 | 2021-10-28 12:04:54 +0530 | [diff] [blame] | 4838 | case BPF_FUNC_kallsyms_lookup_name: |
| 4839 | return &bpf_kallsyms_lookup_name_proto; |
Alexei Starovoitov | 79a7f8b | 2021-05-13 17:36:03 -0700 | [diff] [blame] | 4840 | default: |
| 4841 | return tracing_prog_func_proto(func_id, prog); |
| 4842 | } |
| 4843 | } |
| 4844 | |
| 4845 | const struct bpf_verifier_ops bpf_syscall_verifier_ops = { |
| 4846 | .get_func_proto = syscall_prog_func_proto, |
| 4847 | .is_valid_access = syscall_prog_is_valid_access, |
| 4848 | }; |
| 4849 | |
| 4850 | const struct bpf_prog_ops bpf_syscall_prog_ops = { |
| 4851 | .test_run = bpf_prog_test_run_syscall, |
| 4852 | }; |