Skip to content

Commit cedc12c

Browse files
rupranborkmann
authored andcommitted
libbpf: Add NULL checks to bpf_object__{prev_map,next_map}
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 <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 02480fe commit cedc12c

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10375,7 +10375,7 @@ __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
1037510375
struct bpf_map *
1037610376
bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *prev)
1037710377
{
10378-
if (prev == NULL)
10378+
if (prev == NULL && obj != NULL)
1037910379
return obj->maps;
1038010380

1038110381
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)
1038410384
struct bpf_map *
1038510385
bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *next)
1038610386
{
10387-
if (next == NULL) {
10387+
if (next == NULL && obj != NULL) {
1038810388
if (!obj->nr_maps)
1038910389
return NULL;
1039010390
return obj->maps + obj->nr_maps - 1;

0 commit comments

Comments
 (0)