blob: 941ca06d9dfa1714439f6d13ddfbc1ea607b4d2a [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003 */
4#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01005#include <linux/bpf_trace.h>
Sean Youngf4364dc2018-05-27 12:24:09 +01006#include <linux/bpf_lirc.h>
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02007#include <linux/bpf_verifier.h>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07008#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07009#include <linux/syscalls.h>
10#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010011#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010012#include <linux/vmalloc.h>
13#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070014#include <linux/anon_inodes.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070015#include <linux/fdtable.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070016#include <linux/file.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070017#include <linux/fs.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070018#include <linux/license.h>
19#include <linux/filter.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010020#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070021#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070022#include <linux/cred.h>
23#include <linux/timekeeping.h>
24#include <linux/ctype.h>
Mark Rutland9ef09e32018-05-03 17:04:59 +010025#include <linux/nospec.h>
Daniel Borkmannbae141f2019-12-06 22:49:34 +010026#include <linux/audit.h>
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -070027#include <uapi/linux/btf.h>
Mike Rapoportca5999f2020-06-08 21:32:38 -070028#include <linux/pgtable.h>
KP Singh9e4e01d2020-03-29 01:43:52 +010029#include <linux/bpf_lsm.h>
Andrii Nakryiko457f4432020-05-29 00:54:20 -070030#include <linux/poll.h>
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +020031#include <linux/bpf-netns.h>
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -070032#include <linux/rcupdate_trace.h>
Roman Gushchin48edc1f2020-12-01 13:58:32 -080033#include <linux/memcontrol.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070034
Daniel Borkmannda765a22019-11-22 21:07:58 +010035#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 Lau14dc6f02017-06-27 23:08:34 -070039#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
Daniel Borkmannda765a22019-11-22 21:07:58 +010040#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
41 IS_FD_HASH(map))
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070042
Chenbo Feng6e71b042017-10-18 13:00:22 -070043#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
44
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080045DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070046static DEFINE_IDR(prog_idr);
47static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070048static DEFINE_IDR(map_idr);
49static DEFINE_SPINLOCK(map_idr_lock);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -070050static DEFINE_IDR(link_idr);
51static DEFINE_SPINLOCK(link_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080052
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070053int sysctl_unprivileged_bpf_disabled __read_mostly;
54
Johannes Berg40077e02017-04-11 15:34:58 +020055static const struct bpf_map_ops * const bpf_map_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -080056#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
Johannes Berg40077e02017-04-11 15:34:58 +020057#define BPF_MAP_TYPE(_id, _ops) \
58 [_id] = &_ops,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -070059#define BPF_LINK_TYPE(_id, _name)
Johannes Berg40077e02017-04-11 15:34:58 +020060#include <linux/bpf_types.h>
61#undef BPF_PROG_TYPE
62#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -070063#undef BPF_LINK_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +020064};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070065
Mickaël Salaün752ba562017-08-07 20:45:20 +020066/*
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 Laudcab51f2018-05-22 15:03:31 -070075int bpf_check_uarg_tail_zero(void __user *uaddr,
76 size_t expected_size,
77 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020078{
Al Virob7e4b652020-05-08 00:16:31 -040079 unsigned char __user *addr = uaddr + expected_size;
80 int res;
Mickaël Salaün58291a72017-08-07 20:45:19 +020081
Mickaël Salaün752ba562017-08-07 20:45:20 +020082 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
83 return -E2BIG;
84
Mickaël Salaün58291a72017-08-07 20:45:19 +020085 if (actual_size <= expected_size)
86 return 0;
87
Al Virob7e4b652020-05-08 00:16:31 -040088 res = check_zeroed_user(addr, actual_size - expected_size);
89 if (res < 0)
90 return res;
91 return res ? 0 : -E2BIG;
Mickaël Salaün58291a72017-08-07 20:45:19 +020092}
93
Jakub Kicinskia3884572018-01-11 20:29:09 -080094const struct bpf_map_ops bpf_map_offload_ops = {
Martin KaFai Lauf4d05252020-08-27 18:18:06 -070095 .map_meta_equal = bpf_map_meta_equal,
Jakub Kicinskia3884572018-01-11 20:29:09 -080096 .map_alloc = bpf_map_offload_map_alloc,
97 .map_free = bpf_map_offload_map_free,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020098 .map_check_btf = map_check_no_btf,
Jakub Kicinskia3884572018-01-11 20:29:09 -080099};
100
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700101static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
102{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800103 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100104 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700105 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800106 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700107
Mark Rutland9ef09e32018-05-03 17:04:59 +0100108 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800109 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100110 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
111 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800112 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200113 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700114
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800115 if (ops->map_alloc_check) {
116 err = ops->map_alloc_check(attr);
117 if (err)
118 return ERR_PTR(err);
119 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800120 if (attr->map_ifindex)
121 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800122 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200123 if (IS_ERR(map))
124 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800125 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100126 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200127 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700128}
129
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800130static u32 bpf_map_value_size(const struct bpf_map *map)
Brian Vazquez15c14a32020-01-15 10:43:00 -0800131{
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
143static 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
154static 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 Vazquez15c14a32020-01-15 10:43:00 -0800163 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
164 return map->ops->map_update_elem(map, key, value, flags);
Lorenz Bauer13b79d32020-08-21 11:29:45 +0100165 } 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 Vazquez15c14a32020-01-15 10:43:00 -0800168 } 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 Gleixnerb6e5dae2020-02-24 15:01:49 +0100173 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800174 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 Gleixnerb6e5dae2020-02-24 15:01:49 +0100204 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800205 maybe_wait_bpf_programs(map);
206
207 return err;
208}
209
210static 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 Vazquezcb4d03a2020-01-15 10:43:01 -0800216 if (bpf_map_is_dev_bound(map))
217 return bpf_map_offload_lookup_elem(map, key, value);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800218
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100219 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800220 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 Gleixnerb6e5dae2020-02-24 15:01:49 +0100264 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800265 maybe_wait_bpf_programs(map);
266
267 return err;
268}
269
Roman Gushchind5299b62020-12-01 13:58:33 -0800270/* Please, do not use this function outside from the map creation path
271 * (e.g. in map update path) without taking care of setting the active
272 * memory cgroup (see at bpf_map_kmalloc_node() for example).
273 */
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100274static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100275{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100276 /* We really just want to fail instead of triggering OOM killer
277 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
278 * which is used for lower order allocation requests.
279 *
280 * It has been observed that higher order allocation requests done by
281 * vmalloc with __GFP_NORETRY being set might fail due to not trying
282 * to reclaim memory from the page cache, thus we set
283 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100284 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100285
Roman Gushchind5299b62020-12-01 13:58:33 -0800286 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT;
Christoph Hellwig041de932020-06-01 21:52:02 -0700287 unsigned int flags = 0;
288 unsigned long align = 1;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100289 void *area;
290
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100291 if (size >= SIZE_MAX)
292 return NULL;
293
Andrii Nakryikofc970222019-11-17 09:28:04 -0800294 /* kmalloc()'ed memory can't be mmap()'ed */
Christoph Hellwig041de932020-06-01 21:52:02 -0700295 if (mmapable) {
296 BUG_ON(!PAGE_ALIGNED(size));
297 align = SHMLBA;
298 flags = VM_USERMAP;
299 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
300 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100301 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100302 if (area != NULL)
303 return area;
304 }
Christoph Hellwig041de932020-06-01 21:52:02 -0700305
306 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
307 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
308 flags, numa_node, __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100309}
310
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100311void *bpf_map_area_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800312{
313 return __bpf_map_area_alloc(size, numa_node, false);
314}
315
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100316void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800317{
318 return __bpf_map_area_alloc(size, numa_node, true);
319}
320
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100321void bpf_map_area_free(void *area)
322{
323 kvfree(area);
324}
325
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200326static u32 bpf_map_flags_retain_permanent(u32 flags)
327{
328 /* Some map creation flags are not tied to the map object but
329 * rather to the map fd instead, so they have no meaning upon
330 * map object inspection since multiple file descriptors with
331 * different (access) properties can exist here. Thus, given
332 * this has zero meaning for the map itself, lets clear these
333 * from here.
334 */
335 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
336}
337
Jakub Kicinskibd475642018-01-11 20:29:06 -0800338void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
339{
340 map->map_type = attr->map_type;
341 map->key_size = attr->key_size;
342 map->value_size = attr->value_size;
343 map->max_entries = attr->max_entries;
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200344 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800345 map->numa_node = bpf_map_attr_numa_node(attr);
346}
347
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700348static int bpf_map_alloc_id(struct bpf_map *map)
349{
350 int id;
351
Shaohua Lib76354c2018-03-27 11:53:21 -0700352 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700353 spin_lock_bh(&map_idr_lock);
354 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
355 if (id > 0)
356 map->id = id;
357 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700358 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700359
360 if (WARN_ON_ONCE(!id))
361 return -ENOSPC;
362
363 return id > 0 ? 0 : id;
364}
365
Jakub Kicinskia3884572018-01-11 20:29:09 -0800366void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700367{
Eric Dumazet930651a2017-09-19 09:15:59 -0700368 unsigned long flags;
369
Jakub Kicinskia3884572018-01-11 20:29:09 -0800370 /* Offloaded maps are removed from the IDR store when their device
371 * disappears - even if someone holds an fd to them they are unusable,
372 * the memory is gone, all ops will fail; they are simply waiting for
373 * refcnt to drop to be freed.
374 */
375 if (!map->id)
376 return;
377
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700378 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700379 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700380 else
381 __acquire(&map_idr_lock);
382
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700383 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800384 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700385
386 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700387 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700388 else
389 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700390}
391
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800392#ifdef CONFIG_MEMCG_KMEM
393static void bpf_map_save_memcg(struct bpf_map *map)
394{
395 map->memcg = get_mem_cgroup_from_mm(current->mm);
396}
397
398static void bpf_map_release_memcg(struct bpf_map *map)
399{
400 mem_cgroup_put(map->memcg);
401}
402
403void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
404 int node)
405{
406 struct mem_cgroup *old_memcg;
407 void *ptr;
408
409 old_memcg = set_active_memcg(map->memcg);
410 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
411 set_active_memcg(old_memcg);
412
413 return ptr;
414}
415
416void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
417{
418 struct mem_cgroup *old_memcg;
419 void *ptr;
420
421 old_memcg = set_active_memcg(map->memcg);
422 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
423 set_active_memcg(old_memcg);
424
425 return ptr;
426}
427
428void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
429 size_t align, gfp_t flags)
430{
431 struct mem_cgroup *old_memcg;
432 void __percpu *ptr;
433
434 old_memcg = set_active_memcg(map->memcg);
435 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
436 set_active_memcg(old_memcg);
437
438 return ptr;
439}
440
441#else
442static void bpf_map_save_memcg(struct bpf_map *map)
443{
444}
445
446static void bpf_map_release_memcg(struct bpf_map *map)
447{
448}
449#endif
450
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700451/* called from workqueue */
452static void bpf_map_free_deferred(struct work_struct *work)
453{
454 struct bpf_map *map = container_of(work, struct bpf_map, work);
455
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700456 security_bpf_map_free(map);
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800457 bpf_map_release_memcg(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700458 /* implementation dependent freeing */
459 map->ops->map_free(map);
460}
461
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100462static void bpf_map_put_uref(struct bpf_map *map)
463{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800464 if (atomic64_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700465 if (map->ops->map_release_uref)
466 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100467 }
468}
469
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700470/* decrement map refcnt and schedule it for freeing via workqueue
471 * (unrelying map implementation ops->map_free() might sleep)
472 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700473static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700474{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800475 if (atomic64_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700476 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700477 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700478 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700479 INIT_WORK(&map->work, bpf_map_free_deferred);
480 schedule_work(&map->work);
481 }
482}
483
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700484void bpf_map_put(struct bpf_map *map)
485{
486 __bpf_map_put(map, true);
487}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700488EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700489
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100490void bpf_map_put_with_uref(struct bpf_map *map)
491{
492 bpf_map_put_uref(map);
493 bpf_map_put(map);
494}
495
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700496static int bpf_map_release(struct inode *inode, struct file *filp)
497{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200498 struct bpf_map *map = filp->private_data;
499
500 if (map->ops->map_release)
501 map->ops->map_release(map, filp);
502
503 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700504 return 0;
505}
506
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200507static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
508{
509 fmode_t mode = f.file->f_mode;
510
511 /* Our file permissions may have been overridden by global
512 * map permissions facing syscall side.
513 */
514 if (READ_ONCE(map->frozen))
515 mode &= ~FMODE_CAN_WRITE;
516 return mode;
517}
518
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100519#ifdef CONFIG_PROC_FS
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800520/* Provides an approximation of the map's memory footprint.
521 * Used only to provide a backward compatibility and display
522 * a reasonable "memlock" info.
523 */
524static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
525{
526 unsigned long size;
527
528 size = round_up(map->key_size + bpf_map_value_size(map), 8);
529
530 return round_up(map->max_entries * size, PAGE_SIZE);
531}
532
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100533static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
534{
535 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100536 const struct bpf_array *array;
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100537 u32 type = 0, jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100538
539 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
540 array = container_of(map, struct bpf_array, map);
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100541 type = array->aux->type;
542 jited = array->aux->jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100543 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100544
545 seq_printf(m,
546 "map_type:\t%u\n"
547 "key_size:\t%u\n"
548 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100549 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100550 "map_flags:\t%#x\n"
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800551 "memlock:\t%lu\n"
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200552 "map_id:\t%u\n"
553 "frozen:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100554 map->map_type,
555 map->key_size,
556 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100557 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100558 map->map_flags,
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800559 bpf_map_memory_footprint(map),
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200560 map->id,
561 READ_ONCE(map->frozen));
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100562 if (type) {
563 seq_printf(m, "owner_prog_type:\t%u\n", type);
564 seq_printf(m, "owner_jited:\t%u\n", jited);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200565 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100566}
567#endif
568
Chenbo Feng6e71b042017-10-18 13:00:22 -0700569static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
570 loff_t *ppos)
571{
572 /* We need this handler such that alloc_file() enables
573 * f_mode with FMODE_CAN_READ.
574 */
575 return -EINVAL;
576}
577
578static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
579 size_t siz, loff_t *ppos)
580{
581 /* We need this handler such that alloc_file() enables
582 * f_mode with FMODE_CAN_WRITE.
583 */
584 return -EINVAL;
585}
586
Andrii Nakryikofc970222019-11-17 09:28:04 -0800587/* called for any extra memory-mapped regions (except initial) */
588static void bpf_map_mmap_open(struct vm_area_struct *vma)
589{
590 struct bpf_map *map = vma->vm_file->private_data;
591
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700592 if (vma->vm_flags & VM_MAYWRITE) {
Andrii Nakryikofc970222019-11-17 09:28:04 -0800593 mutex_lock(&map->freeze_mutex);
594 map->writecnt++;
595 mutex_unlock(&map->freeze_mutex);
596 }
597}
598
599/* called for all unmapped memory region (including initial) */
600static void bpf_map_mmap_close(struct vm_area_struct *vma)
601{
602 struct bpf_map *map = vma->vm_file->private_data;
603
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700604 if (vma->vm_flags & VM_MAYWRITE) {
Andrii Nakryikofc970222019-11-17 09:28:04 -0800605 mutex_lock(&map->freeze_mutex);
606 map->writecnt--;
607 mutex_unlock(&map->freeze_mutex);
608 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800609}
610
611static const struct vm_operations_struct bpf_map_default_vmops = {
612 .open = bpf_map_mmap_open,
613 .close = bpf_map_mmap_close,
614};
615
616static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
617{
618 struct bpf_map *map = filp->private_data;
619 int err;
620
621 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
622 return -ENOTSUPP;
623
624 if (!(vma->vm_flags & VM_SHARED))
625 return -EINVAL;
626
627 mutex_lock(&map->freeze_mutex);
628
Andrii Nakryikodfeb3762020-05-18 22:38:24 -0700629 if (vma->vm_flags & VM_WRITE) {
630 if (map->frozen) {
631 err = -EPERM;
632 goto out;
633 }
634 /* map is meant to be read-only, so do not allow mapping as
635 * writable, because it's possible to leak a writable page
636 * reference and allows user-space to still modify it after
637 * freezing, while verifier will assume contents do not change
638 */
639 if (map->map_flags & BPF_F_RDONLY_PROG) {
640 err = -EACCES;
641 goto out;
642 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800643 }
644
645 /* set default open/close callbacks */
646 vma->vm_ops = &bpf_map_default_vmops;
647 vma->vm_private_data = map;
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700648 vma->vm_flags &= ~VM_MAYEXEC;
649 if (!(vma->vm_flags & VM_WRITE))
650 /* disallow re-mapping with PROT_WRITE */
651 vma->vm_flags &= ~VM_MAYWRITE;
Andrii Nakryikofc970222019-11-17 09:28:04 -0800652
653 err = map->ops->map_mmap(map, vma);
654 if (err)
655 goto out;
656
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700657 if (vma->vm_flags & VM_MAYWRITE)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800658 map->writecnt++;
659out:
660 mutex_unlock(&map->freeze_mutex);
661 return err;
662}
663
Andrii Nakryiko457f4432020-05-29 00:54:20 -0700664static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
665{
666 struct bpf_map *map = filp->private_data;
667
668 if (map->ops->map_poll)
669 return map->ops->map_poll(map, filp, pts);
670
671 return EPOLLERR;
672}
673
Chenbo Fengf66e4482017-10-18 13:00:26 -0700674const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100675#ifdef CONFIG_PROC_FS
676 .show_fdinfo = bpf_map_show_fdinfo,
677#endif
678 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700679 .read = bpf_dummy_read,
680 .write = bpf_dummy_write,
Andrii Nakryikofc970222019-11-17 09:28:04 -0800681 .mmap = bpf_map_mmap,
Andrii Nakryiko457f4432020-05-29 00:54:20 -0700682 .poll = bpf_map_poll,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700683};
684
Chenbo Feng6e71b042017-10-18 13:00:22 -0700685int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100686{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700687 int ret;
688
689 ret = security_bpf_map(map, OPEN_FMODE(flags));
690 if (ret < 0)
691 return ret;
692
Daniel Borkmannaa797812015-10-29 14:58:06 +0100693 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700694 flags | O_CLOEXEC);
695}
696
697int bpf_get_file_flag(int flags)
698{
699 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
700 return -EINVAL;
701 if (flags & BPF_F_RDONLY)
702 return O_RDONLY;
703 if (flags & BPF_F_WRONLY)
704 return O_WRONLY;
705 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100706}
707
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700708/* helper macro to check that unused fields 'union bpf_attr' are zero */
709#define CHECK_ATTR(CMD) \
710 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
711 sizeof(attr->CMD##_LAST_FIELD), 0, \
712 sizeof(*attr) - \
713 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
714 sizeof(attr->CMD##_LAST_FIELD)) != NULL
715
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700716/* dst and src must have at least "size" number of bytes.
717 * Return strlen on success and < 0 on error.
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700718 */
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700719int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700720{
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700721 const char *end = src + size;
722 const char *orig_src = src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700723
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700724 memset(dst, 0, size);
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200725 /* Copy all isalnum(), '_' and '.' chars. */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700726 while (src < end && *src) {
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200727 if (!isalnum(*src) &&
728 *src != '_' && *src != '.')
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700729 return -EINVAL;
730 *dst++ = *src++;
731 }
732
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700733 /* No '\0' found in "size" number of bytes */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700734 if (src == end)
735 return -EINVAL;
736
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700737 return src - orig_src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700738}
739
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200740int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800741 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200742 const struct btf_type *key_type,
743 const struct btf_type *value_type)
744{
745 return -ENOTSUPP;
746}
747
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800748static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200749 u32 btf_key_id, u32 btf_value_id)
750{
751 const struct btf_type *key_type, *value_type;
752 u32 key_size, value_size;
753 int ret = 0;
754
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200755 /* Some maps allow key to be unspecified. */
756 if (btf_key_id) {
757 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
758 if (!key_type || key_size != map->key_size)
759 return -EINVAL;
760 } else {
761 key_type = btf_type_by_id(btf, 0);
762 if (!map->ops->map_check_btf)
763 return -EINVAL;
764 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200765
766 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
767 if (!value_type || value_size != map->value_size)
768 return -EINVAL;
769
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800770 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
771
772 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +0200773 if (map->map_flags & BPF_F_RDONLY_PROG)
774 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800775 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f12019-01-31 15:40:05 -0800776 map->map_type != BPF_MAP_TYPE_ARRAY &&
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700777 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
KP Singh8ea63682020-08-25 20:29:17 +0200778 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
KP Singh4cf1bc12020-11-06 10:37:40 +0000779 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
780 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800781 return -ENOTSUPP;
782 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
783 map->value_size) {
784 WARN_ONCE(1,
785 "verifier bug spin_lock_off %d value_size %d\n",
786 map->spin_lock_off, map->value_size);
787 return -EFAULT;
788 }
789 }
790
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200791 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800792 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200793
794 return ret;
795}
796
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800797#define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700798/* called via syscall */
799static int map_create(union bpf_attr *attr)
800{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700801 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700802 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700803 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700804 int err;
805
806 err = CHECK_ATTR(BPF_MAP_CREATE);
807 if (err)
808 return -EINVAL;
809
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800810 if (attr->btf_vmlinux_value_type_id) {
811 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
812 attr->btf_key_type_id || attr->btf_value_type_id)
813 return -EINVAL;
814 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
815 return -EINVAL;
816 }
817
Chenbo Feng6e71b042017-10-18 13:00:22 -0700818 f_flags = bpf_get_file_flag(attr->map_flags);
819 if (f_flags < 0)
820 return f_flags;
821
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700822 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700823 ((unsigned int)numa_node >= nr_node_ids ||
824 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700825 return -EINVAL;
826
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700827 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
828 map = find_and_alloc_map(attr);
829 if (IS_ERR(map))
830 return PTR_ERR(map);
831
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700832 err = bpf_obj_name_cpy(map->name, attr->map_name,
833 sizeof(attr->map_name));
834 if (err < 0)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700835 goto free_map;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700836
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800837 atomic64_set(&map->refcnt, 1);
838 atomic64_set(&map->usercnt, 1);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800839 mutex_init(&map->freeze_mutex);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700840
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800841 map->spin_lock_off = -EINVAL;
842 if (attr->btf_key_type_id || attr->btf_value_type_id ||
843 /* Even the map's value is a kernel's struct,
844 * the bpf_prog.o must have BTF to begin with
845 * to figure out the corresponding kernel's
846 * counter part. Thus, attr->btf_fd has
847 * to be valid also.
848 */
849 attr->btf_vmlinux_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700850 struct btf *btf;
851
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700852 btf = btf_get_by_fd(attr->btf_fd);
853 if (IS_ERR(btf)) {
854 err = PTR_ERR(btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700855 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700856 }
Alexei Starovoitov350a5c42021-03-07 14:52:48 -0800857 if (btf_is_kernel(btf)) {
858 btf_put(btf);
859 err = -EACCES;
860 goto free_map;
861 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800862 map->btf = btf;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700863
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800864 if (attr->btf_value_type_id) {
865 err = map_check_btf(map, btf, attr->btf_key_type_id,
866 attr->btf_value_type_id);
867 if (err)
868 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700869 }
870
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700871 map->btf_key_type_id = attr->btf_key_type_id;
872 map->btf_value_type_id = attr->btf_value_type_id;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800873 map->btf_vmlinux_value_type_id =
874 attr->btf_vmlinux_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700875 }
876
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700877 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700878 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700879 goto free_map;
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700880
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700881 err = bpf_map_alloc_id(map);
882 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700883 goto free_map_sec;
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700884
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800885 bpf_map_save_memcg(map);
886
Chenbo Feng6e71b042017-10-18 13:00:22 -0700887 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700888 if (err < 0) {
889 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800890 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700891 * bpf_map_alloc_id() has published the map
892 * to the userspace and the userspace may
893 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
894 */
Peng Sun352d20d2019-02-27 22:36:25 +0800895 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700896 return err;
897 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700898
899 return err;
900
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700901free_map_sec:
902 security_bpf_map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700903free_map:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700904 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700905 map->ops->map_free(map);
906 return err;
907}
908
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700909/* if error is returned, fd is released.
910 * On success caller should complete fd access with matching fdput()
911 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100912struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700913{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700914 if (!f.file)
915 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700916 if (f.file->f_op != &bpf_map_fops) {
917 fdput(f);
918 return ERR_PTR(-EINVAL);
919 }
920
Daniel Borkmannc2101292015-10-29 14:58:07 +0100921 return f.file->private_data;
922}
923
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800924void bpf_map_inc(struct bpf_map *map)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100925{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800926 atomic64_inc(&map->refcnt);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100927}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700928EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100929
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800930void bpf_map_inc_with_uref(struct bpf_map *map)
931{
932 atomic64_inc(&map->refcnt);
933 atomic64_inc(&map->usercnt);
934}
935EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
936
Martin KaFai Lau1ed4d922020-02-25 15:04:21 -0800937struct bpf_map *bpf_map_get(u32 ufd)
938{
939 struct fd f = fdget(ufd);
940 struct bpf_map *map;
941
942 map = __bpf_map_get(f);
943 if (IS_ERR(map))
944 return map;
945
946 bpf_map_inc(map);
947 fdput(f);
948
949 return map;
950}
951
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100952struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100953{
954 struct fd f = fdget(ufd);
955 struct bpf_map *map;
956
957 map = __bpf_map_get(f);
958 if (IS_ERR(map))
959 return map;
960
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800961 bpf_map_inc_with_uref(map);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100962 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700963
964 return map;
965}
966
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700967/* map_idr_lock should have been held */
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800968static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700969{
970 int refold;
971
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800972 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700973 if (!refold)
974 return ERR_PTR(-ENOENT);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700975 if (uref)
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800976 atomic64_inc(&map->usercnt);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700977
978 return map;
979}
980
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800981struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700982{
983 spin_lock_bh(&map_idr_lock);
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800984 map = __bpf_map_inc_not_zero(map, false);
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700985 spin_unlock_bh(&map_idr_lock);
986
987 return map;
988}
989EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
990
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800991int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
992{
993 return -ENOTSUPP;
994}
995
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200996static void *__bpf_copy_key(void __user *ukey, u64 key_size)
997{
998 if (key_size)
999 return memdup_user(ukey, key_size);
1000
1001 if (ukey)
1002 return ERR_PTR(-EINVAL);
1003
1004 return NULL;
1005}
1006
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001007/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001008#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001009
1010static int map_lookup_elem(union bpf_attr *attr)
1011{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001012 void __user *ukey = u64_to_user_ptr(attr->key);
1013 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001014 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001015 struct bpf_map *map;
Brian Vazquez15c14a32020-01-15 10:43:00 -08001016 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001017 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001018 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001019 int err;
1020
1021 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1022 return -EINVAL;
1023
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001024 if (attr->flags & ~BPF_F_LOCK)
1025 return -EINVAL;
1026
Daniel Borkmann592867b2015-09-08 18:00:09 +02001027 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001028 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001029 if (IS_ERR(map))
1030 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001031 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001032 err = -EPERM;
1033 goto err_put;
1034 }
1035
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001036 if ((attr->flags & BPF_F_LOCK) &&
1037 !map_value_has_spin_lock(map)) {
1038 err = -EINVAL;
1039 goto err_put;
1040 }
1041
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001042 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001043 if (IS_ERR(key)) {
1044 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001045 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001046 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001047
Brian Vazquez15c14a32020-01-15 10:43:00 -08001048 value_size = bpf_map_value_size(map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001049
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001050 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001051 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001052 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001053 goto free_key;
1054
Brian Vazquez15c14a32020-01-15 10:43:00 -08001055 err = bpf_map_copy_value(map, key, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001056 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001057 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001058
1059 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001060 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001061 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001062
1063 err = 0;
1064
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001065free_value:
1066 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001067free_key:
1068 kfree(key);
1069err_put:
1070 fdput(f);
1071 return err;
1072}
1073
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001074
Alexei Starovoitov3274f522014-11-13 17:36:44 -08001075#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001076
1077static int map_update_elem(union bpf_attr *attr)
1078{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001079 void __user *ukey = u64_to_user_ptr(attr->key);
1080 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001081 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001082 struct bpf_map *map;
1083 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001084 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001085 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001086 int err;
1087
1088 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1089 return -EINVAL;
1090
Daniel Borkmann592867b2015-09-08 18:00:09 +02001091 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001092 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001093 if (IS_ERR(map))
1094 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001095 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001096 err = -EPERM;
1097 goto err_put;
1098 }
1099
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001100 if ((attr->flags & BPF_F_LOCK) &&
1101 !map_value_has_spin_lock(map)) {
1102 err = -EINVAL;
1103 goto err_put;
1104 }
1105
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001106 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001107 if (IS_ERR(key)) {
1108 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001109 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001110 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001111
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001112 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001113 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +00001114 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1115 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001116 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1117 else
1118 value_size = map->value_size;
1119
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001120 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001121 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001122 if (!value)
1123 goto free_key;
1124
1125 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001126 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001127 goto free_value;
1128
Brian Vazquez15c14a32020-01-15 10:43:00 -08001129 err = bpf_map_update_value(map, f, key, value, attr->flags);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02001130
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001131free_value:
1132 kfree(value);
1133free_key:
1134 kfree(key);
1135err_put:
1136 fdput(f);
1137 return err;
1138}
1139
1140#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1141
1142static int map_delete_elem(union bpf_attr *attr)
1143{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001144 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001145 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001146 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001147 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001148 void *key;
1149 int err;
1150
1151 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1152 return -EINVAL;
1153
Daniel Borkmann592867b2015-09-08 18:00:09 +02001154 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001155 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001156 if (IS_ERR(map))
1157 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001158 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001159 err = -EPERM;
1160 goto err_put;
1161 }
1162
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001163 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001164 if (IS_ERR(key)) {
1165 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001166 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001167 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001168
Jakub Kicinskia3884572018-01-11 20:29:09 -08001169 if (bpf_map_is_dev_bound(map)) {
1170 err = bpf_map_offload_delete_elem(map, key);
1171 goto out;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001172 } else if (IS_FD_PROG_ARRAY(map) ||
1173 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1174 /* These maps require sleepable context */
Daniel Borkmannda765a22019-11-22 21:07:58 +01001175 err = map->ops->map_delete_elem(map, key);
1176 goto out;
Jakub Kicinskia3884572018-01-11 20:29:09 -08001177 }
1178
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001179 bpf_disable_instrumentation();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001180 rcu_read_lock();
1181 err = map->ops->map_delete_elem(map, key);
1182 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001183 bpf_enable_instrumentation();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001184 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001185out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001186 kfree(key);
1187err_put:
1188 fdput(f);
1189 return err;
1190}
1191
1192/* last field in 'union bpf_attr' used by this command */
1193#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1194
1195static int map_get_next_key(union bpf_attr *attr)
1196{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001197 void __user *ukey = u64_to_user_ptr(attr->key);
1198 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001199 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001200 struct bpf_map *map;
1201 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001202 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001203 int err;
1204
1205 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1206 return -EINVAL;
1207
Daniel Borkmann592867b2015-09-08 18:00:09 +02001208 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001209 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001210 if (IS_ERR(map))
1211 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001212 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001213 err = -EPERM;
1214 goto err_put;
1215 }
1216
Teng Qin8fe45922017-04-24 19:00:37 -07001217 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001218 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001219 if (IS_ERR(key)) {
1220 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001221 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001222 }
Teng Qin8fe45922017-04-24 19:00:37 -07001223 } else {
1224 key = NULL;
1225 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001226
1227 err = -ENOMEM;
1228 next_key = kmalloc(map->key_size, GFP_USER);
1229 if (!next_key)
1230 goto free_key;
1231
Jakub Kicinskia3884572018-01-11 20:29:09 -08001232 if (bpf_map_is_dev_bound(map)) {
1233 err = bpf_map_offload_get_next_key(map, key, next_key);
1234 goto out;
1235 }
1236
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001237 rcu_read_lock();
1238 err = map->ops->map_get_next_key(map, key, next_key);
1239 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001240out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001241 if (err)
1242 goto free_next_key;
1243
1244 err = -EFAULT;
1245 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1246 goto free_next_key;
1247
1248 err = 0;
1249
1250free_next_key:
1251 kfree(next_key);
1252free_key:
1253 kfree(key);
1254err_put:
1255 fdput(f);
1256 return err;
1257}
1258
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001259int generic_map_delete_batch(struct bpf_map *map,
1260 const union bpf_attr *attr,
1261 union bpf_attr __user *uattr)
1262{
1263 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1264 u32 cp, max_count;
1265 int err = 0;
1266 void *key;
1267
1268 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1269 return -EINVAL;
1270
1271 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1272 !map_value_has_spin_lock(map)) {
1273 return -EINVAL;
1274 }
1275
1276 max_count = attr->batch.count;
1277 if (!max_count)
1278 return 0;
1279
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001280 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1281 if (!key)
1282 return -ENOMEM;
1283
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001284 for (cp = 0; cp < max_count; cp++) {
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001285 err = -EFAULT;
1286 if (copy_from_user(key, keys + cp * map->key_size,
1287 map->key_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001288 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001289
1290 if (bpf_map_is_dev_bound(map)) {
1291 err = bpf_map_offload_delete_elem(map, key);
1292 break;
1293 }
1294
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001295 bpf_disable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001296 rcu_read_lock();
1297 err = map->ops->map_delete_elem(map, key);
1298 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001299 bpf_enable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001300 maybe_wait_bpf_programs(map);
1301 if (err)
1302 break;
1303 }
1304 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1305 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001306
1307 kfree(key);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001308 return err;
1309}
1310
1311int generic_map_update_batch(struct bpf_map *map,
1312 const union bpf_attr *attr,
1313 union bpf_attr __user *uattr)
1314{
1315 void __user *values = u64_to_user_ptr(attr->batch.values);
1316 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1317 u32 value_size, cp, max_count;
1318 int ufd = attr->map_fd;
1319 void *key, *value;
1320 struct fd f;
1321 int err = 0;
1322
1323 f = fdget(ufd);
1324 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1325 return -EINVAL;
1326
1327 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1328 !map_value_has_spin_lock(map)) {
1329 return -EINVAL;
1330 }
1331
1332 value_size = bpf_map_value_size(map);
1333
1334 max_count = attr->batch.count;
1335 if (!max_count)
1336 return 0;
1337
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001338 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1339 if (!key)
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001340 return -ENOMEM;
1341
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001342 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1343 if (!value) {
1344 kfree(key);
1345 return -ENOMEM;
1346 }
1347
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001348 for (cp = 0; cp < max_count; cp++) {
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001349 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001350 if (copy_from_user(key, keys + cp * map->key_size,
1351 map->key_size) ||
1352 copy_from_user(value, values + cp * value_size, value_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001353 break;
1354
1355 err = bpf_map_update_value(map, f, key, value,
1356 attr->batch.elem_flags);
1357
1358 if (err)
1359 break;
1360 }
1361
1362 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1363 err = -EFAULT;
1364
1365 kfree(value);
1366 kfree(key);
1367 return err;
1368}
1369
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001370#define MAP_LOOKUP_RETRIES 3
1371
1372int generic_map_lookup_batch(struct bpf_map *map,
1373 const union bpf_attr *attr,
1374 union bpf_attr __user *uattr)
1375{
1376 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1377 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1378 void __user *values = u64_to_user_ptr(attr->batch.values);
1379 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1380 void *buf, *buf_prevkey, *prev_key, *key, *value;
1381 int err, retry = MAP_LOOKUP_RETRIES;
1382 u32 value_size, cp, max_count;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001383
1384 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1385 return -EINVAL;
1386
1387 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1388 !map_value_has_spin_lock(map))
1389 return -EINVAL;
1390
1391 value_size = bpf_map_value_size(map);
1392
1393 max_count = attr->batch.count;
1394 if (!max_count)
1395 return 0;
1396
1397 if (put_user(0, &uattr->batch.count))
1398 return -EFAULT;
1399
1400 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1401 if (!buf_prevkey)
1402 return -ENOMEM;
1403
1404 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1405 if (!buf) {
Denis Efremovbb2359f2020-06-01 19:28:14 +03001406 kfree(buf_prevkey);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001407 return -ENOMEM;
1408 }
1409
1410 err = -EFAULT;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001411 prev_key = NULL;
1412 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1413 goto free_buf;
1414 key = buf;
1415 value = key + map->key_size;
1416 if (ubatch)
1417 prev_key = buf_prevkey;
1418
1419 for (cp = 0; cp < max_count;) {
1420 rcu_read_lock();
1421 err = map->ops->map_get_next_key(map, prev_key, key);
1422 rcu_read_unlock();
1423 if (err)
1424 break;
1425 err = bpf_map_copy_value(map, key, value,
1426 attr->batch.elem_flags);
1427
1428 if (err == -ENOENT) {
1429 if (retry) {
1430 retry--;
1431 continue;
1432 }
1433 err = -EINTR;
1434 break;
1435 }
1436
1437 if (err)
1438 goto free_buf;
1439
1440 if (copy_to_user(keys + cp * map->key_size, key,
1441 map->key_size)) {
1442 err = -EFAULT;
1443 goto free_buf;
1444 }
1445 if (copy_to_user(values + cp * value_size, value, value_size)) {
1446 err = -EFAULT;
1447 goto free_buf;
1448 }
1449
1450 if (!prev_key)
1451 prev_key = buf_prevkey;
1452
1453 swap(prev_key, key);
1454 retry = MAP_LOOKUP_RETRIES;
1455 cp++;
1456 }
1457
1458 if (err == -EFAULT)
1459 goto free_buf;
1460
1461 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1462 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1463 err = -EFAULT;
1464
1465free_buf:
1466 kfree(buf_prevkey);
1467 kfree(buf);
1468 return err;
1469}
1470
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001471#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1472
1473static int map_lookup_and_delete_elem(union bpf_attr *attr)
1474{
1475 void __user *ukey = u64_to_user_ptr(attr->key);
1476 void __user *uvalue = u64_to_user_ptr(attr->value);
1477 int ufd = attr->map_fd;
1478 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001479 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001480 u32 value_size;
1481 struct fd f;
1482 int err;
1483
1484 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1485 return -EINVAL;
1486
1487 f = fdget(ufd);
1488 map = __bpf_map_get(f);
1489 if (IS_ERR(map))
1490 return PTR_ERR(map);
Anton Protopopov1ea0f912020-05-27 18:56:59 +00001491 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1492 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001493 err = -EPERM;
1494 goto err_put;
1495 }
1496
1497 key = __bpf_copy_key(ukey, map->key_size);
1498 if (IS_ERR(key)) {
1499 err = PTR_ERR(key);
1500 goto err_put;
1501 }
1502
1503 value_size = map->value_size;
1504
1505 err = -ENOMEM;
1506 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1507 if (!value)
1508 goto free_key;
1509
1510 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1511 map->map_type == BPF_MAP_TYPE_STACK) {
1512 err = map->ops->map_pop_elem(map, value);
1513 } else {
1514 err = -ENOTSUPP;
1515 }
1516
1517 if (err)
1518 goto free_value;
1519
Wei Yongjun7f645462020-04-30 08:18:51 +00001520 if (copy_to_user(uvalue, value, value_size) != 0) {
1521 err = -EFAULT;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001522 goto free_value;
Wei Yongjun7f645462020-04-30 08:18:51 +00001523 }
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001524
1525 err = 0;
1526
1527free_value:
1528 kfree(value);
1529free_key:
1530 kfree(key);
1531err_put:
1532 fdput(f);
1533 return err;
1534}
1535
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001536#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1537
1538static int map_freeze(const union bpf_attr *attr)
1539{
1540 int err = 0, ufd = attr->map_fd;
1541 struct bpf_map *map;
1542 struct fd f;
1543
1544 if (CHECK_ATTR(BPF_MAP_FREEZE))
1545 return -EINVAL;
1546
1547 f = fdget(ufd);
1548 map = __bpf_map_get(f);
1549 if (IS_ERR(map))
1550 return PTR_ERR(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -08001551
Martin KaFai Lau849b4d92020-03-04 17:34:54 -08001552 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1553 fdput(f);
1554 return -ENOTSUPP;
1555 }
1556
Andrii Nakryikofc970222019-11-17 09:28:04 -08001557 mutex_lock(&map->freeze_mutex);
1558
1559 if (map->writecnt) {
1560 err = -EBUSY;
1561 goto err_put;
1562 }
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001563 if (READ_ONCE(map->frozen)) {
1564 err = -EBUSY;
1565 goto err_put;
1566 }
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07001567 if (!bpf_capable()) {
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001568 err = -EPERM;
1569 goto err_put;
1570 }
1571
1572 WRITE_ONCE(map->frozen, true);
1573err_put:
Andrii Nakryikofc970222019-11-17 09:28:04 -08001574 mutex_unlock(&map->freeze_mutex);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001575 fdput(f);
1576 return err;
1577}
1578
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001579static const struct bpf_prog_ops * const bpf_prog_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08001580#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001581 [_id] = & _name ## _prog_ops,
1582#define BPF_MAP_TYPE(_id, _ops)
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001583#define BPF_LINK_TYPE(_id, _name)
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001584#include <linux/bpf_types.h>
1585#undef BPF_PROG_TYPE
1586#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001587#undef BPF_LINK_TYPE
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001588};
1589
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001590static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1591{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001592 const struct bpf_prog_ops *ops;
1593
1594 if (type >= ARRAY_SIZE(bpf_prog_types))
1595 return -EINVAL;
1596 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1597 ops = bpf_prog_types[type];
1598 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001599 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001600
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001601 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001602 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001603 else
1604 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001605 prog->type = type;
1606 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001607}
1608
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001609enum bpf_audit {
1610 BPF_AUDIT_LOAD,
1611 BPF_AUDIT_UNLOAD,
1612 BPF_AUDIT_MAX,
1613};
1614
1615static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1616 [BPF_AUDIT_LOAD] = "LOAD",
1617 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1618};
1619
1620static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1621{
1622 struct audit_context *ctx = NULL;
1623 struct audit_buffer *ab;
1624
1625 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1626 return;
1627 if (audit_enabled == AUDIT_OFF)
1628 return;
1629 if (op == BPF_AUDIT_LOAD)
1630 ctx = audit_context();
1631 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1632 if (unlikely(!ab))
1633 return;
1634 audit_log_format(ab, "prog-id=%u op=%s",
1635 prog->aux->id, bpf_audit_str[op]);
1636 audit_log_end(ab);
1637}
1638
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001639static int bpf_prog_alloc_id(struct bpf_prog *prog)
1640{
1641 int id;
1642
Shaohua Lib76354c2018-03-27 11:53:21 -07001643 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001644 spin_lock_bh(&prog_idr_lock);
1645 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1646 if (id > 0)
1647 prog->aux->id = id;
1648 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001649 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001650
1651 /* id is in [1, INT_MAX) */
1652 if (WARN_ON_ONCE(!id))
1653 return -ENOSPC;
1654
1655 return id > 0 ? 0 : id;
1656}
1657
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001658void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001659{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001660 /* cBPF to eBPF migrations are currently not in the idr store.
1661 * Offloaded programs are removed from the store when their device
1662 * disappears - even if someone grabs an fd to them they are unusable,
1663 * simply waiting for refcnt to drop to be freed.
1664 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001665 if (!prog->aux->id)
1666 return;
1667
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001668 if (do_idr_lock)
1669 spin_lock_bh(&prog_idr_lock);
1670 else
1671 __acquire(&prog_idr_lock);
1672
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001673 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001674 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001675
1676 if (do_idr_lock)
1677 spin_unlock_bh(&prog_idr_lock);
1678 else
1679 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001680}
1681
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001682static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001683{
1684 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1685
Daniel Borkmann3b4d9eb2019-10-22 23:30:38 +02001686 kvfree(aux->func_info);
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08001687 kfree(aux->func_info_aux);
Roman Gushchin3ac1f012020-12-01 13:58:59 -08001688 free_uid(aux->user);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001689 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001690 bpf_prog_free(aux->prog);
1691}
1692
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001693static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1694{
1695 bpf_prog_kallsyms_del_all(prog);
1696 btf_put(prog->aux->btf);
Martin KaFai Laue16301f2021-03-24 18:51:30 -07001697 kvfree(prog->aux->jited_linfo);
1698 kvfree(prog->aux->linfo);
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07001699 kfree(prog->aux->kfunc_tab);
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08001700 if (prog->aux->attach_btf)
1701 btf_put(prog->aux->attach_btf);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001702
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07001703 if (deferred) {
1704 if (prog->aux->sleepable)
1705 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1706 else
1707 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1708 } else {
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001709 __bpf_prog_put_rcu(&prog->aux->rcu);
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07001710 }
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001711}
1712
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001713static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001714{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001715 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001716 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001717 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001718 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001719 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001720 __bpf_prog_put_noref(prog, true);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001721 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001722}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001723
1724void bpf_prog_put(struct bpf_prog *prog)
1725{
1726 __bpf_prog_put(prog, true);
1727}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001728EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001729
1730static int bpf_prog_release(struct inode *inode, struct file *filp)
1731{
1732 struct bpf_prog *prog = filp->private_data;
1733
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001734 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001735 return 0;
1736}
1737
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001738static void bpf_prog_get_stats(const struct bpf_prog *prog,
1739 struct bpf_prog_stats *stats)
1740{
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001741 u64 nsecs = 0, cnt = 0, misses = 0;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001742 int cpu;
1743
1744 for_each_possible_cpu(cpu) {
1745 const struct bpf_prog_stats *st;
1746 unsigned int start;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001747 u64 tnsecs, tcnt, tmisses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001748
Alexei Starovoitov700d4792021-02-09 19:36:26 -08001749 st = per_cpu_ptr(prog->stats, cpu);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001750 do {
1751 start = u64_stats_fetch_begin_irq(&st->syncp);
1752 tnsecs = st->nsecs;
1753 tcnt = st->cnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001754 tmisses = st->misses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001755 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1756 nsecs += tnsecs;
1757 cnt += tcnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001758 misses += tmisses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001759 }
1760 stats->nsecs = nsecs;
1761 stats->cnt = cnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001762 stats->misses = misses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001763}
1764
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001765#ifdef CONFIG_PROC_FS
1766static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1767{
1768 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001769 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001770 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001771
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001772 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001773 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001774 seq_printf(m,
1775 "prog_type:\t%u\n"
1776 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001777 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001778 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001779 "prog_id:\t%u\n"
1780 "run_time_ns:\t%llu\n"
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001781 "run_cnt:\t%llu\n"
1782 "recursion_misses:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001783 prog->type,
1784 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001785 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001786 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001787 prog->aux->id,
1788 stats.nsecs,
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001789 stats.cnt,
1790 stats.misses);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001791}
1792#endif
1793
Chenbo Fengf66e4482017-10-18 13:00:26 -07001794const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001795#ifdef CONFIG_PROC_FS
1796 .show_fdinfo = bpf_prog_show_fdinfo,
1797#endif
1798 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001799 .read = bpf_dummy_read,
1800 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001801};
1802
Daniel Borkmannb2197752015-10-29 14:58:09 +01001803int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001804{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001805 int ret;
1806
1807 ret = security_bpf_prog(prog);
1808 if (ret < 0)
1809 return ret;
1810
Daniel Borkmannaa797812015-10-29 14:58:06 +01001811 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1812 O_RDWR | O_CLOEXEC);
1813}
1814
Daniel Borkmann113214b2016-06-30 17:24:44 +02001815static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001816{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001817 if (!f.file)
1818 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001819 if (f.file->f_op != &bpf_prog_fops) {
1820 fdput(f);
1821 return ERR_PTR(-EINVAL);
1822 }
1823
Daniel Borkmannc2101292015-10-29 14:58:07 +01001824 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001825}
1826
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001827void bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001828{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001829 atomic64_add(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001830}
Brenden Blanco59d36562016-07-19 12:16:46 -07001831EXPORT_SYMBOL_GPL(bpf_prog_add);
1832
Daniel Borkmannc5405942016-11-09 22:02:34 +01001833void bpf_prog_sub(struct bpf_prog *prog, int i)
1834{
1835 /* Only to be used for undoing previous bpf_prog_add() in some
1836 * error path. We still know that another entity in our call
1837 * path holds a reference to the program, thus atomic_sub() can
1838 * be safely used in such cases!
1839 */
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001840 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
Daniel Borkmannc5405942016-11-09 22:02:34 +01001841}
1842EXPORT_SYMBOL_GPL(bpf_prog_sub);
1843
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001844void bpf_prog_inc(struct bpf_prog *prog)
Brenden Blanco59d36562016-07-19 12:16:46 -07001845{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001846 atomic64_inc(&prog->aux->refcnt);
Brenden Blanco59d36562016-07-19 12:16:46 -07001847}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001848EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001849
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001850/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001851struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001852{
1853 int refold;
1854
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001855 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001856
1857 if (!refold)
1858 return ERR_PTR(-ENOENT);
1859
1860 return prog;
1861}
John Fastabenda6f6df62017-08-15 22:32:22 -07001862EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001863
Al Viro040ee692017-12-02 20:20:38 -05001864bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001865 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001866{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001867 /* not an attachment, just a refcount inc, always allow */
1868 if (!attach_type)
1869 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001870
1871 if (prog->type != *attach_type)
1872 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001873 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001874 return false;
1875
1876 return true;
1877}
1878
1879static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001880 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001881{
1882 struct fd f = fdget(ufd);
1883 struct bpf_prog *prog;
1884
Daniel Borkmann113214b2016-06-30 17:24:44 +02001885 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001886 if (IS_ERR(prog))
1887 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001888 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001889 prog = ERR_PTR(-EINVAL);
1890 goto out;
1891 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001892
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001893 bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001894out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001895 fdput(f);
1896 return prog;
1897}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001898
1899struct bpf_prog *bpf_prog_get(u32 ufd)
1900{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001901 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001902}
1903
Jakub Kicinski248f3462017-11-03 13:56:20 -07001904struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001905 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001906{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001907 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001908}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001909EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001910
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001911/* Initially all BPF programs could be loaded w/o specifying
1912 * expected_attach_type. Later for some of them specifying expected_attach_type
1913 * at load time became required so that program could be validated properly.
1914 * Programs of types that are allowed to be loaded both w/ and w/o (for
1915 * backward compatibility) expected_attach_type, should have the default attach
1916 * type assigned to expected_attach_type for the latter case, so that it can be
1917 * validated later at attach time.
1918 *
1919 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1920 * prog type requires it but has some attach types that have to be backward
1921 * compatible.
1922 */
1923static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1924{
1925 switch (attr->prog_type) {
1926 case BPF_PROG_TYPE_CGROUP_SOCK:
1927 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1928 * exist so checking for non-zero is the way to go here.
1929 */
1930 if (!attr->expected_attach_type)
1931 attr->expected_attach_type =
1932 BPF_CGROUP_INET_SOCK_CREATE;
1933 break;
1934 }
1935}
1936
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001937static int
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07001938bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1939 enum bpf_attach_type expected_attach_type,
Andrii Nakryiko290248a2020-12-03 12:46:30 -08001940 struct btf *attach_btf, u32 btf_id,
1941 struct bpf_prog *dst_prog)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001942{
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001943 if (btf_id) {
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001944 if (btf_id > BTF_MAX_TYPE)
1945 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001946
Andrii Nakryiko290248a2020-12-03 12:46:30 -08001947 if (!attach_btf && !dst_prog)
1948 return -EINVAL;
1949
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001950 switch (prog_type) {
1951 case BPF_PROG_TYPE_TRACING:
KP Singh9e4e01d2020-03-29 01:43:52 +01001952 case BPF_PROG_TYPE_LSM:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001953 case BPF_PROG_TYPE_STRUCT_OPS:
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001954 case BPF_PROG_TYPE_EXT:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001955 break;
1956 default:
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001957 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001958 }
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001959 }
1960
Andrii Nakryiko290248a2020-12-03 12:46:30 -08001961 if (attach_btf && (!btf_id || dst_prog))
1962 return -EINVAL;
1963
1964 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001965 prog_type != BPF_PROG_TYPE_EXT)
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001966 return -EINVAL;
1967
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001968 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001969 case BPF_PROG_TYPE_CGROUP_SOCK:
1970 switch (expected_attach_type) {
1971 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07001972 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001973 case BPF_CGROUP_INET4_POST_BIND:
1974 case BPF_CGROUP_INET6_POST_BIND:
1975 return 0;
1976 default:
1977 return -EINVAL;
1978 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001979 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1980 switch (expected_attach_type) {
1981 case BPF_CGROUP_INET4_BIND:
1982 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001983 case BPF_CGROUP_INET4_CONNECT:
1984 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02001985 case BPF_CGROUP_INET4_GETPEERNAME:
1986 case BPF_CGROUP_INET6_GETPEERNAME:
1987 case BPF_CGROUP_INET4_GETSOCKNAME:
1988 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001989 case BPF_CGROUP_UDP4_SENDMSG:
1990 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02001991 case BPF_CGROUP_UDP4_RECVMSG:
1992 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001993 return 0;
1994 default:
1995 return -EINVAL;
1996 }
brakmo5cf1e912019-05-28 16:59:36 -07001997 case BPF_PROG_TYPE_CGROUP_SKB:
1998 switch (expected_attach_type) {
1999 case BPF_CGROUP_INET_INGRESS:
2000 case BPF_CGROUP_INET_EGRESS:
2001 return 0;
2002 default:
2003 return -EINVAL;
2004 }
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002005 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2006 switch (expected_attach_type) {
2007 case BPF_CGROUP_SETSOCKOPT:
2008 case BPF_CGROUP_GETSOCKOPT:
2009 return 0;
2010 default:
2011 return -EINVAL;
2012 }
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02002013 case BPF_PROG_TYPE_SK_LOOKUP:
2014 if (expected_attach_type == BPF_SK_LOOKUP)
2015 return 0;
2016 return -EINVAL;
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002017 case BPF_PROG_TYPE_EXT:
2018 if (expected_attach_type)
2019 return -EINVAL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002020 fallthrough;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002021 default:
2022 return 0;
2023 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002024}
2025
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002026static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2027{
2028 switch (prog_type) {
2029 case BPF_PROG_TYPE_SCHED_CLS:
2030 case BPF_PROG_TYPE_SCHED_ACT:
2031 case BPF_PROG_TYPE_XDP:
2032 case BPF_PROG_TYPE_LWT_IN:
2033 case BPF_PROG_TYPE_LWT_OUT:
2034 case BPF_PROG_TYPE_LWT_XMIT:
2035 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2036 case BPF_PROG_TYPE_SK_SKB:
2037 case BPF_PROG_TYPE_SK_MSG:
2038 case BPF_PROG_TYPE_LIRC_MODE2:
2039 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2040 case BPF_PROG_TYPE_CGROUP_DEVICE:
2041 case BPF_PROG_TYPE_CGROUP_SOCK:
2042 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2043 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2044 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2045 case BPF_PROG_TYPE_SOCK_OPS:
2046 case BPF_PROG_TYPE_EXT: /* extends any prog */
2047 return true;
2048 case BPF_PROG_TYPE_CGROUP_SKB:
2049 /* always unpriv */
2050 case BPF_PROG_TYPE_SK_REUSEPORT:
2051 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2052 default:
2053 return false;
2054 }
2055}
2056
2057static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2058{
2059 switch (prog_type) {
2060 case BPF_PROG_TYPE_KPROBE:
2061 case BPF_PROG_TYPE_TRACEPOINT:
2062 case BPF_PROG_TYPE_PERF_EVENT:
2063 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2064 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2065 case BPF_PROG_TYPE_TRACING:
2066 case BPF_PROG_TYPE_LSM:
2067 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2068 case BPF_PROG_TYPE_EXT: /* extends any prog */
2069 return true;
2070 default:
2071 return false;
2072 }
2073}
2074
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002075/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002076#define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002077
Yonghong Song838e9692018-11-19 15:29:11 -08002078static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002079{
2080 enum bpf_prog_type type = attr->prog_type;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002081 struct bpf_prog *prog, *dst_prog = NULL;
2082 struct btf *attach_btf = NULL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002083 int err;
2084 char license[128];
2085 bool is_gpl;
2086
2087 if (CHECK_ATTR(BPF_PROG_LOAD))
2088 return -EINVAL;
2089
Jiong Wangc240eff2019-05-24 23:25:16 +01002090 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2091 BPF_F_ANY_ALIGNMENT |
Alexei Starovoitov10d274e2019-08-22 22:52:12 -07002092 BPF_F_TEST_STATE_FREQ |
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07002093 BPF_F_SLEEPABLE |
Jiong Wangc240eff2019-05-24 23:25:16 +01002094 BPF_F_TEST_RND_HI32))
David S. Millere07b98d2017-05-10 11:38:07 -07002095 return -EINVAL;
2096
David Millere9ee9ef2018-11-30 21:08:14 -08002097 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2098 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002099 !bpf_capable())
David Millere9ee9ef2018-11-30 21:08:14 -08002100 return -EPERM;
2101
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002102 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002103 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002104 sizeof(license) - 1) < 0)
2105 return -EFAULT;
2106 license[sizeof(license) - 1] = 0;
2107
2108 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2109 is_gpl = license_is_gpl_compatible(license);
2110
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07002111 if (attr->insn_cnt == 0 ||
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002112 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01002113 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07002114 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2115 type != BPF_PROG_TYPE_CGROUP_SKB &&
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002116 !bpf_capable())
2117 return -EPERM;
2118
Maciej Żenczykowskib338cb92020-06-20 14:26:16 -07002119 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002120 return -EPERM;
2121 if (is_perfmon_prog_type(type) && !perfmon_capable())
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002122 return -EPERM;
2123
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002124 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2125 * or btf, we need to check which one it is
2126 */
2127 if (attr->attach_prog_fd) {
2128 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2129 if (IS_ERR(dst_prog)) {
2130 dst_prog = NULL;
2131 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2132 if (IS_ERR(attach_btf))
2133 return -EINVAL;
2134 if (!btf_is_kernel(attach_btf)) {
Andrii Nakryiko8bdd8e22020-12-07 22:43:26 -08002135 /* attaching through specifying bpf_prog's BTF
2136 * objects directly might be supported eventually
2137 */
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002138 btf_put(attach_btf);
Andrii Nakryiko8bdd8e22020-12-07 22:43:26 -08002139 return -ENOTSUPP;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002140 }
2141 }
2142 } else if (attr->attach_btf_id) {
2143 /* fall back to vmlinux BTF, if BTF type ID is specified */
2144 attach_btf = bpf_get_btf_vmlinux();
2145 if (IS_ERR(attach_btf))
2146 return PTR_ERR(attach_btf);
2147 if (!attach_btf)
2148 return -EINVAL;
2149 btf_get(attach_btf);
2150 }
2151
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002152 bpf_prog_load_fixup_attach_type(attr);
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002153 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002154 attach_btf, attr->attach_btf_id,
2155 dst_prog)) {
2156 if (dst_prog)
2157 bpf_prog_put(dst_prog);
2158 if (attach_btf)
2159 btf_put(attach_btf);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002160 return -EINVAL;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002161 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002162
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002163 /* plain bpf_prog allocation */
2164 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002165 if (!prog) {
2166 if (dst_prog)
2167 bpf_prog_put(dst_prog);
2168 if (attach_btf)
2169 btf_put(attach_btf);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002170 return -ENOMEM;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002171 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002172
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002173 prog->expected_attach_type = attr->expected_attach_type;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002174 prog->aux->attach_btf = attach_btf;
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002175 prog->aux->attach_btf_id = attr->attach_btf_id;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002176 prog->aux->dst_prog = dst_prog;
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002177 prog->aux->offload_requested = !!attr->prog_ifindex;
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07002178 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002179
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002180 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002181 if (err)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002182 goto free_prog;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002183
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002184 prog->aux->user = get_current_user();
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002185 prog->len = attr->insn_cnt;
2186
2187 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002188 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01002189 bpf_prog_insn_size(prog)) != 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002190 goto free_prog_sec;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002191
2192 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002193 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002194
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002195 atomic64_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002196 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002197
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002198 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002199 err = bpf_prog_offload_init(prog, attr);
2200 if (err)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002201 goto free_prog_sec;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002202 }
2203
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002204 /* find program type: socket_filter vs tracing_filter */
2205 err = find_prog_type(type, prog);
2206 if (err < 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002207 goto free_prog_sec;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002208
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002209 prog->aux->load_time = ktime_get_boottime_ns();
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -07002210 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2211 sizeof(attr->prog_name));
2212 if (err < 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002213 goto free_prog_sec;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002214
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002215 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08002216 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002217 if (err < 0)
2218 goto free_used_maps;
2219
Daniel Borkmann9facc332018-06-15 02:30:48 +02002220 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002221 if (err < 0)
2222 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002223
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07002224 err = bpf_prog_alloc_id(prog);
2225 if (err)
2226 goto free_used_maps;
2227
Daniel Borkmannc7517982019-08-23 22:14:23 +02002228 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2229 * effectively publicly exposed. However, retrieving via
2230 * bpf_prog_get_fd_by_id() will take another reference,
2231 * therefore it cannot be gone underneath us.
2232 *
2233 * Only for the time /after/ successful bpf_prog_new_fd()
2234 * and before returning to userspace, we might just hold
2235 * one reference and any parallel close on that fd could
2236 * rip everything out. Hence, below notifications must
2237 * happen before bpf_prog_new_fd().
2238 *
2239 * Also, any failure handling from this point onwards must
2240 * be using bpf_prog_put() given the program is exposed.
2241 */
Daniel Borkmann74451e662017-02-16 22:24:50 +01002242 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08002243 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01002244 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
Daniel Borkmannc7517982019-08-23 22:14:23 +02002245
2246 err = bpf_prog_new_fd(prog);
2247 if (err < 0)
2248 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002249 return err;
2250
2251free_used_maps:
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02002252 /* In case we have subprogs, we need to wait for a grace
2253 * period before we can tear down JIT memory since symbols
2254 * are already exposed under kallsyms.
2255 */
2256 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2257 return err;
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002258free_prog_sec:
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002259 free_uid(prog->aux->user);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002260 security_bpf_prog_free(prog->aux);
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002261free_prog:
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08002262 if (prog->aux->attach_btf)
2263 btf_put(prog->aux->attach_btf);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002264 bpf_prog_free(prog);
2265 return err;
2266}
2267
Chenbo Feng6e71b042017-10-18 13:00:22 -07002268#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01002269
2270static int bpf_obj_pin(const union bpf_attr *attr)
2271{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002272 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002273 return -EINVAL;
2274
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002275 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01002276}
2277
2278static int bpf_obj_get(const union bpf_attr *attr)
2279{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002280 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2281 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002282 return -EINVAL;
2283
Chenbo Feng6e71b042017-10-18 13:00:22 -07002284 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2285 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01002286}
2287
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002288void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002289 const struct bpf_link_ops *ops, struct bpf_prog *prog)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002290{
2291 atomic64_set(&link->refcnt, 1);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002292 link->type = type;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002293 link->id = 0;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002294 link->ops = ops;
2295 link->prog = prog;
2296}
2297
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002298static void bpf_link_free_id(int id)
2299{
2300 if (!id)
2301 return;
2302
2303 spin_lock_bh(&link_idr_lock);
2304 idr_remove(&link_idr, id);
2305 spin_unlock_bh(&link_idr_lock);
2306}
2307
Andrii Nakryiko98868662020-03-12 17:21:28 -07002308/* Clean up bpf_link and corresponding anon_inode file and FD. After
2309 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002310 * anon_inode's release() call. This helper marksbpf_link as
2311 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2312 * is not decremented, it's the responsibility of a calling code that failed
2313 * to complete bpf_link initialization.
Andrii Nakryiko98868662020-03-12 17:21:28 -07002314 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002315void bpf_link_cleanup(struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002316{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002317 primer->link->prog = NULL;
2318 bpf_link_free_id(primer->id);
2319 fput(primer->file);
2320 put_unused_fd(primer->fd);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002321}
2322
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002323void bpf_link_inc(struct bpf_link *link)
2324{
2325 atomic64_inc(&link->refcnt);
2326}
2327
2328/* bpf_link_free is guaranteed to be called from process context */
2329static void bpf_link_free(struct bpf_link *link)
2330{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002331 bpf_link_free_id(link->id);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002332 if (link->prog) {
2333 /* detach BPF program, clean up used resources */
2334 link->ops->release(link);
2335 bpf_prog_put(link->prog);
2336 }
2337 /* free bpf_link and its containing memory */
2338 link->ops->dealloc(link);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002339}
2340
2341static void bpf_link_put_deferred(struct work_struct *work)
2342{
2343 struct bpf_link *link = container_of(work, struct bpf_link, work);
2344
2345 bpf_link_free(link);
2346}
2347
2348/* bpf_link_put can be called from atomic context, but ensures that resources
2349 * are freed from process context
2350 */
2351void bpf_link_put(struct bpf_link *link)
2352{
2353 if (!atomic64_dec_and_test(&link->refcnt))
2354 return;
2355
Alexei Starovoitovf00f2f72020-09-23 19:10:38 -07002356 if (in_atomic()) {
2357 INIT_WORK(&link->work, bpf_link_put_deferred);
2358 schedule_work(&link->work);
2359 } else {
2360 bpf_link_free(link);
2361 }
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002362}
2363
2364static int bpf_link_release(struct inode *inode, struct file *filp)
2365{
2366 struct bpf_link *link = filp->private_data;
2367
2368 bpf_link_put(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002369 return 0;
2370}
2371
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002372#ifdef CONFIG_PROC_FS
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002373#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2374#define BPF_MAP_TYPE(_id, _ops)
2375#define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2376static const char *bpf_link_type_strs[] = {
2377 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2378#include <linux/bpf_types.h>
2379};
2380#undef BPF_PROG_TYPE
2381#undef BPF_MAP_TYPE
2382#undef BPF_LINK_TYPE
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002383
2384static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2385{
2386 const struct bpf_link *link = filp->private_data;
2387 const struct bpf_prog *prog = link->prog;
2388 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002389
2390 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2391 seq_printf(m,
2392 "link_type:\t%s\n"
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002393 "link_id:\t%u\n"
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002394 "prog_tag:\t%s\n"
2395 "prog_id:\t%u\n",
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002396 bpf_link_type_strs[link->type],
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002397 link->id,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002398 prog_tag,
2399 prog->aux->id);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002400 if (link->ops->show_fdinfo)
2401 link->ops->show_fdinfo(link, m);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002402}
2403#endif
2404
Zou Wei6f302bf2020-04-23 10:32:40 +08002405static const struct file_operations bpf_link_fops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002406#ifdef CONFIG_PROC_FS
2407 .show_fdinfo = bpf_link_show_fdinfo,
2408#endif
2409 .release = bpf_link_release,
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002410 .read = bpf_dummy_read,
2411 .write = bpf_dummy_write,
2412};
2413
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002414static int bpf_link_alloc_id(struct bpf_link *link)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002415{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002416 int id;
2417
2418 idr_preload(GFP_KERNEL);
2419 spin_lock_bh(&link_idr_lock);
2420 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2421 spin_unlock_bh(&link_idr_lock);
2422 idr_preload_end();
2423
2424 return id;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002425}
2426
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002427/* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2428 * reserving unused FD and allocating ID from link_idr. This is to be paired
2429 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2430 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2431 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2432 * transient state is passed around in struct bpf_link_primer.
2433 * This is preferred way to create and initialize bpf_link, especially when
2434 * there are complicated and expensive operations inbetween creating bpf_link
2435 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2436 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2437 * expensive (and potentially failing) roll back operations in a rare case
2438 * that file, FD, or ID can't be allocated.
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002439 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002440int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002441{
2442 struct file *file;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002443 int fd, id;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002444
2445 fd = get_unused_fd_flags(O_CLOEXEC);
2446 if (fd < 0)
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002447 return fd;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002448
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002449
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002450 id = bpf_link_alloc_id(link);
2451 if (id < 0) {
2452 put_unused_fd(fd);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002453 return id;
2454 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002455
2456 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2457 if (IS_ERR(file)) {
Andrii Nakryiko138c6762020-05-01 11:56:22 -07002458 bpf_link_free_id(id);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002459 put_unused_fd(fd);
Andrii Nakryiko138c6762020-05-01 11:56:22 -07002460 return PTR_ERR(file);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002461 }
2462
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002463 primer->link = link;
2464 primer->file = file;
2465 primer->fd = fd;
2466 primer->id = id;
2467 return 0;
2468}
2469
2470int bpf_link_settle(struct bpf_link_primer *primer)
2471{
2472 /* make bpf_link fetchable by ID */
2473 spin_lock_bh(&link_idr_lock);
2474 primer->link->id = primer->id;
2475 spin_unlock_bh(&link_idr_lock);
2476 /* make bpf_link fetchable by FD */
2477 fd_install(primer->fd, primer->file);
2478 /* pass through installed FD */
2479 return primer->fd;
2480}
2481
2482int bpf_link_new_fd(struct bpf_link *link)
2483{
2484 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002485}
2486
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002487struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2488{
2489 struct fd f = fdget(ufd);
2490 struct bpf_link *link;
2491
2492 if (!f.file)
2493 return ERR_PTR(-EBADF);
2494 if (f.file->f_op != &bpf_link_fops) {
2495 fdput(f);
2496 return ERR_PTR(-EINVAL);
2497 }
2498
2499 link = f.file->private_data;
2500 bpf_link_inc(link);
2501 fdput(f);
2502
2503 return link;
2504}
2505
2506struct bpf_tracing_link {
2507 struct bpf_link link;
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002508 enum bpf_attach_type attach_type;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002509 struct bpf_trampoline *trampoline;
2510 struct bpf_prog *tgt_prog;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002511};
2512
2513static void bpf_tracing_link_release(struct bpf_link *link)
2514{
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002515 struct bpf_tracing_link *tr_link =
2516 container_of(link, struct bpf_tracing_link, link);
2517
2518 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2519 tr_link->trampoline));
2520
2521 bpf_trampoline_put(tr_link->trampoline);
2522
2523 /* tgt_prog is NULL if target is a kernel function */
2524 if (tr_link->tgt_prog)
2525 bpf_prog_put(tr_link->tgt_prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002526}
2527
2528static void bpf_tracing_link_dealloc(struct bpf_link *link)
2529{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002530 struct bpf_tracing_link *tr_link =
2531 container_of(link, struct bpf_tracing_link, link);
2532
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002533 kfree(tr_link);
2534}
2535
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002536static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2537 struct seq_file *seq)
2538{
2539 struct bpf_tracing_link *tr_link =
2540 container_of(link, struct bpf_tracing_link, link);
2541
2542 seq_printf(seq,
2543 "attach_type:\t%d\n",
2544 tr_link->attach_type);
2545}
2546
2547static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2548 struct bpf_link_info *info)
2549{
2550 struct bpf_tracing_link *tr_link =
2551 container_of(link, struct bpf_tracing_link, link);
2552
2553 info->tracing.attach_type = tr_link->attach_type;
Toke Høiland-Jørgensen441e8c62021-04-13 11:16:06 +02002554 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2555 &info->tracing.target_obj_id,
2556 &info->tracing.target_btf_id);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002557
2558 return 0;
2559}
2560
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002561static const struct bpf_link_ops bpf_tracing_link_lops = {
2562 .release = bpf_tracing_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002563 .dealloc = bpf_tracing_link_dealloc,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002564 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2565 .fill_link_info = bpf_tracing_link_fill_link_info,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002566};
2567
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002568static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2569 int tgt_prog_fd,
2570 u32 btf_id)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002571{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002572 struct bpf_link_primer link_primer;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002573 struct bpf_prog *tgt_prog = NULL;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002574 struct bpf_trampoline *tr = NULL;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002575 struct bpf_tracing_link *link;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002576 u64 key = 0;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002577 int err;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002578
KP Singh9e4e01d2020-03-29 01:43:52 +01002579 switch (prog->type) {
2580 case BPF_PROG_TYPE_TRACING:
2581 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2582 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2583 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2584 err = -EINVAL;
2585 goto out_put_prog;
2586 }
2587 break;
2588 case BPF_PROG_TYPE_EXT:
2589 if (prog->expected_attach_type != 0) {
2590 err = -EINVAL;
2591 goto out_put_prog;
2592 }
2593 break;
2594 case BPF_PROG_TYPE_LSM:
2595 if (prog->expected_attach_type != BPF_LSM_MAC) {
2596 err = -EINVAL;
2597 goto out_put_prog;
2598 }
2599 break;
2600 default:
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002601 err = -EINVAL;
2602 goto out_put_prog;
2603 }
2604
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002605 if (!!tgt_prog_fd != !!btf_id) {
2606 err = -EINVAL;
2607 goto out_put_prog;
2608 }
2609
2610 if (tgt_prog_fd) {
2611 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2612 if (prog->type != BPF_PROG_TYPE_EXT) {
2613 err = -EINVAL;
2614 goto out_put_prog;
2615 }
2616
2617 tgt_prog = bpf_prog_get(tgt_prog_fd);
2618 if (IS_ERR(tgt_prog)) {
2619 err = PTR_ERR(tgt_prog);
2620 tgt_prog = NULL;
2621 goto out_put_prog;
2622 }
2623
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08002624 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002625 }
2626
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002627 link = kzalloc(sizeof(*link), GFP_USER);
2628 if (!link) {
2629 err = -ENOMEM;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002630 goto out_put_prog;
2631 }
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002632 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2633 &bpf_tracing_link_lops, prog);
2634 link->attach_type = prog->expected_attach_type;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002635
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002636 mutex_lock(&prog->aux->dst_mutex);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002637
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002638 /* There are a few possible cases here:
2639 *
2640 * - if prog->aux->dst_trampoline is set, the program was just loaded
2641 * and not yet attached to anything, so we can use the values stored
2642 * in prog->aux
2643 *
2644 * - if prog->aux->dst_trampoline is NULL, the program has already been
2645 * attached to a target and its initial target was cleared (below)
2646 *
2647 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2648 * target_btf_id using the link_create API.
2649 *
2650 * - if tgt_prog == NULL when this function was called using the old
Jiri Olsaf3a95072021-04-14 21:51:41 +02002651 * raw_tracepoint_open API, and we need a target from prog->aux
2652 *
2653 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
2654 * was detached and is going for re-attachment.
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002655 */
2656 if (!prog->aux->dst_trampoline && !tgt_prog) {
Jiri Olsaf3a95072021-04-14 21:51:41 +02002657 /*
2658 * Allow re-attach for TRACING and LSM programs. If it's
2659 * currently linked, bpf_trampoline_link_prog will fail.
2660 * EXT programs need to specify tgt_prog_fd, so they
2661 * re-attach in separate code path.
2662 */
2663 if (prog->type != BPF_PROG_TYPE_TRACING &&
2664 prog->type != BPF_PROG_TYPE_LSM) {
2665 err = -EINVAL;
2666 goto out_unlock;
2667 }
2668 btf_id = prog->aux->attach_btf_id;
2669 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002670 }
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002671
2672 if (!prog->aux->dst_trampoline ||
2673 (key && key != prog->aux->dst_trampoline->key)) {
2674 /* If there is no saved target, or the specified target is
2675 * different from the destination specified at load time, we
2676 * need a new trampoline and a check for compatibility
2677 */
2678 struct bpf_attach_target_info tgt_info = {};
2679
2680 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2681 &tgt_info);
2682 if (err)
2683 goto out_unlock;
2684
2685 tr = bpf_trampoline_get(key, &tgt_info);
2686 if (!tr) {
2687 err = -ENOMEM;
2688 goto out_unlock;
2689 }
2690 } else {
2691 /* The caller didn't specify a target, or the target was the
2692 * same as the destination supplied during program load. This
2693 * means we can reuse the trampoline and reference from program
2694 * load time, and there is no need to allocate a new one. This
2695 * can only happen once for any program, as the saved values in
2696 * prog->aux are cleared below.
2697 */
2698 tr = prog->aux->dst_trampoline;
2699 tgt_prog = prog->aux->dst_prog;
2700 }
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002701
2702 err = bpf_link_prime(&link->link, &link_primer);
2703 if (err)
2704 goto out_unlock;
2705
2706 err = bpf_trampoline_link_prog(prog, tr);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002707 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002708 bpf_link_cleanup(&link_primer);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002709 link = NULL;
2710 goto out_unlock;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002711 }
2712
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002713 link->tgt_prog = tgt_prog;
2714 link->trampoline = tr;
2715
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002716 /* Always clear the trampoline and target prog from prog->aux to make
2717 * sure the original attach destination is not kept alive after a
2718 * program is (re-)attached to another target.
2719 */
2720 if (prog->aux->dst_prog &&
2721 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2722 /* got extra prog ref from syscall, or attaching to different prog */
2723 bpf_prog_put(prog->aux->dst_prog);
2724 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2725 /* we allocated a new trampoline, so free the old one */
2726 bpf_trampoline_put(prog->aux->dst_trampoline);
2727
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002728 prog->aux->dst_prog = NULL;
2729 prog->aux->dst_trampoline = NULL;
2730 mutex_unlock(&prog->aux->dst_mutex);
2731
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002732 return bpf_link_settle(&link_primer);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002733out_unlock:
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002734 if (tr && tr != prog->aux->dst_trampoline)
2735 bpf_trampoline_put(tr);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002736 mutex_unlock(&prog->aux->dst_mutex);
2737 kfree(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002738out_put_prog:
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002739 if (tgt_prog_fd && tgt_prog)
2740 bpf_prog_put(tgt_prog);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002741 return err;
2742}
2743
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002744struct bpf_raw_tp_link {
2745 struct bpf_link link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002746 struct bpf_raw_event_map *btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002747};
2748
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002749static void bpf_raw_tp_link_release(struct bpf_link *link)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002750{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002751 struct bpf_raw_tp_link *raw_tp =
2752 container_of(link, struct bpf_raw_tp_link, link);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002753
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002754 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
Matt Mullinsa38d1102018-12-12 16:42:37 -08002755 bpf_put_raw_tracepoint(raw_tp->btp);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002756}
2757
2758static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2759{
2760 struct bpf_raw_tp_link *raw_tp =
2761 container_of(link, struct bpf_raw_tp_link, link);
2762
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002763 kfree(raw_tp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002764}
2765
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002766static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2767 struct seq_file *seq)
2768{
2769 struct bpf_raw_tp_link *raw_tp_link =
2770 container_of(link, struct bpf_raw_tp_link, link);
2771
2772 seq_printf(seq,
2773 "tp_name:\t%s\n",
2774 raw_tp_link->btp->tp->name);
2775}
2776
2777static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2778 struct bpf_link_info *info)
2779{
2780 struct bpf_raw_tp_link *raw_tp_link =
2781 container_of(link, struct bpf_raw_tp_link, link);
2782 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2783 const char *tp_name = raw_tp_link->btp->tp->name;
2784 u32 ulen = info->raw_tracepoint.tp_name_len;
2785 size_t tp_len = strlen(tp_name);
2786
Yonghong Songb4749592020-08-21 12:10:54 -07002787 if (!ulen ^ !ubuf)
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002788 return -EINVAL;
2789
2790 info->raw_tracepoint.tp_name_len = tp_len + 1;
2791
2792 if (!ubuf)
2793 return 0;
2794
2795 if (ulen >= tp_len + 1) {
2796 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2797 return -EFAULT;
2798 } else {
2799 char zero = '\0';
2800
2801 if (copy_to_user(ubuf, tp_name, ulen - 1))
2802 return -EFAULT;
2803 if (put_user(zero, ubuf + ulen - 1))
2804 return -EFAULT;
2805 return -ENOSPC;
2806 }
2807
2808 return 0;
2809}
2810
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002811static const struct bpf_link_ops bpf_raw_tp_link_lops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002812 .release = bpf_raw_tp_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002813 .dealloc = bpf_raw_tp_link_dealloc,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002814 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2815 .fill_link_info = bpf_raw_tp_link_fill_link_info,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002816};
2817
2818#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2819
2820static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2821{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002822 struct bpf_link_primer link_primer;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002823 struct bpf_raw_tp_link *link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002824 struct bpf_raw_event_map *btp;
2825 struct bpf_prog *prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002826 const char *tp_name;
2827 char buf[128];
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002828 int err;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002829
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002830 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2831 return -EINVAL;
2832
2833 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2834 if (IS_ERR(prog))
2835 return PTR_ERR(prog);
2836
KP Singh9e4e01d2020-03-29 01:43:52 +01002837 switch (prog->type) {
2838 case BPF_PROG_TYPE_TRACING:
2839 case BPF_PROG_TYPE_EXT:
2840 case BPF_PROG_TYPE_LSM:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002841 if (attr->raw_tracepoint.name) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002842 /* The attach point for this category of programs
2843 * should be specified via btf_id during program load.
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002844 */
2845 err = -EINVAL;
2846 goto out_put_prog;
2847 }
KP Singh9e4e01d2020-03-29 01:43:52 +01002848 if (prog->type == BPF_PROG_TYPE_TRACING &&
2849 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002850 tp_name = prog->aux->attach_func_name;
KP Singh9e4e01d2020-03-29 01:43:52 +01002851 break;
2852 }
Jiri Olsa55410752021-01-11 20:16:50 +01002853 err = bpf_tracing_prog_attach(prog, 0, 0);
2854 if (err >= 0)
2855 return err;
2856 goto out_put_prog;
KP Singh9e4e01d2020-03-29 01:43:52 +01002857 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2858 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002859 if (strncpy_from_user(buf,
2860 u64_to_user_ptr(attr->raw_tracepoint.name),
2861 sizeof(buf) - 1) < 0) {
2862 err = -EFAULT;
2863 goto out_put_prog;
2864 }
2865 buf[sizeof(buf) - 1] = 0;
2866 tp_name = buf;
KP Singh9e4e01d2020-03-29 01:43:52 +01002867 break;
2868 default:
2869 err = -EINVAL;
2870 goto out_put_prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002871 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002872
Matt Mullinsa38d1102018-12-12 16:42:37 -08002873 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002874 if (!btp) {
2875 err = -ENOENT;
2876 goto out_put_prog;
2877 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002878
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002879 link = kzalloc(sizeof(*link), GFP_USER);
2880 if (!link) {
Matt Mullinsa38d1102018-12-12 16:42:37 -08002881 err = -ENOMEM;
2882 goto out_put_btp;
2883 }
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002884 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2885 &bpf_raw_tp_link_lops, prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002886 link->btp = btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002887
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002888 err = bpf_link_prime(&link->link, &link_primer);
2889 if (err) {
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002890 kfree(link);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002891 goto out_put_btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002892 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002893
2894 err = bpf_probe_register(link->btp, prog);
2895 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002896 bpf_link_cleanup(&link_primer);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002897 goto out_put_btp;
2898 }
2899
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002900 return bpf_link_settle(&link_primer);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002901
Matt Mullinsa38d1102018-12-12 16:42:37 -08002902out_put_btp:
2903 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002904out_put_prog:
2905 bpf_prog_put(prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002906 return err;
2907}
2908
Anders Roxell33491582018-04-03 14:09:47 +02002909static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2910 enum bpf_attach_type attach_type)
2911{
2912 switch (prog->type) {
2913 case BPF_PROG_TYPE_CGROUP_SOCK:
2914 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002915 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02002916 case BPF_PROG_TYPE_SK_LOOKUP:
Anders Roxell33491582018-04-03 14:09:47 +02002917 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
brakmo5cf1e912019-05-28 16:59:36 -07002918 case BPF_PROG_TYPE_CGROUP_SKB:
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002919 if (!capable(CAP_NET_ADMIN))
2920 /* cg-skb progs can be loaded by unpriv user.
2921 * check permissions at attach time.
2922 */
2923 return -EPERM;
brakmo5cf1e912019-05-28 16:59:36 -07002924 return prog->enforce_expected_attach_type &&
2925 prog->expected_attach_type != attach_type ?
2926 -EINVAL : 0;
Anders Roxell33491582018-04-03 14:09:47 +02002927 default:
2928 return 0;
2929 }
2930}
2931
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002932static enum bpf_prog_type
2933attach_type_to_prog_type(enum bpf_attach_type attach_type)
2934{
2935 switch (attach_type) {
2936 case BPF_CGROUP_INET_INGRESS:
2937 case BPF_CGROUP_INET_EGRESS:
2938 return BPF_PROG_TYPE_CGROUP_SKB;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002939 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07002940 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002941 case BPF_CGROUP_INET4_POST_BIND:
2942 case BPF_CGROUP_INET6_POST_BIND:
2943 return BPF_PROG_TYPE_CGROUP_SOCK;
2944 case BPF_CGROUP_INET4_BIND:
2945 case BPF_CGROUP_INET6_BIND:
2946 case BPF_CGROUP_INET4_CONNECT:
2947 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02002948 case BPF_CGROUP_INET4_GETPEERNAME:
2949 case BPF_CGROUP_INET6_GETPEERNAME:
2950 case BPF_CGROUP_INET4_GETSOCKNAME:
2951 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002952 case BPF_CGROUP_UDP4_SENDMSG:
2953 case BPF_CGROUP_UDP6_SENDMSG:
2954 case BPF_CGROUP_UDP4_RECVMSG:
2955 case BPF_CGROUP_UDP6_RECVMSG:
2956 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2957 case BPF_CGROUP_SOCK_OPS:
2958 return BPF_PROG_TYPE_SOCK_OPS;
2959 case BPF_CGROUP_DEVICE:
2960 return BPF_PROG_TYPE_CGROUP_DEVICE;
2961 case BPF_SK_MSG_VERDICT:
2962 return BPF_PROG_TYPE_SK_MSG;
2963 case BPF_SK_SKB_STREAM_PARSER:
2964 case BPF_SK_SKB_STREAM_VERDICT:
Cong Wanga7ba4552021-03-30 19:32:30 -07002965 case BPF_SK_SKB_VERDICT:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002966 return BPF_PROG_TYPE_SK_SKB;
2967 case BPF_LIRC_MODE2:
2968 return BPF_PROG_TYPE_LIRC_MODE2;
2969 case BPF_FLOW_DISSECTOR:
2970 return BPF_PROG_TYPE_FLOW_DISSECTOR;
2971 case BPF_CGROUP_SYSCTL:
2972 return BPF_PROG_TYPE_CGROUP_SYSCTL;
2973 case BPF_CGROUP_GETSOCKOPT:
2974 case BPF_CGROUP_SETSOCKOPT:
2975 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
Yonghong Songde4e05c2020-05-09 10:59:01 -07002976 case BPF_TRACE_ITER:
2977 return BPF_PROG_TYPE_TRACING;
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02002978 case BPF_SK_LOOKUP:
2979 return BPF_PROG_TYPE_SK_LOOKUP;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07002980 case BPF_XDP:
2981 return BPF_PROG_TYPE_XDP;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002982 default:
2983 return BPF_PROG_TYPE_UNSPEC;
2984 }
2985}
2986
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08002987#define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
John Fastabend174a79f2017-08-15 22:32:47 -07002988
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002989#define BPF_F_ATTACH_MASK \
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08002990 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002991
Daniel Mackf4324552016-11-23 16:52:27 +01002992static int bpf_prog_attach(const union bpf_attr *attr)
2993{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002994 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01002995 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002996 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01002997
Daniel Mackf4324552016-11-23 16:52:27 +01002998 if (CHECK_ATTR(BPF_PROG_ATTACH))
2999 return -EINVAL;
3000
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003001 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003002 return -EINVAL;
3003
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003004 ptype = attach_type_to_prog_type(attr->attach_type);
3005 if (ptype == BPF_PROG_TYPE_UNSPEC)
Daniel Mackf4324552016-11-23 16:52:27 +01003006 return -EINVAL;
Daniel Mackf4324552016-11-23 16:52:27 +01003007
David Ahernb2cd1252016-12-01 08:48:03 -08003008 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3009 if (IS_ERR(prog))
3010 return PTR_ERR(prog);
3011
Andrey Ignatov5e43f892018-03-30 15:08:00 -07003012 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3013 bpf_prog_put(prog);
3014 return -EINVAL;
3015 }
3016
Sean Youngfdb5c452018-06-19 00:04:24 +01003017 switch (ptype) {
3018 case BPF_PROG_TYPE_SK_SKB:
3019 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02003020 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01003021 break;
3022 case BPF_PROG_TYPE_LIRC_MODE2:
3023 ret = lirc_prog_attach(attr, prog);
3024 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07003025 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +02003026 ret = netns_bpf_prog_attach(attr, prog);
Petar Penkovd58e4682018-09-14 07:46:18 -07003027 break;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003028 case BPF_PROG_TYPE_CGROUP_DEVICE:
3029 case BPF_PROG_TYPE_CGROUP_SKB:
3030 case BPF_PROG_TYPE_CGROUP_SOCK:
3031 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3032 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3033 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3034 case BPF_PROG_TYPE_SOCK_OPS:
Sean Youngfdb5c452018-06-19 00:04:24 +01003035 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003036 break;
3037 default:
3038 ret = -EINVAL;
David Ahernb2cd1252016-12-01 08:48:03 -08003039 }
3040
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003041 if (ret)
3042 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003043 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01003044}
3045
3046#define BPF_PROG_DETACH_LAST_FIELD attach_type
3047
3048static int bpf_prog_detach(const union bpf_attr *attr)
3049{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003050 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01003051
Daniel Mackf4324552016-11-23 16:52:27 +01003052 if (CHECK_ATTR(BPF_PROG_DETACH))
3053 return -EINVAL;
3054
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003055 ptype = attach_type_to_prog_type(attr->attach_type);
3056
3057 switch (ptype) {
3058 case BPF_PROG_TYPE_SK_MSG:
3059 case BPF_PROG_TYPE_SK_SKB:
Lorenz Bauerbb0de312020-06-29 10:56:28 +01003060 return sock_map_prog_detach(attr, ptype);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003061 case BPF_PROG_TYPE_LIRC_MODE2:
Sean Youngf4364dc2018-05-27 12:24:09 +01003062 return lirc_prog_detach(attr);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003063 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Lorenz Bauer4ac2add2020-06-29 10:56:26 +01003064 return netns_bpf_prog_detach(attr, ptype);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003065 case BPF_PROG_TYPE_CGROUP_DEVICE:
3066 case BPF_PROG_TYPE_CGROUP_SKB:
3067 case BPF_PROG_TYPE_CGROUP_SOCK:
3068 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3069 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3070 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3071 case BPF_PROG_TYPE_SOCK_OPS:
3072 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01003073 default:
3074 return -EINVAL;
3075 }
Daniel Mackf4324552016-11-23 16:52:27 +01003076}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07003077
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003078#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3079
3080static int bpf_prog_query(const union bpf_attr *attr,
3081 union bpf_attr __user *uattr)
3082{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003083 if (!capable(CAP_NET_ADMIN))
3084 return -EPERM;
3085 if (CHECK_ATTR(BPF_PROG_QUERY))
3086 return -EINVAL;
3087 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3088 return -EINVAL;
3089
3090 switch (attr->query.attach_type) {
3091 case BPF_CGROUP_INET_INGRESS:
3092 case BPF_CGROUP_INET_EGRESS:
3093 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07003094 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07003095 case BPF_CGROUP_INET4_BIND:
3096 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07003097 case BPF_CGROUP_INET4_POST_BIND:
3098 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07003099 case BPF_CGROUP_INET4_CONNECT:
3100 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02003101 case BPF_CGROUP_INET4_GETPEERNAME:
3102 case BPF_CGROUP_INET6_GETPEERNAME:
3103 case BPF_CGROUP_INET4_GETSOCKNAME:
3104 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07003105 case BPF_CGROUP_UDP4_SENDMSG:
3106 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02003107 case BPF_CGROUP_UDP4_RECVMSG:
3108 case BPF_CGROUP_UDP6_RECVMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003109 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05003110 case BPF_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08003111 case BPF_CGROUP_SYSCTL:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07003112 case BPF_CGROUP_GETSOCKOPT:
3113 case BPF_CGROUP_SETSOCKOPT:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003114 return cgroup_bpf_prog_query(attr, uattr);
Sean Youngf4364dc2018-05-27 12:24:09 +01003115 case BPF_LIRC_MODE2:
3116 return lirc_prog_query(attr, uattr);
Stanislav Fomichev118c8e92019-04-25 14:37:23 -07003117 case BPF_FLOW_DISSECTOR:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02003118 case BPF_SK_LOOKUP:
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +02003119 return netns_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003120 default:
3121 return -EINVAL;
3122 }
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003123}
Daniel Mackf4324552016-11-23 16:52:27 +01003124
Song Liu1b4d60e2020-09-25 13:54:29 -07003125#define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003126
3127static int bpf_prog_test_run(const union bpf_attr *attr,
3128 union bpf_attr __user *uattr)
3129{
3130 struct bpf_prog *prog;
3131 int ret = -ENOTSUPP;
3132
3133 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3134 return -EINVAL;
3135
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07003136 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3137 (!attr->test.ctx_size_in && attr->test.ctx_in))
3138 return -EINVAL;
3139
3140 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3141 (!attr->test.ctx_size_out && attr->test.ctx_out))
3142 return -EINVAL;
3143
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003144 prog = bpf_prog_get(attr->test.prog_fd);
3145 if (IS_ERR(prog))
3146 return PTR_ERR(prog);
3147
3148 if (prog->aux->ops->test_run)
3149 ret = prog->aux->ops->test_run(prog, attr, uattr);
3150
3151 bpf_prog_put(prog);
3152 return ret;
3153}
3154
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07003155#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3156
3157static int bpf_obj_get_next_id(const union bpf_attr *attr,
3158 union bpf_attr __user *uattr,
3159 struct idr *idr,
3160 spinlock_t *lock)
3161{
3162 u32 next_id = attr->start_id;
3163 int err = 0;
3164
3165 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3166 return -EINVAL;
3167
3168 if (!capable(CAP_SYS_ADMIN))
3169 return -EPERM;
3170
3171 next_id++;
3172 spin_lock_bh(lock);
3173 if (!idr_get_next(idr, &next_id))
3174 err = -ENOENT;
3175 spin_unlock_bh(lock);
3176
3177 if (!err)
3178 err = put_user(next_id, &uattr->next_id);
3179
3180 return err;
3181}
3182
Yonghong Song6086d292020-05-09 10:59:09 -07003183struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3184{
3185 struct bpf_map *map;
3186
3187 spin_lock_bh(&map_idr_lock);
3188again:
3189 map = idr_get_next(&map_idr, id);
3190 if (map) {
3191 map = __bpf_map_inc_not_zero(map, false);
3192 if (IS_ERR(map)) {
3193 (*id)++;
3194 goto again;
3195 }
3196 }
3197 spin_unlock_bh(&map_idr_lock);
3198
3199 return map;
3200}
3201
Alexei Starovoitova228a642020-07-01 18:10:18 -07003202struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3203{
3204 struct bpf_prog *prog;
3205
3206 spin_lock_bh(&prog_idr_lock);
3207again:
3208 prog = idr_get_next(&prog_idr, id);
3209 if (prog) {
3210 prog = bpf_prog_inc_not_zero(prog);
3211 if (IS_ERR(prog)) {
3212 (*id)++;
3213 goto again;
3214 }
3215 }
3216 spin_unlock_bh(&prog_idr_lock);
3217
3218 return prog;
3219}
3220
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003221#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3222
Björn Töpel7e6897f2019-12-13 18:51:09 +01003223struct bpf_prog *bpf_prog_by_id(u32 id)
3224{
3225 struct bpf_prog *prog;
3226
3227 if (!id)
3228 return ERR_PTR(-ENOENT);
3229
3230 spin_lock_bh(&prog_idr_lock);
3231 prog = idr_find(&prog_idr, id);
3232 if (prog)
3233 prog = bpf_prog_inc_not_zero(prog);
3234 else
3235 prog = ERR_PTR(-ENOENT);
3236 spin_unlock_bh(&prog_idr_lock);
3237 return prog;
3238}
3239
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003240static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3241{
3242 struct bpf_prog *prog;
3243 u32 id = attr->prog_id;
3244 int fd;
3245
3246 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3247 return -EINVAL;
3248
3249 if (!capable(CAP_SYS_ADMIN))
3250 return -EPERM;
3251
Björn Töpel7e6897f2019-12-13 18:51:09 +01003252 prog = bpf_prog_by_id(id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003253 if (IS_ERR(prog))
3254 return PTR_ERR(prog);
3255
3256 fd = bpf_prog_new_fd(prog);
3257 if (fd < 0)
3258 bpf_prog_put(prog);
3259
3260 return fd;
3261}
3262
Chenbo Feng6e71b042017-10-18 13:00:22 -07003263#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003264
3265static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3266{
3267 struct bpf_map *map;
3268 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07003269 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003270 int fd;
3271
Chenbo Feng6e71b042017-10-18 13:00:22 -07003272 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3273 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003274 return -EINVAL;
3275
3276 if (!capable(CAP_SYS_ADMIN))
3277 return -EPERM;
3278
Chenbo Feng6e71b042017-10-18 13:00:22 -07003279 f_flags = bpf_get_file_flag(attr->open_flags);
3280 if (f_flags < 0)
3281 return f_flags;
3282
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003283 spin_lock_bh(&map_idr_lock);
3284 map = idr_find(&map_idr, id);
3285 if (map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -07003286 map = __bpf_map_inc_not_zero(map, true);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003287 else
3288 map = ERR_PTR(-ENOENT);
3289 spin_unlock_bh(&map_idr_lock);
3290
3291 if (IS_ERR(map))
3292 return PTR_ERR(map);
3293
Chenbo Feng6e71b042017-10-18 13:00:22 -07003294 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003295 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08003296 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003297
3298 return fd;
3299}
3300
Daniel Borkmann7105e822017-12-20 13:42:57 +01003301static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003302 unsigned long addr, u32 *off,
3303 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01003304{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003305 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003306 int i;
3307
YiFei Zhu984fe942020-09-15 16:45:39 -07003308 mutex_lock(&prog->aux->used_maps_mutex);
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003309 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3310 map = prog->aux->used_maps[i];
3311 if (map == (void *)addr) {
3312 *type = BPF_PSEUDO_MAP_FD;
YiFei Zhu984fe942020-09-15 16:45:39 -07003313 goto out;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003314 }
3315 if (!map->ops->map_direct_value_meta)
3316 continue;
3317 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3318 *type = BPF_PSEUDO_MAP_VALUE;
YiFei Zhu984fe942020-09-15 16:45:39 -07003319 goto out;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003320 }
3321 }
YiFei Zhu984fe942020-09-15 16:45:39 -07003322 map = NULL;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003323
YiFei Zhu984fe942020-09-15 16:45:39 -07003324out:
3325 mutex_unlock(&prog->aux->used_maps_mutex);
3326 return map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003327}
3328
Kees Cook63960262020-07-02 15:45:23 -07003329static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3330 const struct cred *f_cred)
Daniel Borkmann7105e822017-12-20 13:42:57 +01003331{
3332 const struct bpf_map *map;
3333 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003334 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003335 u64 imm;
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003336 u8 code;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003337 int i;
3338
3339 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3340 GFP_USER);
3341 if (!insns)
3342 return insns;
3343
3344 for (i = 0; i < prog->len; i++) {
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003345 code = insns[i].code;
3346
3347 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003348 insns[i].code = BPF_JMP | BPF_CALL;
3349 insns[i].imm = BPF_FUNC_tail_call;
3350 /* fall-through */
3351 }
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003352 if (code == (BPF_JMP | BPF_CALL) ||
3353 code == (BPF_JMP | BPF_CALL_ARGS)) {
3354 if (code == (BPF_JMP | BPF_CALL_ARGS))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003355 insns[i].code = BPF_JMP | BPF_CALL;
Kees Cook63960262020-07-02 15:45:23 -07003356 if (!bpf_dump_raw_ok(f_cred))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003357 insns[i].imm = 0;
3358 continue;
3359 }
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003360 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3361 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3362 continue;
3363 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01003364
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003365 if (code != (BPF_LD | BPF_IMM | BPF_DW))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003366 continue;
3367
3368 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003369 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003370 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003371 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003372 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003373 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003374 continue;
3375 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01003376 }
3377
3378 return insns;
3379}
3380
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003381static int set_info_rec_size(struct bpf_prog_info *info)
3382{
3383 /*
3384 * Ensure info.*_rec_size is the same as kernel expected size
3385 *
3386 * or
3387 *
3388 * Only allow zero *_rec_size if both _rec_size and _cnt are
3389 * zero. In this case, the kernel will set the expected
3390 * _rec_size back to the info.
3391 */
3392
Yonghong Song11d8b822018-12-10 14:14:08 -08003393 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003394 info->func_info_rec_size != sizeof(struct bpf_func_info))
3395 return -EINVAL;
3396
Yonghong Song11d8b822018-12-10 14:14:08 -08003397 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003398 info->line_info_rec_size != sizeof(struct bpf_line_info))
3399 return -EINVAL;
3400
Yonghong Song11d8b822018-12-10 14:14:08 -08003401 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003402 info->jited_line_info_rec_size != sizeof(__u64))
3403 return -EINVAL;
3404
3405 info->func_info_rec_size = sizeof(struct bpf_func_info);
3406 info->line_info_rec_size = sizeof(struct bpf_line_info);
3407 info->jited_line_info_rec_size = sizeof(__u64);
3408
3409 return 0;
3410}
3411
Kees Cook63960262020-07-02 15:45:23 -07003412static int bpf_prog_get_info_by_fd(struct file *file,
3413 struct bpf_prog *prog,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003414 const union bpf_attr *attr,
3415 union bpf_attr __user *uattr)
3416{
3417 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003418 struct bpf_prog_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003419 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003420 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003421 char __user *uinsns;
3422 u32 ulen;
3423 int err;
3424
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003425 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003426 if (err)
3427 return err;
3428 info_len = min_t(u32, sizeof(info), info_len);
3429
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003430 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003431 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02003432 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003433
3434 info.type = prog->type;
3435 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003436 info.load_time = prog->aux->load_time;
3437 info.created_by_uid = from_kuid_munged(current_user_ns(),
3438 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02003439 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003440
3441 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003442 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3443
YiFei Zhu984fe942020-09-15 16:45:39 -07003444 mutex_lock(&prog->aux->used_maps_mutex);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003445 ulen = info.nr_map_ids;
3446 info.nr_map_ids = prog->aux->used_map_cnt;
3447 ulen = min_t(u32, info.nr_map_ids, ulen);
3448 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07003449 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003450 u32 i;
3451
3452 for (i = 0; i < ulen; i++)
3453 if (put_user(prog->aux->used_maps[i]->id,
YiFei Zhu984fe942020-09-15 16:45:39 -07003454 &user_map_ids[i])) {
3455 mutex_unlock(&prog->aux->used_maps_mutex);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003456 return -EFAULT;
YiFei Zhu984fe942020-09-15 16:45:39 -07003457 }
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003458 }
YiFei Zhu984fe942020-09-15 16:45:39 -07003459 mutex_unlock(&prog->aux->used_maps_mutex);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003460
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003461 err = set_info_rec_size(&info);
3462 if (err)
3463 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08003464
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003465 bpf_prog_get_stats(prog, &stats);
3466 info.run_time_ns = stats.nsecs;
3467 info.run_cnt = stats.cnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08003468 info.recursion_misses = stats.misses;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003469
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07003470 if (!bpf_capable()) {
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003471 info.jited_prog_len = 0;
3472 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303473 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01003474 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08003475 info.nr_func_info = 0;
3476 info.nr_line_info = 0;
3477 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003478 goto done;
3479 }
3480
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003481 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02003482 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003483 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003484 struct bpf_insn *insns_sanitized;
3485 bool fault;
3486
Kees Cook63960262020-07-02 15:45:23 -07003487 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003488 info.xlated_prog_insns = 0;
3489 goto done;
3490 }
Kees Cook63960262020-07-02 15:45:23 -07003491 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003492 if (!insns_sanitized)
3493 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003494 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3495 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003496 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3497 kfree(insns_sanitized);
3498 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003499 return -EFAULT;
3500 }
3501
Jakub Kicinski675fc272017-12-27 18:39:09 -08003502 if (bpf_prog_is_dev_bound(prog->aux)) {
3503 err = bpf_prog_offload_info_fill(&info, prog);
3504 if (err)
3505 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08003506 goto done;
3507 }
3508
3509 /* NOTE: the following code is supposed to be skipped for offload.
3510 * bpf_prog_offload_info_fill() is the place to fill similar fields
3511 * for offload.
3512 */
3513 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05303514 if (prog->aux->func_cnt) {
3515 u32 i;
3516
3517 info.jited_prog_len = 0;
3518 for (i = 0; i < prog->aux->func_cnt; i++)
3519 info.jited_prog_len += prog->aux->func[i]->jited_len;
3520 } else {
3521 info.jited_prog_len = prog->jited_len;
3522 }
3523
Jiong Wangfcfb1262018-01-16 16:05:19 -08003524 if (info.jited_prog_len && ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003525 if (bpf_dump_raw_ok(file->f_cred)) {
Jiong Wangfcfb1262018-01-16 16:05:19 -08003526 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3527 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05303528
3529 /* for multi-function programs, copy the JITed
3530 * instructions for all the functions
3531 */
3532 if (prog->aux->func_cnt) {
3533 u32 len, free, i;
3534 u8 *img;
3535
3536 free = ulen;
3537 for (i = 0; i < prog->aux->func_cnt; i++) {
3538 len = prog->aux->func[i]->jited_len;
3539 len = min_t(u32, len, free);
3540 img = (u8 *) prog->aux->func[i]->bpf_func;
3541 if (copy_to_user(uinsns, img, len))
3542 return -EFAULT;
3543 uinsns += len;
3544 free -= len;
3545 if (!free)
3546 break;
3547 }
3548 } else {
3549 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3550 return -EFAULT;
3551 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08003552 } else {
3553 info.jited_prog_insns = 0;
3554 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08003555 }
3556
Sandipan Dasdbecd732018-05-24 12:26:48 +05303557 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07003558 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003559 if (ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003560 if (bpf_dump_raw_ok(file->f_cred)) {
Song Liuff1889f2018-11-02 10:16:17 -07003561 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303562 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303563 u32 i;
3564
3565 /* copy the address of the kernel symbol
3566 * corresponding to each function
3567 */
3568 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3569 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07003570 if (prog->aux->func_cnt) {
3571 for (i = 0; i < ulen; i++) {
3572 ksym_addr = (unsigned long)
3573 prog->aux->func[i]->bpf_func;
3574 if (put_user((u64) ksym_addr,
3575 &user_ksyms[i]))
3576 return -EFAULT;
3577 }
3578 } else {
3579 ksym_addr = (unsigned long) prog->bpf_func;
3580 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05303581 return -EFAULT;
3582 }
3583 } else {
3584 info.jited_ksyms = 0;
3585 }
3586 }
3587
Sandipan Das815581c2018-05-24 12:26:52 +05303588 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07003589 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003590 if (ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003591 if (bpf_dump_raw_ok(file->f_cred)) {
Sandipan Das815581c2018-05-24 12:26:52 +05303592 u32 __user *user_lens;
3593 u32 func_len, i;
3594
3595 /* copy the JITed image lengths for each function */
3596 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3597 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07003598 if (prog->aux->func_cnt) {
3599 for (i = 0; i < ulen; i++) {
3600 func_len =
3601 prog->aux->func[i]->jited_len;
3602 if (put_user(func_len, &user_lens[i]))
3603 return -EFAULT;
3604 }
3605 } else {
3606 func_len = prog->jited_len;
3607 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05303608 return -EFAULT;
3609 }
3610 } else {
3611 info.jited_func_lens = 0;
3612 }
3613 }
3614
Martin KaFai Lau73372242018-12-05 17:35:43 -08003615 if (prog->aux->btf)
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08003616 info.btf_id = btf_obj_id(prog->aux->btf);
Yonghong Song838e9692018-11-19 15:29:11 -08003617
Yonghong Song11d8b822018-12-10 14:14:08 -08003618 ulen = info.nr_func_info;
3619 info.nr_func_info = prog->aux->func_info_cnt;
3620 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003621 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08003622
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003623 user_finfo = u64_to_user_ptr(info.func_info);
3624 ulen = min_t(u32, info.nr_func_info, ulen);
3625 if (copy_to_user(user_finfo, prog->aux->func_info,
3626 info.func_info_rec_size * ulen))
3627 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08003628 }
3629
Yonghong Song11d8b822018-12-10 14:14:08 -08003630 ulen = info.nr_line_info;
3631 info.nr_line_info = prog->aux->nr_linfo;
3632 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003633 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003634
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003635 user_linfo = u64_to_user_ptr(info.line_info);
3636 ulen = min_t(u32, info.nr_line_info, ulen);
3637 if (copy_to_user(user_linfo, prog->aux->linfo,
3638 info.line_info_rec_size * ulen))
3639 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003640 }
3641
Yonghong Song11d8b822018-12-10 14:14:08 -08003642 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003643 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08003644 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003645 else
Yonghong Song11d8b822018-12-10 14:14:08 -08003646 info.nr_jited_line_info = 0;
3647 if (info.nr_jited_line_info && ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003648 if (bpf_dump_raw_ok(file->f_cred)) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003649 __u64 __user *user_linfo;
3650 u32 i;
3651
3652 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08003653 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003654 for (i = 0; i < ulen; i++) {
3655 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3656 &user_linfo[i]))
3657 return -EFAULT;
3658 }
3659 } else {
3660 info.jited_line_info = 0;
3661 }
3662 }
3663
Song Liuc872bdb2018-12-12 09:37:46 -08003664 ulen = info.nr_prog_tags;
3665 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3666 if (ulen) {
3667 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3668 u32 i;
3669
3670 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3671 ulen = min_t(u32, info.nr_prog_tags, ulen);
3672 if (prog->aux->func_cnt) {
3673 for (i = 0; i < ulen; i++) {
3674 if (copy_to_user(user_prog_tags[i],
3675 prog->aux->func[i]->tag,
3676 BPF_TAG_SIZE))
3677 return -EFAULT;
3678 }
3679 } else {
3680 if (copy_to_user(user_prog_tags[0],
3681 prog->tag, BPF_TAG_SIZE))
3682 return -EFAULT;
3683 }
3684 }
3685
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003686done:
3687 if (copy_to_user(uinfo, &info, info_len) ||
3688 put_user(info_len, &uattr->info.info_len))
3689 return -EFAULT;
3690
3691 return 0;
3692}
3693
Kees Cook63960262020-07-02 15:45:23 -07003694static int bpf_map_get_info_by_fd(struct file *file,
3695 struct bpf_map *map,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003696 const union bpf_attr *attr,
3697 union bpf_attr __user *uattr)
3698{
3699 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003700 struct bpf_map_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003701 u32 info_len = attr->info.info_len;
3702 int err;
3703
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003704 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003705 if (err)
3706 return err;
3707 info_len = min_t(u32, sizeof(info), info_len);
3708
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003709 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003710 info.type = map->map_type;
3711 info.id = map->id;
3712 info.key_size = map->key_size;
3713 info.value_size = map->value_size;
3714 info.max_entries = map->max_entries;
3715 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07003716 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003717
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003718 if (map->btf) {
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08003719 info.btf_id = btf_obj_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07003720 info.btf_key_type_id = map->btf_key_type_id;
3721 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003722 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08003723 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003724
Jakub Kicinski52775b32018-01-17 19:13:28 -08003725 if (bpf_map_is_dev_bound(map)) {
3726 err = bpf_map_offload_info_fill(&info, map);
3727 if (err)
3728 return err;
3729 }
3730
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003731 if (copy_to_user(uinfo, &info, info_len) ||
3732 put_user(info_len, &uattr->info.info_len))
3733 return -EFAULT;
3734
3735 return 0;
3736}
3737
Kees Cook63960262020-07-02 15:45:23 -07003738static int bpf_btf_get_info_by_fd(struct file *file,
3739 struct btf *btf,
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003740 const union bpf_attr *attr,
3741 union bpf_attr __user *uattr)
3742{
3743 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3744 u32 info_len = attr->info.info_len;
3745 int err;
3746
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003747 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003748 if (err)
3749 return err;
3750
3751 return btf_get_info_by_fd(btf, attr, uattr);
3752}
3753
Kees Cook63960262020-07-02 15:45:23 -07003754static int bpf_link_get_info_by_fd(struct file *file,
3755 struct bpf_link *link,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003756 const union bpf_attr *attr,
3757 union bpf_attr __user *uattr)
3758{
3759 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3760 struct bpf_link_info info;
3761 u32 info_len = attr->info.info_len;
3762 int err;
3763
3764 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3765 if (err)
3766 return err;
3767 info_len = min_t(u32, sizeof(info), info_len);
3768
3769 memset(&info, 0, sizeof(info));
3770 if (copy_from_user(&info, uinfo, info_len))
3771 return -EFAULT;
3772
3773 info.type = link->type;
3774 info.id = link->id;
3775 info.prog_id = link->prog->aux->id;
3776
3777 if (link->ops->fill_link_info) {
3778 err = link->ops->fill_link_info(link, &info);
3779 if (err)
3780 return err;
3781 }
3782
3783 if (copy_to_user(uinfo, &info, info_len) ||
3784 put_user(info_len, &uattr->info.info_len))
3785 return -EFAULT;
3786
3787 return 0;
3788}
3789
3790
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003791#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3792
3793static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3794 union bpf_attr __user *uattr)
3795{
3796 int ufd = attr->info.bpf_fd;
3797 struct fd f;
3798 int err;
3799
3800 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3801 return -EINVAL;
3802
3803 f = fdget(ufd);
3804 if (!f.file)
3805 return -EBADFD;
3806
3807 if (f.file->f_op == &bpf_prog_fops)
Kees Cook63960262020-07-02 15:45:23 -07003808 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003809 uattr);
3810 else if (f.file->f_op == &bpf_map_fops)
Kees Cook63960262020-07-02 15:45:23 -07003811 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003812 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07003813 else if (f.file->f_op == &btf_fops)
Kees Cook63960262020-07-02 15:45:23 -07003814 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003815 else if (f.file->f_op == &bpf_link_fops)
Kees Cook63960262020-07-02 15:45:23 -07003816 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003817 attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003818 else
3819 err = -EINVAL;
3820
3821 fdput(f);
3822 return err;
3823}
3824
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003825#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3826
3827static int bpf_btf_load(const union bpf_attr *attr)
3828{
3829 if (CHECK_ATTR(BPF_BTF_LOAD))
3830 return -EINVAL;
3831
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07003832 if (!bpf_capable())
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003833 return -EPERM;
3834
3835 return btf_new_fd(attr);
3836}
3837
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003838#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3839
3840static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3841{
3842 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3843 return -EINVAL;
3844
3845 if (!capable(CAP_SYS_ADMIN))
3846 return -EPERM;
3847
3848 return btf_get_fd_by_id(attr->btf_id);
3849}
3850
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003851static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3852 union bpf_attr __user *uattr,
3853 u32 prog_id, u32 fd_type,
3854 const char *buf, u64 probe_offset,
3855 u64 probe_addr)
3856{
3857 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3858 u32 len = buf ? strlen(buf) : 0, input_len;
3859 int err = 0;
3860
3861 if (put_user(len, &uattr->task_fd_query.buf_len))
3862 return -EFAULT;
3863 input_len = attr->task_fd_query.buf_len;
3864 if (input_len && ubuf) {
3865 if (!len) {
3866 /* nothing to copy, just make ubuf NULL terminated */
3867 char zero = '\0';
3868
3869 if (put_user(zero, ubuf))
3870 return -EFAULT;
3871 } else if (input_len >= len + 1) {
3872 /* ubuf can hold the string with NULL terminator */
3873 if (copy_to_user(ubuf, buf, len + 1))
3874 return -EFAULT;
3875 } else {
3876 /* ubuf cannot hold the string with NULL terminator,
3877 * do a partial copy with NULL terminator.
3878 */
3879 char zero = '\0';
3880
3881 err = -ENOSPC;
3882 if (copy_to_user(ubuf, buf, input_len - 1))
3883 return -EFAULT;
3884 if (put_user(zero, ubuf + input_len - 1))
3885 return -EFAULT;
3886 }
3887 }
3888
3889 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3890 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3891 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3892 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3893 return -EFAULT;
3894
3895 return err;
3896}
3897
3898#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3899
3900static int bpf_task_fd_query(const union bpf_attr *attr,
3901 union bpf_attr __user *uattr)
3902{
3903 pid_t pid = attr->task_fd_query.pid;
3904 u32 fd = attr->task_fd_query.fd;
3905 const struct perf_event *event;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003906 struct task_struct *task;
3907 struct file *file;
3908 int err;
3909
3910 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3911 return -EINVAL;
3912
3913 if (!capable(CAP_SYS_ADMIN))
3914 return -EPERM;
3915
3916 if (attr->task_fd_query.flags != 0)
3917 return -EINVAL;
3918
3919 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3920 if (!task)
3921 return -ENOENT;
3922
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003923 err = 0;
Eric W. Biedermanb48845a2020-11-20 17:14:22 -06003924 file = fget_task(task, fd);
3925 put_task_struct(task);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003926 if (!file)
Eric W. Biedermanb48845a2020-11-20 17:14:22 -06003927 return -EBADF;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003928
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003929 if (file->f_op == &bpf_link_fops) {
3930 struct bpf_link *link = file->private_data;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003931
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003932 if (link->ops == &bpf_raw_tp_link_lops) {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003933 struct bpf_raw_tp_link *raw_tp =
3934 container_of(link, struct bpf_raw_tp_link, link);
3935 struct bpf_raw_event_map *btp = raw_tp->btp;
3936
3937 err = bpf_task_fd_query_copy(attr, uattr,
3938 raw_tp->link.prog->aux->id,
3939 BPF_FD_TYPE_RAW_TRACEPOINT,
3940 btp->tp->name, 0, 0);
3941 goto put_file;
3942 }
3943 goto out_not_supp;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003944 }
3945
3946 event = perf_get_event(file);
3947 if (!IS_ERR(event)) {
3948 u64 probe_offset, probe_addr;
3949 u32 prog_id, fd_type;
3950 const char *buf;
3951
3952 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3953 &buf, &probe_offset,
3954 &probe_addr);
3955 if (!err)
3956 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3957 fd_type, buf,
3958 probe_offset,
3959 probe_addr);
3960 goto put_file;
3961 }
3962
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003963out_not_supp:
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003964 err = -ENOTSUPP;
3965put_file:
3966 fput(file);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003967 return err;
3968}
3969
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003970#define BPF_MAP_BATCH_LAST_FIELD batch.flags
3971
3972#define BPF_DO_BATCH(fn) \
3973 do { \
3974 if (!fn) { \
3975 err = -ENOTSUPP; \
3976 goto err_put; \
3977 } \
3978 err = fn(map, attr, uattr); \
3979 } while (0)
3980
3981static int bpf_map_do_batch(const union bpf_attr *attr,
3982 union bpf_attr __user *uattr,
3983 int cmd)
3984{
3985 struct bpf_map *map;
3986 int err, ufd;
3987 struct fd f;
3988
3989 if (CHECK_ATTR(BPF_MAP_BATCH))
3990 return -EINVAL;
3991
3992 ufd = attr->batch.map_fd;
3993 f = fdget(ufd);
3994 map = __bpf_map_get(f);
3995 if (IS_ERR(map))
3996 return PTR_ERR(map);
3997
Yonghong Song05799632020-01-15 10:43:04 -08003998 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
3999 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004000 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4001 err = -EPERM;
4002 goto err_put;
4003 }
4004
4005 if (cmd != BPF_MAP_LOOKUP_BATCH &&
4006 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4007 err = -EPERM;
4008 goto err_put;
4009 }
4010
4011 if (cmd == BPF_MAP_LOOKUP_BATCH)
4012 BPF_DO_BATCH(map->ops->map_lookup_batch);
Yonghong Song05799632020-01-15 10:43:04 -08004013 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4014 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004015 else if (cmd == BPF_MAP_UPDATE_BATCH)
4016 BPF_DO_BATCH(map->ops->map_update_batch);
4017 else
4018 BPF_DO_BATCH(map->ops->map_delete_batch);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004019
4020err_put:
4021 fdput(f);
4022 return err;
4023}
4024
Yonghong Songde4e05c2020-05-09 10:59:01 -07004025static int tracing_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
4026{
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004027 if (attr->link_create.attach_type != prog->expected_attach_type)
4028 return -EINVAL;
Yonghong Songde4e05c2020-05-09 10:59:01 -07004029
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004030 if (prog->expected_attach_type == BPF_TRACE_ITER)
4031 return bpf_iter_link_attach(attr, prog);
4032 else if (prog->type == BPF_PROG_TYPE_EXT)
4033 return bpf_tracing_prog_attach(prog,
4034 attr->link_create.target_fd,
4035 attr->link_create.target_btf_id);
Yonghong Songde4e05c2020-05-09 10:59:01 -07004036 return -EINVAL;
4037}
4038
Yonghong Song5e7b3022020-08-04 22:50:56 -07004039#define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004040static int link_create(union bpf_attr *attr)
4041{
4042 enum bpf_prog_type ptype;
4043 struct bpf_prog *prog;
4044 int ret;
4045
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004046 if (CHECK_ATTR(BPF_LINK_CREATE))
4047 return -EINVAL;
4048
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004049 prog = bpf_prog_get(attr->link_create.prog_fd);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004050 if (IS_ERR(prog))
4051 return PTR_ERR(prog);
4052
4053 ret = bpf_prog_attach_check_attach_type(prog,
4054 attr->link_create.attach_type);
4055 if (ret)
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004056 goto out;
4057
4058 if (prog->type == BPF_PROG_TYPE_EXT) {
4059 ret = tracing_bpf_link_attach(attr, prog);
4060 goto out;
4061 }
4062
4063 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4064 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4065 ret = -EINVAL;
4066 goto out;
4067 }
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004068
4069 switch (ptype) {
4070 case BPF_PROG_TYPE_CGROUP_SKB:
4071 case BPF_PROG_TYPE_CGROUP_SOCK:
4072 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4073 case BPF_PROG_TYPE_SOCK_OPS:
4074 case BPF_PROG_TYPE_CGROUP_DEVICE:
4075 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4076 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4077 ret = cgroup_bpf_link_attach(attr, prog);
4078 break;
Yonghong Songde4e05c2020-05-09 10:59:01 -07004079 case BPF_PROG_TYPE_TRACING:
4080 ret = tracing_bpf_link_attach(attr, prog);
4081 break;
Jakub Sitnicki7f045a42020-05-31 10:28:38 +02004082 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02004083 case BPF_PROG_TYPE_SK_LOOKUP:
Jakub Sitnicki7f045a42020-05-31 10:28:38 +02004084 ret = netns_bpf_link_create(attr, prog);
4085 break;
Andrii Nakryiko310ad792020-07-28 12:05:27 -07004086#ifdef CONFIG_NET
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07004087 case BPF_PROG_TYPE_XDP:
4088 ret = bpf_xdp_link_attach(attr, prog);
4089 break;
Andrii Nakryiko310ad792020-07-28 12:05:27 -07004090#endif
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004091 default:
4092 ret = -EINVAL;
4093 }
4094
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004095out:
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004096 if (ret < 0)
4097 bpf_prog_put(prog);
4098 return ret;
4099}
4100
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004101#define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4102
4103static int link_update(union bpf_attr *attr)
4104{
4105 struct bpf_prog *old_prog = NULL, *new_prog;
4106 struct bpf_link *link;
4107 u32 flags;
4108 int ret;
4109
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004110 if (CHECK_ATTR(BPF_LINK_UPDATE))
4111 return -EINVAL;
4112
4113 flags = attr->link_update.flags;
4114 if (flags & ~BPF_F_REPLACE)
4115 return -EINVAL;
4116
4117 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4118 if (IS_ERR(link))
4119 return PTR_ERR(link);
4120
4121 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004122 if (IS_ERR(new_prog)) {
4123 ret = PTR_ERR(new_prog);
4124 goto out_put_link;
4125 }
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004126
4127 if (flags & BPF_F_REPLACE) {
4128 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4129 if (IS_ERR(old_prog)) {
4130 ret = PTR_ERR(old_prog);
4131 old_prog = NULL;
4132 goto out_put_progs;
4133 }
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004134 } else if (attr->link_update.old_prog_fd) {
4135 ret = -EINVAL;
4136 goto out_put_progs;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004137 }
4138
Andrii Nakryikof9d04122020-04-28 17:16:05 -07004139 if (link->ops->update_prog)
4140 ret = link->ops->update_prog(link, new_prog, old_prog);
4141 else
Jakub Sitnickife537392020-05-25 14:29:28 +02004142 ret = -EINVAL;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004143
4144out_put_progs:
4145 if (old_prog)
4146 bpf_prog_put(old_prog);
4147 if (ret)
4148 bpf_prog_put(new_prog);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004149out_put_link:
4150 bpf_link_put(link);
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004151 return ret;
4152}
4153
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07004154#define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4155
4156static int link_detach(union bpf_attr *attr)
4157{
4158 struct bpf_link *link;
4159 int ret;
4160
4161 if (CHECK_ATTR(BPF_LINK_DETACH))
4162 return -EINVAL;
4163
4164 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4165 if (IS_ERR(link))
4166 return PTR_ERR(link);
4167
4168 if (link->ops->detach)
4169 ret = link->ops->detach(link);
4170 else
4171 ret = -EOPNOTSUPP;
4172
4173 bpf_link_put(link);
4174 return ret;
4175}
4176
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004177static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004178{
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004179 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4180}
4181
4182struct bpf_link *bpf_link_by_id(u32 id)
4183{
4184 struct bpf_link *link;
4185
4186 if (!id)
4187 return ERR_PTR(-ENOENT);
4188
4189 spin_lock_bh(&link_idr_lock);
4190 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4191 link = idr_find(&link_idr, id);
4192 if (link) {
4193 if (link->id)
4194 link = bpf_link_inc_not_zero(link);
4195 else
4196 link = ERR_PTR(-EAGAIN);
4197 } else {
4198 link = ERR_PTR(-ENOENT);
4199 }
4200 spin_unlock_bh(&link_idr_lock);
4201 return link;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004202}
4203
4204#define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4205
4206static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4207{
4208 struct bpf_link *link;
4209 u32 id = attr->link_id;
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004210 int fd;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004211
4212 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4213 return -EINVAL;
4214
4215 if (!capable(CAP_SYS_ADMIN))
4216 return -EPERM;
4217
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004218 link = bpf_link_by_id(id);
4219 if (IS_ERR(link))
4220 return PTR_ERR(link);
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004221
4222 fd = bpf_link_new_fd(link);
4223 if (fd < 0)
4224 bpf_link_put(link);
4225
4226 return fd;
4227}
4228
Song Liud46edd62020-04-30 00:15:04 -07004229DEFINE_MUTEX(bpf_stats_enabled_mutex);
4230
4231static int bpf_stats_release(struct inode *inode, struct file *file)
4232{
4233 mutex_lock(&bpf_stats_enabled_mutex);
4234 static_key_slow_dec(&bpf_stats_enabled_key.key);
4235 mutex_unlock(&bpf_stats_enabled_mutex);
4236 return 0;
4237}
4238
4239static const struct file_operations bpf_stats_fops = {
4240 .release = bpf_stats_release,
4241};
4242
4243static int bpf_enable_runtime_stats(void)
4244{
4245 int fd;
4246
4247 mutex_lock(&bpf_stats_enabled_mutex);
4248
4249 /* Set a very high limit to avoid overflow */
4250 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4251 mutex_unlock(&bpf_stats_enabled_mutex);
4252 return -EBUSY;
4253 }
4254
4255 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4256 if (fd >= 0)
4257 static_key_slow_inc(&bpf_stats_enabled_key.key);
4258
4259 mutex_unlock(&bpf_stats_enabled_mutex);
4260 return fd;
4261}
4262
4263#define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4264
4265static int bpf_enable_stats(union bpf_attr *attr)
4266{
4267
4268 if (CHECK_ATTR(BPF_ENABLE_STATS))
4269 return -EINVAL;
4270
4271 if (!capable(CAP_SYS_ADMIN))
4272 return -EPERM;
4273
4274 switch (attr->enable_stats.type) {
4275 case BPF_STATS_RUN_TIME:
4276 return bpf_enable_runtime_stats();
4277 default:
4278 break;
4279 }
4280 return -EINVAL;
4281}
4282
Yonghong Songac51d992020-05-09 10:59:05 -07004283#define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4284
4285static int bpf_iter_create(union bpf_attr *attr)
4286{
4287 struct bpf_link *link;
4288 int err;
4289
4290 if (CHECK_ATTR(BPF_ITER_CREATE))
4291 return -EINVAL;
4292
4293 if (attr->iter_create.flags)
4294 return -EINVAL;
4295
4296 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4297 if (IS_ERR(link))
4298 return PTR_ERR(link);
4299
4300 err = bpf_iter_new_fd(link);
4301 bpf_link_put(link);
4302
4303 return err;
4304}
4305
YiFei Zhuef153142020-09-15 16:45:40 -07004306#define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4307
4308static int bpf_prog_bind_map(union bpf_attr *attr)
4309{
4310 struct bpf_prog *prog;
4311 struct bpf_map *map;
4312 struct bpf_map **used_maps_old, **used_maps_new;
4313 int i, ret = 0;
4314
4315 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4316 return -EINVAL;
4317
4318 if (attr->prog_bind_map.flags)
4319 return -EINVAL;
4320
4321 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4322 if (IS_ERR(prog))
4323 return PTR_ERR(prog);
4324
4325 map = bpf_map_get(attr->prog_bind_map.map_fd);
4326 if (IS_ERR(map)) {
4327 ret = PTR_ERR(map);
4328 goto out_prog_put;
4329 }
4330
4331 mutex_lock(&prog->aux->used_maps_mutex);
4332
4333 used_maps_old = prog->aux->used_maps;
4334
4335 for (i = 0; i < prog->aux->used_map_cnt; i++)
Stanislav Fomichev1028ae42020-10-02 17:25:44 -07004336 if (used_maps_old[i] == map) {
4337 bpf_map_put(map);
YiFei Zhuef153142020-09-15 16:45:40 -07004338 goto out_unlock;
Stanislav Fomichev1028ae42020-10-02 17:25:44 -07004339 }
YiFei Zhuef153142020-09-15 16:45:40 -07004340
4341 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4342 sizeof(used_maps_new[0]),
4343 GFP_KERNEL);
4344 if (!used_maps_new) {
4345 ret = -ENOMEM;
4346 goto out_unlock;
4347 }
4348
4349 memcpy(used_maps_new, used_maps_old,
4350 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4351 used_maps_new[prog->aux->used_map_cnt] = map;
4352
4353 prog->aux->used_map_cnt++;
4354 prog->aux->used_maps = used_maps_new;
4355
4356 kfree(used_maps_old);
4357
4358out_unlock:
4359 mutex_unlock(&prog->aux->used_maps_mutex);
4360
4361 if (ret)
4362 bpf_map_put(map);
4363out_prog_put:
4364 bpf_prog_put(prog);
4365 return ret;
4366}
4367
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004368SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4369{
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01004370 union bpf_attr attr;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004371 int err;
4372
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07004373 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004374 return -EPERM;
4375
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07004376 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004377 if (err)
4378 return err;
4379 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004380
4381 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01004382 memset(&attr, 0, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004383 if (copy_from_user(&attr, uattr, size) != 0)
4384 return -EFAULT;
4385
Chenbo Fengafdb09c2017-10-18 13:00:24 -07004386 err = security_bpf(cmd, &attr, size);
4387 if (err < 0)
4388 return err;
4389
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004390 switch (cmd) {
4391 case BPF_MAP_CREATE:
4392 err = map_create(&attr);
4393 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07004394 case BPF_MAP_LOOKUP_ELEM:
4395 err = map_lookup_elem(&attr);
4396 break;
4397 case BPF_MAP_UPDATE_ELEM:
4398 err = map_update_elem(&attr);
4399 break;
4400 case BPF_MAP_DELETE_ELEM:
4401 err = map_delete_elem(&attr);
4402 break;
4403 case BPF_MAP_GET_NEXT_KEY:
4404 err = map_get_next_key(&attr);
4405 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02004406 case BPF_MAP_FREEZE:
4407 err = map_freeze(&attr);
4408 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07004409 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08004410 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07004411 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01004412 case BPF_OBJ_PIN:
4413 err = bpf_obj_pin(&attr);
4414 break;
4415 case BPF_OBJ_GET:
4416 err = bpf_obj_get(&attr);
4417 break;
Daniel Mackf4324552016-11-23 16:52:27 +01004418 case BPF_PROG_ATTACH:
4419 err = bpf_prog_attach(&attr);
4420 break;
4421 case BPF_PROG_DETACH:
4422 err = bpf_prog_detach(&attr);
4423 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07004424 case BPF_PROG_QUERY:
4425 err = bpf_prog_query(&attr, uattr);
4426 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07004427 case BPF_PROG_TEST_RUN:
4428 err = bpf_prog_test_run(&attr, uattr);
4429 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004430 case BPF_PROG_GET_NEXT_ID:
4431 err = bpf_obj_get_next_id(&attr, uattr,
4432 &prog_idr, &prog_idr_lock);
4433 break;
4434 case BPF_MAP_GET_NEXT_ID:
4435 err = bpf_obj_get_next_id(&attr, uattr,
4436 &map_idr, &map_idr_lock);
4437 break;
Quentin Monnet1b9ed842019-08-20 10:31:50 +01004438 case BPF_BTF_GET_NEXT_ID:
4439 err = bpf_obj_get_next_id(&attr, uattr,
4440 &btf_idr, &btf_idr_lock);
4441 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07004442 case BPF_PROG_GET_FD_BY_ID:
4443 err = bpf_prog_get_fd_by_id(&attr);
4444 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07004445 case BPF_MAP_GET_FD_BY_ID:
4446 err = bpf_map_get_fd_by_id(&attr);
4447 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004448 case BPF_OBJ_GET_INFO_BY_FD:
4449 err = bpf_obj_get_info_by_fd(&attr, uattr);
4450 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07004451 case BPF_RAW_TRACEPOINT_OPEN:
4452 err = bpf_raw_tracepoint_open(&attr);
4453 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004454 case BPF_BTF_LOAD:
4455 err = bpf_btf_load(&attr);
4456 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07004457 case BPF_BTF_GET_FD_BY_ID:
4458 err = bpf_btf_get_fd_by_id(&attr);
4459 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004460 case BPF_TASK_FD_QUERY:
4461 err = bpf_task_fd_query(&attr, uattr);
4462 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02004463 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4464 err = map_lookup_and_delete_elem(&attr);
4465 break;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004466 case BPF_MAP_LOOKUP_BATCH:
4467 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
4468 break;
Yonghong Song05799632020-01-15 10:43:04 -08004469 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4470 err = bpf_map_do_batch(&attr, uattr,
4471 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4472 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004473 case BPF_MAP_UPDATE_BATCH:
4474 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
4475 break;
4476 case BPF_MAP_DELETE_BATCH:
4477 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);
4478 break;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004479 case BPF_LINK_CREATE:
4480 err = link_create(&attr);
4481 break;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004482 case BPF_LINK_UPDATE:
4483 err = link_update(&attr);
4484 break;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004485 case BPF_LINK_GET_FD_BY_ID:
4486 err = bpf_link_get_fd_by_id(&attr);
4487 break;
4488 case BPF_LINK_GET_NEXT_ID:
4489 err = bpf_obj_get_next_id(&attr, uattr,
4490 &link_idr, &link_idr_lock);
4491 break;
Song Liud46edd62020-04-30 00:15:04 -07004492 case BPF_ENABLE_STATS:
4493 err = bpf_enable_stats(&attr);
4494 break;
Yonghong Songac51d992020-05-09 10:59:05 -07004495 case BPF_ITER_CREATE:
4496 err = bpf_iter_create(&attr);
4497 break;
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07004498 case BPF_LINK_DETACH:
4499 err = link_detach(&attr);
4500 break;
YiFei Zhuef153142020-09-15 16:45:40 -07004501 case BPF_PROG_BIND_MAP:
4502 err = bpf_prog_bind_map(&attr);
4503 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004504 default:
4505 err = -EINVAL;
4506 break;
4507 }
4508
4509 return err;
4510}