]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
ComboAddress: don't allow invalid ports 4387/head
authorPieter Lexis <pieter.lexis@powerdns.com>
Wed, 31 Aug 2016 13:30:30 +0000 (15:30 +0200)
committerPieter Lexis <pieter.lexis@powerdns.com>
Wed, 31 Aug 2016 13:44:38 +0000 (15:44 +0200)
Add tests for this.

Fixes: #4382
pdns/misc.cc
pdns/test-iputils_hh.cc

index 43e3eb0eec2108eef07d82b0e05403f6784c5767..ddfbc7b641bb2a9ee34149cc51e791c7bf2fbab5 100644 (file)
@@ -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;
 
index adac3c7ec2aa4496c844c40ac1c6d3b2d0634235..84eb5aee07d91470355fa3c1c61fabf52396fbbe 100644 (file)
@@ -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) {