Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 1 | /* |
| 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 Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 18 | #include <fcntl.h> |
Lorenzo Colitti | 2bd804a | 2017-03-10 12:19:08 +0900 | [diff] [blame] | 19 | #include <sys/file.h> |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 20 | #include <sys/socket.h> |
| 21 | #include <sys/un.h> |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 22 | |
Lorenzo Colitti | 2103b6b | 2017-08-14 11:38:18 +0900 | [diff] [blame^] | 23 | #include <gmock/gmock.h> |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
| 25 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 26 | #define LOG_TAG "IptablesRestoreControllerTest" |
| 27 | #include <cutils/log.h> |
| 28 | #include <android-base/stringprintf.h> |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 29 | #include <android-base/strings.h> |
Lorenzo Colitti | 2103b6b | 2017-08-14 11:38:18 +0900 | [diff] [blame^] | 30 | #include <netdutils/MockSyscalls.h> |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 31 | |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 32 | #include "IptablesRestoreController.h" |
| 33 | #include "NetdConstants.h" |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 34 | #include "Stopwatch.h" |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 35 | |
Lorenzo Colitti | 2bd804a | 2017-03-10 12:19:08 +0900 | [diff] [blame] | 36 | #define XT_LOCK_NAME "/system/etc/xtables.lock" |
| 37 | #define XT_LOCK_ATTEMPTS 10 |
| 38 | #define XT_LOCK_POLL_INTERVAL_MS 100 |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 39 | |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 40 | using android::base::Join; |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 41 | using android::base::StringPrintf; |
Lorenzo Colitti | 2103b6b | 2017-08-14 11:38:18 +0900 | [diff] [blame^] | 42 | using android::netdutils::ScopedMockSyscalls; |
| 43 | using testing::Return; |
| 44 | using testing::StrictMock; |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 45 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 46 | class IptablesRestoreControllerTest : public ::testing::Test { |
| 47 | public: |
| 48 | IptablesRestoreController con; |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 49 | int mDefaultMaxRetries = con.MAX_RETRIES; |
| 50 | int mDefaultPollTimeoutMs = con.POLL_TIMEOUT_MS; |
| 51 | int mIptablesLock = -1; |
| 52 | std::string mChainName; |
| 53 | |
Lorenzo Colitti | 839d7d6 | 2017-04-03 15:37:19 +0900 | [diff] [blame] | 54 | static void SetUpTestCase() { |
| 55 | blockSigpipe(); |
| 56 | } |
| 57 | |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 58 | void SetUp() { |
| 59 | ASSERT_EQ(0, createTestChain()); |
| 60 | } |
| 61 | |
| 62 | void TearDown() { |
| 63 | con.MAX_RETRIES = mDefaultMaxRetries; |
| 64 | con.POLL_TIMEOUT_MS = mDefaultPollTimeoutMs; |
| 65 | deleteTestChain(); |
| 66 | } |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 67 | |
Lorenzo Colitti | 2103b6b | 2017-08-14 11:38:18 +0900 | [diff] [blame^] | 68 | void Init() { |
| 69 | con.Init(); |
| 70 | } |
| 71 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 72 | pid_t getIpRestorePid(const IptablesRestoreController::IptablesProcessType type) { |
| 73 | return con.getIpRestorePid(type); |
| 74 | }; |
| 75 | |
| 76 | void expectNoIptablesRestoreProcess(pid_t pid) { |
| 77 | // We can't readlink /proc/PID/exe, because zombie processes don't have it. |
| 78 | // Parse /proc/PID/stat instead. |
| 79 | std::string statPath = StringPrintf("/proc/%d/stat", pid); |
| 80 | int fd = open(statPath.c_str(), O_RDONLY); |
| 81 | if (fd == -1) { |
| 82 | // ENOENT means the process is gone (expected). |
| 83 | ASSERT_EQ(errno, ENOENT) |
| 84 | << "Unexpected error opening " << statPath << ": " << strerror(errno); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | // If the PID exists, it's possible (though very unlikely) that the PID was reused. Check the |
| 89 | // binary name as well, to ensure the test isn't flaky. |
| 90 | char statBuf[1024]; |
| 91 | ASSERT_NE(-1, read(fd, statBuf, sizeof(statBuf))) |
| 92 | << "Could not read from " << statPath << ": " << strerror(errno); |
| 93 | close(fd); |
| 94 | |
| 95 | std::string statString(statBuf); |
| 96 | EXPECT_FALSE(statString.find("iptables-restor") || statString.find("ip6tables-resto")) |
| 97 | << "Previous iptables-restore pid " << pid << " still alive: " << statString; |
| 98 | } |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 99 | |
| 100 | int createTestChain() { |
| 101 | mChainName = StringPrintf("netd_unit_test_%u", arc4random_uniform(10000)).c_str(); |
| 102 | |
| 103 | // Create a chain to list. |
| 104 | std::vector<std::string> createCommands = { |
| 105 | "*filter", |
| 106 | StringPrintf(":%s -", mChainName.c_str()), |
| 107 | StringPrintf("-A %s -j RETURN", mChainName.c_str()), |
| 108 | "COMMIT", |
| 109 | "" |
| 110 | }; |
| 111 | |
| 112 | int ret = con.execute(V4V6, Join(createCommands, "\n"), nullptr); |
| 113 | if (ret) mChainName = ""; |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | void deleteTestChain() { |
| 118 | std::vector<std::string> deleteCommands = { |
| 119 | "*filter", |
| 120 | StringPrintf(":%s -", mChainName.c_str()), // Flush chain (otherwise we can't delete it). |
| 121 | StringPrintf("-X %s", mChainName.c_str()), // Delete it. |
| 122 | "COMMIT", |
| 123 | "" |
| 124 | }; |
| 125 | con.execute(V4V6, Join(deleteCommands, "\n"), nullptr); |
| 126 | mChainName = ""; |
| 127 | } |
| 128 | |
| 129 | int acquireIptablesLock() { |
Lorenzo Colitti | 2bd804a | 2017-03-10 12:19:08 +0900 | [diff] [blame] | 130 | mIptablesLock = open(XT_LOCK_NAME, O_CREAT, 0600); |
| 131 | if (mIptablesLock == -1) return mIptablesLock; |
| 132 | int attempts; |
| 133 | for (attempts = 0; attempts < XT_LOCK_ATTEMPTS; attempts++) { |
| 134 | if (flock(mIptablesLock, LOCK_EX | LOCK_NB) == 0) { |
| 135 | return 0; |
| 136 | } |
| 137 | usleep(XT_LOCK_POLL_INTERVAL_MS * 1000); |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 138 | } |
Lorenzo Colitti | 2bd804a | 2017-03-10 12:19:08 +0900 | [diff] [blame] | 139 | EXPECT_LT(attempts, XT_LOCK_ATTEMPTS) << |
| 140 | "Could not acquire iptables lock after " << XT_LOCK_ATTEMPTS << " attempts " << |
| 141 | XT_LOCK_POLL_INTERVAL_MS << "ms apart"; |
| 142 | return -1; |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void releaseIptablesLock() { |
| 146 | if (mIptablesLock != -1) { |
| 147 | close(mIptablesLock); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void setRetryParameters(int maxRetries, int pollTimeoutMs) { |
| 152 | con.MAX_RETRIES = maxRetries; |
| 153 | con.POLL_TIMEOUT_MS = pollTimeoutMs; |
| 154 | } |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 157 | TEST_F(IptablesRestoreControllerTest, TestBasicCommand) { |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 158 | std::string output; |
| 159 | |
| 160 | EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", nullptr)); |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 161 | |
| 162 | pid_t pid4 = getIpRestorePid(IptablesRestoreController::IPTABLES_PROCESS); |
| 163 | pid_t pid6 = getIpRestorePid(IptablesRestoreController::IP6TABLES_PROCESS); |
| 164 | |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 165 | EXPECT_EQ(0, con.execute(IptablesTarget::V6, "#Test\n", nullptr)); |
| 166 | EXPECT_EQ(0, con.execute(IptablesTarget::V4, "#Test\n", nullptr)); |
| 167 | |
| 168 | EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", &output)); |
| 169 | EXPECT_EQ("#Test\n#Test\n", output); // One for IPv4 and one for IPv6. |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 170 | |
| 171 | // Check the PIDs are the same as they were before. If they're not, the child processes were |
| 172 | // restarted, which causes a 30-60ms delay. |
| 173 | EXPECT_EQ(pid4, getIpRestorePid(IptablesRestoreController::IPTABLES_PROCESS)); |
| 174 | EXPECT_EQ(pid6, getIpRestorePid(IptablesRestoreController::IP6TABLES_PROCESS)); |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 177 | TEST_F(IptablesRestoreControllerTest, TestRestartOnMalformedCommand) { |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 178 | std::string buffer; |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 179 | for (int i = 0; i < 50; i++) { |
| 180 | IptablesTarget target = (IptablesTarget) (i % 3); |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 181 | std::string *output = (i % 2) ? &buffer : nullptr; |
| 182 | ASSERT_EQ(-1, con.execute(target, "malformed command\n", output)) << |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 183 | "Malformed command did not fail at iteration " << i; |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 184 | ASSERT_EQ(0, con.execute(target, "#Test\n", output)) << |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 185 | "No-op command did not succeed at iteration " << i; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | TEST_F(IptablesRestoreControllerTest, TestRestartOnProcessDeath) { |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 190 | std::string output; |
| 191 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 192 | // Run a command to ensure that the processes are running. |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 193 | EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", &output)); |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 194 | |
| 195 | pid_t pid4 = getIpRestorePid(IptablesRestoreController::IPTABLES_PROCESS); |
| 196 | pid_t pid6 = getIpRestorePid(IptablesRestoreController::IP6TABLES_PROCESS); |
| 197 | |
| 198 | ASSERT_EQ(0, kill(pid4, 0)) << "iptables-restore pid " << pid4 << " does not exist"; |
| 199 | ASSERT_EQ(0, kill(pid6, 0)) << "ip6tables-restore pid " << pid6 << " does not exist"; |
| 200 | ASSERT_EQ(0, kill(pid4, SIGTERM)) << "Failed to send SIGTERM to iptables-restore pid " << pid4; |
| 201 | ASSERT_EQ(0, kill(pid6, SIGTERM)) << "Failed to send SIGTERM to ip6tables-restore pid " << pid6; |
| 202 | |
| 203 | // Wait 100ms for processes to terminate. |
| 204 | TEMP_FAILURE_RETRY(usleep(100 * 1000)); |
| 205 | |
| 206 | // Ensure that running a new command properly restarts the processes. |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 207 | EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", nullptr)); |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 208 | EXPECT_NE(pid4, getIpRestorePid(IptablesRestoreController::IPTABLES_PROCESS)); |
| 209 | EXPECT_NE(pid6, getIpRestorePid(IptablesRestoreController::IP6TABLES_PROCESS)); |
| 210 | |
| 211 | // Check there are no zombies. |
| 212 | expectNoIptablesRestoreProcess(pid4); |
| 213 | expectNoIptablesRestoreProcess(pid6); |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 214 | } |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 215 | |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 216 | TEST_F(IptablesRestoreControllerTest, TestCommandTimeout) { |
| 217 | // Don't wait 10 seconds for this test to fail. |
| 218 | setRetryParameters(3, 50); |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 219 | |
| 220 | // Expected contents of the chain. |
| 221 | std::vector<std::string> expectedLines = { |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 222 | StringPrintf("Chain %s (0 references)", mChainName.c_str()), |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 223 | "target prot opt source destination ", |
| 224 | "RETURN all -- 0.0.0.0/0 0.0.0.0/0 ", |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 225 | StringPrintf("Chain %s (0 references)", mChainName.c_str()), |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 226 | "target prot opt source destination ", |
| 227 | "RETURN all ::/0 ::/0 ", |
| 228 | "" |
| 229 | }; |
| 230 | std::string expected = Join(expectedLines, "\n"); |
| 231 | |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 232 | std::vector<std::string> listCommands = { |
| 233 | "*filter", |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 234 | StringPrintf("-n -L %s", mChainName.c_str()), // List chain. |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 235 | "COMMIT", |
| 236 | "" |
| 237 | }; |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 238 | std::string commandString = Join(listCommands, "\n"); |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 239 | std::string output; |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 240 | |
| 241 | EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, commandString, &output)); |
| 242 | EXPECT_EQ(expected, output); |
| 243 | |
| 244 | ASSERT_EQ(0, acquireIptablesLock()); |
| 245 | EXPECT_EQ(-1, con.execute(IptablesTarget::V4V6, commandString, &output)); |
| 246 | EXPECT_EQ(-1, con.execute(IptablesTarget::V4V6, commandString, &output)); |
| 247 | releaseIptablesLock(); |
| 248 | |
| 249 | EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, commandString, &output)); |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 250 | EXPECT_EQ(expected, output); |
| 251 | } |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 252 | |
| 253 | TEST_F(IptablesRestoreControllerTest, TestUidRuleBenchmark) { |
| 254 | const std::vector<int> ITERATIONS = { 1, 5, 10 }; |
| 255 | |
| 256 | const std::string IPTABLES_RESTORE_ADD = |
| 257 | "*filter\n-I fw_powersave -m owner --uid-owner 2000000000 -j RETURN\nCOMMIT\n"; |
| 258 | const std::string IPTABLES_RESTORE_DEL = |
| 259 | "*filter\n-D fw_powersave -m owner --uid-owner 2000000000 -j RETURN\nCOMMIT\n"; |
| 260 | |
| 261 | for (const int iterations : ITERATIONS) { |
| 262 | Stopwatch s; |
| 263 | for (int i = 0; i < iterations; i++) { |
| 264 | EXPECT_EQ(0, con.execute(V4V6, IPTABLES_RESTORE_ADD, nullptr)); |
| 265 | EXPECT_EQ(0, con.execute(V4V6, IPTABLES_RESTORE_DEL, nullptr)); |
| 266 | } |
| 267 | float timeTaken = s.getTimeAndReset(); |
| 268 | fprintf(stderr, " Add/del %d UID rules via restore: %.1fms (%.2fms per operation)\n", |
| 269 | iterations, timeTaken, timeTaken / 2 / iterations); |
| 270 | |
| 271 | for (int i = 0; i < iterations; i++) { |
| 272 | EXPECT_EQ(0, execIptables(V4V6, "-I", "fw_powersave", "-m", "owner", |
| 273 | "--uid-owner", "2000000000", "-j", "RETURN", nullptr)); |
| 274 | EXPECT_EQ(0, execIptables(V4V6, "-D", "fw_powersave", "-m", "owner", |
| 275 | "--uid-owner", "2000000000", "-j", "RETURN", nullptr)); |
| 276 | } |
| 277 | timeTaken = s.getTimeAndReset(); |
| 278 | fprintf(stderr, " Add/del %d UID rules via iptables: %.1fms (%.2fms per operation)\n", |
| 279 | iterations, timeTaken, timeTaken / 2 / iterations); |
| 280 | } |
| 281 | } |
Lorenzo Colitti | 2103b6b | 2017-08-14 11:38:18 +0900 | [diff] [blame^] | 282 | |
| 283 | TEST_F(IptablesRestoreControllerTest, TestStartup) { |
| 284 | // Tests that IptablesRestoreController::Init never sets its processes to null pointers if |
| 285 | // fork() succeeds. |
| 286 | { |
| 287 | // Mock fork(), and check that initializing 100 times never results in a null pointer. |
| 288 | constexpr int NUM_ITERATIONS = 100; // Takes 100-150ms on angler. |
| 289 | constexpr pid_t FAKE_PID = 2000000001; |
| 290 | StrictMock<ScopedMockSyscalls> sys; |
| 291 | |
| 292 | EXPECT_CALL(sys, fork()).Times(NUM_ITERATIONS * 2).WillRepeatedly(Return(FAKE_PID)); |
| 293 | for (int i = 0; i < NUM_ITERATIONS; i++) { |
| 294 | Init(); |
| 295 | EXPECT_NE(0, getIpRestorePid(IptablesRestoreController::IPTABLES_PROCESS)); |
| 296 | EXPECT_NE(0, getIpRestorePid(IptablesRestoreController::IP6TABLES_PROCESS)); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // The controller is now in an invalid state: the pipes are connected to working iptables |
| 301 | // processes, but the PIDs are set to FAKE_PID. Send a malformed command to ensure that the |
| 302 | // processes terminate and close the pipes, then send a valid command to have the controller |
| 303 | // re-initialize properly now that fork() is no longer mocked. |
| 304 | EXPECT_EQ(-1, con.execute(V4V6, "malformed command\n", nullptr)); |
| 305 | EXPECT_EQ(0, con.execute(V4V6, "#Test\n", nullptr)); |
| 306 | } |