net: Add optional length checking to CService::SetSockAddr

In almost all cases (the only exception is `getifaddrs`), we know the
size of the data passed into SetSockAddr, so we can check this to be
what is expected.
This commit is contained in:
laanwj
2024-10-01 16:28:28 +02:00
parent 35bf426e02
commit ab1d3ece02
5 changed files with 15 additions and 13 deletions

View File

@@ -169,16 +169,9 @@ std::optional<CNetAddr> QueryDefaultGatewayImpl(sa_family_t family)
std::optional<CNetAddr> FromSockAddr(const struct sockaddr* addr)
{
// Check valid length. Note that sa_len is not part of POSIX, and exists on MacOS and some BSDs only, so we can't
// do this check in SetSockAddr.
if (!(addr->sa_family == AF_INET && addr->sa_len == sizeof(struct sockaddr_in)) &&
!(addr->sa_family == AF_INET6 && addr->sa_len == sizeof(struct sockaddr_in6))) {
return std::nullopt;
}
// Fill in a CService from the sockaddr, then drop the port part.
CService service;
if (service.SetSockAddr(addr)) {
if (service.SetSockAddr(addr, addr->sa_len)) {
return (CNetAddr)service;
}
return std::nullopt;