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