d8307d
commit 6ca53a2453598804a2559a548a08424fca96434a
d8307d
Author: Florian Weimer <fweimer@redhat.com>
d8307d
Date:   Mon Jan 21 09:26:41 2019 +0100
d8307d
d8307d
    resolv: Do not send queries for non-host-names in nss_dns [BZ #24112]
d8307d
    
d8307d
    Before this commit, nss_dns would send a query which did not contain a
d8307d
    host name as the query name (such as invalid\032name.example.com) and
d8307d
    then reject the answer in getanswer_r and gaih_getanswer_slice, using
d8307d
    a check based on res_hnok.  With this commit, no query is sent, and a
d8307d
    host-not-found error is returned to NSS without network interaction.
d8307d
d8307d
diff --git a/resolv/nss_dns/dns-host.c b/resolv/nss_dns/dns-host.c
d8307d
index 5dc2829cd148a568..99c3b61e1cee4d42 100644
d8307d
--- a/resolv/nss_dns/dns-host.c
d8307d
+++ b/resolv/nss_dns/dns-host.c
d8307d
@@ -274,11 +274,26 @@ gethostbyname3_context (struct resolv_context *ctx,
d8307d
   return status;
d8307d
 }
d8307d
 
d8307d
+/* Verify that the name looks like a host name.  There is no point in
d8307d
+   sending a query which will not produce a usable name in the
d8307d
+   response.  */
d8307d
+static enum nss_status
d8307d
+check_name (const char *name, int *h_errnop)
d8307d
+{
d8307d
+  if (res_hnok (name))
d8307d
+    return NSS_STATUS_SUCCESS;
d8307d
+  *h_errnop = HOST_NOT_FOUND;
d8307d
+  return NSS_STATUS_NOTFOUND;
d8307d
+}
d8307d
+
d8307d
 enum nss_status
d8307d
 _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
d8307d
 			   char *buffer, size_t buflen, int *errnop,
d8307d
 			   int *h_errnop)
d8307d
 {
d8307d
+  enum nss_status status = check_name (name, h_errnop);
d8307d
+  if (status != NSS_STATUS_SUCCESS)
d8307d
+    return status;
d8307d
   return _nss_dns_gethostbyname3_r (name, af, result, buffer, buflen, errnop,
d8307d
 				    h_errnop, NULL, NULL);
d8307d
 }
d8307d
@@ -289,6 +304,9 @@ _nss_dns_gethostbyname_r (const char *name, struct hostent *result,
d8307d
 			  char *buffer, size_t buflen, int *errnop,
d8307d
 			  int *h_errnop)
d8307d
 {
d8307d
+  enum nss_status status = check_name (name, h_errnop);
d8307d
+  if (status != NSS_STATUS_SUCCESS)
d8307d
+    return status;
d8307d
   struct resolv_context *ctx = __resolv_context_get ();
d8307d
   if (ctx == NULL)
d8307d
     {
d8307d
@@ -296,7 +314,7 @@ _nss_dns_gethostbyname_r (const char *name, struct hostent *result,
d8307d
       *h_errnop = NETDB_INTERNAL;
d8307d
       return NSS_STATUS_UNAVAIL;
d8307d
     }
d8307d
-  enum nss_status status = NSS_STATUS_NOTFOUND;
d8307d
+  status = NSS_STATUS_NOTFOUND;
d8307d
   if (res_use_inet6 ())
d8307d
     status = gethostbyname3_context (ctx, name, AF_INET6, result, buffer,
d8307d
 				     buflen, errnop, h_errnop, NULL, NULL);
d8307d
@@ -313,6 +331,9 @@ _nss_dns_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
d8307d
 			   char *buffer, size_t buflen, int *errnop,
d8307d
 			   int *herrnop, int32_t *ttlp)
d8307d
 {
d8307d
+  enum nss_status status = check_name (name, herrnop);
d8307d
+  if (status != NSS_STATUS_SUCCESS)
d8307d
+    return status;
d8307d
   struct resolv_context *ctx = __resolv_context_get ();
d8307d
   if (ctx == NULL)
d8307d
     {
d8307d
@@ -347,7 +368,6 @@ _nss_dns_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
d8307d
   int ans2p_malloced = 0;
d8307d
 
d8307d
   int olderr = errno;
d8307d
-  enum nss_status status;
d8307d
   int n = __res_context_search (ctx, name, C_IN, T_QUERY_A_AND_AAAA,
d8307d
 				host_buffer.buf->buf, 2048, &host_buffer.ptr,
d8307d
 				&ans2p, &nans2p, &resplen2, &ans2p_malloced);