blob: 8515179be389c7f76887141efc74524a6ec36cff [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>
avia6a6a682015-12-27 07:15:14 +09006#include <stddef.h>
jln@chromium.org4faaf212013-01-16 05:16:33 +09007#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
jln@chromium.orgc71a4da2013-01-31 11:23:43 +090010#include <sys/stat.h>
11#include <sys/types.h>
jln@chromium.org4faaf212013-01-16 05:16:33 +090012
13#include <algorithm>
14#include <limits>
dchengcc8e4d82016-04-05 06:25:51 +090015#include <memory>
jln@chromium.org4faaf212013-01-16 05:16:33 +090016
Scott Violet04992cc2018-02-22 11:08:08 +090017#include "base/allocator/buildflags.h"
brettw@chromium.org01f3da42014-08-14 05:22:14 +090018#include "base/files/file_util.h"
jln@chromium.org4faaf212013-01-16 05:16:33 +090019#include "base/logging.h"
dchenga521e152016-03-26 09:16:27 +090020#include "base/memory/free_deleter.h"
jln@chromium.orga55baa62013-02-05 08:39:48 +090021#include "build/build_config.h"
jln@chromium.org4faaf212013-01-16 05:16:33 +090022#include "testing/gtest/include/gtest/gtest.h"
23
jln@chromium.orga55baa62013-02-05 08:39:48 +090024#if defined(OS_POSIX)
25#include <sys/mman.h>
26#include <unistd.h>
27#endif
28
jln@chromium.org4faaf212013-01-16 05:16:33 +090029using std::nothrow;
jln@chromium.orgd06052c2013-01-26 13:41:15 +090030using std::numeric_limits;
jln@chromium.org4faaf212013-01-16 05:16:33 +090031
32namespace {
33
jln@chromium.orgc6d44222013-02-06 12:23:49 +090034// This function acts as a compiler optimization barrier. We use it to
35// prevent the compiler from making an expression a compile-time constant.
36// We also use it so that the compiler doesn't discard certain return values
37// as something we don't need (see the comment with calloc below).
38template <typename Type>
thakis497632e2015-04-23 11:58:29 +090039NOINLINE Type HideValueFromCompiler(volatile Type value) {
jln@chromium.org59755862013-04-04 21:02:35 +090040#if defined(__GNUC__)
41 // In a GCC compatible compiler (GCC or Clang), make this compiler barrier
42 // more robust than merely using "volatile".
43 __asm__ volatile ("" : "+r" (value));
44#endif // __GNUC__
jln@chromium.orgc6d44222013-02-06 12:23:49 +090045 return value;
46}
47
primiano0d396ed2017-05-27 00:48:32 +090048// TCmalloc, currently supported only by Linux/CrOS, supports malloc limits.
dmikurube@chromium.org1df36512014-03-06 05:07:26 +090049// - NO_TCMALLOC (should be defined if compiled with use_allocator!="tcmalloc")
primiano0d396ed2017-05-27 00:48:32 +090050// - ADDRESS_SANITIZER it has its own memory allocator
51#if defined(OS_LINUX) && !defined(NO_TCMALLOC) && !defined(ADDRESS_SANITIZER)
wfhf2a57fa2015-01-13 12:11:40 +090052#define MALLOC_OVERFLOW_TEST(function) function
jln@chromium.org4faaf212013-01-16 05:16:33 +090053#else
wfhf2a57fa2015-01-13 12:11:40 +090054#define MALLOC_OVERFLOW_TEST(function) DISABLED_##function
jln@chromium.org4faaf212013-01-16 05:16:33 +090055#endif
56
jln@chromium.orgd06052c2013-01-26 13:41:15 +090057// There are platforms where these tests are known to fail. We would like to
58// be able to easily check the status on the bots, but marking tests as
59// FAILS_ is too clunky.
60void OverflowTestsSoftExpectTrue(bool overflow_detected) {
61 if (!overflow_detected) {
62#if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX)
63 // Sadly, on Linux, Android, and OSX we don't have a good story yet. Don't
64 // fail the test, but report.
65 printf("Platform has overflow: %s\n",
66 !overflow_detected ? "yes." : "no.");
67#else
68 // Otherwise, fail the test. (Note: EXPECT are ok in subfunctions, ASSERT
69 // aren't).
70 EXPECT_TRUE(overflow_detected);
71#endif
72 }
73}
74
thakis3f3f37c2017-03-01 22:05:35 +090075#if defined(OS_IOS) || defined(ADDRESS_SANITIZER) || \
76 defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER)
John Abd-El-Malekccd3dd32014-10-03 07:55:15 +090077#define MAYBE_NewOverflow DISABLED_NewOverflow
78#else
79#define MAYBE_NewOverflow NewOverflow
80#endif
jln@chromium.orgd06052c2013-01-26 13:41:15 +090081// Test array[TooBig][X] and array[X][TooBig] allocations for int overflows.
82// IOS doesn't honor nothrow, so disable the test there.
thakis4c9e69f2017-02-22 02:20:41 +090083// Disabled under XSan because asan aborts when new returns nullptr,
thakis27301ae2017-02-15 01:21:35 +090084// https://bugs.chromium.org/p/chromium/issues/detail?id=690271#c15
John Abd-El-Malekccd3dd32014-10-03 07:55:15 +090085TEST(SecurityTest, MAYBE_NewOverflow) {
jln@chromium.orgd06052c2013-01-26 13:41:15 +090086 const size_t kArraySize = 4096;
87 // We want something "dynamic" here, so that the compiler doesn't
88 // immediately reject crazy arrays.
89 const size_t kDynamicArraySize = HideValueFromCompiler(kArraySize);
thakis27301ae2017-02-15 01:21:35 +090090 const size_t kMaxSizeT = std::numeric_limits<size_t>::max();
jln@chromium.orgd06052c2013-01-26 13:41:15 +090091 const size_t kArraySize2 = kMaxSizeT / kArraySize + 10;
92 const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2);
93 {
dchengcc8e4d82016-04-05 06:25:51 +090094 std::unique_ptr<char[][kArraySize]> array_pointer(
95 new (nothrow) char[kDynamicArraySize2][kArraySize]);
thakis27301ae2017-02-15 01:21:35 +090096 // Prevent clang from optimizing away the whole test.
97 char* volatile p = reinterpret_cast<char*>(array_pointer.get());
98 OverflowTestsSoftExpectTrue(!p);
jln@chromium.orgd06052c2013-01-26 13:41:15 +090099 }
jln@chromium.org59755862013-04-04 21:02:35 +0900100 // On windows, the compiler prevents static array sizes of more than
101 // 0x7fffffff (error C2148).
Peter Kasting618317c2014-11-21 08:14:08 +0900102#if defined(OS_WIN) && defined(ARCH_CPU_64_BITS)
103 ALLOW_UNUSED_LOCAL(kDynamicArraySize);
104#else
jln@chromium.orgd06052c2013-01-26 13:41:15 +0900105 {
dchengcc8e4d82016-04-05 06:25:51 +0900106 std::unique_ptr<char[][kArraySize2]> array_pointer(
107 new (nothrow) char[kDynamicArraySize][kArraySize2]);
thakis27301ae2017-02-15 01:21:35 +0900108 // Prevent clang from optimizing away the whole test.
109 char* volatile p = reinterpret_cast<char*>(array_pointer.get());
110 OverflowTestsSoftExpectTrue(!p);
jln@chromium.orgd06052c2013-01-26 13:41:15 +0900111 }
jln@chromium.org59755862013-04-04 21:02:35 +0900112#endif // !defined(OS_WIN) || !defined(ARCH_CPU_64_BITS)
jln@chromium.orgd06052c2013-01-26 13:41:15 +0900113}
114
estade@chromium.orgd841d6d2014-04-16 05:58:09 +0900115#if defined(OS_LINUX) && defined(__x86_64__)
jln@chromium.orga55baa62013-02-05 08:39:48 +0900116// Check if ptr1 and ptr2 are separated by less than size chars.
117bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) {
118 ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) -
119 reinterpret_cast<char*>(std::min(ptr1, ptr2));
120 return static_cast<size_t>(ptr_diff) <= size;
121}
122
jln@chromium.orgc71a4da2013-01-31 11:23:43 +0900123// Check if TCMalloc uses an underlying random memory allocator.
wfhf2a57fa2015-01-13 12:11:40 +0900124TEST(SecurityTest, MALLOC_OVERFLOW_TEST(RandomMemoryAllocations)) {
jln@chromium.orga55baa62013-02-05 08:39:48 +0900125 size_t kPageSize = 4096; // We support x86_64 only.
126 // Check that malloc() returns an address that is neither the kernel's
127 // un-hinted mmap area, nor the current brk() area. The first malloc() may
128 // not be at a random address because TCMalloc will first exhaust any memory
129 // that it has allocated early on, before starting the sophisticated
130 // allocators.
131 void* default_mmap_heap_address =
Ivan Kotenkove88f3462017-11-08 21:37:33 +0900132 mmap(nullptr, kPageSize, PROT_READ | PROT_WRITE,
133 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
jln@chromium.orga55baa62013-02-05 08:39:48 +0900134 ASSERT_NE(default_mmap_heap_address,
135 static_cast<void*>(MAP_FAILED));
136 ASSERT_EQ(munmap(default_mmap_heap_address, kPageSize), 0);
137 void* brk_heap_address = sbrk(0);
138 ASSERT_NE(brk_heap_address, reinterpret_cast<void*>(-1));
Ivan Kotenkove88f3462017-11-08 21:37:33 +0900139 ASSERT_TRUE(brk_heap_address != nullptr);
jln@chromium.orga55baa62013-02-05 08:39:48 +0900140 // 1 MB should get us past what TCMalloc pre-allocated before initializing
141 // the sophisticated allocators.
142 size_t kAllocSize = 1<<20;
dchengcc8e4d82016-04-05 06:25:51 +0900143 std::unique_ptr<char, base::FreeDeleter> ptr(
jln@chromium.orga55baa62013-02-05 08:39:48 +0900144 static_cast<char*>(malloc(kAllocSize)));
Ivan Kotenkove88f3462017-11-08 21:37:33 +0900145 ASSERT_TRUE(ptr != nullptr);
jln@chromium.orga55baa62013-02-05 08:39:48 +0900146 // If two pointers are separated by less than 512MB, they are considered
147 // to be in the same area.
148 // Our random pointer could be anywhere within 0x3fffffffffff (46bits),
149 // and we are checking that it's not withing 1GB (30 bits) from two
150 // addresses (brk and mmap heap). We have roughly one chance out of
151 // 2^15 to flake.
152 const size_t kAreaRadius = 1<<29;
153 bool in_default_mmap_heap = ArePointersToSameArea(
154 ptr.get(), default_mmap_heap_address, kAreaRadius);
155 EXPECT_FALSE(in_default_mmap_heap);
156
157 bool in_default_brk_heap = ArePointersToSameArea(
158 ptr.get(), brk_heap_address, kAreaRadius);
159 EXPECT_FALSE(in_default_brk_heap);
160
161 // In the implementation, we always mask our random addresses with
162 // kRandomMask, so we use it as an additional detection mechanism.
163 const uintptr_t kRandomMask = 0x3fffffffffffULL;
164 bool impossible_random_address =
165 reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask;
166 EXPECT_FALSE(impossible_random_address);
jln@chromium.orgc71a4da2013-01-31 11:23:43 +0900167}
168
estade@chromium.orgd841d6d2014-04-16 05:58:09 +0900169#endif // defined(OS_LINUX) && defined(__x86_64__)
jln@chromium.orgc71a4da2013-01-31 11:23:43 +0900170
jln@chromium.org4faaf212013-01-16 05:16:33 +0900171} // namespace