| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/socket.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-event.h" |
| 7 |
#include "sd-json.h" |
| 8 |
#include "sd-lldp-rx.h" |
| 9 |
|
| 10 |
#include "fd-util.h" |
| 11 |
#include "fuzz.h" |
| 12 |
#include "lldp-network.h" |
| 13 |
#include "lldp-rx-internal.h" |
| 14 |
#include "memstream-util.h" |
| 15 |
|
| 16 |
static int test_fd[2] = EBADF_PAIR; |
| 17 |
|
| 18 |
int lldp_network_bind_raw_socket(int ifindex) { |
| 19 |
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) < 0) |
| 20 |
return -errno; |
| 21 |
|
| 22 |
return test_fd[0]; |
| 23 |
} |
| 24 |
|
| 25 |
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 26 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 27 |
_cleanup_(sd_lldp_rx_unrefp) sd_lldp_rx *lldp_rx = NULL; |
| 28 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 29 |
_cleanup_(memstream_done) MemStream m = {}; |
| 30 |
FILE *f; |
| 31 |
|
| 32 |
if (outside_size_range(size, 0, 2048)) |
| 33 |
return 0; |
| 34 |
|
| 35 |
fuzz_setup_logging(); |
| 36 |
|
| 37 |
assert_se(sd_event_new(&e) == 0); |
| 38 |
assert_se(sd_lldp_rx_new(&lldp_rx) >= 0); |
| 39 |
assert_se(sd_lldp_rx_set_ifindex(lldp_rx, 42) >= 0); |
| 40 |
assert_se(sd_lldp_rx_attach_event(lldp_rx, e, 0) >= 0); |
| 41 |
assert_se(sd_lldp_rx_start(lldp_rx) >= 0); |
| 42 |
|
| 43 |
assert_se(write(test_fd[1], data, size) == (ssize_t) size); |
| 44 |
assert_se(sd_event_run(e, 0) >= 0); |
| 45 |
|
| 46 |
assert_se(lldp_rx_build_neighbors_json(lldp_rx, &v) >= 0); |
| 47 |
assert_se(f = memstream_init(&m)); |
| 48 |
(void) sd_json_variant_dump(v, SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR, f, NULL); |
| 49 |
|
| 50 |
assert_se(sd_lldp_rx_stop(lldp_rx) >= 0); |
| 51 |
assert_se(sd_lldp_rx_detach_event(lldp_rx) >= 0); |
| 52 |
test_fd[1] = safe_close(test_fd[1]); |
| 53 |
|
| 54 |
return 0; |
| 55 |
} |