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 | |
| 23 | #include <gtest/gtest.h> |
| 24 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 25 | #define LOG_TAG "IptablesRestoreControllerTest" |
| 26 | #include <cutils/log.h> |
| 27 | #include <android-base/stringprintf.h> |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 28 | #include <android-base/strings.h> |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 29 | |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 30 | #include "IptablesRestoreController.h" |
| 31 | #include "NetdConstants.h" |
| 32 | |
Lorenzo Colitti | 2bd804a | 2017-03-10 12:19:08 +0900 | [diff] [blame] | 33 | #define XT_LOCK_NAME "/system/etc/xtables.lock" |
| 34 | #define XT_LOCK_ATTEMPTS 10 |
| 35 | #define XT_LOCK_POLL_INTERVAL_MS 100 |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 36 | |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 37 | using android::base::Join; |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 38 | using android::base::StringPrintf; |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 39 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 40 | class IptablesRestoreControllerTest : public ::testing::Test { |
| 41 | public: |
| 42 | IptablesRestoreController con; |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 43 | int mDefaultMaxRetries = con.MAX_RETRIES; |
| 44 | int mDefaultPollTimeoutMs = con.POLL_TIMEOUT_MS; |
| 45 | int mIptablesLock = -1; |
| 46 | std::string mChainName; |
| 47 | |
Lorenzo Colitti | 839d7d6 | 2017-04-03 15:37:19 +0900 | [diff] [blame^] | 48 | static void SetUpTestCase() { |
| 49 | blockSigpipe(); |
| 50 | } |
| 51 | |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 52 | 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 Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 61 | |
| 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 Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 89 | |
| 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 Colitti | 2bd804a | 2017-03-10 12:19:08 +0900 | [diff] [blame] | 120 | 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 Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 128 | } |
Lorenzo Colitti | 2bd804a | 2017-03-10 12:19:08 +0900 | [diff] [blame] | 129 | 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 Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 133 | } |
| 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 Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 147 | TEST_F(IptablesRestoreControllerTest, TestBasicCommand) { |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 148 | std::string output; |
| 149 | |
| 150 | EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", nullptr)); |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 151 | |
| 152 | pid_t pid4 = getIpRestorePid(IptablesRestoreController::IPTABLES_PROCESS); |
| 153 | pid_t pid6 = getIpRestorePid(IptablesRestoreController::IP6TABLES_PROCESS); |
| 154 | |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 155 | 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 Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 160 | |
| 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 Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 167 | TEST_F(IptablesRestoreControllerTest, TestRestartOnMalformedCommand) { |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 168 | std::string buffer; |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 169 | for (int i = 0; i < 50; i++) { |
| 170 | IptablesTarget target = (IptablesTarget) (i % 3); |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 171 | std::string *output = (i % 2) ? &buffer : nullptr; |
| 172 | ASSERT_EQ(-1, con.execute(target, "malformed command\n", output)) << |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 173 | "Malformed command did not fail at iteration " << i; |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 174 | ASSERT_EQ(0, con.execute(target, "#Test\n", output)) << |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 175 | "No-op command did not succeed at iteration " << i; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | TEST_F(IptablesRestoreControllerTest, TestRestartOnProcessDeath) { |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 180 | std::string output; |
| 181 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 182 | // Run a command to ensure that the processes are running. |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 183 | EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", &output)); |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 184 | |
| 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 Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 197 | EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", nullptr)); |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 198 | 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 Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 204 | } |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 205 | |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 206 | TEST_F(IptablesRestoreControllerTest, TestCommandTimeout) { |
| 207 | // Don't wait 10 seconds for this test to fail. |
| 208 | setRetryParameters(3, 50); |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 209 | |
| 210 | // Expected contents of the chain. |
| 211 | std::vector<std::string> expectedLines = { |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 212 | StringPrintf("Chain %s (0 references)", mChainName.c_str()), |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 213 | "target prot opt source destination ", |
| 214 | "RETURN all -- 0.0.0.0/0 0.0.0.0/0 ", |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 215 | StringPrintf("Chain %s (0 references)", mChainName.c_str()), |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 216 | "target prot opt source destination ", |
| 217 | "RETURN all ::/0 ::/0 ", |
| 218 | "" |
| 219 | }; |
| 220 | std::string expected = Join(expectedLines, "\n"); |
| 221 | |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 222 | std::vector<std::string> listCommands = { |
| 223 | "*filter", |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 224 | StringPrintf("-n -L %s", mChainName.c_str()), // List chain. |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 225 | "COMMIT", |
| 226 | "" |
| 227 | }; |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 228 | std::string commandString = Join(listCommands, "\n"); |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 229 | std::string output; |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 230 | |
| 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 Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 240 | EXPECT_EQ(expected, output); |
| 241 | } |