From: Pieter Lexis Date: Wed, 31 Aug 2016 13:30:30 +0000 (+0200) Subject: ComboAddress: don't allow invalid ports X-Git-Tag: dnsdist-1.1.0-beta1~4^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F4387%2Fhead;p=thirdparty%2Fpdns.git ComboAddress: don't allow invalid ports Add tests for this. Fixes: #4382 --- diff --git a/pdns/misc.cc b/pdns/misc.cc index 43e3eb0eec..ddfbc7b641 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -739,7 +739,12 @@ int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret) if(pos == string::npos || pos + 2 > addr.size() || addr[pos+1]!=':') return -1; ourAddr.assign(addr.c_str() + 1, pos-1); - port = pdns_stou(addr.substr(pos+2)); + try { + port = pdns_stou(addr.substr(pos+2)); + } + catch(std::out_of_range) { + return -1; + } } ret->sin6_scope_id=0; ret->sin6_family=AF_INET6; @@ -761,6 +766,10 @@ int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret) freeaddrinfo(res); } + if(port > 65535) + // negative ports are found with the pdns_stou above + return -1; + if(port >= 0) ret->sin6_port = htons(port); @@ -787,6 +796,9 @@ int makeIPv4sockaddr(const std::string& str, struct sockaddr_in* ret) char *eptr = (char*)str.c_str() + str.size(); int port = strtol(str.c_str() + pos + 1, &eptr, 10); + if (port < 0 || port > 65535) + return -1; + if(*eptr) return -1; diff --git a/pdns/test-iputils_hh.cc b/pdns/test-iputils_hh.cc index adac3c7ec2..84eb5aee07 100644 --- a/pdns/test-iputils_hh.cc +++ b/pdns/test-iputils_hh.cc @@ -50,6 +50,12 @@ BOOST_AUTO_TEST_CASE(test_ComboAddress) { BOOST_CHECK(c != e); BOOST_CHECK(d != e); BOOST_CHECK(!(a != b)); + + // Verify that we don't allow invalid port numbers + BOOST_CHECK_THROW(ComboAddress("127.0.0.1:70000"), PDNSException); // Port no. too high + BOOST_CHECK_THROW(ComboAddress("127.0.0.1:-6"), PDNSException); // Port no. too low + BOOST_CHECK_THROW(ComboAddress("[::1]:70000"), PDNSException); // Port no. too high + BOOST_CHECK_THROW(ComboAddress("[::1]:-6"), PDNSException); // Port no. too low } BOOST_AUTO_TEST_CASE(test_ComboAddressCompare) {