blob: af9d2bf19d3a52b15a53fdbd1032afe639336fac [file] [log] [blame]
[email protected]b5bf9a132013-01-15 20:16:331// 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
[email protected]1b556f82013-01-31 02:23:435#include <fcntl.h>
avi9b6f42932015-12-26 22:15:146#include <stddef.h>
[email protected]b5bf9a132013-01-15 20:16:337#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
[email protected]1b556f82013-01-31 02:23:4310#include <sys/stat.h>
11#include <sys/types.h>
[email protected]b5bf9a132013-01-15 20:16:3312
13#include <algorithm>
14#include <limits>
dcheng093de9b2016-04-04 21:25:5115#include <memory>
[email protected]b5bf9a132013-01-15 20:16:3316
[email protected]e3177dd52014-08-13 20:22:1417#include "base/files/file_util.h"
[email protected]b5bf9a132013-01-15 20:16:3318#include "base/logging.h"
dchengdb5935f2016-03-26 00:16:2719#include "base/memory/free_deleter.h"
[email protected]547683f2013-02-04 23:39:4820#include "build/build_config.h"
[email protected]b5bf9a132013-01-15 20:16:3321#include "testing/gtest/include/gtest/gtest.h"
22
[email protected]547683f2013-02-04 23:39:4823#if defined(OS_POSIX)
24#include <sys/mman.h>
25#include <unistd.h>
26#endif
27
[email protected]b5bf9a132013-01-15 20:16:3328using std::nothrow;
[email protected]9c4729b2013-01-26 04:41:1529using std::numeric_limits;
[email protected]b5bf9a132013-01-15 20:16:3330
31namespace {
32
[email protected]fe394f32013-02-06 03:23:4933// 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).
37template <typename Type>
thakis189ce1f2015-04-23 02:58:2938NOINLINE Type HideValueFromCompiler(volatile Type value) {
[email protected]1cdfdb72013-04-04 12:02:3539#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__
[email protected]fe394f32013-02-06 03:23:4944 return value;
45}
46
wfh74a449ca2015-01-13 03:11:4047// Tcmalloc and Windows allocator shim support setting malloc limits.
[email protected]5fcce3f72014-03-05 20:07:2648// - NO_TCMALLOC (should be defined if compiled with use_allocator!="tcmalloc")
[email protected]aee2f332014-03-27 15:08:0449// - ADDRESS_SANITIZER and SYZYASAN because they have their own memory allocator
[email protected]d6a6cea2013-02-15 06:10:4050// - IOS does not use tcmalloc
[email protected]b5bf9a132013-01-15 20:16:3351// - OS_MACOSX does not use tcmalloc
wfh74a449ca2015-01-13 03:11:4052// - 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
[email protected]b5bf9a132013-01-15 20:16:3357#else
wfh74a449ca2015-01-13 03:11:4058#define MALLOC_OVERFLOW_TEST(function) DISABLED_##function
[email protected]b5bf9a132013-01-15 20:16:3359#endif
60
wfh1bf93992015-09-21 02:21:1461#if defined(OS_LINUX) && defined(__x86_64__)
[email protected]b5bf9a132013-01-15 20:16:3362// Detect runtime TCMalloc bypasses.
63bool IsTcMallocBypassed() {
[email protected]b5bf9a132013-01-15 20:16:3364 // 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;
[email protected]b5bf9a132013-01-15 20:16:3368 return false;
69}
[email protected]d6a6cea2013-02-15 06:10:4070#endif
[email protected]9c4729b2013-01-26 04:41:1571
[email protected]9c4729b2013-01-26 04:41:1572// 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.
75void 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
rsesek25fea6f2016-04-05 19:43:5290#if defined(OS_IOS) || defined(OS_WIN) || defined(OS_LINUX)
John Abd-El-Malek17727ff2014-10-02 22:55:1591#define MAYBE_NewOverflow DISABLED_NewOverflow
92#else
93#define MAYBE_NewOverflow NewOverflow
94#endif
[email protected]9c4729b2013-01-26 04:41:1595// Test array[TooBig][X] and array[X][TooBig] allocations for int overflows.
96// IOS doesn't honor nothrow, so disable the test there.
[email protected]1cdfdb72013-04-04 12:02:3597// Crashes on Windows Dbg builds, disable there as well.
tommyclibfaeb592016-02-09 22:42:5898// Disabled on Linux because failing Linux Valgrind bot, and Valgrind exclusions
99// are not currently read. See http://crbug.com/582398
John Abd-El-Malek17727ff2014-10-02 22:55:15100TEST(SecurityTest, MAYBE_NewOverflow) {
[email protected]9c4729b2013-01-26 04:41:15101 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 {
dcheng093de9b2016-04-04 21:25:51112 std::unique_ptr<char[][kArraySize]> array_pointer(
113 new (nothrow) char[kDynamicArraySize2][kArraySize]);
[email protected]fe394f32013-02-06 03:23:49114 OverflowTestsSoftExpectTrue(!array_pointer);
[email protected]9c4729b2013-01-26 04:41:15115 }
[email protected]1cdfdb72013-04-04 12:02:35116 // On windows, the compiler prevents static array sizes of more than
117 // 0x7fffffff (error C2148).
Peter Kastingbe940e92014-11-20 23:14:08118#if defined(OS_WIN) && defined(ARCH_CPU_64_BITS)
119 ALLOW_UNUSED_LOCAL(kDynamicArraySize);
120#else
[email protected]9c4729b2013-01-26 04:41:15121 {
dcheng093de9b2016-04-04 21:25:51122 std::unique_ptr<char[][kArraySize2]> array_pointer(
123 new (nothrow) char[kDynamicArraySize][kArraySize2]);
[email protected]fe394f32013-02-06 03:23:49124 OverflowTestsSoftExpectTrue(!array_pointer);
[email protected]9c4729b2013-01-26 04:41:15125 }
[email protected]1cdfdb72013-04-04 12:02:35126#endif // !defined(OS_WIN) || !defined(ARCH_CPU_64_BITS)
[email protected]9c4729b2013-01-26 04:41:15127}
128
[email protected]ab68db2d2014-04-15 20:58:09129#if defined(OS_LINUX) && defined(__x86_64__)
[email protected]547683f2013-02-04 23:39:48130// Check if ptr1 and ptr2 are separated by less than size chars.
131bool 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
[email protected]1b556f82013-01-31 02:23:43137// Check if TCMalloc uses an underlying random memory allocator.
wfh74a449ca2015-01-13 03:11:40138TEST(SecurityTest, MALLOC_OVERFLOW_TEST(RandomMemoryAllocations)) {
[email protected]1b556f82013-01-31 02:23:43139 if (IsTcMallocBypassed())
140 return;
[email protected]547683f2013-02-04 23:39:48141 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;
dcheng093de9b2016-04-04 21:25:51159 std::unique_ptr<char, base::FreeDeleter> ptr(
[email protected]547683f2013-02-04 23:39:48160 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);
[email protected]1b556f82013-01-31 02:23:43183}
184
[email protected]ab68db2d2014-04-15 20:58:09185#endif // defined(OS_LINUX) && defined(__x86_64__)
[email protected]1b556f82013-01-31 02:23:43186
[email protected]b5bf9a132013-01-15 20:16:33187} // namespace