ce426f
commit bae7c7c764413b23e61cb099ce33be4c4ee259bb
ce426f
Author: Florian Weimer <fweimer@redhat.com>
ce426f
Date:   Thu Jan 28 13:59:11 2016 +0100
ce426f
ce426f
    Improve check against integer wraparound in hcreate_r [BZ #18240]
ce426f
ce426f
commit 2f5c1750558fe64bac361f52d6827ab1bcfe52bc
ce426f
Author: Ondřej Bílka <neleai@seznam.cz>
ce426f
Date:   Sat Jul 11 17:44:10 2015 +0200
ce426f
ce426f
    Handle overflow in __hcreate_r
ce426f
ce426f
--- glibc-2.17-c758a686/misc/hsearch_r.c
ce426f
+++ glibc-2.17-c758a686/misc/hsearch_r.c
ce426f
@@ -20,7 +20,7 @@
ce426f
 #include <errno.h>
ce426f
 #include <malloc.h>
ce426f
 #include <string.h>
ce426f
-
ce426f
+#include <stdint.h>
ce426f
 #include <search.h>
ce426f
 
ce426f
 /* [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
ce426f
@@ -47,12 +47,10 @@
ce426f
 isprime (unsigned int number)
ce426f
 {
ce426f
   /* no even number will be passed */
ce426f
-  unsigned int div = 3;
ce426f
-
ce426f
-  while (div * div < number && number % div != 0)
ce426f
-    div += 2;
ce426f
-
ce426f
-  return number % div != 0;
ce426f
+  for (unsigned int div = 3; div <= number / div; div += 2)
ce426f
+    if (number % div == 0)
ce426f
+      return 0;
ce426f
+  return 1;
ce426f
 }
ce426f
 
ce426f
 
ce426f
@@ -74,6 +72,12 @@
ce426f
       return 0;
ce426f
     }
ce426f
 
ce426f
+  if (nel >= SIZE_MAX / sizeof (_ENTRY))
ce426f
+    {
ce426f
+      __set_errno (ENOMEM);
ce426f
+      return 0;
ce426f
+    }
ce426f
+
ce426f
   /* There is still another table active. Return with error. */
ce426f
   if (htab->table != NULL)
ce426f
     return 0;
ce426f
@@ -82,10 +86,19 @@
ce426f
      use will not work.  */
ce426f
   if (nel < 3)
ce426f
     nel = 3;
ce426f
-  /* Change nel to the first prime number not smaller as nel. */
ce426f
-  nel |= 1;      /* make odd */
ce426f
-  while (!isprime (nel))
ce426f
-    nel += 2;
ce426f
+
ce426f
+  /* Change nel to the first prime number in the range [nel, UINT_MAX - 2],
ce426f
+     The '- 2' means 'nel += 2' cannot overflow.  */
ce426f
+  for (nel |= 1; ; nel += 2)
ce426f
+    {
ce426f
+      if (UINT_MAX - 2 < nel)
ce426f
+        {
ce426f
+          __set_errno (ENOMEM);
ce426f
+          return 0;
ce426f
+        }
ce426f
+      if (isprime (nel))
ce426f
+        break;
ce426f
+    }
ce426f
 
ce426f
   htab->size = nel;
ce426f
   htab->filled = 0;
ce426f
--- /dev/null
ce426f
+++ glibc-2.17-c758a686/misc/bug18240.c
ce426f
@@ -0,0 +1,97 @@
ce426f
+/* Test integer wraparound in hcreate.
ce426f
+   Copyright (C) 2016 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 <limits.h>
ce426f
+#include <search.h>
ce426f
+#include <stdbool.h>
ce426f
+#include <stdio.h>
ce426f
+#include <stdlib.h>
ce426f
+#include <sys/resource.h>
ce426f
+
ce426f
+static void
ce426f
+test_size (size_t size)
ce426f
+{
ce426f
+  int res = hcreate (size);
ce426f
+  if (res == 0)
ce426f
+    {
ce426f
+      if (errno == ENOMEM)
ce426f
+        return;
ce426f
+      printf ("error: hcreate (%zu): %m\n", size);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  char *keys[100];
ce426f
+  for (int i = 0; i < 100; ++i)
ce426f
+    {
ce426f
+      if (asprintf (keys + i, "%d", i) < 0)
ce426f
+        {
ce426f
+          printf ("error: asprintf: %m\n");
ce426f
+          exit (1);
ce426f
+        }
ce426f
+      ENTRY e = { keys[i], (char *) "value" };
ce426f
+      if (hsearch (e, ENTER) == NULL)
ce426f
+        {
ce426f
+          printf ("error: hsearch (\"%s\"): %m\n", keys[i]);
ce426f
+          exit (1);
ce426f
+        }
ce426f
+    }
ce426f
+  hdestroy ();
ce426f
+
ce426f
+  for (int i = 0; i < 100; ++i)
ce426f
+    free (keys[i]);
ce426f
+}
ce426f
+
ce426f
+static int
ce426f
+do_test (void)
ce426f
+{
ce426f
+  /* Limit the size of the process, so that memory allocation will
ce426f
+     fail without impacting the entire system.  */
ce426f
+  {
ce426f
+    struct rlimit limit;
ce426f
+    if (getrlimit (RLIMIT_AS, &limit) != 0)
ce426f
+      {
ce426f
+        printf ("getrlimit (RLIMIT_AS) failed: %m\n");
ce426f
+        return 1;
ce426f
+      }
ce426f
+    long target = 100 * 1024 * 1024;
ce426f
+    if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > target)
ce426f
+      {
ce426f
+        limit.rlim_cur = target;
ce426f
+        if (setrlimit (RLIMIT_AS, &limit) != 0)
ce426f
+          {
ce426f
+            printf ("setrlimit (RLIMIT_AS) failed: %m\n");
ce426f
+            return 1;
ce426f
+          }
ce426f
+      }
ce426f
+  }
ce426f
+
ce426f
+  test_size (500);
ce426f
+  test_size (-1);
ce426f
+  test_size (-3);
ce426f
+  test_size (INT_MAX - 2);
ce426f
+  test_size (INT_MAX - 1);
ce426f
+  test_size (INT_MAX);
ce426f
+  test_size (((unsigned) INT_MAX) + 1);
ce426f
+  test_size (UINT_MAX - 2);
ce426f
+  test_size (UINT_MAX - 1);
ce426f
+  test_size (UINT_MAX);
ce426f
+  return 0;
ce426f
+}
ce426f
+
ce426f
+#define TEST_FUNCTION do_test ()
ce426f
+#include "../test-skeleton.c"
ce426f
--- glibc-2.17-c758a686/misc/Makefile
ce426f
+++ glibc-2.17-c758a686/misc/Makefile
ce426f
@@ -76,7 +76,7 @@
ce426f
 gpl2lgpl := error.c error.h
ce426f
 
ce426f
 tests := tst-dirname tst-tsearch tst-fdset tst-efgcvt tst-mntent tst-hsearch \
ce426f
-	 tst-error1 tst-pselect tst-insremque tst-mntent2 bug-hsearch1
ce426f
+	 tst-error1 tst-pselect tst-insremque tst-mntent2 bug-hsearch1 bug18240
ce426f
 ifeq ($(run-built-tests),yes)
ce426f
 tests: $(objpfx)tst-error1-mem
ce426f
 endif