Blame SOURCES/glibc-rh705465.patch

b40826
2011-11-07  Andreas Schwab  <schwab@redhat.com>
b40826
b40826
	* nss/nss_files/files-initgroups.c (_nss_files_initgroups_dyn):
b40826
	Fix size of allocated buffer.
b40826
b40826
2011-05-10  Ulrich Drepper  <drepper@gmail.com>
b40826
b40826
	[BZ #11257]
b40826
	* grp/initgroups.c (internal_getgrouplist): When we found the service
b40826
	list through the initgroups entry in nsswitch.conf do not always
b40826
	continue on a successful lookup.  Don't always use the
b40826
	__nss_group_data-ase value if it is set.
b40826
	* nss/nsswitch.conf (initgroups): Change action for successful db
b40826
	lookup to continue for compatibility.
b40826
b40826
2011-05-06  Ulrich Drepper  <drepper@gmail.com>
b40826
b40826
	* nss/nss_files/files-initgroups.c (_nss_files_initgroups_dyn): Return
b40826
	NSS_STATUS_NOTFOUND if no record was found.
b40826
b40826
2011-04-29  Ulrich Drepper  <drepper@gmail.com>
b40826
b40826
	* grp/initgroups.c (internal_getgrouplist): Prefer initgroups setting
b40826
	to groups setting in database lookup.
b40826
	* nss/nsswitch.conf: Add initgroups entry.
b40826
b40826
2011-04-21  Ulrich Drepper  <drepper@gmail.com>
b40826
b40826
	* nss/nss_files/files-initgroups.c (_nss_files_initgroups_dyn): Fix
b40826
	problem in reallocation in last patch.
b40826
b40826
2011-04-19  Ulrich Drepper  <drepper@gmail.com>
b40826
b40826
	* nss/nss_files/files-initgroups.c: New file.
b40826
	* nss/Makefile (libnss_files-routines): Add files-initgroups.
b40826
	* nss/Versions (libnss_files) [GLIBC_PRIVATE]: Export
b40826
	_nss_files_initgroups_dyn.
b40826
b40826
2011-01-13  Ulrich Drepper  <drepper@gmail.com>
b40826
b40826
	[BZ #10484]
b40826
	* nss/nss_files/files-hosts.c (HOST_DB_LOOKUP): Handle overflows of
b40826
	temporary buffer used to handle multi lookups locally.
b40826
	* include/alloca.h: Add libc_hidden_proto for __libc_alloca_cutoff.
b40826
b40826
2011-01-13  Ulrich Drepper  <drepper@gmail.com>
b40826
b40826
	[BZ #10484]
b40826
	* Versions [libc] (GLIBC_PRIVATE): Export __libc_alloca_cutoff.
b40826
	* alloca_cutoff.c: Add libc_hidden_def.
b40826
b40826
Index: glibc-2.12-2-gc4ccff1/grp/initgroups.c
b40826
===================================================================
b40826
--- glibc-2.12-2-gc4ccff1.orig/grp/initgroups.c
b40826
+++ glibc-2.12-2-gc4ccff1/grp/initgroups.c
b40826
@@ -43,6 +43,8 @@ extern int __nss_group_lookup (service_u
b40826
 extern void *__nss_lookup_function (service_user *ni, const char *fct_name);
b40826
 
b40826
 extern service_user *__nss_group_database attribute_hidden;
b40826
+static service_user *initgroups_database;
b40826
+static bool use_initgroups_entry;
b40826
 
b40826
 
b40826
 #include "compat-initgroups.c"
b40826
@@ -67,32 +69,41 @@ internal_getgrouplist (const char *user,
b40826
     }
b40826
 #endif
b40826
 
b40826
-  service_user *nip = NULL;
b40826
-  initgroups_dyn_function fct;
b40826
   enum nss_status status = NSS_STATUS_UNAVAIL;
b40826
-  int no_more;
b40826
-  /* Start is one, because we have the first group as parameter.  */
b40826
-  long int start = 1;
b40826
+  int no_more = 0;
b40826
 
b40826
   /* Never store more than the starting *SIZE number of elements.  */
b40826
   assert (*size > 0);
b40826
   (*groupsp)[0] = group;
b40826
+  /* Start is one, because we have the first group as parameter.  */
b40826
+  long int start = 1;
b40826
 
b40826
-  if (__nss_group_database != NULL)
b40826
+  if (initgroups_database == NULL)
b40826
     {
b40826
-      no_more = 0;
b40826
-      nip = __nss_group_database;
b40826
+      no_more = __nss_database_lookup ("initgroups", NULL, "",
b40826
+				       &initgroups_database);
b40826
+      if (no_more == 0 && initgroups_database == NULL)
b40826
+	{
b40826
+	  if (__nss_group_database == NULL)
b40826
+	    no_more = __nss_database_lookup ("group", NULL, "compat files",
b40826
+					     &__nss_group_database);
b40826
+
b40826
+	  initgroups_database = __nss_group_database;
b40826
+	}
b40826
+      else if (initgroups_database != NULL)
b40826
+	{
b40826
+	  assert (no_more == 0);
b40826
+	  use_initgroups_entry = true;
b40826
+	}
b40826
     }
b40826
-  else
b40826
-    no_more = __nss_database_lookup ("group", NULL,
b40826
-				     "compat [NOTFOUND=return] files", &nip;;
b40826
 
b40826
+  service_user *nip = initgroups_database;
b40826
   while (! no_more)
b40826
     {
b40826
       long int prev_start = start;
b40826
 
b40826
-      fct = __nss_lookup_function (nip, "initgroups_dyn");
b40826
-
b40826
+      initgroups_dyn_function fct = __nss_lookup_function (nip,
b40826
+							   "initgroups_dyn");
b40826
       if (fct == NULL)
b40826
 	status = compat_call (nip, user, group, &start, size, groupsp,
b40826
 			      limit, &errno);
b40826
@@ -119,7 +130,13 @@ internal_getgrouplist (const char *user,
b40826
       if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
b40826
 	__libc_fatal ("illegal status in internal_getgrouplist");
b40826
 
b40826
-      if (status != NSS_STATUS_SUCCESS
b40826
+      /* For compatibility reason we will continue to look for more
b40826
+	 entries using the next service even though data has already
b40826
+	 been found if the nsswitch.conf file contained only a 'groups'
b40826
+	 line and no 'initgroups' line.  If the latter is available
b40826
+	 we always respect the status.  This means that the default
b40826
+	 for successful lookups is to return.  */
b40826
+      if ((use_initgroups_entry || status != NSS_STATUS_SUCCESS)
b40826
 	  && nss_next_action (nip, status) == NSS_ACTION_RETURN)
b40826
 	 break;
b40826
 
b40826
Index: glibc-2.12-2-gc4ccff1/include/alloca.h
b40826
===================================================================
b40826
--- glibc-2.12-2-gc4ccff1.orig/include/alloca.h
b40826
+++ glibc-2.12-2-gc4ccff1/include/alloca.h
b40826
@@ -14,6 +14,7 @@ extern void *__alloca (size_t __size);
b40826
 
b40826
 extern int __libc_use_alloca (size_t size) __attribute__ ((const));
b40826
 extern int __libc_alloca_cutoff (size_t size) __attribute__ ((const));
b40826
+libc_hidden_proto (__libc_alloca_cutoff)
b40826
 
b40826
 #define __MAX_ALLOCA_CUTOFF	65536
b40826
 
b40826
Index: glibc-2.12-2-gc4ccff1/nptl/Versions
b40826
===================================================================
b40826
--- glibc-2.12-2-gc4ccff1.orig/nptl/Versions
b40826
+++ glibc-2.12-2-gc4ccff1/nptl/Versions
b40826
@@ -27,6 +27,7 @@ libc {
b40826
     pthread_cond_broadcast; pthread_cond_timedwait;
b40826
   }
b40826
   GLIBC_PRIVATE {
b40826
+    __libc_alloca_cutoff;
b40826
     # Internal libc interface to libpthread
b40826
     __libc_dl_error_tsd;
b40826
   }
b40826
Index: glibc-2.12-2-gc4ccff1/nptl/alloca_cutoff.c
b40826
===================================================================
b40826
--- glibc-2.12-2-gc4ccff1.orig/nptl/alloca_cutoff.c
b40826
+++ glibc-2.12-2-gc4ccff1/nptl/alloca_cutoff.c
b40826
@@ -34,3 +34,4 @@ __libc_alloca_cutoff (size_t size)
b40826
 			  assume the maximum available stack space.  */
b40826
 		       ?: __MAX_ALLOCA_CUTOFF * 4));
b40826
 }
b40826
+libc_hidden_def (__libc_alloca_cutoff)
b40826
Index: glibc-2.12-2-gc4ccff1/nss/Makefile
b40826
===================================================================
b40826
--- glibc-2.12-2-gc4ccff1.orig/nss/Makefile
b40826
+++ glibc-2.12-2-gc4ccff1/nss/Makefile
b40826
@@ -63,7 +63,7 @@ vpath %.c $(subdir-dirs)
b40826
 
b40826
 
b40826
 libnss_files-routines	:= $(addprefix files-,$(databases)) \
b40826
-			   files-have_o_cloexec
b40826
+			   files-initgroups files-have_o_cloexec
b40826
 distribute		+= files-XXX.c files-parse.c
b40826
 
b40826
 
b40826
Index: glibc-2.12-2-gc4ccff1/nss/Versions
b40826
===================================================================
b40826
--- glibc-2.12-2-gc4ccff1.orig/nss/Versions
b40826
+++ glibc-2.12-2-gc4ccff1/nss/Versions
b40826
@@ -95,5 +95,7 @@ libnss_files {
b40826
     _nss_netgroup_parseline;
b40826
     _nss_files_getpublickey;
b40826
     _nss_files_getsecretkey;
b40826
+
b40826
+    _nss_files_initgroups_dyn;
b40826
   }
b40826
 }
b40826
Index: glibc-2.12-2-gc4ccff1/nss/nss_files/files-hosts.c
b40826
===================================================================
b40826
--- glibc-2.12-2-gc4ccff1.orig/nss/nss_files/files-hosts.c
b40826
+++ glibc-2.12-2-gc4ccff1/nss/nss_files/files-hosts.c
b40826
@@ -129,19 +129,22 @@ _nss_files_get##name##_r (proto,					   
b40826
 	  && _res_hconf.flags & HCONF_FLAG_MULTI)			      \
b40826
 	{								      \
b40826
 	  /* We have to get all host entries from the file.  */		      \
b40826
-	  const size_t tmp_buflen = MIN (buflen, 4096);			      \
b40826
-	  char tmp_buffer[tmp_buflen]					      \
b40826
+	  size_t tmp_buflen = MIN (buflen, 4096);			      \
b40826
+	  char tmp_buffer_stack[tmp_buflen]				      \
b40826
 	    __attribute__ ((__aligned__ (__alignof__ (struct hostent_data))));\
b40826
+	  char *tmp_buffer = tmp_buffer_stack;				      \
b40826
 	  struct hostent tmp_result_buf;				      \
b40826
 	  int naddrs = 1;						      \
b40826
 	  int naliases = 0;						      \
b40826
 	  char *bufferend;						      \
b40826
+	  bool tmp_buffer_malloced = false;				      \
b40826
 									      \
b40826
 	  while (result->h_aliases[naliases] != NULL)			      \
b40826
 	    ++naliases;							      \
b40826
 									      \
b40826
 	  bufferend = (char *) &result->h_aliases[naliases + 1];	      \
b40826
 									      \
b40826
+	again:								      \
b40826
 	  while ((status = internal_getent (&tmp_result_buf, tmp_buffer,      \
b40826
 					    tmp_buflen, errnop H_ERRNO_ARG    \
b40826
 					    EXTRA_ARGS_VALUE))		      \
b40826
@@ -182,7 +185,7 @@ _nss_files_get##name##_r (proto,					   
b40826
 		    }							      \
b40826
 		  /* If the real name is different add it also to the	      \
b40826
 		     aliases.  This means that there is a duplication	      \
b40826
-		     in the alias list but this is really the users	      \
b40826
+		     in the alias list but this is really the user's	      \
b40826
 		     problem.  */					      \
b40826
 		  if (strcmp (old_result->h_name,			      \
b40826
 			      tmp_result_buf.h_name) != 0)		      \
b40826
@@ -204,7 +207,7 @@ _nss_files_get##name##_r (proto,					   
b40826
 		      *errnop = ERANGE;					      \
b40826
 		      *herrnop = NETDB_INTERNAL;			      \
b40826
 		      status = NSS_STATUS_TRYAGAIN;			      \
b40826
-		      break;						      \
b40826
+		      goto out;						      \
b40826
 		    }							      \
b40826
 									      \
b40826
 		  new_h_addr_list =					      \
b40826
@@ -268,8 +271,54 @@ _nss_files_get##name##_r (proto,					   
b40826
 		}							      \
b40826
 	    }								      \
b40826
 									      \
b40826
-	  if (status != NSS_STATUS_TRYAGAIN)				      \
b40826
+	  if (status == NSS_STATUS_TRYAGAIN)				      \
b40826
+	    {								      \
b40826
+	      size_t newsize = 2 * tmp_buflen;				      \
b40826
+	      if (tmp_buffer_malloced)					      \
b40826
+		{							      \
b40826
+		  char *newp = realloc (tmp_buffer, newsize);		      \
b40826
+		  if (newp != NULL)					      \
b40826
+		    {							      \
b40826
+		      assert ((((uintptr_t) newp)			      \
b40826
+			       & (__alignof__ (struct hostent_data) - 1))     \
b40826
+			      == 0);					      \
b40826
+		      tmp_buffer = newp;				      \
b40826
+		      tmp_buflen = newsize;				      \
b40826
+		      goto again;					      \
b40826
+		    }							      \
b40826
+		}							      \
b40826
+	      else if (!__libc_use_alloca (buflen + newsize))		      \
b40826
+		{							      \
b40826
+		  tmp_buffer = malloc (newsize);			      \
b40826
+		  if (tmp_buffer != NULL)				      \
b40826
+		    {							      \
b40826
+		      assert ((((uintptr_t) tmp_buffer)			      \
b40826
+			       & (__alignof__ (struct hostent_data) - 1))     \
b40826
+			      == 0);					      \
b40826
+		      tmp_buffer_malloced = true;			      \
b40826
+		      tmp_buflen = newsize;				      \
b40826
+		      goto again;					      \
b40826
+		    }							      \
b40826
+		}							      \
b40826
+	      else							      \
b40826
+		{							      \
b40826
+		  tmp_buffer						      \
b40826
+		    = extend_alloca (tmp_buffer, tmp_buflen,		      \
b40826
+				     newsize				      \
b40826
+				     + __alignof__ (struct hostent_data));    \
b40826
+		  tmp_buffer = (char *) (((uintptr_t) tmp_buffer	      \
b40826
+					  + __alignof__ (struct hostent_data) \
b40826
+					  - 1)				      \
b40826
+					 & ~(__alignof__ (struct hostent_data)\
b40826
+					     - 1));			      \
b40826
+		  goto again;						      \
b40826
+		}							      \
b40826
+	    }								      \
b40826
+	  else								      \
b40826
 	    status = NSS_STATUS_SUCCESS;				      \
b40826
+	out:								      \
b40826
+	  if (tmp_buffer_malloced)					      \
b40826
+	    free (tmp_buffer);						      \
b40826
 	}								      \
