]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fuzz: save/load DHCP client leases 31716/head
authorEvgeny Vereshchagin <evvers@ya.ru>
Mon, 11 Mar 2024 05:46:21 +0000 (05:46 +0000)
committerEvgeny Vereshchagin <evvers@ya.ru>
Mon, 11 Mar 2024 09:41:44 +0000 (09:41 +0000)
to make sure it more or less works with all sorts of leases.

Prompted by https://github.com/systemd/systemd/pull/30021#discussion_r1519123802

src/libsystemd-network/fuzz-dhcp-client.c

index 384972f1a0adf4025611b828d78a31dfb99e28e1..11c4ecb5112a7de8b6b63081fee77e6cf210d484 100644 (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;