blob: 515dee6e49d75006291cf540f3122a007dbf1a31 [file] [log] [blame]
Narayan Kamatha5ace892017-01-06 15:10:02 +00001/*
2 * Copyright 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 <string>
Lorenzo Colitti173da322017-02-05 01:56:40 +090018#include <fcntl.h>
Lorenzo Colitti2bd804a2017-03-10 12:19:08 +090019#include <sys/file.h>
Lorenzo Colittia701afb2017-02-28 01:47:11 +090020#include <sys/socket.h>
21#include <sys/un.h>
Narayan Kamatha5ace892017-01-06 15:10:02 +000022
23#include <gtest/gtest.h>
24
Lorenzo Colitti173da322017-02-05 01:56:40 +090025#define LOG_TAG "IptablesRestoreControllerTest"
26#include <cutils/log.h>
27#include <android-base/stringprintf.h>
Lorenzo Colitticd283772017-01-31 19:00:49 +090028#include <android-base/strings.h>
Lorenzo Colitti173da322017-02-05 01:56:40 +090029
Narayan Kamatha5ace892017-01-06 15:10:02 +000030#include "IptablesRestoreController.h"
31#include "NetdConstants.h"
32
Lorenzo Colitti2bd804a2017-03-10 12:19:08 +090033#define XT_LOCK_NAME "/system/etc/xtables.lock"
34#define XT_LOCK_ATTEMPTS 10
35#define XT_LOCK_POLL_INTERVAL_MS 100
Lorenzo Colittia701afb2017-02-28 01:47:11 +090036
Lorenzo Colitticd283772017-01-31 19:00:49 +090037using android::base::Join;
Lorenzo Colitti173da322017-02-05 01:56:40 +090038using android::base::StringPrintf;
Narayan Kamatha5ace892017-01-06 15:10:02 +000039
Lorenzo Colitti173da322017-02-05 01:56:40 +090040class IptablesRestoreControllerTest : public ::testing::Test {
41public:
42 IptablesRestoreController con;
Lorenzo Colittia701afb2017-02-28 01:47:11 +090043 int mDefaultMaxRetries = con.MAX_RETRIES;
44 int mDefaultPollTimeoutMs = con.POLL_TIMEOUT_MS;
45 int mIptablesLock = -1;
46 std::string mChainName;
47
Lorenzo Colitti839d7d62017-04-03 15:37:19 +090048 static void SetUpTestCase() {
49 blockSigpipe();
50 }
51
Lorenzo Colittia701afb2017-02-28 01:47:11 +090052 void SetUp() {
53 ASSERT_EQ(0, createTestChain());
54 }
55
56 void TearDown() {
57 con.MAX_RETRIES = mDefaultMaxRetries;
58 con.POLL_TIMEOUT_MS = mDefaultPollTimeoutMs;
59 deleteTestChain();
60 }
Lorenzo Colitti173da322017-02-05 01:56:40 +090061
62 pid_t getIpRestorePid(const IptablesRestoreController::IptablesProcessType type) {
63 return con.getIpRestorePid(type);
64 };
65
66 void expectNoIptablesRestoreProcess(pid_t pid) {
67 // We can't readlink /proc/PID/exe, because zombie processes don't have it.
68 // Parse /proc/PID/stat instead.
69 std::string statPath = StringPrintf("/proc/%d/stat", pid);
70 int fd = open(statPath.c_str(), O_RDONLY);
71 if (fd == -1) {
72 // ENOENT means the process is gone (expected).
73 ASSERT_EQ(errno, ENOENT)
74 << "Unexpected error opening " << statPath << ": " << strerror(errno);
75 return;
76 }
77
78 // If the PID exists, it's possible (though very unlikely) that the PID was reused. Check the
79 // binary name as well, to ensure the test isn't flaky.
80 char statBuf[1024];
81 ASSERT_NE(-1, read(fd, statBuf, sizeof(statBuf)))
82 << "Could not read from " << statPath << ": " << strerror(errno);
83 close(fd);
84
85 std::string statString(statBuf);
86 EXPECT_FALSE(statString.find("iptables-restor") || statString.find("ip6tables-resto"))
87 << "Previous iptables-restore pid " << pid << " still alive: " << statString;
88 }
Lorenzo Colittia701afb2017-02-28 01:47:11 +090089
90 int createTestChain() {
91 mChainName = StringPrintf("netd_unit_test_%u", arc4random_uniform(10000)).c_str();
92
93 // Create a chain to list.
94 std::vector<std::string> createCommands = {
95 "*filter",
96 StringPrintf(":%s -", mChainName.c_str()),
97 StringPrintf("-A %s -j RETURN", mChainName.c_str()),
98 "COMMIT",
99 ""
100 };
101
102 int ret = con.execute(V4V6, Join(createCommands, "\n"), nullptr);
103 if (ret) mChainName = "";
104 return ret;
105 }
106
107 void deleteTestChain() {
108 std::vector<std::string> deleteCommands = {
109 "*filter",
110 StringPrintf(":%s -", mChainName.c_str()), // Flush chain (otherwise we can't delete it).
111 StringPrintf("-X %s", mChainName.c_str()), // Delete it.
112 "COMMIT",
113 ""
114 };
115 con.execute(V4V6, Join(deleteCommands, "\n"), nullptr);
116 mChainName = "";
117 }
118
119 int acquireIptablesLock() {
Lorenzo Colitti2bd804a2017-03-10 12:19:08 +0900120 mIptablesLock = open(XT_LOCK_NAME, O_CREAT, 0600);
121 if (mIptablesLock == -1) return mIptablesLock;
122 int attempts;
123 for (attempts = 0; attempts < XT_LOCK_ATTEMPTS; attempts++) {
124 if (flock(mIptablesLock, LOCK_EX | LOCK_NB) == 0) {
125 return 0;
126 }
127 usleep(XT_LOCK_POLL_INTERVAL_MS * 1000);
Lorenzo Colittia701afb2017-02-28 01:47:11 +0900128 }
Lorenzo Colitti2bd804a2017-03-10 12:19:08 +0900129 EXPECT_LT(attempts, XT_LOCK_ATTEMPTS) <<
130 "Could not acquire iptables lock after " << XT_LOCK_ATTEMPTS << " attempts " <<
131 XT_LOCK_POLL_INTERVAL_MS << "ms apart";
132 return -1;
Lorenzo Colittia701afb2017-02-28 01:47:11 +0900133 }
134
135 void releaseIptablesLock() {
136 if (mIptablesLock != -1) {
137 close(mIptablesLock);
138 }
139 }
140
141 void setRetryParameters(int maxRetries, int pollTimeoutMs) {
142 con.MAX_RETRIES = maxRetries;
143 con.POLL_TIMEOUT_MS = pollTimeoutMs;
144 }
Narayan Kamatha5ace892017-01-06 15:10:02 +0000145};
146
Lorenzo Colitti173da322017-02-05 01:56:40 +0900147TEST_F(IptablesRestoreControllerTest, TestBasicCommand) {
Lorenzo Colitticd283772017-01-31 19:00:49 +0900148 std::string output;
149
150 EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", nullptr));
Lorenzo Colitti173da322017-02-05 01:56:40 +0900151
152 pid_t pid4 = getIpRestorePid(IptablesRestoreController::IPTABLES_PROCESS);
153 pid_t pid6 = getIpRestorePid(IptablesRestoreController::IP6TABLES_PROCESS);
154
Lorenzo Colitticd283772017-01-31 19:00:49 +0900155 EXPECT_EQ(0, con.execute(IptablesTarget::V6, "#Test\n", nullptr));
156 EXPECT_EQ(0, con.execute(IptablesTarget::V4, "#Test\n", nullptr));
157
158 EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", &output));
159 EXPECT_EQ("#Test\n#Test\n", output); // One for IPv4 and one for IPv6.
Lorenzo Colitti173da322017-02-05 01:56:40 +0900160
161 // Check the PIDs are the same as they were before. If they're not, the child processes were
162 // restarted, which causes a 30-60ms delay.
163 EXPECT_EQ(pid4, getIpRestorePid(IptablesRestoreController::IPTABLES_PROCESS));
164 EXPECT_EQ(pid6, getIpRestorePid(IptablesRestoreController::IP6TABLES_PROCESS));
Narayan Kamatha5ace892017-01-06 15:10:02 +0000165}
166
Lorenzo Colitti173da322017-02-05 01:56:40 +0900167TEST_F(IptablesRestoreControllerTest, TestRestartOnMalformedCommand) {
Lorenzo Colitticd283772017-01-31 19:00:49 +0900168 std::string buffer;
Lorenzo Colitti173da322017-02-05 01:56:40 +0900169 for (int i = 0; i < 50; i++) {
170 IptablesTarget target = (IptablesTarget) (i % 3);
Lorenzo Colitticd283772017-01-31 19:00:49 +0900171 std::string *output = (i % 2) ? &buffer : nullptr;
172 ASSERT_EQ(-1, con.execute(target, "malformed command\n", output)) <<
Lorenzo Colitti173da322017-02-05 01:56:40 +0900173 "Malformed command did not fail at iteration " << i;
Lorenzo Colitticd283772017-01-31 19:00:49 +0900174 ASSERT_EQ(0, con.execute(target, "#Test\n", output)) <<
Lorenzo Colitti173da322017-02-05 01:56:40 +0900175 "No-op command did not succeed at iteration " << i;
176 }
177}
178
179TEST_F(IptablesRestoreControllerTest, TestRestartOnProcessDeath) {
Lorenzo Colitticd283772017-01-31 19:00:49 +0900180 std::string output;
181
Lorenzo Colitti173da322017-02-05 01:56:40 +0900182 // Run a command to ensure that the processes are running.
Lorenzo Colitticd283772017-01-31 19:00:49 +0900183 EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", &output));
Lorenzo Colitti173da322017-02-05 01:56:40 +0900184
185 pid_t pid4 = getIpRestorePid(IptablesRestoreController::IPTABLES_PROCESS);
186 pid_t pid6 = getIpRestorePid(IptablesRestoreController::IP6TABLES_PROCESS);
187
188 ASSERT_EQ(0, kill(pid4, 0)) << "iptables-restore pid " << pid4 << " does not exist";
189 ASSERT_EQ(0, kill(pid6, 0)) << "ip6tables-restore pid " << pid6 << " does not exist";
190 ASSERT_EQ(0, kill(pid4, SIGTERM)) << "Failed to send SIGTERM to iptables-restore pid " << pid4;
191 ASSERT_EQ(0, kill(pid6, SIGTERM)) << "Failed to send SIGTERM to ip6tables-restore pid " << pid6;
192
193 // Wait 100ms for processes to terminate.
194 TEMP_FAILURE_RETRY(usleep(100 * 1000));
195
196 // Ensure that running a new command properly restarts the processes.
Lorenzo Colitticd283772017-01-31 19:00:49 +0900197 EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", nullptr));
Lorenzo Colitti173da322017-02-05 01:56:40 +0900198 EXPECT_NE(pid4, getIpRestorePid(IptablesRestoreController::IPTABLES_PROCESS));
199 EXPECT_NE(pid6, getIpRestorePid(IptablesRestoreController::IP6TABLES_PROCESS));
200
201 // Check there are no zombies.
202 expectNoIptablesRestoreProcess(pid4);
203 expectNoIptablesRestoreProcess(pid6);
Narayan Kamatha5ace892017-01-06 15:10:02 +0000204}
Lorenzo Colitticd283772017-01-31 19:00:49 +0900205
Lorenzo Colittia701afb2017-02-28 01:47:11 +0900206TEST_F(IptablesRestoreControllerTest, TestCommandTimeout) {
207 // Don't wait 10 seconds for this test to fail.
208 setRetryParameters(3, 50);
Lorenzo Colitticd283772017-01-31 19:00:49 +0900209
210 // Expected contents of the chain.
211 std::vector<std::string> expectedLines = {
Lorenzo Colittia701afb2017-02-28 01:47:11 +0900212 StringPrintf("Chain %s (0 references)", mChainName.c_str()),
Lorenzo Colitticd283772017-01-31 19:00:49 +0900213 "target prot opt source destination ",
214 "RETURN all -- 0.0.0.0/0 0.0.0.0/0 ",
Lorenzo Colittia701afb2017-02-28 01:47:11 +0900215 StringPrintf("Chain %s (0 references)", mChainName.c_str()),
Lorenzo Colitticd283772017-01-31 19:00:49 +0900216 "target prot opt source destination ",
217 "RETURN all ::/0 ::/0 ",
218 ""
219 };
220 std::string expected = Join(expectedLines, "\n");
221
Lorenzo Colitticd283772017-01-31 19:00:49 +0900222 std::vector<std::string> listCommands = {
223 "*filter",
Lorenzo Colittia701afb2017-02-28 01:47:11 +0900224 StringPrintf("-n -L %s", mChainName.c_str()), // List chain.
Lorenzo Colitticd283772017-01-31 19:00:49 +0900225 "COMMIT",
226 ""
227 };
Lorenzo Colittia701afb2017-02-28 01:47:11 +0900228 std::string commandString = Join(listCommands, "\n");
Lorenzo Colitticd283772017-01-31 19:00:49 +0900229 std::string output;
Lorenzo Colittia701afb2017-02-28 01:47:11 +0900230
231 EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, commandString, &output));
232 EXPECT_EQ(expected, output);
233
234 ASSERT_EQ(0, acquireIptablesLock());
235 EXPECT_EQ(-1, con.execute(IptablesTarget::V4V6, commandString, &output));
236 EXPECT_EQ(-1, con.execute(IptablesTarget::V4V6, commandString, &output));
237 releaseIptablesLock();
238
239 EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, commandString, &output));
Lorenzo Colitticd283772017-01-31 19:00:49 +0900240 EXPECT_EQ(expected, output);
241}