00db10
commit bae7c7c764413b23e61cb099ce33be4c4ee259bb
00db10
Author: Florian Weimer <fweimer@redhat.com>
00db10
Date:   Thu Jan 28 13:59:11 2016 +0100
00db10
00db10
    Improve check against integer wraparound in hcreate_r [BZ #18240]
00db10
00db10
commit 2f5c1750558fe64bac361f52d6827ab1bcfe52bc
00db10
Author: Ondřej Bílka <neleai@seznam.cz>
00db10
Date:   Sat Jul 11 17:44:10 2015 +0200
00db10
00db10
    Handle overflow in __hcreate_r
00db10
00db10
--- glibc-2.17-c758a686/misc/hsearch_r.c
00db10
+++ glibc-2.17-c758a686/misc/hsearch_r.c
00db10
@@ -20,7 +20,7 @@
00db10
 #include <errno.h>
00db10
 #include <malloc.h>
00db10
 #include <string.h>
00db10
-
00db10
+#include <stdint.h>
00db10
 #include <search.h>
00db10
 
00db10
 /* [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
00db10
@@ -47,12 +47,10 @@
00db10
 isprime (unsigned int number)
00db10
 {
00db10
   /* no even number will be passed */
00db10
-  unsigned int div = 3;
00db10
-
00db10
-  while (div * div < number && number % div != 0)
00db10
-    div += 2;
00db10
-
00db10
-  return number % div != 0;
00db10
+  for (unsigned int div = 3; div <= number / div; div += 2)
00db10
+    if (number % div == 0)
00db10
+      return 0;
00db10
+  return 1;
00db10
 }
00db10
 
00db10
 
00db10
@@ -74,6 +72,12 @@
00db10
       return 0;
00db10
     }
00db10
 
00db10
+  if (nel >= SIZE_MAX / sizeof (_ENTRY))
00db10
+    {
00db10
+      __set_errno (ENOMEM);
00db10
+      return 0;
00db10
+    }
00db10
+
00db10
   /* There is still another table active. Return with error. */
00db10
   if (htab->table != NULL)
00db10
     return 0;
00db10
@@ -82,10 +86,19 @@
00db10
      use will not work.  */
00db10
   if (nel < 3)
00db10
     nel = 3;
00db10
-  /* Change nel to the first prime number not smaller as nel. */
00db10
-  nel |= 1;      /* make odd */
00db10
-  while (!isprime (nel))
00db10
-    nel += 2;
00db10
+
00db10
+  /* Change nel to the first prime number in the range [nel, UINT_MAX - 2],
00db10
+     The '- 2' means 'nel += 2' cannot overflow.  */
00db10
+  for (nel |= 1; ; nel += 2)
00db10
+    {
00db10
+      if (UINT_MAX - 2 < nel)
00db10
+        {
00db10
+          __set_errno (ENOMEM);
00db10
+          return 0;
00db10
+        }
00db10
+      if (isprime (nel))
00db10
+        break;
00db10
+    }
00db10
 
00db10
   htab->size = nel;
00db10
   htab->filled = 0;
00db10
--- /dev/null
00db10
+++ glibc-2.17-c758a686/misc/bug18240.c
00db10
@@ -0,0 +1,97 @@
00db10
+/* Test integer wraparound in hcreate.
00db10
+   Copyright (C) 2016 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <errno.h>
00db10
+#include <limits.h>
00db10
+#include <search.h>
00db10
+#include <stdbool.h>
00db10
+#include <stdio.h>
00db10
+#include <stdlib.h>
00db10
+#include <sys/resource.h>
00db10
+
00db10
+static void
00db10
+test_size (size_t size)
00db10
+{
00db10
+  int res = hcreate (size);
00db10
+  if (res == 0)
00db10
+    {
00db10
+      if (errno == ENOMEM)
00db10
+        return;
00db10
+      printf ("error: hcreate (%zu): %m\n", size);
00db10
+      exit (1);
00db10
+    }
00db10
+  char *keys[100];
00db10
+  for (int i = 0; i < 100; ++i)
00db10
+    {
00db10
+      if (asprintf (keys + i, "%d", i) < 0)
00db10
+        {
00db10
+          printf ("error: asprintf: %m\n");
00db10
+          exit (1);
00db10
+        }
00db10
+      ENTRY e = { keys[i], (char *) "value" };
00db10
+      if (hsearch (e, ENTER) == NULL)
00db10
+        {
00db10
+          printf ("error: hsearch (\"%s\"): %m\n", keys[i]);
00db10
+          exit (1);
00db10
+        }
00db10
+    }
00db10
+  hdestroy ();
00db10
+
00db10
+  for (int i = 0; i < 100; ++i)
00db10
+    free (keys[i]);
00db10
+}
00db10
+
00db10
+static int
00db10
+do_test (void)
00db10
+{
00db10
+  /* Limit the size of the process, so that memory allocation will
00db10
+     fail without impacting the entire system.  */
00db10
+  {
00db10
+    struct rlimit limit;
00db10
+    if (getrlimit (RLIMIT_AS, &limit) != 0)
00db10
+      {
00db10
+        printf ("getrlimit (RLIMIT_AS) failed: %m\n");
00db10
+        return 1;
00db10
+      }
00db10
+    long target = 100 * 1024 * 1024;
00db10
+    if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > target)
00db10
+      {
00db10
+        limit.rlim_cur = target;
00db10
+        if (setrlimit (RLIMIT_AS, &limit) != 0)
00db10
+          {
00db10
+            printf ("setrlimit (RLIMIT_AS) failed: %m\n");
00db10
+            return 1;
00db10
+          }
00db10
+      }
00db10
+  }
00db10
+
00db10
+  test_size (500);
00db10
+  test_size (-1);
00db10
+  test_size (-3);
00db10
+  test_size (INT_MAX - 2);
00db10
+  test_size (INT_MAX - 1);
00db10
+  test_size (INT_MAX);
00db10
+  test_size (((unsigned) INT_MAX) + 1);
00db10
+  test_size (UINT_MAX - 2);
00db10
+  test_size (UINT_MAX - 1);
00db10
+  test_size (UINT_MAX);
00db10
+  return 0;
00db10
+}
00db10
+
00db10
+#define TEST_FUNCTION do_test ()
00db10
+#include "../test-skeleton.c"
00db10
--- glibc-2.17-c758a686/misc/Makefile
00db10
+++ glibc-2.17-c758a686/misc/Makefile
00db10
@@ -76,7 +76,7 @@
00db10
 gpl2lgpl := error.c error.h
00db10
 
00db10
 tests := tst-dirname tst-tsearch tst-fdset tst-efgcvt tst-mntent tst-hsearch \
00db10
-	 tst-error1 tst-pselect tst-insremque tst-mntent2 bug-hsearch1
00db10
+	 tst-error1 tst-pselect tst-insremque tst-mntent2 bug-hsearch1 bug18240
00db10
 ifeq ($(run-built-tests),yes)
00db10
 tests: $(objpfx)tst-error1-mem
00db10
 endif