b40826
 									      \
b40826
 									      \
b40826
Index: glibc-2.12-2-gc4ccff1/nss/nss_files/files-initgroups.c
b40826
===================================================================
b40826
--- /dev/null
b40826
+++ glibc-2.12-2-gc4ccff1/nss/nss_files/files-initgroups.c
b40826
@@ -0,0 +1,137 @@
b40826
+/* Initgroups handling in nss_files module.
b40826
+   Copyright (C) 2011 Free Software Foundation, Inc.
b40826
+   This file is part of the GNU C Library.
b40826
+
b40826
+   The GNU C Library is free software; you can redistribute it and/or
b40826
+   modify it under the terms of the GNU Lesser General Public
b40826
+   License as published by the Free Software Foundation; either
b40826
+   version 2.1 of the License, or (at your option) any later version.
b40826
+
b40826
+   The GNU C Library is distributed in the hope that it will be useful,
b40826
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
b40826
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b40826
+   Lesser General Public License for more details.
b40826
+
b40826
+   You should have received a copy of the GNU Lesser General Public
b40826
+   License along with the GNU C Library; if not, write to the Free
b40826
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
b40826
+   02111-1307 USA.  */
b40826
+
b40826
+#include <alloca.h>
b40826
+#include <errno.h>
b40826
+#include <grp.h>
b40826
+#include <nss.h>
b40826
+#include <stdio_ext.h>
b40826
+#include <string.h>
b40826
+#include <sys/param.h>
b40826
+
b40826
+enum nss_status
b40826
+_nss_files_initgroups_dyn (const char *user, gid_t group, long int *start,
b40826
+			   long int *size, gid_t **groupsp, long int limit,
b40826
+			   int *errnop)
b40826
+{
b40826
+  FILE *stream = fopen ("/etc/group", "re");
b40826
+  if (stream == NULL)
b40826
+    {
b40826
+      *errnop = errno;
b40826
+      return *errnop == ENOMEM ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
b40826
+    }
b40826
+
b40826
+  /* No other thread using this stream.  */
b40826
+  __fsetlocking (stream, FSETLOCKING_BYCALLER);
b40826
+
b40826
+  char *line = NULL;
b40826
+  size_t linelen = 0;
b40826
+  enum nss_status status = NSS_STATUS_SUCCESS;
b40826
+  bool any = false;
b40826
+
b40826
+  size_t buflen = 1024;
b40826
+  void *buffer = alloca (buflen);
b40826
+  bool buffer_use_malloc = false;
b40826
+
b40826
+  gid_t *groups = *groupsp;
b40826
+
b40826
+  /* We have to iterate over the entire file.  */
b40826
+  while (!feof_unlocked (stream))
b40826
+    {
b40826
+      ssize_t n = getline (&line, &linelen, stream);
b40826
+      if (n < 0)
b40826
+	{
b40826
+	  if (! feof_unlocked (stream))
b40826
+	    status = ((*errnop = errno) == ENOMEM
b40826
+		      ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL);
b40826
+	  break;
b40826
+	}
b40826
+
b40826
+      struct group grp;
b40826
+      int res;
b40826
+      while ((res = _nss_files_parse_grent (line, &grp, buffer, buflen,
b40826
+					    errnop)) == -1)
b40826
+	{
b40826
+	  size_t newbuflen = 2 * buflen;
b40826
+	  if (buffer_use_malloc || ! __libc_use_alloca (buflen + newbuflen))
b40826
+	    {
b40826
+	      void *newbuf = realloc (buffer_use_malloc ? buffer : NULL,
b40826
+				      newbuflen);
b40826
+	      if (newbuf == NULL)
b40826
+		{
b40826
+		  *errnop = ENOMEM;
b40826
+		  status = NSS_STATUS_TRYAGAIN;
b40826
+		  goto out;
b40826
+		}
b40826
+	      buffer = newbuf;
b40826
+	      buflen = newbuflen;
b40826
+	      buffer_use_malloc = true;
b40826
+	    }
b40826
+	  else
b40826
+	    buffer = extend_alloca (buffer, buflen, newbuflen);
b40826
+	}
b40826
+
b40826
+      if (res > 0 && grp.gr_gid != group)
b40826
+	for (char **m = grp.gr_mem; *m != NULL; ++m)
b40826
+	  if (strcmp (*m, user) == 0)
b40826
+	    {
b40826
+	      /* Matches user.  Insert this group.  */
b40826
+	      if (*start == *size)
b40826
+		{
b40826
+		  /* Need a bigger buffer.  */
b40826
+		  if (limit > 0 && *size == limit)
b40826
+		    /* We reached the maximum.  */
b40826
+		    goto out;
b40826
+
b40826
+		  long int newsize;
b40826
+		  if (limit <= 0)
b40826
+		    newsize = 2 * *size;
b40826
+		  else
b40826
+		    newsize = MIN (limit, 2 * *size);
b40826
+
b40826
+		  gid_t *newgroups = realloc (groups,
b40826
+					      newsize * sizeof (*groups));
b40826
+		  if (newgroups == NULL)
b40826
+		    {
b40826
+		      *errnop = ENOMEM;
b40826
+		      status = NSS_STATUS_TRYAGAIN;
b40826
+		      goto out;
b40826
+		    }
b40826
+		  *groupsp = groups = newgroups;
b40826
+		  *size = newsize;
b40826
+		}
b40826
+
b40826
+	      groups[*start] = grp.gr_gid;
b40826
+	      *start += 1;
b40826
+	      any = true;
b40826
+
b40826
+	      break;
b40826
+	    }
b40826
+    }
b40826
+
b40826
+ out:
b40826
+  /* Free memory.  */
b40826
+  if (buffer_use_malloc)
b40826
+    free (buffer);
b40826
+  free (line);
b40826
+
b40826
+  fclose (stream);
b40826
+
b40826
+  return status == NSS_STATUS_SUCCESS && !any ? NSS_STATUS_NOTFOUND : status;
b40826
+}
b40826
Index: glibc-2.12-2-gc4ccff1/nss/nsswitch.conf
b40826
===================================================================
b40826
--- glibc-2.12-2-gc4ccff1.orig/nss/nsswitch.conf
b40826
+++ glibc-2.12-2-gc4ccff1/nss/nsswitch.conf
b40826
@@ -5,6 +5,7 @@
b40826
 
b40826
 passwd:		db files
b40826
 group:		db files
b40826
+initgroups:	db [SUCCESS=continue] files
b40826
 shadow:		db files
b40826
 gshadow:	files
b40826