From: Roy Marples Date: Thu, 14 May 2020 00:15:39 +0000 (+0100) Subject: privsep: sockaddr len should be socklen_t X-Git-Tag: v9.1.0~62 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9ede1675c859e45f817f09f226e24e72c24a8a50;p=thirdparty%2Fdhcpcd.git privsep: sockaddr len should be socklen_t While ps_root_getifaddrs is only for capsicum, it's highly portable and thus in the privsep-root rather than privsep-bsd. As such, store the sockaddr len as socklen_t because that's what POSIX demands. It's only a few more bytes and I'd rather make this change now than it potentially bite me later. --- diff --git a/src/privsep-root.c b/src/privsep-root.c index 7e368781..a449d7e3 100644 --- a/src/privsep-root.c +++ b/src/privsep-root.c @@ -302,6 +302,7 @@ ps_root_dogetifaddrs(void **rdata, size_t *rlen) struct ifaddrs *ifaddrs, *ifa; size_t len; uint8_t *buf, *sap; + socklen_t salen; void *ifdata; if (getifaddrs(&ifaddrs) == -1) @@ -321,7 +322,7 @@ ps_root_dogetifaddrs(void **rdata, size_t *rlen) for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { len += ALIGN(sizeof(*ifa)); len += ALIGN(IFNAMSIZ); - len += ALIGN(sizeof(*sap) * IFA_NADDRS); + len += ALIGN(sizeof(salen) * IFA_NADDRS); if (ifa->ifa_addr != NULL) len += ALIGN(sa_len(ifa->ifa_addr)); if (ifa->ifa_netmask != NULL) @@ -352,16 +353,17 @@ ps_root_dogetifaddrs(void **rdata, size_t *rlen) strlcpy((char *)buf, ifa->ifa_name, IFNAMSIZ); buf += ALIGN(IFNAMSIZ); sap = buf; - buf += ALIGN(sizeof(*sap) * IFA_NADDRS); - -#define COPYINSA(addr) \ - do { \ - *sap = sa_len((addr)); \ - if (*sap != 0) { \ - memcpy(buf, (addr), *sap); \ - buf += ALIGN(*sap); \ - } \ - sap++; \ + buf += ALIGN(sizeof(salen) * IFA_NADDRS); + +#define COPYINSA(addr) \ + do { \ + salen = sa_len((addr)); \ + if (salen != 0) { \ + memcpy(sap, &salen, sizeof(salen)); \ + memcpy(buf, (addr), salen); \ + buf += ALIGN(salen); \ + } \ + sap += sizeof(salen); \ } while (0 /*CONSTCOND */) if (ifa->ifa_addr != NULL) @@ -718,8 +720,8 @@ ps_root_getifaddrs(struct dhcpcd_ctx *ctx, struct ifaddrs **ifahead) { struct ifaddrs *ifa; void *buf = NULL; - char *bp; - unsigned char *sap; + char *bp, *sap; + socklen_t salen; size_t len; ssize_t err; @@ -741,26 +743,27 @@ ps_root_getifaddrs(struct dhcpcd_ctx *ctx, struct ifaddrs **ifahead) *ifahead = (struct ifaddrs *)(void *)bp; for (ifa = *ifahead; len != 0; ifa = ifa->ifa_next) { if (len < ALIGN(sizeof(*ifa)) + - ALIGN(IFNAMSIZ) + ALIGN(sizeof(*sap) * IFA_NADDRS)) + ALIGN(IFNAMSIZ) + ALIGN(sizeof(salen) * IFA_NADDRS)) goto err; bp += ALIGN(sizeof(*ifa)); ifa->ifa_name = bp; bp += ALIGN(IFNAMSIZ); - sap = (unsigned char *)bp; - bp += ALIGN(sizeof(*sap) * IFA_NADDRS); + sap = bp; + bp += ALIGN(sizeof(salen) * IFA_NADDRS); len -= ALIGN(sizeof(*ifa)) + - ALIGN(IFNAMSIZ) + ALIGN(sizeof(*sap) * IFA_NADDRS); + ALIGN(IFNAMSIZ) + ALIGN(sizeof(salen) * IFA_NADDRS); #define COPYOUTSA(addr) \ do { \ - if (len < *sap) \ + memcpy(&salen, sap, sizeof(salen)); \ + if (len < salen) \ goto err; \ - if (*sap != 0) { \ + if (salen != 0) { \ (addr) = (struct sockaddr *)bp; \ - bp += ALIGN(*sap); \ - len -= ALIGN(*sap); \ + bp += ALIGN(salen); \ + len -= ALIGN(salen); \ } \ - sap++; \ + sap += sizeof(salen); \ } while (0 /* CONSTCOND */) COPYOUTSA(ifa->ifa_addr);