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