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