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
f57669
(The previous commit fixes upstream bug 16071.)
f57669
f57669
commit ac60763eac3d43b7234dd21286ad3ec3f17957fc
f57669
Author: Andreas Schwab <schwab@suse.de>
f57669
Date:   Mon Jun 23 10:24:45 2014 +0200
f57669
f57669
    Don't ignore too long lines in nss_files (BZ #17079)
f57669
f57669
commit e07aabba73ea62e7dfa0512507c92efb851fbdbe
f57669
Author: Florian Weimer <fweimer@redhat.com>
f57669
Date:   Tue Sep 22 13:20:18 2015 +0200
f57669
f57669
    Add test case for bug 18287
f57669
f57669
commit 90fa42a1d7b78de0d75f7e3af362275b2abe807f
f57669
Author: Florian Weimer <fweimer@redhat.com>
f57669
Date:   Tue Sep 22 13:40:17 2015 +0200
f57669
f57669
    Test in commit e07aabba73ea62e7dfa0512507c92efb851fbdbe is for bug 17079
f57669
f57669
diff -u b/nss/nss_files/files-XXX.c b/nss/nss_files/files-XXX.c
f57669
--- b/nss/nss_files/files-XXX.c
f57669
+++ b/nss/nss_files/files-XXX.c
f57669
@@ -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
+
f57669
+      /* Terminate the line so that we can test for overflow.  */
2e9afc
+      ((unsigned char *) curbuf)[curlen - 1] = 0xff;
2e9afc
+
f57669
+      char *p = fgets_unlocked (curbuf, curlen, stream);
f57669
+
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
f57669
@@ -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)
f57669
@@ -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.  */
f57669
@@ -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
     }
