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