Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | * binder_test.cpp - unit tests for netd binder RPCs. |
| 17 | */ |
| 18 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 19 | #include <cerrno> |
| 20 | #include <cinttypes> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 21 | #include <cstdint> |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 22 | #include <cstdio> |
| 23 | #include <cstdlib> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 24 | #include <set> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <netdb.h> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 29 | #include <sys/socket.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 30 | #include <sys/types.h> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 31 | #include <netinet/in.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 32 | #include <linux/if.h> |
| 33 | #include <linux/if_tun.h> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 34 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 35 | #include <android-base/stringprintf.h> |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 36 | #include <android-base/strings.h> |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 37 | #include <cutils/multiuser.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 38 | #include <gtest/gtest.h> |
| 39 | #include <logwrap/logwrap.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 40 | #include <netutils/ifc.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 41 | |
| 42 | #include "NetdConstants.h" |
| 43 | #include "android/net/INetd.h" |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 44 | #include "android/net/UidRange.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 45 | #include "binder/IServiceManager.h" |
| 46 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 47 | #define TUN_DEV "/dev/tun" |
| 48 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 49 | using namespace android; |
| 50 | using namespace android::base; |
| 51 | using namespace android::binder; |
| 52 | using android::net::INetd; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 53 | using android::net::UidRange; |
| 54 | |
| 55 | static const char* IP_RULE_V4 = "-4"; |
| 56 | static const char* IP_RULE_V6 = "-6"; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 57 | |
| 58 | class BinderTest : public ::testing::Test { |
| 59 | |
| 60 | public: |
| 61 | BinderTest() { |
| 62 | sp<IServiceManager> sm = defaultServiceManager(); |
| 63 | sp<IBinder> binder = sm->getService(String16("netd")); |
| 64 | if (binder != nullptr) { |
| 65 | mNetd = interface_cast<INetd>(binder); |
| 66 | } |
| 67 | } |
| 68 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 69 | void SetUp() override { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 70 | ASSERT_NE(nullptr, mNetd.get()); |
| 71 | } |
| 72 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 73 | // Static because setting up the tun interface takes about 40ms. |
| 74 | static void SetUpTestCase() { |
| 75 | sTunFd = createTunInterface(); |
| 76 | ASSERT_NE(-1, sTunFd); |
| 77 | } |
| 78 | |
| 79 | static void TearDownTestCase() { |
| 80 | // Closing the socket removes the interface and IP addresses. |
| 81 | close(sTunFd); |
| 82 | } |
| 83 | |
| 84 | static void fakeRemoteSocketPair(int *clientSocket, int *serverSocket, int *acceptedSocket); |
| 85 | static int createTunInterface(); |
| 86 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 87 | protected: |
| 88 | sp<INetd> mNetd; |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 89 | static int sTunFd; |
| 90 | static in6_addr sSrcAddr, sDstAddr; |
| 91 | static char sSrcStr[], sDstStr[]; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 92 | }; |
| 93 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 94 | int BinderTest::sTunFd; |
| 95 | in6_addr BinderTest::sSrcAddr; |
| 96 | in6_addr BinderTest::sDstAddr; |
| 97 | char BinderTest::sSrcStr[INET6_ADDRSTRLEN]; |
| 98 | char BinderTest::sDstStr[INET6_ADDRSTRLEN]; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 99 | |
Lorenzo Colitti | 699aa99 | 2016-04-15 10:22:37 +0900 | [diff] [blame] | 100 | class TimedOperation : public Stopwatch { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 101 | public: |
Lorenzo Colitti | 699aa99 | 2016-04-15 10:22:37 +0900 | [diff] [blame] | 102 | TimedOperation(std::string name): mName(name) {} |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 103 | virtual ~TimedOperation() { |
Lorenzo Colitti | 699aa99 | 2016-04-15 10:22:37 +0900 | [diff] [blame] | 104 | fprintf(stderr, " %s: %6.1f ms\n", mName.c_str(), timeTaken()); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private: |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 108 | std::string mName; |
| 109 | }; |
| 110 | |
| 111 | TEST_F(BinderTest, TestIsAlive) { |
| 112 | TimedOperation t("isAlive RPC"); |
| 113 | bool isAlive = false; |
| 114 | mNetd->isAlive(&isAlive); |
| 115 | ASSERT_TRUE(isAlive); |
| 116 | } |
| 117 | |
| 118 | static int randomUid() { |
| 119 | return 100000 * arc4random_uniform(7) + 10000 + arc4random_uniform(5000); |
| 120 | } |
| 121 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 122 | static std::vector<std::string> runCommand(const std::string& command) { |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 123 | std::vector<std::string> lines; |
| 124 | FILE *f; |
| 125 | |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 126 | if ((f = popen(command.c_str(), "r")) == nullptr) { |
| 127 | perror("popen"); |
| 128 | return lines; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 129 | } |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 130 | |
| 131 | char *line = nullptr; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 132 | size_t bufsize = 0; |
| 133 | ssize_t linelen = 0; |
| 134 | while ((linelen = getline(&line, &bufsize, f)) >= 0) { |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 135 | lines.push_back(std::string(line, linelen)); |
| 136 | free(line); |
| 137 | line = nullptr; |
| 138 | } |
| 139 | |
| 140 | pclose(f); |
| 141 | return lines; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 142 | } |
| 143 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 144 | static std::vector<std::string> listIpRules(const char *ipVersion) { |
| 145 | std::string command = StringPrintf("%s %s rule list", IP_PATH, ipVersion); |
| 146 | return runCommand(command); |
| 147 | } |
| 148 | |
| 149 | static std::vector<std::string> listIptablesRule(const char *binary, const char *chainName) { |
Lorenzo Colitti | 8054577 | 2016-06-09 14:20:08 +0900 | [diff] [blame] | 150 | std::string command = StringPrintf("%s -w -n -L %s", binary, chainName); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 151 | return runCommand(command); |
| 152 | } |
| 153 | |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 154 | static int iptablesRuleLineLength(const char *binary, const char *chainName) { |
| 155 | return listIptablesRule(binary, chainName).size(); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 156 | } |
| 157 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 158 | TEST_F(BinderTest, TestFirewallReplaceUidChain) { |
| 159 | std::string chainName = StringPrintf("netd_binder_test_%u", arc4random_uniform(10000)); |
| 160 | const int kNumUids = 500; |
| 161 | std::vector<int32_t> noUids(0); |
| 162 | std::vector<int32_t> uids(kNumUids); |
| 163 | for (int i = 0; i < kNumUids; i++) { |
| 164 | uids[i] = randomUid(); |
| 165 | } |
| 166 | |
| 167 | bool ret; |
| 168 | { |
| 169 | TimedOperation op(StringPrintf("Programming %d-UID whitelist chain", kNumUids)); |
| 170 | mNetd->firewallReplaceUidChain(String16(chainName.c_str()), true, uids, &ret); |
| 171 | } |
| 172 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 173 | EXPECT_EQ((int) uids.size() + 5, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 174 | EXPECT_EQ((int) uids.size() + 11, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 175 | { |
| 176 | TimedOperation op("Clearing whitelist chain"); |
| 177 | mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, noUids, &ret); |
| 178 | } |
| 179 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 180 | EXPECT_EQ(3, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 181 | EXPECT_EQ(3, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 182 | |
| 183 | { |
| 184 | TimedOperation op(StringPrintf("Programming %d-UID blacklist chain", kNumUids)); |
| 185 | mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, uids, &ret); |
| 186 | } |
| 187 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 188 | EXPECT_EQ((int) uids.size() + 3, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 189 | EXPECT_EQ((int) uids.size() + 3, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 190 | |
| 191 | { |
| 192 | TimedOperation op("Clearing blacklist chain"); |
| 193 | mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, noUids, &ret); |
| 194 | } |
| 195 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 196 | EXPECT_EQ(3, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 197 | EXPECT_EQ(3, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 198 | |
| 199 | // Check that the call fails if iptables returns an error. |
| 200 | std::string veryLongStringName = "netd_binder_test_UnacceptablyLongIptablesChainName"; |
| 201 | mNetd->firewallReplaceUidChain(String16(veryLongStringName.c_str()), true, noUids, &ret); |
| 202 | EXPECT_EQ(false, ret); |
| 203 | } |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 204 | |
| 205 | static int bandwidthDataSaverEnabled(const char *binary) { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 206 | std::vector<std::string> lines = listIptablesRule(binary, "bw_data_saver"); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 207 | |
| 208 | // Output looks like this: |
| 209 | // |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 210 | // Chain bw_data_saver (1 references) |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 211 | // target prot opt source destination |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 212 | // RETURN all -- 0.0.0.0/0 0.0.0.0/0 |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 213 | EXPECT_EQ(3U, lines.size()); |
| 214 | if (lines.size() != 3) return -1; |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 215 | |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 216 | EXPECT_TRUE(android::base::StartsWith(lines[2], "RETURN ") || |
| 217 | android::base::StartsWith(lines[2], "REJECT ")); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 218 | |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 219 | return android::base::StartsWith(lines[2], "REJECT"); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | bool enableDataSaver(sp<INetd>& netd, bool enable) { |
| 223 | TimedOperation op(enable ? " Enabling data saver" : "Disabling data saver"); |
| 224 | bool ret; |
| 225 | netd->bandwidthEnableDataSaver(enable, &ret); |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | int getDataSaverState() { |
| 230 | const int enabled4 = bandwidthDataSaverEnabled(IPTABLES_PATH); |
| 231 | const int enabled6 = bandwidthDataSaverEnabled(IP6TABLES_PATH); |
| 232 | EXPECT_EQ(enabled4, enabled6); |
| 233 | EXPECT_NE(-1, enabled4); |
| 234 | EXPECT_NE(-1, enabled6); |
| 235 | if (enabled4 != enabled6 || (enabled6 != 0 && enabled6 != 1)) { |
| 236 | return -1; |
| 237 | } |
| 238 | return enabled6; |
| 239 | } |
| 240 | |
| 241 | TEST_F(BinderTest, TestBandwidthEnableDataSaver) { |
| 242 | const int wasEnabled = getDataSaverState(); |
| 243 | ASSERT_NE(-1, wasEnabled); |
| 244 | |
| 245 | if (wasEnabled) { |
| 246 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 247 | EXPECT_EQ(0, getDataSaverState()); |
| 248 | } |
| 249 | |
| 250 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 251 | EXPECT_EQ(0, getDataSaverState()); |
| 252 | |
| 253 | ASSERT_TRUE(enableDataSaver(mNetd, true)); |
| 254 | EXPECT_EQ(1, getDataSaverState()); |
| 255 | |
| 256 | ASSERT_TRUE(enableDataSaver(mNetd, true)); |
| 257 | EXPECT_EQ(1, getDataSaverState()); |
| 258 | |
| 259 | if (!wasEnabled) { |
| 260 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 261 | EXPECT_EQ(0, getDataSaverState()); |
| 262 | } |
| 263 | } |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 264 | |
| 265 | static bool ipRuleExistsForRange(const uint32_t priority, const UidRange& range, |
| 266 | const std::string& action, const char* ipVersion) { |
| 267 | // Output looks like this: |
Robin Lee | 6c84ef6 | 2016-05-03 13:17:58 +0100 | [diff] [blame] | 268 | // "12500:\tfrom all fwmark 0x0/0x20000 iif lo uidrange 1000-2000 prohibit" |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 269 | std::vector<std::string> rules = listIpRules(ipVersion); |
| 270 | |
| 271 | std::string prefix = StringPrintf("%" PRIu32 ":", priority); |
| 272 | std::string suffix = StringPrintf(" iif lo uidrange %d-%d %s\n", |
| 273 | range.getStart(), range.getStop(), action.c_str()); |
| 274 | for (std::string line : rules) { |
| 275 | if (android::base::StartsWith(line, prefix.c_str()) |
| 276 | && android::base::EndsWith(line, suffix.c_str())) { |
| 277 | return true; |
| 278 | } |
| 279 | } |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | static bool ipRuleExistsForRange(const uint32_t priority, const UidRange& range, |
| 284 | const std::string& action) { |
| 285 | bool existsIp4 = ipRuleExistsForRange(priority, range, action, IP_RULE_V4); |
| 286 | bool existsIp6 = ipRuleExistsForRange(priority, range, action, IP_RULE_V6); |
| 287 | EXPECT_EQ(existsIp4, existsIp6); |
| 288 | return existsIp4; |
| 289 | } |
| 290 | |
| 291 | TEST_F(BinderTest, TestNetworkRejectNonSecureVpn) { |
Robin Lee | 6c84ef6 | 2016-05-03 13:17:58 +0100 | [diff] [blame] | 292 | constexpr uint32_t RULE_PRIORITY = 12500; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 293 | |
| 294 | constexpr int baseUid = MULTIUSER_APP_PER_USER_RANGE * 5; |
| 295 | std::vector<UidRange> uidRanges = { |
| 296 | {baseUid + 150, baseUid + 224}, |
| 297 | {baseUid + 226, baseUid + 300} |
| 298 | }; |
| 299 | |
| 300 | const std::vector<std::string> initialRulesV4 = listIpRules(IP_RULE_V4); |
| 301 | const std::vector<std::string> initialRulesV6 = listIpRules(IP_RULE_V6); |
| 302 | |
| 303 | // Create two valid rules. |
| 304 | ASSERT_TRUE(mNetd->networkRejectNonSecureVpn(true, uidRanges).isOk()); |
| 305 | EXPECT_EQ(initialRulesV4.size() + 2, listIpRules(IP_RULE_V4).size()); |
| 306 | EXPECT_EQ(initialRulesV6.size() + 2, listIpRules(IP_RULE_V6).size()); |
| 307 | for (auto const& range : uidRanges) { |
| 308 | EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY, range, "prohibit")); |
| 309 | } |
| 310 | |
| 311 | // Remove the rules. |
| 312 | ASSERT_TRUE(mNetd->networkRejectNonSecureVpn(false, uidRanges).isOk()); |
| 313 | EXPECT_EQ(initialRulesV4.size(), listIpRules(IP_RULE_V4).size()); |
| 314 | EXPECT_EQ(initialRulesV6.size(), listIpRules(IP_RULE_V6).size()); |
| 315 | for (auto const& range : uidRanges) { |
| 316 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY, range, "prohibit")); |
| 317 | } |
| 318 | |
| 319 | // Fail to remove the rules a second time after they are already deleted. |
| 320 | binder::Status status = mNetd->networkRejectNonSecureVpn(false, uidRanges); |
| 321 | ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 322 | EXPECT_EQ(ENOENT, status.serviceSpecificErrorCode()); |
| 323 | |
| 324 | // All rules should be the same as before. |
| 325 | EXPECT_EQ(initialRulesV4, listIpRules(IP_RULE_V4)); |
| 326 | EXPECT_EQ(initialRulesV6, listIpRules(IP_RULE_V6)); |
| 327 | } |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 328 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 329 | int BinderTest::createTunInterface() { |
| 330 | // Generate a random ULA address pair. |
| 331 | arc4random_buf(&sSrcAddr, sizeof(sSrcAddr)); |
| 332 | sSrcAddr.s6_addr[0] = 0xfd; |
| 333 | memcpy(&sDstAddr, &sSrcAddr, sizeof(sDstAddr)); |
| 334 | sDstAddr.s6_addr[15] ^= 1; |
| 335 | |
| 336 | // Convert the addresses to strings because that's what ifc_add_address takes. |
| 337 | sockaddr_in6 src6 = { .sin6_family = AF_INET6, .sin6_addr = sSrcAddr, }; |
| 338 | sockaddr_in6 dst6 = { .sin6_family = AF_INET6, .sin6_addr = sDstAddr, }; |
| 339 | int flags = NI_NUMERICHOST; |
| 340 | if (getnameinfo((sockaddr *) &src6, sizeof(src6), sSrcStr, sizeof(sSrcStr), NULL, 0, flags) || |
| 341 | getnameinfo((sockaddr *) &dst6, sizeof(dst6), sDstStr, sizeof(sDstStr), NULL, 0, flags)) { |
| 342 | return -1; |
| 343 | } |
| 344 | |
| 345 | // Create a tun interface with a name based on our PID. |
| 346 | struct ifreq ifr = { |
| 347 | .ifr_ifru = { .ifru_flags = IFF_TUN }, |
| 348 | }; |
| 349 | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "netdtest%u", getpid()); |
| 350 | |
| 351 | int fd = open(TUN_DEV, O_RDWR | O_NONBLOCK | O_CLOEXEC); |
| 352 | EXPECT_NE(-1, fd) << TUN_DEV << ": " << strerror(errno); |
| 353 | if (fd == -1) return fd; |
| 354 | |
| 355 | int ret = ioctl(fd, TUNSETIFF, &ifr, sizeof(ifr)); |
| 356 | EXPECT_EQ(0, ret) << "TUNSETIFF: " << strerror(errno); |
| 357 | if (ret) { |
| 358 | close(fd); |
| 359 | return -1; |
| 360 | } |
| 361 | |
| 362 | if (ifc_add_address(ifr.ifr_name, sSrcStr, 64) || |
| 363 | ifc_add_address(ifr.ifr_name, sDstStr, 64)) { |
| 364 | close(fd); |
| 365 | return -1; |
| 366 | } |
| 367 | return fd; |
| 368 | } |
| 369 | |
| 370 | // Create a socket pair that isLoopbackSocket won't think is local. |
| 371 | void BinderTest::fakeRemoteSocketPair(int *clientSocket, int *serverSocket, int *acceptedSocket) { |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 372 | *serverSocket = socket(AF_INET6, SOCK_STREAM, 0); |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 373 | struct sockaddr_in6 server6 = { .sin6_family = AF_INET6, .sin6_addr = sDstAddr }; |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 374 | ASSERT_EQ(0, bind(*serverSocket, (struct sockaddr *) &server6, sizeof(server6))); |
| 375 | |
| 376 | socklen_t addrlen = sizeof(server6); |
| 377 | ASSERT_EQ(0, getsockname(*serverSocket, (struct sockaddr *) &server6, &addrlen)); |
| 378 | ASSERT_EQ(0, listen(*serverSocket, 10)); |
| 379 | |
| 380 | *clientSocket = socket(AF_INET6, SOCK_STREAM, 0); |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 381 | struct sockaddr_in6 client6 = { .sin6_family = AF_INET6, .sin6_addr = sSrcAddr }; |
| 382 | ASSERT_EQ(0, bind(*clientSocket, (struct sockaddr *) &client6, sizeof(client6))); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 383 | ASSERT_EQ(0, connect(*clientSocket, (struct sockaddr *) &server6, sizeof(server6))); |
| 384 | ASSERT_EQ(0, getsockname(*clientSocket, (struct sockaddr *) &client6, &addrlen)); |
| 385 | |
| 386 | *acceptedSocket = accept(*serverSocket, (struct sockaddr *) &server6, &addrlen); |
| 387 | ASSERT_NE(-1, *acceptedSocket); |
| 388 | |
| 389 | ASSERT_EQ(0, memcmp(&client6, &server6, sizeof(client6))); |
| 390 | } |
| 391 | |
| 392 | void checkSocketpairOpen(int clientSocket, int acceptedSocket) { |
| 393 | char buf[4096]; |
| 394 | EXPECT_EQ(4, write(clientSocket, "foo", sizeof("foo"))); |
| 395 | EXPECT_EQ(4, read(acceptedSocket, buf, sizeof(buf))); |
| 396 | EXPECT_EQ(0, memcmp(buf, "foo", sizeof("foo"))); |
| 397 | } |
| 398 | |
| 399 | void checkSocketpairClosed(int clientSocket, int acceptedSocket) { |
| 400 | // Check that the client socket was closed with ECONNABORTED. |
| 401 | int ret = write(clientSocket, "foo", sizeof("foo")); |
| 402 | int err = errno; |
| 403 | EXPECT_EQ(-1, ret); |
| 404 | EXPECT_EQ(ECONNABORTED, err); |
| 405 | |
| 406 | // Check that it sent a RST to the server. |
| 407 | ret = write(acceptedSocket, "foo", sizeof("foo")); |
| 408 | err = errno; |
| 409 | EXPECT_EQ(-1, ret); |
| 410 | EXPECT_EQ(ECONNRESET, err); |
| 411 | } |
| 412 | |
| 413 | TEST_F(BinderTest, TestSocketDestroy) { |
| 414 | int clientSocket, serverSocket, acceptedSocket; |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 415 | ASSERT_NO_FATAL_FAILURE(fakeRemoteSocketPair(&clientSocket, &serverSocket, &acceptedSocket)); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 416 | |
| 417 | // Pick a random UID in the system UID range. |
| 418 | constexpr int baseUid = AID_APP - 2000; |
| 419 | static_assert(baseUid > 0, "Not enough UIDs? Please fix this test."); |
| 420 | int uid = baseUid + 500 + arc4random_uniform(1000); |
| 421 | EXPECT_EQ(0, fchown(clientSocket, uid, -1)); |
| 422 | |
| 423 | // UID ranges that don't contain uid. |
| 424 | std::vector<UidRange> uidRanges = { |
| 425 | {baseUid + 42, baseUid + 449}, |
| 426 | {baseUid + 1536, AID_APP - 4}, |
| 427 | {baseUid + 498, uid - 1}, |
| 428 | {uid + 1, baseUid + 1520}, |
| 429 | }; |
| 430 | // A skip list that doesn't contain UID. |
| 431 | std::vector<int32_t> skipUids { baseUid + 123, baseUid + 1600 }; |
| 432 | |
| 433 | // Close sockets. Our test socket should be intact. |
| 434 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 435 | checkSocketpairOpen(clientSocket, acceptedSocket); |
| 436 | |
| 437 | // UID ranges that do contain uid. |
| 438 | uidRanges = { |
| 439 | {baseUid + 42, baseUid + 449}, |
| 440 | {baseUid + 1536, AID_APP - 4}, |
| 441 | {baseUid + 498, baseUid + 1520}, |
| 442 | }; |
| 443 | // Add uid to the skip list. |
| 444 | skipUids.push_back(uid); |
| 445 | |
| 446 | // Close sockets. Our test socket should still be intact because it's in the skip list. |
| 447 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 448 | checkSocketpairOpen(clientSocket, acceptedSocket); |
| 449 | |
| 450 | // Now remove uid from skipUids, and close sockets. Our test socket should have been closed. |
| 451 | skipUids.resize(skipUids.size() - 1); |
| 452 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 453 | checkSocketpairClosed(clientSocket, acceptedSocket); |
| 454 | |
| 455 | close(clientSocket); |
| 456 | close(serverSocket); |
| 457 | close(acceptedSocket); |
| 458 | } |