Skip to content

Commit

Permalink
Another getifaddrs fix
Browse files Browse the repository at this point in the history
ia.ifa_data was written out-of-bounds
  • Loading branch information
th-otto committed Jun 28, 2024
1 parent 12d2585 commit b1d929a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion socket/ifaddrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ int __getifaddrs(struct ifaddrs **ifap)
storage->broadaddr = ifr->ifr_dstaddr;
}
}
storage[i].ia.ifa_data = NULL; /* Nothing here for now. */
storage->ia.ifa_data = NULL; /* Nothing here for now. */
}
free(ibuf);
close(fd);
Expand Down
17 changes: 12 additions & 5 deletions socket/test-ifaddrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ int main(void)
{
struct ifaddrs *ifap;
struct ifaddrs *ifa;
const char *addr;
struct sockaddr_in *sa;
char *addr;
char buf[1024];

if (getifaddrs(&ifap) == -1)
{
Expand All @@ -24,11 +25,17 @@ int main(void)
}
for (ifa = ifap; ifa; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET)
if (ifa->ifa_addr)
{
sa = (struct sockaddr_in *) ifa->ifa_addr;
addr = inet_ntoa(sa->sin_addr);
printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, addr);
switch (ifa->ifa_addr->sa_family)
{
case AF_INET:
case AF_INET6:
sa = (struct sockaddr_in *) ifa->ifa_addr;
addr = inet_ntop(ifa->ifa_addr->sa_family, &sa->sin_addr, buf, sizeof(buf));
printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, addr);
break;
}
}

}
Expand Down

0 comments on commit b1d929a

Please sign in to comment.