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