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