179894
This is a downstream rework of this upstream patch:
179894
179894
  [PATCH v2 2/2] nss: Protect against errno changes in function lookup (bug 28953)
179894
  <https://sourceware.org/pipermail/libc-alpha/2022-March/137015.html>
179894
179894
The NSS module loading code has been rewritten upstream, which is why
179894
only the test can be reused.  NSS_DECLARE_MODULE_FUNCTIONS does not yet
179894
exist downstream, so this part had to be skipped.
179894
179894
diff --git a/nss/Makefile b/nss/Makefile
179894
index d5c28a6b5ed3661c..e8a7d9c7b3cefcdf 100644
179894
--- a/nss/Makefile
179894
+++ b/nss/Makefile
179894
@@ -59,7 +59,8 @@ tests			= test-netdb test-digits-dots tst-nss-getpwent bug17079 \
179894
 			  tst-nss-test2 \
179894
 			  tst-nss-test3 \
179894
 			  tst-nss-test4 \
179894
-			  tst-nss-test5
179894
+			  tst-nss-test5 \
179894
+			  tst-nss-test_errno
179894
 xtests			= bug-erange
179894
 
179894
 tests-container = \
179894
@@ -130,7 +131,7 @@ routines                += $(libnss_files-routines)
179894
 static-only-routines    += $(libnss_files-routines)
179894
 tests-static		+= tst-nss-static
179894
 endif
179894
-extra-test-objs		+= nss_test1.os nss_test2.os
179894
+extra-test-objs		+= nss_test1.os nss_test2.os nss_test_errno.os
179894
 
179894
 include ../Rules
179894
 
179894
@@ -166,10 +167,13 @@ rtld-tests-LDFLAGS += -Wl,--dynamic-list=nss_test.ver
179894
 
179894
 libof-nss_test1 = extramodules
179894
 libof-nss_test2 = extramodules
179894
+libof-nss_test_errno = extramodules
179894
 $(objpfx)/libnss_test1.so: $(objpfx)nss_test1.os $(link-libc-deps)
179894
 	$(build-module)
179894
 $(objpfx)/libnss_test2.so: $(objpfx)nss_test2.os $(link-libc-deps)
179894
 	$(build-module)
179894
+$(objpfx)/libnss_test_errno.so: $(objpfx)nss_test_errno.os $(link-libc-deps)
179894
+	$(build-module)
179894
 $(objpfx)nss_test2.os : nss_test1.c
179894
 ifdef libnss_test1.so-version
179894
 $(objpfx)/libnss_test1.so$(libnss_test1.so-version): $(objpfx)/libnss_test1.so
179894
@@ -179,9 +183,13 @@ ifdef libnss_test2.so-version
179894
 $(objpfx)/libnss_test2.so$(libnss_test2.so-version): $(objpfx)/libnss_test2.so
179894
 	$(make-link)
179894
 endif
179894
+$(objpfx)/libnss_test_errno.so$(libnss_files.so-version): \
179894
+  $(objpfx)/libnss_test_errno.so
179894
+	$(make-link)
179894
 $(patsubst %,$(objpfx)%.out,$(tests)) : \
179894
 	$(objpfx)/libnss_test1.so$(libnss_test1.so-version) \
179894
-	$(objpfx)/libnss_test2.so$(libnss_test2.so-version)
179894
+	$(objpfx)/libnss_test2.so$(libnss_test2.so-version) \
179894
+	$(objpfx)/libnss_test_errno.so$(libnss_files.so-version)
179894
 
179894
 ifeq (yes,$(have-thread-library))
179894
 $(objpfx)tst-cancel-getpwuid_r: $(shared-thread-library)
