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 void cgroup_bpf_offline(struct cgroup *cgrp)
28 percpu_ref_kill(&cgrp->bpf.refcnt);
31 static void bpf_cgroup_storages_free(struct bpf_cgroup_storage *storages[])
33 enum bpf_cgroup_storage_type stype;
35 for_each_cgroup_storage_type(stype)
36 bpf_cgroup_storage_free(storages[stype]);
39 static int bpf_cgroup_storages_alloc(struct bpf_cgroup_storage *storages[],
40 struct bpf_cgroup_storage *new_storages[],
41 enum bpf_attach_type type,
42 struct bpf_prog *prog,
45 enum bpf_cgroup_storage_type stype;
46 struct bpf_cgroup_storage_key key;
49 key.cgroup_inode_id = cgroup_id(cgrp);
50 key.attach_type = type;
52 for_each_cgroup_storage_type(stype) {
53 map = prog->aux->cgroup_storage[stype];
57 storages[stype] = cgroup_storage_lookup((void *)map, &key, false);
61 storages[stype] = bpf_cgroup_storage_alloc(prog, stype);
62 if (IS_ERR(storages[stype])) {
63 bpf_cgroup_storages_free(new_storages);
67 new_storages[stype] = storages[stype];
73 static void bpf_cgroup_storages_assign(struct bpf_cgroup_storage *dst[],
74 struct bpf_cgroup_storage *src[])
76 enum bpf_cgroup_storage_type stype;
78 for_each_cgroup_storage_type(stype)
79 dst[stype] = src[stype];
82 static void bpf_cgroup_storages_link(struct bpf_cgroup_storage *storages[],
84 enum bpf_attach_type attach_type)
86 enum bpf_cgroup_storage_type stype;
88 for_each_cgroup_storage_type(stype)
89 bpf_cgroup_storage_link(storages[stype], cgrp, attach_type);
92 /* Called when bpf_cgroup_link is auto-detached from dying cgroup.
93 * It drops cgroup and bpf_prog refcounts, and marks bpf_link as defunct. It
94 * doesn't free link memory, which will eventually be done by bpf_link's
95 * release() callback, when its last FD is closed.
97 static void bpf_cgroup_link_auto_detach(struct bpf_cgroup_link *link)
99 cgroup_put(link->cgroup);
104 * cgroup_bpf_release() - put references of all bpf programs and
105 * release all cgroup bpf data
106 * @work: work structure embedded into the cgroup to modify
108 static void cgroup_bpf_release(struct work_struct *work)
110 struct cgroup *p, *cgrp = container_of(work, struct cgroup,
112 struct bpf_prog_array *old_array;
113 struct list_head *storages = &cgrp->bpf.storages;
114 struct bpf_cgroup_storage *storage, *stmp;
118 mutex_lock(&cgroup_mutex);
120 for (atype = 0; atype < ARRAY_SIZE(cgrp->bpf.progs); atype++) {
121 struct list_head *progs = &cgrp->bpf.progs[atype];
122 struct bpf_prog_list *pl, *pltmp;
124 list_for_each_entry_safe(pl, pltmp, progs, node) {
127 bpf_prog_put(pl->prog);
129 bpf_cgroup_link_auto_detach(pl->link);
131 static_branch_dec(&cgroup_bpf_enabled_key[atype]);
133 old_array = rcu_dereference_protected(
134 cgrp->bpf.effective[atype],
135 lockdep_is_held(&cgroup_mutex));
136 bpf_prog_array_free(old_array);
139 list_for_each_entry_safe(storage, stmp, storages, list_cg) {
140 bpf_cgroup_storage_unlink(storage);
141 bpf_cgroup_storage_free(storage);
144 mutex_unlock(&cgroup_mutex);
146 for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
149 percpu_ref_exit(&cgrp->bpf.refcnt);
154 * cgroup_bpf_release_fn() - callback used to schedule releasing
156 * @ref: percpu ref counter structure
158 static void cgroup_bpf_release_fn(struct percpu_ref *ref)
160 struct cgroup *cgrp = container_of(ref, struct cgroup, bpf.refcnt);
162 INIT_WORK(&cgrp->bpf.release_work, cgroup_bpf_release);
163 queue_work(system_wq, &cgrp->bpf.release_work);
166 /* Get underlying bpf_prog of bpf_prog_list entry, regardless if it's through
167 * link or direct prog.
169 static struct bpf_prog *prog_list_prog(struct bpf_prog_list *pl)
174 return pl->link->link.prog;
178 /* count number of elements in the list.
179 * it's slow but the list cannot be long
181 static u32 prog_list_length(struct list_head *head)
183 struct bpf_prog_list *pl;
186 list_for_each_entry(pl, head, node) {
187 if (!prog_list_prog(pl))
194 /* if parent has non-overridable prog attached,
195 * disallow attaching new programs to the descendent cgroup.
196 * if parent has overridable or multi-prog, allow attaching
198 static bool hierarchy_allows_attach(struct cgroup *cgrp,
199 enum cgroup_bpf_attach_type atype)
203 p = cgroup_parent(cgrp);
207 u32 flags = p->bpf.flags[atype];
210 if (flags & BPF_F_ALLOW_MULTI)
212 cnt = prog_list_length(&p->bpf.progs[atype]);
213 WARN_ON_ONCE(cnt > 1);
215 return !!(flags & BPF_F_ALLOW_OVERRIDE);
216 p = cgroup_parent(p);
221 /* compute a chain of effective programs for a given cgroup:
222 * start from the list of programs in this cgroup and add
223 * all parent programs.
224 * Note that parent's F_ALLOW_OVERRIDE-type program is yielding
225 * to programs in this cgroup
227 static int compute_effective_progs(struct cgroup *cgrp,
228 enum cgroup_bpf_attach_type atype,
229 struct bpf_prog_array **array)
231 struct bpf_prog_array_item *item;
232 struct bpf_prog_array *progs;
233 struct bpf_prog_list *pl;
234 struct cgroup *p = cgrp;
237 /* count number of effective programs by walking parents */
239 if (cnt == 0 || (p->bpf.flags[atype] & BPF_F_ALLOW_MULTI))
240 cnt += prog_list_length(&p->bpf.progs[atype]);
241 p = cgroup_parent(p);
244 progs = bpf_prog_array_alloc(cnt, GFP_KERNEL);
248 /* populate the array with effective progs */
252 if (cnt > 0 && !(p->bpf.flags[atype] & BPF_F_ALLOW_MULTI))
255 list_for_each_entry(pl, &p->bpf.progs[atype], node) {
256 if (!prog_list_prog(pl))
259 item = &progs->items[cnt];
260 item->prog = prog_list_prog(pl);
261 bpf_cgroup_storages_assign(item->cgroup_storage,
265 } while ((p = cgroup_parent(p)));
271 static void activate_effective_progs(struct cgroup *cgrp,
272 enum cgroup_bpf_attach_type atype,
273 struct bpf_prog_array *old_array)
275 old_array = rcu_replace_pointer(cgrp->bpf.effective[atype], old_array,
276 lockdep_is_held(&cgroup_mutex));
277 /* free prog array after grace period, since __cgroup_bpf_run_*()
278 * might be still walking the array
280 bpf_prog_array_free(old_array);
284 * cgroup_bpf_inherit() - inherit effective programs from parent
285 * @cgrp: the cgroup to modify
287 int cgroup_bpf_inherit(struct cgroup *cgrp)
289 /* has to use marco instead of const int, since compiler thinks
290 * that array below is variable length
292 #define NR ARRAY_SIZE(cgrp->bpf.effective)
293 struct bpf_prog_array *arrays[NR] = {};
297 ret = percpu_ref_init(&cgrp->bpf.refcnt, cgroup_bpf_release_fn, 0,
302 for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
305 for (i = 0; i < NR; i++)
306 INIT_LIST_HEAD(&cgrp->bpf.progs[i]);
308 INIT_LIST_HEAD(&cgrp->bpf.storages);
310 for (i = 0; i < NR; i++)
311 if (compute_effective_progs(cgrp, i, &arrays[i]))
314 for (i = 0; i < NR; i++)
315 activate_effective_progs(cgrp, i, arrays[i]);
319 for (i = 0; i < NR; i++)
320 bpf_prog_array_free(arrays[i]);
322 for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
325 percpu_ref_exit(&cgrp->bpf.refcnt);
330 static int update_effective_progs(struct cgroup *cgrp,
331 enum cgroup_bpf_attach_type atype)
333 struct cgroup_subsys_state *css;
336 /* allocate and recompute effective prog arrays */
337 css_for_each_descendant_pre(css, &cgrp->self) {
338 struct cgroup *desc = container_of(css, struct cgroup, self);
340 if (percpu_ref_is_zero(&desc->bpf.refcnt))
343 err = compute_effective_progs(desc, atype, &desc->bpf.inactive);
348 /* all allocations were successful. Activate all prog arrays */
349 css_for_each_descendant_pre(css, &cgrp->self) {
350 struct cgroup *desc = container_of(css, struct cgroup, self);
352 if (percpu_ref_is_zero(&desc->bpf.refcnt)) {
353 if (unlikely(desc->bpf.inactive)) {
354 bpf_prog_array_free(desc->bpf.inactive);
355 desc->bpf.inactive = NULL;
360 activate_effective_progs(desc, atype, desc->bpf.inactive);
361 desc->bpf.inactive = NULL;
367 /* oom while computing effective. Free all computed effective arrays
368 * since they were not activated
370 css_for_each_descendant_pre(css, &cgrp->self) {
371 struct cgroup *desc = container_of(css, struct cgroup, self);
373 bpf_prog_array_free(desc->bpf.inactive);
374 desc->bpf.inactive = NULL;
380 #define BPF_CGROUP_MAX_PROGS 64
382 static struct bpf_prog_list *find_attach_entry(struct list_head *progs,
383 struct bpf_prog *prog,
384 struct bpf_cgroup_link *link,
385 struct bpf_prog *replace_prog,
388 struct bpf_prog_list *pl;
390 /* single-attach case */
392 if (list_empty(progs))
394 return list_first_entry(progs, typeof(*pl), node);
397 list_for_each_entry(pl, progs, node) {
398 if (prog && pl->prog == prog && prog != replace_prog)
399 /* disallow attaching the same prog twice */
400 return ERR_PTR(-EINVAL);
401 if (link && pl->link == link)
402 /* disallow attaching the same link twice */
403 return ERR_PTR(-EINVAL);
406 /* direct prog multi-attach w/ replacement case */
408 list_for_each_entry(pl, progs, node) {
409 if (pl->prog == replace_prog)
413 /* prog to replace not found for cgroup */
414 return ERR_PTR(-ENOENT);
421 * __cgroup_bpf_attach() - Attach the program or the link to a cgroup, and
422 * propagate the change to descendants
423 * @cgrp: The cgroup which descendants to traverse
424 * @prog: A program to attach
425 * @link: A link to attach
426 * @replace_prog: Previously attached program to replace if BPF_F_REPLACE is set
427 * @type: Type of attach operation
428 * @flags: Option flags
430 * Exactly one of @prog or @link can be non-null.
431 * Must be called with cgroup_mutex held.
433 int __cgroup_bpf_attach(struct cgroup *cgrp,
434 struct bpf_prog *prog, struct bpf_prog *replace_prog,
435 struct bpf_cgroup_link *link,
436 enum bpf_attach_type type, u32 flags)
438 u32 saved_flags = (flags & (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI));
439 struct bpf_prog *old_prog = NULL;
440 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
441 struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
442 enum cgroup_bpf_attach_type atype;
443 struct bpf_prog_list *pl;
444 struct list_head *progs;
447 if (((flags & BPF_F_ALLOW_OVERRIDE) && (flags & BPF_F_ALLOW_MULTI)) ||
448 ((flags & BPF_F_REPLACE) && !(flags & BPF_F_ALLOW_MULTI)))
449 /* invalid combination */
451 if (link && (prog || replace_prog))
452 /* only either link or prog/replace_prog can be specified */
454 if (!!replace_prog != !!(flags & BPF_F_REPLACE))
455 /* replace_prog implies BPF_F_REPLACE, and vice versa */
458 atype = to_cgroup_bpf_attach_type(type);
462 progs = &cgrp->bpf.progs[atype];
464 if (!hierarchy_allows_attach(cgrp, atype))
467 if (!list_empty(progs) && cgrp->bpf.flags[atype] != saved_flags)
468 /* Disallow attaching non-overridable on top
469 * of existing overridable in this cgroup.
470 * Disallow attaching multi-prog if overridable or none
474 if (prog_list_length(progs) >= BPF_CGROUP_MAX_PROGS)
477 pl = find_attach_entry(progs, prog, link, replace_prog,
478 flags & BPF_F_ALLOW_MULTI);
482 if (bpf_cgroup_storages_alloc(storage, new_storage, type,
483 prog ? : link->link.prog, cgrp))
489 pl = kmalloc(sizeof(*pl), GFP_KERNEL);
491 bpf_cgroup_storages_free(new_storage);
494 list_add_tail(&pl->node, progs);
499 bpf_cgroup_storages_assign(pl->storage, storage);
500 cgrp->bpf.flags[atype] = saved_flags;
502 err = update_effective_progs(cgrp, atype);
507 bpf_prog_put(old_prog);
509 static_branch_inc(&cgroup_bpf_enabled_key[atype]);
510 bpf_cgroup_storages_link(new_storage, cgrp, type);
518 bpf_cgroup_storages_free(new_storage);
526 /* Swap updated BPF program for given link in effective program arrays across
527 * all descendant cgroups. This function is guaranteed to succeed.
529 static void replace_effective_prog(struct cgroup *cgrp,
530 enum cgroup_bpf_attach_type atype,
531 struct bpf_cgroup_link *link)
533 struct bpf_prog_array_item *item;
534 struct cgroup_subsys_state *css;
535 struct bpf_prog_array *progs;
536 struct bpf_prog_list *pl;
537 struct list_head *head;
541 css_for_each_descendant_pre(css, &cgrp->self) {
542 struct cgroup *desc = container_of(css, struct cgroup, self);
544 if (percpu_ref_is_zero(&desc->bpf.refcnt))
547 /* find position of link in effective progs array */
548 for (pos = 0, cg = desc; cg; cg = cgroup_parent(cg)) {
549 if (pos && !(cg->bpf.flags[atype] & BPF_F_ALLOW_MULTI))
552 head = &cg->bpf.progs[atype];
553 list_for_each_entry(pl, head, node) {
554 if (!prog_list_prog(pl))
556 if (pl->link == link)
563 progs = rcu_dereference_protected(
564 desc->bpf.effective[atype],
565 lockdep_is_held(&cgroup_mutex));
566 item = &progs->items[pos];
567 WRITE_ONCE(item->prog, link->link.prog);
572 * __cgroup_bpf_replace() - Replace link's program and propagate the change
574 * @cgrp: The cgroup which descendants to traverse
575 * @link: A link for which to replace BPF program
576 * @type: Type of attach operation
578 * Must be called with cgroup_mutex held.
580 static int __cgroup_bpf_replace(struct cgroup *cgrp,
581 struct bpf_cgroup_link *link,
582 struct bpf_prog *new_prog)
584 enum cgroup_bpf_attach_type atype;
585 struct bpf_prog *old_prog;
586 struct bpf_prog_list *pl;
587 struct list_head *progs;
590 atype = to_cgroup_bpf_attach_type(link->type);
594 progs = &cgrp->bpf.progs[atype];
596 if (link->link.prog->type != new_prog->type)
599 list_for_each_entry(pl, progs, node) {
600 if (pl->link == link) {
608 old_prog = xchg(&link->link.prog, new_prog);
609 replace_effective_prog(cgrp, atype, link);
610 bpf_prog_put(old_prog);
614 static int cgroup_bpf_replace(struct bpf_link *link, struct bpf_prog *new_prog,
615 struct bpf_prog *old_prog)
617 struct bpf_cgroup_link *cg_link;
620 cg_link = container_of(link, struct bpf_cgroup_link, link);
622 mutex_lock(&cgroup_mutex);
623 /* link might have been auto-released by dying cgroup, so fail */
624 if (!cg_link->cgroup) {
628 if (old_prog && link->prog != old_prog) {
632 ret = __cgroup_bpf_replace(cg_link->cgroup, cg_link, new_prog);
634 mutex_unlock(&cgroup_mutex);
638 static struct bpf_prog_list *find_detach_entry(struct list_head *progs,
639 struct bpf_prog *prog,
640 struct bpf_cgroup_link *link,
643 struct bpf_prog_list *pl;
646 if (list_empty(progs))
647 /* report error when trying to detach and nothing is attached */
648 return ERR_PTR(-ENOENT);
650 /* to maintain backward compatibility NONE and OVERRIDE cgroups
651 * allow detaching with invalid FD (prog==NULL) in legacy mode
653 return list_first_entry(progs, typeof(*pl), node);
657 /* to detach MULTI prog the user has to specify valid FD
658 * of the program or link to be detached
660 return ERR_PTR(-EINVAL);
662 /* find the prog or link and detach it */
663 list_for_each_entry(pl, progs, node) {
664 if (pl->prog == prog && pl->link == link)
667 return ERR_PTR(-ENOENT);
671 * __cgroup_bpf_detach() - Detach the program or link from a cgroup, and
672 * propagate the change to descendants
673 * @cgrp: The cgroup which descendants to traverse
674 * @prog: A program to detach or NULL
675 * @prog: A link to detach or NULL
676 * @type: Type of detach operation
678 * At most one of @prog or @link can be non-NULL.
679 * Must be called with cgroup_mutex held.
681 int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
682 struct bpf_cgroup_link *link, enum bpf_attach_type type)
684 enum cgroup_bpf_attach_type atype;
685 struct bpf_prog *old_prog;
686 struct bpf_prog_list *pl;
687 struct list_head *progs;
691 atype = to_cgroup_bpf_attach_type(type);
695 progs = &cgrp->bpf.progs[atype];
696 flags = cgrp->bpf.flags[atype];
699 /* only one of prog or link can be specified */
702 pl = find_detach_entry(progs, prog, link, flags & BPF_F_ALLOW_MULTI);
706 /* mark it deleted, so it's ignored while recomputing effective */
711 err = update_effective_progs(cgrp, atype);
715 /* now can actually delete it from this cgroup list */
718 if (list_empty(progs))
719 /* last program was detached, reset flags to zero */
720 cgrp->bpf.flags[atype] = 0;
722 bpf_prog_put(old_prog);
723 static_branch_dec(&cgroup_bpf_enabled_key[atype]);
727 /* restore back prog or link */
733 /* Must be called with cgroup_mutex held to avoid races. */
734 int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
735 union bpf_attr __user *uattr)
737 __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
738 enum bpf_attach_type type = attr->query.attach_type;
739 enum cgroup_bpf_attach_type atype;
740 struct bpf_prog_array *effective;
741 struct list_head *progs;
742 struct bpf_prog *prog;
746 atype = to_cgroup_bpf_attach_type(type);
750 progs = &cgrp->bpf.progs[atype];
751 flags = cgrp->bpf.flags[atype];
753 effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
754 lockdep_is_held(&cgroup_mutex));
756 if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE)
757 cnt = bpf_prog_array_length(effective);
759 cnt = prog_list_length(progs);
761 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
763 if (copy_to_user(&uattr->query.prog_cnt, &cnt, sizeof(cnt)))
765 if (attr->query.prog_cnt == 0 || !prog_ids || !cnt)
766 /* return early if user requested only program count + flags */
768 if (attr->query.prog_cnt < cnt) {
769 cnt = attr->query.prog_cnt;
773 if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
774 return bpf_prog_array_copy_to_user(effective, prog_ids, cnt);
776 struct bpf_prog_list *pl;
780 list_for_each_entry(pl, progs, node) {
781 prog = prog_list_prog(pl);
783 if (copy_to_user(prog_ids + i, &id, sizeof(id)))
792 int cgroup_bpf_prog_attach(const union bpf_attr *attr,
793 enum bpf_prog_type ptype, struct bpf_prog *prog)
795 struct bpf_prog *replace_prog = NULL;
799 cgrp = cgroup_get_from_fd(attr->target_fd);
801 return PTR_ERR(cgrp);
803 if ((attr->attach_flags & BPF_F_ALLOW_MULTI) &&
804 (attr->attach_flags & BPF_F_REPLACE)) {
805 replace_prog = bpf_prog_get_type(attr->replace_bpf_fd, ptype);
806 if (IS_ERR(replace_prog)) {
808 return PTR_ERR(replace_prog);
812 ret = cgroup_bpf_attach(cgrp, prog, replace_prog, NULL,
813 attr->attach_type, attr->attach_flags);
816 bpf_prog_put(replace_prog);
821 int cgroup_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
823 struct bpf_prog *prog;
827 cgrp = cgroup_get_from_fd(attr->target_fd);
829 return PTR_ERR(cgrp);
831 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
835 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type);
843 static void bpf_cgroup_link_release(struct bpf_link *link)
845 struct bpf_cgroup_link *cg_link =
846 container_of(link, struct bpf_cgroup_link, link);
849 /* link might have been auto-detached by dying cgroup already,
850 * in that case our work is done here
852 if (!cg_link->cgroup)
855 mutex_lock(&cgroup_mutex);
857 /* re-check cgroup under lock again */
858 if (!cg_link->cgroup) {
859 mutex_unlock(&cgroup_mutex);
863 WARN_ON(__cgroup_bpf_detach(cg_link->cgroup, NULL, cg_link,
866 cg = cg_link->cgroup;
867 cg_link->cgroup = NULL;
869 mutex_unlock(&cgroup_mutex);
874 static void bpf_cgroup_link_dealloc(struct bpf_link *link)
876 struct bpf_cgroup_link *cg_link =
877 container_of(link, struct bpf_cgroup_link, link);
882 static int bpf_cgroup_link_detach(struct bpf_link *link)
884 bpf_cgroup_link_release(link);
889 static void bpf_cgroup_link_show_fdinfo(const struct bpf_link *link,
890 struct seq_file *seq)
892 struct bpf_cgroup_link *cg_link =
893 container_of(link, struct bpf_cgroup_link, link);
896 mutex_lock(&cgroup_mutex);
898 cg_id = cgroup_id(cg_link->cgroup);
899 mutex_unlock(&cgroup_mutex);
903 "attach_type:\t%d\n",
908 static int bpf_cgroup_link_fill_link_info(const struct bpf_link *link,
909 struct bpf_link_info *info)
911 struct bpf_cgroup_link *cg_link =
912 container_of(link, struct bpf_cgroup_link, link);
915 mutex_lock(&cgroup_mutex);
917 cg_id = cgroup_id(cg_link->cgroup);
918 mutex_unlock(&cgroup_mutex);
920 info->cgroup.cgroup_id = cg_id;
921 info->cgroup.attach_type = cg_link->type;
925 static const struct bpf_link_ops bpf_cgroup_link_lops = {
926 .release = bpf_cgroup_link_release,
927 .dealloc = bpf_cgroup_link_dealloc,
928 .detach = bpf_cgroup_link_detach,
929 .update_prog = cgroup_bpf_replace,
930 .show_fdinfo = bpf_cgroup_link_show_fdinfo,
931 .fill_link_info = bpf_cgroup_link_fill_link_info,
934 int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
936 struct bpf_link_primer link_primer;
937 struct bpf_cgroup_link *link;
941 if (attr->link_create.flags)
944 cgrp = cgroup_get_from_fd(attr->link_create.target_fd);
946 return PTR_ERR(cgrp);
948 link = kzalloc(sizeof(*link), GFP_USER);
953 bpf_link_init(&link->link, BPF_LINK_TYPE_CGROUP, &bpf_cgroup_link_lops,
956 link->type = attr->link_create.attach_type;
958 err = bpf_link_prime(&link->link, &link_primer);
964 err = cgroup_bpf_attach(cgrp, NULL, NULL, link,
965 link->type, BPF_F_ALLOW_MULTI);
967 bpf_link_cleanup(&link_primer);
971 return bpf_link_settle(&link_primer);
978 int cgroup_bpf_prog_query(const union bpf_attr *attr,
979 union bpf_attr __user *uattr)
984 cgrp = cgroup_get_from_fd(attr->query.target_fd);
986 return PTR_ERR(cgrp);
988 ret = cgroup_bpf_query(cgrp, attr, uattr);
995 * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
996 * @sk: The socket sending or receiving traffic
997 * @skb: The skb that is being sent or received
998 * @type: The type of program to be exectuted
1000 * If no socket is passed, or the socket is not of type INET or INET6,
1001 * this function does nothing and returns 0.
1003 * The program type passed in via @type must be suitable for network
1004 * filtering. No further check is performed to assert that.
1006 * For egress packets, this function can return:
1007 * NET_XMIT_SUCCESS (0) - continue with packet output
1008 * NET_XMIT_DROP (1) - drop packet and notify TCP to call cwr
1009 * NET_XMIT_CN (2) - continue with packet output and notify TCP
1011 * -EPERM - drop packet
1013 * For ingress packets, this function will return -EPERM if any
1014 * attached program was found and if it returned != 1 during execution.
1015 * Otherwise 0 is returned.
1017 int __cgroup_bpf_run_filter_skb(struct sock *sk,
1018 struct sk_buff *skb,
1019 enum cgroup_bpf_attach_type atype)
1021 unsigned int offset = skb->data - skb_network_header(skb);
1022 struct sock *save_sk;
1023 void *saved_data_end;
1024 struct cgroup *cgrp;
1027 if (!sk || !sk_fullsock(sk))
1030 if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
1033 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1036 __skb_push(skb, offset);
1038 /* compute pointers for the bpf prog */
1039 bpf_compute_and_save_data_end(skb, &saved_data_end);
1041 if (atype == CGROUP_INET_EGRESS) {
1042 ret = BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(
1043 cgrp->bpf.effective[atype], skb, __bpf_prog_run_save_cb);
1045 ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[atype], skb,
1046 __bpf_prog_run_save_cb);
1047 ret = (ret == 1 ? 0 : -EPERM);
1049 bpf_restore_data_end(skb, saved_data_end);
1050 __skb_pull(skb, offset);
1055 EXPORT_SYMBOL(__cgroup_bpf_run_filter_skb);
1058 * __cgroup_bpf_run_filter_sk() - Run a program on a sock
1059 * @sk: sock structure to manipulate
1060 * @type: The type of program to be exectuted
1062 * socket is passed is expected to be of type INET or INET6.
1064 * The program type passed in via @type must be suitable for sock
1065 * filtering. No further check is performed to assert that.
1067 * This function will return %-EPERM if any if an attached program was found
1068 * and if it returned != 1 during execution. In all other cases, 0 is returned.
1070 int __cgroup_bpf_run_filter_sk(struct sock *sk,
1071 enum cgroup_bpf_attach_type atype)
1073 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1076 ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[atype], sk, bpf_prog_run);
1077 return ret == 1 ? 0 : -EPERM;
1079 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sk);
1082 * __cgroup_bpf_run_filter_sock_addr() - Run a program on a sock and
1083 * provided by user sockaddr
1084 * @sk: sock struct that will use sockaddr
1085 * @uaddr: sockaddr struct provided by user
1086 * @type: The type of program to be exectuted
1087 * @t_ctx: Pointer to attach type specific context
1088 * @flags: Pointer to u32 which contains higher bits of BPF program
1089 * return value (OR'ed together).
1091 * socket is expected to be of type INET or INET6.
1093 * This function will return %-EPERM if an attached program is found and
1094 * returned value != 1 during execution. In all other cases, 0 is returned.
1096 int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
1097 struct sockaddr *uaddr,
1098 enum cgroup_bpf_attach_type atype,
1102 struct bpf_sock_addr_kern ctx = {
1107 struct sockaddr_storage unspec;
1108 struct cgroup *cgrp;
1111 /* Check socket family since not all sockets represent network
1112 * endpoint (e.g. AF_UNIX).
1114 if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
1118 memset(&unspec, 0, sizeof(unspec));
1119 ctx.uaddr = (struct sockaddr *)&unspec;
1122 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1123 ret = BPF_PROG_RUN_ARRAY_CG_FLAGS(cgrp->bpf.effective[atype], &ctx,
1124 bpf_prog_run, flags);
1126 return ret == 1 ? 0 : -EPERM;
1128 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_addr);
1131 * __cgroup_bpf_run_filter_sock_ops() - Run a program on a sock
1132 * @sk: socket to get cgroup from
1133 * @sock_ops: bpf_sock_ops_kern struct to pass to program. Contains
1134 * sk with connection information (IP addresses, etc.) May not contain
1135 * cgroup info if it is a req sock.
1136 * @type: The type of program to be exectuted
1138 * socket passed is expected to be of type INET or INET6.
1140 * The program type passed in via @type must be suitable for sock_ops
1141 * filtering. No further check is performed to assert that.
1143 * This function will return %-EPERM if any if an attached program was found
1144 * and if it returned != 1 during execution. In all other cases, 0 is returned.
1146 int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
1147 struct bpf_sock_ops_kern *sock_ops,
1148 enum cgroup_bpf_attach_type atype)
1150 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1153 ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[atype], sock_ops,
1155 return ret == 1 ? 0 : -EPERM;
1157 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_ops);
1159 int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
1160 short access, enum cgroup_bpf_attach_type atype)
1162 struct cgroup *cgrp;
1163 struct bpf_cgroup_dev_ctx ctx = {
1164 .access_type = (access << 16) | dev_type,
1171 cgrp = task_dfl_cgroup(current);
1172 allow = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[atype], &ctx,
1179 static const struct bpf_func_proto *
1180 cgroup_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1183 case BPF_FUNC_get_current_uid_gid:
1184 return &bpf_get_current_uid_gid_proto;
1185 case BPF_FUNC_get_local_storage:
1186 return &bpf_get_local_storage_proto;
1187 case BPF_FUNC_get_current_cgroup_id:
1188 return &bpf_get_current_cgroup_id_proto;
1189 case BPF_FUNC_perf_event_output:
1190 return &bpf_event_output_data_proto;
1192 return bpf_base_func_proto(func_id);
1196 static const struct bpf_func_proto *
1197 cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1199 return cgroup_base_func_proto(func_id, prog);
1202 static bool cgroup_dev_is_valid_access(int off, int size,
1203 enum bpf_access_type type,
1204 const struct bpf_prog *prog,
1205 struct bpf_insn_access_aux *info)
1207 const int size_default = sizeof(__u32);
1209 if (type == BPF_WRITE)
1212 if (off < 0 || off + size > sizeof(struct bpf_cgroup_dev_ctx))
1214 /* The verifier guarantees that size > 0. */
1215 if (off % size != 0)
1219 case bpf_ctx_range(struct bpf_cgroup_dev_ctx, access_type):
1220 bpf_ctx_record_field_size(info, size_default);
1221 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
1225 if (size != size_default)
1232 const struct bpf_prog_ops cg_dev_prog_ops = {
1235 const struct bpf_verifier_ops cg_dev_verifier_ops = {
1236 .get_func_proto = cgroup_dev_func_proto,
1237 .is_valid_access = cgroup_dev_is_valid_access,
1241 * __cgroup_bpf_run_filter_sysctl - Run a program on sysctl
1243 * @head: sysctl table header
1244 * @table: sysctl table
1245 * @write: sysctl is being read (= 0) or written (= 1)
1246 * @buf: pointer to buffer (in and out)
1247 * @pcount: value-result argument: value is size of buffer pointed to by @buf,
1248 * result is size of @new_buf if program set new value, initial value
1250 * @ppos: value-result argument: value is position at which read from or write
1251 * to sysctl is happening, result is new position if program overrode it,
1252 * initial value otherwise
1253 * @type: type of program to be executed
1255 * Program is run when sysctl is being accessed, either read or written, and
1256 * can allow or deny such access.
1258 * This function will return %-EPERM if an attached program is found and
1259 * returned value != 1 during execution. In all other cases 0 is returned.
1261 int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
1262 struct ctl_table *table, int write,
1263 char **buf, size_t *pcount, loff_t *ppos,
1264 enum cgroup_bpf_attach_type atype)
1266 struct bpf_sysctl_kern ctx = {
1272 .cur_len = PAGE_SIZE,
1277 struct cgroup *cgrp;
1281 ctx.cur_val = kmalloc_track_caller(ctx.cur_len, GFP_KERNEL);
1283 table->proc_handler(table, 0, ctx.cur_val, &ctx.cur_len, &pos)) {
1284 /* Let BPF program decide how to proceed. */
1288 if (write && *buf && *pcount) {
1289 /* BPF program should be able to override new value with a
1290 * buffer bigger than provided by user.
1292 ctx.new_val = kmalloc_track_caller(PAGE_SIZE, GFP_KERNEL);
1293 ctx.new_len = min_t(size_t, PAGE_SIZE, *pcount);
1295 memcpy(ctx.new_val, *buf, ctx.new_len);
1297 /* Let BPF program decide how to proceed. */
1303 cgrp = task_dfl_cgroup(current);
1304 ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[atype], &ctx, bpf_prog_run);
1309 if (ret == 1 && ctx.new_updated) {
1312 *pcount = ctx.new_len;
1317 return ret == 1 ? 0 : -EPERM;
1321 static bool __cgroup_bpf_prog_array_is_empty(struct cgroup *cgrp,
1322 enum cgroup_bpf_attach_type attach_type)
1324 struct bpf_prog_array *prog_array;
1328 prog_array = rcu_dereference(cgrp->bpf.effective[attach_type]);
1329 empty = bpf_prog_array_is_empty(prog_array);
1335 static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
1336 struct bpf_sockopt_buf *buf)
1338 if (unlikely(max_optlen < 0))
1341 if (unlikely(max_optlen > PAGE_SIZE)) {
1342 /* We don't expose optvals that are greater than PAGE_SIZE
1343 * to the BPF program.
1345 max_optlen = PAGE_SIZE;
1348 if (max_optlen <= sizeof(buf->data)) {
1349 /* When the optval fits into BPF_SOCKOPT_KERN_BUF_SIZE
1350 * bytes avoid the cost of kzalloc.
1352 ctx->optval = buf->data;
1353 ctx->optval_end = ctx->optval + max_optlen;
1357 ctx->optval = kzalloc(max_optlen, GFP_USER);
1361 ctx->optval_end = ctx->optval + max_optlen;
1366 static void sockopt_free_buf(struct bpf_sockopt_kern *ctx,
1367 struct bpf_sockopt_buf *buf)
1369 if (ctx->optval == buf->data)
1374 static bool sockopt_buf_allocated(struct bpf_sockopt_kern *ctx,
1375 struct bpf_sockopt_buf *buf)
1377 return ctx->optval != buf->data;
1380 int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
1381 int *optname, char __user *optval,
1382 int *optlen, char **kernel_optval)
1384 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1385 struct bpf_sockopt_buf buf = {};
1386 struct bpf_sockopt_kern ctx = {
1389 .optname = *optname,
1391 int ret, max_optlen;
1393 /* Opportunistic check to see whether we have any BPF program
1394 * attached to the hook so we don't waste time allocating
1395 * memory and locking the socket.
1397 if (__cgroup_bpf_prog_array_is_empty(cgrp, CGROUP_SETSOCKOPT))
1400 /* Allocate a bit more than the initial user buffer for
1401 * BPF program. The canonical use case is overriding
1402 * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic).
1404 max_optlen = max_t(int, 16, *optlen);
1406 max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
1410 ctx.optlen = *optlen;
1412 if (copy_from_user(ctx.optval, optval, min(*optlen, max_optlen)) != 0) {
1418 ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[CGROUP_SETSOCKOPT],
1419 &ctx, bpf_prog_run);
1427 if (ctx.optlen == -1) {
1428 /* optlen set to -1, bypass kernel */
1430 } else if (ctx.optlen > max_optlen || ctx.optlen < -1) {
1431 /* optlen is out of bounds */
1434 /* optlen within bounds, run kernel handler */
1437 /* export any potential modifications */
1439 *optname = ctx.optname;
1441 /* optlen == 0 from BPF indicates that we should
1442 * use original userspace data.
1444 if (ctx.optlen != 0) {
1445 *optlen = ctx.optlen;
1446 /* We've used bpf_sockopt_kern->buf as an intermediary
1447 * storage, but the BPF program indicates that we need
1448 * to pass this data to the kernel setsockopt handler.
1449 * No way to export on-stack buf, have to allocate a
1452 if (!sockopt_buf_allocated(&ctx, &buf)) {
1453 void *p = kmalloc(ctx.optlen, GFP_USER);
1459 memcpy(p, ctx.optval, ctx.optlen);
1462 *kernel_optval = ctx.optval;
1464 /* export and don't free sockopt buf */
1470 sockopt_free_buf(&ctx, &buf);
1474 int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
1475 int optname, char __user *optval,
1476 int __user *optlen, int max_optlen,
1479 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1480 struct bpf_sockopt_buf buf = {};
1481 struct bpf_sockopt_kern ctx = {
1489 /* Opportunistic check to see whether we have any BPF program
1490 * attached to the hook so we don't waste time allocating
1491 * memory and locking the socket.
1493 if (__cgroup_bpf_prog_array_is_empty(cgrp, CGROUP_GETSOCKOPT))
1496 ctx.optlen = max_optlen;
1498 max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
1503 /* If kernel getsockopt finished successfully,
1504 * copy whatever was returned to the user back
1505 * into our temporary buffer. Set optlen to the
1506 * one that kernel returned as well to let
1507 * BPF programs inspect the value.
1510 if (get_user(ctx.optlen, optlen)) {
1515 if (ctx.optlen < 0) {
1520 if (copy_from_user(ctx.optval, optval,
1521 min(ctx.optlen, max_optlen)) != 0) {
1528 ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[CGROUP_GETSOCKOPT],
1529 &ctx, bpf_prog_run);
1537 if (ctx.optlen > max_optlen || ctx.optlen < 0) {
1542 /* BPF programs only allowed to set retval to 0, not some
1545 if (ctx.retval != 0 && ctx.retval != retval) {
1550 if (ctx.optlen != 0) {
1551 if (copy_to_user(optval, ctx.optval, ctx.optlen) ||
1552 put_user(ctx.optlen, optlen)) {
1561 sockopt_free_buf(&ctx, &buf);
1565 int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level,
1566 int optname, void *optval,
1567 int *optlen, int retval)
1569 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1570 struct bpf_sockopt_kern ctx = {
1577 .optval_end = optval + *optlen,
1581 /* Note that __cgroup_bpf_run_filter_getsockopt doesn't copy
1582 * user data back into BPF buffer when reval != 0. This is
1583 * done as an optimization to avoid extra copy, assuming
1584 * kernel won't populate the data in case of an error.
1585 * Here we always pass the data and memset() should
1586 * be called if that data shouldn't be "exported".
1589 ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[CGROUP_GETSOCKOPT],
1590 &ctx, bpf_prog_run);
1594 if (ctx.optlen > *optlen)
1597 /* BPF programs only allowed to set retval to 0, not some
1600 if (ctx.retval != 0 && ctx.retval != retval)
1603 /* BPF programs can shrink the buffer, export the modifications.
1605 if (ctx.optlen != 0)
1606 *optlen = ctx.optlen;
1612 static ssize_t sysctl_cpy_dir(const struct ctl_dir *dir, char **bufp,
1615 ssize_t tmp_ret = 0, ret;
1617 if (dir->header.parent) {
1618 tmp_ret = sysctl_cpy_dir(dir->header.parent, bufp, lenp);
1623 ret = strscpy(*bufp, dir->header.ctl_table[0].procname, *lenp);
1630 /* Avoid leading slash. */
1634 tmp_ret = strscpy(*bufp, "/", *lenp);
1640 return ret + tmp_ret;
1643 BPF_CALL_4(bpf_sysctl_get_name, struct bpf_sysctl_kern *, ctx, char *, buf,
1644 size_t, buf_len, u64, flags)
1646 ssize_t tmp_ret = 0, ret;
1651 if (!(flags & BPF_F_SYSCTL_BASE_NAME)) {
1654 tmp_ret = sysctl_cpy_dir(ctx->head->parent, &buf, &buf_len);
1659 ret = strscpy(buf, ctx->table->procname, buf_len);
1661 return ret < 0 ? ret : tmp_ret + ret;
1664 static const struct bpf_func_proto bpf_sysctl_get_name_proto = {
1665 .func = bpf_sysctl_get_name,
1667 .ret_type = RET_INTEGER,
1668 .arg1_type = ARG_PTR_TO_CTX,
1669 .arg2_type = ARG_PTR_TO_MEM,
1670 .arg3_type = ARG_CONST_SIZE,
1671 .arg4_type = ARG_ANYTHING,
1674 static int copy_sysctl_value(char *dst, size_t dst_len, char *src,
1683 if (!src || !src_len) {
1684 memset(dst, 0, dst_len);
1688 memcpy(dst, src, min(dst_len, src_len));
1690 if (dst_len > src_len) {
1691 memset(dst + src_len, '\0', dst_len - src_len);
1695 dst[dst_len - 1] = '\0';
1700 BPF_CALL_3(bpf_sysctl_get_current_value, struct bpf_sysctl_kern *, ctx,
1701 char *, buf, size_t, buf_len)
1703 return copy_sysctl_value(buf, buf_len, ctx->cur_val, ctx->cur_len);
1706 static const struct bpf_func_proto bpf_sysctl_get_current_value_proto = {
1707 .func = bpf_sysctl_get_current_value,
1709 .ret_type = RET_INTEGER,
1710 .arg1_type = ARG_PTR_TO_CTX,
1711 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1712 .arg3_type = ARG_CONST_SIZE,
1715 BPF_CALL_3(bpf_sysctl_get_new_value, struct bpf_sysctl_kern *, ctx, char *, buf,
1720 memset(buf, '\0', buf_len);
1723 return copy_sysctl_value(buf, buf_len, ctx->new_val, ctx->new_len);
1726 static const struct bpf_func_proto bpf_sysctl_get_new_value_proto = {
1727 .func = bpf_sysctl_get_new_value,
1729 .ret_type = RET_INTEGER,
1730 .arg1_type = ARG_PTR_TO_CTX,
1731 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1732 .arg3_type = ARG_CONST_SIZE,
1735 BPF_CALL_3(bpf_sysctl_set_new_value, struct bpf_sysctl_kern *, ctx,
1736 const char *, buf, size_t, buf_len)
1738 if (!ctx->write || !ctx->new_val || !ctx->new_len || !buf || !buf_len)
1741 if (buf_len > PAGE_SIZE - 1)
1744 memcpy(ctx->new_val, buf, buf_len);
1745 ctx->new_len = buf_len;
1746 ctx->new_updated = 1;
1751 static const struct bpf_func_proto bpf_sysctl_set_new_value_proto = {
1752 .func = bpf_sysctl_set_new_value,
1754 .ret_type = RET_INTEGER,
1755 .arg1_type = ARG_PTR_TO_CTX,
1756 .arg2_type = ARG_PTR_TO_MEM,
1757 .arg3_type = ARG_CONST_SIZE,
1760 static const struct bpf_func_proto *
1761 sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1764 case BPF_FUNC_strtol:
1765 return &bpf_strtol_proto;
1766 case BPF_FUNC_strtoul:
1767 return &bpf_strtoul_proto;
1768 case BPF_FUNC_sysctl_get_name:
1769 return &bpf_sysctl_get_name_proto;
1770 case BPF_FUNC_sysctl_get_current_value:
1771 return &bpf_sysctl_get_current_value_proto;
1772 case BPF_FUNC_sysctl_get_new_value:
1773 return &bpf_sysctl_get_new_value_proto;
1774 case BPF_FUNC_sysctl_set_new_value:
1775 return &bpf_sysctl_set_new_value_proto;
1777 return cgroup_base_func_proto(func_id, prog);
1781 static bool sysctl_is_valid_access(int off, int size, enum bpf_access_type type,
1782 const struct bpf_prog *prog,
1783 struct bpf_insn_access_aux *info)
1785 const int size_default = sizeof(__u32);
1787 if (off < 0 || off + size > sizeof(struct bpf_sysctl) || off % size)
1791 case bpf_ctx_range(struct bpf_sysctl, write):
1792 if (type != BPF_READ)
1794 bpf_ctx_record_field_size(info, size_default);
1795 return bpf_ctx_narrow_access_ok(off, size, size_default);
1796 case bpf_ctx_range(struct bpf_sysctl, file_pos):
1797 if (type == BPF_READ) {
1798 bpf_ctx_record_field_size(info, size_default);
1799 return bpf_ctx_narrow_access_ok(off, size, size_default);
1801 return size == size_default;
1808 static u32 sysctl_convert_ctx_access(enum bpf_access_type type,
1809 const struct bpf_insn *si,
1810 struct bpf_insn *insn_buf,
1811 struct bpf_prog *prog, u32 *target_size)
1813 struct bpf_insn *insn = insn_buf;
1817 case offsetof(struct bpf_sysctl, write):
1818 *insn++ = BPF_LDX_MEM(
1819 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
1820 bpf_target_off(struct bpf_sysctl_kern, write,
1821 sizeof_field(struct bpf_sysctl_kern,
1825 case offsetof(struct bpf_sysctl, file_pos):
1826 /* ppos is a pointer so it should be accessed via indirect
1827 * loads and stores. Also for stores additional temporary
1828 * register is used since neither src_reg nor dst_reg can be
1831 if (type == BPF_WRITE) {
1832 int treg = BPF_REG_9;
1834 if (si->src_reg == treg || si->dst_reg == treg)
1836 if (si->src_reg == treg || si->dst_reg == treg)
1838 *insn++ = BPF_STX_MEM(
1839 BPF_DW, si->dst_reg, treg,
1840 offsetof(struct bpf_sysctl_kern, tmp_reg));
1841 *insn++ = BPF_LDX_MEM(
1842 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
1844 offsetof(struct bpf_sysctl_kern, ppos));
1845 *insn++ = BPF_STX_MEM(
1846 BPF_SIZEOF(u32), treg, si->src_reg,
1847 bpf_ctx_narrow_access_offset(
1848 0, sizeof(u32), sizeof(loff_t)));
1849 *insn++ = BPF_LDX_MEM(
1850 BPF_DW, treg, si->dst_reg,
1851 offsetof(struct bpf_sysctl_kern, tmp_reg));
1853 *insn++ = BPF_LDX_MEM(
1854 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
1855 si->dst_reg, si->src_reg,
1856 offsetof(struct bpf_sysctl_kern, ppos));
1857 read_size = bpf_size_to_bytes(BPF_SIZE(si->code));
1858 *insn++ = BPF_LDX_MEM(
1859 BPF_SIZE(si->code), si->dst_reg, si->dst_reg,
1860 bpf_ctx_narrow_access_offset(
1861 0, read_size, sizeof(loff_t)));
1863 *target_size = sizeof(u32);
1867 return insn - insn_buf;
1870 const struct bpf_verifier_ops cg_sysctl_verifier_ops = {
1871 .get_func_proto = sysctl_func_proto,
1872 .is_valid_access = sysctl_is_valid_access,
1873 .convert_ctx_access = sysctl_convert_ctx_access,
1876 const struct bpf_prog_ops cg_sysctl_prog_ops = {
1880 BPF_CALL_1(bpf_get_netns_cookie_sockopt, struct bpf_sockopt_kern *, ctx)
1882 const struct net *net = ctx ? sock_net(ctx->sk) : &init_net;
1884 return net->net_cookie;
1887 static const struct bpf_func_proto bpf_get_netns_cookie_sockopt_proto = {
1888 .func = bpf_get_netns_cookie_sockopt,
1890 .ret_type = RET_INTEGER,
1891 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
1895 static const struct bpf_func_proto *
1896 cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1900 case BPF_FUNC_get_netns_cookie:
1901 return &bpf_get_netns_cookie_sockopt_proto;
1902 case BPF_FUNC_sk_storage_get:
1903 return &bpf_sk_storage_get_proto;
1904 case BPF_FUNC_sk_storage_delete:
1905 return &bpf_sk_storage_delete_proto;
1906 case BPF_FUNC_setsockopt:
1907 if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT)
1908 return &bpf_sk_setsockopt_proto;
1910 case BPF_FUNC_getsockopt:
1911 if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT)
1912 return &bpf_sk_getsockopt_proto;
1916 case BPF_FUNC_tcp_sock:
1917 return &bpf_tcp_sock_proto;
1920 return cgroup_base_func_proto(func_id, prog);
1924 static bool cg_sockopt_is_valid_access(int off, int size,
1925 enum bpf_access_type type,
1926 const struct bpf_prog *prog,
1927 struct bpf_insn_access_aux *info)
1929 const int size_default = sizeof(__u32);
1931 if (off < 0 || off >= sizeof(struct bpf_sockopt))
1934 if (off % size != 0)
1937 if (type == BPF_WRITE) {
1939 case offsetof(struct bpf_sockopt, retval):
1940 if (size != size_default)
1942 return prog->expected_attach_type ==
1943 BPF_CGROUP_GETSOCKOPT;
1944 case offsetof(struct bpf_sockopt, optname):
1946 case offsetof(struct bpf_sockopt, level):
1947 if (size != size_default)
1949 return prog->expected_attach_type ==
1950 BPF_CGROUP_SETSOCKOPT;
1951 case offsetof(struct bpf_sockopt, optlen):
1952 return size == size_default;
1959 case offsetof(struct bpf_sockopt, sk):
1960 if (size != sizeof(__u64))
1962 info->reg_type = PTR_TO_SOCKET;
1964 case offsetof(struct bpf_sockopt, optval):
1965 if (size != sizeof(__u64))
1967 info->reg_type = PTR_TO_PACKET;
1969 case offsetof(struct bpf_sockopt, optval_end):
1970 if (size != sizeof(__u64))
1972 info->reg_type = PTR_TO_PACKET_END;
1974 case offsetof(struct bpf_sockopt, retval):
1975 if (size != size_default)
1977 return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT;
1979 if (size != size_default)
1986 #define CG_SOCKOPT_ACCESS_FIELD(T, F) \
1987 T(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, F), \
1988 si->dst_reg, si->src_reg, \
1989 offsetof(struct bpf_sockopt_kern, F))
1991 static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type,
1992 const struct bpf_insn *si,
1993 struct bpf_insn *insn_buf,
1994 struct bpf_prog *prog,
1997 struct bpf_insn *insn = insn_buf;
2000 case offsetof(struct bpf_sockopt, sk):
2001 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, sk);
2003 case offsetof(struct bpf_sockopt, level):
2004 if (type == BPF_WRITE)
2005 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, level);
2007 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, level);
2009 case offsetof(struct bpf_sockopt, optname):
2010 if (type == BPF_WRITE)
2011 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optname);
2013 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optname);
2015 case offsetof(struct bpf_sockopt, optlen):
2016 if (type == BPF_WRITE)
2017 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optlen);
2019 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optlen);
2021 case offsetof(struct bpf_sockopt, retval):
2022 if (type == BPF_WRITE)
2023 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, retval);
2025 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, retval);
2027 case offsetof(struct bpf_sockopt, optval):
2028 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval);
2030 case offsetof(struct bpf_sockopt, optval_end):
2031 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval_end);
2035 return insn - insn_buf;
2038 static int cg_sockopt_get_prologue(struct bpf_insn *insn_buf,
2040 const struct bpf_prog *prog)
2042 /* Nothing to do for sockopt argument. The data is kzalloc'ated.
2047 const struct bpf_verifier_ops cg_sockopt_verifier_ops = {
2048 .get_func_proto = cg_sockopt_func_proto,
2049 .is_valid_access = cg_sockopt_is_valid_access,
2050 .convert_ctx_access = cg_sockopt_convert_ctx_access,
2051 .gen_prologue = cg_sockopt_get_prologue,
2054 const struct bpf_prog_ops cg_sockopt_prog_ops = {