about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2016-06-11 12:12:56 +0200
committerRyan S. Arnold <ryan.arnold@linaro.org>2016-10-22 11:49:09 -0500
commit88227449533116f025c75d1d5f26f554797bfadc (patch)
tree34d70cf3b9431019b33eb5b8fed0e847d8b26f15
parent679988f02255d3d70eaffbb0e02c052a8b322656 (diff)
downloadglibc-88227449533116f025c75d1d5f26f554797bfadc.tar.gz
glibc-88227449533116f025c75d1d5f26f554797bfadc.tar.xz
glibc-88227449533116f025c75d1d5f26f554797bfadc.zip
nss_db: Fix initialization of iteration position [BZ #20237]
When get*ent is called without a preceding set*ent, we need
to set the initial iteration position in get*ent.

Reproducer: Add “services: db files” to /etc/nsswitch.conf, then run
“perl -e getservent”.  It will segfault before this change, and exit
silently after it.

(cherry picked from commit 31d0a4fa646db8b8c97ce24e0ec0a7b73de4fca1)
-rw-r--r--ChangeLog7
-rw-r--r--nss/nss_db/db-XXX.c8
2 files changed, 14 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index c2191da87e..7e37e6806c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2016-06-11  Florian Weimer  <fweimer@redhat.com>
+
+	[BZ #20237]
+	* nss/nss_db/db-XXX.c (set*ent): Reset entidx to NULL.
+	(get*ent): Set entidx to NULL during initialization.  If entidx is
+	NULL, start iteration from the beginning.
+
 2016-06-30  Andreas Schwab  <schwab@suse.de>
 
 	[BZ #20262]
diff --git a/nss/nss_db/db-XXX.c b/nss/nss_db/db-XXX.c
index 125a5e9b12..2d13edda27 100644
--- a/nss/nss_db/db-XXX.c
+++ b/nss/nss_db/db-XXX.c
@@ -77,7 +77,7 @@ CONCAT(_nss_db_set,ENTNAME) (int stayopen)
       keep_db |= stayopen;
 
       /* Reset the sequential index.  */
-      entidx  = (const char *) state.header + state.header->valstroffset;
+      entidx  = NULL;
     }
 
   __libc_lock_unlock (lock);
@@ -253,8 +253,14 @@ CONCAT(_nss_db_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
 	  H_ERRNO_SET (NETDB_INTERNAL);
 	  goto out;
 	}
+      entidx = NULL;
     }
 
+  /* Start from the beginning if freshly initialized or reset
+     requested by set*ent.  */
+  if (entidx == NULL)
+    entidx = (const char *) state.header + state.header->valstroffset;
+
   status = NSS_STATUS_UNAVAIL;
   if (state.header != MAP_FAILED)
     {