e67796
Avoid UAF in getcanonname (CVE-2023-4806)
e67796
e67796
When an NSS plugin only implements the _gethostbyname2_r and
e67796
_getcanonname_r callbacks, getaddrinfo could use memory that was freed
e67796
during tmpbuf resizing, through h_name in a previous query response.
e67796
e67796
The backing store for res->at->name when doing a query with
e67796
gethostbyname3_r or gethostbyname2_r is tmpbuf, which is reallocated in
e67796
gethosts during the query.  For AF_INET6 lookup with AI_ALL |
e67796
AI_V4MAPPED, gethosts gets called twice, once for a v6 lookup and second
e67796
for a v4 lookup.  In this case, if the first call reallocates tmpbuf
e67796
enough number of times, resulting in a malloc, th->h_name (that
e67796
res->at->name refers to) ends up on a heap allocated storage in tmpbuf.
e67796
Now if the second call to gethosts also causes the plugin callback to
e67796
return NSS_STATUS_TRYAGAIN, tmpbuf will get freed, resulting in a UAF
e67796
reference in res->at->name.  This then gets dereferenced in the
e67796
getcanonname_r plugin call, resulting in the use after free.
e67796
e67796
Fix this by copying h_name over and freeing it at the end.  This
e67796
resolves BZ #30843, which is assigned CVE-2023-4806.  This is a minimal
e67796
RHEL-8-specific fix.  Test case differences from upstream:
e67796
e67796
- The test module needs to explicitly link against libnss_files on
e67796
  RHEL-8; upstream libnss_files is built into libc.so.
e67796
e67796
- Test module code was adapted to not use the upstream NSS module
e67796
  convenience macros.
e67796
e67796
This change is adapted from the following commit from upstream:
e67796
e67796
commit 973fe93a5675c42798b2161c6f29c01b0e243994
e67796
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
e67796
Date:   Fri Sep 15 13:51:12 2023 -0400
e67796
e67796
    getaddrinfo: Fix use after free in getcanonname (CVE-2023-4806)
e67796
    
e67796
    When an NSS plugin only implements the _gethostbyname2_r and
e67796
    _getcanonname_r callbacks, getaddrinfo could use memory that was freed
e67796
    during tmpbuf resizing, through h_name in a previous query response.
e67796
    
e67796
    The backing store for res->at->name when doing a query with
e67796
    gethostbyname3_r or gethostbyname2_r is tmpbuf, which is reallocated in
e67796
    gethosts during the query.  For AF_INET6 lookup with AI_ALL |
e67796
    AI_V4MAPPED, gethosts gets called twice, once for a v6 lookup and second
e67796
    for a v4 lookup.  In this case, if the first call reallocates tmpbuf
e67796
    enough number of times, resulting in a malloc, th->h_name (that
e67796
    res->at->name refers to) ends up on a heap allocated storage in tmpbuf.
e67796
    Now if the second call to gethosts also causes the plugin callback to
e67796
    return NSS_STATUS_TRYAGAIN, tmpbuf will get freed, resulting in a UAF
e67796
    reference in res->at->name.  This then gets dereferenced in the
e67796
    getcanonname_r plugin call, resulting in the use after free.
e67796
    
e67796
    Fix this by copying h_name over and freeing it at the end.  This
e67796
    resolves BZ #30843, which is assigned CVE-2023-4806.
e67796
    
e67796
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
e67796
e67796
diff --git a/nss/Makefile b/nss/Makefile
e67796
index cfb255c6e7a3a4de..5829a2539306ddb5 100644
e67796
--- a/nss/Makefile
e67796
+++ b/nss/Makefile
e67796
@@ -66,7 +66,8 @@ xtests			= bug-erange
e67796
 tests-container = \
e67796
 			  tst-nss-db-endpwent \
e67796
 			  tst-nss-db-endgrent \
e67796
-			  tst-nss-gai-actions
e67796
+			  tst-nss-gai-actions \
e67796
+			  tst-nss-gai-hv2-canonname
e67796
 
e67796
 # Tests which need libdl
e67796
 ifeq (yes,$(build-shared))
e67796
@@ -132,7 +133,8 @@ routines                += $(libnss_files-routines)
e67796
 static-only-routines    += $(libnss_files-routines)
e67796
 tests-static		+= tst-nss-static
e67796
 endif
e67796
-extra-test-objs		+= nss_test1.os nss_test2.os nss_test_errno.os
e67796
+extra-test-objs		+= nss_test1.os nss_test2.os nss_test_errno.os \
e67796
+			   nss_test_gai_hv2_canonname.os
e67796
 
e67796
 include ../Rules
e67796
 
e67796
@@ -169,12 +171,17 @@ rtld-tests-LDFLAGS += -Wl,--dynamic-list=nss_test.ver
e67796
 libof-nss_test1 = extramodules