179894
diff --git a/nss/nss_test_errno.c b/nss/nss_test_errno.c
179894
new file mode 100644
179894
index 0000000000000000..ca75c890aa057869
179894
--- /dev/null
179894
+++ b/nss/nss_test_errno.c
179894
@@ -0,0 +1,53 @@
179894
+/* NSS service provider with errno clobber.
179894
+   Copyright (C) 2022 Free Software Foundation, Inc.
179894
+   This file is part of the GNU C Library.
179894
+
179894
+   The GNU C Library is free software; you can redistribute it and/or
179894
+   modify it under the terms of the GNU Lesser General Public
179894
+   License as published by the Free Software Foundation; either
179894
+   version 2.1 of the License, or (at your option) any later version.
179894
+
179894
+   The GNU C Library is distributed in the hope that it will be useful,
179894
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
179894
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
179894
+   Lesser General Public License for more details.
179894
+
179894
+   You should have received a copy of the GNU Lesser General Public
179894
+   License along with the GNU C Library; if not, see
179894
+   <https://www.gnu.org/licenses/>.  */
179894
+
179894
+#include <errno.h>
179894
+#include <nss.h>
179894
+#include <pwd.h>
179894
+#include <stdlib.h>
179894
+
179894
+static void __attribute__ ((constructor))
179894
+init (void)
179894
+{
179894
+  /* An arbitrary error code which is otherwise not used.  */
179894
+  errno = ELIBBAD;
179894
+}
179894
+
179894
+/* Lookup functions for pwd follow that do not return any data.  */
179894
+
179894
+enum nss_status
179894
+_nss_test_errno_setpwent (int stayopen)
179894
+{
179894
+  setenv ("_nss_test_errno_setpwent", "yes", 1);
179894
+  return NSS_STATUS_SUCCESS;
179894
+}
179894
+
179894
+enum nss_status
179894
+_nss_test_errno_getpwent_r (struct passwd *result,
179894
+                            char *buffer, size_t size, int *errnop)
179894
+{
179894
+  setenv ("_nss_test_errno_getpwent_r", "yes", 1);
179894
+  return NSS_STATUS_NOTFOUND;
179894
+}
179894
+
179894
+enum nss_status
179894
+_nss_test_errno_endpwent (void)
179894
+{
179894
+  setenv ("_nss_test_errno_endpwent", "yes", 1);
179894
+  return NSS_STATUS_SUCCESS;
179894
+}
179894
diff --git a/nss/nsswitch.c b/nss/nsswitch.c
179894
index 17adf1ef03f93d60..e59ab674e0426b26 100644
179894
--- a/nss/nsswitch.c
179894
+++ b/nss/nsswitch.c
179894
@@ -401,6 +401,7 @@ void *
179894
 __nss_lookup_function (service_user *ni, const char *fct_name)
179894
 {
179894
   void **found, *result;
179894
+  int saved_errno = errno;
179894
 
179894
   /* We now modify global data.  Protect it.  */
179894
   __libc_lock_lock (lock);
179894
@@ -523,6 +524,8 @@ __nss_lookup_function (service_user *ni, const char *fct_name)
179894
   /* Remove the lock.  */
179894
   __libc_lock_unlock (lock);
179894
 
179894
+  __set_errno (saved_errno);
179894
+
179894
   return result;
179894
 }
179894
 libc_hidden_def (__nss_lookup_function)
179894
diff --git a/nss/tst-nss-test_errno.c b/nss/tst-nss-test_errno.c
179894
new file mode 100644
179894
index 0000000000000000..d2c42dd363a38b0e
179894
--- /dev/null
179894
+++ b/nss/tst-nss-test_errno.c
179894
@@ -0,0 +1,61 @@
179894
+/* getpwent failure when dlopen clobbers errno (bug 28953).
179894
+   Copyright (C) 2022 Free Software Foundation, Inc.
179894
+   This file is part of the GNU C Library.
179894
+
179894
+   The GNU C Library is free software; you can redistribute it and/or
179894
+   modify it under the terms of the GNU Lesser General Public
179894
+   License as published by the Free Software Foundation; either
179894
+   version 2.1 of the License, or (at your option) any later version.
179894
+
179894
+   The GNU C Library is distributed in the hope that it will be useful,
179894
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
179894
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
179894
+   Lesser General Public License for more details.
179894
+
179894
+   You should have received a copy of the GNU Lesser General Public
179894
+   License along with the GNU C Library; if not, see
179894
+   <https://www.gnu.org/licenses/>.  */
179894
+
179894
+#include <nss.h>
179894
+#include <support/check.h>
179894
+#include <stdlib.h>
179894
+#include <errno.h>
179894
+#include <stdbool.h>
179894
+#include <pwd.h>
179894
+#include <string.h>
179894
+
179894
+static int
179894
+do_test (void)
179894
+{
179894
+  __nss_configure_lookup ("passwd", "files test_errno");
179894
+
179894
+  errno = 0;
179894
+  setpwent ();
179894
+  TEST_COMPARE (errno, 0);
179894
+
179894
+  bool root_seen = false;
179894
+  while (true)
179894
+    {
179894
+      errno = 0;
179894
+      struct passwd *e = getpwent ();
179894
+      if (e == NULL)
179894
+        break;
179894
+      if (strcmp (e->pw_name, "root"))
179894
+        root_seen = true;
179894
+    }
179894
+
179894
+  TEST_COMPARE (errno, 0);
179894
+  TEST_VERIFY (root_seen);
179894
+
179894
+  errno = 0;
179894
+  endpwent ();
179894
+  TEST_COMPARE (errno, 0);
179894
+
179894
+  TEST_COMPARE_STRING (getenv ("_nss_test_errno_setpwent"), "yes");
179894
+  TEST_COMPARE_STRING (getenv ("_nss_test_errno_getpwent_r"), "yes");
179894
+  TEST_COMPARE_STRING (getenv ("_nss_test_errno_endpwent"), "yes");
179894
+
179894
+  return 0;
179894
+}
179894
+
179894
+#include <support/test-driver.c>