blob: a9c73f7c5927592ecdb8a9f867c0dd24ad60c3fb [file] [log] [blame]
jln@chromium.org4faaf212013-01-16 05:16:33 +09001// 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.orgc71a4da2013-01-31 11:23:43 +09005#include <fcntl.h>
jln@chromium.org4faaf212013-01-16 05:16:33 +09006#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
jln@chromium.orgc71a4da2013-01-31 11:23:43 +09009#include <sys/stat.h>
10#include <sys/types.h>
jln@chromium.org4faaf212013-01-16 05:16:33 +090011
12#include <algorithm>
13#include <limits>
14
brettw@chromium.org01f3da42014-08-14 05:22:14 +090015#include "base/files/file_util.h"
jln@chromium.org4faaf212013-01-16 05:16:33 +090016#include "base/logging.h"
17#include "base/memory/scoped_ptr.h"
jln@chromium.orga55baa62013-02-05 08:39:48 +090018#include "build/build_config.h"
jln@chromium.org4faaf212013-01-16 05:16:33 +090019#include "testing/gtest/include/gtest/gtest.h"
20
jln@chromium.orga55baa62013-02-05 08:39:48 +090021#if defined(OS_POSIX)
22#include <sys/mman.h>
23#include <unistd.h>
24#endif
25
jln@chromium.org4faaf212013-01-16 05:16:33 +090026using std::nothrow;
jln@chromium.orgd06052c2013-01-26 13:41:15 +090027using std::numeric_limits;
jln@chromium.org4faaf212013-01-16 05:16:33 +090028
29namespace {
30
jln@chromium.orgc6d44222013-02-06 12:23:49 +090031// This function acts as a compiler optimization barrier. We use it to
32// 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
34// as something we don't need (see the comment with calloc below).
35template <typename Type>
thakis497632e2015-04-23 11:58:29 +090036NOINLINE Type HideValueFromCompiler(volatile Type value) {
jln@chromium.org59755862013-04-04 21:02:35 +090037#if defined(__GNUC__)
38 // In a GCC compatible compiler (GCC or Clang), make this compiler barrier
39 // more robust than merely using "volatile".
40 __asm__ volatile ("" : "+r" (value));
41#endif // __GNUC__
jln@chromium.orgc6d44222013-02-06 12:23:49 +090042 return value;
43}
44
wfhf2a57fa2015-01-13 12:11:40 +090045// Tcmalloc and Windows allocator shim support setting malloc limits.
dmikurube@chromium.org1df36512014-03-06 05:07:26 +090046// - NO_TCMALLOC (should be defined if compiled with use_allocator!="tcmalloc")
chrisha@google.com87839ab2014-03-28 00:08:04 +090047// - ADDRESS_SANITIZER and SYZYASAN because they have their own memory allocator
jln@chromium.orgfe78d552013-02-15 15:10:40 +090048// - IOS does not use tcmalloc
jln@chromium.org4faaf212013-01-16 05:16:33 +090049// - OS_MACOSX does not use tcmalloc
wfhf2a57fa2015-01-13 12:11:40 +090050// - Windows allocator shim defines ALLOCATOR_SHIM
51#if (!defined(NO_TCMALLOC) || defined(ALLOCATOR_SHIM)) && \
52 !defined(ADDRESS_SANITIZER) && !defined(OS_IOS) && !defined(OS_MACOSX) && \
53 !defined(SYZYASAN)
54#define MALLOC_OVERFLOW_TEST(function) function
jln@chromium.org4faaf212013-01-16 05:16:33 +090055#else
wfhf2a57fa2015-01-13 12:11:40 +090056#define MALLOC_OVERFLOW_TEST(function) DISABLED_##function
jln@chromium.org4faaf212013-01-16 05:16:33 +090057#endif
58
wfh26bb6442015-09-21 11:21:14 +090059#if defined(OS_LINUX) && defined(__x86_64__)
jln@chromium.org4faaf212013-01-16 05:16:33 +090060// Detect runtime TCMalloc bypasses.
61bool IsTcMallocBypassed() {
jln@chromium.org4faaf212013-01-16 05:16:33 +090062 // This should detect a TCMalloc bypass from Valgrind.
63 char* g_slice = getenv("G_SLICE");
64 if (g_slice && !strcmp(g_slice, "always-malloc"))
65 return true;
jln@chromium.org4faaf212013-01-16 05:16:33 +090066 return false;
67}
jln@chromium.orgfe78d552013-02-15 15:10:40 +090068#endif
jln@chromium.orgd06052c2013-01-26 13:41:15 +090069
jln@chromium.orgd06052c2013-01-26 13:41:15 +090070// There are platforms where these tests are known to fail. We would like to
71// be able to easily check the status on the bots, but marking tests as
72// FAILS_ is too clunky.
73void OverflowTestsSoftExpectTrue(bool overflow_detected) {
74 if (!overflow_detected) {
75#if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX)
76 // Sadly, on Linux, Android, and OSX we don't have a good story yet. Don't
77 // fail the test, but report.
78 printf("Platform has overflow: %s\n",
79 !overflow_detected ? "yes." : "no.");
80#else
81 // Otherwise, fail the test. (Note: EXPECT are ok in subfunctions, ASSERT
82 // aren't).
83 EXPECT_TRUE(overflow_detected);
84#endif
85 }
86}
87
John Abd-El-Malekccd3dd32014-10-03 07:55:15 +090088#if defined(OS_IOS) || defined(OS_WIN) || defined(THREAD_SANITIZER) || defined(OS_MACOSX)
89#define MAYBE_NewOverflow DISABLED_NewOverflow
90#else
91#define MAYBE_NewOverflow NewOverflow
92#endif
jln@chromium.orgd06052c2013-01-26 13:41:15 +090093// Test array[TooBig][X] and array[X][TooBig] allocations for int overflows.
94// IOS doesn't honor nothrow, so disable the test there.
jln@chromium.org59755862013-04-04 21:02:35 +090095// Crashes on Windows Dbg builds, disable there as well.
John Abd-El-Malekccd3dd32014-10-03 07:55:15 +090096// Fails on Mac 10.8 http://crbug.com/227092
97TEST(SecurityTest, MAYBE_NewOverflow) {
jln@chromium.orgd06052c2013-01-26 13:41:15 +090098 const size_t kArraySize = 4096;
99 // We want something "dynamic" here, so that the compiler doesn't
100 // immediately reject crazy arrays.
101 const size_t kDynamicArraySize = HideValueFromCompiler(kArraySize);
102 // numeric_limits are still not constexpr until we switch to C++11, so we
103 // use an ugly cast.
104 const size_t kMaxSizeT = ~static_cast<size_t>(0);
105 ASSERT_EQ(numeric_limits<size_t>::max(), kMaxSizeT);
106 const size_t kArraySize2 = kMaxSizeT / kArraySize + 10;
107 const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2);
108 {
109 scoped_ptr<char[][kArraySize]> array_pointer(new (nothrow)
110 char[kDynamicArraySize2][kArraySize]);
jln@chromium.orgc6d44222013-02-06 12:23:49 +0900111 OverflowTestsSoftExpectTrue(!array_pointer);
jln@chromium.orgd06052c2013-01-26 13:41:15 +0900112 }
jln@chromium.org59755862013-04-04 21:02:35 +0900113 // On windows, the compiler prevents static array sizes of more than
114 // 0x7fffffff (error C2148).
Peter Kasting618317c2014-11-21 08:14:08 +0900115#if defined(OS_WIN) && defined(ARCH_CPU_64_BITS)
116 ALLOW_UNUSED_LOCAL(kDynamicArraySize);
117#else
jln@chromium.orgd06052c2013-01-26 13:41:15 +0900118 {
119 scoped_ptr<char[][kArraySize2]> array_pointer(new (nothrow)
120 char[kDynamicArraySize][kArraySize2]);
jln@chromium.orgc6d44222013-02-06 12:23:49 +0900121 OverflowTestsSoftExpectTrue(!array_pointer);
jln@chromium.orgd06052c2013-01-26 13:41:15 +0900122 }
jln@chromium.org59755862013-04-04 21:02:35 +0900123#endif // !defined(OS_WIN) || !defined(ARCH_CPU_64_BITS)
jln@chromium.orgd06052c2013-01-26 13:41:15 +0900124}
125
estade@chromium.orgd841d6d2014-04-16 05:58:09 +0900126#if defined(OS_LINUX) && defined(__x86_64__)
jln@chromium.orga55baa62013-02-05 08:39:48 +0900127// Check if ptr1 and ptr2 are separated by less than size chars.
128bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) {
129 ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) -
130 reinterpret_cast<char*>(std::min(ptr1, ptr2));
131 return static_cast<size_t>(ptr_diff) <= size;
132}
133
jln@chromium.orgc71a4da2013-01-31 11:23:43 +0900134// Check if TCMalloc uses an underlying random memory allocator.
wfhf2a57fa2015-01-13 12:11:40 +0900135TEST(SecurityTest, MALLOC_OVERFLOW_TEST(RandomMemoryAllocations)) {
jln@chromium.orgc71a4da2013-01-31 11:23:43 +0900136 if (IsTcMallocBypassed())
137 return;
jln@chromium.orga55baa62013-02-05 08:39:48 +0900138 size_t kPageSize = 4096; // We support x86_64 only.
139 // Check that malloc() returns an address that is neither the kernel's
140 // un-hinted mmap area, nor the current brk() area. The first malloc() may
141 // not be at a random address because TCMalloc will first exhaust any memory
142 // that it has allocated early on, before starting the sophisticated
143 // allocators.
144 void* default_mmap_heap_address =
145 mmap(0, kPageSize, PROT_READ|PROT_WRITE,
146 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
147 ASSERT_NE(default_mmap_heap_address,
148 static_cast<void*>(MAP_FAILED));
149 ASSERT_EQ(munmap(default_mmap_heap_address, kPageSize), 0);
150 void* brk_heap_address = sbrk(0);
151 ASSERT_NE(brk_heap_address, reinterpret_cast<void*>(-1));
152 ASSERT_TRUE(brk_heap_address != NULL);
153 // 1 MB should get us past what TCMalloc pre-allocated before initializing
154 // the sophisticated allocators.
155 size_t kAllocSize = 1<<20;
156 scoped_ptr<char, base::FreeDeleter> ptr(
157 static_cast<char*>(malloc(kAllocSize)));
158 ASSERT_TRUE(ptr != NULL);
159 // If two pointers are separated by less than 512MB, they are considered
160 // to be in the same area.
161 // Our random pointer could be anywhere within 0x3fffffffffff (46bits),
162 // and we are checking that it's not withing 1GB (30 bits) from two
163 // addresses (brk and mmap heap). We have roughly one chance out of
164 // 2^15 to flake.
165 const size_t kAreaRadius = 1<<29;
166 bool in_default_mmap_heap = ArePointersToSameArea(
167 ptr.get(), default_mmap_heap_address, kAreaRadius);
168 EXPECT_FALSE(in_default_mmap_heap);
169
170 bool in_default_brk_heap = ArePointersToSameArea(
171 ptr.get(), brk_heap_address, kAreaRadius);
172 EXPECT_FALSE(in_default_brk_heap);
173
174 // In the implementation, we always mask our random addresses with
175 // kRandomMask, so we use it as an additional detection mechanism.
176 const uintptr_t kRandomMask = 0x3fffffffffffULL;
177 bool impossible_random_address =
178 reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask;
179 EXPECT_FALSE(impossible_random_address);
jln@chromium.orgc71a4da2013-01-31 11:23:43 +0900180}
181
estade@chromium.orgd841d6d2014-04-16 05:58:09 +0900182#endif // defined(OS_LINUX) && defined(__x86_64__)
jln@chromium.orgc71a4da2013-01-31 11:23:43 +0900183
jln@chromium.org4faaf212013-01-16 05:16:33 +0900184} // namespace