]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Don't read potentially uninitalized memory if gethostname() failed 9114/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 17 Feb 2020 14:20:32 +0000 (15:20 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 13 May 2020 09:39:02 +0000 (11:39 +0200)
If the buffer is smaller than `HOST_NAME_MAX` (64 on Linux but up to
255 bytes in POSIX, which FreeBSD, MacOS etc honor) gethostname()
might return -1 without null-terminating the buffer, causing an
out-of-bounds read.
As we look for the first '.' using `strchr()`, replacing it with a
null byte, we also have a one-byte out-of-bounds write which might
result in a crash or, albeit very unlikely, arbitrary code execution.

pdns/auth-carbon.cc
pdns/dnsdist-carbon.cc
pdns/rec-carbon.cc

index 09f776aa7c943d62cd5fcd8c90330efa497bb330..5fd968c82cb55095b9516d3009988d3226a07803 100644 (file)
@@ -41,9 +41,11 @@ try
   string namespace_name=arg()["carbon-namespace"];
   string hostname=arg()["carbon-ourname"];
   if(hostname.empty()) {
-    char tmp[80];
+    char tmp[HOST_NAME_MAX+1];
     memset(tmp, 0, sizeof(tmp));
-    gethostname(tmp, sizeof(tmp));
+    if (gethostname(tmp, sizeof(tmp)) != 0) {
+      throw std::runtime_error("The carbon-ourname setting has not been set and we are unable to determine the system's hostname: " + stringerror());
+    }
     char *p = strchr(tmp, '.');
     if(p) *p=0;
     hostname=tmp;
index 26702dcb65db8e6320e31fc157dec8740f515920..c5c1381d0b5ac09dcfe3052a32663919304c25a1 100644 (file)
@@ -59,9 +59,11 @@ try
       const std::string& namespace_name = conf.namespace_name;
       std::string hostname = conf.ourname;
       if(hostname.empty()) {
-        char tmp[80];
+        char tmp[HOST_NAME_MAX+1];
         memset(tmp, 0, sizeof(tmp));
-        gethostname(tmp, sizeof(tmp));
+        if (gethostname(tmp, sizeof(tmp)) != 0) {
+          throw std::runtime_error("The 'ourname' setting in 'carbonServer()' has not been set and we are unable to determine the system's hostname: " + stringerror());
+        }
         char *p = strchr(tmp, '.');
         if(p) *p=0;
         hostname=tmp;
index 3eba50bd8c5bb9d48cedce4c5e3bf6fce9687ba7..ef1204d6ec1d3231742bf7df74f6835143a64b8e 100644 (file)
@@ -33,9 +33,11 @@ try
     namespace_name="pdns";
   }
   if(hostname.empty()) {
-    char tmp[80];
+    char tmp[HOST_NAME_MAX+1];
     memset(tmp, 0, sizeof(tmp));
-    gethostname(tmp, sizeof(tmp));
+    if (gethostname(tmp, sizeof(tmp)) != 0) {
+      throw std::runtime_error("The 'carbon-ourname' setting has not been set and we are unable to determine the system's hostname: " + stringerror());
+    }
     char *p = strchr(tmp, '.');
     if(p) *p=0;