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()
385 /* if parent has non-overridable prog attached,
387 * if parent has overridable or multi-prog, allow attaching
398 u32 flags = p->bpf.flags[atype]; in hierarchy_allows_attach()
403 cnt = prog_list_length(&p->bpf.progs[atype]); in hierarchy_allows_attach()
412 /* compute a chain of effective programs for a given cgroup:
415 * Note that parent's F_ALLOW_OVERRIDE-type program is yielding
428 /* count number of effective programs by walking parents */ in compute_effective_progs()
430 if (cnt == 0 || (p->bpf.flags[atype] & BPF_F_ALLOW_MULTI)) in compute_effective_progs()
431 cnt += prog_list_length(&p->bpf.progs[atype]); in compute_effective_progs()
437 return -ENOMEM; in compute_effective_progs()
439 /* populate the array with effective progs */ in compute_effective_progs()
443 if (cnt > 0 && !(p->bpf.flags[atype] & BPF_F_ALLOW_MULTI)) in compute_effective_progs()
446 hlist_for_each_entry(pl, &p->bpf.progs[atype], node) { in compute_effective_progs()
450 item = &progs->items[cnt]; in compute_effective_progs()
451 item->prog = prog_list_prog(pl); in compute_effective_progs()
452 bpf_cgroup_storages_assign(item->cgroup_storage, in compute_effective_progs()
453 pl->storage); in compute_effective_progs()
466 old_array = rcu_replace_pointer(cgrp->bpf.effective[atype], old_array, in activate_effective_progs()
475 * cgroup_bpf_inherit() - inherit effective programs from parent
483 #define NR ARRAY_SIZE(cgrp->bpf.effective) in cgroup_bpf_inherit()
488 ret = percpu_ref_init(&cgrp->bpf.refcnt, cgroup_bpf_release_fn, 0, in cgroup_bpf_inherit()
497 INIT_HLIST_HEAD(&cgrp->bpf.progs[i]); in cgroup_bpf_inherit()
499 INIT_LIST_HEAD(&cgrp->bpf.storages); in cgroup_bpf_inherit()
516 percpu_ref_exit(&cgrp->bpf.refcnt); in cgroup_bpf_inherit()
518 return -ENOMEM; in cgroup_bpf_inherit()
527 /* allocate and recompute effective prog arrays */ in update_effective_progs()
528 css_for_each_descendant_pre(css, &cgrp->self) { in update_effective_progs()
531 if (percpu_ref_is_zero(&desc->bpf.refcnt)) in update_effective_progs()
534 err = compute_effective_progs(desc, atype, &desc->bpf.inactive); in update_effective_progs()
540 css_for_each_descendant_pre(css, &cgrp->self) { in update_effective_progs()
543 if (percpu_ref_is_zero(&desc->bpf.refcnt)) { in update_effective_progs()
544 if (unlikely(desc->bpf.inactive)) { in update_effective_progs()
545 bpf_prog_array_free(desc->bpf.inactive); in update_effective_progs()
546 desc->bpf.inactive = NULL; in update_effective_progs()
551 activate_effective_progs(desc, atype, desc->bpf.inactive); in update_effective_progs()
552 desc->bpf.inactive = NULL; in update_effective_progs()
558 /* oom while computing effective. Free all computed effective arrays in update_effective_progs()
561 css_for_each_descendant_pre(css, &cgrp->self) { in update_effective_progs()
564 bpf_prog_array_free(desc->bpf.inactive); in update_effective_progs()
565 desc->bpf.inactive = NULL; in update_effective_progs()
581 /* single-attach case */ in find_attach_entry()
585 return hlist_entry(progs->first, typeof(*pl), node); in find_attach_entry()
589 if (prog && pl->prog == prog && prog != replace_prog) in find_attach_entry()
591 return ERR_PTR(-EINVAL); in find_attach_entry()
592 if (link && pl->link == link) in find_attach_entry()
594 return ERR_PTR(-EINVAL); in find_attach_entry()
597 /* direct prog multi-attach w/ replacement case */ in find_attach_entry()
600 if (pl->prog == replace_prog) in find_attach_entry()
605 return ERR_PTR(-ENOENT); in find_attach_entry()
612 * __cgroup_bpf_attach() - Attach the program or the link to a cgroup, and
621 * Exactly one of @prog or @link can be non-null.
633 struct bpf_prog *new_prog = prog ? : link->link.prog; in __cgroup_bpf_attach()
642 return -EINVAL; in __cgroup_bpf_attach()
645 return -EINVAL; in __cgroup_bpf_attach()
648 return -EINVAL; in __cgroup_bpf_attach()
650 atype = bpf_cgroup_atype_find(type, new_prog->aux->attach_btf_id); in __cgroup_bpf_attach()
652 return -EINVAL; in __cgroup_bpf_attach()
654 progs = &cgrp->bpf.progs[atype]; in __cgroup_bpf_attach()
657 return -EPERM; in __cgroup_bpf_attach()
659 if (!hlist_empty(progs) && cgrp->bpf.flags[atype] != saved_flags) in __cgroup_bpf_attach()
660 /* Disallow attaching non-overridable on top in __cgroup_bpf_attach()
662 * Disallow attaching multi-prog if overridable or none in __cgroup_bpf_attach()
664 return -EPERM; in __cgroup_bpf_attach()
667 return -E2BIG; in __cgroup_bpf_attach()
675 prog ? : link->link.prog, cgrp)) in __cgroup_bpf_attach()
676 return -ENOMEM; in __cgroup_bpf_attach()
679 old_prog = pl->prog; in __cgroup_bpf_attach()
686 return -ENOMEM; in __cgroup_bpf_attach()
689 hlist_add_head(&pl->node, progs); in __cgroup_bpf_attach()
692 if (last->next) in __cgroup_bpf_attach()
694 hlist_add_behind(&pl->node, last); in __cgroup_bpf_attach()
699 pl->prog = prog; in __cgroup_bpf_attach()
700 pl->link = link; in __cgroup_bpf_attach()
701 bpf_cgroup_storages_assign(pl->storage, storage); in __cgroup_bpf_attach()
702 cgrp->bpf.flags[atype] = saved_flags; in __cgroup_bpf_attach()
730 pl->prog = old_prog; in __cgroup_bpf_attach()
731 pl->link = NULL; in __cgroup_bpf_attach()
735 hlist_del(&pl->node); in __cgroup_bpf_attach()
755 /* Swap updated BPF program for given link in effective program arrays across
770 css_for_each_descendant_pre(css, &cgrp->self) { in replace_effective_prog()
773 if (percpu_ref_is_zero(&desc->bpf.refcnt)) in replace_effective_prog()
776 /* find position of link in effective progs array */ in replace_effective_prog()
778 if (pos && !(cg->bpf.flags[atype] & BPF_F_ALLOW_MULTI)) in replace_effective_prog()
781 head = &cg->bpf.progs[atype]; in replace_effective_prog()
785 if (pl->link == link) in replace_effective_prog()
793 desc->bpf.effective[atype], in replace_effective_prog()
795 item = &progs->items[pos]; in replace_effective_prog()
796 WRITE_ONCE(item->prog, link->link.prog); in replace_effective_prog()
801 * __cgroup_bpf_replace() - Replace link's program and propagate the change
820 atype = bpf_cgroup_atype_find(link->type, new_prog->aux->attach_btf_id); in __cgroup_bpf_replace()
822 return -EINVAL; in __cgroup_bpf_replace()
824 progs = &cgrp->bpf.progs[atype]; in __cgroup_bpf_replace()
826 if (link->link.prog->type != new_prog->type) in __cgroup_bpf_replace()
827 return -EINVAL; in __cgroup_bpf_replace()
830 if (pl->link == link) { in __cgroup_bpf_replace()
836 return -ENOENT; in __cgroup_bpf_replace()
838 old_prog = xchg(&link->link.prog, new_prog); in __cgroup_bpf_replace()
853 /* link might have been auto-released by dying cgroup, so fail */ in cgroup_bpf_replace()
854 if (!cg_link->cgroup) { in cgroup_bpf_replace()
855 ret = -ENOLINK; in cgroup_bpf_replace()
858 if (old_prog && link->prog != old_prog) { in cgroup_bpf_replace()
859 ret = -EPERM; in cgroup_bpf_replace()
862 ret = __cgroup_bpf_replace(cg_link->cgroup, cg_link, new_prog); in cgroup_bpf_replace()
878 return ERR_PTR(-ENOENT); in find_detach_entry()
883 return hlist_entry(progs->first, typeof(*pl), node); in find_detach_entry()
890 return ERR_PTR(-EINVAL); in find_detach_entry()
894 if (pl->prog == prog && pl->link == link) in find_detach_entry()
897 return ERR_PTR(-ENOENT); in find_detach_entry()
901 * purge_effective_progs() - After compute_effective_progs fails to alloc new
902 * cgrp->bpf.inactive table we can recover by
921 /* recompute effective prog array in place */ in purge_effective_progs()
922 css_for_each_descendant_pre(css, &cgrp->self) { in purge_effective_progs()
925 if (percpu_ref_is_zero(&desc->bpf.refcnt)) in purge_effective_progs()
928 /* find position of link or prog in effective progs array */ in purge_effective_progs()
930 if (pos && !(cg->bpf.flags[atype] & BPF_F_ALLOW_MULTI)) in purge_effective_progs()
933 head = &cg->bpf.progs[atype]; in purge_effective_progs()
937 if (pl->prog == prog && pl->link == link) in purge_effective_progs()
947 desc->bpf.effective[atype], in purge_effective_progs()
957 * __cgroup_bpf_detach() - Detach the program or link from a cgroup, and
964 * At most one of @prog or @link can be non-NULL.
978 attach_btf_id = prog->aux->attach_btf_id; in __cgroup_bpf_detach()
980 attach_btf_id = link->link.prog->aux->attach_btf_id; in __cgroup_bpf_detach()
984 return -EINVAL; in __cgroup_bpf_detach()
986 progs = &cgrp->bpf.progs[atype]; in __cgroup_bpf_detach()
987 flags = cgrp->bpf.flags[atype]; in __cgroup_bpf_detach()
991 return -EINVAL; in __cgroup_bpf_detach()
997 /* mark it deleted, so it's ignored while recomputing effective */ in __cgroup_bpf_detach()
998 old_prog = pl->prog; in __cgroup_bpf_detach()
999 pl->prog = NULL; in __cgroup_bpf_detach()
1000 pl->link = NULL; in __cgroup_bpf_detach()
1003 /* if update effective array failed replace the prog with a dummy prog*/ in __cgroup_bpf_detach()
1004 pl->prog = old_prog; in __cgroup_bpf_detach()
1005 pl->link = link; in __cgroup_bpf_detach()
1010 hlist_del(&pl->node); in __cgroup_bpf_detach()
1015 cgrp->bpf.flags[atype] = 0; in __cgroup_bpf_detach()
1040 __u32 __user *prog_attach_flags = u64_to_user_ptr(attr->query.prog_attach_flags); in __cgroup_bpf_query()
1041 bool effective_query = attr->query.query_flags & BPF_F_QUERY_EFFECTIVE; in __cgroup_bpf_query()
1042 __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids); in __cgroup_bpf_query()
1043 enum bpf_attach_type type = attr->query.attach_type; in __cgroup_bpf_query()
1046 struct bpf_prog_array *effective; in __cgroup_bpf_query() local
1052 return -EINVAL; in __cgroup_bpf_query()
1055 if (!effective_query && attr->query.prog_cnt && in __cgroup_bpf_query()
1057 return -EINVAL; in __cgroup_bpf_query()
1065 return -EINVAL; in __cgroup_bpf_query()
1067 flags = cgrp->bpf.flags[from_atype]; in __cgroup_bpf_query()
1072 effective = rcu_dereference_protected(cgrp->bpf.effective[atype], in __cgroup_bpf_query()
1074 total_cnt += bpf_prog_array_length(effective); in __cgroup_bpf_query()
1076 total_cnt += prog_list_length(&cgrp->bpf.progs[atype]); in __cgroup_bpf_query()
1080 /* always output uattr->query.attach_flags as 0 during effective query */ in __cgroup_bpf_query()
1082 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags))) in __cgroup_bpf_query()
1083 return -EFAULT; in __cgroup_bpf_query()
1084 if (copy_to_user(&uattr->query.prog_cnt, &total_cnt, sizeof(total_cnt))) in __cgroup_bpf_query()
1085 return -EFAULT; in __cgroup_bpf_query()
1086 if (attr->query.prog_cnt == 0 || !prog_ids || !total_cnt) in __cgroup_bpf_query()
1090 if (attr->query.prog_cnt < total_cnt) { in __cgroup_bpf_query()
1091 total_cnt = attr->query.prog_cnt; in __cgroup_bpf_query()
1092 ret = -ENOSPC; in __cgroup_bpf_query()
1097 effective = rcu_dereference_protected(cgrp->bpf.effective[atype], in __cgroup_bpf_query()
1099 cnt = min_t(int, bpf_prog_array_length(effective), total_cnt); in __cgroup_bpf_query()
1100 ret = bpf_prog_array_copy_to_user(effective, prog_ids, cnt); in __cgroup_bpf_query()
1107 progs = &cgrp->bpf.progs[atype]; in __cgroup_bpf_query()
1112 id = prog->aux->id; in __cgroup_bpf_query()
1114 return -EFAULT; in __cgroup_bpf_query()
1120 flags = cgrp->bpf.flags[atype]; in __cgroup_bpf_query()
1125 return -EFAULT; in __cgroup_bpf_query()
1131 total_cnt -= cnt; in __cgroup_bpf_query()
1154 cgrp = cgroup_get_from_fd(attr->target_fd); in cgroup_bpf_prog_attach()
1158 if ((attr->attach_flags & BPF_F_ALLOW_MULTI) && in cgroup_bpf_prog_attach()
1159 (attr->attach_flags & BPF_F_REPLACE)) { in cgroup_bpf_prog_attach()
1160 replace_prog = bpf_prog_get_type(attr->replace_bpf_fd, ptype); in cgroup_bpf_prog_attach()
1168 attr->attach_type, attr->attach_flags); in cgroup_bpf_prog_attach()
1182 cgrp = cgroup_get_from_fd(attr->target_fd); in cgroup_bpf_prog_detach()
1186 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); in cgroup_bpf_prog_detach()
1190 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type); in cgroup_bpf_prog_detach()
1204 /* link might have been auto-detached by dying cgroup already, in bpf_cgroup_link_release()
1207 if (!cg_link->cgroup) in bpf_cgroup_link_release()
1212 /* re-check cgroup under lock again */ in bpf_cgroup_link_release()
1213 if (!cg_link->cgroup) { in bpf_cgroup_link_release()
1218 WARN_ON(__cgroup_bpf_detach(cg_link->cgroup, NULL, cg_link, in bpf_cgroup_link_release()
1219 cg_link->type)); in bpf_cgroup_link_release()
1220 if (cg_link->type == BPF_LSM_CGROUP) in bpf_cgroup_link_release()
1221 bpf_trampoline_unlink_cgroup_shim(cg_link->link.prog); in bpf_cgroup_link_release()
1223 cg = cg_link->cgroup; in bpf_cgroup_link_release()
1224 cg_link->cgroup = NULL; in bpf_cgroup_link_release()
1254 if (cg_link->cgroup) in bpf_cgroup_link_show_fdinfo()
1255 cg_id = cgroup_id(cg_link->cgroup); in bpf_cgroup_link_show_fdinfo()
1262 cg_link->type); in bpf_cgroup_link_show_fdinfo()
1273 if (cg_link->cgroup) in bpf_cgroup_link_fill_link_info()
1274 cg_id = cgroup_id(cg_link->cgroup); in bpf_cgroup_link_fill_link_info()
1277 info->cgroup.cgroup_id = cg_id; in bpf_cgroup_link_fill_link_info()
1278 info->cgroup.attach_type = cg_link->type; in bpf_cgroup_link_fill_link_info()
1298 if (attr->link_create.flags) in cgroup_bpf_link_attach()
1299 return -EINVAL; in cgroup_bpf_link_attach()
1301 cgrp = cgroup_get_from_fd(attr->link_create.target_fd); in cgroup_bpf_link_attach()
1307 err = -ENOMEM; in cgroup_bpf_link_attach()
1310 bpf_link_init(&link->link, BPF_LINK_TYPE_CGROUP, &bpf_cgroup_link_lops, in cgroup_bpf_link_attach()
1312 link->cgroup = cgrp; in cgroup_bpf_link_attach()
1313 link->type = attr->link_create.attach_type; in cgroup_bpf_link_attach()
1315 err = bpf_link_prime(&link->link, &link_primer); in cgroup_bpf_link_attach()
1322 link->type, BPF_F_ALLOW_MULTI); in cgroup_bpf_link_attach()
1341 cgrp = cgroup_get_from_fd(attr->query.target_fd); in cgroup_bpf_prog_query()
1352 * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
1364 * NET_XMIT_SUCCESS (0) - continue with packet output
1365 * NET_XMIT_DROP (1) - drop packet and notify TCP to call cwr
1366 * NET_XMIT_CN (2) - continue with packet output and notify TCP
1368 * -err - drop packet
1370 * For ingress packets, this function will return -EPERM if any
1378 unsigned int offset = -skb_network_offset(skb); in __cgroup_bpf_run_filter_skb()
1384 if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6) in __cgroup_bpf_run_filter_skb()
1387 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_filter_skb()
1388 save_sk = skb->sk; in __cgroup_bpf_run_filter_skb()
1389 skb->sk = sk; in __cgroup_bpf_run_filter_skb()
1399 ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, skb, in __cgroup_bpf_run_filter_skb()
1414 * 3: -err skb should be dropped in __cgroup_bpf_run_filter_skb()
1419 ret = -EFAULT; in __cgroup_bpf_run_filter_skb()
1425 ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, in __cgroup_bpf_run_filter_skb()
1429 ret = -EFAULT; in __cgroup_bpf_run_filter_skb()
1433 skb->sk = save_sk; in __cgroup_bpf_run_filter_skb()
1440 * __cgroup_bpf_run_filter_sk() - Run a program on a sock
1449 * This function will return %-EPERM if any if an attached program was found
1455 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_filter_sk()
1457 return bpf_prog_run_array_cg(&cgrp->bpf, atype, sk, bpf_prog_run, 0, in __cgroup_bpf_run_filter_sk()
1463 * __cgroup_bpf_run_filter_sock_addr() - Run a program on a sock and
1468 * read-only for AF_INET[6] uaddr but can be modified for AF_UNIX
1477 * This function will return %-EPERM if an attached program is found and
1499 if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6 && in __cgroup_bpf_run_filter_sock_addr()
1500 sk->sk_family != AF_UNIX) in __cgroup_bpf_run_filter_sock_addr()
1511 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_filter_sock_addr()
1512 ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run, in __cgroup_bpf_run_filter_sock_addr()
1523 * __cgroup_bpf_run_filter_sock_ops() - Run a program on a sock
1535 * This function will return %-EPERM if any if an attached program was found
1542 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_filter_sock_ops()
1544 return bpf_prog_run_array_cg(&cgrp->bpf, atype, sock_ops, bpf_prog_run, in __cgroup_bpf_run_filter_sock_ops()
1562 ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run, 0, in __cgroup_bpf_check_dev_permission()
1581 ctx = container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx); in BPF_CALL_2()
1582 storage = ctx->prog_item->cgroup_storage[stype]; in BPF_CALL_2()
1585 ptr = &READ_ONCE(storage->buf)->data[0]; in BPF_CALL_2()
1587 ptr = this_cpu_ptr(storage->percpu_buf); in BPF_CALL_2()
1603 container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx); in BPF_CALL_0()
1605 return ctx->retval; in BPF_CALL_0()
1617 container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx); in BPF_CALL_1()
1619 ctx->retval = retval; in BPF_CALL_1()
1690 * __cgroup_bpf_run_filter_sysctl - Run a program on sysctl
1696 * @pcount: value-result argument: value is size of buffer pointed to by @buf,
1699 * @ppos: value-result argument: value is position at which read from or write
1707 * This function will return %-EPERM if an attached program is found and
1732 table->proc_handler(table, 0, ctx.cur_val, &ctx.cur_len, &pos)) { in __cgroup_bpf_run_filter_sysctl()
1753 ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run, 0, in __cgroup_bpf_run_filter_sysctl()
1775 return -EINVAL; in sockopt_alloc_buf()
1784 if (max_optlen <= sizeof(buf->data)) { in sockopt_alloc_buf()
1786 * bytes avoid the cost of kzalloc. in sockopt_alloc_buf()
1788 ctx->optval = buf->data; in sockopt_alloc_buf()
1789 ctx->optval_end = ctx->optval + max_optlen; in sockopt_alloc_buf()
1793 ctx->optval = kzalloc(max_optlen, GFP_USER); in sockopt_alloc_buf()
1794 if (!ctx->optval) in sockopt_alloc_buf()
1795 return -ENOMEM; in sockopt_alloc_buf()
1797 ctx->optval_end = ctx->optval + max_optlen; in sockopt_alloc_buf()
1805 if (ctx->optval == buf->data) in sockopt_free_buf()
1807 kfree(ctx->optval); in sockopt_free_buf()
1813 return ctx->optval != buf->data; in sockopt_buf_allocated()
1820 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_filter_setsockopt()
1842 ret = -EFAULT; in __cgroup_bpf_run_filter_setsockopt()
1847 ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_SETSOCKOPT, in __cgroup_bpf_run_filter_setsockopt()
1854 if (ctx.optlen == -1) { in __cgroup_bpf_run_filter_setsockopt()
1855 /* optlen set to -1, bypass kernel */ in __cgroup_bpf_run_filter_setsockopt()
1857 } else if (ctx.optlen > max_optlen || ctx.optlen < -1) { in __cgroup_bpf_run_filter_setsockopt()
1865 ret = -EFAULT; in __cgroup_bpf_run_filter_setsockopt()
1879 /* We've used bpf_sockopt_kern->buf as an intermediary in __cgroup_bpf_run_filter_setsockopt()
1882 * No way to export on-stack buf, have to allocate a in __cgroup_bpf_run_filter_setsockopt()
1889 ret = -ENOMEM; in __cgroup_bpf_run_filter_setsockopt()
1912 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_filter_getsockopt()
1938 ret = -EFAULT; in __cgroup_bpf_run_filter_getsockopt()
1943 ret = -EFAULT; in __cgroup_bpf_run_filter_getsockopt()
1950 ret = -EFAULT; in __cgroup_bpf_run_filter_getsockopt()
1956 ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_GETSOCKOPT, in __cgroup_bpf_run_filter_getsockopt()
1971 ret = -EFAULT; in __cgroup_bpf_run_filter_getsockopt()
1978 ret = -EFAULT; in __cgroup_bpf_run_filter_getsockopt()
1982 ret = -EFAULT; in __cgroup_bpf_run_filter_getsockopt()
1996 struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); in __cgroup_bpf_run_filter_getsockopt_kern()
2016 ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_GETSOCKOPT, in __cgroup_bpf_run_filter_getsockopt_kern()
2022 return -EFAULT; in __cgroup_bpf_run_filter_getsockopt_kern()
2038 if (dir->header.parent) { in sysctl_cpy_dir()
2039 tmp_ret = sysctl_cpy_dir(dir->header.parent, bufp, lenp); in sysctl_cpy_dir()
2044 ret = strscpy(*bufp, dir->header.ctl_table[0].procname, *lenp); in sysctl_cpy_dir()
2048 *lenp -= ret; in sysctl_cpy_dir()
2059 *lenp -= tmp_ret; in sysctl_cpy_dir()
2070 return -EINVAL; in BPF_CALL_4()
2073 if (!ctx->head) in BPF_CALL_4()
2074 return -EINVAL; in BPF_CALL_4()
2075 tmp_ret = sysctl_cpy_dir(ctx->head->parent, &buf, &buf_len); in BPF_CALL_4()
2080 ret = strscpy(buf, ctx->table->procname, buf_len); in BPF_CALL_4()
2099 return -EINVAL; in copy_sysctl_value()
2102 return -E2BIG; in copy_sysctl_value()
2106 return -EINVAL; in copy_sysctl_value()
2112 memset(dst + src_len, '\0', dst_len - src_len); in copy_sysctl_value()
2116 dst[dst_len - 1] = '\0'; in copy_sysctl_value()
2118 return -E2BIG; in copy_sysctl_value()
2124 return copy_sysctl_value(buf, buf_len, ctx->cur_val, ctx->cur_len); in BPF_CALL_3()
2139 if (!ctx->write) { in BPF_CALL_3()
2142 return -EINVAL; in BPF_CALL_3()
2144 return copy_sysctl_value(buf, buf_len, ctx->new_val, ctx->new_len); in BPF_CALL_3()
2159 if (!ctx->write || !ctx->new_val || !ctx->new_len || !buf || !buf_len) in BPF_CALL_3()
2160 return -EINVAL; in BPF_CALL_3()
2162 if (buf_len > PAGE_SIZE - 1) in BPF_CALL_3()
2163 return -E2BIG; in BPF_CALL_3()
2165 memcpy(ctx->new_val, buf, buf_len); in BPF_CALL_3()
2166 ctx->new_len = buf_len; in BPF_CALL_3()
2167 ctx->new_updated = 1; in BPF_CALL_3()
2247 switch (si->off) { in sysctl_convert_ctx_access()
2250 BPF_SIZE(si->code), si->dst_reg, si->src_reg, in sysctl_convert_ctx_access()
2265 if (si->src_reg == treg || si->dst_reg == treg) in sysctl_convert_ctx_access()
2266 --treg; in sysctl_convert_ctx_access()
2267 if (si->src_reg == treg || si->dst_reg == treg) in sysctl_convert_ctx_access()
2268 --treg; in sysctl_convert_ctx_access()
2270 BPF_DW, si->dst_reg, treg, in sysctl_convert_ctx_access()
2274 treg, si->dst_reg, in sysctl_convert_ctx_access()
2277 BPF_CLASS(si->code) | BPF_MEM | BPF_SIZEOF(u32), in sysctl_convert_ctx_access()
2278 treg, si->src_reg, in sysctl_convert_ctx_access()
2281 si->imm); in sysctl_convert_ctx_access()
2283 BPF_DW, treg, si->dst_reg, in sysctl_convert_ctx_access()
2288 si->dst_reg, si->src_reg, in sysctl_convert_ctx_access()
2290 read_size = bpf_size_to_bytes(BPF_SIZE(si->code)); in sysctl_convert_ctx_access()
2292 BPF_SIZE(si->code), si->dst_reg, si->dst_reg, in sysctl_convert_ctx_access()
2300 return insn - insn_buf; in sysctl_convert_ctx_access()
2315 const struct net *net = ctx ? sock_net(ctx->sk) : &init_net; in BPF_CALL_1()
2317 return net->net_cookie; in BPF_CALL_1()
2350 if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT) in cg_sockopt_func_proto()
2354 if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT) in cg_sockopt_func_proto()
2387 return prog->expected_attach_type == in cg_sockopt_is_valid_access()
2394 return prog->expected_attach_type == in cg_sockopt_is_valid_access()
2407 info->reg_type = PTR_TO_SOCKET; in cg_sockopt_is_valid_access()
2412 info->reg_type = PTR_TO_PACKET; in cg_sockopt_is_valid_access()
2417 info->reg_type = PTR_TO_PACKET_END; in cg_sockopt_is_valid_access()
2422 return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT; in cg_sockopt_is_valid_access()
2433 si->dst_reg, si->src_reg, \
2438 BPF_MEM | BPF_CLASS(si->code)), \
2439 si->dst_reg, si->src_reg, \
2441 si->imm)
2451 switch (si->off) { in cg_sockopt_convert_ctx_access()
2479 if (si->src_reg == treg || si->dst_reg == treg) in cg_sockopt_convert_ctx_access()
2480 --treg; in cg_sockopt_convert_ctx_access()
2481 if (si->src_reg == treg || si->dst_reg == treg) in cg_sockopt_convert_ctx_access()
2482 --treg; in cg_sockopt_convert_ctx_access()
2483 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, treg, in cg_sockopt_convert_ctx_access()
2486 treg, si->dst_reg, in cg_sockopt_convert_ctx_access()
2491 *insn++ = BPF_RAW_INSN(BPF_CLASS(si->code) | BPF_MEM | in cg_sockopt_convert_ctx_access()
2493 treg, si->src_reg, in cg_sockopt_convert_ctx_access()
2495 si->imm); in cg_sockopt_convert_ctx_access()
2496 *insn++ = BPF_LDX_MEM(BPF_DW, treg, si->dst_reg, in cg_sockopt_convert_ctx_access()
2500 si->dst_reg, si->src_reg, in cg_sockopt_convert_ctx_access()
2503 si->dst_reg, si->dst_reg, in cg_sockopt_convert_ctx_access()
2506 si->dst_reg, si->dst_reg, in cg_sockopt_convert_ctx_access()
2518 return insn - insn_buf; in cg_sockopt_convert_ctx_access()
2548 switch (prog->expected_attach_type) { in cgroup_common_func_proto()
2566 switch (prog->expected_attach_type) { in cgroup_common_func_proto()