00db10
The upstream patch is backported by excluding tests for reallocarray because
00db10
this function is not present in RHEL-7.
00db10
00db10
commit 8e448310d74b283c5cd02b9ed7fb997b47bf9b22
00db10
Author: Arjun Shankar <arjun.is@lostca.se>
00db10
Date:   Thu Jan 18 16:47:06 2018 +0000
00db10
00db10
    Fix integer overflows in internal memalign and malloc functions [BZ #22343]
00db10
    
00db10
    When posix_memalign is called with an alignment less than MALLOC_ALIGNMENT
00db10
    and a requested size close to SIZE_MAX, it falls back to malloc code
00db10
    (because the alignment of a block returned by malloc is sufficient to
00db10
    satisfy the call).  In this case, an integer overflow in _int_malloc leads
00db10
    to posix_memalign incorrectly returning successfully.
00db10
    
00db10
    Upon fixing this and writing a somewhat thorough regression test, it was
00db10
    discovered that when posix_memalign is called with an alignment larger than
00db10
    MALLOC_ALIGNMENT (so it uses _int_memalign instead) and a requested size
00db10
    close to SIZE_MAX, a different integer overflow in _int_memalign leads to
00db10
    posix_memalign incorrectly returning successfully.
00db10
    
00db10
    Both integer overflows affect other memory allocation functions that use
00db10
    _int_malloc (one affected malloc in x86) or _int_memalign as well.
00db10
    
00db10
    This commit fixes both integer overflows.  In addition to this, it adds a
00db10
    regression test to guard against false successful allocations by the
00db10
    following memory allocation functions when called with too-large allocation
00db10
    sizes and, where relevant, various valid alignments:
00db10
    malloc, realloc, calloc, reallocarray, memalign, posix_memalign,
00db10
    aligned_alloc, valloc, and pvalloc.
00db10
00db10
Index: b/malloc/Makefile
00db10
===================================================================
00db10
--- a/malloc/Makefile
00db10
+++ b/malloc/Makefile
00db10
@@ -38,6 +38,7 @@ tests := mallocbug tst-malloc tst-valloc
00db10
 	 tst-dynarray-fail \
00db10
 	 tst-dynarray-at-fail \
00db10
 	 tst-alloc_buffer \
00db10
+	 tst-malloc-too-large \
00db10
 
00db10
 tests-static := \
00db10
 	 tst-interpose-static-nothread \
00db10
Index: b/malloc/malloc.c
00db10
===================================================================
00db10
--- a/malloc/malloc.c
00db10
+++ b/malloc/malloc.c
00db10
@@ -1273,14 +1273,21 @@ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-
00db10
    MINSIZE :                                                      \
00db10
    ((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)
00db10
 
00db10
-/*  Same, except also perform argument check */
00db10
-
00db10
-#define checked_request2size(req, sz)                             \
00db10
-  if (REQUEST_OUT_OF_RANGE(req)) {                                \
00db10
-    __set_errno (ENOMEM);					  \
00db10
-    return 0;                                                     \
00db10
-  }                                                               \
00db10
-  (sz) = request2size(req);
00db10
+/* Same, except also perform an argument and result check.  First, we check
00db10
+   that the padding done by request2size didn't result in an integer
00db10
+   overflow.  Then we check (using REQUEST_OUT_OF_RANGE) that the resulting
00db10
+   size isn't so large that a later alignment would lead to another integer
00db10
+   overflow.  */
00db10
+#define checked_request2size(req, sz) \
00db10
+({				    \
00db10
+  (sz) = request2size (req);	    \
00db10
+  if (((sz) < (req))		    \
00db10
+      || REQUEST_OUT_OF_RANGE (sz)) \
00db10
+    {				    \
00db10
+      __set_errno (ENOMEM);	    \
00db10
+      return 0;			    \
00db10
+    }				    \
00db10
+})
00db10
 
00db10
 /*
00db10
   --------------- Physical chunk operations ---------------
00db10
@@ -4389,6 +4396,13 @@ _int_memalign(mstate av, size_t alignmen
00db10
   */
00db10
 
00db10
 
00db10
+  /* Check for overflow.  */
00db10
+  if (nb > SIZE_MAX - alignment - MINSIZE)
00db10
+    {
00db10
+      __set_errno (ENOMEM);
00db10
+      return 0;
00db10
+    }
00db10
+
00db10
   /* Call malloc with worst case padding to hit alignment. */
00db10
 
00db10
   m  = (char*)(_int_malloc(av, nb + alignment + MINSIZE));
00db10
Index: b/malloc/tst-malloc-too-large.c
00db10
===================================================================
00db10
--- /dev/null
00db10
+++ b/malloc/tst-malloc-too-large.c
00db10
@@ -0,0 +1,237 @@
00db10
+/* Test and verify that too-large memory allocations fail with ENOMEM.
00db10
+   Copyright (C) 2018 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
+/* Bug 22375 reported a regression in malloc where if after malloc'ing then
00db10
+   free'ing a small block of memory, malloc is then called with a really
00db10
+   large size argument (close to SIZE_MAX): instead of returning NULL and
00db10
+   setting errno to ENOMEM, malloc incorrectly returns the previously
00db10
+   allocated block instead.  Bug 22343 reported a similar case where
00db10
+   posix_memalign incorrectly returns successfully when called with an with
00db10
+   a really large size argument.
00db10
+
00db10
+   Both of these were caused by integer overflows in the allocator when it
00db10
+   was trying to pad the requested size to allow for book-keeping or
00db10
+   alignment.  This test guards against such bugs by repeatedly allocating
00db10
+   and freeing small blocks of memory then trying to allocate various block
00db10
+   sizes larger than the memory bus width of 64-bit targets, or almost
00db10
+   as large as SIZE_MAX on 32-bit targets supported by glibc.  In each case,
00db10
+   it verifies that such impossibly large allocations correctly fail.  */
00db10
+
00db10
+
00db10
+#include <stdlib.h>
00db10
+#include <malloc.h>
00db10
+#include <errno.h>
00db10
+#include <stdint.h>
00db10
+#include <sys/resource.h>
00db10
+#include <libc-diag.h>
00db10
+#include <support/check.h>
00db10
+#include <unistd.h>
00db10
+#include <sys/param.h>
00db10
+
00db10
+
00db10
+/* This function prepares for each 'too-large memory allocation' test by
00db10
+   performing a small successful malloc/free and resetting errno prior to
00db10
+   the actual test.  */
00db10
+static void
00db10
+test_setup (void)
00db10
+{
00db10
+  void *volatile ptr = malloc (16);
00db10
+  TEST_VERIFY_EXIT (ptr != NULL);
00db10
+  free (ptr);
00db10
+  errno = 0;
00db10
+}
00db10
+
00db10
+
00db10
+/* This function tests each of:
00db10
+   - malloc (SIZE)
00db10
+   - realloc (PTR_FOR_REALLOC, SIZE)
00db10
+   - for various values of NMEMB:
00db10
+    - calloc (NMEMB, SIZE/NMEMB)
00db10
+    - calloc (SIZE/NMEMB, NMEMB)
00db10
+   and precedes each of these tests with a small malloc/free before it.  */
00db10
+static void
00db10
+test_large_allocations (size_t size)
00db10
+{
00db10
+  void * ptr_to_realloc;
00db10
+
00db10
+  test_setup ();
00db10
+  TEST_VERIFY (malloc (size) == NULL);
00db10
+  TEST_VERIFY (errno == ENOMEM);
00db10
+
00db10
+  ptr_to_realloc = malloc (16);
00db10
+  TEST_VERIFY_EXIT (ptr_to_realloc != NULL);
00db10
+  test_setup ();
00db10
+  TEST_VERIFY (realloc (ptr_to_realloc, size) == NULL);
00db10
+  TEST_VERIFY (errno == ENOMEM);
00db10
+  free (ptr_to_realloc);
00db10
+
00db10
+  for (size_t nmemb = 1; nmemb <= 8; nmemb *= 2)
00db10
+    if ((size % nmemb) == 0)
00db10
+      {
00db10
+        test_setup ();
00db10
+        TEST_VERIFY (calloc (nmemb, size / nmemb) == NULL);
00db10
+        TEST_VERIFY (errno == ENOMEM);
00db10
+
00db10
+        test_setup ();
00db10
+        TEST_VERIFY (calloc (size / nmemb, nmemb) == NULL);
00db10
+        TEST_VERIFY (errno == ENOMEM);
00db10
+      }
00db10
+    else
00db10
+      break;
00db10
+}
00db10
+
00db10
+
00db10
+static long pagesize;
00db10
+
00db10
+/* This function tests the following aligned memory allocation functions
00db10
+   using several valid alignments and precedes each allocation test with a
00db10
+   small malloc/free before it:
00db10
+   memalign, posix_memalign, aligned_alloc, valloc, pvalloc.  */
00db10
+static void
00db10
+test_large_aligned_allocations (size_t size)
00db10
+{
00db10
+  /* ptr stores the result of posix_memalign but since all those calls
00db10
+     should fail, posix_memalign should never change ptr.  We set it to
00db10
+     NULL here and later on we check that it remains NULL after each
00db10
+     posix_memalign call.  */
00db10
+  void * ptr = NULL;
00db10
+
00db10
+  size_t align;
00db10
+
00db10
+  /* All aligned memory allocation functions expect an alignment that is a
00db10
+     power of 2.  Given this, we test each of them with every valid
00db10
+     alignment from 1 thru PAGESIZE.  */
00db10
+  for (align = 1; align <= pagesize; align *= 2)
00db10
+    {
00db10
+      test_setup ();
00db10
+      TEST_VERIFY (memalign (align, size) == NULL);
00db10
+      TEST_VERIFY (errno == ENOMEM);
00db10
+
00db10
+      /* posix_memalign expects an alignment that is a power of 2 *and* a
00db10
+         multiple of sizeof (void *).  */
00db10
+      if ((align % sizeof (void *)) == 0)
00db10
+        {
00db10
+          test_setup ();
00db10
+          TEST_VERIFY (posix_memalign (&ptr, align, size) == ENOMEM);
00db10
+          TEST_VERIFY (ptr == NULL);
00db10
+        }
00db10
+
00db10
+      /* aligned_alloc expects a size that is a multiple of alignment.  */
00db10
+      if ((size % align) == 0)
00db10
+        {
00db10
+          test_setup ();
00db10
+          TEST_VERIFY (aligned_alloc (align, size) == NULL);
00db10
+          TEST_VERIFY (errno == ENOMEM);
00db10
+        }
00db10
+    }
00db10
+
00db10
+  /* Both valloc and pvalloc return page-aligned memory.  */
00db10
+
00db10
+  test_setup ();
00db10
+  TEST_VERIFY (valloc (size) == NULL);
00db10
+  TEST_VERIFY (errno == ENOMEM);
00db10
+
00db10
+  test_setup ();
00db10
+  TEST_VERIFY (pvalloc (size) == NULL);
00db10
+  TEST_VERIFY (errno == ENOMEM);
00db10
+}
00db10
+
00db10
+
00db10
+#define FOURTEEN_ON_BITS ((1UL << 14) - 1)
00db10
+#define FIFTY_ON_BITS ((1UL << 50) - 1)
00db10
+
00db10
+
00db10
+static int
00db10
+do_test (void)
00db10
+{
00db10
+
00db10
+#if __WORDSIZE >= 64
00db10
+
00db10
+  /* This test assumes that none of the supported targets have an address
00db10
+     bus wider than 50 bits, and that therefore allocations for sizes wider
00db10
+     than 50 bits will fail.  Here, we ensure that the assumption continues
00db10
+     to be true in the future when we might have address buses wider than 50
00db10
+     bits.  */
00db10
+
00db10
+  struct rlimit alloc_size_limit
00db10
+    = {
00db10
+        .rlim_cur = FIFTY_ON_BITS,
00db10
+        .rlim_max = FIFTY_ON_BITS
00db10
+      };
00db10
+
00db10
+  setrlimit (RLIMIT_AS, &alloc_size_limit);
00db10
+
00db10
+#endif /* __WORDSIZE >= 64 */
00db10
+
00db10
+  DIAG_PUSH_NEEDS_COMMENT;
00db10
+#if __GNUC_PREREQ (7, 0)
00db10
+  /* GCC 7 warns about too-large allocations; here we want to test
00db10
+     that they fail.  */
00db10
+  DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
00db10
+#endif
00db10
+
00db10
+  /* Aligned memory allocation functions need to be tested up to alignment
00db10
+     size equivalent to page size, which should be a power of 2.  */
00db10
+  pagesize = sysconf (_SC_PAGESIZE);
00db10
+  TEST_VERIFY_EXIT (powerof2 (pagesize));
00db10
+
00db10
+  /* Loop 1: Ensure that all allocations with SIZE close to SIZE_MAX, i.e.
00db10
+     in the range (SIZE_MAX - 2^14, SIZE_MAX], fail.
00db10
+
00db10
+     We can expect that this range of allocation sizes will always lead to
00db10
+     an allocation failure on both 64 and 32 bit targets, because:
00db10
+
00db10
+     1. no currently supported 64-bit target has an address bus wider than
00db10
+     50 bits -- and (2^64 - 2^14) is much wider than that;
00db10
+
00db10
+     2. on 32-bit targets, even though 2^32 is only 4 GB and potentially
00db10
+     addressable, glibc itself is more than 2^14 bytes in size, and
00db10
+     therefore once glibc is loaded, less than (2^32 - 2^14) bytes remain
00db10
+     available.  */
00db10
+
00db10
+  for (size_t i = 0; i <= FOURTEEN_ON_BITS; i++)
00db10
+    {
00db10
+      test_large_allocations (SIZE_MAX - i);
00db10
+      test_large_aligned_allocations (SIZE_MAX - i);
00db10
+    }
00db10
+
00db10
+#if __WORDSIZE >= 64
00db10
+  /* On 64-bit targets, we need to test a much wider range of too-large
00db10
+     sizes, so we test at intervals of (1 << 50) that allocation sizes
00db10
+     ranging from SIZE_MAX down to (1 << 50) fail:
00db10
+     The 14 MSBs are decremented starting from "all ON" going down to 1,
00db10
+     the 50 LSBs are "all ON" and then "all OFF" during every iteration.  */
00db10
+  for (size_t msbs = FOURTEEN_ON_BITS; msbs >= 1; msbs--)
00db10
+    {
00db10
+      size_t size = (msbs << 50) | FIFTY_ON_BITS;
00db10
+      test_large_allocations (size);
00db10
+      test_large_aligned_allocations (size);
00db10
+
00db10
+      size = msbs << 50;
00db10
+      test_large_allocations (size);
00db10
+      test_large_aligned_allocations (size);
00db10
+    }
00db10
+#endif /* __WORDSIZE >= 64 */
00db10
+
00db10
+  DIAG_POP_NEEDS_COMMENT;
00db10
+
00db10
+  return 0;
00db10
+}
00db10
+
00db10
+
00db10
+#include <support/test-driver.c>