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