e67796
 libof-nss_test2 = extramodules
e67796
 libof-nss_test_errno = extramodules
e67796
+libof-nss_test_gai_hv2_canonname = extramodules
e67796
 $(objpfx)/libnss_test1.so: $(objpfx)nss_test1.os $(link-libc-deps)
e67796
 	$(build-module)
e67796
 $(objpfx)/libnss_test2.so: $(objpfx)nss_test2.os $(link-libc-deps)
e67796
 	$(build-module)
e67796
 $(objpfx)/libnss_test_errno.so: $(objpfx)nss_test_errno.os $(link-libc-deps)
e67796
 	$(build-module)
e67796
+$(objpfx)/libnss_test_gai_hv2_canonname.so: \
e67796
+  $(objpfx)nss_test_gai_hv2_canonname.os $(link-libc-deps) \
e67796
+  $(objpfx)/libnss_files.so
e67796
+	$(build-module)
e67796
 $(objpfx)nss_test2.os : nss_test1.c
e67796
 ifdef libnss_test1.so-version
e67796
 $(objpfx)/libnss_test1.so$(libnss_test1.so-version): $(objpfx)/libnss_test1.so
e67796
@@ -187,10 +194,14 @@ endif
e67796
 $(objpfx)/libnss_test_errno.so$(libnss_files.so-version): \
e67796
   $(objpfx)/libnss_test_errno.so
e67796
 	$(make-link)
e67796
+$(objpfx)/libnss_test_gai_hv2_canonname.so$(libnss_files.so-version): \
e67796
+  $(objpfx)/libnss_test_gai_hv2_canonname.so
e67796
+	$(make-link)
e67796
 $(patsubst %,$(objpfx)%.out,$(tests)) : \
e67796
 	$(objpfx)/libnss_test1.so$(libnss_test1.so-version) \
e67796
 	$(objpfx)/libnss_test2.so$(libnss_test2.so-version) \
e67796
-	$(objpfx)/libnss_test_errno.so$(libnss_files.so-version)
e67796
+	$(objpfx)/libnss_test_errno.so$(libnss_files.so-version) \
e67796
+	$(objpfx)/libnss_test_gai_hv2_canonname.so$(libnss_files.so-version)
e67796
 
e67796
 ifeq (yes,$(have-thread-library))
e67796
 $(objpfx)tst-cancel-getpwuid_r: $(shared-thread-library)
