Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | */ |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 16 | #define DEBUG false |
| 17 | #include "Log.h" |
| 18 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 19 | #include "incidentd_util.h" |
| 20 | |
Mike Ma | 892ccd9 | 2020-03-20 16:30:37 -0700 | [diff] [blame] | 21 | #include <android/util/EncodedBuffer.h> |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 22 | #include <fcntl.h> |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 23 | #include <sys/prctl.h> |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 24 | #include <wait.h> |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 25 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 26 | #include "section_list.h" |
| 27 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace os { |
| 30 | namespace incidentd { |
| 31 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 32 | const Privacy* get_privacy_of_section(int id) { |
| 33 | int l = 0; |
| 34 | int r = PRIVACY_POLICY_COUNT - 1; |
| 35 | while (l <= r) { |
| 36 | int mid = (l + r) >> 1; |
| 37 | const Privacy* p = PRIVACY_POLICY_LIST[mid]; |
| 38 | |
| 39 | if (p->field_id < (uint32_t)id) { |
| 40 | l = mid + 1; |
| 41 | } else if (p->field_id > (uint32_t)id) { |
| 42 | r = mid - 1; |
| 43 | } else { |
| 44 | return p; |
| 45 | } |
| 46 | } |
| 47 | return NULL; |
| 48 | } |
| 49 | |
Mike Ma | 892ccd9 | 2020-03-20 16:30:37 -0700 | [diff] [blame] | 50 | std::vector<sp<EncodedBuffer>> gBufferPool; |
| 51 | std::mutex gBufferPoolLock; |
| 52 | |
| 53 | sp<EncodedBuffer> get_buffer_from_pool() { |
| 54 | std::scoped_lock<std::mutex> lock(gBufferPoolLock); |
| 55 | if (gBufferPool.size() == 0) { |
| 56 | return new EncodedBuffer(); |
| 57 | } |
| 58 | sp<EncodedBuffer> buffer = gBufferPool.back(); |
| 59 | gBufferPool.pop_back(); |
| 60 | return buffer; |
| 61 | } |
| 62 | |
| 63 | void return_buffer_to_pool(sp<EncodedBuffer> buffer) { |
| 64 | buffer->clear(); |
| 65 | std::scoped_lock<std::mutex> lock(gBufferPoolLock); |
| 66 | gBufferPool.push_back(buffer); |
| 67 | } |
| 68 | |
| 69 | void clear_buffer_pool() { |
| 70 | std::scoped_lock<std::mutex> lock(gBufferPoolLock); |
| 71 | gBufferPool.clear(); |
| 72 | } |
| 73 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 74 | // ================================================================================ |
| 75 | Fpipe::Fpipe() : mRead(), mWrite() {} |
| 76 | |
| 77 | Fpipe::~Fpipe() { close(); } |
| 78 | |
| 79 | bool Fpipe::close() { |
| 80 | mRead.reset(); |
| 81 | mWrite.reset(); |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | bool Fpipe::init() { return Pipe(&mRead, &mWrite); } |
| 86 | |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 87 | unique_fd& Fpipe::readFd() { return mRead; } |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 88 | |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 89 | unique_fd& Fpipe::writeFd() { return mWrite; } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 90 | |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 91 | pid_t fork_execute_cmd(char* const argv[], Fpipe* input, Fpipe* output, int* status) { |
| 92 | int in = -1; |
| 93 | if (input != nullptr) { |
| 94 | in = input->readFd().release(); |
| 95 | // Auto close write end of the input pipe on exec to prevent leaking fd in child process |
| 96 | fcntl(input->writeFd().get(), F_SETFD, FD_CLOEXEC); |
| 97 | } |
| 98 | int out = output->writeFd().release(); |
| 99 | // Auto close read end of the output pipe on exec |
| 100 | fcntl(output->readFd().get(), F_SETFD, FD_CLOEXEC); |
| 101 | return fork_execute_cmd(argv, in, out, status); |
| 102 | } |
| 103 | |
| 104 | pid_t fork_execute_cmd(char* const argv[], int in, int out, int* status) { |
| 105 | int dummy_status = 0; |
| 106 | if (status == nullptr) { |
| 107 | status = &dummy_status; |
| 108 | } |
| 109 | *status = 0; |
Mike Ma | 892ccd9 | 2020-03-20 16:30:37 -0700 | [diff] [blame] | 110 | pid_t pid = vfork(); |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 111 | if (pid < 0) { |
| 112 | *status = -errno; |
| 113 | return -1; |
| 114 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 115 | if (pid == 0) { |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 116 | // In child |
| 117 | if (in >= 0 && (TEMP_FAILURE_RETRY(dup2(in, STDIN_FILENO)) < 0 || close(in))) { |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 118 | ALOGW("Failed to dup2 stdin."); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 119 | _exit(EXIT_FAILURE); |
| 120 | } |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 121 | if (TEMP_FAILURE_RETRY(dup2(out, STDOUT_FILENO)) < 0 || close(out)) { |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 122 | ALOGW("Failed to dup2 stdout."); |
| 123 | _exit(EXIT_FAILURE); |
| 124 | } |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 125 | // Make sure the child dies when incidentd dies |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 126 | prctl(PR_SET_PDEATHSIG, SIGKILL); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 127 | execvp(argv[0], argv); |
| 128 | _exit(errno); // always exits with failure if any |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 129 | } |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 130 | // In parent |
| 131 | if ((in >= 0 && close(in) < 0) || close(out) < 0) { |
| 132 | ALOGW("Failed to close pd. Killing child process"); |
| 133 | *status = -errno; |
| 134 | kill_child(pid); |
| 135 | return -1; |
| 136 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 137 | return pid; |
| 138 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 139 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 140 | // ================================================================================ |
| 141 | const char** varargs(const char* first, va_list rest) { |
| 142 | va_list copied_rest; |
| 143 | int numOfArgs = 1; // first is already count. |
| 144 | |
| 145 | va_copy(copied_rest, rest); |
| 146 | while (va_arg(copied_rest, const char*) != NULL) { |
| 147 | numOfArgs++; |
| 148 | } |
| 149 | va_end(copied_rest); |
| 150 | |
| 151 | // allocate extra 1 for NULL terminator |
| 152 | const char** ret = (const char**)malloc(sizeof(const char*) * (numOfArgs + 1)); |
| 153 | ret[0] = first; |
Yi Jin | 06ebd1a | 2018-02-28 11:25:58 -0800 | [diff] [blame] | 154 | for (int i = 1; i < numOfArgs; i++) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 155 | const char* arg = va_arg(rest, const char*); |
Yi Jin | 06ebd1a | 2018-02-28 11:25:58 -0800 | [diff] [blame] | 156 | ret[i] = arg; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 157 | } |
Yi Jin | 06ebd1a | 2018-02-28 11:25:58 -0800 | [diff] [blame] | 158 | ret[numOfArgs] = NULL; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 159 | return ret; |
| 160 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 161 | |
| 162 | // ================================================================================ |
| 163 | const uint64_t NANOS_PER_SEC = 1000000000; |
| 164 | uint64_t Nanotime() { |
| 165 | timespec ts; |
| 166 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 167 | return static_cast<uint64_t>(ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec); |
| 168 | } |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 169 | |
| 170 | // ================================================================================ |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 171 | static status_t statusCode(int status) { |
| 172 | if (WIFSIGNALED(status)) { |
| 173 | VLOG("return by signal: %s", strerror(WTERMSIG(status))); |
| 174 | return -WTERMSIG(status); |
| 175 | } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) { |
| 176 | VLOG("return by exit: %s", strerror(WEXITSTATUS(status))); |
| 177 | return -WEXITSTATUS(status); |
| 178 | } |
| 179 | return NO_ERROR; |
| 180 | } |
| 181 | |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 182 | static bool waitpid_with_timeout(pid_t pid, int timeout_ms, int* status) { |
| 183 | sigset_t child_mask, old_mask; |
| 184 | sigemptyset(&child_mask); |
| 185 | sigaddset(&child_mask, SIGCHLD); |
| 186 | |
| 187 | if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) { |
| 188 | ALOGW("sigprocmask failed: %s", strerror(errno)); |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | timespec ts; |
| 193 | ts.tv_sec = timeout_ms / 1000; |
| 194 | ts.tv_nsec = (timeout_ms % 1000) * 1000000; |
| 195 | int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, nullptr, &ts)); |
| 196 | int saved_errno = errno; |
| 197 | |
| 198 | // Set the signals back the way they were. |
| 199 | if (sigprocmask(SIG_SETMASK, &old_mask, nullptr) == -1) { |
| 200 | ALOGW("sigprocmask failed: %s", strerror(errno)); |
| 201 | if (ret == 0) { |
| 202 | return false; |
| 203 | } |
| 204 | } |
| 205 | if (ret == -1) { |
| 206 | errno = saved_errno; |
| 207 | if (errno == EAGAIN) { |
| 208 | errno = ETIMEDOUT; |
| 209 | } else { |
| 210 | ALOGW("sigtimedwait failed: %s", strerror(errno)); |
| 211 | } |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | pid_t child_pid = waitpid(pid, status, WNOHANG); |
| 216 | if (child_pid == pid) { |
| 217 | return true; |
| 218 | } |
| 219 | if (child_pid == -1) { |
| 220 | ALOGW("waitpid failed: %s", strerror(errno)); |
| 221 | } else { |
| 222 | ALOGW("Waiting for pid %d, got pid %d instead", pid, child_pid); |
| 223 | } |
| 224 | return false; |
| 225 | } |
| 226 | |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 227 | status_t kill_child(pid_t pid) { |
| 228 | int status; |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 229 | kill(pid, SIGKILL); |
| 230 | if (waitpid(pid, &status, 0) == -1) return -1; |
| 231 | return statusCode(status); |
| 232 | } |
| 233 | |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 234 | status_t wait_child(pid_t pid, int timeout_ms) { |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 235 | int status; |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 236 | if (waitpid_with_timeout(pid, timeout_ms, &status)) { |
| 237 | return statusCode(status); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 238 | } |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 239 | return kill_child(pid); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 240 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 241 | |
| 242 | } // namespace incidentd |
| 243 | } // namespace os |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 244 | } // namespace android |