| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <net/if_arp.h> |
| 4 |
#include <sys/socket.h> |
| 5 |
|
| 6 |
#include "sd-event.h" |
| 7 |
#include "sd-json.h" |
| 8 |
|
| 9 |
#include "dhcp-client-internal.h" /* IWYU pragma: keep */ |
| 10 |
#include "dhcp-lease-internal.h" |
| 11 |
#include "dhcp-message.h" |
| 12 |
#include "fd-util.h" |
| 13 |
#include "fuzz.h" |
| 14 |
#include "iovec-util.h" |
| 15 |
#include "iovec-wrapper.h" |
| 16 |
#include "tests.h" |
| 17 |
|
| 18 |
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 19 |
static const struct hw_addr_data hw_addr = { |
| 20 |
.length = ETH_ALEN, |
| 21 |
.ether = {{ 'A', 'B', 'C', '1', '2', '3' }}, |
| 22 |
}, bcast_addr = { |
| 23 |
.length = ETH_ALEN, |
| 24 |
.ether = {{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }}, |
| 25 |
}; |
| 26 |
|
| 27 |
ASSERT_OK_ERRNO(setenv("SYSTEMD_NETWORK_TEST_MODE", "1", /* overwrite= */ true)); |
| 28 |
|
| 29 |
fuzz_setup_logging(); |
| 30 |
|
| 31 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 32 |
ASSERT_OK(sd_event_new(&e)); |
| 33 |
ASSERT_NOT_NULL(e); |
| 34 |
|
| 35 |
_cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; |
| 36 |
ASSERT_OK(sd_dhcp_client_new(&client)); |
| 37 |
ASSERT_NOT_NULL(client); |
| 38 |
|
| 39 |
_cleanup_close_pair_ int socket_fd[2] = EBADF_PAIR; |
| 40 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, socket_fd)); |
| 41 |
client->socket_fd = TAKE_FD(socket_fd[0]); |
| 42 |
|
| 43 |
ASSERT_OK(sd_dhcp_client_attach_event(client, e, /* priority= */ 0)); |
| 44 |
|
| 45 |
ASSERT_OK(sd_dhcp_client_set_ifindex(client, 42)); |
| 46 |
ASSERT_OK(sd_dhcp_client_set_mac(client, hw_addr.bytes, bcast_addr.bytes, hw_addr.length, ARPHRD_ETHER)); |
| 47 |
|
| 48 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 49 |
client->xid = 2; |
| 50 |
client->state = DHCP_STATE_SELECTING; |
| 51 |
|
| 52 |
_cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease = NULL; |
| 53 |
if (dhcp_client_parse_message(client, &IOVEC_MAKE(data, size), &lease) >= 0) { |
| 54 |
/* Build json variant and parse it. */ |
| 55 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 56 |
ASSERT_OK(dhcp_message_build_json(lease->message, &v)); |
| 57 |
|
| 58 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *m = NULL; |
| 59 |
ASSERT_OK(dhcp_message_parse_json(v, &m)); |
| 60 |
|
| 61 |
/* Build UDP payload and parse it. Note, currently we do not use file and sname overload on |
| 62 |
* build. Hence, dhcp_message_build() may fail with -E2BIG if the input uses file and/or |
| 63 |
* sname overload. */ |
| 64 |
_cleanup_(iovw_done_free) struct iovec_wrapper iovw = {}; |
| 65 |
if (ASSERT_OK_OR(dhcp_message_build(lease->message, &iovw), -E2BIG) >= 0) { |
| 66 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 67 |
ASSERT_OK(iovw_concat(&iovw, &iov)); |
| 68 |
|
| 69 |
_cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease2 = NULL; |
| 70 |
ASSERT_OK(dhcp_client_parse_message(client, &iov, &lease2)); |
| 71 |
|
| 72 |
/* Build UDP payload again, and compare with the previous one. */ |
| 73 |
_cleanup_(iovw_done_free) struct iovec_wrapper iovw2 = {}; |
| 74 |
ASSERT_OK(dhcp_message_build(lease2->message, &iovw2)); |
| 75 |
|
| 76 |
ASSERT_TRUE(iovw_equal(&iovw, &iovw2)); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
ASSERT_OK(sd_dhcp_client_stop(client)); |
| 81 |
return 0; |
| 82 |
} |