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