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 5 #include <fcntl.h> 6 #include <stddef.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <string.h> 10 #include <sys/stat.h> 11 #include <sys/types.h> 12 13 #include <algorithm> 14 #include <limits> 15 #include <memory> 16 17 #include "base/files/file_util.h" 18 #include "base/logging.h" 19 #include "base/memory/free_deleter.h" 20 #include "build/build_config.h" 21 #include "testing/gtest/include/gtest/gtest.h" 22 23 #if defined(OS_POSIX) 24 #include <sys/mman.h> 25 #include <unistd.h> 26 #endif 27 28 using std::nothrow; 29 using std::numeric_limits; 30 31 namespace { 32 33 // This function acts as a compiler optimization barrier. We use it to 34 // prevent the compiler from making an expression a compile-time constant. 35 // We also use it so that the compiler doesn't discard certain return values 36 // as something we don't need (see the comment with calloc below). 37 template <typename Type> 38 NOINLINE Type HideValueFromCompiler(volatile Type value) { 39 #if defined(__GNUC__) 40 // In a GCC compatible compiler (GCC or Clang), make this compiler barrier 41 // more robust than merely using "volatile". 42 __asm__ volatile ("" : "+r" (value)); 43 #endif // __GNUC__ 44 return value; 45 } 46 47 // Tcmalloc and Windows allocator shim support setting malloc limits. 48 // - NO_TCMALLOC (should be defined if compiled with use_allocator!="tcmalloc") 49 // - ADDRESS_SANITIZER and SYZYASAN because they have their own memory allocator 50 // - IOS does not use tcmalloc 51 // - OS_MACOSX does not use tcmalloc 52 // - Windows allocator shim defines ALLOCATOR_SHIM 53 #if (!defined(NO_TCMALLOC) || defined(ALLOCATOR_SHIM)) && \ 54 !defined(ADDRESS_SANITIZER) && !defined(OS_IOS) && !defined(OS_MACOSX) && \ 55 !defined(SYZYASAN) 56 #define MALLOC_OVERFLOW_TEST(function) function 57 #else 58 #define MALLOC_OVERFLOW_TEST(function) DISABLED_##function 59 #endif 60 61 #if defined(OS_LINUX) && defined(__x86_64__) 62 // Detect runtime TCMalloc bypasses. 63 bool IsTcMallocBypassed() { 64 // This should detect a TCMalloc bypass from Valgrind. 65 char* g_slice = getenv("G_SLICE"); 66 if (g_slice && !strcmp(g_slice, "always-malloc")) 67 return true; 68 return false; 69 } 70 #endif 71 72 // There are platforms where these tests are known to fail. We would like to 73 // be able to easily check the status on the bots, but marking tests as 74 // FAILS_ is too clunky. 75 void OverflowTestsSoftExpectTrue(bool overflow_detected) { 76 if (!overflow_detected) { 77 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX) 78 // Sadly, on Linux, Android, and OSX we don't have a good story yet. Don't 79 // fail the test, but report. 80 printf("Platform has overflow: %s\n", 81 !overflow_detected ? "yes." : "no."); 82 #else 83 // Otherwise, fail the test. (Note: EXPECT are ok in subfunctions, ASSERT 84 // aren't). 85 EXPECT_TRUE(overflow_detected); 86 #endif 87 } 88 } 89 90 #if defined(OS_IOS) || defined(ADDRESS_SANITIZER) || \ 91 defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) 92 #define MAYBE_NewOverflow DISABLED_NewOverflow 93 #else 94 #define MAYBE_NewOverflow NewOverflow 95 #endif 96 // Test array[TooBig][X] and array[X][TooBig] allocations for int overflows. 97 // IOS doesn't honor nothrow, so disable the test there. 98 // Disabled under XSan because asan aborts when new returns nullptr, 99 // https://bugs.chromium.org/p/chromium/issues/detail?id=690271#c15 100 TEST(SecurityTest, MAYBE_NewOverflow) { 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 const size_t kMaxSizeT = std::numeric_limits<size_t>::max(); 106 const size_t kArraySize2 = kMaxSizeT / kArraySize + 10; 107 const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2); 108 { 109 std::unique_ptr<char[][kArraySize]> array_pointer( 110 new (nothrow) char[kDynamicArraySize2][kArraySize]); 111 // Prevent clang from optimizing away the whole test. 112 char* volatile p = reinterpret_cast<char*>(array_pointer.get()); 113 OverflowTestsSoftExpectTrue(!p); 114 } 115 // On windows, the compiler prevents static array sizes of more than 116 // 0x7fffffff (error C2148). 117 #if defined(OS_WIN) && defined(ARCH_CPU_64_BITS) 118 ALLOW_UNUSED_LOCAL(kDynamicArraySize); 119 #else 120 { 121 std::unique_ptr<char[][kArraySize2]> array_pointer( 122 new (nothrow) char[kDynamicArraySize][kArraySize2]); 123 // Prevent clang from optimizing away the whole test. 124 char* volatile p = reinterpret_cast<char*>(array_pointer.get()); 125 OverflowTestsSoftExpectTrue(!p); 126 } 127 #endif // !defined(OS_WIN) || !defined(ARCH_CPU_64_BITS) 128 } 129 130 #if defined(OS_LINUX) && defined(__x86_64__) 131 // Check if ptr1 and ptr2 are separated by less than size chars. 132 bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) { 133 ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) - 134 reinterpret_cast<char*>(std::min(ptr1, ptr2)); 135 return static_cast<size_t>(ptr_diff) <= size; 136 } 137 138 // Check if TCMalloc uses an underlying random memory allocator. 139 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(RandomMemoryAllocations)) { 140 if (IsTcMallocBypassed()) 141 return; 142 size_t kPageSize = 4096; // We support x86_64 only. 143 // Check that malloc() returns an address that is neither the kernel's 144 // un-hinted mmap area, nor the current brk() area. The first malloc() may 145 // not be at a random address because TCMalloc will first exhaust any memory 146 // that it has allocated early on, before starting the sophisticated 147 // allocators. 148 void* default_mmap_heap_address = 149 mmap(0, kPageSize, PROT_READ|PROT_WRITE, 150 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); 151 ASSERT_NE(default_mmap_heap_address, 152 static_cast<void*>(MAP_FAILED)); 153 ASSERT_EQ(munmap(default_mmap_heap_address, kPageSize), 0); 154 void* brk_heap_address = sbrk(0); 155 ASSERT_NE(brk_heap_address, reinterpret_cast<void*>(-1)); 156 ASSERT_TRUE(brk_heap_address != NULL); 157 // 1 MB should get us past what TCMalloc pre-allocated before initializing 158 // the sophisticated allocators. 159 size_t kAllocSize = 1<<20; 160 std::unique_ptr<char, base::FreeDeleter> ptr( 161 static_cast<char*>(malloc(kAllocSize))); 162 ASSERT_TRUE(ptr != NULL); 163 // If two pointers are separated by less than 512MB, they are considered 164 // to be in the same area. 165 // Our random pointer could be anywhere within 0x3fffffffffff (46bits), 166 // and we are checking that it's not withing 1GB (30 bits) from two 167 // addresses (brk and mmap heap). We have roughly one chance out of 168 // 2^15 to flake. 169 const size_t kAreaRadius = 1<<29; 170 bool in_default_mmap_heap = ArePointersToSameArea( 171 ptr.get(), default_mmap_heap_address, kAreaRadius); 172 EXPECT_FALSE(in_default_mmap_heap); 173 174 bool in_default_brk_heap = ArePointersToSameArea( 175 ptr.get(), brk_heap_address, kAreaRadius); 176 EXPECT_FALSE(in_default_brk_heap); 177 178 // In the implementation, we always mask our random addresses with 179 // kRandomMask, so we use it as an additional detection mechanism. 180 const uintptr_t kRandomMask = 0x3fffffffffffULL; 181 bool impossible_random_address = 182 reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask; 183 EXPECT_FALSE(impossible_random_address); 184 } 185 186 #endif // defined(OS_LINUX) && defined(__x86_64__) 187 188 } // namespace 189