blob: 410c4ae5973bd67eff2071d236c16bf6b5b918fe [file] [log] [blame]
Josh Gaocbe70cb2016-10-18 18:17:52 -07001/*
2 * Copyright 2016, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <err.h>
18#include <fcntl.h>
19#include <unistd.h>
Josh Gao502cfd22017-02-17 01:39:15 -080020#include <sys/capability.h>
Josh Gaofca7ca32017-01-23 12:05:35 -080021#include <sys/prctl.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070022#include <sys/types.h>
23
24#include <chrono>
25#include <regex>
26#include <thread>
27
Josh Gao502cfd22017-02-17 01:39:15 -080028#include <android/set_abort_message.h>
29
Josh Gaocbe70cb2016-10-18 18:17:52 -070030#include <android-base/file.h>
31#include <android-base/logging.h>
32#include <android-base/parseint.h>
33#include <android-base/properties.h>
34#include <android-base/strings.h>
35#include <android-base/unique_fd.h>
36#include <cutils/sockets.h>
37#include <debuggerd/handler.h>
38#include <debuggerd/protocol.h>
39#include <debuggerd/util.h>
40#include <gtest/gtest.h>
41
42using namespace std::chrono_literals;
43using android::base::unique_fd;
44
45#if defined(__LP64__)
Josh Gaocbe70cb2016-10-18 18:17:52 -070046#define ARCH_SUFFIX "64"
47#else
Josh Gaocbe70cb2016-10-18 18:17:52 -070048#define ARCH_SUFFIX ""
49#endif
50
51constexpr char kWaitForGdbKey[] = "debug.debuggerd.wait_for_gdb";
52
53#define TIMEOUT(seconds, expr) \
54 [&]() { \
55 struct sigaction old_sigaction; \
56 struct sigaction new_sigaction = {}; \
57 new_sigaction.sa_handler = [](int) {}; \
58 if (sigaction(SIGALRM, &new_sigaction, &new_sigaction) != 0) { \
59 err(1, "sigaction failed"); \
60 } \
61 alarm(seconds); \
62 auto value = expr; \
63 int saved_errno = errno; \
64 if (sigaction(SIGALRM, &old_sigaction, nullptr) != 0) { \
65 err(1, "sigaction failed"); \
66 } \
67 alarm(0); \
68 errno = saved_errno; \
69 return value; \
70 }()
71
72#define ASSERT_MATCH(str, pattern) \
73 do { \
74 std::regex r((pattern)); \
75 if (!std::regex_search((str), r)) { \
76 FAIL() << "regex mismatch: expected " << (pattern) << " in: \n" << (str); \
77 } \
78 } while (0)
79
Josh Gao460b3362017-03-30 16:40:47 -070080static void tombstoned_intercept(pid_t target_pid, unique_fd* intercept_fd, unique_fd* output_fd) {
81 intercept_fd->reset(socket_local_client(kTombstonedInterceptSocketName,
82 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
83 if (intercept_fd->get() == -1) {
84 FAIL() << "failed to contact tombstoned: " << strerror(errno);
85 }
86
87 InterceptRequest req = {.pid = target_pid};
88
89 unique_fd output_pipe_write;
90 if (!Pipe(output_fd, &output_pipe_write)) {
91 FAIL() << "failed to create output pipe: " << strerror(errno);
92 }
93
94 std::string pipe_size_str;
95 int pipe_buffer_size;
96 if (!android::base::ReadFileToString("/proc/sys/fs/pipe-max-size", &pipe_size_str)) {
97 FAIL() << "failed to read /proc/sys/fs/pipe-max-size: " << strerror(errno);
98 }
99
100 pipe_size_str = android::base::Trim(pipe_size_str);
101
102 if (!android::base::ParseInt(pipe_size_str.c_str(), &pipe_buffer_size, 0)) {
103 FAIL() << "failed to parse pipe max size";
104 }
105
106 if (fcntl(output_fd->get(), F_SETPIPE_SZ, pipe_buffer_size) != pipe_buffer_size) {
107 FAIL() << "failed to set pipe size: " << strerror(errno);
108 }
109
110 if (send_fd(intercept_fd->get(), &req, sizeof(req), std::move(output_pipe_write)) != sizeof(req)) {
111 FAIL() << "failed to send output fd to tombstoned: " << strerror(errno);
112 }
113
114 InterceptResponse response;
115 ssize_t rc = TEMP_FAILURE_RETRY(read(intercept_fd->get(), &response, sizeof(response)));
116 if (rc == -1) {
117 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
118 } else if (rc == 0) {
119 FAIL() << "failed to read response from tombstoned (EOF)";
120 } else if (rc != sizeof(response)) {
121 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
122 << ", received " << rc;
123 }
124
125 ASSERT_EQ(InterceptStatus::kRegistered, response.status);
126}
127
Josh Gaocbe70cb2016-10-18 18:17:52 -0700128class CrasherTest : public ::testing::Test {
129 public:
130 pid_t crasher_pid = -1;
131 bool previous_wait_for_gdb;
132 unique_fd crasher_pipe;
133 unique_fd intercept_fd;
134
135 CrasherTest();
136 ~CrasherTest();
137
138 void StartIntercept(unique_fd* output_fd);
139
140 // Returns -1 if we fail to read a response from tombstoned, otherwise the received return code.
141 void FinishIntercept(int* result);
142
Josh Gaofca7ca32017-01-23 12:05:35 -0800143 void StartProcess(std::function<void()> function);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700144 void StartCrasher(const std::string& crash_type);
145 void FinishCrasher();
146 void AssertDeath(int signo);
147};
148
149CrasherTest::CrasherTest() {
150 previous_wait_for_gdb = android::base::GetBoolProperty(kWaitForGdbKey, false);
151 android::base::SetProperty(kWaitForGdbKey, "0");
152}
153
154CrasherTest::~CrasherTest() {
155 if (crasher_pid != -1) {
156 kill(crasher_pid, SIGKILL);
157 int status;
158 waitpid(crasher_pid, &status, WUNTRACED);
159 }
160
161 android::base::SetProperty(kWaitForGdbKey, previous_wait_for_gdb ? "1" : "0");
162}
163
164void CrasherTest::StartIntercept(unique_fd* output_fd) {
165 if (crasher_pid == -1) {
166 FAIL() << "crasher hasn't been started";
167 }
168
Josh Gao460b3362017-03-30 16:40:47 -0700169 tombstoned_intercept(crasher_pid, &this->intercept_fd, output_fd);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700170}
171
172void CrasherTest::FinishIntercept(int* result) {
173 InterceptResponse response;
174
175 // Timeout for tombstoned intercept is 10 seconds.
176 ssize_t rc = TIMEOUT(20, read(intercept_fd.get(), &response, sizeof(response)));
177 if (rc == -1) {
178 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
179 } else if (rc == 0) {
180 *result = -1;
181 } else if (rc != sizeof(response)) {
182 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
183 << ", received " << rc;
184 } else {
Josh Gao460b3362017-03-30 16:40:47 -0700185 *result = response.status == InterceptStatus::kStarted ? 1 : 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700186 }
187}
188
Josh Gaofca7ca32017-01-23 12:05:35 -0800189void CrasherTest::StartProcess(std::function<void()> function) {
190 unique_fd read_pipe;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700191 unique_fd crasher_read_pipe;
192 if (!Pipe(&crasher_read_pipe, &crasher_pipe)) {
193 FAIL() << "failed to create pipe: " << strerror(errno);
194 }
195
196 crasher_pid = fork();
197 if (crasher_pid == -1) {
198 FAIL() << "fork failed: " << strerror(errno);
199 } else if (crasher_pid == 0) {
Josh Gao502cfd22017-02-17 01:39:15 -0800200 char dummy;
201 crasher_pipe.reset();
202 TEMP_FAILURE_RETRY(read(crasher_read_pipe.get(), &dummy, 1));
Josh Gaofca7ca32017-01-23 12:05:35 -0800203 function();
204 _exit(0);
205 }
206}
207
Josh Gaocbe70cb2016-10-18 18:17:52 -0700208void CrasherTest::FinishCrasher() {
209 if (crasher_pipe == -1) {
210 FAIL() << "crasher pipe uninitialized";
211 }
212
213 ssize_t rc = write(crasher_pipe.get(), "\n", 1);
214 if (rc == -1) {
215 FAIL() << "failed to write to crasher pipe: " << strerror(errno);
216 } else if (rc == 0) {
217 FAIL() << "crasher pipe was closed";
218 }
219}
220
221void CrasherTest::AssertDeath(int signo) {
222 int status;
223 pid_t pid = TIMEOUT(5, waitpid(crasher_pid, &status, 0));
224 if (pid != crasher_pid) {
225 FAIL() << "failed to wait for crasher: " << strerror(errno);
226 }
227
Josh Gao7a0ee642017-02-07 13:31:25 -0800228 if (WIFEXITED(status)) {
229 FAIL() << "crasher failed to exec: " << strerror(WEXITSTATUS(status));
230 } else if (!WIFSIGNALED(status)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700231 FAIL() << "crasher didn't terminate via a signal";
232 }
233 ASSERT_EQ(signo, WTERMSIG(status));
234 crasher_pid = -1;
235}
236
237static void ConsumeFd(unique_fd fd, std::string* output) {
238 constexpr size_t read_length = PAGE_SIZE;
239 std::string result;
240
241 while (true) {
242 size_t offset = result.size();
243 result.resize(result.size() + PAGE_SIZE);
244 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &result[offset], read_length));
245 if (rc == -1) {
246 FAIL() << "read failed: " << strerror(errno);
247 } else if (rc == 0) {
248 result.resize(result.size() - PAGE_SIZE);
249 break;
250 }
251
252 result.resize(result.size() - PAGE_SIZE + rc);
253 }
254
255 *output = std::move(result);
256}
257
258TEST_F(CrasherTest, smoke) {
259 int intercept_result;
260 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800261 StartProcess([]() {
262 *reinterpret_cast<volatile char*>(0xdead) = '1';
263 });
264
Josh Gaocbe70cb2016-10-18 18:17:52 -0700265 StartIntercept(&output_fd);
266 FinishCrasher();
267 AssertDeath(SIGSEGV);
268 FinishIntercept(&intercept_result);
269
270 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
271
272 std::string result;
273 ConsumeFd(std::move(output_fd), &result);
274 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
275}
276
277TEST_F(CrasherTest, abort) {
278 int intercept_result;
279 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800280 StartProcess([]() {
281 abort();
282 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700283 StartIntercept(&output_fd);
284 FinishCrasher();
285 AssertDeath(SIGABRT);
286 FinishIntercept(&intercept_result);
287
288 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
289
290 std::string result;
291 ConsumeFd(std::move(output_fd), &result);
292 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)");
293}
294
295TEST_F(CrasherTest, signal) {
296 int intercept_result;
297 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800298 StartProcess([]() {
299 abort();
300 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700301 StartIntercept(&output_fd);
302
303 // Wait for a bit, or we might end up killing the process before the signal
304 // handler even gets a chance to be registered.
305 std::this_thread::sleep_for(100ms);
306 ASSERT_EQ(0, kill(crasher_pid, SIGSEGV));
307
308 AssertDeath(SIGSEGV);
309 FinishIntercept(&intercept_result);
310
311 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
312
313 std::string result;
314 ConsumeFd(std::move(output_fd), &result);
315 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER\), fault addr --------)");
316 ASSERT_MATCH(result, R"(backtrace:)");
317}
318
319TEST_F(CrasherTest, abort_message) {
320 int intercept_result;
321 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800322 StartProcess([]() {
323 android_set_abort_message("abort message goes here");
324 abort();
325 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700326 StartIntercept(&output_fd);
327 FinishCrasher();
328 AssertDeath(SIGABRT);
329 FinishIntercept(&intercept_result);
330
331 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
332
333 std::string result;
334 ConsumeFd(std::move(output_fd), &result);
Josh Gao502cfd22017-02-17 01:39:15 -0800335 ASSERT_MATCH(result, R"(Abort message: 'abort message goes here')");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700336}
337
338TEST_F(CrasherTest, intercept_timeout) {
339 int intercept_result;
340 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800341 StartProcess([]() {
342 abort();
343 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700344 StartIntercept(&output_fd);
345
346 // Don't let crasher finish until we timeout.
347 FinishIntercept(&intercept_result);
348
349 ASSERT_NE(1, intercept_result) << "tombstoned reported success? (intercept_result = "
350 << intercept_result << ")";
351
352 FinishCrasher();
353 AssertDeath(SIGABRT);
354}
355
356TEST_F(CrasherTest, wait_for_gdb) {
357 if (!android::base::SetProperty(kWaitForGdbKey, "1")) {
358 FAIL() << "failed to enable wait_for_gdb";
359 }
360 sleep(1);
361
Josh Gao502cfd22017-02-17 01:39:15 -0800362 StartProcess([]() {
363 abort();
364 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700365 FinishCrasher();
366
367 int status;
368 ASSERT_EQ(crasher_pid, waitpid(crasher_pid, &status, WUNTRACED));
369 ASSERT_TRUE(WIFSTOPPED(status));
370 ASSERT_EQ(SIGSTOP, WSTOPSIG(status));
371
372 ASSERT_EQ(0, kill(crasher_pid, SIGCONT));
373
374 AssertDeath(SIGABRT);
375}
376
Josh Gao7c6e3132017-01-22 17:59:02 -0800377// wait_for_gdb shouldn't trigger on manually sent signals.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700378TEST_F(CrasherTest, wait_for_gdb_signal) {
379 if (!android::base::SetProperty(kWaitForGdbKey, "1")) {
380 FAIL() << "failed to enable wait_for_gdb";
381 }
382
Josh Gao502cfd22017-02-17 01:39:15 -0800383 StartProcess([]() {
384 abort();
385 });
Josh Gao7c6e3132017-01-22 17:59:02 -0800386 ASSERT_EQ(0, kill(crasher_pid, SIGSEGV)) << strerror(errno);
387 AssertDeath(SIGSEGV);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700388}
389
390TEST_F(CrasherTest, backtrace) {
391 std::string result;
392 int intercept_result;
393 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800394
395 StartProcess([]() {
396 abort();
397 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700398 StartIntercept(&output_fd);
399
400 std::this_thread::sleep_for(500ms);
401
402 sigval val;
403 val.sival_int = 1;
404 ASSERT_EQ(0, sigqueue(crasher_pid, DEBUGGER_SIGNAL, val)) << strerror(errno);
405 FinishIntercept(&intercept_result);
406 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
407 ConsumeFd(std::move(output_fd), &result);
408 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(read\+)");
409
410 int status;
411 ASSERT_EQ(0, waitpid(crasher_pid, &status, WNOHANG | WUNTRACED));
412
413 StartIntercept(&output_fd);
414 FinishCrasher();
415 AssertDeath(SIGABRT);
416 FinishIntercept(&intercept_result);
417 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
418 ConsumeFd(std::move(output_fd), &result);
419 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)");
420}
Josh Gaofca7ca32017-01-23 12:05:35 -0800421
422TEST_F(CrasherTest, PR_SET_DUMPABLE_0_crash) {
Josh Gao502cfd22017-02-17 01:39:15 -0800423 int intercept_result;
424 unique_fd output_fd;
Josh Gaofca7ca32017-01-23 12:05:35 -0800425 StartProcess([]() {
426 prctl(PR_SET_DUMPABLE, 0);
Josh Gao502cfd22017-02-17 01:39:15 -0800427 abort();
Josh Gaofca7ca32017-01-23 12:05:35 -0800428 });
Josh Gao502cfd22017-02-17 01:39:15 -0800429
430 StartIntercept(&output_fd);
431 FinishCrasher();
432 AssertDeath(SIGABRT);
433 FinishIntercept(&intercept_result);
434
435 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
436
437 std::string result;
438 ConsumeFd(std::move(output_fd), &result);
439 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)");
Josh Gaofca7ca32017-01-23 12:05:35 -0800440}
441
Josh Gao502cfd22017-02-17 01:39:15 -0800442TEST_F(CrasherTest, capabilities) {
443 ASSERT_EQ(0U, getuid()) << "capability test requires root";
444
Josh Gaofca7ca32017-01-23 12:05:35 -0800445 StartProcess([]() {
Josh Gao502cfd22017-02-17 01:39:15 -0800446 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
447 err(1, "failed to set PR_SET_KEEPCAPS");
448 }
449
450 if (setresuid(1, 1, 1) != 0) {
451 err(1, "setresuid failed");
452 }
453
454 __user_cap_header_struct capheader;
455 __user_cap_data_struct capdata[2];
456 memset(&capheader, 0, sizeof(capheader));
457 memset(&capdata, 0, sizeof(capdata));
458
459 capheader.version = _LINUX_CAPABILITY_VERSION_3;
460 capheader.pid = 0;
461
462 // Turn on every third capability.
463 static_assert(CAP_LAST_CAP > 33, "CAP_LAST_CAP <= 32");
464 for (int i = 0; i < CAP_LAST_CAP; i += 3) {
465 capdata[CAP_TO_INDEX(i)].permitted |= CAP_TO_MASK(i);
466 capdata[CAP_TO_INDEX(i)].effective |= CAP_TO_MASK(i);
467 }
468
469 // Make sure CAP_SYS_PTRACE is off.
470 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].permitted &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
471 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].effective &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
472
473 if (capset(&capheader, &capdata[0]) != 0) {
474 err(1, "capset failed");
475 }
476
477 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != 0) {
478 err(1, "failed to drop ambient capabilities");
479 }
480
481 raise(SIGSYS);
Josh Gaofca7ca32017-01-23 12:05:35 -0800482 });
Josh Gao502cfd22017-02-17 01:39:15 -0800483
484 unique_fd output_fd;
485 StartIntercept(&output_fd);
486 FinishCrasher();
487 AssertDeath(SIGSYS);
488
489 std::string result;
490 int intercept_result;
491 FinishIntercept(&intercept_result);
492 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
493 ConsumeFd(std::move(output_fd), &result);
494 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)");
Josh Gaofca7ca32017-01-23 12:05:35 -0800495}
Josh Gaoc3c8c022017-02-13 16:36:18 -0800496
497TEST(crash_dump, zombie) {
498 pid_t forkpid = fork();
499
500 int pipefd[2];
501 ASSERT_EQ(0, pipe2(pipefd, O_CLOEXEC));
502
503 pid_t rc;
504 int status;
505
506 if (forkpid == 0) {
507 errno = 0;
508 rc = waitpid(-1, &status, WNOHANG | __WALL | __WNOTHREAD);
509 if (rc != -1 || errno != ECHILD) {
510 errx(2, "first waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
511 }
512
513 raise(DEBUGGER_SIGNAL);
514
515 errno = 0;
516 rc = waitpid(-1, &status, __WALL | __WNOTHREAD);
517 if (rc != -1 || errno != ECHILD) {
518 errx(2, "second waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
519 }
520 _exit(0);
521 } else {
522 rc = waitpid(forkpid, &status, 0);
523 ASSERT_EQ(forkpid, rc);
524 ASSERT_TRUE(WIFEXITED(status));
525 ASSERT_EQ(0, WEXITSTATUS(status));
526 }
527}