libnetwork: Fixed getifaddrs() implementation.
* It overwrote the request buffer, and tried to access its previous
contents afterwards.
* It incorrectly copied sockaddrs.
* It did not initialize all members.
* It did not check for error codes from ioctl().
* It did not properly set errno in many cases, but just returned an
error right away.
* This hopefully fixes bug #12156. Adrien obviously had a bad day :-)
This commit is contained in:
@@ -4,74 +4,105 @@
|
|||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
* Adrien Destugues, [email protected]
|
* Adrien Destugues, [email protected]
|
||||||
|
* Axel Dörfler, [email protected]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <new>
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/sockio.h>
|
#include <sys/sockio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <new>
|
|
||||||
|
|
||||||
#include <AutoDeleter.h>
|
#include <AutoDeleter.h>
|
||||||
|
|
||||||
#include <ifaddrs.h>
|
#include <ifaddrs.h>
|
||||||
|
|
||||||
|
|
||||||
int getifaddrs(struct ifaddrs **ifap)
|
static sockaddr*
|
||||||
|
copy_address(const sockaddr& address)
|
||||||
{
|
{
|
||||||
if (ifap == NULL) {
|
if (address.sa_family == AF_UNSPEC)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
sockaddr_storage* copy = new (std::nothrow) sockaddr_storage;
|
||||||
|
if (copy == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
size_t length = std::min(sizeof(sockaddr_storage), (size_t)address.sa_len);
|
||||||
|
switch (address.sa_family) {
|
||||||
|
case AF_INET:
|
||||||
|
length = sizeof(sockaddr_in);
|
||||||
|
break;
|
||||||
|
case AF_INET6:
|
||||||
|
length = sizeof(sockaddr_in6);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
memcpy(copy, &address, length);
|
||||||
|
copy->ss_len = length;
|
||||||
|
return (sockaddr*)copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*! Returns a chained list of all interfaces.
|
||||||
|
|
||||||
|
We follow BSD semantics, and only return one entry per interface,
|
||||||
|
not per address; since this is mainly used by NetBSD's netresolv, it's
|
||||||
|
probably what it expects.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
getifaddrs(struct ifaddrs** _ifaddrs)
|
||||||
|
{
|
||||||
|
if (_ifaddrs == NULL) {
|
||||||
errno = B_BAD_VALUE;
|
errno = B_BAD_VALUE;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
|
||||||
uint32 cookie;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// get a list of all interfaces
|
|
||||||
|
|
||||||
int socket = ::socket(AF_INET, SOCK_DGRAM, 0);
|
int socket = ::socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
if (socket < 0)
|
if (socket < 0)
|
||||||
return errno;
|
return -1;
|
||||||
|
|
||||||
FileDescriptorCloser closer(socket);
|
FileDescriptorCloser closer(socket);
|
||||||
|
|
||||||
// get interface count
|
// Get interface count
|
||||||
ifconf config;
|
ifconf config;
|
||||||
config.ifc_len = sizeof(config.ifc_value);
|
config.ifc_len = sizeof(config.ifc_value);
|
||||||
if (ioctl(socket, SIOCGIFCOUNT, &config, sizeof(struct ifconf)) < 0)
|
if (ioctl(socket, SIOCGIFCOUNT, &config, sizeof(struct ifconf)) < 0)
|
||||||
return errno;
|
return -1;
|
||||||
|
|
||||||
size_t count = (size_t)config.ifc_value;
|
size_t count = (size_t)config.ifc_value;
|
||||||
if (count == 0)
|
if (count == 0) {
|
||||||
return B_BAD_VALUE;
|
errno = B_BAD_VALUE;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// allocate a buffer for ifreqs for all interfaces
|
// Allocate a buffer for ifreqs for all interfaces
|
||||||
char* buffer = (char*)malloc(count * sizeof(struct ifreq));
|
char* buffer = (char*)malloc(count * sizeof(struct ifreq));
|
||||||
if (buffer == NULL)
|
if (buffer == NULL) {
|
||||||
return B_NO_MEMORY;
|
errno = B_NO_MEMORY;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
MemoryDeleter deleter(buffer);
|
MemoryDeleter deleter(buffer);
|
||||||
|
|
||||||
// get interfaces configuration
|
// Get interfaces configuration
|
||||||
config.ifc_len = count * sizeof(struct ifreq);
|
config.ifc_len = count * sizeof(struct ifreq);
|
||||||
config.ifc_buf = buffer;
|
config.ifc_buf = buffer;
|
||||||
if (ioctl(socket, SIOCGIFCONF, &config, sizeof(struct ifconf)) < 0)
|
if (ioctl(socket, SIOCGIFCONF, &config, sizeof(struct ifconf)) < 0)
|
||||||
return errno;
|
return -1;
|
||||||
|
|
||||||
ifreq* interfaces = (ifreq*)buffer;
|
ifreq* interfaces = (ifreq*)buffer;
|
||||||
ifreq* end = (ifreq*)(buffer + config.ifc_len);
|
ifreq* end = (ifreq*)(buffer + config.ifc_len);
|
||||||
|
|
||||||
struct ifaddrs* previous = NULL;
|
struct ifaddrs* previous = NULL;
|
||||||
struct ifaddrs* current = NULL;
|
|
||||||
|
|
||||||
for (uint32_t i = 0; interfaces < end; i++) {
|
for (uint32_t i = 0; interfaces < end; i++) {
|
||||||
current = new(std::nothrow) ifaddrs();
|
struct ifaddrs* current = new(std::nothrow) ifaddrs();
|
||||||
if (current == NULL) {
|
if (current == NULL) {
|
||||||
freeifaddrs(previous);
|
freeifaddrs(previous);
|
||||||
errno = B_NO_MEMORY;
|
errno = B_NO_MEMORY;
|
||||||
@@ -83,73 +114,45 @@ int getifaddrs(struct ifaddrs **ifap)
|
|||||||
previous = current;
|
previous = current;
|
||||||
|
|
||||||
current->ifa_name = strdup(interfaces[0].ifr_name);
|
current->ifa_name = strdup(interfaces[0].ifr_name);
|
||||||
current->ifa_addr = new sockaddr(interfaces[0].ifr_addr);
|
current->ifa_addr = copy_address(interfaces[0].ifr_addr);
|
||||||
// still needs: flags, netmask, dst:broadcast addr
|
current->ifa_netmask = NULL;
|
||||||
|
current->ifa_dstaddr = NULL;
|
||||||
|
current->ifa_data = NULL;
|
||||||
|
|
||||||
ioctl(socket, SIOCGIFFLAGS, &interfaces[0], sizeof(struct ifconf));
|
ifreq request;
|
||||||
current->ifa_flags = interfaces[0].ifr_flags;
|
strlcpy(request.ifr_name, interfaces[0].ifr_name, IF_NAMESIZE);
|
||||||
ioctl(socket, SIOCGIFNETMASK, &interfaces[0], sizeof(struct ifconf));
|
|
||||||
current->ifa_netmask = new sockaddr(interfaces[0].ifr_mask);
|
|
||||||
ioctl(socket, SIOCGIFDSTADDR, &interfaces[0], sizeof(struct ifconf));
|
|
||||||
current->ifa_dstaddr = new sockaddr(interfaces[0].ifr_dstaddr);
|
|
||||||
|
|
||||||
// TODO we may also need to get other addresses for the same interface?
|
if (ioctl(socket, SIOCGIFFLAGS, &request, sizeof(struct ifreq)) == 0)
|
||||||
#if 0
|
current->ifa_flags = request.ifr_flags;
|
||||||
BNetworkInterfaceAddress address;
|
if (ioctl(socket, SIOCGIFNETMASK, &request, sizeof(struct ifreq))
|
||||||
int32 i = 0;
|
== 0) {
|
||||||
while (interface->GetAddressAt(i++, address) == B_OK) {
|
current->ifa_netmask = copy_address(request.ifr_mask);
|
||||||
if (interface == NULL) {
|
}
|
||||||
freeifaddrs(previous);
|
if (ioctl(socket, SIOCGIFDSTADDR, &request, sizeof(struct ifreq))
|
||||||
errno = B_NO_MEMORY;
|
== 0) {
|
||||||
return -1;
|
current->ifa_dstaddr = copy_address(request.ifr_dstaddr);
|
||||||
}
|
|
||||||
|
|
||||||
current = new(std::nothrow) ifaddrs();
|
|
||||||
if (current == NULL) {
|
|
||||||
freeifaddrs(previous);
|
|
||||||
errno = B_NO_MEMORY;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Chain this interface with the next one
|
|
||||||
current->ifa_next = previous;
|
|
||||||
previous = current;
|
|
||||||
|
|
||||||
current->ifa_data = interface;
|
|
||||||
current->ifa_name = interface->Name();
|
|
||||||
// Points to the name in the BNetworkInterface instance, which
|
|
||||||
// is added as ifa_data so freeifaddrs can release it.
|
|
||||||
current->ifa_flags = address.Flags();
|
|
||||||
current->ifa_addr = new sockaddr(address.Address().SockAddr());
|
|
||||||
current->ifa_netmask = new sockaddr(address.Mask().SockAddr());
|
|
||||||
current->ifa_dstaddr = new sockaddr(address.Destination().SockAddr());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface = new(std::nothrow) BNetworkInterface();
|
// Move on to next interface
|
||||||
#endif
|
|
||||||
|
|
||||||
// move on to next interface
|
|
||||||
interfaces = (ifreq*)((uint8_t*)interfaces
|
interfaces = (ifreq*)((uint8_t*)interfaces
|
||||||
+ _SIZEOF_ADDR_IFREQ(interfaces[0]));
|
+ _SIZEOF_ADDR_IFREQ(interfaces[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
*ifap = current;
|
*_ifaddrs = previous;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
freeifaddrs(struct ifaddrs *ifa)
|
freeifaddrs(struct ifaddrs* ifa)
|
||||||
{
|
{
|
||||||
struct ifaddrs* next;
|
|
||||||
while (ifa != NULL) {
|
while (ifa != NULL) {
|
||||||
free ((void*)ifa->ifa_name);
|
free((void*)ifa->ifa_name);
|
||||||
delete ifa->ifa_addr;
|
delete ifa->ifa_addr;
|
||||||
delete ifa->ifa_netmask;
|
delete ifa->ifa_netmask;
|
||||||
delete ifa->ifa_dstaddr;
|
delete ifa->ifa_dstaddr;
|
||||||
|
|
||||||
next = ifa->ifa_next;
|
struct ifaddrs* next = ifa->ifa_next;
|
||||||
delete ifa;
|
delete ifa;
|
||||||
ifa = next;
|
ifa = next;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user