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_ARRAY_FALSE(cgroup_bpf_enabled_key, MAX_CGROUP_BPF_ATTACH_TYPE);
23 EXPORT_SYMBOL(cgroup_bpf_enabled_key);
25 /* __always_inline is necessary to prevent indirect call through run_prog
28 static __always_inline int
29 bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp,
30 enum cgroup_bpf_attach_type atype,
31 const void *ctx, bpf_prog_run_fn run_prog,
32 int retval, u32 *ret_flags)
34 const struct bpf_prog_array_item *item;
35 const struct bpf_prog *prog;
36 const struct bpf_prog_array *array;
37 struct bpf_run_ctx *old_run_ctx;
38 struct bpf_cg_run_ctx run_ctx;
41 run_ctx.retval = retval;
44 array = rcu_dereference(cgrp->effective[atype]);
45 item = &array->items[0];
46 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
47 while ((prog = READ_ONCE(item->prog))) {
48 run_ctx.prog_item = item;
49 func_ret = run_prog(prog, ctx);
51 *(ret_flags) |= (func_ret >> 1);
54 if (!func_ret && !IS_ERR_VALUE((long)run_ctx.retval))
55 run_ctx.retval = -EPERM;
58 bpf_reset_run_ctx(old_run_ctx);
61 return run_ctx.retval;
64 void cgroup_bpf_offline(struct cgroup *cgrp)
67 percpu_ref_kill(&cgrp->bpf.refcnt);
70 static void bpf_cgroup_storages_free(struct bpf_cgroup_storage *storages[])
72 enum bpf_cgroup_storage_type stype;
74 for_each_cgroup_storage_type(stype)
75 bpf_cgroup_storage_free(storages[stype]);
78 static int bpf_cgroup_storages_alloc(struct bpf_cgroup_storage *storages[],
79 struct bpf_cgroup_storage *new_storages[],
80 enum bpf_attach_type type,
81 struct bpf_prog *prog,
84 enum bpf_cgroup_storage_type stype;
85 struct bpf_cgroup_storage_key key;
88 key.cgroup_inode_id = cgroup_id(cgrp);
89 key.attach_type = type;
91 for_each_cgroup_storage_type(stype) {
92 map = prog->aux->cgroup_storage[stype];
96 storages[stype] = cgroup_storage_lookup((void *)map, &key, false);
100 storages[stype] = bpf_cgroup_storage_alloc(prog, stype);
101 if (IS_ERR(storages[stype])) {
102 bpf_cgroup_storages_free(new_storages);
106 new_storages[stype] = storages[stype];
112 static void bpf_cgroup_storages_assign(struct bpf_cgroup_storage *dst[],
113 struct bpf_cgroup_storage *src[])
115 enum bpf_cgroup_storage_type stype;
117 for_each_cgroup_storage_type(stype)
118 dst[stype] = src[stype];
121 static void bpf_cgroup_storages_link(struct bpf_cgroup_storage *storages[],
123 enum bpf_attach_type attach_type)
125 enum bpf_cgroup_storage_type stype;
127 for_each_cgroup_storage_type(stype)
128 bpf_cgroup_storage_link(storages[stype], cgrp, attach_type);
131 /* Called when bpf_cgroup_link is auto-detached from dying cgroup.
132 * It drops cgroup and bpf_prog refcounts, and marks bpf_link as defunct. It
133 * doesn't free link memory, which will eventually be done by bpf_link's
134 * release() callback, when its last FD is closed.
136 static void bpf_cgroup_link_auto_detach(struct bpf_cgroup_link *link)
138 cgroup_put(link->cgroup);
143 * cgroup_bpf_release() - put references of all bpf programs and
144 * release all cgroup bpf data
145 * @work: work structure embedded into the cgroup to modify
147 static void cgroup_bpf_release(struct work_struct *work)
149 struct cgroup *p, *cgrp = container_of(work, struct cgroup,
151 struct bpf_prog_array *old_array;
152 struct list_head *storages = &cgrp->bpf.storages;
153 struct bpf_cgroup_storage *storage, *stmp;
157 mutex_lock(&cgroup_mutex);
159 for (atype = 0; atype < ARRAY_SIZE(cgrp->bpf.progs); atype++) {
160 struct list_head *progs = &cgrp->bpf.progs[atype];
161 struct bpf_prog_list *pl, *pltmp;
163 list_for_each_entry_safe(pl, pltmp, progs, node) {
166 bpf_prog_put(pl->prog);
168 bpf_cgroup_link_auto_detach(pl->link);
170 static_branch_dec(&cgroup_bpf_enabled_key[atype]);
172 old_array = rcu_dereference_protected(
173 cgrp->bpf.effective[atype],
174 lockdep_is_held(&cgroup_mutex));
175 bpf_prog_array_free(old_array);
178 list_for_each_entry_safe(storage, stmp, storages, list_cg) {
179 bpf_cgroup_storage_unlink(storage);
180 bpf_cgroup_storage_free(storage);
183 mutex_unlock(&cgroup_mutex);
185 for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
188 percpu_ref_exit(&cgrp->bpf.refcnt);
193 * cgroup_bpf_release_fn() - callback used to schedule releasing
195 * @ref: percpu ref counter structure
197 static void cgroup_bpf_release_fn(struct percpu_ref *ref)
199 struct cgroup *cgrp = container_of(ref, struct cgroup, bpf.refcnt);
201 INIT_WORK(&cgrp->bpf.release_work, cgroup_bpf_release);
202 queue_work(system_wq, &cgrp->bpf.release_work);
205 /* Get underlying bpf_prog of bpf_prog_list entry, regardless if it's through
206 * link or direct prog.
208 static struct bpf_prog *prog_list_prog(struct bpf_prog_list *pl)
213 return pl->link->link.prog;
217 /* count number of elements in the list.
218 * it's slow but the list cannot be long
220 static u32 prog_list_length(struct list_head *head)
222 struct bpf_prog_list *pl;
225 list_for_each_entry(pl, head, node) {
226 if (!prog_list_prog(pl))
233 /* if parent has non-overridable prog attached,
234 * disallow attaching new programs to the descendent cgroup.
235 * if parent has overridable or multi-prog, allow attaching
237 static bool hierarchy_allows_attach(struct cgroup *cgrp,
238 enum cgroup_bpf_attach_type atype)
242 p = cgroup_parent(cgrp);
246 u32 flags = p->bpf.flags[atype];
249 if (flags & BPF_F_ALLOW_MULTI)
251 cnt = prog_list_length(&p->bpf.progs[atype]);
252 WARN_ON_ONCE(cnt > 1);
254 return !!(flags & BPF_F_ALLOW_OVERRIDE);
255 p = cgroup_parent(p);
260 /* compute a chain of effective programs for a given cgroup:
261 * start from the list of programs in this cgroup and add
262 * all parent programs.
263 * Note that parent's F_ALLOW_OVERRIDE-type program is yielding
264 * to programs in this cgroup
266 static int compute_effective_progs(struct cgroup *cgrp,
267 enum cgroup_bpf_attach_type atype,
268 struct bpf_prog_array **array)
270 struct bpf_prog_array_item *item;
271 struct bpf_prog_array *progs;
272 struct bpf_prog_list *pl;
273 struct cgroup *p = cgrp;
276 /* count number of effective programs by walking parents */
278 if (cnt == 0 || (p->bpf.flags[atype] & BPF_F_ALLOW_MULTI))
279 cnt += prog_list_length(&p->bpf.progs[atype]);
280 p = cgroup_parent(p);
283 progs = bpf_prog_array_alloc(cnt, GFP_KERNEL);
287 /* populate the array with effective progs */
291 if (cnt > 0 && !(p->bpf.flags[atype] & BPF_F_ALLOW_MULTI))
294 list_for_each_entry(pl, &p->bpf.progs[atype], node) {
295 if (!prog_list_prog(pl))
298 item = &progs->items[cnt];
299 item->prog = prog_list_prog(pl);
300 bpf_cgroup_storages_assign(item->cgroup_storage,
304 } while ((p = cgroup_parent(p)));
310 static void activate_effective_progs(struct cgroup *cgrp,
311 enum cgroup_bpf_attach_type atype,
312 struct bpf_prog_array *old_array)
314 old_array = rcu_replace_pointer(cgrp->bpf.effective[atype], old_array,
315 lockdep_is_held(&cgroup_mutex));
316 /* free prog array after grace period, since __cgroup_bpf_run_*()
317 * might be still walking the array
319 bpf_prog_array_free(old_array);
323 * cgroup_bpf_inherit() - inherit effective programs from parent
324 * @cgrp: the cgroup to modify
326 int cgroup_bpf_inherit(struct cgroup *cgrp)
328 /* has to use marco instead of const int, since compiler thinks
329 * that array below is variable length
331 #define NR ARRAY_SIZE(cgrp->bpf.effective)
332 struct bpf_prog_array *arrays[NR] = {};
336 ret = percpu_ref_init(&cgrp->bpf.refcnt, cgroup_bpf_release_fn, 0,
341 for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
344 for (i = 0; i < NR; i++)
345 INIT_LIST_HEAD(&cgrp->bpf.progs[i]);
347 INIT_LIST_HEAD(&cgrp->bpf.storages);
349 for (i = 0; i < NR; i++)
350 if (compute_effective_progs(cgrp, i, &arrays[i]))
353 for (i = 0; i < NR; i++)
354 activate_effective_progs(cgrp, i, arrays[i]);
358 for (i = 0; i < NR; i++)
359 bpf_prog_array_free(arrays[i]);
361 for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
364 percpu_ref_exit(&cgrp->bpf.refcnt);
369 static int update_effective_progs(struct cgroup *cgrp,
370 enum cgroup_bpf_attach_type atype)
372 struct cgroup_subsys_state *css;
375 /* allocate and recompute effective prog arrays */
376 css_for_each_descendant_pre(css, &cgrp->self) {
377 struct cgroup *desc = container_of(css, struct cgroup, self);
379 if (percpu_ref_is_zero(&desc->bpf.refcnt))
382 err = compute_effective_progs(desc, atype, &desc->bpf.inactive);
387 /* all allocations were successful. Activate all prog arrays */
388 css_for_each_descendant_pre(css, &cgrp->self) {
389 struct cgroup *desc = container_of(css, struct cgroup, self);
391 if (percpu_ref_is_zero(&desc->bpf.refcnt)) {
392 if (unlikely(desc->bpf.inactive)) {
393 bpf_prog_array_free(desc->bpf.inactive);
394 desc->bpf.inactive = NULL;
399 activate_effective_progs(desc, atype, desc->bpf.inactive);
400 desc->bpf.inactive = NULL;
406 /* oom while computing effective. Free all computed effective arrays
407 * since they were not activated
409 css_for_each_descendant_pre(css, &cgrp->self) {
410 struct cgroup *desc = container_of(css, struct cgroup, self);
412 bpf_prog_array_free(desc->bpf.inactive);
413 desc->bpf.inactive = NULL;
419 #define BPF_CGROUP_MAX_PROGS 64
421 static struct bpf_prog_list *find_attach_entry(struct list_head *progs,
422 struct bpf_prog *prog,
423 struct bpf_cgroup_link *link,
424 struct bpf_prog *replace_prog,
427 struct bpf_prog_list *pl;
429 /* single-attach case */
431 if (list_empty(progs))
433 return list_first_entry(progs, typeof(*pl), node);
436 list_for_each_entry(pl, progs, node) {
437 if (prog && pl->prog == prog && prog != replace_prog)
438 /* disallow attaching the same prog twice */
439 return ERR_PTR(-EINVAL);
440 if (link && pl->link == link)
441 /* disallow attaching the same link twice */
442 return ERR_PTR(-EINVAL);
445 /* direct prog multi-attach w/ replacement case */
447 list_for_each_entry(pl, progs, node) {
448 if (pl->prog == replace_prog)
452 /* prog to replace not found for cgroup */
453 return ERR_PTR(-ENOENT);
460 * __cgroup_bpf_attach() - Attach the program or the link to a cgroup, and
461 * propagate the change to descendants
462 * @cgrp: The cgroup which descendants to traverse
463 * @prog: A program to attach
464 * @link: A link to attach
465 * @replace_prog: Previously attached program to replace if BPF_F_REPLACE is set
466 * @type: Type of attach operation
467 * @flags: Option flags
469 * Exactly one of @prog or @link can be non-null.
470 * Must be called with cgroup_mutex held.
472 static int __cgroup_bpf_attach(struct cgroup *cgrp,
473 struct bpf_prog *prog, struct bpf_prog *replace_prog,
474 struct bpf_cgroup_link *link,
475 enum bpf_attach_type type, u32 flags)
477 u32 saved_flags = (flags & (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI));
478 struct bpf_prog *old_prog = NULL;
479 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
480 struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
481 enum cgroup_bpf_attach_type atype;
482 struct bpf_prog_list *pl;
483 struct list_head *progs;
486 if (((flags & BPF_F_ALLOW_OVERRIDE) && (flags & BPF_F_ALLOW_MULTI)) ||
487 ((flags & BPF_F_REPLACE) && !(flags & BPF_F_ALLOW_MULTI)))
488 /* invalid combination */
490 if (link && (prog || replace_prog))
491 /* only either link or prog/replace_prog can be specified */
493 if (!!replace_prog != !!(flags & BPF_F_REPLACE))
494 /* replace_prog implies BPF_F_REPLACE, and vice versa */
497 atype = to_cgroup_bpf_attach_type(type);
501 progs = &cgrp->bpf.progs[atype];
503 if (!hierarchy_allows_attach(cgrp, atype))
506 if (!list_empty(progs) && cgrp->bpf.flags[atype] != saved_flags)
507 /* Disallow attaching non-overridable on top
508 * of existing overridable in this cgroup.
509 * Disallow attaching multi-prog if overridable or none
513 if (prog_list_length(progs) >= BPF_CGROUP_MAX_PROGS)
516 pl = find_attach_entry(progs, prog, link, replace_prog,
517 flags & BPF_F_ALLOW_MULTI);
521 if (bpf_cgroup_storages_alloc(storage, new_storage, type,
522 prog ? : link->link.prog, cgrp))
528 pl = kmalloc(sizeof(*pl), GFP_KERNEL);
530 bpf_cgroup_storages_free(new_storage);
533 list_add_tail(&pl->node, progs);
538 bpf_cgroup_storages_assign(pl->storage, storage);
539 cgrp->bpf.flags[atype] = saved_flags;
541 err = update_effective_progs(cgrp, atype);
546 bpf_prog_put(old_prog);
548 static_branch_inc(&cgroup_bpf_enabled_key[atype]);
549 bpf_cgroup_storages_link(new_storage, cgrp, type);
557 bpf_cgroup_storages_free(new_storage);
565 static int cgroup_bpf_attach(struct cgroup *cgrp,
566 struct bpf_prog *prog, struct bpf_prog *replace_prog,
567 struct bpf_cgroup_link *link,
568 enum bpf_attach_type type,
573 mutex_lock(&cgroup_mutex);
574 ret = __cgroup_bpf_attach(cgrp, prog, replace_prog, link, type, flags);
575 mutex_unlock(&cgroup_mutex);
579 /* Swap updated BPF program for given link in effective program arrays across
580 * all descendant cgroups. This function is guaranteed to succeed.
582 static void replace_effective_prog(struct cgroup *cgrp,
583 enum cgroup_bpf_attach_type atype,
584 struct bpf_cgroup_link *link)
586 struct bpf_prog_array_item *item;
587 struct cgroup_subsys_state *css;
588 struct bpf_prog_array *progs;
589 struct bpf_prog_list *pl;
590 struct list_head *head;
594 css_for_each_descendant_pre(css, &cgrp->self) {
595 struct cgroup *desc = container_of(css, struct cgroup, self);
597 if (percpu_ref_is_zero(&desc->bpf.refcnt))
600 /* find position of link in effective progs array */
601 for (pos = 0, cg = desc; cg; cg = cgroup_parent(cg)) {
602 if (pos && !(cg->bpf.flags[atype] & BPF_F_ALLOW_MULTI))
605 head = &cg->bpf.progs[atype];
606 list_for_each_entry(pl, head, node) {
607 if (!prog_list_prog(pl))
609 if (pl->link == link)
616 progs = rcu_dereference_protected(
617 desc->bpf.effective[atype],
618 lockdep_is_held(&cgroup_mutex));
619 item = &progs->items[pos];
620 WRITE_ONCE(item->prog, link->link.prog);
625 * __cgroup_bpf_replace() - Replace link's program and propagate the change
627 * @cgrp: The cgroup which descendants to traverse
628 * @link: A link for which to replace BPF program
629 * @type: Type of attach operation
631 * Must be called with cgroup_mutex held.
633 static int __cgroup_bpf_replace(struct cgroup *cgrp,
634 struct bpf_cgroup_link *link,
635 struct bpf_prog *new_prog)
637 enum cgroup_bpf_attach_type atype;
638 struct bpf_prog *old_prog;
639 struct bpf_prog_list *pl;
640 struct list_head *progs;
643 atype = to_cgroup_bpf_attach_type(link->type);
647 progs = &cgrp->bpf.progs[atype];
649 if (link->link.prog->type != new_prog->type)
652 list_for_each_entry(pl, progs, node) {
653 if (pl->link == link) {
661 old_prog = xchg(&link->link.prog, new_prog);
662 replace_effective_prog(cgrp, atype, link);
663 bpf_prog_put(old_prog);
667 static int cgroup_bpf_replace(struct bpf_link *link, struct bpf_prog *new_prog,
668 struct bpf_prog *old_prog)
670 struct bpf_cgroup_link *cg_link;
673 cg_link = container_of(link, struct bpf_cgroup_link, link);
675 mutex_lock(&cgroup_mutex);
676 /* link might have been auto-released by dying cgroup, so fail */
677 if (!cg_link->cgroup) {
681 if (old_prog && link->prog != old_prog) {
685 ret = __cgroup_bpf_replace(cg_link->cgroup, cg_link, new_prog);
687 mutex_unlock(&cgroup_mutex);
691 static struct bpf_prog_list *find_detach_entry(struct list_head *progs,
692 struct bpf_prog *prog,
693 struct bpf_cgroup_link *link,
696 struct bpf_prog_list *pl;
699 if (list_empty(progs))
700 /* report error when trying to detach and nothing is attached */
701 return ERR_PTR(-ENOENT);
703 /* to maintain backward compatibility NONE and OVERRIDE cgroups
704 * allow detaching with invalid FD (prog==NULL) in legacy mode
706 return list_first_entry(progs, typeof(*pl), node);
710 /* to detach MULTI prog the user has to specify valid FD
711 * of the program or link to be detached
713 return ERR_PTR(-EINVAL);
715 /* find the prog or link and detach it */
716 list_for_each_entry(pl, progs, node) {
717 if (pl->prog == prog && pl->link == link)
720 return ERR_PTR(-ENOENT);
724 * __cgroup_bpf_detach() - Detach the program or link from a cgroup, and
725 * propagate the change to descendants
726 * @cgrp: The cgroup which descendants to traverse
727 * @prog: A program to detach or NULL
728 * @link: A link to detach or NULL
729 * @type: Type of detach operation
731 * At most one of @prog or @link can be non-NULL.
732 * Must be called with cgroup_mutex held.
734 static int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
735 struct bpf_cgroup_link *link, enum bpf_attach_type type)
737 enum cgroup_bpf_attach_type atype;
738 struct bpf_prog *old_prog;
739 struct bpf_prog_list *pl;
740 struct list_head *progs;
744 atype = to_cgroup_bpf_attach_type(type);
748 progs = &cgrp->bpf.progs[atype];
749 flags = cgrp->bpf.flags[atype];
752 /* only one of prog or link can be specified */
755 pl = find_detach_entry(progs, prog, link, flags & BPF_F_ALLOW_MULTI);
759 /* mark it deleted, so it's ignored while recomputing effective */
764 err = update_effective_progs(cgrp, atype);
768 /* now can actually delete it from this cgroup list */
771 if (list_empty(progs))
772 /* last program was detached, reset flags to zero */
773 cgrp->bpf.flags[atype] = 0;
775 bpf_prog_put(old_prog);
776 static_branch_dec(&cgroup_bpf_enabled_key[atype]);
780 /* restore back prog or link */
786 static int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
787 enum bpf_attach_type type)
791 mutex_lock(&cgroup_mutex);
792 ret = __cgroup_bpf_detach(cgrp, prog, NULL, type);
793 mutex_unlock(&cgroup_mutex);
797 /* Must be called with cgroup_mutex held to avoid races. */
798 static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
799 union bpf_attr __user *uattr)
801 __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
802 enum bpf_attach_type type = attr->query.attach_type;
803 enum cgroup_bpf_attach_type atype;
804 struct bpf_prog_array *effective;
805 struct list_head *progs;
806 struct bpf_prog *prog;
810 atype = to_cgroup_bpf_attach_type(type);
814 progs = &cgrp->bpf.progs[atype];
815 flags = cgrp->bpf.flags[atype];
817 effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
818 lockdep_is_held(&cgroup_mutex));
820 if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE)
821 cnt = bpf_prog_array_length(effective);
823 cnt = prog_list_length(progs);
825 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
827 if (copy_to_user(&uattr->query.prog_cnt, &cnt, sizeof(cnt)))
829 if (attr->query.prog_cnt == 0 || !prog_ids || !cnt)
830 /* return early if user requested only program count + flags */
832 if (attr->query.prog_cnt < cnt) {
833 cnt = attr->query.prog_cnt;
837 if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
838 return bpf_prog_array_copy_to_user(effective, prog_ids, cnt);
840 struct bpf_prog_list *pl;
844 list_for_each_entry(pl, progs, node) {
845 prog = prog_list_prog(pl);
847 if (copy_to_user(prog_ids + i, &id, sizeof(id)))
856 static int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
857 union bpf_attr __user *uattr)
861 mutex_lock(&cgroup_mutex);
862 ret = __cgroup_bpf_query(cgrp, attr, uattr);
863 mutex_unlock(&cgroup_mutex);
867 int cgroup_bpf_prog_attach(const union bpf_attr *attr,
868 enum bpf_prog_type ptype, struct bpf_prog *prog)
870 struct bpf_prog *replace_prog = NULL;
874 cgrp = cgroup_get_from_fd(attr->target_fd);
876 return PTR_ERR(cgrp);
878 if ((attr->attach_flags & BPF_F_ALLOW_MULTI) &&
879 (attr->attach_flags & BPF_F_REPLACE)) {
880 replace_prog = bpf_prog_get_type(attr->replace_bpf_fd, ptype);
881 if (IS_ERR(replace_prog)) {
883 return PTR_ERR(replace_prog);
887 ret = cgroup_bpf_attach(cgrp, prog, replace_prog, NULL,
888 attr->attach_type, attr->attach_flags);
891 bpf_prog_put(replace_prog);
896 int cgroup_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
898 struct bpf_prog *prog;
902 cgrp = cgroup_get_from_fd(attr->target_fd);
904 return PTR_ERR(cgrp);
906 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
910 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type);
918 static void bpf_cgroup_link_release(struct bpf_link *link)
920 struct bpf_cgroup_link *cg_link =
921 container_of(link, struct bpf_cgroup_link, link);
924 /* link might have been auto-detached by dying cgroup already,
925 * in that case our work is done here
927 if (!cg_link->cgroup)
930 mutex_lock(&cgroup_mutex);
932 /* re-check cgroup under lock again */
933 if (!cg_link->cgroup) {
934 mutex_unlock(&cgroup_mutex);
938 WARN_ON(__cgroup_bpf_detach(cg_link->cgroup, NULL, cg_link,
941 cg = cg_link->cgroup;
942 cg_link->cgroup = NULL;
944 mutex_unlock(&cgroup_mutex);
949 static void bpf_cgroup_link_dealloc(struct bpf_link *link)
951 struct bpf_cgroup_link *cg_link =
952 container_of(link, struct bpf_cgroup_link, link);
957 static int bpf_cgroup_link_detach(struct bpf_link *link)
959 bpf_cgroup_link_release(link);
964 static void bpf_cgroup_link_show_fdinfo(const struct bpf_link *link,
965 struct seq_file *seq)
967 struct bpf_cgroup_link *cg_link =
968 container_of(link, struct bpf_cgroup_link, link);
971 mutex_lock(&cgroup_mutex);
973 cg_id = cgroup_id(cg_link->cgroup);
974 mutex_unlock(&cgroup_mutex);
978 "attach_type:\t%d\n",
983 static int bpf_cgroup_link_fill_link_info(const struct bpf_link *link,
984 struct bpf_link_info *info)
986 struct bpf_cgroup_link *cg_link =
987 container_of(link, struct bpf_cgroup_link, link);
990 mutex_lock(&cgroup_mutex);
992 cg_id = cgroup_id(cg_link->cgroup);
993 mutex_unlock(&cgroup_mutex);
995 info->cgroup.cgroup_id = cg_id;
996 info->cgroup.attach_type = cg_link->type;
1000 static const struct bpf_link_ops bpf_cgroup_link_lops = {
1001 .release = bpf_cgroup_link_release,
1002 .dealloc = bpf_cgroup_link_dealloc,
1003 .detach = bpf_cgroup_link_detach,
1004 .update_prog = cgroup_bpf_replace,
1005 .show_fdinfo = bpf_cgroup_link_show_fdinfo,
1006 .fill_link_info = bpf_cgroup_link_fill_link_info,
1009 int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
1011 struct bpf_link_primer link_primer;
1012 struct bpf_cgroup_link *link;
1013 struct cgroup *cgrp;
1016 if (attr->link_create.flags)
1019 cgrp = cgroup_get_from_fd(attr->link_create.target_fd);
1021 return PTR_ERR(cgrp);
1023 link = kzalloc(sizeof(*link), GFP_USER);
1026 goto out_put_cgroup;
1028 bpf_link_init(&link->link, BPF_LINK_TYPE_CGROUP, &bpf_cgroup_link_lops,
1030 link->cgroup = cgrp;
1031 link->type = attr->link_create.attach_type;
1033 err = bpf_link_prime(&link->link, &link_primer);
1036 goto out_put_cgroup;
1039 err = cgroup_bpf_attach(cgrp, NULL, NULL, link,
1040 link->type, BPF_F_ALLOW_MULTI);
1042 bpf_link_cleanup(&link_primer);
1043 goto out_put_cgroup;
1046 return bpf_link_settle(&link_primer);
1053 int cgroup_bpf_prog_query(const union bpf_attr *attr,
1054 union bpf_attr __user *uattr)
1056 struct cgroup *cgrp;
1059 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1061 return PTR_ERR(cgrp);
1063 ret = cgroup_bpf_query(cgrp, attr, uattr);
1070 * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
1071 * @sk: The socket sending or receiving traffic
1072 * @skb: The skb that is being sent or received
1073 * @type: The type of program to be executed
1075 * If no socket is passed, or the socket is not of type INET or INET6,
1076 * this function does nothing and returns 0.
1078 * The program type passed in via @type must be suitable for network
1079 * filtering. No further check is performed to assert that.
1081 * For egress packets, this function can return:
1082 * NET_XMIT_SUCCESS (0) - continue with packet output
1083 * NET_XMIT_DROP (1) - drop packet and notify TCP to call cwr
1084 * NET_XMIT_CN (2) - continue with packet output and notify TCP
1086 * -err - drop packet
1088 * For ingress packets, this function will return -EPERM if any
1089 * attached program was found and if it returned != 1 during execution.
1090 * Otherwise 0 is returned.
1092 int __cgroup_bpf_run_filter_skb(struct sock *sk,
1093 struct sk_buff *skb,
1094 enum cgroup_bpf_attach_type atype)
1096 unsigned int offset = skb->data - skb_network_header(skb);
1097 struct sock *save_sk;
1098 void *saved_data_end;
1099 struct cgroup *cgrp;
1102 if (!sk || !sk_fullsock(sk))
1105 if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
1108 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1111 __skb_push(skb, offset);
1113 /* compute pointers for the bpf prog */
1114 bpf_compute_and_save_data_end(skb, &saved_data_end);
1116 if (atype == CGROUP_INET_EGRESS) {
1120 ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, skb,
1121 __bpf_prog_run_save_cb, 0, &flags);
1123 /* Return values of CGROUP EGRESS BPF programs are:
1126 * 2: drop packet and cn
1127 * 3: keep packet and cn
1129 * The returned value is then converted to one of the NET_XMIT
1130 * or an error code that is then interpreted as drop packet
1132 * 0: NET_XMIT_SUCCESS skb should be transmitted
1133 * 1: NET_XMIT_DROP skb should be dropped and cn
1134 * 2: NET_XMIT_CN skb should be transmitted and cn
1135 * 3: -err skb should be dropped
1138 cn = flags & BPF_RET_SET_CN;
1139 if (ret && !IS_ERR_VALUE((long)ret))
1142 ret = (cn ? NET_XMIT_CN : NET_XMIT_SUCCESS);
1144 ret = (cn ? NET_XMIT_DROP : ret);
1146 ret = bpf_prog_run_array_cg(&cgrp->bpf, atype,
1147 skb, __bpf_prog_run_save_cb, 0,
1149 if (ret && !IS_ERR_VALUE((long)ret))
1152 bpf_restore_data_end(skb, saved_data_end);
1153 __skb_pull(skb, offset);
1158 EXPORT_SYMBOL(__cgroup_bpf_run_filter_skb);
1161 * __cgroup_bpf_run_filter_sk() - Run a program on a sock
1162 * @sk: sock structure to manipulate
1163 * @type: The type of program to be executed
1165 * socket is passed is expected to be of type INET or INET6.
1167 * The program type passed in via @type must be suitable for sock
1168 * filtering. No further check is performed to assert that.
1170 * This function will return %-EPERM if any if an attached program was found
1171 * and if it returned != 1 during execution. In all other cases, 0 is returned.
1173 int __cgroup_bpf_run_filter_sk(struct sock *sk,
1174 enum cgroup_bpf_attach_type atype)
1176 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1178 return bpf_prog_run_array_cg(&cgrp->bpf, atype, sk, bpf_prog_run, 0,
1181 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sk);
1184 * __cgroup_bpf_run_filter_sock_addr() - Run a program on a sock and
1185 * provided by user sockaddr
1186 * @sk: sock struct that will use sockaddr
1187 * @uaddr: sockaddr struct provided by user
1188 * @type: The type of program to be executed
1189 * @t_ctx: Pointer to attach type specific context
1190 * @flags: Pointer to u32 which contains higher bits of BPF program
1191 * return value (OR'ed together).
1193 * socket is expected to be of type INET or INET6.
1195 * This function will return %-EPERM if an attached program is found and
1196 * returned value != 1 during execution. In all other cases, 0 is returned.
1198 int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
1199 struct sockaddr *uaddr,
1200 enum cgroup_bpf_attach_type atype,
1204 struct bpf_sock_addr_kern ctx = {
1209 struct sockaddr_storage unspec;
1210 struct cgroup *cgrp;
1212 /* Check socket family since not all sockets represent network
1213 * endpoint (e.g. AF_UNIX).
1215 if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
1219 memset(&unspec, 0, sizeof(unspec));
1220 ctx.uaddr = (struct sockaddr *)&unspec;
1223 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1224 return bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run,
1227 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_addr);
1230 * __cgroup_bpf_run_filter_sock_ops() - Run a program on a sock
1231 * @sk: socket to get cgroup from
1232 * @sock_ops: bpf_sock_ops_kern struct to pass to program. Contains
1233 * sk with connection information (IP addresses, etc.) May not contain
1234 * cgroup info if it is a req sock.
1235 * @type: The type of program to be executed
1237 * socket passed is expected to be of type INET or INET6.
1239 * The program type passed in via @type must be suitable for sock_ops
1240 * filtering. No further check is performed to assert that.
1242 * This function will return %-EPERM if any if an attached program was found
1243 * and if it returned != 1 during execution. In all other cases, 0 is returned.
1245 int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
1246 struct bpf_sock_ops_kern *sock_ops,
1247 enum cgroup_bpf_attach_type atype)
1249 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1251 return bpf_prog_run_array_cg(&cgrp->bpf, atype, sock_ops, bpf_prog_run,
1254 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_ops);
1256 int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
1257 short access, enum cgroup_bpf_attach_type atype)
1259 struct cgroup *cgrp;
1260 struct bpf_cgroup_dev_ctx ctx = {
1261 .access_type = (access << 16) | dev_type,
1268 cgrp = task_dfl_cgroup(current);
1269 ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run, 0,
1276 BPF_CALL_0(bpf_get_retval)
1278 struct bpf_cg_run_ctx *ctx =
1279 container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx);
1284 static const struct bpf_func_proto bpf_get_retval_proto = {
1285 .func = bpf_get_retval,
1287 .ret_type = RET_INTEGER,
1290 BPF_CALL_1(bpf_set_retval, int, retval)
1292 struct bpf_cg_run_ctx *ctx =
1293 container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx);
1295 ctx->retval = retval;
1299 static const struct bpf_func_proto bpf_set_retval_proto = {
1300 .func = bpf_set_retval,
1302 .ret_type = RET_INTEGER,
1303 .arg1_type = ARG_ANYTHING,
1306 static const struct bpf_func_proto *
1307 cgroup_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1310 case BPF_FUNC_get_current_uid_gid:
1311 return &bpf_get_current_uid_gid_proto;
1312 case BPF_FUNC_get_local_storage:
1313 return &bpf_get_local_storage_proto;
1314 case BPF_FUNC_get_current_cgroup_id:
1315 return &bpf_get_current_cgroup_id_proto;
1316 case BPF_FUNC_perf_event_output:
1317 return &bpf_event_output_data_proto;
1318 case BPF_FUNC_get_retval:
1319 return &bpf_get_retval_proto;
1320 case BPF_FUNC_set_retval:
1321 return &bpf_set_retval_proto;
1323 return bpf_base_func_proto(func_id);
1327 static const struct bpf_func_proto *
1328 cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1330 return cgroup_base_func_proto(func_id, prog);
1333 static bool cgroup_dev_is_valid_access(int off, int size,
1334 enum bpf_access_type type,
1335 const struct bpf_prog *prog,
1336 struct bpf_insn_access_aux *info)
1338 const int size_default = sizeof(__u32);
1340 if (type == BPF_WRITE)
1343 if (off < 0 || off + size > sizeof(struct bpf_cgroup_dev_ctx))
1345 /* The verifier guarantees that size > 0. */
1346 if (off % size != 0)
1350 case bpf_ctx_range(struct bpf_cgroup_dev_ctx, access_type):
1351 bpf_ctx_record_field_size(info, size_default);
1352 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
1356 if (size != size_default)
1363 const struct bpf_prog_ops cg_dev_prog_ops = {
1366 const struct bpf_verifier_ops cg_dev_verifier_ops = {
1367 .get_func_proto = cgroup_dev_func_proto,
1368 .is_valid_access = cgroup_dev_is_valid_access,
1372 * __cgroup_bpf_run_filter_sysctl - Run a program on sysctl
1374 * @head: sysctl table header
1375 * @table: sysctl table
1376 * @write: sysctl is being read (= 0) or written (= 1)
1377 * @buf: pointer to buffer (in and out)
1378 * @pcount: value-result argument: value is size of buffer pointed to by @buf,
1379 * result is size of @new_buf if program set new value, initial value
1381 * @ppos: value-result argument: value is position at which read from or write
1382 * to sysctl is happening, result is new position if program overrode it,
1383 * initial value otherwise
1384 * @type: type of program to be executed
1386 * Program is run when sysctl is being accessed, either read or written, and
1387 * can allow or deny such access.
1389 * This function will return %-EPERM if an attached program is found and
1390 * returned value != 1 during execution. In all other cases 0 is returned.
1392 int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
1393 struct ctl_table *table, int write,
1394 char **buf, size_t *pcount, loff_t *ppos,
1395 enum cgroup_bpf_attach_type atype)
1397 struct bpf_sysctl_kern ctx = {
1403 .cur_len = PAGE_SIZE,
1408 struct cgroup *cgrp;
1412 ctx.cur_val = kmalloc_track_caller(ctx.cur_len, GFP_KERNEL);
1414 table->proc_handler(table, 0, ctx.cur_val, &ctx.cur_len, &pos)) {
1415 /* Let BPF program decide how to proceed. */
1419 if (write && *buf && *pcount) {
1420 /* BPF program should be able to override new value with a
1421 * buffer bigger than provided by user.
1423 ctx.new_val = kmalloc_track_caller(PAGE_SIZE, GFP_KERNEL);
1424 ctx.new_len = min_t(size_t, PAGE_SIZE, *pcount);
1426 memcpy(ctx.new_val, *buf, ctx.new_len);
1428 /* Let BPF program decide how to proceed. */
1434 cgrp = task_dfl_cgroup(current);
1435 ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run, 0,
1441 if (ret == 1 && ctx.new_updated) {
1444 *pcount = ctx.new_len;
1453 static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
1454 struct bpf_sockopt_buf *buf)
1456 if (unlikely(max_optlen < 0))
1459 if (unlikely(max_optlen > PAGE_SIZE)) {
1460 /* We don't expose optvals that are greater than PAGE_SIZE
1461 * to the BPF program.
1463 max_optlen = PAGE_SIZE;
1466 if (max_optlen <= sizeof(buf->data)) {
1467 /* When the optval fits into BPF_SOCKOPT_KERN_BUF_SIZE
1468 * bytes avoid the cost of kzalloc.
1470 ctx->optval = buf->data;
1471 ctx->optval_end = ctx->optval + max_optlen;
1475 ctx->optval = kzalloc(max_optlen, GFP_USER);
1479 ctx->optval_end = ctx->optval + max_optlen;
1484 static void sockopt_free_buf(struct bpf_sockopt_kern *ctx,
1485 struct bpf_sockopt_buf *buf)
1487 if (ctx->optval == buf->data)
1492 static bool sockopt_buf_allocated(struct bpf_sockopt_kern *ctx,
1493 struct bpf_sockopt_buf *buf)
1495 return ctx->optval != buf->data;
1498 int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
1499 int *optname, char __user *optval,
1500 int *optlen, char **kernel_optval)
1502 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1503 struct bpf_sockopt_buf buf = {};
1504 struct bpf_sockopt_kern ctx = {
1507 .optname = *optname,
1509 int ret, max_optlen;
1511 /* Allocate a bit more than the initial user buffer for
1512 * BPF program. The canonical use case is overriding
1513 * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic).
1515 max_optlen = max_t(int, 16, *optlen);
1516 max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
1520 ctx.optlen = *optlen;
1522 if (copy_from_user(ctx.optval, optval, min(*optlen, max_optlen)) != 0) {
1528 ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_SETSOCKOPT,
1529 &ctx, bpf_prog_run, 0, NULL);
1535 if (ctx.optlen == -1) {
1536 /* optlen set to -1, bypass kernel */
1538 } else if (ctx.optlen > max_optlen || ctx.optlen < -1) {
1539 /* optlen is out of bounds */
1542 /* optlen within bounds, run kernel handler */
1545 /* export any potential modifications */
1547 *optname = ctx.optname;
1549 /* optlen == 0 from BPF indicates that we should
1550 * use original userspace data.
1552 if (ctx.optlen != 0) {
1553 *optlen = ctx.optlen;
1554 /* We've used bpf_sockopt_kern->buf as an intermediary
1555 * storage, but the BPF program indicates that we need
1556 * to pass this data to the kernel setsockopt handler.
1557 * No way to export on-stack buf, have to allocate a
1560 if (!sockopt_buf_allocated(&ctx, &buf)) {
1561 void *p = kmalloc(ctx.optlen, GFP_USER);
1567 memcpy(p, ctx.optval, ctx.optlen);
1570 *kernel_optval = ctx.optval;
1572 /* export and don't free sockopt buf */
1578 sockopt_free_buf(&ctx, &buf);
1582 int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
1583 int optname, char __user *optval,
1584 int __user *optlen, int max_optlen,
1587 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1588 struct bpf_sockopt_buf buf = {};
1589 struct bpf_sockopt_kern ctx = {
1593 .current_task = current,
1597 ctx.optlen = max_optlen;
1598 max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
1603 /* If kernel getsockopt finished successfully,
1604 * copy whatever was returned to the user back
1605 * into our temporary buffer. Set optlen to the
1606 * one that kernel returned as well to let
1607 * BPF programs inspect the value.
1610 if (get_user(ctx.optlen, optlen)) {
1615 if (ctx.optlen < 0) {
1620 if (copy_from_user(ctx.optval, optval,
1621 min(ctx.optlen, max_optlen)) != 0) {
1628 ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_GETSOCKOPT,
1629 &ctx, bpf_prog_run, retval, NULL);
1635 if (ctx.optlen > max_optlen || ctx.optlen < 0) {
1640 if (ctx.optlen != 0) {
1641 if (copy_to_user(optval, ctx.optval, ctx.optlen) ||
1642 put_user(ctx.optlen, optlen)) {
1649 sockopt_free_buf(&ctx, &buf);
1653 int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level,
1654 int optname, void *optval,
1655 int *optlen, int retval)
1657 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1658 struct bpf_sockopt_kern ctx = {
1664 .optval_end = optval + *optlen,
1665 .current_task = current,
1669 /* Note that __cgroup_bpf_run_filter_getsockopt doesn't copy
1670 * user data back into BPF buffer when reval != 0. This is
1671 * done as an optimization to avoid extra copy, assuming
1672 * kernel won't populate the data in case of an error.
1673 * Here we always pass the data and memset() should
1674 * be called if that data shouldn't be "exported".
1677 ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_GETSOCKOPT,
1678 &ctx, bpf_prog_run, retval, NULL);
1682 if (ctx.optlen > *optlen)
1685 /* BPF programs can shrink the buffer, export the modifications.
1687 if (ctx.optlen != 0)
1688 *optlen = ctx.optlen;
1694 static ssize_t sysctl_cpy_dir(const struct ctl_dir *dir, char **bufp,
1697 ssize_t tmp_ret = 0, ret;
1699 if (dir->header.parent) {
1700 tmp_ret = sysctl_cpy_dir(dir->header.parent, bufp, lenp);
1705 ret = strscpy(*bufp, dir->header.ctl_table[0].procname, *lenp);
1712 /* Avoid leading slash. */
1716 tmp_ret = strscpy(*bufp, "/", *lenp);
1722 return ret + tmp_ret;
1725 BPF_CALL_4(bpf_sysctl_get_name, struct bpf_sysctl_kern *, ctx, char *, buf,
1726 size_t, buf_len, u64, flags)
1728 ssize_t tmp_ret = 0, ret;
1733 if (!(flags & BPF_F_SYSCTL_BASE_NAME)) {
1736 tmp_ret = sysctl_cpy_dir(ctx->head->parent, &buf, &buf_len);
1741 ret = strscpy(buf, ctx->table->procname, buf_len);
1743 return ret < 0 ? ret : tmp_ret + ret;
1746 static const struct bpf_func_proto bpf_sysctl_get_name_proto = {
1747 .func = bpf_sysctl_get_name,
1749 .ret_type = RET_INTEGER,
1750 .arg1_type = ARG_PTR_TO_CTX,
1751 .arg2_type = ARG_PTR_TO_MEM,
1752 .arg3_type = ARG_CONST_SIZE,
1753 .arg4_type = ARG_ANYTHING,
1756 static int copy_sysctl_value(char *dst, size_t dst_len, char *src,
1765 if (!src || !src_len) {
1766 memset(dst, 0, dst_len);
1770 memcpy(dst, src, min(dst_len, src_len));
1772 if (dst_len > src_len) {
1773 memset(dst + src_len, '\0', dst_len - src_len);
1777 dst[dst_len - 1] = '\0';
1782 BPF_CALL_3(bpf_sysctl_get_current_value, struct bpf_sysctl_kern *, ctx,
1783 char *, buf, size_t, buf_len)
1785 return copy_sysctl_value(buf, buf_len, ctx->cur_val, ctx->cur_len);
1788 static const struct bpf_func_proto bpf_sysctl_get_current_value_proto = {
1789 .func = bpf_sysctl_get_current_value,
1791 .ret_type = RET_INTEGER,
1792 .arg1_type = ARG_PTR_TO_CTX,
1793 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1794 .arg3_type = ARG_CONST_SIZE,
1797 BPF_CALL_3(bpf_sysctl_get_new_value, struct bpf_sysctl_kern *, ctx, char *, buf,
1802 memset(buf, '\0', buf_len);
1805 return copy_sysctl_value(buf, buf_len, ctx->new_val, ctx->new_len);
1808 static const struct bpf_func_proto bpf_sysctl_get_new_value_proto = {
1809 .func = bpf_sysctl_get_new_value,
1811 .ret_type = RET_INTEGER,
1812 .arg1_type = ARG_PTR_TO_CTX,
1813 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1814 .arg3_type = ARG_CONST_SIZE,
1817 BPF_CALL_3(bpf_sysctl_set_new_value, struct bpf_sysctl_kern *, ctx,
1818 const char *, buf, size_t, buf_len)
1820 if (!ctx->write || !ctx->new_val || !ctx->new_len || !buf || !buf_len)
1823 if (buf_len > PAGE_SIZE - 1)
1826 memcpy(ctx->new_val, buf, buf_len);
1827 ctx->new_len = buf_len;
1828 ctx->new_updated = 1;
1833 static const struct bpf_func_proto bpf_sysctl_set_new_value_proto = {
1834 .func = bpf_sysctl_set_new_value,
1836 .ret_type = RET_INTEGER,
1837 .arg1_type = ARG_PTR_TO_CTX,
1838 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1839 .arg3_type = ARG_CONST_SIZE,
1842 static const struct bpf_func_proto *
1843 sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1846 case BPF_FUNC_strtol:
1847 return &bpf_strtol_proto;
1848 case BPF_FUNC_strtoul:
1849 return &bpf_strtoul_proto;
1850 case BPF_FUNC_sysctl_get_name:
1851 return &bpf_sysctl_get_name_proto;
1852 case BPF_FUNC_sysctl_get_current_value:
1853 return &bpf_sysctl_get_current_value_proto;
1854 case BPF_FUNC_sysctl_get_new_value:
1855 return &bpf_sysctl_get_new_value_proto;
1856 case BPF_FUNC_sysctl_set_new_value:
1857 return &bpf_sysctl_set_new_value_proto;
1858 case BPF_FUNC_ktime_get_coarse_ns:
1859 return &bpf_ktime_get_coarse_ns_proto;
1861 return cgroup_base_func_proto(func_id, prog);
1865 static bool sysctl_is_valid_access(int off, int size, enum bpf_access_type type,
1866 const struct bpf_prog *prog,
1867 struct bpf_insn_access_aux *info)
1869 const int size_default = sizeof(__u32);
1871 if (off < 0 || off + size > sizeof(struct bpf_sysctl) || off % size)
1875 case bpf_ctx_range(struct bpf_sysctl, write):
1876 if (type != BPF_READ)
1878 bpf_ctx_record_field_size(info, size_default);
1879 return bpf_ctx_narrow_access_ok(off, size, size_default);
1880 case bpf_ctx_range(struct bpf_sysctl, file_pos):
1881 if (type == BPF_READ) {
1882 bpf_ctx_record_field_size(info, size_default);
1883 return bpf_ctx_narrow_access_ok(off, size, size_default);
1885 return size == size_default;
1892 static u32 sysctl_convert_ctx_access(enum bpf_access_type type,
1893 const struct bpf_insn *si,
1894 struct bpf_insn *insn_buf,
1895 struct bpf_prog *prog, u32 *target_size)
1897 struct bpf_insn *insn = insn_buf;
1901 case offsetof(struct bpf_sysctl, write):
1902 *insn++ = BPF_LDX_MEM(
1903 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
1904 bpf_target_off(struct bpf_sysctl_kern, write,
1905 sizeof_field(struct bpf_sysctl_kern,
1909 case offsetof(struct bpf_sysctl, file_pos):
1910 /* ppos is a pointer so it should be accessed via indirect
1911 * loads and stores. Also for stores additional temporary
1912 * register is used since neither src_reg nor dst_reg can be
1915 if (type == BPF_WRITE) {
1916 int treg = BPF_REG_9;
1918 if (si->src_reg == treg || si->dst_reg == treg)
1920 if (si->src_reg == treg || si->dst_reg == treg)
1922 *insn++ = BPF_STX_MEM(
1923 BPF_DW, si->dst_reg, treg,
1924 offsetof(struct bpf_sysctl_kern, tmp_reg));
1925 *insn++ = BPF_LDX_MEM(
1926 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
1928 offsetof(struct bpf_sysctl_kern, ppos));
1929 *insn++ = BPF_STX_MEM(
1930 BPF_SIZEOF(u32), treg, si->src_reg,
1931 bpf_ctx_narrow_access_offset(
1932 0, sizeof(u32), sizeof(loff_t)));
1933 *insn++ = BPF_LDX_MEM(
1934 BPF_DW, treg, si->dst_reg,
1935 offsetof(struct bpf_sysctl_kern, tmp_reg));
1937 *insn++ = BPF_LDX_MEM(
1938 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
1939 si->dst_reg, si->src_reg,
1940 offsetof(struct bpf_sysctl_kern, ppos));
1941 read_size = bpf_size_to_bytes(BPF_SIZE(si->code));
1942 *insn++ = BPF_LDX_MEM(
1943 BPF_SIZE(si->code), si->dst_reg, si->dst_reg,
1944 bpf_ctx_narrow_access_offset(
1945 0, read_size, sizeof(loff_t)));
1947 *target_size = sizeof(u32);
1951 return insn - insn_buf;
1954 const struct bpf_verifier_ops cg_sysctl_verifier_ops = {
1955 .get_func_proto = sysctl_func_proto,
1956 .is_valid_access = sysctl_is_valid_access,
1957 .convert_ctx_access = sysctl_convert_ctx_access,
1960 const struct bpf_prog_ops cg_sysctl_prog_ops = {
1964 BPF_CALL_1(bpf_get_netns_cookie_sockopt, struct bpf_sockopt_kern *, ctx)
1966 const struct net *net = ctx ? sock_net(ctx->sk) : &init_net;
1968 return net->net_cookie;
1971 static const struct bpf_func_proto bpf_get_netns_cookie_sockopt_proto = {
1972 .func = bpf_get_netns_cookie_sockopt,
1974 .ret_type = RET_INTEGER,
1975 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
1979 static const struct bpf_func_proto *
1980 cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1984 case BPF_FUNC_get_netns_cookie:
1985 return &bpf_get_netns_cookie_sockopt_proto;
1986 case BPF_FUNC_sk_storage_get:
1987 return &bpf_sk_storage_get_proto;
1988 case BPF_FUNC_sk_storage_delete:
1989 return &bpf_sk_storage_delete_proto;
1990 case BPF_FUNC_setsockopt:
1991 if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT)
1992 return &bpf_sk_setsockopt_proto;
1994 case BPF_FUNC_getsockopt:
1995 if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT)
1996 return &bpf_sk_getsockopt_proto;
2000 case BPF_FUNC_tcp_sock:
2001 return &bpf_tcp_sock_proto;
2004 return cgroup_base_func_proto(func_id, prog);
2008 static bool cg_sockopt_is_valid_access(int off, int size,
2009 enum bpf_access_type type,
2010 const struct bpf_prog *prog,
2011 struct bpf_insn_access_aux *info)
2013 const int size_default = sizeof(__u32);
2015 if (off < 0 || off >= sizeof(struct bpf_sockopt))
2018 if (off % size != 0)
2021 if (type == BPF_WRITE) {
2023 case offsetof(struct bpf_sockopt, retval):
2024 if (size != size_default)
2026 return prog->expected_attach_type ==
2027 BPF_CGROUP_GETSOCKOPT;
2028 case offsetof(struct bpf_sockopt, optname):
2030 case offsetof(struct bpf_sockopt, level):
2031 if (size != size_default)
2033 return prog->expected_attach_type ==
2034 BPF_CGROUP_SETSOCKOPT;
2035 case offsetof(struct bpf_sockopt, optlen):
2036 return size == size_default;
2043 case offsetof(struct bpf_sockopt, sk):
2044 if (size != sizeof(__u64))
2046 info->reg_type = PTR_TO_SOCKET;
2048 case offsetof(struct bpf_sockopt, optval):
2049 if (size != sizeof(__u64))
2051 info->reg_type = PTR_TO_PACKET;
2053 case offsetof(struct bpf_sockopt, optval_end):
2054 if (size != sizeof(__u64))
2056 info->reg_type = PTR_TO_PACKET_END;
2058 case offsetof(struct bpf_sockopt, retval):
2059 if (size != size_default)
2061 return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT;
2063 if (size != size_default)
2070 #define CG_SOCKOPT_ACCESS_FIELD(T, F) \
2071 T(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, F), \
2072 si->dst_reg, si->src_reg, \
2073 offsetof(struct bpf_sockopt_kern, F))
2075 static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type,
2076 const struct bpf_insn *si,
2077 struct bpf_insn *insn_buf,
2078 struct bpf_prog *prog,
2081 struct bpf_insn *insn = insn_buf;
2084 case offsetof(struct bpf_sockopt, sk):
2085 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, sk);
2087 case offsetof(struct bpf_sockopt, level):
2088 if (type == BPF_WRITE)
2089 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, level);
2091 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, level);
2093 case offsetof(struct bpf_sockopt, optname):
2094 if (type == BPF_WRITE)
2095 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optname);
2097 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optname);
2099 case offsetof(struct bpf_sockopt, optlen):
2100 if (type == BPF_WRITE)
2101 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optlen);
2103 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optlen);
2105 case offsetof(struct bpf_sockopt, retval):
2106 BUILD_BUG_ON(offsetof(struct bpf_cg_run_ctx, run_ctx) != 0);
2108 if (type == BPF_WRITE) {
2109 int treg = BPF_REG_9;
2111 if (si->src_reg == treg || si->dst_reg == treg)
2113 if (si->src_reg == treg || si->dst_reg == treg)
2115 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, treg,
2116 offsetof(struct bpf_sockopt_kern, tmp_reg));
2117 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, current_task),
2119 offsetof(struct bpf_sockopt_kern, current_task));
2120 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct task_struct, bpf_ctx),
2122 offsetof(struct task_struct, bpf_ctx));
2123 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(struct bpf_cg_run_ctx, retval),
2125 offsetof(struct bpf_cg_run_ctx, retval));
2126 *insn++ = BPF_LDX_MEM(BPF_DW, treg, si->dst_reg,
2127 offsetof(struct bpf_sockopt_kern, tmp_reg));
2129 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, current_task),
2130 si->dst_reg, si->src_reg,
2131 offsetof(struct bpf_sockopt_kern, current_task));
2132 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct task_struct, bpf_ctx),
2133 si->dst_reg, si->dst_reg,
2134 offsetof(struct task_struct, bpf_ctx));
2135 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_cg_run_ctx, retval),
2136 si->dst_reg, si->dst_reg,
2137 offsetof(struct bpf_cg_run_ctx, retval));
2140 case offsetof(struct bpf_sockopt, optval):
2141 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval);
2143 case offsetof(struct bpf_sockopt, optval_end):
2144 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval_end);
2148 return insn - insn_buf;
2151 static int cg_sockopt_get_prologue(struct bpf_insn *insn_buf,
2153 const struct bpf_prog *prog)
2155 /* Nothing to do for sockopt argument. The data is kzalloc'ated.
2160 const struct bpf_verifier_ops cg_sockopt_verifier_ops = {
2161 .get_func_proto = cg_sockopt_func_proto,
2162 .is_valid_access = cg_sockopt_is_valid_access,
2163 .convert_ctx_access = cg_sockopt_convert_ctx_access,
2164 .gen_prologue = cg_sockopt_get_prologue,
2167 const struct bpf_prog_ops cg_sockopt_prog_ops = {