Skip to content

Commit 5b06b06

Browse files
rupransmb49
authored andcommitted
libbpf: Add NULL checks to bpf_object__{prev_map,next_map}
BugLink: https://bugs.launchpad.net/bugs/2081279 [ Upstream commit cedc12c ] 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@siemens.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20240703083436.505124-1-ziegler.andreas@siemens.com Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Koichiro Den <koichiro.den@canonical.com> Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
1 parent f175ff5 commit 5b06b06

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
@@ -8729,7 +8729,7 @@ __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
87298729
struct bpf_map *
87308730
bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj)
87318731
{
8732-
if (prev == NULL)
8732+
if (prev == NULL && obj != NULL)
87338733
return obj->maps;
87348734

87358735
return __bpf_map__iter(prev, obj, 1);
@@ -8738,7 +8738,7 @@ bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj)
87388738
struct bpf_map *
87398739
bpf_map__prev(const struct bpf_map *next, const struct bpf_object *obj)
87408740
{
8741-
if (next == NULL) {
8741+
if (next == NULL && obj != NULL) {
87428742
if (!obj->nr_maps)
87438743
return NULL;
87448744
return obj->maps + obj->nr_maps - 1;

0 commit comments

Comments
 (0)