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