Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Side by Side Diff: base/security_unittest.cc

Issue 11857007: TCMalloc: restrict maximum size of memory ranges (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Drive-by patch of int to size_t. Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
Chris Evans 2013/01/15 01:53:08 2013
jln (very slow on Chromium) 2013/01/15 02:00:51 Done.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include <algorithm>
10 #include <limits>
11
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using std::nothrow;
17
18 namespace {
19
20 // Check that we can not allocate a memory range that cannot be indexed
21 // via an int. This is used to mitigate vulnerabilities in libraries that use
22 // int instead of size_t.
23 // See crbug.com/169327.
24
25 // - NO_TCMALLOC because we only patched tcmalloc
26 // - ADDRESS_SANITIZER because it has its own memory allocator
27 // - IOS does not seem to honor nothrow in new properly
28 // - OS_MACOSX does not use tcmalloc
29 #if !defined(NO_TCMALLOC) && !defined(ADDRESS_SANITIZER) && \
30 !defined(OS_IOS) && !defined(OS_MACOSX)
31 #define ALLOC_TEST(function) function
32 #else
33 #define ALLOC_TEST(function) DISABLED_##function
34 #endif
35
36 // TODO(jln): switch to std::numeric_limits<int>::max() when we switch to
37 // C++11.
38 const size_t kMaxAllocSize = INT_MAX;
39
40 // Detect runtime TCMalloc bypasses.
41 bool IsTcMallocBypassed() {
42 #if defined(OS_LINUX)
43 // This should detect a TCMalloc bypass from Valgrind.
44 char* g_slice = getenv("G_SLICE");
45 if (g_slice && !strcmp(g_slice, "always-malloc"))
46 return true;
47 #endif
48 return false;
49 }
50
51 // Fake test that allow to know the state of TCMalloc by looking at bots.
52 TEST(SecurityTest, ALLOC_TEST(IsTCMallocDynamicallyBypassed)) {
53 printf("Malloc is dynamically bypassed: %s\n",
54 IsTcMallocBypassed() ? "yes." : "no.");
55 }
56
57 TEST(SecurityTest, ALLOC_TEST(MemoryAllocationRestrictionsMalloc)) {
58 if (!IsTcMallocBypassed()) {
59 scoped_ptr<char, base::FreeDeleter>
60 ptr(static_cast<char*>(malloc(kMaxAllocSize)));
Chris Evans 2013/01/15 01:53:08 It's weird to have a test that "kMaxAllocSize" act
jln (very slow on Chromium) 2013/01/15 02:00:51 Good point, done.
61 ASSERT_TRUE(ptr == NULL);
Chris Evans 2013/01/15 01:53:08 Indentation.
Chris Evans 2013/01/15 01:53:08 Indentation
jln (very slow on Chromium) 2013/01/15 02:00:51 Done.
62 }
63 }
64
65 TEST(SecurityTest, ALLOC_TEST(MemoryAllocationRestrictionsCalloc)) {
66 if (!IsTcMallocBypassed()) {
67 scoped_ptr<char, base::FreeDeleter>
68 ptr(static_cast<char*>(calloc(kMaxAllocSize, 1)));
69 ASSERT_TRUE(ptr == NULL);
70 }
71 }
72
73 TEST(SecurityTest, ALLOC_TEST(MemoryAllocationRestrictionsRealloc)) {
74 if (!IsTcMallocBypassed()) {
75 char* orig_ptr = static_cast<char*>(malloc(1));
76 ASSERT_TRUE(orig_ptr != NULL);
77 scoped_ptr<char, base::FreeDeleter>
78 ptr(static_cast<char*>(realloc(orig_ptr, kMaxAllocSize)));
79 ASSERT_TRUE(ptr == NULL);
80 // If realloc() did not succeed, we need to free orig_ptr.
81 free(orig_ptr);
82 }
83 }
84
85 typedef struct {
86 char large_array[kMaxAllocSize];
87 } VeryLargeStruct;
88
89 TEST(SecurityTest, ALLOC_TEST(MemoryAllocationRestrictionsNew)) {
90 if (!IsTcMallocBypassed()) {
91 scoped_ptr<VeryLargeStruct> ptr(new (nothrow) VeryLargeStruct);
92 ASSERT_TRUE(ptr == NULL);
93 }
94 }
95
96 TEST(SecurityTest, ALLOC_TEST(MemoryAllocationRestrictionsNewArray)) {
97 if (!IsTcMallocBypassed()) {
98 scoped_array<char> ptr(new (nothrow) char[kMaxAllocSize]);
99 ASSERT_TRUE(ptr == NULL);
100 }
101 }
102
103 // This would be a test for a much stricter restriction preventing a contiguous
104 // memory area of size MaximumAllocSize to be allocated via malloc.
105 // Implementing this requires more effort and the test is disabled.
Chris Evans 2013/01/15 01:53:08 I think we're better off not landing to be honest?
jln (very slow on Chromium) 2013/01/15 02:00:51 Done.
106 TEST(SecurityTest, DISABLED_MemoryAllocationRestrictionsStepped) {
107 const int kAllocNumber = 8;
108 const size_t kSteppedAllocSize = kMaxAllocSize / kAllocNumber;
109 // Allow 1/32th for MetaData and padding.
110 const size_t kExpectedExtra = kSteppedAllocSize >> 5;
111
112 bool is_contiguous = true;
113 // Make kAllocNumber successive allocations and compare the successive
114 // pointers to detect adjacent mappings.
115 scoped_ptr<char, base::FreeDeleter> ptr[kAllocNumber];
116 for (int i = 0; i < kAllocNumber; ++i) {
117 ptr[i].reset(static_cast<char*>(malloc(kSteppedAllocSize)));
118 if (ptr[i] == NULL) {
119 is_contiguous = false;
120 break;
121 }
122 if (i > 0) {
123 size_t pointer_diff = static_cast<size_t>(
124 std::max(ptr[i].get(), ptr[i - 1].get()) -
125 std::min(ptr[i].get(), ptr[i - 1].get()));
126 if (pointer_diff > (kSteppedAllocSize + kExpectedExtra)) {
127 is_contiguous = false;
128 break;
129 }
130 }
131 }
132 ASSERT_FALSE(is_contiguous);
133 }
134
135 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698