1 // SPDX-License-Identifier: GPL-2.0-only
3 * Functions to manage eBPF programs attached to cgroups
5 * Copyright (c) 2016 Daniel Mack
8 #include <linux/kernel.h>
9 #include <linux/atomic.h>
10 #include <linux/cgroup.h>
11 #include <linux/filter.h>
12 #include <linux/slab.h>
13 #include <linux/sysctl.h>
14 #include <linux/string.h>
15 #include <linux/bpf.h>
16 #include <linux/bpf-cgroup.h>
18 #include <net/bpf_sk_storage.h>
20 #include "../cgroup/cgroup-internal.h"
22 DEFINE_STATIC_KEY_FALSE(cgroup_bpf_enabled_key);
23 EXPORT_SYMBOL(cgroup_bpf_enabled_key);
25 void cgroup_bpf_offline(struct cgroup *cgrp)
28 percpu_ref_kill(&cgrp->bpf.refcnt);
32 * cgroup_bpf_release() - put references of all bpf programs and
33 * release all cgroup bpf data
34 * @work: work structure embedded into the cgroup to modify
36 static void cgroup_bpf_release(struct work_struct *work)
38 struct cgroup *p, *cgrp = container_of(work, struct cgroup,
40 enum bpf_cgroup_storage_type stype;
41 struct bpf_prog_array *old_array;
44 mutex_lock(&cgroup_mutex);
46 for (type = 0; type < ARRAY_SIZE(cgrp->bpf.progs); type++) {
47 struct list_head *progs = &cgrp->bpf.progs[type];
48 struct bpf_prog_list *pl, *tmp;
50 list_for_each_entry_safe(pl, tmp, progs, node) {
52 bpf_prog_put(pl->prog);
53 for_each_cgroup_storage_type(stype) {
54 bpf_cgroup_storage_unlink(pl->storage[stype]);
55 bpf_cgroup_storage_free(pl->storage[stype]);
58 static_branch_dec(&cgroup_bpf_enabled_key);
60 old_array = rcu_dereference_protected(
61 cgrp->bpf.effective[type],
62 lockdep_is_held(&cgroup_mutex));
63 bpf_prog_array_free(old_array);
66 mutex_unlock(&cgroup_mutex);
68 for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
71 percpu_ref_exit(&cgrp->bpf.refcnt);
76 * cgroup_bpf_release_fn() - callback used to schedule releasing
78 * @ref: percpu ref counter structure
80 static void cgroup_bpf_release_fn(struct percpu_ref *ref)
82 struct cgroup *cgrp = container_of(ref, struct cgroup, bpf.refcnt);
84 INIT_WORK(&cgrp->bpf.release_work, cgroup_bpf_release);
85 queue_work(system_wq, &cgrp->bpf.release_work);
88 /* count number of elements in the list.
89 * it's slow but the list cannot be long
91 static u32 prog_list_length(struct list_head *head)
93 struct bpf_prog_list *pl;
96 list_for_each_entry(pl, head, node) {
104 /* if parent has non-overridable prog attached,
105 * disallow attaching new programs to the descendent cgroup.
106 * if parent has overridable or multi-prog, allow attaching
108 static bool hierarchy_allows_attach(struct cgroup *cgrp,
109 enum bpf_attach_type type,
114 p = cgroup_parent(cgrp);
118 u32 flags = p->bpf.flags[type];
121 if (flags & BPF_F_ALLOW_MULTI)
123 cnt = prog_list_length(&p->bpf.progs[type]);
124 WARN_ON_ONCE(cnt > 1);
126 return !!(flags & BPF_F_ALLOW_OVERRIDE);
127 p = cgroup_parent(p);
132 /* compute a chain of effective programs for a given cgroup:
133 * start from the list of programs in this cgroup and add
134 * all parent programs.
135 * Note that parent's F_ALLOW_OVERRIDE-type program is yielding
136 * to programs in this cgroup
138 static int compute_effective_progs(struct cgroup *cgrp,
139 enum bpf_attach_type type,
140 struct bpf_prog_array **array)
142 enum bpf_cgroup_storage_type stype;
143 struct bpf_prog_array *progs;
144 struct bpf_prog_list *pl;
145 struct cgroup *p = cgrp;
148 /* count number of effective programs by walking parents */
150 if (cnt == 0 || (p->bpf.flags[type] & BPF_F_ALLOW_MULTI))
151 cnt += prog_list_length(&p->bpf.progs[type]);
152 p = cgroup_parent(p);
155 progs = bpf_prog_array_alloc(cnt, GFP_KERNEL);
159 /* populate the array with effective progs */
163 if (cnt > 0 && !(p->bpf.flags[type] & BPF_F_ALLOW_MULTI))
166 list_for_each_entry(pl, &p->bpf.progs[type], node) {
170 progs->items[cnt].prog = pl->prog;
171 for_each_cgroup_storage_type(stype)
172 progs->items[cnt].cgroup_storage[stype] =
176 } while ((p = cgroup_parent(p)));
182 static void activate_effective_progs(struct cgroup *cgrp,
183 enum bpf_attach_type type,
184 struct bpf_prog_array *old_array)
186 rcu_swap_protected(cgrp->bpf.effective[type], old_array,
187 lockdep_is_held(&cgroup_mutex));
188 /* free prog array after grace period, since __cgroup_bpf_run_*()
189 * might be still walking the array
191 bpf_prog_array_free(old_array);
195 * cgroup_bpf_inherit() - inherit effective programs from parent
196 * @cgrp: the cgroup to modify
198 int cgroup_bpf_inherit(struct cgroup *cgrp)
200 /* has to use marco instead of const int, since compiler thinks
201 * that array below is variable length
203 #define NR ARRAY_SIZE(cgrp->bpf.effective)
204 struct bpf_prog_array *arrays[NR] = {};
208 ret = percpu_ref_init(&cgrp->bpf.refcnt, cgroup_bpf_release_fn, 0,
213 for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
216 for (i = 0; i < NR; i++)
217 INIT_LIST_HEAD(&cgrp->bpf.progs[i]);
219 for (i = 0; i < NR; i++)
220 if (compute_effective_progs(cgrp, i, &arrays[i]))
223 for (i = 0; i < NR; i++)
224 activate_effective_progs(cgrp, i, arrays[i]);
228 for (i = 0; i < NR; i++)
229 bpf_prog_array_free(arrays[i]);
231 for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
234 percpu_ref_exit(&cgrp->bpf.refcnt);
239 static int update_effective_progs(struct cgroup *cgrp,
240 enum bpf_attach_type type)
242 struct cgroup_subsys_state *css;
245 /* allocate and recompute effective prog arrays */
246 css_for_each_descendant_pre(css, &cgrp->self) {
247 struct cgroup *desc = container_of(css, struct cgroup, self);
249 if (percpu_ref_is_zero(&desc->bpf.refcnt))
252 err = compute_effective_progs(desc, type, &desc->bpf.inactive);
257 /* all allocations were successful. Activate all prog arrays */
258 css_for_each_descendant_pre(css, &cgrp->self) {
259 struct cgroup *desc = container_of(css, struct cgroup, self);
261 if (percpu_ref_is_zero(&desc->bpf.refcnt)) {
262 if (unlikely(desc->bpf.inactive)) {
263 bpf_prog_array_free(desc->bpf.inactive);
264 desc->bpf.inactive = NULL;
269 activate_effective_progs(desc, type, desc->bpf.inactive);
270 desc->bpf.inactive = NULL;
276 /* oom while computing effective. Free all computed effective arrays
277 * since they were not activated
279 css_for_each_descendant_pre(css, &cgrp->self) {
280 struct cgroup *desc = container_of(css, struct cgroup, self);
282 bpf_prog_array_free(desc->bpf.inactive);
283 desc->bpf.inactive = NULL;
289 #define BPF_CGROUP_MAX_PROGS 64
292 * __cgroup_bpf_attach() - Attach the program to a cgroup, and
293 * propagate the change to descendants
294 * @cgrp: The cgroup which descendants to traverse
295 * @prog: A program to attach
296 * @type: Type of attach operation
297 * @flags: Option flags
299 * Must be called with cgroup_mutex held.
301 int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
302 enum bpf_attach_type type, u32 flags)
304 struct list_head *progs = &cgrp->bpf.progs[type];
305 struct bpf_prog *old_prog = NULL;
306 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
307 struct bpf_cgroup_storage *old_storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
308 enum bpf_cgroup_storage_type stype;
309 struct bpf_prog_list *pl;
310 bool pl_was_allocated;
313 if ((flags & BPF_F_ALLOW_OVERRIDE) && (flags & BPF_F_ALLOW_MULTI))
314 /* invalid combination */
317 if (!hierarchy_allows_attach(cgrp, type, flags))
320 if (!list_empty(progs) && cgrp->bpf.flags[type] != flags)
321 /* Disallow attaching non-overridable on top
322 * of existing overridable in this cgroup.
323 * Disallow attaching multi-prog if overridable or none
327 if (prog_list_length(progs) >= BPF_CGROUP_MAX_PROGS)
330 for_each_cgroup_storage_type(stype) {
331 storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
332 if (IS_ERR(storage[stype])) {
333 storage[stype] = NULL;
334 for_each_cgroup_storage_type(stype)
335 bpf_cgroup_storage_free(storage[stype]);
340 if (flags & BPF_F_ALLOW_MULTI) {
341 list_for_each_entry(pl, progs, node) {
342 if (pl->prog == prog) {
343 /* disallow attaching the same prog twice */
344 for_each_cgroup_storage_type(stype)
345 bpf_cgroup_storage_free(storage[stype]);
350 pl = kmalloc(sizeof(*pl), GFP_KERNEL);
352 for_each_cgroup_storage_type(stype)
353 bpf_cgroup_storage_free(storage[stype]);
357 pl_was_allocated = true;
359 for_each_cgroup_storage_type(stype)
360 pl->storage[stype] = storage[stype];
361 list_add_tail(&pl->node, progs);
363 if (list_empty(progs)) {
364 pl = kmalloc(sizeof(*pl), GFP_KERNEL);
366 for_each_cgroup_storage_type(stype)
367 bpf_cgroup_storage_free(storage[stype]);
370 pl_was_allocated = true;
371 list_add_tail(&pl->node, progs);
373 pl = list_first_entry(progs, typeof(*pl), node);
375 for_each_cgroup_storage_type(stype) {
376 old_storage[stype] = pl->storage[stype];
377 bpf_cgroup_storage_unlink(old_storage[stype]);
379 pl_was_allocated = false;
382 for_each_cgroup_storage_type(stype)
383 pl->storage[stype] = storage[stype];
386 cgrp->bpf.flags[type] = flags;
388 err = update_effective_progs(cgrp, type);
392 static_branch_inc(&cgroup_bpf_enabled_key);
393 for_each_cgroup_storage_type(stype) {
394 if (!old_storage[stype])
396 bpf_cgroup_storage_free(old_storage[stype]);
399 bpf_prog_put(old_prog);
400 static_branch_dec(&cgroup_bpf_enabled_key);
402 for_each_cgroup_storage_type(stype)
403 bpf_cgroup_storage_link(storage[stype], cgrp, type);
407 /* and cleanup the prog list */
409 for_each_cgroup_storage_type(stype) {
410 bpf_cgroup_storage_free(pl->storage[stype]);
411 pl->storage[stype] = old_storage[stype];
412 bpf_cgroup_storage_link(old_storage[stype], cgrp, type);
414 if (pl_was_allocated) {
422 * __cgroup_bpf_detach() - Detach the program from a cgroup, and
423 * propagate the change to descendants
424 * @cgrp: The cgroup which descendants to traverse
425 * @prog: A program to detach or NULL
426 * @type: Type of detach operation
428 * Must be called with cgroup_mutex held.
430 int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
431 enum bpf_attach_type type)
433 struct list_head *progs = &cgrp->bpf.progs[type];
434 enum bpf_cgroup_storage_type stype;
435 u32 flags = cgrp->bpf.flags[type];
436 struct bpf_prog *old_prog = NULL;
437 struct bpf_prog_list *pl;
440 if (flags & BPF_F_ALLOW_MULTI) {
442 /* to detach MULTI prog the user has to specify valid FD
443 * of the program to be detached
447 if (list_empty(progs))
448 /* report error when trying to detach and nothing is attached */
452 if (flags & BPF_F_ALLOW_MULTI) {
453 /* find the prog and detach it */
454 list_for_each_entry(pl, progs, node) {
455 if (pl->prog != prog)
458 /* mark it deleted, so it's ignored while
459 * recomputing effective
467 /* to maintain backward compatibility NONE and OVERRIDE cgroups
468 * allow detaching with invalid FD (prog==NULL)
470 pl = list_first_entry(progs, typeof(*pl), node);
475 err = update_effective_progs(cgrp, type);
479 /* now can actually delete it from this cgroup list */
481 for_each_cgroup_storage_type(stype) {
482 bpf_cgroup_storage_unlink(pl->storage[stype]);
483 bpf_cgroup_storage_free(pl->storage[stype]);
486 if (list_empty(progs))
487 /* last program was detached, reset flags to zero */
488 cgrp->bpf.flags[type] = 0;
490 bpf_prog_put(old_prog);
491 static_branch_dec(&cgroup_bpf_enabled_key);
495 /* and restore back old_prog */
500 /* Must be called with cgroup_mutex held to avoid races. */
501 int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
502 union bpf_attr __user *uattr)
504 __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
505 enum bpf_attach_type type = attr->query.attach_type;
506 struct list_head *progs = &cgrp->bpf.progs[type];
507 u32 flags = cgrp->bpf.flags[type];
508 struct bpf_prog_array *effective;
511 effective = rcu_dereference_protected(cgrp->bpf.effective[type],
512 lockdep_is_held(&cgroup_mutex));
514 if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE)
515 cnt = bpf_prog_array_length(effective);
517 cnt = prog_list_length(progs);
519 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
521 if (copy_to_user(&uattr->query.prog_cnt, &cnt, sizeof(cnt)))
523 if (attr->query.prog_cnt == 0 || !prog_ids || !cnt)
524 /* return early if user requested only program count + flags */
526 if (attr->query.prog_cnt < cnt) {
527 cnt = attr->query.prog_cnt;
531 if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
532 return bpf_prog_array_copy_to_user(effective, prog_ids, cnt);
534 struct bpf_prog_list *pl;
538 list_for_each_entry(pl, progs, node) {
539 id = pl->prog->aux->id;
540 if (copy_to_user(prog_ids + i, &id, sizeof(id)))
549 int cgroup_bpf_prog_attach(const union bpf_attr *attr,
550 enum bpf_prog_type ptype, struct bpf_prog *prog)
555 cgrp = cgroup_get_from_fd(attr->target_fd);
557 return PTR_ERR(cgrp);
559 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
565 int cgroup_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
567 struct bpf_prog *prog;
571 cgrp = cgroup_get_from_fd(attr->target_fd);
573 return PTR_ERR(cgrp);
575 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
579 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
587 int cgroup_bpf_prog_query(const union bpf_attr *attr,
588 union bpf_attr __user *uattr)
593 cgrp = cgroup_get_from_fd(attr->query.target_fd);
595 return PTR_ERR(cgrp);
597 ret = cgroup_bpf_query(cgrp, attr, uattr);
604 * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
605 * @sk: The socket sending or receiving traffic
606 * @skb: The skb that is being sent or received
607 * @type: The type of program to be exectuted
609 * If no socket is passed, or the socket is not of type INET or INET6,
610 * this function does nothing and returns 0.
612 * The program type passed in via @type must be suitable for network
613 * filtering. No further check is performed to assert that.
615 * For egress packets, this function can return:
616 * NET_XMIT_SUCCESS (0) - continue with packet output
617 * NET_XMIT_DROP (1) - drop packet and notify TCP to call cwr
618 * NET_XMIT_CN (2) - continue with packet output and notify TCP
620 * -EPERM - drop packet
622 * For ingress packets, this function will return -EPERM if any
623 * attached program was found and if it returned != 1 during execution.
624 * Otherwise 0 is returned.
626 int __cgroup_bpf_run_filter_skb(struct sock *sk,
628 enum bpf_attach_type type)
630 unsigned int offset = skb->data - skb_network_header(skb);
631 struct sock *save_sk;
632 void *saved_data_end;
636 if (!sk || !sk_fullsock(sk))
639 if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
642 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
645 __skb_push(skb, offset);
647 /* compute pointers for the bpf prog */
648 bpf_compute_and_save_data_end(skb, &saved_data_end);
650 if (type == BPF_CGROUP_INET_EGRESS) {
651 ret = BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(
652 cgrp->bpf.effective[type], skb, __bpf_prog_run_save_cb);
654 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], skb,
655 __bpf_prog_run_save_cb);
656 ret = (ret == 1 ? 0 : -EPERM);
658 bpf_restore_data_end(skb, saved_data_end);
659 __skb_pull(skb, offset);
664 EXPORT_SYMBOL(__cgroup_bpf_run_filter_skb);
667 * __cgroup_bpf_run_filter_sk() - Run a program on a sock
668 * @sk: sock structure to manipulate
669 * @type: The type of program to be exectuted
671 * socket is passed is expected to be of type INET or INET6.
673 * The program type passed in via @type must be suitable for sock
674 * filtering. No further check is performed to assert that.
676 * This function will return %-EPERM if any if an attached program was found
677 * and if it returned != 1 during execution. In all other cases, 0 is returned.
679 int __cgroup_bpf_run_filter_sk(struct sock *sk,
680 enum bpf_attach_type type)
682 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
685 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], sk, BPF_PROG_RUN);
686 return ret == 1 ? 0 : -EPERM;
688 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sk);
691 * __cgroup_bpf_run_filter_sock_addr() - Run a program on a sock and
692 * provided by user sockaddr
693 * @sk: sock struct that will use sockaddr
694 * @uaddr: sockaddr struct provided by user
695 * @type: The type of program to be exectuted
696 * @t_ctx: Pointer to attach type specific context
698 * socket is expected to be of type INET or INET6.
700 * This function will return %-EPERM if an attached program is found and
701 * returned value != 1 during execution. In all other cases, 0 is returned.
703 int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
704 struct sockaddr *uaddr,
705 enum bpf_attach_type type,
708 struct bpf_sock_addr_kern ctx = {
713 struct sockaddr_storage unspec;
717 /* Check socket family since not all sockets represent network
718 * endpoint (e.g. AF_UNIX).
720 if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
724 memset(&unspec, 0, sizeof(unspec));
725 ctx.uaddr = (struct sockaddr *)&unspec;
728 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
729 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], &ctx, BPF_PROG_RUN);
731 return ret == 1 ? 0 : -EPERM;
733 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_addr);
736 * __cgroup_bpf_run_filter_sock_ops() - Run a program on a sock
737 * @sk: socket to get cgroup from
738 * @sock_ops: bpf_sock_ops_kern struct to pass to program. Contains
739 * sk with connection information (IP addresses, etc.) May not contain
740 * cgroup info if it is a req sock.
741 * @type: The type of program to be exectuted
743 * socket passed is expected to be of type INET or INET6.
745 * The program type passed in via @type must be suitable for sock_ops
746 * filtering. No further check is performed to assert that.
748 * This function will return %-EPERM if any if an attached program was found
749 * and if it returned != 1 during execution. In all other cases, 0 is returned.
751 int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
752 struct bpf_sock_ops_kern *sock_ops,
753 enum bpf_attach_type type)
755 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
758 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], sock_ops,
760 return ret == 1 ? 0 : -EPERM;
762 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_ops);
764 int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
765 short access, enum bpf_attach_type type)
768 struct bpf_cgroup_dev_ctx ctx = {
769 .access_type = (access << 16) | dev_type,
776 cgrp = task_dfl_cgroup(current);
777 allow = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], &ctx,
783 EXPORT_SYMBOL(__cgroup_bpf_check_dev_permission);
785 static const struct bpf_func_proto *
786 cgroup_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
789 case BPF_FUNC_map_lookup_elem:
790 return &bpf_map_lookup_elem_proto;
791 case BPF_FUNC_map_update_elem:
792 return &bpf_map_update_elem_proto;
793 case BPF_FUNC_map_delete_elem:
794 return &bpf_map_delete_elem_proto;
795 case BPF_FUNC_map_push_elem:
796 return &bpf_map_push_elem_proto;
797 case BPF_FUNC_map_pop_elem:
798 return &bpf_map_pop_elem_proto;
799 case BPF_FUNC_map_peek_elem:
800 return &bpf_map_peek_elem_proto;
801 case BPF_FUNC_get_current_uid_gid:
802 return &bpf_get_current_uid_gid_proto;
803 case BPF_FUNC_get_local_storage:
804 return &bpf_get_local_storage_proto;
805 case BPF_FUNC_get_current_cgroup_id:
806 return &bpf_get_current_cgroup_id_proto;
807 case BPF_FUNC_trace_printk:
808 if (capable(CAP_SYS_ADMIN))
809 return bpf_get_trace_printk_proto();
816 static const struct bpf_func_proto *
817 cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
819 return cgroup_base_func_proto(func_id, prog);
822 static bool cgroup_dev_is_valid_access(int off, int size,
823 enum bpf_access_type type,
824 const struct bpf_prog *prog,
825 struct bpf_insn_access_aux *info)
827 const int size_default = sizeof(__u32);
829 if (type == BPF_WRITE)
832 if (off < 0 || off + size > sizeof(struct bpf_cgroup_dev_ctx))
834 /* The verifier guarantees that size > 0. */
839 case bpf_ctx_range(struct bpf_cgroup_dev_ctx, access_type):
840 bpf_ctx_record_field_size(info, size_default);
841 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
845 if (size != size_default)
852 const struct bpf_prog_ops cg_dev_prog_ops = {
855 const struct bpf_verifier_ops cg_dev_verifier_ops = {
856 .get_func_proto = cgroup_dev_func_proto,
857 .is_valid_access = cgroup_dev_is_valid_access,
861 * __cgroup_bpf_run_filter_sysctl - Run a program on sysctl
863 * @head: sysctl table header
864 * @table: sysctl table
865 * @write: sysctl is being read (= 0) or written (= 1)
866 * @buf: pointer to buffer passed by user space
867 * @pcount: value-result argument: value is size of buffer pointed to by @buf,
868 * result is size of @new_buf if program set new value, initial value
870 * @ppos: value-result argument: value is position at which read from or write
871 * to sysctl is happening, result is new position if program overrode it,
872 * initial value otherwise
873 * @new_buf: pointer to pointer to new buffer that will be allocated if program
874 * overrides new value provided by user space on sysctl write
875 * NOTE: it's caller responsibility to free *new_buf if it was set
876 * @type: type of program to be executed
878 * Program is run when sysctl is being accessed, either read or written, and
879 * can allow or deny such access.
881 * This function will return %-EPERM if an attached program is found and
882 * returned value != 1 during execution. In all other cases 0 is returned.
884 int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
885 struct ctl_table *table, int write,
886 void __user *buf, size_t *pcount,
887 loff_t *ppos, void **new_buf,
888 enum bpf_attach_type type)
890 struct bpf_sysctl_kern ctx = {
896 .cur_len = PAGE_SIZE,
904 ctx.cur_val = kmalloc_track_caller(ctx.cur_len, GFP_KERNEL);
911 if (table->proc_handler(table, 0, (void __user *)ctx.cur_val,
912 &ctx.cur_len, &pos)) {
913 /* Let BPF program decide how to proceed. */
918 /* Let BPF program decide how to proceed. */
922 if (write && buf && *pcount) {
923 /* BPF program should be able to override new value with a
924 * buffer bigger than provided by user.
926 ctx.new_val = kmalloc_track_caller(PAGE_SIZE, GFP_KERNEL);
927 ctx.new_len = min_t(size_t, PAGE_SIZE, *pcount);
929 copy_from_user(ctx.new_val, buf, ctx.new_len))
930 /* Let BPF program decide how to proceed. */
935 cgrp = task_dfl_cgroup(current);
936 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], &ctx, BPF_PROG_RUN);
941 if (ret == 1 && ctx.new_updated) {
942 *new_buf = ctx.new_val;
943 *pcount = ctx.new_len;
948 return ret == 1 ? 0 : -EPERM;
950 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sysctl);
953 static bool __cgroup_bpf_prog_array_is_empty(struct cgroup *cgrp,
954 enum bpf_attach_type attach_type)
956 struct bpf_prog_array *prog_array;
960 prog_array = rcu_dereference(cgrp->bpf.effective[attach_type]);
961 empty = bpf_prog_array_is_empty(prog_array);
967 static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen)
969 if (unlikely(max_optlen < 0))
972 if (unlikely(max_optlen > PAGE_SIZE)) {
973 /* We don't expose optvals that are greater than PAGE_SIZE
974 * to the BPF program.
976 max_optlen = PAGE_SIZE;
979 ctx->optval = kzalloc(max_optlen, GFP_USER);
983 ctx->optval_end = ctx->optval + max_optlen;
988 static void sockopt_free_buf(struct bpf_sockopt_kern *ctx)
993 int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
994 int *optname, char __user *optval,
995 int *optlen, char **kernel_optval)
997 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
998 struct bpf_sockopt_kern ctx = {
1001 .optname = *optname,
1003 int ret, max_optlen;
1005 /* Opportunistic check to see whether we have any BPF program
1006 * attached to the hook so we don't waste time allocating
1007 * memory and locking the socket.
1009 if (!cgroup_bpf_enabled ||
1010 __cgroup_bpf_prog_array_is_empty(cgrp, BPF_CGROUP_SETSOCKOPT))
1013 /* Allocate a bit more than the initial user buffer for
1014 * BPF program. The canonical use case is overriding
1015 * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic).
1017 max_optlen = max_t(int, 16, *optlen);
1019 max_optlen = sockopt_alloc_buf(&ctx, max_optlen);
1023 ctx.optlen = *optlen;
1025 if (copy_from_user(ctx.optval, optval, min(*optlen, max_optlen)) != 0) {
1031 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[BPF_CGROUP_SETSOCKOPT],
1032 &ctx, BPF_PROG_RUN);
1040 if (ctx.optlen == -1) {
1041 /* optlen set to -1, bypass kernel */
1043 } else if (ctx.optlen > max_optlen || ctx.optlen < -1) {
1044 /* optlen is out of bounds */
1047 /* optlen within bounds, run kernel handler */
1050 /* export any potential modifications */
1052 *optname = ctx.optname;
1054 /* optlen == 0 from BPF indicates that we should
1055 * use original userspace data.
1057 if (ctx.optlen != 0) {
1058 *optlen = ctx.optlen;
1059 *kernel_optval = ctx.optval;
1060 /* export and don't free sockopt buf */
1066 sockopt_free_buf(&ctx);
1069 EXPORT_SYMBOL(__cgroup_bpf_run_filter_setsockopt);
1071 int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
1072 int optname, char __user *optval,
1073 int __user *optlen, int max_optlen,
1076 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1077 struct bpf_sockopt_kern ctx = {
1085 /* Opportunistic check to see whether we have any BPF program
1086 * attached to the hook so we don't waste time allocating
1087 * memory and locking the socket.
1089 if (!cgroup_bpf_enabled ||
1090 __cgroup_bpf_prog_array_is_empty(cgrp, BPF_CGROUP_GETSOCKOPT))
1093 ctx.optlen = max_optlen;
1095 max_optlen = sockopt_alloc_buf(&ctx, max_optlen);
1100 /* If kernel getsockopt finished successfully,
1101 * copy whatever was returned to the user back
1102 * into our temporary buffer. Set optlen to the
1103 * one that kernel returned as well to let
1104 * BPF programs inspect the value.
1107 if (get_user(ctx.optlen, optlen)) {
1112 if (ctx.optlen < 0) {
1117 if (copy_from_user(ctx.optval, optval,
1118 min(ctx.optlen, max_optlen)) != 0) {
1125 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[BPF_CGROUP_GETSOCKOPT],
1126 &ctx, BPF_PROG_RUN);
1134 if (ctx.optlen > max_optlen || ctx.optlen < 0) {
1139 /* BPF programs only allowed to set retval to 0, not some
1142 if (ctx.retval != 0 && ctx.retval != retval) {
1147 if (ctx.optlen != 0) {
1148 if (copy_to_user(optval, ctx.optval, ctx.optlen) ||
1149 put_user(ctx.optlen, optlen)) {
1158 sockopt_free_buf(&ctx);
1161 EXPORT_SYMBOL(__cgroup_bpf_run_filter_getsockopt);
1164 static ssize_t sysctl_cpy_dir(const struct ctl_dir *dir, char **bufp,
1167 ssize_t tmp_ret = 0, ret;
1169 if (dir->header.parent) {
1170 tmp_ret = sysctl_cpy_dir(dir->header.parent, bufp, lenp);
1175 ret = strscpy(*bufp, dir->header.ctl_table[0].procname, *lenp);
1182 /* Avoid leading slash. */
1186 tmp_ret = strscpy(*bufp, "/", *lenp);
1192 return ret + tmp_ret;
1195 BPF_CALL_4(bpf_sysctl_get_name, struct bpf_sysctl_kern *, ctx, char *, buf,
1196 size_t, buf_len, u64, flags)
1198 ssize_t tmp_ret = 0, ret;
1203 if (!(flags & BPF_F_SYSCTL_BASE_NAME)) {
1206 tmp_ret = sysctl_cpy_dir(ctx->head->parent, &buf, &buf_len);
1211 ret = strscpy(buf, ctx->table->procname, buf_len);
1213 return ret < 0 ? ret : tmp_ret + ret;
1216 static const struct bpf_func_proto bpf_sysctl_get_name_proto = {
1217 .func = bpf_sysctl_get_name,
1219 .ret_type = RET_INTEGER,
1220 .arg1_type = ARG_PTR_TO_CTX,
1221 .arg2_type = ARG_PTR_TO_MEM,
1222 .arg3_type = ARG_CONST_SIZE,
1223 .arg4_type = ARG_ANYTHING,
1226 static int copy_sysctl_value(char *dst, size_t dst_len, char *src,
1235 if (!src || !src_len) {
1236 memset(dst, 0, dst_len);
1240 memcpy(dst, src, min(dst_len, src_len));
1242 if (dst_len > src_len) {
1243 memset(dst + src_len, '\0', dst_len - src_len);
1247 dst[dst_len - 1] = '\0';
1252 BPF_CALL_3(bpf_sysctl_get_current_value, struct bpf_sysctl_kern *, ctx,
1253 char *, buf, size_t, buf_len)
1255 return copy_sysctl_value(buf, buf_len, ctx->cur_val, ctx->cur_len);
1258 static const struct bpf_func_proto bpf_sysctl_get_current_value_proto = {
1259 .func = bpf_sysctl_get_current_value,
1261 .ret_type = RET_INTEGER,
1262 .arg1_type = ARG_PTR_TO_CTX,
1263 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1264 .arg3_type = ARG_CONST_SIZE,
1267 BPF_CALL_3(bpf_sysctl_get_new_value, struct bpf_sysctl_kern *, ctx, char *, buf,
1272 memset(buf, '\0', buf_len);
1275 return copy_sysctl_value(buf, buf_len, ctx->new_val, ctx->new_len);
1278 static const struct bpf_func_proto bpf_sysctl_get_new_value_proto = {
1279 .func = bpf_sysctl_get_new_value,
1281 .ret_type = RET_INTEGER,
1282 .arg1_type = ARG_PTR_TO_CTX,
1283 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1284 .arg3_type = ARG_CONST_SIZE,
1287 BPF_CALL_3(bpf_sysctl_set_new_value, struct bpf_sysctl_kern *, ctx,
1288 const char *, buf, size_t, buf_len)
1290 if (!ctx->write || !ctx->new_val || !ctx->new_len || !buf || !buf_len)
1293 if (buf_len > PAGE_SIZE - 1)
1296 memcpy(ctx->new_val, buf, buf_len);
1297 ctx->new_len = buf_len;
1298 ctx->new_updated = 1;
1303 static const struct bpf_func_proto bpf_sysctl_set_new_value_proto = {
1304 .func = bpf_sysctl_set_new_value,
1306 .ret_type = RET_INTEGER,
1307 .arg1_type = ARG_PTR_TO_CTX,
1308 .arg2_type = ARG_PTR_TO_MEM,
1309 .arg3_type = ARG_CONST_SIZE,
1312 static const struct bpf_func_proto *
1313 sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1316 case BPF_FUNC_strtol:
1317 return &bpf_strtol_proto;
1318 case BPF_FUNC_strtoul:
1319 return &bpf_strtoul_proto;
1320 case BPF_FUNC_sysctl_get_name:
1321 return &bpf_sysctl_get_name_proto;
1322 case BPF_FUNC_sysctl_get_current_value:
1323 return &bpf_sysctl_get_current_value_proto;
1324 case BPF_FUNC_sysctl_get_new_value:
1325 return &bpf_sysctl_get_new_value_proto;
1326 case BPF_FUNC_sysctl_set_new_value:
1327 return &bpf_sysctl_set_new_value_proto;
1329 return cgroup_base_func_proto(func_id, prog);
1333 static bool sysctl_is_valid_access(int off, int size, enum bpf_access_type type,
1334 const struct bpf_prog *prog,
1335 struct bpf_insn_access_aux *info)
1337 const int size_default = sizeof(__u32);
1339 if (off < 0 || off + size > sizeof(struct bpf_sysctl) || off % size)
1343 case bpf_ctx_range(struct bpf_sysctl, write):
1344 if (type != BPF_READ)
1346 bpf_ctx_record_field_size(info, size_default);
1347 return bpf_ctx_narrow_access_ok(off, size, size_default);
1348 case bpf_ctx_range(struct bpf_sysctl, file_pos):
1349 if (type == BPF_READ) {
1350 bpf_ctx_record_field_size(info, size_default);
1351 return bpf_ctx_narrow_access_ok(off, size, size_default);
1353 return size == size_default;
1360 static u32 sysctl_convert_ctx_access(enum bpf_access_type type,
1361 const struct bpf_insn *si,
1362 struct bpf_insn *insn_buf,
1363 struct bpf_prog *prog, u32 *target_size)
1365 struct bpf_insn *insn = insn_buf;
1369 case offsetof(struct bpf_sysctl, write):
1370 *insn++ = BPF_LDX_MEM(
1371 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
1372 bpf_target_off(struct bpf_sysctl_kern, write,
1373 FIELD_SIZEOF(struct bpf_sysctl_kern,
1377 case offsetof(struct bpf_sysctl, file_pos):
1378 /* ppos is a pointer so it should be accessed via indirect
1379 * loads and stores. Also for stores additional temporary
1380 * register is used since neither src_reg nor dst_reg can be
1383 if (type == BPF_WRITE) {
1384 int treg = BPF_REG_9;
1386 if (si->src_reg == treg || si->dst_reg == treg)
1388 if (si->src_reg == treg || si->dst_reg == treg)
1390 *insn++ = BPF_STX_MEM(
1391 BPF_DW, si->dst_reg, treg,
1392 offsetof(struct bpf_sysctl_kern, tmp_reg));
1393 *insn++ = BPF_LDX_MEM(
1394 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
1396 offsetof(struct bpf_sysctl_kern, ppos));
1397 *insn++ = BPF_STX_MEM(
1398 BPF_SIZEOF(u32), treg, si->src_reg,
1399 bpf_ctx_narrow_access_offset(
1400 0, sizeof(u32), sizeof(loff_t)));
1401 *insn++ = BPF_LDX_MEM(
1402 BPF_DW, treg, si->dst_reg,
1403 offsetof(struct bpf_sysctl_kern, tmp_reg));
1405 *insn++ = BPF_LDX_MEM(
1406 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
1407 si->dst_reg, si->src_reg,
1408 offsetof(struct bpf_sysctl_kern, ppos));
1409 read_size = bpf_size_to_bytes(BPF_SIZE(si->code));
1410 *insn++ = BPF_LDX_MEM(
1411 BPF_SIZE(si->code), si->dst_reg, si->dst_reg,
1412 bpf_ctx_narrow_access_offset(
1413 0, read_size, sizeof(loff_t)));
1415 *target_size = sizeof(u32);
1419 return insn - insn_buf;
1422 const struct bpf_verifier_ops cg_sysctl_verifier_ops = {
1423 .get_func_proto = sysctl_func_proto,
1424 .is_valid_access = sysctl_is_valid_access,
1425 .convert_ctx_access = sysctl_convert_ctx_access,
1428 const struct bpf_prog_ops cg_sysctl_prog_ops = {
1431 static const struct bpf_func_proto *
1432 cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1436 case BPF_FUNC_sk_storage_get:
1437 return &bpf_sk_storage_get_proto;
1438 case BPF_FUNC_sk_storage_delete:
1439 return &bpf_sk_storage_delete_proto;
1442 case BPF_FUNC_tcp_sock:
1443 return &bpf_tcp_sock_proto;
1446 return cgroup_base_func_proto(func_id, prog);
1450 static bool cg_sockopt_is_valid_access(int off, int size,
1451 enum bpf_access_type type,
1452 const struct bpf_prog *prog,
1453 struct bpf_insn_access_aux *info)
1455 const int size_default = sizeof(__u32);
1457 if (off < 0 || off >= sizeof(struct bpf_sockopt))
1460 if (off % size != 0)
1463 if (type == BPF_WRITE) {
1465 case offsetof(struct bpf_sockopt, retval):
1466 if (size != size_default)
1468 return prog->expected_attach_type ==
1469 BPF_CGROUP_GETSOCKOPT;
1470 case offsetof(struct bpf_sockopt, optname):
1472 case offsetof(struct bpf_sockopt, level):
1473 if (size != size_default)
1475 return prog->expected_attach_type ==
1476 BPF_CGROUP_SETSOCKOPT;
1477 case offsetof(struct bpf_sockopt, optlen):
1478 return size == size_default;
1485 case offsetof(struct bpf_sockopt, sk):
1486 if (size != sizeof(__u64))
1488 info->reg_type = PTR_TO_SOCKET;
1490 case offsetof(struct bpf_sockopt, optval):
1491 if (size != sizeof(__u64))
1493 info->reg_type = PTR_TO_PACKET;
1495 case offsetof(struct bpf_sockopt, optval_end):
1496 if (size != sizeof(__u64))
1498 info->reg_type = PTR_TO_PACKET_END;
1500 case offsetof(struct bpf_sockopt, retval):
1501 if (size != size_default)
1503 return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT;
1505 if (size != size_default)
1512 #define CG_SOCKOPT_ACCESS_FIELD(T, F) \
1513 T(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, F), \
1514 si->dst_reg, si->src_reg, \
1515 offsetof(struct bpf_sockopt_kern, F))
1517 static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type,
1518 const struct bpf_insn *si,
1519 struct bpf_insn *insn_buf,
1520 struct bpf_prog *prog,
1523 struct bpf_insn *insn = insn_buf;
1526 case offsetof(struct bpf_sockopt, sk):
1527 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, sk);
1529 case offsetof(struct bpf_sockopt, level):
1530 if (type == BPF_WRITE)
1531 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, level);
1533 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, level);
1535 case offsetof(struct bpf_sockopt, optname):
1536 if (type == BPF_WRITE)
1537 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optname);
1539 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optname);
1541 case offsetof(struct bpf_sockopt, optlen):
1542 if (type == BPF_WRITE)
1543 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optlen);
1545 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optlen);
1547 case offsetof(struct bpf_sockopt, retval):
1548 if (type == BPF_WRITE)
1549 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, retval);
1551 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, retval);
1553 case offsetof(struct bpf_sockopt, optval):
1554 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval);
1556 case offsetof(struct bpf_sockopt, optval_end):
1557 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval_end);
1561 return insn - insn_buf;
1564 static int cg_sockopt_get_prologue(struct bpf_insn *insn_buf,
1566 const struct bpf_prog *prog)
1568 /* Nothing to do for sockopt argument. The data is kzalloc'ated.
1573 const struct bpf_verifier_ops cg_sockopt_verifier_ops = {
1574 .get_func_proto = cg_sockopt_func_proto,
1575 .is_valid_access = cg_sockopt_is_valid_access,
1576 .convert_ctx_access = cg_sockopt_convert_ctx_access,
1577 .gen_prologue = cg_sockopt_get_prologue,
1580 const struct bpf_prog_ops cg_sockopt_prog_ops = {