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