[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CAEf4Bzb=ByjP1VgKLZ_4JXE-t5ig+D3gxcKaqi=ZgO=iETGqig@mail.gmail.com>
Date: Mon, 8 Jul 2024 13:56:04 -0700
From: Andrii Nakryiko <andrii.nakryiko@...il.com>
To: Andreas Ziegler <ziegler.andreas@...mens.com>
Cc: Alexei Starovoitov <ast@...nel.org>, Daniel Borkmann <daniel@...earbox.net>,
Andrii Nakryiko <andrii@...nel.org>, Martin KaFai Lau <martin.lau@...ux.dev>,
Eduard Zingerman <eddyz87@...il.com>, Song Liu <song@...nel.org>,
Yonghong Song <yonghong.song@...ux.dev>, John Fastabend <john.fastabend@...il.com>,
KP Singh <kpsingh@...nel.org>, Stanislav Fomichev <sdf@...gle.com>, Hao Luo <haoluo@...gle.com>,
Jiri Olsa <jolsa@...nel.org>, bpf@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] libbpf: add NULL checks to bpf_object__{prev_map,next_map}
On Wed, Jul 3, 2024 at 1:35 AM Andreas Ziegler
<ziegler.andreas@...mens.com> wrote:
>
> In the current state, an erroneous call to
> bpf_object__find_map_by_name(NULL, ...) leads to a segmentation fault
> through the following call chain:
>
> bpf_object__find_map_by_name(obj = NULL, ...)
> -> bpf_object__for_each_map(pos, obj = NULL)
> -> bpf_object__next_map((obj = NULL), NULL)
> -> return (obj = NULL)->maps
>
> While calling bpf_object__find_map_by_name with obj = NULL is
> obviously incorrect, this should not lead to a segmentation
> fault but rather be handled gracefully.
>
> As __bpf_map__iter already handles this situation correctly,
> we can delegate the check for the regular case there and only
> add a check in case the prev or next parameter is NULL.
>
> Signed-off-by: Andreas Ziegler <ziegler.andreas@...mens.com>
> ---
> tools/lib/bpf/libbpf.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
Generally speaking libbpf's APIs don't check non-optional parameters
for NULL. We historically did check that in some APIs and didn't in
others, it wasn't consistent. But since a long while ago we decided on
not checking arguments for NULL defensively. So I don't think this
patch is necessary.
pw-bot: cr
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 4a28fac4908a..30f121754d83 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -10375,7 +10375,7 @@ __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
> struct bpf_map *
> bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *prev)
> {
> - if (prev == NULL)
> + if (prev == NULL && obj != NULL)
> return obj->maps;
>
> return __bpf_map__iter(prev, obj, 1);
> @@ -10384,7 +10384,7 @@ bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *prev)
> struct bpf_map *
> bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *next)
> {
> - if (next == NULL) {
> + if (next == NULL && obj != NULL) {
> if (!obj->nr_maps)
> return NULL;
> return obj->maps + obj->nr_maps - 1;
> --
> 2.39.2
>
Powered by blists - more mailing lists