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