1 // Copyright 2017 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 "base/memory/protected_memory.h"
6 #include "base/cfi_buildflags.h"
7 #include "base/memory/protected_memory_cfi.h"
8 #include "base/synchronization/lock.h"
9 #include "base/test/gtest_util.h"
10 #include "build/build_config.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace base {
14
15 namespace {
16
17 struct Data {
18 Data() = default;
Database::__anone29f78230111::Data19 Data(int foo_) : foo(foo_) {}
20 int foo;
21 };
22
23 } // namespace
24
25 class ProtectedMemoryTest : public ::testing::Test {
26 protected:
27 // Run tests one at a time. Some of the negative tests can not be made thread
28 // safe.
SetUp()29 void SetUp() final { lock.Acquire(); }
TearDown()30 void TearDown() final { lock.Release(); }
31
32 Lock lock;
33 };
34
35 PROTECTED_MEMORY_SECTION ProtectedMemory<int> init;
36
TEST_F(ProtectedMemoryTest,Initializer)37 TEST_F(ProtectedMemoryTest, Initializer) {
38 static ProtectedMemory<int>::Initializer I(&init, 4);
39 EXPECT_EQ(*init, 4);
40 }
41
42 PROTECTED_MEMORY_SECTION ProtectedMemory<Data> data;
43
TEST_F(ProtectedMemoryTest,Basic)44 TEST_F(ProtectedMemoryTest, Basic) {
45 AutoWritableMemory writer = AutoWritableMemory::Create(data);
46 data->foo = 5;
47 EXPECT_EQ(data->foo, 5);
48 }
49
50 #if defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
51
52 #if PROTECTED_MEMORY_ENABLED
TEST_F(ProtectedMemoryTest,ReadOnlyOnStart)53 TEST_F(ProtectedMemoryTest, ReadOnlyOnStart) {
54 EXPECT_DEATH({ data->foo = 6; AutoWritableMemory::Create(data); }, "");
55 }
56
TEST_F(ProtectedMemoryTest,ReadOnlyAfterSetWritable)57 TEST_F(ProtectedMemoryTest, ReadOnlyAfterSetWritable) {
58 { AutoWritableMemory writer = AutoWritableMemory::Create(data); }
59 EXPECT_DEATH({ data->foo = 7; }, "");
60 }
61
TEST_F(ProtectedMemoryTest,AssertMemoryIsReadOnly)62 TEST_F(ProtectedMemoryTest, AssertMemoryIsReadOnly) {
63 AssertMemoryIsReadOnly(&data->foo);
64 { AutoWritableMemory::Create(data); }
65 AssertMemoryIsReadOnly(&data->foo);
66
67 ProtectedMemory<Data> writable_data;
68 EXPECT_DCHECK_DEATH({ AssertMemoryIsReadOnly(&writable_data->foo); });
69 }
70
TEST_F(ProtectedMemoryTest,FailsIfDefinedOutsideOfProtectMemoryRegion)71 TEST_F(ProtectedMemoryTest, FailsIfDefinedOutsideOfProtectMemoryRegion) {
72 ProtectedMemory<Data> data;
73 EXPECT_DCHECK_DEATH({ AutoWritableMemory::Create(data); });
74 }
75
TEST_F(ProtectedMemoryTest,UnsanitizedCfiCallOutsideOfProtectedMemoryRegion)76 TEST_F(ProtectedMemoryTest, UnsanitizedCfiCallOutsideOfProtectedMemoryRegion) {
77 ProtectedMemory<void (*)(void)> data;
78 EXPECT_DCHECK_DEATH({ UnsanitizedCfiCall(data)(); });
79 }
80 #endif // PROTECTED_MEMORY_ENABLED
81
82 namespace {
83
84 struct BadIcall {
85 BadIcall() = default;
BadIcallbase::__anone29f78230211::BadIcall86 BadIcall(int (*fp_)(int)) : fp(fp_) {}
87 int (*fp)(int);
88 };
89
bad_icall(int i)90 unsigned int bad_icall(int i) {
91 return 4 + i;
92 }
93
94 } // namespace
95
96 PROTECTED_MEMORY_SECTION ProtectedMemory<BadIcall> icall_pm1;
97
TEST_F(ProtectedMemoryTest,BadMemberCall)98 TEST_F(ProtectedMemoryTest, BadMemberCall) {
99 static ProtectedMemory<BadIcall>::Initializer I(
100 &icall_pm1, BadIcall(reinterpret_cast<int (*)(int)>(&bad_icall)));
101
102 EXPECT_EQ(UnsanitizedCfiCall(icall_pm1, &BadIcall::fp)(1), 5);
103 #if !BUILDFLAG(CFI_ICALL_CHECK)
104 EXPECT_EQ(icall_pm1->fp(1), 5);
105 #elif BUILDFLAG(CFI_ENFORCEMENT_TRAP) || BUILDFLAG(CFI_ENFORCEMENT_DIAGNOSTIC)
106 EXPECT_DEATH({ icall_pm1->fp(1); }, "");
107 #endif
108 }
109
110 PROTECTED_MEMORY_SECTION ProtectedMemory<int (*)(int)> icall_pm2;
111
TEST_F(ProtectedMemoryTest,BadFnPtrCall)112 TEST_F(ProtectedMemoryTest, BadFnPtrCall) {
113 static ProtectedMemory<int (*)(int)>::Initializer I(
114 &icall_pm2, reinterpret_cast<int (*)(int)>(&bad_icall));
115
116 EXPECT_EQ(UnsanitizedCfiCall(icall_pm2)(1), 5);
117 #if !BUILDFLAG(CFI_ICALL_CHECK)
118 EXPECT_EQ((*icall_pm2)(1), 5);
119 #elif BUILDFLAG(CFI_ENFORCEMENT_TRAP) || BUILDFLAG(CFI_ENFORCEMENT_DIAGNOSTIC)
120 EXPECT_DEATH({ (*icall_pm2)(1); }, "");
121 #endif
122 }
123
124 #endif // defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
125
126 } // namespace base
127