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