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 enum bpf_link_type type = link->type;
2403 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2404
2405 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2406 if (type < ARRAY_SIZE(bpf_link_type_strs) && bpf_link_type_strs[type]) {
2407 seq_printf(m, "link_type:\t%s\n", bpf_link_type_strs[type]);
2408 } else {
2409 WARN_ONCE(1, "missing BPF_LINK_TYPE(...) for link type %u\n", type);
2410 seq_printf(m, "link_type:\t<%u>\n", type);
2411 }
2412 seq_printf(m,
2413 "link_id:\t%u\n"
2414 "prog_tag:\t%s\n"
2415 "prog_id:\t%u\n",
2416 link->id,
2417 prog_tag,
2418 prog->aux->id);
2419 if (link->ops->show_fdinfo)
2420 link->ops->show_fdinfo(link, m);
2421 }
2422 #endif
2423
2424 static const struct file_operations bpf_link_fops = {
2425 #ifdef CONFIG_PROC_FS
2426 .show_fdinfo = bpf_link_show_fdinfo,
2427 #endif
2428 .release = bpf_link_release,
2429 .read = bpf_dummy_read,
2430 .write = bpf_dummy_write,
2431 };
2432
bpf_link_alloc_id(struct bpf_link * link)2433 static int bpf_link_alloc_id(struct bpf_link *link)
2434 {
2435 int id;
2436
2437 idr_preload(GFP_KERNEL);
2438 spin_lock_bh(&link_idr_lock);
2439 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2440 spin_unlock_bh(&link_idr_lock);
2441 idr_preload_end();
2442
2443 return id;
2444 }
2445
2446 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2447 * reserving unused FD and allocating ID from link_idr. This is to be paired
2448 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2449 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2450 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2451 * transient state is passed around in struct bpf_link_primer.
2452 * This is preferred way to create and initialize bpf_link, especially when
2453 * there are complicated and expensive operations inbetween creating bpf_link
2454 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2455 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2456 * expensive (and potentially failing) roll back operations in a rare case
2457 * that file, FD, or ID can't be allocated.
2458 */
bpf_link_prime(struct bpf_link * link,struct bpf_link_primer * primer)2459 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2460 {
2461 struct file *file;
2462 int fd, id;
2463
2464 fd = get_unused_fd_flags(O_CLOEXEC);
2465 if (fd < 0)
2466 return fd;
2467
2468
2469 id = bpf_link_alloc_id(link);
2470 if (id < 0) {
2471 put_unused_fd(fd);
2472 return id;
2473 }
2474
2475 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2476 if (IS_ERR(file)) {
2477 bpf_link_free_id(id);
2478 put_unused_fd(fd);
2479 return PTR_ERR(file);
2480 }
2481
2482 primer->link = link;
2483 primer->file = file;
2484 primer->fd = fd;
2485 primer->id = id;
2486 return 0;
2487 }
2488
bpf_link_settle(struct bpf_link_primer * primer)2489 int bpf_link_settle(struct bpf_link_primer *primer)
2490 {
2491 /* make bpf_link fetchable by ID */
2492 spin_lock_bh(&link_idr_lock);
2493 primer->link->id = primer->id;
2494 spin_unlock_bh(&link_idr_lock);
2495 /* make bpf_link fetchable by FD */
2496 fd_install(primer->fd, primer->file);
2497 /* pass through installed FD */
2498 return primer->fd;
2499 }
2500
bpf_link_new_fd(struct bpf_link * link)2501 int bpf_link_new_fd(struct bpf_link *link)
2502 {
2503 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2504 }
2505
bpf_link_get_from_fd(u32 ufd)2506 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2507 {
2508 struct fd f = fdget(ufd);
2509 struct bpf_link *link;
2510
2511 if (!f.file)
2512 return ERR_PTR(-EBADF);
2513 if (f.file->f_op != &bpf_link_fops) {
2514 fdput(f);
2515 return ERR_PTR(-EINVAL);
2516 }
2517
2518 link = f.file->private_data;
2519 bpf_link_inc(link);
2520 fdput(f);
2521
2522 return link;
2523 }
2524
2525 struct bpf_tracing_link {
2526 struct bpf_link link;
2527 enum bpf_attach_type attach_type;
2528 struct bpf_trampoline *trampoline;
2529 struct bpf_prog *tgt_prog;
2530 };
2531
bpf_tracing_link_release(struct bpf_link * link)2532 static void bpf_tracing_link_release(struct bpf_link *link)
2533 {
2534 struct bpf_tracing_link *tr_link =
2535 container_of(link, struct bpf_tracing_link, link);
2536
2537 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2538 tr_link->trampoline));
2539
2540 bpf_trampoline_put(tr_link->trampoline);
2541
2542 /* tgt_prog is NULL if target is a kernel function */
2543 if (tr_link->tgt_prog)
2544 bpf_prog_put(tr_link->tgt_prog);
2545 }
2546
bpf_tracing_link_dealloc(struct bpf_link * link)2547 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2548 {
2549 struct bpf_tracing_link *tr_link =
2550 container_of(link, struct bpf_tracing_link, link);
2551
2552 kfree(tr_link);
2553 }
2554
bpf_tracing_link_show_fdinfo(const struct bpf_link * link,struct seq_file * seq)2555 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2556 struct seq_file *seq)
2557 {
2558 struct bpf_tracing_link *tr_link =
2559 container_of(link, struct bpf_tracing_link, link);
2560
2561 seq_printf(seq,
2562 "attach_type:\t%d\n",
2563 tr_link->attach_type);
2564 }
2565
bpf_tracing_link_fill_link_info(const struct bpf_link * link,struct bpf_link_info * info)2566 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2567 struct bpf_link_info *info)
2568 {
2569 struct bpf_tracing_link *tr_link =
2570 container_of(link, struct bpf_tracing_link, link);
2571
2572 info->tracing.attach_type = tr_link->attach_type;
2573
2574 return 0;
2575 }
2576
2577 static const struct bpf_link_ops bpf_tracing_link_lops = {
2578 .release = bpf_tracing_link_release,
2579 .dealloc = bpf_tracing_link_dealloc,
2580 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2581 .fill_link_info = bpf_tracing_link_fill_link_info,
2582 };
2583
bpf_tracing_prog_attach(struct bpf_prog * prog,int tgt_prog_fd,u32 btf_id)2584 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2585 int tgt_prog_fd,
2586 u32 btf_id)
2587 {
2588 struct bpf_link_primer link_primer;
2589 struct bpf_prog *tgt_prog = NULL;
2590 struct bpf_trampoline *tr = NULL;
2591 struct bpf_tracing_link *link;
2592 u64 key = 0;
2593 int err;
2594
2595 switch (prog->type) {
2596 case BPF_PROG_TYPE_TRACING:
2597 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2598 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2599 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2600 err = -EINVAL;
2601 goto out_put_prog;
2602 }
2603 break;
2604 case BPF_PROG_TYPE_EXT:
2605 if (prog->expected_attach_type != 0) {
2606 err = -EINVAL;
2607 goto out_put_prog;
2608 }
2609 break;
2610 case BPF_PROG_TYPE_LSM:
2611 if (prog->expected_attach_type != BPF_LSM_MAC) {
2612 err = -EINVAL;
2613 goto out_put_prog;
2614 }
2615 break;
2616 default:
2617 err = -EINVAL;
2618 goto out_put_prog;
2619 }
2620
2621 if (!!tgt_prog_fd != !!btf_id) {
2622 err = -EINVAL;
2623 goto out_put_prog;
2624 }
2625
2626 if (tgt_prog_fd) {
2627 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2628 if (prog->type != BPF_PROG_TYPE_EXT) {
2629 err = -EINVAL;
2630 goto out_put_prog;
2631 }
2632
2633 tgt_prog = bpf_prog_get(tgt_prog_fd);
2634 if (IS_ERR(tgt_prog)) {
2635 err = PTR_ERR(tgt_prog);
2636 tgt_prog = NULL;
2637 goto out_put_prog;
2638 }
2639
2640 key = bpf_trampoline_compute_key(tgt_prog, btf_id);
2641 }
2642
2643 link = kzalloc(sizeof(*link), GFP_USER);
2644 if (!link) {
2645 err = -ENOMEM;
2646 goto out_put_prog;
2647 }
2648 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2649 &bpf_tracing_link_lops, prog);
2650 link->attach_type = prog->expected_attach_type;
2651
2652 mutex_lock(&prog->aux->dst_mutex);
2653
2654 /* There are a few possible cases here:
2655 *
2656 * - if prog->aux->dst_trampoline is set, the program was just loaded
2657 * and not yet attached to anything, so we can use the values stored
2658 * in prog->aux
2659 *
2660 * - if prog->aux->dst_trampoline is NULL, the program has already been
2661 * attached to a target and its initial target was cleared (below)
2662 *
2663 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2664 * target_btf_id using the link_create API.
2665 *
2666 * - if tgt_prog == NULL when this function was called using the old
2667 * raw_tracepoint_open API, and we need a target from prog->aux
2668 *
2669 * The combination of no saved target in prog->aux, and no target
2670 * specified on load is illegal, and we reject that here.
2671 */
2672 if (!prog->aux->dst_trampoline && !tgt_prog) {
2673 err = -ENOENT;
2674 goto out_unlock;
2675 }
2676
2677 if (!prog->aux->dst_trampoline ||
2678 (key && key != prog->aux->dst_trampoline->key)) {
2679 /* If there is no saved target, or the specified target is
2680 * different from the destination specified at load time, we
2681 * need a new trampoline and a check for compatibility
2682 */
2683 struct bpf_attach_target_info tgt_info = {};
2684
2685 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2686 &tgt_info);
2687 if (err)
2688 goto out_unlock;
2689
2690 tr = bpf_trampoline_get(key, &tgt_info);
2691 if (!tr) {
2692 err = -ENOMEM;
2693 goto out_unlock;
2694 }
2695 } else {
2696 /* The caller didn't specify a target, or the target was the
2697 * same as the destination supplied during program load. This
2698 * means we can reuse the trampoline and reference from program
2699 * load time, and there is no need to allocate a new one. This
2700 * can only happen once for any program, as the saved values in
2701 * prog->aux are cleared below.
2702 */
2703 tr = prog->aux->dst_trampoline;
2704 tgt_prog = prog->aux->dst_prog;
2705 }
2706
2707 err = bpf_link_prime(&link->link, &link_primer);
2708 if (err)
2709 goto out_unlock;
2710
2711 err = bpf_trampoline_link_prog(prog, tr);
2712 if (err) {
2713 bpf_link_cleanup(&link_primer);
2714 link = NULL;
2715 goto out_unlock;
2716 }
2717
2718 link->tgt_prog = tgt_prog;
2719 link->trampoline = tr;
2720
2721 /* Always clear the trampoline and target prog from prog->aux to make
2722 * sure the original attach destination is not kept alive after a
2723 * program is (re-)attached to another target.
2724 */
2725 if (prog->aux->dst_prog &&
2726 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2727 /* got extra prog ref from syscall, or attaching to different prog */
2728 bpf_prog_put(prog->aux->dst_prog);
2729 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2730 /* we allocated a new trampoline, so free the old one */
2731 bpf_trampoline_put(prog->aux->dst_trampoline);
2732
2733 prog->aux->dst_prog = NULL;
2734 prog->aux->dst_trampoline = NULL;
2735 mutex_unlock(&prog->aux->dst_mutex);
2736
2737 return bpf_link_settle(&link_primer);
2738 out_unlock:
2739 if (tr && tr != prog->aux->dst_trampoline)
2740 bpf_trampoline_put(tr);
2741 mutex_unlock(&prog->aux->dst_mutex);
2742 kfree(link);
2743 out_put_prog:
2744 if (tgt_prog_fd && tgt_prog)
2745 bpf_prog_put(tgt_prog);
2746 return err;
2747 }
2748
2749 struct bpf_raw_tp_link {
2750 struct bpf_link link;
2751 struct bpf_raw_event_map *btp;
2752 };
2753
bpf_raw_tp_link_release(struct bpf_link * link)2754 static void bpf_raw_tp_link_release(struct bpf_link *link)
2755 {
2756 struct bpf_raw_tp_link *raw_tp =
2757 container_of(link, struct bpf_raw_tp_link, link);
2758
2759 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
2760 bpf_put_raw_tracepoint(raw_tp->btp);
2761 }
2762
bpf_raw_tp_link_dealloc(struct bpf_link * link)2763 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2764 {
2765 struct bpf_raw_tp_link *raw_tp =
2766 container_of(link, struct bpf_raw_tp_link, link);
2767
2768 kfree(raw_tp);
2769 }
2770
bpf_raw_tp_link_show_fdinfo(const struct bpf_link * link,struct seq_file * seq)2771 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2772 struct seq_file *seq)
2773 {
2774 struct bpf_raw_tp_link *raw_tp_link =
2775 container_of(link, struct bpf_raw_tp_link, link);
2776
2777 seq_printf(seq,
2778 "tp_name:\t%s\n",
2779 raw_tp_link->btp->tp->name);
2780 }
2781
bpf_raw_tp_link_fill_link_info(const struct bpf_link * link,struct bpf_link_info * info)2782 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2783 struct bpf_link_info *info)
2784 {
2785 struct bpf_raw_tp_link *raw_tp_link =
2786 container_of(link, struct bpf_raw_tp_link, link);
2787 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2788 const char *tp_name = raw_tp_link->btp->tp->name;
2789 u32 ulen = info->raw_tracepoint.tp_name_len;
2790 size_t tp_len = strlen(tp_name);
2791
2792 if (!ulen ^ !ubuf)
2793 return -EINVAL;
2794
2795 info->raw_tracepoint.tp_name_len = tp_len + 1;
2796
2797 if (!ubuf)
2798 return 0;
2799
2800 if (ulen >= tp_len + 1) {
2801 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2802 return -EFAULT;
2803 } else {
2804 char zero = '\0';
2805
2806 if (copy_to_user(ubuf, tp_name, ulen - 1))
2807 return -EFAULT;
2808 if (put_user(zero, ubuf + ulen - 1))
2809 return -EFAULT;
2810 return -ENOSPC;
2811 }
2812
2813 return 0;
2814 }
2815
2816 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
2817 .release = bpf_raw_tp_link_release,
2818 .dealloc = bpf_raw_tp_link_dealloc,
2819 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2820 .fill_link_info = bpf_raw_tp_link_fill_link_info,
2821 };
2822
2823 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2824
bpf_raw_tracepoint_open(const union bpf_attr * attr)2825 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2826 {
2827 struct bpf_link_primer link_primer;
2828 struct bpf_raw_tp_link *link;
2829 struct bpf_raw_event_map *btp;
2830 struct bpf_prog *prog;
2831 const char *tp_name;
2832 char buf[128];
2833 int err;
2834
2835 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2836 return -EINVAL;
2837
2838 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2839 if (IS_ERR(prog))
2840 return PTR_ERR(prog);
2841
2842 switch (prog->type) {
2843 case BPF_PROG_TYPE_TRACING:
2844 case BPF_PROG_TYPE_EXT:
2845 case BPF_PROG_TYPE_LSM:
2846 if (attr->raw_tracepoint.name) {
2847 /* The attach point for this category of programs
2848 * should be specified via btf_id during program load.
2849 */
2850 err = -EINVAL;
2851 goto out_put_prog;
2852 }
2853 if (prog->type == BPF_PROG_TYPE_TRACING &&
2854 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
2855 tp_name = prog->aux->attach_func_name;
2856 break;
2857 }
2858 err = bpf_tracing_prog_attach(prog, 0, 0);
2859 if (err >= 0)
2860 return err;
2861 goto out_put_prog;
2862 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2863 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2864 if (strncpy_from_user(buf,
2865 u64_to_user_ptr(attr->raw_tracepoint.name),
2866 sizeof(buf) - 1) < 0) {
2867 err = -EFAULT;
2868 goto out_put_prog;
2869 }
2870 buf[sizeof(buf) - 1] = 0;
2871 tp_name = buf;
2872 break;
2873 default:
2874 err = -EINVAL;
2875 goto out_put_prog;
2876 }
2877
2878 btp = bpf_get_raw_tracepoint(tp_name);
2879 if (!btp) {
2880 err = -ENOENT;
2881 goto out_put_prog;
2882 }
2883
2884 link = kzalloc(sizeof(*link), GFP_USER);
2885 if (!link) {
2886 err = -ENOMEM;
2887 goto out_put_btp;
2888 }
2889 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2890 &bpf_raw_tp_link_lops, prog);
2891 link->btp = btp;
2892
2893 err = bpf_link_prime(&link->link, &link_primer);
2894 if (err) {
2895 kfree(link);
2896 goto out_put_btp;
2897 }
2898
2899 err = bpf_probe_register(link->btp, prog);
2900 if (err) {
2901 bpf_link_cleanup(&link_primer);
2902 goto out_put_btp;
2903 }
2904
2905 return bpf_link_settle(&link_primer);
2906
2907 out_put_btp:
2908 bpf_put_raw_tracepoint(btp);
2909 out_put_prog:
2910 bpf_prog_put(prog);
2911 return err;
2912 }
2913
2914 static enum bpf_prog_type
attach_type_to_prog_type(enum bpf_attach_type attach_type)2915 attach_type_to_prog_type(enum bpf_attach_type attach_type)
2916 {
2917 switch (attach_type) {
2918 case BPF_CGROUP_INET_INGRESS:
2919 case BPF_CGROUP_INET_EGRESS:
2920 return BPF_PROG_TYPE_CGROUP_SKB;
2921 case BPF_CGROUP_INET_SOCK_CREATE:
2922 case BPF_CGROUP_INET_SOCK_RELEASE:
2923 case BPF_CGROUP_INET4_POST_BIND:
2924 case BPF_CGROUP_INET6_POST_BIND:
2925 return BPF_PROG_TYPE_CGROUP_SOCK;
2926 case BPF_CGROUP_INET4_BIND:
2927 case BPF_CGROUP_INET6_BIND:
2928 case BPF_CGROUP_INET4_CONNECT:
2929 case BPF_CGROUP_INET6_CONNECT:
2930 case BPF_CGROUP_INET4_GETPEERNAME:
2931 case BPF_CGROUP_INET6_GETPEERNAME:
2932 case BPF_CGROUP_INET4_GETSOCKNAME:
2933 case BPF_CGROUP_INET6_GETSOCKNAME:
2934 case BPF_CGROUP_UDP4_SENDMSG:
2935 case BPF_CGROUP_UDP6_SENDMSG:
2936 case BPF_CGROUP_UDP4_RECVMSG:
2937 case BPF_CGROUP_UDP6_RECVMSG:
2938 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2939 case BPF_CGROUP_SOCK_OPS:
2940 return BPF_PROG_TYPE_SOCK_OPS;
2941 case BPF_CGROUP_DEVICE:
2942 return BPF_PROG_TYPE_CGROUP_DEVICE;
2943 case BPF_SK_MSG_VERDICT:
2944 return BPF_PROG_TYPE_SK_MSG;
2945 case BPF_SK_SKB_STREAM_PARSER:
2946 case BPF_SK_SKB_STREAM_VERDICT:
2947 return BPF_PROG_TYPE_SK_SKB;
2948 case BPF_LIRC_MODE2:
2949 return BPF_PROG_TYPE_LIRC_MODE2;
2950 case BPF_FLOW_DISSECTOR:
2951 return BPF_PROG_TYPE_FLOW_DISSECTOR;
2952 case BPF_CGROUP_SYSCTL:
2953 return BPF_PROG_TYPE_CGROUP_SYSCTL;
2954 case BPF_CGROUP_GETSOCKOPT:
2955 case BPF_CGROUP_SETSOCKOPT:
2956 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
2957 case BPF_TRACE_ITER:
2958 return BPF_PROG_TYPE_TRACING;
2959 case BPF_SK_LOOKUP:
2960 return BPF_PROG_TYPE_SK_LOOKUP;
2961 case BPF_XDP:
2962 return BPF_PROG_TYPE_XDP;
2963 default:
2964 return BPF_PROG_TYPE_UNSPEC;
2965 }
2966 }
2967
bpf_prog_attach_check_attach_type(const struct bpf_prog * prog,enum bpf_attach_type attach_type)2968 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2969 enum bpf_attach_type attach_type)
2970 {
2971 enum bpf_prog_type ptype;
2972
2973 switch (prog->type) {
2974 case BPF_PROG_TYPE_CGROUP_SOCK:
2975 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2976 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2977 case BPF_PROG_TYPE_SK_LOOKUP:
2978 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
2979 case BPF_PROG_TYPE_CGROUP_SKB:
2980 if (!capable(CAP_NET_ADMIN))
2981 /* cg-skb progs can be loaded by unpriv user.
2982 * check permissions at attach time.
2983 */
2984 return -EPERM;
2985
2986 ptype = attach_type_to_prog_type(attach_type);
2987 if (prog->type != ptype)
2988 return -EINVAL;
2989
2990 return prog->enforce_expected_attach_type &&
2991 prog->expected_attach_type != attach_type ?
2992 -EINVAL : 0;
2993 default:
2994 return 0;
2995 }
2996 }
2997
2998 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
2999
3000 #define BPF_F_ATTACH_MASK \
3001 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
3002
bpf_prog_attach(const union bpf_attr * attr)3003 static int bpf_prog_attach(const union bpf_attr *attr)
3004 {
3005 enum bpf_prog_type ptype;
3006 struct bpf_prog *prog;
3007 int ret;
3008
3009 if (CHECK_ATTR(BPF_PROG_ATTACH))
3010 return -EINVAL;
3011
3012 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
3013 return -EINVAL;
3014
3015 ptype = attach_type_to_prog_type(attr->attach_type);
3016 if (ptype == BPF_PROG_TYPE_UNSPEC)
3017 return -EINVAL;
3018
3019 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3020 if (IS_ERR(prog))
3021 return PTR_ERR(prog);
3022
3023 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3024 bpf_prog_put(prog);
3025 return -EINVAL;
3026 }
3027
3028 switch (ptype) {
3029 case BPF_PROG_TYPE_SK_SKB:
3030 case BPF_PROG_TYPE_SK_MSG:
3031 ret = sock_map_get_from_fd(attr, prog);
3032 break;
3033 case BPF_PROG_TYPE_LIRC_MODE2:
3034 ret = lirc_prog_attach(attr, prog);
3035 break;
3036 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3037 ret = netns_bpf_prog_attach(attr, prog);
3038 break;
3039 case BPF_PROG_TYPE_CGROUP_DEVICE:
3040 case BPF_PROG_TYPE_CGROUP_SKB:
3041 case BPF_PROG_TYPE_CGROUP_SOCK:
3042 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3043 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3044 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3045 case BPF_PROG_TYPE_SOCK_OPS:
3046 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3047 break;
3048 default:
3049 ret = -EINVAL;
3050 }
3051
3052 if (ret)
3053 bpf_prog_put(prog);
3054 return ret;
3055 }
3056
3057 #define BPF_PROG_DETACH_LAST_FIELD attach_type
3058
bpf_prog_detach(const union bpf_attr * attr)3059 static int bpf_prog_detach(const union bpf_attr *attr)
3060 {
3061 enum bpf_prog_type ptype;
3062
3063 if (CHECK_ATTR(BPF_PROG_DETACH))
3064 return -EINVAL;
3065
3066 ptype = attach_type_to_prog_type(attr->attach_type);
3067
3068 switch (ptype) {
3069 case BPF_PROG_TYPE_SK_MSG:
3070 case BPF_PROG_TYPE_SK_SKB:
3071 return sock_map_prog_detach(attr, ptype);
3072 case BPF_PROG_TYPE_LIRC_MODE2:
3073 return lirc_prog_detach(attr);
3074 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3075 return netns_bpf_prog_detach(attr, ptype);
3076 case BPF_PROG_TYPE_CGROUP_DEVICE:
3077 case BPF_PROG_TYPE_CGROUP_SKB:
3078 case BPF_PROG_TYPE_CGROUP_SOCK:
3079 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3080 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3081 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3082 case BPF_PROG_TYPE_SOCK_OPS:
3083 return cgroup_bpf_prog_detach(attr, ptype);
3084 default:
3085 return -EINVAL;
3086 }
3087 }
3088
3089 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3090
bpf_prog_query(const union bpf_attr * attr,union bpf_attr __user * uattr)3091 static int bpf_prog_query(const union bpf_attr *attr,
3092 union bpf_attr __user *uattr)
3093 {
3094 if (!capable(CAP_NET_ADMIN))
3095 return -EPERM;
3096 if (CHECK_ATTR(BPF_PROG_QUERY))
3097 return -EINVAL;
3098 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3099 return -EINVAL;
3100
3101 switch (attr->query.attach_type) {
3102 case BPF_CGROUP_INET_INGRESS:
3103 case BPF_CGROUP_INET_EGRESS:
3104 case BPF_CGROUP_INET_SOCK_CREATE:
3105 case BPF_CGROUP_INET_SOCK_RELEASE:
3106 case BPF_CGROUP_INET4_BIND:
3107 case BPF_CGROUP_INET6_BIND:
3108 case BPF_CGROUP_INET4_POST_BIND:
3109 case BPF_CGROUP_INET6_POST_BIND:
3110 case BPF_CGROUP_INET4_CONNECT:
3111 case BPF_CGROUP_INET6_CONNECT:
3112 case BPF_CGROUP_INET4_GETPEERNAME:
3113 case BPF_CGROUP_INET6_GETPEERNAME:
3114 case BPF_CGROUP_INET4_GETSOCKNAME:
3115 case BPF_CGROUP_INET6_GETSOCKNAME:
3116 case BPF_CGROUP_UDP4_SENDMSG:
3117 case BPF_CGROUP_UDP6_SENDMSG:
3118 case BPF_CGROUP_UDP4_RECVMSG:
3119 case BPF_CGROUP_UDP6_RECVMSG:
3120 case BPF_CGROUP_SOCK_OPS:
3121 case BPF_CGROUP_DEVICE:
3122 case BPF_CGROUP_SYSCTL:
3123 case BPF_CGROUP_GETSOCKOPT:
3124 case BPF_CGROUP_SETSOCKOPT:
3125 return cgroup_bpf_prog_query(attr, uattr);
3126 case BPF_LIRC_MODE2:
3127 return lirc_prog_query(attr, uattr);
3128 case BPF_FLOW_DISSECTOR:
3129 case BPF_SK_LOOKUP:
3130 return netns_bpf_prog_query(attr, uattr);
3131 default:
3132 return -EINVAL;
3133 }
3134 }
3135
3136 #define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
3137
bpf_prog_test_run(const union bpf_attr * attr,union bpf_attr __user * uattr)3138 static int bpf_prog_test_run(const union bpf_attr *attr,
3139 union bpf_attr __user *uattr)
3140 {
3141 struct bpf_prog *prog;
3142 int ret = -ENOTSUPP;
3143
3144 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3145 return -EINVAL;
3146
3147 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3148 (!attr->test.ctx_size_in && attr->test.ctx_in))
3149 return -EINVAL;
3150
3151 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3152 (!attr->test.ctx_size_out && attr->test.ctx_out))
3153 return -EINVAL;
3154
3155 prog = bpf_prog_get(attr->test.prog_fd);
3156 if (IS_ERR(prog))
3157 return PTR_ERR(prog);
3158
3159 if (prog->aux->ops->test_run)
3160 ret = prog->aux->ops->test_run(prog, attr, uattr);
3161
3162 bpf_prog_put(prog);
3163 return ret;
3164 }
3165
3166 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3167
bpf_obj_get_next_id(const union bpf_attr * attr,union bpf_attr __user * uattr,struct idr * idr,spinlock_t * lock)3168 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3169 union bpf_attr __user *uattr,
3170 struct idr *idr,
3171 spinlock_t *lock)
3172 {
3173 u32 next_id = attr->start_id;
3174 int err = 0;
3175
3176 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3177 return -EINVAL;
3178
3179 if (!capable(CAP_SYS_ADMIN))
3180 return -EPERM;
3181
3182 next_id++;
3183 spin_lock_bh(lock);
3184 if (!idr_get_next(idr, &next_id))
3185 err = -ENOENT;
3186 spin_unlock_bh(lock);
3187
3188 if (!err)
3189 err = put_user(next_id, &uattr->next_id);
3190
3191 return err;
3192 }
3193
bpf_map_get_curr_or_next(u32 * id)3194 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3195 {
3196 struct bpf_map *map;
3197
3198 spin_lock_bh(&map_idr_lock);
3199 again:
3200 map = idr_get_next(&map_idr, id);
3201 if (map) {
3202 map = __bpf_map_inc_not_zero(map, false);
3203 if (IS_ERR(map)) {
3204 (*id)++;
3205 goto again;
3206 }
3207 }
3208 spin_unlock_bh(&map_idr_lock);
3209
3210 return map;
3211 }
3212
bpf_prog_get_curr_or_next(u32 * id)3213 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3214 {
3215 struct bpf_prog *prog;
3216
3217 spin_lock_bh(&prog_idr_lock);
3218 again:
3219 prog = idr_get_next(&prog_idr, id);
3220 if (prog) {
3221 prog = bpf_prog_inc_not_zero(prog);
3222 if (IS_ERR(prog)) {
3223 (*id)++;
3224 goto again;
3225 }
3226 }
3227 spin_unlock_bh(&prog_idr_lock);
3228
3229 return prog;
3230 }
3231
3232 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3233
bpf_prog_by_id(u32 id)3234 struct bpf_prog *bpf_prog_by_id(u32 id)
3235 {
3236 struct bpf_prog *prog;
3237
3238 if (!id)
3239 return ERR_PTR(-ENOENT);
3240
3241 spin_lock_bh(&prog_idr_lock);
3242 prog = idr_find(&prog_idr, id);
3243 if (prog)
3244 prog = bpf_prog_inc_not_zero(prog);
3245 else
3246 prog = ERR_PTR(-ENOENT);
3247 spin_unlock_bh(&prog_idr_lock);
3248 return prog;
3249 }
3250
bpf_prog_get_fd_by_id(const union bpf_attr * attr)3251 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3252 {
3253 struct bpf_prog *prog;
3254 u32 id = attr->prog_id;
3255 int fd;
3256
3257 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3258 return -EINVAL;
3259
3260 if (!capable(CAP_SYS_ADMIN))
3261 return -EPERM;
3262
3263 prog = bpf_prog_by_id(id);
3264 if (IS_ERR(prog))
3265 return PTR_ERR(prog);
3266
3267 fd = bpf_prog_new_fd(prog);
3268 if (fd < 0)
3269 bpf_prog_put(prog);
3270
3271 return fd;
3272 }
3273
3274 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3275
bpf_map_get_fd_by_id(const union bpf_attr * attr)3276 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3277 {
3278 struct bpf_map *map;
3279 u32 id = attr->map_id;
3280 int f_flags;
3281 int fd;
3282
3283 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3284 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3285 return -EINVAL;
3286
3287 if (!capable(CAP_SYS_ADMIN))
3288 return -EPERM;
3289
3290 f_flags = bpf_get_file_flag(attr->open_flags);
3291 if (f_flags < 0)
3292 return f_flags;
3293
3294 spin_lock_bh(&map_idr_lock);
3295 map = idr_find(&map_idr, id);
3296 if (map)
3297 map = __bpf_map_inc_not_zero(map, true);
3298 else
3299 map = ERR_PTR(-ENOENT);
3300 spin_unlock_bh(&map_idr_lock);
3301
3302 if (IS_ERR(map))
3303 return PTR_ERR(map);
3304
3305 fd = bpf_map_new_fd(map, f_flags);
3306 if (fd < 0)
3307 bpf_map_put_with_uref(map);
3308
3309 return fd;
3310 }
3311
bpf_map_from_imm(const struct bpf_prog * prog,unsigned long addr,u32 * off,u32 * type)3312 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3313 unsigned long addr, u32 *off,
3314 u32 *type)
3315 {
3316 const struct bpf_map *map;
3317 int i;
3318
3319 mutex_lock(&prog->aux->used_maps_mutex);
3320 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3321 map = prog->aux->used_maps[i];
3322 if (map == (void *)addr) {
3323 *type = BPF_PSEUDO_MAP_FD;
3324 goto out;
3325 }
3326 if (!map->ops->map_direct_value_meta)
3327 continue;
3328 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3329 *type = BPF_PSEUDO_MAP_VALUE;
3330 goto out;
3331 }
3332 }
3333 map = NULL;
3334
3335 out:
3336 mutex_unlock(&prog->aux->used_maps_mutex);
3337 return map;
3338 }
3339
bpf_insn_prepare_dump(const struct bpf_prog * prog,const struct cred * f_cred)3340 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3341 const struct cred *f_cred)
3342 {
3343 const struct bpf_map *map;
3344 struct bpf_insn *insns;
3345 u32 off, type;
3346 u64 imm;
3347 u8 code;
3348 int i;
3349
3350 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3351 GFP_USER);
3352 if (!insns)
3353 return insns;
3354
3355 for (i = 0; i < prog->len; i++) {
3356 code = insns[i].code;
3357
3358 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3359 insns[i].code = BPF_JMP | BPF_CALL;
3360 insns[i].imm = BPF_FUNC_tail_call;
3361 /* fall-through */
3362 }
3363 if (code == (BPF_JMP | BPF_CALL) ||
3364 code == (BPF_JMP | BPF_CALL_ARGS)) {
3365 if (code == (BPF_JMP | BPF_CALL_ARGS))
3366 insns[i].code = BPF_JMP | BPF_CALL;
3367 if (!bpf_dump_raw_ok(f_cred))
3368 insns[i].imm = 0;
3369 continue;
3370 }
3371 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3372 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3373 continue;
3374 }
3375
3376 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3377 continue;
3378
3379 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3380 map = bpf_map_from_imm(prog, imm, &off, &type);
3381 if (map) {
3382 insns[i].src_reg = type;
3383 insns[i].imm = map->id;
3384 insns[i + 1].imm = off;
3385 continue;
3386 }
3387 }
3388
3389 return insns;
3390 }
3391
set_info_rec_size(struct bpf_prog_info * info)3392 static int set_info_rec_size(struct bpf_prog_info *info)
3393 {
3394 /*
3395 * Ensure info.*_rec_size is the same as kernel expected size
3396 *
3397 * or
3398 *
3399 * Only allow zero *_rec_size if both _rec_size and _cnt are
3400 * zero. In this case, the kernel will set the expected
3401 * _rec_size back to the info.
3402 */
3403
3404 if ((info->nr_func_info || info->func_info_rec_size) &&
3405 info->func_info_rec_size != sizeof(struct bpf_func_info))
3406 return -EINVAL;
3407
3408 if ((info->nr_line_info || info->line_info_rec_size) &&
3409 info->line_info_rec_size != sizeof(struct bpf_line_info))
3410 return -EINVAL;
3411
3412 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3413 info->jited_line_info_rec_size != sizeof(__u64))
3414 return -EINVAL;
3415
3416 info->func_info_rec_size = sizeof(struct bpf_func_info);
3417 info->line_info_rec_size = sizeof(struct bpf_line_info);
3418 info->jited_line_info_rec_size = sizeof(__u64);
3419
3420 return 0;
3421 }
3422
bpf_prog_get_info_by_fd(struct file * file,struct bpf_prog * prog,const union bpf_attr * attr,union bpf_attr __user * uattr)3423 static int bpf_prog_get_info_by_fd(struct file *file,
3424 struct bpf_prog *prog,
3425 const union bpf_attr *attr,
3426 union bpf_attr __user *uattr)
3427 {
3428 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3429 struct bpf_prog_info info;
3430 u32 info_len = attr->info.info_len;
3431 struct bpf_prog_stats stats;
3432 char __user *uinsns;
3433 u32 ulen;
3434 int err;
3435
3436 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3437 if (err)
3438 return err;
3439 info_len = min_t(u32, sizeof(info), info_len);
3440
3441 memset(&info, 0, sizeof(info));
3442 if (copy_from_user(&info, uinfo, info_len))
3443 return -EFAULT;
3444
3445 info.type = prog->type;
3446 info.id = prog->aux->id;
3447 info.load_time = prog->aux->load_time;
3448 info.created_by_uid = from_kuid_munged(current_user_ns(),
3449 prog->aux->user->uid);
3450 info.gpl_compatible = prog->gpl_compatible;
3451
3452 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3453 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3454
3455 mutex_lock(&prog->aux->used_maps_mutex);
3456 ulen = info.nr_map_ids;
3457 info.nr_map_ids = prog->aux->used_map_cnt;
3458 ulen = min_t(u32, info.nr_map_ids, ulen);
3459 if (ulen) {
3460 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3461 u32 i;
3462
3463 for (i = 0; i < ulen; i++)
3464 if (put_user(prog->aux->used_maps[i]->id,
3465 &user_map_ids[i])) {
3466 mutex_unlock(&prog->aux->used_maps_mutex);
3467 return -EFAULT;
3468 }
3469 }
3470 mutex_unlock(&prog->aux->used_maps_mutex);
3471
3472 err = set_info_rec_size(&info);
3473 if (err)
3474 return err;
3475
3476 bpf_prog_get_stats(prog, &stats);
3477 info.run_time_ns = stats.nsecs;
3478 info.run_cnt = stats.cnt;
3479
3480 if (!bpf_capable()) {
3481 info.jited_prog_len = 0;
3482 info.xlated_prog_len = 0;
3483 info.nr_jited_ksyms = 0;
3484 info.nr_jited_func_lens = 0;
3485 info.nr_func_info = 0;
3486 info.nr_line_info = 0;
3487 info.nr_jited_line_info = 0;
3488 goto done;
3489 }
3490
3491 ulen = info.xlated_prog_len;
3492 info.xlated_prog_len = bpf_prog_insn_size(prog);
3493 if (info.xlated_prog_len && ulen) {
3494 struct bpf_insn *insns_sanitized;
3495 bool fault;
3496
3497 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3498 info.xlated_prog_insns = 0;
3499 goto done;
3500 }
3501 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3502 if (!insns_sanitized)
3503 return -ENOMEM;
3504 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3505 ulen = min_t(u32, info.xlated_prog_len, ulen);
3506 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3507 kfree(insns_sanitized);
3508 if (fault)
3509 return -EFAULT;
3510 }
3511
3512 if (bpf_prog_is_dev_bound(prog->aux)) {
3513 err = bpf_prog_offload_info_fill(&info, prog);
3514 if (err)
3515 return err;
3516 goto done;
3517 }
3518
3519 /* NOTE: the following code is supposed to be skipped for offload.
3520 * bpf_prog_offload_info_fill() is the place to fill similar fields
3521 * for offload.
3522 */
3523 ulen = info.jited_prog_len;
3524 if (prog->aux->func_cnt) {
3525 u32 i;
3526
3527 info.jited_prog_len = 0;
3528 for (i = 0; i < prog->aux->func_cnt; i++)
3529 info.jited_prog_len += prog->aux->func[i]->jited_len;
3530 } else {
3531 info.jited_prog_len = prog->jited_len;
3532 }
3533
3534 if (info.jited_prog_len && ulen) {
3535 if (bpf_dump_raw_ok(file->f_cred)) {
3536 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3537 ulen = min_t(u32, info.jited_prog_len, ulen);
3538
3539 /* for multi-function programs, copy the JITed
3540 * instructions for all the functions
3541 */
3542 if (prog->aux->func_cnt) {
3543 u32 len, free, i;
3544 u8 *img;
3545
3546 free = ulen;
3547 for (i = 0; i < prog->aux->func_cnt; i++) {
3548 len = prog->aux->func[i]->jited_len;
3549 len = min_t(u32, len, free);
3550 img = (u8 *) prog->aux->func[i]->bpf_func;
3551 if (copy_to_user(uinsns, img, len))
3552 return -EFAULT;
3553 uinsns += len;
3554 free -= len;
3555 if (!free)
3556 break;
3557 }
3558 } else {
3559 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3560 return -EFAULT;
3561 }
3562 } else {
3563 info.jited_prog_insns = 0;
3564 }
3565 }
3566
3567 ulen = info.nr_jited_ksyms;
3568 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
3569 if (ulen) {
3570 if (bpf_dump_raw_ok(file->f_cred)) {
3571 unsigned long ksym_addr;
3572 u64 __user *user_ksyms;
3573 u32 i;
3574
3575 /* copy the address of the kernel symbol
3576 * corresponding to each function
3577 */
3578 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3579 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
3580 if (prog->aux->func_cnt) {
3581 for (i = 0; i < ulen; i++) {
3582 ksym_addr = (unsigned long)
3583 prog->aux->func[i]->bpf_func;
3584 if (put_user((u64) ksym_addr,
3585 &user_ksyms[i]))
3586 return -EFAULT;
3587 }
3588 } else {
3589 ksym_addr = (unsigned long) prog->bpf_func;
3590 if (put_user((u64) ksym_addr, &user_ksyms[0]))
3591 return -EFAULT;
3592 }
3593 } else {
3594 info.jited_ksyms = 0;
3595 }
3596 }
3597
3598 ulen = info.nr_jited_func_lens;
3599 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
3600 if (ulen) {
3601 if (bpf_dump_raw_ok(file->f_cred)) {
3602 u32 __user *user_lens;
3603 u32 func_len, i;
3604
3605 /* copy the JITed image lengths for each function */
3606 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3607 user_lens = u64_to_user_ptr(info.jited_func_lens);
3608 if (prog->aux->func_cnt) {
3609 for (i = 0; i < ulen; i++) {
3610 func_len =
3611 prog->aux->func[i]->jited_len;
3612 if (put_user(func_len, &user_lens[i]))
3613 return -EFAULT;
3614 }
3615 } else {
3616 func_len = prog->jited_len;
3617 if (put_user(func_len, &user_lens[0]))
3618 return -EFAULT;
3619 }
3620 } else {
3621 info.jited_func_lens = 0;
3622 }
3623 }
3624
3625 if (prog->aux->btf)
3626 info.btf_id = btf_id(prog->aux->btf);
3627
3628 ulen = info.nr_func_info;
3629 info.nr_func_info = prog->aux->func_info_cnt;
3630 if (info.nr_func_info && ulen) {
3631 char __user *user_finfo;
3632
3633 user_finfo = u64_to_user_ptr(info.func_info);
3634 ulen = min_t(u32, info.nr_func_info, ulen);
3635 if (copy_to_user(user_finfo, prog->aux->func_info,
3636 info.func_info_rec_size * ulen))
3637 return -EFAULT;
3638 }
3639
3640 ulen = info.nr_line_info;
3641 info.nr_line_info = prog->aux->nr_linfo;
3642 if (info.nr_line_info && ulen) {
3643 __u8 __user *user_linfo;
3644
3645 user_linfo = u64_to_user_ptr(info.line_info);
3646 ulen = min_t(u32, info.nr_line_info, ulen);
3647 if (copy_to_user(user_linfo, prog->aux->linfo,
3648 info.line_info_rec_size * ulen))
3649 return -EFAULT;
3650 }
3651
3652 ulen = info.nr_jited_line_info;
3653 if (prog->aux->jited_linfo)
3654 info.nr_jited_line_info = prog->aux->nr_linfo;
3655 else
3656 info.nr_jited_line_info = 0;
3657 if (info.nr_jited_line_info && ulen) {
3658 if (bpf_dump_raw_ok(file->f_cred)) {
3659 __u64 __user *user_linfo;
3660 u32 i;
3661
3662 user_linfo = u64_to_user_ptr(info.jited_line_info);
3663 ulen = min_t(u32, info.nr_jited_line_info, ulen);
3664 for (i = 0; i < ulen; i++) {
3665 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3666 &user_linfo[i]))
3667 return -EFAULT;
3668 }
3669 } else {
3670 info.jited_line_info = 0;
3671 }
3672 }
3673
3674 ulen = info.nr_prog_tags;
3675 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3676 if (ulen) {
3677 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3678 u32 i;
3679
3680 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3681 ulen = min_t(u32, info.nr_prog_tags, ulen);
3682 if (prog->aux->func_cnt) {
3683 for (i = 0; i < ulen; i++) {
3684 if (copy_to_user(user_prog_tags[i],
3685 prog->aux->func[i]->tag,
3686 BPF_TAG_SIZE))
3687 return -EFAULT;
3688 }
3689 } else {
3690 if (copy_to_user(user_prog_tags[0],
3691 prog->tag, BPF_TAG_SIZE))
3692 return -EFAULT;
3693 }
3694 }
3695
3696 done:
3697 if (copy_to_user(uinfo, &info, info_len) ||
3698 put_user(info_len, &uattr->info.info_len))
3699 return -EFAULT;
3700
3701 return 0;
3702 }
3703
bpf_map_get_info_by_fd(struct file * file,struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)3704 static int bpf_map_get_info_by_fd(struct file *file,
3705 struct bpf_map *map,
3706 const union bpf_attr *attr,
3707 union bpf_attr __user *uattr)
3708 {
3709 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3710 struct bpf_map_info info;
3711 u32 info_len = attr->info.info_len;
3712 int err;
3713
3714 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3715 if (err)
3716 return err;
3717 info_len = min_t(u32, sizeof(info), info_len);
3718
3719 memset(&info, 0, sizeof(info));
3720 info.type = map->map_type;
3721 info.id = map->id;
3722 info.key_size = map->key_size;
3723 info.value_size = map->value_size;
3724 info.max_entries = map->max_entries;
3725 info.map_flags = map->map_flags;
3726 memcpy(info.name, map->name, sizeof(map->name));
3727
3728 if (map->btf) {
3729 info.btf_id = btf_id(map->btf);
3730 info.btf_key_type_id = map->btf_key_type_id;
3731 info.btf_value_type_id = map->btf_value_type_id;
3732 }
3733 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
3734
3735 if (bpf_map_is_dev_bound(map)) {
3736 err = bpf_map_offload_info_fill(&info, map);
3737 if (err)
3738 return err;
3739 }
3740
3741 if (copy_to_user(uinfo, &info, info_len) ||
3742 put_user(info_len, &uattr->info.info_len))
3743 return -EFAULT;
3744
3745 return 0;
3746 }
3747
bpf_btf_get_info_by_fd(struct file * file,struct btf * btf,const union bpf_attr * attr,union bpf_attr __user * uattr)3748 static int bpf_btf_get_info_by_fd(struct file *file,
3749 struct btf *btf,
3750 const union bpf_attr *attr,
3751 union bpf_attr __user *uattr)
3752 {
3753 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3754 u32 info_len = attr->info.info_len;
3755 int err;
3756
3757 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
3758 if (err)
3759 return err;
3760
3761 return btf_get_info_by_fd(btf, attr, uattr);
3762 }
3763
bpf_link_get_info_by_fd(struct file * file,struct bpf_link * link,const union bpf_attr * attr,union bpf_attr __user * uattr)3764 static int bpf_link_get_info_by_fd(struct file *file,
3765 struct bpf_link *link,
3766 const union bpf_attr *attr,
3767 union bpf_attr __user *uattr)
3768 {
3769 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3770 struct bpf_link_info info;
3771 u32 info_len = attr->info.info_len;
3772 int err;
3773
3774 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3775 if (err)
3776 return err;
3777 info_len = min_t(u32, sizeof(info), info_len);
3778
3779 memset(&info, 0, sizeof(info));
3780 if (copy_from_user(&info, uinfo, info_len))
3781 return -EFAULT;
3782
3783 info.type = link->type;
3784 info.id = link->id;
3785 info.prog_id = link->prog->aux->id;
3786
3787 if (link->ops->fill_link_info) {
3788 err = link->ops->fill_link_info(link, &info);
3789 if (err)
3790 return err;
3791 }
3792
3793 if (copy_to_user(uinfo, &info, info_len) ||
3794 put_user(info_len, &uattr->info.info_len))
3795 return -EFAULT;
3796
3797 return 0;
3798 }
3799
3800
3801 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3802
bpf_obj_get_info_by_fd(const union bpf_attr * attr,union bpf_attr __user * uattr)3803 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3804 union bpf_attr __user *uattr)
3805 {
3806 int ufd = attr->info.bpf_fd;
3807 struct fd f;
3808 int err;
3809
3810 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3811 return -EINVAL;
3812
3813 f = fdget(ufd);
3814 if (!f.file)
3815 return -EBADFD;
3816
3817 if (f.file->f_op == &bpf_prog_fops)
3818 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
3819 uattr);
3820 else if (f.file->f_op == &bpf_map_fops)
3821 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
3822 uattr);
3823 else if (f.file->f_op == &btf_fops)
3824 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
3825 else if (f.file->f_op == &bpf_link_fops)
3826 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
3827 attr, uattr);
3828 else
3829 err = -EINVAL;
3830
3831 fdput(f);
3832 return err;
3833 }
3834
3835 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3836
bpf_btf_load(const union bpf_attr * attr)3837 static int bpf_btf_load(const union bpf_attr *attr)
3838 {
3839 if (CHECK_ATTR(BPF_BTF_LOAD))
3840 return -EINVAL;
3841
3842 if (!bpf_capable())
3843 return -EPERM;
3844
3845 return btf_new_fd(attr);
3846 }
3847
3848 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3849
bpf_btf_get_fd_by_id(const union bpf_attr * attr)3850 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3851 {
3852 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3853 return -EINVAL;
3854
3855 if (!capable(CAP_SYS_ADMIN))
3856 return -EPERM;
3857
3858 return btf_get_fd_by_id(attr->btf_id);
3859 }
3860
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)3861 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3862 union bpf_attr __user *uattr,
3863 u32 prog_id, u32 fd_type,
3864 const char *buf, u64 probe_offset,
3865 u64 probe_addr)
3866 {
3867 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3868 u32 len = buf ? strlen(buf) : 0, input_len;
3869 int err = 0;
3870
3871 if (put_user(len, &uattr->task_fd_query.buf_len))
3872 return -EFAULT;
3873 input_len = attr->task_fd_query.buf_len;
3874 if (input_len && ubuf) {
3875 if (!len) {
3876 /* nothing to copy, just make ubuf NULL terminated */
3877 char zero = '\0';
3878
3879 if (put_user(zero, ubuf))
3880 return -EFAULT;
3881 } else if (input_len >= len + 1) {
3882 /* ubuf can hold the string with NULL terminator */
3883 if (copy_to_user(ubuf, buf, len + 1))
3884 return -EFAULT;
3885 } else {
3886 /* ubuf cannot hold the string with NULL terminator,
3887 * do a partial copy with NULL terminator.
3888 */
3889 char zero = '\0';
3890
3891 err = -ENOSPC;
3892 if (copy_to_user(ubuf, buf, input_len - 1))
3893 return -EFAULT;
3894 if (put_user(zero, ubuf + input_len - 1))
3895 return -EFAULT;
3896 }
3897 }
3898
3899 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3900 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3901 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3902 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3903 return -EFAULT;
3904
3905 return err;
3906 }
3907
3908 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3909
bpf_task_fd_query(const union bpf_attr * attr,union bpf_attr __user * uattr)3910 static int bpf_task_fd_query(const union bpf_attr *attr,
3911 union bpf_attr __user *uattr)
3912 {
3913 pid_t pid = attr->task_fd_query.pid;
3914 u32 fd = attr->task_fd_query.fd;
3915 const struct perf_event *event;
3916 struct files_struct *files;
3917 struct task_struct *task;
3918 struct file *file;
3919 int err;
3920
3921 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3922 return -EINVAL;
3923
3924 if (!capable(CAP_SYS_ADMIN))
3925 return -EPERM;
3926
3927 if (attr->task_fd_query.flags != 0)
3928 return -EINVAL;
3929
3930 rcu_read_lock();
3931 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3932 rcu_read_unlock();
3933 if (!task)
3934 return -ENOENT;
3935
3936 files = get_files_struct(task);
3937 put_task_struct(task);
3938 if (!files)
3939 return -ENOENT;
3940
3941 err = 0;
3942 spin_lock(&files->file_lock);
3943 file = fcheck_files(files, fd);
3944 if (!file)
3945 err = -EBADF;
3946 else
3947 get_file(file);
3948 spin_unlock(&files->file_lock);
3949 put_files_struct(files);
3950
3951 if (err)
3952 goto out;
3953
3954 if (file->f_op == &bpf_link_fops) {
3955 struct bpf_link *link = file->private_data;
3956
3957 if (link->ops == &bpf_raw_tp_link_lops) {
3958 struct bpf_raw_tp_link *raw_tp =
3959 container_of(link, struct bpf_raw_tp_link, link);
3960 struct bpf_raw_event_map *btp = raw_tp->btp;
3961
3962 err = bpf_task_fd_query_copy(attr, uattr,
3963 raw_tp->link.prog->aux->id,
3964 BPF_FD_TYPE_RAW_TRACEPOINT,
3965 btp->tp->name, 0, 0);
3966 goto put_file;
3967 }
3968 goto out_not_supp;
3969 }
3970
3971 event = perf_get_event(file);
3972 if (!IS_ERR(event)) {
3973 u64 probe_offset, probe_addr;
3974 u32 prog_id, fd_type;
3975 const char *buf;
3976
3977 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3978 &buf, &probe_offset,
3979 &probe_addr);
3980 if (!err)
3981 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3982 fd_type, buf,
3983 probe_offset,
3984 probe_addr);
3985 goto put_file;
3986 }
3987
3988 out_not_supp:
3989 err = -ENOTSUPP;
3990 put_file:
3991 fput(file);
3992 out:
3993 return err;
3994 }
3995
3996 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
3997
3998 #define BPF_DO_BATCH(fn) \
3999 do { \
4000 if (!fn) { \
4001 err = -ENOTSUPP; \
4002 goto err_put; \
4003 } \
4004 err = fn(map, attr, uattr); \
4005 } while (0)
4006
bpf_map_do_batch(const union bpf_attr * attr,union bpf_attr __user * uattr,int cmd)4007 static int bpf_map_do_batch(const union bpf_attr *attr,
4008 union bpf_attr __user *uattr,
4009 int cmd)
4010 {
4011 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH ||
4012 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH;
4013 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH;
4014 struct bpf_map *map;
4015 int err, ufd;
4016 struct fd f;
4017
4018 if (CHECK_ATTR(BPF_MAP_BATCH))
4019 return -EINVAL;
4020
4021 ufd = attr->batch.map_fd;
4022 f = fdget(ufd);
4023 map = __bpf_map_get(f);
4024 if (IS_ERR(map))
4025 return PTR_ERR(map);
4026 if (has_write)
4027 bpf_map_write_active_inc(map);
4028 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4029 err = -EPERM;
4030 goto err_put;
4031 }
4032 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4033 err = -EPERM;
4034 goto err_put;
4035 }
4036
4037 if (cmd == BPF_MAP_LOOKUP_BATCH)
4038 BPF_DO_BATCH(map->ops->map_lookup_batch);
4039 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4040 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
4041 else if (cmd == BPF_MAP_UPDATE_BATCH)
4042 BPF_DO_BATCH(map->ops->map_update_batch);
4043 else
4044 BPF_DO_BATCH(map->ops->map_delete_batch);
4045 err_put:
4046 if (has_write)
4047 bpf_map_write_active_dec(map);
4048 fdput(f);
4049 return err;
4050 }
4051
tracing_bpf_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)4052 static int tracing_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
4053 {
4054 if (attr->link_create.attach_type != prog->expected_attach_type)
4055 return -EINVAL;
4056
4057 if (prog->expected_attach_type == BPF_TRACE_ITER)
4058 return bpf_iter_link_attach(attr, prog);
4059 else if (prog->type == BPF_PROG_TYPE_EXT)
4060 return bpf_tracing_prog_attach(prog,
4061 attr->link_create.target_fd,
4062 attr->link_create.target_btf_id);
4063 return -EINVAL;
4064 }
4065
4066 #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
link_create(union bpf_attr * attr)4067 static int link_create(union bpf_attr *attr)
4068 {
4069 enum bpf_prog_type ptype;
4070 struct bpf_prog *prog;
4071 int ret;
4072
4073 if (CHECK_ATTR(BPF_LINK_CREATE))
4074 return -EINVAL;
4075
4076 prog = bpf_prog_get(attr->link_create.prog_fd);
4077 if (IS_ERR(prog))
4078 return PTR_ERR(prog);
4079
4080 ret = bpf_prog_attach_check_attach_type(prog,
4081 attr->link_create.attach_type);
4082 if (ret)
4083 goto out;
4084
4085 if (prog->type == BPF_PROG_TYPE_EXT) {
4086 ret = tracing_bpf_link_attach(attr, prog);
4087 goto out;
4088 }
4089
4090 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4091 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4092 ret = -EINVAL;
4093 goto out;
4094 }
4095
4096 switch (ptype) {
4097 case BPF_PROG_TYPE_CGROUP_SKB:
4098 case BPF_PROG_TYPE_CGROUP_SOCK:
4099 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4100 case BPF_PROG_TYPE_SOCK_OPS:
4101 case BPF_PROG_TYPE_CGROUP_DEVICE:
4102 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4103 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4104 ret = cgroup_bpf_link_attach(attr, prog);
4105 break;
4106 case BPF_PROG_TYPE_TRACING:
4107 ret = tracing_bpf_link_attach(attr, prog);
4108 break;
4109 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4110 case BPF_PROG_TYPE_SK_LOOKUP:
4111 ret = netns_bpf_link_create(attr, prog);
4112 break;
4113 #ifdef CONFIG_NET
4114 case BPF_PROG_TYPE_XDP:
4115 ret = bpf_xdp_link_attach(attr, prog);
4116 break;
4117 #endif
4118 default:
4119 ret = -EINVAL;
4120 }
4121
4122 out:
4123 if (ret < 0)
4124 bpf_prog_put(prog);
4125 return ret;
4126 }
4127
4128 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4129
link_update(union bpf_attr * attr)4130 static int link_update(union bpf_attr *attr)
4131 {
4132 struct bpf_prog *old_prog = NULL, *new_prog;
4133 struct bpf_link *link;
4134 u32 flags;
4135 int ret;
4136
4137 if (CHECK_ATTR(BPF_LINK_UPDATE))
4138 return -EINVAL;
4139
4140 flags = attr->link_update.flags;
4141 if (flags & ~BPF_F_REPLACE)
4142 return -EINVAL;
4143
4144 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4145 if (IS_ERR(link))
4146 return PTR_ERR(link);
4147
4148 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4149 if (IS_ERR(new_prog)) {
4150 ret = PTR_ERR(new_prog);
4151 goto out_put_link;
4152 }
4153
4154 if (flags & BPF_F_REPLACE) {
4155 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4156 if (IS_ERR(old_prog)) {
4157 ret = PTR_ERR(old_prog);
4158 old_prog = NULL;
4159 goto out_put_progs;
4160 }
4161 } else if (attr->link_update.old_prog_fd) {
4162 ret = -EINVAL;
4163 goto out_put_progs;
4164 }
4165
4166 if (link->ops->update_prog)
4167 ret = link->ops->update_prog(link, new_prog, old_prog);
4168 else
4169 ret = -EINVAL;
4170
4171 out_put_progs:
4172 if (old_prog)
4173 bpf_prog_put(old_prog);
4174 if (ret)
4175 bpf_prog_put(new_prog);
4176 out_put_link:
4177 bpf_link_put(link);
4178 return ret;
4179 }
4180
4181 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4182
link_detach(union bpf_attr * attr)4183 static int link_detach(union bpf_attr *attr)
4184 {
4185 struct bpf_link *link;
4186 int ret;
4187
4188 if (CHECK_ATTR(BPF_LINK_DETACH))
4189 return -EINVAL;
4190
4191 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4192 if (IS_ERR(link))
4193 return PTR_ERR(link);
4194
4195 if (link->ops->detach)
4196 ret = link->ops->detach(link);
4197 else
4198 ret = -EOPNOTSUPP;
4199
4200 bpf_link_put(link);
4201 return ret;
4202 }
4203
bpf_link_inc_not_zero(struct bpf_link * link)4204 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4205 {
4206 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4207 }
4208
bpf_link_by_id(u32 id)4209 struct bpf_link *bpf_link_by_id(u32 id)
4210 {
4211 struct bpf_link *link;
4212
4213 if (!id)
4214 return ERR_PTR(-ENOENT);
4215
4216 spin_lock_bh(&link_idr_lock);
4217 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4218 link = idr_find(&link_idr, id);
4219 if (link) {
4220 if (link->id)
4221 link = bpf_link_inc_not_zero(link);
4222 else
4223 link = ERR_PTR(-EAGAIN);
4224 } else {
4225 link = ERR_PTR(-ENOENT);
4226 }
4227 spin_unlock_bh(&link_idr_lock);
4228 return link;
4229 }
4230
4231 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4232
bpf_link_get_fd_by_id(const union bpf_attr * attr)4233 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4234 {
4235 struct bpf_link *link;
4236 u32 id = attr->link_id;
4237 int fd;
4238
4239 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4240 return -EINVAL;
4241
4242 if (!capable(CAP_SYS_ADMIN))
4243 return -EPERM;
4244
4245 link = bpf_link_by_id(id);
4246 if (IS_ERR(link))
4247 return PTR_ERR(link);
4248
4249 fd = bpf_link_new_fd(link);
4250 if (fd < 0)
4251 bpf_link_put(link);
4252
4253 return fd;
4254 }
4255
4256 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4257
bpf_stats_release(struct inode * inode,struct file * file)4258 static int bpf_stats_release(struct inode *inode, struct file *file)
4259 {
4260 mutex_lock(&bpf_stats_enabled_mutex);
4261 static_key_slow_dec(&bpf_stats_enabled_key.key);
4262 mutex_unlock(&bpf_stats_enabled_mutex);
4263 return 0;
4264 }
4265
4266 static const struct file_operations bpf_stats_fops = {
4267 .release = bpf_stats_release,
4268 };
4269
bpf_enable_runtime_stats(void)4270 static int bpf_enable_runtime_stats(void)
4271 {
4272 int fd;
4273
4274 mutex_lock(&bpf_stats_enabled_mutex);
4275
4276 /* Set a very high limit to avoid overflow */
4277 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4278 mutex_unlock(&bpf_stats_enabled_mutex);
4279 return -EBUSY;
4280 }
4281
4282 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4283 if (fd >= 0)
4284 static_key_slow_inc(&bpf_stats_enabled_key.key);
4285
4286 mutex_unlock(&bpf_stats_enabled_mutex);
4287 return fd;
4288 }
4289
4290 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4291
bpf_enable_stats(union bpf_attr * attr)4292 static int bpf_enable_stats(union bpf_attr *attr)
4293 {
4294
4295 if (CHECK_ATTR(BPF_ENABLE_STATS))
4296 return -EINVAL;
4297
4298 if (!capable(CAP_SYS_ADMIN))
4299 return -EPERM;
4300
4301 switch (attr->enable_stats.type) {
4302 case BPF_STATS_RUN_TIME:
4303 return bpf_enable_runtime_stats();
4304 default:
4305 break;
4306 }
4307 return -EINVAL;
4308 }
4309
4310 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4311
bpf_iter_create(union bpf_attr * attr)4312 static int bpf_iter_create(union bpf_attr *attr)
4313 {
4314 struct bpf_link *link;
4315 int err;
4316
4317 if (CHECK_ATTR(BPF_ITER_CREATE))
4318 return -EINVAL;
4319
4320 if (attr->iter_create.flags)
4321 return -EINVAL;
4322
4323 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4324 if (IS_ERR(link))
4325 return PTR_ERR(link);
4326
4327 err = bpf_iter_new_fd(link);
4328 bpf_link_put(link);
4329
4330 return err;
4331 }
4332
4333 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4334
bpf_prog_bind_map(union bpf_attr * attr)4335 static int bpf_prog_bind_map(union bpf_attr *attr)
4336 {
4337 struct bpf_prog *prog;
4338 struct bpf_map *map;
4339 struct bpf_map **used_maps_old, **used_maps_new;
4340 int i, ret = 0;
4341
4342 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4343 return -EINVAL;
4344
4345 if (attr->prog_bind_map.flags)
4346 return -EINVAL;
4347
4348 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4349 if (IS_ERR(prog))
4350 return PTR_ERR(prog);
4351
4352 map = bpf_map_get(attr->prog_bind_map.map_fd);
4353 if (IS_ERR(map)) {
4354 ret = PTR_ERR(map);
4355 goto out_prog_put;
4356 }
4357
4358 mutex_lock(&prog->aux->used_maps_mutex);
4359
4360 used_maps_old = prog->aux->used_maps;
4361
4362 for (i = 0; i < prog->aux->used_map_cnt; i++)
4363 if (used_maps_old[i] == map) {
4364 bpf_map_put(map);
4365 goto out_unlock;
4366 }
4367
4368 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4369 sizeof(used_maps_new[0]),
4370 GFP_KERNEL);
4371 if (!used_maps_new) {
4372 ret = -ENOMEM;
4373 goto out_unlock;
4374 }
4375
4376 memcpy(used_maps_new, used_maps_old,
4377 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4378 used_maps_new[prog->aux->used_map_cnt] = map;
4379
4380 prog->aux->used_map_cnt++;
4381 prog->aux->used_maps = used_maps_new;
4382
4383 kfree(used_maps_old);
4384
4385 out_unlock:
4386 mutex_unlock(&prog->aux->used_maps_mutex);
4387
4388 if (ret)
4389 bpf_map_put(map);
4390 out_prog_put:
4391 bpf_prog_put(prog);
4392 return ret;
4393 }
4394
SYSCALL_DEFINE3(bpf,int,cmd,union bpf_attr __user *,uattr,unsigned int,size)4395 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4396 {
4397 union bpf_attr attr;
4398 int err;
4399
4400 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
4401 return -EPERM;
4402
4403 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4404 if (err)
4405 return err;
4406 size = min_t(u32, size, sizeof(attr));
4407
4408 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4409 memset(&attr, 0, sizeof(attr));
4410 if (copy_from_user(&attr, uattr, size) != 0)
4411 return -EFAULT;
4412
4413 err = security_bpf(cmd, &attr, size);
4414 if (err < 0)
4415 return err;
4416
4417 switch (cmd) {
4418 case BPF_MAP_CREATE:
4419 err = map_create(&attr);
4420 break;
4421 case BPF_MAP_LOOKUP_ELEM:
4422 err = map_lookup_elem(&attr);
4423 break;
4424 case BPF_MAP_UPDATE_ELEM:
4425 err = map_update_elem(&attr);
4426 break;
4427 case BPF_MAP_DELETE_ELEM:
4428 err = map_delete_elem(&attr);
4429 break;
4430 case BPF_MAP_GET_NEXT_KEY:
4431 err = map_get_next_key(&attr);
4432 break;
4433 case BPF_MAP_FREEZE:
4434 err = map_freeze(&attr);
4435 break;
4436 case BPF_PROG_LOAD:
4437 err = bpf_prog_load(&attr, uattr);
4438 break;
4439 case BPF_OBJ_PIN:
4440 err = bpf_obj_pin(&attr);
4441 break;
4442 case BPF_OBJ_GET:
4443 err = bpf_obj_get(&attr);
4444 break;
4445 case BPF_PROG_ATTACH:
4446 err = bpf_prog_attach(&attr);
4447 break;
4448 case BPF_PROG_DETACH:
4449 err = bpf_prog_detach(&attr);
4450 break;
4451 case BPF_PROG_QUERY:
4452 err = bpf_prog_query(&attr, uattr);
4453 break;
4454 case BPF_PROG_TEST_RUN:
4455 err = bpf_prog_test_run(&attr, uattr);
4456 break;
4457 case BPF_PROG_GET_NEXT_ID:
4458 err = bpf_obj_get_next_id(&attr, uattr,
4459 &prog_idr, &prog_idr_lock);
4460 break;
4461 case BPF_MAP_GET_NEXT_ID:
4462 err = bpf_obj_get_next_id(&attr, uattr,
4463 &map_idr, &map_idr_lock);
4464 break;
4465 case BPF_BTF_GET_NEXT_ID:
4466 err = bpf_obj_get_next_id(&attr, uattr,
4467 &btf_idr, &btf_idr_lock);
4468 break;
4469 case BPF_PROG_GET_FD_BY_ID:
4470 err = bpf_prog_get_fd_by_id(&attr);
4471 break;
4472 case BPF_MAP_GET_FD_BY_ID:
4473 err = bpf_map_get_fd_by_id(&attr);
4474 break;
4475 case BPF_OBJ_GET_INFO_BY_FD:
4476 err = bpf_obj_get_info_by_fd(&attr, uattr);
4477 break;
4478 case BPF_RAW_TRACEPOINT_OPEN:
4479 err = bpf_raw_tracepoint_open(&attr);
4480 break;
4481 case BPF_BTF_LOAD:
4482 err = bpf_btf_load(&attr);
4483 break;
4484 case BPF_BTF_GET_FD_BY_ID:
4485 err = bpf_btf_get_fd_by_id(&attr);
4486 break;
4487 case BPF_TASK_FD_QUERY:
4488 err = bpf_task_fd_query(&attr, uattr);
4489 break;
4490 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4491 err = map_lookup_and_delete_elem(&attr);
4492 break;
4493 case BPF_MAP_LOOKUP_BATCH:
4494 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
4495 break;
4496 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4497 err = bpf_map_do_batch(&attr, uattr,
4498 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4499 break;
4500 case BPF_MAP_UPDATE_BATCH:
4501 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
4502 break;
4503 case BPF_MAP_DELETE_BATCH:
4504 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);
4505 break;
4506 case BPF_LINK_CREATE:
4507 err = link_create(&attr);
4508 break;
4509 case BPF_LINK_UPDATE:
4510 err = link_update(&attr);
4511 break;
4512 case BPF_LINK_GET_FD_BY_ID:
4513 err = bpf_link_get_fd_by_id(&attr);
4514 break;
4515 case BPF_LINK_GET_NEXT_ID:
4516 err = bpf_obj_get_next_id(&attr, uattr,
4517 &link_idr, &link_idr_lock);
4518 break;
4519 case BPF_ENABLE_STATS:
4520 err = bpf_enable_stats(&attr);
4521 break;
4522 case BPF_ITER_CREATE:
4523 err = bpf_iter_create(&attr);
4524 break;
4525 case BPF_LINK_DETACH:
4526 err = link_detach(&attr);
4527 break;
4528 case BPF_PROG_BIND_MAP:
4529 err = bpf_prog_bind_map(&attr);
4530 break;
4531 default:
4532 err = -EINVAL;
4533 break;
4534 }
4535
4536 return err;
4537 }
4538