1feee8
commit c288e032ae107c48679ef3c46fb84af6de0a6baf
1feee8
Author: Florian Weimer <fweimer@redhat.com>
1feee8
Date:   Tue Aug 30 10:02:49 2022 +0200
1feee8
1feee8
    resolv: Add internal __ns_name_length_uncompressed function
1feee8
    
1feee8
    This function is useful for checking that the question name is
1feee8
    uncompressed (as it should be).
1feee8
    
1feee8
    Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
1feee8
    (cherry picked from commit 78b1a4f0e49064e5dfb686c7cd87bd4df2640b29)
1feee8
1feee8
diff --git a/include/arpa/nameser.h b/include/arpa/nameser.h
1feee8
index bb1dede187cf1500..6e4808f00d60caf9 100644
1feee8
--- a/include/arpa/nameser.h
1feee8
+++ b/include/arpa/nameser.h
1feee8
@@ -95,5 +95,13 @@ libc_hidden_proto (__ns_name_unpack)
1feee8
 extern __typeof (ns_samename) __libc_ns_samename;
1feee8
 libc_hidden_proto (__libc_ns_samename)
1feee8
 
1feee8
+/* Packet parser helper functions.  */
1feee8
+
1feee8
+/* Verify that P points to an uncompressed domain name in wire format.
1feee8
+   On success, return the length of the encoded name, including the
1feee8
+   terminating null byte.  On failure, return -1 and set errno.  EOM
1feee8
+   must point one past the last byte in the packet.  */
1feee8
+int __ns_name_length_uncompressed (const unsigned char *p,
1feee8
+				   const unsigned char *eom) attribute_hidden;
1feee8
 # endif /* !_ISOMAC */
1feee8
 #endif
1feee8
diff --git a/resolv/Makefile b/resolv/Makefile
1feee8
index 0b4fa30716af3b8a..308f18622a04965a 100644
1feee8
--- a/resolv/Makefile
1feee8
+++ b/resolv/Makefile
1feee8
@@ -40,6 +40,7 @@ routines := \
1feee8
   inet_pton \
1feee8
   ns_makecanon \
1feee8
   ns_name_compress \
1feee8
+  ns_name_length_uncompressed \
1feee8
   ns_name_ntop \
1feee8
   ns_name_pack \
1feee8
   ns_name_pton \
1feee8
@@ -112,6 +113,10 @@ tests-static += tst-resolv-txnid-collision
1feee8
 tests-internal += tst-ns_samebinaryname
1feee8
 tests-static += tst-ns_samebinaryname
1feee8
 
1feee8
+# Likewise for __ns_name_length_uncompressed.
1feee8
+tests-internal += tst-ns_name_length_uncompressed
1feee8
+tests-static += tst-ns_name_length_uncompressed
1feee8
+
1feee8
 # These tests need libdl.
1feee8
 ifeq (yes,$(build-shared))
1feee8
 tests += \
