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> |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 28 | #include <ifaddrs.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 29 | #include <netdb.h> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 30 | #include <sys/socket.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 31 | #include <sys/types.h> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 32 | #include <netinet/in.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 33 | #include <linux/if.h> |
| 34 | #include <linux/if_tun.h> |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 35 | #include <openssl/base64.h> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 36 | |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 37 | #include <android-base/macros.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 38 | #include <android-base/stringprintf.h> |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 39 | #include <android-base/strings.h> |
Chenbo Feng | 837ddfc | 2018-05-08 13:45:08 -0700 | [diff] [blame] | 40 | #include <bpf/BpfUtils.h> |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 41 | #include <cutils/multiuser.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 42 | #include <gtest/gtest.h> |
| 43 | #include <logwrap/logwrap.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 44 | #include <netutils/ifc.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 45 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 46 | #include "InterfaceController.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 47 | #include "NetdConstants.h" |
Robin Lee | 7e05cc9 | 2016-09-21 16:31:33 +0900 | [diff] [blame] | 48 | #include "Stopwatch.h" |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 49 | #include "XfrmController.h" |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 50 | #include "tun_interface.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 51 | #include "android/net/INetd.h" |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 52 | #include "android/net/UidRange.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 53 | #include "binder/IServiceManager.h" |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 54 | #include "netdutils/Syscalls.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 55 | |
Lorenzo Colitti | 5c68b9c | 2017-08-10 18:50:10 +0900 | [diff] [blame] | 56 | #define IP_PATH "/system/bin/ip" |
| 57 | #define IP6TABLES_PATH "/system/bin/ip6tables" |
| 58 | #define IPTABLES_PATH "/system/bin/iptables" |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 59 | #define TUN_DEV "/dev/tun" |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 60 | #define RAW_TABLE "raw" |
| 61 | #define MANGLE_TABLE "mangle" |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 62 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 63 | namespace binder = android::binder; |
| 64 | namespace netdutils = android::netdutils; |
| 65 | |
| 66 | using android::IBinder; |
| 67 | using android::IServiceManager; |
| 68 | using android::sp; |
| 69 | using android::String16; |
| 70 | using android::String8; |
| 71 | using android::base::Join; |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 72 | using android::base::StartsWith; |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 73 | using android::base::StringPrintf; |
Chenbo Feng | 837ddfc | 2018-05-08 13:45:08 -0700 | [diff] [blame] | 74 | using android::bpf::hasBpfSupport; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 75 | using android::net::INetd; |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 76 | using android::net::TunInterface; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 77 | using android::net::UidRange; |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 78 | using android::net::XfrmController; |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 79 | using android::os::PersistableBundle; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 80 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 81 | #define SKIP_IF_BPF_SUPPORTED \ |
| 82 | do { \ |
| 83 | if (hasBpfSupport()) return; \ |
| 84 | } while (0) |
Chenbo Feng | 837ddfc | 2018-05-08 13:45:08 -0700 | [diff] [blame] | 85 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 86 | static const char* IP_RULE_V4 = "-4"; |
| 87 | static const char* IP_RULE_V6 = "-6"; |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 88 | static const int TEST_NETID1 = 65501; |
| 89 | static const int TEST_NETID2 = 65502; |
| 90 | constexpr int BASE_UID = AID_USER_OFFSET * 5; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 91 | |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 92 | static const std::string NO_SOCKET_ALLOW_RULE("! owner UID match 0-4294967294"); |
| 93 | static const std::string ESP_ALLOW_RULE("esp"); |
| 94 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 95 | class BinderTest : public ::testing::Test { |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 96 | public: |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 97 | BinderTest() { |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 98 | sp<IServiceManager> sm = android::defaultServiceManager(); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 99 | sp<IBinder> binder = sm->getService(String16("netd")); |
| 100 | if (binder != nullptr) { |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 101 | mNetd = android::interface_cast<INetd>(binder); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 105 | void SetUp() override { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 106 | ASSERT_NE(nullptr, mNetd.get()); |
| 107 | } |
| 108 | |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 109 | void TearDown() override { |
| 110 | mNetd->networkDestroy(TEST_NETID1); |
| 111 | mNetd->networkDestroy(TEST_NETID2); |
| 112 | } |
| 113 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 114 | bool allocateIpSecResources(bool expectOk, int32_t *spi); |
| 115 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 116 | // Static because setting up the tun interface takes about 40ms. |
| 117 | static void SetUpTestCase() { |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 118 | ASSERT_EQ(0, sTun.init()); |
| 119 | ASSERT_LE(sTun.name().size(), static_cast<size_t>(IFNAMSIZ)); |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static void TearDownTestCase() { |
| 123 | // Closing the socket removes the interface and IP addresses. |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 124 | sTun.destroy(); |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static void fakeRemoteSocketPair(int *clientSocket, int *serverSocket, int *acceptedSocket); |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 128 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 129 | protected: |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 130 | sp<INetd> mNetd; |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 131 | static TunInterface sTun; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 132 | }; |
| 133 | |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 134 | TunInterface BinderTest::sTun; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 135 | |
Lorenzo Colitti | 699aa99 | 2016-04-15 10:22:37 +0900 | [diff] [blame] | 136 | class TimedOperation : public Stopwatch { |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 137 | public: |
Chih-Hung Hsieh | 1805105 | 2016-05-06 10:36:13 -0700 | [diff] [blame] | 138 | explicit TimedOperation(const std::string &name): mName(name) {} |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 139 | virtual ~TimedOperation() { |
Lorenzo Colitti | 699aa99 | 2016-04-15 10:22:37 +0900 | [diff] [blame] | 140 | fprintf(stderr, " %s: %6.1f ms\n", mName.c_str(), timeTaken()); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 141 | } |
| 142 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 143 | private: |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 144 | std::string mName; |
| 145 | }; |
| 146 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 147 | TEST_F(BinderTest, IsAlive) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 148 | TimedOperation t("isAlive RPC"); |
| 149 | bool isAlive = false; |
| 150 | mNetd->isAlive(&isAlive); |
| 151 | ASSERT_TRUE(isAlive); |
| 152 | } |
| 153 | |
| 154 | static int randomUid() { |
| 155 | return 100000 * arc4random_uniform(7) + 10000 + arc4random_uniform(5000); |
| 156 | } |
| 157 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 158 | static std::vector<std::string> runCommand(const std::string& command) { |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 159 | std::vector<std::string> lines; |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 160 | FILE *f = popen(command.c_str(), "r"); // NOLINT(cert-env33-c) |
| 161 | if (f == nullptr) { |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 162 | perror("popen"); |
| 163 | return lines; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 164 | } |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 165 | |
| 166 | char *line = nullptr; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 167 | size_t bufsize = 0; |
| 168 | ssize_t linelen = 0; |
| 169 | while ((linelen = getline(&line, &bufsize, f)) >= 0) { |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 170 | lines.push_back(std::string(line, linelen)); |
| 171 | free(line); |
| 172 | line = nullptr; |
| 173 | } |
| 174 | |
| 175 | pclose(f); |
| 176 | return lines; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 177 | } |
| 178 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 179 | static std::vector<std::string> listIpRules(const char *ipVersion) { |
| 180 | std::string command = StringPrintf("%s %s rule list", IP_PATH, ipVersion); |
| 181 | return runCommand(command); |
| 182 | } |
| 183 | |
| 184 | static std::vector<std::string> listIptablesRule(const char *binary, const char *chainName) { |
Lorenzo Colitti | 8054577 | 2016-06-09 14:20:08 +0900 | [diff] [blame] | 185 | std::string command = StringPrintf("%s -w -n -L %s", binary, chainName); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 186 | return runCommand(command); |
| 187 | } |
| 188 | |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 189 | static int iptablesRuleLineLength(const char *binary, const char *chainName) { |
| 190 | return listIptablesRule(binary, chainName).size(); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 191 | } |
| 192 | |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 193 | static bool iptablesRuleExists(const char *binary, |
| 194 | const char *chainName, |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 195 | const std::string& expectedRule) { |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 196 | std::vector<std::string> rules = listIptablesRule(binary, chainName); |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 197 | for (const auto& rule : rules) { |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 198 | if(rule.find(expectedRule) != std::string::npos) { |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | static bool iptablesNoSocketAllowRuleExists(const char *chainName){ |
| 206 | return iptablesRuleExists(IPTABLES_PATH, chainName, NO_SOCKET_ALLOW_RULE) && |
| 207 | iptablesRuleExists(IP6TABLES_PATH, chainName, NO_SOCKET_ALLOW_RULE); |
| 208 | } |
| 209 | |
| 210 | static bool iptablesEspAllowRuleExists(const char *chainName){ |
| 211 | return iptablesRuleExists(IPTABLES_PATH, chainName, ESP_ALLOW_RULE) && |
| 212 | iptablesRuleExists(IP6TABLES_PATH, chainName, ESP_ALLOW_RULE); |
| 213 | } |
| 214 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 215 | TEST_F(BinderTest, FirewallReplaceUidChain) { |
Chenbo Feng | 837ddfc | 2018-05-08 13:45:08 -0700 | [diff] [blame] | 216 | SKIP_IF_BPF_SUPPORTED; |
| 217 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 218 | std::string chainName = StringPrintf("netd_binder_test_%u", arc4random_uniform(10000)); |
| 219 | const int kNumUids = 500; |
| 220 | std::vector<int32_t> noUids(0); |
| 221 | std::vector<int32_t> uids(kNumUids); |
| 222 | for (int i = 0; i < kNumUids; i++) { |
| 223 | uids[i] = randomUid(); |
| 224 | } |
| 225 | |
| 226 | bool ret; |
| 227 | { |
| 228 | TimedOperation op(StringPrintf("Programming %d-UID whitelist chain", kNumUids)); |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 229 | mNetd->firewallReplaceUidChain(chainName, true, uids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 230 | } |
| 231 | EXPECT_EQ(true, ret); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 232 | EXPECT_EQ((int) uids.size() + 9, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 233 | EXPECT_EQ((int) uids.size() + 15, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
| 234 | EXPECT_EQ(true, iptablesNoSocketAllowRuleExists(chainName.c_str())); |
| 235 | EXPECT_EQ(true, iptablesEspAllowRuleExists(chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 236 | { |
| 237 | TimedOperation op("Clearing whitelist chain"); |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 238 | mNetd->firewallReplaceUidChain(chainName, false, noUids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 239 | } |
| 240 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 241 | EXPECT_EQ(5, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 242 | EXPECT_EQ(5, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 243 | |
| 244 | { |
| 245 | TimedOperation op(StringPrintf("Programming %d-UID blacklist chain", kNumUids)); |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 246 | mNetd->firewallReplaceUidChain(chainName, false, uids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 247 | } |
| 248 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 249 | EXPECT_EQ((int) uids.size() + 5, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 250 | EXPECT_EQ((int) uids.size() + 5, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 251 | EXPECT_EQ(false, iptablesNoSocketAllowRuleExists(chainName.c_str())); |
| 252 | EXPECT_EQ(false, iptablesEspAllowRuleExists(chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 253 | |
| 254 | { |
| 255 | TimedOperation op("Clearing blacklist chain"); |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 256 | mNetd->firewallReplaceUidChain(chainName, false, noUids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 257 | } |
| 258 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 259 | EXPECT_EQ(5, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 260 | EXPECT_EQ(5, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 261 | |
| 262 | // Check that the call fails if iptables returns an error. |
| 263 | std::string veryLongStringName = "netd_binder_test_UnacceptablyLongIptablesChainName"; |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 264 | mNetd->firewallReplaceUidChain(veryLongStringName, true, noUids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 265 | EXPECT_EQ(false, ret); |
| 266 | } |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 267 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 268 | TEST_F(BinderTest, VirtualTunnelInterface) { |
George Burgess IV | c4a6d27 | 2018-05-21 14:41:57 -0700 | [diff] [blame] | 269 | const struct TestData { |
| 270 | const std::string family; |
| 271 | const std::string deviceName; |
| 272 | const std::string localAddress; |
| 273 | const std::string remoteAddress; |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 274 | int32_t iKey; |
| 275 | int32_t oKey; |
| 276 | } kTestData[] = { |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 277 | {"IPV4", "test_vti", "127.0.0.1", "8.8.8.8", 0x1234 + 53, 0x1234 + 53}, |
| 278 | {"IPV6", "test_vti6", "::1", "2001:4860:4860::8888", 0x1234 + 50, 0x1234 + 50}, |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | for (unsigned int i = 0; i < arraysize(kTestData); i++) { |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 282 | const auto& td = kTestData[i]; |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 283 | |
| 284 | binder::Status status; |
| 285 | |
| 286 | // Create Virtual Tunnel Interface. |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 287 | status = mNetd->addVirtualTunnelInterface(td.deviceName, td.localAddress, td.remoteAddress, |
| 288 | td.iKey, td.oKey); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 289 | EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage(); |
| 290 | |
| 291 | // Update Virtual Tunnel Interface. |
| 292 | status = mNetd->updateVirtualTunnelInterface(td.deviceName, td.localAddress, |
| 293 | td.remoteAddress, td.iKey, td.oKey); |
| 294 | EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage(); |
| 295 | |
| 296 | // Remove Virtual Tunnel Interface. |
| 297 | status = mNetd->removeVirtualTunnelInterface(td.deviceName); |
| 298 | EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage(); |
| 299 | } |
| 300 | } |
| 301 | |
Nathan Harold | 2deff32 | 2018-05-10 14:03:48 -0700 | [diff] [blame] | 302 | // IPsec tests are not run in 32 bit mode; both 32-bit kernels and |
| 303 | // mismatched ABIs (64-bit kernel with 32-bit userspace) are unsupported. |
| 304 | #if INTPTR_MAX != INT32_MAX |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 305 | #define RETURN_FALSE_IF_NEQ(_expect_, _ret_) \ |
| 306 | do { if ((_expect_) != (_ret_)) return false; } while(false) |
| 307 | bool BinderTest::allocateIpSecResources(bool expectOk, int32_t *spi) { |
| 308 | netdutils::Status status = XfrmController::ipSecAllocateSpi(0, "::", "::1", 123, spi); |
| 309 | SCOPED_TRACE(status); |
| 310 | RETURN_FALSE_IF_NEQ(status.ok(), expectOk); |
| 311 | |
| 312 | // Add a policy |
| 313 | status = XfrmController::ipSecAddSecurityPolicy(0, 0, "::", "::1", 123, 0, 0); |
| 314 | SCOPED_TRACE(status); |
| 315 | RETURN_FALSE_IF_NEQ(status.ok(), expectOk); |
| 316 | |
| 317 | // Add an ipsec interface |
| 318 | status = netdutils::statusFromErrno( |
| 319 | XfrmController::addVirtualTunnelInterface( |
| 320 | "ipsec_test", "::", "::1", 0xF00D, 0xD00D, false), |
| 321 | "addVirtualTunnelInterface"); |
| 322 | return (status.ok() == expectOk); |
| 323 | } |
| 324 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 325 | TEST_F(BinderTest, XfrmControllerInit) { |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 326 | netdutils::Status status; |
| 327 | status = XfrmController::Init(); |
| 328 | SCOPED_TRACE(status); |
Nathan Harold | 2deff32 | 2018-05-10 14:03:48 -0700 | [diff] [blame] | 329 | |
| 330 | // Older devices or devices with mismatched Kernel/User ABI cannot support the IPsec |
| 331 | // feature. |
| 332 | if (status.code() == EOPNOTSUPP) return; |
| 333 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 334 | ASSERT_TRUE(status.ok()); |
| 335 | |
| 336 | int32_t spi = 0; |
| 337 | |
| 338 | ASSERT_TRUE(allocateIpSecResources(true, &spi)); |
| 339 | ASSERT_TRUE(allocateIpSecResources(false, &spi)); |
| 340 | |
| 341 | status = XfrmController::Init(); |
Nathan Harold | 39ad662 | 2018-04-25 12:56:56 -0700 | [diff] [blame] | 342 | ASSERT_TRUE(status.ok()); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 343 | ASSERT_TRUE(allocateIpSecResources(true, &spi)); |
| 344 | |
| 345 | // Clean up |
| 346 | status = XfrmController::ipSecDeleteSecurityAssociation(0, "::", "::1", 123, spi, 0); |
| 347 | SCOPED_TRACE(status); |
| 348 | ASSERT_TRUE(status.ok()); |
| 349 | |
| 350 | status = XfrmController::ipSecDeleteSecurityPolicy(0, 0, "::", "::1", 0, 0); |
| 351 | SCOPED_TRACE(status); |
| 352 | ASSERT_TRUE(status.ok()); |
| 353 | |
| 354 | // Remove Virtual Tunnel Interface. |
| 355 | status = netdutils::statusFromErrno( |
| 356 | XfrmController::removeVirtualTunnelInterface("ipsec_test"), |
| 357 | "removeVirtualTunnelInterface"); |
| 358 | |
| 359 | ASSERT_TRUE(status.ok()); |
| 360 | } |
Nathan Harold | 2deff32 | 2018-05-10 14:03:48 -0700 | [diff] [blame] | 361 | #endif |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 362 | |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 363 | static int bandwidthDataSaverEnabled(const char *binary) { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 364 | std::vector<std::string> lines = listIptablesRule(binary, "bw_data_saver"); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 365 | |
| 366 | // Output looks like this: |
| 367 | // |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 368 | // Chain bw_data_saver (1 references) |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 369 | // target prot opt source destination |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 370 | // RETURN all -- 0.0.0.0/0 0.0.0.0/0 |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 371 | // |
| 372 | // or: |
| 373 | // |
| 374 | // Chain bw_data_saver (1 references) |
| 375 | // target prot opt source destination |
| 376 | // ... possibly connectivity critical packet rules here ... |
| 377 | // REJECT all -- ::/0 ::/0 |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 378 | |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 379 | EXPECT_GE(lines.size(), 3U); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 380 | |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 381 | if (lines.size() == 3 && StartsWith(lines[2], "RETURN ")) { |
| 382 | // Data saver disabled. |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | size_t minSize = (std::string(binary) == IPTABLES_PATH) ? 3 : 9; |
| 387 | |
| 388 | if (lines.size() >= minSize && StartsWith(lines[lines.size() -1], "REJECT ")) { |
| 389 | // Data saver enabled. |
| 390 | return 1; |
| 391 | } |
| 392 | |
| 393 | return -1; |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | bool enableDataSaver(sp<INetd>& netd, bool enable) { |
| 397 | TimedOperation op(enable ? " Enabling data saver" : "Disabling data saver"); |
| 398 | bool ret; |
| 399 | netd->bandwidthEnableDataSaver(enable, &ret); |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | int getDataSaverState() { |
| 404 | const int enabled4 = bandwidthDataSaverEnabled(IPTABLES_PATH); |
| 405 | const int enabled6 = bandwidthDataSaverEnabled(IP6TABLES_PATH); |
| 406 | EXPECT_EQ(enabled4, enabled6); |
| 407 | EXPECT_NE(-1, enabled4); |
| 408 | EXPECT_NE(-1, enabled6); |
| 409 | if (enabled4 != enabled6 || (enabled6 != 0 && enabled6 != 1)) { |
| 410 | return -1; |
| 411 | } |
| 412 | return enabled6; |
| 413 | } |
| 414 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 415 | TEST_F(BinderTest, BandwidthEnableDataSaver) { |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 416 | const int wasEnabled = getDataSaverState(); |
| 417 | ASSERT_NE(-1, wasEnabled); |
| 418 | |
| 419 | if (wasEnabled) { |
| 420 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 421 | EXPECT_EQ(0, getDataSaverState()); |
| 422 | } |
| 423 | |
| 424 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 425 | EXPECT_EQ(0, getDataSaverState()); |
| 426 | |
| 427 | ASSERT_TRUE(enableDataSaver(mNetd, true)); |
| 428 | EXPECT_EQ(1, getDataSaverState()); |
| 429 | |
| 430 | ASSERT_TRUE(enableDataSaver(mNetd, true)); |
| 431 | EXPECT_EQ(1, getDataSaverState()); |
| 432 | |
| 433 | if (!wasEnabled) { |
| 434 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 435 | EXPECT_EQ(0, getDataSaverState()); |
| 436 | } |
| 437 | } |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 438 | |
| 439 | static bool ipRuleExistsForRange(const uint32_t priority, const UidRange& range, |
| 440 | const std::string& action, const char* ipVersion) { |
| 441 | // Output looks like this: |
Robin Lee | 6c84ef6 | 2016-05-03 13:17:58 +0100 | [diff] [blame] | 442 | // "12500:\tfrom all fwmark 0x0/0x20000 iif lo uidrange 1000-2000 prohibit" |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 443 | std::vector<std::string> rules = listIpRules(ipVersion); |
| 444 | |
| 445 | std::string prefix = StringPrintf("%" PRIu32 ":", priority); |
| 446 | std::string suffix = StringPrintf(" iif lo uidrange %d-%d %s\n", |
| 447 | range.getStart(), range.getStop(), action.c_str()); |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 448 | for (const auto& line : rules) { |
Elliott Hughes | 2f44508 | 2017-12-20 12:39:35 -0800 | [diff] [blame] | 449 | if (android::base::StartsWith(line, prefix) && android::base::EndsWith(line, suffix)) { |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 450 | return true; |
| 451 | } |
| 452 | } |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | static bool ipRuleExistsForRange(const uint32_t priority, const UidRange& range, |
| 457 | const std::string& action) { |
| 458 | bool existsIp4 = ipRuleExistsForRange(priority, range, action, IP_RULE_V4); |
| 459 | bool existsIp6 = ipRuleExistsForRange(priority, range, action, IP_RULE_V6); |
| 460 | EXPECT_EQ(existsIp4, existsIp6); |
| 461 | return existsIp4; |
| 462 | } |
| 463 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 464 | TEST_F(BinderTest, NetworkInterfaces) { |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 465 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, "").isOk()); |
| 466 | EXPECT_EQ(EEXIST, mNetd->networkCreatePhysical(TEST_NETID1, "").serviceSpecificErrorCode()); |
| 467 | EXPECT_EQ(EEXIST, mNetd->networkCreateVpn(TEST_NETID1, false, true).serviceSpecificErrorCode()); |
| 468 | EXPECT_TRUE(mNetd->networkCreateVpn(TEST_NETID2, false, true).isOk()); |
| 469 | |
| 470 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 471 | EXPECT_EQ(EBUSY, |
| 472 | mNetd->networkAddInterface(TEST_NETID2, sTun.name()).serviceSpecificErrorCode()); |
| 473 | |
| 474 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 475 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID2, sTun.name()).isOk()); |
| 476 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID2).isOk()); |
| 477 | } |
| 478 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 479 | TEST_F(BinderTest, NetworkUidRules) { |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 480 | const uint32_t RULE_PRIORITY_SECURE_VPN = 12000; |
| 481 | |
| 482 | EXPECT_TRUE(mNetd->networkCreateVpn(TEST_NETID1, false, true).isOk()); |
| 483 | EXPECT_EQ(EEXIST, mNetd->networkCreateVpn(TEST_NETID1, false, true).serviceSpecificErrorCode()); |
| 484 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 485 | |
| 486 | std::vector<UidRange> uidRanges = { |
| 487 | {BASE_UID + 8005, BASE_UID + 8012}, |
| 488 | {BASE_UID + 8090, BASE_UID + 8099} |
| 489 | }; |
| 490 | UidRange otherRange(BASE_UID + 8190, BASE_UID + 8299); |
| 491 | std::string suffix = StringPrintf("lookup %s ", sTun.name().c_str()); |
| 492 | |
| 493 | EXPECT_TRUE(mNetd->networkAddUidRanges(TEST_NETID1, uidRanges).isOk()); |
| 494 | |
| 495 | EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[0], suffix)); |
| 496 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, otherRange, suffix)); |
| 497 | EXPECT_TRUE(mNetd->networkRemoveUidRanges(TEST_NETID1, uidRanges).isOk()); |
| 498 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[0], suffix)); |
| 499 | |
| 500 | EXPECT_TRUE(mNetd->networkAddUidRanges(TEST_NETID1, uidRanges).isOk()); |
| 501 | EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[1], suffix)); |
| 502 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 503 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[1], suffix)); |
| 504 | |
| 505 | EXPECT_EQ(ENONET, mNetd->networkDestroy(TEST_NETID1).serviceSpecificErrorCode()); |
| 506 | } |
| 507 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 508 | TEST_F(BinderTest, NetworkRejectNonSecureVpn) { |
Robin Lee | 6c84ef6 | 2016-05-03 13:17:58 +0100 | [diff] [blame] | 509 | constexpr uint32_t RULE_PRIORITY = 12500; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 510 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 511 | std::vector<UidRange> uidRanges = { |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 512 | {BASE_UID + 150, BASE_UID + 224}, |
| 513 | {BASE_UID + 226, BASE_UID + 300} |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 514 | }; |
| 515 | |
| 516 | const std::vector<std::string> initialRulesV4 = listIpRules(IP_RULE_V4); |
| 517 | const std::vector<std::string> initialRulesV6 = listIpRules(IP_RULE_V6); |
| 518 | |
| 519 | // Create two valid rules. |
| 520 | ASSERT_TRUE(mNetd->networkRejectNonSecureVpn(true, uidRanges).isOk()); |
| 521 | EXPECT_EQ(initialRulesV4.size() + 2, listIpRules(IP_RULE_V4).size()); |
| 522 | EXPECT_EQ(initialRulesV6.size() + 2, listIpRules(IP_RULE_V6).size()); |
| 523 | for (auto const& range : uidRanges) { |
| 524 | EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY, range, "prohibit")); |
| 525 | } |
| 526 | |
| 527 | // Remove the rules. |
| 528 | ASSERT_TRUE(mNetd->networkRejectNonSecureVpn(false, uidRanges).isOk()); |
| 529 | EXPECT_EQ(initialRulesV4.size(), listIpRules(IP_RULE_V4).size()); |
| 530 | EXPECT_EQ(initialRulesV6.size(), listIpRules(IP_RULE_V6).size()); |
| 531 | for (auto const& range : uidRanges) { |
| 532 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY, range, "prohibit")); |
| 533 | } |
| 534 | |
| 535 | // Fail to remove the rules a second time after they are already deleted. |
| 536 | binder::Status status = mNetd->networkRejectNonSecureVpn(false, uidRanges); |
| 537 | ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 538 | EXPECT_EQ(ENOENT, status.serviceSpecificErrorCode()); |
| 539 | |
| 540 | // All rules should be the same as before. |
| 541 | EXPECT_EQ(initialRulesV4, listIpRules(IP_RULE_V4)); |
| 542 | EXPECT_EQ(initialRulesV6, listIpRules(IP_RULE_V6)); |
| 543 | } |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 544 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 545 | // Create a socket pair that isLoopbackSocket won't think is local. |
| 546 | void BinderTest::fakeRemoteSocketPair(int *clientSocket, int *serverSocket, int *acceptedSocket) { |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 547 | *serverSocket = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0); |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 548 | struct sockaddr_in6 server6 = { .sin6_family = AF_INET6, .sin6_addr = sTun.dstAddr() }; |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 549 | ASSERT_EQ(0, bind(*serverSocket, (struct sockaddr *) &server6, sizeof(server6))); |
| 550 | |
| 551 | socklen_t addrlen = sizeof(server6); |
| 552 | ASSERT_EQ(0, getsockname(*serverSocket, (struct sockaddr *) &server6, &addrlen)); |
| 553 | ASSERT_EQ(0, listen(*serverSocket, 10)); |
| 554 | |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 555 | *clientSocket = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0); |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 556 | struct sockaddr_in6 client6 = { .sin6_family = AF_INET6, .sin6_addr = sTun.srcAddr() }; |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 557 | ASSERT_EQ(0, bind(*clientSocket, (struct sockaddr *) &client6, sizeof(client6))); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 558 | ASSERT_EQ(0, connect(*clientSocket, (struct sockaddr *) &server6, sizeof(server6))); |
| 559 | ASSERT_EQ(0, getsockname(*clientSocket, (struct sockaddr *) &client6, &addrlen)); |
| 560 | |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 561 | *acceptedSocket = accept4(*serverSocket, (struct sockaddr *) &server6, &addrlen, SOCK_CLOEXEC); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 562 | ASSERT_NE(-1, *acceptedSocket); |
| 563 | |
| 564 | ASSERT_EQ(0, memcmp(&client6, &server6, sizeof(client6))); |
| 565 | } |
| 566 | |
| 567 | void checkSocketpairOpen(int clientSocket, int acceptedSocket) { |
| 568 | char buf[4096]; |
| 569 | EXPECT_EQ(4, write(clientSocket, "foo", sizeof("foo"))); |
| 570 | EXPECT_EQ(4, read(acceptedSocket, buf, sizeof(buf))); |
| 571 | EXPECT_EQ(0, memcmp(buf, "foo", sizeof("foo"))); |
| 572 | } |
| 573 | |
| 574 | void checkSocketpairClosed(int clientSocket, int acceptedSocket) { |
| 575 | // Check that the client socket was closed with ECONNABORTED. |
| 576 | int ret = write(clientSocket, "foo", sizeof("foo")); |
| 577 | int err = errno; |
| 578 | EXPECT_EQ(-1, ret); |
| 579 | EXPECT_EQ(ECONNABORTED, err); |
| 580 | |
| 581 | // Check that it sent a RST to the server. |
| 582 | ret = write(acceptedSocket, "foo", sizeof("foo")); |
| 583 | err = errno; |
| 584 | EXPECT_EQ(-1, ret); |
| 585 | EXPECT_EQ(ECONNRESET, err); |
| 586 | } |
| 587 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 588 | TEST_F(BinderTest, SocketDestroy) { |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 589 | int clientSocket, serverSocket, acceptedSocket; |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 590 | ASSERT_NO_FATAL_FAILURE(fakeRemoteSocketPair(&clientSocket, &serverSocket, &acceptedSocket)); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 591 | |
| 592 | // Pick a random UID in the system UID range. |
| 593 | constexpr int baseUid = AID_APP - 2000; |
| 594 | static_assert(baseUid > 0, "Not enough UIDs? Please fix this test."); |
| 595 | int uid = baseUid + 500 + arc4random_uniform(1000); |
| 596 | EXPECT_EQ(0, fchown(clientSocket, uid, -1)); |
| 597 | |
| 598 | // UID ranges that don't contain uid. |
| 599 | std::vector<UidRange> uidRanges = { |
| 600 | {baseUid + 42, baseUid + 449}, |
| 601 | {baseUid + 1536, AID_APP - 4}, |
| 602 | {baseUid + 498, uid - 1}, |
| 603 | {uid + 1, baseUid + 1520}, |
| 604 | }; |
| 605 | // A skip list that doesn't contain UID. |
| 606 | std::vector<int32_t> skipUids { baseUid + 123, baseUid + 1600 }; |
| 607 | |
| 608 | // Close sockets. Our test socket should be intact. |
| 609 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 610 | checkSocketpairOpen(clientSocket, acceptedSocket); |
| 611 | |
| 612 | // UID ranges that do contain uid. |
| 613 | uidRanges = { |
| 614 | {baseUid + 42, baseUid + 449}, |
| 615 | {baseUid + 1536, AID_APP - 4}, |
| 616 | {baseUid + 498, baseUid + 1520}, |
| 617 | }; |
| 618 | // Add uid to the skip list. |
| 619 | skipUids.push_back(uid); |
| 620 | |
| 621 | // Close sockets. Our test socket should still be intact because it's in the skip list. |
| 622 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 623 | checkSocketpairOpen(clientSocket, acceptedSocket); |
| 624 | |
| 625 | // Now remove uid from skipUids, and close sockets. Our test socket should have been closed. |
| 626 | skipUids.resize(skipUids.size() - 1); |
| 627 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 628 | checkSocketpairClosed(clientSocket, acceptedSocket); |
| 629 | |
| 630 | close(clientSocket); |
| 631 | close(serverSocket); |
| 632 | close(acceptedSocket); |
| 633 | } |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 634 | |
| 635 | namespace { |
| 636 | |
| 637 | int netmaskToPrefixLength(const uint8_t *buf, size_t buflen) { |
| 638 | if (buf == nullptr) return -1; |
| 639 | |
| 640 | int prefixLength = 0; |
| 641 | bool endOfContiguousBits = false; |
| 642 | for (unsigned int i = 0; i < buflen; i++) { |
| 643 | const uint8_t value = buf[i]; |
| 644 | |
| 645 | // Bad bit sequence: check for a contiguous set of bits from the high |
| 646 | // end by verifying that the inverted value + 1 is a power of 2 |
| 647 | // (power of 2 iff. (v & (v - 1)) == 0). |
| 648 | const uint8_t inverse = ~value + 1; |
| 649 | if ((inverse & (inverse - 1)) != 0) return -1; |
| 650 | |
| 651 | prefixLength += (value == 0) ? 0 : CHAR_BIT - ffs(value) + 1; |
| 652 | |
| 653 | // Bogus netmask. |
| 654 | if (endOfContiguousBits && value != 0) return -1; |
| 655 | |
| 656 | if (value != 0xff) endOfContiguousBits = true; |
| 657 | } |
| 658 | |
| 659 | return prefixLength; |
| 660 | } |
| 661 | |
| 662 | template<typename T> |
| 663 | int netmaskToPrefixLength(const T *p) { |
| 664 | return netmaskToPrefixLength(reinterpret_cast<const uint8_t*>(p), sizeof(T)); |
| 665 | } |
| 666 | |
| 667 | |
| 668 | static bool interfaceHasAddress( |
| 669 | const std::string &ifname, const char *addrString, int prefixLength) { |
| 670 | struct addrinfo *addrinfoList = nullptr; |
| 671 | ScopedAddrinfo addrinfoCleanup(addrinfoList); |
| 672 | |
| 673 | const struct addrinfo hints = { |
| 674 | .ai_flags = AI_NUMERICHOST, |
| 675 | .ai_family = AF_UNSPEC, |
| 676 | .ai_socktype = SOCK_DGRAM, |
| 677 | }; |
| 678 | if (getaddrinfo(addrString, nullptr, &hints, &addrinfoList) != 0 || |
| 679 | addrinfoList == nullptr || addrinfoList->ai_addr == nullptr) { |
| 680 | return false; |
| 681 | } |
| 682 | |
| 683 | struct ifaddrs *ifaddrsList = nullptr; |
| 684 | ScopedIfaddrs ifaddrsCleanup(ifaddrsList); |
| 685 | |
| 686 | if (getifaddrs(&ifaddrsList) != 0) { |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | for (struct ifaddrs *addr = ifaddrsList; addr != nullptr; addr = addr->ifa_next) { |
| 691 | if (std::string(addr->ifa_name) != ifname || |
| 692 | addr->ifa_addr == nullptr || |
| 693 | addr->ifa_addr->sa_family != addrinfoList->ai_addr->sa_family) { |
| 694 | continue; |
| 695 | } |
| 696 | |
| 697 | switch (addr->ifa_addr->sa_family) { |
| 698 | case AF_INET: { |
| 699 | auto *addr4 = reinterpret_cast<const struct sockaddr_in*>(addr->ifa_addr); |
| 700 | auto *want = reinterpret_cast<const struct sockaddr_in*>(addrinfoList->ai_addr); |
| 701 | if (memcmp(&addr4->sin_addr, &want->sin_addr, sizeof(want->sin_addr)) != 0) { |
| 702 | continue; |
| 703 | } |
| 704 | |
| 705 | if (prefixLength < 0) return true; // not checking prefix lengths |
| 706 | |
| 707 | if (addr->ifa_netmask == nullptr) return false; |
| 708 | auto *nm = reinterpret_cast<const struct sockaddr_in*>(addr->ifa_netmask); |
| 709 | EXPECT_EQ(prefixLength, netmaskToPrefixLength(&nm->sin_addr)); |
| 710 | return (prefixLength == netmaskToPrefixLength(&nm->sin_addr)); |
| 711 | } |
| 712 | case AF_INET6: { |
| 713 | auto *addr6 = reinterpret_cast<const struct sockaddr_in6*>(addr->ifa_addr); |
| 714 | auto *want = reinterpret_cast<const struct sockaddr_in6*>(addrinfoList->ai_addr); |
| 715 | if (memcmp(&addr6->sin6_addr, &want->sin6_addr, sizeof(want->sin6_addr)) != 0) { |
| 716 | continue; |
| 717 | } |
| 718 | |
| 719 | if (prefixLength < 0) return true; // not checking prefix lengths |
| 720 | |
| 721 | if (addr->ifa_netmask == nullptr) return false; |
| 722 | auto *nm = reinterpret_cast<const struct sockaddr_in6*>(addr->ifa_netmask); |
| 723 | EXPECT_EQ(prefixLength, netmaskToPrefixLength(&nm->sin6_addr)); |
| 724 | return (prefixLength == netmaskToPrefixLength(&nm->sin6_addr)); |
| 725 | } |
| 726 | default: |
| 727 | // Cannot happen because we have already screened for matching |
| 728 | // address families at the top of each iteration. |
| 729 | continue; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | } // namespace |
| 737 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 738 | TEST_F(BinderTest, InterfaceAddRemoveAddress) { |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 739 | static const struct TestData { |
| 740 | const char *addrString; |
| 741 | const int prefixLength; |
| 742 | const bool expectSuccess; |
| 743 | } kTestData[] = { |
| 744 | { "192.0.2.1", 24, true }, |
| 745 | { "192.0.2.2", 25, true }, |
| 746 | { "192.0.2.3", 32, true }, |
| 747 | { "192.0.2.4", 33, false }, |
| 748 | { "192.not.an.ip", 24, false }, |
| 749 | { "2001:db8::1", 64, true }, |
| 750 | { "2001:db8::2", 65, true }, |
| 751 | { "2001:db8::3", 128, true }, |
| 752 | { "2001:db8::4", 129, false }, |
| 753 | { "foo:bar::bad", 64, false }, |
| 754 | }; |
| 755 | |
| 756 | for (unsigned int i = 0; i < arraysize(kTestData); i++) { |
| 757 | const auto &td = kTestData[i]; |
| 758 | |
| 759 | // [1.a] Add the address. |
| 760 | binder::Status status = mNetd->interfaceAddAddress( |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 761 | sTun.name(), td.addrString, td.prefixLength); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 762 | if (td.expectSuccess) { |
| 763 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 764 | } else { |
| 765 | ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 766 | ASSERT_NE(0, status.serviceSpecificErrorCode()); |
| 767 | } |
| 768 | |
| 769 | // [1.b] Verify the addition meets the expectation. |
| 770 | if (td.expectSuccess) { |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 771 | EXPECT_TRUE(interfaceHasAddress(sTun.name(), td.addrString, td.prefixLength)); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 772 | } else { |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 773 | EXPECT_FALSE(interfaceHasAddress(sTun.name(), td.addrString, -1)); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | // [2.a] Try to remove the address. If it was not previously added, removing it fails. |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 777 | status = mNetd->interfaceDelAddress(sTun.name(), td.addrString, td.prefixLength); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 778 | if (td.expectSuccess) { |
| 779 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 780 | } else { |
| 781 | ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 782 | ASSERT_NE(0, status.serviceSpecificErrorCode()); |
| 783 | } |
| 784 | |
| 785 | // [2.b] No matter what, the address should not be present. |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 786 | EXPECT_FALSE(interfaceHasAddress(sTun.name(), td.addrString, -1)); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 787 | } |
| 788 | } |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 789 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 790 | TEST_F(BinderTest, SetProcSysNet) { |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 791 | static const struct TestData { |
| 792 | const int family; |
| 793 | const int which; |
| 794 | const char *ifname; |
| 795 | const char *parameter; |
| 796 | const char *value; |
| 797 | const int expectedReturnCode; |
| 798 | } kTestData[] = { |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 799 | { INetd::IPV4, INetd::CONF, sTun.name().c_str(), "arp_ignore", "1", 0 }, |
| 800 | { -1, INetd::CONF, sTun.name().c_str(), "arp_ignore", "1", EAFNOSUPPORT }, |
| 801 | { INetd::IPV4, -1, sTun.name().c_str(), "arp_ignore", "1", EINVAL }, |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 802 | { INetd::IPV4, INetd::CONF, "..", "conf/lo/arp_ignore", "1", EINVAL }, |
| 803 | { INetd::IPV4, INetd::CONF, ".", "lo/arp_ignore", "1", EINVAL }, |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 804 | { INetd::IPV4, INetd::CONF, sTun.name().c_str(), "../all/arp_ignore", "1", EINVAL }, |
| 805 | { INetd::IPV6, INetd::NEIGH, sTun.name().c_str(), "ucast_solicit", "7", 0 }, |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 806 | }; |
| 807 | |
| 808 | for (unsigned int i = 0; i < arraysize(kTestData); i++) { |
| 809 | const auto &td = kTestData[i]; |
| 810 | |
| 811 | const binder::Status status = mNetd->setProcSysNet( |
| 812 | td.family, td.which, td.ifname, td.parameter, |
| 813 | td.value); |
| 814 | |
| 815 | if (td.expectedReturnCode == 0) { |
| 816 | SCOPED_TRACE(String8::format("test case %d should have passed", i)); |
| 817 | EXPECT_EQ(0, status.exceptionCode()); |
| 818 | EXPECT_EQ(0, status.serviceSpecificErrorCode()); |
| 819 | } else { |
| 820 | SCOPED_TRACE(String8::format("test case %d should have failed", i)); |
| 821 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 822 | EXPECT_EQ(td.expectedReturnCode, status.serviceSpecificErrorCode()); |
| 823 | } |
| 824 | } |
| 825 | } |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 826 | |
| 827 | static std::string base64Encode(const std::vector<uint8_t>& input) { |
| 828 | size_t out_len; |
| 829 | EXPECT_EQ(1, EVP_EncodedLength(&out_len, input.size())); |
| 830 | // out_len includes the trailing NULL. |
| 831 | uint8_t output_bytes[out_len]; |
| 832 | EXPECT_EQ(out_len - 1, EVP_EncodeBlock(output_bytes, input.data(), input.size())); |
| 833 | return std::string(reinterpret_cast<char*>(output_bytes)); |
| 834 | } |
| 835 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 836 | TEST_F(BinderTest, SetResolverConfiguration_Tls) { |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 837 | const std::vector<std::string> LOCALLY_ASSIGNED_DNS{"8.8.8.8", "2001:4860:4860::8888"}; |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 838 | std::vector<uint8_t> fp(SHA256_SIZE); |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 839 | std::vector<uint8_t> short_fp(1); |
| 840 | std::vector<uint8_t> long_fp(SHA256_SIZE + 1); |
| 841 | std::vector<std::string> test_domains; |
| 842 | std::vector<int> test_params = { 300, 25, 8, 8 }; |
| 843 | unsigned test_netid = 0; |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 844 | static const struct TestData { |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 845 | const std::vector<std::string> servers; |
| 846 | const std::string tlsName; |
| 847 | const std::vector<std::vector<uint8_t>> tlsFingerprints; |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 848 | const int expectedReturnCode; |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 849 | } kTlsTestData[] = { |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 850 | { {"192.0.2.1"}, "", {}, 0 }, |
| 851 | { {"2001:db8::2"}, "host.name", {}, 0 }, |
| 852 | { {"192.0.2.3"}, "@@@@", { fp }, 0 }, |
| 853 | { {"2001:db8::4"}, "", { fp }, 0 }, |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 854 | { {}, "", {}, 0 }, |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 855 | { {""}, "", {}, EINVAL }, |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 856 | { {"192.0.*.5"}, "", {}, EINVAL }, |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 857 | { {"2001:dg8::6"}, "", {}, EINVAL }, |
| 858 | { {"2001:db8::c"}, "", { short_fp }, EINVAL }, |
| 859 | { {"192.0.2.12"}, "", { long_fp }, EINVAL }, |
| 860 | { {"2001:db8::e"}, "", { fp, fp, fp }, 0 }, |
| 861 | { {"192.0.2.14"}, "", { fp, short_fp }, EINVAL }, |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 862 | }; |
| 863 | |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 864 | for (unsigned int i = 0; i < arraysize(kTlsTestData); i++) { |
| 865 | const auto &td = kTlsTestData[i]; |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 866 | |
| 867 | std::vector<std::string> fingerprints; |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 868 | for (const auto& fingerprint : td.tlsFingerprints) { |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 869 | fingerprints.push_back(base64Encode(fingerprint)); |
| 870 | } |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 871 | binder::Status status = mNetd->setResolverConfiguration( |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 872 | test_netid, LOCALLY_ASSIGNED_DNS, test_domains, test_params, |
| 873 | td.tlsName, td.servers, fingerprints); |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 874 | |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 875 | if (td.expectedReturnCode == 0) { |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 876 | SCOPED_TRACE(String8::format("test case %d should have passed", i)); |
| 877 | SCOPED_TRACE(status.toString8()); |
| 878 | EXPECT_EQ(0, status.exceptionCode()); |
| 879 | } else { |
| 880 | SCOPED_TRACE(String8::format("test case %d should have failed", i)); |
| 881 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 882 | EXPECT_EQ(td.expectedReturnCode, status.serviceSpecificErrorCode()); |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 883 | } |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 884 | } |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 885 | // Ensure TLS is disabled before the start of the next test. |
| 886 | mNetd->setResolverConfiguration( |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 887 | test_netid, kTlsTestData[0].servers, test_domains, test_params, |
| 888 | "", {}, {}); |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 889 | } |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 890 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 891 | namespace { |
| 892 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 893 | void expectNoTestCounterRules() { |
| 894 | for (const auto& binary : { IPTABLES_PATH, IP6TABLES_PATH }) { |
| 895 | std::string command = StringPrintf("%s -w -nvL tetherctrl_counters", binary); |
| 896 | std::string allRules = Join(runCommand(command), "\n"); |
| 897 | EXPECT_EQ(std::string::npos, allRules.find("netdtest_")); |
| 898 | } |
| 899 | } |
| 900 | |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 901 | void addTetherCounterValues(const char* path, const std::string& if1, const std::string& if2, |
| 902 | int byte, int pkt) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 903 | runCommand(StringPrintf("%s -w -A tetherctrl_counters -i %s -o %s -j RETURN -c %d %d", |
| 904 | path, if1.c_str(), if2.c_str(), pkt, byte)); |
| 905 | } |
| 906 | |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 907 | void delTetherCounterValues(const char* path, const std::string& if1, const std::string& if2) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 908 | runCommand(StringPrintf("%s -w -D tetherctrl_counters -i %s -o %s -j RETURN", |
| 909 | path, if1.c_str(), if2.c_str())); |
| 910 | runCommand(StringPrintf("%s -w -D tetherctrl_counters -i %s -o %s -j RETURN", |
| 911 | path, if2.c_str(), if1.c_str())); |
| 912 | } |
| 913 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 914 | } // namespace |
| 915 | |
| 916 | TEST_F(BinderTest, TetherGetStats) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 917 | expectNoTestCounterRules(); |
| 918 | |
| 919 | // TODO: fold this into more comprehensive tests once we have binder RPCs for enabling and |
| 920 | // disabling tethering. We don't check the return value because these commands will fail if |
| 921 | // tethering is already enabled. |
| 922 | runCommand(StringPrintf("%s -w -N tetherctrl_counters", IPTABLES_PATH)); |
| 923 | runCommand(StringPrintf("%s -w -N tetherctrl_counters", IP6TABLES_PATH)); |
| 924 | |
| 925 | std::string intIface1 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
| 926 | std::string intIface2 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
| 927 | std::string intIface3 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
| 928 | std::string extIface1 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
| 929 | std::string extIface2 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
| 930 | |
| 931 | addTetherCounterValues(IPTABLES_PATH, intIface1, extIface1, 123, 111); |
| 932 | addTetherCounterValues(IP6TABLES_PATH, intIface1, extIface1, 456, 10); |
| 933 | addTetherCounterValues(IPTABLES_PATH, extIface1, intIface1, 321, 222); |
| 934 | addTetherCounterValues(IP6TABLES_PATH, extIface1, intIface1, 654, 20); |
| 935 | // RX is from external to internal, and TX is from internal to external. |
| 936 | // So rxBytes is 321 + 654 = 975, txBytes is 123 + 456 = 579, etc. |
| 937 | std::vector<int64_t> expected1 = { 975, 242, 579, 121 }; |
| 938 | |
| 939 | addTetherCounterValues(IPTABLES_PATH, intIface2, extIface2, 1000, 333); |
| 940 | addTetherCounterValues(IP6TABLES_PATH, intIface2, extIface2, 3000, 30); |
| 941 | |
| 942 | addTetherCounterValues(IPTABLES_PATH, extIface2, intIface2, 2000, 444); |
| 943 | addTetherCounterValues(IP6TABLES_PATH, extIface2, intIface2, 4000, 40); |
| 944 | |
| 945 | addTetherCounterValues(IP6TABLES_PATH, intIface3, extIface2, 1000, 25); |
| 946 | addTetherCounterValues(IP6TABLES_PATH, extIface2, intIface3, 2000, 35); |
| 947 | std::vector<int64_t> expected2 = { 8000, 519, 5000, 388 }; |
| 948 | |
| 949 | PersistableBundle stats; |
| 950 | binder::Status status = mNetd->tetherGetStats(&stats); |
| 951 | EXPECT_TRUE(status.isOk()) << "Getting tethering stats failed: " << status; |
| 952 | |
| 953 | std::vector<int64_t> actual1; |
| 954 | EXPECT_TRUE(stats.getLongVector(String16(extIface1.c_str()), &actual1)); |
| 955 | EXPECT_EQ(expected1, actual1); |
| 956 | |
| 957 | std::vector<int64_t> actual2; |
| 958 | EXPECT_TRUE(stats.getLongVector(String16(extIface2.c_str()), &actual2)); |
| 959 | EXPECT_EQ(expected2, actual2); |
| 960 | |
| 961 | for (const auto& path : { IPTABLES_PATH, IP6TABLES_PATH }) { |
| 962 | delTetherCounterValues(path, intIface1, extIface1); |
| 963 | delTetherCounterValues(path, intIface2, extIface2); |
| 964 | if (path == IP6TABLES_PATH) { |
| 965 | delTetherCounterValues(path, intIface3, extIface2); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | expectNoTestCounterRules(); |
| 970 | } |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 971 | |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 972 | namespace { |
| 973 | |
| 974 | constexpr char chainName_LOCAL_RAW_PREROUTING[] = "idletimer_raw_PREROUTING"; |
| 975 | constexpr char chainName_MANGLE_POSTROUTING[] = "idletimer_mangle_POSTROUTING"; |
| 976 | |
| 977 | static std::vector<std::string> listIptablesRuleByTable(const char* binary, const char* table, |
| 978 | const char* chainName) { |
| 979 | std::string command = StringPrintf("%s -t %s -w -n -v -L %s", binary, table, chainName); |
| 980 | return runCommand(command); |
| 981 | } |
| 982 | |
| 983 | bool iptablesIdleTimerInterfcaeRuleExists(const char* binary, const char* chainName, |
| 984 | const std::string& expectedInterface, |
| 985 | const std::string& expectedRule, const char* table) { |
| 986 | std::vector<std::string> rules = listIptablesRuleByTable(binary, table, chainName); |
| 987 | for (const auto& rule : rules) { |
| 988 | if (rule.find(expectedInterface) != std::string::npos) { |
| 989 | if (rule.find(expectedRule) != std::string::npos) { |
| 990 | return true; |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | return false; |
| 995 | } |
| 996 | |
| 997 | void expectIdletimerInterfaceRuleExists(const std::string& ifname, int timeout, |
| 998 | const std::string& classLable) { |
| 999 | std::string IdletimerRule = |
| 1000 | StringPrintf("timeout:%u label:%s send_nl_msg:1", timeout, classLable.c_str()); |
| 1001 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 1002 | EXPECT_TRUE(iptablesIdleTimerInterfcaeRuleExists(binary, chainName_LOCAL_RAW_PREROUTING, |
| 1003 | ifname, IdletimerRule, RAW_TABLE)); |
| 1004 | EXPECT_TRUE(iptablesIdleTimerInterfcaeRuleExists(binary, chainName_MANGLE_POSTROUTING, |
| 1005 | ifname, IdletimerRule, MANGLE_TABLE)); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | void expectIdletimerInterfaceRuleNotExists(const std::string& ifname, int timeout, |
| 1010 | const std::string& classLable) { |
| 1011 | std::string IdletimerRule = |
| 1012 | StringPrintf("timeout:%u label:%s send_nl_msg:1", timeout, classLable.c_str()); |
| 1013 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 1014 | EXPECT_FALSE(iptablesIdleTimerInterfcaeRuleExists(binary, chainName_LOCAL_RAW_PREROUTING, |
| 1015 | ifname, IdletimerRule, RAW_TABLE)); |
| 1016 | EXPECT_FALSE(iptablesIdleTimerInterfcaeRuleExists(binary, chainName_MANGLE_POSTROUTING, |
| 1017 | ifname, IdletimerRule, MANGLE_TABLE)); |
| 1018 | } |
| 1019 | } |
| 1020 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 1021 | } // namespace |
| 1022 | |
| 1023 | TEST_F(BinderTest, IdletimerAddRemoveInterface) { |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1024 | // TODO: We will get error in if expectIdletimerInterfaceRuleNotExists if there are the same |
| 1025 | // rule in the table. Because we only check the result after calling remove function. We might |
| 1026 | // check the actual rule which is removed by our function (maybe compare the results between |
| 1027 | // calling function before and after) |
| 1028 | binder::Status status; |
| 1029 | const struct TestData { |
| 1030 | const std::string ifname; |
| 1031 | int32_t timeout; |
| 1032 | const std::string classLabel; |
| 1033 | } idleTestData[] = { |
| 1034 | {"wlan0", 1234, "happyday"}, |
| 1035 | {"rmnet_data0", 4567, "friday"}, |
| 1036 | }; |
| 1037 | for (const auto& td : idleTestData) { |
| 1038 | status = mNetd->idletimerAddInterface(td.ifname, td.timeout, td.classLabel); |
| 1039 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1040 | expectIdletimerInterfaceRuleExists(td.ifname, td.timeout, td.classLabel); |
| 1041 | |
| 1042 | status = mNetd->idletimerRemoveInterface(td.ifname, td.timeout, td.classLabel); |
| 1043 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1044 | expectIdletimerInterfaceRuleNotExists(td.ifname, td.timeout, td.classLabel); |
| 1045 | } |
| 1046 | } |
| 1047 | |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 1048 | namespace { |
| 1049 | |
| 1050 | constexpr char STRICT_OUTPUT[] = "st_OUTPUT"; |
| 1051 | constexpr char STRICT_CLEAR_CAUGHT[] = "st_clear_caught"; |
| 1052 | |
| 1053 | void expectStrictSetUidAccept(const int uid) { |
| 1054 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 1055 | std::string perUidChain = StringPrintf("st_clear_caught_%u", uid); |
| 1056 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 1057 | EXPECT_FALSE(iptablesRuleExists(binary, STRICT_OUTPUT, uidRule.c_str())); |
| 1058 | EXPECT_FALSE(iptablesRuleExists(binary, STRICT_CLEAR_CAUGHT, uidRule.c_str())); |
| 1059 | EXPECT_EQ(0, iptablesRuleLineLength(binary, perUidChain.c_str())); |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | void expectStrictSetUidLog(const int uid) { |
| 1064 | static const char logRule[] = "st_penalty_log all"; |
| 1065 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 1066 | std::string perUidChain = StringPrintf("st_clear_caught_%u", uid); |
| 1067 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 1068 | EXPECT_TRUE(iptablesRuleExists(binary, STRICT_OUTPUT, uidRule.c_str())); |
| 1069 | EXPECT_TRUE(iptablesRuleExists(binary, STRICT_CLEAR_CAUGHT, uidRule.c_str())); |
| 1070 | EXPECT_TRUE(iptablesRuleExists(binary, perUidChain.c_str(), logRule)); |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | void expectStrictSetUidReject(const int uid) { |
| 1075 | static const char rejectRule[] = "st_penalty_reject all"; |
| 1076 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 1077 | std::string perUidChain = StringPrintf("st_clear_caught_%u", uid); |
| 1078 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 1079 | EXPECT_TRUE(iptablesRuleExists(binary, STRICT_OUTPUT, uidRule.c_str())); |
| 1080 | EXPECT_TRUE(iptablesRuleExists(binary, STRICT_CLEAR_CAUGHT, uidRule.c_str())); |
| 1081 | EXPECT_TRUE(iptablesRuleExists(binary, perUidChain.c_str(), rejectRule)); |
| 1082 | } |
| 1083 | } |
| 1084 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 1085 | } // namespace |
| 1086 | |
| 1087 | TEST_F(BinderTest, StrictSetUidCleartextPenalty) { |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 1088 | binder::Status status; |
| 1089 | int32_t uid = randomUid(); |
| 1090 | |
| 1091 | // setUidCleartextPenalty Policy:Log with randomUid |
| 1092 | status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_LOG); |
| 1093 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1094 | expectStrictSetUidLog(uid); |
| 1095 | |
| 1096 | // setUidCleartextPenalty Policy:Accept with randomUid |
| 1097 | status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_ACCEPT); |
| 1098 | expectStrictSetUidAccept(uid); |
| 1099 | |
| 1100 | // setUidCleartextPenalty Policy:Reject with randomUid |
| 1101 | status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_REJECT); |
| 1102 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1103 | expectStrictSetUidReject(uid); |
| 1104 | |
| 1105 | // setUidCleartextPenalty Policy:Accept with randomUid |
| 1106 | status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_ACCEPT); |
| 1107 | expectStrictSetUidAccept(uid); |
| 1108 | |
| 1109 | // test wrong policy |
| 1110 | int32_t wrongPolicy = -123; |
| 1111 | status = mNetd->strictUidCleartextPenalty(uid, wrongPolicy); |
| 1112 | EXPECT_EQ(EINVAL, status.serviceSpecificErrorCode()); |
| 1113 | } |
| 1114 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 1115 | namespace { |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 1116 | |
| 1117 | static bool processExists(const std::string& processName) { |
| 1118 | std::string cmd = StringPrintf("ps -A | grep '%s'", processName.c_str()); |
| 1119 | return (runCommand(cmd.c_str()).size()) ? true : false; |
| 1120 | } |
| 1121 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 1122 | } // namespace |
| 1123 | |
| 1124 | TEST_F(BinderTest, ClatdStartStop) { |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 1125 | binder::Status status; |
| 1126 | // use dummy0 for test since it is set ready |
| 1127 | static const char testIf[] = "dummy0"; |
| 1128 | const std::string clatdName = StringPrintf("clatd-%s", testIf); |
| 1129 | |
| 1130 | status = mNetd->clatdStart(testIf); |
| 1131 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1132 | EXPECT_TRUE(processExists(clatdName)); |
| 1133 | |
| 1134 | mNetd->clatdStop(testIf); |
| 1135 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1136 | EXPECT_FALSE(processExists(clatdName)); |
| 1137 | } |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 1138 | |
| 1139 | namespace { |
| 1140 | |
| 1141 | bool getIpfwdV4Enable() { |
| 1142 | static const char ipv4IpfwdCmd[] = "cat /proc/sys/net/ipv4/ip_forward"; |
| 1143 | std::vector<std::string> result = runCommand(ipv4IpfwdCmd); |
| 1144 | EXPECT_TRUE(!result.empty()); |
| 1145 | int v4Enable = std::stoi(result[0]); |
| 1146 | return v4Enable; |
| 1147 | } |
| 1148 | |
| 1149 | bool getIpfwdV6Enable() { |
| 1150 | static const char ipv6IpfwdCmd[] = "cat proc/sys/net/ipv6/conf/all/forwarding"; |
| 1151 | std::vector<std::string> result = runCommand(ipv6IpfwdCmd); |
| 1152 | EXPECT_TRUE(!result.empty()); |
| 1153 | int v6Enable = std::stoi(result[0]); |
| 1154 | return v6Enable; |
| 1155 | } |
| 1156 | |
| 1157 | void expectIpfwdEnable(bool enable) { |
| 1158 | int enableIPv4 = getIpfwdV4Enable(); |
| 1159 | int enableIPv6 = getIpfwdV6Enable(); |
| 1160 | EXPECT_EQ(enable, enableIPv4); |
| 1161 | EXPECT_EQ(enable, enableIPv6); |
| 1162 | } |
| 1163 | |
| 1164 | bool ipRuleIpfwdExists(const char* ipVersion, const std::string ipfwdRule) { |
| 1165 | std::vector<std::string> rules = listIpRules(ipVersion); |
| 1166 | for (const auto& rule : rules) { |
| 1167 | if (rule.find(ipfwdRule) != std::string::npos) { |
| 1168 | return true; |
| 1169 | } |
| 1170 | } |
| 1171 | return false; |
| 1172 | } |
| 1173 | |
| 1174 | void expectIpfwdRuleExists(const char* fromIf, const char* toIf) { |
| 1175 | std::string ipfwdRule = StringPrintf("18000:\tfrom all iif %s lookup %s ", fromIf, toIf); |
| 1176 | |
| 1177 | for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { |
| 1178 | EXPECT_TRUE(ipRuleIpfwdExists(ipVersion, ipfwdRule)); |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | void expectIpfwdRuleNotExists(const char* fromIf, const char* toIf) { |
| 1183 | std::string ipfwdRule = StringPrintf("18000:\tfrom all iif %s lookup %s ", fromIf, toIf); |
| 1184 | |
| 1185 | for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { |
| 1186 | EXPECT_FALSE(ipRuleIpfwdExists(ipVersion, ipfwdRule)); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | } // namespace |
| 1191 | |
| 1192 | TEST_F(BinderTest, TestIpfwdEnableDisableStatusForwarding) { |
| 1193 | // Netd default enable Ipfwd with requester NetdHwService |
| 1194 | const std::string defaultRequester = "NetdHwService"; |
| 1195 | |
| 1196 | binder::Status status = mNetd->ipfwdDisableForwarding(defaultRequester); |
| 1197 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1198 | expectIpfwdEnable(false); |
| 1199 | |
| 1200 | bool ipfwdEnabled; |
| 1201 | status = mNetd->ipfwdEnabled(&ipfwdEnabled); |
| 1202 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1203 | EXPECT_FALSE(ipfwdEnabled); |
| 1204 | |
| 1205 | status = mNetd->ipfwdEnableForwarding(defaultRequester); |
| 1206 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1207 | expectIpfwdEnable(true); |
| 1208 | |
| 1209 | status = mNetd->ipfwdEnabled(&ipfwdEnabled); |
| 1210 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1211 | EXPECT_TRUE(ipfwdEnabled); |
| 1212 | } |
| 1213 | |
| 1214 | TEST_F(BinderTest, TestIpfwdAddRemoveInterfaceForward) { |
| 1215 | static const char testFromIf[] = "dummy0"; |
| 1216 | static const char testToIf[] = "dummy0"; |
| 1217 | |
| 1218 | binder::Status status = mNetd->ipfwdAddInterfaceForward(testFromIf, testToIf); |
| 1219 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1220 | expectIpfwdRuleExists(testFromIf, testToIf); |
| 1221 | |
| 1222 | status = mNetd->ipfwdRemoveInterfaceForward(testFromIf, testToIf); |
| 1223 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1224 | expectIpfwdRuleNotExists(testFromIf, testToIf); |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame^] | 1225 | } |