Skip to content

Commit

Permalink
Don't abort program if next_key is NULL in ebpf_map_get_next_key()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lmb committed Oct 5, 2024
1 parent 8250e8e commit 70b4642
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libs/api/api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion libs/api/ebpf_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/libbpf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 70b4642

Please sign in to comment.