Blame SOURCES/glibc-rh1032435.patch

147e83
commit 977f4b31b7ca4a4e498c397f3fd70510694bbd86
147e83
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
147e83
Date:   Wed Oct 30 16:13:37 2013 +0530
147e83
147e83
    Fix reads for sizes larger than INT_MAX in AF_INET lookup
147e83
    
147e83
    Currently for AF_INET lookups from the hosts file, buffer sizes larger
147e83
    than INT_MAX silently overflow and may result in access beyond bounds
147e83
    of a buffer.  This happens when the number of results in an AF_INET
147e83
    lookup in /etc/hosts are very large.
147e83
    
147e83
    There are two aspects to the problem.  One problem is that the size
147e83
    computed from the buffer size is stored into an int, which results in
147e83
    overflow for large sizes.  Additionally, even if this size was
147e83
    expanded, the function used to read content into the buffer (fgets)
147e83
    accepts only int sizes.  As a result, the fix is to have a function
147e83
    wrap around fgets that calls it multiple times with int sizes if
147e83
    necessary.
147e83
147e83
(The previous commit fixes upstream bug 16071.)
147e83
147e83
commit ac60763eac3d43b7234dd21286ad3ec3f17957fc
147e83
Author: Andreas Schwab <schwab@suse.de>
147e83
Date:   Mon Jun 23 10:24:45 2014 +0200
147e83
147e83
    Don't ignore too long lines in nss_files (BZ #17079)
147e83
147e83
commit e07aabba73ea62e7dfa0512507c92efb851fbdbe
147e83
Author: Florian Weimer <fweimer@redhat.com>
147e83
Date:   Tue Sep 22 13:20:18 2015 +0200
147e83
147e83
    Add test case for bug 18287
147e83
147e83
commit 90fa42a1d7b78de0d75f7e3af362275b2abe807f
147e83
Author: Florian Weimer <fweimer@redhat.com>
147e83
Date:   Tue Sep 22 13:40:17 2015 +0200
147e83
147e83
    Test in commit e07aabba73ea62e7dfa0512507c92efb851fbdbe is for bug 17079
147e83
147e83
diff -u b/nss/nss_files/files-XXX.c b/nss/nss_files/files-XXX.c
147e83
--- b/nss/nss_files/files-XXX.c
147e83
+++ b/nss/nss_files/files-XXX.c
147e83
@@ -179,8 +179,53 @@
147e83
   return NSS_STATUS_SUCCESS;
147e83
 }
147e83
 
147e83
-/* Parsing the database file into `struct STRUCTURE' data structures.  */
147e83
 
147e83
+typedef enum
147e83
+{
147e83
+  gcr_ok = 0,
147e83
+  gcr_error = -1,
147e83
+  gcr_overflow = -2
147e83
+} get_contents_ret;
147e83
+
147e83
+/* Hack around the fact that fgets only accepts int sizes.  */
147e83
+static get_contents_ret
147e83
+get_contents (char *linebuf, size_t len, FILE *stream)
147e83
+{
147e83
+  size_t remaining_len = len;
147e83
+  char *curbuf = linebuf;
147e83
+
147e83
+  do
147e83
+    {
147e83
+      int curlen = ((remaining_len > (size_t) INT_MAX) ? INT_MAX
147e83
+		    : remaining_len);
147e83
+
147e83
+      /* Terminate the line so that we can test for overflow.  */
147e83
+      ((unsigned char *) curbuf)[curlen - 1] = 0xff;
147e83
+
147e83
+      char *p = fgets_unlocked (curbuf, curlen, stream);
147e83
+
147e83
+      /* EOF or read error.  */
147e83
+      if (p == NULL)
147e83
+        return gcr_error;
147e83
+
147e83
+      /* Done reading in the line.  */
147e83
+      if (((unsigned char *) curbuf)[curlen - 1] == 0xff)
147e83
+        return gcr_ok;
147e83
+
147e83
+      /* Drop the terminating '\0'.  */
147e83
+      remaining_len -= curlen - 1;
147e83
+      curbuf += curlen - 1;
147e83
+    }
147e83
+  /* fgets copies one less than the input length.  Our last iteration is of
147e83
+     REMAINING_LEN and once that is done, REMAINING_LEN is decremented by
147e83
+     REMAINING_LEN - 1, leaving the result as 1.  */
147e83
+  while (remaining_len > 1);
147e83
+
147e83
+  /* This means that the current buffer was not large enough.  */
147e83
+  return gcr_overflow;
147e83
+}
147e83
+
147e83
+/* Parsing the database file into `struct STRUCTURE' data structures.  */
147e83
 static enum nss_status
