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

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: Nits from Cevans. 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) 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
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 kTooBigAllocSize = INT_MAX;
39
40 // Detect runtime TCMalloc bypasses.
41 bool IsTcMallocBypassed() {
42 #if defined(OS_LINUX) || defined(OS_CHROMEOS)
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(kTooBigAllocSize)));
61 ASSERT_TRUE(ptr == NULL);
62 }
63 }
64
65 TEST(SecurityTest, ALLOC_TEST(MemoryAllocationRestrictionsCalloc)) {
66 if (!IsTcMallocBypassed()) {
67 scoped_ptr<char, base::FreeDeleter>
68 ptr(static_cast<char*>(calloc(kTooBigAllocSize, 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, kTooBigAllocSize)));
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[kTooBigAllocSize];
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[kTooBigAllocSize]);
99 ASSERT_TRUE(ptr == NULL);
100 }
101 }
102
103 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698