e67796
diff --git a/nss/nss_test_gai_hv2_canonname.c b/nss/nss_test_gai_hv2_canonname.c
e67796
new file mode 100644
e67796
index 0000000000000000..4195d7d24fdd5f6d
e67796
--- /dev/null
e67796
+++ b/nss/nss_test_gai_hv2_canonname.c
e67796
@@ -0,0 +1,64 @@
e67796
+/* NSS service provider that only provides gethostbyname2_r.
e67796
+   Copyright The GNU Toolchain Authors.
e67796
+   This file is part of the GNU C Library.
e67796
+
e67796
+   The GNU C Library is free software; you can redistribute it and/or
e67796
+   modify it under the terms of the GNU Lesser General Public
e67796
+   License as published by the Free Software Foundation; either
e67796
+   version 2.1 of the License, or (at your option) any later version.
e67796
+
e67796
+   The GNU C Library is distributed in the hope that it will be useful,
e67796
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e67796
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e67796
+   Lesser General Public License for more details.
e67796
+
e67796
+   You should have received a copy of the GNU Lesser General Public
e67796
+   License along with the GNU C Library; if not, see
e67796
+   <https://www.gnu.org/licenses/>.  */
e67796
+
e67796
+#include <netdb.h>
e67796
+#include <nss.h>
e67796
+#include <stdlib.h>
e67796
+#include <string.h>
e67796
+#include "nss/tst-nss-gai-hv2-canonname.h"
e67796
+
e67796
+/* Catch misnamed and functions.  */
e67796
+#pragma GCC diagnostic error "-Wmissing-prototypes"
e67796
+
e67796
+extern enum nss_status _nss_files_gethostbyname2_r (const char *, int,
e67796
+						    struct hostent *, char *,
e67796
+						    size_t, int *, int *);
e67796
+
e67796
+enum nss_status
e67796
+_nss_test_gai_hv2_canonname_gethostbyname2_r (const char *, int, struct hostent
e67796
+					      *, char *, size_t, int *, int *);
e67796
+
e67796
+enum nss_status
e67796
+_nss_test_gai_hv2_canonname_getcanonname_r (const char *, char *, size_t, char
e67796
+					    **, int *, int *);
e67796
+
e67796
+enum nss_status
e67796
+_nss_test_gai_hv2_canonname_gethostbyname2_r (const char *name, int af,
e67796
+					      struct hostent *result,
e67796
+					      char *buffer, size_t buflen,
e67796
+					      int *errnop, int *herrnop)
e67796
+{
e67796
+  return _nss_files_gethostbyname2_r (name, af, result, buffer, buflen, errnop,
e67796
+				      herrnop);
e67796
+}
e67796
+
e67796
+enum nss_status
e67796
+_nss_test_gai_hv2_canonname_getcanonname_r (const char *name, char *buffer,
e67796
+					    size_t buflen, char **result,
e67796
+					    int *errnop, int *h_errnop)
e67796
+{
e67796
+  /* We expect QUERYNAME, which is a small enough string that it shouldn't fail
e67796
+     the test.  */
e67796
+  if (memcmp (QUERYNAME, name, sizeof (QUERYNAME))
e67796
+      || buflen < sizeof (QUERYNAME))
e67796
+    abort ();
e67796
+
e67796
+  strncpy (buffer, name, buflen);
e67796
+  *result = buffer;
e67796
+  return NSS_STATUS_SUCCESS;
e67796
+}
e67796
diff --git a/nss/tst-nss-gai-hv2-canonname.c b/nss/tst-nss-gai-hv2-canonname.c
e67796
new file mode 100644
e67796
index 0000000000000000..d5f10c07d6a90773
e67796
--- /dev/null
e67796
+++ b/nss/tst-nss-gai-hv2-canonname.c
e67796
@@ -0,0 +1,63 @@
e67796
+/* Test NSS query path for plugins that only implement gethostbyname2
e67796
+   (#30843).
e67796
+   Copyright The GNU Toolchain Authors.
e67796
+   This file is part of the GNU C Library.
e67796
+
e67796
+   The GNU C Library is free software; you can redistribute it and/or
e67796
+   modify it under the terms of the GNU Lesser General Public
e67796
+   License as published by the Free Software Foundation; either
e67796
+   version 2.1 of the License, or (at your option) any later version.
e67796
+
e67796
+   The GNU C Library is distributed in the hope that it will be useful,
e67796
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e67796
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e67796
+   Lesser General Public License for more details.
e67796
+
e67796
+   You should have received a copy of the GNU Lesser General Public
e67796
+   License along with the GNU C Library; if not, see
e67796
+   <https://www.gnu.org/licenses/>.  */
e67796
+
e67796
+#include <nss.h>
e67796
+#include <netdb.h>
e67796
+#include <stdlib.h>
e67796
+#include <string.h>
e67796
+#include <support/check.h>
e67796
+#include <support/xstdio.h>
e67796
+#include "nss/tst-nss-gai-hv2-canonname.h"
e67796
+
e67796
+#define PREPARE do_prepare
e67796
+
e67796
+static void do_prepare (int a, char **av)
e67796
+{
e67796
+  FILE *hosts = xfopen ("/etc/hosts", "w");
e67796
+  for (unsigned i = 2; i < 255; i++)
e67796
+    {
e67796
+      fprintf (hosts, "ff01::ff02:ff03:%u:2\ttest.example.com\n", i);
e67796
+      fprintf (hosts, "192.168.0.%u\ttest.example.com\n", i);
e67796
+    }
e67796
+  xfclose (hosts);
e67796
+}
e67796
+
e67796
+static int
e67796
+do_test (void)
e67796
+{
e67796
+  __nss_configure_lookup ("hosts", "test_gai_hv2_canonname");
e67796
+
e67796
+  struct addrinfo hints = {};
e67796
+  struct addrinfo *result = NULL;
e67796
+
e67796
+  hints.ai_family = AF_INET6;
e67796
+  hints.ai_flags = AI_ALL | AI_V4MAPPED | AI_CANONNAME;
e67796
+
e67796
+  int ret = getaddrinfo (QUERYNAME, NULL, &hints, &result);
e67796
+
e67796
+  if (ret != 0)
e67796
+    FAIL_EXIT1 ("getaddrinfo failed: %s\n", gai_strerror (ret));
e67796
+
e67796
+  TEST_COMPARE_STRING (result->ai_canonname, QUERYNAME);
e67796
+
e67796
+  freeaddrinfo(result);
e67796
+  return 0;
e67796
+}
e67796
+
e67796
+#include <support/test-driver.c>
e67796
diff --git a/nss/tst-nss-gai-hv2-canonname.h b/nss/tst-nss-gai-hv2-canonname.h
e67796
new file mode 100644
e67796
index 0000000000000000..14f2a9cb0867dff9
e67796
--- /dev/null
e67796
+++ b/nss/tst-nss-gai-hv2-canonname.h
e67796
@@ -0,0 +1 @@
e67796
+#define QUERYNAME "test.example.com"
e67796
diff --git a/nss/tst-nss-gai-hv2-canonname.root/postclean.req b/nss/tst-nss-gai-hv2-canonname.root/postclean.req
e67796
new file mode 100644
e67796
index 0000000000000000..e69de29bb2d1d643
e67796
diff --git a/nss/tst-nss-gai-hv2-canonname.root/tst-nss-gai-hv2-canonname.script b/nss/tst-nss-gai-hv2-canonname.root/tst-nss-gai-hv2-canonname.script
e67796
new file mode 100644
e67796
index 0000000000000000..31848b4a28524af6
e67796
--- /dev/null
e67796
+++ b/nss/tst-nss-gai-hv2-canonname.root/tst-nss-gai-hv2-canonname.script
e67796
@@ -0,0 +1,2 @@
e67796
+cp $B/nss/libnss_test_gai_hv2_canonname.so $L/libnss_test_gai_hv2_canonname.so.2
e67796
+su
e67796
diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c
e67796
index 4fa963644af8b7d5..46046504a6858f2e 100644
e67796
--- a/sysdeps/posix/getaddrinfo.c
e67796
+++ b/sysdeps/posix/getaddrinfo.c
e67796
@@ -233,7 +233,6 @@ convert_hostent_to_gaih_addrtuple (const struct addrinfo *req,
e67796
 	}
