From 70b4642277481f66b28ce2e7319e89bddafcf5e2 Mon Sep 17 00:00:00 2001 From: Lorenz Bauer Date: Sat, 5 Oct 2024 12:27:29 +0100 Subject: [PATCH] Don't abort program if next_key is NULL in ebpf_map_get_next_key() On Linux it is valid to pass NULL as next_key for queue type maps, since that map type doesn't really have a key. Return EINVAL instead of aborting the process via an assert. --- libs/api/api_internal.h | 2 +- libs/api/ebpf_api.cpp | 5 ++++- tests/unit/libbpf_test.cpp | 5 +++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/libs/api/api_internal.h b/libs/api/api_internal.h index 5d051600c9..9f5427f67f 100644 --- a/libs/api/api_internal.h +++ b/libs/api/api_internal.h @@ -415,7 +415,7 @@ ebpf_map_lookup_and_delete_element_batch( * @retval EBPF_NO_MORE_KEYS previous_key was the last key. */ _Must_inspect_result_ ebpf_result_t -ebpf_map_get_next_key(fd_t map_fd, _In_opt_ const void* previous_key, _Out_ void* next_key) noexcept; +ebpf_map_get_next_key(fd_t map_fd, _In_opt_ const void* previous_key, _Out_opt_ void* next_key) noexcept; /** * @brief Detach a link given a file descriptor. diff --git a/libs/api/ebpf_api.cpp b/libs/api/ebpf_api.cpp index d7f4260e4c..8be8edde0c 100644 --- a/libs/api/ebpf_api.cpp +++ b/libs/api/ebpf_api.cpp @@ -1142,7 +1142,10 @@ ebpf_map_get_next_key(fd_t map_fd, _In_opt_ const void* previous_key, _Out_ void uint32_t type; ebpf_handle_t map_handle = ebpf_handle_invalid; - ebpf_assert(next_key); + if (next_key == NULL) { + result = EBPF_INVALID_ARGUMENT; + goto Exit; + } map_handle = _get_handle_from_file_descriptor(map_fd); if (map_handle == ebpf_handle_invalid) { diff --git a/tests/unit/libbpf_test.cpp b/tests/unit/libbpf_test.cpp index abda2e4acb..0da27edceb 100644 --- a/tests/unit/libbpf_test.cpp +++ b/tests/unit/libbpf_test.cpp @@ -1005,6 +1005,11 @@ TEST_CASE("libbpf map", "[libbpf]") REQUIRE(result < 0); REQUIRE(errno == EINVAL); + // next_key is NULL. + result = bpf_map_get_next_key(map_fd, NULL, &value); + REQUIRE(result < 0); + REQUIRE(errno == EINVAL); + bpf_object__close(object); } #endif