From: Yu Watanabe Date: Mon, 7 Feb 2022 06:31:33 +0000 (+0900) Subject: dhcp-identifier: generate static and constant DUID-EN when the client is running... X-Git-Tag: v251-rc1~291^2~12 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ac680f766d03be2ba80e3b3c67e9c9506d2a8a8c;p=thirdparty%2Fsystemd.git dhcp-identifier: generate static and constant DUID-EN when the client is running in test mode Follow-up for 9216fddc5a8ac2742e6cfa7660f95c20ca4f2193. --- diff --git a/src/libsystemd-network/dhcp-identifier.c b/src/libsystemd-network/dhcp-identifier.c index f9769300d52..ce59216c1ba 100644 --- a/src/libsystemd-network/dhcp-identifier.c +++ b/src/libsystemd-network/dhcp-identifier.c @@ -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: diff --git a/src/libsystemd-network/dhcp-identifier.h b/src/libsystemd-network/dhcp-identifier.h index acf203c057d..697ba3bfbb0 100644 --- a/src/libsystemd-network/dhcp-identifier.h +++ b/src/libsystemd-network/dhcp-identifier.h @@ -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( diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index 2106fa8a2c0..84ae5cddd64 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -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; diff --git a/src/libsystemd-network/sd-dhcp6-client.c b/src/libsystemd-network/sd-dhcp6-client.c index ce543b7dfb5..ad6c2b550d4 100644 --- a/src/libsystemd-network/sd-dhcp6-client.c +++ b/src/libsystemd-network/sd-dhcp6-client.c @@ -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 " diff --git a/src/libsystemd-network/test-dhcp-client.c b/src/libsystemd-network/test-dhcp-client.c index 4b11cdbc167..476dcce85ef 100644 --- a/src/libsystemd-network/test-dhcp-client.c +++ b/src/libsystemd-network/test-dhcp-client.c @@ -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);