forked from chromium/crashpad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_snapshot_sanitized_test.cc
312 lines (257 loc) · 9.52 KB
/
process_snapshot_sanitized_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright 2018 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "snapshot/sanitized/process_snapshot_sanitized.h"
#include <string.h>
#include "base/macros.h"
#include "base/notreached.h"
#include "base/stl_util.h"
#include "build/build_config.h"
#include "gtest/gtest.h"
#include "test/multiprocess_exec.h"
#include "util/file/file_io.h"
#include "util/misc/address_sanitizer.h"
#include "util/numeric/safe_assignment.h"
#if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_ANDROID)
#include <sys/syscall.h>
#include "snapshot/linux/process_snapshot_linux.h"
#include "util/linux/direct_ptrace_connection.h"
#include "util/linux/exception_information.h"
#include "util/posix/signals.h"
#endif
namespace crashpad {
namespace test {
namespace {
class ExceptionGenerator {
public:
static ExceptionGenerator* Get() {
static ExceptionGenerator* instance = new ExceptionGenerator();
return instance;
}
bool Initialize(FileHandle in, FileHandle out) {
in_ = in;
out_ = out;
return Signals::InstallCrashHandlers(HandleCrash, 0, nullptr);
}
private:
ExceptionGenerator() = default;
~ExceptionGenerator() = delete;
static void HandleCrash(int signo, siginfo_t* siginfo, void* context) {
auto state = Get();
ExceptionInformation info = {};
info.siginfo_address = FromPointerCast<VMAddress>(siginfo);
info.context_address = FromPointerCast<VMAddress>(context);
info.thread_id = syscall(SYS_gettid);
auto info_addr = FromPointerCast<VMAddress>(&info);
ASSERT_TRUE(LoggingWriteFile(state->out_, &info_addr, sizeof(info_addr)));
CheckedReadFileAtEOF(state->in_);
Signals::RestoreHandlerAndReraiseSignalOnReturn(siginfo, nullptr);
}
FileHandle in_;
FileHandle out_;
DISALLOW_COPY_AND_ASSIGN(ExceptionGenerator);
};
constexpr char kAllowedAnnotationName[] = "name_of_allowed_anno";
constexpr char kAllowedAnnotationValue[] = "some_value";
constexpr char kNonAllowedAnnotationName[] = "non_allowed_anno";
constexpr char kNonAllowedAnnotationValue[] = "private_annotation";
constexpr char kSensitiveStackData[] = "sensitive_stack_data";
struct ChildTestAddresses {
VMAddress string_address;
VMAddress module_address;
VMAddress non_module_address;
VMAddress code_pointer_address;
VMAddress code_pointer_value;
};
void ChildTestFunction() {
FileHandle in = StdioFileHandle(StdioStream::kStandardInput);
FileHandle out = StdioFileHandle(StdioStream::kStandardOutput);
static StringAnnotation<32> allowed_annotation(kAllowedAnnotationName);
allowed_annotation.Set(kAllowedAnnotationValue);
static StringAnnotation<32> non_allowed_annotation(kNonAllowedAnnotationName);
non_allowed_annotation.Set(kNonAllowedAnnotationValue);
char string_data[base::size(kSensitiveStackData)];
strcpy(string_data, kSensitiveStackData);
void (*code_pointer)(void) = ChildTestFunction;
ChildTestAddresses addrs = {};
addrs.string_address = FromPointerCast<VMAddress>(string_data);
addrs.module_address = FromPointerCast<VMAddress>(ChildTestFunction);
addrs.non_module_address = FromPointerCast<VMAddress>(&addrs);
addrs.code_pointer_address = FromPointerCast<VMAddress>(&code_pointer);
addrs.code_pointer_value = FromPointerCast<VMAddress>(code_pointer);
ASSERT_TRUE(LoggingWriteFile(out, &addrs, sizeof(addrs)));
auto gen = ExceptionGenerator::Get();
ASSERT_TRUE(gen->Initialize(in, out));
__builtin_trap();
}
CRASHPAD_CHILD_TEST_MAIN(ChildToBeSanitized) {
ChildTestFunction();
NOTREACHED();
return EXIT_SUCCESS;
}
void ExpectAnnotations(ProcessSnapshot* snapshot, bool sanitized) {
bool found_allowed = false;
bool found_non_allowed = false;
for (auto module : snapshot->Modules()) {
for (const auto& anno : module->AnnotationObjects()) {
if (anno.name == kAllowedAnnotationName) {
found_allowed = true;
} else if (anno.name == kNonAllowedAnnotationName) {
found_non_allowed = true;
}
}
}
EXPECT_TRUE(found_allowed);
if (sanitized) {
EXPECT_FALSE(found_non_allowed);
} else {
EXPECT_TRUE(found_non_allowed);
}
}
void ExpectProcessMemory(ProcessSnapshot* snapshot,
VMAddress allowed_byte,
bool sanitized) {
auto memory = snapshot->Memory();
char out;
EXPECT_TRUE(memory->Read(allowed_byte, 1, &out));
bool disallowed_read = memory->Read(allowed_byte + 1, 1, &out);
if (sanitized) {
EXPECT_FALSE(disallowed_read);
} else {
EXPECT_TRUE(disallowed_read);
}
}
class StackSanitizationChecker : public MemorySnapshot::Delegate {
public:
StackSanitizationChecker() = default;
~StackSanitizationChecker() = default;
void CheckStack(const MemorySnapshot* stack,
const ChildTestAddresses& addrs,
bool is_64_bit,
bool sanitized) {
stack_ = stack;
addrs_ = addrs;
is_64_bit_ = is_64_bit;
sanitized_ = sanitized;
EXPECT_TRUE(stack_->Read(this));
}
// MemorySnapshot::Delegate
bool MemorySnapshotDelegateRead(void* data, size_t size) override {
// AddressSanitizer with use-after-return detection causes stack variables
// to be allocated on the heap.
#if !defined(ADDRESS_SANITIZER)
size_t pointer_offset;
if (!AssignIfInRange(&pointer_offset,
addrs_.code_pointer_address - stack_->Address())) {
ADD_FAILURE();
return false;
}
const auto data_c = static_cast<char*>(data);
VMAddress pointer_value;
if (is_64_bit_) {
pointer_value = *reinterpret_cast<uint64_t*>(data_c + pointer_offset);
} else {
pointer_value = *reinterpret_cast<uint32_t*>(data_c + pointer_offset);
}
EXPECT_EQ(pointer_value, addrs_.code_pointer_value);
size_t string_offset;
if (!AssignIfInRange(&string_offset,
addrs_.string_address - stack_->Address())) {
ADD_FAILURE();
return false;
}
auto string = data_c + string_offset;
if (sanitized_) {
EXPECT_STRNE(string, kSensitiveStackData);
} else {
EXPECT_STREQ(string, kSensitiveStackData);
}
#endif // !ADDRESS_SANITIZER
return true;
}
private:
const MemorySnapshot* stack_;
ChildTestAddresses addrs_;
bool is_64_bit_;
bool sanitized_;
};
void ExpectStackData(ProcessSnapshot* snapshot,
const ChildTestAddresses& addrs,
bool sanitized) {
const ThreadSnapshot* crasher = nullptr;
for (const auto thread : snapshot->Threads()) {
if (thread->ThreadID() == snapshot->Exception()->ThreadID()) {
crasher = thread;
break;
}
}
ASSERT_TRUE(crasher);
const MemorySnapshot* stack = crasher->Stack();
StackSanitizationChecker().CheckStack(
stack, addrs, crasher->Context()->Is64Bit(), sanitized);
}
class SanitizeTest : public MultiprocessExec {
public:
SanitizeTest() : MultiprocessExec() {
SetChildTestMainFunction("ChildToBeSanitized");
SetExpectedChildTerminationBuiltinTrap();
}
~SanitizeTest() = default;
private:
void MultiprocessParent() {
ChildTestAddresses addrs = {};
ASSERT_TRUE(
LoggingReadFileExactly(ReadPipeHandle(), &addrs, sizeof(addrs)));
VMAddress exception_info_addr;
ASSERT_TRUE(LoggingReadFileExactly(
ReadPipeHandle(), &exception_info_addr, sizeof(exception_info_addr)));
DirectPtraceConnection connection;
ASSERT_TRUE(connection.Initialize(ChildProcess()));
ProcessSnapshotLinux snapshot;
ASSERT_TRUE(snapshot.Initialize(&connection));
ASSERT_TRUE(snapshot.InitializeException(exception_info_addr));
ExpectAnnotations(&snapshot, /* sanitized= */ false);
ExpectStackData(&snapshot, addrs, /* sanitized= */ false);
ExpectProcessMemory(&snapshot,
addrs.string_address,
/* sanitized= */ false);
auto allowed_annotations = std::make_unique<std::vector<std::string>>();
allowed_annotations->push_back(kAllowedAnnotationName);
auto allowed_memory_ranges =
std::make_unique<std::vector<std::pair<VMAddress, VMAddress>>>();
allowed_memory_ranges->push_back(
std::make_pair(addrs.string_address, addrs.string_address + 1));
ProcessSnapshotSanitized sanitized;
ASSERT_TRUE(sanitized.Initialize(&snapshot,
std::move(allowed_annotations),
std::move(allowed_memory_ranges),
addrs.module_address,
true));
ExpectAnnotations(&sanitized, /* sanitized= */ true);
ExpectStackData(&sanitized, addrs, /* sanitized= */ true);
ExpectProcessMemory(&sanitized,
addrs.string_address,
/* sanitized= */ true);
ProcessSnapshotSanitized screened_snapshot;
EXPECT_FALSE(screened_snapshot.Initialize(
&snapshot, nullptr, nullptr, addrs.non_module_address, false));
}
DISALLOW_COPY_AND_ASSIGN(SanitizeTest);
};
TEST(ProcessSnapshotSanitized, Sanitize) {
SanitizeTest test;
test.Run();
}
} // namespace
} // namespace test
} // namespace crashpad