1feee8
diff --git a/resolv/ns_name_length_uncompressed.c b/resolv/ns_name_length_uncompressed.c
1feee8
new file mode 100644
1feee8
index 0000000000000000..51296b47efbf1849
1feee8
--- /dev/null
1feee8
+++ b/resolv/ns_name_length_uncompressed.c
1feee8
@@ -0,0 +1,72 @@
1feee8
+/* Skip over an uncompressed name in wire format.
1feee8
+   Copyright (C) 2022 Free Software Foundation, Inc.
1feee8
+   This file is part of the GNU C Library.
1feee8
+
1feee8
+   The GNU C Library is free software; you can redistribute it and/or
1feee8
+   modify it under the terms of the GNU Lesser General Public
1feee8
+   License as published by the Free Software Foundation; either
1feee8
+   version 2.1 of the License, or (at your option) any later version.
1feee8
+
1feee8
+   The GNU C Library is distributed in the hope that it will be useful,
1feee8
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
1feee8
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1feee8
+   Lesser General Public License for more details.
1feee8
+
1feee8
+   You should have received a copy of the GNU Lesser General Public
1feee8
+   License along with the GNU C Library; if not, see
1feee8
+   <https://www.gnu.org/licenses/>.  */
1feee8
+
1feee8
+#include <arpa/nameser.h>
1feee8
+#include <errno.h>
1feee8
+#include <stdbool.h>
1feee8
+
1feee8
+int
1feee8
+__ns_name_length_uncompressed (const unsigned char *p,
1feee8
+                                const unsigned char *eom)
1feee8
+{
1feee8
+  const unsigned char *start = p;
1feee8
+
1feee8
+  while (true)
1feee8
+    {
1feee8
+      if (p == eom)
1feee8
+        {
1feee8
+          /* Truncated packet: no room for label length.  */
1feee8
+          __set_errno (EMSGSIZE);
1feee8
+          return -1;
1feee8
+        }
1feee8
+
1feee8
+      unsigned char b = *p;
1feee8
+      ++p;
1feee8
+      if (b == 0)
1feee8
+        {
1feee8
+          /* Root label.  */
1feee8
+          size_t length = p - start;
1feee8
+          if (length > NS_MAXCDNAME)
1feee8
+            {
1feee8
+              /* Domain name too long.  */
1feee8
+              __set_errno (EMSGSIZE);
1feee8
+              return -1;
1feee8
+            }
1feee8
+          return length;
1feee8
+        }
1feee8
+
1feee8
+      if (b <= 63)
1feee8
+        {
1feee8
+          /* Regular label.  */
1feee8
+          if (b <= eom - p)
1feee8
+            p += b;
1feee8
+          else
1feee8
+            {
1feee8
+              /* Truncated packet: label incomplete.  */
1feee8
+              __set_errno (EMSGSIZE);
1feee8
+              return -1;
1feee8
+            }
1feee8
+        }
1feee8
+      else
1feee8
+        {
1feee8
+          /* Compression reference or corrupted label length.  */
1feee8
+          __set_errno (EMSGSIZE);
1feee8
+          return -1;
1feee8
+        }
1feee8
+    }
1feee8
+}
1feee8
diff --git a/resolv/tst-ns_name_length_uncompressed.c b/resolv/tst-ns_name_length_uncompressed.c
1feee8
new file mode 100644
1feee8
index 0000000000000000..c4a2904db75d1221
1feee8
--- /dev/null
1feee8
+++ b/resolv/tst-ns_name_length_uncompressed.c
1feee8
@@ -0,0 +1,135 @@
1feee8
+/* Test __ns_name_length_uncompressed.
1feee8
+   Copyright (C) 2022 Free Software Foundation, Inc.
1feee8
+   This file is part of the GNU C Library.
1feee8
+
1feee8
+   The GNU C Library is free software; you can redistribute it and/or
1feee8
+   modify it under the terms of the GNU Lesser General Public
1feee8
+   License as published by the Free Software Foundation; either
1feee8
+   version 2.1 of the License, or (at your option) any later version.
1feee8
+
1feee8
+   The GNU C Library is distributed in the hope that it will be useful,
1feee8
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
1feee8
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1feee8
+   Lesser General Public License for more details.
1feee8
+
1feee8
+   You should have received a copy of the GNU Lesser General Public
1feee8
+   License along with the GNU C Library; if not, see
1feee8
+   <https://www.gnu.org/licenses/>.  */
1feee8
+
1feee8
+#include <arpa/nameser.h>
1feee8
+#include <array_length.h>
1feee8
+#include <errno.h>
1feee8
+#include <stdio.h>
1feee8
+#include <support/check.h>
1feee8
+#include <support/next_to_fault.h>
1feee8
+
1feee8
+/* Reference implementation based on other building blocks.  */
1feee8
+static int
1feee8
+reference_length (const unsigned char *p, const unsigned char *eom)
1feee8
+{
1feee8
+  unsigned char buf[NS_MAXCDNAME];
1feee8
+  int n = __ns_name_unpack (p, eom, p, buf, sizeof (buf));
1feee8
+  if (n < 0)
1feee8
+    return n;
1feee8
+  const unsigned char *q = buf;
1feee8
+  if (__ns_name_skip (&q, array_end (buf)) < 0)
1feee8
+    return -1;
1feee8
+  if (q - buf != n)
1feee8
+    /* Compressed name.  */
1feee8
+    return -1;
1feee8
+  return n;
1feee8
+}
1feee8
+
1feee8
+static int
1feee8
+do_test (void)
1feee8
+{
1feee8
+  {
1feee8
+    unsigned char buf[] = { 3, 'w', 'w', 'w', 0, 0, 0 };
1feee8
+    TEST_COMPARE (reference_length (buf, array_end (buf)), sizeof (buf) - 2);
1feee8
+    TEST_COMPARE (__ns_name_length_uncompressed (buf, array_end (buf)),
1feee8
+                  sizeof (buf) - 2);
1feee8
+    TEST_COMPARE (reference_length (array_end (buf) - 1, array_end (buf)), 1);
1feee8
+    TEST_COMPARE (__ns_name_length_uncompressed (array_end (buf) - 1,
1feee8
+                                                 array_end (buf)), 1);
1feee8
+    buf[4]  = 0xc0;             /* Forward compression reference.  */
1feee8
+    buf[5]  = 0x06;
1feee8
+    TEST_COMPARE (reference_length (buf, array_end (buf)), -1);
1feee8
+    TEST_COMPARE (__ns_name_length_uncompressed (buf, array_end (buf)), -1);
1feee8
+  }
1feee8
+
1feee8
+  struct support_next_to_fault ntf = support_next_to_fault_allocate (300);
1feee8
+
1feee8
+  /* Buffer region with all possible bytes at start and end.  */
1feee8
+  for (int length = 1; length <= 300; ++length)
1feee8
+    {
1feee8
+      unsigned char *end = (unsigned char *) ntf.buffer + ntf.length;
1feee8
+      unsigned char *start = end - length;
1feee8
+      memset (start, 'X', length);
1feee8
+      for (int first = 0; first <= 255; ++first)
1feee8
+        {
1feee8
+          *start = first;
1feee8
+          for (int last = 0; last <= 255; ++last)
1feee8
+            {
1feee8
+              start[length - 1] = last;
1feee8
+              TEST_COMPARE (reference_length (start, end),
1feee8
+                            __ns_name_length_uncompressed (start, end));
1feee8
+            }
1feee8
+        }
1feee8
+    }
1feee8
+
1feee8
+  /* Poor man's fuzz testing: patch two bytes.   */
1feee8
+  {
1feee8
+    unsigned char ref[] =
1feee8
+      {
1feee8
+        7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'n', 'e', 't', 0, 0, 0
1feee8
+      };
1feee8
+    TEST_COMPARE (reference_length (ref, array_end (ref)), 13);
1feee8
+    TEST_COMPARE (__ns_name_length_uncompressed (ref, array_end (ref)), 13);
1feee8
+
1feee8
+    int good = 0;
1feee8
+    int bad = 0;
1feee8
+    for (int length = 1; length <= sizeof (ref); ++length)
1feee8
+      {
1feee8
+        unsigned char *end = (unsigned char *) ntf.buffer + ntf.length;
1feee8
+        unsigned char *start = end - length;
1feee8
+        memcpy (start, ref, length);
1feee8
+
1feee8
+        for (int patch1_pos = 0; patch1_pos < length; ++patch1_pos)
1feee8
+          {
1feee8
+            for (int patch1_value = 0; patch1_value <= 255; ++patch1_value)
1feee8
+              {
1feee8
+                start[patch1_pos] = patch1_value;
1feee8
+                for (int patch2_pos = 0; patch2_pos < length; ++patch2_pos)
1feee8
+                  {
1feee8
+                    for (int patch2_value = 0; patch2_value <= 255;
1feee8
+                         ++patch2_value)
1feee8
+                      {
1feee8
+                        start[patch2_pos] = patch2_value;
1feee8
+                        int expected = reference_length (start, end);
1feee8
+                        errno = EINVAL;
1feee8
+                        int actual
1feee8
+                          =  __ns_name_length_uncompressed (start, end);
1feee8
+                        if (actual > 0)
1feee8
+                          ++good;
1feee8
+                        else
1feee8
+                          {
1feee8
+                            TEST_COMPARE (errno, EMSGSIZE);
1feee8
+                            ++bad;
1feee8
+                          }
1feee8
+                        TEST_COMPARE (expected, actual);
1feee8
+                      }
1feee8
+                    start[patch2_pos] = ref[patch2_pos];
1feee8
+                  }
1feee8
+              }
1feee8
+            start[patch1_pos] = ref[patch1_pos];
1feee8
+          }
1feee8
+      }
1feee8
+    printf ("info: patched inputs with success: %d\n", good);
1feee8
+    printf ("info: patched inputs with failure: %d\n", bad);
1feee8
+  }
1feee8
+
1feee8
+  support_next_to_fault_free (&ntf;;
1feee8
+  return 0;
1feee8
+}
1feee8
+
1feee8
+#include <support/test-driver.c>