From: Martin KaFai Lau <kafai@fb.com>
To: Stanislav Fomichev <sdf@google.com>
Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, ast@kernel.org,
daniel@iogearbox.net, andrii@kernel.org
Subject: Re: [PATCH bpf-next v9 05/10] bpf: implement BPF_PROG_QUERY for BPF_LSM_CGROUP
Date: Thu, 16 Jun 2022 17:58:29 -0700 [thread overview]
Message-ID: <20220617005829.66pboow5uubbrdcu@kafai-mbp> (raw)
In-Reply-To: <20220610165803.2860154-6-sdf@google.com>
On Fri, Jun 10, 2022 at 09:57:58AM -0700, Stanislav Fomichev wrote:
> diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
> index ba402d50e130..c869317479ec 100644
> --- a/kernel/bpf/cgroup.c
> +++ b/kernel/bpf/cgroup.c
> @@ -1029,57 +1029,92 @@ static int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
> static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
> union bpf_attr __user *uattr)
> {
> + __u32 __user *prog_attach_flags = u64_to_user_ptr(attr->query.prog_attach_flags);
> __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
> enum bpf_attach_type type = attr->query.attach_type;
> + enum cgroup_bpf_attach_type from_atype, to_atype;
> enum cgroup_bpf_attach_type atype;
> struct bpf_prog_array *effective;
> struct hlist_head *progs;
> struct bpf_prog *prog;
> int cnt, ret = 0, i;
> + int total_cnt = 0;
> u32 flags;
>
> - atype = to_cgroup_bpf_attach_type(type);
> - if (atype < 0)
> - return -EINVAL;
> + if (type == BPF_LSM_CGROUP) {
> + if (attr->query.prog_cnt && prog_ids && !prog_attach_flags)
> + return -EINVAL;
>
> - progs = &cgrp->bpf.progs[atype];
> - flags = cgrp->bpf.flags[atype];
> + from_atype = CGROUP_LSM_START;
> + to_atype = CGROUP_LSM_END;
> + flags = 0;
> + } else {
> + from_atype = to_cgroup_bpf_attach_type(type);
> + if (from_atype < 0)
> + return -EINVAL;
> + to_atype = from_atype;
> + flags = cgrp->bpf.flags[from_atype];
> + }
>
> - effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
> - lockdep_is_held(&cgroup_mutex));
> + for (atype = from_atype; atype <= to_atype; atype++) {
> + progs = &cgrp->bpf.progs[atype];
nit. Move the 'progs = ...' into the 'else {}' case below.
>
> - if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE)
> - cnt = bpf_prog_array_length(effective);
> - else
> - cnt = prog_list_length(progs);
> + if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
> + effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
> + lockdep_is_held(&cgroup_mutex));
> + total_cnt += bpf_prog_array_length(effective);
> + } else {
> + total_cnt += prog_list_length(progs);
> + }
> + }
>
> if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
> return -EFAULT;
> - if (copy_to_user(&uattr->query.prog_cnt, &cnt, sizeof(cnt)))
> + if (copy_to_user(&uattr->query.prog_cnt, &total_cnt, sizeof(total_cnt)))
> return -EFAULT;
> - if (attr->query.prog_cnt == 0 || !prog_ids || !cnt)
> + if (attr->query.prog_cnt == 0 || !prog_ids || !total_cnt)
> /* return early if user requested only program count + flags */
> return 0;
> - if (attr->query.prog_cnt < cnt) {
> - cnt = attr->query.prog_cnt;
> +
> + if (attr->query.prog_cnt < total_cnt) {
> + total_cnt = attr->query.prog_cnt;
> ret = -ENOSPC;
> }
>
> - if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
> - return bpf_prog_array_copy_to_user(effective, prog_ids, cnt);
> - } else {
> - struct bpf_prog_list *pl;
> - u32 id;
> + for (atype = from_atype; atype <= to_atype && total_cnt; atype++) {
> + progs = &cgrp->bpf.progs[atype];
same here.
> + flags = cgrp->bpf.flags[atype];
and the 'flags = ...' can be moved to 'if (prog_attach_flags) {}'
Others lgtm.
Reviewed-by: Martin KaFai Lau <kafai@fb.com>
>
> - i = 0;
> - hlist_for_each_entry(pl, progs, node) {
> - prog = prog_list_prog(pl);
> - id = prog->aux->id;
> - if (copy_to_user(prog_ids + i, &id, sizeof(id)))
> - return -EFAULT;
> - if (++i == cnt)
> - break;
> + if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
> + effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
> + lockdep_is_held(&cgroup_mutex));
> + cnt = min_t(int, bpf_prog_array_length(effective), total_cnt);
> + ret = bpf_prog_array_copy_to_user(effective, prog_ids, cnt);
> + } else {
> + struct bpf_prog_list *pl;
> + u32 id;
> +
> + cnt = min_t(int, prog_list_length(progs), total_cnt);
> + i = 0;
> + hlist_for_each_entry(pl, progs, node) {
> + prog = prog_list_prog(pl);
> + id = prog->aux->id;
> + if (copy_to_user(prog_ids + i, &id, sizeof(id)))
> + return -EFAULT;
> + if (++i == cnt)
> + break;
> + }
> }
> +
> + if (prog_attach_flags) {
> + for (i = 0; i < cnt; i++)
> + if (copy_to_user(prog_attach_flags + i, &flags, sizeof(flags)))
> + return -EFAULT;
> + prog_attach_flags += cnt;
> + }
> +
> + prog_ids += cnt;
> + total_cnt -= cnt;
> }
> return ret;
> }
next prev parent reply other threads:[~2022-06-17 0:58 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-10 16:57 [PATCH bpf-next v9 00/10] bpf: cgroup_sock lsm flavor Stanislav Fomichev
2022-06-10 16:57 ` [PATCH bpf-next v9 01/10] bpf: add bpf_func_t and trampoline helpers Stanislav Fomichev
2022-06-16 19:53 ` Martin KaFai Lau
2022-06-10 16:57 ` [PATCH bpf-next v9 02/10] bpf: convert cgroup_bpf.progs to hlist Stanislav Fomichev
2022-06-16 19:59 ` Martin KaFai Lau
2022-06-10 16:57 ` [PATCH bpf-next v9 03/10] bpf: per-cgroup lsm flavor Stanislav Fomichev
2022-06-16 22:25 ` Martin KaFai Lau
2022-06-17 18:28 ` Stanislav Fomichev
2022-06-17 22:25 ` Martin KaFai Lau
2022-06-10 16:57 ` [PATCH bpf-next v9 04/10] bpf: minimize number of allocated lsm slots per program Stanislav Fomichev
2022-06-11 16:53 ` kernel test robot
2022-06-17 0:43 ` Martin KaFai Lau
2022-06-17 18:28 ` Stanislav Fomichev
2022-06-17 22:27 ` Martin KaFai Lau
2022-06-10 16:57 ` [PATCH bpf-next v9 05/10] bpf: implement BPF_PROG_QUERY for BPF_LSM_CGROUP Stanislav Fomichev
2022-06-17 0:58 ` Martin KaFai Lau [this message]
2022-06-17 18:28 ` Stanislav Fomichev
2022-06-17 22:29 ` Martin KaFai Lau
2022-06-10 16:57 ` [PATCH bpf-next v9 06/10] bpf: expose bpf_{g,s}etsockopt to lsm cgroup Stanislav Fomichev
2022-06-17 5:42 ` Martin KaFai Lau
2022-06-17 18:28 ` Stanislav Fomichev
2022-06-17 23:07 ` Martin KaFai Lau
2022-06-21 17:51 ` Stanislav Fomichev
2022-06-10 16:58 ` [PATCH bpf-next v9 07/10] libbpf: add lsm_cgoup_sock type Stanislav Fomichev
2022-06-10 16:58 ` [PATCH bpf-next v9 08/10] libbpf: implement bpf_prog_query_opts Stanislav Fomichev
2022-06-10 16:58 ` [PATCH bpf-next v9 09/10] bpftool: implement cgroup tree for BPF_LSM_CGROUP Stanislav Fomichev
2022-06-13 12:07 ` Quentin Monnet
2022-06-13 15:53 ` Stanislav Fomichev
2022-06-17 5:58 ` Martin KaFai Lau
2022-06-17 18:28 ` Stanislav Fomichev
2022-06-10 16:58 ` [PATCH bpf-next v9 10/10] selftests/bpf: lsm_cgroup functional test Stanislav Fomichev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220617005829.66pboow5uubbrdcu@kafai-mbp \
--to=kafai@fb.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=netdev@vger.kernel.org \
--cc=sdf@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).