fuzz: save/load DHCP client leases

to make sure it more or less works with all sorts of leases.

Prompted by https://github.com/systemd/systemd/pull/30021#discussion_r1519123802
This commit is contained in:
Evgeny Vereshchagin
2024-03-11 09:41:44 +00:00
parent d3ca25645f
commit 5801a18ed3
+20 -1
View File
@@ -7,8 +7,12 @@
#include "sd-dhcp-client.c"
#include "alloc-util.h"
#include "dhcp-lease-internal.h"
#include "dhcp-network.h"
#include "fs-util.h"
#include "fuzz.h"
#include "network-internal.h"
#include "tmpfile-util.h"
int dhcp_network_bind_raw_socket(
int ifindex,
@@ -52,6 +56,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
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;
assert_se(setenv("SYSTEMD_NETWORK_TEST_MODE", "1", 1) >= 0);
@@ -75,8 +82,20 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
client->xid = 2;
client->state = DHCP_STATE_SELECTING;
(void) client_handle_offer_or_rapid_ack(client, (DHCPMessage*) data, size, NULL);
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);
/* The result isn't checked because non UTF-8 options saved by
* dhcp_lease_save can't be loaded currently */
(void) dhcp_lease_load(&lease, lease_file);
end:
assert_se(sd_dhcp_client_stop(client) >= 0);
return 0;