blob: ba3279c1136eda215e6526d08ba952257388877c [file] [log] [blame]
Narayan Kamatha5ace892017-01-06 15:10:02 +00001/*
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 Colitti173da322017-02-05 01:56:40 +090024#define LOG_TAG "IptablesRestoreController"
Narayan Kamatha5ace892017-01-06 15:10:02 +000025#include <android-base/logging.h>
26#include <android-base/file.h>
27
28#include "Controllers.h"
29
30constexpr char IPTABLES_RESTORE_PATH[] = "/system/bin/iptables-restore";
31constexpr char IP6TABLES_RESTORE_PATH[] = "/system/bin/ip6tables-restore";
32
33constexpr char PING[] = "#PING\n";
34
35constexpr 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
42class IptablesProcess {
43public:
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 Colitti173da322017-02-05 01:56:40 +090059 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 Kamatha5ace892017-01-06 15:10:02 +0000101 const pid_t pid;
102 const int stdIn;
103
104 struct pollfd pollFds[2];
105 std::string errBuf;
106
Lorenzo Colitti173da322017-02-05 01:56:40 +0900107 std::atomic_bool processTerminated;
Narayan Kamatha5ace892017-01-06 15:10:02 +0000108
109 static constexpr size_t STDOUT_IDX = 0;
110 static constexpr size_t STDERR_IDX = 1;
111};
112
113IptablesRestoreController::IptablesRestoreController() :
114 mIpRestore(nullptr),
115 mIp6Restore(nullptr) {
116}
117
118IptablesRestoreController::~IptablesRestoreController() {
119}
120
121/* static */
122IptablesProcess* 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 ||
Lorenzo Colitticd283772017-01-31 19:00:49 +0900133 pipe2(stdout_pipe, O_NONBLOCK) == -1 ||
134 pipe2(stderr_pipe, O_NONBLOCK) == -1) {
Narayan Kamatha5ace892017-01-06 15:10:02 +0000135
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 Kamatha5ace892017-01-06 15:10:02 +0000198// 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.
201int IptablesRestoreController::sendCommand(const IptablesProcessType type,
Lorenzo Colitticd283772017-01-31 19:00:49 +0900202 const std::string& command,
203 std::string *output) {
Narayan Kamatha5ace892017-01-06 15:10:02 +0000204 std::unique_ptr<IptablesProcess> *process =
205 (type == IPTABLES_PROCESS) ? &mIpRestore : &mIp6Restore;
206
Lorenzo Colitti173da322017-02-05 01:56:40 +0900207
Narayan Kamatha5ace892017-01-06 15:10:02 +0000208 // We might need to fork a new process if we haven't forked one yet, or
209 // if the forked process terminated.
210 //
211 // NOTE: For a given command, this is the last point at which we try to
212 // recover from a child death. If the child dies at some later point during
213 // the execution of this method, we will receive an EPIPE and return an
214 // error. The command will then need to be retried at a higher level.
Lorenzo Colitti173da322017-02-05 01:56:40 +0900215 IptablesProcess *existingProcess = process->get();
216 if (existingProcess != nullptr && !existingProcess->outputReady()) {
217 existingProcess->stop();
218 existingProcess = nullptr;
219 }
220
221 if (existingProcess == nullptr) {
Narayan Kamatha5ace892017-01-06 15:10:02 +0000222 // Fork a new iptables[6]-restore process.
223 IptablesProcess *newProcess = IptablesRestoreController::forkAndExec(type);
224 if (newProcess == nullptr) {
225 LOG(ERROR) << "Unable to fork ip[6]tables-restore, type: " << type;
226 return -1;
227 }
228
229 process->reset(newProcess);
230 }
231
232 // TODO: Investigate why this horrible hackery is necessary. We're currently
233 // sending iptables[6]-restore malformed commands. They appear to contain garbage
234 // after the last "\n". They obviously "work" because we fork a new process
235 // for every command so it doesn't matter whether the process chokes after
236 // the last successful COMMIT.
237 const std::string fixedCommand = fixCommandString(command);
238
239 if (!android::base::WriteFully((*process)->stdIn,
240 fixedCommand.data(),
241 fixedCommand.length())) {
242 PLOG(ERROR) << "Unable to send command";
Lorenzo Colitti173da322017-02-05 01:56:40 +0900243 return -1;
Narayan Kamatha5ace892017-01-06 15:10:02 +0000244 }
245
246 if (!android::base::WriteFully((*process)->stdIn, PING, PING_SIZE)) {
247 PLOG(ERROR) << "Unable to send ping command : " << type;
248 return -1;
249 }
250
Lorenzo Colitticd283772017-01-31 19:00:49 +0900251 if (!drainAndWaitForAck(*process, output)) {
Lorenzo Colitti173da322017-02-05 01:56:40 +0900252 // drainAndWaitForAck has already logged an error.
Narayan Kamatha5ace892017-01-06 15:10:02 +0000253 return -1;
254 }
255
256 return 0;
257}
258
259/* static */
260std::string IptablesRestoreController::fixCommandString(const std::string& command) {
261 std::string commandDup = command;
262 commandDup.erase(commandDup.find_last_of("\n") + 1);
263 return commandDup;
264}
265
266void IptablesRestoreController::maybeLogStderr(const std::unique_ptr<IptablesProcess> &process,
267 const char* buf, ssize_t numBytes) {
268 ssize_t lastNewline = 0;
269 for (ssize_t i = 0; i < numBytes; ++i) {
270 if (buf[i] == '\n') {
271 process->errBuf.append(buf + lastNewline, (i - lastNewline));
272 LOG(ERROR) << "Iptables : " << process->errBuf;
273 process->errBuf.clear();
274 lastNewline = i;
275 }
276 }
277
278 // Append all remaining characters to the buffer so that they're logged the
279 // next time 'round.
280 if (lastNewline < (static_cast<ssize_t>(numBytes) - 1)) {
281 process->errBuf.append(buf + lastNewline,
282 static_cast<ssize_t>(numBytes) - 1 - lastNewline);
283 }
284}
285
286// The maximum number of times we poll(2) for a response on our set of polled
287// fds. Chosen so that the overall timeout is 1s.
288static constexpr int MAX_RETRIES = 10;
289
290// The timeout (in millis) for each call to poll. The maximum wait is
291// |POLL_TIMEOUT_MS * MAX_RETRIES|. Chosen so that the overall timeout is 1s.
292static constexpr int POLL_TIMEOUT_MS = 100;
293
294/* static */
Lorenzo Colitticd283772017-01-31 19:00:49 +0900295bool IptablesRestoreController::drainAndWaitForAck(const std::unique_ptr<IptablesProcess> &process,
296 std::string *output) {
Narayan Kamatha5ace892017-01-06 15:10:02 +0000297 bool receivedAck = false;
298 int timeout = 0;
Narayan Kamatha5ace892017-01-06 15:10:02 +0000299 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 Colitti173da322017-02-05 01:56:40 +0900308 // become available to read with the ACK message, or that stderr should have been available
309 // to read with an error message.
Narayan Kamatha5ace892017-01-06 15:10:02 +0000310 if (numEvents == 0) {
311 continue;
312 }
313
Lorenzo Colitticd283772017-01-31 19:00:49 +0900314 char buffer[PIPE_BUF];
Narayan Kamatha5ace892017-01-06 15:10:02 +0000315 for (size_t i = 0; i < ARRAY_SIZE(process->pollFds); ++i) {
316 const struct pollfd &pollfd = process->pollFds[i];
317 if (pollfd.revents & POLLIN) {
Lorenzo Colitticd283772017-01-31 19:00:49 +0900318 ssize_t size;
319 do {
320 size = TEMP_FAILURE_RETRY(read(pollfd.fd, buffer, sizeof(buffer)));
Narayan Kamatha5ace892017-01-06 15:10:02 +0000321
Lorenzo Colitticd283772017-01-31 19:00:49 +0900322 if (size == -1) {
323 if (errno != EAGAIN) {
324 PLOG(ERROR) << "Unable to read from descriptor";
325 }
326 break;
Narayan Kamatha5ace892017-01-06 15:10:02 +0000327 }
Lorenzo Colitticd283772017-01-31 19:00:49 +0900328
329 if (i == IptablesProcess::STDOUT_IDX) {
330 // i == STDOUT_IDX: accumulate stdout into *output, and look
331 // for the ping response.
332 output->append(buffer, size);
333 size_t pos = output->find(PING);
334 if (pos != std::string::npos) {
335 if (output->size() > pos + PING_SIZE) {
336 size_t extra = output->size() - (pos + PING_SIZE);
337 ALOGW("%zd extra characters after iptables response: '%s...'",
338 extra, output->substr(pos + PING_SIZE, 128).c_str());
339 }
340 output->resize(pos);
341 receivedAck = true;
342 }
343 } else {
344 // i == STDERR_IDX implies stderr, log.
345 IptablesRestoreController::maybeLogStderr(process, buffer, size);
346 }
347 } while (size > 0);
Narayan Kamatha5ace892017-01-06 15:10:02 +0000348 }
Lorenzo Colitti173da322017-02-05 01:56:40 +0900349 if (pollfd.revents & POLLHUP) {
350 // The pipe was closed. This likely means the subprocess is exiting, since
351 // iptables-restore only closes stdin on error.
352 process->stop();
353 break;
354 }
Narayan Kamatha5ace892017-01-06 15:10:02 +0000355 }
356 }
357
Lorenzo Colitti173da322017-02-05 01:56:40 +0900358 if (!receivedAck) {
359 if (process->processTerminated)
360 ALOGE("iptables-restore process %d terminated", process->pid);
361 else
362 ALOGE("Timed out waiting for response from iptables process %d", process->pid);
363 }
364
Narayan Kamatha5ace892017-01-06 15:10:02 +0000365 return receivedAck;
366}
367
Lorenzo Colitticd283772017-01-31 19:00:49 +0900368int IptablesRestoreController::execute(const IptablesTarget target, const std::string& command,
369 std::string *output) {
Narayan Kamatha5ace892017-01-06 15:10:02 +0000370 std::lock_guard<std::mutex> lock(mLock);
371
Lorenzo Colitticd283772017-01-31 19:00:49 +0900372 std::string buffer;
373 if (output == nullptr) {
374 output = &buffer;
375 }
376
Narayan Kamatha5ace892017-01-06 15:10:02 +0000377 int res = 0;
378 if (target == V4 || target == V4V6) {
Lorenzo Colitticd283772017-01-31 19:00:49 +0900379 res |= sendCommand(IPTABLES_PROCESS, command, output);
Narayan Kamatha5ace892017-01-06 15:10:02 +0000380 }
381 if (target == V6 || target == V4V6) {
Lorenzo Colitticd283772017-01-31 19:00:49 +0900382 res |= sendCommand(IP6TABLES_PROCESS, command, output);
Narayan Kamatha5ace892017-01-06 15:10:02 +0000383 }
384 return res;
385}
Lorenzo Colitti173da322017-02-05 01:56:40 +0900386
387int IptablesRestoreController::getIpRestorePid(const IptablesProcessType type) {
388 return type == IPTABLES_PROCESS ? mIpRestore->pid : mIp6Restore->pid;
389}