Lines Matching +full:cost +full:- +full:effective
1 // SPDX-License-Identifier: GPL-2.0-only
16 #include <linux/bpf-cgroup.h>
22 #include "../cgroup/cgroup-internal.h"
63 array = rcu_dereference(cgrp->effective[atype]); in bpf_prog_run_array_cg()
64 item = &array->items[0]; in bpf_prog_run_array_cg()
66 while ((prog = READ_ONCE(item->prog))) { in bpf_prog_run_array_cg()
74 run_ctx.retval = -EPERM; in bpf_prog_run_array_cg()
95 shim_prog = (const struct bpf_prog *)((void *)insn - offsetof(struct bpf_prog, insnsi)); in __cgroup_bpf_run_lsm_sock()
97 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_lsm_sock()
99 ret = bpf_prog_run_array_cg(&cgrp->bpf, in __cgroup_bpf_run_lsm_sock()
100 shim_prog->aux->cgroup_atype, in __cgroup_bpf_run_lsm_sock()
117 shim_prog = (const struct bpf_prog *)((void *)insn - offsetof(struct bpf_prog, insnsi)); in __cgroup_bpf_run_lsm_socket()
119 cgrp = sock_cgroup_ptr(&sock->sk->sk_cgrp_data); in __cgroup_bpf_run_lsm_socket()
121 ret = bpf_prog_run_array_cg(&cgrp->bpf, in __cgroup_bpf_run_lsm_socket()
122 shim_prog->aux->cgroup_atype, in __cgroup_bpf_run_lsm_socket()
135 shim_prog = (const struct bpf_prog *)((void *)insn - offsetof(struct bpf_prog, insnsi)); in __cgroup_bpf_run_lsm_current()
140 ret = bpf_prog_run_array_cg(&cgrp->bpf, in __cgroup_bpf_run_lsm_current()
141 shim_prog->aux->cgroup_atype, in __cgroup_bpf_run_lsm_current()
172 return -E2BIG; in bpf_cgroup_atype_find()
178 int i = cgroup_atype - CGROUP_LSM_START; in bpf_cgroup_atype_get()
191 int i = cgroup_atype - CGROUP_LSM_START; in bpf_cgroup_atype_put()
194 if (--cgroup_lsm_atype[i].refcnt <= 0) in bpf_cgroup_atype_put()
205 return -EOPNOTSUPP; in bpf_cgroup_atype_find()
212 percpu_ref_kill(&cgrp->bpf.refcnt); in cgroup_bpf_offline()
237 map = prog->aux->cgroup_storage[stype]; in bpf_cgroup_storages_alloc()
248 return -ENOMEM; in bpf_cgroup_storages_alloc()
276 /* Called when bpf_cgroup_link is auto-detached from dying cgroup.
283 cgroup_put(link->cgroup); in bpf_cgroup_link_auto_detach()
284 link->cgroup = NULL; in bpf_cgroup_link_auto_detach()
288 * cgroup_bpf_release() - put references of all bpf programs and
297 struct list_head *storages = &cgrp->bpf.storages; in cgroup_bpf_release()
304 for (atype = 0; atype < ARRAY_SIZE(cgrp->bpf.progs); atype++) { in cgroup_bpf_release()
305 struct hlist_head *progs = &cgrp->bpf.progs[atype]; in cgroup_bpf_release()
310 hlist_del(&pl->node); in cgroup_bpf_release()
311 if (pl->prog) { in cgroup_bpf_release()
312 if (pl->prog->expected_attach_type == BPF_LSM_CGROUP) in cgroup_bpf_release()
313 bpf_trampoline_unlink_cgroup_shim(pl->prog); in cgroup_bpf_release()
314 bpf_prog_put(pl->prog); in cgroup_bpf_release()
316 if (pl->link) { in cgroup_bpf_release()
317 if (pl->link->link.prog->expected_attach_type == BPF_LSM_CGROUP) in cgroup_bpf_release()
318 bpf_trampoline_unlink_cgroup_shim(pl->link->link.prog); in cgroup_bpf_release()
319 bpf_cgroup_link_auto_detach(pl->link); in cgroup_bpf_release()
325 cgrp->bpf.effective[atype], in cgroup_bpf_release()
340 percpu_ref_exit(&cgrp->bpf.refcnt); in cgroup_bpf_release()
345 * cgroup_bpf_release_fn() - callback used to schedule releasing
353 INIT_WORK(&cgrp->bpf.release_work, cgroup_bpf_release); in cgroup_bpf_release_fn()
354 queue_work(cgroup_bpf_destroy_wq, &cgrp->bpf.release_work); in cgroup_bpf_release_fn()
362 if (pl->prog) in prog_list_prog()
363 return pl->prog; in prog_list_prog()
364 if (pl->link) in prog_list_prog()
365 return pl->link->link.prog; in prog_list_prog()
380 if (preorder_cnt && (pl->flags & BPF_F_PREORDER)) in prog_list_length()
387 /* if parent has non-overridable prog attached,
389 * if parent has overridable or multi-prog, allow attaching
400 u32 flags = p->bpf.flags[atype]; in hierarchy_allows_attach()
405 cnt = prog_list_length(&p->bpf.progs[atype], NULL); in hierarchy_allows_attach()
414 /* compute a chain of effective programs for a given cgroup:
417 * Note that parent's F_ALLOW_OVERRIDE-type program is yielding
430 /* count number of effective programs by walking parents */ in compute_effective_progs()
432 if (cnt == 0 || (p->bpf.flags[atype] & BPF_F_ALLOW_MULTI)) in compute_effective_progs()
433 cnt += prog_list_length(&p->bpf.progs[atype], &preorder_cnt); in compute_effective_progs()
439 return -ENOMEM; in compute_effective_progs()
441 /* populate the array with effective progs */ in compute_effective_progs()
445 bstart = preorder_cnt - 1; in compute_effective_progs()
447 if (cnt > 0 && !(p->bpf.flags[atype] & BPF_F_ALLOW_MULTI)) in compute_effective_progs()
451 hlist_for_each_entry(pl, &p->bpf.progs[atype], node) { in compute_effective_progs()
455 if (pl->flags & BPF_F_PREORDER) { in compute_effective_progs()
456 item = &progs->items[bstart]; in compute_effective_progs()
457 bstart--; in compute_effective_progs()
459 item = &progs->items[fstart]; in compute_effective_progs()
462 item->prog = prog_list_prog(pl); in compute_effective_progs()
463 bpf_cgroup_storages_assign(item->cgroup_storage, in compute_effective_progs()
464 pl->storage); in compute_effective_progs()
468 /* reverse pre-ordering progs at this cgroup level */ in compute_effective_progs()
469 for (i = bstart + 1, j = init_bstart; i < j; i++, j--) in compute_effective_progs()
470 swap(progs->items[i], progs->items[j]); in compute_effective_progs()
482 old_array = rcu_replace_pointer(cgrp->bpf.effective[atype], old_array, in activate_effective_progs()
491 * cgroup_bpf_inherit() - inherit effective programs from parent
499 #define NR ARRAY_SIZE(cgrp->bpf.effective) in cgroup_bpf_inherit()
504 ret = percpu_ref_init(&cgrp->bpf.refcnt, cgroup_bpf_release_fn, 0, in cgroup_bpf_inherit()
513 INIT_HLIST_HEAD(&cgrp->bpf.progs[i]); in cgroup_bpf_inherit()
515 INIT_LIST_HEAD(&cgrp->bpf.storages); in cgroup_bpf_inherit()
532 percpu_ref_exit(&cgrp->bpf.refcnt); in cgroup_bpf_inherit()
534 return -ENOMEM; in cgroup_bpf_inherit()
543 /* allocate and recompute effective prog arrays */ in update_effective_progs()
544 css_for_each_descendant_pre(css, &cgrp->self) { in update_effective_progs()
547 if (percpu_ref_is_zero(&desc->bpf.refcnt)) in update_effective_progs()
550 err = compute_effective_progs(desc, atype, &desc->bpf.inactive); in update_effective_progs()
556 css_for_each_descendant_pre(css, &cgrp->self) { in update_effective_progs()
559 if (percpu_ref_is_zero(&desc->bpf.refcnt)) { in update_effective_progs()
560 if (unlikely(desc->bpf.inactive)) { in update_effective_progs()
561 bpf_prog_array_free(desc->bpf.inactive); in update_effective_progs()
562 desc->bpf.inactive = NULL; in update_effective_progs()
567 activate_effective_progs(desc, atype, desc->bpf.inactive); in update_effective_progs()
568 desc->bpf.inactive = NULL; in update_effective_progs()
574 /* oom while computing effective. Free all computed effective arrays in update_effective_progs()
577 css_for_each_descendant_pre(css, &cgrp->self) { in update_effective_progs()
580 bpf_prog_array_free(desc->bpf.inactive); in update_effective_progs()
581 desc->bpf.inactive = NULL; in update_effective_progs()
597 /* single-attach case */ in find_attach_entry()
601 return hlist_entry(progs->first, typeof(*pl), node); in find_attach_entry()
605 if (prog && pl->prog == prog && prog != replace_prog) in find_attach_entry()
607 return ERR_PTR(-EINVAL); in find_attach_entry()
608 if (link && pl->link == link) in find_attach_entry()
610 return ERR_PTR(-EINVAL); in find_attach_entry()
613 /* direct prog multi-attach w/ replacement case */ in find_attach_entry()
616 if (pl->prog == replace_prog) in find_attach_entry()
621 return ERR_PTR(-ENOENT); in find_attach_entry()
628 * __cgroup_bpf_attach() - Attach the program or the link to a cgroup, and
637 * Exactly one of @prog or @link can be non-null.
649 struct bpf_prog *new_prog = prog ? : link->link.prog; in __cgroup_bpf_attach()
658 return -EINVAL; in __cgroup_bpf_attach()
661 return -EINVAL; in __cgroup_bpf_attach()
664 return -EINVAL; in __cgroup_bpf_attach()
666 atype = bpf_cgroup_atype_find(type, new_prog->aux->attach_btf_id); in __cgroup_bpf_attach()
668 return -EINVAL; in __cgroup_bpf_attach()
670 progs = &cgrp->bpf.progs[atype]; in __cgroup_bpf_attach()
673 return -EPERM; in __cgroup_bpf_attach()
675 if (!hlist_empty(progs) && cgrp->bpf.flags[atype] != saved_flags) in __cgroup_bpf_attach()
676 /* Disallow attaching non-overridable on top in __cgroup_bpf_attach()
678 * Disallow attaching multi-prog if overridable or none in __cgroup_bpf_attach()
680 return -EPERM; in __cgroup_bpf_attach()
683 return -E2BIG; in __cgroup_bpf_attach()
691 prog ? : link->link.prog, cgrp)) in __cgroup_bpf_attach()
692 return -ENOMEM; in __cgroup_bpf_attach()
695 old_prog = pl->prog; in __cgroup_bpf_attach()
702 return -ENOMEM; in __cgroup_bpf_attach()
705 hlist_add_head(&pl->node, progs); in __cgroup_bpf_attach()
708 if (last->next) in __cgroup_bpf_attach()
710 hlist_add_behind(&pl->node, last); in __cgroup_bpf_attach()
715 pl->prog = prog; in __cgroup_bpf_attach()
716 pl->link = link; in __cgroup_bpf_attach()
717 pl->flags = flags; in __cgroup_bpf_attach()
718 bpf_cgroup_storages_assign(pl->storage, storage); in __cgroup_bpf_attach()
719 cgrp->bpf.flags[atype] = saved_flags; in __cgroup_bpf_attach()
747 pl->prog = old_prog; in __cgroup_bpf_attach()
748 pl->link = NULL; in __cgroup_bpf_attach()
752 hlist_del(&pl->node); in __cgroup_bpf_attach()
772 /* Swap updated BPF program for given link in effective program arrays across
787 css_for_each_descendant_pre(css, &cgrp->self) { in replace_effective_prog()
790 if (percpu_ref_is_zero(&desc->bpf.refcnt)) in replace_effective_prog()
793 /* find position of link in effective progs array */ in replace_effective_prog()
795 if (pos && !(cg->bpf.flags[atype] & BPF_F_ALLOW_MULTI)) in replace_effective_prog()
798 head = &cg->bpf.progs[atype]; in replace_effective_prog()
802 if (pl->link == link) in replace_effective_prog()
810 desc->bpf.effective[atype], in replace_effective_prog()
812 item = &progs->items[pos]; in replace_effective_prog()
813 WRITE_ONCE(item->prog, link->link.prog); in replace_effective_prog()
818 * __cgroup_bpf_replace() - Replace link's program and propagate the change
837 atype = bpf_cgroup_atype_find(link->type, new_prog->aux->attach_btf_id); in __cgroup_bpf_replace()
839 return -EINVAL; in __cgroup_bpf_replace()
841 progs = &cgrp->bpf.progs[atype]; in __cgroup_bpf_replace()
843 if (link->link.prog->type != new_prog->type) in __cgroup_bpf_replace()
844 return -EINVAL; in __cgroup_bpf_replace()
847 if (pl->link == link) { in __cgroup_bpf_replace()
853 return -ENOENT; in __cgroup_bpf_replace()
855 old_prog = xchg(&link->link.prog, new_prog); in __cgroup_bpf_replace()
870 /* link might have been auto-released by dying cgroup, so fail */ in cgroup_bpf_replace()
871 if (!cg_link->cgroup) { in cgroup_bpf_replace()
872 ret = -ENOLINK; in cgroup_bpf_replace()
875 if (old_prog && link->prog != old_prog) { in cgroup_bpf_replace()
876 ret = -EPERM; in cgroup_bpf_replace()
879 ret = __cgroup_bpf_replace(cg_link->cgroup, cg_link, new_prog); in cgroup_bpf_replace()
895 return ERR_PTR(-ENOENT); in find_detach_entry()
900 return hlist_entry(progs->first, typeof(*pl), node); in find_detach_entry()
907 return ERR_PTR(-EINVAL); in find_detach_entry()
911 if (pl->prog == prog && pl->link == link) in find_detach_entry()
914 return ERR_PTR(-ENOENT); in find_detach_entry()
918 * purge_effective_progs() - After compute_effective_progs fails to alloc new
919 * cgrp->bpf.inactive table we can recover by
938 /* recompute effective prog array in place */ in purge_effective_progs()
939 css_for_each_descendant_pre(css, &cgrp->self) { in purge_effective_progs()
942 if (percpu_ref_is_zero(&desc->bpf.refcnt)) in purge_effective_progs()
945 /* find position of link or prog in effective progs array */ in purge_effective_progs()
947 if (pos && !(cg->bpf.flags[atype] & BPF_F_ALLOW_MULTI)) in purge_effective_progs()
950 head = &cg->bpf.progs[atype]; in purge_effective_progs()
954 if (pl->prog == prog && pl->link == link) in purge_effective_progs()
964 desc->bpf.effective[atype], in purge_effective_progs()
974 * __cgroup_bpf_detach() - Detach the program or link from a cgroup, and
981 * At most one of @prog or @link can be non-NULL.
995 attach_btf_id = prog->aux->attach_btf_id; in __cgroup_bpf_detach()
997 attach_btf_id = link->link.prog->aux->attach_btf_id; in __cgroup_bpf_detach()
1001 return -EINVAL; in __cgroup_bpf_detach()
1003 progs = &cgrp->bpf.progs[atype]; in __cgroup_bpf_detach()
1004 flags = cgrp->bpf.flags[atype]; in __cgroup_bpf_detach()
1008 return -EINVAL; in __cgroup_bpf_detach()
1014 /* mark it deleted, so it's ignored while recomputing effective */ in __cgroup_bpf_detach()
1015 old_prog = pl->prog; in __cgroup_bpf_detach()
1016 pl->prog = NULL; in __cgroup_bpf_detach()
1017 pl->link = NULL; in __cgroup_bpf_detach()
1020 /* if update effective array failed replace the prog with a dummy prog*/ in __cgroup_bpf_detach()
1021 pl->prog = old_prog; in __cgroup_bpf_detach()
1022 pl->link = link; in __cgroup_bpf_detach()
1027 hlist_del(&pl->node); in __cgroup_bpf_detach()
1032 cgrp->bpf.flags[atype] = 0; in __cgroup_bpf_detach()
1057 __u32 __user *prog_attach_flags = u64_to_user_ptr(attr->query.prog_attach_flags); in __cgroup_bpf_query()
1058 bool effective_query = attr->query.query_flags & BPF_F_QUERY_EFFECTIVE; in __cgroup_bpf_query()
1059 __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids); in __cgroup_bpf_query()
1060 enum bpf_attach_type type = attr->query.attach_type; in __cgroup_bpf_query()
1063 struct bpf_prog_array *effective; in __cgroup_bpf_query() local
1069 return -EINVAL; in __cgroup_bpf_query()
1072 if (!effective_query && attr->query.prog_cnt && in __cgroup_bpf_query()
1074 return -EINVAL; in __cgroup_bpf_query()
1082 return -EINVAL; in __cgroup_bpf_query()
1084 flags = cgrp->bpf.flags[from_atype]; in __cgroup_bpf_query()
1089 effective = rcu_dereference_protected(cgrp->bpf.effective[atype], in __cgroup_bpf_query()
1091 total_cnt += bpf_prog_array_length(effective); in __cgroup_bpf_query()
1093 total_cnt += prog_list_length(&cgrp->bpf.progs[atype], NULL); in __cgroup_bpf_query()
1097 /* always output uattr->query.attach_flags as 0 during effective query */ in __cgroup_bpf_query()
1099 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags))) in __cgroup_bpf_query()
1100 return -EFAULT; in __cgroup_bpf_query()
1101 if (copy_to_user(&uattr->query.prog_cnt, &total_cnt, sizeof(total_cnt))) in __cgroup_bpf_query()
1102 return -EFAULT; in __cgroup_bpf_query()
1103 if (attr->query.prog_cnt == 0 || !prog_ids || !total_cnt) in __cgroup_bpf_query()
1107 if (attr->query.prog_cnt < total_cnt) { in __cgroup_bpf_query()
1108 total_cnt = attr->query.prog_cnt; in __cgroup_bpf_query()
1109 ret = -ENOSPC; in __cgroup_bpf_query()
1114 effective = rcu_dereference_protected(cgrp->bpf.effective[atype], in __cgroup_bpf_query()
1116 cnt = min_t(int, bpf_prog_array_length(effective), total_cnt); in __cgroup_bpf_query()
1117 ret = bpf_prog_array_copy_to_user(effective, prog_ids, cnt); in __cgroup_bpf_query()
1124 progs = &cgrp->bpf.progs[atype]; in __cgroup_bpf_query()
1129 id = prog->aux->id; in __cgroup_bpf_query()
1131 return -EFAULT; in __cgroup_bpf_query()
1137 flags = cgrp->bpf.flags[atype]; in __cgroup_bpf_query()
1142 return -EFAULT; in __cgroup_bpf_query()
1148 total_cnt -= cnt; in __cgroup_bpf_query()
1171 cgrp = cgroup_get_from_fd(attr->target_fd); in cgroup_bpf_prog_attach()
1175 if ((attr->attach_flags & BPF_F_ALLOW_MULTI) && in cgroup_bpf_prog_attach()
1176 (attr->attach_flags & BPF_F_REPLACE)) { in cgroup_bpf_prog_attach()
1177 replace_prog = bpf_prog_get_type(attr->replace_bpf_fd, ptype); in cgroup_bpf_prog_attach()
1185 attr->attach_type, attr->attach_flags); in cgroup_bpf_prog_attach()
1199 cgrp = cgroup_get_from_fd(attr->target_fd); in cgroup_bpf_prog_detach()
1203 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); in cgroup_bpf_prog_detach()
1207 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type); in cgroup_bpf_prog_detach()
1221 /* link might have been auto-detached by dying cgroup already, in bpf_cgroup_link_release()
1224 if (!cg_link->cgroup) in bpf_cgroup_link_release()
1229 /* re-check cgroup under lock again */ in bpf_cgroup_link_release()
1230 if (!cg_link->cgroup) { in bpf_cgroup_link_release()
1235 WARN_ON(__cgroup_bpf_detach(cg_link->cgroup, NULL, cg_link, in bpf_cgroup_link_release()
1236 cg_link->type)); in bpf_cgroup_link_release()
1237 if (cg_link->type == BPF_LSM_CGROUP) in bpf_cgroup_link_release()
1238 bpf_trampoline_unlink_cgroup_shim(cg_link->link.prog); in bpf_cgroup_link_release()
1240 cg = cg_link->cgroup; in bpf_cgroup_link_release()
1241 cg_link->cgroup = NULL; in bpf_cgroup_link_release()
1271 if (cg_link->cgroup) in bpf_cgroup_link_show_fdinfo()
1272 cg_id = cgroup_id(cg_link->cgroup); in bpf_cgroup_link_show_fdinfo()
1279 cg_link->type); in bpf_cgroup_link_show_fdinfo()
1290 if (cg_link->cgroup) in bpf_cgroup_link_fill_link_info()
1291 cg_id = cgroup_id(cg_link->cgroup); in bpf_cgroup_link_fill_link_info()
1294 info->cgroup.cgroup_id = cg_id; in bpf_cgroup_link_fill_link_info()
1295 info->cgroup.attach_type = cg_link->type; in bpf_cgroup_link_fill_link_info()
1315 if (attr->link_create.flags) in cgroup_bpf_link_attach()
1316 return -EINVAL; in cgroup_bpf_link_attach()
1318 cgrp = cgroup_get_from_fd(attr->link_create.target_fd); in cgroup_bpf_link_attach()
1324 err = -ENOMEM; in cgroup_bpf_link_attach()
1327 bpf_link_init(&link->link, BPF_LINK_TYPE_CGROUP, &bpf_cgroup_link_lops, in cgroup_bpf_link_attach()
1329 link->cgroup = cgrp; in cgroup_bpf_link_attach()
1330 link->type = attr->link_create.attach_type; in cgroup_bpf_link_attach()
1332 err = bpf_link_prime(&link->link, &link_primer); in cgroup_bpf_link_attach()
1339 link->type, BPF_F_ALLOW_MULTI); in cgroup_bpf_link_attach()
1358 cgrp = cgroup_get_from_fd(attr->query.target_fd); in cgroup_bpf_prog_query()
1369 * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
1381 * NET_XMIT_SUCCESS (0) - continue with packet output
1382 * NET_XMIT_DROP (1) - drop packet and notify TCP to call cwr
1383 * NET_XMIT_CN (2) - continue with packet output and notify TCP
1385 * -err - drop packet
1387 * For ingress packets, this function will return -EPERM if any
1395 unsigned int offset = skb->data - skb_network_header(skb); in __cgroup_bpf_run_filter_skb()
1404 if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6) in __cgroup_bpf_run_filter_skb()
1407 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_filter_skb()
1408 save_sk = skb->sk; in __cgroup_bpf_run_filter_skb()
1409 skb->sk = sk; in __cgroup_bpf_run_filter_skb()
1419 ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, skb, in __cgroup_bpf_run_filter_skb()
1434 * 3: -err skb should be dropped in __cgroup_bpf_run_filter_skb()
1439 ret = -EFAULT; in __cgroup_bpf_run_filter_skb()
1445 ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, in __cgroup_bpf_run_filter_skb()
1449 ret = -EFAULT; in __cgroup_bpf_run_filter_skb()
1453 skb->sk = save_sk; in __cgroup_bpf_run_filter_skb()
1460 * __cgroup_bpf_run_filter_sk() - Run a program on a sock
1469 * This function will return %-EPERM if any if an attached program was found
1475 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_filter_sk()
1477 return bpf_prog_run_array_cg(&cgrp->bpf, atype, sk, bpf_prog_run, 0, in __cgroup_bpf_run_filter_sk()
1483 * __cgroup_bpf_run_filter_sock_addr() - Run a program on a sock and
1488 * read-only for AF_INET[6] uaddr but can be modified for AF_UNIX
1497 * This function will return %-EPERM if an attached program is found and
1519 if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6) in __cgroup_bpf_run_filter_sock_addr()
1530 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_filter_sock_addr()
1531 ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run, in __cgroup_bpf_run_filter_sock_addr()
1542 * __cgroup_bpf_run_filter_sock_ops() - Run a program on a sock
1554 * This function will return %-EPERM if any if an attached program was found
1561 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_filter_sock_ops()
1563 return bpf_prog_run_array_cg(&cgrp->bpf, atype, sock_ops, bpf_prog_run, in __cgroup_bpf_run_filter_sock_ops()
1581 ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run, 0, in __cgroup_bpf_check_dev_permission()
1600 ctx = container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx); in BPF_CALL_2()
1601 storage = ctx->prog_item->cgroup_storage[stype]; in BPF_CALL_2()
1604 ptr = &READ_ONCE(storage->buf)->data[0]; in BPF_CALL_2()
1606 ptr = this_cpu_ptr(storage->percpu_buf); in BPF_CALL_2()
1622 container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx); in BPF_CALL_0()
1624 return ctx->retval; in BPF_CALL_0()
1636 container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx); in BPF_CALL_1()
1638 ctx->retval = retval; in BPF_CALL_1()
1709 * __cgroup_bpf_run_filter_sysctl - Run a program on sysctl
1715 * @pcount: value-result argument: value is size of buffer pointed to by @buf,
1718 * @ppos: value-result argument: value is position at which read from or write
1726 * This function will return %-EPERM if an attached program is found and
1751 table->proc_handler(table, 0, ctx.cur_val, &ctx.cur_len, &pos)) { in __cgroup_bpf_run_filter_sysctl()
1772 ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run, 0, in __cgroup_bpf_run_filter_sysctl()
1794 return -EINVAL; in sockopt_alloc_buf()
1803 if (max_optlen <= sizeof(buf->data)) { in sockopt_alloc_buf()
1805 * bytes avoid the cost of kzalloc. in sockopt_alloc_buf()
1807 ctx->optval = buf->data; in sockopt_alloc_buf()
1808 ctx->optval_end = ctx->optval + max_optlen; in sockopt_alloc_buf()
1812 ctx->optval = kzalloc(max_optlen, GFP_USER); in sockopt_alloc_buf()
1813 if (!ctx->optval) in sockopt_alloc_buf()
1814 return -ENOMEM; in sockopt_alloc_buf()
1816 ctx->optval_end = ctx->optval + max_optlen; in sockopt_alloc_buf()
1824 if (ctx->optval == buf->data) in sockopt_free_buf()
1826 kfree(ctx->optval); in sockopt_free_buf()
1832 return ctx->optval != buf->data; in sockopt_buf_allocated()
1839 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_filter_setsockopt()
1861 ret = -EFAULT; in __cgroup_bpf_run_filter_setsockopt()
1866 ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_SETSOCKOPT, in __cgroup_bpf_run_filter_setsockopt()
1873 if (ctx.optlen == -1) { in __cgroup_bpf_run_filter_setsockopt()
1874 /* optlen set to -1, bypass kernel */ in __cgroup_bpf_run_filter_setsockopt()
1876 } else if (ctx.optlen > max_optlen || ctx.optlen < -1) { in __cgroup_bpf_run_filter_setsockopt()
1884 ret = -EFAULT; in __cgroup_bpf_run_filter_setsockopt()
1898 /* We've used bpf_sockopt_kern->buf as an intermediary in __cgroup_bpf_run_filter_setsockopt()
1901 * No way to export on-stack buf, have to allocate a in __cgroup_bpf_run_filter_setsockopt()
1908 ret = -ENOMEM; in __cgroup_bpf_run_filter_setsockopt()
1931 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_filter_getsockopt()
1957 ret = -EFAULT; in __cgroup_bpf_run_filter_getsockopt()
1962 ret = -EFAULT; in __cgroup_bpf_run_filter_getsockopt()
1969 ret = -EFAULT; in __cgroup_bpf_run_filter_getsockopt()
1975 ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_GETSOCKOPT, in __cgroup_bpf_run_filter_getsockopt()
1990 ret = -EFAULT; in __cgroup_bpf_run_filter_getsockopt()
1997 ret = -EFAULT; in __cgroup_bpf_run_filter_getsockopt()
2001 ret = -EFAULT; in __cgroup_bpf_run_filter_getsockopt()
2015 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_filter_getsockopt_kern()
2035 ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_GETSOCKOPT, in __cgroup_bpf_run_filter_getsockopt_kern()
2041 return -EFAULT; in __cgroup_bpf_run_filter_getsockopt_kern()
2057 if (dir->header.parent) { in sysctl_cpy_dir()
2058 tmp_ret = sysctl_cpy_dir(dir->header.parent, bufp, lenp); in sysctl_cpy_dir()
2063 ret = strscpy(*bufp, dir->header.ctl_table[0].procname, *lenp); in sysctl_cpy_dir()
2067 *lenp -= ret; in sysctl_cpy_dir()
2078 *lenp -= tmp_ret; in sysctl_cpy_dir()
2089 return -EINVAL; in BPF_CALL_4()
2092 if (!ctx->head) in BPF_CALL_4()
2093 return -EINVAL; in BPF_CALL_4()
2094 tmp_ret = sysctl_cpy_dir(ctx->head->parent, &buf, &buf_len); in BPF_CALL_4()
2099 ret = strscpy(buf, ctx->table->procname, buf_len); in BPF_CALL_4()
2118 return -EINVAL; in copy_sysctl_value()
2121 return -E2BIG; in copy_sysctl_value()
2125 return -EINVAL; in copy_sysctl_value()
2131 memset(dst + src_len, '\0', dst_len - src_len); in copy_sysctl_value()
2135 dst[dst_len - 1] = '\0'; in copy_sysctl_value()
2137 return -E2BIG; in copy_sysctl_value()
2143 return copy_sysctl_value(buf, buf_len, ctx->cur_val, ctx->cur_len); in BPF_CALL_3()
2158 if (!ctx->write) { in BPF_CALL_3()
2161 return -EINVAL; in BPF_CALL_3()
2163 return copy_sysctl_value(buf, buf_len, ctx->new_val, ctx->new_len); in BPF_CALL_3()
2178 if (!ctx->write || !ctx->new_val || !ctx->new_len || !buf || !buf_len) in BPF_CALL_3()
2179 return -EINVAL; in BPF_CALL_3()
2181 if (buf_len > PAGE_SIZE - 1) in BPF_CALL_3()
2182 return -E2BIG; in BPF_CALL_3()
2184 memcpy(ctx->new_val, buf, buf_len); in BPF_CALL_3()
2185 ctx->new_len = buf_len; in BPF_CALL_3()
2186 ctx->new_updated = 1; in BPF_CALL_3()
2266 switch (si->off) { in sysctl_convert_ctx_access()
2269 BPF_SIZE(si->code), si->dst_reg, si->src_reg, in sysctl_convert_ctx_access()
2284 if (si->src_reg == treg || si->dst_reg == treg) in sysctl_convert_ctx_access()
2285 --treg; in sysctl_convert_ctx_access()
2286 if (si->src_reg == treg || si->dst_reg == treg) in sysctl_convert_ctx_access()
2287 --treg; in sysctl_convert_ctx_access()
2289 BPF_DW, si->dst_reg, treg, in sysctl_convert_ctx_access()
2293 treg, si->dst_reg, in sysctl_convert_ctx_access()
2296 BPF_CLASS(si->code) | BPF_MEM | BPF_SIZEOF(u32), in sysctl_convert_ctx_access()
2297 treg, si->src_reg, in sysctl_convert_ctx_access()
2300 si->imm); in sysctl_convert_ctx_access()
2302 BPF_DW, treg, si->dst_reg, in sysctl_convert_ctx_access()
2307 si->dst_reg, si->src_reg, in sysctl_convert_ctx_access()
2309 read_size = bpf_size_to_bytes(BPF_SIZE(si->code)); in sysctl_convert_ctx_access()
2311 BPF_SIZE(si->code), si->dst_reg, si->dst_reg, in sysctl_convert_ctx_access()
2319 return insn - insn_buf; in sysctl_convert_ctx_access()
2334 const struct net *net = ctx ? sock_net(ctx->sk) : &init_net; in BPF_CALL_1()
2336 return net->net_cookie; in BPF_CALL_1()
2369 if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT) in cg_sockopt_func_proto()
2373 if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT) in cg_sockopt_func_proto()
2406 return prog->expected_attach_type == in cg_sockopt_is_valid_access()
2413 return prog->expected_attach_type == in cg_sockopt_is_valid_access()
2426 info->reg_type = PTR_TO_SOCKET; in cg_sockopt_is_valid_access()
2431 info->reg_type = PTR_TO_PACKET; in cg_sockopt_is_valid_access()
2436 info->reg_type = PTR_TO_PACKET_END; in cg_sockopt_is_valid_access()
2441 return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT; in cg_sockopt_is_valid_access()
2452 si->dst_reg, si->src_reg, \
2457 BPF_MEM | BPF_CLASS(si->code)), \
2458 si->dst_reg, si->src_reg, \
2460 si->imm)
2470 switch (si->off) { in cg_sockopt_convert_ctx_access()
2498 if (si->src_reg == treg || si->dst_reg == treg) in cg_sockopt_convert_ctx_access()
2499 --treg; in cg_sockopt_convert_ctx_access()
2500 if (si->src_reg == treg || si->dst_reg == treg) in cg_sockopt_convert_ctx_access()
2501 --treg; in cg_sockopt_convert_ctx_access()
2502 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, treg, in cg_sockopt_convert_ctx_access()
2505 treg, si->dst_reg, in cg_sockopt_convert_ctx_access()
2510 *insn++ = BPF_RAW_INSN(BPF_CLASS(si->code) | BPF_MEM | in cg_sockopt_convert_ctx_access()
2512 treg, si->src_reg, in cg_sockopt_convert_ctx_access()
2514 si->imm); in cg_sockopt_convert_ctx_access()
2515 *insn++ = BPF_LDX_MEM(BPF_DW, treg, si->dst_reg, in cg_sockopt_convert_ctx_access()
2519 si->dst_reg, si->src_reg, in cg_sockopt_convert_ctx_access()
2522 si->dst_reg, si->dst_reg, in cg_sockopt_convert_ctx_access()
2525 si->dst_reg, si->dst_reg, in cg_sockopt_convert_ctx_access()
2537 return insn - insn_buf; in cg_sockopt_convert_ctx_access()
2567 switch (prog->expected_attach_type) { in cgroup_common_func_proto()
2582 switch (prog->expected_attach_type) { in cgroup_common_func_proto()