Use offsetof macro for all AF_UNIX address length calculations

This commit is contained in:
Jeremy Lakeman 2017-04-26 15:52:33 +09:30
parent 556dabe458
commit 73136461ad
3 changed files with 14 additions and 14 deletions

View File

@ -915,7 +915,7 @@ static int send_local_packet(int fd, const uint8_t *bytes, size_t len, const cha
return 1; return 1;
addr.local.sun_family = AF_UNIX; addr.local.sun_family = AF_UNIX;
addr.addrlen = sizeof(addr.local.sun_family) + strlen(addr.local.sun_path)+1; addr.addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(addr.local.sun_path)+1;
ssize_t sent = sendto(fd, bytes, len, 0, ssize_t sent = sendto(fd, bytes, len, 0,
&addr.addr, addr.addrlen); &addr.addr, addr.addrlen);
@ -1392,12 +1392,12 @@ static void file_interface_init(const struct config_network_interface *ifconfig)
addr.local.sun_family=AF_UNIX; addr.local.sun_family=AF_UNIX;
size_t len = strlen(addr.local.sun_path); size_t len = strlen(addr.local.sun_path);
addr.addrlen=sizeof addr.local.sun_family + len + 1; addr.addrlen = offsetof(struct sockaddr_un, sun_path) + len + 1;
broadcast = addr; broadcast = addr;
while(len && broadcast.local.sun_path[len]!='/') while(len && broadcast.local.sun_path[len]!='/')
broadcast.local.sun_path[len--]='\0'; broadcast.local.sun_path[len--]='\0';
broadcast.addrlen = sizeof addr.local.sun_family + len + 2; broadcast.addrlen = offsetof(struct sockaddr_un, sun_path) + len + 2;
break; break;
} }

View File

