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.
10 #include <sys/types.h>
15 #include "base/files/file_util.h"
16 #include "base/logging.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "build/build_config.h"
19 #include "testing/gtest/include/gtest/gtest.h"
31 using std::numeric_limits
;
36 // This is a permitted size but exhausts memory pretty quickly.
37 const size_t kLargePermittedAllocation
= 0x7FFFE000;
39 int OnNoMemory(size_t) {
43 void ExhaustMemoryWithMalloc() {
45 void* buf
= malloc(kLargePermittedAllocation
);
51 void ExhaustMemoryWithRealloc() {
52 size_t size
= kLargePermittedAllocation
;
53 void* buf
= malloc(size
);
57 size
+= kLargePermittedAllocation
;
58 void* new_buf
= realloc(buf
, size
);
66 // This function acts as a compiler optimization barrier. We use it to
67 // prevent the compiler from making an expression a compile-time constant.
68 // We also use it so that the compiler doesn't discard certain return values
69 // as something we don't need (see the comment with calloc below).
70 template <typename Type
>
71 Type
HideValueFromCompiler(volatile Type value
) {
73 // In a GCC compatible compiler (GCC or Clang), make this compiler barrier
74 // more robust than merely using "volatile".
75 __asm__
volatile ("" : "+r" (value
));
80 // Tcmalloc and Windows allocator shim support setting malloc limits.
81 // - NO_TCMALLOC (should be defined if compiled with use_allocator!="tcmalloc")
82 // - ADDRESS_SANITIZER and SYZYASAN because they have their own memory allocator
83 // - IOS does not use tcmalloc
84 // - OS_MACOSX does not use tcmalloc
85 // - Windows allocator shim defines ALLOCATOR_SHIM
86 #if (!defined(NO_TCMALLOC) || defined(ALLOCATOR_SHIM)) && \
87 !defined(ADDRESS_SANITIZER) && !defined(OS_IOS) && !defined(OS_MACOSX) && \
89 #define MALLOC_OVERFLOW_TEST(function) function
91 #define MALLOC_OVERFLOW_TEST(function) DISABLED_##function
94 // TODO(jln): switch to std::numeric_limits<int>::max() when we switch to
96 const size_t kTooBigAllocSize
= INT_MAX
;
98 // Detect runtime TCMalloc bypasses.
99 bool IsTcMallocBypassed() {
100 #if defined(OS_LINUX)
101 // This should detect a TCMalloc bypass from Valgrind.
102 char* g_slice
= getenv("G_SLICE");
103 if (g_slice
&& !strcmp(g_slice
, "always-malloc"))
109 bool CallocDiesOnOOM() {
110 // The sanitizers' calloc dies on OOM instead of returning NULL.
111 // The wrapper function in base/process_util_linux.cc that is used when we
112 // compile without TCMalloc will just die on OOM instead of returning NULL.
113 #if defined(ADDRESS_SANITIZER) || \
114 defined(MEMORY_SANITIZER) || \
115 defined(THREAD_SANITIZER) || \
116 (defined(OS_LINUX) && defined(NO_TCMALLOC))
123 // Fake test that allow to know the state of TCMalloc by looking at bots.
124 TEST(SecurityTest
, MALLOC_OVERFLOW_TEST(IsTCMallocDynamicallyBypassed
)) {
125 printf("Malloc is dynamically bypassed: %s\n",
126 IsTcMallocBypassed() ? "yes." : "no.");
129 // The MemoryAllocationRestrictions* tests test that we can not allocate a
130 // memory range that cannot be indexed via an int. This is used to mitigate
131 // vulnerabilities in libraries that use int instead of size_t. See
134 TEST(SecurityTest
, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsMalloc
)) {
135 if (!IsTcMallocBypassed()) {
136 scoped_ptr
<char, base::FreeDeleter
> ptr(static_cast<char*>(
137 HideValueFromCompiler(malloc(kTooBigAllocSize
))));
142 #if defined(GTEST_HAS_DEATH_TEST) && defined(OS_WIN)
143 TEST(SecurityTest
, MALLOC_OVERFLOW_TEST(MemoryAllocationMallocDeathTest
)) {
144 _set_new_handler(&OnNoMemory
);
147 scoped_ptr
<char, base::FreeDeleter
> ptr
;
148 EXPECT_DEATH(ptr
.reset(static_cast<char*>(
149 HideValueFromCompiler(malloc(kTooBigAllocSize
)))),
153 _set_new_handler(NULL
);
157 TEST(SecurityTest
, MALLOC_OVERFLOW_TEST(MemoryAllocationExhaustDeathTest
)) {
158 _set_new_handler(&OnNoMemory
);
161 ASSERT_DEATH(ExhaustMemoryWithMalloc(), "");
163 _set_new_handler(NULL
);
167 TEST(SecurityTest
, MALLOC_OVERFLOW_TEST(MemoryReallocationExhaustDeathTest
)) {
168 _set_new_handler(&OnNoMemory
);
171 ASSERT_DEATH(ExhaustMemoryWithRealloc(), "");
173 _set_new_handler(NULL
);
178 TEST(SecurityTest
, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsCalloc
)) {
179 if (!IsTcMallocBypassed()) {
180 scoped_ptr
<char, base::FreeDeleter
> ptr(static_cast<char*>(
181 HideValueFromCompiler(calloc(kTooBigAllocSize
, 1))));
186 TEST(SecurityTest
, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsRealloc
)) {
187 if (!IsTcMallocBypassed()) {
188 char* orig_ptr
= static_cast<char*>(malloc(1));
189 ASSERT_TRUE(orig_ptr
);
190 scoped_ptr
<char, base::FreeDeleter
> ptr(static_cast<char*>(
191 HideValueFromCompiler(realloc(orig_ptr
, kTooBigAllocSize
))));
193 // If realloc() did not succeed, we need to free orig_ptr.
199 char large_array
[kTooBigAllocSize
];
202 TEST(SecurityTest
, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsNew
)) {
203 if (!IsTcMallocBypassed()) {
204 scoped_ptr
<VeryLargeStruct
> ptr(
205 HideValueFromCompiler(new (nothrow
) VeryLargeStruct
));
210 #if defined(GTEST_HAS_DEATH_TEST) && defined(OS_WIN)
211 TEST(SecurityTest
, MALLOC_OVERFLOW_TEST(MemoryAllocationNewDeathTest
)) {
212 _set_new_handler(&OnNoMemory
);
214 scoped_ptr
<VeryLargeStruct
> ptr
;
216 ptr
.reset(HideValueFromCompiler(new (nothrow
) VeryLargeStruct
)), "");
219 _set_new_handler(NULL
);
223 TEST(SecurityTest
, MALLOC_OVERFLOW_TEST(MemoryAllocationRestrictionsNewArray
)) {
224 if (!IsTcMallocBypassed()) {
225 scoped_ptr
<char[]> ptr(
226 HideValueFromCompiler(new (nothrow
) char[kTooBigAllocSize
]));
231 // The tests bellow check for overflows in new[] and calloc().
233 // There are platforms where these tests are known to fail. We would like to
234 // be able to easily check the status on the bots, but marking tests as
235 // FAILS_ is too clunky.
236 void OverflowTestsSoftExpectTrue(bool overflow_detected
) {
237 if (!overflow_detected
) {
238 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX)
239 // Sadly, on Linux, Android, and OSX we don't have a good story yet. Don't
240 // fail the test, but report.
241 printf("Platform has overflow: %s\n",
242 !overflow_detected
? "yes." : "no.");
244 // Otherwise, fail the test. (Note: EXPECT are ok in subfunctions, ASSERT
246 EXPECT_TRUE(overflow_detected
);
251 #if defined(OS_IOS) || defined(OS_WIN) || defined(THREAD_SANITIZER) || defined(OS_MACOSX)
252 #define MAYBE_NewOverflow DISABLED_NewOverflow
254 #define MAYBE_NewOverflow NewOverflow
256 // Test array[TooBig][X] and array[X][TooBig] allocations for int overflows.
257 // IOS doesn't honor nothrow, so disable the test there.
258 // Crashes on Windows Dbg builds, disable there as well.
259 // Fails on Mac 10.8 http://crbug.com/227092
260 TEST(SecurityTest
, MAYBE_NewOverflow
) {
261 const size_t kArraySize
= 4096;
262 // We want something "dynamic" here, so that the compiler doesn't
263 // immediately reject crazy arrays.
264 const size_t kDynamicArraySize
= HideValueFromCompiler(kArraySize
);
265 // numeric_limits are still not constexpr until we switch to C++11, so we
267 const size_t kMaxSizeT
= ~static_cast<size_t>(0);
268 ASSERT_EQ(numeric_limits
<size_t>::max(), kMaxSizeT
);
269 const size_t kArraySize2
= kMaxSizeT
/ kArraySize
+ 10;
270 const size_t kDynamicArraySize2
= HideValueFromCompiler(kArraySize2
);
272 scoped_ptr
<char[][kArraySize
]> array_pointer(new (nothrow
)
273 char[kDynamicArraySize2
][kArraySize
]);
274 OverflowTestsSoftExpectTrue(!array_pointer
);
276 // On windows, the compiler prevents static array sizes of more than
277 // 0x7fffffff (error C2148).
278 #if defined(OS_WIN) && defined(ARCH_CPU_64_BITS)
279 ALLOW_UNUSED_LOCAL(kDynamicArraySize
);
282 scoped_ptr
<char[][kArraySize2
]> array_pointer(new (nothrow
)
283 char[kDynamicArraySize
][kArraySize2
]);
284 OverflowTestsSoftExpectTrue(!array_pointer
);
286 #endif // !defined(OS_WIN) || !defined(ARCH_CPU_64_BITS)
289 // Call calloc(), eventually free the memory and return whether or not
290 // calloc() did succeed.
291 bool CallocReturnsNull(size_t nmemb
, size_t size
) {
292 scoped_ptr
<char, base::FreeDeleter
> array_pointer(
293 static_cast<char*>(calloc(nmemb
, size
)));
294 // We need the call to HideValueFromCompiler(): we have seen LLVM
295 // optimize away the call to calloc() entirely and assume
296 // the pointer to not be NULL.
297 return HideValueFromCompiler(array_pointer
.get()) == NULL
;
300 // Test if calloc() can overflow.
301 TEST(SecurityTest
, CallocOverflow
) {
302 const size_t kArraySize
= 4096;
303 const size_t kMaxSizeT
= numeric_limits
<size_t>::max();
304 const size_t kArraySize2
= kMaxSizeT
/ kArraySize
+ 10;
305 if (!CallocDiesOnOOM()) {
306 EXPECT_TRUE(CallocReturnsNull(kArraySize
, kArraySize2
));
307 EXPECT_TRUE(CallocReturnsNull(kArraySize2
, kArraySize
));
309 // It's also ok for calloc to just terminate the process.
310 #if defined(GTEST_HAS_DEATH_TEST)
311 EXPECT_DEATH(CallocReturnsNull(kArraySize
, kArraySize2
), "");
312 EXPECT_DEATH(CallocReturnsNull(kArraySize2
, kArraySize
), "");
313 #endif // GTEST_HAS_DEATH_TEST
317 #if defined(OS_LINUX) && defined(__x86_64__)
318 // Check if ptr1 and ptr2 are separated by less than size chars.
319 bool ArePointersToSameArea(void* ptr1
, void* ptr2
, size_t size
) {
320 ptrdiff_t ptr_diff
= reinterpret_cast<char*>(std::max(ptr1
, ptr2
)) -
321 reinterpret_cast<char*>(std::min(ptr1
, ptr2
));
322 return static_cast<size_t>(ptr_diff
) <= size
;
325 // Check if TCMalloc uses an underlying random memory allocator.
326 TEST(SecurityTest
, MALLOC_OVERFLOW_TEST(RandomMemoryAllocations
)) {
327 if (IsTcMallocBypassed())
329 size_t kPageSize
= 4096; // We support x86_64 only.
330 // Check that malloc() returns an address that is neither the kernel's
331 // un-hinted mmap area, nor the current brk() area. The first malloc() may
332 // not be at a random address because TCMalloc will first exhaust any memory
333 // that it has allocated early on, before starting the sophisticated
335 void* default_mmap_heap_address
=
336 mmap(0, kPageSize
, PROT_READ
|PROT_WRITE
,
337 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
338 ASSERT_NE(default_mmap_heap_address
,
339 static_cast<void*>(MAP_FAILED
));
340 ASSERT_EQ(munmap(default_mmap_heap_address
, kPageSize
), 0);
341 void* brk_heap_address
= sbrk(0);
342 ASSERT_NE(brk_heap_address
, reinterpret_cast<void*>(-1));
343 ASSERT_TRUE(brk_heap_address
!= NULL
);
344 // 1 MB should get us past what TCMalloc pre-allocated before initializing
345 // the sophisticated allocators.
346 size_t kAllocSize
= 1<<20;
347 scoped_ptr
<char, base::FreeDeleter
> ptr(
348 static_cast<char*>(malloc(kAllocSize
)));
349 ASSERT_TRUE(ptr
!= NULL
);
350 // If two pointers are separated by less than 512MB, they are considered
351 // to be in the same area.
352 // Our random pointer could be anywhere within 0x3fffffffffff (46bits),
353 // and we are checking that it's not withing 1GB (30 bits) from two
354 // addresses (brk and mmap heap). We have roughly one chance out of
356 const size_t kAreaRadius
= 1<<29;
357 bool in_default_mmap_heap
= ArePointersToSameArea(
358 ptr
.get(), default_mmap_heap_address
, kAreaRadius
);
359 EXPECT_FALSE(in_default_mmap_heap
);
361 bool in_default_brk_heap
= ArePointersToSameArea(
362 ptr
.get(), brk_heap_address
, kAreaRadius
);
363 EXPECT_FALSE(in_default_brk_heap
);
365 // In the implementation, we always mask our random addresses with
366 // kRandomMask, so we use it as an additional detection mechanism.
367 const uintptr_t kRandomMask
= 0x3fffffffffffULL
;
368 bool impossible_random_address
=
369 reinterpret_cast<uintptr_t>(ptr
.get()) & ~kRandomMask
;
370 EXPECT_FALSE(impossible_random_address
);
373 #endif // defined(OS_LINUX) && defined(__x86_64__)