From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Kees Cook <keescook@chromium.org>
Cc: Andrii Nakryiko <andrii@kernel.org>,
bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
kpsingh@kernel.org, paul@paul-moore.com,
linux-security-module@vger.kernel.org
Subject: Re: [PATCH bpf-next 2/8] bpf: inline map creation logic in map_create() function
Date: Wed, 12 Apr 2023 17:22:46 -0700 [thread overview]
Message-ID: <CAEf4BzZhNJuSfOCgDWnDjMv5nYiCeZEJHyDRsU-h7-2CGZvhMg@mail.gmail.com> (raw)
In-Reply-To: <6436f01e.a70a0220.e12e2.828e@mx.google.com>
On Wed, Apr 12, 2023 at 10:53 AM Kees Cook <keescook@chromium.org> wrote:
>
> On Tue, Apr 11, 2023 at 09:32:54PM -0700, Andrii Nakryiko wrote:
> > Keep all the relevant generic sanity checks, permission checks, and
> > creation and initialization logic in one linear piece of code. Currently
> > helper function that handles memory allocation and partial
> > initialization is split apart and is about 1000 lines higher in the
> > file, hurting readability.
>
> At first glance, this seems like a step in the wrong direction: having a
> single-purpose function pulled out of a larger one seems like a good
> thing for stuff like unit testing, etc. Unless there's a reason later in
> the series for this inlining (which should be called out in the
> changelog here), I would say if it is only readability, just move the
> function down 1000 lines but leave it a separate function.
Oh, I should probably clarify this in the commit message. This
function is not that single-function, really. It performs some sanity
checking and then allocates and (partially) initializes the BPF map
itself. By "inlining" it, it makes it possible to perform these sanity
checks first, then do capabilities/security checks, and only if both
pass, allocate and initialize the map. Next patch inserts
(centralizes) all the spread out capabilities checks from per-map
custom callbacks into the same function, right before performing map
allocation and initialization, but after validation of parameters.
So yeah, I do take advantage of this in the next patch, because LSM
hook gets validated bpf_uattr. I'll call this out more clearly. It's
definitely not just moving code around for no good reason.
>
> -Kees
>
> >
> > Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> > ---
> > kernel/bpf/syscall.c | 54 ++++++++++++++++++--------------------------
> > 1 file changed, 22 insertions(+), 32 deletions(-)
> >
> > diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> > index c1d268025985..a090737f98ea 100644
> > --- a/kernel/bpf/syscall.c
> > +++ b/kernel/bpf/syscall.c
> > @@ -108,37 +108,6 @@ const struct bpf_map_ops bpf_map_offload_ops = {
> > .map_mem_usage = bpf_map_offload_map_mem_usage,
> > };
> >
> > -static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
> > -{
> > - const struct bpf_map_ops *ops;
> > - u32 type = attr->map_type;
> > - struct bpf_map *map;
> > - int err;
> > -
> > - if (type >= ARRAY_SIZE(bpf_map_types))
> > - return ERR_PTR(-EINVAL);
> > - type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
> > - ops = bpf_map_types[type];
> > - if (!ops)
> > - return ERR_PTR(-EINVAL);
> > -
> > - if (ops->map_alloc_check) {
> > - err = ops->map_alloc_check(attr);
> > - if (err)
> > - return ERR_PTR(err);
> > - }
> > - if (attr->map_ifindex)
> > - ops = &bpf_map_offload_ops;
> > - if (!ops->map_mem_usage)
> > - return ERR_PTR(-EINVAL);
> > - map = ops->map_alloc(attr);
> > - if (IS_ERR(map))
> > - return map;
> > - map->ops = ops;
> > - map->map_type = type;
> > - return map;
> > -}
> > -
> > static void bpf_map_write_active_inc(struct bpf_map *map)
> > {
> > atomic64_inc(&map->writecnt);
> > @@ -1124,7 +1093,9 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf,
> > /* called via syscall */
> > static int map_create(union bpf_attr *attr)
> > {
> > + const struct bpf_map_ops *ops;
> > int numa_node = bpf_map_attr_numa_node(attr);
> > + u32 map_type = attr->map_type;
> > struct btf_field_offs *foffs;
> > struct bpf_map *map;
> > int f_flags;
> > @@ -1167,9 +1138,28 @@ static int map_create(union bpf_attr *attr)
> > return -EINVAL;
> >
> > /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
> > - map = find_and_alloc_map(attr);
> > + map_type = attr->map_type;
> > + if (map_type >= ARRAY_SIZE(bpf_map_types))
> > + return -EINVAL;
> > + map_type = array_index_nospec(map_type, ARRAY_SIZE(bpf_map_types));
> > + ops = bpf_map_types[map_type];
> > + if (!ops)
> > + return -EINVAL;
> > +
> > + if (ops->map_alloc_check) {
> > + err = ops->map_alloc_check(attr);
> > + if (err)
> > + return err;
> > + }
> > + if (attr->map_ifindex)
> > + ops = &bpf_map_offload_ops;
> > + if (!ops->map_mem_usage)
> > + return -EINVAL;
> > + map = ops->map_alloc(attr);
> > if (IS_ERR(map))
> > return PTR_ERR(map);
> > + map->ops = ops;
> > + map->map_type = map_type;
> >
> > err = bpf_obj_name_cpy(map->name, attr->map_name,
> > sizeof(attr->map_name));
> > --
> > 2.34.1
> >
>
> --
> Kees Cook
next prev parent reply other threads:[~2023-04-13 0:23 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-12 4:32 [PATCH bpf-next 0/8] New BPF map and BTF security LSM hooks Andrii Nakryiko
2023-04-12 4:32 ` [PATCH bpf-next 1/8] bpf: move unprivileged checks into map_create() and bpf_prog_load() Andrii Nakryiko
2023-04-12 17:49 ` Kees Cook
2023-04-13 0:22 ` Andrii Nakryiko
2023-04-12 4:32 ` [PATCH bpf-next 2/8] bpf: inline map creation logic in map_create() function Andrii Nakryiko
2023-04-12 17:53 ` Kees Cook
2023-04-13 0:22 ` Andrii Nakryiko [this message]
2023-04-12 4:32 ` [PATCH bpf-next 3/8] bpf: centralize permissions checks for all BPF map types Andrii Nakryiko
2023-04-12 18:01 ` Kees Cook
2023-04-13 0:23 ` Andrii Nakryiko
2023-04-12 4:32 ` [PATCH bpf-next 4/8] bpf, lsm: implement bpf_map_create_security LSM hook Andrii Nakryiko
2023-04-12 18:20 ` Kees Cook
2023-04-13 0:23 ` Andrii Nakryiko
2023-04-12 4:32 ` [PATCH bpf-next 5/8] selftests/bpf: validate new " Andrii Nakryiko
2023-04-12 18:23 ` Kees Cook
2023-04-13 0:23 ` Andrii Nakryiko
2023-04-12 4:32 ` [PATCH bpf-next 6/8] bpf: drop unnecessary bpf_capable() check in BPF_MAP_FREEZE command Andrii Nakryiko
2023-04-12 18:24 ` Kees Cook
2023-04-13 0:17 ` Andrii Nakryiko
2023-04-12 4:32 ` [PATCH bpf-next 7/8] bpf, lsm: implement bpf_btf_load_security LSM hook Andrii Nakryiko
2023-04-12 16:52 ` Paul Moore
2023-04-13 1:43 ` Andrii Nakryiko
2023-04-13 2:47 ` Paul Moore
2023-04-12 4:33 ` [PATCH bpf-next 8/8] selftests/bpf: enhance lsm_map_create test with BTF LSM control Andrii Nakryiko
2023-04-12 16:49 ` [PATCH bpf-next 0/8] New BPF map and BTF security LSM hooks Paul Moore
2023-04-12 17:47 ` Kees Cook
2023-04-12 18:06 ` Paul Moore
2023-04-12 18:28 ` Kees Cook
2023-04-12 19:06 ` Paul Moore
2023-04-13 1:43 ` Andrii Nakryiko
2023-04-13 2:56 ` Paul Moore
2023-04-13 5:16 ` Andrii Nakryiko
2023-04-13 15:11 ` Paul Moore
2023-04-17 23:29 ` Andrii Nakryiko
2023-04-18 0:47 ` Casey Schaufler
2023-04-21 0:00 ` Andrii Nakryiko
2023-04-18 14:21 ` Paul Moore
2023-04-21 0:00 ` Andrii Nakryiko
2023-04-21 18:57 ` Kees Cook
2023-04-13 16:54 ` Casey Schaufler
2023-04-17 23:31 ` Andrii Nakryiko
2023-04-13 19:03 ` Jonathan Corbet
2023-04-17 23:28 ` Andrii Nakryiko
2023-04-13 16:27 ` Casey Schaufler
2023-04-17 23:31 ` Andrii Nakryiko
2023-04-17 23:53 ` Casey Schaufler
2023-04-18 0:28 ` Andrii Nakryiko
2023-04-18 0:52 ` Casey Schaufler
2023-04-12 18:38 ` Casey Schaufler
2023-04-14 20:23 ` Dr. Greg
2023-04-17 23:31 ` Andrii Nakryiko
2023-04-19 10:53 ` Dr. Greg
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=CAEf4BzZhNJuSfOCgDWnDjMv5nYiCeZEJHyDRsU-h7-2CGZvhMg@mail.gmail.com \
--to=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=keescook@chromium.org \
--cc=kpsingh@kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=paul@paul-moore.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).