147e83
 internal_getent (struct STRUCTURE *result,
147e83
 		 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO
147e83
@@ -188,7 +233,7 @@
147e83
 {
147e83
   char *p;
147e83
   struct parser_data *data = (void *) buffer;
147e83
-  int linebuflen = buffer + buflen - data->linebuffer;
147e83
+  size_t linebuflen = buffer + buflen - data->linebuffer;
147e83
   int parse_result;
147e83
 
147e83
   if (buflen < sizeof *data + 2)
147e83
@@ -200,17 +245,16 @@
147e83
 
147e83
   do
147e83
     {
147e83
-      /* Terminate the line so that we can test for overflow.  */
147e83
-      ((unsigned char *) data->linebuffer)[linebuflen - 1] = '\xff';
147e83
+      get_contents_ret r = get_contents (data->linebuffer, linebuflen, stream);
147e83
 
147e83
-      p = fgets_unlocked (data->linebuffer, linebuflen, stream);
147e83
-      if (p == NULL)
147e83
+      if (r == gcr_error)
147e83
 	{
147e83
 	  /* End of file or read error.  */
147e83
 	  H_ERRNO_SET (HOST_NOT_FOUND);
147e83
 	  return NSS_STATUS_NOTFOUND;
147e83
 	}
147e83
-      else if (((unsigned char *) data->linebuffer)[linebuflen - 1] != 0xff)
147e83
+
147e83
+      if (r == gcr_overflow)
147e83
 	{
147e83
 	  /* The line is too long.  Give the user the opportunity to
147e83
 	     enlarge the buffer.  */
147e83
@@ -219,7 +263,8 @@
147e83
 	  return NSS_STATUS_TRYAGAIN;
147e83
 	}
147e83
 
147e83
-      /* Skip leading blanks.  */
147e83
+      /* Everything OK.  Now skip leading blanks.  */
147e83
+      p = data->linebuffer;
147e83
       while (isspace (*p))
147e83
 	++p;
147e83
     }
147e83
147e83
diff a/nss/bug17079.c b/nss/bug17079.c
147e83
--- /dev/null
147e83
+++ b/nss/bug17079.c
147e83
@@ -0,0 +1,236 @@
147e83
+/* Test for bug 17079: heap overflow in NSS with small buffers.
147e83
+   Copyright (C) 2015 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+#include <errno.h>
147e83
+#include <pwd.h>
147e83
+#include <stdbool.h>
147e83
+#include <stdio.h>
147e83
+#include <stdlib.h>
147e83
+#include <string.h>
147e83
+
147e83
+/* Check if two passwd structs contain the same data.  */
147e83
+static bool
147e83
+equal (const struct passwd *a, const struct passwd *b)
147e83
+{
147e83
+  return strcmp (a->pw_name, b->pw_name) == 0
147e83
+    && strcmp (a->pw_passwd, b->pw_passwd) == 0
147e83
+    && a->pw_uid == b->pw_uid
147e83
+    && a->pw_gid == b->pw_gid
147e83
+    && strcmp (a->pw_gecos, b->pw_gecos) == 0
147e83
+    && strcmp (a->pw_dir, b->pw_dir) == 0
147e83
+    && strcmp (a->pw_shell, b->pw_shell) == 0;
147e83
+}
147e83
+
147e83
+enum { MAX_TEST_ITEMS = 10 };
147e83
+static struct passwd test_items[MAX_TEST_ITEMS];
147e83
+static int test_count;
147e83
+
147e83
+/* Initialize test_items and test_count above, with data from the
147e83
+   passwd database.  */
147e83
+static bool
147e83
+init_test_items (void)
147e83
+{
147e83
+  setpwent ();
147e83
+  do
147e83
+    {
147e83
+      struct passwd *pwd = getpwent ();
147e83
+      if (pwd == NULL)
147e83
+        break;
147e83
+      struct passwd *target = test_items + test_count;
147e83
+      target->pw_name = strdup (pwd->pw_name);
147e83
+      target->pw_passwd = strdup (pwd->pw_passwd);
147e83
+      target->pw_uid = pwd->pw_uid;
147e83
+      target->pw_gid = pwd->pw_gid;
147e83
+      target->pw_gecos = strdup (pwd->pw_gecos);
147e83
+      target->pw_dir = strdup (pwd->pw_dir);
147e83
+      target->pw_shell = strdup (pwd->pw_shell);
147e83
+    }
147e83
+  while (++test_count < MAX_TEST_ITEMS);
147e83
+  endpwent ();
147e83
+
147e83
+  /* Filter out those test items which cannot be looked up by name or
147e83
+     UID.  */
147e83
+  bool found = false;
147e83
+  for (int i = 0; i < test_count; ++i)
147e83
+    {
147e83
+      struct passwd *pwd1 = getpwnam (test_items[i].pw_name);
147e83
+      struct passwd *pwd2 = getpwuid (test_items[i].pw_uid);
147e83
+      if (pwd1 == NULL || !equal (pwd1, test_items + i)
147e83
+          || pwd2 == NULL || !equal (pwd2, test_items + i))
147e83
+        test_items[i].pw_name = NULL;
147e83
+      else
147e83
+        found = true;
147e83
+    }
147e83
+
147e83
+  if (!found)
147e83
+    puts ("error: no accounts found which can be looked up by name and UID.");
147e83
+  return found;
147e83
+}
147e83
+
147e83
+/* Set to true if an error is encountered.  */
147e83
+static bool errors;
147e83
+
147e83
+/* Return true if the padding has not been tampered with.  */
147e83
+static bool
147e83
+check_padding (char *buffer, size_t size, char pad)
147e83
+{
147e83
+  char *end = buffer + size;
147e83
+  while (buffer < end)
147e83
+    {
147e83
+      if (*buffer != pad)
147e83
+        return false;
147e83
+      ++buffer;
147e83
+    }
147e83
+  return true;
147e83
+}
147e83
+
147e83
+/* Test one buffer size and padding combination.  */
147e83
+static void
147e83
+test_one (const struct passwd *item, size_t buffer_size,
147e83
+           char pad, size_t padding_size)
147e83
+{
147e83
+  char *buffer = malloc (buffer_size + padding_size);
147e83
+  if (buffer == NULL)
147e83
+    {
147e83
+      puts ("error: malloc failure");
147e83
+      errors = true;
147e83
+      return;
147e83
+    }
147e83
+
147e83
+  struct passwd pwd;
147e83
+  struct passwd *result;
147e83
+  int ret;
147e83
+
147e83
+  /* Test getpwname_r.  */
147e83
+  memset (buffer, pad, buffer_size + padding_size);
147e83
+  pwd = (struct passwd) {};
147e83
+  ret = getpwnam_r (item->pw_name, &pwd, buffer, buffer_size, &result);
147e83
+  if (!check_padding (buffer + buffer_size, padding_size, pad))
147e83
+    {
147e83
+      printf ("error: padding change: "
147e83
+              "name \"%s\", buffer size %zu, padding size %zu, pad 0x%02x\n",
147e83
+              item->pw_name, buffer_size, padding_size, (unsigned char) pad);
147e83
+      errors = true;
147e83
+    }
147e83
+  if (ret == 0)
147e83
+    {
147e83
+      if (result == NULL)
147e83
+        {
147e83
+          printf ("error: no data: name \"%s\", buffer size %zu\n",
147e83
+                  item->pw_name, buffer_size);
147e83
+          errors = true;
147e83
+        }
147e83
+      else if (!equal (item, result))
147e83
+        {
147e83
+          printf ("error: lookup mismatch: name \"%s\", buffer size %zu\n",
147e83
+                  item->pw_name, buffer_size);
147e83
+          errors = true;
147e83
+        }
147e83
+    }
147e83
+  else if (ret != ERANGE)
147e83
+    {
147e83
+      errno = ret;
147e83
+      printf ("error: lookup failure for name \"%s\": %m (%d)\n",
147e83
+              item->pw_name, ret);
147e83
+      errors = true;
147e83
+    }
147e83
+
147e83
+  /* Test getpwuid_r.  */
147e83
+  memset (buffer, pad, buffer_size + padding_size);
147e83
+  pwd = (struct passwd) {};
147e83
+  ret = getpwuid_r (item->pw_uid, &pwd, buffer, buffer_size, &result);
147e83
+  if (!check_padding (buffer + buffer_size, padding_size, pad))
147e83
+    {
147e83
+      printf ("error: padding change: "
147e83
+              "UID %ld, buffer size %zu, padding size %zu, pad 0x%02x\n",
147e83
+              (long) item->pw_uid, buffer_size, padding_size,
147e83
+              (unsigned char) pad);
147e83
+      errors = true;
147e83
+    }
147e83
+  if (ret == 0)
147e83
+    {
147e83
+      if (result == NULL)
147e83
+        {
147e83
+          printf ("error: no data: UID %ld, buffer size %zu\n",
147e83
+                  (long) item->pw_uid, buffer_size);
147e83
+          errors = true;
147e83
+        }
147e83
+      else if (!equal (item, result))
147e83
+        {
147e83
+          printf ("error: lookup mismatch: UID %ld, buffer size %zu\n",
147e83
+                  (long) item->pw_uid, buffer_size);
147e83
+          errors = true;
147e83
+        }
147e83
+    }
147e83
+  else if (ret != ERANGE)
147e83
+    {
147e83
+      errno = ret;
147e83
+      printf ("error: lookup failure for UID \"%ld\": %m (%d)\n",
147e83
+              (long) item->pw_uid, ret);
147e83
+      errors = true;
147e83
+    }
147e83
+
147e83
+  free (buffer);
147e83
+}
147e83
+
147e83
+/* Test one buffer size with different paddings.  */
147e83
+static void
147e83
+test_buffer_size (size_t buffer_size)
147e83
+{
147e83
+  for (int i = 0; i < test_count; ++i)
147e83
+    for (size_t padding_size = 0; padding_size < 3; ++padding_size)
147e83
+      {
147e83
+        test_one (test_items + i, buffer_size, '\0', padding_size);
147e83
+        if (padding_size > 0)
147e83
+          {
147e83
+            test_one (test_items + i, buffer_size, ':', padding_size);
147e83
+            test_one (test_items + i, buffer_size, '\n', padding_size);
147e83
+            test_one (test_items + i, buffer_size, '\xff', padding_size);
147e83
+            test_one (test_items + i, buffer_size, '@', padding_size);
147e83
+          }
147e83
+      }
147e83
+}
147e83
+
147e83
+int
147e83
+do_test (void)
147e83
+{
147e83
+  if (!init_test_items ())
147e83
+    return 1;
147e83
+  printf ("info: %d test items\n", test_count);
147e83
+
147e83
+  for (size_t buffer_size = 0; buffer_size <= 65; ++buffer_size)
147e83
+    test_buffer_size (buffer_size);
147e83
+  for (size_t buffer_size = 64 + 4; buffer_size < 256; buffer_size += 4)
147e83
+    test_buffer_size (buffer_size);
147e83
+  test_buffer_size (255);
147e83
+  test_buffer_size (257);
147e83
+  for (size_t buffer_size = 256; buffer_size < 512; buffer_size += 8)
147e83
+    test_buffer_size (buffer_size);
147e83
+  test_buffer_size (511);
147e83
+  test_buffer_size (513);
147e83
+  test_buffer_size (1024);
147e83
+  test_buffer_size (2048);
147e83
+
147e83
+  if (errors)
147e83
+    return 1;
147e83
+  else
147e83
+    return 0;
147e83
+}
147e83
+
147e83
+#define TEST_FUNCTION do_test ()
147e83
+#include "../test-skeleton.c"
147e83
diff a/nss/Makefile b/nss/Makefile
147e83
--- a/nss/Makefile
147e83
+++ b/nss/Makefile
147e83
@@ -39,6 +39,6 @@
147e83
 extra-objs		+= $(makedb-modules:=.o)
147e83
 
147e83
-tests			= test-netdb tst-nss-test1
147e83
+tests			= test-netdb tst-nss-test1 bug17079
147e83
 xtests			= bug-erange
147e83
 
147e83
 include ../Makeconfig