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