1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-01 08:58:29 +03:00

dhcp-identifier: generate static and constant DUID-EN when the client is running in test mode

Follow-up for 9216fddc5a8ac2742e6cfa7660f95c20ca4f2193.
This commit is contained in:
Yu Watanabe 2022-02-07 15:31:33 +09:00
parent 5e1618fafa
commit ac680f766d
5 changed files with 24 additions and 15 deletions

View 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:

View 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(

View 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;

View 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 "

View 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);