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 cond_resched();
1312 }
1313 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1314 err = -EFAULT;
1315
1316 kfree(key);
1317 return err;
1318 }
1319
generic_map_update_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)1320 int generic_map_update_batch(struct bpf_map *map,
1321 const union bpf_attr *attr,
1322 union bpf_attr __user *uattr)
1323 {
1324 void __user *values = u64_to_user_ptr(attr->batch.values);
1325 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1326 u32 value_size, cp, max_count;
1327 int ufd = attr->batch.map_fd;
1328 void *key, *value;
1329 struct fd f;
1330 int err = 0;
1331
1332 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1333 return -EINVAL;
1334
1335 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1336 !map_value_has_spin_lock(map)) {
1337 return -EINVAL;
1338 }
1339
1340 value_size = bpf_map_value_size(map);
1341
1342 max_count = attr->batch.count;
1343 if (!max_count)
1344 return 0;
1345
1346 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1347 if (!key)
1348 return -ENOMEM;
1349
1350 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1351 if (!value) {
1352 kfree(key);
1353 return -ENOMEM;
1354 }
1355
1356 f = fdget(ufd); /* bpf_map_do_batch() guarantees ufd is valid */
1357 for (cp = 0; cp < max_count; cp++) {
1358 err = -EFAULT;
1359 if (copy_from_user(key, keys + cp * map->key_size,
1360 map->key_size) ||
1361 copy_from_user(value, values + cp * value_size, value_size))
1362 break;
1363
1364 err = bpf_map_update_value(map, f, key, value,
1365 attr->batch.elem_flags);
1366
1367 if (err)
1368 break;
1369 cond_resched();
1370 }
1371
1372 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1373 err = -EFAULT;
1374
1375 kfree(value);
1376 kfree(key);
1377 fdput(f);
1378 return err;
1379 }
1380
1381 #define MAP_LOOKUP_RETRIES 3
1382
generic_map_lookup_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)1383 int generic_map_lookup_batch(struct bpf_map *map,
1384 const union bpf_attr *attr,
1385 union bpf_attr __user *uattr)
1386 {
1387 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1388 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1389 void __user *values = u64_to_user_ptr(attr->batch.values);
1390 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1391 void *buf, *buf_prevkey, *prev_key, *key, *value;
1392 int err, retry = MAP_LOOKUP_RETRIES;
1393 u32 value_size, cp, max_count;
1394
1395 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1396 return -EINVAL;
1397
1398 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1399 !map_value_has_spin_lock(map))
1400 return -EINVAL;
1401
1402 value_size = bpf_map_value_size(map);
1403
1404 max_count = attr->batch.count;
1405 if (!max_count)
1406 return 0;
1407
1408 if (put_user(0, &uattr->batch.count))
1409 return -EFAULT;
1410
1411 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1412 if (!buf_prevkey)
1413 return -ENOMEM;
1414
1415 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1416 if (!buf) {
1417 kfree(buf_prevkey);
1418 return -ENOMEM;
1419 }
1420
1421 err = -EFAULT;
1422 prev_key = NULL;
1423 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1424 goto free_buf;
1425 key = buf;
1426 value = key + map->key_size;
1427 if (ubatch)
1428 prev_key = buf_prevkey;
1429
1430 for (cp = 0; cp < max_count;) {
1431 rcu_read_lock();
1432 err = map->ops->map_get_next_key(map, prev_key, key);
1433 rcu_read_unlock();
1434 if (err)
1435 break;
1436 err = bpf_map_copy_value(map, key, value,
1437 attr->batch.elem_flags);
1438
1439 if (err == -ENOENT) {
1440 if (retry) {
1441 retry--;
1442 continue;
1443 }
1444 err = -EINTR;
1445 break;
1446 }
1447
1448 if (err)
1449 goto free_buf;
1450
1451 if (copy_to_user(keys + cp * map->key_size, key,
1452 map->key_size)) {
1453 err = -EFAULT;
1454 goto free_buf;
1455 }
1456 if (copy_to_user(values + cp * value_size, value, value_size)) {
1457 err = -EFAULT;
1458 goto free_buf;
1459 }
1460
1461 if (!prev_key)
1462 prev_key = buf_prevkey;
1463
1464 swap(prev_key, key);
1465 retry = MAP_LOOKUP_RETRIES;
1466 cp++;
1467 cond_resched();
1468 }
1469
1470 if (err == -EFAULT)
1471 goto free_buf;
1472
1473 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1474 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1475 err = -EFAULT;
1476
1477 free_buf:
1478 kfree(buf_prevkey);
1479 kfree(buf);
1480 return err;
1481 }
1482
1483 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1484
map_lookup_and_delete_elem(union bpf_attr * attr)1485 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1486 {
1487 void __user *ukey = u64_to_user_ptr(attr->key);
1488 void __user *uvalue = u64_to_user_ptr(attr->value);
1489 int ufd = attr->map_fd;
1490 struct bpf_map *map;
1491 void *key, *value;
1492 u32 value_size;
1493 struct fd f;
1494 int err;
1495
1496 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1497 return -EINVAL;
1498
1499 f = fdget(ufd);
1500 map = __bpf_map_get(f);
1501 if (IS_ERR(map))
1502 return PTR_ERR(map);
1503 bpf_map_write_active_inc(map);
1504 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1505 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1506 err = -EPERM;
1507 goto err_put;
1508 }
1509
1510 key = __bpf_copy_key(ukey, map->key_size);
1511 if (IS_ERR(key)) {
1512 err = PTR_ERR(key);
1513 goto err_put;
1514 }
1515
1516 value_size = map->value_size;
1517
1518 err = -ENOMEM;
1519 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1520 if (!value)
1521 goto free_key;
1522
1523 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1524 map->map_type == BPF_MAP_TYPE_STACK) {
1525 err = map->ops->map_pop_elem(map, value);
1526 } else {
1527 err = -ENOTSUPP;
1528 }
1529
1530 if (err)
1531 goto free_value;
1532
1533 if (copy_to_user(uvalue, value, value_size) != 0) {
1534 err = -EFAULT;
1535 goto free_value;
1536 }
1537
1538 err = 0;
1539
1540 free_value:
1541 kfree(value);
1542 free_key:
1543 kfree(key);
1544 err_put:
1545 bpf_map_write_active_dec(map);
1546 fdput(f);
1547 return err;
1548 }
1549
1550 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1551
map_freeze(const union bpf_attr * attr)1552 static int map_freeze(const union bpf_attr *attr)
1553 {
1554 int err = 0, ufd = attr->map_fd;
1555 struct bpf_map *map;
1556 struct fd f;
1557
1558 if (CHECK_ATTR(BPF_MAP_FREEZE))
1559 return -EINVAL;
1560
1561 f = fdget(ufd);
1562 map = __bpf_map_get(f);
1563 if (IS_ERR(map))
1564 return PTR_ERR(map);
1565
1566 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1567 fdput(f);
1568 return -ENOTSUPP;
1569 }
1570
1571 mutex_lock(&map->freeze_mutex);
1572 if (bpf_map_write_active(map)) {
1573 err = -EBUSY;
1574 goto err_put;
1575 }
1576 if (READ_ONCE(map->frozen)) {
1577 err = -EBUSY;
1578 goto err_put;
1579 }
1580 if (!bpf_capable()) {
1581 err = -EPERM;
1582 goto err_put;
1583 }
1584
1585 WRITE_ONCE(map->frozen, true);
1586 err_put:
1587 mutex_unlock(&map->freeze_mutex);
1588 fdput(f);
1589 return err;
1590 }
1591
1592 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1593 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1594 [_id] = & _name ## _prog_ops,
1595 #define BPF_MAP_TYPE(_id, _ops)
1596 #define BPF_LINK_TYPE(_id, _name)
1597 #include <linux/bpf_types.h>
1598 #undef BPF_PROG_TYPE
1599 #undef BPF_MAP_TYPE
1600 #undef BPF_LINK_TYPE
1601 };
1602
find_prog_type(enum bpf_prog_type type,struct bpf_prog * prog)1603 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1604 {
1605 const struct bpf_prog_ops *ops;
1606
1607 if (type >= ARRAY_SIZE(bpf_prog_types))
1608 return -EINVAL;
1609 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1610 ops = bpf_prog_types[type];
1611 if (!ops)
1612 return -EINVAL;
1613
1614 if (!bpf_prog_is_dev_bound(prog->aux))
1615 prog->aux->ops = ops;
1616 else
1617 prog->aux->ops = &bpf_offload_prog_ops;
1618 prog->type = type;
1619 return 0;
1620 }
1621
1622 enum bpf_audit {
1623 BPF_AUDIT_LOAD,
1624 BPF_AUDIT_UNLOAD,
1625 BPF_AUDIT_MAX,
1626 };
1627
1628 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1629 [BPF_AUDIT_LOAD] = "LOAD",
1630 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1631 };
1632
bpf_audit_prog(const struct bpf_prog * prog,unsigned int op)1633 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1634 {
1635 struct audit_context *ctx = NULL;
1636 struct audit_buffer *ab;
1637
1638 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1639 return;
1640 if (audit_enabled == AUDIT_OFF)
1641 return;
1642 if (op == BPF_AUDIT_LOAD)
1643 ctx = audit_context();
1644 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1645 if (unlikely(!ab))
1646 return;
1647 audit_log_format(ab, "prog-id=%u op=%s",
1648 prog->aux->id, bpf_audit_str[op]);
1649 audit_log_end(ab);
1650 }
1651
__bpf_prog_charge(struct user_struct * user,u32 pages)1652 int __bpf_prog_charge(struct user_struct *user, u32 pages)
1653 {
1654 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1655 unsigned long user_bufs;
1656
1657 if (user) {
1658 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1659 if (user_bufs > memlock_limit) {
1660 atomic_long_sub(pages, &user->locked_vm);
1661 return -EPERM;
1662 }
1663 }
1664
1665 return 0;
1666 }
1667
__bpf_prog_uncharge(struct user_struct * user,u32 pages)1668 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1669 {
1670 if (user)
1671 atomic_long_sub(pages, &user->locked_vm);
1672 }
1673
bpf_prog_charge_memlock(struct bpf_prog * prog)1674 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1675 {
1676 struct user_struct *user = get_current_user();
1677 int ret;
1678
1679 ret = __bpf_prog_charge(user, prog->pages);
1680 if (ret) {
1681 free_uid(user);
1682 return ret;
1683 }
1684
1685 prog->aux->user = user;
1686 return 0;
1687 }
1688
bpf_prog_uncharge_memlock(struct bpf_prog * prog)1689 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1690 {
1691 struct user_struct *user = prog->aux->user;
1692
1693 __bpf_prog_uncharge(user, prog->pages);
1694 free_uid(user);
1695 }
1696
bpf_prog_alloc_id(struct bpf_prog * prog)1697 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1698 {
1699 int id;
1700
1701 idr_preload(GFP_KERNEL);
1702 spin_lock_bh(&prog_idr_lock);
1703 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1704 if (id > 0)
1705 prog->aux->id = id;
1706 spin_unlock_bh(&prog_idr_lock);
1707 idr_preload_end();
1708
1709 /* id is in [1, INT_MAX) */
1710 if (WARN_ON_ONCE(!id))
1711 return -ENOSPC;
1712
1713 return id > 0 ? 0 : id;
1714 }
1715
bpf_prog_free_id(struct bpf_prog * prog,bool do_idr_lock)1716 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1717 {
1718 /* cBPF to eBPF migrations are currently not in the idr store.
1719 * Offloaded programs are removed from the store when their device
1720 * disappears - even if someone grabs an fd to them they are unusable,
1721 * simply waiting for refcnt to drop to be freed.
1722 */
1723 if (!prog->aux->id)
1724 return;
1725
1726 if (do_idr_lock)
1727 spin_lock_bh(&prog_idr_lock);
1728 else
1729 __acquire(&prog_idr_lock);
1730
1731 idr_remove(&prog_idr, prog->aux->id);
1732 prog->aux->id = 0;
1733
1734 if (do_idr_lock)
1735 spin_unlock_bh(&prog_idr_lock);
1736 else
1737 __release(&prog_idr_lock);
1738 }
1739
__bpf_prog_put_rcu(struct rcu_head * rcu)1740 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1741 {
1742 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1743
1744 kvfree(aux->func_info);
1745 kfree(aux->func_info_aux);
1746 bpf_prog_uncharge_memlock(aux->prog);
1747 security_bpf_prog_free(aux);
1748 bpf_prog_free(aux->prog);
1749 }
1750
__bpf_prog_put_noref(struct bpf_prog * prog,bool deferred)1751 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1752 {
1753 bpf_prog_kallsyms_del_all(prog);
1754 btf_put(prog->aux->btf);
1755 bpf_prog_free_linfo(prog);
1756
1757 if (deferred) {
1758 if (prog->aux->sleepable)
1759 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1760 else
1761 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1762 } else {
1763 __bpf_prog_put_rcu(&prog->aux->rcu);
1764 }
1765 }
1766
__bpf_prog_put(struct bpf_prog * prog,bool do_idr_lock)1767 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1768 {
1769 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
1770 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1771 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1772 /* bpf_prog_free_id() must be called first */
1773 bpf_prog_free_id(prog, do_idr_lock);
1774 __bpf_prog_put_noref(prog, true);
1775 }
1776 }
1777
bpf_prog_put(struct bpf_prog * prog)1778 void bpf_prog_put(struct bpf_prog *prog)
1779 {
1780 __bpf_prog_put(prog, true);
1781 }
1782 EXPORT_SYMBOL_GPL(bpf_prog_put);
1783
bpf_prog_release(struct inode * inode,struct file * filp)1784 static int bpf_prog_release(struct inode *inode, struct file *filp)
1785 {
1786 struct bpf_prog *prog = filp->private_data;
1787
1788 bpf_prog_put(prog);
1789 return 0;
1790 }
1791
bpf_prog_get_stats(const struct bpf_prog * prog,struct bpf_prog_stats * stats)1792 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1793 struct bpf_prog_stats *stats)
1794 {
1795 u64 nsecs = 0, cnt = 0;
1796 int cpu;
1797
1798 for_each_possible_cpu(cpu) {
1799 const struct bpf_prog_stats *st;
1800 unsigned int start;
1801 u64 tnsecs, tcnt;
1802
1803 st = per_cpu_ptr(prog->aux->stats, cpu);
1804 do {
1805 start = u64_stats_fetch_begin_irq(&st->syncp);
1806 tnsecs = st->nsecs;
1807 tcnt = st->cnt;
1808 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1809 nsecs += tnsecs;
1810 cnt += tcnt;
1811 }
1812 stats->nsecs = nsecs;
1813 stats->cnt = cnt;
1814 }
1815
1816 #ifdef CONFIG_PROC_FS
bpf_prog_show_fdinfo(struct seq_file * m,struct file * filp)1817 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1818 {
1819 const struct bpf_prog *prog = filp->private_data;
1820 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1821 struct bpf_prog_stats stats;
1822
1823 bpf_prog_get_stats(prog, &stats);
1824 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1825 seq_printf(m,
1826 "prog_type:\t%u\n"
1827 "prog_jited:\t%u\n"
1828 "prog_tag:\t%s\n"
1829 "memlock:\t%llu\n"
1830 "prog_id:\t%u\n"
1831 "run_time_ns:\t%llu\n"
1832 "run_cnt:\t%llu\n",
1833 prog->type,
1834 prog->jited,
1835 prog_tag,
1836 prog->pages * 1ULL << PAGE_SHIFT,
1837 prog->aux->id,
1838 stats.nsecs,
1839 stats.cnt);
1840 }
1841 #endif
1842
1843 const struct file_operations bpf_prog_fops = {
1844 #ifdef CONFIG_PROC_FS
1845 .show_fdinfo = bpf_prog_show_fdinfo,
1846 #endif
1847 .release = bpf_prog_release,
1848 .read = bpf_dummy_read,
1849 .write = bpf_dummy_write,
1850 };
1851
bpf_prog_new_fd(struct bpf_prog * prog)1852 int bpf_prog_new_fd(struct bpf_prog *prog)
1853 {
1854 int ret;
1855
1856 ret = security_bpf_prog(prog);
1857 if (ret < 0)
1858 return ret;
1859
1860 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1861 O_RDWR | O_CLOEXEC);
1862 }
1863
____bpf_prog_get(struct fd f)1864 static struct bpf_prog *____bpf_prog_get(struct fd f)
1865 {
1866 if (!f.file)
1867 return ERR_PTR(-EBADF);
1868 if (f.file->f_op != &bpf_prog_fops) {
1869 fdput(f);
1870 return ERR_PTR(-EINVAL);
1871 }
1872
1873 return f.file->private_data;
1874 }
1875
bpf_prog_add(struct bpf_prog * prog,int i)1876 void bpf_prog_add(struct bpf_prog *prog, int i)
1877 {
1878 atomic64_add(i, &prog->aux->refcnt);
1879 }
1880 EXPORT_SYMBOL_GPL(bpf_prog_add);
1881
bpf_prog_sub(struct bpf_prog * prog,int i)1882 void bpf_prog_sub(struct bpf_prog *prog, int i)
1883 {
1884 /* Only to be used for undoing previous bpf_prog_add() in some
1885 * error path. We still know that another entity in our call
1886 * path holds a reference to the program, thus atomic_sub() can
1887 * be safely used in such cases!
1888 */
1889 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1890 }
1891 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1892
bpf_prog_inc(struct bpf_prog * prog)1893 void bpf_prog_inc(struct bpf_prog *prog)
1894 {
1895 atomic64_inc(&prog->aux->refcnt);
1896 }
1897 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1898
1899 /* prog_idr_lock should have been held */
bpf_prog_inc_not_zero(struct bpf_prog * prog)1900 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1901 {
1902 int refold;
1903
1904 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1905
1906 if (!refold)
1907 return ERR_PTR(-ENOENT);
1908
1909 return prog;
1910 }
1911 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1912
bpf_prog_get_ok(struct bpf_prog * prog,enum bpf_prog_type * attach_type,bool attach_drv)1913 bool bpf_prog_get_ok(struct bpf_prog *prog,
1914 enum bpf_prog_type *attach_type, bool attach_drv)
1915 {
1916 /* not an attachment, just a refcount inc, always allow */
1917 if (!attach_type)
1918 return true;
1919
1920 if (prog->type != *attach_type)
1921 return false;
1922 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1923 return false;
1924
1925 return true;
1926 }
1927
__bpf_prog_get(u32 ufd,enum bpf_prog_type * attach_type,bool attach_drv)1928 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1929 bool attach_drv)
1930 {
1931 struct fd f = fdget(ufd);
1932 struct bpf_prog *prog;
1933
1934 prog = ____bpf_prog_get(f);
1935 if (IS_ERR(prog))
1936 return prog;
1937 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1938 prog = ERR_PTR(-EINVAL);
1939 goto out;
1940 }
1941
1942 bpf_prog_inc(prog);
1943 out:
1944 fdput(f);
1945 return prog;
1946 }
1947
bpf_prog_get(u32 ufd)1948 struct bpf_prog *bpf_prog_get(u32 ufd)
1949 {
1950 return __bpf_prog_get(ufd, NULL, false);
1951 }
1952
bpf_prog_get_type_dev(u32 ufd,enum bpf_prog_type type,bool attach_drv)1953 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1954 bool attach_drv)
1955 {
1956 return __bpf_prog_get(ufd, &type, attach_drv);
1957 }
1958 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1959
1960 /* Initially all BPF programs could be loaded w/o specifying
1961 * expected_attach_type. Later for some of them specifying expected_attach_type
1962 * at load time became required so that program could be validated properly.
1963 * Programs of types that are allowed to be loaded both w/ and w/o (for
1964 * backward compatibility) expected_attach_type, should have the default attach
1965 * type assigned to expected_attach_type for the latter case, so that it can be
1966 * validated later at attach time.
1967 *
1968 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1969 * prog type requires it but has some attach types that have to be backward
1970 * compatible.
1971 */
bpf_prog_load_fixup_attach_type(union bpf_attr * attr)1972 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1973 {
1974 switch (attr->prog_type) {
1975 case BPF_PROG_TYPE_CGROUP_SOCK:
1976 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1977 * exist so checking for non-zero is the way to go here.
1978 */
1979 if (!attr->expected_attach_type)
1980 attr->expected_attach_type =
1981 BPF_CGROUP_INET_SOCK_CREATE;
1982 break;
1983 }
1984 }
1985
1986 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)1987 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1988 enum bpf_attach_type expected_attach_type,
1989 u32 btf_id, u32 prog_fd)
1990 {
1991 if (btf_id) {
1992 if (btf_id > BTF_MAX_TYPE)
1993 return -EINVAL;
1994
1995 switch (prog_type) {
1996 case BPF_PROG_TYPE_TRACING:
1997 case BPF_PROG_TYPE_LSM:
1998 case BPF_PROG_TYPE_STRUCT_OPS:
1999 case BPF_PROG_TYPE_EXT:
2000 break;
2001 default:
2002 return -EINVAL;
2003 }
2004 }
2005
2006 if (prog_fd && prog_type != BPF_PROG_TYPE_TRACING &&
2007 prog_type != BPF_PROG_TYPE_EXT)
2008 return -EINVAL;
2009
2010 switch (prog_type) {
2011 case BPF_PROG_TYPE_CGROUP_SOCK:
2012 switch (expected_attach_type) {
2013 case BPF_CGROUP_INET_SOCK_CREATE:
2014 case BPF_CGROUP_INET_SOCK_RELEASE:
2015 case BPF_CGROUP_INET4_POST_BIND:
2016 case BPF_CGROUP_INET6_POST_BIND:
2017 return 0;
2018 default:
2019 return -EINVAL;
2020 }
2021 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2022 switch (expected_attach_type) {
2023 case BPF_CGROUP_INET4_BIND:
2024 case BPF_CGROUP_INET6_BIND:
2025 case BPF_CGROUP_INET4_CONNECT:
2026 case BPF_CGROUP_INET6_CONNECT:
2027 case BPF_CGROUP_INET4_GETPEERNAME:
2028 case BPF_CGROUP_INET6_GETPEERNAME:
2029 case BPF_CGROUP_INET4_GETSOCKNAME:
2030 case BPF_CGROUP_INET6_GETSOCKNAME:
2031 case BPF_CGROUP_UDP4_SENDMSG:
2032 case BPF_CGROUP_UDP6_SENDMSG:
2033 case BPF_CGROUP_UDP4_RECVMSG:
2034 case BPF_CGROUP_UDP6_RECVMSG:
2035 return 0;
2036 default:
2037 return -EINVAL;
2038 }
2039 case BPF_PROG_TYPE_CGROUP_SKB:
2040 switch (expected_attach_type) {
2041 case BPF_CGROUP_INET_INGRESS:
2042 case BPF_CGROUP_INET_EGRESS:
2043 return 0;
2044 default:
2045 return -EINVAL;
2046 }
2047 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2048 switch (expected_attach_type) {
2049 case BPF_CGROUP_SETSOCKOPT:
2050 case BPF_CGROUP_GETSOCKOPT:
2051 return 0;
2052 default:
2053 return -EINVAL;
2054 }
2055 case BPF_PROG_TYPE_SK_LOOKUP:
2056 if (expected_attach_type == BPF_SK_LOOKUP)
2057 return 0;
2058 return -EINVAL;
2059 case BPF_PROG_TYPE_EXT:
2060 if (expected_attach_type)
2061 return -EINVAL;
2062 fallthrough;
2063 default:
2064 return 0;
2065 }
2066 }
2067
is_net_admin_prog_type(enum bpf_prog_type prog_type)2068 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2069 {
2070 switch (prog_type) {
2071 case BPF_PROG_TYPE_SCHED_CLS:
2072 case BPF_PROG_TYPE_SCHED_ACT:
2073 case BPF_PROG_TYPE_XDP:
2074 case BPF_PROG_TYPE_LWT_IN:
2075 case BPF_PROG_TYPE_LWT_OUT:
2076 case BPF_PROG_TYPE_LWT_XMIT:
2077 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2078 case BPF_PROG_TYPE_SK_SKB:
2079 case BPF_PROG_TYPE_SK_MSG:
2080 case BPF_PROG_TYPE_LIRC_MODE2:
2081 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2082 case BPF_PROG_TYPE_CGROUP_DEVICE:
2083 case BPF_PROG_TYPE_CGROUP_SOCK:
2084 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2085 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2086 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2087 case BPF_PROG_TYPE_SOCK_OPS:
2088 case BPF_PROG_TYPE_EXT: /* extends any prog */
2089 return true;
2090 case BPF_PROG_TYPE_CGROUP_SKB:
2091 /* always unpriv */
2092 case BPF_PROG_TYPE_SK_REUSEPORT:
2093 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2094 default:
2095 return false;
2096 }
2097 }
2098
is_perfmon_prog_type(enum bpf_prog_type prog_type)2099 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2100 {
2101 switch (prog_type) {
2102 case BPF_PROG_TYPE_KPROBE:
2103 case BPF_PROG_TYPE_TRACEPOINT:
2104 case BPF_PROG_TYPE_PERF_EVENT:
2105 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2106 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2107 case BPF_PROG_TYPE_TRACING:
2108 case BPF_PROG_TYPE_LSM:
2109 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2110 case BPF_PROG_TYPE_EXT: /* extends any prog */
2111 return true;
2112 default:
2113 return false;
2114 }
2115 }
2116
2117 /* last field in 'union bpf_attr' used by this command */
2118 #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
2119
bpf_prog_load(union bpf_attr * attr,union bpf_attr __user * uattr)2120 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
2121 {
2122 enum bpf_prog_type type = attr->prog_type;
2123 struct bpf_prog *prog;
2124 int err;
2125 char license[128];
2126 bool is_gpl;
2127
2128 if (CHECK_ATTR(BPF_PROG_LOAD))
2129 return -EINVAL;
2130
2131 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2132 BPF_F_ANY_ALIGNMENT |
2133 BPF_F_TEST_STATE_FREQ |
2134 BPF_F_SLEEPABLE |
2135 BPF_F_TEST_RND_HI32))
2136 return -EINVAL;
2137
2138 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2139 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2140 !bpf_capable())
2141 return -EPERM;
2142
2143 /* copy eBPF program license from user space */
2144 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
2145 sizeof(license) - 1) < 0)
2146 return -EFAULT;
2147 license[sizeof(license) - 1] = 0;
2148
2149 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2150 is_gpl = license_is_gpl_compatible(license);
2151
2152 if (attr->insn_cnt == 0 ||
2153 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2154 return -E2BIG;
2155 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2156 type != BPF_PROG_TYPE_CGROUP_SKB &&
2157 !bpf_capable())
2158 return -EPERM;
2159
2160 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2161 return -EPERM;
2162 if (is_perfmon_prog_type(type) && !perfmon_capable())
2163 return -EPERM;
2164
2165 bpf_prog_load_fixup_attach_type(attr);
2166 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2167 attr->attach_btf_id,
2168 attr->attach_prog_fd))
2169 return -EINVAL;
2170
2171 /* plain bpf_prog allocation */
2172 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2173 if (!prog)
2174 return -ENOMEM;
2175
2176 prog->expected_attach_type = attr->expected_attach_type;
2177 prog->aux->attach_btf_id = attr->attach_btf_id;
2178 if (attr->attach_prog_fd) {
2179 struct bpf_prog *dst_prog;
2180
2181 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2182 if (IS_ERR(dst_prog)) {
2183 err = PTR_ERR(dst_prog);
2184 goto free_prog_nouncharge;
2185 }
2186 prog->aux->dst_prog = dst_prog;
2187 }
2188
2189 prog->aux->offload_requested = !!attr->prog_ifindex;
2190 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2191
2192 err = security_bpf_prog_alloc(prog->aux);
2193 if (err)
2194 goto free_prog_nouncharge;
2195
2196 err = bpf_prog_charge_memlock(prog);
2197 if (err)
2198 goto free_prog_sec;
2199
2200 prog->len = attr->insn_cnt;
2201
2202 err = -EFAULT;
2203 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
2204 bpf_prog_insn_size(prog)) != 0)
2205 goto free_prog;
2206
2207 prog->orig_prog = NULL;
2208 prog->jited = 0;
2209
2210 atomic64_set(&prog->aux->refcnt, 1);
2211 prog->gpl_compatible = is_gpl ? 1 : 0;
2212
2213 if (bpf_prog_is_dev_bound(prog->aux)) {
2214 err = bpf_prog_offload_init(prog, attr);
2215 if (err)
2216 goto free_prog;
2217 }
2218
2219 /* find program type: socket_filter vs tracing_filter */
2220 err = find_prog_type(type, prog);
2221 if (err < 0)
2222 goto free_prog;
2223
2224 prog->aux->load_time = ktime_get_boottime_ns();
2225 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2226 sizeof(attr->prog_name));
2227 if (err < 0)
2228 goto free_prog;
2229
2230 /* run eBPF verifier */
2231 err = bpf_check(&prog, attr, uattr);
2232 if (err < 0)
2233 goto free_used_maps;
2234
2235 prog = bpf_prog_select_runtime(prog, &err);
2236 if (err < 0)
2237 goto free_used_maps;
2238
2239 err = bpf_prog_alloc_id(prog);
2240 if (err)
2241 goto free_used_maps;
2242
2243 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2244 * effectively publicly exposed. However, retrieving via
2245 * bpf_prog_get_fd_by_id() will take another reference,
2246 * therefore it cannot be gone underneath us.
2247 *
2248 * Only for the time /after/ successful bpf_prog_new_fd()
2249 * and before returning to userspace, we might just hold
2250 * one reference and any parallel close on that fd could
2251 * rip everything out. Hence, below notifications must
2252 * happen before bpf_prog_new_fd().
2253 *
2254 * Also, any failure handling from this point onwards must
2255 * be using bpf_prog_put() given the program is exposed.
2256 */
2257 bpf_prog_kallsyms_add(prog);
2258 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2259 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2260
2261 err = bpf_prog_new_fd(prog);
2262 if (err < 0)
2263 bpf_prog_put(prog);
2264 return err;
2265
2266 free_used_maps:
2267 /* In case we have subprogs, we need to wait for a grace
2268 * period before we can tear down JIT memory since symbols
2269 * are already exposed under kallsyms.
2270 */
2271 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2272 return err;
2273 free_prog:
2274 bpf_prog_uncharge_memlock(prog);
2275 free_prog_sec:
2276 security_bpf_prog_free(prog->aux);
2277 free_prog_nouncharge:
2278 bpf_prog_free(prog);
2279 return err;
2280 }
2281
2282 #define BPF_OBJ_LAST_FIELD file_flags
2283
bpf_obj_pin(const union bpf_attr * attr)2284 static int bpf_obj_pin(const union bpf_attr *attr)
2285 {
2286 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2287 return -EINVAL;
2288
2289 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2290 }
2291
bpf_obj_get(const union bpf_attr * attr)2292 static int bpf_obj_get(const union bpf_attr *attr)
2293 {
2294 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2295 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2296 return -EINVAL;
2297
2298 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2299 attr->file_flags);
2300 }
2301
bpf_link_init(struct bpf_link * link,enum bpf_link_type type,const struct bpf_link_ops * ops,struct bpf_prog * prog)2302 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2303 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2304 {
2305 atomic64_set(&link->refcnt, 1);
2306 link->type = type;
2307 link->id = 0;
2308 link->ops = ops;
2309 link->prog = prog;
2310 }
2311
bpf_link_free_id(int id)2312 static void bpf_link_free_id(int id)
2313 {
2314 if (!id)
2315 return;
2316
2317 spin_lock_bh(&link_idr_lock);
2318 idr_remove(&link_idr, id);
2319 spin_unlock_bh(&link_idr_lock);
2320 }
2321
2322 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2323 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2324 * anon_inode's release() call. This helper marksbpf_link as
2325 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2326 * is not decremented, it's the responsibility of a calling code that failed
2327 * to complete bpf_link initialization.
2328 */
bpf_link_cleanup(struct bpf_link_primer * primer)2329 void bpf_link_cleanup(struct bpf_link_primer *primer)
2330 {
2331 primer->link->prog = NULL;
2332 bpf_link_free_id(primer->id);
2333 fput(primer->file);
2334 put_unused_fd(primer->fd);
2335 }
2336
bpf_link_inc(struct bpf_link * link)2337 void bpf_link_inc(struct bpf_link *link)
2338 {
2339 atomic64_inc(&link->refcnt);
2340 }
2341
2342 /* bpf_link_free is guaranteed to be called from process context */
bpf_link_free(struct bpf_link * link)2343 static void bpf_link_free(struct bpf_link *link)
2344 {
2345 bpf_link_free_id(link->id);
2346 if (link->prog) {
2347 /* detach BPF program, clean up used resources */
2348 link->ops->release(link);
2349 bpf_prog_put(link->prog);
2350 }
2351 /* free bpf_link and its containing memory */
2352 link->ops->dealloc(link);
2353 }
2354
bpf_link_put_deferred(struct work_struct * work)2355 static void bpf_link_put_deferred(struct work_struct *work)
2356 {
2357 struct bpf_link *link = container_of(work, struct bpf_link, work);
2358
2359 bpf_link_free(link);
2360 }
2361
2362 /* bpf_link_put can be called from atomic context, but ensures that resources
2363 * are freed from process context
2364 */
bpf_link_put(struct bpf_link * link)2365 void bpf_link_put(struct bpf_link *link)
2366 {
2367 if (!atomic64_dec_and_test(&link->refcnt))
2368 return;
2369
2370 if (in_atomic()) {
2371 INIT_WORK(&link->work, bpf_link_put_deferred);
2372 schedule_work(&link->work);
2373 } else {
2374 bpf_link_free(link);
2375 }
2376 }
2377
bpf_link_release(struct inode * inode,struct file * filp)2378 static int bpf_link_release(struct inode *inode, struct file *filp)
2379 {
2380 struct bpf_link *link = filp->private_data;
2381
2382 bpf_link_put(link);
2383 return 0;
2384 }
2385
2386 #ifdef CONFIG_PROC_FS
2387 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2388 #define BPF_MAP_TYPE(_id, _ops)
2389 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2390 static const char *bpf_link_type_strs[] = {
2391 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2392 #include <linux/bpf_types.h>
2393 };
2394 #undef BPF_PROG_TYPE
2395 #undef BPF_MAP_TYPE
2396 #undef BPF_LINK_TYPE
2397
bpf_link_show_fdinfo(struct seq_file * m,struct file * filp)2398 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2399 {
2400 const struct bpf_link *link = filp->private_data;
2401 const struct bpf_prog *prog = link->prog;
2402 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2403
2404 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2405 seq_printf(m,
2406 "link_type:\t%s\n"
2407 "link_id:\t%u\n"
2408 "prog_tag:\t%s\n"
2409 "prog_id:\t%u\n",
2410 bpf_link_type_strs[link->type],
2411 link->id,
2412 prog_tag,
2413 prog->aux->id);
2414 if (link->ops->show_fdinfo)
2415 link->ops->show_fdinfo(link, m);
2416 }
2417 #endif
2418
2419 static const struct file_operations bpf_link_fops = {
2420 #ifdef CONFIG_PROC_FS
2421 .show_fdinfo = bpf_link_show_fdinfo,
2422 #endif
2423 .release = bpf_link_release,
2424 .read = bpf_dummy_read,
2425 .write = bpf_dummy_write,
2426 };
2427
bpf_link_alloc_id(struct bpf_link * link)2428 static int bpf_link_alloc_id(struct bpf_link *link)
2429 {
2430 int id;
2431
2432 idr_preload(GFP_KERNEL);
2433 spin_lock_bh(&link_idr_lock);
2434 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2435 spin_unlock_bh(&link_idr_lock);
2436 idr_preload_end();
2437
2438 return id;
2439 }
2440
2441 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2442 * reserving unused FD and allocating ID from link_idr. This is to be paired
2443 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2444 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2445 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2446 * transient state is passed around in struct bpf_link_primer.
2447 * This is preferred way to create and initialize bpf_link, especially when
2448 * there are complicated and expensive operations inbetween creating bpf_link
2449 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2450 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2451 * expensive (and potentially failing) roll back operations in a rare case
2452 * that file, FD, or ID can't be allocated.
2453 */
bpf_link_prime(struct bpf_link * link,struct bpf_link_primer * primer)2454 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2455 {
2456 struct file *file;
2457 int fd, id;
2458
2459 fd = get_unused_fd_flags(O_CLOEXEC);
2460 if (fd < 0)
2461 return fd;
2462
2463
2464 id = bpf_link_alloc_id(link);
2465 if (id < 0) {
2466 put_unused_fd(fd);
2467 return id;
2468 }
2469
2470 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2471 if (IS_ERR(file)) {
2472 bpf_link_free_id(id);
2473 put_unused_fd(fd);
2474 return PTR_ERR(file);
2475 }
2476
2477 primer->link = link;
2478 primer->file = file;
2479 primer->fd = fd;
2480 primer->id = id;
2481 return 0;
2482 }
2483
bpf_link_settle(struct bpf_link_primer * primer)2484 int bpf_link_settle(struct bpf_link_primer *primer)
2485 {
2486 /* make bpf_link fetchable by ID */
2487 spin_lock_bh(&link_idr_lock);
2488 primer->link->id = primer->id;
2489 spin_unlock_bh(&link_idr_lock);
2490 /* make bpf_link fetchable by FD */
2491 fd_install(primer->fd, primer->file);
2492 /* pass through installed FD */
2493 return primer->fd;
2494 }
2495
bpf_link_new_fd(struct bpf_link * link)2496 int bpf_link_new_fd(struct bpf_link *link)
2497 {
2498 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2499 }
2500
bpf_link_get_from_fd(u32 ufd)2501 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2502 {
2503 struct fd f = fdget(ufd);
2504 struct bpf_link *link;
2505
2506 if (!f.file)
2507 return ERR_PTR(-EBADF);
2508 if (f.file->f_op != &bpf_link_fops) {
2509 fdput(f);
2510 return ERR_PTR(-EINVAL);
2511 }
2512
2513 link = f.file->private_data;
2514 bpf_link_inc(link);
2515 fdput(f);
2516
2517 return link;
2518 }
2519
2520 struct bpf_tracing_link {
2521 struct bpf_link link;
2522 enum bpf_attach_type attach_type;
2523 struct bpf_trampoline *trampoline;
2524 struct bpf_prog *tgt_prog;
2525 };
2526
bpf_tracing_link_release(struct bpf_link * link)2527 static void bpf_tracing_link_release(struct bpf_link *link)
2528 {
2529 struct bpf_tracing_link *tr_link =
2530 container_of(link, struct bpf_tracing_link, link);
2531
2532 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2533 tr_link->trampoline));
2534
2535 bpf_trampoline_put(tr_link->trampoline);
2536
2537 /* tgt_prog is NULL if target is a kernel function */
2538 if (tr_link->tgt_prog)
2539 bpf_prog_put(tr_link->tgt_prog);
2540 }
2541
bpf_tracing_link_dealloc(struct bpf_link * link)2542 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2543 {
2544 struct bpf_tracing_link *tr_link =
2545 container_of(link, struct bpf_tracing_link, link);
2546
2547 kfree(tr_link);
2548 }
2549
bpf_tracing_link_show_fdinfo(const struct bpf_link * link,struct seq_file * seq)2550 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2551 struct seq_file *seq)
2552 {
2553 struct bpf_tracing_link *tr_link =
2554 container_of(link, struct bpf_tracing_link, link);
2555
2556 seq_printf(seq,
2557 "attach_type:\t%d\n",
2558 tr_link->attach_type);
2559 }
2560
bpf_tracing_link_fill_link_info(const struct bpf_link * link,struct bpf_link_info * info)2561 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2562 struct bpf_link_info *info)
2563 {
2564 struct bpf_tracing_link *tr_link =
2565 container_of(link, struct bpf_tracing_link, link);
2566
2567 info->tracing.attach_type = tr_link->attach_type;
2568
2569 return 0;
2570 }
2571
2572 static const struct bpf_link_ops bpf_tracing_link_lops = {
2573 .release = bpf_tracing_link_release,
2574 .dealloc = bpf_tracing_link_dealloc,
2575 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2576 .fill_link_info = bpf_tracing_link_fill_link_info,
2577 };
2578
bpf_tracing_prog_attach(struct bpf_prog * prog,int tgt_prog_fd,u32 btf_id)2579 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2580 int tgt_prog_fd,
2581 u32 btf_id)
2582 {
2583 struct bpf_link_primer link_primer;
2584 struct bpf_prog *tgt_prog = NULL;
2585 struct bpf_trampoline *tr = NULL;
2586 struct bpf_tracing_link *link;
2587 u64 key = 0;
2588 int err;
2589
2590 switch (prog->type) {
2591 case BPF_PROG_TYPE_TRACING:
2592 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2593 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2594 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2595 err = -EINVAL;
2596 goto out_put_prog;
2597 }
2598 break;
2599 case BPF_PROG_TYPE_EXT:
2600 if (prog->expected_attach_type != 0) {
2601 err = -EINVAL;
2602 goto out_put_prog;
2603 }
2604 break;
2605 case BPF_PROG_TYPE_LSM:
2606 if (prog->expected_attach_type != BPF_LSM_MAC) {
2607 err = -EINVAL;
2608 goto out_put_prog;
2609 }
2610 break;
2611 default:
2612 err = -EINVAL;
2613 goto out_put_prog;
2614 }
2615
2616 if (!!tgt_prog_fd != !!btf_id) {
2617 err = -EINVAL;
2618 goto out_put_prog;
2619 }
2620
2621 if (tgt_prog_fd) {
2622 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2623 if (prog->type != BPF_PROG_TYPE_EXT) {
2624 err = -EINVAL;
2625 goto out_put_prog;
2626 }
2627
2628 tgt_prog = bpf_prog_get(tgt_prog_fd);
2629 if (IS_ERR(tgt_prog)) {
2630 err = PTR_ERR(tgt_prog);
2631 tgt_prog = NULL;
2632 goto out_put_prog;
2633 }
2634
2635 key = bpf_trampoline_compute_key(tgt_prog, btf_id);
2636 }
2637
2638 link = kzalloc(sizeof(*link), GFP_USER);
2639 if (!link) {
2640 err = -ENOMEM;
2641 goto out_put_prog;
2642 }
2643 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2644 &bpf_tracing_link_lops, prog);
2645 link->attach_type = prog->expected_attach_type;
2646
2647 mutex_lock(&prog->aux->dst_mutex);
2648
2649 /* There are a few possible cases here:
2650 *
2651 * - if prog->aux->dst_trampoline is set, the program was just loaded
2652 * and not yet attached to anything, so we can use the values stored
2653 * in prog->aux
2654 *
2655 * - if prog->aux->dst_trampoline is NULL, the program has already been
2656 * attached to a target and its initial target was cleared (below)
2657 *
2658 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2659 * target_btf_id using the link_create API.
2660 *
2661 * - if tgt_prog == NULL when this function was called using the old
2662 * raw_tracepoint_open API, and we need a target from prog->aux
2663 *
2664 * The combination of no saved target in prog->aux, and no target
2665 * specified on load is illegal, and we reject that here.
2666 */
2667 if (!prog->aux->dst_trampoline && !tgt_prog) {
2668 err = -ENOENT;
2669 goto out_unlock;
2670 }
2671
2672 if (!prog->aux->dst_trampoline ||
2673 (key && key != prog->aux->dst_trampoline->key)) {
2674 /* If there is no saved target, or the specified target is
2675 * different from the destination specified at load time, we
2676 * need a new trampoline and a check for compatibility
2677 */
2678 struct bpf_attach_target_info tgt_info = {};
2679
2680 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2681 &tgt_info);
2682 if (err)
2683 goto out_unlock;
2684
2685 tr = bpf_trampoline_get(key, &tgt_info);
2686 if (!tr) {
2687 err = -ENOMEM;
2688 goto out_unlock;
2689 }
2690 } else {
2691 /* The caller didn't specify a target, or the target was the
2692 * same as the destination supplied during program load. This
2693 * means we can reuse the trampoline and reference from program
2694 * load time, and there is no need to allocate a new one. This
2695 * can only happen once for any program, as the saved values in
2696 * prog->aux are cleared below.
2697 */
2698 tr = prog->aux->dst_trampoline;
2699 tgt_prog = prog->aux->dst_prog;
2700 }
2701
2702 err = bpf_link_prime(&link->link, &link_primer);
2703 if (err)
2704 goto out_unlock;
2705
2706 err = bpf_trampoline_link_prog(prog, tr);
2707 if (err) {
2708 bpf_link_cleanup(&link_primer);
2709 link = NULL;
2710 goto out_unlock;
2711 }
2712
2713 link->tgt_prog = tgt_prog;
2714 link->trampoline = tr;
2715
2716 /* Always clear the trampoline and target prog from prog->aux to make
2717 * sure the original attach destination is not kept alive after a
2718 * program is (re-)attached to another target.
2719 */
2720 if (prog->aux->dst_prog &&
2721 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2722 /* got extra prog ref from syscall, or attaching to different prog */
2723 bpf_prog_put(prog->aux->dst_prog);
2724 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2725 /* we allocated a new trampoline, so free the old one */
2726 bpf_trampoline_put(prog->aux->dst_trampoline);
2727
2728 prog->aux->dst_prog = NULL;
2729 prog->aux->dst_trampoline = NULL;
2730 mutex_unlock(&prog->aux->dst_mutex);
2731
2732 return bpf_link_settle(&link_primer);
2733 out_unlock:
2734 if (tr && tr != prog->aux->dst_trampoline)
2735 bpf_trampoline_put(tr);
2736 mutex_unlock(&prog->aux->dst_mutex);
2737 kfree(link);
2738 out_put_prog:
2739 if (tgt_prog_fd && tgt_prog)
2740 bpf_prog_put(tgt_prog);
2741 return err;
2742 }
2743
2744 struct bpf_raw_tp_link {
2745 struct bpf_link link;
2746 struct bpf_raw_event_map *btp;
2747 };
2748
bpf_raw_tp_link_release(struct bpf_link * link)2749 static void bpf_raw_tp_link_release(struct bpf_link *link)
2750 {
2751 struct bpf_raw_tp_link *raw_tp =
2752 container_of(link, struct bpf_raw_tp_link, link);
2753
2754 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
2755 bpf_put_raw_tracepoint(raw_tp->btp);
2756 }
2757
bpf_raw_tp_link_dealloc(struct bpf_link * link)2758 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2759 {
2760 struct bpf_raw_tp_link *raw_tp =
2761 container_of(link, struct bpf_raw_tp_link, link);
2762
2763 kfree(raw_tp);
2764 }
2765
bpf_raw_tp_link_show_fdinfo(const struct bpf_link * link,struct seq_file * seq)2766 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2767 struct seq_file *seq)
2768 {
2769 struct bpf_raw_tp_link *raw_tp_link =
2770 container_of(link, struct bpf_raw_tp_link, link);
2771
2772 seq_printf(seq,
2773 "tp_name:\t%s\n",
2774 raw_tp_link->btp->tp->name);
2775 }
2776
bpf_raw_tp_link_fill_link_info(const struct bpf_link * link,struct bpf_link_info * info)2777 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2778 struct bpf_link_info *info)
2779 {
2780 struct bpf_raw_tp_link *raw_tp_link =
2781 container_of(link, struct bpf_raw_tp_link, link);
2782 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2783 const char *tp_name = raw_tp_link->btp->tp->name;
2784 u32 ulen = info->raw_tracepoint.tp_name_len;
2785 size_t tp_len = strlen(tp_name);
2786
2787 if (!ulen ^ !ubuf)
2788 return -EINVAL;
2789
2790 info->raw_tracepoint.tp_name_len = tp_len + 1;
2791
2792 if (!ubuf)
2793 return 0;
2794
2795 if (ulen >= tp_len + 1) {
2796 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2797 return -EFAULT;
2798 } else {
2799 char zero = '\0';
2800
2801 if (copy_to_user(ubuf, tp_name, ulen - 1))
2802 return -EFAULT;
2803 if (put_user(zero, ubuf + ulen - 1))
2804 return -EFAULT;
2805 return -ENOSPC;
2806 }
2807
2808 return 0;
2809 }
2810
2811 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
2812 .release = bpf_raw_tp_link_release,
2813 .dealloc = bpf_raw_tp_link_dealloc,
2814 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2815 .fill_link_info = bpf_raw_tp_link_fill_link_info,
2816 };
2817
2818 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2819
bpf_raw_tracepoint_open(const union bpf_attr * attr)2820 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2821 {
2822 struct bpf_link_primer link_primer;
2823 struct bpf_raw_tp_link *link;
2824 struct bpf_raw_event_map *btp;
2825 struct bpf_prog *prog;
2826 const char *tp_name;
2827 char buf[128];
2828 int err;
2829
2830 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2831 return -EINVAL;
2832
2833 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2834 if (IS_ERR(prog))
2835 return PTR_ERR(prog);
2836
2837 switch (prog->type) {
2838 case BPF_PROG_TYPE_TRACING:
2839 case BPF_PROG_TYPE_EXT:
2840 case BPF_PROG_TYPE_LSM:
2841 if (attr->raw_tracepoint.name) {
2842 /* The attach point for this category of programs
2843 * should be specified via btf_id during program load.
2844 */
2845 err = -EINVAL;
2846 goto out_put_prog;
2847 }
2848 if (prog->type == BPF_PROG_TYPE_TRACING &&
2849 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
2850 tp_name = prog->aux->attach_func_name;
2851 break;
2852 }
2853 err = bpf_tracing_prog_attach(prog, 0, 0);
2854 if (err >= 0)
2855 return err;
2856 goto out_put_prog;
2857 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2858 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2859 if (strncpy_from_user(buf,
2860 u64_to_user_ptr(attr->raw_tracepoint.name),
2861 sizeof(buf) - 1) < 0) {
2862 err = -EFAULT;
2863 goto out_put_prog;
2864 }
2865 buf[sizeof(buf) - 1] = 0;
2866 tp_name = buf;
2867 break;
2868 default:
2869 err = -EINVAL;
2870 goto out_put_prog;
2871 }
2872
2873 btp = bpf_get_raw_tracepoint(tp_name);
2874 if (!btp) {
2875 err = -ENOENT;
2876 goto out_put_prog;
2877 }
2878
2879 link = kzalloc(sizeof(*link), GFP_USER);
2880 if (!link) {
2881 err = -ENOMEM;
2882 goto out_put_btp;
2883 }
2884 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2885 &bpf_raw_tp_link_lops, prog);
2886 link->btp = btp;
2887
2888 err = bpf_link_prime(&link->link, &link_primer);
2889 if (err) {
2890 kfree(link);
2891 goto out_put_btp;
2892 }
2893
2894 err = bpf_probe_register(link->btp, prog);
2895 if (err) {
2896 bpf_link_cleanup(&link_primer);
2897 goto out_put_btp;
2898 }
2899
2900 return bpf_link_settle(&link_primer);
2901
2902 out_put_btp:
2903 bpf_put_raw_tracepoint(btp);
2904 out_put_prog:
2905 bpf_prog_put(prog);
2906 return err;
2907 }
2908
bpf_prog_attach_check_attach_type(const struct bpf_prog * prog,enum bpf_attach_type attach_type)2909 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2910 enum bpf_attach_type attach_type)
2911 {
2912 switch (prog->type) {
2913 case BPF_PROG_TYPE_CGROUP_SOCK:
2914 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2915 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2916 case BPF_PROG_TYPE_SK_LOOKUP:
2917 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
2918 case BPF_PROG_TYPE_CGROUP_SKB:
2919 if (!capable(CAP_NET_ADMIN))
2920 /* cg-skb progs can be loaded by unpriv user.
2921 * check permissions at attach time.
2922 */
2923 return -EPERM;
2924 return prog->enforce_expected_attach_type &&
2925 prog->expected_attach_type != attach_type ?
2926 -EINVAL : 0;
2927 default:
2928 return 0;
2929 }
2930 }
2931
2932 static enum bpf_prog_type
attach_type_to_prog_type(enum bpf_attach_type attach_type)2933 attach_type_to_prog_type(enum bpf_attach_type attach_type)
2934 {
2935 switch (attach_type) {
2936 case BPF_CGROUP_INET_INGRESS:
2937 case BPF_CGROUP_INET_EGRESS:
2938 return BPF_PROG_TYPE_CGROUP_SKB;
2939 case BPF_CGROUP_INET_SOCK_CREATE:
2940 case BPF_CGROUP_INET_SOCK_RELEASE:
2941 case BPF_CGROUP_INET4_POST_BIND:
2942 case BPF_CGROUP_INET6_POST_BIND:
2943 return BPF_PROG_TYPE_CGROUP_SOCK;
2944 case BPF_CGROUP_INET4_BIND:
2945 case BPF_CGROUP_INET6_BIND:
2946 case BPF_CGROUP_INET4_CONNECT:
2947 case BPF_CGROUP_INET6_CONNECT:
2948 case BPF_CGROUP_INET4_GETPEERNAME:
2949 case BPF_CGROUP_INET6_GETPEERNAME:
2950 case BPF_CGROUP_INET4_GETSOCKNAME:
2951 case BPF_CGROUP_INET6_GETSOCKNAME:
2952 case BPF_CGROUP_UDP4_SENDMSG:
2953 case BPF_CGROUP_UDP6_SENDMSG:
2954 case BPF_CGROUP_UDP4_RECVMSG:
2955 case BPF_CGROUP_UDP6_RECVMSG:
2956 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2957 case BPF_CGROUP_SOCK_OPS:
2958 return BPF_PROG_TYPE_SOCK_OPS;
2959 case BPF_CGROUP_DEVICE:
2960 return BPF_PROG_TYPE_CGROUP_DEVICE;
2961 case BPF_SK_MSG_VERDICT:
2962 return BPF_PROG_TYPE_SK_MSG;
2963 case BPF_SK_SKB_STREAM_PARSER:
2964 case BPF_SK_SKB_STREAM_VERDICT:
2965 return BPF_PROG_TYPE_SK_SKB;
2966 case BPF_LIRC_MODE2:
2967 return BPF_PROG_TYPE_LIRC_MODE2;
2968 case BPF_FLOW_DISSECTOR:
2969 return BPF_PROG_TYPE_FLOW_DISSECTOR;
2970 case BPF_CGROUP_SYSCTL:
2971 return BPF_PROG_TYPE_CGROUP_SYSCTL;
2972 case BPF_CGROUP_GETSOCKOPT:
2973 case BPF_CGROUP_SETSOCKOPT:
2974 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
2975 case BPF_TRACE_ITER:
2976 return BPF_PROG_TYPE_TRACING;
2977 case BPF_SK_LOOKUP:
2978 return BPF_PROG_TYPE_SK_LOOKUP;
2979 case BPF_XDP:
2980 return BPF_PROG_TYPE_XDP;
2981 default:
2982 return BPF_PROG_TYPE_UNSPEC;
2983 }
2984 }
2985
2986 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
2987
2988 #define BPF_F_ATTACH_MASK \
2989 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
2990
bpf_prog_attach(const union bpf_attr * attr)2991 static int bpf_prog_attach(const union bpf_attr *attr)
2992 {
2993 enum bpf_prog_type ptype;
2994 struct bpf_prog *prog;
2995 int ret;
2996
2997 if (CHECK_ATTR(BPF_PROG_ATTACH))
2998 return -EINVAL;
2999
3000 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
3001 return -EINVAL;
3002
3003 ptype = attach_type_to_prog_type(attr->attach_type);
3004 if (ptype == BPF_PROG_TYPE_UNSPEC)
3005 return -EINVAL;
3006
3007 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3008 if (IS_ERR(prog))
3009 return PTR_ERR(prog);
3010
3011 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3012 bpf_prog_put(prog);
3013 return -EINVAL;
3014 }
3015
3016 switch (ptype) {
3017 case BPF_PROG_TYPE_SK_SKB:
3018 case BPF_PROG_TYPE_SK_MSG:
3019 ret = sock_map_get_from_fd(attr, prog);
3020 break;
3021 case BPF_PROG_TYPE_LIRC_MODE2:
3022 ret = lirc_prog_attach(attr, prog);
3023 break;
3024 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3025 ret = netns_bpf_prog_attach(attr, prog);
3026 break;
3027 case BPF_PROG_TYPE_CGROUP_DEVICE:
3028 case BPF_PROG_TYPE_CGROUP_SKB:
3029 case BPF_PROG_TYPE_CGROUP_SOCK:
3030 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3031 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3032 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3033 case BPF_PROG_TYPE_SOCK_OPS:
3034 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3035 break;
3036 default:
3037 ret = -EINVAL;
3038 }
3039
3040 if (ret)
3041 bpf_prog_put(prog);
3042 return ret;
3043 }
3044
3045 #define BPF_PROG_DETACH_LAST_FIELD attach_type
3046
bpf_prog_detach(const union bpf_attr * attr)3047 static int bpf_prog_detach(const union bpf_attr *attr)
3048 {
3049 enum bpf_prog_type ptype;
3050
3051 if (CHECK_ATTR(BPF_PROG_DETACH))
3052 return -EINVAL;
3053
3054 ptype = attach_type_to_prog_type(attr->attach_type);
3055
3056 switch (ptype) {
3057 case BPF_PROG_TYPE_SK_MSG:
3058 case BPF_PROG_TYPE_SK_SKB:
3059 return sock_map_prog_detach(attr, ptype);
3060 case BPF_PROG_TYPE_LIRC_MODE2:
3061 return lirc_prog_detach(attr);
3062 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3063 return netns_bpf_prog_detach(attr, ptype);
3064 case BPF_PROG_TYPE_CGROUP_DEVICE:
3065 case BPF_PROG_TYPE_CGROUP_SKB:
3066 case BPF_PROG_TYPE_CGROUP_SOCK:
3067 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3068 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3069 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3070 case BPF_PROG_TYPE_SOCK_OPS:
3071 return cgroup_bpf_prog_detach(attr, ptype);
3072 default:
3073 return -EINVAL;
3074 }
3075 }
3076
3077 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3078
bpf_prog_query(const union bpf_attr * attr,union bpf_attr __user * uattr)3079 static int bpf_prog_query(const union bpf_attr *attr,
3080 union bpf_attr __user *uattr)
3081 {
3082 if (!capable(CAP_NET_ADMIN))
3083 return -EPERM;
3084 if (CHECK_ATTR(BPF_PROG_QUERY))
3085 return -EINVAL;
3086 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3087 return -EINVAL;
3088
3089 switch (attr->query.attach_type) {
3090 case BPF_CGROUP_INET_INGRESS:
3091 case BPF_CGROUP_INET_EGRESS:
3092 case BPF_CGROUP_INET_SOCK_CREATE:
3093 case BPF_CGROUP_INET_SOCK_RELEASE:
3094 case BPF_CGROUP_INET4_BIND:
3095 case BPF_CGROUP_INET6_BIND:
3096 case BPF_CGROUP_INET4_POST_BIND:
3097 case BPF_CGROUP_INET6_POST_BIND:
3098 case BPF_CGROUP_INET4_CONNECT:
3099 case BPF_CGROUP_INET6_CONNECT:
3100 case BPF_CGROUP_INET4_GETPEERNAME:
3101 case BPF_CGROUP_INET6_GETPEERNAME:
3102 case BPF_CGROUP_INET4_GETSOCKNAME:
3103 case BPF_CGROUP_INET6_GETSOCKNAME:
3104 case BPF_CGROUP_UDP4_SENDMSG:
3105 case BPF_CGROUP_UDP6_SENDMSG:
3106 case BPF_CGROUP_UDP4_RECVMSG:
3107 case BPF_CGROUP_UDP6_RECVMSG:
3108 case BPF_CGROUP_SOCK_OPS:
3109 case BPF_CGROUP_DEVICE:
3110 case BPF_CGROUP_SYSCTL:
3111 case BPF_CGROUP_GETSOCKOPT:
3112 case BPF_CGROUP_SETSOCKOPT:
3113 return cgroup_bpf_prog_query(attr, uattr);
3114 case BPF_LIRC_MODE2:
3115 return lirc_prog_query(attr, uattr);
3116 case BPF_FLOW_DISSECTOR:
3117 case BPF_SK_LOOKUP:
3118 return netns_bpf_prog_query(attr, uattr);
3119 default:
3120 return -EINVAL;
3121 }
3122 }
3123
3124 #define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
3125
bpf_prog_test_run(const union bpf_attr * attr,union bpf_attr __user * uattr)3126 static int bpf_prog_test_run(const union bpf_attr *attr,
3127 union bpf_attr __user *uattr)
3128 {
3129 struct bpf_prog *prog;
3130 int ret = -ENOTSUPP;
3131
3132 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3133 return -EINVAL;
3134
3135 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3136 (!attr->test.ctx_size_in && attr->test.ctx_in))
3137 return -EINVAL;
3138
3139 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3140 (!attr->test.ctx_size_out && attr->test.ctx_out))
3141 return -EINVAL;
3142
3143 prog = bpf_prog_get(attr->test.prog_fd);
3144 if (IS_ERR(prog))
3145 return PTR_ERR(prog);
3146
3147 if (prog->aux->ops->test_run)
3148 ret = prog->aux->ops->test_run(prog, attr, uattr);
3149
3150 bpf_prog_put(prog);
3151 return ret;
3152 }
3153
3154 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3155
bpf_obj_get_next_id(const union bpf_attr * attr,union bpf_attr __user * uattr,struct idr * idr,spinlock_t * lock)3156 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3157 union bpf_attr __user *uattr,
3158 struct idr *idr,
3159 spinlock_t *lock)
3160 {
3161 u32 next_id = attr->start_id;
3162 int err = 0;
3163
3164 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3165 return -EINVAL;
3166
3167 if (!capable(CAP_SYS_ADMIN))
3168 return -EPERM;
3169
3170 next_id++;
3171 spin_lock_bh(lock);
3172 if (!idr_get_next(idr, &next_id))
3173 err = -ENOENT;
3174 spin_unlock_bh(lock);
3175
3176 if (!err)
3177 err = put_user(next_id, &uattr->next_id);
3178
3179 return err;
3180 }
3181
bpf_map_get_curr_or_next(u32 * id)3182 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3183 {
3184 struct bpf_map *map;
3185
3186 spin_lock_bh(&map_idr_lock);
3187 again:
3188 map = idr_get_next(&map_idr, id);
3189 if (map) {
3190 map = __bpf_map_inc_not_zero(map, false);
3191 if (IS_ERR(map)) {
3192 (*id)++;
3193 goto again;
3194 }
3195 }
3196 spin_unlock_bh(&map_idr_lock);
3197
3198 return map;
3199 }
3200
bpf_prog_get_curr_or_next(u32 * id)3201 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3202 {
3203 struct bpf_prog *prog;
3204
3205 spin_lock_bh(&prog_idr_lock);
3206 again:
3207 prog = idr_get_next(&prog_idr, id);
3208 if (prog) {
3209 prog = bpf_prog_inc_not_zero(prog);
3210 if (IS_ERR(prog)) {
3211 (*id)++;
3212 goto again;
3213 }
3214 }
3215 spin_unlock_bh(&prog_idr_lock);
3216
3217 return prog;
3218 }
3219
3220 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3221
bpf_prog_by_id(u32 id)3222 struct bpf_prog *bpf_prog_by_id(u32 id)
3223 {
3224 struct bpf_prog *prog;
3225
3226 if (!id)
3227 return ERR_PTR(-ENOENT);
3228
3229 spin_lock_bh(&prog_idr_lock);
3230 prog = idr_find(&prog_idr, id);
3231 if (prog)
3232 prog = bpf_prog_inc_not_zero(prog);
3233 else
3234 prog = ERR_PTR(-ENOENT);
3235 spin_unlock_bh(&prog_idr_lock);
3236 return prog;
3237 }
3238
bpf_prog_get_fd_by_id(const union bpf_attr * attr)3239 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3240 {
3241 struct bpf_prog *prog;
3242 u32 id = attr->prog_id;
3243 int fd;
3244
3245 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3246 return -EINVAL;
3247
3248 if (!capable(CAP_SYS_ADMIN))
3249 return -EPERM;
3250
3251 prog = bpf_prog_by_id(id);
3252 if (IS_ERR(prog))
3253 return PTR_ERR(prog);
3254
3255 fd = bpf_prog_new_fd(prog);
3256 if (fd < 0)
3257 bpf_prog_put(prog);
3258
3259 return fd;
3260 }
3261
3262 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3263
bpf_map_get_fd_by_id(const union bpf_attr * attr)3264 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3265 {
3266 struct bpf_map *map;
3267 u32 id = attr->map_id;
3268 int f_flags;
3269 int fd;
3270
3271 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3272 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3273 return -EINVAL;
3274
3275 if (!capable(CAP_SYS_ADMIN))
3276 return -EPERM;
3277
3278 f_flags = bpf_get_file_flag(attr->open_flags);
3279 if (f_flags < 0)
3280 return f_flags;
3281
3282 spin_lock_bh(&map_idr_lock);
3283 map = idr_find(&map_idr, id);
3284 if (map)
3285 map = __bpf_map_inc_not_zero(map, true);
3286 else
3287 map = ERR_PTR(-ENOENT);
3288 spin_unlock_bh(&map_idr_lock);
3289
3290 if (IS_ERR(map))
3291 return PTR_ERR(map);
3292
3293 fd = bpf_map_new_fd(map, f_flags);
3294 if (fd < 0)
3295 bpf_map_put_with_uref(map);
3296
3297 return fd;
3298 }
3299
bpf_map_from_imm(const struct bpf_prog * prog,unsigned long addr,u32 * off,u32 * type)3300 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3301 unsigned long addr, u32 *off,
3302 u32 *type)
3303 {
3304 const struct bpf_map *map;
3305 int i;
3306
3307 mutex_lock(&prog->aux->used_maps_mutex);
3308 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3309 map = prog->aux->used_maps[i];
3310 if (map == (void *)addr) {
3311 *type = BPF_PSEUDO_MAP_FD;
3312 goto out;
3313 }
3314 if (!map->ops->map_direct_value_meta)
3315 continue;
3316 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3317 *type = BPF_PSEUDO_MAP_VALUE;
3318 goto out;
3319 }
3320 }
3321 map = NULL;
3322
3323 out:
3324 mutex_unlock(&prog->aux->used_maps_mutex);
3325 return map;
3326 }
3327
bpf_insn_prepare_dump(const struct bpf_prog * prog,const struct cred * f_cred)3328 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3329 const struct cred *f_cred)
3330 {
3331 const struct bpf_map *map;
3332 struct bpf_insn *insns;
3333 u32 off, type;
3334 u64 imm;
3335 u8 code;
3336 int i;
3337
3338 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3339 GFP_USER);
3340 if (!insns)
3341 return insns;
3342
3343 for (i = 0; i < prog->len; i++) {
3344 code = insns[i].code;
3345
3346 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3347 insns[i].code = BPF_JMP | BPF_CALL;
3348 insns[i].imm = BPF_FUNC_tail_call;
3349 /* fall-through */
3350 }
3351 if (code == (BPF_JMP | BPF_CALL) ||
3352 code == (BPF_JMP | BPF_CALL_ARGS)) {
3353 if (code == (BPF_JMP | BPF_CALL_ARGS))
3354 insns[i].code = BPF_JMP | BPF_CALL;
3355 if (!bpf_dump_raw_ok(f_cred))
3356 insns[i].imm = 0;
3357 continue;
3358 }
3359 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3360 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3361 continue;
3362 }
3363
3364 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3365 continue;
3366
3367 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3368 map = bpf_map_from_imm(prog, imm, &off, &type);
3369 if (map) {
3370 insns[i].src_reg = type;
3371 insns[i].imm = map->id;
3372 insns[i + 1].imm = off;
3373 continue;
3374 }
3375 }
3376
3377 return insns;
3378 }
3379
set_info_rec_size(struct bpf_prog_info * info)3380 static int set_info_rec_size(struct bpf_prog_info *info)
3381 {
3382 /*
3383 * Ensure info.*_rec_size is the same as kernel expected size
3384 *
3385 * or
3386 *
3387 * Only allow zero *_rec_size if both _rec_size and _cnt are
3388 * zero. In this case, the kernel will set the expected
3389 * _rec_size back to the info.
3390 */
3391
3392 if ((info->nr_func_info || info->func_info_rec_size) &&
3393 info->func_info_rec_size != sizeof(struct bpf_func_info))
3394 return -EINVAL;
3395
3396 if ((info->nr_line_info || info->line_info_rec_size) &&
3397 info->line_info_rec_size != sizeof(struct bpf_line_info))
3398 return -EINVAL;
3399
3400 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3401 info->jited_line_info_rec_size != sizeof(__u64))
3402 return -EINVAL;
3403
3404 info->func_info_rec_size = sizeof(struct bpf_func_info);
3405 info->line_info_rec_size = sizeof(struct bpf_line_info);
3406 info->jited_line_info_rec_size = sizeof(__u64);
3407
3408 return 0;
3409 }
3410
bpf_prog_get_info_by_fd(struct file * file,struct bpf_prog * prog,const union bpf_attr * attr,union bpf_attr __user * uattr)3411 static int bpf_prog_get_info_by_fd(struct file *file,
3412 struct bpf_prog *prog,
3413 const union bpf_attr *attr,
3414 union bpf_attr __user *uattr)
3415 {
3416 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3417 struct bpf_prog_info info;
3418 u32 info_len = attr->info.info_len;
3419 struct bpf_prog_stats stats;
3420 char __user *uinsns;
3421 u32 ulen;
3422 int err;
3423
3424 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3425 if (err)
3426 return err;
3427 info_len = min_t(u32, sizeof(info), info_len);
3428
3429 memset(&info, 0, sizeof(info));
3430 if (copy_from_user(&info, uinfo, info_len))
3431 return -EFAULT;
3432
3433 info.type = prog->type;
3434 info.id = prog->aux->id;
3435 info.load_time = prog->aux->load_time;
3436 info.created_by_uid = from_kuid_munged(current_user_ns(),
3437 prog->aux->user->uid);
3438 info.gpl_compatible = prog->gpl_compatible;
3439
3440 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3441 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3442
3443 mutex_lock(&prog->aux->used_maps_mutex);
3444 ulen = info.nr_map_ids;
3445 info.nr_map_ids = prog->aux->used_map_cnt;
3446 ulen = min_t(u32, info.nr_map_ids, ulen);
3447 if (ulen) {
3448 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3449 u32 i;
3450
3451 for (i = 0; i < ulen; i++)
3452 if (put_user(prog->aux->used_maps[i]->id,
3453 &user_map_ids[i])) {
3454 mutex_unlock(&prog->aux->used_maps_mutex);
3455 return -EFAULT;
3456 }
3457 }
3458 mutex_unlock(&prog->aux->used_maps_mutex);
3459
3460 err = set_info_rec_size(&info);
3461 if (err)
3462 return err;
3463
3464 bpf_prog_get_stats(prog, &stats);
3465 info.run_time_ns = stats.nsecs;
3466 info.run_cnt = stats.cnt;
3467
3468 if (!bpf_capable()) {
3469 info.jited_prog_len = 0;
3470 info.xlated_prog_len = 0;
3471 info.nr_jited_ksyms = 0;
3472 info.nr_jited_func_lens = 0;
3473 info.nr_func_info = 0;
3474 info.nr_line_info = 0;
3475 info.nr_jited_line_info = 0;
3476 goto done;
3477 }
3478
3479 ulen = info.xlated_prog_len;
3480 info.xlated_prog_len = bpf_prog_insn_size(prog);
3481 if (info.xlated_prog_len && ulen) {
3482 struct bpf_insn *insns_sanitized;
3483 bool fault;
3484
3485 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3486 info.xlated_prog_insns = 0;
3487 goto done;
3488 }
3489 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3490 if (!insns_sanitized)
3491 return -ENOMEM;
3492 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3493 ulen = min_t(u32, info.xlated_prog_len, ulen);
3494 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3495 kfree(insns_sanitized);
3496 if (fault)
3497 return -EFAULT;
3498 }
3499
3500 if (bpf_prog_is_dev_bound(prog->aux)) {
3501 err = bpf_prog_offload_info_fill(&info, prog);
3502 if (err)
3503 return err;
3504 goto done;
3505 }
3506
3507 /* NOTE: the following code is supposed to be skipped for offload.
3508 * bpf_prog_offload_info_fill() is the place to fill similar fields
3509 * for offload.
3510 */
3511 ulen = info.jited_prog_len;
3512 if (prog->aux->func_cnt) {
3513 u32 i;
3514
3515 info.jited_prog_len = 0;
3516 for (i = 0; i < prog->aux->func_cnt; i++)
3517 info.jited_prog_len += prog->aux->func[i]->jited_len;
3518 } else {
3519 info.jited_prog_len = prog->jited_len;
3520 }
3521
3522 if (info.jited_prog_len && ulen) {
3523 if (bpf_dump_raw_ok(file->f_cred)) {
3524 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3525 ulen = min_t(u32, info.jited_prog_len, ulen);
3526
3527 /* for multi-function programs, copy the JITed
3528 * instructions for all the functions
3529 */
3530 if (prog->aux->func_cnt) {
3531 u32 len, free, i;
3532 u8 *img;
3533
3534 free = ulen;
3535 for (i = 0; i < prog->aux->func_cnt; i++) {
3536 len = prog->aux->func[i]->jited_len;
3537 len = min_t(u32, len, free);
3538 img = (u8 *) prog->aux->func[i]->bpf_func;
3539 if (copy_to_user(uinsns, img, len))
3540 return -EFAULT;
3541 uinsns += len;
3542 free -= len;
3543 if (!free)
3544 break;
3545 }
3546 } else {
3547 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3548 return -EFAULT;
3549 }
3550 } else {
3551 info.jited_prog_insns = 0;
3552 }
3553 }
3554
3555 ulen = info.nr_jited_ksyms;
3556 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
3557 if (ulen) {
3558 if (bpf_dump_raw_ok(file->f_cred)) {
3559 unsigned long ksym_addr;
3560 u64 __user *user_ksyms;
3561 u32 i;
3562
3563 /* copy the address of the kernel symbol
3564 * corresponding to each function
3565 */
3566 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3567 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
3568 if (prog->aux->func_cnt) {
3569 for (i = 0; i < ulen; i++) {
3570 ksym_addr = (unsigned long)
3571 prog->aux->func[i]->bpf_func;
3572 if (put_user((u64) ksym_addr,
3573 &user_ksyms[i]))
3574 return -EFAULT;
3575 }
3576 } else {
3577 ksym_addr = (unsigned long) prog->bpf_func;
3578 if (put_user((u64) ksym_addr, &user_ksyms[0]))
3579 return -EFAULT;
3580 }
3581 } else {
3582 info.jited_ksyms = 0;
3583 }
3584 }
3585
3586 ulen = info.nr_jited_func_lens;
3587 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
3588 if (ulen) {
3589 if (bpf_dump_raw_ok(file->f_cred)) {
3590 u32 __user *user_lens;
3591 u32 func_len, i;
3592
3593 /* copy the JITed image lengths for each function */
3594 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3595 user_lens = u64_to_user_ptr(info.jited_func_lens);
3596 if (prog->aux->func_cnt) {
3597 for (i = 0; i < ulen; i++) {
3598 func_len =
3599 prog->aux->func[i]->jited_len;
3600 if (put_user(func_len, &user_lens[i]))
3601 return -EFAULT;
3602 }
3603 } else {
3604 func_len = prog->jited_len;
3605 if (put_user(func_len, &user_lens[0]))
3606 return -EFAULT;
3607 }
3608 } else {
3609 info.jited_func_lens = 0;
3610 }
3611 }
3612
3613 if (prog->aux->btf)
3614 info.btf_id = btf_id(prog->aux->btf);
3615
3616 ulen = info.nr_func_info;
3617 info.nr_func_info = prog->aux->func_info_cnt;
3618 if (info.nr_func_info && ulen) {
3619 char __user *user_finfo;
3620
3621 user_finfo = u64_to_user_ptr(info.func_info);
3622 ulen = min_t(u32, info.nr_func_info, ulen);
3623 if (copy_to_user(user_finfo, prog->aux->func_info,
3624 info.func_info_rec_size * ulen))
3625 return -EFAULT;
3626 }
3627
3628 ulen = info.nr_line_info;
3629 info.nr_line_info = prog->aux->nr_linfo;
3630 if (info.nr_line_info && ulen) {
3631 __u8 __user *user_linfo;
3632
3633 user_linfo = u64_to_user_ptr(info.line_info);
3634 ulen = min_t(u32, info.nr_line_info, ulen);
3635 if (copy_to_user(user_linfo, prog->aux->linfo,
3636 info.line_info_rec_size * ulen))
3637 return -EFAULT;
3638 }
3639
3640 ulen = info.nr_jited_line_info;
3641 if (prog->aux->jited_linfo)
3642 info.nr_jited_line_info = prog->aux->nr_linfo;
3643 else
3644 info.nr_jited_line_info = 0;
3645 if (info.nr_jited_line_info && ulen) {
3646 if (bpf_dump_raw_ok(file->f_cred)) {
3647 __u64 __user *user_linfo;
3648 u32 i;
3649
3650 user_linfo = u64_to_user_ptr(info.jited_line_info);
3651 ulen = min_t(u32, info.nr_jited_line_info, ulen);
3652 for (i = 0; i < ulen; i++) {
3653 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3654 &user_linfo[i]))
3655 return -EFAULT;
3656 }
3657 } else {
3658 info.jited_line_info = 0;
3659 }
3660 }
3661
3662 ulen = info.nr_prog_tags;
3663 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3664 if (ulen) {
3665 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3666 u32 i;
3667
3668 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3669 ulen = min_t(u32, info.nr_prog_tags, ulen);
3670 if (prog->aux->func_cnt) {
3671 for (i = 0; i < ulen; i++) {
3672 if (copy_to_user(user_prog_tags[i],
3673 prog->aux->func[i]->tag,
3674 BPF_TAG_SIZE))
3675 return -EFAULT;
3676 }
3677 } else {
3678 if (copy_to_user(user_prog_tags[0],
3679 prog->tag, BPF_TAG_SIZE))
3680 return -EFAULT;
3681 }
3682 }
3683
3684 done:
3685 if (copy_to_user(uinfo, &info, info_len) ||
3686 put_user(info_len, &uattr->info.info_len))
3687 return -EFAULT;
3688
3689 return 0;
3690 }
3691
bpf_map_get_info_by_fd(struct file * file,struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)3692 static int bpf_map_get_info_by_fd(struct file *file,
3693 struct bpf_map *map,
3694 const union bpf_attr *attr,
3695 union bpf_attr __user *uattr)
3696 {
3697 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3698 struct bpf_map_info info;
3699 u32 info_len = attr->info.info_len;
3700 int err;
3701
3702 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3703 if (err)
3704 return err;
3705 info_len = min_t(u32, sizeof(info), info_len);
3706
3707 memset(&info, 0, sizeof(info));
3708 info.type = map->map_type;
3709 info.id = map->id;
3710 info.key_size = map->key_size;
3711 info.value_size = map->value_size;
3712 info.max_entries = map->max_entries;
3713 info.map_flags = map->map_flags;
3714 memcpy(info.name, map->name, sizeof(map->name));
3715
3716 if (map->btf) {
3717 info.btf_id = btf_id(map->btf);
3718 info.btf_key_type_id = map->btf_key_type_id;
3719 info.btf_value_type_id = map->btf_value_type_id;
3720 }
3721 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
3722
3723 if (bpf_map_is_dev_bound(map)) {
3724 err = bpf_map_offload_info_fill(&info, map);
3725 if (err)
3726 return err;
3727 }
3728
3729 if (copy_to_user(uinfo, &info, info_len) ||
3730 put_user(info_len, &uattr->info.info_len))
3731 return -EFAULT;
3732
3733 return 0;
3734 }
3735
bpf_btf_get_info_by_fd(struct file * file,struct btf * btf,const union bpf_attr * attr,union bpf_attr __user * uattr)3736 static int bpf_btf_get_info_by_fd(struct file *file,
3737 struct btf *btf,
3738 const union bpf_attr *attr,
3739 union bpf_attr __user *uattr)
3740 {
3741 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3742 u32 info_len = attr->info.info_len;
3743 int err;
3744
3745 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
3746 if (err)
3747 return err;
3748
3749 return btf_get_info_by_fd(btf, attr, uattr);
3750 }
3751
bpf_link_get_info_by_fd(struct file * file,struct bpf_link * link,const union bpf_attr * attr,union bpf_attr __user * uattr)3752 static int bpf_link_get_info_by_fd(struct file *file,
3753 struct bpf_link *link,
3754 const union bpf_attr *attr,
3755 union bpf_attr __user *uattr)
3756 {
3757 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3758 struct bpf_link_info info;
3759 u32 info_len = attr->info.info_len;
3760 int err;
3761
3762 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3763 if (err)
3764 return err;
3765 info_len = min_t(u32, sizeof(info), info_len);
3766
3767 memset(&info, 0, sizeof(info));
3768 if (copy_from_user(&info, uinfo, info_len))
3769 return -EFAULT;
3770
3771 info.type = link->type;
3772 info.id = link->id;
3773 info.prog_id = link->prog->aux->id;
3774
3775 if (link->ops->fill_link_info) {
3776 err = link->ops->fill_link_info(link, &info);
3777 if (err)
3778 return err;
3779 }
3780
3781 if (copy_to_user(uinfo, &info, info_len) ||
3782 put_user(info_len, &uattr->info.info_len))
3783 return -EFAULT;
3784
3785 return 0;
3786 }
3787
3788
3789 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3790
bpf_obj_get_info_by_fd(const union bpf_attr * attr,union bpf_attr __user * uattr)3791 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3792 union bpf_attr __user *uattr)
3793 {
3794 int ufd = attr->info.bpf_fd;
3795 struct fd f;
3796 int err;
3797
3798 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3799 return -EINVAL;
3800
3801 f = fdget(ufd);
3802 if (!f.file)
3803 return -EBADFD;
3804
3805 if (f.file->f_op == &bpf_prog_fops)
3806 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
3807 uattr);
3808 else if (f.file->f_op == &bpf_map_fops)
3809 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
3810 uattr);
3811 else if (f.file->f_op == &btf_fops)
3812 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
3813 else if (f.file->f_op == &bpf_link_fops)
3814 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
3815 attr, uattr);
3816 else
3817 err = -EINVAL;
3818
3819 fdput(f);
3820 return err;
3821 }
3822
3823 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3824
bpf_btf_load(const union bpf_attr * attr)3825 static int bpf_btf_load(const union bpf_attr *attr)
3826 {
3827 if (CHECK_ATTR(BPF_BTF_LOAD))
3828 return -EINVAL;
3829
3830 if (!bpf_capable())
3831 return -EPERM;
3832
3833 return btf_new_fd(attr);
3834 }
3835
3836 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3837
bpf_btf_get_fd_by_id(const union bpf_attr * attr)3838 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3839 {
3840 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3841 return -EINVAL;
3842
3843 if (!capable(CAP_SYS_ADMIN))
3844 return -EPERM;
3845
3846 return btf_get_fd_by_id(attr->btf_id);
3847 }
3848
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)3849 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3850 union bpf_attr __user *uattr,
3851 u32 prog_id, u32 fd_type,
3852 const char *buf, u64 probe_offset,
3853 u64 probe_addr)
3854 {
3855 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3856 u32 len = buf ? strlen(buf) : 0, input_len;
3857 int err = 0;
3858
3859 if (put_user(len, &uattr->task_fd_query.buf_len))
3860 return -EFAULT;
3861 input_len = attr->task_fd_query.buf_len;
3862 if (input_len && ubuf) {
3863 if (!len) {
3864 /* nothing to copy, just make ubuf NULL terminated */
3865 char zero = '\0';
3866
3867 if (put_user(zero, ubuf))
3868 return -EFAULT;
3869 } else if (input_len >= len + 1) {
3870 /* ubuf can hold the string with NULL terminator */
3871 if (copy_to_user(ubuf, buf, len + 1))
3872 return -EFAULT;
3873 } else {
3874 /* ubuf cannot hold the string with NULL terminator,
3875 * do a partial copy with NULL terminator.
3876 */
3877 char zero = '\0';
3878
3879 err = -ENOSPC;
3880 if (copy_to_user(ubuf, buf, input_len - 1))
3881 return -EFAULT;
3882 if (put_user(zero, ubuf + input_len - 1))
3883 return -EFAULT;
3884 }
3885 }
3886
3887 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3888 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3889 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3890 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3891 return -EFAULT;
3892
3893 return err;
3894 }
3895
3896 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3897
bpf_task_fd_query(const union bpf_attr * attr,union bpf_attr __user * uattr)3898 static int bpf_task_fd_query(const union bpf_attr *attr,
3899 union bpf_attr __user *uattr)
3900 {
3901 pid_t pid = attr->task_fd_query.pid;
3902 u32 fd = attr->task_fd_query.fd;
3903 const struct perf_event *event;
3904 struct files_struct *files;
3905 struct task_struct *task;
3906 struct file *file;
3907 int err;
3908
3909 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3910 return -EINVAL;
3911
3912 if (!capable(CAP_SYS_ADMIN))
3913 return -EPERM;
3914
3915 if (attr->task_fd_query.flags != 0)
3916 return -EINVAL;
3917
3918 rcu_read_lock();
3919 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3920 rcu_read_unlock();
3921 if (!task)
3922 return -ENOENT;
3923
3924 files = get_files_struct(task);
3925 put_task_struct(task);
3926 if (!files)
3927 return -ENOENT;
3928
3929 err = 0;
3930 spin_lock(&files->file_lock);
3931 file = fcheck_files(files, fd);
3932 if (!file)
3933 err = -EBADF;
3934 else
3935 get_file(file);
3936 spin_unlock(&files->file_lock);
3937 put_files_struct(files);
3938
3939 if (err)
3940 goto out;
3941
3942 if (file->f_op == &bpf_link_fops) {
3943 struct bpf_link *link = file->private_data;
3944
3945 if (link->ops == &bpf_raw_tp_link_lops) {
3946 struct bpf_raw_tp_link *raw_tp =
3947 container_of(link, struct bpf_raw_tp_link, link);
3948 struct bpf_raw_event_map *btp = raw_tp->btp;
3949
3950 err = bpf_task_fd_query_copy(attr, uattr,
3951 raw_tp->link.prog->aux->id,
3952 BPF_FD_TYPE_RAW_TRACEPOINT,
3953 btp->tp->name, 0, 0);
3954 goto put_file;
3955 }
3956 goto out_not_supp;
3957 }
3958
3959 event = perf_get_event(file);
3960 if (!IS_ERR(event)) {
3961 u64 probe_offset, probe_addr;
3962 u32 prog_id, fd_type;
3963 const char *buf;
3964
3965 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3966 &buf, &probe_offset,
3967 &probe_addr);
3968 if (!err)
3969 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3970 fd_type, buf,
3971 probe_offset,
3972 probe_addr);
3973 goto put_file;
3974 }
3975
3976 out_not_supp:
3977 err = -ENOTSUPP;
3978 put_file:
3979 fput(file);
3980 out:
3981 return err;
3982 }
3983
3984 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
3985
3986 #define BPF_DO_BATCH(fn) \
3987 do { \
3988 if (!fn) { \
3989 err = -ENOTSUPP; \
3990 goto err_put; \
3991 } \
3992 err = fn(map, attr, uattr); \
3993 } while (0)
3994
bpf_map_do_batch(const union bpf_attr * attr,union bpf_attr __user * uattr,int cmd)3995 static int bpf_map_do_batch(const union bpf_attr *attr,
3996 union bpf_attr __user *uattr,
3997 int cmd)
3998 {
3999 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH ||
4000 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH;
4001 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH;
4002 struct bpf_map *map;
4003 int err, ufd;
4004 struct fd f;
4005
4006 if (CHECK_ATTR(BPF_MAP_BATCH))
4007 return -EINVAL;
4008
4009 ufd = attr->batch.map_fd;
4010 f = fdget(ufd);
4011 map = __bpf_map_get(f);
4012 if (IS_ERR(map))
4013 return PTR_ERR(map);
4014 if (has_write)
4015 bpf_map_write_active_inc(map);
4016 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4017 err = -EPERM;
4018 goto err_put;
4019 }
4020 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4021 err = -EPERM;
4022 goto err_put;
4023 }
4024
4025 if (cmd == BPF_MAP_LOOKUP_BATCH)
4026 BPF_DO_BATCH(map->ops->map_lookup_batch);
4027 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4028 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
4029 else if (cmd == BPF_MAP_UPDATE_BATCH)
4030 BPF_DO_BATCH(map->ops->map_update_batch);
4031 else
4032 BPF_DO_BATCH(map->ops->map_delete_batch);
4033 err_put:
4034 if (has_write)
4035 bpf_map_write_active_dec(map);
4036 fdput(f);
4037 return err;
4038 }
4039
tracing_bpf_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)4040 static int tracing_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
4041 {
4042 if (attr->link_create.attach_type != prog->expected_attach_type)
4043 return -EINVAL;
4044
4045 if (prog->expected_attach_type == BPF_TRACE_ITER)
4046 return bpf_iter_link_attach(attr, prog);
4047 else if (prog->type == BPF_PROG_TYPE_EXT)
4048 return bpf_tracing_prog_attach(prog,
4049 attr->link_create.target_fd,
4050 attr->link_create.target_btf_id);
4051 return -EINVAL;
4052 }
4053
4054 #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
link_create(union bpf_attr * attr)4055 static int link_create(union bpf_attr *attr)
4056 {
4057 enum bpf_prog_type ptype;
4058 struct bpf_prog *prog;
4059 int ret;
4060
4061 if (CHECK_ATTR(BPF_LINK_CREATE))
4062 return -EINVAL;
4063
4064 prog = bpf_prog_get(attr->link_create.prog_fd);
4065 if (IS_ERR(prog))
4066 return PTR_ERR(prog);
4067
4068 ret = bpf_prog_attach_check_attach_type(prog,
4069 attr->link_create.attach_type);
4070 if (ret)
4071 goto out;
4072
4073 if (prog->type == BPF_PROG_TYPE_EXT) {
4074 ret = tracing_bpf_link_attach(attr, prog);
4075 goto out;
4076 }
4077
4078 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4079 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4080 ret = -EINVAL;
4081 goto out;
4082 }
4083
4084 switch (ptype) {
4085 case BPF_PROG_TYPE_CGROUP_SKB:
4086 case BPF_PROG_TYPE_CGROUP_SOCK:
4087 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4088 case BPF_PROG_TYPE_SOCK_OPS:
4089 case BPF_PROG_TYPE_CGROUP_DEVICE:
4090 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4091 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4092 ret = cgroup_bpf_link_attach(attr, prog);
4093 break;
4094 case BPF_PROG_TYPE_TRACING:
4095 ret = tracing_bpf_link_attach(attr, prog);
4096 break;
4097 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4098 case BPF_PROG_TYPE_SK_LOOKUP:
4099 ret = netns_bpf_link_create(attr, prog);
4100 break;
4101 #ifdef CONFIG_NET
4102 case BPF_PROG_TYPE_XDP:
4103 ret = bpf_xdp_link_attach(attr, prog);
4104 break;
4105 #endif
4106 default:
4107 ret = -EINVAL;
4108 }
4109
4110 out:
4111 if (ret < 0)
4112 bpf_prog_put(prog);
4113 return ret;
4114 }
4115
4116 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4117
link_update(union bpf_attr * attr)4118 static int link_update(union bpf_attr *attr)
4119 {
4120 struct bpf_prog *old_prog = NULL, *new_prog;
4121 struct bpf_link *link;
4122 u32 flags;
4123 int ret;
4124
4125 if (CHECK_ATTR(BPF_LINK_UPDATE))
4126 return -EINVAL;
4127
4128 flags = attr->link_update.flags;
4129 if (flags & ~BPF_F_REPLACE)
4130 return -EINVAL;
4131
4132 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4133 if (IS_ERR(link))
4134 return PTR_ERR(link);
4135
4136 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4137 if (IS_ERR(new_prog)) {
4138 ret = PTR_ERR(new_prog);
4139 goto out_put_link;
4140 }
4141
4142 if (flags & BPF_F_REPLACE) {
4143 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4144 if (IS_ERR(old_prog)) {
4145 ret = PTR_ERR(old_prog);
4146 old_prog = NULL;
4147 goto out_put_progs;
4148 }
4149 } else if (attr->link_update.old_prog_fd) {
4150 ret = -EINVAL;
4151 goto out_put_progs;
4152 }
4153
4154 if (link->ops->update_prog)
4155 ret = link->ops->update_prog(link, new_prog, old_prog);
4156 else
4157 ret = -EINVAL;
4158
4159 out_put_progs:
4160 if (old_prog)
4161 bpf_prog_put(old_prog);
4162 if (ret)
4163 bpf_prog_put(new_prog);
4164 out_put_link:
4165 bpf_link_put(link);
4166 return ret;
4167 }
4168
4169 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4170
link_detach(union bpf_attr * attr)4171 static int link_detach(union bpf_attr *attr)
4172 {
4173 struct bpf_link *link;
4174 int ret;
4175
4176 if (CHECK_ATTR(BPF_LINK_DETACH))
4177 return -EINVAL;
4178
4179 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4180 if (IS_ERR(link))
4181 return PTR_ERR(link);
4182
4183 if (link->ops->detach)
4184 ret = link->ops->detach(link);
4185 else
4186 ret = -EOPNOTSUPP;
4187
4188 bpf_link_put(link);
4189 return ret;
4190 }
4191
bpf_link_inc_not_zero(struct bpf_link * link)4192 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4193 {
4194 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4195 }
4196
bpf_link_by_id(u32 id)4197 struct bpf_link *bpf_link_by_id(u32 id)
4198 {
4199 struct bpf_link *link;
4200
4201 if (!id)
4202 return ERR_PTR(-ENOENT);
4203
4204 spin_lock_bh(&link_idr_lock);
4205 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4206 link = idr_find(&link_idr, id);
4207 if (link) {
4208 if (link->id)
4209 link = bpf_link_inc_not_zero(link);
4210 else
4211 link = ERR_PTR(-EAGAIN);
4212 } else {
4213 link = ERR_PTR(-ENOENT);
4214 }
4215 spin_unlock_bh(&link_idr_lock);
4216 return link;
4217 }
4218
4219 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4220
bpf_link_get_fd_by_id(const union bpf_attr * attr)4221 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4222 {
4223 struct bpf_link *link;
4224 u32 id = attr->link_id;
4225 int fd;
4226
4227 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4228 return -EINVAL;
4229
4230 if (!capable(CAP_SYS_ADMIN))
4231 return -EPERM;
4232
4233 link = bpf_link_by_id(id);
4234 if (IS_ERR(link))
4235 return PTR_ERR(link);
4236
4237 fd = bpf_link_new_fd(link);
4238 if (fd < 0)
4239 bpf_link_put(link);
4240
4241 return fd;
4242 }
4243
4244 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4245
bpf_stats_release(struct inode * inode,struct file * file)4246 static int bpf_stats_release(struct inode *inode, struct file *file)
4247 {
4248 mutex_lock(&bpf_stats_enabled_mutex);
4249 static_key_slow_dec(&bpf_stats_enabled_key.key);
4250 mutex_unlock(&bpf_stats_enabled_mutex);
4251 return 0;
4252 }
4253
4254 static const struct file_operations bpf_stats_fops = {
4255 .release = bpf_stats_release,
4256 };
4257
bpf_enable_runtime_stats(void)4258 static int bpf_enable_runtime_stats(void)
4259 {
4260 int fd;
4261
4262 mutex_lock(&bpf_stats_enabled_mutex);
4263
4264 /* Set a very high limit to avoid overflow */
4265 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4266 mutex_unlock(&bpf_stats_enabled_mutex);
4267 return -EBUSY;
4268 }
4269
4270 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4271 if (fd >= 0)
4272 static_key_slow_inc(&bpf_stats_enabled_key.key);
4273
4274 mutex_unlock(&bpf_stats_enabled_mutex);
4275 return fd;
4276 }
4277
4278 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4279
bpf_enable_stats(union bpf_attr * attr)4280 static int bpf_enable_stats(union bpf_attr *attr)
4281 {
4282
4283 if (CHECK_ATTR(BPF_ENABLE_STATS))
4284 return -EINVAL;
4285
4286 if (!capable(CAP_SYS_ADMIN))
4287 return -EPERM;
4288
4289 switch (attr->enable_stats.type) {
4290 case BPF_STATS_RUN_TIME:
4291 return bpf_enable_runtime_stats();
4292 default:
4293 break;
4294 }
4295 return -EINVAL;
4296 }
4297
4298 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4299
bpf_iter_create(union bpf_attr * attr)4300 static int bpf_iter_create(union bpf_attr *attr)
4301 {
4302 struct bpf_link *link;
4303 int err;
4304
4305 if (CHECK_ATTR(BPF_ITER_CREATE))
4306 return -EINVAL;
4307
4308 if (attr->iter_create.flags)
4309 return -EINVAL;
4310
4311 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4312 if (IS_ERR(link))
4313 return PTR_ERR(link);
4314
4315 err = bpf_iter_new_fd(link);
4316 bpf_link_put(link);
4317
4318 return err;
4319 }
4320
4321 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4322
bpf_prog_bind_map(union bpf_attr * attr)4323 static int bpf_prog_bind_map(union bpf_attr *attr)
4324 {
4325 struct bpf_prog *prog;
4326 struct bpf_map *map;
4327 struct bpf_map **used_maps_old, **used_maps_new;
4328 int i, ret = 0;
4329
4330 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4331 return -EINVAL;
4332
4333 if (attr->prog_bind_map.flags)
4334 return -EINVAL;
4335
4336 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4337 if (IS_ERR(prog))
4338 return PTR_ERR(prog);
4339
4340 map = bpf_map_get(attr->prog_bind_map.map_fd);
4341 if (IS_ERR(map)) {
4342 ret = PTR_ERR(map);
4343 goto out_prog_put;
4344 }
4345
4346 mutex_lock(&prog->aux->used_maps_mutex);
4347
4348 used_maps_old = prog->aux->used_maps;
4349
4350 for (i = 0; i < prog->aux->used_map_cnt; i++)
4351 if (used_maps_old[i] == map) {
4352 bpf_map_put(map);
4353 goto out_unlock;
4354 }
4355
4356 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4357 sizeof(used_maps_new[0]),
4358 GFP_KERNEL);
4359 if (!used_maps_new) {
4360 ret = -ENOMEM;
4361 goto out_unlock;
4362 }
4363
4364 memcpy(used_maps_new, used_maps_old,
4365 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4366 used_maps_new[prog->aux->used_map_cnt] = map;
4367
4368 prog->aux->used_map_cnt++;
4369 prog->aux->used_maps = used_maps_new;
4370
4371 kfree(used_maps_old);
4372
4373 out_unlock:
4374 mutex_unlock(&prog->aux->used_maps_mutex);
4375
4376 if (ret)
4377 bpf_map_put(map);
4378 out_prog_put:
4379 bpf_prog_put(prog);
4380 return ret;
4381 }
4382
SYSCALL_DEFINE3(bpf,int,cmd,union bpf_attr __user *,uattr,unsigned int,size)4383 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4384 {
4385 union bpf_attr attr;
4386 int err;
4387
4388 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
4389 return -EPERM;
4390
4391 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4392 if (err)
4393 return err;
4394 size = min_t(u32, size, sizeof(attr));
4395
4396 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4397 memset(&attr, 0, sizeof(attr));
4398 if (copy_from_user(&attr, uattr, size) != 0)
4399 return -EFAULT;
4400
4401 err = security_bpf(cmd, &attr, size);
4402 if (err < 0)
4403 return err;
4404
4405 switch (cmd) {
4406 case BPF_MAP_CREATE:
4407 err = map_create(&attr);
4408 break;
4409 case BPF_MAP_LOOKUP_ELEM:
4410 err = map_lookup_elem(&attr);
4411 break;
4412 case BPF_MAP_UPDATE_ELEM:
4413 err = map_update_elem(&attr);
4414 break;
4415 case BPF_MAP_DELETE_ELEM:
4416 err = map_delete_elem(&attr);
4417 break;
4418 case BPF_MAP_GET_NEXT_KEY:
4419 err = map_get_next_key(&attr);
4420 break;
4421 case BPF_MAP_FREEZE:
4422 err = map_freeze(&attr);
4423 break;
4424 case BPF_PROG_LOAD:
4425 err = bpf_prog_load(&attr, uattr);
4426 break;
4427 case BPF_OBJ_PIN:
4428 err = bpf_obj_pin(&attr);
4429 break;
4430 case BPF_OBJ_GET:
4431 err = bpf_obj_get(&attr);
4432 break;
4433 case BPF_PROG_ATTACH:
4434 err = bpf_prog_attach(&attr);
4435 break;
4436 case BPF_PROG_DETACH:
4437 err = bpf_prog_detach(&attr);
4438 break;
4439 case BPF_PROG_QUERY:
4440 err = bpf_prog_query(&attr, uattr);
4441 break;
4442 case BPF_PROG_TEST_RUN:
4443 err = bpf_prog_test_run(&attr, uattr);
4444 break;
4445 case BPF_PROG_GET_NEXT_ID:
4446 err = bpf_obj_get_next_id(&attr, uattr,
4447 &prog_idr, &prog_idr_lock);
4448 break;
4449 case BPF_MAP_GET_NEXT_ID:
4450 err = bpf_obj_get_next_id(&attr, uattr,
4451 &map_idr, &map_idr_lock);
4452 break;
4453 case BPF_BTF_GET_NEXT_ID:
4454 err = bpf_obj_get_next_id(&attr, uattr,
4455 &btf_idr, &btf_idr_lock);
4456 break;
4457 case BPF_PROG_GET_FD_BY_ID:
4458 err = bpf_prog_get_fd_by_id(&attr);
4459 break;
4460 case BPF_MAP_GET_FD_BY_ID:
4461 err = bpf_map_get_fd_by_id(&attr);
4462 break;
4463 case BPF_OBJ_GET_INFO_BY_FD:
4464 err = bpf_obj_get_info_by_fd(&attr, uattr);
4465 break;
4466 case BPF_RAW_TRACEPOINT_OPEN:
4467 err = bpf_raw_tracepoint_open(&attr);
4468 break;
4469 case BPF_BTF_LOAD:
4470 err = bpf_btf_load(&attr);
4471 break;
4472 case BPF_BTF_GET_FD_BY_ID:
4473 err = bpf_btf_get_fd_by_id(&attr);
4474 break;
4475 case BPF_TASK_FD_QUERY:
4476 err = bpf_task_fd_query(&attr, uattr);
4477 break;
4478 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4479 err = map_lookup_and_delete_elem(&attr);
4480 break;
4481 case BPF_MAP_LOOKUP_BATCH:
4482 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
4483 break;
4484 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4485 err = bpf_map_do_batch(&attr, uattr,
4486 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4487 break;
4488 case BPF_MAP_UPDATE_BATCH:
4489 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
4490 break;
4491 case BPF_MAP_DELETE_BATCH:
4492 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);
4493 break;
4494 case BPF_LINK_CREATE:
4495 err = link_create(&attr);
4496 break;
4497 case BPF_LINK_UPDATE:
4498 err = link_update(&attr);
4499 break;
4500 case BPF_LINK_GET_FD_BY_ID:
4501 err = bpf_link_get_fd_by_id(&attr);
4502 break;
4503 case BPF_LINK_GET_NEXT_ID:
4504 err = bpf_obj_get_next_id(&attr, uattr,
4505 &link_idr, &link_idr_lock);
4506 break;
4507 case BPF_ENABLE_STATS:
4508 err = bpf_enable_stats(&attr);
4509 break;
4510 case BPF_ITER_CREATE:
4511 err = bpf_iter_create(&attr);
4512 break;
4513 case BPF_LINK_DETACH:
4514 err = link_detach(&attr);
4515 break;
4516 case BPF_PROG_BIND_MAP:
4517 err = bpf_prog_bind_map(&attr);
4518 break;
4519 default:
4520 err = -EINVAL;
4521 break;
4522 }
4523
4524 return err;
4525 }
4526