5d7892
commit 790b8dda4455865cb8c3a47801f4304c1a43baf6
5d7892
Author: Florian Weimer <fweimer@redhat.com>
5d7892
Date:   Tue May 19 14:09:38 2020 +0200
5d7892
5d7892
    nss_compat: internal_end*ent may clobber errno, hiding ERANGE [BZ #25976]
5d7892
    
5d7892
    During cleanup, before returning from get*_r functions, the end*ent
5d7892
    calls must not change errno.  Otherwise, an ERANGE error from the
5d7892
    underlying implementation can be hidden, causing unexpected lookup
5d7892
    failures.  This commit introduces an internal_end*ent_noerror
5d7892
    function which saves and restore errno, and marks the original
5d7892
    internal_end*ent function as warn_unused_result, so that it is used
5d7892
    only in contexts were errors from it can be handled explicitly.
5d7892
    
5d7892
    Reviewed-by: DJ Delorie <dj@redhat.com>
5d7892
5d7892
diff --git a/nss/nss_compat/compat-grp.c b/nss/nss_compat/compat-grp.c
5d7892
index 012929eae7048702..fca9f4860f72e3d2 100644
5d7892
--- a/nss/nss_compat/compat-grp.c
5d7892
+++ b/nss/nss_compat/compat-grp.c
5d7892
@@ -142,7 +142,7 @@ _nss_compat_setgrent (int stayopen)
5d7892
 }
5d7892
 
5d7892
 
5d7892
-static enum nss_status
5d7892
+static enum nss_status __attribute_warn_unused_result__
5d7892
 internal_endgrent (ent_t *ent)
5d7892
 {
5d7892
   if (ent->stream != NULL)
5d7892
@@ -163,6 +163,15 @@ internal_endgrent (ent_t *ent)
5d7892
   return NSS_STATUS_SUCCESS;
5d7892
 }
5d7892
 
5d7892
+/* Like internal_endgrent, but preserve errno in all cases.  */
5d7892
+static void
5d7892
+internal_endgrent_noerror (ent_t *ent)
5d7892
+{
5d7892
+  int saved_errno = errno;
5d7892
+  enum nss_status unused __attribute__ ((unused)) = internal_endgrent (ent);
5d7892
+  __set_errno (saved_errno);
5d7892
+}
5d7892
+
5d7892
 enum nss_status
5d7892
 _nss_compat_endgrent (void)
