Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <fcntl.h> | 5 #include <fcntl.h> |
| 6 #include <stdio.h> | 6 #include <stdio.h> |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 | 11 |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <limits> | 13 #include <limits> |
| 14 | 14 |
| 15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 #if defined(OS_POSIX) | 21 #if defined(OS_POSIX) |
| 22 #include <sys/mman.h> | 22 #include <sys/mman.h> |
| 23 #include <unistd.h> | 23 #include <unistd.h> |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 #if defined(OS_WIN) | |
| 27 #include <new.h> | |
| 28 #endif | |
| 29 | |
| 26 using std::nothrow; | 30 using std::nothrow; |
| 27 using std::numeric_limits; | 31 using std::numeric_limits; |
| 28 | 32 |
| 29 namespace { | 33 namespace { |
| 30 | 34 |
| 35 #if defined(OS_WIN) | |
| 36 int OnNoMemory(size_t) { | |
| 37 _exit(1); | |
| 38 } | |
| 39 #endif | |
| 40 | |
| 31 // This function acts as a compiler optimization barrier. We use it to | 41 // This function acts as a compiler optimization barrier. We use it to |
| 32 // prevent the compiler from making an expression a compile-time constant. | 42 // prevent the compiler from making an expression a compile-time constant. |
| 33 // We also use it so that the compiler doesn't discard certain return values | 43 // We also use it so that the compiler doesn't discard certain return values |
| 34 // as something we don't need (see the comment with calloc below). | 44 // as something we don't need (see the comment with calloc below). |
| 35 template <typename Type> | 45 template <typename Type> |
| 36 Type HideValueFromCompiler(volatile Type value) { | 46 Type HideValueFromCompiler(volatile Type value) { |
| 37 #if defined(__GNUC__) | 47 #if defined(__GNUC__) |
| 38 // In a GCC compatible compiler (GCC or Clang), make this compiler barrier | 48 // In a GCC compatible compiler (GCC or Clang), make this compiler barrier |
| 39 // more robust than merely using "volatile". | 49 // more robust than merely using "volatile". |
| 40 __asm__ volatile ("" : "+r" (value)); | 50 __asm__ volatile ("" : "+r" (value)); |
| 41 #endif // __GNUC__ | 51 #endif // __GNUC__ |
| 42 return value; | 52 return value; |
| 43 } | 53 } |
| 44 | 54 |
| 55 // Tcmalloc and Windows allocator shim support setting malloc limits. | |
| 45 // - NO_TCMALLOC (should be defined if compiled with use_allocator!="tcmalloc") | 56 // - NO_TCMALLOC (should be defined if compiled with use_allocator!="tcmalloc") |
| 46 // - ADDRESS_SANITIZER and SYZYASAN because they have their own memory allocator | 57 // - ADDRESS_SANITIZER and SYZYASAN because they have their own memory allocator |
| 47 // - IOS does not use tcmalloc | 58 // - IOS does not use tcmalloc |
| 48 // - OS_MACOSX does not use tcmalloc | 59 // - OS_MACOSX does not use tcmalloc |
| 49 #if !defined(NO_TCMALLOC) && !defined(ADDRESS_SANITIZER) && \ | 60 // - Windows allocator shim defines ALLOCATOR_SHIM |
| 50 !defined(OS_IOS) && !defined(OS_MACOSX) && !defined(SYZYASAN) | 61 #if (!defined(NO_TCMALLOC) || defined(ALLOCATOR_SHIM)) && \ |
|
scottmg
2015/01/09 23:32:48
Do we have to have another define? i.e. is there s
Will Harris
2015/01/11 06:30:40
Allocator shim isn't enabled for shared_library bu
| |
| 51 #define TCMALLOC_TEST(function) function | 62 !defined(ADDRESS_SANITIZER) && !defined(OS_IOS) && !defined(OS_MACOSX) && \ |
| 63 !defined(SYZYASAN) | |
| 64 #define MALLOC_OVERFLOW_TEST(function) function | |
| 52 #else | 65 #else |
| 53 #define TCMALLOC_TEST(function) DISABLED_##function | 66 #define MALLOC_OVERFLOW_TEST(function) DISABLED_##function |
| 54 #endif | 67 #endif |
| 55 | 68 |
| 56 // TODO(jln): switch to std::numeric_limits<int>::max() when we switch to | 69 // TODO(jln): switch to std::numeric_limits<int>::max() when we switch to |
| 57 // C++11. | 70 // C++11. |
| 58 const size_t kTooBigAllocSize = INT_MAX; | 71 const size_t kTooBigAllocSize = INT_MAX; |
| 59 | 72 |
| 60 // Detect runtime TCMalloc bypasses. | 73 // Detect runtime TCMalloc bypasses. |
| 61 bool IsTcMallocBypassed() { | 74 bool IsTcMallocBypassed() { |
| 62 #if defined(OS_LINUX) | 75 #if defined(OS_LINUX) |
| 63 // This should detect a TCMalloc bypass from Valgrind. | 76 // This should detect a TCMalloc bypass from Valgrind. |
| 64 char* g_slice = getenv("G_SLICE"); | 77 char* g_slice = getenv("G_SLICE"); |
| 65 if (g_slice && !strcmp(g_slice, "always-malloc")) | 78 if (g_slice && !strcmp(g_slice, "always-malloc")) |
| 66 return true; | 79 return true; |
| 67 #elif defined(OS_WIN) | |
| 68 // This should detect a TCMalloc bypass from setting | |
| 69 // the CHROME_ALLOCATOR environment variable. | |
| 70 char* allocator = getenv("CHROME_ALLOCATOR"); | |
| 71 if (allocator && strcmp(allocator, "tcmalloc")) | |
| 72 return true; | |
| 73 #endif | 80 #endif |
| 74 return false; | 81 return false; |
| 75 } | 82 } |
| 76 | 83 |
| 77 bool CallocDiesOnOOM() { | 84 bool CallocDiesOnOOM() { |
| 78 // The sanitizers' calloc dies on OOM instead of returning NULL. | 85 // The sanitizers' calloc dies on OOM instead of returning NULL. |
| 79 // The wrapper function in base/process_util_linux.cc that is used when we | 86 // The wrapper function in base/process_util_linux.cc that is used when we |
| 80 // compile without TCMalloc will just die on OOM instead of returning NULL. | 87 // compile without TCMalloc will just die on OOM instead of returning NULL. |
| 81 #if defined(ADDRESS_SANITIZER) || \ | 88 #if defined(ADDRESS_SANITIZER) || \ |
| 82 defined(MEMORY_SANITIZER) || \ | 89 defined(MEMORY_SANITIZER) || \ |
| 83 defined(THREAD_SANITIZER) || \ | 90 defined(THREAD_SANITIZER) || \ |
| 84 (defined(OS_LINUX) && defined(NO_TCMALLOC)) | 91 (defined(OS_LINUX) && defined(NO_TCMALLOC)) |
| 85 return true; | 92 return true; |
| 86 #else | 93 #else |
| 87 return false; | 94 return false; |
| 88 #endif | 95 #endif |
| 89 } | 96 } |
| 90 | 97 |
| 91 // Fake test that allow to know the state of TCMalloc by looking at bots. | 98 // Fake test that allow to know the state of TCMalloc by looking at bots. |
| 92 TEST(SecurityTest, TCMALLOC_TEST(IsTCMallocDynamicallyBypassed)) { | 99 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(IsTCMallocDynamicallyBypassed)) { |
| 93 printf("Malloc is dynamically bypassed: %s\n", | 100 printf("Malloc is dynamically bypassed: %s\n", |
| 94 IsTcMallocBypassed() ? "yes." : "no."); | 101 IsTcMallocBypassed() ? "yes." : "no."); |
| 95 } | 102 } |
| 96 | 103 |
| 97 // The MemoryAllocationRestrictions* tests test that we can not allocate a | 104 // The MemoryAllocationRestrictions* tests test that we can not allocate a |
| 98 // memory range that cannot be indexed via an int. This is used to mitigate | 105 // memory range that cannot be indexed via an int. This is used to mitigate |
| 99 // vulnerabilities in libraries that use int instead of size_t. See | 106 // vulnerabilities in libraries that use int instead of size_t. See |
| 100 // crbug.com/169327. | 107 // crbug.com/169327. |
| 101 | 108 |
| 102 TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsMalloc)) { | 109 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsMalloc)) { |
| 103 if (!IsTcMallocBypassed()) { | 110 if (!IsTcMallocBypassed()) { |
| 104 scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>( | 111 scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>( |
| 105 HideValueFromCompiler(malloc(kTooBigAllocSize)))); | 112 HideValueFromCompiler(malloc(kTooBigAllocSize)))); |
| 106 ASSERT_TRUE(!ptr); | 113 ASSERT_TRUE(!ptr); |
| 107 } | 114 } |
| 108 } | 115 } |
| 109 | 116 |
| 110 TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsCalloc)) { | 117 #if defined(GTEST_HAS_DEATH_TEST) && defined(OS_WIN) |
| 118 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationDeathMalloc)) { | |
| 119 if (!IsTcMallocBypassed()) { | |
| 120 _set_new_handler(&OnNoMemory); | |
| 121 _set_new_mode(1); | |
| 122 { | |
| 123 scoped_ptr<char, base::FreeDeleter> ptr; | |
| 124 EXPECT_DEATH(ptr.reset(static_cast<char*>( | |
| 125 HideValueFromCompiler(malloc(kTooBigAllocSize)))), | |
| 126 ""); | |
| 127 ASSERT_TRUE(!ptr); | |
| 128 } | |
| 129 _set_new_handler(NULL); | |
| 130 _set_new_mode(0); | |
| 131 } | |
| 132 } | |
| 133 #endif | |
| 134 | |
| 135 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsCalloc)) { | |
| 111 if (!IsTcMallocBypassed()) { | 136 if (!IsTcMallocBypassed()) { |
| 112 scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>( | 137 scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>( |
| 113 HideValueFromCompiler(calloc(kTooBigAllocSize, 1)))); | 138 HideValueFromCompiler(calloc(kTooBigAllocSize, 1)))); |
| 114 ASSERT_TRUE(!ptr); | 139 ASSERT_TRUE(!ptr); |
| 115 } | 140 } |
| 116 } | 141 } |
| 117 | 142 |
| 118 TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsRealloc)) { | 143 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsRealloc)) { |
| 119 if (!IsTcMallocBypassed()) { | 144 if (!IsTcMallocBypassed()) { |
| 120 char* orig_ptr = static_cast<char*>(malloc(1)); | 145 char* orig_ptr = static_cast<char*>(malloc(1)); |
| 121 ASSERT_TRUE(orig_ptr); | 146 ASSERT_TRUE(orig_ptr); |
| 122 scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>( | 147 scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>( |
| 123 HideValueFromCompiler(realloc(orig_ptr, kTooBigAllocSize)))); | 148 HideValueFromCompiler(realloc(orig_ptr, kTooBigAllocSize)))); |
| 124 ASSERT_TRUE(!ptr); | 149 ASSERT_TRUE(!ptr); |
| 125 // If realloc() did not succeed, we need to free orig_ptr. | 150 // If realloc() did not succeed, we need to free orig_ptr. |
| 126 free(orig_ptr); | 151 free(orig_ptr); |
| 127 } | 152 } |
| 128 } | 153 } |
| 129 | 154 |
| 130 typedef struct { | 155 typedef struct { |
| 131 char large_array[kTooBigAllocSize]; | 156 char large_array[kTooBigAllocSize]; |
| 132 } VeryLargeStruct; | 157 } VeryLargeStruct; |
| 133 | 158 |
| 134 TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsNew)) { | 159 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsNew)) { |
| 135 if (!IsTcMallocBypassed()) { | 160 if (!IsTcMallocBypassed()) { |
| 136 scoped_ptr<VeryLargeStruct> ptr( | 161 scoped_ptr<VeryLargeStruct> ptr( |
| 137 HideValueFromCompiler(new (nothrow) VeryLargeStruct)); | 162 HideValueFromCompiler(new (nothrow) VeryLargeStruct)); |
| 138 ASSERT_TRUE(!ptr); | 163 ASSERT_TRUE(!ptr); |
| 139 } | 164 } |
| 140 } | 165 } |
| 141 | 166 |
| 142 TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsNewArray)) { | 167 #if defined(GTEST_HAS_DEATH_TEST) && defined(OS_WIN) |
| 168 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationDeathNew)) { | |
| 169 if (!IsTcMallocBypassed()) { | |
| 170 _set_new_handler(&OnNoMemory); | |
| 171 { | |
| 172 scoped_ptr<VeryLargeStruct> ptr; | |
| 173 EXPECT_DEATH( | |
| 174 ptr.reset(HideValueFromCompiler(new (nothrow) VeryLargeStruct)), ""); | |
| 175 ASSERT_TRUE(!ptr); | |
| 176 } | |
| 177 _set_new_handler(NULL); | |
| 178 } | |
| 179 } | |
| 180 #endif | |
| 181 | |
| 182 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsNewArray)) { | |
| 143 if (!IsTcMallocBypassed()) { | 183 if (!IsTcMallocBypassed()) { |
| 144 scoped_ptr<char[]> ptr( | 184 scoped_ptr<char[]> ptr( |
| 145 HideValueFromCompiler(new (nothrow) char[kTooBigAllocSize])); | 185 HideValueFromCompiler(new (nothrow) char[kTooBigAllocSize])); |
| 146 ASSERT_TRUE(!ptr); | 186 ASSERT_TRUE(!ptr); |
| 147 } | 187 } |
| 148 } | 188 } |
| 149 | 189 |
| 150 // The tests bellow check for overflows in new[] and calloc(). | 190 // The tests bellow check for overflows in new[] and calloc(). |
| 151 | 191 |
| 152 // There are platforms where these tests are known to fail. We would like to | 192 // There are platforms where these tests are known to fail. We would like to |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 | 275 |
| 236 #if defined(OS_LINUX) && defined(__x86_64__) | 276 #if defined(OS_LINUX) && defined(__x86_64__) |
| 237 // Check if ptr1 and ptr2 are separated by less than size chars. | 277 // Check if ptr1 and ptr2 are separated by less than size chars. |
| 238 bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) { | 278 bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) { |
| 239 ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) - | 279 ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) - |
| 240 reinterpret_cast<char*>(std::min(ptr1, ptr2)); | 280 reinterpret_cast<char*>(std::min(ptr1, ptr2)); |
| 241 return static_cast<size_t>(ptr_diff) <= size; | 281 return static_cast<size_t>(ptr_diff) <= size; |
| 242 } | 282 } |
| 243 | 283 |
| 244 // Check if TCMalloc uses an underlying random memory allocator. | 284 // Check if TCMalloc uses an underlying random memory allocator. |
| 245 TEST(SecurityTest, TCMALLOC_TEST(RandomMemoryAllocations)) { | 285 TEST(SecurityTest, MALLOC_OVERFLOW_TEST(RandomMemoryAllocations)) { |
| 246 if (IsTcMallocBypassed()) | 286 if (IsTcMallocBypassed()) |
| 247 return; | 287 return; |
| 248 size_t kPageSize = 4096; // We support x86_64 only. | 288 size_t kPageSize = 4096; // We support x86_64 only. |
| 249 // Check that malloc() returns an address that is neither the kernel's | 289 // Check that malloc() returns an address that is neither the kernel's |
| 250 // un-hinted mmap area, nor the current brk() area. The first malloc() may | 290 // un-hinted mmap area, nor the current brk() area. The first malloc() may |
| 251 // not be at a random address because TCMalloc will first exhaust any memory | 291 // not be at a random address because TCMalloc will first exhaust any memory |
| 252 // that it has allocated early on, before starting the sophisticated | 292 // that it has allocated early on, before starting the sophisticated |
| 253 // allocators. | 293 // allocators. |
| 254 void* default_mmap_heap_address = | 294 void* default_mmap_heap_address = |
| 255 mmap(0, kPageSize, PROT_READ|PROT_WRITE, | 295 mmap(0, kPageSize, PROT_READ|PROT_WRITE, |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 285 // kRandomMask, so we use it as an additional detection mechanism. | 325 // kRandomMask, so we use it as an additional detection mechanism. |
| 286 const uintptr_t kRandomMask = 0x3fffffffffffULL; | 326 const uintptr_t kRandomMask = 0x3fffffffffffULL; |
| 287 bool impossible_random_address = | 327 bool impossible_random_address = |
| 288 reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask; | 328 reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask; |
| 289 EXPECT_FALSE(impossible_random_address); | 329 EXPECT_FALSE(impossible_random_address); |
| 290 } | 330 } |
| 291 | 331 |
| 292 #endif // defined(OS_LINUX) && defined(__x86_64__) | 332 #endif // defined(OS_LINUX) && defined(__x86_64__) |
| 293 | 333 |
| 294 } // namespace | 334 } // namespace |
| OLD | NEW |