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_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 (type = 0; type < ARRAY_SIZE(cgrp->bpf.progs); type++) {
121 struct list_head *progs = &cgrp->bpf.progs[type];
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[type]);
133 old_array = rcu_dereference_protected(
134 cgrp->bpf.effective[type],
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 bpf_attach_type type)
203 p = cgroup_parent(cgrp);
207 u32 flags = p->bpf.flags[type];
210 if (flags & BPF_F_ALLOW_MULTI)
212 cnt = prog_list_length(&p->bpf.progs[type]);
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 bpf_attach_type type,
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[type] & BPF_F_ALLOW_MULTI))
240 cnt += prog_list_length(&p->bpf.progs[type]);
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[type] & BPF_F_ALLOW_MULTI))
255 list_for_each_entry(pl, &p->bpf.progs[type], 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 bpf_attach_type type,
273 struct bpf_prog_array *old_array)
275 old_array = rcu_replace_pointer(cgrp->bpf.effective[type], 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 bpf_attach_type type)
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, type, &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, type, 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 list_head *progs = &cgrp->bpf.progs[type];
440 struct bpf_prog *old_prog = NULL;
441 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
442 struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
443 struct bpf_prog_list *pl;
446 if (((flags & BPF_F_ALLOW_OVERRIDE) && (flags & BPF_F_ALLOW_MULTI)) ||
447 ((flags & BPF_F_REPLACE) && !(flags & BPF_F_ALLOW_MULTI)))
448 /* invalid combination */
450 if (link && (prog || replace_prog))
451 /* only either link or prog/replace_prog can be specified */
453 if (!!replace_prog != !!(flags & BPF_F_REPLACE))
454 /* replace_prog implies BPF_F_REPLACE, and vice versa */
457 if (!hierarchy_allows_attach(cgrp, type))
460 if (!list_empty(progs) && cgrp->bpf.flags[type] != saved_flags)
461 /* Disallow attaching non-overridable on top
462 * of existing overridable in this cgroup.
463 * Disallow attaching multi-prog if overridable or none
467 if (prog_list_length(progs) >= BPF_CGROUP_MAX_PROGS)
470 pl = find_attach_entry(progs, prog, link, replace_prog,
471 flags & BPF_F_ALLOW_MULTI);
475 if (bpf_cgroup_storages_alloc(storage, new_storage, type,
476 prog ? : link->link.prog, cgrp))
482 pl = kmalloc(sizeof(*pl), GFP_KERNEL);
484 bpf_cgroup_storages_free(new_storage);
487 list_add_tail(&pl->node, progs);
492 bpf_cgroup_storages_assign(pl->storage, storage);
493 cgrp->bpf.flags[type] = saved_flags;
495 err = update_effective_progs(cgrp, type);
500 bpf_prog_put(old_prog);
502 static_branch_inc(&cgroup_bpf_enabled_key[type]);
503 bpf_cgroup_storages_link(new_storage, cgrp, type);
511 bpf_cgroup_storages_free(new_storage);
519 /* Swap updated BPF program for given link in effective program arrays across
520 * all descendant cgroups. This function is guaranteed to succeed.
522 static void replace_effective_prog(struct cgroup *cgrp,
523 enum bpf_attach_type type,
524 struct bpf_cgroup_link *link)
526 struct bpf_prog_array_item *item;
527 struct cgroup_subsys_state *css;
528 struct bpf_prog_array *progs;
529 struct bpf_prog_list *pl;
530 struct list_head *head;
534 css_for_each_descendant_pre(css, &cgrp->self) {
535 struct cgroup *desc = container_of(css, struct cgroup, self);
537 if (percpu_ref_is_zero(&desc->bpf.refcnt))
540 /* find position of link in effective progs array */
541 for (pos = 0, cg = desc; cg; cg = cgroup_parent(cg)) {
542 if (pos && !(cg->bpf.flags[type] & BPF_F_ALLOW_MULTI))
545 head = &cg->bpf.progs[type];
546 list_for_each_entry(pl, head, node) {
547 if (!prog_list_prog(pl))
549 if (pl->link == link)
556 progs = rcu_dereference_protected(
557 desc->bpf.effective[type],
558 lockdep_is_held(&cgroup_mutex));
559 item = &progs->items[pos];
560 WRITE_ONCE(item->prog, link->link.prog);
565 * __cgroup_bpf_replace() - Replace link's program and propagate the change
567 * @cgrp: The cgroup which descendants to traverse
568 * @link: A link for which to replace BPF program
569 * @type: Type of attach operation
571 * Must be called with cgroup_mutex held.
573 static int __cgroup_bpf_replace(struct cgroup *cgrp,
574 struct bpf_cgroup_link *link,
575 struct bpf_prog *new_prog)
577 struct list_head *progs = &cgrp->bpf.progs[link->type];
578 struct bpf_prog *old_prog;
579 struct bpf_prog_list *pl;
582 if (link->link.prog->type != new_prog->type)
585 list_for_each_entry(pl, progs, node) {
586 if (pl->link == link) {
594 old_prog = xchg(&link->link.prog, new_prog);
595 replace_effective_prog(cgrp, link->type, link);
596 bpf_prog_put(old_prog);
600 static int cgroup_bpf_replace(struct bpf_link *link, struct bpf_prog *new_prog,
601 struct bpf_prog *old_prog)
603 struct bpf_cgroup_link *cg_link;
606 cg_link = container_of(link, struct bpf_cgroup_link, link);
608 mutex_lock(&cgroup_mutex);
609 /* link might have been auto-released by dying cgroup, so fail */
610 if (!cg_link->cgroup) {
614 if (old_prog && link->prog != old_prog) {
618 ret = __cgroup_bpf_replace(cg_link->cgroup, cg_link, new_prog);
620 mutex_unlock(&cgroup_mutex);
624 static struct bpf_prog_list *find_detach_entry(struct list_head *progs,
625 struct bpf_prog *prog,
626 struct bpf_cgroup_link *link,
629 struct bpf_prog_list *pl;
632 if (list_empty(progs))
633 /* report error when trying to detach and nothing is attached */
634 return ERR_PTR(-ENOENT);
636 /* to maintain backward compatibility NONE and OVERRIDE cgroups
637 * allow detaching with invalid FD (prog==NULL) in legacy mode
639 return list_first_entry(progs, typeof(*pl), node);
643 /* to detach MULTI prog the user has to specify valid FD
644 * of the program or link to be detached
646 return ERR_PTR(-EINVAL);
648 /* find the prog or link and detach it */
649 list_for_each_entry(pl, progs, node) {
650 if (pl->prog == prog && pl->link == link)
653 return ERR_PTR(-ENOENT);
657 * __cgroup_bpf_detach() - Detach the program or link from a cgroup, and
658 * propagate the change to descendants
659 * @cgrp: The cgroup which descendants to traverse
660 * @prog: A program to detach or NULL
661 * @prog: A link to detach or NULL
662 * @type: Type of detach operation
664 * At most one of @prog or @link can be non-NULL.
665 * Must be called with cgroup_mutex held.
667 int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
668 struct bpf_cgroup_link *link, enum bpf_attach_type type)
670 struct list_head *progs = &cgrp->bpf.progs[type];
671 u32 flags = cgrp->bpf.flags[type];
672 struct bpf_prog_list *pl;
673 struct bpf_prog *old_prog;
677 /* only one of prog or link can be specified */
680 pl = find_detach_entry(progs, prog, link, flags & BPF_F_ALLOW_MULTI);
684 /* mark it deleted, so it's ignored while recomputing effective */
689 err = update_effective_progs(cgrp, type);
693 /* now can actually delete it from this cgroup list */
696 if (list_empty(progs))
697 /* last program was detached, reset flags to zero */
698 cgrp->bpf.flags[type] = 0;
700 bpf_prog_put(old_prog);
701 static_branch_dec(&cgroup_bpf_enabled_key[type]);
705 /* restore back prog or link */
711 /* Must be called with cgroup_mutex held to avoid races. */
712 int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
713 union bpf_attr __user *uattr)
715 __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
716 enum bpf_attach_type type = attr->query.attach_type;
717 struct list_head *progs = &cgrp->bpf.progs[type];
718 u32 flags = cgrp->bpf.flags[type];
719 struct bpf_prog_array *effective;
720 struct bpf_prog *prog;
723 effective = rcu_dereference_protected(cgrp->bpf.effective[type],
724 lockdep_is_held(&cgroup_mutex));
726 if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE)
727 cnt = bpf_prog_array_length(effective);
729 cnt = prog_list_length(progs);
731 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
733 if (copy_to_user(&uattr->query.prog_cnt, &cnt, sizeof(cnt)))
735 if (attr->query.prog_cnt == 0 || !prog_ids || !cnt)
736 /* return early if user requested only program count + flags */
738 if (attr->query.prog_cnt < cnt) {
739 cnt = attr->query.prog_cnt;
743 if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
744 return bpf_prog_array_copy_to_user(effective, prog_ids, cnt);
746 struct bpf_prog_list *pl;
750 list_for_each_entry(pl, progs, node) {
751 prog = prog_list_prog(pl);
753 if (copy_to_user(prog_ids + i, &id, sizeof(id)))
762 int cgroup_bpf_prog_attach(const union bpf_attr *attr,
763 enum bpf_prog_type ptype, struct bpf_prog *prog)
765 struct bpf_prog *replace_prog = NULL;
769 cgrp = cgroup_get_from_fd(attr->target_fd);
771 return PTR_ERR(cgrp);
773 if ((attr->attach_flags & BPF_F_ALLOW_MULTI) &&
774 (attr->attach_flags & BPF_F_REPLACE)) {
775 replace_prog = bpf_prog_get_type(attr->replace_bpf_fd, ptype);
776 if (IS_ERR(replace_prog)) {
778 return PTR_ERR(replace_prog);
782 ret = cgroup_bpf_attach(cgrp, prog, replace_prog, NULL,
783 attr->attach_type, attr->attach_flags);
786 bpf_prog_put(replace_prog);
791 int cgroup_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
793 struct bpf_prog *prog;
797 cgrp = cgroup_get_from_fd(attr->target_fd);
799 return PTR_ERR(cgrp);
801 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
805 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type);
813 static void bpf_cgroup_link_release(struct bpf_link *link)
815 struct bpf_cgroup_link *cg_link =
816 container_of(link, struct bpf_cgroup_link, link);
819 /* link might have been auto-detached by dying cgroup already,
820 * in that case our work is done here
822 if (!cg_link->cgroup)
825 mutex_lock(&cgroup_mutex);
827 /* re-check cgroup under lock again */
828 if (!cg_link->cgroup) {
829 mutex_unlock(&cgroup_mutex);
833 WARN_ON(__cgroup_bpf_detach(cg_link->cgroup, NULL, cg_link,
836 cg = cg_link->cgroup;
837 cg_link->cgroup = NULL;
839 mutex_unlock(&cgroup_mutex);
844 static void bpf_cgroup_link_dealloc(struct bpf_link *link)
846 struct bpf_cgroup_link *cg_link =
847 container_of(link, struct bpf_cgroup_link, link);
852 static int bpf_cgroup_link_detach(struct bpf_link *link)
854 bpf_cgroup_link_release(link);
859 static void bpf_cgroup_link_show_fdinfo(const struct bpf_link *link,
860 struct seq_file *seq)
862 struct bpf_cgroup_link *cg_link =
863 container_of(link, struct bpf_cgroup_link, link);
866 mutex_lock(&cgroup_mutex);
868 cg_id = cgroup_id(cg_link->cgroup);
869 mutex_unlock(&cgroup_mutex);
873 "attach_type:\t%d\n",
878 static int bpf_cgroup_link_fill_link_info(const struct bpf_link *link,
879 struct bpf_link_info *info)
881 struct bpf_cgroup_link *cg_link =
882 container_of(link, struct bpf_cgroup_link, link);
885 mutex_lock(&cgroup_mutex);
887 cg_id = cgroup_id(cg_link->cgroup);
888 mutex_unlock(&cgroup_mutex);
890 info->cgroup.cgroup_id = cg_id;
891 info->cgroup.attach_type = cg_link->type;
895 static const struct bpf_link_ops bpf_cgroup_link_lops = {
896 .release = bpf_cgroup_link_release,
897 .dealloc = bpf_cgroup_link_dealloc,
898 .detach = bpf_cgroup_link_detach,
899 .update_prog = cgroup_bpf_replace,
900 .show_fdinfo = bpf_cgroup_link_show_fdinfo,
901 .fill_link_info = bpf_cgroup_link_fill_link_info,
904 int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
906 struct bpf_link_primer link_primer;
907 struct bpf_cgroup_link *link;
911 if (attr->link_create.flags)
914 cgrp = cgroup_get_from_fd(attr->link_create.target_fd);
916 return PTR_ERR(cgrp);
918 link = kzalloc(sizeof(*link), GFP_USER);
923 bpf_link_init(&link->link, BPF_LINK_TYPE_CGROUP, &bpf_cgroup_link_lops,
926 link->type = attr->link_create.attach_type;
928 err = bpf_link_prime(&link->link, &link_primer);
934 err = cgroup_bpf_attach(cgrp, NULL, NULL, link, link->type,
937 bpf_link_cleanup(&link_primer);
941 return bpf_link_settle(&link_primer);
948 int cgroup_bpf_prog_query(const union bpf_attr *attr,
949 union bpf_attr __user *uattr)
954 cgrp = cgroup_get_from_fd(attr->query.target_fd);
956 return PTR_ERR(cgrp);
958 ret = cgroup_bpf_query(cgrp, attr, uattr);
965 * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
966 * @sk: The socket sending or receiving traffic
967 * @skb: The skb that is being sent or received
968 * @type: The type of program to be exectuted
970 * If no socket is passed, or the socket is not of type INET or INET6,
971 * this function does nothing and returns 0.
973 * The program type passed in via @type must be suitable for network
974 * filtering. No further check is performed to assert that.
976 * For egress packets, this function can return:
977 * NET_XMIT_SUCCESS (0) - continue with packet output
978 * NET_XMIT_DROP (1) - drop packet and notify TCP to call cwr
979 * NET_XMIT_CN (2) - continue with packet output and notify TCP
981 * -EPERM - drop packet
983 * For ingress packets, this function will return -EPERM if any
984 * attached program was found and if it returned != 1 during execution.
985 * Otherwise 0 is returned.
987 int __cgroup_bpf_run_filter_skb(struct sock *sk,
989 enum bpf_attach_type type)
991 unsigned int offset = skb->data - skb_network_header(skb);
992 struct sock *save_sk;
993 void *saved_data_end;
997 if (!sk || !sk_fullsock(sk))
1000 if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
1003 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1006 __skb_push(skb, offset);
1008 /* compute pointers for the bpf prog */
1009 bpf_compute_and_save_data_end(skb, &saved_data_end);
1011 if (type == BPF_CGROUP_INET_EGRESS) {
1012 ret = BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(
1013 cgrp->bpf.effective[type], skb, __bpf_prog_run_save_cb);
1015 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], skb,
1016 __bpf_prog_run_save_cb);
1017 ret = (ret == 1 ? 0 : -EPERM);
1019 bpf_restore_data_end(skb, saved_data_end);
1020 __skb_pull(skb, offset);
1025 EXPORT_SYMBOL(__cgroup_bpf_run_filter_skb);
1028 * __cgroup_bpf_run_filter_sk() - Run a program on a sock
1029 * @sk: sock structure to manipulate
1030 * @type: The type of program to be exectuted
1032 * socket is passed is expected to be of type INET or INET6.
1034 * The program type passed in via @type must be suitable for sock
1035 * filtering. No further check is performed to assert that.
1037 * This function will return %-EPERM if any if an attached program was found
1038 * and if it returned != 1 during execution. In all other cases, 0 is returned.
1040 int __cgroup_bpf_run_filter_sk(struct sock *sk,
1041 enum bpf_attach_type type)
1043 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1046 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], sk, BPF_PROG_RUN);
1047 return ret == 1 ? 0 : -EPERM;
1049 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sk);
1052 * __cgroup_bpf_run_filter_sock_addr() - Run a program on a sock and
1053 * provided by user sockaddr
1054 * @sk: sock struct that will use sockaddr
1055 * @uaddr: sockaddr struct provided by user
1056 * @type: The type of program to be exectuted
1057 * @t_ctx: Pointer to attach type specific context
1058 * @flags: Pointer to u32 which contains higher bits of BPF program
1059 * return value (OR'ed together).
1061 * socket is expected to be of type INET or INET6.
1063 * This function will return %-EPERM if an attached program is found and
1064 * returned value != 1 during execution. In all other cases, 0 is returned.
1066 int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
1067 struct sockaddr *uaddr,
1068 enum bpf_attach_type type,
1072 struct bpf_sock_addr_kern ctx = {
1077 struct sockaddr_storage unspec;
1078 struct cgroup *cgrp;
1081 /* Check socket family since not all sockets represent network
1082 * endpoint (e.g. AF_UNIX).
1084 if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
1088 memset(&unspec, 0, sizeof(unspec));
1089 ctx.uaddr = (struct sockaddr *)&unspec;
1092 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1093 ret = BPF_PROG_RUN_ARRAY_FLAGS(cgrp->bpf.effective[type], &ctx,
1094 BPF_PROG_RUN, flags);
1096 return ret == 1 ? 0 : -EPERM;
1098 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_addr);
1101 * __cgroup_bpf_run_filter_sock_ops() - Run a program on a sock
1102 * @sk: socket to get cgroup from
1103 * @sock_ops: bpf_sock_ops_kern struct to pass to program. Contains
1104 * sk with connection information (IP addresses, etc.) May not contain
1105 * cgroup info if it is a req sock.
1106 * @type: The type of program to be exectuted
1108 * socket passed is expected to be of type INET or INET6.
1110 * The program type passed in via @type must be suitable for sock_ops
1111 * filtering. No further check is performed to assert that.
1113 * This function will return %-EPERM if any if an attached program was found
1114 * and if it returned != 1 during execution. In all other cases, 0 is returned.
1116 int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
1117 struct bpf_sock_ops_kern *sock_ops,
1118 enum bpf_attach_type type)
1120 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1123 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], sock_ops,
1125 return ret == 1 ? 0 : -EPERM;
1127 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_ops);
1129 int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
1130 short access, enum bpf_attach_type type)
1132 struct cgroup *cgrp;
1133 struct bpf_cgroup_dev_ctx ctx = {
1134 .access_type = (access << 16) | dev_type,
1141 cgrp = task_dfl_cgroup(current);
1142 allow = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], &ctx,
1149 static const struct bpf_func_proto *
1150 cgroup_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1153 case BPF_FUNC_get_current_uid_gid:
1154 return &bpf_get_current_uid_gid_proto;
1155 case BPF_FUNC_get_local_storage:
1156 return &bpf_get_local_storage_proto;
1157 case BPF_FUNC_get_current_cgroup_id:
1158 return &bpf_get_current_cgroup_id_proto;
1159 case BPF_FUNC_perf_event_output:
1160 return &bpf_event_output_data_proto;
1162 return bpf_base_func_proto(func_id);
1166 static const struct bpf_func_proto *
1167 cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1169 return cgroup_base_func_proto(func_id, prog);
1172 static bool cgroup_dev_is_valid_access(int off, int size,
1173 enum bpf_access_type type,
1174 const struct bpf_prog *prog,
1175 struct bpf_insn_access_aux *info)
1177 const int size_default = sizeof(__u32);
1179 if (type == BPF_WRITE)
1182 if (off < 0 || off + size > sizeof(struct bpf_cgroup_dev_ctx))
1184 /* The verifier guarantees that size > 0. */
1185 if (off % size != 0)
1189 case bpf_ctx_range(struct bpf_cgroup_dev_ctx, access_type):
1190 bpf_ctx_record_field_size(info, size_default);
1191 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
1195 if (size != size_default)
1202 const struct bpf_prog_ops cg_dev_prog_ops = {
1205 const struct bpf_verifier_ops cg_dev_verifier_ops = {
1206 .get_func_proto = cgroup_dev_func_proto,
1207 .is_valid_access = cgroup_dev_is_valid_access,
1211 * __cgroup_bpf_run_filter_sysctl - Run a program on sysctl
1213 * @head: sysctl table header
1214 * @table: sysctl table
1215 * @write: sysctl is being read (= 0) or written (= 1)
1216 * @buf: pointer to buffer (in and out)
1217 * @pcount: value-result argument: value is size of buffer pointed to by @buf,
1218 * result is size of @new_buf if program set new value, initial value
1220 * @ppos: value-result argument: value is position at which read from or write
1221 * to sysctl is happening, result is new position if program overrode it,
1222 * initial value otherwise
1223 * @type: type of program to be executed
1225 * Program is run when sysctl is being accessed, either read or written, and
1226 * can allow or deny such access.
1228 * This function will return %-EPERM if an attached program is found and
1229 * returned value != 1 during execution. In all other cases 0 is returned.
1231 int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
1232 struct ctl_table *table, int write,
1233 char **buf, size_t *pcount, loff_t *ppos,
1234 enum bpf_attach_type type)
1236 struct bpf_sysctl_kern ctx = {
1242 .cur_len = PAGE_SIZE,
1247 struct cgroup *cgrp;
1251 ctx.cur_val = kmalloc_track_caller(ctx.cur_len, GFP_KERNEL);
1253 table->proc_handler(table, 0, ctx.cur_val, &ctx.cur_len, &pos)) {
1254 /* Let BPF program decide how to proceed. */
1258 if (write && *buf && *pcount) {
1259 /* BPF program should be able to override new value with a
1260 * buffer bigger than provided by user.
1262 ctx.new_val = kmalloc_track_caller(PAGE_SIZE, GFP_KERNEL);
1263 ctx.new_len = min_t(size_t, PAGE_SIZE, *pcount);
1265 memcpy(ctx.new_val, *buf, ctx.new_len);
1267 /* Let BPF program decide how to proceed. */
1273 cgrp = task_dfl_cgroup(current);
1274 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], &ctx, BPF_PROG_RUN);
1279 if (ret == 1 && ctx.new_updated) {
1282 *pcount = ctx.new_len;
1287 return ret == 1 ? 0 : -EPERM;
1291 static bool __cgroup_bpf_prog_array_is_empty(struct cgroup *cgrp,
1292 enum bpf_attach_type attach_type)
1294 struct bpf_prog_array *prog_array;
1298 prog_array = rcu_dereference(cgrp->bpf.effective[attach_type]);
1299 empty = bpf_prog_array_is_empty(prog_array);
1305 static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
1306 struct bpf_sockopt_buf *buf)
1308 if (unlikely(max_optlen < 0))
1311 if (unlikely(max_optlen > PAGE_SIZE)) {
1312 /* We don't expose optvals that are greater than PAGE_SIZE
1313 * to the BPF program.
1315 max_optlen = PAGE_SIZE;
1318 if (max_optlen <= sizeof(buf->data)) {
1319 /* When the optval fits into BPF_SOCKOPT_KERN_BUF_SIZE
1320 * bytes avoid the cost of kzalloc.
1322 ctx->optval = buf->data;
1323 ctx->optval_end = ctx->optval + max_optlen;
1327 ctx->optval = kzalloc(max_optlen, GFP_USER);
1331 ctx->optval_end = ctx->optval + max_optlen;
1336 static void sockopt_free_buf(struct bpf_sockopt_kern *ctx,
1337 struct bpf_sockopt_buf *buf)
1339 if (ctx->optval == buf->data)
1344 static bool sockopt_buf_allocated(struct bpf_sockopt_kern *ctx,
1345 struct bpf_sockopt_buf *buf)
1347 return ctx->optval != buf->data;
1350 int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
1351 int *optname, char __user *optval,
1352 int *optlen, char **kernel_optval)
1354 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1355 struct bpf_sockopt_buf buf = {};
1356 struct bpf_sockopt_kern ctx = {
1359 .optname = *optname,
1361 int ret, max_optlen;
1363 /* Opportunistic check to see whether we have any BPF program
1364 * attached to the hook so we don't waste time allocating
1365 * memory and locking the socket.
1367 if (__cgroup_bpf_prog_array_is_empty(cgrp, BPF_CGROUP_SETSOCKOPT))
1370 /* Allocate a bit more than the initial user buffer for
1371 * BPF program. The canonical use case is overriding
1372 * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic).
1374 max_optlen = max_t(int, 16, *optlen);
1376 max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
1380 ctx.optlen = *optlen;
1382 if (copy_from_user(ctx.optval, optval, min(*optlen, max_optlen)) != 0) {
1388 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[BPF_CGROUP_SETSOCKOPT],
1389 &ctx, BPF_PROG_RUN);
1397 if (ctx.optlen == -1) {
1398 /* optlen set to -1, bypass kernel */
1400 } else if (ctx.optlen > max_optlen || ctx.optlen < -1) {
1401 /* optlen is out of bounds */
1404 /* optlen within bounds, run kernel handler */
1407 /* export any potential modifications */
1409 *optname = ctx.optname;
1411 /* optlen == 0 from BPF indicates that we should
1412 * use original userspace data.
1414 if (ctx.optlen != 0) {
1415 *optlen = ctx.optlen;
1416 /* We've used bpf_sockopt_kern->buf as an intermediary
1417 * storage, but the BPF program indicates that we need
1418 * to pass this data to the kernel setsockopt handler.
1419 * No way to export on-stack buf, have to allocate a
1422 if (!sockopt_buf_allocated(&ctx, &buf)) {
1423 void *p = kmalloc(ctx.optlen, GFP_USER);
1429 memcpy(p, ctx.optval, ctx.optlen);
1432 *kernel_optval = ctx.optval;
1434 /* export and don't free sockopt buf */
1440 sockopt_free_buf(&ctx, &buf);
1444 int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
1445 int optname, char __user *optval,
1446 int __user *optlen, int max_optlen,
1449 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1450 struct bpf_sockopt_buf buf = {};
1451 struct bpf_sockopt_kern ctx = {
1459 /* Opportunistic check to see whether we have any BPF program
1460 * attached to the hook so we don't waste time allocating
1461 * memory and locking the socket.
1463 if (__cgroup_bpf_prog_array_is_empty(cgrp, BPF_CGROUP_GETSOCKOPT))
1466 ctx.optlen = max_optlen;
1468 max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
1473 /* If kernel getsockopt finished successfully,
1474 * copy whatever was returned to the user back
1475 * into our temporary buffer. Set optlen to the
1476 * one that kernel returned as well to let
1477 * BPF programs inspect the value.
1480 if (get_user(ctx.optlen, optlen)) {
1485 if (ctx.optlen < 0) {
1490 if (copy_from_user(ctx.optval, optval,
1491 min(ctx.optlen, max_optlen)) != 0) {
1498 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[BPF_CGROUP_GETSOCKOPT],
1499 &ctx, BPF_PROG_RUN);
1507 if (ctx.optlen > max_optlen || ctx.optlen < 0) {
1512 /* BPF programs only allowed to set retval to 0, not some
1515 if (ctx.retval != 0 && ctx.retval != retval) {
1520 if (ctx.optlen != 0) {
1521 if (copy_to_user(optval, ctx.optval, ctx.optlen) ||
1522 put_user(ctx.optlen, optlen)) {
1531 sockopt_free_buf(&ctx, &buf);
1535 int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level,
1536 int optname, void *optval,
1537 int *optlen, int retval)
1539 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1540 struct bpf_sockopt_kern ctx = {
1547 .optval_end = optval + *optlen,
1551 /* Note that __cgroup_bpf_run_filter_getsockopt doesn't copy
1552 * user data back into BPF buffer when reval != 0. This is
1553 * done as an optimization to avoid extra copy, assuming
1554 * kernel won't populate the data in case of an error.
1555 * Here we always pass the data and memset() should
1556 * be called if that data shouldn't be "exported".
1559 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[BPF_CGROUP_GETSOCKOPT],
1560 &ctx, BPF_PROG_RUN);
1564 if (ctx.optlen > *optlen)
1567 /* BPF programs only allowed to set retval to 0, not some
1570 if (ctx.retval != 0 && ctx.retval != retval)
1573 /* BPF programs can shrink the buffer, export the modifications.
1575 if (ctx.optlen != 0)
1576 *optlen = ctx.optlen;
1582 static ssize_t sysctl_cpy_dir(const struct ctl_dir *dir, char **bufp,
1585 ssize_t tmp_ret = 0, ret;
1587 if (dir->header.parent) {
1588 tmp_ret = sysctl_cpy_dir(dir->header.parent, bufp, lenp);
1593 ret = strscpy(*bufp, dir->header.ctl_table[0].procname, *lenp);
1600 /* Avoid leading slash. */
1604 tmp_ret = strscpy(*bufp, "/", *lenp);
1610 return ret + tmp_ret;
1613 BPF_CALL_4(bpf_sysctl_get_name, struct bpf_sysctl_kern *, ctx, char *, buf,
1614 size_t, buf_len, u64, flags)
1616 ssize_t tmp_ret = 0, ret;
1621 if (!(flags & BPF_F_SYSCTL_BASE_NAME)) {
1624 tmp_ret = sysctl_cpy_dir(ctx->head->parent, &buf, &buf_len);
1629 ret = strscpy(buf, ctx->table->procname, buf_len);
1631 return ret < 0 ? ret : tmp_ret + ret;
1634 static const struct bpf_func_proto bpf_sysctl_get_name_proto = {
1635 .func = bpf_sysctl_get_name,
1637 .ret_type = RET_INTEGER,
1638 .arg1_type = ARG_PTR_TO_CTX,
1639 .arg2_type = ARG_PTR_TO_MEM,
1640 .arg3_type = ARG_CONST_SIZE,
1641 .arg4_type = ARG_ANYTHING,
1644 static int copy_sysctl_value(char *dst, size_t dst_len, char *src,
1653 if (!src || !src_len) {
1654 memset(dst, 0, dst_len);
1658 memcpy(dst, src, min(dst_len, src_len));
1660 if (dst_len > src_len) {
1661 memset(dst + src_len, '\0', dst_len - src_len);
1665 dst[dst_len - 1] = '\0';
1670 BPF_CALL_3(bpf_sysctl_get_current_value, struct bpf_sysctl_kern *, ctx,
1671 char *, buf, size_t, buf_len)
1673 return copy_sysctl_value(buf, buf_len, ctx->cur_val, ctx->cur_len);
1676 static const struct bpf_func_proto bpf_sysctl_get_current_value_proto = {
1677 .func = bpf_sysctl_get_current_value,
1679 .ret_type = RET_INTEGER,
1680 .arg1_type = ARG_PTR_TO_CTX,
1681 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1682 .arg3_type = ARG_CONST_SIZE,
1685 BPF_CALL_3(bpf_sysctl_get_new_value, struct bpf_sysctl_kern *, ctx, char *, buf,
1690 memset(buf, '\0', buf_len);
1693 return copy_sysctl_value(buf, buf_len, ctx->new_val, ctx->new_len);
1696 static const struct bpf_func_proto bpf_sysctl_get_new_value_proto = {
1697 .func = bpf_sysctl_get_new_value,
1699 .ret_type = RET_INTEGER,
1700 .arg1_type = ARG_PTR_TO_CTX,
1701 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1702 .arg3_type = ARG_CONST_SIZE,
1705 BPF_CALL_3(bpf_sysctl_set_new_value, struct bpf_sysctl_kern *, ctx,
1706 const char *, buf, size_t, buf_len)
1708 if (!ctx->write || !ctx->new_val || !ctx->new_len || !buf || !buf_len)
1711 if (buf_len > PAGE_SIZE - 1)
1714 memcpy(ctx->new_val, buf, buf_len);
1715 ctx->new_len = buf_len;
1716 ctx->new_updated = 1;
1721 static const struct bpf_func_proto bpf_sysctl_set_new_value_proto = {
1722 .func = bpf_sysctl_set_new_value,
1724 .ret_type = RET_INTEGER,
1725 .arg1_type = ARG_PTR_TO_CTX,
1726 .arg2_type = ARG_PTR_TO_MEM,
1727 .arg3_type = ARG_CONST_SIZE,
1730 static const struct bpf_func_proto *
1731 sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1734 case BPF_FUNC_strtol:
1735 return &bpf_strtol_proto;
1736 case BPF_FUNC_strtoul:
1737 return &bpf_strtoul_proto;
1738 case BPF_FUNC_sysctl_get_name:
1739 return &bpf_sysctl_get_name_proto;
1740 case BPF_FUNC_sysctl_get_current_value:
1741 return &bpf_sysctl_get_current_value_proto;
1742 case BPF_FUNC_sysctl_get_new_value:
1743 return &bpf_sysctl_get_new_value_proto;
1744 case BPF_FUNC_sysctl_set_new_value:
1745 return &bpf_sysctl_set_new_value_proto;
1747 return cgroup_base_func_proto(func_id, prog);
1751 static bool sysctl_is_valid_access(int off, int size, enum bpf_access_type type,
1752 const struct bpf_prog *prog,
1753 struct bpf_insn_access_aux *info)
1755 const int size_default = sizeof(__u32);
1757 if (off < 0 || off + size > sizeof(struct bpf_sysctl) || off % size)
1761 case bpf_ctx_range(struct bpf_sysctl, write):
1762 if (type != BPF_READ)
1764 bpf_ctx_record_field_size(info, size_default);
1765 return bpf_ctx_narrow_access_ok(off, size, size_default);
1766 case bpf_ctx_range(struct bpf_sysctl, file_pos):
1767 if (type == BPF_READ) {
1768 bpf_ctx_record_field_size(info, size_default);
1769 return bpf_ctx_narrow_access_ok(off, size, size_default);
1771 return size == size_default;
1778 static u32 sysctl_convert_ctx_access(enum bpf_access_type type,
1779 const struct bpf_insn *si,
1780 struct bpf_insn *insn_buf,
1781 struct bpf_prog *prog, u32 *target_size)
1783 struct bpf_insn *insn = insn_buf;
1787 case offsetof(struct bpf_sysctl, write):
1788 *insn++ = BPF_LDX_MEM(
1789 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
1790 bpf_target_off(struct bpf_sysctl_kern, write,
1791 sizeof_field(struct bpf_sysctl_kern,
1795 case offsetof(struct bpf_sysctl, file_pos):
1796 /* ppos is a pointer so it should be accessed via indirect
1797 * loads and stores. Also for stores additional temporary
1798 * register is used since neither src_reg nor dst_reg can be
1801 if (type == BPF_WRITE) {
1802 int treg = BPF_REG_9;
1804 if (si->src_reg == treg || si->dst_reg == treg)
1806 if (si->src_reg == treg || si->dst_reg == treg)
1808 *insn++ = BPF_STX_MEM(
1809 BPF_DW, si->dst_reg, treg,
1810 offsetof(struct bpf_sysctl_kern, tmp_reg));
1811 *insn++ = BPF_LDX_MEM(
1812 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
1814 offsetof(struct bpf_sysctl_kern, ppos));
1815 *insn++ = BPF_STX_MEM(
1816 BPF_SIZEOF(u32), treg, si->src_reg,
1817 bpf_ctx_narrow_access_offset(
1818 0, sizeof(u32), sizeof(loff_t)));
1819 *insn++ = BPF_LDX_MEM(
1820 BPF_DW, treg, si->dst_reg,
1821 offsetof(struct bpf_sysctl_kern, tmp_reg));
1823 *insn++ = BPF_LDX_MEM(
1824 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
1825 si->dst_reg, si->src_reg,
1826 offsetof(struct bpf_sysctl_kern, ppos));
1827 read_size = bpf_size_to_bytes(BPF_SIZE(si->code));
1828 *insn++ = BPF_LDX_MEM(
1829 BPF_SIZE(si->code), si->dst_reg, si->dst_reg,
1830 bpf_ctx_narrow_access_offset(
1831 0, read_size, sizeof(loff_t)));
1833 *target_size = sizeof(u32);
1837 return insn - insn_buf;
1840 const struct bpf_verifier_ops cg_sysctl_verifier_ops = {
1841 .get_func_proto = sysctl_func_proto,
1842 .is_valid_access = sysctl_is_valid_access,
1843 .convert_ctx_access = sysctl_convert_ctx_access,
1846 const struct bpf_prog_ops cg_sysctl_prog_ops = {
1849 static const struct bpf_func_proto *
1850 cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1854 case BPF_FUNC_sk_storage_get:
1855 return &bpf_sk_storage_get_proto;
1856 case BPF_FUNC_sk_storage_delete:
1857 return &bpf_sk_storage_delete_proto;
1860 case BPF_FUNC_tcp_sock:
1861 return &bpf_tcp_sock_proto;
1864 return cgroup_base_func_proto(func_id, prog);
1868 static bool cg_sockopt_is_valid_access(int off, int size,
1869 enum bpf_access_type type,
1870 const struct bpf_prog *prog,
1871 struct bpf_insn_access_aux *info)
1873 const int size_default = sizeof(__u32);
1875 if (off < 0 || off >= sizeof(struct bpf_sockopt))
1878 if (off % size != 0)
1881 if (type == BPF_WRITE) {
1883 case offsetof(struct bpf_sockopt, retval):
1884 if (size != size_default)
1886 return prog->expected_attach_type ==
1887 BPF_CGROUP_GETSOCKOPT;
1888 case offsetof(struct bpf_sockopt, optname):
1890 case offsetof(struct bpf_sockopt, level):
1891 if (size != size_default)
1893 return prog->expected_attach_type ==
1894 BPF_CGROUP_SETSOCKOPT;
1895 case offsetof(struct bpf_sockopt, optlen):
1896 return size == size_default;
1903 case offsetof(struct bpf_sockopt, sk):
1904 if (size != sizeof(__u64))
1906 info->reg_type = PTR_TO_SOCKET;
1908 case offsetof(struct bpf_sockopt, optval):
1909 if (size != sizeof(__u64))
1911 info->reg_type = PTR_TO_PACKET;
1913 case offsetof(struct bpf_sockopt, optval_end):
1914 if (size != sizeof(__u64))
1916 info->reg_type = PTR_TO_PACKET_END;
1918 case offsetof(struct bpf_sockopt, retval):
1919 if (size != size_default)
1921 return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT;
1923 if (size != size_default)
1930 #define CG_SOCKOPT_ACCESS_FIELD(T, F) \
1931 T(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, F), \
1932 si->dst_reg, si->src_reg, \
1933 offsetof(struct bpf_sockopt_kern, F))
1935 static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type,
1936 const struct bpf_insn *si,
1937 struct bpf_insn *insn_buf,
1938 struct bpf_prog *prog,
1941 struct bpf_insn *insn = insn_buf;
1944 case offsetof(struct bpf_sockopt, sk):
1945 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, sk);
1947 case offsetof(struct bpf_sockopt, level):
1948 if (type == BPF_WRITE)
1949 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, level);
1951 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, level);
1953 case offsetof(struct bpf_sockopt, optname):
1954 if (type == BPF_WRITE)
1955 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optname);
1957 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optname);
1959 case offsetof(struct bpf_sockopt, optlen):
1960 if (type == BPF_WRITE)
1961 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optlen);
1963 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optlen);
1965 case offsetof(struct bpf_sockopt, retval):
1966 if (type == BPF_WRITE)
1967 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, retval);
1969 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, retval);
1971 case offsetof(struct bpf_sockopt, optval):
1972 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval);
1974 case offsetof(struct bpf_sockopt, optval_end):
1975 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval_end);
1979 return insn - insn_buf;
1982 static int cg_sockopt_get_prologue(struct bpf_insn *insn_buf,
1984 const struct bpf_prog *prog)
1986 /* Nothing to do for sockopt argument. The data is kzalloc'ated.
1991 const struct bpf_verifier_ops cg_sockopt_verifier_ops = {
1992 .get_func_proto = cg_sockopt_func_proto,
1993 .is_valid_access = cg_sockopt_is_valid_access,
1994 .convert_ctx_access = cg_sockopt_convert_ctx_access,
1995 .gen_prologue = cg_sockopt_get_prologue,
1998 const struct bpf_prog_ops cg_sockopt_prog_ops = {