From 2a12960bcdb90638bf28da237f89383bdd19b344 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 29 Jul 2018 16:01:37 +0900 Subject: [PATCH 1/2] sd-resolve: use structured initialization at more places --- src/libsystemd/sd-resolve/sd-resolve.c | 53 ++++++++++++++------------ 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/src/libsystemd/sd-resolve/sd-resolve.c b/src/libsystemd/sd-resolve/sd-resolve.c index c4fefb1b9a5..8cf3d7d54b3 100644 --- a/src/libsystemd/sd-resolve/sd-resolve.c +++ b/src/libsystemd/sd-resolve/sd-resolve.c @@ -200,12 +200,14 @@ static void *serialize_addrinfo(void *p, const struct addrinfo *ai, size_t *leng if (*length + l > maxlength) return NULL; - s.ai_flags = ai->ai_flags; - s.ai_family = ai->ai_family; - s.ai_socktype = ai->ai_socktype; - s.ai_protocol = ai->ai_protocol; - s.ai_addrlen = ai->ai_addrlen; - s.canonname_len = cnl; + s = (AddrInfoSerialization) { + .ai_flags = ai->ai_flags, + .ai_family = ai->ai_family, + .ai_socktype = ai->ai_socktype, + .ai_protocol = ai->ai_protocol, + .ai_addrlen = ai->ai_addrlen, + .canonname_len = cnl, + }; memcpy((uint8_t*) p, &s, sizeof(AddrInfoSerialization)); memcpy((uint8_t*) p + sizeof(AddrInfoSerialization), ai->ai_addr, ai->ai_addrlen); @@ -278,14 +280,7 @@ static int send_nameinfo_reply( int _errno, int _h_errno) { - NameInfoResponse resp = { - .header.type = RESPONSE_NAMEINFO, - .header.id = id, - .ret = ret, - ._errno = _errno, - ._h_errno = _h_errno, - }; - + NameInfoResponse resp; struct iovec iov[3]; struct msghdr mh; size_t hl, sl; @@ -295,9 +290,16 @@ static int send_nameinfo_reply( sl = serv ? strlen(serv)+1 : 0; hl = host ? strlen(host)+1 : 0; - resp.header.length = sizeof(NameInfoResponse) + hl + sl; - resp.hostlen = hl; - resp.servlen = sl; + resp = (NameInfoResponse) { + .header.type = RESPONSE_NAMEINFO, + .header.id = id, + .header.length = sizeof(NameInfoResponse) + hl + sl, + .hostlen = hl, + .servlen = sl, + .ret = ret, + ._errno = _errno, + ._h_errno = _h_errno, + }; iov[0] = (struct iovec) { .iov_base = &resp, .iov_len = sizeof(NameInfoResponse) }; iov[1] = (struct iovec) { .iov_base = (void*) host, .iov_len = hl }; @@ -500,7 +502,6 @@ _public_ int sd_resolve_new(sd_resolve **ret) { } _public_ int sd_resolve_default(sd_resolve **ret) { - static thread_local sd_resolve *default_resolve = NULL; sd_resolve *e = NULL; int r; @@ -681,8 +682,8 @@ static int complete_query(sd_resolve *resolve, sd_resolve_query *q) { static int unserialize_addrinfo(const void **p, size_t *length, struct addrinfo **ret_ai) { AddrInfoSerialization s; - size_t l; struct addrinfo *ai; + size_t l; assert(p); assert(*p); @@ -698,15 +699,17 @@ static int unserialize_addrinfo(const void **p, size_t *length, struct addrinfo if (*length < l) return -EBADMSG; - ai = new0(struct addrinfo, 1); + ai = new(struct addrinfo, 1); if (!ai) return -ENOMEM; - ai->ai_flags = s.ai_flags; - ai->ai_family = s.ai_family; - ai->ai_socktype = s.ai_socktype; - ai->ai_protocol = s.ai_protocol; - ai->ai_addrlen = s.ai_addrlen; + *ai = (struct addrinfo) { + .ai_flags = s.ai_flags, + .ai_family = s.ai_family, + .ai_socktype = s.ai_socktype, + .ai_protocol = s.ai_protocol, + .ai_addrlen = s.ai_addrlen, + }; if (s.ai_addrlen > 0) { ai->ai_addr = memdup((const uint8_t*) *p + sizeof(AddrInfoSerialization), s.ai_addrlen); From b127bc99d13cede3b1ba6bd8acd936ff7a0d2f31 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 29 Jul 2018 16:04:56 +0900 Subject: [PATCH 2/2] sd-resolve: workaround for structured initialization to nested structs When a nested struct is initialized by structured initializer, then padding space is not cleared by zero. So, before setting values, this makes explicitly set zero including padding. This fixes the following false positive warning by valgrind: ``` ==492== Syscall param sendmsg(msg.msg_iov[0]) points to uninitialised byte(s) ==492== at 0x56D0CF7: sendmsg (in /usr/lib64/libpthread-2.27.so) ==492== by 0x4FDD3C5: sd_resolve_getaddrinfo (sd-resolve.c:975) ==492== by 0x110B9E: manager_connect (timesyncd-manager.c:879) ==492== by 0x10B729: main (timesyncd.c:165) ==492== Address 0x1fff0008f1 is on thread 1's stack ==492== in frame #1, created by sd_resolve_getaddrinfo (sd-resolve.c:928) ==492== ``` --- src/libsystemd/sd-resolve/sd-resolve.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/libsystemd/sd-resolve/sd-resolve.c b/src/libsystemd/sd-resolve/sd-resolve.c index 8cf3d7d54b3..3eb42c59eba 100644 --- a/src/libsystemd/sd-resolve/sd-resolve.c +++ b/src/libsystemd/sd-resolve/sd-resolve.c @@ -226,15 +226,7 @@ static int send_addrinfo_reply( int _errno, int _h_errno) { - AddrInfoResponse resp = { - .header.type = RESPONSE_ADDRINFO, - .header.id = id, - .header.length = sizeof(AddrInfoResponse), - .ret = ret, - ._errno = _errno, - ._h_errno = _h_errno, - }; - + AddrInfoResponse resp = {}; union { AddrInfoSerialization ais; uint8_t space[BUFSIZE]; @@ -244,6 +236,15 @@ static int send_addrinfo_reply( assert(out_fd >= 0); + resp = (AddrInfoResponse) { + .header.type = RESPONSE_ADDRINFO, + .header.id = id, + .header.length = sizeof(AddrInfoResponse), + .ret = ret, + ._errno = _errno, + ._h_errno = _h_errno, + }; + if (ret == 0 && ai) { void *p = &buffer; struct addrinfo *k; @@ -280,7 +281,7 @@ static int send_nameinfo_reply( int _errno, int _h_errno) { - NameInfoResponse resp; + NameInfoResponse resp = {}; struct iovec iov[3]; struct msghdr mh; size_t hl, sl; @@ -931,7 +932,7 @@ _public_ int sd_resolve_getaddrinfo( sd_resolve_getaddrinfo_handler_t callback, void *userdata) { _cleanup_(sd_resolve_query_unrefp) sd_resolve_query *q = NULL; - AddrInfoRequest req; + AddrInfoRequest req = {}; struct iovec iov[3]; struct msghdr mh = {}; int r; @@ -1008,7 +1009,7 @@ _public_ int sd_resolve_getnameinfo( void *userdata) { _cleanup_(sd_resolve_query_unrefp) sd_resolve_query *q = NULL; - NameInfoRequest req; + NameInfoRequest req = {}; struct iovec iov[2]; struct msghdr mh; int r;