f57669
f57669
diff a/nss/bug17079.c b/nss/bug17079.c
f57669
--- /dev/null
f57669
+++ b/nss/bug17079.c
f57669
@@ -0,0 +1,236 @@
f57669
+/* Test for bug 17079: heap overflow in NSS with small buffers.
f57669
+   Copyright (C) 2015 Free Software Foundation, Inc.
f57669
+   This file is part of the GNU C Library.
f57669
+
f57669
+   The GNU C Library is free software; you can redistribute it and/or
f57669
+   modify it under the terms of the GNU Lesser General Public
f57669
+   License as published by the Free Software Foundation; either
f57669
+   version 2.1 of the License, or (at your option) any later version.
f57669
+
f57669
+   The GNU C Library is distributed in the hope that it will be useful,
f57669
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
f57669
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
f57669
+   Lesser General Public License for more details.
f57669
+
f57669
+   You should have received a copy of the GNU Lesser General Public
f57669
+   License along with the GNU C Library; if not, see
f57669
+   <http://www.gnu.org/licenses/>.  */
f57669
+
f57669
+#include <errno.h>
f57669
+#include <pwd.h>
f57669
+#include <stdbool.h>
f57669
+#include <stdio.h>
f57669
+#include <stdlib.h>
f57669
+#include <string.h>
f57669
+
f57669
+/* Check if two passwd structs contain the same data.  */
f57669
+static bool
f57669
+equal (const struct passwd *a, const struct passwd *b)
f57669
+{
f57669
+  return strcmp (a->pw_name, b->pw_name) == 0
f57669
+    && strcmp (a->pw_passwd, b->pw_passwd) == 0
f57669
+    && a->pw_uid == b->pw_uid
f57669
+    && a->pw_gid == b->pw_gid
f57669
+    && strcmp (a->pw_gecos, b->pw_gecos) == 0
f57669
+    && strcmp (a->pw_dir, b->pw_dir) == 0
f57669
+    && strcmp (a->pw_shell, b->pw_shell) == 0;
f57669
+}
f57669
+
f57669
+enum { MAX_TEST_ITEMS = 10 };
f57669
+static struct passwd test_items[MAX_TEST_ITEMS];
f57669
+static int test_count;
f57669
+
f57669
+/* Initialize test_items and test_count above, with data from the
f57669
+   passwd database.  */
f57669
+static bool
f57669
+init_test_items (void)
f57669
+{
f57669
+  setpwent ();
f57669
+  do
f57669
+    {
f57669
+      struct passwd *pwd = getpwent ();
f57669
+      if (pwd == NULL)
f57669
+        break;
f57669
+      struct passwd *target = test_items + test_count;
f57669
+      target->pw_name = strdup (pwd->pw_name);
f57669
+      target->pw_passwd = strdup (pwd->pw_passwd);
f57669
+      target->pw_uid = pwd->pw_uid;
f57669
+      target->pw_gid = pwd->pw_gid;
f57669
+      target->pw_gecos = strdup (pwd->pw_gecos);
f57669
+      target->pw_dir = strdup (pwd->pw_dir);
f57669
+      target->pw_shell = strdup (pwd->pw_shell);
f57669
+    }
f57669
+  while (++test_count < MAX_TEST_ITEMS);
f57669
+  endpwent ();
f57669
+
f57669
+  /* Filter out those test items which cannot be looked up by name or
f57669
+     UID.  */
f57669
+  bool found = false;
f57669
+  for (int i = 0; i < test_count; ++i)
f57669
+    {
f57669
+      struct passwd *pwd1 = getpwnam (test_items[i].pw_name);
f57669
+      struct passwd *pwd2 = getpwuid (test_items[i].pw_uid);
f57669
+      if (pwd1 == NULL || !equal (pwd1, test_items + i)
f57669
+          || pwd2 == NULL || !equal (pwd2, test_items + i))
f57669
+        test_items[i].pw_name = NULL;
f57669
+      else
f57669
+        found = true;
f57669
+    }
f57669
+
f57669
+  if (!found)
f57669
+    puts ("error: no accounts found which can be looked up by name and UID.");
f57669
+  return found;
f57669
+}
f57669
+
f57669
+/* Set to true if an error is encountered.  */
f57669
+static bool errors;
f57669
+
f57669
+/* Return true if the padding has not been tampered with.  */
f57669
+static bool
f57669
+check_padding (char *buffer, size_t size, char pad)
f57669
+{
f57669
+  char *end = buffer + size;
f57669
+  while (buffer < end)
f57669
+    {
f57669
+      if (*buffer != pad)
f57669
+        return false;
f57669
+      ++buffer;
f57669
+    }
f57669
+  return true;
f57669
+}
f57669
+
f57669
+/* Test one buffer size and padding combination.  */
f57669
+static void
f57669
+test_one (const struct passwd *item, size_t buffer_size,
f57669
+           char pad, size_t padding_size)
f57669
+{
f57669
+  char *buffer = malloc (buffer_size + padding_size);
f57669
+  if (buffer == NULL)
f57669
+    {
f57669
+      puts ("error: malloc failure");
f57669
+      errors = true;
f57669
+      return;
f57669
+    }
f57669
+
f57669
+  struct passwd pwd;
f57669
+  struct passwd *result;
f57669
+  int ret;
f57669
+
f57669
+  /* Test getpwname_r.  */
f57669
+  memset (buffer, pad, buffer_size + padding_size);
f57669
+  pwd = (struct passwd) {};
f57669
+  ret = getpwnam_r (item->pw_name, &pwd, buffer, buffer_size, &result);
f57669
+  if (!check_padding (buffer + buffer_size, padding_size, pad))
f57669
+    {
f57669
+      printf ("error: padding change: "
f57669
+              "name \"%s\", buffer size %zu, padding size %zu, pad 0x%02x\n",
f57669
+              item->pw_name, buffer_size, padding_size, (unsigned char) pad);
f57669
+      errors = true;
f57669
+    }
f57669
+  if (ret == 0)
f57669
+    {
f57669
+      if (result == NULL)
f57669
+        {
f57669
+          printf ("error: no data: name \"%s\", buffer size %zu\n",
f57669
+                  item->pw_name, buffer_size);
f57669
+          errors = true;
f57669
+        }
f57669
+      else if (!equal (item, result))
f57669
+        {
f57669
+          printf ("error: lookup mismatch: name \"%s\", buffer size %zu\n",
f57669
+                  item->pw_name, buffer_size);
f57669
+          errors = true;
f57669
+        }
f57669
+    }
f57669
+  else if (ret != ERANGE)
f57669
+    {
f57669
+      errno = ret;
f57669
+      printf ("error: lookup failure for name \"%s\": %m (%d)\n",
f57669
+              item->pw_name, ret);
f57669
+      errors = true;
f57669
+    }
f57669
+
f57669
+  /* Test getpwuid_r.  */
f57669
+  memset (buffer, pad, buffer_size + padding_size);
f57669
+  pwd = (struct passwd) {};
f57669
+  ret = getpwuid_r (item->pw_uid, &pwd, buffer, buffer_size, &result);
f57669
+  if (!check_padding (buffer + buffer_size, padding_size, pad))
f57669
+    {
f57669
+      printf ("error: padding change: "
f57669
+              "UID %ld, buffer size %zu, padding size %zu, pad 0x%02x\n",
f57669
+              (long) item->pw_uid, buffer_size, padding_size,
f57669
+              (unsigned char) pad);
f57669
+      errors = true;
f57669
+    }
f57669
+  if (ret == 0)
f57669
+    {
f57669
+      if (result == NULL)
f57669
+        {
f57669
+          printf ("error: no data: UID %ld, buffer size %zu\n",
f57669
+                  (long) item->pw_uid, buffer_size);
f57669
+          errors = true;
f57669
+        }
f57669
+      else if (!equal (item, result))
f57669
+        {
f57669
+          printf ("error: lookup mismatch: UID %ld, buffer size %zu\n",
f57669
+                  (long) item->pw_uid, buffer_size);
f57669
+          errors = true;
f57669
+        }
f57669
+    }
f57669
+  else if (ret != ERANGE)
f57669
+    {
f57669
+      errno = ret;
f57669
+      printf ("error: lookup failure for UID \"%ld\": %m (%d)\n",
f57669
+              (long) item->pw_uid, ret);
f57669
+      errors = true;
f57669
+    }
f57669
+
f57669
+  free (buffer);
f57669
+}
f57669
+
f57669
+/* Test one buffer size with different paddings.  */
f57669
+static void
f57669
+test_buffer_size (size_t buffer_size)
f57669
+{
f57669
+  for (int i = 0; i < test_count; ++i)
f57669
+    for (size_t padding_size = 0; padding_size < 3; ++padding_size)
f57669
+      {
f57669
+        test_one (test_items + i, buffer_size, '\0', padding_size);
f57669
+        if (padding_size > 0)
f57669
+          {
f57669
+            test_one (test_items + i, buffer_size, ':', padding_size);
f57669
+            test_one (test_items + i, buffer_size, '\n', padding_size);
f57669
+            test_one (test_items + i, buffer_size, '\xff', padding_size);
f57669
+            test_one (test_items + i, buffer_size, '@', padding_size);
f57669
+          }
f57669
+      }
f57669
+}
f57669
+
f57669
+int
f57669
+do_test (void)
f57669
+{
f57669
+  if (!init_test_items ())
f57669
+    return 1;
f57669
+  printf ("info: %d test items\n", test_count);
f57669
+
f57669
+  for (size_t buffer_size = 0; buffer_size <= 65; ++buffer_size)
f57669
+    test_buffer_size (buffer_size);
f57669
+  for (size_t buffer_size = 64 + 4; buffer_size < 256; buffer_size += 4)
f57669
+    test_buffer_size (buffer_size);
f57669
+  test_buffer_size (255);
f57669
+  test_buffer_size (257);
f57669
+  for (size_t buffer_size = 256; buffer_size < 512; buffer_size += 8)
f57669
+    test_buffer_size (buffer_size);
f57669
+  test_buffer_size (511);
f57669
+  test_buffer_size (513);
f57669
+  test_buffer_size (1024);
f57669
+  test_buffer_size (2048);
f57669
+
f57669
+  if (errors)
f57669
+    return 1;
f57669
+  else
f57669
+    return 0;
f57669
+}
f57669
+
f57669
+#define TEST_FUNCTION do_test ()
f57669
+#include "../test-skeleton.c"
f57669
diff a/nss/Makefile b/nss/Makefile
f57669
--- a/nss/Makefile
f57669
+++ b/nss/Makefile
f57669
@@ -39,6 +39,6 @@
f57669
 extra-objs		+= $(makedb-modules:=.o)
f57669
 
f57669
-tests			= test-netdb tst-nss-test1
f57669
+tests			= test-netdb tst-nss-test1 bug17079
f57669
 xtests			= bug-erange
f57669
 
f57669
 include ../Makeconfig