5d7892
 {
5d7892
@@ -483,7 +492,7 @@ _nss_compat_getgrnam_r (const char *name, struct group *grp,
5d7892
   if (result == NSS_STATUS_SUCCESS)
5d7892
     result = internal_getgrnam_r (name, grp, &ent, buffer, buflen, errnop);
5d7892
 
5d7892
-  internal_endgrent (&ent;;
5d7892
+  internal_endgrent_noerror (&ent;;
5d7892
 
5d7892
   return result;
5d7892
 }
5d7892
@@ -612,7 +621,7 @@ _nss_compat_getgrgid_r (gid_t gid, struct group *grp,
5d7892
   if (result == NSS_STATUS_SUCCESS)
5d7892
     result = internal_getgrgid_r (gid, grp, &ent, buffer, buflen, errnop);
5d7892
 
5d7892
-  internal_endgrent (&ent;;
5d7892
+  internal_endgrent_noerror (&ent;;
5d7892
 
5d7892
   return result;
5d7892
 }
5d7892
diff --git a/nss/nss_compat/compat-initgroups.c b/nss/nss_compat/compat-initgroups.c
5d7892
index 5beaa6b88b7e1764..d7a89ea8e7718ab0 100644
5d7892
--- a/nss/nss_compat/compat-initgroups.c
5d7892
+++ b/nss/nss_compat/compat-initgroups.c
5d7892
@@ -133,7 +133,7 @@ internal_setgrent (ent_t *ent)
5d7892
 }
5d7892
 
5d7892
 
5d7892
-static enum nss_status
5d7892
+static enum nss_status __attribute_warn_unused_result__
5d7892
 internal_endgrent (ent_t *ent)
5d7892
 {
5d7892
   if (ent->stream != NULL)
5d7892
@@ -157,6 +157,15 @@ internal_endgrent (ent_t *ent)
5d7892
   return NSS_STATUS_SUCCESS;
5d7892
 }
5d7892
 
5d7892
+/* Like internal_endgrent, but preserve errno in all cases.  */
5d7892
+static void
5d7892
+internal_endgrent_noerror (ent_t *ent)
5d7892
+{
5d7892
+  int saved_errno = errno;
5d7892
+  enum nss_status unused __attribute__ ((unused)) = internal_endgrent (ent);
5d7892
+  __set_errno (saved_errno);
5d7892
+}
5d7892
+
5d7892
 /* Add new group record.  */
5d7892
 static void
5d7892
 add_group (long int *start, long int *size, gid_t **groupsp, long int limit,
5d7892
@@ -501,7 +510,7 @@ _nss_compat_initgroups_dyn (const char *user, gid_t group, long int *start,
5d7892
  done:
5d7892
   scratch_buffer_free (&tmpbuf);
5d7892
 
5d7892
-  internal_endgrent (&intern;;
5d7892
+  internal_endgrent_noerror (&intern;;
5d7892
 
5d7892
   return status;
5d7892
 }
5d7892
diff --git a/nss/nss_compat/compat-pwd.c b/nss/nss_compat/compat-pwd.c
5d7892
index a903452cdf96de74..8832fb7518d8bbff 100644
5d7892
--- a/nss/nss_compat/compat-pwd.c
5d7892
+++ b/nss/nss_compat/compat-pwd.c
5d7892
@@ -259,7 +259,7 @@ _nss_compat_setpwent (int stayopen)
5d7892
 }
5d7892
 
5d7892
 
5d7892
-static enum nss_status
5d7892
+static enum nss_status __attribute_warn_unused_result__
5d7892
 internal_endpwent (ent_t *ent)
5d7892
 {
5d7892
   if (ent->stream != NULL)
5d7892
@@ -287,6 +287,15 @@ internal_endpwent (ent_t *ent)
5d7892
   return NSS_STATUS_SUCCESS;
5d7892
 }
5d7892
 
5d7892
+/* Like internal_endpwent, but preserve errno in all cases.  */
5d7892
+static void
5d7892
+internal_endpwent_noerror (ent_t *ent)
5d7892
+{
5d7892
+  int saved_errno = errno;
5d7892
+  enum nss_status unused __attribute__ ((unused)) = internal_endpwent (ent);
5d7892
+  __set_errno (saved_errno);
5d7892
+}
5d7892
+
5d7892
 enum nss_status
5d7892
 _nss_compat_endpwent (void)
5d7892
 {
5d7892
@@ -822,7 +831,7 @@ _nss_compat_getpwnam_r (const char *name, struct passwd *pwd,
5d7892
   if (result == NSS_STATUS_SUCCESS)
5d7892
     result = internal_getpwnam_r (name, pwd, &ent, buffer, buflen, errnop);
5d7892
 
5d7892
-  internal_endpwent (&ent;;
5d7892
+  internal_endpwent_noerror (&ent;;
5d7892
 
5d7892
   return result;
5d7892
 }
5d7892
@@ -1061,7 +1070,7 @@ _nss_compat_getpwuid_r (uid_t uid, struct passwd *pwd,
5d7892
   if (result == NSS_STATUS_SUCCESS)
5d7892
     result = internal_getpwuid_r (uid, pwd, &ent, buffer, buflen, errnop);
5d7892
 
5d7892
-  internal_endpwent (&ent;;
5d7892
+  internal_endpwent_noerror (&ent;;
5d7892
 
5d7892
   return result;
5d7892
 }
5d7892
diff --git a/nss/nss_compat/compat-spwd.c b/nss/nss_compat/compat-spwd.c
5d7892
index eb96ca09172d5743..684a06007ab84ac9 100644
5d7892
--- a/nss/nss_compat/compat-spwd.c
5d7892
+++ b/nss/nss_compat/compat-spwd.c
5d7892
@@ -215,7 +215,7 @@ _nss_compat_setspent (int stayopen)
5d7892
 }
5d7892
 
5d7892
 
5d7892
-static enum nss_status
5d7892
+static enum nss_status __attribute_warn_unused_result__
5d7892
 internal_endspent (ent_t *ent)
5d7892
 {
5d7892
   if (ent->stream != NULL)
5d7892
@@ -244,6 +244,15 @@ internal_endspent (ent_t *ent)
5d7892
   return NSS_STATUS_SUCCESS;
5d7892
 }
5d7892
 
5d7892
+/* Like internal_endspent, but preserve errno in all cases.  */
5d7892
+static void
5d7892
+internal_endspent_noerror (ent_t *ent)
5d7892
+{
5d7892
+  int saved_errno = errno;
5d7892
+  enum nss_status unused __attribute__ ((unused)) = internal_endspent (ent);
5d7892
+  __set_errno (saved_errno);
5d7892
+}
5d7892
+
5d7892
 enum nss_status
5d7892
 _nss_compat_endspent (void)
5d7892
 {
5d7892
@@ -261,7 +270,6 @@ _nss_compat_endspent (void)
5d7892
   return result;
5d7892
 }
5d7892
 
5d7892
-
5d7892
 static enum nss_status
5d7892
 getspent_next_nss_netgr (const char *name, struct spwd *result, ent_t *ent,
5d7892
 			 char *group, char *buffer, size_t buflen,
5d7892
@@ -786,7 +794,7 @@ _nss_compat_getspnam_r (const char *name, struct spwd *pwd,
5d7892
   if (result == NSS_STATUS_SUCCESS)
5d7892
     result = internal_getspnam_r (name, pwd, &ent, buffer, buflen, errnop);
5d7892
 
5d7892
-  internal_endspent (&ent;;
5d7892
+  internal_endspent_noerror (&ent;;
5d7892
 
5d7892
   return result;
5d7892
 }