@ -160,7 +160,7 @@ static void recv_packet(int fd, struct network *network, struct peer *destinatio
return; return;
} }
unicast_addr.addrlen=sizeof unicast_addr.local - sizeof unicast_addr.local.sun_path + strlen(unicast_addr.local.sun_path) + 1; unicast_addr.addrlen=offsetof(struct sockaddr_un, sun_path) + strlen(unicast_addr.local.sun_path) + 1;
peer = emalloc_zero(sizeof(struct peer)); peer = emalloc_zero(sizeof(struct peer));
if (!peer) { if (!peer) {
@ -414,7 +414,7 @@ static int console_create(const struct cli_parsed *parsed, struct cli_context *U
if (strbuf_overrun(b)) if (strbuf_overrun(b))
return WHY("Path too long"); return WHY("Path too long");
addr.addrlen=sizeof addr.local.sun_family + strlen(addr.local.sun_path) + 1; addr.addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(addr.local.sun_path) + 1;
int fd = esocket(AF_UNIX, SOCK_DGRAM, 0); int fd = esocket(AF_UNIX, SOCK_DGRAM, 0);
if (fd==-1) if (fd==-1)
return -1; return -1;

View File

@ -55,7 +55,7 @@ int _make_local_sockaddr(struct __sourceloc __whence, struct socket_address *add
va_end(ap); va_end(ap);
if (!r) if (!r)
return WHY("socket name overflow"); return WHY("socket name overflow");
addr->addrlen=sizeof addr->local - sizeof addr->local.sun_path + strlen(addr->local.sun_path) + 1; addr->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(addr->local.sun_path) + 1;
// TODO perform real path transformation in making the serval instance path // TODO perform real path transformation in making the serval instance path
// if (real_sockaddr(addr, addr) == -1) // if (real_sockaddr(addr, addr) == -1)
// return -1; // return -1;
@ -85,9 +85,9 @@ int _make_local_sockaddr(struct __sourceloc __whence, struct socket_address *add
int real_sockaddr(const struct socket_address *src_addr, struct socket_address *dst_addr) int real_sockaddr(const struct socket_address *src_addr, struct socket_address *dst_addr)
{ {
DEBUGF2(io, verbose_io, "real_sockaddr(src_addr=%p %s, dst_addr=%p)", src_addr, alloca_socket_address(src_addr), dst_addr); DEBUGF2(io, verbose_io, "real_sockaddr(src_addr=%p %s, dst_addr=%p)", src_addr, alloca_socket_address(src_addr), dst_addr);
assert(src_addr->addrlen > (socklen_t)sizeof src_addr->local.sun_family); assert(src_addr->addrlen > (socklen_t)offsetof(struct sockaddr_un, sun_path));
size_t src_path_len = src_addr->addrlen - sizeof src_addr->local.sun_family; size_t src_path_len = src_addr->addrlen - offsetof(struct sockaddr_un, sun_path);
if ( (size_t)src_addr->addrlen >= sizeof src_addr->local.sun_family + 1 if ( src_path_len > 1
&& src_addr->local.sun_family == AF_UNIX && src_addr->local.sun_family == AF_UNIX
&& src_addr->local.sun_path[0] != '\0' && src_addr->local.sun_path[0] != '\0'
&& src_addr->local.sun_path[src_path_len - 1] == '\0' && src_addr->local.sun_path[src_path_len - 1] == '\0'
@ -103,7 +103,7 @@ int real_sockaddr(const struct socket_address *src_addr, struct socket_address *
|| memcmp(real_path, src_addr->local.sun_path, src_path_len) != 0 || memcmp(real_path, src_addr->local.sun_path, src_path_len) != 0
) { ) {
memcpy(dst_addr->local.sun_path, real_path, real_path_len); memcpy(dst_addr->local.sun_path, real_path, real_path_len);
dst_addr->addrlen = real_path_len + sizeof dst_addr->local.sun_family; dst_addr->addrlen = real_path_len + offsetof(struct sockaddr_un, sun_path);
DEBUGF2(io, verbose_io, " --> return %s", alloca_socket_address(dst_addr)); DEBUGF2(io, verbose_io, " --> return %s", alloca_socket_address(dst_addr));
return 1; return 1;
} }
@ -152,8 +152,8 @@ int cmp_sockaddr(const struct socket_address *addrA, const struct socket_address
return 0; return 0;
}break; }break;
case AF_UNIX: { case AF_UNIX: {
unsigned pathlenA = addrA->addrlen - sizeof (addrA->local.sun_family); unsigned pathlenA = addrA->addrlen - offsetof(struct sockaddr_un, sun_path);
unsigned pathlenB = addrB->addrlen - sizeof (addrB->local.sun_family); unsigned pathlenB = addrB->addrlen - offsetof(struct sockaddr_un, sun_path);
int c; int c;
if ( pathlenA > 1 && pathlenB > 1 if ( pathlenA > 1 && pathlenB > 1
&& addrA->local.sun_path[0] == '\0' && addrA->local.sun_path[0] == '\0'
@ -220,7 +220,7 @@ int _socket_bind(struct __sourceloc __whence, int sock, const struct socket_addr
{ {
assert(addr->addrlen > (socklen_t)sizeof addr->addr.sa_family); assert(addr->addrlen > (socklen_t)sizeof addr->addr.sa_family);
if (addr->addr.sa_family == AF_UNIX && addr->local.sun_path[0] != '\0') { if (addr->addr.sa_family == AF_UNIX && addr->local.sun_path[0] != '\0') {
assert(addr->local.sun_path[addr->addrlen - sizeof addr->local.sun_family - 1] == '\0'); assert(addr->local.sun_path[addr->addrlen - offsetof(struct sockaddr_un, sun_path) -1] == '\0');
// make sure the path exists, create it if we can // make sure the path exists, create it if we can
size_t dirsiz = strlen(addr->local.sun_path) + 1; size_t dirsiz = strlen(addr->local.sun_path) + 1;
char dir_buf[dirsiz]; char dir_buf[dirsiz];
@ -275,7 +275,7 @@ int socket_unlink_close(int sock)
if (getsockname(sock, &addr.addr, &addr.addrlen)) if (getsockname(sock, &addr.addr, &addr.addrlen))
WHYF_perror("getsockname(%d)", sock); WHYF_perror("getsockname(%d)", sock);
else if (addr.addr.sa_family==AF_UNIX else if (addr.addr.sa_family==AF_UNIX
&& (size_t)addr.addrlen > sizeof addr.local.sun_family && (size_t)addr.addrlen >= offsetof(struct sockaddr_un, sun_path)
&& (size_t)addr.addrlen <= sizeof addr.local && (size_t)addr.addrlen <= sizeof addr.local
&& addr.local.sun_path[0] != '\0') { && addr.local.sun_path[0] != '\0') {
if (unlink(addr.local.sun_path) == -1) if (unlink(addr.local.sun_path) == -1)