00db10
commit c0a25aa92b612786f4e45292c4aee1d7d47123f8
00db10
Author: Florian Weimer <fweimer@redhat.com>
00db10
Date:   Sat Nov 11 11:51:08 2017 +0100
00db10
00db10
    resolv: More precise checks in res_hnok, res_dnok [BZ #22409] [BZ #22412]
00db10
    
00db10
    res_hnok rejected some host names used on the Internet, such as
00db10
    www-.example.com.  res_hnok and res_dnok failed to perform basic syntax
00db10
    checking on DNS domain names.
00db10
    
00db10
    Also fix res_mailok, res_ownok.
00db10
00db10
diff --git a/resolv/res_comp.c b/resolv/res_comp.c
00db10
index ffb2ed59147d3680..79760e891f607daa 100644
00db10
--- a/resolv/res_comp.c
00db10
+++ b/resolv/res_comp.c
00db10
@@ -1,3 +1,21 @@
00db10
+/* Domain name processing functions.
00db10
+   Copyright (C) 1995-2017 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
 /*
00db10
  * Copyright (c) 1985, 1993
00db10
  *    The Regents of the University of California.  All rights reserved.
00db10
@@ -121,110 +139,118 @@ dn_skipname(const u_char *ptr, const u_char *eom) {
00db10
 }
00db10
 libresolv_hidden_def (dn_skipname)
00db10
 
00db10
-/*
00db10
- * Verify that a domain name uses an acceptable character set.
00db10
- */
00db10
+/* Return true if the string consists of printable ASCII characters
00db10
+   only.  */
00db10
+static bool
00db10
+printable_string (const char *dn)
00db10
+{
00db10
+  while (true)
00db10
+    {
00db10
+      char ch = *dn;
00db10
+      if (ch == '\0')
00db10
+	return true;
00db10
+      if (ch <= ' ' || ch > '~')
00db10
+	return false;
00db10
+      ++dn;
00db10
+    }
00db10
+}
00db10
 
00db10
-/*
00db10
- * Note the conspicuous absence of ctype macros in these definitions.  On
00db10
- * non-ASCII hosts, we can't depend on string literals or ctype macros to
00db10
- * tell us anything about network-format data.  The rest of the BIND system
00db10
- * is not careful about this, but for some reason, we're doing it right here.
00db10
- */
00db10
-#define PERIOD 0x2e
00db10
-#define	hyphenchar(c) ((c) == 0x2d)
00db10
-#define	underscorechar(c) ((c) == 0x5f)
00db10
-#define bslashchar(c) ((c) == 0x5c)
00db10
-#define periodchar(c) ((c) == PERIOD)
00db10
-#define asterchar(c) ((c) == 0x2a)
00db10
-#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
00db10
-		   || ((c) >= 0x61 && (c) <= 0x7a))
00db10
-#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
00db10
-
00db10
-#define borderchar(c) (alphachar(c) || digitchar(c))
00db10
-#define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c))
00db10
-#define	domainchar(c) ((c) > 0x20 && (c) < 0x7f)
00db10
+/* Return true if DN points to a name consisting only of [0-9a-zA-Z_-]
00db10
+   characters.  DN must be in DNS wire format, without
00db10
+   compression.  */
00db10
+static bool
00db10
+binary_hnok (const unsigned char *dn)
00db10
+{
00db10
+  while (true)
00db10
+    {
00db10
+      size_t label_length = *dn;
00db10
+      if (label_length == 0)
00db10
+	break;
00db10
+      ++dn;
00db10
+      const unsigned char *label_end = dn + label_length;
00db10
+      do
00db10
+	{
00db10
+	  unsigned char ch = *dn;
00db10
+	  if (!(('0' <= ch && ch <= '9')
00db10
+		|| ('A' <= ch && ch <= 'Z')
00db10
+		|| ('a' <= ch && ch <= 'z')
00db10
+		|| ch == '-' || ch == '_'))
00db10
+	    return false;
00db10
+	  ++dn;
00db10
+	}
00db10
+      while (dn < label_end);
00db10
+    }
00db10
+  return true;
00db10
+}
00db10
+
00db10
+/* Return true if the binary domain name has a first labels which
00db10
+   starts with '-'.  */
00db10
+static inline bool
00db10
+binary_leading_dash (const unsigned char *dn)
00db10
+{
00db10
+  return dn[0] > 0 && dn[1] == '-';
00db10
+}
00db10
 
00db10
+/* Return 1 if res_hnok is a valid host name.  Labels must only
00db10
+   contain [0-9a-zA-Z_-] characters, and the name must not start with
00db10
+   a '-'.  The latter is to avoid confusion with program options.  */
00db10
 int