e67796
       array[i].next = array + i + 1;
e67796
     }
e67796
-  array[0].name = h->h_name;
e67796
   array[count - 1].next = NULL;
e67796
 
e67796
   *result = array;
e67796
@@ -287,6 +286,18 @@ convert_hostent_to_gaih_addrtuple (const struct addrinfo *req,
e67796
 	}								      \
e67796
       *pat = addrmem;							      \
e67796
 									      \
e67796
+      /* Store h_name so that it survives accidental deallocation when	      \
e67796
+	 gethosts is called again and tmpbuf gets reallocated.  */	      \
e67796
+      if (h_name == NULL && th.h_name != NULL)				      \
e67796
+        {								      \
e67796
+	  h_name = __strdup (th.h_name);				      \
e67796
+	  if (h_name == NULL)						      \
e67796
+	    {								      \
e67796
+	      __resolv_context_put (res_ctx);				      \
e67796
+	      result = -EAI_SYSTEM;					      \
e67796
+	      goto free_and_return;					      \
e67796
+	    }								      \
e67796
+	}								      \
e67796
       if (localcanon != NULL && canon == NULL)				      \
e67796
 	{								      \
e67796
 	  canonbuf = __strdup (localcanon);				      \
e67796
@@ -323,15 +334,15 @@ typedef enum nss_status (*nss_getcanonname_r)
e67796
    memory allocation failure.  The returned string is allocated on the
e67796
    heap; the caller has to free it.  */
e67796
 static char *
e67796
-getcanonname (service_user *nip, struct gaih_addrtuple *at, const char *name)
e67796
+getcanonname (service_user *nip, const char *hname, const char *name)
e67796
 {
e67796
   nss_getcanonname_r cfct = __nss_lookup_function (nip, "getcanonname_r");
e67796
   char *s = (char *) name;
e67796
   if (cfct != NULL)
e67796
     {
e67796
       char buf[256];
e67796
-      if (DL_CALL_FCT (cfct, (at->name ?: name, buf, sizeof (buf),
e67796
-			      &s, &errno, &h_errno)) != NSS_STATUS_SUCCESS)
e67796
+      if (DL_CALL_FCT (cfct, (hname ?: name, buf, sizeof (buf), &s, &errno,
e67796
+			      &h_errno)) != NSS_STATUS_SUCCESS)
e67796
 	/* If the canonical name cannot be determined, use the passed
e67796
 	   string.  */
e67796
 	s = (char *) name;
e67796
@@ -349,6 +360,7 @@ gaih_inet (const char *name, const struct gaih_service *service,
e67796
   struct gaih_addrtuple *at = NULL;
e67796
   bool got_ipv6 = false;
e67796
   const char *canon = NULL;
e67796
+  char *h_name = NULL;
e67796
   const char *orig_name = name;
e67796
 
e67796
   /* Reserve stack memory for the scratch buffer in the getaddrinfo
e67796
@@ -919,7 +931,7 @@ gaih_inet (const char *name, const struct gaih_service *service,
e67796
 			  if ((req->ai_flags & AI_CANONNAME) != 0
e67796
 			      && canon == NULL)
e67796
 			    {
e67796
-			      canonbuf = getcanonname (nip, at, name);
e67796
+			      canonbuf = getcanonname (nip, h_name, name);
e67796
 			      if (canonbuf == NULL)
e67796
 				{
e67796
 				  __resolv_context_enable_inet6
e67796
@@ -1169,6 +1181,7 @@ gaih_inet (const char *name, const struct gaih_service *service,
e67796
     free ((char *) name);
e67796
   free (addrmem);
e67796
   free (canonbuf);
e67796
+  free (h_name);
e67796
 
e67796
   return result;
e67796
 }