#include "fuzz.h"
#include "network-internal.h"
#include "sd-dhcp-client.c"
+#include "tests.h"
#include "tmpfile-util.h"
int dhcp_network_bind_raw_socket(
bool so_priority_set,
int so_priority) {
- int fd;
- fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
- if (fd < 0)
- return -errno;
-
- return fd;
+ return ASSERT_OK_ERRNO(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
}
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link, const void *packet, size_t len) {
- return len;
+ return 0;
}
int dhcp_network_bind_udp_socket(int ifindex, be32_t address, uint16_t port, int ip_service_type) {
- int fd;
-
- fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
- if (fd < 0)
- return -errno;
-
- return fd;
+ return ASSERT_OK_ERRNO(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
}
int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port, const void *packet, size_t len) {
- return len;
+ return 0;
}
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
- uint8_t mac_addr[] = {'A', 'B', 'C', '1', '2', '3'};
- uint8_t bcast_addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
- _cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL;
- _cleanup_(sd_event_unrefp) sd_event *e = NULL;
- _cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease = NULL;
- _cleanup_(unlink_tempfilep) char lease_file[] = "/tmp/fuzz-dhcp-client.XXXXXX";
- _cleanup_close_ int fd = -1;
- int res, r;
+ static const uint8_t mac_addr[] = {'A', 'B', 'C', '1', '2', '3'};
+ static const uint8_t bcast_addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
- assert_se(setenv("SYSTEMD_NETWORK_TEST_MODE", "1", 1) >= 0);
+ ASSERT_OK_ERRNO(setenv("SYSTEMD_NETWORK_TEST_MODE", "1", /* overwrite= */ true));
fuzz_setup_logging();
- r = sd_dhcp_client_new(&client, false);
- assert_se(r >= 0);
- assert_se(client);
+ _cleanup_(sd_event_unrefp) sd_event *e = NULL;
+ ASSERT_OK(sd_event_new(&e));
+ ASSERT_NOT_NULL(e);
- assert_se(sd_event_new(&e) >= 0);
+ _cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL;
+ ASSERT_OK(sd_dhcp_client_new(&client, /* anonymize= */ false));
+ ASSERT_NOT_NULL(client);
- r = sd_dhcp_client_attach_event(client, e, 0);
- assert_se(r >= 0);
+ ASSERT_OK(sd_dhcp_client_attach_event(client, e, /* priority= */ 0));
- assert_se(sd_dhcp_client_set_ifindex(client, 42) >= 0);
- assert_se(sd_dhcp_client_set_mac(client, mac_addr, bcast_addr, ETH_ALEN, ARPHRD_ETHER) >= 0);
+ ASSERT_OK(sd_dhcp_client_set_ifindex(client, 42));
+ ASSERT_OK(sd_dhcp_client_set_mac(client, mac_addr, bcast_addr, ETH_ALEN, ARPHRD_ETHER));
- res = sd_dhcp_client_start(client);
- assert_se(IN_SET(res, 0, -EINPROGRESS));
+ ASSERT_OK(sd_dhcp_client_start(client));
client->xid = 2;
client->state = DHCP_STATE_SELECTING;
- if (client_handle_offer_or_rapid_ack(client, (DHCPMessage*) data, size, NULL) < 0)
- goto end;
-
- fd = mkostemp_safe(lease_file);
- assert_se(fd >= 0);
-
- r = dhcp_lease_save(client->lease, lease_file);
- assert_se(r >= 0);
+ if (client_handle_offer_or_rapid_ack(client, (DHCPMessage*) data, size, NULL) >= 0) {
+ _cleanup_(unlink_tempfilep) char lease_file[] = "/tmp/fuzz-dhcp-client.XXXXXX";
+ _unused_ _cleanup_close_ int fd = ASSERT_OK(mkostemp_safe(lease_file));
- r = dhcp_lease_load(&lease, lease_file);
- assert_se(r >= 0);
+ ASSERT_OK(dhcp_lease_save(client->lease, lease_file));
-end:
- assert_se(sd_dhcp_client_stop(client) >= 0);
+ _cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *lease = NULL;
+ ASSERT_OK(dhcp_lease_load(&lease, lease_file));
+ }
+ ASSERT_OK(sd_dhcp_client_stop(client));
return 0;
}