00db10
-res_hnok(const char *dn) {
00db10
-	int pch = PERIOD, ch = *dn++;
00db10
-
00db10
-	while (ch != '\0') {
00db10
-		int nch = *dn++;
00db10
-
00db10
-		if (periodchar(ch)) {
00db10
-			(void)NULL;
00db10
-		} else if (periodchar(pch)) {
00db10
-			if (!borderchar(ch))
00db10
-				return (0);
00db10
-		} else if (periodchar(nch) || nch == '\0') {
00db10
-			if (!borderchar(ch))
00db10
-				return (0);
00db10
-		} else {
00db10
-			if (!middlechar(ch))
00db10
-				return (0);
00db10
-		}
00db10
-		pch = ch, ch = nch;
00db10
-	}
00db10
-	return (1);
00db10
+res_hnok (const char *dn)
00db10
+{
00db10
+  unsigned char buf[NS_MAXCDNAME];
00db10
+  if (!printable_string (dn)
00db10
+      || ns_name_pton (dn, buf, sizeof (buf)) < 0
00db10
+      || binary_leading_dash (buf))
00db10
+    return 0;
00db10
+  return binary_hnok (buf);
00db10
 }
00db10
 libresolv_hidden_def (res_hnok)
00db10
 
00db10
-/*
00db10
- * hostname-like (A, MX, WKS) owners can have "*" as their first label
00db10
- * but must otherwise be as a host name.
00db10
- */
00db10
+/* Hostname-like (A, MX, WKS) owners can have "*" as their first label
00db10
+   but must otherwise be as a host name.  */
00db10
 int
00db10
-res_ownok(const char *dn) {
00db10
-	if (asterchar(dn[0])) {
00db10
-		if (periodchar(dn[1]))
00db10
-			return (res_hnok(dn+2));
00db10
-		if (dn[1] == '\0')
00db10
-			return (1);
00db10
-	}
00db10
-	return (res_hnok(dn));
00db10
+res_ownok (const char *dn)
00db10
+{
00db10
+  unsigned char buf[NS_MAXCDNAME];
00db10
+  if (!printable_string (dn)
00db10
+      || ns_name_pton (dn, buf, sizeof (buf)) < 0
00db10
+      || binary_leading_dash (buf))
00db10
+    return 0;
00db10
+  if (buf[0] == 1 && buf [1] == '*')
00db10
+    /* Skip over the leading "*." part.  */
00db10
+    return binary_hnok (buf + 2);
00db10
+  else
00db10
+    return binary_hnok (buf);
00db10
 }
00db10
 
00db10
-/*
00db10
- * SOA RNAMEs and RP RNAMEs can have any printable character in their first
00db10
- * label, but the rest of the name has to look like a host name.
00db10
- */
00db10
+/* SOA RNAMEs and RP RNAMEs can have any byte in their first label,
00db10
+   but the rest of the name has to look like a host name.  */
00db10
 int
00db10
-res_mailok(const char *dn) {
00db10
-	int ch, escaped = 0;
00db10
-
00db10
-	/* "." is a valid missing representation */
00db10
-	if (*dn == '\0')
00db10
-		return (1);
00db10
-
00db10
-	/* otherwise <label>.<hostname> */
00db10
-	while ((ch = *dn++) != '\0') {
00db10
-		if (!domainchar(ch))
00db10
-			return (0);
00db10
-		if (!escaped && periodchar(ch))
00db10
-			break;
00db10
-		if (escaped)
00db10
-			escaped = 0;
00db10
-		else if (bslashchar(ch))
00db10
-			escaped = 1;
00db10
-	}
00db10
-	if (periodchar(ch))
00db10
-		return (res_hnok(dn));
00db10
-	return (0);
00db10
+res_mailok (const char *dn)
00db10
+{
00db10
+  unsigned char buf[NS_MAXCDNAME];
00db10
+  if (!printable_string (dn)
00db10
+      || ns_name_pton (dn, buf, sizeof (buf)) < 0)
00db10
+    return 0;
00db10
+  unsigned char label_length = buf[0];
00db10
+  /* "." is a valid missing representation */
00db10
+  if (label_length == 0)
00db10
+    return 1;
00db10
+  /* Skip over the first label.  */
00db10
+  unsigned char *tail = buf + 1 + label_length;
00db10
+  if (*tail == 0)
00db10
+    /* More than one label is required (except for ".").  */
00db10
+    return 0;
00db10
+  return binary_hnok (tail);
00db10
 }
00db10
 
00db10
-/*
00db10
- * This function is quite liberal, since RFC 1034's character sets are only
00db10
- * recommendations.
00db10
- */
00db10
+/* Return 1 if DN is a syntactically valid domain name.  Empty names
00db10
+   are accepted.  */
00db10
 int
00db10
-res_dnok(const char *dn) {
00db10
-	int ch;
00db10
-
00db10
-	while ((ch = *dn++) != '\0')
00db10
-		if (!domainchar(ch))
00db10
-			return (0);
00db10
-	return (1);
00db10
+res_dnok (const char *dn)
00db10
+{
00db10
+  unsigned char buf[NS_MAXCDNAME];
00db10
+  return printable_string (dn) && ns_name_pton (dn, buf, sizeof (buf)) >= 0;
00db10
 }
00db10
 libresolv_hidden_def (res_dnok)
00db10
 
00db10
diff --git a/resolv/tst-res_hnok.c b/resolv/tst-res_hnok.c
00db10
index 9c923038218e965c..314477a2ce2661c0 100644
00db10
--- a/resolv/tst-res_hnok.c
00db10
+++ b/resolv/tst-res_hnok.c
00db10
@@ -51,19 +51,31 @@ static const struct test_case tests[] =
00db10
   {
00db10
     { "", allok },
00db10
     { ".", allok },
00db10
+    { "..", 0 },
00db10
     { "www", allnomailok },
00db10
+    { "www.", allnomailok },
00db10
     { "example", allnomailok },
00db10
     { "example.com", allok },
00db10
     { "www.example.com", allok },
00db10
     { "www.example.com.", allok },
00db10
+    { "www-.example.com.", allok },
00db10
+    { "www.-example.com.", allok },
00db10
     { "*.example.com", dnok | mailok | ownok },
00db10
     { "-v", dnok },
00db10
     { "-v.example.com", mailok | dnok },
00db10
     { "**.example.com", dnok | mailok },
00db10
+    { "www.example.com\\", 0 },
00db10
     { STRING63, allnomailok },
00db10
+    { STRING63 ".", allnomailok },
00db10
+    { STRING63 "\\.", 0 },
00db10
+    { STRING63 "z", 0 },
00db10
     { STRING63 ".example.com", allok },
00db10
     { STRING63 "." STRING63 "." STRING63 "." STRING60 "z", allok },
00db10
+    { STRING63 "." STRING63 "." STRING63 "." STRING60 "z.", allok },
00db10
+    { STRING63 "." STRING63 "." STRING63 "." STRING60 "zz", 0 },
00db10
+    { STRING63 "." STRING63 "." STRING63 "." STRING60 "zzz", 0 },
00db10
     { "hostmaster@mail.example.com", dnok | mailok },
00db10
+    { "hostmaster\\@mail.example.com", dnok | mailok },
00db10
     { "with whitespace", 0 },
00db10
     { "with\twhitespace", 0 },
00db10
     { "with\nwhitespace", 0 },
00db10
@@ -116,6 +128,12 @@ one_char (const char *prefix, const char *accepted, const char *suffix,
00db10
     }
00db10
 }
00db10
 
00db10
+#define LETTERSDIGITS \
00db10
+  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
00db10
+
00db10
+#define PRINTABLE \
00db10
+  "!\"#$%&'()*+,/:;<=>?@[\\]^`{|}~"
00db10
+
00db10
 static int
00db10
 do_test (void)
00db10
 {
00db10
@@ -131,20 +149,18 @@ do_test (void)
00db10
     }
00db10
 
00db10
   one_char
00db10
-    ("", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.",
00db10
-     "", "res_hnok", res_hnok);
00db10
+    ("", LETTERSDIGITS "._", "", "res_hnok", res_hnok);
00db10
   one_char
00db10
     ("middle",
00db10
-     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_",
00db10
+     LETTERSDIGITS ".-_\\", /* "middle\\suffix" == "middlesuffix", so good.  */
00db10
      "suffix", "res_hnok", res_hnok);
00db10
   one_char
00db10
     ("middle",
00db10
-     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_"
00db10
-     "!\"#$%&'()*+,/:;<=>?@[\\]^`{|}~",
00db10
+     LETTERSDIGITS ".-_" PRINTABLE,
00db10
      "suffix.example", "res_mailok", res_mailok);
00db10
   one_char
00db10
     ("mailbox.middle",
00db10
-     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_",
00db10
+     LETTERSDIGITS ".-_\\",
00db10
      "suffix.example", "res_mailok", res_mailok);
00db10
 
00db10
   return 0;