]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dhcp-identifier: generate static and constant DUID-EN when the client is running...
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 7 Feb 2022 06:31:33 +0000 (15:31 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 14 Feb 2022 06:00:59 +0000 (15:00 +0900)
Follow-up for 9216fddc5a8ac2742e6cfa7660f95c20ca4f2193.

src/libsystemd-network/dhcp-identifier.c
src/libsystemd-network/dhcp-identifier.h
src/libsystemd-network/sd-dhcp-client.c
src/libsystemd-network/sd-dhcp6-client.c
src/libsystemd-network/test-dhcp-client.c

index f9769300d52f3d35bd7c649454cd0411dd3149a8..ce59216c1ba60dc4244483fa5df34c5143104125 100644 (file)
@@ -120,20 +120,23 @@ static int dhcp_identifier_set_duid_ll(const uint8_t *addr, size_t addr_len, uin
         return 0;
 }
 
-int dhcp_identifier_set_duid_en(struct duid *ret_duid, size_t *ret_len) {
+int dhcp_identifier_set_duid_en(bool test_mode, struct duid *ret_duid, size_t *ret_len) {
         sd_id128_t machine_id;
         uint64_t hash;
+        int r;
 
         assert(ret_duid);
         assert(ret_len);
 
-#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
-        int r = sd_id128_get_machine(&machine_id);
-        if (r < 0)
-                return r;
-#else
-        machine_id = SD_ID128_MAKE(01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 10);
-#endif
+        if (!test_mode) {
+                r = sd_id128_get_machine(&machine_id);
+                if (r < 0)
+                        return r;
+        } else
+                /* For tests, especially for fuzzers, reproducibility is important.
+                 * Hence, use a static and constant machine ID.
+                 * See 9216fddc5a8ac2742e6cfa7660f95c20ca4f2193. */
+                machine_id = SD_ID128_MAKE(01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 10);
 
         unaligned_write_be16(&ret_duid->type, DUID_TYPE_EN);
         unaligned_write_be32(&ret_duid->en.pen, SYSTEMD_PEN);
@@ -145,6 +148,9 @@ int dhcp_identifier_set_duid_en(struct duid *ret_duid, size_t *ret_len) {
 
         *ret_len = sizeof(ret_duid->type) + sizeof(ret_duid->en);
 
+        if (test_mode)
+                assert_se(memcmp(ret_duid, (const uint8_t[]) { 0x00, 0x02, 0x00, 0x00, 0xab, 0x11, 0x61, 0x77, 0x40, 0xde, 0x13, 0x42, 0xc3, 0xa2 }, *ret_len) == 0);
+
         return 0;
 }
 
@@ -173,6 +179,7 @@ int dhcp_identifier_set_duid(
                 size_t addr_len,
                 uint16_t arp_type,
                 usec_t llt_time,
+                bool test_mode,
                 struct duid *ret_duid,
                 size_t *ret_len) {
 
@@ -180,7 +187,7 @@ int dhcp_identifier_set_duid(
         case DUID_TYPE_LLT:
                 return dhcp_identifier_set_duid_llt(addr, addr_len, arp_type, llt_time, ret_duid, ret_len);
         case DUID_TYPE_EN:
-                return dhcp_identifier_set_duid_en(ret_duid, ret_len);
+                return dhcp_identifier_set_duid_en(test_mode, ret_duid, ret_len);
         case DUID_TYPE_LL:
                 return dhcp_identifier_set_duid_ll(addr, addr_len, arp_type, ret_duid, ret_len);
         case DUID_TYPE_UUID:
index acf203c057dc9f7de98d5fb95981c75ec1c2d9ff..697ba3bfbb0d28d77a4841ca546cfca6333e43d2 100644 (file)
@@ -55,13 +55,14 @@ struct duid {
 } _packed_;
 
 int dhcp_validate_duid_len(DUIDType duid_type, size_t duid_len, bool strict);
-int dhcp_identifier_set_duid_en(struct duid *ret_duid, size_t *ret_len);
+int dhcp_identifier_set_duid_en(bool test_mode, struct duid *ret_duid, size_t *ret_len);
 int dhcp_identifier_set_duid(
                 DUIDType duid_type,
                 const uint8_t *addr,
                 size_t addr_len,
                 uint16_t arp_type,
                 usec_t llt_time,
+                bool test_mode,
                 struct duid *ret_duid,
                 size_t *ret_len);
 int dhcp_identifier_set_iaid(
index 2106fa8a2c0559f4e2588b65b64165503b7de0ac..84ae5cddd6477f4097bd86fa787811e0f3174e83 100644 (file)
@@ -492,7 +492,8 @@ static int dhcp_client_set_iaid_duid_internal(
 
         } else {
                 r = dhcp_identifier_set_duid(duid_type, client->mac_addr, client->mac_addr_len,
-                                             client->arp_type, llt_time, &client->client_id.ns.duid, &len);
+                                             client->arp_type, llt_time, client->test_mode,
+                                             &client->client_id.ns.duid, &len);
                 if (r == -EOPNOTSUPP)
                         return log_dhcp_client_errno(client, r,
                                                      "Failed to set %s. MAC address is not set or "
@@ -858,7 +859,7 @@ static int client_message_init(
                 if (r < 0)
                         return r;
 
-                r = dhcp_identifier_set_duid_en(&client->client_id.ns.duid, &duid_len);
+                r = dhcp_identifier_set_duid_en(client->test_mode, &client->client_id.ns.duid, &duid_len);
                 if (r < 0)
                         return r;
 
index ce543b7dfb5176b754a583953794ead88590cab3..ad6c2b550d4987cb47f15c0195021dd521cfa737 100644 (file)
@@ -271,7 +271,7 @@ static int client_ensure_duid(sd_dhcp6_client *client) {
         if (client->duid_len != 0)
                 return 0;
 
-        return dhcp_identifier_set_duid_en(&client->duid, &client->duid_len);
+        return dhcp_identifier_set_duid_en(client->test_mode, &client->duid, &client->duid_len);
 }
 
 /**
@@ -307,7 +307,7 @@ static int dhcp6_client_set_duid_internal(
 
         } else {
                 r = dhcp_identifier_set_duid(duid_type, client->hw_addr.bytes, client->hw_addr.length,
-                                             client->arp_type, llt_time, &client->duid, &client->duid_len);
+                                             client->arp_type, llt_time, client->test_mode, &client->duid, &client->duid_len);
                 if (r == -EOPNOTSUPP)
                         return log_dhcp6_client_errno(client, r,
                                                       "Failed to set %s. MAC address is not set or "
index 4b11cdbc167d6d4edd90e0ccf769bd729562226b..476dcce85ef2077b5935cd23297d5173b1cf5f43 100644 (file)
@@ -170,7 +170,7 @@ static int check_options(uint8_t code, uint8_t len, const void *option, void *us
                 struct duid duid;
                 size_t duid_len;
 
-                assert_se(dhcp_identifier_set_duid_en(&duid, &duid_len) >= 0);
+                assert_se(dhcp_identifier_set_duid_en(/* test_mode = */ true, &duid, &duid_len) >= 0);
                 assert_se(dhcp_identifier_set_iaid(42, mac_addr, ETH_ALEN, true, /* use_mac = */ true, &iaid) >= 0);
 
                 assert_se(len == sizeof(uint8_t) + sizeof(uint32_t) + duid_len);