From fc62d1c54f84a9b203c76b58bb63d4e1e37406e8 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 15 Feb 2022 23:36:45 +0900 Subject: [PATCH 1/4] sd-dhcp6-client: add comment about ia_na and ia_pd --- src/libsystemd-network/dhcp6-lease-internal.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libsystemd-network/dhcp6-lease-internal.h b/src/libsystemd-network/dhcp6-lease-internal.h index 1fbaab96e9..1f10dccbbb 100644 --- a/src/libsystemd-network/dhcp6-lease-internal.h +++ b/src/libsystemd-network/dhcp6-lease-internal.h @@ -28,8 +28,8 @@ struct sd_dhcp6_lease { usec_t lifetime_valid; struct in6_addr server_address; - DHCP6IA *ia_na; - DHCP6IA *ia_pd; + DHCP6IA *ia_na; /* Identity association non-temporary addresses */ + DHCP6IA *ia_pd; /* Identity association prefix delegation */ DHCP6Address *addr_iter; DHCP6Address *prefix_iter; From c9333c236ff6a6100e7c9eefb72d9527637dc76a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 15 Feb 2022 23:43:15 +0900 Subject: [PATCH 2/4] dhcp-identifier: use offsetof() Also use appropriate place to store UUID. --- src/libsystemd-network/dhcp-identifier.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libsystemd-network/dhcp-identifier.c b/src/libsystemd-network/dhcp-identifier.c index ce59216c1b..d2b190244f 100644 --- a/src/libsystemd-network/dhcp-identifier.c +++ b/src/libsystemd-network/dhcp-identifier.c @@ -91,7 +91,7 @@ static int dhcp_identifier_set_duid_llt(const uint8_t *addr, size_t addr_len, ui unaligned_write_be32(&ret_duid->llt.time, time_from_2000y); memcpy(ret_duid->llt.haddr, addr, addr_len); - *ret_len = sizeof(ret_duid->type) + sizeof(ret_duid->llt.htype) + sizeof(ret_duid->llt.time) + addr_len; + *ret_len = offsetof(struct duid, llt.haddr) + addr_len; return 0; } @@ -115,7 +115,7 @@ static int dhcp_identifier_set_duid_ll(const uint8_t *addr, size_t addr_len, uin unaligned_write_be16(&ret_duid->ll.htype, arp_type); memcpy(ret_duid->ll.haddr, addr, addr_len); - *ret_len = sizeof(ret_duid->type) + sizeof(ret_duid->ll.htype) + addr_len; + *ret_len = offsetof(struct duid, ll.haddr) + addr_len; return 0; } @@ -146,7 +146,7 @@ int dhcp_identifier_set_duid_en(bool test_mode, struct duid *ret_duid, size_t *r hash = htole64(siphash24(&machine_id, sizeof(machine_id), HASH_KEY.bytes)); memcpy(ret_duid->en.id, &hash, sizeof(ret_duid->en.id)); - *ret_len = sizeof(ret_duid->type) + sizeof(ret_duid->en); + *ret_len = offsetof(struct duid, en.id) + sizeof(ret_duid->en.id); 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); @@ -166,9 +166,9 @@ static int dhcp_identifier_set_duid_uuid(struct duid *ret_duid, size_t *ret_len) return r; unaligned_write_be16(&ret_duid->type, DUID_TYPE_UUID); - memcpy(ret_duid->raw.data, &machine_id, sizeof(machine_id)); + memcpy(&ret_duid->uuid.uuid, &machine_id, sizeof(machine_id)); - *ret_len = sizeof(ret_duid->type) + sizeof(machine_id); + *ret_len = offsetof(struct duid, uuid.uuid) + sizeof(machine_id); return 0; } From 507cd76085482022949fbf18c0a8da3356765967 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 15 Feb 2022 23:50:14 +0900 Subject: [PATCH 3/4] memory-util: introdyce mempcpy_safe() --- src/basic/memory-util.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index 9f37431fc1..6e3280b9df 100644 --- a/src/basic/memory-util.h +++ b/src/basic/memory-util.h @@ -15,7 +15,7 @@ size_t page_size(void) _pure_; #define PAGE_ALIGN_DOWN(l) ((l) & ~(page_size() - 1)) #define PAGE_OFFSET(l) ((l) & (page_size() - 1)) -/* Normal memcpy requires src to be nonnull. We do nothing if n is 0. */ +/* Normal memcpy() requires src to be nonnull. We do nothing if n is 0. */ static inline void *memcpy_safe(void *dst, const void *src, size_t n) { if (n == 0) return dst; @@ -23,7 +23,15 @@ static inline void *memcpy_safe(void *dst, const void *src, size_t n) { return memcpy(dst, src, n); } -/* Normal memcmp requires s1 and s2 to be nonnull. We do nothing if n is 0. */ +/* Normal mempcpy() requires src to be nonnull. We do nothing if n is 0. */ +static inline void *mempcpy_safe(void *dst, const void *src, size_t n) { + if (n == 0) + return dst; + assert(src); + return mempcpy(dst, src, n); +} + +/* Normal memcmp() requires s1 and s2 to be nonnull. We do nothing if n is 0. */ static inline int memcmp_safe(const void *s1, const void *s2, size_t n) { if (n == 0) return 0; From 37598ac6eb56c3411fc5b1adfa8546a3517df406 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 15 Feb 2022 23:51:28 +0900 Subject: [PATCH 4/4] sd-dhcp6-client: use mempcpy() or mempcpy_safe() --- src/libsystemd-network/dhcp6-option.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/libsystemd-network/dhcp6-option.c b/src/libsystemd-network/dhcp6-option.c index 27c907d9e9..d25377d377 100644 --- a/src/libsystemd-network/dhcp6-option.c +++ b/src/libsystemd-network/dhcp6-option.c @@ -236,9 +236,7 @@ int dhcp6_option_append(uint8_t **buf, size_t *buflen, uint16_t code, if (r < 0) return r; - memcpy_safe(*buf, optval, optlen); - - *buf += optlen; + *buf = mempcpy_safe(*buf, optval, optlen); *buflen -= optlen; return 0; @@ -294,9 +292,7 @@ static int option_append_ia_address(uint8_t **buf, size_t *buflen, const struct if (r < 0) return r; - memcpy(*buf, &a, sizeof(struct iaaddr)); - - *buf += sizeof(struct iaaddr); + *buf = mempcpy(*buf, &a, sizeof(struct iaaddr)); *buflen -= sizeof(struct iaaddr); return offsetof(DHCP6Option, data) + sizeof(struct iaaddr); @@ -324,9 +320,7 @@ static int option_append_pd_prefix(uint8_t **buf, size_t *buflen, const struct i if (r < 0) return r; - memcpy(*buf, &p, sizeof(struct iapdprefix)); - - *buf += sizeof(struct iapdprefix); + *buf = mempcpy(*buf, &p, sizeof(struct iapdprefix)); *buflen -= sizeof(struct iapdprefix); return offsetof(DHCP6Option, data) + sizeof(struct iapdprefix); @@ -357,7 +351,7 @@ int dhcp6_option_append_ia(uint8_t **buf, size_t *buflen, const DHCP6IA *ia) { break; case SD_DHCP6_OPTION_IA_TA: - len = sizeof(be32_t); /* IA_TA does not have lifetime. */ + len = sizeof(header.id); /* IA_TA does not have lifetime. */ header = (struct ia_header) { .id = ia->header.id, }; @@ -377,8 +371,7 @@ int dhcp6_option_append_ia(uint8_t **buf, size_t *buflen, const DHCP6IA *ia) { *buf += offsetof(DHCP6Option, data); *buflen -= offsetof(DHCP6Option, data); - memcpy(*buf, &header, len); - *buf += len; + *buf = mempcpy(*buf, &header, len); *buflen -= len; LIST_FOREACH(addresses, addr, ia->addresses) {