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