jln@chromium.org | 4faaf21 | 2013-01-16 05:16:33 +0900 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
jln@chromium.org | c71a4da | 2013-01-31 11:23:43 +0900 | [diff] [blame] | 5 | #include <fcntl.h> |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 6 | #include <stddef.h> |
jln@chromium.org | 4faaf21 | 2013-01-16 05:16:33 +0900 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
jln@chromium.org | c71a4da | 2013-01-31 11:23:43 +0900 | [diff] [blame] | 10 | #include <sys/stat.h> |
| 11 | #include <sys/types.h> |
jln@chromium.org | 4faaf21 | 2013-01-16 05:16:33 +0900 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | #include <limits> |
| 15 | |
brettw@chromium.org | 01f3da4 | 2014-08-14 05:22:14 +0900 | [diff] [blame] | 16 | #include "base/files/file_util.h" |
jln@chromium.org | 4faaf21 | 2013-01-16 05:16:33 +0900 | [diff] [blame] | 17 | #include "base/logging.h" |
| 18 | #include "base/memory/scoped_ptr.h" |
jln@chromium.org | a55baa6 | 2013-02-05 08:39:48 +0900 | [diff] [blame] | 19 | #include "build/build_config.h" |
jln@chromium.org | 4faaf21 | 2013-01-16 05:16:33 +0900 | [diff] [blame] | 20 | #include "testing/gtest/include/gtest/gtest.h" |
| 21 | |
jln@chromium.org | a55baa6 | 2013-02-05 08:39:48 +0900 | [diff] [blame] | 22 | #if defined(OS_POSIX) |
| 23 | #include <sys/mman.h> |
| 24 | #include <unistd.h> |
| 25 | #endif |
| 26 | |
jln@chromium.org | 4faaf21 | 2013-01-16 05:16:33 +0900 | [diff] [blame] | 27 | using std::nothrow; |
jln@chromium.org | d06052c | 2013-01-26 13:41:15 +0900 | [diff] [blame] | 28 | using std::numeric_limits; |
jln@chromium.org | 4faaf21 | 2013-01-16 05:16:33 +0900 | [diff] [blame] | 29 | |
| 30 | namespace { |
| 31 | |
jln@chromium.org | c6d4422 | 2013-02-06 12:23:49 +0900 | [diff] [blame] | 32 | // This function acts as a compiler optimization barrier. We use it to |
| 33 | // prevent the compiler from making an expression a compile-time constant. |
| 34 | // We also use it so that the compiler doesn't discard certain return values |
| 35 | // as something we don't need (see the comment with calloc below). |
| 36 | template <typename Type> |
thakis | 497632e | 2015-04-23 11:58:29 +0900 | [diff] [blame] | 37 | NOINLINE Type HideValueFromCompiler(volatile Type value) { |
jln@chromium.org | 5975586 | 2013-04-04 21:02:35 +0900 | [diff] [blame] | 38 | #if defined(__GNUC__) |
| 39 | // In a GCC compatible compiler (GCC or Clang), make this compiler barrier |
| 40 | // more robust than merely using "volatile". |
| 41 | __asm__ volatile ("" : "+r" (value)); |
| 42 | #endif // __GNUC__ |
jln@chromium.org | c6d4422 | 2013-02-06 12:23:49 +0900 | [diff] [blame] | 43 | return value; |
| 44 | } |
| 45 | |
wfh | f2a57fa | 2015-01-13 12:11:40 +0900 | [diff] [blame] | 46 | // Tcmalloc and Windows allocator shim support setting malloc limits. |
dmikurube@chromium.org | 1df3651 | 2014-03-06 05:07:26 +0900 | [diff] [blame] | 47 | // - NO_TCMALLOC (should be defined if compiled with use_allocator!="tcmalloc") |
chrisha@google.com | 87839ab | 2014-03-28 00:08:04 +0900 | [diff] [blame] | 48 | // - ADDRESS_SANITIZER and SYZYASAN because they have their own memory allocator |
jln@chromium.org | fe78d55 | 2013-02-15 15:10:40 +0900 | [diff] [blame] | 49 | // - IOS does not use tcmalloc |
jln@chromium.org | 4faaf21 | 2013-01-16 05:16:33 +0900 | [diff] [blame] | 50 | // - OS_MACOSX does not use tcmalloc |
wfh | f2a57fa | 2015-01-13 12:11:40 +0900 | [diff] [blame] | 51 | // - Windows allocator shim defines ALLOCATOR_SHIM |
| 52 | #if (!defined(NO_TCMALLOC) || defined(ALLOCATOR_SHIM)) && \ |
| 53 | !defined(ADDRESS_SANITIZER) && !defined(OS_IOS) && !defined(OS_MACOSX) && \ |
| 54 | !defined(SYZYASAN) |
| 55 | #define MALLOC_OVERFLOW_TEST(function) function |
jln@chromium.org | 4faaf21 | 2013-01-16 05:16:33 +0900 | [diff] [blame] | 56 | #else |
wfh | f2a57fa | 2015-01-13 12:11:40 +0900 | [diff] [blame] | 57 | #define MALLOC_OVERFLOW_TEST(function) DISABLED_##function |
jln@chromium.org | 4faaf21 | 2013-01-16 05:16:33 +0900 | [diff] [blame] | 58 | #endif |
| 59 | |
wfh | 26bb644 | 2015-09-21 11:21:14 +0900 | [diff] [blame] | 60 | #if defined(OS_LINUX) && defined(__x86_64__) |
jln@chromium.org | 4faaf21 | 2013-01-16 05:16:33 +0900 | [diff] [blame] | 61 | // Detect runtime TCMalloc bypasses. |
| 62 | bool IsTcMallocBypassed() { |
jln@chromium.org | 4faaf21 | 2013-01-16 05:16:33 +0900 | [diff] [blame] | 63 | // This should detect a TCMalloc bypass from Valgrind. |
| 64 | char* g_slice = getenv("G_SLICE"); |
| 65 | if (g_slice && !strcmp(g_slice, "always-malloc")) |
| 66 | return true; |
jln@chromium.org | 4faaf21 | 2013-01-16 05:16:33 +0900 | [diff] [blame] | 67 | return false; |
| 68 | } |
jln@chromium.org | fe78d55 | 2013-02-15 15:10:40 +0900 | [diff] [blame] | 69 | #endif |
jln@chromium.org | d06052c | 2013-01-26 13:41:15 +0900 | [diff] [blame] | 70 | |
jln@chromium.org | d06052c | 2013-01-26 13:41:15 +0900 | [diff] [blame] | 71 | // There are platforms where these tests are known to fail. We would like to |
| 72 | // be able to easily check the status on the bots, but marking tests as |
| 73 | // FAILS_ is too clunky. |
| 74 | void OverflowTestsSoftExpectTrue(bool overflow_detected) { |
| 75 | if (!overflow_detected) { |
| 76 | #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX) |
| 77 | // Sadly, on Linux, Android, and OSX we don't have a good story yet. Don't |
| 78 | // fail the test, but report. |
| 79 | printf("Platform has overflow: %s\n", |
| 80 | !overflow_detected ? "yes." : "no."); |
| 81 | #else |
| 82 | // Otherwise, fail the test. (Note: EXPECT are ok in subfunctions, ASSERT |
| 83 | // aren't). |
| 84 | EXPECT_TRUE(overflow_detected); |
| 85 | #endif |
| 86 | } |
| 87 | } |
| 88 | |
tommycli | 153da34 | 2016-02-10 07:42:58 +0900 | [diff] [blame] | 89 | #if defined(OS_IOS) || defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) |
John Abd-El-Malek | ccd3dd3 | 2014-10-03 07:55:15 +0900 | [diff] [blame] | 90 | #define MAYBE_NewOverflow DISABLED_NewOverflow |
| 91 | #else |
| 92 | #define MAYBE_NewOverflow NewOverflow |
| 93 | #endif |
jln@chromium.org | d06052c | 2013-01-26 13:41:15 +0900 | [diff] [blame] | 94 | // Test array[TooBig][X] and array[X][TooBig] allocations for int overflows. |
| 95 | // IOS doesn't honor nothrow, so disable the test there. |
jln@chromium.org | 5975586 | 2013-04-04 21:02:35 +0900 | [diff] [blame] | 96 | // Crashes on Windows Dbg builds, disable there as well. |
John Abd-El-Malek | ccd3dd3 | 2014-10-03 07:55:15 +0900 | [diff] [blame] | 97 | // Fails on Mac 10.8 http://crbug.com/227092 |
tommycli | 153da34 | 2016-02-10 07:42:58 +0900 | [diff] [blame] | 98 | // Disabled on Linux because failing Linux Valgrind bot, and Valgrind exclusions |
| 99 | // are not currently read. See http://crbug.com/582398 |
John Abd-El-Malek | ccd3dd3 | 2014-10-03 07:55:15 +0900 | [diff] [blame] | 100 | TEST(SecurityTest, MAYBE_NewOverflow) { |
jln@chromium.org | d06052c | 2013-01-26 13:41:15 +0900 | [diff] [blame] | 101 | const size_t kArraySize = 4096; |
| 102 | // We want something "dynamic" here, so that the compiler doesn't |
| 103 | // immediately reject crazy arrays. |
| 104 | const size_t kDynamicArraySize = HideValueFromCompiler(kArraySize); |
| 105 | // numeric_limits are still not constexpr until we switch to C++11, so we |
| 106 | // use an ugly cast. |
| 107 | const size_t kMaxSizeT = ~static_cast<size_t>(0); |
| 108 | ASSERT_EQ(numeric_limits<size_t>::max(), kMaxSizeT); |
| 109 | const size_t kArraySize2 = kMaxSizeT / kArraySize + 10; |
| 110 | const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2); |
| 111 | { |
| 112 | scoped_ptr<char[][kArraySize]> array_pointer(new (nothrow) |
| 113 | char[kDynamicArraySize2][kArraySize]); |
jln@chromium.org | c6d4422 | 2013-02-06 12:23:49 +0900 | [diff] [blame] | 114 | OverflowTestsSoftExpectTrue(!array_pointer); |
jln@chromium.org | d06052c | 2013-01-26 13:41:15 +0900 | [diff] [blame] | 115 | } |
jln@chromium.org | 5975586 | 2013-04-04 21:02:35 +0900 | [diff] [blame] | 116 | // On windows, the compiler prevents static array sizes of more than |
| 117 | // 0x7fffffff (error C2148). |
Peter Kasting | 618317c | 2014-11-21 08:14:08 +0900 | [diff] [blame] | 118 | #if defined(OS_WIN) && defined(ARCH_CPU_64_BITS) |
| 119 | ALLOW_UNUSED_LOCAL(kDynamicArraySize); |
| 120 | #else |
jln@chromium.org | d06052c | 2013-01-26 13:41:15 +0900 | [diff] [blame] | 121 | { |
| 122 | scoped_ptr<char[][kArraySize2]> array_pointer(new (nothrow) |
| 123 | char[kDynamicArraySize][kArraySize2]); |
jln@chromium.org | c6d4422 | 2013-02-06 12:23:49 +0900 | [diff] [blame] | 124 | OverflowTestsSoftExpectTrue(!array_pointer); |
jln@chromium.org | d06052c | 2013-01-26 13:41:15 +0900 | [diff] [blame] | 125 | } |
jln@chromium.org | 5975586 | 2013-04-04 21:02:35 +0900 | [diff] [blame] | 126 | #endif // !defined(OS_WIN) || !defined(ARCH_CPU_64_BITS) |
jln@chromium.org | d06052c | 2013-01-26 13:41:15 +0900 | [diff] [blame] | 127 | } |
| 128 | |
estade@chromium.org | d841d6d | 2014-04-16 05:58:09 +0900 | [diff] [blame] | 129 | #if defined(OS_LINUX) && defined(__x86_64__) |
jln@chromium.org | a55baa6 | 2013-02-05 08:39:48 +0900 | [diff] [blame] | 130 | // Check if ptr1 and ptr2 are separated by less than size chars. |
| 131 | bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) { |
| 132 | ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) - |
| 133 | reinterpret_cast<char*>(std::min(ptr1, ptr2)); |
| 134 | return static_cast<size_t>(ptr_diff) <= size; |
| 135 | } |
| 136 | |
jln@chromium.org | c71a4da | 2013-01-31 11:23:43 +0900 | [diff] [blame] | 137 | // Check if TCMalloc uses an underlying random memory allocator. |
wfh | f2a57fa | 2015-01-13 12:11:40 +0900 | [diff] [blame] | 138 | TEST(SecurityTest, MALLOC_OVERFLOW_TEST(RandomMemoryAllocations)) { |
jln@chromium.org | c71a4da | 2013-01-31 11:23:43 +0900 | [diff] [blame] | 139 | if (IsTcMallocBypassed()) |
| 140 | return; |
jln@chromium.org | a55baa6 | 2013-02-05 08:39:48 +0900 | [diff] [blame] | 141 | size_t kPageSize = 4096; // We support x86_64 only. |
| 142 | // Check that malloc() returns an address that is neither the kernel's |
| 143 | // un-hinted mmap area, nor the current brk() area. The first malloc() may |
| 144 | // not be at a random address because TCMalloc will first exhaust any memory |
| 145 | // that it has allocated early on, before starting the sophisticated |
| 146 | // allocators. |
| 147 | void* default_mmap_heap_address = |
| 148 | mmap(0, kPageSize, PROT_READ|PROT_WRITE, |
| 149 | MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 150 | ASSERT_NE(default_mmap_heap_address, |
| 151 | static_cast<void*>(MAP_FAILED)); |
| 152 | ASSERT_EQ(munmap(default_mmap_heap_address, kPageSize), 0); |
| 153 | void* brk_heap_address = sbrk(0); |
| 154 | ASSERT_NE(brk_heap_address, reinterpret_cast<void*>(-1)); |
| 155 | ASSERT_TRUE(brk_heap_address != NULL); |
| 156 | // 1 MB should get us past what TCMalloc pre-allocated before initializing |
| 157 | // the sophisticated allocators. |
| 158 | size_t kAllocSize = 1<<20; |
| 159 | scoped_ptr<char, base::FreeDeleter> ptr( |
| 160 | static_cast<char*>(malloc(kAllocSize))); |
| 161 | ASSERT_TRUE(ptr != NULL); |
| 162 | // If two pointers are separated by less than 512MB, they are considered |
| 163 | // to be in the same area. |
| 164 | // Our random pointer could be anywhere within 0x3fffffffffff (46bits), |
| 165 | // and we are checking that it's not withing 1GB (30 bits) from two |
| 166 | // addresses (brk and mmap heap). We have roughly one chance out of |
| 167 | // 2^15 to flake. |
| 168 | const size_t kAreaRadius = 1<<29; |
| 169 | bool in_default_mmap_heap = ArePointersToSameArea( |
| 170 | ptr.get(), default_mmap_heap_address, kAreaRadius); |
| 171 | EXPECT_FALSE(in_default_mmap_heap); |
| 172 | |
| 173 | bool in_default_brk_heap = ArePointersToSameArea( |
| 174 | ptr.get(), brk_heap_address, kAreaRadius); |
| 175 | EXPECT_FALSE(in_default_brk_heap); |
| 176 | |
| 177 | // In the implementation, we always mask our random addresses with |
| 178 | // kRandomMask, so we use it as an additional detection mechanism. |
| 179 | const uintptr_t kRandomMask = 0x3fffffffffffULL; |
| 180 | bool impossible_random_address = |
| 181 | reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask; |
| 182 | EXPECT_FALSE(impossible_random_address); |
jln@chromium.org | c71a4da | 2013-01-31 11:23:43 +0900 | [diff] [blame] | 183 | } |
| 184 | |
estade@chromium.org | d841d6d | 2014-04-16 05:58:09 +0900 | [diff] [blame] | 185 | #endif // defined(OS_LINUX) && defined(__x86_64__) |
jln@chromium.org | c71a4da | 2013-01-31 11:23:43 +0900 | [diff] [blame] | 186 | |
jln@chromium.org | 4faaf21 | 2013-01-16 05:16:33 +0900 | [diff] [blame] | 187 | } // namespace |