Skip to content

Commit 8047b4a

Browse files
rupranSherryYang1
authored andcommitted
libbpf: Add NULL checks to bpf_object__{prev_map,next_map}
[ 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 <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Sasha Levin <[email protected]> (cherry picked from commit ed9cc6b6c36a8c96ba2382216806b1aef4fa538e) Signed-off-by: Sherry Yang <[email protected]>
1 parent 40a07c3 commit 8047b4a

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
@@ -4781,7 +4781,7 @@ __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
47814781
struct bpf_map *
47824782
bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj)
47834783
{
4784-
if (prev == NULL)
4784+
if (prev == NULL && obj != NULL)
47854785
return obj->maps;
47864786

47874787
return __bpf_map__iter(prev, obj, 1);
@@ -4790,7 +4790,7 @@ bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj)
47904790
struct bpf_map *
47914791
bpf_map__prev(const struct bpf_map *next, const struct bpf_object *obj)
47924792
{
4793-
if (next == NULL) {
4793+
if (next == NULL && obj != NULL) {
47944794
if (!obj->nr_maps)
47954795
return NULL;
47964796
return obj->maps + obj->nr_maps - 1;

0 commit comments

Comments
 (0)