Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [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 | */ |
| 16 | |
| 17 | #include "IptablesRestoreController.h" |
| 18 | |
| 19 | #include <poll.h> |
| 20 | #include <signal.h> |
| 21 | #include <sys/wait.h> |
| 22 | #include <unistd.h> |
| 23 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame^] | 24 | #define LOG_TAG "IptablesRestoreController" |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/file.h> |
| 27 | |
| 28 | #include "Controllers.h" |
| 29 | |
| 30 | constexpr char IPTABLES_RESTORE_PATH[] = "/system/bin/iptables-restore"; |
| 31 | constexpr char IP6TABLES_RESTORE_PATH[] = "/system/bin/ip6tables-restore"; |
| 32 | |
| 33 | constexpr char PING[] = "#PING\n"; |
| 34 | |
| 35 | constexpr size_t PING_SIZE = sizeof(PING) - 1; |
| 36 | |
| 37 | // TODO: This mirrors &gCtls.iptablesRestoreCtrl in production and is duplicated |
| 38 | // here to aid testing. It allows us to unit-test IptablesRestoreController without |
| 39 | // needing to construct a fully fledged Controllers object. |
| 40 | /* static */ IptablesRestoreController* sInstance = nullptr; |
| 41 | |
| 42 | class IptablesProcess { |
| 43 | public: |
| 44 | IptablesProcess(pid_t pid, int stdIn, int stdOut, int stdErr) : |
| 45 | pid(pid), |
| 46 | stdIn(stdIn), |
| 47 | processTerminated(false) { |
| 48 | |
| 49 | pollFds[STDOUT_IDX] = { .fd = stdOut, .events = POLLIN }; |
| 50 | pollFds[STDERR_IDX] = { .fd = stdErr, .events = POLLIN }; |
| 51 | } |
| 52 | |
| 53 | ~IptablesProcess() { |
| 54 | close(stdIn); |
| 55 | close(pollFds[STDOUT_IDX].fd); |
| 56 | close(pollFds[STDERR_IDX].fd); |
| 57 | } |
| 58 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame^] | 59 | bool outputReady() { |
| 60 | struct pollfd pollfd = { .fd = stdIn, .events = POLLOUT }; |
| 61 | int ret = poll(&pollfd, 1, 0); |
| 62 | if (ret == -1) { |
| 63 | ALOGE("outputReady poll failed: %s", strerror(errno)); |
| 64 | return false; |
| 65 | } |
| 66 | return (ret == 1) && !(pollfd.revents & POLLERR); |
| 67 | } |
| 68 | |
| 69 | void stop() { |
| 70 | if (processTerminated) return; |
| 71 | |
| 72 | // This can be called by drainAndWaitForAck (after a POLLHUP) or by sendCommand (if the |
| 73 | // process was killed by something else on the system). In both cases, it's safe to send the |
| 74 | // PID a SIGTERM, because the PID continues to exist until its parent (i.e., us) calls |
| 75 | // waitpid on it, so there's no risk that the PID is reused. |
| 76 | int err = kill(pid, SIGTERM); |
| 77 | if (err) { |
| 78 | err = errno; |
| 79 | } |
| 80 | |
| 81 | if (err == ESRCH) { |
| 82 | // This means that someone else inside netd but outside this class called waitpid(), |
| 83 | // which is a programming error. There's no point in calling waitpid() here since we |
| 84 | // know that the process is gone. |
| 85 | ALOGE("iptables child process %d unexpectedly disappeared", pid); |
| 86 | processTerminated = true; |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | if (err) { |
| 91 | ALOGE("Error killing iptables child process %d: %s", pid, strerror(err)); |
| 92 | } |
| 93 | |
| 94 | if (waitpid(pid, nullptr, 0) == -1) { |
| 95 | ALOGE("Error waiting for iptables child process %d: %s", pid, strerror(errno)); |
| 96 | } |
| 97 | |
| 98 | processTerminated = true; |
| 99 | } |
| 100 | |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 101 | const pid_t pid; |
| 102 | const int stdIn; |
| 103 | |
| 104 | struct pollfd pollFds[2]; |
| 105 | std::string errBuf; |
| 106 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame^] | 107 | std::atomic_bool processTerminated; |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 108 | |
| 109 | static constexpr size_t STDOUT_IDX = 0; |
| 110 | static constexpr size_t STDERR_IDX = 1; |
| 111 | }; |
| 112 | |
| 113 | IptablesRestoreController::IptablesRestoreController() : |
| 114 | mIpRestore(nullptr), |
| 115 | mIp6Restore(nullptr) { |
| 116 | } |
| 117 | |
| 118 | IptablesRestoreController::~IptablesRestoreController() { |
| 119 | } |
| 120 | |
| 121 | /* static */ |
| 122 | IptablesProcess* IptablesRestoreController::forkAndExec(const IptablesProcessType type) { |
| 123 | const char* const cmd = (type == IPTABLES_PROCESS) ? |
| 124 | IPTABLES_RESTORE_PATH : IP6TABLES_RESTORE_PATH; |
| 125 | |
| 126 | // Create the pipes we'll use for communication with the child |
| 127 | // process. One each for the child's in, out and err files. |
| 128 | int stdin_pipe[2]; |
| 129 | int stdout_pipe[2]; |
| 130 | int stderr_pipe[2]; |
| 131 | |
| 132 | if (pipe2(stdin_pipe, 0) == -1 || |
| 133 | pipe2(stdout_pipe, 0) == -1 || |
| 134 | pipe2(stderr_pipe, 0) == -1) { |
| 135 | |
| 136 | PLOG(ERROR) << "pipe2() failed"; |
| 137 | return nullptr; |
| 138 | } |
| 139 | |
| 140 | pid_t child_pid = fork(); |
| 141 | if (child_pid == 0) { |
| 142 | // The child process. Reads from stdin, writes to stderr and stdout. |
| 143 | |
| 144 | // stdin_pipe[1] : The write end of the stdin pipe. |
| 145 | // stdout_pipe[0] : The read end of the stdout pipe. |
| 146 | // stderr_pipe[0] : The read end of the stderr pipe. |
| 147 | if (close(stdin_pipe[1]) == -1 || |
| 148 | close(stdout_pipe[0]) == -1 || |
| 149 | close(stderr_pipe[0]) == -1) { |
| 150 | |
| 151 | PLOG(WARNING) << "close() failed"; |
| 152 | } |
| 153 | |
| 154 | // stdin_pipe[0] : The read end of the stdin pipe. |
| 155 | // stdout_pipe[1] : The write end of the stdout pipe. |
| 156 | // stderr_pipe[1] : The write end of the stderr pipe. |
| 157 | if (dup2(stdin_pipe[0], 0) == -1 || |
| 158 | dup2(stdout_pipe[1], 1) == -1 || |
| 159 | dup2(stderr_pipe[1], 2) == -1) { |
| 160 | PLOG(ERROR) << "dup2() failed"; |
| 161 | abort(); |
| 162 | } |
| 163 | |
| 164 | if (execl(cmd, |
| 165 | cmd, |
| 166 | "--noflush", // Don't flush the whole table. |
| 167 | "-w", // Wait instead of failing if the lock is held. |
| 168 | "-v", // Verbose mode, to make sure our ping is echoed |
| 169 | // back to us. |
| 170 | nullptr) == -1) { |
| 171 | PLOG(ERROR) << "execl(" << cmd << ", ...) failed"; |
| 172 | abort(); |
| 173 | } |
| 174 | |
| 175 | // This statement is unreachable. We abort() upon error, and execl |
| 176 | // if everything goes well. |
| 177 | return nullptr; |
| 178 | } |
| 179 | |
| 180 | // The parent process. Writes to stdout and stderr and reads from stdin. |
| 181 | if (child_pid == -1) { |
| 182 | PLOG(ERROR) << "fork() failed"; |
| 183 | return nullptr; |
| 184 | } |
| 185 | |
| 186 | // stdin_pipe[0] : The read end of the stdin pipe. |
| 187 | // stdout_pipe[1] : The write end of the stdout pipe. |
| 188 | // stderr_pipe[1] : The write end of the stderr pipe. |
| 189 | if (close(stdin_pipe[0]) == -1 || |
| 190 | close(stdout_pipe[1]) == -1 || |
| 191 | close(stderr_pipe[1]) == -1) { |
| 192 | PLOG(WARNING) << "close() failed"; |
| 193 | } |
| 194 | |
| 195 | return new IptablesProcess(child_pid, stdin_pipe[1], stdout_pipe[0], stderr_pipe[0]); |
| 196 | } |
| 197 | |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 198 | // TODO: Return -errno on failure instead of -1. |
| 199 | // TODO: Maybe we should keep a rotating buffer of the last N commands |
| 200 | // so that they can be dumped on dumpsys. |
| 201 | int IptablesRestoreController::sendCommand(const IptablesProcessType type, |
| 202 | const std::string& command) { |
| 203 | std::unique_ptr<IptablesProcess> *process = |
| 204 | (type == IPTABLES_PROCESS) ? &mIpRestore : &mIp6Restore; |
| 205 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame^] | 206 | |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 207 | // We might need to fork a new process if we haven't forked one yet, or |
| 208 | // if the forked process terminated. |
| 209 | // |
| 210 | // NOTE: For a given command, this is the last point at which we try to |
| 211 | // recover from a child death. If the child dies at some later point during |
| 212 | // the execution of this method, we will receive an EPIPE and return an |
| 213 | // error. The command will then need to be retried at a higher level. |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame^] | 214 | IptablesProcess *existingProcess = process->get(); |
| 215 | if (existingProcess != nullptr && !existingProcess->outputReady()) { |
| 216 | existingProcess->stop(); |
| 217 | existingProcess = nullptr; |
| 218 | } |
| 219 | |
| 220 | if (existingProcess == nullptr) { |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 221 | // Fork a new iptables[6]-restore process. |
| 222 | IptablesProcess *newProcess = IptablesRestoreController::forkAndExec(type); |
| 223 | if (newProcess == nullptr) { |
| 224 | LOG(ERROR) << "Unable to fork ip[6]tables-restore, type: " << type; |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | process->reset(newProcess); |
| 229 | } |
| 230 | |
| 231 | // TODO: Investigate why this horrible hackery is necessary. We're currently |
| 232 | // sending iptables[6]-restore malformed commands. They appear to contain garbage |
| 233 | // after the last "\n". They obviously "work" because we fork a new process |
| 234 | // for every command so it doesn't matter whether the process chokes after |
| 235 | // the last successful COMMIT. |
| 236 | const std::string fixedCommand = fixCommandString(command); |
| 237 | |
| 238 | if (!android::base::WriteFully((*process)->stdIn, |
| 239 | fixedCommand.data(), |
| 240 | fixedCommand.length())) { |
| 241 | PLOG(ERROR) << "Unable to send command"; |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame^] | 242 | return -1; |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | if (!android::base::WriteFully((*process)->stdIn, PING, PING_SIZE)) { |
| 246 | PLOG(ERROR) << "Unable to send ping command : " << type; |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | if (!drainAndWaitForAck(*process)) { |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame^] | 251 | // drainAndWaitForAck has already logged an error. |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | /* static */ |
| 259 | std::string IptablesRestoreController::fixCommandString(const std::string& command) { |
| 260 | std::string commandDup = command; |
| 261 | commandDup.erase(commandDup.find_last_of("\n") + 1); |
| 262 | return commandDup; |
| 263 | } |
| 264 | |
| 265 | void IptablesRestoreController::maybeLogStderr(const std::unique_ptr<IptablesProcess> &process, |
| 266 | const char* buf, ssize_t numBytes) { |
| 267 | ssize_t lastNewline = 0; |
| 268 | for (ssize_t i = 0; i < numBytes; ++i) { |
| 269 | if (buf[i] == '\n') { |
| 270 | process->errBuf.append(buf + lastNewline, (i - lastNewline)); |
| 271 | LOG(ERROR) << "Iptables : " << process->errBuf; |
| 272 | process->errBuf.clear(); |
| 273 | lastNewline = i; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // Append all remaining characters to the buffer so that they're logged the |
| 278 | // next time 'round. |
| 279 | if (lastNewline < (static_cast<ssize_t>(numBytes) - 1)) { |
| 280 | process->errBuf.append(buf + lastNewline, |
| 281 | static_cast<ssize_t>(numBytes) - 1 - lastNewline); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // The maximum number of times we poll(2) for a response on our set of polled |
| 286 | // fds. Chosen so that the overall timeout is 1s. |
| 287 | static constexpr int MAX_RETRIES = 10; |
| 288 | |
| 289 | // The timeout (in millis) for each call to poll. The maximum wait is |
| 290 | // |POLL_TIMEOUT_MS * MAX_RETRIES|. Chosen so that the overall timeout is 1s. |
| 291 | static constexpr int POLL_TIMEOUT_MS = 100; |
| 292 | |
| 293 | /* static */ |
| 294 | bool IptablesRestoreController::drainAndWaitForAck( |
| 295 | const std::unique_ptr<IptablesProcess> &process) { |
| 296 | bool receivedAck = false; |
| 297 | int timeout = 0; |
| 298 | std::string out; |
| 299 | while (!receivedAck && (timeout++ < MAX_RETRIES)) { |
| 300 | int numEvents = TEMP_FAILURE_RETRY( |
| 301 | poll(process->pollFds, ARRAY_SIZE(process->pollFds), POLL_TIMEOUT_MS)); |
| 302 | if (numEvents == -1) { |
| 303 | PLOG(ERROR) << "Poll failed."; |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | // We've timed out, which means something has gone wrong - we know that stdout should have |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame^] | 308 | // become available to read with the ACK message, or that stderr should have been available |
| 309 | // to read with an error message. |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 310 | if (numEvents == 0) { |
| 311 | continue; |
| 312 | } |
| 313 | |
| 314 | char buffer[256]; |
| 315 | for (size_t i = 0; i < ARRAY_SIZE(process->pollFds); ++i) { |
| 316 | const struct pollfd &pollfd = process->pollFds[i]; |
| 317 | if (pollfd.revents & POLLIN) { |
| 318 | // TODO: We read a maximum of 256 bytes for each call to poll. |
| 319 | // We should change this so that we can read as much input as we |
| 320 | // can from the descriptor without blocking. |
| 321 | const ssize_t size = TEMP_FAILURE_RETRY(read(pollfd.fd, buffer, sizeof(buffer))); |
| 322 | |
| 323 | // This should never happen. Poll just told us that we have |
| 324 | // something available. |
| 325 | if (size == -1) { |
| 326 | PLOG(ERROR) << "Unable to read from descriptor"; |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | if (i == IptablesProcess::STDOUT_IDX) { |
| 331 | // i == STDOUT_IDX : look for the ping response. We use |
| 332 | // a string buffer here because it's possible (but unlikely) |
| 333 | // that only a subsection of the PING response is available |
| 334 | // on the pipe when poll returns for the first time. We use |
| 335 | // find instead of operator== to be robust in the case of |
| 336 | // additional stdout logging. |
| 337 | out.append(buffer, size); |
| 338 | if (out.find(PING) != std::string::npos) { |
| 339 | receivedAck = true; |
| 340 | } |
| 341 | } else { |
| 342 | // i == STDERR_IDX implies stderr, log. |
| 343 | IptablesRestoreController::maybeLogStderr(process, buffer, size); |
| 344 | } |
| 345 | } |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame^] | 346 | if (pollfd.revents & POLLHUP) { |
| 347 | // The pipe was closed. This likely means the subprocess is exiting, since |
| 348 | // iptables-restore only closes stdin on error. |
| 349 | process->stop(); |
| 350 | break; |
| 351 | } |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame^] | 355 | if (!receivedAck) { |
| 356 | if (process->processTerminated) |
| 357 | ALOGE("iptables-restore process %d terminated", process->pid); |
| 358 | else |
| 359 | ALOGE("Timed out waiting for response from iptables process %d", process->pid); |
| 360 | } |
| 361 | |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 362 | return receivedAck; |
| 363 | } |
| 364 | |
| 365 | int IptablesRestoreController::execute(const IptablesTarget target, const std::string& command) { |
| 366 | std::lock_guard<std::mutex> lock(mLock); |
| 367 | |
| 368 | int res = 0; |
| 369 | if (target == V4 || target == V4V6) { |
| 370 | res |= sendCommand(IPTABLES_PROCESS, command); |
| 371 | } |
| 372 | if (target == V6 || target == V4V6) { |
| 373 | res |= sendCommand(IP6TABLES_PROCESS, command); |
| 374 | } |
| 375 | return res; |
| 376 | } |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame^] | 377 | |
| 378 | int IptablesRestoreController::getIpRestorePid(const IptablesProcessType type) { |
| 379 | return type == IPTABLES_PROCESS ? mIpRestore->pid : mIp6Restore->pid; |
| 380 | } |