• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <stdlib.h>
20 #include <sys/capability.h>
21 #include <sys/prctl.h>
22 #include <sys/ptrace.h>
23 #include <sys/resource.h>
24 #include <sys/syscall.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 
28 #include <chrono>
29 #include <regex>
30 #include <thread>
31 
32 #include <android/fdsan.h>
33 #include <android/set_abort_message.h>
34 #include <bionic/reserved_signals.h>
35 
36 #include <android-base/cmsg.h>
37 #include <android-base/file.h>
38 #include <android-base/logging.h>
39 #include <android-base/macros.h>
40 #include <android-base/parseint.h>
41 #include <android-base/properties.h>
42 #include <android-base/stringprintf.h>
43 #include <android-base/strings.h>
44 #include <android-base/test_utils.h>
45 #include <android-base/unique_fd.h>
46 #include <cutils/sockets.h>
47 #include <gtest/gtest.h>
48 
49 #include <libminijail.h>
50 #include <scoped_minijail.h>
51 
52 #include "debuggerd/handler.h"
53 #include "protocol.h"
54 #include "tombstoned/tombstoned.h"
55 #include "util.h"
56 
57 using namespace std::chrono_literals;
58 
59 using android::base::SendFileDescriptors;
60 using android::base::unique_fd;
61 
62 #if defined(__LP64__)
63 #define ARCH_SUFFIX "64"
64 #else
65 #define ARCH_SUFFIX ""
66 #endif
67 
68 constexpr char kWaitForGdbKey[] = "debug.debuggerd.wait_for_gdb";
69 
70 #define TIMEOUT(seconds, expr)                                     \
71   [&]() {                                                          \
72     struct sigaction old_sigaction;                                \
73     struct sigaction new_sigaction = {};                           \
74     new_sigaction.sa_handler = [](int) {};                         \
75     if (sigaction(SIGALRM, &new_sigaction, &new_sigaction) != 0) { \
76       err(1, "sigaction failed");                                  \
77     }                                                              \
78     alarm(seconds);                                                \
79     auto value = expr;                                             \
80     int saved_errno = errno;                                       \
81     if (sigaction(SIGALRM, &old_sigaction, nullptr) != 0) {        \
82       err(1, "sigaction failed");                                  \
83     }                                                              \
84     alarm(0);                                                      \
85     errno = saved_errno;                                           \
86     return value;                                                  \
87   }()
88 
89 // Backtrace frame dump could contain:
90 //   #01 pc 0001cded  /data/tmp/debuggerd_test32 (raise_debugger_signal+80)
91 // or
92 //   #01 pc 00022a09  /data/tmp/debuggerd_test32 (offset 0x12000) (raise_debugger_signal+80)
93 #define ASSERT_BACKTRACE_FRAME(result, frame_name) \
94   ASSERT_MATCH(result,                             \
95                R"(#\d\d pc [0-9a-f]+\s+ \S+ (\(offset 0x[0-9a-f]+\) )?\()" frame_name R"(\+)");
96 
tombstoned_intercept(pid_t target_pid,unique_fd * intercept_fd,unique_fd * output_fd,InterceptStatus * status,DebuggerdDumpType intercept_type)97 static void tombstoned_intercept(pid_t target_pid, unique_fd* intercept_fd, unique_fd* output_fd,
98                                  InterceptStatus* status, DebuggerdDumpType intercept_type) {
99   intercept_fd->reset(socket_local_client(kTombstonedInterceptSocketName,
100                                           ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
101   if (intercept_fd->get() == -1) {
102     FAIL() << "failed to contact tombstoned: " << strerror(errno);
103   }
104 
105   InterceptRequest req = {
106       .dump_type = intercept_type,
107       .pid = target_pid,
108   };
109 
110   unique_fd output_pipe_write;
111   if (!Pipe(output_fd, &output_pipe_write)) {
112     FAIL() << "failed to create output pipe: " << strerror(errno);
113   }
114 
115   std::string pipe_size_str;
116   int pipe_buffer_size;
117   if (!android::base::ReadFileToString("/proc/sys/fs/pipe-max-size", &pipe_size_str)) {
118     FAIL() << "failed to read /proc/sys/fs/pipe-max-size: " << strerror(errno);
119   }
120 
121   pipe_size_str = android::base::Trim(pipe_size_str);
122 
123   if (!android::base::ParseInt(pipe_size_str.c_str(), &pipe_buffer_size, 0)) {
124     FAIL() << "failed to parse pipe max size";
125   }
126 
127   if (fcntl(output_fd->get(), F_SETPIPE_SZ, pipe_buffer_size) != pipe_buffer_size) {
128     FAIL() << "failed to set pipe size: " << strerror(errno);
129   }
130 
131   ASSERT_GE(pipe_buffer_size, 1024 * 1024);
132 
133   ssize_t rc = SendFileDescriptors(intercept_fd->get(), &req, sizeof(req), output_pipe_write.get());
134   output_pipe_write.reset();
135   if (rc != sizeof(req)) {
136     FAIL() << "failed to send output fd to tombstoned: " << strerror(errno);
137   }
138 
139   InterceptResponse response;
140   rc = TEMP_FAILURE_RETRY(read(intercept_fd->get(), &response, sizeof(response)));
141   if (rc == -1) {
142     FAIL() << "failed to read response from tombstoned: " << strerror(errno);
143   } else if (rc == 0) {
144     FAIL() << "failed to read response from tombstoned (EOF)";
145   } else if (rc != sizeof(response)) {
146     FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
147            << ", received " << rc;
148   }
149 
150   *status = response.status;
151 }
152 
153 class CrasherTest : public ::testing::Test {
154  public:
155   pid_t crasher_pid = -1;
156   bool previous_wait_for_gdb;
157   unique_fd crasher_pipe;
158   unique_fd intercept_fd;
159 
160   CrasherTest();
161   ~CrasherTest();
162 
163   void StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type = kDebuggerdTombstone);
164 
165   // Returns -1 if we fail to read a response from tombstoned, otherwise the received return code.
166   void FinishIntercept(int* result);
167 
168   void StartProcess(std::function<void()> function, std::function<pid_t()> forker = fork);
169   void StartCrasher(const std::string& crash_type);
170   void FinishCrasher();
171   void AssertDeath(int signo);
172 };
173 
CrasherTest()174 CrasherTest::CrasherTest() {
175   previous_wait_for_gdb = android::base::GetBoolProperty(kWaitForGdbKey, false);
176   android::base::SetProperty(kWaitForGdbKey, "0");
177 }
178 
~CrasherTest()179 CrasherTest::~CrasherTest() {
180   if (crasher_pid != -1) {
181     kill(crasher_pid, SIGKILL);
182     int status;
183     TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, WUNTRACED));
184   }
185 
186   android::base::SetProperty(kWaitForGdbKey, previous_wait_for_gdb ? "1" : "0");
187 }
188 
StartIntercept(unique_fd * output_fd,DebuggerdDumpType intercept_type)189 void CrasherTest::StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type) {
190   if (crasher_pid == -1) {
191     FAIL() << "crasher hasn't been started";
192   }
193 
194   InterceptStatus status;
195   tombstoned_intercept(crasher_pid, &this->intercept_fd, output_fd, &status, intercept_type);
196   ASSERT_EQ(InterceptStatus::kRegistered, status);
197 }
198 
FinishIntercept(int * result)199 void CrasherTest::FinishIntercept(int* result) {
200   InterceptResponse response;
201 
202   ssize_t rc = TIMEOUT(30, read(intercept_fd.get(), &response, sizeof(response)));
203   if (rc == -1) {
204     FAIL() << "failed to read response from tombstoned: " << strerror(errno);
205   } else if (rc == 0) {
206     *result = -1;
207   } else if (rc != sizeof(response)) {
208     FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
209            << ", received " << rc;
210   } else {
211     *result = response.status == InterceptStatus::kStarted ? 1 : 0;
212   }
213 }
214 
StartProcess(std::function<void ()> function,std::function<pid_t ()> forker)215 void CrasherTest::StartProcess(std::function<void()> function, std::function<pid_t()> forker) {
216   unique_fd read_pipe;
217   unique_fd crasher_read_pipe;
218   if (!Pipe(&crasher_read_pipe, &crasher_pipe)) {
219     FAIL() << "failed to create pipe: " << strerror(errno);
220   }
221 
222   crasher_pid = forker();
223   if (crasher_pid == -1) {
224     FAIL() << "fork failed: " << strerror(errno);
225   } else if (crasher_pid == 0) {
226     char dummy;
227     crasher_pipe.reset();
228     TEMP_FAILURE_RETRY(read(crasher_read_pipe.get(), &dummy, 1));
229     function();
230     _exit(0);
231   }
232 }
233 
FinishCrasher()234 void CrasherTest::FinishCrasher() {
235   if (crasher_pipe == -1) {
236     FAIL() << "crasher pipe uninitialized";
237   }
238 
239   ssize_t rc = TEMP_FAILURE_RETRY(write(crasher_pipe.get(), "\n", 1));
240   if (rc == -1) {
241     FAIL() << "failed to write to crasher pipe: " << strerror(errno);
242   } else if (rc == 0) {
243     FAIL() << "crasher pipe was closed";
244   }
245 }
246 
AssertDeath(int signo)247 void CrasherTest::AssertDeath(int signo) {
248   int status;
249   pid_t pid = TIMEOUT(30, waitpid(crasher_pid, &status, 0));
250   if (pid != crasher_pid) {
251     printf("failed to wait for crasher (expected pid %d, return value %d): %s\n", crasher_pid, pid,
252            strerror(errno));
253     sleep(100);
254     FAIL() << "failed to wait for crasher: " << strerror(errno);
255   }
256 
257   if (signo == 0) {
258     ASSERT_TRUE(WIFEXITED(status));
259     ASSERT_EQ(0, WEXITSTATUS(signo));
260   } else {
261     ASSERT_FALSE(WIFEXITED(status));
262     ASSERT_TRUE(WIFSIGNALED(status)) << "crasher didn't terminate via a signal";
263     ASSERT_EQ(signo, WTERMSIG(status));
264   }
265   crasher_pid = -1;
266 }
267 
ConsumeFd(unique_fd fd,std::string * output)268 static void ConsumeFd(unique_fd fd, std::string* output) {
269   constexpr size_t read_length = PAGE_SIZE;
270   std::string result;
271 
272   while (true) {
273     size_t offset = result.size();
274     result.resize(result.size() + PAGE_SIZE);
275     ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &result[offset], read_length));
276     if (rc == -1) {
277       FAIL() << "read failed: " << strerror(errno);
278     } else if (rc == 0) {
279       result.resize(result.size() - PAGE_SIZE);
280       break;
281     }
282 
283     result.resize(result.size() - PAGE_SIZE + rc);
284   }
285 
286   *output = std::move(result);
287 }
288 
TEST_F(CrasherTest,smoke)289 TEST_F(CrasherTest, smoke) {
290   int intercept_result;
291   unique_fd output_fd;
292   StartProcess([]() {
293     *reinterpret_cast<volatile char*>(0xdead) = '1';
294   });
295 
296   StartIntercept(&output_fd);
297   FinishCrasher();
298   AssertDeath(SIGSEGV);
299   FinishIntercept(&intercept_result);
300 
301   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
302 
303   std::string result;
304   ConsumeFd(std::move(output_fd), &result);
305   ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
306 }
307 
TEST_F(CrasherTest,LD_PRELOAD)308 TEST_F(CrasherTest, LD_PRELOAD) {
309   int intercept_result;
310   unique_fd output_fd;
311   StartProcess([]() {
312     setenv("LD_PRELOAD", "nonexistent.so", 1);
313     *reinterpret_cast<volatile char*>(0xdead) = '1';
314   });
315 
316   StartIntercept(&output_fd);
317   FinishCrasher();
318   AssertDeath(SIGSEGV);
319   FinishIntercept(&intercept_result);
320 
321   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
322 
323   std::string result;
324   ConsumeFd(std::move(output_fd), &result);
325   ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
326 }
327 
TEST_F(CrasherTest,abort)328 TEST_F(CrasherTest, abort) {
329   int intercept_result;
330   unique_fd output_fd;
331   StartProcess([]() {
332     abort();
333   });
334   StartIntercept(&output_fd);
335   FinishCrasher();
336   AssertDeath(SIGABRT);
337   FinishIntercept(&intercept_result);
338 
339   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
340 
341   std::string result;
342   ConsumeFd(std::move(output_fd), &result);
343   ASSERT_BACKTRACE_FRAME(result, "abort");
344 }
345 
TEST_F(CrasherTest,signal)346 TEST_F(CrasherTest, signal) {
347   int intercept_result;
348   unique_fd output_fd;
349   StartProcess([]() {
350     while (true) {
351       sleep(1);
352     }
353   });
354   StartIntercept(&output_fd);
355   FinishCrasher();
356   ASSERT_EQ(0, kill(crasher_pid, SIGSEGV));
357 
358   AssertDeath(SIGSEGV);
359   FinishIntercept(&intercept_result);
360 
361   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
362 
363   std::string result;
364   ConsumeFd(std::move(output_fd), &result);
365   ASSERT_MATCH(
366       result,
367       R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER from pid \d+, uid \d+\), fault addr --------)");
368   ASSERT_MATCH(result, R"(backtrace:)");
369 }
370 
TEST_F(CrasherTest,abort_message)371 TEST_F(CrasherTest, abort_message) {
372   int intercept_result;
373   unique_fd output_fd;
374   StartProcess([]() {
375     // Arrived at experimentally;
376     // logd truncates at 4062.
377     // strlen("Abort message: ''") is 17.
378     // That's 4045, but we also want a NUL.
379     char buf[4045 + 1];
380     memset(buf, 'x', sizeof(buf));
381     buf[sizeof(buf) - 1] = '\0';
382     android_set_abort_message(buf);
383     abort();
384   });
385   StartIntercept(&output_fd);
386   FinishCrasher();
387   AssertDeath(SIGABRT);
388   FinishIntercept(&intercept_result);
389 
390   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
391 
392   std::string result;
393   ConsumeFd(std::move(output_fd), &result);
394   ASSERT_MATCH(result, R"(Abort message: 'x{4045}')");
395 }
396 
TEST_F(CrasherTest,abort_message_backtrace)397 TEST_F(CrasherTest, abort_message_backtrace) {
398   int intercept_result;
399   unique_fd output_fd;
400   StartProcess([]() {
401     android_set_abort_message("not actually aborting");
402     raise(BIONIC_SIGNAL_DEBUGGER);
403     exit(0);
404   });
405   StartIntercept(&output_fd);
406   FinishCrasher();
407   AssertDeath(0);
408   FinishIntercept(&intercept_result);
409 
410   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
411 
412   std::string result;
413   ConsumeFd(std::move(output_fd), &result);
414   ASSERT_NOT_MATCH(result, R"(Abort message:)");
415 }
416 
TEST_F(CrasherTest,intercept_timeout)417 TEST_F(CrasherTest, intercept_timeout) {
418   int intercept_result;
419   unique_fd output_fd;
420   StartProcess([]() {
421     abort();
422   });
423   StartIntercept(&output_fd);
424 
425   // Don't let crasher finish until we timeout.
426   FinishIntercept(&intercept_result);
427 
428   ASSERT_NE(1, intercept_result) << "tombstoned reported success? (intercept_result = "
429                                  << intercept_result << ")";
430 
431   FinishCrasher();
432   AssertDeath(SIGABRT);
433 }
434 
TEST_F(CrasherTest,wait_for_gdb)435 TEST_F(CrasherTest, wait_for_gdb) {
436   if (!android::base::SetProperty(kWaitForGdbKey, "1")) {
437     FAIL() << "failed to enable wait_for_gdb";
438   }
439   sleep(1);
440 
441   StartProcess([]() {
442     abort();
443   });
444   FinishCrasher();
445 
446   int status;
447   ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, WUNTRACED)));
448   ASSERT_TRUE(WIFSTOPPED(status));
449   ASSERT_EQ(SIGSTOP, WSTOPSIG(status));
450 
451   ASSERT_EQ(0, kill(crasher_pid, SIGCONT));
452 
453   AssertDeath(SIGABRT);
454 }
455 
TEST_F(CrasherTest,backtrace)456 TEST_F(CrasherTest, backtrace) {
457   std::string result;
458   int intercept_result;
459   unique_fd output_fd;
460 
461   StartProcess([]() {
462     abort();
463   });
464   StartIntercept(&output_fd, kDebuggerdNativeBacktrace);
465 
466   std::this_thread::sleep_for(500ms);
467 
468   sigval val;
469   val.sival_int = 1;
470   ASSERT_EQ(0, sigqueue(crasher_pid, BIONIC_SIGNAL_DEBUGGER, val)) << strerror(errno);
471   FinishIntercept(&intercept_result);
472   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
473   ConsumeFd(std::move(output_fd), &result);
474   ASSERT_BACKTRACE_FRAME(result, "read");
475 
476   int status;
477   ASSERT_EQ(0, waitpid(crasher_pid, &status, WNOHANG | WUNTRACED));
478 
479   StartIntercept(&output_fd);
480   FinishCrasher();
481   AssertDeath(SIGABRT);
482   FinishIntercept(&intercept_result);
483   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
484   ConsumeFd(std::move(output_fd), &result);
485   ASSERT_BACKTRACE_FRAME(result, "abort");
486 }
487 
TEST_F(CrasherTest,PR_SET_DUMPABLE_0_crash)488 TEST_F(CrasherTest, PR_SET_DUMPABLE_0_crash) {
489   int intercept_result;
490   unique_fd output_fd;
491   StartProcess([]() {
492     prctl(PR_SET_DUMPABLE, 0);
493     abort();
494   });
495 
496   StartIntercept(&output_fd);
497   FinishCrasher();
498   AssertDeath(SIGABRT);
499   FinishIntercept(&intercept_result);
500 
501   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
502 
503   std::string result;
504   ConsumeFd(std::move(output_fd), &result);
505   ASSERT_BACKTRACE_FRAME(result, "abort");
506 }
507 
TEST_F(CrasherTest,capabilities)508 TEST_F(CrasherTest, capabilities) {
509   ASSERT_EQ(0U, getuid()) << "capability test requires root";
510 
511   StartProcess([]() {
512     if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
513       err(1, "failed to set PR_SET_KEEPCAPS");
514     }
515 
516     if (setresuid(1, 1, 1) != 0) {
517       err(1, "setresuid failed");
518     }
519 
520     __user_cap_header_struct capheader;
521     __user_cap_data_struct capdata[2];
522     memset(&capheader, 0, sizeof(capheader));
523     memset(&capdata, 0, sizeof(capdata));
524 
525     capheader.version = _LINUX_CAPABILITY_VERSION_3;
526     capheader.pid = 0;
527 
528     // Turn on every third capability.
529     static_assert(CAP_LAST_CAP > 33, "CAP_LAST_CAP <= 32");
530     for (int i = 0; i < CAP_LAST_CAP; i += 3) {
531       capdata[CAP_TO_INDEX(i)].permitted |= CAP_TO_MASK(i);
532       capdata[CAP_TO_INDEX(i)].effective |= CAP_TO_MASK(i);
533     }
534 
535     // Make sure CAP_SYS_PTRACE is off.
536     capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].permitted &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
537     capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].effective &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
538 
539     if (capset(&capheader, &capdata[0]) != 0) {
540       err(1, "capset failed");
541     }
542 
543     if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != 0) {
544       err(1, "failed to drop ambient capabilities");
545     }
546 
547     pthread_setname_np(pthread_self(), "thread_name");
548     raise(SIGSYS);
549   });
550 
551   unique_fd output_fd;
552   StartIntercept(&output_fd);
553   FinishCrasher();
554   AssertDeath(SIGSYS);
555 
556   std::string result;
557   int intercept_result;
558   FinishIntercept(&intercept_result);
559   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
560   ConsumeFd(std::move(output_fd), &result);
561   ASSERT_MATCH(result, R"(name: thread_name\s+>>> .+debuggerd_test(32|64) <<<)");
562   ASSERT_BACKTRACE_FRAME(result, "tgkill");
563 }
564 
TEST_F(CrasherTest,fake_pid)565 TEST_F(CrasherTest, fake_pid) {
566   int intercept_result;
567   unique_fd output_fd;
568 
569   // Prime the getpid/gettid caches.
570   UNUSED(getpid());
571   UNUSED(gettid());
572 
573   std::function<pid_t()> clone_fn = []() {
574     return syscall(__NR_clone, SIGCHLD, nullptr, nullptr, nullptr, nullptr);
575   };
576   StartProcess(
577       []() {
578         ASSERT_NE(getpid(), syscall(__NR_getpid));
579         ASSERT_NE(gettid(), syscall(__NR_gettid));
580         raise(SIGSEGV);
581       },
582       clone_fn);
583 
584   StartIntercept(&output_fd);
585   FinishCrasher();
586   AssertDeath(SIGSEGV);
587   FinishIntercept(&intercept_result);
588 
589   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
590 
591   std::string result;
592   ConsumeFd(std::move(output_fd), &result);
593   ASSERT_BACKTRACE_FRAME(result, "tgkill");
594 }
595 
596 static const char* const kDebuggerdSeccompPolicy =
597     "/system/etc/seccomp_policy/crash_dump." ABI_STRING ".policy";
598 
seccomp_fork_impl(void (* prejail)())599 static pid_t seccomp_fork_impl(void (*prejail)()) {
600   std::string policy;
601   if (!android::base::ReadFileToString(kDebuggerdSeccompPolicy, &policy)) {
602     PLOG(FATAL) << "failed to read policy file";
603   }
604 
605   // Allow a bunch of syscalls used by the tests.
606   policy += "\nclone: 1";
607   policy += "\nsigaltstack: 1";
608   policy += "\nnanosleep: 1";
609   policy += "\ngetrlimit: 1";
610   policy += "\nugetrlimit: 1";
611 
612   FILE* tmp_file = tmpfile();
613   if (!tmp_file) {
614     PLOG(FATAL) << "tmpfile failed";
615   }
616 
617   unique_fd tmp_fd(TEMP_FAILURE_RETRY(dup(fileno(tmp_file))));
618   if (!android::base::WriteStringToFd(policy, tmp_fd.get())) {
619     PLOG(FATAL) << "failed to write policy to tmpfile";
620   }
621 
622   if (lseek(tmp_fd.get(), 0, SEEK_SET) != 0) {
623     PLOG(FATAL) << "failed to seek tmp_fd";
624   }
625 
626   ScopedMinijail jail{minijail_new()};
627   if (!jail) {
628     LOG(FATAL) << "failed to create minijail";
629   }
630 
631   minijail_no_new_privs(jail.get());
632   minijail_log_seccomp_filter_failures(jail.get());
633   minijail_use_seccomp_filter(jail.get());
634   minijail_parse_seccomp_filters_from_fd(jail.get(), tmp_fd.release());
635 
636   pid_t result = fork();
637   if (result == -1) {
638     return result;
639   } else if (result != 0) {
640     return result;
641   }
642 
643   // Spawn and detach a thread that spins forever.
644   std::atomic<bool> thread_ready(false);
645   std::thread thread([&jail, &thread_ready]() {
646     minijail_enter(jail.get());
647     thread_ready = true;
648     for (;;)
649       ;
650   });
651   thread.detach();
652 
653   while (!thread_ready) {
654     continue;
655   }
656 
657   if (prejail) {
658     prejail();
659   }
660 
661   minijail_enter(jail.get());
662   return result;
663 }
664 
seccomp_fork()665 static pid_t seccomp_fork() {
666   return seccomp_fork_impl(nullptr);
667 }
668 
TEST_F(CrasherTest,seccomp_crash)669 TEST_F(CrasherTest, seccomp_crash) {
670   int intercept_result;
671   unique_fd output_fd;
672 
673   StartProcess([]() { abort(); }, &seccomp_fork);
674 
675   StartIntercept(&output_fd);
676   FinishCrasher();
677   AssertDeath(SIGABRT);
678   FinishIntercept(&intercept_result);
679   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
680 
681   std::string result;
682   ConsumeFd(std::move(output_fd), &result);
683   ASSERT_BACKTRACE_FRAME(result, "abort");
684 }
685 
seccomp_fork_rlimit()686 static pid_t seccomp_fork_rlimit() {
687   return seccomp_fork_impl([]() {
688     struct rlimit rlim = {
689         .rlim_cur = 512 * 1024 * 1024,
690         .rlim_max = 512 * 1024 * 1024,
691     };
692 
693     if (setrlimit(RLIMIT_AS, &rlim) != 0) {
694       raise(SIGINT);
695     }
696   });
697 }
698 
TEST_F(CrasherTest,seccomp_crash_oom)699 TEST_F(CrasherTest, seccomp_crash_oom) {
700   int intercept_result;
701   unique_fd output_fd;
702 
703   StartProcess(
704       []() {
705         std::vector<void*> vec;
706         for (int i = 0; i < 512; ++i) {
707           char* buf = static_cast<char*>(malloc(1024 * 1024));
708           if (!buf) {
709             abort();
710           }
711           memset(buf, 0xff, 1024 * 1024);
712           vec.push_back(buf);
713         }
714       },
715       &seccomp_fork_rlimit);
716 
717   StartIntercept(&output_fd);
718   FinishCrasher();
719   AssertDeath(SIGABRT);
720   FinishIntercept(&intercept_result);
721   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
722 
723   // We can't actually generate a backtrace, just make sure that the process terminates.
724 }
725 
raise_debugger_signal(DebuggerdDumpType dump_type)726 __attribute__((noinline)) extern "C" bool raise_debugger_signal(DebuggerdDumpType dump_type) {
727   siginfo_t siginfo;
728   siginfo.si_code = SI_QUEUE;
729   siginfo.si_pid = getpid();
730   siginfo.si_uid = getuid();
731 
732   if (dump_type != kDebuggerdNativeBacktrace && dump_type != kDebuggerdTombstone) {
733     PLOG(FATAL) << "invalid dump type";
734   }
735 
736   siginfo.si_value.sival_int = dump_type == kDebuggerdNativeBacktrace;
737 
738   if (syscall(__NR_rt_tgsigqueueinfo, getpid(), gettid(), BIONIC_SIGNAL_DEBUGGER, &siginfo) != 0) {
739     PLOG(ERROR) << "libdebuggerd_client: failed to send signal to self";
740     return false;
741   }
742 
743   return true;
744 }
745 
TEST_F(CrasherTest,seccomp_tombstone)746 TEST_F(CrasherTest, seccomp_tombstone) {
747   int intercept_result;
748   unique_fd output_fd;
749 
750   static const auto dump_type = kDebuggerdTombstone;
751   StartProcess(
752       []() {
753         raise_debugger_signal(dump_type);
754         _exit(0);
755       },
756       &seccomp_fork);
757 
758   StartIntercept(&output_fd, dump_type);
759   FinishCrasher();
760   AssertDeath(0);
761   FinishIntercept(&intercept_result);
762   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
763 
764   std::string result;
765   ConsumeFd(std::move(output_fd), &result);
766   ASSERT_BACKTRACE_FRAME(result, "raise_debugger_signal");
767 }
768 
foo()769 extern "C" void foo() {
770   LOG(INFO) << "foo";
771   std::this_thread::sleep_for(1s);
772 }
773 
bar()774 extern "C" void bar() {
775   LOG(INFO) << "bar";
776   std::this_thread::sleep_for(1s);
777 }
778 
TEST_F(CrasherTest,seccomp_backtrace)779 TEST_F(CrasherTest, seccomp_backtrace) {
780   int intercept_result;
781   unique_fd output_fd;
782 
783   static const auto dump_type = kDebuggerdNativeBacktrace;
784   StartProcess(
785       []() {
786         std::thread a(foo);
787         std::thread b(bar);
788 
789         std::this_thread::sleep_for(100ms);
790 
791         raise_debugger_signal(dump_type);
792         _exit(0);
793       },
794       &seccomp_fork);
795 
796   StartIntercept(&output_fd, dump_type);
797   FinishCrasher();
798   AssertDeath(0);
799   FinishIntercept(&intercept_result);
800   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
801 
802   std::string result;
803   ConsumeFd(std::move(output_fd), &result);
804   ASSERT_BACKTRACE_FRAME(result, "raise_debugger_signal");
805   ASSERT_BACKTRACE_FRAME(result, "foo");
806   ASSERT_BACKTRACE_FRAME(result, "bar");
807 }
808 
TEST_F(CrasherTest,seccomp_crash_logcat)809 TEST_F(CrasherTest, seccomp_crash_logcat) {
810   StartProcess([]() { abort(); }, &seccomp_fork);
811   FinishCrasher();
812 
813   // Make sure we don't get SIGSYS when trying to dump a crash to logcat.
814   AssertDeath(SIGABRT);
815 }
816 
TEST_F(CrasherTest,competing_tracer)817 TEST_F(CrasherTest, competing_tracer) {
818   int intercept_result;
819   unique_fd output_fd;
820   StartProcess([]() {
821     raise(SIGABRT);
822   });
823 
824   StartIntercept(&output_fd);
825 
826   ASSERT_EQ(0, ptrace(PTRACE_SEIZE, crasher_pid, 0, 0));
827   FinishCrasher();
828 
829   int status;
830   ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, 0)));
831   ASSERT_TRUE(WIFSTOPPED(status));
832   ASSERT_EQ(SIGABRT, WSTOPSIG(status));
833 
834   ASSERT_EQ(0, ptrace(PTRACE_CONT, crasher_pid, 0, SIGABRT));
835   FinishIntercept(&intercept_result);
836   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
837 
838   std::string result;
839   ConsumeFd(std::move(output_fd), &result);
840   std::string regex = R"(failed to attach to thread \d+, already traced by )";
841   regex += std::to_string(gettid());
842   regex += R"( \(.+debuggerd_test)";
843   ASSERT_MATCH(result, regex.c_str());
844 
845   ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, 0)));
846   ASSERT_TRUE(WIFSTOPPED(status));
847   ASSERT_EQ(SIGABRT, WSTOPSIG(status));
848 
849   ASSERT_EQ(0, ptrace(PTRACE_DETACH, crasher_pid, 0, SIGABRT));
850   AssertDeath(SIGABRT);
851 }
852 
TEST_F(CrasherTest,fdsan_warning_abort_message)853 TEST_F(CrasherTest, fdsan_warning_abort_message) {
854   int intercept_result;
855   unique_fd output_fd;
856 
857   StartProcess([]() {
858     android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
859     unique_fd fd(TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY | O_CLOEXEC)));
860     if (fd == -1) {
861       abort();
862     }
863     close(fd.get());
864     _exit(0);
865   });
866 
867   StartIntercept(&output_fd);
868   FinishCrasher();
869   AssertDeath(0);
870   FinishIntercept(&intercept_result);
871   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
872 
873   std::string result;
874   ConsumeFd(std::move(output_fd), &result);
875   ASSERT_MATCH(result, "Abort message: 'attempted to close");
876 }
877 
TEST(crash_dump,zombie)878 TEST(crash_dump, zombie) {
879   pid_t forkpid = fork();
880 
881   pid_t rc;
882   int status;
883 
884   if (forkpid == 0) {
885     errno = 0;
886     rc = waitpid(-1, &status, WNOHANG | __WALL | __WNOTHREAD);
887     if (rc != -1 || errno != ECHILD) {
888       errx(2, "first waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
889     }
890 
891     raise(BIONIC_SIGNAL_DEBUGGER);
892 
893     errno = 0;
894     rc = TEMP_FAILURE_RETRY(waitpid(-1, &status, __WALL | __WNOTHREAD));
895     if (rc != -1 || errno != ECHILD) {
896       errx(2, "second waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
897     }
898     _exit(0);
899   } else {
900     rc = TEMP_FAILURE_RETRY(waitpid(forkpid, &status, 0));
901     ASSERT_EQ(forkpid, rc);
902     ASSERT_TRUE(WIFEXITED(status));
903     ASSERT_EQ(0, WEXITSTATUS(status));
904   }
905 }
906 
TEST(tombstoned,no_notify)907 TEST(tombstoned, no_notify) {
908   // Do this a few times.
909   for (int i = 0; i < 3; ++i) {
910     pid_t pid = 123'456'789 + i;
911 
912     unique_fd intercept_fd, output_fd;
913     InterceptStatus status;
914     tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
915     ASSERT_EQ(InterceptStatus::kRegistered, status);
916 
917     {
918       unique_fd tombstoned_socket, input_fd;
919       ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
920       ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
921     }
922 
923     pid_t read_pid;
924     ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
925     ASSERT_EQ(read_pid, pid);
926   }
927 }
928 
TEST(tombstoned,stress)929 TEST(tombstoned, stress) {
930   // Spawn threads to simultaneously do a bunch of failing dumps and a bunch of successful dumps.
931   static constexpr int kDumpCount = 100;
932 
933   std::atomic<bool> start(false);
934   std::vector<std::thread> threads;
935   threads.emplace_back([&start]() {
936     while (!start) {
937       continue;
938     }
939 
940     // Use a way out of range pid, to avoid stomping on an actual process.
941     pid_t pid_base = 1'000'000;
942 
943     for (int dump = 0; dump < kDumpCount; ++dump) {
944       pid_t pid = pid_base + dump;
945 
946       unique_fd intercept_fd, output_fd;
947       InterceptStatus status;
948       tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
949       ASSERT_EQ(InterceptStatus::kRegistered, status);
950 
951       // Pretend to crash, and then immediately close the socket.
952       unique_fd sockfd(socket_local_client(kTombstonedCrashSocketName,
953                                            ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
954       if (sockfd == -1) {
955         FAIL() << "failed to connect to tombstoned: " << strerror(errno);
956       }
957       TombstonedCrashPacket packet = {};
958       packet.packet_type = CrashPacketType::kDumpRequest;
959       packet.packet.dump_request.pid = pid;
960       if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) {
961         FAIL() << "failed to write to tombstoned: " << strerror(errno);
962       }
963 
964       continue;
965     }
966   });
967 
968   threads.emplace_back([&start]() {
969     while (!start) {
970       continue;
971     }
972 
973     // Use a way out of range pid, to avoid stomping on an actual process.
974     pid_t pid_base = 2'000'000;
975 
976     for (int dump = 0; dump < kDumpCount; ++dump) {
977       pid_t pid = pid_base + dump;
978 
979       unique_fd intercept_fd, output_fd;
980       InterceptStatus status;
981       tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
982       ASSERT_EQ(InterceptStatus::kRegistered, status);
983 
984       {
985         unique_fd tombstoned_socket, input_fd;
986         ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
987         ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
988         tombstoned_notify_completion(tombstoned_socket.get());
989       }
990 
991       // TODO: Fix the race that requires this sleep.
992       std::this_thread::sleep_for(50ms);
993 
994       pid_t read_pid;
995       ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
996       ASSERT_EQ(read_pid, pid);
997     }
998   });
999 
1000   start = true;
1001 
1002   for (std::thread& thread : threads) {
1003     thread.join();
1004   }
1005 }
1006 
TEST(tombstoned,java_trace_intercept_smoke)1007 TEST(tombstoned, java_trace_intercept_smoke) {
1008   // Using a "real" PID is a little dangerous here - if the test fails
1009   // or crashes, we might end up getting a bogus / unreliable stack
1010   // trace.
1011   const pid_t self = getpid();
1012 
1013   unique_fd intercept_fd, output_fd;
1014   InterceptStatus status;
1015   tombstoned_intercept(self, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
1016   ASSERT_EQ(InterceptStatus::kRegistered, status);
1017 
1018   // First connect to tombstoned requesting a native backtrace. This
1019   // should result in a "regular" FD and not the installed intercept.
1020   const char native[] = "native";
1021   unique_fd tombstoned_socket, input_fd;
1022   ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdNativeBacktrace));
1023   ASSERT_TRUE(android::base::WriteFully(input_fd.get(), native, sizeof(native)));
1024   tombstoned_notify_completion(tombstoned_socket.get());
1025 
1026   // Then, connect to tombstoned asking for a java backtrace. This *should*
1027   // trigger the intercept.
1028   const char java[] = "java";
1029   ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdJavaBacktrace));
1030   ASSERT_TRUE(android::base::WriteFully(input_fd.get(), java, sizeof(java)));
1031   tombstoned_notify_completion(tombstoned_socket.get());
1032 
1033   char outbuf[sizeof(java)];
1034   ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
1035   ASSERT_STREQ("java", outbuf);
1036 }
1037 
TEST(tombstoned,multiple_intercepts)1038 TEST(tombstoned, multiple_intercepts) {
1039   const pid_t fake_pid = 1'234'567;
1040   unique_fd intercept_fd, output_fd;
1041   InterceptStatus status;
1042   tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
1043   ASSERT_EQ(InterceptStatus::kRegistered, status);
1044 
1045   unique_fd intercept_fd_2, output_fd_2;
1046   tombstoned_intercept(fake_pid, &intercept_fd_2, &output_fd_2, &status, kDebuggerdNativeBacktrace);
1047   ASSERT_EQ(InterceptStatus::kFailedAlreadyRegistered, status);
1048 }
1049 
TEST(tombstoned,intercept_any)1050 TEST(tombstoned, intercept_any) {
1051   const pid_t fake_pid = 1'234'567;
1052 
1053   unique_fd intercept_fd, output_fd;
1054   InterceptStatus status;
1055   tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdNativeBacktrace);
1056   ASSERT_EQ(InterceptStatus::kRegistered, status);
1057 
1058   const char any[] = "any";
1059   unique_fd tombstoned_socket, input_fd;
1060   ASSERT_TRUE(tombstoned_connect(fake_pid, &tombstoned_socket, &input_fd, kDebuggerdAnyIntercept));
1061   ASSERT_TRUE(android::base::WriteFully(input_fd.get(), any, sizeof(any)));
1062   tombstoned_notify_completion(tombstoned_socket.get());
1063 
1064   char outbuf[sizeof(any)];
1065   ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
1066   ASSERT_STREQ("any", outbuf);
1067 }
1068 
TEST(tombstoned,interceptless_backtrace)1069 TEST(tombstoned, interceptless_backtrace) {
1070   // Generate 50 backtraces, and then check to see that we haven't created 50 new tombstones.
1071   auto get_tombstone_timestamps = []() -> std::map<int, time_t> {
1072     std::map<int, time_t> result;
1073     for (int i = 0; i < 99; ++i) {
1074       std::string path = android::base::StringPrintf("/data/tombstones/tombstone_%02d", i);
1075       struct stat st;
1076       if (stat(path.c_str(), &st) == 0) {
1077         result[i] = st.st_mtim.tv_sec;
1078       }
1079     }
1080     return result;
1081   };
1082 
1083   auto before = get_tombstone_timestamps();
1084   for (int i = 0; i < 50; ++i) {
1085     raise_debugger_signal(kDebuggerdNativeBacktrace);
1086   }
1087   auto after = get_tombstone_timestamps();
1088 
1089   int diff = 0;
1090   for (int i = 0; i < 99; ++i) {
1091     if (after.count(i) == 0) {
1092       continue;
1093     }
1094     if (before.count(i) == 0) {
1095       ++diff;
1096       continue;
1097     }
1098     if (before[i] != after[i]) {
1099       ++diff;
1100     }
1101   }
1102 
1103   // We can't be sure that nothing's crash looping in the background.
1104   // This should be good enough, though...
1105   ASSERT_LT(diff, 10) << "too many new tombstones; is something crashing in the background?";
1106 }
1107 
overflow_stack(void * p)1108 static __attribute__((__noinline__)) void overflow_stack(void* p) {
1109   void* buf[1];
1110   buf[0] = p;
1111   static volatile void* global = buf;
1112   if (global) {
1113     global = buf;
1114     overflow_stack(&buf);
1115   }
1116 }
1117 
TEST_F(CrasherTest,stack_overflow)1118 TEST_F(CrasherTest, stack_overflow) {
1119   int intercept_result;
1120   unique_fd output_fd;
1121   StartProcess([]() { overflow_stack(nullptr); });
1122 
1123   StartIntercept(&output_fd);
1124   FinishCrasher();
1125   AssertDeath(SIGSEGV);
1126   FinishIntercept(&intercept_result);
1127 
1128   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
1129 
1130   std::string result;
1131   ConsumeFd(std::move(output_fd), &result);
1132   ASSERT_MATCH(result, R"(Cause: stack pointer[^\n]*stack overflow.\n)");
1133 }
1134