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 | |
Chalard Jean | 38bc084 | 2022-12-26 19:36:13 +0900 | [diff] [blame] | 19 | #include <algorithm> |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 20 | #include <cerrno> |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 21 | #include <chrono> |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 22 | #include <cinttypes> |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 23 | #include <condition_variable> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 24 | #include <cstdint> |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 25 | #include <cstdlib> |
Bernie Innocenti | 196f1b8 | 2019-05-20 16:34:16 +0900 | [diff] [blame] | 26 | #include <iostream> |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 27 | #include <mutex> |
Taras Antoshchuk | 35987de | 2021-09-21 17:45:15 +0200 | [diff] [blame] | 28 | #include <numeric> |
Lorenzo Colitti | 23554ba | 2020-11-05 01:43:46 +0900 | [diff] [blame] | 29 | #include <regex> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 30 | #include <set> |
Lorenzo Colitti | 23554ba | 2020-11-05 01:43:46 +0900 | [diff] [blame] | 31 | #include <string> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 34 | #include <dirent.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 35 | #include <fcntl.h> |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 36 | #include <ifaddrs.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 37 | #include <linux/if.h> |
| 38 | #include <linux/if_tun.h> |
Lorenzo Colitti | 8363bf4 | 2020-05-29 21:37:09 +0900 | [diff] [blame] | 39 | #include <net/ethernet.h> |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 40 | #include <net/if.h> |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 41 | #include <netdb.h> |
| 42 | #include <netinet/in.h> |
Lorenzo Colitti | 1b21374 | 2020-03-27 17:50:19 +0900 | [diff] [blame] | 43 | #include <netinet/tcp.h> |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 44 | #include <openssl/base64.h> |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 45 | #include <sys/socket.h> |
| 46 | #include <sys/types.h> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 47 | |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 48 | #include <android-base/file.h> |
Lorenzo Colitti | 8363bf4 | 2020-05-29 21:37:09 +0900 | [diff] [blame] | 49 | #include <android-base/format.h> |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 50 | #include <android-base/macros.h> |
Lorenzo Colitti | 1b21374 | 2020-03-27 17:50:19 +0900 | [diff] [blame] | 51 | #include <android-base/scopeguard.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 52 | #include <android-base/stringprintf.h> |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 53 | #include <android-base/strings.h> |
Florian Mayer | da0e822 | 2022-10-25 23:22:52 +0000 | [diff] [blame] | 54 | #include <android-base/test_utils.h> |
Chiachang Wang | 00fc62f | 2019-12-04 20:38:26 +0800 | [diff] [blame] | 55 | #include <android/multinetwork.h> |
Luke Huang | 2ff8b34 | 2019-04-30 15:33:33 +0800 | [diff] [blame] | 56 | #include <binder/IPCThreadState.h> |
Patrick Rohr | 72cc450 | 2023-05-17 11:39:33 -0700 | [diff] [blame] | 57 | #include <bpf/KernelUtils.h> |
Luke Huang | 2ff8b34 | 2019-04-30 15:33:33 +0800 | [diff] [blame] | 58 | #include <com/android/internal/net/BnOemNetdUnsolicitedEventListener.h> |
| 59 | #include <com/android/internal/net/IOemNetd.h> |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 60 | #include <cutils/multiuser.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 61 | #include <gtest/gtest.h> |
Paul Hu | 1c64f08 | 2022-06-21 03:42:50 +0000 | [diff] [blame] | 62 | #include <netdutils/NetNativeTestBase.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 63 | #include <netutils/ifc.h> |
Lorenzo Colitti | 23554ba | 2020-11-05 01:43:46 +0900 | [diff] [blame] | 64 | #include <utils/Errors.h> |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 65 | #include "Fwmark.h" |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 66 | #include "InterfaceController.h" |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 67 | #include "NetdClient.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 68 | #include "NetdConstants.h" |
Chiachang Wang | 00fc62f | 2019-12-04 20:38:26 +0800 | [diff] [blame] | 69 | #include "NetworkController.h" |
Ken Chen | 69839af | 2021-01-05 22:48:32 +0800 | [diff] [blame] | 70 | #include "RouteController.h" |
Lorenzo Colitti | 1b21374 | 2020-03-27 17:50:19 +0900 | [diff] [blame] | 71 | #include "SockDiag.h" |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 72 | #include "TestUnsolService.h" |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 73 | #include "XfrmController.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 74 | #include "android/net/INetd.h" |
| 75 | #include "binder/IServiceManager.h" |
Luke Huang | 2559932 | 2019-06-14 00:34:05 +0800 | [diff] [blame] | 76 | #include "netdutils/InternetAddresses.h" |
Mike Yu | e7e332f | 2019-03-13 17:15:48 +0800 | [diff] [blame] | 77 | #include "netdutils/Stopwatch.h" |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 78 | #include "netdutils/Syscalls.h" |
waynema | 5851b03 | 2021-11-24 17:08:25 +0800 | [diff] [blame] | 79 | #include "netdutils/Utils.h" |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 80 | #include "netid_client.h" // NETID_UNSET |
Ken Chen | cf082a3 | 2022-01-15 10:58:55 +0800 | [diff] [blame] | 81 | #include "nettestutils/DumpService.h" |
Lorenzo Colitti | 758bccc | 2019-06-26 22:12:35 +0900 | [diff] [blame] | 82 | #include "test_utils.h" |
Mike Yu | 5ae6154 | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 83 | #include "tun_interface.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 84 | |
Lorenzo Colitti | 5c68b9c | 2017-08-10 18:50:10 +0900 | [diff] [blame] | 85 | #define IP6TABLES_PATH "/system/bin/ip6tables" |
| 86 | #define IPTABLES_PATH "/system/bin/iptables" |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 87 | #define RAW_TABLE "raw" |
| 88 | #define MANGLE_TABLE "mangle" |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 89 | #define FILTER_TABLE "filter" |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 90 | #define NAT_TABLE "nat" |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 91 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 92 | namespace binder = android::binder; |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 93 | |
| 94 | using android::IBinder; |
| 95 | using android::IServiceManager; |
| 96 | using android::sp; |
| 97 | using android::String16; |
| 98 | using android::String8; |
| 99 | using android::base::Join; |
Lorenzo Colitti | 1b21374 | 2020-03-27 17:50:19 +0900 | [diff] [blame] | 100 | using android::base::make_scope_guard; |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 101 | using android::base::ReadFileToString; |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 102 | using android::base::StartsWith; |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 103 | using android::base::StringPrintf; |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 104 | using android::base::Trim; |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 105 | using android::base::unique_fd; |
paulhu | 7274295 | 2022-02-09 21:24:09 +0800 | [diff] [blame] | 106 | using android::binder::Status; |
Yan Yan | f9c4679 | 2022-10-13 00:25:43 +0000 | [diff] [blame] | 107 | using android::bpf::isAtLeastKernelVersion; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 108 | using android::net::INetd; |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 109 | using android::net::InterfaceConfigurationParcel; |
| 110 | using android::net::InterfaceController; |
Chiachang Wang | 00fc62f | 2019-12-04 20:38:26 +0800 | [diff] [blame] | 111 | using android::net::MarkMaskParcel; |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 112 | using android::net::NativeNetworkConfig; |
| 113 | using android::net::NativeNetworkType; |
| 114 | using android::net::NativeVpnType; |
Ken Chen | d81356c | 2023-08-24 18:39:26 +0800 | [diff] [blame] | 115 | using android::net::NetworkController; |
Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 116 | using android::net::RULE_PRIORITY_BYPASSABLE_VPN_LOCAL_EXCLUSION; |
| 117 | using android::net::RULE_PRIORITY_BYPASSABLE_VPN_NO_LOCAL_EXCLUSION; |
Ken Chen | 7099e2e | 2021-02-08 00:57:47 +0800 | [diff] [blame] | 118 | using android::net::RULE_PRIORITY_DEFAULT_NETWORK; |
| 119 | using android::net::RULE_PRIORITY_EXPLICIT_NETWORK; |
Chalard Jean | e479f31 | 2022-12-12 18:27:49 +0900 | [diff] [blame] | 120 | using android::net::RULE_PRIORITY_LOCAL_NETWORK; |
Chiachang Wang | e7bf5f5 | 2022-01-17 11:37:39 +0800 | [diff] [blame] | 121 | using android::net::RULE_PRIORITY_LOCAL_ROUTES; |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 122 | using android::net::RULE_PRIORITY_OUTPUT_INTERFACE; |
Ken Chen | 7099e2e | 2021-02-08 00:57:47 +0800 | [diff] [blame] | 123 | using android::net::RULE_PRIORITY_PROHIBIT_NON_VPN; |
Ken Chen | 69839af | 2021-01-05 22:48:32 +0800 | [diff] [blame] | 124 | using android::net::RULE_PRIORITY_SECURE_VPN; |
Ken Chen | 7099e2e | 2021-02-08 00:57:47 +0800 | [diff] [blame] | 125 | using android::net::RULE_PRIORITY_TETHERING; |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 126 | using android::net::RULE_PRIORITY_UID_DEFAULT_NETWORK; |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 127 | using android::net::RULE_PRIORITY_UID_DEFAULT_UNREACHABLE; |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 128 | using android::net::RULE_PRIORITY_UID_EXPLICIT_NETWORK; |
| 129 | using android::net::RULE_PRIORITY_UID_IMPLICIT_NETWORK; |
chiachangwang | 0d5ae98 | 2022-05-17 05:16:16 +0000 | [diff] [blame] | 130 | using android::net::RULE_PRIORITY_UID_LOCAL_ROUTES; |
Ken Chen | 69839af | 2021-01-05 22:48:32 +0800 | [diff] [blame] | 131 | using android::net::RULE_PRIORITY_VPN_FALLTHROUGH; |
Lorenzo Colitti | 1b21374 | 2020-03-27 17:50:19 +0900 | [diff] [blame] | 132 | using android::net::SockDiag; |
Lorenzo Colitti | 182cd3c | 2020-04-04 00:44:01 +0900 | [diff] [blame] | 133 | using android::net::TetherOffloadRuleParcel; |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 134 | using android::net::TetherStatsParcel; |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 135 | using android::net::TunInterface; |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 136 | using android::net::UidRangeParcel; |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 137 | using android::net::UidRanges; |
| 138 | using android::net::netd::aidl::NativeUidRangeConfig; |
waynema | 5851b03 | 2021-11-24 17:08:25 +0800 | [diff] [blame] | 139 | using android::netdutils::getIfaceNames; |
Lorenzo Colitti | 1b21374 | 2020-03-27 17:50:19 +0900 | [diff] [blame] | 140 | using android::netdutils::IPAddress; |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 141 | using android::netdutils::IPSockAddr; |
Luke Huang | 2559932 | 2019-06-14 00:34:05 +0800 | [diff] [blame] | 142 | using android::netdutils::ScopedAddrinfo; |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 143 | using android::netdutils::sSyscalls; |
Mike Yu | e7e332f | 2019-03-13 17:15:48 +0800 | [diff] [blame] | 144 | using android::netdutils::Stopwatch; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 145 | |
| 146 | static const char* IP_RULE_V4 = "-4"; |
| 147 | static const char* IP_RULE_V6 = "-6"; |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 148 | static const int TEST_NETID1 = 65501; |
| 149 | static const int TEST_NETID2 = 65502; |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 150 | static const int TEST_NETID3 = 65503; |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 151 | static const int TEST_NETID4 = 65504; |
Lorenzo Colitti | 23554ba | 2020-11-05 01:43:46 +0900 | [diff] [blame] | 152 | static const int TEST_DUMP_NETID = 65123; |
Lorenzo Colitti | 1b21374 | 2020-03-27 17:50:19 +0900 | [diff] [blame] | 153 | static const char* DNSMASQ = "dnsmasq"; |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 154 | |
| 155 | // Use maximum reserved appId for applications to avoid conflict with existing |
| 156 | // uids. |
| 157 | static const int TEST_UID1 = 99999; |
| 158 | static const int TEST_UID2 = 99998; |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 159 | static const int TEST_UID3 = 99997; |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 160 | static const int TEST_UID4 = 99996; |
| 161 | static const int TEST_UID5 = 99995; |
| 162 | static const int TEST_UID6 = 99994; |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 163 | |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 164 | constexpr int BASE_UID = AID_USER_OFFSET * 5; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 165 | |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 166 | static const std::string NO_SOCKET_ALLOW_RULE("! owner UID match 0-4294967294"); |
| 167 | static const std::string ESP_ALLOW_RULE("esp"); |
| 168 | |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 169 | static const in6_addr V6_ADDR = { |
| 170 | {// 2001:db8:cafe::8888 |
| 171 | .u6_addr8 = {0x20, 0x01, 0x0d, 0xb8, 0xca, 0xfe, 0, 0, 0, 0, 0, 0, 0, 0, 0x88, 0x88}}}; |
| 172 | |
Chalard Jean | e479f31 | 2022-12-12 18:27:49 +0900 | [diff] [blame] | 173 | typedef enum { ALL_EXIST, NONE_EXIST } ExistMode; |
| 174 | |
Patrick Rohr | 088276b | 2024-05-15 11:36:07 -0700 | [diff] [blame] | 175 | static void clearQueue(int tunFd) { |
| 176 | char buf[4096]; |
| 177 | int ret; |
| 178 | do { |
| 179 | ret = read(tunFd, buf, sizeof(buf)); |
| 180 | } while (ret > 0); |
| 181 | } |
| 182 | |
Paul Hu | 1c64f08 | 2022-06-21 03:42:50 +0000 | [diff] [blame] | 183 | class NetdBinderTest : public NetNativeTestBase { |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 184 | public: |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 185 | NetdBinderTest() { |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 186 | sp<IServiceManager> sm = android::defaultServiceManager(); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 187 | sp<IBinder> binder = sm->getService(String16("netd")); |
| 188 | if (binder != nullptr) { |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 189 | mNetd = android::interface_cast<INetd>(binder); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 193 | void SetUp() override { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 194 | ASSERT_NE(nullptr, mNetd.get()); |
Patrick Rohr | 088276b | 2024-05-15 11:36:07 -0700 | [diff] [blame] | 195 | // drain tun interfaces before every test. |
| 196 | clearQueue(sTun.getFdForTesting()); |
| 197 | clearQueue(sTun2.getFdForTesting()); |
| 198 | clearQueue(sTun3.getFdForTesting()); |
| 199 | clearQueue(sTun4.getFdForTesting()); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 200 | } |
| 201 | |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 202 | void TearDown() override { |
| 203 | mNetd->networkDestroy(TEST_NETID1); |
| 204 | mNetd->networkDestroy(TEST_NETID2); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 205 | mNetd->networkDestroy(TEST_NETID3); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 206 | mNetd->networkDestroy(TEST_NETID4); |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 207 | setNetworkForProcess(NETID_UNSET); |
| 208 | // Restore default network |
| 209 | if (mStoredDefaultNetwork >= 0) mNetd->networkSetDefault(mStoredDefaultNetwork); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 210 | } |
| 211 | |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 212 | bool allocateIpSecResources(bool expectOk, int32_t* spi); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 213 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 214 | // Static because setting up the tun interface takes about 40ms. |
| 215 | static void SetUpTestCase() { |
Suprabh Shukla | 995728e | 2024-02-07 13:22:49 -0800 | [diff] [blame] | 216 | setBackgroundNetworkingEnabledForUid(TEST_UID1, true); |
| 217 | setBackgroundNetworkingEnabledForUid(TEST_UID2, true); |
| 218 | setBackgroundNetworkingEnabledForUid(TEST_UID3, true); |
| 219 | setBackgroundNetworkingEnabledForUid(TEST_UID4, true); |
| 220 | setBackgroundNetworkingEnabledForUid(TEST_UID5, true); |
| 221 | setBackgroundNetworkingEnabledForUid(TEST_UID6, true); |
| 222 | |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 223 | ASSERT_EQ(0, sTun.init()); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 224 | ASSERT_EQ(0, sTun2.init()); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 225 | ASSERT_EQ(0, sTun3.init()); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 226 | ASSERT_EQ(0, sTun4.init()); |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 227 | ASSERT_LE(sTun.name().size(), static_cast<size_t>(IFNAMSIZ)); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 228 | ASSERT_LE(sTun2.name().size(), static_cast<size_t>(IFNAMSIZ)); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 229 | ASSERT_LE(sTun3.name().size(), static_cast<size_t>(IFNAMSIZ)); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 230 | ASSERT_LE(sTun4.name().size(), static_cast<size_t>(IFNAMSIZ)); |
Patrick Rohr | 088276b | 2024-05-15 11:36:07 -0700 | [diff] [blame] | 231 | |
| 232 | // Wait for initial IPv6 packets (MLD, DAD, and RS) to be sent on tuns, so they can be |
| 233 | // drained in SetUp(). |
| 234 | sleep(1); |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static void TearDownTestCase() { |
Suprabh Shukla | 995728e | 2024-02-07 13:22:49 -0800 | [diff] [blame] | 238 | setBackgroundNetworkingEnabledForUid(TEST_UID1, false); |
| 239 | setBackgroundNetworkingEnabledForUid(TEST_UID2, false); |
| 240 | setBackgroundNetworkingEnabledForUid(TEST_UID3, false); |
| 241 | setBackgroundNetworkingEnabledForUid(TEST_UID4, false); |
| 242 | setBackgroundNetworkingEnabledForUid(TEST_UID5, false); |
| 243 | setBackgroundNetworkingEnabledForUid(TEST_UID6, false); |
| 244 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 245 | // Closing the socket removes the interface and IP addresses. |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 246 | sTun.destroy(); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 247 | sTun2.destroy(); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 248 | sTun3.destroy(); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 249 | sTun4.destroy(); |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 250 | } |
| 251 | |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 252 | static void fakeRemoteSocketPair(unique_fd* clientSocket, unique_fd* serverSocket, |
| 253 | unique_fd* acceptedSocket); |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 254 | |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 255 | void createVpnNetworkWithUid(bool secure, uid_t uid, int vpnNetId = TEST_NETID2, |
Chiachang | 2271c12 | 2022-05-04 07:45:37 +0000 | [diff] [blame] | 256 | int fallthroughNetId = TEST_NETID1, |
| 257 | int nonDefaultNetId = TEST_NETID3); |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 258 | |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 259 | void createAndSetDefaultNetwork(int netId, const std::string& interface, |
| 260 | int permission = INetd::PERMISSION_NONE); |
| 261 | |
| 262 | void createPhysicalNetwork(int netId, const std::string& interface, |
| 263 | int permission = INetd::PERMISSION_NONE); |
| 264 | |
| 265 | void createDefaultAndOtherPhysicalNetwork(int defaultNetId, int otherNetId); |
| 266 | |
| 267 | void createVpnAndOtherPhysicalNetwork(int systemDefaultNetId, int otherNetId, int vpnNetId, |
| 268 | bool secure); |
| 269 | |
| 270 | void createVpnAndAppDefaultNetworkWithUid(int systemDefaultNetId, int appDefaultNetId, |
| 271 | int vpnNetId, bool secure, |
| 272 | std::vector<UidRangeParcel>&& appDefaultUidRanges, |
| 273 | std::vector<UidRangeParcel>&& vpnUidRanges); |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 274 | |
| 275 | void setupNetworkRoutesForVpnAndDefaultNetworks( |
| 276 | int systemDefaultNetId, int appDefaultNetId, int vpnNetId, int otherNetId, bool secure, |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 277 | bool testV6, bool differentLocalRoutes, |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 278 | std::vector<UidRangeParcel>&& appDefaultUidRanges, |
| 279 | std::vector<UidRangeParcel>&& vpnUidRanges); |
| 280 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 281 | protected: |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 282 | // Use -1 to represent that default network was not modified because |
| 283 | // real netId must be an unsigned value. |
| 284 | int mStoredDefaultNetwork = -1; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 285 | sp<INetd> mNetd; |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 286 | static TunInterface sTun; |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 287 | static TunInterface sTun2; |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 288 | static TunInterface sTun3; |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 289 | static TunInterface sTun4; |
Suprabh Shukla | 995728e | 2024-02-07 13:22:49 -0800 | [diff] [blame] | 290 | |
| 291 | private: |
| 292 | static void setBackgroundNetworkingEnabledForUid(int uid, bool enabled) { |
| 293 | runBinderCommand("connectivity", |
| 294 | StringPrintf("set-background-networking-enabled-for-uid %d %s", uid, |
| 295 | enabled ? "true" : "false")); |
| 296 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 297 | }; |
| 298 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 299 | TunInterface NetdBinderTest::sTun; |
| 300 | TunInterface NetdBinderTest::sTun2; |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 301 | TunInterface NetdBinderTest::sTun3; |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 302 | TunInterface NetdBinderTest::sTun4; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 303 | |
Lorenzo Colitti | 699aa99 | 2016-04-15 10:22:37 +0900 | [diff] [blame] | 304 | class TimedOperation : public Stopwatch { |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 305 | public: |
Chih-Hung Hsieh | 1805105 | 2016-05-06 10:36:13 -0700 | [diff] [blame] | 306 | explicit TimedOperation(const std::string &name): mName(name) {} |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 307 | virtual ~TimedOperation() { |
Bernie Innocenti | 196f1b8 | 2019-05-20 16:34:16 +0900 | [diff] [blame] | 308 | std::cerr << " " << mName << ": " << timeTakenUs() << "us" << std::endl; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 309 | } |
| 310 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 311 | private: |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 312 | std::string mName; |
| 313 | }; |
| 314 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 315 | TEST_F(NetdBinderTest, IsAlive) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 316 | TimedOperation t("isAlive RPC"); |
| 317 | bool isAlive = false; |
| 318 | mNetd->isAlive(&isAlive); |
| 319 | ASSERT_TRUE(isAlive); |
| 320 | } |
| 321 | |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 322 | namespace { |
| 323 | |
| 324 | NativeNetworkConfig makeNativeNetworkConfig(int netId, NativeNetworkType networkType, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 325 | int permission, bool secure, bool excludeLocalRoutes) { |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 326 | NativeNetworkConfig config = {}; |
| 327 | config.netId = netId; |
| 328 | config.networkType = networkType; |
| 329 | config.permission = permission; |
| 330 | config.secure = secure; |
| 331 | // The vpnType doesn't matter in AOSP. Just pick a well defined one from INetd. |
| 332 | config.vpnType = NativeVpnType::PLATFORM; |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 333 | config.excludeLocalRoutes = excludeLocalRoutes; |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 334 | return config; |
| 335 | } |
| 336 | |
| 337 | } // namespace |
| 338 | |
Ken Chen | a2238de | 2021-04-27 13:32:40 +0800 | [diff] [blame] | 339 | bool testNetworkExistsButCannotConnect(const sp<INetd>& netd, TunInterface& ifc, const int netId) { |
Chalard Jean | d96b0f2 | 2021-01-20 14:16:06 +0900 | [diff] [blame] | 340 | // If this network exists, we should definitely not be able to create it. |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 341 | // Note that this networkCreate is never allowed to create reserved network IDs, so |
Chalard Jean | d96b0f2 | 2021-01-20 14:16:06 +0900 | [diff] [blame] | 342 | // this call may fail for other reasons than the network already existing. |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 343 | const auto& config = makeNativeNetworkConfig(netId, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 344 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 345 | EXPECT_FALSE(netd->networkCreate(config).isOk()); |
Ken Chen | a2238de | 2021-04-27 13:32:40 +0800 | [diff] [blame] | 346 | // Test if the network exist by adding interface. INetd has no dedicated method to query. When |
| 347 | // the network exists and the interface can be added, the function succeeds. When the network |
| 348 | // exists but the interface cannot be added, it fails with EINVAL, otherwise it is ENONET. |
| 349 | binder::Status status = netd->networkAddInterface(netId, ifc.name()); |
| 350 | if (status.isOk()) { // clean up |
| 351 | EXPECT_TRUE(netd->networkRemoveInterface(netId, ifc.name()).isOk()); |
| 352 | } else if (status.serviceSpecificErrorCode() == ENONET) { |
| 353 | return false; |
| 354 | } |
Chalard Jean | d96b0f2 | 2021-01-20 14:16:06 +0900 | [diff] [blame] | 355 | |
| 356 | const sockaddr_in6 sin6 = {.sin6_family = AF_INET6, |
Maciej Żenczykowski | 861bef8 | 2023-10-22 22:18:11 -0700 | [diff] [blame] | 357 | .sin6_port = htons(53), |
Chalard Jean | d96b0f2 | 2021-01-20 14:16:06 +0900 | [diff] [blame] | 358 | .sin6_addr = {{.u6_addr32 = {htonl(0x20010db8), 0, 0, 0}}}, |
Maciej Żenczykowski | 3d0b37e | 2023-10-22 11:06:01 -0700 | [diff] [blame] | 359 | }; |
Chalard Jean | d96b0f2 | 2021-01-20 14:16:06 +0900 | [diff] [blame] | 360 | const int s = socket(AF_INET6, SOCK_DGRAM, 0); |
Ken Chen | a2238de | 2021-04-27 13:32:40 +0800 | [diff] [blame] | 361 | EXPECT_NE(-1, s); |
| 362 | if (s == -1) return true; |
Lorenzo Colitti | bf1c0e3 | 2021-02-05 18:04:21 +0900 | [diff] [blame] | 363 | Fwmark fwmark; |
| 364 | fwmark.explicitlySelected = true; |
| 365 | fwmark.netId = netId; |
| 366 | EXPECT_EQ(0, setsockopt(s, SOL_SOCKET, SO_MARK, &fwmark.intValue, sizeof(fwmark.intValue))); |
Chalard Jean | d96b0f2 | 2021-01-20 14:16:06 +0900 | [diff] [blame] | 367 | const int ret = connect(s, (struct sockaddr*)&sin6, sizeof(sin6)); |
| 368 | const int err = errno; |
| 369 | EXPECT_EQ(-1, ret); |
| 370 | EXPECT_EQ(ENETUNREACH, err); |
| 371 | close(s); |
Ken Chen | a2238de | 2021-04-27 13:32:40 +0800 | [diff] [blame] | 372 | return true; |
Chalard Jean | d96b0f2 | 2021-01-20 14:16:06 +0900 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | TEST_F(NetdBinderTest, InitialNetworksExist) { |
Ken Chen | a2238de | 2021-04-27 13:32:40 +0800 | [diff] [blame] | 376 | EXPECT_TRUE(testNetworkExistsButCannotConnect(mNetd, sTun, INetd::DUMMY_NET_ID)); |
| 377 | EXPECT_TRUE(testNetworkExistsButCannotConnect(mNetd, sTun, INetd::LOCAL_NET_ID)); |
| 378 | EXPECT_TRUE(testNetworkExistsButCannotConnect(mNetd, sTun, INetd::UNREACHABLE_NET_ID)); |
| 379 | EXPECT_FALSE(testNetworkExistsButCannotConnect(mNetd, sTun, 77 /* not exist */)); |
Chalard Jean | d96b0f2 | 2021-01-20 14:16:06 +0900 | [diff] [blame] | 380 | } |
| 381 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 382 | TEST_F(NetdBinderTest, IpSecTunnelInterface) { |
George Burgess IV | c4a6d27 | 2018-05-21 14:41:57 -0700 | [diff] [blame] | 383 | const struct TestData { |
| 384 | const std::string family; |
| 385 | const std::string deviceName; |
| 386 | const std::string localAddress; |
| 387 | const std::string remoteAddress; |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 388 | int32_t iKey; |
| 389 | int32_t oKey; |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 390 | int32_t ifId; |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 391 | } kTestData[] = { |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 392 | {"IPV4", "ipsec_test", "127.0.0.1", "8.8.8.8", 0x1234 + 53, 0x1234 + 53, 0xFFFE}, |
| 393 | {"IPV6", "ipsec_test6", "::1", "2001:4860:4860::8888", 0x1234 + 50, 0x1234 + 50, |
| 394 | 0xFFFE}, |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 395 | }; |
| 396 | |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 397 | for (size_t i = 0; i < std::size(kTestData); i++) { |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 398 | const auto& td = kTestData[i]; |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 399 | |
| 400 | binder::Status status; |
| 401 | |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 402 | // Create Tunnel Interface. |
| 403 | status = mNetd->ipSecAddTunnelInterface(td.deviceName, td.localAddress, td.remoteAddress, |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 404 | td.iKey, td.oKey, td.ifId); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 405 | EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage(); |
| 406 | |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 407 | // Check that the interface exists |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 408 | EXPECT_NE(0U, if_nametoindex(td.deviceName.c_str())); |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 409 | |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 410 | // Update Tunnel Interface. |
| 411 | status = mNetd->ipSecUpdateTunnelInterface(td.deviceName, td.localAddress, td.remoteAddress, |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 412 | td.iKey, td.oKey, td.ifId); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 413 | EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage(); |
| 414 | |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 415 | // Remove Tunnel Interface. |
| 416 | status = mNetd->ipSecRemoveTunnelInterface(td.deviceName); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 417 | EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage(); |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 418 | |
| 419 | // Check that the interface no longer exists |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 420 | EXPECT_EQ(0U, if_nametoindex(td.deviceName.c_str())); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 424 | TEST_F(NetdBinderTest, IpSecSetEncapSocketOwner) { |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 425 | unique_fd uniqueFd(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
Benedict Wong | 1cb73df | 2018-12-03 00:54:14 -0800 | [diff] [blame] | 426 | android::os::ParcelFileDescriptor sockFd(std::move(uniqueFd)); |
| 427 | |
| 428 | int sockOptVal = UDP_ENCAP_ESPINUDP; |
| 429 | setsockopt(sockFd.get(), IPPROTO_UDP, UDP_ENCAP, &sockOptVal, sizeof(sockOptVal)); |
| 430 | |
| 431 | binder::Status res = mNetd->ipSecSetEncapSocketOwner(sockFd, 1001); |
| 432 | EXPECT_TRUE(res.isOk()); |
| 433 | |
| 434 | struct stat info; |
| 435 | EXPECT_EQ(0, fstat(sockFd.get(), &info)); |
| 436 | EXPECT_EQ(1001, (int) info.st_uid); |
| 437 | } |
| 438 | |
Nathan Harold | 2deff32 | 2018-05-10 14:03:48 -0700 | [diff] [blame] | 439 | // IPsec tests are not run in 32 bit mode; both 32-bit kernels and |
| 440 | // mismatched ABIs (64-bit kernel with 32-bit userspace) are unsupported. |
| 441 | #if INTPTR_MAX != INT32_MAX |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 442 | |
| 443 | using android::net::XfrmController; |
| 444 | |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 445 | static const int XFRM_DIRECTIONS[] = {static_cast<int>(android::net::XfrmDirection::IN), |
| 446 | static_cast<int>(android::net::XfrmDirection::OUT)}; |
| 447 | static const int ADDRESS_FAMILIES[] = {AF_INET, AF_INET6}; |
| 448 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 449 | #define RETURN_FALSE_IF_NEQ(_expect_, _ret_) \ |
| 450 | do { if ((_expect_) != (_ret_)) return false; } while(false) |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 451 | bool NetdBinderTest::allocateIpSecResources(bool expectOk, int32_t* spi) { |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 452 | android::netdutils::Status status = XfrmController::ipSecAllocateSpi(0, "::", "::1", 123, spi); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 453 | SCOPED_TRACE(status); |
| 454 | RETURN_FALSE_IF_NEQ(status.ok(), expectOk); |
| 455 | |
| 456 | // Add a policy |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 457 | status = XfrmController::ipSecAddSecurityPolicy(0, AF_INET6, 0, "::", "::1", 123, 0, 0, 0); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 458 | SCOPED_TRACE(status); |
| 459 | RETURN_FALSE_IF_NEQ(status.ok(), expectOk); |
| 460 | |
| 461 | // Add an ipsec interface |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 462 | return expectOk == XfrmController::ipSecAddTunnelInterface("ipsec_test", "::", "::1", 0xF00D, |
| 463 | 0xD00D, 0xE00D, false) |
| 464 | .ok(); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 467 | TEST_F(NetdBinderTest, XfrmDualSelectorTunnelModePoliciesV4) { |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 468 | android::binder::Status status; |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 469 | |
| 470 | // Repeat to ensure cleanup and recreation works correctly |
| 471 | for (int i = 0; i < 2; i++) { |
| 472 | for (int direction : XFRM_DIRECTIONS) { |
| 473 | for (int addrFamily : ADDRESS_FAMILIES) { |
| 474 | status = mNetd->ipSecAddSecurityPolicy(0, addrFamily, direction, "127.0.0.5", |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 475 | "127.0.0.6", 123, 0, 0, 0); |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 476 | EXPECT_TRUE(status.isOk()) |
| 477 | << " family: " << addrFamily << " direction: " << direction; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | // Cleanup |
| 482 | for (int direction : XFRM_DIRECTIONS) { |
| 483 | for (int addrFamily : ADDRESS_FAMILIES) { |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 484 | status = mNetd->ipSecDeleteSecurityPolicy(0, addrFamily, direction, 0, 0, 0); |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 485 | EXPECT_TRUE(status.isOk()); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 491 | TEST_F(NetdBinderTest, XfrmDualSelectorTunnelModePoliciesV6) { |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 492 | binder::Status status; |
| 493 | |
| 494 | // Repeat to ensure cleanup and recreation works correctly |
| 495 | for (int i = 0; i < 2; i++) { |
| 496 | for (int direction : XFRM_DIRECTIONS) { |
| 497 | for (int addrFamily : ADDRESS_FAMILIES) { |
| 498 | status = mNetd->ipSecAddSecurityPolicy(0, addrFamily, direction, "2001:db8::f00d", |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 499 | "2001:db8::d00d", 123, 0, 0, 0); |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 500 | EXPECT_TRUE(status.isOk()) |
| 501 | << " family: " << addrFamily << " direction: " << direction; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | // Cleanup |
| 506 | for (int direction : XFRM_DIRECTIONS) { |
| 507 | for (int addrFamily : ADDRESS_FAMILIES) { |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 508 | status = mNetd->ipSecDeleteSecurityPolicy(0, addrFamily, direction, 0, 0, 0); |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 509 | EXPECT_TRUE(status.isOk()); |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 515 | TEST_F(NetdBinderTest, XfrmControllerInit) { |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 516 | android::netdutils::Status status; |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 517 | status = XfrmController::Init(); |
| 518 | SCOPED_TRACE(status); |
Nathan Harold | 2deff32 | 2018-05-10 14:03:48 -0700 | [diff] [blame] | 519 | |
| 520 | // Older devices or devices with mismatched Kernel/User ABI cannot support the IPsec |
| 521 | // feature. |
| 522 | if (status.code() == EOPNOTSUPP) return; |
| 523 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 524 | ASSERT_TRUE(status.ok()); |
| 525 | |
| 526 | int32_t spi = 0; |
| 527 | |
| 528 | ASSERT_TRUE(allocateIpSecResources(true, &spi)); |
| 529 | ASSERT_TRUE(allocateIpSecResources(false, &spi)); |
| 530 | |
| 531 | status = XfrmController::Init(); |
Nathan Harold | 39ad662 | 2018-04-25 12:56:56 -0700 | [diff] [blame] | 532 | ASSERT_TRUE(status.ok()); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 533 | ASSERT_TRUE(allocateIpSecResources(true, &spi)); |
| 534 | |
| 535 | // Clean up |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 536 | status = XfrmController::ipSecDeleteSecurityAssociation(0, "::", "::1", 123, spi, 0, 0); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 537 | SCOPED_TRACE(status); |
| 538 | ASSERT_TRUE(status.ok()); |
| 539 | |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 540 | status = XfrmController::ipSecDeleteSecurityPolicy(0, AF_INET6, 0, 0, 0, 0); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 541 | SCOPED_TRACE(status); |
| 542 | ASSERT_TRUE(status.ok()); |
| 543 | |
| 544 | // Remove Virtual Tunnel Interface. |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 545 | ASSERT_TRUE(XfrmController::ipSecRemoveTunnelInterface("ipsec_test").ok()); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 546 | } |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 547 | |
Yan Yan | f9c4679 | 2022-10-13 00:25:43 +0000 | [diff] [blame] | 548 | // Two kernel fixes have been added in 5.17 to allow XFRM_MIGRATE to work correctly |
| 549 | // when (1) there are multiple tunnels with the same selectors; and (2) addresses |
| 550 | // are updated to a different IP family. These two fixes were pulled into upstream |
| 551 | // LTS releases 4.14.273, 4.19.236, 5.4.186, 5.10.107 and 5.15.30, from whence they |
| 552 | // flowed into the Android Common Kernel (via standard LTS merges). |
| 553 | // As such we require 4.14.273+, 4.19.236+, 5.4.186+, 5.10.107+, 5.15.30+ and 5.17+ |
| 554 | // to have these fixes. |
| 555 | bool hasXfrmMigrateKernelFixes() { |
| 556 | return (isAtLeastKernelVersion(4, 14, 273) && !isAtLeastKernelVersion(4, 19, 0)) || |
| 557 | (isAtLeastKernelVersion(4, 19, 236) && !isAtLeastKernelVersion(5, 4, 0)) || |
| 558 | (isAtLeastKernelVersion(5, 4, 186) && !isAtLeastKernelVersion(5, 10, 0)) || |
| 559 | (isAtLeastKernelVersion(5, 10, 107) && !isAtLeastKernelVersion(5, 15, 0)) || |
| 560 | isAtLeastKernelVersion(5, 15, 30); |
| 561 | } |
| 562 | |
| 563 | // Does the kernel support CONFIG_XFRM_MIGRATE and include the kernel fixes? |
| 564 | bool supportsXfrmMigrate() { |
| 565 | if (!hasXfrmMigrateKernelFixes()) return false; |
| 566 | |
| 567 | // 5.10+ VINTF requires CONFIG_XFRM_MIGRATE enabled |
| 568 | if (isAtLeastKernelVersion(5, 10, 0)) return true; |
| 569 | |
| 570 | const std::string wildcardAddr = "::"; |
| 571 | |
| 572 | // Expect migration to fail with EINVAL because it is trying to migrate a |
| 573 | // non-existent SA. |
| 574 | auto status = XfrmController::ipSecMigrate( |
| 575 | 0 /* resourceId */, AF_INET6, 0 /* direction == out */, |
| 576 | wildcardAddr /* sourceAddress */, wildcardAddr /* destinationAddress */, |
| 577 | wildcardAddr /* newSourceAddress */, wildcardAddr /* newDestinationAddress */, |
| 578 | 0 /* xfrmInterfaceId */); |
| 579 | |
| 580 | if (android::netdutils::equalToErrno(status, EINVAL)) { |
| 581 | return true; |
| 582 | } else if (android::netdutils::equalToErrno(status, ENOPROTOOPT)) { |
| 583 | return false; |
| 584 | } else { |
| 585 | GTEST_LOG_(WARNING) << "Unexpected migration result: " |
| 586 | << android::netdutils::toString(status) |
| 587 | << "Assuming XFRM_MIGRATE is enabled."; |
| 588 | return true; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | #define SKIP_IF_XFRM_MIGRATE_NOT_SUPPORTED \ |
| 593 | do { \ |
| 594 | if (!supportsXfrmMigrate()) \ |
| 595 | GTEST_SKIP() << "This test is skipped since xfrm migrate feature " \ |
| 596 | << "not supported\n"; \ |
| 597 | } while (0) |
| 598 | |
| 599 | TEST_F(NetdBinderTest, XfrmMigrate) { |
| 600 | SKIP_IF_XFRM_MIGRATE_NOT_SUPPORTED; |
| 601 | |
| 602 | static const struct TestData { |
| 603 | const int32_t addrFamily; |
| 604 | const int32_t newAddrFamily; |
| 605 | const std::string srcAddr; |
| 606 | const std::string dstAddr; |
| 607 | const std::string newSrcAddr; |
| 608 | const std::string newDstAddr; |
| 609 | } kTestData[] = { |
| 610 | {AF_INET, AF_INET, "192.0.2.1", "192.0.2.2", "192.0.2.101", "192.0.2.102"}, |
| 611 | {AF_INET, AF_INET6, "192.0.2.1", "192.0.2.2", "2001:db8::101", "2001:db8::102"}, |
| 612 | {AF_INET6, AF_INET6, "2001:db8::1", "2001:db8::2", "2001:db8::101", "2001:db8::102"}, |
| 613 | {AF_INET6, AF_INET, "2001:db8::1", "2001:db8::2", "192.0.2.101", "192.0.2.102"}, |
| 614 | }; |
| 615 | |
| 616 | const int32_t xfrmInterfaceId = 0xFFFE; |
| 617 | const std::string tunnelDeviceName = "ipsec_test"; |
| 618 | |
| 619 | auto status = mNetd->ipSecAddTunnelInterface(tunnelDeviceName, "2001:db8::fe", "2001:db8::ff", |
| 620 | 0x1234 + 50 /* iKey */, 0x1234 + 50 /* oKey */, |
| 621 | xfrmInterfaceId); |
| 622 | |
| 623 | SCOPED_TRACE(status); |
| 624 | ASSERT_TRUE(status.isOk()); |
| 625 | |
| 626 | for (auto& td : kTestData) { |
| 627 | const int32_t direction = static_cast<int>(android::net::XfrmDirection::OUT); |
| 628 | const int32_t resourceId = 0; |
| 629 | const int32_t spiReq = 123; |
| 630 | int32_t spi = 0; |
| 631 | |
| 632 | status = mNetd->ipSecAllocateSpi(resourceId, td.srcAddr, td.dstAddr, spiReq, &spi); |
| 633 | SCOPED_TRACE(status); |
| 634 | ASSERT_TRUE(status.isOk()); |
| 635 | |
| 636 | status = mNetd->ipSecAddSecurityAssociation( |
| 637 | resourceId, static_cast<int32_t>(android::net::XfrmMode::TUNNEL), td.srcAddr, |
| 638 | td.dstAddr, 100 /* underlyingNetid */, spiReq, 0 /* markValue */, 0 /* markMask */, |
| 639 | "digest_null" /* authAlgo */, {} /* authKey */, 0 /* authTruncBits */, |
| 640 | "ecb(cipher_null)" /* cryptAlgo */, {} /* cryptKey */, 0 /* cryptTruncBits */, |
| 641 | "" /* aeadAlgo */, {} /* aeadKey */, 0 /* aeadIcvBits */, |
| 642 | 0 /* encapType == ENCAP_NONE */, 0 /* encapLocalPort */, 0 /* encapRemotePort */, |
| 643 | xfrmInterfaceId); |
| 644 | SCOPED_TRACE(status); |
| 645 | ASSERT_TRUE(status.isOk()); |
| 646 | |
| 647 | for (int addrFamily : ADDRESS_FAMILIES) { |
| 648 | // Add a policy |
| 649 | status = mNetd->ipSecAddSecurityPolicy(resourceId, addrFamily, direction, td.srcAddr, |
| 650 | td.dstAddr, spiReq, 0 /* markValue */, |
| 651 | 0 /* markMask */, xfrmInterfaceId); |
| 652 | SCOPED_TRACE(status); |
| 653 | ASSERT_TRUE(status.isOk()); |
| 654 | |
| 655 | // Migrate tunnel mode SA |
| 656 | android::net::IpSecMigrateInfoParcel parcel; |
| 657 | parcel.requestId = resourceId; |
| 658 | parcel.selAddrFamily = addrFamily; |
| 659 | parcel.direction = direction; |
| 660 | parcel.oldSourceAddress = td.srcAddr; |
| 661 | parcel.oldDestinationAddress = td.dstAddr; |
| 662 | parcel.newSourceAddress = td.newSrcAddr; |
| 663 | parcel.newDestinationAddress = td.newDstAddr; |
| 664 | parcel.interfaceId = xfrmInterfaceId; |
| 665 | |
| 666 | status = mNetd->ipSecMigrate(parcel); |
| 667 | SCOPED_TRACE(status); |
| 668 | ASSERT_TRUE(status.isOk()); |
| 669 | } |
| 670 | |
| 671 | // Clean up |
| 672 | status = mNetd->ipSecDeleteSecurityAssociation(resourceId, td.newSrcAddr, td.newDstAddr, |
| 673 | spiReq, 0 /* markValue */, 0 /* markMask */, |
| 674 | xfrmInterfaceId); |
| 675 | SCOPED_TRACE(status); |
| 676 | ASSERT_TRUE(status.isOk()); |
| 677 | |
| 678 | for (int addrFamily : ADDRESS_FAMILIES) { |
| 679 | status = mNetd->ipSecDeleteSecurityPolicy(resourceId, addrFamily, direction, |
| 680 | 0 /* markValue */, 0 /* markMask */, |
| 681 | xfrmInterfaceId); |
| 682 | SCOPED_TRACE(status); |
| 683 | ASSERT_TRUE(status.isOk()); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | // Remove Tunnel Interface. |
| 688 | status = mNetd->ipSecRemoveTunnelInterface(tunnelDeviceName); |
| 689 | SCOPED_TRACE(status); |
| 690 | EXPECT_TRUE(status.isOk()); |
| 691 | } |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 692 | #endif // INTPTR_MAX != INT32_MAX |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 693 | |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 694 | static int bandwidthDataSaverEnabled(const char *binary) { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 695 | std::vector<std::string> lines = listIptablesRule(binary, "bw_data_saver"); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 696 | |
| 697 | // Output looks like this: |
| 698 | // |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 699 | // Chain bw_data_saver (1 references) |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 700 | // target prot opt source destination |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 701 | // RETURN all -- 0.0.0.0/0 0.0.0.0/0 |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 702 | // |
| 703 | // or: |
| 704 | // |
| 705 | // Chain bw_data_saver (1 references) |
| 706 | // target prot opt source destination |
| 707 | // ... possibly connectivity critical packet rules here ... |
| 708 | // REJECT all -- ::/0 ::/0 |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 709 | |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 710 | EXPECT_GE(lines.size(), 3U); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 711 | |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 712 | if (lines.size() == 3 && StartsWith(lines[2], "RETURN ")) { |
| 713 | // Data saver disabled. |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | size_t minSize = (std::string(binary) == IPTABLES_PATH) ? 3 : 9; |
| 718 | |
| 719 | if (lines.size() >= minSize && StartsWith(lines[lines.size() -1], "REJECT ")) { |
| 720 | // Data saver enabled. |
| 721 | return 1; |
| 722 | } |
| 723 | |
| 724 | return -1; |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | bool enableDataSaver(sp<INetd>& netd, bool enable) { |
| 728 | TimedOperation op(enable ? " Enabling data saver" : "Disabling data saver"); |
| 729 | bool ret; |
| 730 | netd->bandwidthEnableDataSaver(enable, &ret); |
| 731 | return ret; |
| 732 | } |
| 733 | |
| 734 | int getDataSaverState() { |
| 735 | const int enabled4 = bandwidthDataSaverEnabled(IPTABLES_PATH); |
| 736 | const int enabled6 = bandwidthDataSaverEnabled(IP6TABLES_PATH); |
| 737 | EXPECT_EQ(enabled4, enabled6); |
| 738 | EXPECT_NE(-1, enabled4); |
| 739 | EXPECT_NE(-1, enabled6); |
| 740 | if (enabled4 != enabled6 || (enabled6 != 0 && enabled6 != 1)) { |
| 741 | return -1; |
| 742 | } |
| 743 | return enabled6; |
| 744 | } |
| 745 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 746 | TEST_F(NetdBinderTest, BandwidthEnableDataSaver) { |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 747 | const int wasEnabled = getDataSaverState(); |
| 748 | ASSERT_NE(-1, wasEnabled); |
| 749 | |
| 750 | if (wasEnabled) { |
| 751 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 752 | EXPECT_EQ(0, getDataSaverState()); |
| 753 | } |
| 754 | |
| 755 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 756 | EXPECT_EQ(0, getDataSaverState()); |
| 757 | |
| 758 | ASSERT_TRUE(enableDataSaver(mNetd, true)); |
| 759 | EXPECT_EQ(1, getDataSaverState()); |
| 760 | |
| 761 | ASSERT_TRUE(enableDataSaver(mNetd, true)); |
| 762 | EXPECT_EQ(1, getDataSaverState()); |
| 763 | |
| 764 | if (!wasEnabled) { |
| 765 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 766 | EXPECT_EQ(0, getDataSaverState()); |
| 767 | } |
| 768 | } |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 769 | |
Chalard Jean | e479f31 | 2022-12-12 18:27:49 +0900 | [diff] [blame] | 770 | static bool ipRuleExists(const char* ipVersion, const std::string& ipRule) { |
| 771 | std::vector<std::string> rules = listIpRules(ipVersion); |
Chalard Jean | 38bc084 | 2022-12-26 19:36:13 +0900 | [diff] [blame] | 772 | return std::find(rules.begin(), rules.end(), ipRule) != rules.end(); |
Chalard Jean | e479f31 | 2022-12-12 18:27:49 +0900 | [diff] [blame] | 773 | } |
| 774 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 775 | static bool ipRuleExistsForRange(const uint32_t priority, const UidRangeParcel& range, |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 776 | const std::string& action, const char* ipVersion, |
| 777 | const char* oif) { |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 778 | // Output looks like this: |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 779 | // "<priority>:\tfrom all iif lo oif netdc0ca6 uidrange 500000-500000 lookup netdc0ca6" |
Ken Chen | 7099e2e | 2021-02-08 00:57:47 +0800 | [diff] [blame] | 780 | // "<priority>:\tfrom all fwmark 0x0/0x20000 iif lo uidrange 1000-2000 prohibit" |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 781 | std::vector<std::string> rules = listIpRules(ipVersion); |
| 782 | |
| 783 | std::string prefix = StringPrintf("%" PRIu32 ":", priority); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 784 | std::string suffix; |
| 785 | if (oif) { |
Chalard Jean | 38bc084 | 2022-12-26 19:36:13 +0900 | [diff] [blame] | 786 | suffix = StringPrintf(" iif lo oif %s uidrange %d-%d %s", oif, range.start, range.stop, |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 787 | action.c_str()); |
| 788 | } else { |
Chalard Jean | 38bc084 | 2022-12-26 19:36:13 +0900 | [diff] [blame] | 789 | suffix = StringPrintf(" iif lo uidrange %d-%d %s", range.start, range.stop, action.c_str()); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 790 | } |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 791 | for (const auto& line : rules) { |
Elliott Hughes | 2f44508 | 2017-12-20 12:39:35 -0800 | [diff] [blame] | 792 | if (android::base::StartsWith(line, prefix) && android::base::EndsWith(line, suffix)) { |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 793 | return true; |
| 794 | } |
| 795 | } |
| 796 | return false; |
| 797 | } |
| 798 | |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 799 | // Overloads function with oif parameter for VPN rules compare. |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 800 | static bool ipRuleExistsForRange(const uint32_t priority, const UidRangeParcel& range, |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 801 | const std::string& action, const char* oif) { |
| 802 | bool existsIp4 = ipRuleExistsForRange(priority, range, action, IP_RULE_V4, oif); |
| 803 | bool existsIp6 = ipRuleExistsForRange(priority, range, action, IP_RULE_V6, oif); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 804 | EXPECT_EQ(existsIp4, existsIp6); |
| 805 | return existsIp4; |
| 806 | } |
| 807 | |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 808 | static bool ipRuleExistsForRange(const uint32_t priority, const UidRangeParcel& range, |
| 809 | const std::string& action) { |
| 810 | return ipRuleExistsForRange(priority, range, action, nullptr); |
| 811 | } |
| 812 | |
Chalard Jean | e479f31 | 2022-12-12 18:27:49 +0900 | [diff] [blame] | 813 | static void expectRuleForV4AndV6(ExistMode mode, const std::string& rule) { |
| 814 | for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { |
| 815 | if (mode == ALL_EXIST) { |
| 816 | EXPECT_TRUE(ipRuleExists(ipVersion, rule)); |
| 817 | } else { |
| 818 | EXPECT_FALSE(ipRuleExists(ipVersion, rule)); |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | static void expectLocalIpRuleExists(ExistMode mode, const std::string& ifName) { |
| 824 | std::string localIpRule = StringPrintf("%u:\tfrom all fwmark 0x0/0x10000 lookup %s", |
| 825 | RULE_PRIORITY_LOCAL_NETWORK, ifName.c_str()); |
| 826 | expectRuleForV4AndV6(mode, localIpRule); |
| 827 | |
| 828 | std::string dnsMasqRule = StringPrintf("%u:\tfrom all fwmark 0x10063/0x1ffff iif lo lookup %s", |
| 829 | RULE_PRIORITY_EXPLICIT_NETWORK, ifName.c_str()); |
| 830 | expectRuleForV4AndV6(mode, dnsMasqRule); |
| 831 | } |
| 832 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 833 | namespace { |
| 834 | |
| 835 | UidRangeParcel makeUidRangeParcel(int start, int stop) { |
| 836 | UidRangeParcel res; |
| 837 | res.start = start; |
| 838 | res.stop = stop; |
| 839 | |
| 840 | return res; |
| 841 | } |
| 842 | |
Ken Chen | a43e7ce | 2021-07-08 17:09:42 +0800 | [diff] [blame] | 843 | UidRangeParcel makeUidRangeParcel(int uid) { |
| 844 | return makeUidRangeParcel(uid, uid); |
| 845 | } |
| 846 | |
| 847 | NativeUidRangeConfig makeNativeUidRangeConfig(unsigned netId, std::vector<UidRangeParcel> uidRanges, |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 848 | int32_t subPriority) { |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 849 | NativeUidRangeConfig res; |
| 850 | res.netId = netId; |
donghongweng | a72addd | 2022-08-17 15:52:38 +0800 | [diff] [blame] | 851 | res.uidRanges = std::move(uidRanges); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 852 | res.subPriority = subPriority; |
| 853 | |
| 854 | return res; |
| 855 | } |
| 856 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 857 | } // namespace |
| 858 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 859 | TEST_F(NetdBinderTest, NetworkInterfaces) { |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 860 | auto config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 861 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 862 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
| 863 | EXPECT_EQ(EEXIST, mNetd->networkCreate(config).serviceSpecificErrorCode()); |
| 864 | |
| 865 | config.networkType = NativeNetworkType::VIRTUAL; |
| 866 | config.secure = true; |
| 867 | EXPECT_EQ(EEXIST, mNetd->networkCreate(config).serviceSpecificErrorCode()); |
| 868 | |
| 869 | config.netId = TEST_NETID2; |
| 870 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 871 | |
| 872 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 873 | EXPECT_EQ(EBUSY, |
| 874 | mNetd->networkAddInterface(TEST_NETID2, sTun.name()).serviceSpecificErrorCode()); |
| 875 | |
| 876 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 877 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID2, sTun.name()).isOk()); |
| 878 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID2).isOk()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 879 | EXPECT_EQ(ENONET, mNetd->networkDestroy(TEST_NETID1).serviceSpecificErrorCode()); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 880 | } |
| 881 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 882 | TEST_F(NetdBinderTest, NetworkUidRules) { |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 883 | auto config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::VIRTUAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 884 | INetd::PERMISSION_NONE, true, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 885 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
| 886 | EXPECT_EQ(EEXIST, mNetd->networkCreate(config).serviceSpecificErrorCode()); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 887 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 888 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 889 | std::vector<UidRangeParcel> uidRanges = {makeUidRangeParcel(BASE_UID + 8005, BASE_UID + 8012), |
| 890 | makeUidRangeParcel(BASE_UID + 8090, BASE_UID + 8099)}; |
| 891 | UidRangeParcel otherRange = makeUidRangeParcel(BASE_UID + 8190, BASE_UID + 8299); |
Chalard Jean | 38bc084 | 2022-12-26 19:36:13 +0900 | [diff] [blame] | 892 | std::string action = StringPrintf("lookup %s", sTun.name().c_str()); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 893 | |
| 894 | EXPECT_TRUE(mNetd->networkAddUidRanges(TEST_NETID1, uidRanges).isOk()); |
| 895 | |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 896 | EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[0], action)); |
| 897 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, otherRange, action)); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 898 | EXPECT_TRUE(mNetd->networkRemoveUidRanges(TEST_NETID1, uidRanges).isOk()); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 899 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[0], action)); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 900 | |
| 901 | EXPECT_TRUE(mNetd->networkAddUidRanges(TEST_NETID1, uidRanges).isOk()); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 902 | EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[1], action)); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 903 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 904 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[1], action)); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 905 | |
| 906 | EXPECT_EQ(ENONET, mNetd->networkDestroy(TEST_NETID1).serviceSpecificErrorCode()); |
| 907 | } |
| 908 | |
Chalard Jean | e479f31 | 2022-12-12 18:27:49 +0900 | [diff] [blame] | 909 | class LocalNetworkParameterizedTest : public NetdBinderTest, |
| 910 | public testing::WithParamInterface<bool> {}; |
| 911 | |
| 912 | // Exercise both local and non-local networks |
| 913 | INSTANTIATE_TEST_SUITE_P(LocalNetworkTests, LocalNetworkParameterizedTest, testing::Bool(), |
| 914 | [](const testing::TestParamInfo<bool>& info) { |
| 915 | return info.param ? "Local" : "NonLocal"; |
| 916 | }); |
| 917 | |
| 918 | TEST_P(LocalNetworkParameterizedTest, LocalNetworkUidRules) { |
| 919 | const bool local = GetParam(); |
| 920 | const auto type = local ? NativeNetworkType::PHYSICAL_LOCAL : NativeNetworkType::PHYSICAL; |
| 921 | auto config = makeNativeNetworkConfig(TEST_NETID1, type, INetd::PERMISSION_NONE, |
| 922 | false /* secure */, false /* excludeLocalRoutes */); |
| 923 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
| 924 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 925 | |
| 926 | expectLocalIpRuleExists(local ? ALL_EXIST : NONE_EXIST, sTun.name()); |
| 927 | } |
| 928 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 929 | TEST_F(NetdBinderTest, NetworkRejectNonSecureVpn) { |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 930 | std::vector<UidRangeParcel> uidRanges = {makeUidRangeParcel(BASE_UID + 150, BASE_UID + 224), |
| 931 | makeUidRangeParcel(BASE_UID + 226, BASE_UID + 300)}; |
Luke Huang | 841f3ef | 2019-05-30 15:09:19 +0800 | [diff] [blame] | 932 | // Make sure no rules existed before calling commands. |
| 933 | for (auto const& range : uidRanges) { |
Ken Chen | 7099e2e | 2021-02-08 00:57:47 +0800 | [diff] [blame] | 934 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_PROHIBIT_NON_VPN, range, "prohibit")); |
Luke Huang | 841f3ef | 2019-05-30 15:09:19 +0800 | [diff] [blame] | 935 | } |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 936 | // Create two valid rules. |
| 937 | ASSERT_TRUE(mNetd->networkRejectNonSecureVpn(true, uidRanges).isOk()); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 938 | for (auto const& range : uidRanges) { |
Ken Chen | 7099e2e | 2021-02-08 00:57:47 +0800 | [diff] [blame] | 939 | EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY_PROHIBIT_NON_VPN, range, "prohibit")); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | // Remove the rules. |
| 943 | ASSERT_TRUE(mNetd->networkRejectNonSecureVpn(false, uidRanges).isOk()); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 944 | for (auto const& range : uidRanges) { |
Ken Chen | 7099e2e | 2021-02-08 00:57:47 +0800 | [diff] [blame] | 945 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_PROHIBIT_NON_VPN, range, "prohibit")); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | // Fail to remove the rules a second time after they are already deleted. |
| 949 | binder::Status status = mNetd->networkRejectNonSecureVpn(false, uidRanges); |
| 950 | ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 951 | EXPECT_EQ(ENOENT, status.serviceSpecificErrorCode()); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 952 | } |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 953 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 954 | // Create a socket pair that isLoopbackSocket won't think is local. |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 955 | void NetdBinderTest::fakeRemoteSocketPair(unique_fd* clientSocket, unique_fd* serverSocket, |
| 956 | unique_fd* acceptedSocket) { |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 957 | serverSocket->reset(socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0)); |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 958 | struct sockaddr_in6 server6 = { .sin6_family = AF_INET6, .sin6_addr = sTun.dstAddr() }; |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 959 | ASSERT_EQ(0, bind(*serverSocket, (struct sockaddr *) &server6, sizeof(server6))); |
| 960 | |
| 961 | socklen_t addrlen = sizeof(server6); |
| 962 | ASSERT_EQ(0, getsockname(*serverSocket, (struct sockaddr *) &server6, &addrlen)); |
| 963 | ASSERT_EQ(0, listen(*serverSocket, 10)); |
| 964 | |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 965 | clientSocket->reset(socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0)); |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 966 | struct sockaddr_in6 client6 = { .sin6_family = AF_INET6, .sin6_addr = sTun.srcAddr() }; |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 967 | ASSERT_EQ(0, bind(*clientSocket, (struct sockaddr *) &client6, sizeof(client6))); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 968 | ASSERT_EQ(0, connect(*clientSocket, (struct sockaddr *) &server6, sizeof(server6))); |
| 969 | ASSERT_EQ(0, getsockname(*clientSocket, (struct sockaddr *) &client6, &addrlen)); |
| 970 | |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 971 | acceptedSocket->reset( |
| 972 | accept4(*serverSocket, (struct sockaddr*)&server6, &addrlen, SOCK_CLOEXEC)); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 973 | ASSERT_NE(-1, *acceptedSocket); |
| 974 | |
| 975 | ASSERT_EQ(0, memcmp(&client6, &server6, sizeof(client6))); |
| 976 | } |
| 977 | |
| 978 | void checkSocketpairOpen(int clientSocket, int acceptedSocket) { |
| 979 | char buf[4096]; |
| 980 | EXPECT_EQ(4, write(clientSocket, "foo", sizeof("foo"))); |
| 981 | EXPECT_EQ(4, read(acceptedSocket, buf, sizeof(buf))); |
| 982 | EXPECT_EQ(0, memcmp(buf, "foo", sizeof("foo"))); |
| 983 | } |
| 984 | |
| 985 | void checkSocketpairClosed(int clientSocket, int acceptedSocket) { |
| 986 | // Check that the client socket was closed with ECONNABORTED. |
| 987 | int ret = write(clientSocket, "foo", sizeof("foo")); |
| 988 | int err = errno; |
| 989 | EXPECT_EQ(-1, ret); |
| 990 | EXPECT_EQ(ECONNABORTED, err); |
| 991 | |
| 992 | // Check that it sent a RST to the server. |
| 993 | ret = write(acceptedSocket, "foo", sizeof("foo")); |
| 994 | err = errno; |
| 995 | EXPECT_EQ(-1, ret); |
| 996 | EXPECT_EQ(ECONNRESET, err); |
| 997 | } |
| 998 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 999 | TEST_F(NetdBinderTest, SocketDestroy) { |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 1000 | unique_fd clientSocket, serverSocket, acceptedSocket; |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 1001 | ASSERT_NO_FATAL_FAILURE(fakeRemoteSocketPair(&clientSocket, &serverSocket, &acceptedSocket)); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 1002 | |
| 1003 | // Pick a random UID in the system UID range. |
| 1004 | constexpr int baseUid = AID_APP - 2000; |
| 1005 | static_assert(baseUid > 0, "Not enough UIDs? Please fix this test."); |
| 1006 | int uid = baseUid + 500 + arc4random_uniform(1000); |
| 1007 | EXPECT_EQ(0, fchown(clientSocket, uid, -1)); |
| 1008 | |
| 1009 | // UID ranges that don't contain uid. |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 1010 | std::vector<UidRangeParcel> uidRanges = { |
| 1011 | makeUidRangeParcel(baseUid + 42, baseUid + 449), |
| 1012 | makeUidRangeParcel(baseUid + 1536, AID_APP - 4), |
| 1013 | makeUidRangeParcel(baseUid + 498, uid - 1), |
| 1014 | makeUidRangeParcel(uid + 1, baseUid + 1520), |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 1015 | }; |
| 1016 | // A skip list that doesn't contain UID. |
| 1017 | std::vector<int32_t> skipUids { baseUid + 123, baseUid + 1600 }; |
| 1018 | |
| 1019 | // Close sockets. Our test socket should be intact. |
| 1020 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 1021 | checkSocketpairOpen(clientSocket, acceptedSocket); |
| 1022 | |
| 1023 | // UID ranges that do contain uid. |
| 1024 | uidRanges = { |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 1025 | makeUidRangeParcel(baseUid + 42, baseUid + 449), |
| 1026 | makeUidRangeParcel(baseUid + 1536, AID_APP - 4), |
| 1027 | makeUidRangeParcel(baseUid + 498, baseUid + 1520), |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 1028 | }; |
| 1029 | // Add uid to the skip list. |
| 1030 | skipUids.push_back(uid); |
| 1031 | |
| 1032 | // Close sockets. Our test socket should still be intact because it's in the skip list. |
| 1033 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 1034 | checkSocketpairOpen(clientSocket, acceptedSocket); |
| 1035 | |
| 1036 | // Now remove uid from skipUids, and close sockets. Our test socket should have been closed. |
| 1037 | skipUids.resize(skipUids.size() - 1); |
| 1038 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 1039 | checkSocketpairClosed(clientSocket, acceptedSocket); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 1040 | } |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1041 | |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1042 | TEST_F(NetdBinderTest, SocketDestroyLinkLocal) { |
| 1043 | // Add the same link-local address to two interfaces. |
| 1044 | const char* kLinkLocalAddress = "fe80::ace:d00d"; |
| 1045 | |
| 1046 | const struct addrinfo hints = { |
Maciej Żenczykowski | 3d0b37e | 2023-10-22 11:06:01 -0700 | [diff] [blame] | 1047 | .ai_flags = AI_NUMERICHOST, |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1048 | .ai_family = AF_INET6, |
| 1049 | .ai_socktype = SOCK_STREAM, |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1050 | }; |
| 1051 | |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1052 | // The ~ in ~64 enables 'nodad' which makes these operations faster and reduces test flakiness. |
| 1053 | // Unfortunately even with 'nodad' these are still very slightly asynchronous. |
| 1054 | binder::Status status = mNetd->interfaceAddAddress(sTun.name(), kLinkLocalAddress, ~64); |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1055 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1056 | status = mNetd->interfaceAddAddress(sTun2.name(), kLinkLocalAddress, ~64); |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1057 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1058 | |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1059 | // Add IPs to two more interfaces - purely to slow things down a little bit more. |
| 1060 | status = mNetd->interfaceAddAddress(sTun3.name(), kLinkLocalAddress, ~64); |
| 1061 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1062 | status = mNetd->interfaceAddAddress(sTun4.name(), kLinkLocalAddress, ~64); |
| 1063 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1064 | |
| 1065 | // Bind a listening socket to the address on each of the interfaces. |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1066 | // The sockets must be open at the same time, because this test checks that SOCK_DESTROY only |
| 1067 | // destroys the sockets on the interface where the address is deleted. |
| 1068 | struct addrinfo* addrinfoList = nullptr; |
| 1069 | int ret = getaddrinfo(kLinkLocalAddress, nullptr, &hints, &addrinfoList); |
| 1070 | ScopedAddrinfo addrinfoCleanup(addrinfoList); |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1071 | ASSERT_EQ(0, ret) << "errno:" << errno; |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1072 | |
| 1073 | socklen_t len = addrinfoList[0].ai_addrlen; |
| 1074 | sockaddr_in6 sin6_1 = *reinterpret_cast<sockaddr_in6*>(addrinfoList[0].ai_addr); |
| 1075 | sockaddr_in6 sin6_2 = sin6_1; |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1076 | sockaddr_in6 sin6_3 = sin6_1; |
| 1077 | sockaddr_in6 sin6_4 = sin6_1; |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1078 | sin6_1.sin6_scope_id = if_nametoindex(sTun.name().c_str()); |
| 1079 | sin6_2.sin6_scope_id = if_nametoindex(sTun2.name().c_str()); |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1080 | sin6_3.sin6_scope_id = if_nametoindex(sTun3.name().c_str()); |
| 1081 | sin6_4.sin6_scope_id = if_nametoindex(sTun4.name().c_str()); |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1082 | |
Lorenzo Colitti | 582b024 | 2021-06-17 23:19:22 +0900 | [diff] [blame] | 1083 | int s1 = socket(AF_INET6, SOCK_STREAM | SOCK_NONBLOCK, 0); |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1084 | ASSERT_EQ(0, bind(s1, reinterpret_cast<sockaddr*>(&sin6_1), len)) << "errno:" << errno; |
| 1085 | ASSERT_EQ(0, getsockname(s1, reinterpret_cast<sockaddr*>(&sin6_1), &len)) << "errno:" << errno; |
Lorenzo Colitti | 582b024 | 2021-06-17 23:19:22 +0900 | [diff] [blame] | 1086 | // getsockname technically writes to len, but sizeof(sockaddr_in6) doesn't change. |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1087 | |
Lorenzo Colitti | 582b024 | 2021-06-17 23:19:22 +0900 | [diff] [blame] | 1088 | int s2 = socket(AF_INET6, SOCK_STREAM | SOCK_NONBLOCK, 0); |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1089 | ASSERT_EQ(0, bind(s2, reinterpret_cast<sockaddr*>(&sin6_2), len)) << "errno:" << errno; |
| 1090 | ASSERT_EQ(0, getsockname(s2, reinterpret_cast<sockaddr*>(&sin6_2), &len)) << "errno:" << errno; |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1091 | |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1092 | int s3 = socket(AF_INET6, SOCK_STREAM | SOCK_NONBLOCK, 0); |
| 1093 | ASSERT_EQ(0, bind(s3, reinterpret_cast<sockaddr*>(&sin6_3), len)) << "errno:" << errno; |
| 1094 | ASSERT_EQ(0, getsockname(s3, reinterpret_cast<sockaddr*>(&sin6_3), &len)) << "errno:" << errno; |
| 1095 | |
| 1096 | int s4 = socket(AF_INET6, SOCK_STREAM | SOCK_NONBLOCK, 0); |
| 1097 | ASSERT_EQ(0, bind(s4, reinterpret_cast<sockaddr*>(&sin6_4), len)) << "errno:" << errno; |
| 1098 | ASSERT_EQ(0, getsockname(s4, reinterpret_cast<sockaddr*>(&sin6_4), &len)) << "errno:" << errno; |
| 1099 | |
| 1100 | ASSERT_EQ(0, listen(s1, 10)) << "errno:" << errno; |
| 1101 | ASSERT_EQ(0, listen(s2, 10)) << "errno:" << errno; |
| 1102 | ASSERT_EQ(0, listen(s3, 10)) << "errno:" << errno; |
| 1103 | ASSERT_EQ(0, listen(s4, 10)) << "errno:" << errno; |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1104 | |
| 1105 | // Connect one client socket to each and accept the connections. |
| 1106 | int c1 = socket(AF_INET6, SOCK_STREAM, 0); |
| 1107 | int c2 = socket(AF_INET6, SOCK_STREAM, 0); |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1108 | |
| 1109 | // ~0.1% chance the kernel isn't quite done yet with adding the IPs. |
| 1110 | // In such a case these may fail with -1, errno = 101 (ENETUNREACH) |
| 1111 | ASSERT_EQ(0, connect(c1, reinterpret_cast<sockaddr*>(&sin6_1), len)) << "errno:" << errno; |
| 1112 | ASSERT_EQ(0, connect(c2, reinterpret_cast<sockaddr*>(&sin6_2), len)) << "errno:" << errno; |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1113 | int a1 = accept(s1, nullptr, 0); |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1114 | ASSERT_NE(-1, a1) << "errno:" << errno; |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1115 | int a2 = accept(s2, nullptr, 0); |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1116 | ASSERT_NE(-1, a2) << "errno:" << errno; |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1117 | |
| 1118 | // Delete the address on sTun2. |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1119 | status = mNetd->interfaceDelAddress(sTun2.name(), kLinkLocalAddress, ~64); |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1120 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1121 | |
Lorenzo Colitti | 582b024 | 2021-06-17 23:19:22 +0900 | [diff] [blame] | 1122 | // The client sockets on sTun2 are closed, but the ones on sTun1 remain open. |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1123 | char buf[1024]; |
| 1124 | EXPECT_EQ(-1, read(c2, buf, sizeof(buf))); |
Maciej Żenczykowski | 8c18fca | 2023-04-20 16:47:44 +0000 | [diff] [blame] | 1125 | EXPECT_TRUE(errno == ECONNABORTED || errno == ECONNRESET) << "errno:" << errno; |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1126 | // The blocking read above ensures that SOCK_DESTROY has completed. |
| 1127 | |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1128 | EXPECT_EQ(3, write(a1, "foo", 3)) << "errno:" << errno; |
| 1129 | EXPECT_EQ(3, read(c1, buf, sizeof(buf))) << "errno:" << errno; |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1130 | EXPECT_EQ(-1, write(a2, "foo", 3)); |
Maciej Żenczykowski | 8c18fca | 2023-04-20 16:47:44 +0000 | [diff] [blame] | 1131 | EXPECT_TRUE(errno == ECONNABORTED || errno == ECONNRESET) << "errno:" << errno; |
Lorenzo Colitti | 582b024 | 2021-06-17 23:19:22 +0900 | [diff] [blame] | 1132 | |
| 1133 | // Check the server sockets too. |
| 1134 | EXPECT_EQ(-1, accept(s1, nullptr, 0)); |
| 1135 | EXPECT_EQ(EAGAIN, errno); |
| 1136 | EXPECT_EQ(-1, accept(s2, nullptr, 0)); |
| 1137 | EXPECT_EQ(EINVAL, errno); |
Maciej Żenczykowski | efb3652 | 2023-06-29 10:54:36 +0000 | [diff] [blame] | 1138 | |
| 1139 | close(a1); |
| 1140 | close(a2); |
| 1141 | |
| 1142 | close(c1); |
| 1143 | close(c2); |
| 1144 | |
| 1145 | close(s1); |
| 1146 | close(s2); |
| 1147 | close(s3); |
| 1148 | close(s4); |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1149 | } |
| 1150 | |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1151 | namespace { |
| 1152 | |
| 1153 | int netmaskToPrefixLength(const uint8_t *buf, size_t buflen) { |
| 1154 | if (buf == nullptr) return -1; |
| 1155 | |
| 1156 | int prefixLength = 0; |
| 1157 | bool endOfContiguousBits = false; |
| 1158 | for (unsigned int i = 0; i < buflen; i++) { |
| 1159 | const uint8_t value = buf[i]; |
| 1160 | |
| 1161 | // Bad bit sequence: check for a contiguous set of bits from the high |
| 1162 | // end by verifying that the inverted value + 1 is a power of 2 |
| 1163 | // (power of 2 iff. (v & (v - 1)) == 0). |
| 1164 | const uint8_t inverse = ~value + 1; |
| 1165 | if ((inverse & (inverse - 1)) != 0) return -1; |
| 1166 | |
| 1167 | prefixLength += (value == 0) ? 0 : CHAR_BIT - ffs(value) + 1; |
| 1168 | |
| 1169 | // Bogus netmask. |
| 1170 | if (endOfContiguousBits && value != 0) return -1; |
| 1171 | |
| 1172 | if (value != 0xff) endOfContiguousBits = true; |
| 1173 | } |
| 1174 | |
| 1175 | return prefixLength; |
| 1176 | } |
| 1177 | |
| 1178 | template<typename T> |
| 1179 | int netmaskToPrefixLength(const T *p) { |
| 1180 | return netmaskToPrefixLength(reinterpret_cast<const uint8_t*>(p), sizeof(T)); |
| 1181 | } |
| 1182 | |
| 1183 | |
| 1184 | static bool interfaceHasAddress( |
| 1185 | const std::string &ifname, const char *addrString, int prefixLength) { |
| 1186 | struct addrinfo *addrinfoList = nullptr; |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1187 | |
| 1188 | const struct addrinfo hints = { |
| 1189 | .ai_flags = AI_NUMERICHOST, |
| 1190 | .ai_family = AF_UNSPEC, |
| 1191 | .ai_socktype = SOCK_DGRAM, |
| 1192 | }; |
| 1193 | if (getaddrinfo(addrString, nullptr, &hints, &addrinfoList) != 0 || |
| 1194 | addrinfoList == nullptr || addrinfoList->ai_addr == nullptr) { |
| 1195 | return false; |
| 1196 | } |
Bernie Innocenti | 9bf749f | 2018-08-30 08:37:22 +0900 | [diff] [blame] | 1197 | ScopedAddrinfo addrinfoCleanup(addrinfoList); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1198 | |
| 1199 | struct ifaddrs *ifaddrsList = nullptr; |
| 1200 | ScopedIfaddrs ifaddrsCleanup(ifaddrsList); |
| 1201 | |
| 1202 | if (getifaddrs(&ifaddrsList) != 0) { |
| 1203 | return false; |
| 1204 | } |
| 1205 | |
| 1206 | for (struct ifaddrs *addr = ifaddrsList; addr != nullptr; addr = addr->ifa_next) { |
| 1207 | if (std::string(addr->ifa_name) != ifname || |
| 1208 | addr->ifa_addr == nullptr || |
| 1209 | addr->ifa_addr->sa_family != addrinfoList->ai_addr->sa_family) { |
| 1210 | continue; |
| 1211 | } |
| 1212 | |
| 1213 | switch (addr->ifa_addr->sa_family) { |
| 1214 | case AF_INET: { |
| 1215 | auto *addr4 = reinterpret_cast<const struct sockaddr_in*>(addr->ifa_addr); |
| 1216 | auto *want = reinterpret_cast<const struct sockaddr_in*>(addrinfoList->ai_addr); |
| 1217 | if (memcmp(&addr4->sin_addr, &want->sin_addr, sizeof(want->sin_addr)) != 0) { |
| 1218 | continue; |
| 1219 | } |
| 1220 | |
| 1221 | if (prefixLength < 0) return true; // not checking prefix lengths |
| 1222 | |
| 1223 | if (addr->ifa_netmask == nullptr) return false; |
| 1224 | auto *nm = reinterpret_cast<const struct sockaddr_in*>(addr->ifa_netmask); |
| 1225 | EXPECT_EQ(prefixLength, netmaskToPrefixLength(&nm->sin_addr)); |
| 1226 | return (prefixLength == netmaskToPrefixLength(&nm->sin_addr)); |
| 1227 | } |
| 1228 | case AF_INET6: { |
| 1229 | auto *addr6 = reinterpret_cast<const struct sockaddr_in6*>(addr->ifa_addr); |
| 1230 | auto *want = reinterpret_cast<const struct sockaddr_in6*>(addrinfoList->ai_addr); |
| 1231 | if (memcmp(&addr6->sin6_addr, &want->sin6_addr, sizeof(want->sin6_addr)) != 0) { |
| 1232 | continue; |
| 1233 | } |
| 1234 | |
| 1235 | if (prefixLength < 0) return true; // not checking prefix lengths |
| 1236 | |
| 1237 | if (addr->ifa_netmask == nullptr) return false; |
| 1238 | auto *nm = reinterpret_cast<const struct sockaddr_in6*>(addr->ifa_netmask); |
| 1239 | EXPECT_EQ(prefixLength, netmaskToPrefixLength(&nm->sin6_addr)); |
| 1240 | return (prefixLength == netmaskToPrefixLength(&nm->sin6_addr)); |
| 1241 | } |
| 1242 | default: |
| 1243 | // Cannot happen because we have already screened for matching |
| 1244 | // address families at the top of each iteration. |
| 1245 | continue; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | return false; |
| 1250 | } |
| 1251 | |
| 1252 | } // namespace |
| 1253 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 1254 | TEST_F(NetdBinderTest, InterfaceAddRemoveAddress) { |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1255 | static const struct TestData { |
| 1256 | const char *addrString; |
| 1257 | const int prefixLength; |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 1258 | const int expectAddResult; |
| 1259 | const int expectRemoveResult; |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1260 | } kTestData[] = { |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 1261 | {"192.0.2.1", 24, 0, 0}, |
| 1262 | {"192.0.2.2", 25, 0, 0}, |
| 1263 | {"192.0.2.3", 32, 0, 0}, |
| 1264 | {"192.0.2.4", 33, EINVAL, EADDRNOTAVAIL}, |
| 1265 | {"192.not.an.ip", 24, EINVAL, EINVAL}, |
| 1266 | {"2001:db8::1", 64, 0, 0}, |
| 1267 | {"2001:db8::2", 65, 0, 0}, |
| 1268 | {"2001:db8::3", 128, 0, 0}, |
Lorenzo Colitti | caccab4 | 2021-06-17 12:16:37 +0900 | [diff] [blame] | 1269 | {"fe80::1234", 64, 0, 0}, |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 1270 | {"2001:db8::4", 129, EINVAL, EINVAL}, |
| 1271 | {"foo:bar::bad", 64, EINVAL, EINVAL}, |
| 1272 | {"2001:db8::1/64", 64, EINVAL, EINVAL}, |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1273 | }; |
| 1274 | |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 1275 | for (size_t i = 0; i < std::size(kTestData); i++) { |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1276 | const auto &td = kTestData[i]; |
| 1277 | |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 1278 | SCOPED_TRACE(String8::format("Offending IP address %s/%d", td.addrString, td.prefixLength)); |
| 1279 | |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1280 | // [1.a] Add the address. |
| 1281 | binder::Status status = mNetd->interfaceAddAddress( |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 1282 | sTun.name(), td.addrString, td.prefixLength); |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 1283 | if (td.expectAddResult == 0) { |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1284 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1285 | } else { |
| 1286 | ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 1287 | ASSERT_EQ(td.expectAddResult, status.serviceSpecificErrorCode()); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | // [1.b] Verify the addition meets the expectation. |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 1291 | if (td.expectAddResult == 0) { |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 1292 | EXPECT_TRUE(interfaceHasAddress(sTun.name(), td.addrString, td.prefixLength)); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1293 | } else { |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 1294 | EXPECT_FALSE(interfaceHasAddress(sTun.name(), td.addrString, -1)); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | // [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] | 1298 | status = mNetd->interfaceDelAddress(sTun.name(), td.addrString, td.prefixLength); |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 1299 | if (td.expectRemoveResult == 0) { |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1300 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1301 | } else { |
| 1302 | ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 1303 | ASSERT_EQ(td.expectRemoveResult, status.serviceSpecificErrorCode()); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | // [2.b] No matter what, the address should not be present. |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 1307 | EXPECT_FALSE(interfaceHasAddress(sTun.name(), td.addrString, -1)); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1308 | } |
Lorenzo Colitti | 9ae1771 | 2019-12-02 17:28:49 +0900 | [diff] [blame] | 1309 | |
| 1310 | // Check that netlink errors are returned correctly. |
| 1311 | // We do this by attempting to create an IPv6 address on an interface that has IPv6 disabled, |
| 1312 | // which returns EACCES. |
| 1313 | TunInterface tun; |
| 1314 | ASSERT_EQ(0, tun.init()); |
| 1315 | binder::Status status = |
| 1316 | mNetd->setProcSysNet(INetd::IPV6, INetd::CONF, tun.name(), "disable_ipv6", "1"); |
| 1317 | ASSERT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1318 | status = mNetd->interfaceAddAddress(tun.name(), "2001:db8::1", 64); |
| 1319 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 1320 | EXPECT_EQ(EACCES, status.serviceSpecificErrorCode()); |
| 1321 | tun.destroy(); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 1322 | } |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 1323 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 1324 | TEST_F(NetdBinderTest, GetProcSysNet) { |
Evgenii Stepanov | 803aff3 | 2021-05-01 23:57:27 -0700 | [diff] [blame] | 1325 | const char* LOOPBACK = "lo"; |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 1326 | static const struct { |
| 1327 | const int ipversion; |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 1328 | const int which; |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 1329 | const char* ifname; |
| 1330 | const char* parameter; |
| 1331 | const char* expectedValue; |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 1332 | const int expectedReturnCode; |
| 1333 | } kTestData[] = { |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 1334 | {INetd::IPV4, INetd::CONF, LOOPBACK, "arp_ignore", "0", 0}, |
| 1335 | {-1, INetd::CONF, sTun.name().c_str(), "arp_ignore", nullptr, EAFNOSUPPORT}, |
| 1336 | {INetd::IPV4, -1, sTun.name().c_str(), "arp_ignore", nullptr, EINVAL}, |
| 1337 | {INetd::IPV4, INetd::CONF, "..", "conf/lo/arp_ignore", nullptr, EINVAL}, |
| 1338 | {INetd::IPV4, INetd::CONF, ".", "lo/arp_ignore", nullptr, EINVAL}, |
| 1339 | {INetd::IPV4, INetd::CONF, sTun.name().c_str(), "../all/arp_ignore", nullptr, EINVAL}, |
| 1340 | {INetd::IPV6, INetd::NEIGH, LOOPBACK, "ucast_solicit", "3", 0}, |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 1341 | }; |
| 1342 | |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 1343 | for (size_t i = 0; i < std::size(kTestData); i++) { |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 1344 | const auto& td = kTestData[i]; |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 1345 | |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 1346 | std::string value; |
| 1347 | const binder::Status status = |
| 1348 | mNetd->getProcSysNet(td.ipversion, td.which, td.ifname, td.parameter, &value); |
| 1349 | |
| 1350 | if (td.expectedReturnCode == 0) { |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 1351 | SCOPED_TRACE(String8::format("test case %zu should have passed", i)); |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 1352 | EXPECT_EQ(0, status.exceptionCode()); |
| 1353 | EXPECT_EQ(0, status.serviceSpecificErrorCode()); |
| 1354 | EXPECT_EQ(td.expectedValue, value); |
| 1355 | } else { |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 1356 | SCOPED_TRACE(String8::format("test case %zu should have failed", i)); |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 1357 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 1358 | EXPECT_EQ(td.expectedReturnCode, status.serviceSpecificErrorCode()); |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 1363 | TEST_F(NetdBinderTest, SetProcSysNet) { |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 1364 | static const struct { |
| 1365 | const int ipversion; |
| 1366 | const int which; |
| 1367 | const char* ifname; |
| 1368 | const char* parameter; |
| 1369 | const char* value; |
| 1370 | const int expectedReturnCode; |
| 1371 | } kTestData[] = { |
| 1372 | {INetd::IPV4, INetd::CONF, sTun.name().c_str(), "arp_ignore", "1", 0}, |
| 1373 | {-1, INetd::CONF, sTun.name().c_str(), "arp_ignore", "1", EAFNOSUPPORT}, |
| 1374 | {INetd::IPV4, -1, sTun.name().c_str(), "arp_ignore", "1", EINVAL}, |
| 1375 | {INetd::IPV4, INetd::CONF, "..", "conf/lo/arp_ignore", "1", EINVAL}, |
| 1376 | {INetd::IPV4, INetd::CONF, ".", "lo/arp_ignore", "1", EINVAL}, |
| 1377 | {INetd::IPV4, INetd::CONF, sTun.name().c_str(), "../all/arp_ignore", "1", EINVAL}, |
| 1378 | {INetd::IPV6, INetd::NEIGH, sTun.name().c_str(), "ucast_solicit", "7", 0}, |
| 1379 | }; |
| 1380 | |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 1381 | for (size_t i = 0; i < std::size(kTestData); i++) { |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 1382 | const auto& td = kTestData[i]; |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 1383 | const binder::Status status = |
| 1384 | mNetd->setProcSysNet(td.ipversion, td.which, td.ifname, td.parameter, td.value); |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 1385 | |
| 1386 | if (td.expectedReturnCode == 0) { |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 1387 | SCOPED_TRACE(String8::format("test case %zu should have passed", i)); |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 1388 | EXPECT_EQ(0, status.exceptionCode()); |
| 1389 | EXPECT_EQ(0, status.serviceSpecificErrorCode()); |
| 1390 | } else { |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 1391 | SCOPED_TRACE(String8::format("test case %zu should have failed", i)); |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 1392 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 1393 | EXPECT_EQ(td.expectedReturnCode, status.serviceSpecificErrorCode()); |
| 1394 | } |
| 1395 | } |
| 1396 | } |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 1397 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 1398 | TEST_F(NetdBinderTest, GetSetProcSysNet) { |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 1399 | const int ipversion = INetd::IPV6; |
| 1400 | const int category = INetd::NEIGH; |
| 1401 | const std::string& tun = sTun.name(); |
| 1402 | const std::string parameter("ucast_solicit"); |
| 1403 | |
| 1404 | std::string value{}; |
| 1405 | EXPECT_TRUE(mNetd->getProcSysNet(ipversion, category, tun, parameter, &value).isOk()); |
Maciej Żenczykowski | 7b789b9 | 2019-04-09 15:55:06 -0700 | [diff] [blame] | 1406 | ASSERT_FALSE(value.empty()); |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 1407 | const int ival = std::stoi(value); |
| 1408 | EXPECT_GT(ival, 0); |
| 1409 | // Try doubling the parameter value (always best!). |
| 1410 | EXPECT_TRUE(mNetd->setProcSysNet(ipversion, category, tun, parameter, std::to_string(2 * ival)) |
| 1411 | .isOk()); |
| 1412 | EXPECT_TRUE(mNetd->getProcSysNet(ipversion, category, tun, parameter, &value).isOk()); |
| 1413 | EXPECT_EQ(2 * ival, std::stoi(value)); |
| 1414 | // Try resetting the parameter. |
| 1415 | EXPECT_TRUE(mNetd->setProcSysNet(ipversion, category, tun, parameter, std::to_string(ival)) |
| 1416 | .isOk()); |
| 1417 | EXPECT_TRUE(mNetd->getProcSysNet(ipversion, category, tun, parameter, &value).isOk()); |
| 1418 | EXPECT_EQ(ival, std::stoi(value)); |
| 1419 | } |
| 1420 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 1421 | namespace { |
| 1422 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1423 | void expectNoTestCounterRules() { |
| 1424 | for (const auto& binary : { IPTABLES_PATH, IP6TABLES_PATH }) { |
| 1425 | std::string command = StringPrintf("%s -w -nvL tetherctrl_counters", binary); |
| 1426 | std::string allRules = Join(runCommand(command), "\n"); |
| 1427 | EXPECT_EQ(std::string::npos, allRules.find("netdtest_")); |
| 1428 | } |
| 1429 | } |
| 1430 | |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 1431 | void addTetherCounterValues(const char* path, const std::string& if1, const std::string& if2, |
| 1432 | int byte, int pkt) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1433 | runCommand(StringPrintf("%s -w -A tetherctrl_counters -i %s -o %s -j RETURN -c %d %d", |
| 1434 | path, if1.c_str(), if2.c_str(), pkt, byte)); |
| 1435 | } |
| 1436 | |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 1437 | 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] | 1438 | runCommand(StringPrintf("%s -w -D tetherctrl_counters -i %s -o %s -j RETURN", |
| 1439 | path, if1.c_str(), if2.c_str())); |
| 1440 | runCommand(StringPrintf("%s -w -D tetherctrl_counters -i %s -o %s -j RETURN", |
| 1441 | path, if2.c_str(), if1.c_str())); |
| 1442 | } |
| 1443 | |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 1444 | std::vector<int64_t> getStatsVectorByIf(const std::vector<TetherStatsParcel>& statsVec, |
| 1445 | const std::string& iface) { |
| 1446 | for (auto& stats : statsVec) { |
| 1447 | if (stats.iface == iface) { |
| 1448 | return {stats.rxBytes, stats.rxPackets, stats.txBytes, stats.txPackets}; |
| 1449 | } |
| 1450 | } |
| 1451 | return {}; |
| 1452 | } |
| 1453 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 1454 | } // namespace |
| 1455 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 1456 | TEST_F(NetdBinderTest, TetherGetStats) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1457 | expectNoTestCounterRules(); |
| 1458 | |
| 1459 | // TODO: fold this into more comprehensive tests once we have binder RPCs for enabling and |
| 1460 | // disabling tethering. We don't check the return value because these commands will fail if |
| 1461 | // tethering is already enabled. |
| 1462 | runCommand(StringPrintf("%s -w -N tetherctrl_counters", IPTABLES_PATH)); |
| 1463 | runCommand(StringPrintf("%s -w -N tetherctrl_counters", IP6TABLES_PATH)); |
| 1464 | |
| 1465 | std::string intIface1 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
| 1466 | std::string intIface2 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
| 1467 | std::string intIface3 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
Luke Huang | 0f91cdc | 2019-05-29 17:55:51 +0800 | [diff] [blame] | 1468 | |
| 1469 | // Ensure we won't use the same interface name, otherwise the test will fail. |
| 1470 | u_int32_t rNumber = arc4random_uniform(10000); |
| 1471 | std::string extIface1 = StringPrintf("netdtest_%u", rNumber); |
| 1472 | std::string extIface2 = StringPrintf("netdtest_%u", rNumber + 1); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1473 | |
| 1474 | addTetherCounterValues(IPTABLES_PATH, intIface1, extIface1, 123, 111); |
| 1475 | addTetherCounterValues(IP6TABLES_PATH, intIface1, extIface1, 456, 10); |
| 1476 | addTetherCounterValues(IPTABLES_PATH, extIface1, intIface1, 321, 222); |
| 1477 | addTetherCounterValues(IP6TABLES_PATH, extIface1, intIface1, 654, 20); |
| 1478 | // RX is from external to internal, and TX is from internal to external. |
| 1479 | // So rxBytes is 321 + 654 = 975, txBytes is 123 + 456 = 579, etc. |
| 1480 | std::vector<int64_t> expected1 = { 975, 242, 579, 121 }; |
| 1481 | |
| 1482 | addTetherCounterValues(IPTABLES_PATH, intIface2, extIface2, 1000, 333); |
| 1483 | addTetherCounterValues(IP6TABLES_PATH, intIface2, extIface2, 3000, 30); |
| 1484 | |
| 1485 | addTetherCounterValues(IPTABLES_PATH, extIface2, intIface2, 2000, 444); |
| 1486 | addTetherCounterValues(IP6TABLES_PATH, extIface2, intIface2, 4000, 40); |
| 1487 | |
| 1488 | addTetherCounterValues(IP6TABLES_PATH, intIface3, extIface2, 1000, 25); |
| 1489 | addTetherCounterValues(IP6TABLES_PATH, extIface2, intIface3, 2000, 35); |
| 1490 | std::vector<int64_t> expected2 = { 8000, 519, 5000, 388 }; |
| 1491 | |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 1492 | std::vector<TetherStatsParcel> statsVec; |
| 1493 | binder::Status status = mNetd->tetherGetStats(&statsVec); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1494 | EXPECT_TRUE(status.isOk()) << "Getting tethering stats failed: " << status; |
| 1495 | |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 1496 | EXPECT_EQ(expected1, getStatsVectorByIf(statsVec, extIface1)); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1497 | |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 1498 | EXPECT_EQ(expected2, getStatsVectorByIf(statsVec, extIface2)); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1499 | |
| 1500 | for (const auto& path : { IPTABLES_PATH, IP6TABLES_PATH }) { |
| 1501 | delTetherCounterValues(path, intIface1, extIface1); |
| 1502 | delTetherCounterValues(path, intIface2, extIface2); |
Yi Kong | 5db97e5 | 2022-05-18 15:29:30 +0800 | [diff] [blame] | 1503 | if (strcmp(path, IP6TABLES_PATH) == 0) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1504 | delTetherCounterValues(path, intIface3, extIface2); |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | expectNoTestCounterRules(); |
| 1509 | } |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 1510 | |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1511 | namespace { |
| 1512 | |
Luke Huang | a521107 | 2018-08-01 23:36:29 +0800 | [diff] [blame] | 1513 | constexpr char IDLETIMER_RAW_PREROUTING[] = "idletimer_raw_PREROUTING"; |
| 1514 | constexpr char IDLETIMER_MANGLE_POSTROUTING[] = "idletimer_mangle_POSTROUTING"; |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1515 | |
| 1516 | static std::vector<std::string> listIptablesRuleByTable(const char* binary, const char* table, |
| 1517 | const char* chainName) { |
| 1518 | std::string command = StringPrintf("%s -t %s -w -n -v -L %s", binary, table, chainName); |
| 1519 | return runCommand(command); |
| 1520 | } |
| 1521 | |
Ken Chen | 8022f31 | 2023-11-16 04:13:19 +0800 | [diff] [blame] | 1522 | bool iptablesInterfaceRuleExists(const char* binary, const char* chainName, |
| 1523 | const std::string& expectedInterface, |
| 1524 | const std::string& expectedRule, const char* table) { |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1525 | std::vector<std::string> rules = listIptablesRuleByTable(binary, table, chainName); |
| 1526 | for (const auto& rule : rules) { |
| 1527 | if (rule.find(expectedInterface) != std::string::npos) { |
| 1528 | if (rule.find(expectedRule) != std::string::npos) { |
| 1529 | return true; |
| 1530 | } |
| 1531 | } |
| 1532 | } |
| 1533 | return false; |
| 1534 | } |
| 1535 | |
| 1536 | void expectIdletimerInterfaceRuleExists(const std::string& ifname, int timeout, |
Luke Huang | a521107 | 2018-08-01 23:36:29 +0800 | [diff] [blame] | 1537 | const std::string& classLabel) { |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1538 | std::string IdletimerRule = |
Maciej Żenczykowski | 09d6460 | 2021-03-23 15:50:32 -0700 | [diff] [blame] | 1539 | StringPrintf("timeout:%u label:%s send_nl_msg", timeout, classLabel.c_str()); |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1540 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
Ken Chen | 8022f31 | 2023-11-16 04:13:19 +0800 | [diff] [blame] | 1541 | EXPECT_TRUE(iptablesInterfaceRuleExists(binary, IDLETIMER_RAW_PREROUTING, ifname, |
| 1542 | IdletimerRule, RAW_TABLE)); |
| 1543 | EXPECT_TRUE(iptablesInterfaceRuleExists(binary, IDLETIMER_MANGLE_POSTROUTING, ifname, |
| 1544 | IdletimerRule, MANGLE_TABLE)); |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | void expectIdletimerInterfaceRuleNotExists(const std::string& ifname, int timeout, |
Luke Huang | a521107 | 2018-08-01 23:36:29 +0800 | [diff] [blame] | 1549 | const std::string& classLabel) { |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1550 | std::string IdletimerRule = |
Maciej Żenczykowski | 09d6460 | 2021-03-23 15:50:32 -0700 | [diff] [blame] | 1551 | StringPrintf("timeout:%u label:%s send_nl_msg", timeout, classLabel.c_str()); |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1552 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
Ken Chen | 8022f31 | 2023-11-16 04:13:19 +0800 | [diff] [blame] | 1553 | EXPECT_FALSE(iptablesInterfaceRuleExists(binary, IDLETIMER_RAW_PREROUTING, ifname, |
| 1554 | IdletimerRule, RAW_TABLE)); |
| 1555 | EXPECT_FALSE(iptablesInterfaceRuleExists(binary, IDLETIMER_MANGLE_POSTROUTING, ifname, |
| 1556 | IdletimerRule, MANGLE_TABLE)); |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 1560 | } // namespace |
| 1561 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 1562 | TEST_F(NetdBinderTest, IdletimerAddRemoveInterface) { |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1563 | // TODO: We will get error in if expectIdletimerInterfaceRuleNotExists if there are the same |
| 1564 | // rule in the table. Because we only check the result after calling remove function. We might |
| 1565 | // check the actual rule which is removed by our function (maybe compare the results between |
| 1566 | // calling function before and after) |
| 1567 | binder::Status status; |
| 1568 | const struct TestData { |
| 1569 | const std::string ifname; |
| 1570 | int32_t timeout; |
| 1571 | const std::string classLabel; |
| 1572 | } idleTestData[] = { |
| 1573 | {"wlan0", 1234, "happyday"}, |
| 1574 | {"rmnet_data0", 4567, "friday"}, |
| 1575 | }; |
| 1576 | for (const auto& td : idleTestData) { |
| 1577 | status = mNetd->idletimerAddInterface(td.ifname, td.timeout, td.classLabel); |
| 1578 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1579 | expectIdletimerInterfaceRuleExists(td.ifname, td.timeout, td.classLabel); |
| 1580 | |
| 1581 | status = mNetd->idletimerRemoveInterface(td.ifname, td.timeout, td.classLabel); |
| 1582 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1583 | expectIdletimerInterfaceRuleNotExists(td.ifname, td.timeout, td.classLabel); |
| 1584 | } |
| 1585 | } |
| 1586 | |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 1587 | namespace { |
| 1588 | |
| 1589 | constexpr char STRICT_OUTPUT[] = "st_OUTPUT"; |
| 1590 | constexpr char STRICT_CLEAR_CAUGHT[] = "st_clear_caught"; |
| 1591 | |
Chiachang Wang | 911265a | 2022-03-01 17:17:05 +0800 | [diff] [blame] | 1592 | // Output looks like this: |
| 1593 | // |
| 1594 | // IPv4: |
| 1595 | // |
| 1596 | // throw dst proto static scope link |
| 1597 | // unreachable dst proto static scope link |
| 1598 | // dst via nextHop dev ifName proto static |
| 1599 | // dst dev ifName proto static scope link |
| 1600 | // |
| 1601 | // IPv6: |
| 1602 | // |
| 1603 | // throw dst dev lo proto static metric 1024 |
| 1604 | // unreachable dst dev lo proto static metric 1024 |
| 1605 | // dst via nextHop dev ifName proto static metric 1024 |
| 1606 | // dst dev ifName proto static metric 1024 |
| 1607 | std::string ipRoutePrefix(const std::string& ifName, const std::string& dst, |
| 1608 | const std::string& nextHop) { |
| 1609 | std::string prefixString; |
| 1610 | |
| 1611 | bool isThrow = nextHop == "throw"; |
| 1612 | bool isUnreachable = nextHop == "unreachable"; |
| 1613 | bool isDefault = (dst == "0.0.0.0/0" || dst == "::/0"); |
| 1614 | bool isIPv6 = dst.find(':') != std::string::npos; |
| 1615 | bool isThrowOrUnreachable = isThrow || isUnreachable; |
| 1616 | |
| 1617 | if (isThrowOrUnreachable) { |
| 1618 | prefixString += nextHop + " "; |
| 1619 | } |
| 1620 | |
| 1621 | prefixString += isDefault ? "default" : dst; |
| 1622 | |
| 1623 | if (!nextHop.empty() && !isThrowOrUnreachable) { |
| 1624 | prefixString += " via " + nextHop; |
| 1625 | } |
| 1626 | |
| 1627 | if (isThrowOrUnreachable) { |
| 1628 | if (isIPv6) { |
| 1629 | prefixString += " dev lo"; |
| 1630 | } |
| 1631 | } else { |
| 1632 | prefixString += " dev " + ifName; |
| 1633 | } |
| 1634 | |
| 1635 | prefixString += " proto static"; |
| 1636 | |
| 1637 | // IPv6 routes report the metric, IPv4 routes report the scope. |
| 1638 | if (isIPv6) { |
| 1639 | prefixString += " metric 1024"; |
| 1640 | } else { |
| 1641 | if (nextHop.empty() || isThrowOrUnreachable) { |
| 1642 | prefixString += " scope link"; |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | return prefixString; |
| 1647 | } |
| 1648 | |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 1649 | void expectStrictSetUidAccept(const int uid) { |
| 1650 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 1651 | std::string perUidChain = StringPrintf("st_clear_caught_%u", uid); |
| 1652 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
Greg Kaiser | 46a1d53 | 2019-03-26 12:10:58 -0700 | [diff] [blame] | 1653 | EXPECT_FALSE(iptablesRuleExists(binary, STRICT_OUTPUT, uidRule)); |
| 1654 | EXPECT_FALSE(iptablesRuleExists(binary, STRICT_CLEAR_CAUGHT, uidRule)); |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 1655 | EXPECT_EQ(0, iptablesRuleLineLength(binary, perUidChain.c_str())); |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | void expectStrictSetUidLog(const int uid) { |
| 1660 | static const char logRule[] = "st_penalty_log all"; |
| 1661 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 1662 | std::string perUidChain = StringPrintf("st_clear_caught_%u", uid); |
| 1663 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
Greg Kaiser | 46a1d53 | 2019-03-26 12:10:58 -0700 | [diff] [blame] | 1664 | EXPECT_TRUE(iptablesRuleExists(binary, STRICT_OUTPUT, uidRule)); |
| 1665 | EXPECT_TRUE(iptablesRuleExists(binary, STRICT_CLEAR_CAUGHT, uidRule)); |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 1666 | EXPECT_TRUE(iptablesRuleExists(binary, perUidChain.c_str(), logRule)); |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | void expectStrictSetUidReject(const int uid) { |
| 1671 | static const char rejectRule[] = "st_penalty_reject all"; |
| 1672 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 1673 | std::string perUidChain = StringPrintf("st_clear_caught_%u", uid); |
| 1674 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
Greg Kaiser | 46a1d53 | 2019-03-26 12:10:58 -0700 | [diff] [blame] | 1675 | EXPECT_TRUE(iptablesRuleExists(binary, STRICT_OUTPUT, uidRule)); |
| 1676 | EXPECT_TRUE(iptablesRuleExists(binary, STRICT_CLEAR_CAUGHT, uidRule)); |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 1677 | EXPECT_TRUE(iptablesRuleExists(binary, perUidChain.c_str(), rejectRule)); |
| 1678 | } |
| 1679 | } |
| 1680 | |
Chiachang Wang | 911265a | 2022-03-01 17:17:05 +0800 | [diff] [blame] | 1681 | std::vector<std::string> ipRouteSubstrings(const std::string& ifName, const std::string& dst, |
| 1682 | const std::string& nextHop, const std::string& mtu) { |
| 1683 | std::vector<std::string> routeSubstrings; |
| 1684 | |
| 1685 | routeSubstrings.push_back(ipRoutePrefix(ifName, dst, nextHop)); |
| 1686 | |
| 1687 | if (!mtu.empty()) { |
| 1688 | // Add separate substring to match mtu value. |
| 1689 | // This is needed because on some devices "error -11"/"error -113" appears between ip prefix |
| 1690 | // and mtu for throw/unreachable routes. |
| 1691 | routeSubstrings.push_back("mtu " + mtu); |
| 1692 | } |
| 1693 | |
| 1694 | return routeSubstrings; |
| 1695 | } |
| 1696 | |
| 1697 | void expectNetworkRouteDoesNotExistWithMtu(const char* ipVersion, const std::string& ifName, |
| 1698 | const std::string& dst, const std::string& nextHop, |
| 1699 | const std::string& mtu, const char* table) { |
| 1700 | std::vector<std::string> routeSubstrings = ipRouteSubstrings(ifName, dst, nextHop, mtu); |
| 1701 | EXPECT_FALSE(ipRouteExists(ipVersion, table, routeSubstrings)) |
| 1702 | << "Found unexpected route [" << Join(routeSubstrings, ", ") << "] in table " << table; |
| 1703 | } |
| 1704 | |
| 1705 | void expectNetworkRouteExistsWithMtu(const char* ipVersion, const std::string& ifName, |
| 1706 | const std::string& dst, const std::string& nextHop, |
| 1707 | const std::string& mtu, const char* table) { |
| 1708 | std::vector<std::string> routeSubstrings = ipRouteSubstrings(ifName, dst, nextHop, mtu); |
| 1709 | EXPECT_TRUE(ipRouteExists(ipVersion, table, routeSubstrings)) |
| 1710 | << "Couldn't find route to " << dst << ": [" << Join(routeSubstrings, ", ") |
| 1711 | << "] in table " << table; |
| 1712 | } |
| 1713 | |
Chiachang Wang | e39cc75 | 2022-03-01 17:35:31 +0800 | [diff] [blame] | 1714 | void expectVpnLocalExclusionRuleExists(const std::string& ifName, bool expectExists) { |
Chiachang Wang | 911265a | 2022-03-01 17:17:05 +0800 | [diff] [blame] | 1715 | std::string tableName = std::string(ifName + "_local"); |
| 1716 | // Check if rule exists |
| 1717 | std::string vpnLocalExclusionRule = |
| 1718 | StringPrintf("%d:\tfrom all fwmark 0x0/0x10000 iif lo lookup %s", |
| 1719 | RULE_PRIORITY_LOCAL_ROUTES, tableName.c_str()); |
| 1720 | for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { |
Chiachang Wang | e39cc75 | 2022-03-01 17:35:31 +0800 | [diff] [blame] | 1721 | EXPECT_EQ(expectExists, ipRuleExists(ipVersion, vpnLocalExclusionRule)); |
Chiachang Wang | 911265a | 2022-03-01 17:17:05 +0800 | [diff] [blame] | 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | void expectNetworkRouteExists(const char* ipVersion, const std::string& ifName, |
| 1726 | const std::string& dst, const std::string& nextHop, |
| 1727 | const char* table) { |
| 1728 | expectNetworkRouteExistsWithMtu(ipVersion, ifName, dst, nextHop, "", table); |
| 1729 | } |
| 1730 | |
| 1731 | void expectNetworkRouteDoesNotExist(const char* ipVersion, const std::string& ifName, |
| 1732 | const std::string& dst, const std::string& nextHop, |
| 1733 | const char* table) { |
| 1734 | expectNetworkRouteDoesNotExistWithMtu(ipVersion, ifName, dst, nextHop, "", table); |
| 1735 | } |
| 1736 | |
| 1737 | void expectNetworkDefaultIpRuleExists(const char* ifName) { |
| 1738 | std::string networkDefaultRule = |
| 1739 | StringPrintf("%u:\tfrom all fwmark 0x0/0xffff iif lo lookup %s", |
| 1740 | RULE_PRIORITY_DEFAULT_NETWORK, ifName); |
| 1741 | |
Chalard Jean | e479f31 | 2022-12-12 18:27:49 +0900 | [diff] [blame] | 1742 | expectRuleForV4AndV6(ALL_EXIST, networkDefaultRule); |
Chiachang Wang | 911265a | 2022-03-01 17:17:05 +0800 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | void expectNetworkDefaultIpRuleDoesNotExist() { |
| 1746 | std::string networkDefaultRule = |
| 1747 | StringPrintf("%u:\tfrom all fwmark 0x0/0xffff iif lo", RULE_PRIORITY_DEFAULT_NETWORK); |
| 1748 | |
Chalard Jean | 38bc084 | 2022-12-26 19:36:13 +0900 | [diff] [blame] | 1749 | for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { |
| 1750 | std::vector<std::string> rules = listIpRules(ipVersion); |
| 1751 | for (const auto& line : rules) { |
| 1752 | if (android::base::StartsWith(line, networkDefaultRule)) { |
| 1753 | FAIL(); |
| 1754 | } |
| 1755 | } |
| 1756 | } |
Chiachang Wang | 911265a | 2022-03-01 17:17:05 +0800 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | void expectNetworkPermissionIpRuleExists(const char* ifName, int permission) { |
| 1760 | std::string networkPermissionRule = ""; |
| 1761 | switch (permission) { |
| 1762 | case INetd::PERMISSION_NONE: |
| 1763 | networkPermissionRule = |
| 1764 | StringPrintf("%u:\tfrom all fwmark 0x1ffdd/0x1ffff iif lo lookup %s", |
| 1765 | RULE_PRIORITY_EXPLICIT_NETWORK, ifName); |
| 1766 | break; |
| 1767 | case INetd::PERMISSION_NETWORK: |
| 1768 | networkPermissionRule = |
| 1769 | StringPrintf("%u:\tfrom all fwmark 0x5ffdd/0x5ffff iif lo lookup %s", |
| 1770 | RULE_PRIORITY_EXPLICIT_NETWORK, ifName); |
| 1771 | break; |
| 1772 | case INetd::PERMISSION_SYSTEM: |
| 1773 | networkPermissionRule = |
| 1774 | StringPrintf("%u:\tfrom all fwmark 0xdffdd/0xdffff iif lo lookup %s", |
| 1775 | RULE_PRIORITY_EXPLICIT_NETWORK, ifName); |
| 1776 | break; |
| 1777 | } |
| 1778 | |
Chalard Jean | e479f31 | 2022-12-12 18:27:49 +0900 | [diff] [blame] | 1779 | expectRuleForV4AndV6(ALL_EXIST, networkPermissionRule); |
Chiachang Wang | 911265a | 2022-03-01 17:17:05 +0800 | [diff] [blame] | 1780 | } |
| 1781 | |
Chiachang Wang | 911265a | 2022-03-01 17:17:05 +0800 | [diff] [blame] | 1782 | void expectNetworkPermissionIptablesRuleExists(const char* ifName, int permission) { |
| 1783 | static const char ROUTECTRL_INPUT[] = "routectrl_mangle_INPUT"; |
| 1784 | std::string networkIncomingPacketMarkRule = ""; |
| 1785 | switch (permission) { |
| 1786 | case INetd::PERMISSION_NONE: |
Maciej Żenczykowski | 0a47ca4 | 2023-11-15 08:00:03 +0000 | [diff] [blame] | 1787 | networkIncomingPacketMarkRule = "MARK xset 0x3ffdd/0x7fefffff"; |
Chiachang Wang | 911265a | 2022-03-01 17:17:05 +0800 | [diff] [blame] | 1788 | break; |
| 1789 | case INetd::PERMISSION_NETWORK: |
Maciej Żenczykowski | 0a47ca4 | 2023-11-15 08:00:03 +0000 | [diff] [blame] | 1790 | networkIncomingPacketMarkRule = "MARK xset 0x7ffdd/0x7fefffff"; |
Chiachang Wang | 911265a | 2022-03-01 17:17:05 +0800 | [diff] [blame] | 1791 | break; |
| 1792 | case INetd::PERMISSION_SYSTEM: |
Maciej Żenczykowski | 0a47ca4 | 2023-11-15 08:00:03 +0000 | [diff] [blame] | 1793 | networkIncomingPacketMarkRule = "MARK xset 0xfffdd/0x7fefffff"; |
Chiachang Wang | 911265a | 2022-03-01 17:17:05 +0800 | [diff] [blame] | 1794 | break; |
| 1795 | } |
| 1796 | |
| 1797 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
Ken Chen | 8022f31 | 2023-11-16 04:13:19 +0800 | [diff] [blame] | 1798 | EXPECT_TRUE(iptablesInterfaceRuleExists(binary, ROUTECTRL_INPUT, ifName, |
| 1799 | networkIncomingPacketMarkRule, MANGLE_TABLE)); |
Chiachang Wang | 911265a | 2022-03-01 17:17:05 +0800 | [diff] [blame] | 1800 | } |
| 1801 | } |
| 1802 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 1803 | } // namespace |
| 1804 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 1805 | TEST_F(NetdBinderTest, StrictSetUidCleartextPenalty) { |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 1806 | binder::Status status; |
| 1807 | int32_t uid = randomUid(); |
| 1808 | |
| 1809 | // setUidCleartextPenalty Policy:Log with randomUid |
| 1810 | status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_LOG); |
| 1811 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1812 | expectStrictSetUidLog(uid); |
| 1813 | |
| 1814 | // setUidCleartextPenalty Policy:Accept with randomUid |
| 1815 | status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_ACCEPT); |
| 1816 | expectStrictSetUidAccept(uid); |
| 1817 | |
| 1818 | // setUidCleartextPenalty Policy:Reject with randomUid |
| 1819 | status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_REJECT); |
| 1820 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1821 | expectStrictSetUidReject(uid); |
| 1822 | |
| 1823 | // setUidCleartextPenalty Policy:Accept with randomUid |
| 1824 | status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_ACCEPT); |
| 1825 | expectStrictSetUidAccept(uid); |
| 1826 | |
| 1827 | // test wrong policy |
| 1828 | int32_t wrongPolicy = -123; |
| 1829 | status = mNetd->strictUidCleartextPenalty(uid, wrongPolicy); |
| 1830 | EXPECT_EQ(EINVAL, status.serviceSpecificErrorCode()); |
| 1831 | } |
| 1832 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 1833 | namespace { |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 1834 | |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1835 | std::vector<std::string> tryToFindProcesses(const std::string& processName, uint32_t maxTries = 1, |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 1836 | uint32_t intervalMs = 50) { |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1837 | // Output looks like:(clatd) |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 1838 | // clat 4963 850 1 12:16:51 ? 00:00:00 clatd-netd10a88 -i netd10a88 ... |
| 1839 | // ... |
| 1840 | // root 5221 5219 0 12:18:12 ? 00:00:00 sh -c ps -Af | grep ' clatd-netdcc1a0' |
| 1841 | |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1842 | // (dnsmasq) |
| 1843 | // dns_tether 4620 792 0 16:51:28 ? 00:00:00 dnsmasq --keep-in-foreground ... |
| 1844 | |
| 1845 | if (maxTries == 0) return {}; |
| 1846 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 1847 | std::string cmd = StringPrintf("ps -Af | grep '[0-9] %s'", processName.c_str()); |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1848 | std::vector<std::string> result; |
| 1849 | for (uint32_t run = 1;;) { |
Greg Kaiser | 46a1d53 | 2019-03-26 12:10:58 -0700 | [diff] [blame] | 1850 | result = runCommand(cmd); |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1851 | if (result.size() || ++run > maxTries) { |
| 1852 | break; |
| 1853 | } |
| 1854 | |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 1855 | usleep(intervalMs * 1000); |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1856 | } |
| 1857 | return result; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 1858 | } |
| 1859 | |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1860 | void expectProcessExists(const std::string& processName) { |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 1861 | EXPECT_EQ(1U, tryToFindProcesses(processName, 5 /*maxTries*/).size()); |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1862 | } |
| 1863 | |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 1864 | void expectProcessDoesNotExist(const std::string& processName) { |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1865 | EXPECT_FALSE(tryToFindProcesses(processName).size()); |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 1866 | } |
| 1867 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 1868 | } // namespace |
| 1869 | |
Chiachang | f9e81ac | 2022-05-12 05:21:20 +0000 | [diff] [blame] | 1870 | TEST_F(NetdBinderTest, NetworkAddRemoveRouteToLocalExcludeTable) { |
| 1871 | static const struct { |
| 1872 | const char* ipVersion; |
| 1873 | const char* testDest; |
| 1874 | const char* testNextHop; |
| 1875 | const bool expectInLocalTable; |
Chiachang | cff5e88 | 2022-05-12 05:55:45 +0000 | [diff] [blame] | 1876 | } kTestData[] = {{IP_RULE_V6, "::/0", "fe80::", false}, |
| 1877 | {IP_RULE_V6, "::/0", "", false}, |
| 1878 | {IP_RULE_V6, "2001:db8:cafe::/64", "fe80::", false}, |
| 1879 | {IP_RULE_V6, "fe80::/64", "", true}, |
| 1880 | {IP_RULE_V6, "2001:db8:cafe::/48", "", true}, |
| 1881 | {IP_RULE_V6, "2001:db8:cafe::/64", "unreachable", false}, |
| 1882 | {IP_RULE_V6, "2001:db8:ca00::/40", "", true}, |
| 1883 | {IP_RULE_V4, "0.0.0.0/0", "10.251.10.1", false}, |
| 1884 | {IP_RULE_V4, "192.1.0.0/16", "", false}, |
| 1885 | {IP_RULE_V4, "192.168.0.0/15", "", false}, |
| 1886 | {IP_RULE_V4, "192.168.0.0/16", "", true}, |
| 1887 | {IP_RULE_V4, "192.168.0.0/24", "", true}, |
| 1888 | {IP_RULE_V4, "100.1.0.0/16", "", false}, |
| 1889 | {IP_RULE_V4, "100.0.0.0/8", "", false}, |
| 1890 | {IP_RULE_V4, "100.64.0.0/10", "", true}, |
| 1891 | {IP_RULE_V4, "100.64.0.0/16", "", true}, |
| 1892 | {IP_RULE_V4, "100.64.0.0/10", "throw", false}, |
| 1893 | {IP_RULE_V4, "172.0.0.0/8", "", false}, |
| 1894 | {IP_RULE_V4, "172.16.0.0/12", "", true}, |
| 1895 | {IP_RULE_V4, "172.16.0.0/16", "", true}, |
| 1896 | {IP_RULE_V4, "172.16.0.0/12", "unreachable", false}, |
| 1897 | {IP_RULE_V4, "172.32.0.0/12", "", false}, |
| 1898 | {IP_RULE_V4, "169.0.0.0/8", "", false}, |
| 1899 | {IP_RULE_V4, "169.254.0.0/16", "", true}, |
| 1900 | {IP_RULE_V4, "169.254.0.0/20", "", true}, |
| 1901 | {IP_RULE_V4, "169.254.3.0/24", "", true}, |
| 1902 | {IP_RULE_V4, "170.254.0.0/16", "", false}, |
| 1903 | {IP_RULE_V4, "10.0.0.0/8", "", true}, |
| 1904 | {IP_RULE_V4, "10.0.0.0/7", "", false}, |
| 1905 | {IP_RULE_V4, "10.0.0.0/16", "", true}, |
| 1906 | {IP_RULE_V4, "10.251.0.0/16", "", true}, |
| 1907 | {IP_RULE_V4, "10.251.250.0/24", "", true}, |
| 1908 | {IP_RULE_V4, "10.251.10.2/31", "throw", false}, |
| 1909 | {IP_RULE_V4, "10.251.10.2/31", "unreachable", false}}; |
Chiachang | f9e81ac | 2022-05-12 05:21:20 +0000 | [diff] [blame] | 1910 | |
Chiachang | cff5e88 | 2022-05-12 05:55:45 +0000 | [diff] [blame] | 1911 | // To ensure that the nexthops for the above are reachable. |
| 1912 | // Otherwise, the routes can't be created. |
Chiachang | f9e81ac | 2022-05-12 05:21:20 +0000 | [diff] [blame] | 1913 | static const struct { |
| 1914 | const char* ipVersion; |
| 1915 | const char* testDest; |
| 1916 | const char* testNextHop; |
Chiachang | cff5e88 | 2022-05-12 05:55:45 +0000 | [diff] [blame] | 1917 | } kDirectlyConnectedRoutes[] = { |
| 1918 | {IP_RULE_V4, "10.251.10.0/30", ""}, |
| 1919 | {IP_RULE_V6, "2001:db8::/32", ""}, |
| 1920 | }; |
Chiachang | f9e81ac | 2022-05-12 05:21:20 +0000 | [diff] [blame] | 1921 | |
chiachangwang | d3f3d06 | 2022-10-31 10:50:18 +0000 | [diff] [blame] | 1922 | // This should ba aligned with V4_FIXED_LOCAL_PREFIXES in system/netd/server/RouteController.cpp |
| 1923 | // An expandable array for fixed local prefix though it's only one element now. |
| 1924 | static const char* kV4LocalPrefixes[] = {"224.0.0.0/24"}; |
| 1925 | |
Chiachang | f9e81ac | 2022-05-12 05:21:20 +0000 | [diff] [blame] | 1926 | // Add test physical network |
| 1927 | const auto& config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
| 1928 | INetd::PERMISSION_NONE, false, false); |
| 1929 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
| 1930 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 1931 | |
| 1932 | // Get current default network NetId |
| 1933 | binder::Status status = mNetd->networkGetDefault(&mStoredDefaultNetwork); |
| 1934 | ASSERT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1935 | |
| 1936 | // Set default network |
| 1937 | EXPECT_TRUE(mNetd->networkSetDefault(TEST_NETID1).isOk()); |
| 1938 | |
| 1939 | std::string localTableName = std::string(sTun.name() + "_local"); |
chiachangwang | 5308c04 | 2022-08-25 06:55:19 +0000 | [diff] [blame] | 1940 | |
| 1941 | // Verify the fixed routes exist in the local table. |
chiachangwang | d3f3d06 | 2022-10-31 10:50:18 +0000 | [diff] [blame] | 1942 | for (size_t i = 0; i < std::size(kV4LocalPrefixes); i++) { |
| 1943 | expectNetworkRouteExists(IP_RULE_V4, sTun.name(), kV4LocalPrefixes[i], "", |
chiachangwang | 5308c04 | 2022-08-25 06:55:19 +0000 | [diff] [blame] | 1944 | localTableName.c_str()); |
| 1945 | } |
| 1946 | |
Chiachang | f9e81ac | 2022-05-12 05:21:20 +0000 | [diff] [blame] | 1947 | // Set up link-local routes for connectivity to the "gateway" |
Chiachang | cff5e88 | 2022-05-12 05:55:45 +0000 | [diff] [blame] | 1948 | for (size_t i = 0; i < std::size(kDirectlyConnectedRoutes); i++) { |
| 1949 | const auto& td = kDirectlyConnectedRoutes[i]; |
Chiachang | f9e81ac | 2022-05-12 05:21:20 +0000 | [diff] [blame] | 1950 | |
| 1951 | binder::Status status = |
| 1952 | mNetd->networkAddRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop); |
| 1953 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1954 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1955 | sTun.name().c_str()); |
| 1956 | // Verify routes in local table |
| 1957 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1958 | localTableName.c_str()); |
| 1959 | } |
| 1960 | |
| 1961 | for (size_t i = 0; i < std::size(kTestData); i++) { |
| 1962 | const auto& td = kTestData[i]; |
Chiachang | cff5e88 | 2022-05-12 05:55:45 +0000 | [diff] [blame] | 1963 | SCOPED_TRACE(StringPrintf("case ip:%s, dest:%s, nexHop:%s, expect:%d", td.ipVersion, |
| 1964 | td.testDest, td.testNextHop, td.expectInLocalTable)); |
Chiachang | f9e81ac | 2022-05-12 05:21:20 +0000 | [diff] [blame] | 1965 | binder::Status status = |
| 1966 | mNetd->networkAddRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop); |
| 1967 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1968 | // Verify routes in local table |
| 1969 | if (td.expectInLocalTable) { |
| 1970 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1971 | localTableName.c_str()); |
| 1972 | } else { |
| 1973 | expectNetworkRouteDoesNotExist(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1974 | localTableName.c_str()); |
| 1975 | } |
| 1976 | |
| 1977 | status = mNetd->networkRemoveRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop); |
| 1978 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1979 | expectNetworkRouteDoesNotExist(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1980 | localTableName.c_str()); |
| 1981 | } |
| 1982 | |
Chiachang | cff5e88 | 2022-05-12 05:55:45 +0000 | [diff] [blame] | 1983 | for (size_t i = 0; i < std::size(kDirectlyConnectedRoutes); i++) { |
| 1984 | const auto& td = kDirectlyConnectedRoutes[i]; |
Chiachang | f9e81ac | 2022-05-12 05:21:20 +0000 | [diff] [blame] | 1985 | status = mNetd->networkRemoveRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop); |
| 1986 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1987 | } |
| 1988 | |
| 1989 | // Set default network back |
| 1990 | status = mNetd->networkSetDefault(mStoredDefaultNetwork); |
| 1991 | |
| 1992 | // Remove test physical network |
| 1993 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 1994 | } |
| 1995 | |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 1996 | namespace { |
| 1997 | |
| 1998 | bool getIpfwdV4Enable() { |
| 1999 | static const char ipv4IpfwdCmd[] = "cat /proc/sys/net/ipv4/ip_forward"; |
| 2000 | std::vector<std::string> result = runCommand(ipv4IpfwdCmd); |
| 2001 | EXPECT_TRUE(!result.empty()); |
| 2002 | int v4Enable = std::stoi(result[0]); |
| 2003 | return v4Enable; |
| 2004 | } |
| 2005 | |
| 2006 | bool getIpfwdV6Enable() { |
Lorenzo Colitti | 76edb4b | 2019-05-09 11:46:45 +0900 | [diff] [blame] | 2007 | static const char ipv6IpfwdCmd[] = "cat /proc/sys/net/ipv6/conf/all/forwarding"; |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 2008 | std::vector<std::string> result = runCommand(ipv6IpfwdCmd); |
| 2009 | EXPECT_TRUE(!result.empty()); |
| 2010 | int v6Enable = std::stoi(result[0]); |
| 2011 | return v6Enable; |
| 2012 | } |
| 2013 | |
| 2014 | void expectIpfwdEnable(bool enable) { |
| 2015 | int enableIPv4 = getIpfwdV4Enable(); |
| 2016 | int enableIPv6 = getIpfwdV6Enable(); |
| 2017 | EXPECT_EQ(enable, enableIPv4); |
| 2018 | EXPECT_EQ(enable, enableIPv6); |
| 2019 | } |
| 2020 | |
Bernie Innocenti | 1bdf10d | 2018-09-10 18:46:07 +0900 | [diff] [blame] | 2021 | bool ipRuleIpfwdExists(const char* ipVersion, const std::string& ipfwdRule) { |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 2022 | std::vector<std::string> rules = listIpRules(ipVersion); |
| 2023 | for (const auto& rule : rules) { |
| 2024 | if (rule.find(ipfwdRule) != std::string::npos) { |
| 2025 | return true; |
| 2026 | } |
| 2027 | } |
| 2028 | return false; |
| 2029 | } |
| 2030 | |
| 2031 | void expectIpfwdRuleExists(const char* fromIf, const char* toIf) { |
Ken Chen | 7099e2e | 2021-02-08 00:57:47 +0800 | [diff] [blame] | 2032 | std::string ipfwdRule = |
Chalard Jean | 38bc084 | 2022-12-26 19:36:13 +0900 | [diff] [blame] | 2033 | StringPrintf("%u:\tfrom all iif %s lookup %s", RULE_PRIORITY_TETHERING, fromIf, toIf); |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 2034 | |
| 2035 | for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { |
| 2036 | EXPECT_TRUE(ipRuleIpfwdExists(ipVersion, ipfwdRule)); |
| 2037 | } |
| 2038 | } |
| 2039 | |
| 2040 | void expectIpfwdRuleNotExists(const char* fromIf, const char* toIf) { |
Ken Chen | 7099e2e | 2021-02-08 00:57:47 +0800 | [diff] [blame] | 2041 | std::string ipfwdRule = |
Chalard Jean | 38bc084 | 2022-12-26 19:36:13 +0900 | [diff] [blame] | 2042 | StringPrintf("%u:\tfrom all iif %s lookup %s", RULE_PRIORITY_TETHERING, fromIf, toIf); |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 2043 | |
| 2044 | for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { |
| 2045 | EXPECT_FALSE(ipRuleIpfwdExists(ipVersion, ipfwdRule)); |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | } // namespace |
| 2050 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 2051 | TEST_F(NetdBinderTest, TestIpfwdEnableDisableStatusForwarding) { |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 2052 | // Get ipfwd requester list from Netd |
| 2053 | std::vector<std::string> requesterList; |
| 2054 | binder::Status status = mNetd->ipfwdGetRequesterList(&requesterList); |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 2055 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 2056 | |
| 2057 | bool ipfwdEnabled; |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 2058 | if (requesterList.size() == 0) { |
| 2059 | // No requester in Netd, ipfwd should be disabled |
| 2060 | // So add one test requester and verify |
| 2061 | status = mNetd->ipfwdEnableForwarding("TestRequester"); |
| 2062 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 2063 | |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 2064 | expectIpfwdEnable(true); |
| 2065 | status = mNetd->ipfwdEnabled(&ipfwdEnabled); |
| 2066 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2067 | EXPECT_TRUE(ipfwdEnabled); |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 2068 | |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 2069 | // Remove test one, verify again |
| 2070 | status = mNetd->ipfwdDisableForwarding("TestRequester"); |
| 2071 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2072 | |
| 2073 | expectIpfwdEnable(false); |
| 2074 | status = mNetd->ipfwdEnabled(&ipfwdEnabled); |
| 2075 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2076 | EXPECT_FALSE(ipfwdEnabled); |
| 2077 | } else { |
| 2078 | // Disable all requesters |
| 2079 | for (const auto& requester : requesterList) { |
| 2080 | status = mNetd->ipfwdDisableForwarding(requester); |
| 2081 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2082 | } |
| 2083 | |
| 2084 | // After disable all requester, ipfwd should be disabled |
| 2085 | expectIpfwdEnable(false); |
| 2086 | status = mNetd->ipfwdEnabled(&ipfwdEnabled); |
| 2087 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2088 | EXPECT_FALSE(ipfwdEnabled); |
| 2089 | |
| 2090 | // Enable them back |
| 2091 | for (const auto& requester : requesterList) { |
| 2092 | status = mNetd->ipfwdEnableForwarding(requester); |
| 2093 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2094 | } |
| 2095 | |
| 2096 | // ipfwd should be enabled |
| 2097 | expectIpfwdEnable(true); |
| 2098 | status = mNetd->ipfwdEnabled(&ipfwdEnabled); |
| 2099 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2100 | EXPECT_TRUE(ipfwdEnabled); |
| 2101 | } |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 2102 | } |
| 2103 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 2104 | TEST_F(NetdBinderTest, TestIpfwdAddRemoveInterfaceForward) { |
Lorenzo Colitti | faa0edf | 2020-06-03 21:56:46 +0900 | [diff] [blame] | 2105 | // Add test physical network |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 2106 | auto config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 2107 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 2108 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Lorenzo Colitti | faa0edf | 2020-06-03 21:56:46 +0900 | [diff] [blame] | 2109 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 2110 | |
| 2111 | config.netId = TEST_NETID2; |
| 2112 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Lorenzo Colitti | faa0edf | 2020-06-03 21:56:46 +0900 | [diff] [blame] | 2113 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID2, sTun2.name()).isOk()); |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 2114 | |
Lorenzo Colitti | faa0edf | 2020-06-03 21:56:46 +0900 | [diff] [blame] | 2115 | binder::Status status = mNetd->ipfwdAddInterfaceForward(sTun.name(), sTun2.name()); |
| 2116 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2117 | expectIpfwdRuleExists(sTun.name().c_str(), sTun2.name().c_str()); |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 2118 | |
Lorenzo Colitti | faa0edf | 2020-06-03 21:56:46 +0900 | [diff] [blame] | 2119 | status = mNetd->ipfwdRemoveInterfaceForward(sTun.name(), sTun2.name()); |
| 2120 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2121 | expectIpfwdRuleNotExists(sTun.name().c_str(), sTun2.name().c_str()); |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 2122 | } |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 2123 | |
| 2124 | namespace { |
| 2125 | |
| 2126 | constexpr char BANDWIDTH_INPUT[] = "bw_INPUT"; |
| 2127 | constexpr char BANDWIDTH_OUTPUT[] = "bw_OUTPUT"; |
| 2128 | constexpr char BANDWIDTH_FORWARD[] = "bw_FORWARD"; |
| 2129 | constexpr char BANDWIDTH_NAUGHTY[] = "bw_penalty_box"; |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 2130 | constexpr char BANDWIDTH_ALERT[] = "bw_global_alert"; |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 2131 | |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 2132 | // TODO: Move iptablesTargetsExists and listIptablesRuleByTable to the top. |
| 2133 | // Use either a std::vector<std::string> of things to match, or a variadic function. |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 2134 | bool iptablesTargetsExists(const char* binary, int expectedCount, const char* table, |
| 2135 | const char* chainName, const std::string& expectedTargetA, |
| 2136 | const std::string& expectedTargetB) { |
| 2137 | std::vector<std::string> rules = listIptablesRuleByTable(binary, table, chainName); |
| 2138 | int matchCount = 0; |
| 2139 | |
| 2140 | for (const auto& rule : rules) { |
| 2141 | if (rule.find(expectedTargetA) != std::string::npos) { |
| 2142 | if (rule.find(expectedTargetB) != std::string::npos) { |
| 2143 | matchCount++; |
| 2144 | } |
| 2145 | } |
| 2146 | } |
| 2147 | return matchCount == expectedCount; |
| 2148 | } |
| 2149 | |
| 2150 | void expectXtQuotaValueEqual(const char* ifname, long quotaBytes) { |
| 2151 | std::string path = StringPrintf("/proc/net/xt_quota/%s", ifname); |
| 2152 | std::string result = ""; |
| 2153 | |
| 2154 | EXPECT_TRUE(ReadFileToString(path, &result)); |
Luke Huang | 4953ca2 | 2018-09-14 14:08:50 +0800 | [diff] [blame] | 2155 | // Quota value might be decreased while matching packets |
| 2156 | EXPECT_GE(quotaBytes, std::stol(Trim(result))); |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | void expectBandwidthInterfaceQuotaRuleExists(const char* ifname, long quotaBytes) { |
| 2160 | std::string BANDWIDTH_COSTLY_IF = StringPrintf("bw_costly_%s", ifname); |
| 2161 | std::string quotaRule = StringPrintf("quota %s", ifname); |
| 2162 | |
| 2163 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 2164 | EXPECT_TRUE(iptablesTargetsExists(binary, 1, FILTER_TABLE, BANDWIDTH_INPUT, ifname, |
| 2165 | BANDWIDTH_COSTLY_IF)); |
| 2166 | EXPECT_TRUE(iptablesTargetsExists(binary, 1, FILTER_TABLE, BANDWIDTH_OUTPUT, ifname, |
| 2167 | BANDWIDTH_COSTLY_IF)); |
| 2168 | EXPECT_TRUE(iptablesTargetsExists(binary, 2, FILTER_TABLE, BANDWIDTH_FORWARD, ifname, |
| 2169 | BANDWIDTH_COSTLY_IF)); |
| 2170 | EXPECT_TRUE(iptablesRuleExists(binary, BANDWIDTH_COSTLY_IF.c_str(), BANDWIDTH_NAUGHTY)); |
| 2171 | EXPECT_TRUE(iptablesRuleExists(binary, BANDWIDTH_COSTLY_IF.c_str(), quotaRule)); |
| 2172 | } |
| 2173 | expectXtQuotaValueEqual(ifname, quotaBytes); |
| 2174 | } |
| 2175 | |
| 2176 | void expectBandwidthInterfaceQuotaRuleDoesNotExist(const char* ifname) { |
| 2177 | std::string BANDWIDTH_COSTLY_IF = StringPrintf("bw_costly_%s", ifname); |
| 2178 | std::string quotaRule = StringPrintf("quota %s", ifname); |
| 2179 | |
| 2180 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 2181 | EXPECT_FALSE(iptablesTargetsExists(binary, 1, FILTER_TABLE, BANDWIDTH_INPUT, ifname, |
| 2182 | BANDWIDTH_COSTLY_IF)); |
| 2183 | EXPECT_FALSE(iptablesTargetsExists(binary, 1, FILTER_TABLE, BANDWIDTH_OUTPUT, ifname, |
| 2184 | BANDWIDTH_COSTLY_IF)); |
| 2185 | EXPECT_FALSE(iptablesTargetsExists(binary, 2, FILTER_TABLE, BANDWIDTH_FORWARD, ifname, |
| 2186 | BANDWIDTH_COSTLY_IF)); |
| 2187 | EXPECT_FALSE(iptablesRuleExists(binary, BANDWIDTH_COSTLY_IF.c_str(), BANDWIDTH_NAUGHTY)); |
| 2188 | EXPECT_FALSE(iptablesRuleExists(binary, BANDWIDTH_COSTLY_IF.c_str(), quotaRule)); |
| 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | void expectBandwidthInterfaceAlertRuleExists(const char* ifname, long alertBytes) { |
| 2193 | std::string BANDWIDTH_COSTLY_IF = StringPrintf("bw_costly_%s", ifname); |
| 2194 | std::string alertRule = StringPrintf("quota %sAlert", ifname); |
| 2195 | std::string alertName = StringPrintf("%sAlert", ifname); |
| 2196 | |
| 2197 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 2198 | EXPECT_TRUE(iptablesRuleExists(binary, BANDWIDTH_COSTLY_IF.c_str(), alertRule)); |
| 2199 | } |
| 2200 | expectXtQuotaValueEqual(alertName.c_str(), alertBytes); |
| 2201 | } |
| 2202 | |
| 2203 | void expectBandwidthInterfaceAlertRuleDoesNotExist(const char* ifname) { |
| 2204 | std::string BANDWIDTH_COSTLY_IF = StringPrintf("bw_costly_%s", ifname); |
| 2205 | std::string alertRule = StringPrintf("quota %sAlert", ifname); |
| 2206 | |
| 2207 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 2208 | EXPECT_FALSE(iptablesRuleExists(binary, BANDWIDTH_COSTLY_IF.c_str(), alertRule)); |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | void expectBandwidthGlobalAlertRuleExists(long alertBytes) { |
| 2213 | static const char globalAlertRule[] = "quota globalAlert"; |
| 2214 | static const char globalAlertName[] = "globalAlert"; |
| 2215 | |
| 2216 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 2217 | EXPECT_TRUE(iptablesRuleExists(binary, BANDWIDTH_ALERT, globalAlertRule)); |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 2218 | } |
| 2219 | expectXtQuotaValueEqual(globalAlertName, alertBytes); |
| 2220 | } |
| 2221 | |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 2222 | } // namespace |
| 2223 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 2224 | TEST_F(NetdBinderTest, BandwidthSetRemoveInterfaceQuota) { |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 2225 | long testQuotaBytes = 5550; |
| 2226 | |
| 2227 | // Add test physical network |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 2228 | const auto& config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 2229 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 2230 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 2231 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 2232 | |
| 2233 | binder::Status status = mNetd->bandwidthSetInterfaceQuota(sTun.name(), testQuotaBytes); |
| 2234 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2235 | expectBandwidthInterfaceQuotaRuleExists(sTun.name().c_str(), testQuotaBytes); |
| 2236 | |
| 2237 | status = mNetd->bandwidthRemoveInterfaceQuota(sTun.name()); |
| 2238 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2239 | expectBandwidthInterfaceQuotaRuleDoesNotExist(sTun.name().c_str()); |
| 2240 | |
| 2241 | // Remove test physical network |
| 2242 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 2243 | } |
| 2244 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 2245 | TEST_F(NetdBinderTest, BandwidthSetRemoveInterfaceAlert) { |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 2246 | long testAlertBytes = 373; |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 2247 | // Add test physical network |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 2248 | const auto& config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 2249 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 2250 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 2251 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 2252 | // Need to have a prior interface quota set to set an alert |
| 2253 | binder::Status status = mNetd->bandwidthSetInterfaceQuota(sTun.name(), testAlertBytes); |
| 2254 | status = mNetd->bandwidthSetInterfaceAlert(sTun.name(), testAlertBytes); |
| 2255 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2256 | expectBandwidthInterfaceAlertRuleExists(sTun.name().c_str(), testAlertBytes); |
| 2257 | |
| 2258 | status = mNetd->bandwidthRemoveInterfaceAlert(sTun.name()); |
| 2259 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2260 | expectBandwidthInterfaceAlertRuleDoesNotExist(sTun.name().c_str()); |
| 2261 | |
| 2262 | // Remove interface quota |
| 2263 | status = mNetd->bandwidthRemoveInterfaceQuota(sTun.name()); |
| 2264 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2265 | expectBandwidthInterfaceQuotaRuleDoesNotExist(sTun.name().c_str()); |
| 2266 | |
| 2267 | // Remove test physical network |
| 2268 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 2269 | } |
| 2270 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 2271 | TEST_F(NetdBinderTest, BandwidthSetGlobalAlert) { |
Luke Huang | 8ca0f1c | 2019-05-29 15:56:42 +0800 | [diff] [blame] | 2272 | int64_t testAlertBytes = 2097200; |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 2273 | |
| 2274 | binder::Status status = mNetd->bandwidthSetGlobalAlert(testAlertBytes); |
| 2275 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2276 | expectBandwidthGlobalAlertRuleExists(testAlertBytes); |
| 2277 | |
Luke Huang | 8ca0f1c | 2019-05-29 15:56:42 +0800 | [diff] [blame] | 2278 | testAlertBytes = 2098230; |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 2279 | status = mNetd->bandwidthSetGlobalAlert(testAlertBytes); |
| 2280 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2281 | expectBandwidthGlobalAlertRuleExists(testAlertBytes); |
| 2282 | } |
| 2283 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 2284 | TEST_F(NetdBinderTest, NetworkAddRemoveRouteUserPermission) { |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2285 | static const struct { |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2286 | const char* ipVersion; |
| 2287 | const char* testDest; |
| 2288 | const char* testNextHop; |
| 2289 | const bool expectSuccess; |
| 2290 | } kTestData[] = { |
| 2291 | {IP_RULE_V4, "0.0.0.0/0", "", true}, |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2292 | {IP_RULE_V4, "0.0.0.0/0", "10.251.10.0", true}, |
| 2293 | {IP_RULE_V4, "10.251.0.0/16", "", true}, |
| 2294 | {IP_RULE_V4, "10.251.0.0/16", "10.251.10.0", true}, |
| 2295 | {IP_RULE_V4, "10.251.0.0/16", "fe80::/64", false}, |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2296 | {IP_RULE_V6, "::/0", "", true}, |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2297 | {IP_RULE_V6, "::/0", "2001:db8::", true}, |
| 2298 | {IP_RULE_V6, "2001:db8:cafe::/64", "2001:db8::", true}, |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2299 | {IP_RULE_V4, "fe80::/64", "0.0.0.0", false}, |
Taras Antoshchuk | 35987de | 2021-09-21 17:45:15 +0200 | [diff] [blame] | 2300 | {IP_RULE_V4, "10.251.10.2/31", "throw", true}, |
| 2301 | {IP_RULE_V4, "10.251.10.2/31", "unreachable", true}, |
| 2302 | {IP_RULE_V4, "0.0.0.0/0", "throw", true}, |
| 2303 | {IP_RULE_V4, "0.0.0.0/0", "unreachable", true}, |
| 2304 | {IP_RULE_V6, "::/0", "throw", true}, |
| 2305 | {IP_RULE_V6, "::/0", "unreachable", true}, |
| 2306 | {IP_RULE_V6, "2001:db8:cafe::/64", "throw", true}, |
| 2307 | {IP_RULE_V6, "2001:db8:cafe::/64", "unreachable", true}, |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2308 | }; |
| 2309 | |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2310 | static const struct { |
| 2311 | const char* ipVersion; |
| 2312 | const char* testDest; |
| 2313 | const char* testNextHop; |
| 2314 | } kTestDataWithNextHop[] = { |
| 2315 | {IP_RULE_V4, "10.251.10.0/30", ""}, |
| 2316 | {IP_RULE_V6, "2001:db8::/32", ""}, |
| 2317 | }; |
| 2318 | |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2319 | static const char testTableLegacySystem[] = "legacy_system"; |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2320 | static const char testTableLegacyNetwork[] = "legacy_network"; |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2321 | const int testUid = randomUid(); |
| 2322 | const std::vector<int32_t> testUids = {testUid}; |
| 2323 | |
| 2324 | // Add test physical network |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 2325 | const auto& config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 2326 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 2327 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2328 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 2329 | |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2330 | // Setup route for testing nextHop |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 2331 | for (size_t i = 0; i < std::size(kTestDataWithNextHop); i++) { |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2332 | const auto& td = kTestDataWithNextHop[i]; |
| 2333 | |
| 2334 | // All route for test tun will disappear once the tun interface is deleted. |
| 2335 | binder::Status status = |
| 2336 | mNetd->networkAddRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop); |
| 2337 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2338 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 2339 | sTun.name().c_str()); |
| 2340 | |
| 2341 | // Add system permission for test uid, setup route in legacy system table. |
| 2342 | EXPECT_TRUE(mNetd->networkSetPermissionForUser(INetd::PERMISSION_SYSTEM, testUids).isOk()); |
| 2343 | |
| 2344 | status = mNetd->networkAddLegacyRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop, |
| 2345 | testUid); |
| 2346 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2347 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 2348 | testTableLegacySystem); |
| 2349 | |
| 2350 | // Remove system permission for test uid, setup route in legacy network table. |
| 2351 | EXPECT_TRUE(mNetd->networkClearPermissionForUser(testUids).isOk()); |
| 2352 | |
| 2353 | status = mNetd->networkAddLegacyRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop, |
| 2354 | testUid); |
| 2355 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2356 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 2357 | testTableLegacyNetwork); |
| 2358 | } |
| 2359 | |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 2360 | for (size_t i = 0; i < std::size(kTestData); i++) { |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2361 | const auto& td = kTestData[i]; |
| 2362 | |
| 2363 | binder::Status status = |
| 2364 | mNetd->networkAddRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop); |
| 2365 | if (td.expectSuccess) { |
| 2366 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2367 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2368 | sTun.name().c_str()); |
| 2369 | } else { |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2370 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 2371 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | status = mNetd->networkRemoveRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop); |
| 2375 | if (td.expectSuccess) { |
| 2376 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2377 | expectNetworkRouteDoesNotExist(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 2378 | sTun.name().c_str()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2379 | } else { |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2380 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 2381 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2382 | } |
| 2383 | |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2384 | // Add system permission for test uid, route will be added into legacy system table. |
| 2385 | EXPECT_TRUE(mNetd->networkSetPermissionForUser(INetd::PERMISSION_SYSTEM, testUids).isOk()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2386 | |
| 2387 | status = mNetd->networkAddLegacyRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop, |
| 2388 | testUid); |
| 2389 | if (td.expectSuccess) { |
| 2390 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2391 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2392 | testTableLegacySystem); |
| 2393 | } else { |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2394 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 2395 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | status = mNetd->networkRemoveLegacyRoute(TEST_NETID1, sTun.name(), td.testDest, |
| 2399 | td.testNextHop, testUid); |
| 2400 | if (td.expectSuccess) { |
| 2401 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2402 | expectNetworkRouteDoesNotExist(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 2403 | testTableLegacySystem); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2404 | } else { |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2405 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 2406 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2407 | } |
| 2408 | |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2409 | // Remove system permission for test uid, route will be added into legacy network table. |
| 2410 | EXPECT_TRUE(mNetd->networkClearPermissionForUser(testUids).isOk()); |
| 2411 | |
| 2412 | status = mNetd->networkAddLegacyRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop, |
| 2413 | testUid); |
| 2414 | if (td.expectSuccess) { |
| 2415 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2416 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 2417 | testTableLegacyNetwork); |
| 2418 | } else { |
| 2419 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 2420 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
| 2421 | } |
| 2422 | |
| 2423 | status = mNetd->networkRemoveLegacyRoute(TEST_NETID1, sTun.name(), td.testDest, |
| 2424 | td.testNextHop, testUid); |
| 2425 | if (td.expectSuccess) { |
| 2426 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2427 | expectNetworkRouteDoesNotExist(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 2428 | testTableLegacyNetwork); |
| 2429 | } else { |
| 2430 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 2431 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
| 2432 | } |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2433 | } |
| 2434 | |
Taras Antoshchuk | 8fba2bb | 2021-09-24 18:37:17 +0200 | [diff] [blame] | 2435 | /* |
| 2436 | * Test networkUpdateRouteParcel behavior in case of route MTU change. |
| 2437 | * |
| 2438 | * Change of route MTU should be treated as an update of the route: |
| 2439 | * - networkUpdateRouteParcel should succeed and update route MTU. |
| 2440 | */ |
Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 2441 | for (size_t i = 0; i < std::size(kTestData); i++) { |
| 2442 | const auto& td = kTestData[i]; |
| 2443 | int mtu = (i % 2) ? 1480 : 1280; |
| 2444 | |
| 2445 | android::net::RouteInfoParcel parcel; |
| 2446 | parcel.ifName = sTun.name(); |
| 2447 | parcel.destination = td.testDest; |
| 2448 | parcel.nextHop = td.testNextHop; |
| 2449 | parcel.mtu = mtu; |
| 2450 | binder::Status status = mNetd->networkAddRouteParcel(TEST_NETID1, parcel); |
| 2451 | if (td.expectSuccess) { |
| 2452 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2453 | expectNetworkRouteExistsWithMtu(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 2454 | std::to_string(parcel.mtu), sTun.name().c_str()); |
| 2455 | } else { |
| 2456 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 2457 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
| 2458 | } |
| 2459 | |
| 2460 | parcel.mtu = 1337; |
| 2461 | status = mNetd->networkUpdateRouteParcel(TEST_NETID1, parcel); |
| 2462 | if (td.expectSuccess) { |
| 2463 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2464 | expectNetworkRouteExistsWithMtu(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 2465 | std::to_string(parcel.mtu), sTun.name().c_str()); |
| 2466 | } else { |
| 2467 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 2468 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
| 2469 | } |
| 2470 | |
| 2471 | status = mNetd->networkRemoveRouteParcel(TEST_NETID1, parcel); |
| 2472 | if (td.expectSuccess) { |
| 2473 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2474 | expectNetworkRouteDoesNotExist(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 2475 | sTun.name().c_str()); |
| 2476 | } else { |
| 2477 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 2478 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
| 2479 | } |
| 2480 | } |
Taras Antoshchuk | 8fba2bb | 2021-09-24 18:37:17 +0200 | [diff] [blame] | 2481 | |
| 2482 | /* |
| 2483 | * Test network[Update|Add]RouteParcel behavior in case of route type change. |
| 2484 | * |
| 2485 | * Change of route type should be treated as an update of the route: |
| 2486 | * - networkUpdateRouteParcel should succeed and update route type. |
| 2487 | * - networkAddRouteParcel should silently fail, because the route already exists. Route type |
| 2488 | * should not be changed in this case. |
| 2489 | */ |
| 2490 | for (size_t i = 0; i < std::size(kTestData); i++) { |
| 2491 | const auto& td = kTestData[i]; |
| 2492 | |
| 2493 | if (!td.expectSuccess) { |
| 2494 | continue; |
| 2495 | } |
| 2496 | |
| 2497 | android::net::RouteInfoParcel parcel; |
| 2498 | parcel.ifName = sTun.name(); |
| 2499 | parcel.destination = td.testDest; |
| 2500 | parcel.nextHop = td.testNextHop; |
| 2501 | parcel.mtu = 1280; |
| 2502 | binder::Status status = mNetd->networkAddRouteParcel(TEST_NETID1, parcel); |
| 2503 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2504 | expectNetworkRouteExistsWithMtu(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 2505 | std::to_string(parcel.mtu), sTun.name().c_str()); |
| 2506 | |
| 2507 | parcel.nextHop = parcel.nextHop == "throw" ? "unreachable" : "throw"; |
| 2508 | const char* oldNextHop = td.testNextHop; |
| 2509 | const char* newNextHop = parcel.nextHop.c_str(); |
| 2510 | |
| 2511 | // Trying to add same route with changed type, this should silently fail. |
| 2512 | status = mNetd->networkAddRouteParcel(TEST_NETID1, parcel); |
| 2513 | // No error reported. |
| 2514 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2515 | // Old route still exists. |
| 2516 | expectNetworkRouteExistsWithMtu(td.ipVersion, sTun.name(), td.testDest, oldNextHop, |
| 2517 | std::to_string(parcel.mtu), sTun.name().c_str()); |
| 2518 | // New route was not actually added. |
| 2519 | expectNetworkRouteDoesNotExistWithMtu(td.ipVersion, sTun.name(), td.testDest, newNextHop, |
| 2520 | std::to_string(parcel.mtu), sTun.name().c_str()); |
| 2521 | |
| 2522 | // Update should succeed. |
| 2523 | status = mNetd->networkUpdateRouteParcel(TEST_NETID1, parcel); |
| 2524 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2525 | expectNetworkRouteExistsWithMtu(td.ipVersion, sTun.name(), td.testDest, newNextHop, |
| 2526 | std::to_string(parcel.mtu), sTun.name().c_str()); |
| 2527 | expectNetworkRouteDoesNotExistWithMtu(td.ipVersion, sTun.name(), td.testDest, oldNextHop, |
| 2528 | std::to_string(parcel.mtu), sTun.name().c_str()); |
| 2529 | |
| 2530 | status = mNetd->networkRemoveRouteParcel(TEST_NETID1, parcel); |
| 2531 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2532 | expectNetworkRouteDoesNotExistWithMtu(td.ipVersion, sTun.name(), td.testDest, newNextHop, |
| 2533 | std::to_string(parcel.mtu), sTun.name().c_str()); |
| 2534 | } |
| 2535 | |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2536 | // Remove test physical network |
| 2537 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 2538 | } |
| 2539 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 2540 | TEST_F(NetdBinderTest, NetworkPermissionDefault) { |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2541 | // Add test physical network |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 2542 | const auto& config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 2543 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 2544 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2545 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 2546 | |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 2547 | // Get current default network NetId |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 2548 | binder::Status status = mNetd->networkGetDefault(&mStoredDefaultNetwork); |
| 2549 | ASSERT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2550 | |
| 2551 | // Test SetDefault |
| 2552 | status = mNetd->networkSetDefault(TEST_NETID1); |
| 2553 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2554 | expectNetworkDefaultIpRuleExists(sTun.name().c_str()); |
| 2555 | |
| 2556 | status = mNetd->networkClearDefault(); |
| 2557 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2558 | expectNetworkDefaultIpRuleDoesNotExist(); |
| 2559 | |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 2560 | // Set default network back |
| 2561 | status = mNetd->networkSetDefault(mStoredDefaultNetwork); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2562 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2563 | |
| 2564 | // Test SetPermission |
| 2565 | status = mNetd->networkSetPermissionForNetwork(TEST_NETID1, INetd::PERMISSION_SYSTEM); |
| 2566 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2567 | expectNetworkPermissionIpRuleExists(sTun.name().c_str(), INetd::PERMISSION_SYSTEM); |
| 2568 | expectNetworkPermissionIptablesRuleExists(sTun.name().c_str(), INetd::PERMISSION_SYSTEM); |
| 2569 | |
| 2570 | status = mNetd->networkSetPermissionForNetwork(TEST_NETID1, INetd::PERMISSION_NONE); |
| 2571 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2572 | expectNetworkPermissionIpRuleExists(sTun.name().c_str(), INetd::PERMISSION_NONE); |
| 2573 | expectNetworkPermissionIptablesRuleExists(sTun.name().c_str(), INetd::PERMISSION_NONE); |
| 2574 | |
| 2575 | // Remove test physical network |
| 2576 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 2577 | } |
| 2578 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 2579 | TEST_F(NetdBinderTest, NetworkSetProtectAllowDeny) { |
Luke Huang | 854e299 | 2019-05-29 16:42:48 +0800 | [diff] [blame] | 2580 | binder::Status status = mNetd->networkSetProtectAllow(TEST_UID1); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2581 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2582 | bool ret = false; |
Luke Huang | 854e299 | 2019-05-29 16:42:48 +0800 | [diff] [blame] | 2583 | status = mNetd->networkCanProtect(TEST_UID1, &ret); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2584 | EXPECT_TRUE(ret); |
| 2585 | |
Luke Huang | 854e299 | 2019-05-29 16:42:48 +0800 | [diff] [blame] | 2586 | status = mNetd->networkSetProtectDeny(TEST_UID1); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2587 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | 854e299 | 2019-05-29 16:42:48 +0800 | [diff] [blame] | 2588 | |
| 2589 | // Clear uid permission before calling networkCanProtect to ensure |
| 2590 | // the call won't be affected by uid permission. |
| 2591 | EXPECT_TRUE(mNetd->networkClearPermissionForUser({TEST_UID1}).isOk()); |
| 2592 | |
| 2593 | status = mNetd->networkCanProtect(TEST_UID1, &ret); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2594 | EXPECT_FALSE(ret); |
| 2595 | } |
| 2596 | |
| 2597 | namespace { |
| 2598 | |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2599 | int readIntFromPath(const std::string& path) { |
| 2600 | std::string result = ""; |
| 2601 | EXPECT_TRUE(ReadFileToString(path, &result)); |
| 2602 | return std::stoi(result); |
| 2603 | } |
| 2604 | |
| 2605 | int getTetherAcceptIPv6Ra(const std::string& ifName) { |
| 2606 | std::string path = StringPrintf("/proc/sys/net/ipv6/conf/%s/accept_ra", ifName.c_str()); |
| 2607 | return readIntFromPath(path); |
| 2608 | } |
| 2609 | |
| 2610 | bool getTetherAcceptIPv6Dad(const std::string& ifName) { |
| 2611 | std::string path = StringPrintf("/proc/sys/net/ipv6/conf/%s/accept_dad", ifName.c_str()); |
| 2612 | return readIntFromPath(path); |
| 2613 | } |
| 2614 | |
| 2615 | int getTetherIPv6DadTransmits(const std::string& ifName) { |
| 2616 | std::string path = StringPrintf("/proc/sys/net/ipv6/conf/%s/dad_transmits", ifName.c_str()); |
| 2617 | return readIntFromPath(path); |
| 2618 | } |
| 2619 | |
| 2620 | bool getTetherEnableIPv6(const std::string& ifName) { |
| 2621 | std::string path = StringPrintf("/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifName.c_str()); |
| 2622 | int disableIPv6 = readIntFromPath(path); |
| 2623 | return !disableIPv6; |
| 2624 | } |
| 2625 | |
| 2626 | bool interfaceListContains(const std::vector<std::string>& ifList, const std::string& ifName) { |
| 2627 | for (const auto& iface : ifList) { |
| 2628 | if (iface == ifName) { |
| 2629 | return true; |
| 2630 | } |
| 2631 | } |
| 2632 | return false; |
| 2633 | } |
| 2634 | |
| 2635 | void expectTetherInterfaceConfigureForIPv6Router(const std::string& ifName) { |
| 2636 | EXPECT_EQ(getTetherAcceptIPv6Ra(ifName), 0); |
| 2637 | EXPECT_FALSE(getTetherAcceptIPv6Dad(ifName)); |
| 2638 | EXPECT_EQ(getTetherIPv6DadTransmits(ifName), 0); |
| 2639 | EXPECT_TRUE(getTetherEnableIPv6(ifName)); |
| 2640 | } |
| 2641 | |
| 2642 | void expectTetherInterfaceConfigureForIPv6Client(const std::string& ifName) { |
| 2643 | EXPECT_EQ(getTetherAcceptIPv6Ra(ifName), 2); |
| 2644 | EXPECT_TRUE(getTetherAcceptIPv6Dad(ifName)); |
| 2645 | EXPECT_EQ(getTetherIPv6DadTransmits(ifName), 1); |
| 2646 | EXPECT_FALSE(getTetherEnableIPv6(ifName)); |
| 2647 | } |
| 2648 | |
| 2649 | void expectTetherInterfaceExists(const std::vector<std::string>& ifList, |
| 2650 | const std::string& ifName) { |
| 2651 | EXPECT_TRUE(interfaceListContains(ifList, ifName)); |
| 2652 | } |
| 2653 | |
| 2654 | void expectTetherInterfaceNotExists(const std::vector<std::string>& ifList, |
| 2655 | const std::string& ifName) { |
| 2656 | EXPECT_FALSE(interfaceListContains(ifList, ifName)); |
| 2657 | } |
| 2658 | |
| 2659 | void expectTetherDnsListEquals(const std::vector<std::string>& dnsList, |
| 2660 | const std::vector<std::string>& testDnsAddrs) { |
| 2661 | EXPECT_TRUE(dnsList == testDnsAddrs); |
| 2662 | } |
| 2663 | |
| 2664 | } // namespace |
| 2665 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 2666 | TEST_F(NetdBinderTest, TetherStartStopStatus) { |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2667 | std::vector<std::string> noDhcpRange = {}; |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 2668 | for (bool usingLegacyDnsProxy : {true, false}) { |
Chiachang Wang | 08cb211 | 2019-12-10 09:53:24 +0800 | [diff] [blame] | 2669 | android::net::TetherConfigParcel config; |
| 2670 | config.usingLegacyDnsProxy = usingLegacyDnsProxy; |
| 2671 | config.dhcpRanges = noDhcpRange; |
| 2672 | binder::Status status = mNetd->tetherStartWithConfiguration(config); |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 2673 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2674 | SCOPED_TRACE(StringPrintf("usingLegacyDnsProxy: %d", usingLegacyDnsProxy)); |
| 2675 | if (usingLegacyDnsProxy == true) { |
Lorenzo Colitti | 1b21374 | 2020-03-27 17:50:19 +0900 | [diff] [blame] | 2676 | expectProcessExists(DNSMASQ); |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 2677 | } else { |
Lorenzo Colitti | 1b21374 | 2020-03-27 17:50:19 +0900 | [diff] [blame] | 2678 | expectProcessDoesNotExist(DNSMASQ); |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 2679 | } |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2680 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 2681 | bool tetherEnabled; |
| 2682 | status = mNetd->tetherIsEnabled(&tetherEnabled); |
| 2683 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2684 | EXPECT_TRUE(tetherEnabled); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2685 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 2686 | status = mNetd->tetherStop(); |
| 2687 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Lorenzo Colitti | 1b21374 | 2020-03-27 17:50:19 +0900 | [diff] [blame] | 2688 | expectProcessDoesNotExist(DNSMASQ); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2689 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 2690 | status = mNetd->tetherIsEnabled(&tetherEnabled); |
| 2691 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2692 | EXPECT_FALSE(tetherEnabled); |
| 2693 | } |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2694 | } |
| 2695 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 2696 | TEST_F(NetdBinderTest, TetherInterfaceAddRemoveList) { |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2697 | // TODO: verify if dnsmasq update interface successfully |
| 2698 | |
| 2699 | binder::Status status = mNetd->tetherInterfaceAdd(sTun.name()); |
| 2700 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2701 | expectTetherInterfaceConfigureForIPv6Router(sTun.name()); |
| 2702 | |
| 2703 | std::vector<std::string> ifList; |
| 2704 | status = mNetd->tetherInterfaceList(&ifList); |
| 2705 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2706 | expectTetherInterfaceExists(ifList, sTun.name()); |
| 2707 | |
| 2708 | status = mNetd->tetherInterfaceRemove(sTun.name()); |
| 2709 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2710 | expectTetherInterfaceConfigureForIPv6Client(sTun.name()); |
| 2711 | |
| 2712 | status = mNetd->tetherInterfaceList(&ifList); |
| 2713 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2714 | expectTetherInterfaceNotExists(ifList, sTun.name()); |
chiachangwang | f281d2a | 2022-09-13 11:40:32 +0000 | [diff] [blame] | 2715 | |
| 2716 | // Disable IPv6 tethering will disable IPv6 abilities by changing IPv6 settings(accept_ra, |
| 2717 | // dad_transmits, accept_dad, disable_ipv6). See tetherInterfaceRemove in details. |
| 2718 | // Re-init sTun to reset the interface to prevent affecting other test that requires IPv6 with |
| 2719 | // the same interface. |
| 2720 | sTun.destroy(); |
| 2721 | sTun.init(); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2722 | } |
| 2723 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 2724 | TEST_F(NetdBinderTest, TetherDnsSetList) { |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2725 | // TODO: verify if dnsmasq update dns successfully |
Luke Huang | 8dc1cac | 2019-03-30 16:12:31 +0800 | [diff] [blame] | 2726 | std::vector<std::string> testDnsAddrs = {"192.168.1.37", "213.137.100.3", |
| 2727 | "fe80::1%" + sTun.name()}; |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2728 | |
| 2729 | binder::Status status = mNetd->tetherDnsSet(TEST_NETID1, testDnsAddrs); |
| 2730 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2731 | |
| 2732 | std::vector<std::string> dnsList; |
| 2733 | status = mNetd->tetherDnsList(&dnsList); |
| 2734 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2735 | expectTetherDnsListEquals(dnsList, testDnsAddrs); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | namespace { |
| 2739 | |
Lorenzo Colitti | 1b21374 | 2020-03-27 17:50:19 +0900 | [diff] [blame] | 2740 | std::vector<IPAddress> findDnsSockets(SockDiag* sd, unsigned numExpected) { |
| 2741 | std::vector<IPAddress> listenAddrs; |
| 2742 | |
| 2743 | // Callback lambda that finds all IPv4 sockets with source port 53. |
| 2744 | auto findDnsSockets = [&](uint8_t /* proto */, const inet_diag_msg* msg) { |
| 2745 | // Always return false, which means do not destroy this socket. |
| 2746 | if (msg->id.idiag_sport != htons(53)) return false; |
| 2747 | IPAddress addr(*(in_addr*)msg->id.idiag_src); |
| 2748 | listenAddrs.push_back(addr); |
| 2749 | return false; |
| 2750 | }; |
| 2751 | |
| 2752 | // There is no way to know if dnsmasq has finished processing the update_interfaces command and |
| 2753 | // opened listening sockets. So, just spin a few times and return the first list of sockets |
| 2754 | // that is at least numExpected long. |
Lorenzo Colitti | f12027e | 2020-04-30 14:06:10 +0900 | [diff] [blame] | 2755 | // Pick a relatively large timeout to avoid flaky tests, particularly when running on shared |
| 2756 | // devices. |
| 2757 | constexpr int kMaxAttempts = 50; |
Lorenzo Colitti | 1b21374 | 2020-03-27 17:50:19 +0900 | [diff] [blame] | 2758 | constexpr int kSleepMs = 100; |
| 2759 | for (int i = 0; i < kMaxAttempts; i++) { |
| 2760 | listenAddrs.clear(); |
| 2761 | EXPECT_EQ(0, sd->sendDumpRequest(IPPROTO_TCP, AF_INET, 1 << TCP_LISTEN)) |
| 2762 | << "Failed to dump sockets, attempt " << i << " of " << kMaxAttempts; |
| 2763 | sd->readDiagMsg(IPPROTO_TCP, findDnsSockets); |
| 2764 | if (listenAddrs.size() >= numExpected) { |
| 2765 | break; |
| 2766 | } |
| 2767 | usleep(kSleepMs * 1000); |
| 2768 | } |
| 2769 | |
| 2770 | return listenAddrs; |
| 2771 | } |
| 2772 | |
| 2773 | } // namespace |
| 2774 | |
| 2775 | // Checks that when starting dnsmasq on an interface that no longer exists, it doesn't attempt to |
| 2776 | // start on other interfaces instead. |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 2777 | TEST_F(NetdBinderTest, TetherDeletedInterface) { |
Lorenzo Colitti | 1b21374 | 2020-03-27 17:50:19 +0900 | [diff] [blame] | 2778 | // Do this first so we don't need to clean up anything else if it fails. |
| 2779 | SockDiag sd; |
| 2780 | ASSERT_TRUE(sd.open()) << "Failed to open SOCK_DIAG socket"; |
| 2781 | |
| 2782 | // Create our own TunInterfaces (so we can delete them without affecting other tests), and add |
| 2783 | // IP addresses to them. They must be IPv4 because tethering an interface disables and |
| 2784 | // re-enables IPv6 on the interface, which clears all addresses. |
| 2785 | TunInterface tun1, tun2; |
| 2786 | ASSERT_EQ(0, tun1.init()); |
| 2787 | ASSERT_EQ(0, tun2.init()); |
| 2788 | |
| 2789 | // Clean up. It is safe to call TunInterface::destroy multiple times. |
| 2790 | auto guard = android::base::make_scope_guard([&] { |
| 2791 | tun1.destroy(); |
| 2792 | tun2.destroy(); |
| 2793 | mNetd->tetherStop(); |
| 2794 | mNetd->tetherInterfaceRemove(tun1.name()); |
| 2795 | mNetd->tetherInterfaceRemove(tun2.name()); |
| 2796 | }); |
| 2797 | |
| 2798 | IPAddress addr1, addr2; |
| 2799 | ASSERT_TRUE(IPAddress::forString("192.0.2.1", &addr1)); |
| 2800 | ASSERT_TRUE(IPAddress::forString("192.0.2.2", &addr2)); |
| 2801 | EXPECT_EQ(0, tun1.addAddress(addr1.toString(), 32)); |
| 2802 | EXPECT_EQ(0, tun2.addAddress(addr2.toString(), 32)); |
| 2803 | |
| 2804 | // Stop tethering. |
| 2805 | binder::Status status = mNetd->tetherStop(); |
| 2806 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2807 | |
| 2808 | // Start dnsmasq on an interface that doesn't exist. |
| 2809 | // First, tether our tun interface... |
| 2810 | status = mNetd->tetherInterfaceAdd(tun1.name()); |
| 2811 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2812 | expectTetherInterfaceConfigureForIPv6Router(tun1.name()); |
| 2813 | |
| 2814 | // ... then delete it... |
| 2815 | tun1.destroy(); |
| 2816 | |
| 2817 | // ... then start dnsmasq. |
| 2818 | android::net::TetherConfigParcel config; |
| 2819 | config.usingLegacyDnsProxy = true; |
| 2820 | config.dhcpRanges = {}; |
| 2821 | status = mNetd->tetherStartWithConfiguration(config); |
| 2822 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2823 | |
| 2824 | // Wait for dnsmasq to start. |
| 2825 | expectProcessExists(DNSMASQ); |
| 2826 | |
| 2827 | // Make sure that netd thinks the interface is tethered (even though it doesn't exist). |
| 2828 | std::vector<std::string> ifList; |
| 2829 | status = mNetd->tetherInterfaceList(&ifList); |
| 2830 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2831 | ASSERT_EQ(1U, ifList.size()); |
| 2832 | EXPECT_EQ(tun1.name(), ifList[0]); |
| 2833 | |
| 2834 | // Give dnsmasq some time to start up. |
| 2835 | usleep(200 * 1000); |
| 2836 | |
| 2837 | // Check that dnsmasq is not listening on any IP addresses. It shouldn't, because it was only |
| 2838 | // told to run on tun1, and tun1 does not exist. Ensure it stays running and doesn't listen on |
| 2839 | // any IP addresses. |
| 2840 | std::vector<IPAddress> listenAddrs = findDnsSockets(&sd, 0); |
| 2841 | EXPECT_EQ(0U, listenAddrs.size()) << "Unexpectedly found IPv4 socket(s) listening on port 53"; |
| 2842 | |
| 2843 | // Now add an interface to dnsmasq and check that we can see the sockets. This confirms that |
| 2844 | // findDnsSockets is actually able to see sockets when they exist. |
| 2845 | status = mNetd->tetherInterfaceAdd(tun2.name()); |
| 2846 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2847 | |
| 2848 | in_addr loopback = {htonl(INADDR_LOOPBACK)}; |
| 2849 | listenAddrs = findDnsSockets(&sd, 2); |
| 2850 | EXPECT_EQ(2U, listenAddrs.size()) << "Expected exactly 2 IPv4 sockets listening on port 53"; |
| 2851 | EXPECT_EQ(1, std::count(listenAddrs.begin(), listenAddrs.end(), addr2)); |
| 2852 | EXPECT_EQ(1, std::count(listenAddrs.begin(), listenAddrs.end(), IPAddress(loopback))); |
| 2853 | |
| 2854 | // Clean up. |
| 2855 | status = mNetd->tetherStop(); |
| 2856 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2857 | |
| 2858 | expectProcessDoesNotExist(DNSMASQ); |
| 2859 | |
| 2860 | status = mNetd->tetherInterfaceRemove(tun1.name()); |
| 2861 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2862 | |
| 2863 | status = mNetd->tetherInterfaceRemove(tun2.name()); |
| 2864 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2865 | } |
| 2866 | |
| 2867 | namespace { |
| 2868 | |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2869 | constexpr char FIREWALL_INPUT[] = "fw_INPUT"; |
| 2870 | constexpr char FIREWALL_OUTPUT[] = "fw_OUTPUT"; |
| 2871 | constexpr char FIREWALL_FORWARD[] = "fw_FORWARD"; |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2872 | |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 2873 | void expectFirewallAllowlistMode() { |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2874 | static const char dropRule[] = "DROP all"; |
| 2875 | static const char rejectRule[] = "REJECT all"; |
| 2876 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 2877 | EXPECT_TRUE(iptablesRuleExists(binary, FIREWALL_INPUT, dropRule)); |
| 2878 | EXPECT_TRUE(iptablesRuleExists(binary, FIREWALL_OUTPUT, rejectRule)); |
| 2879 | EXPECT_TRUE(iptablesRuleExists(binary, FIREWALL_FORWARD, rejectRule)); |
| 2880 | } |
| 2881 | } |
| 2882 | |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 2883 | void expectFirewallDenylistMode() { |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 2884 | EXPECT_EQ(2, iptablesRuleLineLength(IPTABLES_PATH, FIREWALL_INPUT)); |
| 2885 | EXPECT_EQ(2, iptablesRuleLineLength(IPTABLES_PATH, FIREWALL_OUTPUT)); |
| 2886 | EXPECT_EQ(2, iptablesRuleLineLength(IPTABLES_PATH, FIREWALL_FORWARD)); |
| 2887 | |
| 2888 | // for IPv6 there is an extra OUTPUT rule to DROP ::1 sourced packets to non-loopback devices |
| 2889 | EXPECT_EQ(2, iptablesRuleLineLength(IP6TABLES_PATH, FIREWALL_INPUT)); |
| 2890 | EXPECT_EQ(3, iptablesRuleLineLength(IP6TABLES_PATH, FIREWALL_OUTPUT)); |
| 2891 | EXPECT_EQ(2, iptablesRuleLineLength(IP6TABLES_PATH, FIREWALL_FORWARD)); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | bool iptablesFirewallInterfaceFirstRuleExists(const char* binary, const char* chainName, |
| 2895 | const std::string& expectedInterface, |
| 2896 | const std::string& expectedRule) { |
| 2897 | std::vector<std::string> rules = listIptablesRuleByTable(binary, FILTER_TABLE, chainName); |
| 2898 | // Expected rule: |
| 2899 | // Chain fw_INPUT (1 references) |
| 2900 | // pkts bytes target prot opt in out source destination |
| 2901 | // 0 0 RETURN all -- expectedInterface * 0.0.0.0/0 0.0.0.0/0 |
| 2902 | // 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 |
| 2903 | int firstRuleIndex = 2; |
| 2904 | if (rules.size() < 4) return false; |
| 2905 | if (rules[firstRuleIndex].find(expectedInterface) != std::string::npos) { |
| 2906 | if (rules[firstRuleIndex].find(expectedRule) != std::string::npos) { |
| 2907 | return true; |
| 2908 | } |
| 2909 | } |
| 2910 | return false; |
| 2911 | } |
| 2912 | |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2913 | void expectFirewallInterfaceRuleAllowExists(const std::string& ifname) { |
| 2914 | static const char returnRule[] = "RETURN all"; |
| 2915 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 2916 | EXPECT_TRUE(iptablesFirewallInterfaceFirstRuleExists(binary, FIREWALL_INPUT, ifname, |
| 2917 | returnRule)); |
| 2918 | EXPECT_TRUE(iptablesFirewallInterfaceFirstRuleExists(binary, FIREWALL_OUTPUT, ifname, |
| 2919 | returnRule)); |
| 2920 | } |
| 2921 | } |
| 2922 | |
| 2923 | void expectFireWallInterfaceRuleAllowDoesNotExist(const std::string& ifname) { |
| 2924 | static const char returnRule[] = "RETURN all"; |
| 2925 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
Ken Chen | 8022f31 | 2023-11-16 04:13:19 +0800 | [diff] [blame] | 2926 | EXPECT_FALSE(iptablesInterfaceRuleExists(binary, FIREWALL_INPUT, ifname, returnRule, |
| 2927 | FILTER_TABLE)); |
| 2928 | EXPECT_FALSE(iptablesInterfaceRuleExists(binary, FIREWALL_OUTPUT, ifname, returnRule, |
| 2929 | FILTER_TABLE)); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2930 | } |
| 2931 | } |
| 2932 | |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2933 | } // namespace |
| 2934 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 2935 | TEST_F(NetdBinderTest, FirewallSetFirewallType) { |
Jooyung Han | f3e8bbc | 2021-01-07 15:38:40 +0900 | [diff] [blame] | 2936 | binder::Status status = mNetd->firewallSetFirewallType(INetd::FIREWALL_ALLOWLIST); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2937 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 2938 | expectFirewallAllowlistMode(); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2939 | |
Jooyung Han | f3e8bbc | 2021-01-07 15:38:40 +0900 | [diff] [blame] | 2940 | status = mNetd->firewallSetFirewallType(INetd::FIREWALL_DENYLIST); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2941 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 2942 | expectFirewallDenylistMode(); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2943 | |
| 2944 | // set firewall type blacklist twice |
Jooyung Han | f3e8bbc | 2021-01-07 15:38:40 +0900 | [diff] [blame] | 2945 | mNetd->firewallSetFirewallType(INetd::FIREWALL_DENYLIST); |
| 2946 | status = mNetd->firewallSetFirewallType(INetd::FIREWALL_DENYLIST); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2947 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 2948 | expectFirewallDenylistMode(); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2949 | |
| 2950 | // set firewall type whitelist twice |
Jooyung Han | f3e8bbc | 2021-01-07 15:38:40 +0900 | [diff] [blame] | 2951 | mNetd->firewallSetFirewallType(INetd::FIREWALL_ALLOWLIST); |
| 2952 | status = mNetd->firewallSetFirewallType(INetd::FIREWALL_ALLOWLIST); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2953 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 2954 | expectFirewallAllowlistMode(); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2955 | |
| 2956 | // reset firewall type to default |
Jooyung Han | f3e8bbc | 2021-01-07 15:38:40 +0900 | [diff] [blame] | 2957 | status = mNetd->firewallSetFirewallType(INetd::FIREWALL_DENYLIST); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2958 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 2959 | expectFirewallDenylistMode(); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2960 | } |
| 2961 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 2962 | TEST_F(NetdBinderTest, FirewallSetInterfaceRule) { |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2963 | // setinterfaceRule is not supported in BLACKLIST MODE |
Jooyung Han | f3e8bbc | 2021-01-07 15:38:40 +0900 | [diff] [blame] | 2964 | binder::Status status = mNetd->firewallSetFirewallType(INetd::FIREWALL_DENYLIST); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2965 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2966 | |
| 2967 | status = mNetd->firewallSetInterfaceRule(sTun.name(), INetd::FIREWALL_RULE_ALLOW); |
| 2968 | EXPECT_FALSE(status.isOk()) << status.exceptionMessage(); |
| 2969 | |
| 2970 | // set WHITELIST mode first |
Jooyung Han | f3e8bbc | 2021-01-07 15:38:40 +0900 | [diff] [blame] | 2971 | status = mNetd->firewallSetFirewallType(INetd::FIREWALL_ALLOWLIST); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2972 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2973 | |
| 2974 | status = mNetd->firewallSetInterfaceRule(sTun.name(), INetd::FIREWALL_RULE_ALLOW); |
| 2975 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2976 | expectFirewallInterfaceRuleAllowExists(sTun.name()); |
| 2977 | |
| 2978 | status = mNetd->firewallSetInterfaceRule(sTun.name(), INetd::FIREWALL_RULE_DENY); |
| 2979 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2980 | expectFireWallInterfaceRuleAllowDoesNotExist(sTun.name()); |
| 2981 | |
| 2982 | // reset firewall mode to default |
Jooyung Han | f3e8bbc | 2021-01-07 15:38:40 +0900 | [diff] [blame] | 2983 | status = mNetd->firewallSetFirewallType(INetd::FIREWALL_DENYLIST); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2984 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 2985 | expectFirewallDenylistMode(); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2986 | } |
| 2987 | |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 2988 | namespace { |
| 2989 | |
| 2990 | std::string hwAddrToStr(unsigned char* hwaddr) { |
| 2991 | return StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x", hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], |
| 2992 | hwaddr[4], hwaddr[5]); |
| 2993 | } |
| 2994 | |
| 2995 | int ipv4NetmaskToPrefixLength(in_addr_t mask) { |
| 2996 | int prefixLength = 0; |
| 2997 | uint32_t m = ntohl(mask); |
| 2998 | while (m & (1 << 31)) { |
| 2999 | prefixLength++; |
| 3000 | m = m << 1; |
| 3001 | } |
| 3002 | return prefixLength; |
| 3003 | } |
| 3004 | |
| 3005 | std::string toStdString(const String16& s) { |
Tomasz Wasilczyk | 434060c | 2023-08-11 16:10:13 +0000 | [diff] [blame] | 3006 | return std::string(String8(s.c_str())); |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | android::netdutils::StatusOr<ifreq> ioctlByIfName(const std::string& ifName, unsigned long flag) { |
| 3010 | const auto& sys = sSyscalls.get(); |
| 3011 | auto fd = sys.socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 3012 | EXPECT_TRUE(isOk(fd.status())); |
| 3013 | |
| 3014 | struct ifreq ifr = {}; |
| 3015 | strlcpy(ifr.ifr_name, ifName.c_str(), IFNAMSIZ); |
| 3016 | |
| 3017 | return sys.ioctl(fd.value(), flag, &ifr); |
| 3018 | } |
| 3019 | |
| 3020 | std::string getInterfaceHwAddr(const std::string& ifName) { |
| 3021 | auto res = ioctlByIfName(ifName, SIOCGIFHWADDR); |
| 3022 | |
| 3023 | unsigned char hwaddr[ETH_ALEN] = {}; |
| 3024 | if (isOk(res.status())) { |
| 3025 | memcpy((void*) hwaddr, &res.value().ifr_hwaddr.sa_data, ETH_ALEN); |
| 3026 | } |
| 3027 | |
| 3028 | return hwAddrToStr(hwaddr); |
| 3029 | } |
| 3030 | |
| 3031 | int getInterfaceIPv4Prefix(const std::string& ifName) { |
| 3032 | auto res = ioctlByIfName(ifName, SIOCGIFNETMASK); |
| 3033 | |
| 3034 | int prefixLength = 0; |
| 3035 | if (isOk(res.status())) { |
| 3036 | prefixLength = ipv4NetmaskToPrefixLength( |
| 3037 | ((struct sockaddr_in*) &res.value().ifr_addr)->sin_addr.s_addr); |
| 3038 | } |
| 3039 | |
| 3040 | return prefixLength; |
| 3041 | } |
| 3042 | |
| 3043 | std::string getInterfaceIPv4Addr(const std::string& ifName) { |
| 3044 | auto res = ioctlByIfName(ifName, SIOCGIFADDR); |
| 3045 | |
| 3046 | struct in_addr addr = {}; |
| 3047 | if (isOk(res.status())) { |
| 3048 | addr.s_addr = ((struct sockaddr_in*) &res.value().ifr_addr)->sin_addr.s_addr; |
| 3049 | } |
| 3050 | |
| 3051 | return std::string(inet_ntoa(addr)); |
| 3052 | } |
| 3053 | |
| 3054 | std::vector<std::string> getInterfaceFlags(const std::string& ifName) { |
| 3055 | auto res = ioctlByIfName(ifName, SIOCGIFFLAGS); |
| 3056 | |
| 3057 | unsigned flags = 0; |
| 3058 | if (isOk(res.status())) { |
| 3059 | flags = res.value().ifr_flags; |
| 3060 | } |
| 3061 | |
| 3062 | std::vector<std::string> ifFlags; |
| 3063 | ifFlags.push_back(flags & IFF_UP ? toStdString(INetd::IF_STATE_UP()) |
| 3064 | : toStdString(INetd::IF_STATE_DOWN())); |
| 3065 | |
| 3066 | if (flags & IFF_BROADCAST) ifFlags.push_back(toStdString(INetd::IF_FLAG_BROADCAST())); |
| 3067 | if (flags & IFF_LOOPBACK) ifFlags.push_back(toStdString(INetd::IF_FLAG_LOOPBACK())); |
| 3068 | if (flags & IFF_POINTOPOINT) ifFlags.push_back(toStdString(INetd::IF_FLAG_POINTOPOINT())); |
| 3069 | if (flags & IFF_RUNNING) ifFlags.push_back(toStdString(INetd::IF_FLAG_RUNNING())); |
| 3070 | if (flags & IFF_MULTICAST) ifFlags.push_back(toStdString(INetd::IF_FLAG_MULTICAST())); |
| 3071 | |
| 3072 | return ifFlags; |
| 3073 | } |
| 3074 | |
| 3075 | bool compareListInterface(const std::vector<std::string>& interfaceList) { |
waynema | 5851b03 | 2021-11-24 17:08:25 +0800 | [diff] [blame] | 3076 | const auto& res = getIfaceNames(); |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3077 | EXPECT_TRUE(isOk(res)); |
| 3078 | |
| 3079 | std::vector<std::string> resIfList; |
| 3080 | resIfList.reserve(res.value().size()); |
| 3081 | resIfList.insert(end(resIfList), begin(res.value()), end(res.value())); |
| 3082 | |
| 3083 | return resIfList == interfaceList; |
| 3084 | } |
| 3085 | |
| 3086 | int getInterfaceIPv6PrivacyExtensions(const std::string& ifName) { |
| 3087 | std::string path = StringPrintf("/proc/sys/net/ipv6/conf/%s/use_tempaddr", ifName.c_str()); |
| 3088 | return readIntFromPath(path); |
| 3089 | } |
| 3090 | |
| 3091 | bool getInterfaceEnableIPv6(const std::string& ifName) { |
| 3092 | std::string path = StringPrintf("/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifName.c_str()); |
| 3093 | |
| 3094 | int disableIPv6 = readIntFromPath(path); |
| 3095 | return !disableIPv6; |
| 3096 | } |
| 3097 | |
| 3098 | int getInterfaceMtu(const std::string& ifName) { |
| 3099 | std::string path = StringPrintf("/sys/class/net/%s/mtu", ifName.c_str()); |
| 3100 | return readIntFromPath(path); |
| 3101 | } |
| 3102 | |
| 3103 | void expectInterfaceList(const std::vector<std::string>& interfaceList) { |
| 3104 | EXPECT_TRUE(compareListInterface(interfaceList)); |
| 3105 | } |
| 3106 | |
| 3107 | void expectCurrentInterfaceConfigurationEquals(const std::string& ifName, |
| 3108 | const InterfaceConfigurationParcel& interfaceCfg) { |
| 3109 | EXPECT_EQ(getInterfaceIPv4Addr(ifName), interfaceCfg.ipv4Addr); |
| 3110 | EXPECT_EQ(getInterfaceIPv4Prefix(ifName), interfaceCfg.prefixLength); |
| 3111 | EXPECT_EQ(getInterfaceHwAddr(ifName), interfaceCfg.hwAddr); |
| 3112 | EXPECT_EQ(getInterfaceFlags(ifName), interfaceCfg.flags); |
| 3113 | } |
| 3114 | |
| 3115 | void expectCurrentInterfaceConfigurationAlmostEqual(const InterfaceConfigurationParcel& setCfg) { |
| 3116 | EXPECT_EQ(getInterfaceIPv4Addr(setCfg.ifName), setCfg.ipv4Addr); |
| 3117 | EXPECT_EQ(getInterfaceIPv4Prefix(setCfg.ifName), setCfg.prefixLength); |
| 3118 | |
| 3119 | const auto& ifFlags = getInterfaceFlags(setCfg.ifName); |
| 3120 | for (const auto& flag : setCfg.flags) { |
| 3121 | EXPECT_TRUE(std::find(ifFlags.begin(), ifFlags.end(), flag) != ifFlags.end()); |
| 3122 | } |
| 3123 | } |
| 3124 | |
| 3125 | void expectInterfaceIPv6PrivacyExtensions(const std::string& ifName, bool enable) { |
| 3126 | int v6PrivacyExtensions = getInterfaceIPv6PrivacyExtensions(ifName); |
| 3127 | EXPECT_EQ(v6PrivacyExtensions, enable ? 2 : 0); |
| 3128 | } |
| 3129 | |
| 3130 | void expectInterfaceNoAddr(const std::string& ifName) { |
| 3131 | // noAddr |
| 3132 | EXPECT_EQ(getInterfaceIPv4Addr(ifName), "0.0.0.0"); |
| 3133 | // noPrefix |
| 3134 | EXPECT_EQ(getInterfaceIPv4Prefix(ifName), 0); |
| 3135 | } |
| 3136 | |
| 3137 | void expectInterfaceEnableIPv6(const std::string& ifName, bool enable) { |
| 3138 | int enableIPv6 = getInterfaceEnableIPv6(ifName); |
| 3139 | EXPECT_EQ(enableIPv6, enable); |
| 3140 | } |
| 3141 | |
| 3142 | void expectInterfaceMtu(const std::string& ifName, const int mtu) { |
| 3143 | int mtuSize = getInterfaceMtu(ifName); |
| 3144 | EXPECT_EQ(mtu, mtuSize); |
| 3145 | } |
| 3146 | |
| 3147 | InterfaceConfigurationParcel makeInterfaceCfgParcel(const std::string& ifName, |
| 3148 | const std::string& addr, int prefixLength, |
| 3149 | const std::vector<std::string>& flags) { |
| 3150 | InterfaceConfigurationParcel cfg; |
| 3151 | cfg.ifName = ifName; |
| 3152 | cfg.hwAddr = ""; |
| 3153 | cfg.ipv4Addr = addr; |
| 3154 | cfg.prefixLength = prefixLength; |
| 3155 | cfg.flags = flags; |
| 3156 | return cfg; |
| 3157 | } |
| 3158 | |
| 3159 | void expectTunFlags(const InterfaceConfigurationParcel& interfaceCfg) { |
| 3160 | std::vector<std::string> expectedFlags = {"up", "point-to-point", "running", "multicast"}; |
| 3161 | std::vector<std::string> unexpectedFlags = {"down", "broadcast"}; |
| 3162 | |
| 3163 | for (const auto& flag : expectedFlags) { |
| 3164 | EXPECT_TRUE(std::find(interfaceCfg.flags.begin(), interfaceCfg.flags.end(), flag) != |
| 3165 | interfaceCfg.flags.end()); |
| 3166 | } |
| 3167 | |
| 3168 | for (const auto& flag : unexpectedFlags) { |
| 3169 | EXPECT_TRUE(std::find(interfaceCfg.flags.begin(), interfaceCfg.flags.end(), flag) == |
| 3170 | interfaceCfg.flags.end()); |
| 3171 | } |
| 3172 | } |
| 3173 | |
| 3174 | } // namespace |
| 3175 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3176 | TEST_F(NetdBinderTest, InterfaceList) { |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3177 | std::vector<std::string> interfaceListResult; |
| 3178 | |
| 3179 | binder::Status status = mNetd->interfaceGetList(&interfaceListResult); |
| 3180 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3181 | expectInterfaceList(interfaceListResult); |
| 3182 | } |
| 3183 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3184 | TEST_F(NetdBinderTest, InterfaceGetCfg) { |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3185 | InterfaceConfigurationParcel interfaceCfgResult; |
| 3186 | |
| 3187 | // Add test physical network |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3188 | const auto& config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 3189 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3190 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3191 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 3192 | |
| 3193 | binder::Status status = mNetd->interfaceGetCfg(sTun.name(), &interfaceCfgResult); |
| 3194 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3195 | expectCurrentInterfaceConfigurationEquals(sTun.name(), interfaceCfgResult); |
| 3196 | expectTunFlags(interfaceCfgResult); |
| 3197 | |
| 3198 | // Remove test physical network |
| 3199 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 3200 | } |
| 3201 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3202 | TEST_F(NetdBinderTest, InterfaceSetCfg) { |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3203 | const std::string testAddr = "192.0.2.3"; |
| 3204 | const int testPrefixLength = 24; |
| 3205 | std::vector<std::string> upFlags = {"up"}; |
| 3206 | std::vector<std::string> downFlags = {"down"}; |
| 3207 | |
| 3208 | // Add test physical network |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3209 | const auto& config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 3210 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3211 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3212 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 3213 | |
| 3214 | // Set tun interface down. |
| 3215 | auto interfaceCfg = makeInterfaceCfgParcel(sTun.name(), testAddr, testPrefixLength, downFlags); |
| 3216 | binder::Status status = mNetd->interfaceSetCfg(interfaceCfg); |
| 3217 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3218 | expectCurrentInterfaceConfigurationAlmostEqual(interfaceCfg); |
| 3219 | |
| 3220 | // Set tun interface up again. |
| 3221 | interfaceCfg = makeInterfaceCfgParcel(sTun.name(), testAddr, testPrefixLength, upFlags); |
| 3222 | status = mNetd->interfaceSetCfg(interfaceCfg); |
| 3223 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3224 | status = mNetd->interfaceClearAddrs(sTun.name()); |
| 3225 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3226 | |
| 3227 | // Remove test physical network |
| 3228 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 3229 | } |
| 3230 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3231 | TEST_F(NetdBinderTest, InterfaceSetIPv6PrivacyExtensions) { |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3232 | // enable |
| 3233 | binder::Status status = mNetd->interfaceSetIPv6PrivacyExtensions(sTun.name(), true); |
| 3234 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3235 | expectInterfaceIPv6PrivacyExtensions(sTun.name(), true); |
| 3236 | |
| 3237 | // disable |
| 3238 | status = mNetd->interfaceSetIPv6PrivacyExtensions(sTun.name(), false); |
| 3239 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3240 | expectInterfaceIPv6PrivacyExtensions(sTun.name(), false); |
| 3241 | } |
| 3242 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3243 | TEST_F(NetdBinderTest, InterfaceClearAddr) { |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3244 | const std::string testAddr = "192.0.2.3"; |
| 3245 | const int testPrefixLength = 24; |
| 3246 | std::vector<std::string> noFlags{}; |
| 3247 | |
| 3248 | // Add test physical network |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3249 | const auto& config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 3250 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3251 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3252 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 3253 | |
| 3254 | auto interfaceCfg = makeInterfaceCfgParcel(sTun.name(), testAddr, testPrefixLength, noFlags); |
| 3255 | binder::Status status = mNetd->interfaceSetCfg(interfaceCfg); |
| 3256 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3257 | expectCurrentInterfaceConfigurationAlmostEqual(interfaceCfg); |
| 3258 | |
| 3259 | status = mNetd->interfaceClearAddrs(sTun.name()); |
| 3260 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3261 | expectInterfaceNoAddr(sTun.name()); |
| 3262 | |
| 3263 | // Remove test physical network |
| 3264 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 3265 | } |
| 3266 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3267 | TEST_F(NetdBinderTest, InterfaceSetEnableIPv6) { |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3268 | // Add test physical network |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3269 | const auto& config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 3270 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3271 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3272 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 3273 | |
| 3274 | // disable |
| 3275 | binder::Status status = mNetd->interfaceSetEnableIPv6(sTun.name(), false); |
| 3276 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3277 | expectInterfaceEnableIPv6(sTun.name(), false); |
| 3278 | |
| 3279 | // enable |
| 3280 | status = mNetd->interfaceSetEnableIPv6(sTun.name(), true); |
| 3281 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3282 | expectInterfaceEnableIPv6(sTun.name(), true); |
| 3283 | |
| 3284 | // Remove test physical network |
| 3285 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 3286 | } |
| 3287 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3288 | TEST_F(NetdBinderTest, InterfaceSetMtu) { |
chiachangwang | f281d2a | 2022-09-13 11:40:32 +0000 | [diff] [blame] | 3289 | const int currentMtu = getInterfaceMtu(sTun.name()); |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3290 | const int testMtu = 1200; |
| 3291 | |
| 3292 | // Add test physical network |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3293 | const auto& config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 3294 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3295 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3296 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 3297 | |
| 3298 | binder::Status status = mNetd->interfaceSetMtu(sTun.name(), testMtu); |
| 3299 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3300 | expectInterfaceMtu(sTun.name(), testMtu); |
| 3301 | |
chiachangwang | f281d2a | 2022-09-13 11:40:32 +0000 | [diff] [blame] | 3302 | // restore the MTU back |
| 3303 | status = mNetd->interfaceSetMtu(sTun.name(), currentMtu); |
| 3304 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3305 | |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 3306 | // Remove test physical network |
| 3307 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 3308 | } |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 3309 | |
| 3310 | namespace { |
| 3311 | |
| 3312 | constexpr const char TETHER_FORWARD[] = "tetherctrl_FORWARD"; |
| 3313 | constexpr const char TETHER_NAT_POSTROUTING[] = "tetherctrl_nat_POSTROUTING"; |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 3314 | constexpr const char TETHER_RAW_PREROUTING[] = "tetherctrl_raw_PREROUTING"; |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 3315 | constexpr const char TETHER_COUNTERS_CHAIN[] = "tetherctrl_counters"; |
| 3316 | |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 3317 | int iptablesCountRules(const char* binary, const char* table, const char* chainName) { |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 3318 | return listIptablesRuleByTable(binary, table, chainName).size(); |
| 3319 | } |
| 3320 | |
| 3321 | bool iptablesChainMatch(const char* binary, const char* table, const char* chainName, |
| 3322 | const std::vector<std::string>& targetVec) { |
| 3323 | std::vector<std::string> rules = listIptablesRuleByTable(binary, table, chainName); |
| 3324 | if (targetVec.size() != rules.size() - 2) { |
| 3325 | return false; |
| 3326 | } |
| 3327 | |
| 3328 | /* |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 3329 | * Check that the rules match. Note that this function matches substrings, not entire rules, |
| 3330 | * because otherwise rules where "pkts" or "bytes" are nonzero would not match. |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 3331 | * Skip first two lines since rules start from third line. |
| 3332 | * Chain chainName (x references) |
| 3333 | * pkts bytes target prot opt in out source destination |
| 3334 | * ... |
| 3335 | */ |
| 3336 | int rIndex = 2; |
| 3337 | for (const auto& target : targetVec) { |
| 3338 | if (rules[rIndex].find(target) == std::string::npos) { |
| 3339 | return false; |
| 3340 | } |
| 3341 | rIndex++; |
| 3342 | } |
| 3343 | return true; |
| 3344 | } |
| 3345 | |
| 3346 | void expectNatEnable(const std::string& intIf, const std::string& extIf) { |
| 3347 | std::vector<std::string> postroutingV4Match = {"MASQUERADE"}; |
| 3348 | std::vector<std::string> preroutingV4Match = {"CT helper ftp", "CT helper pptp"}; |
| 3349 | std::vector<std::string> forwardV4Match = { |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 3350 | "bw_global_alert", "state RELATED", "state INVALID", |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 3351 | StringPrintf("tetherctrl_counters all -- %s %s", intIf.c_str(), extIf.c_str()), |
| 3352 | "DROP"}; |
| 3353 | |
| 3354 | // V4 |
| 3355 | EXPECT_TRUE(iptablesChainMatch(IPTABLES_PATH, NAT_TABLE, TETHER_NAT_POSTROUTING, |
| 3356 | postroutingV4Match)); |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 3357 | EXPECT_TRUE( |
| 3358 | iptablesChainMatch(IPTABLES_PATH, RAW_TABLE, TETHER_RAW_PREROUTING, preroutingV4Match)); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 3359 | EXPECT_TRUE(iptablesChainMatch(IPTABLES_PATH, FILTER_TABLE, TETHER_FORWARD, forwardV4Match)); |
| 3360 | |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 3361 | std::vector<std::string> forwardV6Match = {"bw_global_alert", "tetherctrl_counters"}; |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 3362 | std::vector<std::string> preroutingV6Match = {"rpfilter invert"}; |
| 3363 | |
| 3364 | // V6 |
| 3365 | EXPECT_TRUE(iptablesChainMatch(IP6TABLES_PATH, FILTER_TABLE, TETHER_FORWARD, forwardV6Match)); |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 3366 | EXPECT_TRUE(iptablesChainMatch(IP6TABLES_PATH, RAW_TABLE, TETHER_RAW_PREROUTING, |
| 3367 | preroutingV6Match)); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 3368 | |
| 3369 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 3370 | EXPECT_TRUE(iptablesTargetsExists(binary, 2, FILTER_TABLE, TETHER_COUNTERS_CHAIN, intIf, |
| 3371 | extIf)); |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | void expectNatDisable() { |
| 3376 | // It is the default DROP rule with tethering disable. |
| 3377 | // Chain tetherctrl_FORWARD (1 references) |
| 3378 | // pkts bytes target prot opt in out source destination |
| 3379 | // 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 |
| 3380 | std::vector<std::string> forwardV4Match = {"DROP"}; |
| 3381 | EXPECT_TRUE(iptablesChainMatch(IPTABLES_PATH, FILTER_TABLE, TETHER_FORWARD, forwardV4Match)); |
| 3382 | |
| 3383 | // We expect that these chains should be empty. |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 3384 | EXPECT_EQ(2, iptablesCountRules(IPTABLES_PATH, NAT_TABLE, TETHER_NAT_POSTROUTING)); |
| 3385 | EXPECT_EQ(2, iptablesCountRules(IPTABLES_PATH, RAW_TABLE, TETHER_RAW_PREROUTING)); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 3386 | |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 3387 | EXPECT_EQ(2, iptablesCountRules(IP6TABLES_PATH, FILTER_TABLE, TETHER_FORWARD)); |
| 3388 | EXPECT_EQ(2, iptablesCountRules(IP6TABLES_PATH, RAW_TABLE, TETHER_RAW_PREROUTING)); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 3389 | |
| 3390 | // Netd won't clear tether quota rule, we don't care rule in tetherctrl_counters. |
| 3391 | } |
| 3392 | |
| 3393 | } // namespace |
| 3394 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3395 | TEST_F(NetdBinderTest, TetherForwardAddRemove) { |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 3396 | binder::Status status = mNetd->tetherAddForward(sTun.name(), sTun2.name()); |
| 3397 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3398 | expectNatEnable(sTun.name(), sTun2.name()); |
| 3399 | |
| 3400 | status = mNetd->tetherRemoveForward(sTun.name(), sTun2.name()); |
| 3401 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3402 | expectNatDisable(); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 3403 | } |
Chenbo Feng | f5663d8 | 2018-11-08 16:10:48 -0800 | [diff] [blame] | 3404 | |
| 3405 | namespace { |
| 3406 | |
| 3407 | using TripleInt = std::array<int, 3>; |
| 3408 | |
| 3409 | TripleInt readProcFileToTripleInt(const std::string& path) { |
| 3410 | std::string valueString; |
| 3411 | int min, def, max; |
| 3412 | EXPECT_TRUE(ReadFileToString(path, &valueString)); |
| 3413 | EXPECT_EQ(3, sscanf(valueString.c_str(), "%d %d %d", &min, &def, &max)); |
| 3414 | return {min, def, max}; |
| 3415 | } |
| 3416 | |
| 3417 | void updateAndCheckTcpBuffer(sp<INetd>& netd, TripleInt& rmemValues, TripleInt& wmemValues) { |
| 3418 | std::string testRmemValues = |
| 3419 | StringPrintf("%u %u %u", rmemValues[0], rmemValues[1], rmemValues[2]); |
| 3420 | std::string testWmemValues = |
| 3421 | StringPrintf("%u %u %u", wmemValues[0], wmemValues[1], wmemValues[2]); |
| 3422 | EXPECT_TRUE(netd->setTcpRWmemorySize(testRmemValues, testWmemValues).isOk()); |
| 3423 | |
| 3424 | TripleInt newRmemValues = readProcFileToTripleInt(TCP_RMEM_PROC_FILE); |
| 3425 | TripleInt newWmemValues = readProcFileToTripleInt(TCP_WMEM_PROC_FILE); |
| 3426 | |
| 3427 | for (int i = 0; i < 3; i++) { |
| 3428 | SCOPED_TRACE(StringPrintf("tcp_mem value %d should be equal", i)); |
| 3429 | EXPECT_EQ(rmemValues[i], newRmemValues[i]); |
| 3430 | EXPECT_EQ(wmemValues[i], newWmemValues[i]); |
| 3431 | } |
| 3432 | } |
| 3433 | |
| 3434 | } // namespace |
| 3435 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3436 | TEST_F(NetdBinderTest, TcpBufferSet) { |
Chenbo Feng | f5663d8 | 2018-11-08 16:10:48 -0800 | [diff] [blame] | 3437 | TripleInt rmemValue = readProcFileToTripleInt(TCP_RMEM_PROC_FILE); |
| 3438 | TripleInt testRmemValue{rmemValue[0] + 42, rmemValue[1] + 42, rmemValue[2] + 42}; |
| 3439 | TripleInt wmemValue = readProcFileToTripleInt(TCP_WMEM_PROC_FILE); |
| 3440 | TripleInt testWmemValue{wmemValue[0] + 42, wmemValue[1] + 42, wmemValue[2] + 42}; |
| 3441 | |
| 3442 | updateAndCheckTcpBuffer(mNetd, testRmemValue, testWmemValue); |
| 3443 | updateAndCheckTcpBuffer(mNetd, rmemValue, wmemValue); |
| 3444 | } |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 3445 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3446 | TEST_F(NetdBinderTest, UnsolEvents) { |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 3447 | auto testUnsolService = android::net::TestUnsolService::start(); |
| 3448 | std::string oldTunName = sTun.name(); |
| 3449 | std::string newTunName = "unsolTest"; |
| 3450 | testUnsolService->tarVec.push_back(oldTunName); |
| 3451 | testUnsolService->tarVec.push_back(newTunName); |
| 3452 | auto& cv = testUnsolService->getCv(); |
| 3453 | auto& cvMutex = testUnsolService->getCvMutex(); |
| 3454 | binder::Status status = mNetd->registerUnsolicitedEventListener( |
| 3455 | android::interface_cast<android::net::INetdUnsolicitedEventListener>(testUnsolService)); |
| 3456 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3457 | |
| 3458 | // TODO: Add test for below events |
| 3459 | // StrictCleartextDetected / InterfaceDnsServersAdded |
| 3460 | // InterfaceClassActivity / QuotaLimitReached / InterfaceAddressRemoved |
| 3461 | |
| 3462 | { |
| 3463 | std::unique_lock lock(cvMutex); |
| 3464 | |
| 3465 | // Re-init test Tun, and we expect that we will get some unsol events. |
| 3466 | // Use the test Tun device name to verify if we receive its unsol events. |
| 3467 | sTun.destroy(); |
| 3468 | // Use predefined name |
| 3469 | sTun.init(newTunName); |
| 3470 | |
| 3471 | EXPECT_EQ(std::cv_status::no_timeout, cv.wait_for(lock, std::chrono::seconds(2))); |
| 3472 | } |
| 3473 | |
| 3474 | // bit mask 1101101000 |
| 3475 | // Test only covers below events currently |
| 3476 | const uint32_t kExpectedEvents = InterfaceAddressUpdated | InterfaceAdded | InterfaceRemoved | |
| 3477 | InterfaceLinkStatusChanged | RouteChanged; |
| 3478 | EXPECT_EQ(kExpectedEvents, testUnsolService->getReceived()); |
Luke Huang | cfd04b2 | 2019-03-18 15:53:21 +0800 | [diff] [blame] | 3479 | |
| 3480 | // Re-init sTun to clear predefined name |
| 3481 | sTun.destroy(); |
| 3482 | sTun.init(); |
| 3483 | } |
| 3484 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3485 | TEST_F(NetdBinderTest, NDC) { |
Luke Huang | cfd04b2 | 2019-03-18 15:53:21 +0800 | [diff] [blame] | 3486 | struct Command { |
| 3487 | const std::string cmdString; |
| 3488 | const std::string expectedResult; |
| 3489 | }; |
| 3490 | |
| 3491 | // clang-format off |
| 3492 | // Do not change the commands order |
| 3493 | const Command networkCmds[] = { |
| 3494 | {StringPrintf("ndc network create %d", TEST_NETID1), |
| 3495 | "200 0 success"}, |
| 3496 | {StringPrintf("ndc network interface add %d %s", TEST_NETID1, sTun.name().c_str()), |
| 3497 | "200 0 success"}, |
| 3498 | {StringPrintf("ndc network interface remove %d %s", TEST_NETID1, sTun.name().c_str()), |
| 3499 | "200 0 success"}, |
| 3500 | {StringPrintf("ndc network interface add %d %s", TEST_NETID2, sTun.name().c_str()), |
| 3501 | "400 0 addInterfaceToNetwork() failed (Machine is not on the network)"}, |
| 3502 | {StringPrintf("ndc network destroy %d", TEST_NETID1), |
| 3503 | "200 0 success"}, |
| 3504 | }; |
| 3505 | |
| 3506 | const std::vector<Command> ipfwdCmds = { |
| 3507 | {"ndc ipfwd enable " + sTun.name(), |
| 3508 | "200 0 ipfwd operation succeeded"}, |
| 3509 | {"ndc ipfwd disable " + sTun.name(), |
| 3510 | "200 0 ipfwd operation succeeded"}, |
| 3511 | {"ndc ipfwd add lo2 lo3", |
| 3512 | "400 0 ipfwd operation failed (No such process)"}, |
| 3513 | {"ndc ipfwd add " + sTun.name() + " " + sTun2.name(), |
| 3514 | "200 0 ipfwd operation succeeded"}, |
| 3515 | {"ndc ipfwd remove " + sTun.name() + " " + sTun2.name(), |
| 3516 | "200 0 ipfwd operation succeeded"}, |
| 3517 | }; |
| 3518 | |
| 3519 | static const struct { |
| 3520 | const char* ipVersion; |
| 3521 | const char* testDest; |
| 3522 | const char* testNextHop; |
| 3523 | const bool expectSuccess; |
| 3524 | const std::string expectedResult; |
| 3525 | } kTestData[] = { |
| 3526 | {IP_RULE_V4, "0.0.0.0/0", "", true, |
| 3527 | "200 0 success"}, |
| 3528 | {IP_RULE_V4, "10.251.0.0/16", "", true, |
| 3529 | "200 0 success"}, |
| 3530 | {IP_RULE_V4, "10.251.0.0/16", "fe80::/64", false, |
| 3531 | "400 0 addRoute() failed (Invalid argument)",}, |
| 3532 | {IP_RULE_V6, "::/0", "", true, |
| 3533 | "200 0 success"}, |
| 3534 | {IP_RULE_V6, "2001:db8:cafe::/64", "", true, |
| 3535 | "200 0 success"}, |
| 3536 | {IP_RULE_V6, "fe80::/64", "0.0.0.0", false, |
| 3537 | "400 0 addRoute() failed (Invalid argument)"}, |
| 3538 | }; |
| 3539 | // clang-format on |
| 3540 | |
| 3541 | for (const auto& cmd : networkCmds) { |
| 3542 | const std::vector<std::string> result = runCommand(cmd.cmdString); |
| 3543 | SCOPED_TRACE(cmd.cmdString); |
| 3544 | EXPECT_EQ(result.size(), 1U); |
| 3545 | EXPECT_EQ(cmd.expectedResult, Trim(result[0])); |
| 3546 | } |
| 3547 | |
| 3548 | for (const auto& cmd : ipfwdCmds) { |
| 3549 | const std::vector<std::string> result = runCommand(cmd.cmdString); |
| 3550 | SCOPED_TRACE(cmd.cmdString); |
| 3551 | EXPECT_EQ(result.size(), 1U); |
| 3552 | EXPECT_EQ(cmd.expectedResult, Trim(result[0])); |
| 3553 | } |
| 3554 | |
| 3555 | // Add test physical network |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3556 | const auto& config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 3557 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3558 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Luke Huang | cfd04b2 | 2019-03-18 15:53:21 +0800 | [diff] [blame] | 3559 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 3560 | |
| 3561 | for (const auto& td : kTestData) { |
| 3562 | const std::string routeAddCmd = |
| 3563 | StringPrintf("ndc network route add %d %s %s %s", TEST_NETID1, sTun.name().c_str(), |
| 3564 | td.testDest, td.testNextHop); |
| 3565 | const std::string routeRemoveCmd = |
| 3566 | StringPrintf("ndc network route remove %d %s %s %s", TEST_NETID1, |
| 3567 | sTun.name().c_str(), td.testDest, td.testNextHop); |
| 3568 | std::vector<std::string> result = runCommand(routeAddCmd); |
| 3569 | SCOPED_TRACE(routeAddCmd); |
| 3570 | EXPECT_EQ(result.size(), 1U); |
| 3571 | EXPECT_EQ(td.expectedResult, Trim(result[0])); |
| 3572 | if (td.expectSuccess) { |
| 3573 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 3574 | sTun.name().c_str()); |
| 3575 | result = runCommand(routeRemoveCmd); |
| 3576 | EXPECT_EQ(result.size(), 1U); |
| 3577 | EXPECT_EQ(td.expectedResult, Trim(result[0])); |
| 3578 | expectNetworkRouteDoesNotExist(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 3579 | sTun.name().c_str()); |
| 3580 | } |
| 3581 | } |
| 3582 | // Remove test physical network |
| 3583 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 3584 | } |
Luke Huang | 0e5e69d | 2019-03-06 15:42:38 +0800 | [diff] [blame] | 3585 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3586 | TEST_F(NetdBinderTest, OemNetdRelated) { |
Luke Huang | 0e5e69d | 2019-03-06 15:42:38 +0800 | [diff] [blame] | 3587 | sp<IBinder> binder; |
| 3588 | binder::Status status = mNetd->getOemNetd(&binder); |
| 3589 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3590 | sp<com::android::internal::net::IOemNetd> oemNetd; |
| 3591 | if (binder != nullptr) { |
| 3592 | oemNetd = android::interface_cast<com::android::internal::net::IOemNetd>(binder); |
| 3593 | } |
Luke Huang | 2ff8b34 | 2019-04-30 15:33:33 +0800 | [diff] [blame] | 3594 | ASSERT_NE(nullptr, oemNetd.get()); |
Luke Huang | 0e5e69d | 2019-03-06 15:42:38 +0800 | [diff] [blame] | 3595 | |
| 3596 | TimedOperation t("OemNetd isAlive RPC"); |
| 3597 | bool isAlive = false; |
| 3598 | oemNetd->isAlive(&isAlive); |
| 3599 | ASSERT_TRUE(isAlive); |
Luke Huang | 2ff8b34 | 2019-04-30 15:33:33 +0800 | [diff] [blame] | 3600 | |
| 3601 | class TestOemUnsolListener |
| 3602 | : public com::android::internal::net::BnOemNetdUnsolicitedEventListener { |
| 3603 | public: |
| 3604 | android::binder::Status onRegistered() override { |
| 3605 | std::lock_guard lock(mCvMutex); |
| 3606 | mCv.notify_one(); |
| 3607 | return android::binder::Status::ok(); |
| 3608 | } |
| 3609 | std::condition_variable& getCv() { return mCv; } |
| 3610 | std::mutex& getCvMutex() { return mCvMutex; } |
| 3611 | |
| 3612 | private: |
| 3613 | std::mutex mCvMutex; |
| 3614 | std::condition_variable mCv; |
| 3615 | }; |
| 3616 | |
| 3617 | // Start the Binder thread pool. |
| 3618 | android::ProcessState::self()->startThreadPool(); |
| 3619 | |
| 3620 | android::sp<TestOemUnsolListener> testListener = new TestOemUnsolListener(); |
| 3621 | |
| 3622 | auto& cv = testListener->getCv(); |
| 3623 | auto& cvMutex = testListener->getCvMutex(); |
| 3624 | |
| 3625 | { |
| 3626 | std::unique_lock lock(cvMutex); |
| 3627 | |
| 3628 | status = oemNetd->registerOemUnsolicitedEventListener( |
| 3629 | ::android::interface_cast< |
| 3630 | com::android::internal::net::IOemNetdUnsolicitedEventListener>( |
| 3631 | testListener)); |
| 3632 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3633 | |
| 3634 | // Wait for receiving expected events. |
| 3635 | EXPECT_EQ(std::cv_status::no_timeout, cv.wait_for(lock, std::chrono::seconds(2))); |
| 3636 | } |
Luke Huang | 0e5e69d | 2019-03-06 15:42:38 +0800 | [diff] [blame] | 3637 | } |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3638 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3639 | void NetdBinderTest::createVpnNetworkWithUid(bool secure, uid_t uid, int vpnNetId, |
Chiachang | 2271c12 | 2022-05-04 07:45:37 +0000 | [diff] [blame] | 3640 | int fallthroughNetId, int nonDefaultNetId) { |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3641 | // Re-init sTun* to ensure route rule exists. |
| 3642 | sTun.destroy(); |
| 3643 | sTun.init(); |
| 3644 | sTun2.destroy(); |
| 3645 | sTun2.init(); |
Chiachang | 2271c12 | 2022-05-04 07:45:37 +0000 | [diff] [blame] | 3646 | sTun3.destroy(); |
| 3647 | sTun3.init(); |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3648 | |
| 3649 | // Create physical network with fallthroughNetId but not set it as default network |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3650 | auto config = makeNativeNetworkConfig(fallthroughNetId, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 3651 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3652 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3653 | EXPECT_TRUE(mNetd->networkAddInterface(fallthroughNetId, sTun.name()).isOk()); |
chiachangwang | 0d5ae98 | 2022-05-17 05:16:16 +0000 | [diff] [blame] | 3654 | // Create another physical network in order to test VPN behaviour with multiple networks |
| 3655 | // connected, of which one may be the default. |
Chiachang | 2271c12 | 2022-05-04 07:45:37 +0000 | [diff] [blame] | 3656 | auto nonDefaultNetworkConfig = makeNativeNetworkConfig( |
| 3657 | nonDefaultNetId, NativeNetworkType::PHYSICAL, INetd::PERMISSION_NONE, false, false); |
| 3658 | EXPECT_TRUE(mNetd->networkCreate(nonDefaultNetworkConfig).isOk()); |
| 3659 | EXPECT_TRUE(mNetd->networkAddInterface(nonDefaultNetId, sTun3.name()).isOk()); |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3660 | |
| 3661 | // Create VPN with vpnNetId |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3662 | config.netId = vpnNetId; |
| 3663 | config.networkType = NativeNetworkType::VIRTUAL; |
| 3664 | config.secure = secure; |
| 3665 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3666 | |
| 3667 | // Add uid to VPN |
| 3668 | EXPECT_TRUE(mNetd->networkAddUidRanges(vpnNetId, {makeUidRangeParcel(uid, uid)}).isOk()); |
| 3669 | EXPECT_TRUE(mNetd->networkAddInterface(vpnNetId, sTun2.name()).isOk()); |
| 3670 | |
| 3671 | // Add default route to fallthroughNetwork |
| 3672 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID1, sTun.name(), "::/0", "").isOk()); |
| 3673 | // Add limited route |
| 3674 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID2, sTun2.name(), "2001:db8::/32", "").isOk()); |
Chiachang | f9e81ac | 2022-05-12 05:21:20 +0000 | [diff] [blame] | 3675 | |
| 3676 | // Also add default route to non-default network for per app default use. |
| 3677 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID3, sTun3.name(), "::/0", "").isOk()); |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3678 | } |
| 3679 | |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 3680 | void NetdBinderTest::createAndSetDefaultNetwork(int netId, const std::string& interface, |
| 3681 | int permission) { |
| 3682 | // backup current default network. |
| 3683 | ASSERT_TRUE(mNetd->networkGetDefault(&mStoredDefaultNetwork).isOk()); |
| 3684 | |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3685 | const auto& config = |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 3686 | makeNativeNetworkConfig(netId, NativeNetworkType::PHYSICAL, permission, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3687 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 3688 | EXPECT_TRUE(mNetd->networkAddInterface(netId, interface).isOk()); |
| 3689 | EXPECT_TRUE(mNetd->networkSetDefault(netId).isOk()); |
| 3690 | } |
| 3691 | |
| 3692 | void NetdBinderTest::createPhysicalNetwork(int netId, const std::string& interface, |
| 3693 | int permission) { |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3694 | const auto& config = |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 3695 | makeNativeNetworkConfig(netId, NativeNetworkType::PHYSICAL, permission, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3696 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 3697 | EXPECT_TRUE(mNetd->networkAddInterface(netId, interface).isOk()); |
| 3698 | } |
| 3699 | |
| 3700 | // 1. Create a physical network on sTun, and set it as the system default network. |
| 3701 | // 2. Create another physical network on sTun2. |
| 3702 | void NetdBinderTest::createDefaultAndOtherPhysicalNetwork(int defaultNetId, int otherNetId) { |
| 3703 | createAndSetDefaultNetwork(defaultNetId, sTun.name()); |
| 3704 | EXPECT_TRUE(mNetd->networkAddRoute(defaultNetId, sTun.name(), "::/0", "").isOk()); |
| 3705 | |
| 3706 | createPhysicalNetwork(otherNetId, sTun2.name()); |
| 3707 | EXPECT_TRUE(mNetd->networkAddRoute(otherNetId, sTun2.name(), "::/0", "").isOk()); |
| 3708 | } |
| 3709 | |
| 3710 | // 1. Create a system default network and a physical network. |
| 3711 | // 2. Create a VPN on sTun3. |
| 3712 | void NetdBinderTest::createVpnAndOtherPhysicalNetwork(int systemDefaultNetId, int otherNetId, |
| 3713 | int vpnNetId, bool secure) { |
| 3714 | createDefaultAndOtherPhysicalNetwork(systemDefaultNetId, otherNetId); |
| 3715 | |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3716 | auto config = makeNativeNetworkConfig(vpnNetId, NativeNetworkType::VIRTUAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 3717 | INetd::PERMISSION_NONE, secure, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3718 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 3719 | EXPECT_TRUE(mNetd->networkAddInterface(vpnNetId, sTun3.name()).isOk()); |
| 3720 | EXPECT_TRUE(mNetd->networkAddRoute(vpnNetId, sTun3.name(), "2001:db8::/32", "").isOk()); |
| 3721 | } |
| 3722 | |
| 3723 | // 1. Create system default network, a physical network (for per-app default), and a VPN. |
| 3724 | // 2. Add per-app uid ranges and VPN ranges. |
| 3725 | void NetdBinderTest::createVpnAndAppDefaultNetworkWithUid( |
| 3726 | int systemDefaultNetId, int appDefaultNetId, int vpnNetId, bool secure, |
| 3727 | std::vector<UidRangeParcel>&& appDefaultUidRanges, |
| 3728 | std::vector<UidRangeParcel>&& vpnUidRanges) { |
| 3729 | createVpnAndOtherPhysicalNetwork(systemDefaultNetId, appDefaultNetId, vpnNetId, secure); |
| 3730 | // add per-app uid ranges. |
| 3731 | EXPECT_TRUE(mNetd->networkAddUidRanges(appDefaultNetId, appDefaultUidRanges).isOk()); |
| 3732 | // add VPN uid ranges. |
| 3733 | EXPECT_TRUE(mNetd->networkAddUidRanges(vpnNetId, vpnUidRanges).isOk()); |
| 3734 | } |
| 3735 | |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3736 | namespace { |
| 3737 | |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 3738 | void checkDataReceived(int udpSocket, int tunFd, sockaddr* dstAddr, int addrLen) { |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3739 | char buf[4096] = {}; |
| 3740 | // Clear tunFd's queue before write something because there might be some |
| 3741 | // arbitrary packets in the queue. (e.g. ICMPv6 packet) |
| 3742 | clearQueue(tunFd); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 3743 | EXPECT_EQ(4, sendto(udpSocket, "foo", sizeof("foo"), 0, dstAddr, addrLen)); |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3744 | // TODO: extract header and verify data |
| 3745 | EXPECT_GT(read(tunFd, buf, sizeof(buf)), 0); |
| 3746 | } |
| 3747 | |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 3748 | bool sendPacketFromUid(uid_t uid, IPSockAddr& dstAddr, Fwmark* fwmark, int tunFd, |
| 3749 | bool doConnect = true) { |
| 3750 | int family = dstAddr.family(); |
| 3751 | ScopedUidChange scopedUidChange(uid); |
| 3752 | unique_fd testSocket(socket(family, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 3753 | |
| 3754 | if (testSocket < 0) return false; |
| 3755 | const sockaddr_storage dst = IPSockAddr(dstAddr.ip(), dstAddr.port()); |
| 3756 | if (doConnect && connect(testSocket, (sockaddr*)&dst, sizeof(dst)) == -1) return false; |
| 3757 | |
| 3758 | socklen_t fwmarkLen = sizeof(fwmark->intValue); |
| 3759 | EXPECT_NE(-1, getsockopt(testSocket, SOL_SOCKET, SO_MARK, &(fwmark->intValue), &fwmarkLen)); |
| 3760 | |
| 3761 | int addr_len = (family == AF_INET) ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN; |
| 3762 | char addr[addr_len]; |
| 3763 | inet_ntop(family, &dstAddr, addr, addr_len); |
| 3764 | SCOPED_TRACE(StringPrintf("sendPacket, addr: %s, uid: %u, doConnect: %s", addr, uid, |
| 3765 | doConnect ? "true" : "false")); |
| 3766 | if (doConnect) { |
| 3767 | checkDataReceived(testSocket, tunFd, nullptr, 0); |
| 3768 | } else { |
| 3769 | checkDataReceived(testSocket, tunFd, (sockaddr*)&dst, sizeof(dst)); |
| 3770 | } |
| 3771 | |
| 3772 | return true; |
| 3773 | } |
| 3774 | |
| 3775 | bool sendIPv4PacketFromUid(uid_t uid, const in_addr& dstAddr, Fwmark* fwmark, int tunFd, |
| 3776 | bool doConnect = true) { |
| 3777 | const sockaddr_in dst = {.sin_family = AF_INET, .sin_port = 42, .sin_addr = dstAddr}; |
| 3778 | IPSockAddr addr = IPSockAddr(dst); |
| 3779 | |
| 3780 | return sendPacketFromUid(uid, addr, fwmark, tunFd, doConnect); |
| 3781 | } |
| 3782 | |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 3783 | bool sendIPv6PacketFromUid(uid_t uid, const in6_addr& dstAddr, Fwmark* fwmark, int tunFd, |
| 3784 | bool doConnect = true) { |
Nick Desaulniers | 6b35750 | 2019-10-11 09:26:44 -0700 | [diff] [blame] | 3785 | const sockaddr_in6 dst6 = { |
| 3786 | .sin6_family = AF_INET6, |
| 3787 | .sin6_port = 42, |
| 3788 | .sin6_addr = dstAddr, |
| 3789 | }; |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 3790 | IPSockAddr addr = IPSockAddr(dst6); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 3791 | |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 3792 | return sendPacketFromUid(uid, addr, fwmark, tunFd, doConnect); |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3793 | } |
| 3794 | |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 3795 | // Send an IPv6 packet from the uid. Expect to fail and get specified errno. |
| 3796 | bool sendIPv6PacketFromUidFail(uid_t uid, const in6_addr& dstAddr, Fwmark* fwmark, bool doConnect, |
| 3797 | int expectedErr) { |
| 3798 | ScopedUidChange scopedUidChange(uid); |
| 3799 | unique_fd s(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 3800 | if (s < 0) return false; |
| 3801 | |
| 3802 | const sockaddr_in6 dst6 = { |
| 3803 | .sin6_family = AF_INET6, |
| 3804 | .sin6_port = 42, |
| 3805 | .sin6_addr = dstAddr, |
| 3806 | }; |
| 3807 | if (doConnect) { |
| 3808 | if (connect(s, (sockaddr*)&dst6, sizeof(dst6)) == 0) return false; |
| 3809 | if (errno != expectedErr) return false; |
| 3810 | } |
| 3811 | |
| 3812 | socklen_t fwmarkLen = sizeof(fwmark->intValue); |
| 3813 | EXPECT_NE(-1, getsockopt(s, SOL_SOCKET, SO_MARK, &(fwmark->intValue), &fwmarkLen)); |
| 3814 | |
| 3815 | char addr[INET6_ADDRSTRLEN]; |
| 3816 | inet_ntop(AF_INET6, &dstAddr, addr, INET6_ADDRSTRLEN); |
| 3817 | SCOPED_TRACE(StringPrintf("sendIPv6PacketFail, addr: %s, uid: %u, doConnect: %s", addr, uid, |
| 3818 | doConnect ? "true" : "false")); |
| 3819 | if (!doConnect) { |
| 3820 | if (sendto(s, "foo", sizeof("foo"), 0, (sockaddr*)&dst6, sizeof(dst6)) == 0) return false; |
| 3821 | if (errno != expectedErr) return false; |
| 3822 | } |
| 3823 | return true; |
| 3824 | } |
| 3825 | |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3826 | void expectVpnFallthroughRuleExists(const std::string& ifName, int vpnNetId) { |
| 3827 | std::string vpnFallthroughRule = |
| 3828 | StringPrintf("%d:\tfrom all fwmark 0x%x/0xffff lookup %s", |
| 3829 | RULE_PRIORITY_VPN_FALLTHROUGH, vpnNetId, ifName.c_str()); |
| 3830 | for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { |
| 3831 | EXPECT_TRUE(ipRuleExists(ipVersion, vpnFallthroughRule)); |
| 3832 | } |
| 3833 | } |
| 3834 | |
| 3835 | void expectVpnFallthroughWorks(android::net::INetd* netdService, bool bypassable, uid_t uid, |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 3836 | uid_t uidNotInVpn, const TunInterface& fallthroughNetwork, |
Chiachang | f9e81ac | 2022-05-12 05:21:20 +0000 | [diff] [blame] | 3837 | const TunInterface& vpnNetwork, const TunInterface& otherNetwork, |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 3838 | int vpnNetId = TEST_NETID2, int fallthroughNetId = TEST_NETID1, |
| 3839 | int otherNetId = TEST_NETID3) { |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3840 | // Set default network to NETID_UNSET |
| 3841 | EXPECT_TRUE(netdService->networkSetDefault(NETID_UNSET).isOk()); |
| 3842 | |
| 3843 | // insideVpnAddr based on the route we added in createVpnNetworkWithUid |
| 3844 | in6_addr insideVpnAddr = { |
| 3845 | {// 2001:db8:cafe::1 |
| 3846 | .u6_addr8 = {0x20, 0x01, 0x0d, 0xb8, 0xca, 0xfe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}}; |
| 3847 | // outsideVpnAddr will hit the route in the fallthrough network route table |
| 3848 | // because we added default route in createVpnNetworkWithUid |
| 3849 | in6_addr outsideVpnAddr = { |
| 3850 | {// 2607:f0d0:1002::4 |
| 3851 | .u6_addr8 = {0x26, 0x07, 0xf0, 0xd0, 0x10, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}}}; |
| 3852 | |
| 3853 | int fallthroughFd = fallthroughNetwork.getFdForTesting(); |
| 3854 | int vpnFd = vpnNetwork.getFdForTesting(); |
| 3855 | // Expect all connections to fail because UID 0 is not routed to the VPN and there is no |
| 3856 | // default network. |
| 3857 | Fwmark fwmark; |
| 3858 | EXPECT_FALSE(sendIPv6PacketFromUid(0, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3859 | EXPECT_FALSE(sendIPv6PacketFromUid(0, insideVpnAddr, &fwmark, fallthroughFd)); |
| 3860 | |
| 3861 | // Set default network |
| 3862 | EXPECT_TRUE(netdService->networkSetDefault(fallthroughNetId).isOk()); |
| 3863 | |
| 3864 | // Connections go on the default network because UID 0 is not subject to the VPN. |
| 3865 | EXPECT_TRUE(sendIPv6PacketFromUid(0, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3866 | EXPECT_EQ(fallthroughNetId | 0xC0000, static_cast<int>(fwmark.intValue)); |
| 3867 | EXPECT_TRUE(sendIPv6PacketFromUid(0, insideVpnAddr, &fwmark, fallthroughFd)); |
| 3868 | EXPECT_EQ(fallthroughNetId | 0xC0000, static_cast<int>(fwmark.intValue)); |
| 3869 | |
| 3870 | // Check if fallthrough rule exists |
| 3871 | expectVpnFallthroughRuleExists(fallthroughNetwork.name(), vpnNetId); |
| 3872 | |
Chiachang | 2271c12 | 2022-05-04 07:45:37 +0000 | [diff] [blame] | 3873 | // Check if local exclusion rule exists for default network |
Chiachang Wang | e39cc75 | 2022-03-01 17:35:31 +0800 | [diff] [blame] | 3874 | expectVpnLocalExclusionRuleExists(fallthroughNetwork.name(), true); |
Chiachang | 2271c12 | 2022-05-04 07:45:37 +0000 | [diff] [blame] | 3875 | // No local exclusion rule for non-default network |
Chiachang | f9e81ac | 2022-05-12 05:21:20 +0000 | [diff] [blame] | 3876 | expectVpnLocalExclusionRuleExists(otherNetwork.name(), false); |
Chiachang Wang | e7bf5f5 | 2022-01-17 11:37:39 +0800 | [diff] [blame] | 3877 | |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3878 | // Expect fallthrough to default network |
| 3879 | // The fwmark differs depending on whether the VPN is bypassable or not. |
| 3880 | EXPECT_TRUE(sendIPv6PacketFromUid(uid, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3881 | EXPECT_EQ(bypassable ? vpnNetId : fallthroughNetId, static_cast<int>(fwmark.intValue)); |
| 3882 | |
| 3883 | // Expect connect success, packet will be sent to vpnFd. |
| 3884 | EXPECT_TRUE(sendIPv6PacketFromUid(uid, insideVpnAddr, &fwmark, vpnFd)); |
| 3885 | EXPECT_EQ(bypassable ? vpnNetId : fallthroughNetId, static_cast<int>(fwmark.intValue)); |
| 3886 | |
| 3887 | // Explicitly select vpn network |
| 3888 | setNetworkForProcess(vpnNetId); |
| 3889 | |
| 3890 | // Expect fallthrough to default network |
| 3891 | EXPECT_TRUE(sendIPv6PacketFromUid(0, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3892 | // Expect the mark contains all the bit because we've selected network. |
| 3893 | EXPECT_EQ(vpnNetId | 0xF0000, static_cast<int>(fwmark.intValue)); |
| 3894 | |
| 3895 | // Expect connect success, packet will be sent to vpnFd. |
| 3896 | EXPECT_TRUE(sendIPv6PacketFromUid(0, insideVpnAddr, &fwmark, vpnFd)); |
| 3897 | // Expect the mark contains all the bit because we've selected network. |
| 3898 | EXPECT_EQ(vpnNetId | 0xF0000, static_cast<int>(fwmark.intValue)); |
| 3899 | |
| 3900 | // Explicitly select fallthrough network |
| 3901 | setNetworkForProcess(fallthroughNetId); |
| 3902 | |
| 3903 | // The mark is set to fallthrough network because we've selected it. |
| 3904 | EXPECT_TRUE(sendIPv6PacketFromUid(0, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3905 | EXPECT_TRUE(sendIPv6PacketFromUid(0, insideVpnAddr, &fwmark, fallthroughFd)); |
| 3906 | |
| 3907 | // If vpn is BypassableVPN, connections can also go on the fallthrough network under vpn uid. |
| 3908 | if (bypassable) { |
| 3909 | EXPECT_TRUE(sendIPv6PacketFromUid(uid, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3910 | EXPECT_TRUE(sendIPv6PacketFromUid(uid, insideVpnAddr, &fwmark, fallthroughFd)); |
| 3911 | } else { |
| 3912 | // If not, no permission to bypass vpn. |
| 3913 | EXPECT_FALSE(sendIPv6PacketFromUid(uid, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3914 | EXPECT_FALSE(sendIPv6PacketFromUid(uid, insideVpnAddr, &fwmark, fallthroughFd)); |
| 3915 | } |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 3916 | |
| 3917 | // Add per-app uid ranges. |
| 3918 | EXPECT_TRUE(netdService |
| 3919 | ->networkAddUidRanges(otherNetId, |
| 3920 | {makeUidRangeParcel(uidNotInVpn, uidNotInVpn)}) |
| 3921 | .isOk()); |
| 3922 | |
| 3923 | int appDefaultFd = otherNetwork.getFdForTesting(); |
| 3924 | |
| 3925 | // UID is not inside the VPN range, so it won't go to vpn network. |
| 3926 | // It won't fall into per app local rule because it's explicitly selected. |
| 3927 | EXPECT_TRUE(sendIPv6PacketFromUid(uidNotInVpn, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3928 | EXPECT_TRUE(sendIPv6PacketFromUid(uidNotInVpn, insideVpnAddr, &fwmark, fallthroughFd)); |
| 3929 | |
| 3930 | // Reset explicitly selection. |
| 3931 | setNetworkForProcess(NETID_UNSET); |
| 3932 | // Connections can go to app default network. |
| 3933 | EXPECT_TRUE(sendIPv6PacketFromUid(uidNotInVpn, insideVpnAddr, &fwmark, appDefaultFd)); |
| 3934 | EXPECT_TRUE(sendIPv6PacketFromUid(uidNotInVpn, outsideVpnAddr, &fwmark, appDefaultFd)); |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3935 | } |
| 3936 | |
| 3937 | } // namespace |
| 3938 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3939 | TEST_F(NetdBinderTest, SecureVPNFallthrough) { |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3940 | createVpnNetworkWithUid(true /* secure */, TEST_UID1); |
| 3941 | // Get current default network NetId |
| 3942 | ASSERT_TRUE(mNetd->networkGetDefault(&mStoredDefaultNetwork).isOk()); |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 3943 | expectVpnFallthroughWorks(mNetd.get(), false /* bypassable */, TEST_UID1, TEST_UID2, sTun, |
| 3944 | sTun2, sTun3); |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3945 | } |
| 3946 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3947 | TEST_F(NetdBinderTest, BypassableVPNFallthrough) { |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3948 | createVpnNetworkWithUid(false /* secure */, TEST_UID1); |
| 3949 | // Get current default network NetId |
| 3950 | ASSERT_TRUE(mNetd->networkGetDefault(&mStoredDefaultNetwork).isOk()); |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 3951 | expectVpnFallthroughWorks(mNetd.get(), true /* bypassable */, TEST_UID1, TEST_UID2, sTun, sTun2, |
| 3952 | sTun3); |
Bernie Innocenti | 80ffd0f | 2019-06-05 15:27:46 +0900 | [diff] [blame] | 3953 | } |
Chiachang Wang | 00fc62f | 2019-12-04 20:38:26 +0800 | [diff] [blame] | 3954 | |
| 3955 | namespace { |
| 3956 | |
| 3957 | int32_t createIpv6SocketAndCheckMark(int type, const in6_addr& dstAddr) { |
| 3958 | const sockaddr_in6 dst6 = { |
| 3959 | .sin6_family = AF_INET6, |
| 3960 | .sin6_port = 1234, |
| 3961 | .sin6_addr = dstAddr, |
| 3962 | }; |
| 3963 | // create non-blocking socket. |
| 3964 | int sockFd = socket(AF_INET6, type | SOCK_NONBLOCK, 0); |
| 3965 | EXPECT_NE(-1, sockFd); |
| 3966 | EXPECT_EQ((type == SOCK_STREAM) ? -1 : 0, connect(sockFd, (sockaddr*)&dst6, sizeof(dst6))); |
| 3967 | |
| 3968 | // Get socket fwmark. |
| 3969 | Fwmark fwmark; |
| 3970 | socklen_t fwmarkLen = sizeof(fwmark.intValue); |
| 3971 | EXPECT_EQ(0, getsockopt(sockFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen)); |
| 3972 | EXPECT_EQ(0, close(sockFd)); |
| 3973 | return fwmark.intValue; |
| 3974 | } |
| 3975 | |
| 3976 | } // namespace |
| 3977 | |
Lorenzo Colitti | 76f4bdb | 2020-06-03 21:50:41 +0900 | [diff] [blame] | 3978 | TEST_F(NetdBinderTest, GetFwmarkForNetwork) { |
Maciej Żenczykowski | 084e0ef | 2020-05-20 02:01:23 -0700 | [diff] [blame] | 3979 | // Save current default network. |
| 3980 | ASSERT_TRUE(mNetd->networkGetDefault(&mStoredDefaultNetwork).isOk()); |
| 3981 | |
Chiachang Wang | 00fc62f | 2019-12-04 20:38:26 +0800 | [diff] [blame] | 3982 | // Add test physical network 1 and set as default network. |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3983 | auto config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 3984 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3985 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Chiachang Wang | 00fc62f | 2019-12-04 20:38:26 +0800 | [diff] [blame] | 3986 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 3987 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID1, sTun.name(), "2001:db8::/32", "").isOk()); |
| 3988 | EXPECT_TRUE(mNetd->networkSetDefault(TEST_NETID1).isOk()); |
| 3989 | // Add test physical network 2 |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 3990 | config.netId = TEST_NETID2; |
| 3991 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Chiachang Wang | 00fc62f | 2019-12-04 20:38:26 +0800 | [diff] [blame] | 3992 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID2, sTun2.name()).isOk()); |
| 3993 | |
| 3994 | // Get fwmark for network 1. |
| 3995 | MarkMaskParcel maskMarkNet1; |
| 3996 | ASSERT_TRUE(mNetd->getFwmarkForNetwork(TEST_NETID1, &maskMarkNet1).isOk()); |
| 3997 | |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 3998 | uint32_t fwmarkTcp = createIpv6SocketAndCheckMark(SOCK_STREAM, V6_ADDR); |
| 3999 | uint32_t fwmarkUdp = createIpv6SocketAndCheckMark(SOCK_DGRAM, V6_ADDR); |
Chiachang Wang | 00fc62f | 2019-12-04 20:38:26 +0800 | [diff] [blame] | 4000 | EXPECT_EQ(maskMarkNet1.mark, static_cast<int>(fwmarkTcp & maskMarkNet1.mask)); |
| 4001 | EXPECT_EQ(maskMarkNet1.mark, static_cast<int>(fwmarkUdp & maskMarkNet1.mask)); |
| 4002 | |
| 4003 | // Get fwmark for network 2. |
| 4004 | MarkMaskParcel maskMarkNet2; |
| 4005 | ASSERT_TRUE(mNetd->getFwmarkForNetwork(TEST_NETID2, &maskMarkNet2).isOk()); |
| 4006 | EXPECT_NE(maskMarkNet2.mark, static_cast<int>(fwmarkTcp & maskMarkNet2.mask)); |
| 4007 | EXPECT_NE(maskMarkNet2.mark, static_cast<int>(fwmarkUdp & maskMarkNet2.mask)); |
| 4008 | |
| 4009 | // Remove test physical network. |
| 4010 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID2).isOk()); |
| 4011 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 4012 | } |
Hungming Chen | 9257104 | 2020-02-20 17:01:31 +0800 | [diff] [blame] | 4013 | |
Lorenzo Colitti | 23554ba | 2020-11-05 01:43:46 +0900 | [diff] [blame] | 4014 | TEST_F(NetdBinderTest, TestServiceDump) { |
| 4015 | sp<IBinder> binder = INetd::asBinder(mNetd); |
| 4016 | ASSERT_NE(nullptr, binder); |
| 4017 | |
| 4018 | struct TestData { |
| 4019 | // Expected contents of the dump command. |
| 4020 | const std::string output; |
| 4021 | // A regex that might be helpful in matching relevant lines in the output. |
| 4022 | // Used to make it easier to add test cases for this code. |
| 4023 | const std::string hintRegex; |
| 4024 | }; |
| 4025 | std::vector<TestData> testData; |
| 4026 | |
| 4027 | // Send some IPCs and for each one add an element to testData telling us what to expect. |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 4028 | const auto& config = makeNativeNetworkConfig(TEST_DUMP_NETID, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 4029 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 4030 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Lorenzo Colitti | 23554ba | 2020-11-05 01:43:46 +0900 | [diff] [blame] | 4031 | testData.push_back( |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 4032 | {"networkCreate(NativeNetworkConfig{netId: 65123, networkType: PHYSICAL, " |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 4033 | "permission: 0, secure: false, vpnType: PLATFORM, excludeLocalRoutes: false})", |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 4034 | "networkCreate.*65123"}); |
| 4035 | |
| 4036 | EXPECT_EQ(EEXIST, mNetd->networkCreate(config).serviceSpecificErrorCode()); |
| 4037 | testData.push_back( |
| 4038 | {"networkCreate(NativeNetworkConfig{netId: 65123, networkType: PHYSICAL, " |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 4039 | "permission: 0, secure: false, vpnType: PLATFORM, excludeLocalRoutes: false}) " |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 4040 | "-> ServiceSpecificException(17, \"File exists\")", |
| 4041 | "networkCreate.*65123.*17"}); |
Lorenzo Colitti | 23554ba | 2020-11-05 01:43:46 +0900 | [diff] [blame] | 4042 | |
| 4043 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_DUMP_NETID, sTun.name()).isOk()); |
Jooyung Han | f242258 | 2020-11-04 14:21:46 +0900 | [diff] [blame] | 4044 | testData.push_back({StringPrintf("networkAddInterface(65123, %s)", sTun.name().c_str()), |
Lorenzo Colitti | 23554ba | 2020-11-05 01:43:46 +0900 | [diff] [blame] | 4045 | StringPrintf("networkAddInterface.*65123.*%s", sTun.name().c_str())}); |
| 4046 | |
| 4047 | android::net::RouteInfoParcel parcel; |
| 4048 | parcel.ifName = sTun.name(); |
| 4049 | parcel.destination = "2001:db8:dead:beef::/64"; |
| 4050 | parcel.nextHop = "fe80::dead:beef"; |
| 4051 | parcel.mtu = 1234; |
| 4052 | EXPECT_TRUE(mNetd->networkAddRouteParcel(TEST_DUMP_NETID, parcel).isOk()); |
| 4053 | testData.push_back( |
Jooyung Han | f242258 | 2020-11-04 14:21:46 +0900 | [diff] [blame] | 4054 | {StringPrintf("networkAddRouteParcel(65123, RouteInfoParcel{destination:" |
Lorenzo Colitti | 23554ba | 2020-11-05 01:43:46 +0900 | [diff] [blame] | 4055 | " 2001:db8:dead:beef::/64, ifName: %s, nextHop: fe80::dead:beef," |
Jooyung Han | f242258 | 2020-11-04 14:21:46 +0900 | [diff] [blame] | 4056 | " mtu: 1234})", |
Lorenzo Colitti | 23554ba | 2020-11-05 01:43:46 +0900 | [diff] [blame] | 4057 | sTun.name().c_str()), |
| 4058 | "networkAddRouteParcel.*65123.*dead:beef"}); |
| 4059 | |
| 4060 | EXPECT_TRUE(mNetd->networkDestroy(TEST_DUMP_NETID).isOk()); |
| 4061 | testData.push_back({"networkDestroy(65123)", "networkDestroy.*65123"}); |
| 4062 | |
| 4063 | // Send the service dump request to netd. |
Ken Chen | cf082a3 | 2022-01-15 10:58:55 +0800 | [diff] [blame] | 4064 | std::vector<std::string> lines = {}; |
| 4065 | android::status_t ret = dumpService(binder, {}, lines); |
| 4066 | ASSERT_EQ(android::OK, ret) << "Error dumping service: " << android::statusToString(ret); |
Lorenzo Colitti | 23554ba | 2020-11-05 01:43:46 +0900 | [diff] [blame] | 4067 | |
| 4068 | // Basic regexp to match dump output lines. Matches the beginning and end of the line, and |
| 4069 | // puts the output of the command itself into the first match group. |
| 4070 | // Example: " 11-05 00:23:39.481 myCommand(args) <2.02ms>". |
| 4071 | const std::basic_regex lineRegex( |
| 4072 | "^ [0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}[.][0-9]{3} " |
| 4073 | "(.*)" |
| 4074 | " <[0-9]+[.][0-9]{2}ms>$"); |
| 4075 | |
| 4076 | // For each element of testdata, check that the expected output appears in the dump output. |
| 4077 | // If not, fail the test and use hintRegex to print similar lines to assist in debugging. |
| 4078 | for (const TestData& td : testData) { |
| 4079 | const bool found = std::any_of(lines.begin(), lines.end(), [&](const std::string& line) { |
| 4080 | std::smatch match; |
| 4081 | if (!std::regex_match(line, match, lineRegex)) return false; |
| 4082 | return (match.size() == 2) && (match[1].str() == td.output); |
| 4083 | }); |
| 4084 | EXPECT_TRUE(found) << "Didn't find line '" << td.output << "' in dumpsys output."; |
| 4085 | if (found) continue; |
| 4086 | std::cerr << "Similar lines" << std::endl; |
| 4087 | for (const auto& line : lines) { |
| 4088 | if (std::regex_search(line, std::basic_regex(td.hintRegex))) { |
Lorenzo Colitti | 6bc038c | 2020-11-12 13:42:59 +0900 | [diff] [blame] | 4089 | std::cerr << line << std::endl; |
Lorenzo Colitti | 23554ba | 2020-11-05 01:43:46 +0900 | [diff] [blame] | 4090 | } |
| 4091 | } |
| 4092 | } |
| 4093 | } |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 4094 | |
| 4095 | namespace { |
| 4096 | |
| 4097 | // aliases for better reading |
| 4098 | #define SYSTEM_DEFAULT_NETID TEST_NETID1 |
| 4099 | #define APP_DEFAULT_NETID TEST_NETID2 |
| 4100 | #define VPN_NETID TEST_NETID3 |
| 4101 | |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 4102 | #define ENTERPRISE_NETID_1 TEST_NETID2 |
| 4103 | #define ENTERPRISE_NETID_2 TEST_NETID3 |
| 4104 | #define ENTERPRISE_NETID_3 TEST_NETID4 |
| 4105 | |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 4106 | void verifyAppUidRules(std::vector<bool>&& expectedResults, std::vector<UidRangeParcel>& uidRanges, |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 4107 | const std::string& iface, int32_t subPriority) { |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 4108 | ASSERT_EQ(expectedResults.size(), uidRanges.size()); |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 4109 | if (iface.size()) { |
Chalard Jean | 38bc084 | 2022-12-26 19:36:13 +0900 | [diff] [blame] | 4110 | std::string action = StringPrintf("lookup %s", iface.c_str()); |
| 4111 | std::string action_local = StringPrintf("lookup %s_local", iface.c_str()); |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 4112 | for (unsigned long i = 0; i < uidRanges.size(); i++) { |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4113 | EXPECT_EQ(expectedResults[i], |
| 4114 | ipRuleExistsForRange(RULE_PRIORITY_UID_EXPLICIT_NETWORK + subPriority, |
| 4115 | uidRanges[i], action)); |
| 4116 | EXPECT_EQ(expectedResults[i], |
| 4117 | ipRuleExistsForRange(RULE_PRIORITY_UID_IMPLICIT_NETWORK + subPriority, |
| 4118 | uidRanges[i], action)); |
| 4119 | EXPECT_EQ(expectedResults[i], |
| 4120 | ipRuleExistsForRange(RULE_PRIORITY_UID_DEFAULT_NETWORK + subPriority, |
| 4121 | uidRanges[i], action)); |
chiachangwang | 0d5ae98 | 2022-05-17 05:16:16 +0000 | [diff] [blame] | 4122 | EXPECT_EQ(expectedResults[i], ipRuleExistsForRange(RULE_PRIORITY_UID_LOCAL_ROUTES, |
| 4123 | uidRanges[i], action_local)); |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 4124 | } |
| 4125 | } else { |
| 4126 | std::string action = "unreachable"; |
| 4127 | for (unsigned long i = 0; i < uidRanges.size(); i++) { |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 4128 | EXPECT_EQ(expectedResults[i], |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4129 | ipRuleExistsForRange(RULE_PRIORITY_UID_EXPLICIT_NETWORK + subPriority, |
| 4130 | uidRanges[i], action)); |
| 4131 | EXPECT_EQ(expectedResults[i], |
| 4132 | ipRuleExistsForRange(RULE_PRIORITY_UID_IMPLICIT_NETWORK + subPriority, |
| 4133 | uidRanges[i], action)); |
| 4134 | EXPECT_EQ(expectedResults[i], |
| 4135 | ipRuleExistsForRange(RULE_PRIORITY_UID_DEFAULT_UNREACHABLE + subPriority, |
| 4136 | uidRanges[i], action)); |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 4137 | } |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 4138 | } |
| 4139 | } |
| 4140 | |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4141 | void verifyAppUidRules(std::vector<bool>&& expectedResults, NativeUidRangeConfig& uidRangeConfig, |
| 4142 | const std::string& iface) { |
donghongweng | a72addd | 2022-08-17 15:52:38 +0800 | [diff] [blame] | 4143 | verifyAppUidRules(std::move(expectedResults), uidRangeConfig.uidRanges, iface, |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4144 | uidRangeConfig.subPriority); |
| 4145 | } |
| 4146 | |
| 4147 | void verifyVpnUidRules(std::vector<bool>&& expectedResults, NativeUidRangeConfig& uidRangeConfig, |
Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 4148 | const std::string& iface, bool secure, bool excludeLocalRoutes) { |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4149 | ASSERT_EQ(expectedResults.size(), uidRangeConfig.uidRanges.size()); |
Chalard Jean | 38bc084 | 2022-12-26 19:36:13 +0900 | [diff] [blame] | 4150 | std::string action = StringPrintf("lookup %s", iface.c_str()); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4151 | |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 4152 | int32_t priority; |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4153 | if (secure) { |
| 4154 | priority = RULE_PRIORITY_SECURE_VPN; |
| 4155 | } else { |
Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 4156 | // Set to no local exclusion here to reflect the default value of local exclusion. |
| 4157 | priority = excludeLocalRoutes ? RULE_PRIORITY_BYPASSABLE_VPN_LOCAL_EXCLUSION |
| 4158 | : RULE_PRIORITY_BYPASSABLE_VPN_NO_LOCAL_EXCLUSION; |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4159 | } |
| 4160 | for (unsigned long i = 0; i < uidRangeConfig.uidRanges.size(); i++) { |
| 4161 | EXPECT_EQ(expectedResults[i], ipRuleExistsForRange(priority + uidRangeConfig.subPriority, |
| 4162 | uidRangeConfig.uidRanges[i], action)); |
| 4163 | EXPECT_EQ(expectedResults[i], |
| 4164 | ipRuleExistsForRange(RULE_PRIORITY_EXPLICIT_NETWORK + uidRangeConfig.subPriority, |
| 4165 | uidRangeConfig.uidRanges[i], action)); |
| 4166 | EXPECT_EQ(expectedResults[i], |
| 4167 | ipRuleExistsForRange(RULE_PRIORITY_OUTPUT_INTERFACE + uidRangeConfig.subPriority, |
| 4168 | uidRangeConfig.uidRanges[i], action, iface.c_str())); |
| 4169 | } |
| 4170 | } |
| 4171 | |
Patrick Rohr | e2f1b5a | 2022-01-25 21:36:50 +0100 | [diff] [blame] | 4172 | constexpr int SUB_PRIORITY_1 = UidRanges::SUB_PRIORITY_HIGHEST + 1; |
| 4173 | constexpr int SUB_PRIORITY_2 = UidRanges::SUB_PRIORITY_HIGHEST + 2; |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4174 | |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 4175 | constexpr int IMPLICITLY_SELECT = 0; |
| 4176 | constexpr int EXPLICITLY_SELECT = 1; |
| 4177 | constexpr int UNCONNECTED_SOCKET = 2; |
| 4178 | |
| 4179 | // 1. Send data with the specified UID, on a connected or unconnected socket. |
| 4180 | // 2. Verify if data is received from the specified fd. The fd should belong to a TUN, which has |
| 4181 | // been assigned to the test network. |
| 4182 | // 3. Verify if fwmark of data is correct. |
| 4183 | // Note: This is a helper function used by per-app default network tests. It does not implement full |
| 4184 | // fwmark logic in netd, and it's currently sufficient. Extension may be required for more |
| 4185 | // complicated tests. |
| 4186 | void expectPacketSentOnNetId(uid_t uid, unsigned netId, int fd, int selectionMode) { |
| 4187 | Fwmark fwmark; |
| 4188 | const bool doConnect = (selectionMode != UNCONNECTED_SOCKET); |
| 4189 | EXPECT_TRUE(sendIPv6PacketFromUid(uid, V6_ADDR, &fwmark, fd, doConnect)); |
| 4190 | |
| 4191 | Fwmark expected; |
| 4192 | expected.netId = netId; |
| 4193 | expected.explicitlySelected = (selectionMode == EXPLICITLY_SELECT); |
| 4194 | if (uid == AID_ROOT && selectionMode == EXPLICITLY_SELECT) { |
| 4195 | expected.protectedFromVpn = true; |
| 4196 | } else { |
| 4197 | expected.protectedFromVpn = false; |
| 4198 | } |
| 4199 | if (selectionMode == UNCONNECTED_SOCKET) { |
| 4200 | expected.permission = PERMISSION_NONE; |
| 4201 | } else { |
| 4202 | expected.permission = (uid == AID_ROOT) ? PERMISSION_SYSTEM : PERMISSION_NONE; |
| 4203 | } |
| 4204 | |
| 4205 | EXPECT_EQ(expected.intValue, fwmark.intValue); |
| 4206 | } |
| 4207 | |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 4208 | void expectUnreachableError(uid_t uid, unsigned netId, int selectionMode) { |
| 4209 | Fwmark fwmark; |
| 4210 | const bool doConnect = (selectionMode != UNCONNECTED_SOCKET); |
| 4211 | EXPECT_TRUE(sendIPv6PacketFromUidFail(uid, V6_ADDR, &fwmark, doConnect, ENETUNREACH)); |
| 4212 | |
| 4213 | Fwmark expected; |
| 4214 | expected.netId = netId; |
| 4215 | expected.explicitlySelected = (selectionMode == EXPLICITLY_SELECT); |
| 4216 | if (uid == AID_ROOT && selectionMode == EXPLICITLY_SELECT) { |
| 4217 | expected.protectedFromVpn = true; |
| 4218 | } else { |
| 4219 | expected.protectedFromVpn = false; |
| 4220 | } |
| 4221 | if (selectionMode == UNCONNECTED_SOCKET) { |
| 4222 | expected.permission = PERMISSION_NONE; |
| 4223 | } else { |
| 4224 | expected.permission = (uid == AID_ROOT) ? PERMISSION_SYSTEM : PERMISSION_NONE; |
| 4225 | } |
| 4226 | |
| 4227 | EXPECT_EQ(expected.intValue, fwmark.intValue); |
| 4228 | } |
| 4229 | |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 4230 | } // namespace |
| 4231 | |
chiachangwang | 65bc4ea | 2022-09-07 08:10:30 +0000 | [diff] [blame] | 4232 | // Verify how the API handle overlapped UID ranges |
Ken Chen | 868ae63 | 2021-02-24 17:50:08 +0800 | [diff] [blame] | 4233 | TEST_F(NetdBinderTest, PerAppDefaultNetwork_OverlappedUidRanges) { |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 4234 | const auto& config = makeNativeNetworkConfig(APP_DEFAULT_NETID, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 4235 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 4236 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Ken Chen | 868ae63 | 2021-02-24 17:50:08 +0800 | [diff] [blame] | 4237 | EXPECT_TRUE(mNetd->networkAddInterface(APP_DEFAULT_NETID, sTun.name()).isOk()); |
| 4238 | |
| 4239 | std::vector<UidRangeParcel> uidRanges = {makeUidRangeParcel(BASE_UID + 1, BASE_UID + 1), |
| 4240 | makeUidRangeParcel(BASE_UID + 10, BASE_UID + 12)}; |
| 4241 | EXPECT_TRUE(mNetd->networkAddUidRanges(APP_DEFAULT_NETID, uidRanges).isOk()); |
| 4242 | |
| 4243 | binder::Status status; |
| 4244 | status = mNetd->networkAddUidRanges(APP_DEFAULT_NETID, |
| 4245 | {makeUidRangeParcel(BASE_UID + 1, BASE_UID + 1)}); |
chiachangwang | 65bc4ea | 2022-09-07 08:10:30 +0000 | [diff] [blame] | 4246 | EXPECT_TRUE(status.isOk()); |
Ken Chen | 868ae63 | 2021-02-24 17:50:08 +0800 | [diff] [blame] | 4247 | |
| 4248 | status = mNetd->networkAddUidRanges(APP_DEFAULT_NETID, |
| 4249 | {makeUidRangeParcel(BASE_UID + 9, BASE_UID + 10)}); |
chiachangwang | 65bc4ea | 2022-09-07 08:10:30 +0000 | [diff] [blame] | 4250 | EXPECT_TRUE(status.isOk()); |
Ken Chen | 868ae63 | 2021-02-24 17:50:08 +0800 | [diff] [blame] | 4251 | |
| 4252 | status = mNetd->networkAddUidRanges(APP_DEFAULT_NETID, |
| 4253 | {makeUidRangeParcel(BASE_UID + 11, BASE_UID + 11)}); |
chiachangwang | 65bc4ea | 2022-09-07 08:10:30 +0000 | [diff] [blame] | 4254 | EXPECT_TRUE(status.isOk()); |
Ken Chen | 868ae63 | 2021-02-24 17:50:08 +0800 | [diff] [blame] | 4255 | |
| 4256 | status = mNetd->networkAddUidRanges(APP_DEFAULT_NETID, |
| 4257 | {makeUidRangeParcel(BASE_UID + 12, BASE_UID + 13)}); |
chiachangwang | 65bc4ea | 2022-09-07 08:10:30 +0000 | [diff] [blame] | 4258 | EXPECT_TRUE(status.isOk()); |
Ken Chen | 868ae63 | 2021-02-24 17:50:08 +0800 | [diff] [blame] | 4259 | |
| 4260 | status = mNetd->networkAddUidRanges(APP_DEFAULT_NETID, |
| 4261 | {makeUidRangeParcel(BASE_UID + 9, BASE_UID + 13)}); |
chiachangwang | 65bc4ea | 2022-09-07 08:10:30 +0000 | [diff] [blame] | 4262 | EXPECT_TRUE(status.isOk()); |
Ken Chen | 868ae63 | 2021-02-24 17:50:08 +0800 | [diff] [blame] | 4263 | |
| 4264 | std::vector<UidRangeParcel> selfOverlappedUidRanges = { |
| 4265 | makeUidRangeParcel(BASE_UID + 20, BASE_UID + 20), |
| 4266 | makeUidRangeParcel(BASE_UID + 20, BASE_UID + 21)}; |
| 4267 | status = mNetd->networkAddUidRanges(APP_DEFAULT_NETID, selfOverlappedUidRanges); |
| 4268 | EXPECT_FALSE(status.isOk()); |
| 4269 | EXPECT_EQ(EINVAL, status.serviceSpecificErrorCode()); |
| 4270 | } |
| 4271 | |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 4272 | // Verify whether IP rules for app default network are correctly configured. |
| 4273 | TEST_F(NetdBinderTest, PerAppDefaultNetwork_VerifyIpRules) { |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 4274 | const auto& config = makeNativeNetworkConfig(APP_DEFAULT_NETID, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 4275 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 4276 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 4277 | EXPECT_TRUE(mNetd->networkAddInterface(APP_DEFAULT_NETID, sTun.name()).isOk()); |
| 4278 | |
| 4279 | std::vector<UidRangeParcel> uidRanges = {makeUidRangeParcel(BASE_UID + 8005, BASE_UID + 8012), |
| 4280 | makeUidRangeParcel(BASE_UID + 8090, BASE_UID + 8099)}; |
| 4281 | |
| 4282 | EXPECT_TRUE(mNetd->networkAddUidRanges(APP_DEFAULT_NETID, uidRanges).isOk()); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4283 | verifyAppUidRules({true, true} /*expectedResults*/, uidRanges, sTun.name(), |
Patrick Rohr | e2f1b5a | 2022-01-25 21:36:50 +0100 | [diff] [blame] | 4284 | UidRanges::SUB_PRIORITY_HIGHEST); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 4285 | EXPECT_TRUE(mNetd->networkRemoveUidRanges(APP_DEFAULT_NETID, {uidRanges.at(0)}).isOk()); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4286 | verifyAppUidRules({false, true} /*expectedResults*/, uidRanges, sTun.name(), |
Patrick Rohr | e2f1b5a | 2022-01-25 21:36:50 +0100 | [diff] [blame] | 4287 | UidRanges::SUB_PRIORITY_HIGHEST); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 4288 | EXPECT_TRUE(mNetd->networkRemoveUidRanges(APP_DEFAULT_NETID, {uidRanges.at(1)}).isOk()); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4289 | verifyAppUidRules({false, false} /*expectedResults*/, uidRanges, sTun.name(), |
Patrick Rohr | e2f1b5a | 2022-01-25 21:36:50 +0100 | [diff] [blame] | 4290 | UidRanges::SUB_PRIORITY_HIGHEST); |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 4291 | |
| 4292 | EXPECT_TRUE(mNetd->networkAddUidRanges(INetd::UNREACHABLE_NET_ID, uidRanges).isOk()); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4293 | verifyAppUidRules({true, true} /*expectedResults*/, uidRanges, "", |
Patrick Rohr | e2f1b5a | 2022-01-25 21:36:50 +0100 | [diff] [blame] | 4294 | UidRanges::SUB_PRIORITY_HIGHEST); |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 4295 | EXPECT_TRUE(mNetd->networkRemoveUidRanges(INetd::UNREACHABLE_NET_ID, {uidRanges.at(0)}).isOk()); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4296 | verifyAppUidRules({false, true} /*expectedResults*/, uidRanges, "", |
Patrick Rohr | e2f1b5a | 2022-01-25 21:36:50 +0100 | [diff] [blame] | 4297 | UidRanges::SUB_PRIORITY_HIGHEST); |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 4298 | EXPECT_TRUE(mNetd->networkRemoveUidRanges(INetd::UNREACHABLE_NET_ID, {uidRanges.at(1)}).isOk()); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4299 | verifyAppUidRules({false, false} /*expectedResults*/, uidRanges, "", |
Patrick Rohr | e2f1b5a | 2022-01-25 21:36:50 +0100 | [diff] [blame] | 4300 | UidRanges::SUB_PRIORITY_HIGHEST); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | // Verify whether packets go through the right network with and without per-app default network. |
| 4304 | // Meaning of Fwmark bits (from Fwmark.h): |
| 4305 | // 0x0000ffff - Network ID |
| 4306 | // 0x00010000 - Explicit mark bit |
| 4307 | // 0x00020000 - VPN protect bit |
| 4308 | // 0x000c0000 - Permission bits |
| 4309 | TEST_F(NetdBinderTest, PerAppDefaultNetwork_ImplicitlySelectNetwork) { |
| 4310 | createDefaultAndOtherPhysicalNetwork(SYSTEM_DEFAULT_NETID, APP_DEFAULT_NETID); |
| 4311 | |
| 4312 | int systemDefaultFd = sTun.getFdForTesting(); |
| 4313 | int appDefaultFd = sTun2.getFdForTesting(); |
| 4314 | |
| 4315 | // Connections go through the system default network. |
| 4316 | expectPacketSentOnNetId(AID_ROOT, SYSTEM_DEFAULT_NETID, systemDefaultFd, IMPLICITLY_SELECT); |
| 4317 | expectPacketSentOnNetId(TEST_UID1, SYSTEM_DEFAULT_NETID, systemDefaultFd, IMPLICITLY_SELECT); |
| 4318 | |
| 4319 | // Add TEST_UID1 to per-app default network. |
| 4320 | EXPECT_TRUE(mNetd->networkAddUidRanges(APP_DEFAULT_NETID, |
| 4321 | {makeUidRangeParcel(TEST_UID1, TEST_UID1)}) |
| 4322 | .isOk()); |
| 4323 | expectPacketSentOnNetId(AID_ROOT, SYSTEM_DEFAULT_NETID, systemDefaultFd, IMPLICITLY_SELECT); |
| 4324 | expectPacketSentOnNetId(TEST_UID1, APP_DEFAULT_NETID, appDefaultFd, IMPLICITLY_SELECT); |
| 4325 | |
| 4326 | // Remove TEST_UID1 from per-app default network. |
| 4327 | EXPECT_TRUE(mNetd->networkRemoveUidRanges(APP_DEFAULT_NETID, |
| 4328 | {makeUidRangeParcel(TEST_UID1, TEST_UID1)}) |
| 4329 | .isOk()); |
| 4330 | expectPacketSentOnNetId(AID_ROOT, SYSTEM_DEFAULT_NETID, systemDefaultFd, IMPLICITLY_SELECT); |
| 4331 | expectPacketSentOnNetId(TEST_UID1, SYSTEM_DEFAULT_NETID, systemDefaultFd, IMPLICITLY_SELECT); |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 4332 | |
| 4333 | // Prohibit TEST_UID1 from using the default network. |
| 4334 | EXPECT_TRUE(mNetd->networkAddUidRanges(INetd::UNREACHABLE_NET_ID, |
| 4335 | {makeUidRangeParcel(TEST_UID1, TEST_UID1)}) |
| 4336 | .isOk()); |
| 4337 | expectPacketSentOnNetId(AID_ROOT, SYSTEM_DEFAULT_NETID, systemDefaultFd, IMPLICITLY_SELECT); |
| 4338 | expectUnreachableError(TEST_UID1, INetd::UNREACHABLE_NET_ID, IMPLICITLY_SELECT); |
| 4339 | |
| 4340 | // restore IP rules |
| 4341 | EXPECT_TRUE(mNetd->networkRemoveUidRanges(INetd::UNREACHABLE_NET_ID, |
| 4342 | {makeUidRangeParcel(TEST_UID1, TEST_UID1)}) |
| 4343 | .isOk()); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 4344 | } |
| 4345 | |
| 4346 | // Verify whether packets go through the right network when app explicitly selects a network. |
| 4347 | TEST_F(NetdBinderTest, PerAppDefaultNetwork_ExplicitlySelectNetwork) { |
| 4348 | createDefaultAndOtherPhysicalNetwork(SYSTEM_DEFAULT_NETID, APP_DEFAULT_NETID); |
| 4349 | |
| 4350 | int systemDefaultFd = sTun.getFdForTesting(); |
| 4351 | int appDefaultFd = sTun2.getFdForTesting(); |
| 4352 | |
| 4353 | // Explicitly select the system default network. |
| 4354 | setNetworkForProcess(SYSTEM_DEFAULT_NETID); |
| 4355 | // Connections go through the system default network. |
| 4356 | expectPacketSentOnNetId(AID_ROOT, SYSTEM_DEFAULT_NETID, systemDefaultFd, EXPLICITLY_SELECT); |
| 4357 | expectPacketSentOnNetId(TEST_UID1, SYSTEM_DEFAULT_NETID, systemDefaultFd, EXPLICITLY_SELECT); |
| 4358 | |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 4359 | // Set TEST_UID1 to default unreachable, which won't affect the explicitly selected network. |
| 4360 | // Connections go through the system default network. |
| 4361 | EXPECT_TRUE(mNetd->networkAddUidRanges(INetd::UNREACHABLE_NET_ID, |
| 4362 | {makeUidRangeParcel(TEST_UID1, TEST_UID1)}) |
| 4363 | .isOk()); |
| 4364 | expectPacketSentOnNetId(AID_ROOT, SYSTEM_DEFAULT_NETID, systemDefaultFd, EXPLICITLY_SELECT); |
| 4365 | expectPacketSentOnNetId(TEST_UID1, SYSTEM_DEFAULT_NETID, systemDefaultFd, EXPLICITLY_SELECT); |
| 4366 | |
| 4367 | // restore IP rules |
| 4368 | EXPECT_TRUE(mNetd->networkRemoveUidRanges(INetd::UNREACHABLE_NET_ID, |
| 4369 | {makeUidRangeParcel(TEST_UID1, TEST_UID1)}) |
| 4370 | .isOk()); |
| 4371 | |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 4372 | // Add TEST_UID1 to per-app default network, which won't affect the explicitly selected network. |
| 4373 | EXPECT_TRUE(mNetd->networkAddUidRanges(APP_DEFAULT_NETID, |
| 4374 | {makeUidRangeParcel(TEST_UID1, TEST_UID1)}) |
| 4375 | .isOk()); |
| 4376 | expectPacketSentOnNetId(AID_ROOT, SYSTEM_DEFAULT_NETID, systemDefaultFd, EXPLICITLY_SELECT); |
| 4377 | expectPacketSentOnNetId(TEST_UID1, SYSTEM_DEFAULT_NETID, systemDefaultFd, EXPLICITLY_SELECT); |
| 4378 | |
| 4379 | // Explicitly select the per-app default network. |
| 4380 | setNetworkForProcess(APP_DEFAULT_NETID); |
| 4381 | // Connections go through the per-app default network. |
| 4382 | expectPacketSentOnNetId(AID_ROOT, APP_DEFAULT_NETID, appDefaultFd, EXPLICITLY_SELECT); |
| 4383 | expectPacketSentOnNetId(TEST_UID1, APP_DEFAULT_NETID, appDefaultFd, EXPLICITLY_SELECT); |
| 4384 | } |
| 4385 | |
| 4386 | // Verify whether packets go through the right network if app does not implicitly or explicitly |
| 4387 | // select any network. |
| 4388 | TEST_F(NetdBinderTest, PerAppDefaultNetwork_UnconnectedSocket) { |
| 4389 | createDefaultAndOtherPhysicalNetwork(SYSTEM_DEFAULT_NETID, APP_DEFAULT_NETID); |
| 4390 | |
| 4391 | int systemDefaultFd = sTun.getFdForTesting(); |
| 4392 | int appDefaultFd = sTun2.getFdForTesting(); |
| 4393 | |
| 4394 | // Connections go through the system default network. |
| 4395 | expectPacketSentOnNetId(AID_ROOT, NETID_UNSET, systemDefaultFd, UNCONNECTED_SOCKET); |
| 4396 | expectPacketSentOnNetId(TEST_UID1, NETID_UNSET, systemDefaultFd, UNCONNECTED_SOCKET); |
| 4397 | |
| 4398 | // Add TEST_UID1 to per-app default network. Traffic should go through the per-app default |
| 4399 | // network if UID is in range. Otherwise, go through the system default network. |
| 4400 | EXPECT_TRUE(mNetd->networkAddUidRanges(APP_DEFAULT_NETID, |
| 4401 | {makeUidRangeParcel(TEST_UID1, TEST_UID1)}) |
| 4402 | .isOk()); |
| 4403 | expectPacketSentOnNetId(AID_ROOT, NETID_UNSET, systemDefaultFd, UNCONNECTED_SOCKET); |
| 4404 | expectPacketSentOnNetId(TEST_UID1, NETID_UNSET, appDefaultFd, UNCONNECTED_SOCKET); |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 4405 | |
Ken Chen | 55db380 | 2021-03-30 13:47:49 +0800 | [diff] [blame] | 4406 | // Set TEST_UID1's default network to unreachable. Its traffic should still go through the |
| 4407 | // per-app default network. Other traffic go through the system default network. |
| 4408 | // PS: per-app default network take precedence over unreachable network. This should happens |
| 4409 | // only in the transition period when both rules are briefly set. |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 4410 | EXPECT_TRUE(mNetd->networkAddUidRanges(INetd::UNREACHABLE_NET_ID, |
| 4411 | {makeUidRangeParcel(TEST_UID1, TEST_UID1)}) |
| 4412 | .isOk()); |
| 4413 | expectPacketSentOnNetId(AID_ROOT, NETID_UNSET, systemDefaultFd, UNCONNECTED_SOCKET); |
Ken Chen | 55db380 | 2021-03-30 13:47:49 +0800 | [diff] [blame] | 4414 | expectPacketSentOnNetId(TEST_UID1, NETID_UNSET, appDefaultFd, UNCONNECTED_SOCKET); |
| 4415 | |
| 4416 | // Remove TEST_UID1's default network from OEM-paid network. Its traffic should get ENETUNREACH |
| 4417 | // error. Other traffic still go through the system default network. |
| 4418 | EXPECT_TRUE(mNetd->networkRemoveUidRanges(APP_DEFAULT_NETID, |
| 4419 | {makeUidRangeParcel(TEST_UID1, TEST_UID1)}) |
| 4420 | .isOk()); |
| 4421 | expectPacketSentOnNetId(AID_ROOT, NETID_UNSET, systemDefaultFd, UNCONNECTED_SOCKET); |
Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 4422 | expectUnreachableError(TEST_UID1, NETID_UNSET, UNCONNECTED_SOCKET); |
| 4423 | |
| 4424 | // restore IP rules |
| 4425 | EXPECT_TRUE(mNetd->networkRemoveUidRanges(INetd::UNREACHABLE_NET_ID, |
| 4426 | {makeUidRangeParcel(TEST_UID1, TEST_UID1)}) |
| 4427 | .isOk()); |
Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | TEST_F(NetdBinderTest, PerAppDefaultNetwork_PermissionCheck) { |
| 4431 | createPhysicalNetwork(APP_DEFAULT_NETID, sTun2.name(), INetd::PERMISSION_SYSTEM); |
| 4432 | |
| 4433 | { // uid is not in app range. Can not set network for process. |
| 4434 | ScopedUidChange scopedUidChange(TEST_UID1); |
| 4435 | EXPECT_EQ(-EACCES, setNetworkForProcess(APP_DEFAULT_NETID)); |
| 4436 | } |
| 4437 | |
| 4438 | EXPECT_TRUE(mNetd->networkAddUidRanges(APP_DEFAULT_NETID, |
| 4439 | {makeUidRangeParcel(TEST_UID1, TEST_UID1)}) |
| 4440 | .isOk()); |
| 4441 | |
| 4442 | { // uid is in app range. Can set network for process. |
| 4443 | ScopedUidChange scopedUidChange(TEST_UID1); |
| 4444 | EXPECT_EQ(0, setNetworkForProcess(APP_DEFAULT_NETID)); |
| 4445 | } |
| 4446 | } |
| 4447 | |
| 4448 | class VpnParameterizedTest : public NetdBinderTest, public testing::WithParamInterface<bool> {}; |
| 4449 | |
| 4450 | // Exercise secure and bypassable VPN. |
| 4451 | INSTANTIATE_TEST_SUITE_P(PerAppDefaultNetwork, VpnParameterizedTest, testing::Bool(), |
| 4452 | [](const testing::TestParamInfo<bool>& info) { |
| 4453 | return info.param ? "SecureVPN" : "BypassableVPN"; |
| 4454 | }); |
| 4455 | |
| 4456 | // Verify per-app default network + VPN. |
| 4457 | TEST_P(VpnParameterizedTest, ImplicitlySelectNetwork) { |
| 4458 | const bool isSecureVPN = GetParam(); |
| 4459 | createVpnAndAppDefaultNetworkWithUid( |
| 4460 | SYSTEM_DEFAULT_NETID, APP_DEFAULT_NETID, VPN_NETID, isSecureVPN, |
| 4461 | {makeUidRangeParcel(TEST_UID2, TEST_UID1)} /* app range */, |
| 4462 | {makeUidRangeParcel(TEST_UID3, TEST_UID2)} /* VPN range */); |
| 4463 | |
| 4464 | int systemDefaultFd = sTun.getFdForTesting(); |
| 4465 | int appDefaultFd = sTun2.getFdForTesting(); |
| 4466 | int vpnFd = sTun3.getFdForTesting(); |
| 4467 | |
| 4468 | // uid is neither in app range, nor in VPN range. Traffic goes through system default network. |
| 4469 | expectPacketSentOnNetId(AID_ROOT, SYSTEM_DEFAULT_NETID, systemDefaultFd, IMPLICITLY_SELECT); |
| 4470 | // uid is in VPN range, not in app range. Traffic goes through VPN. |
| 4471 | expectPacketSentOnNetId(TEST_UID3, (isSecureVPN ? SYSTEM_DEFAULT_NETID : VPN_NETID), vpnFd, |
| 4472 | IMPLICITLY_SELECT); |
| 4473 | // uid is in app range, not in VPN range. Traffic goes through per-app default network. |
| 4474 | expectPacketSentOnNetId(TEST_UID1, APP_DEFAULT_NETID, appDefaultFd, IMPLICITLY_SELECT); |
| 4475 | // uid is in both app and VPN range. Traffic goes through VPN. |
| 4476 | expectPacketSentOnNetId(TEST_UID2, (isSecureVPN ? APP_DEFAULT_NETID : VPN_NETID), vpnFd, |
| 4477 | IMPLICITLY_SELECT); |
| 4478 | } |
| 4479 | |
| 4480 | class VpnAndSelectNetworkParameterizedTest |
| 4481 | : public NetdBinderTest, |
| 4482 | public testing::WithParamInterface<std::tuple<bool, int>> {}; |
| 4483 | |
| 4484 | // Exercise the combination of different VPN types and different user selected networks. e.g. |
| 4485 | // secure VPN + select on system default network |
| 4486 | // secure VPN + select on app default network |
| 4487 | // secure VPN + select on VPN |
| 4488 | // bypassable VPN + select on system default network |
| 4489 | // ... |
| 4490 | INSTANTIATE_TEST_SUITE_P(PerAppDefaultNetwork, VpnAndSelectNetworkParameterizedTest, |
| 4491 | testing::Combine(testing::Bool(), |
| 4492 | testing::Values(SYSTEM_DEFAULT_NETID, APP_DEFAULT_NETID, |
| 4493 | VPN_NETID)), |
| 4494 | [](const testing::TestParamInfo<std::tuple<bool, int>>& info) { |
| 4495 | const std::string vpnType = std::get<0>(info.param) |
| 4496 | ? std::string("SecureVPN") |
| 4497 | : std::string("BypassableVPN"); |
| 4498 | std::string selectedNetwork; |
| 4499 | switch (std::get<1>(info.param)) { |
| 4500 | case SYSTEM_DEFAULT_NETID: |
| 4501 | selectedNetwork = "SystemDefaultNetwork"; |
| 4502 | break; |
| 4503 | case APP_DEFAULT_NETID: |
| 4504 | selectedNetwork = "AppDefaultNetwork"; |
| 4505 | break; |
| 4506 | case VPN_NETID: |
| 4507 | selectedNetwork = "VPN"; |
| 4508 | break; |
| 4509 | default: |
| 4510 | selectedNetwork = "InvalidParameter"; // Should not happen. |
| 4511 | } |
| 4512 | return vpnType + "_select" + selectedNetwork; |
| 4513 | }); |
| 4514 | |
| 4515 | TEST_P(VpnAndSelectNetworkParameterizedTest, ExplicitlySelectNetwork) { |
| 4516 | bool isSecureVPN; |
| 4517 | int selectedNetId; |
| 4518 | std::tie(isSecureVPN, selectedNetId) = GetParam(); |
| 4519 | createVpnAndAppDefaultNetworkWithUid( |
| 4520 | SYSTEM_DEFAULT_NETID, APP_DEFAULT_NETID, VPN_NETID, isSecureVPN, |
| 4521 | {makeUidRangeParcel(TEST_UID2, TEST_UID1)} /* app range */, |
| 4522 | {makeUidRangeParcel(TEST_UID3, TEST_UID2)} /* VPN range */); |
| 4523 | |
| 4524 | int expectedFd = -1; |
| 4525 | switch (selectedNetId) { |
| 4526 | case SYSTEM_DEFAULT_NETID: |
| 4527 | expectedFd = sTun.getFdForTesting(); |
| 4528 | break; |
| 4529 | case APP_DEFAULT_NETID: |
| 4530 | expectedFd = sTun2.getFdForTesting(); |
| 4531 | break; |
| 4532 | case VPN_NETID: |
| 4533 | expectedFd = sTun3.getFdForTesting(); |
| 4534 | break; |
| 4535 | default: |
| 4536 | GTEST_LOG_(ERROR) << "unexpected netId:" << selectedNetId; // Should not happen. |
| 4537 | } |
| 4538 | |
| 4539 | // In all following permutations, Traffic should go through the specified network if a process |
| 4540 | // can select network for itself. The fwmark should contain process UID and the explicit select |
| 4541 | // bit. |
| 4542 | { // uid is neither in app range, nor in VPN range. Permission bits, protect bit, and explicit |
| 4543 | // select bit are all set because of AID_ROOT. |
| 4544 | ScopedUidChange scopedUidChange(AID_ROOT); |
| 4545 | EXPECT_EQ(0, setNetworkForProcess(selectedNetId)); |
| 4546 | expectPacketSentOnNetId(AID_ROOT, selectedNetId, expectedFd, EXPLICITLY_SELECT); |
| 4547 | } |
| 4548 | { // uid is in VPN range, not in app range. |
| 4549 | ScopedUidChange scopedUidChange(TEST_UID3); |
| 4550 | // Cannot select non-VPN networks when uid is subject to secure VPN. |
| 4551 | if (isSecureVPN && selectedNetId != VPN_NETID) { |
| 4552 | EXPECT_EQ(-EPERM, setNetworkForProcess(selectedNetId)); |
| 4553 | } else { |
| 4554 | EXPECT_EQ(0, setNetworkForProcess(selectedNetId)); |
| 4555 | expectPacketSentOnNetId(TEST_UID3, selectedNetId, expectedFd, EXPLICITLY_SELECT); |
| 4556 | } |
| 4557 | } |
| 4558 | { // uid is in app range, not in VPN range. |
| 4559 | ScopedUidChange scopedUidChange(TEST_UID1); |
| 4560 | // Cannot select the VPN because the VPN does not applies to the UID. |
| 4561 | if (selectedNetId == VPN_NETID) { |
| 4562 | EXPECT_EQ(-EPERM, setNetworkForProcess(selectedNetId)); |
| 4563 | } else { |
| 4564 | EXPECT_EQ(0, setNetworkForProcess(selectedNetId)); |
| 4565 | expectPacketSentOnNetId(TEST_UID1, selectedNetId, expectedFd, EXPLICITLY_SELECT); |
| 4566 | } |
| 4567 | } |
| 4568 | { // uid is in both app range and VPN range. |
| 4569 | ScopedUidChange scopedUidChange(TEST_UID2); |
| 4570 | // Cannot select non-VPN networks when uid is subject to secure VPN. |
| 4571 | if (isSecureVPN && selectedNetId != VPN_NETID) { |
| 4572 | EXPECT_EQ(-EPERM, setNetworkForProcess(selectedNetId)); |
| 4573 | } else { |
| 4574 | EXPECT_EQ(0, setNetworkForProcess(selectedNetId)); |
| 4575 | expectPacketSentOnNetId(TEST_UID2, selectedNetId, expectedFd, EXPLICITLY_SELECT); |
| 4576 | } |
| 4577 | } |
| 4578 | } |
| 4579 | |
| 4580 | TEST_P(VpnParameterizedTest, UnconnectedSocket) { |
| 4581 | const bool isSecureVPN = GetParam(); |
| 4582 | createVpnAndAppDefaultNetworkWithUid( |
| 4583 | SYSTEM_DEFAULT_NETID, APP_DEFAULT_NETID, VPN_NETID, isSecureVPN, |
| 4584 | {makeUidRangeParcel(TEST_UID2, TEST_UID1)} /* app range */, |
| 4585 | {makeUidRangeParcel(TEST_UID3, TEST_UID2)} /* VPN range */); |
| 4586 | |
| 4587 | int systemDefaultFd = sTun.getFdForTesting(); |
| 4588 | int appDefaultFd = sTun2.getFdForTesting(); |
| 4589 | int vpnFd = sTun3.getFdForTesting(); |
| 4590 | |
| 4591 | // uid is neither in app range, nor in VPN range. Traffic goes through system default network. |
| 4592 | expectPacketSentOnNetId(AID_ROOT, NETID_UNSET, systemDefaultFd, UNCONNECTED_SOCKET); |
| 4593 | // uid is in VPN range, not in app range. Traffic goes through VPN. |
| 4594 | expectPacketSentOnNetId(TEST_UID3, NETID_UNSET, vpnFd, UNCONNECTED_SOCKET); |
| 4595 | // uid is in app range, not in VPN range. Traffic goes through per-app default network. |
| 4596 | expectPacketSentOnNetId(TEST_UID1, NETID_UNSET, appDefaultFd, UNCONNECTED_SOCKET); |
| 4597 | // uid is in both app and VPN range. Traffic goes through VPN. |
| 4598 | expectPacketSentOnNetId(TEST_UID2, NETID_UNSET, vpnFd, UNCONNECTED_SOCKET); |
| 4599 | } |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 4600 | |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4601 | class VpnLocalRoutesParameterizedTest |
| 4602 | : public NetdBinderTest, |
| 4603 | public testing::WithParamInterface<std::tuple<int, int, bool, bool, bool, bool>> { |
| 4604 | protected: |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4605 | // Local/non-local addresses based on the route added in |
| 4606 | // setupNetworkRoutesForVpnAndDefaultNetworks. |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4607 | in_addr V4_LOCAL_ADDR = {htonl(0xC0A80008)}; // 192.168.0.8 |
| 4608 | in_addr V4_APP_LOCAL_ADDR = {htonl(0xAC100008)}; // 172.16.0.8 |
| 4609 | in_addr V4_GLOBAL_ADDR = {htonl(0x08080808)}; // 8.8.8.8 |
| 4610 | |
| 4611 | in6_addr V6_LOCAL_ADDR = { |
| 4612 | {// 2001:db8:cafe::1 |
| 4613 | .u6_addr8 = {0x20, 0x01, 0x0d, 0xb8, 0xca, 0xfe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}}; |
| 4614 | in6_addr V6_APP_LOCAL_ADDR = { |
| 4615 | {// 2607:f0d0:1234::4 |
| 4616 | .u6_addr8 = {0x26, 0x07, 0xf0, 0xd0, 0x12, 0x34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}}}; |
| 4617 | in6_addr V6_GLOBAL_ADDR = { |
| 4618 | {// 2607:1234:1002::4 |
| 4619 | .u6_addr8 = {0x26, 0x07, 0x12, 0x34, 0x10, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}}}; |
| 4620 | }; |
| 4621 | |
| 4622 | const int SEND_TO_GLOBAL = 0; |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4623 | const int SEND_TO_SYSTEM_DEFAULT_LOCAL = 1; |
| 4624 | const int SEND_TO_PER_APP_DEFAULT_LOCAL = 2; |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4625 | |
| 4626 | // Exercise the combination of different explicitly selected network, different uid, local/non-local |
| 4627 | // address on local route exclusion VPN. E.g. |
| 4628 | // explicitlySelected systemDefault + uid in VPN range + no app default + non local address |
| 4629 | // explicitlySelected systemDefault + uid in VPN range + has app default + non local address |
| 4630 | // explicitlySelected systemDefault + uid in VPN range + has app default + local address |
| 4631 | // explicitlySelected appDefault + uid not in VPN range + has app default + non local address |
| 4632 | INSTANTIATE_TEST_SUITE_P( |
| 4633 | PerAppDefaultNetwork, VpnLocalRoutesParameterizedTest, |
| 4634 | testing::Combine(testing::Values(SYSTEM_DEFAULT_NETID, APP_DEFAULT_NETID, NETID_UNSET), |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4635 | testing::Values(SEND_TO_GLOBAL, SEND_TO_SYSTEM_DEFAULT_LOCAL, |
| 4636 | SEND_TO_PER_APP_DEFAULT_LOCAL), |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4637 | testing::Bool(), testing::Bool(), testing::Bool(), testing::Bool()), |
| 4638 | [](const testing::TestParamInfo<std::tuple<int, int, bool, bool, bool, bool>>& info) { |
| 4639 | std::string explicitlySelected; |
| 4640 | switch (std::get<0>(info.param)) { |
| 4641 | case SYSTEM_DEFAULT_NETID: |
| 4642 | explicitlySelected = "explicitlySelectedSystemDefault"; |
| 4643 | break; |
| 4644 | case APP_DEFAULT_NETID: |
| 4645 | explicitlySelected = "explicitlySelectedAppDefault"; |
| 4646 | break; |
| 4647 | case NETID_UNSET: |
| 4648 | explicitlySelected = "implicitlySelected"; |
| 4649 | break; |
| 4650 | default: |
| 4651 | explicitlySelected = "InvalidParameter"; // Should not happen. |
| 4652 | } |
| 4653 | |
| 4654 | std::string sendToAddr; |
| 4655 | switch (std::get<1>(info.param)) { |
| 4656 | case SEND_TO_GLOBAL: |
| 4657 | sendToAddr = "GlobalAddr"; |
| 4658 | break; |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4659 | case SEND_TO_SYSTEM_DEFAULT_LOCAL: |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4660 | sendToAddr = "SystemLocal"; |
| 4661 | break; |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4662 | case SEND_TO_PER_APP_DEFAULT_LOCAL: |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4663 | sendToAddr = "AppLocal"; |
| 4664 | break; |
| 4665 | default: |
| 4666 | sendToAddr = "InvalidAddr"; // Should not happen. |
| 4667 | } |
| 4668 | |
| 4669 | const std::string isSubjectToVpn = std::get<2>(info.param) |
| 4670 | ? std::string("SubjectToVpn") |
| 4671 | : std::string("NotSubjectToVpn"); |
| 4672 | |
| 4673 | const std::string hasAppDefaultNetwork = std::get<3>(info.param) |
| 4674 | ? std::string("HasAppDefault") |
| 4675 | : std::string("NothasAppDefault"); |
| 4676 | |
| 4677 | const std::string testV6 = |
| 4678 | std::get<4>(info.param) ? std::string("v6") : std::string("v4"); |
| 4679 | |
| 4680 | // Apply the same or different local address in app default and system default. |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4681 | const std::string differentLocalRoutes = std::get<5>(info.param) |
| 4682 | ? std::string("DifferentLocalRoutes") |
| 4683 | : std::string("SameLocalAddr"); |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4684 | |
| 4685 | return explicitlySelected + "_uid" + isSubjectToVpn + hasAppDefaultNetwork + |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4686 | "Range_with" + testV6 + sendToAddr + differentLocalRoutes; |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4687 | }); |
| 4688 | |
| 4689 | int getTargetIfaceForLocalRoutesExclusion(bool isSubjectToVpn, bool hasAppDefaultNetwork, |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4690 | bool differentLocalRoutes, int sendToAddr, |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4691 | int selectedNetId, int fallthroughFd, int appDefaultFd, |
| 4692 | int vpnFd) { |
| 4693 | int expectedIface; |
| 4694 | |
| 4695 | // Setup the expected interface based on the condition. |
| 4696 | if (isSubjectToVpn && hasAppDefaultNetwork) { |
| 4697 | switch (sendToAddr) { |
| 4698 | case SEND_TO_GLOBAL: |
| 4699 | expectedIface = vpnFd; |
| 4700 | break; |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4701 | case SEND_TO_SYSTEM_DEFAULT_LOCAL: |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4702 | // Go to app default if the app default and system default are the same range |
| 4703 | // TODO(b/237351736): It should go to VPN if the system local and app local are |
| 4704 | // different. |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4705 | expectedIface = differentLocalRoutes ? fallthroughFd : appDefaultFd; |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4706 | break; |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4707 | case SEND_TO_PER_APP_DEFAULT_LOCAL: |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4708 | expectedIface = appDefaultFd; |
| 4709 | break; |
| 4710 | default: |
| 4711 | expectedIface = -1; // should not happen |
| 4712 | } |
| 4713 | } else if (isSubjectToVpn && !hasAppDefaultNetwork) { |
| 4714 | switch (sendToAddr) { |
| 4715 | case SEND_TO_GLOBAL: |
| 4716 | expectedIface = vpnFd; |
| 4717 | break; |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4718 | case SEND_TO_SYSTEM_DEFAULT_LOCAL: |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4719 | // TODO(b/237351736): It should go to app default if the system local and app local |
| 4720 | // are different. |
| 4721 | expectedIface = fallthroughFd; |
| 4722 | break; |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4723 | case SEND_TO_PER_APP_DEFAULT_LOCAL: |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4724 | // Go to system default if the system default and app default are the same range. |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4725 | expectedIface = differentLocalRoutes ? vpnFd : fallthroughFd; |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4726 | break; |
| 4727 | default: |
| 4728 | expectedIface = -1; // should not happen |
| 4729 | } |
| 4730 | } else if (!isSubjectToVpn && hasAppDefaultNetwork) { |
| 4731 | expectedIface = appDefaultFd; |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4732 | } else { // !isSubjectToVpn && !hasAppDefaultNetwork |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4733 | expectedIface = fallthroughFd; |
| 4734 | } |
| 4735 | |
| 4736 | // Override the target if it's explicitly selected. |
| 4737 | switch (selectedNetId) { |
| 4738 | case SYSTEM_DEFAULT_NETID: |
| 4739 | expectedIface = fallthroughFd; |
| 4740 | break; |
| 4741 | case APP_DEFAULT_NETID: |
| 4742 | expectedIface = appDefaultFd; |
| 4743 | break; |
| 4744 | default: |
| 4745 | break; |
| 4746 | // Based on the uid range. |
| 4747 | } |
| 4748 | |
| 4749 | return expectedIface; |
| 4750 | } |
| 4751 | |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4752 | // Routes configured on the system default network and on the VPN. |
| 4753 | // This allows the test to verify the worst case where the physical network and the VPN configure |
| 4754 | // the same routes. This ensures that routing is determined by the IP rules and doesn't just happen |
| 4755 | // to work because the routes don't overlap. If differentLocalRoutes is false, these routes are also |
| 4756 | // configured on the per-app default network. |
| 4757 | // For both IPv4 and IPv6, the first route is local, the second is not. |
chiachangwang | d5c6b4c | 2022-11-23 01:29:11 +0000 | [diff] [blame] | 4758 | std::vector<std::string> SYSTEM_DEFAULT_ROUTES = {"192.168.0.0/16", "0.0.0.0/0", |
| 4759 | "2001:db8:cafe::/48", "::/0"}; |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4760 | // Routes configured on the per-app default network if differentLocalRoutes is true. |
| 4761 | // For both IPv4 and IPv6, the first route is local, the second is not. |
chiachangwang | d5c6b4c | 2022-11-23 01:29:11 +0000 | [diff] [blame] | 4762 | std::vector<std::string> APP_DEFAULT_ROUTES = {"172.16.0.0/16", "0.0.0.0/0", "2607:f0d0:1234::/48", |
| 4763 | "::/0"}; |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4764 | void NetdBinderTest::setupNetworkRoutesForVpnAndDefaultNetworks( |
| 4765 | int systemDefaultNetId, int appDefaultNetId, int vpnNetId, int otherNetId, bool secure, |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4766 | bool testV6, bool differentLocalRoutes, std::vector<UidRangeParcel>&& appDefaultUidRanges, |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4767 | std::vector<UidRangeParcel>&& vpnUidRanges) { |
| 4768 | // Create a physical network on sTun, and set it as the system default network |
| 4769 | createAndSetDefaultNetwork(systemDefaultNetId, sTun.name()); |
| 4770 | |
| 4771 | // Routes are configured to system default, app default and vpn network to verify if the packets |
| 4772 | // are routed correctly. |
| 4773 | |
| 4774 | // Setup system default routing. |
chiachangwang | d5c6b4c | 2022-11-23 01:29:11 +0000 | [diff] [blame] | 4775 | for (const auto& route : SYSTEM_DEFAULT_ROUTES) { |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4776 | EXPECT_TRUE(mNetd->networkAddRoute(systemDefaultNetId, sTun.name(), route, "").isOk()); |
| 4777 | } |
| 4778 | |
| 4779 | // Create another physical network on sTun2 as per app default network |
| 4780 | createPhysicalNetwork(appDefaultNetId, sTun2.name()); |
| 4781 | |
| 4782 | // Setup app default routing. |
| 4783 | std::vector<std::string> appDefaultRoutes = |
chiachangwang | d5c6b4c | 2022-11-23 01:29:11 +0000 | [diff] [blame] | 4784 | (differentLocalRoutes ? APP_DEFAULT_ROUTES : SYSTEM_DEFAULT_ROUTES); |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4785 | for (const auto& route : appDefaultRoutes) { |
| 4786 | EXPECT_TRUE(mNetd->networkAddRoute(appDefaultNetId, sTun2.name(), route, "").isOk()); |
| 4787 | } |
| 4788 | |
| 4789 | // Create a bypassable VPN on sTun3. |
| 4790 | auto config = makeNativeNetworkConfig(vpnNetId, NativeNetworkType::VIRTUAL, |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4791 | INetd::PERMISSION_NONE, secure, true); |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4792 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
| 4793 | EXPECT_TRUE(mNetd->networkAddInterface(vpnNetId, sTun3.name()).isOk()); |
| 4794 | |
| 4795 | // Setup vpn routing. |
chiachangwang | d5c6b4c | 2022-11-23 01:29:11 +0000 | [diff] [blame] | 4796 | for (const auto& route : SYSTEM_DEFAULT_ROUTES) { |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4797 | EXPECT_TRUE(mNetd->networkAddRoute(vpnNetId, sTun3.name(), route, "").isOk()); |
| 4798 | } |
| 4799 | |
| 4800 | // Create another interface that is neither system default nor the app default to make sure |
| 4801 | // the traffic won't be mis-routed. |
| 4802 | createPhysicalNetwork(otherNetId, sTun4.name()); |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4803 | EXPECT_TRUE(mNetd->networkAddRoute(otherNetId, sTun4.name(), testV6 ? "::/0" : "0.0.0.0/0", "") |
| 4804 | .isOk()); |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4805 | // Add per-app uid ranges. |
| 4806 | EXPECT_TRUE(mNetd->networkAddUidRanges(appDefaultNetId, appDefaultUidRanges).isOk()); |
| 4807 | |
| 4808 | // Add VPN uid ranges. |
| 4809 | EXPECT_TRUE(mNetd->networkAddUidRanges(vpnNetId, vpnUidRanges).isOk()); |
| 4810 | } |
| 4811 | |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4812 | // Rules are in approximately the following order for bypassable VPNs that allow local network |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4813 | // access: |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4814 | // - Local routes to the per-app default network (UID guarded) |
| 4815 | // - Local routes to the system default network |
| 4816 | // - Both local and global routs to VPN network (UID guarded) |
| 4817 | // - Global routes to per-app default network(UID guarded) |
| 4818 | // - Global routes to system default network |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4819 | TEST_P(VpnLocalRoutesParameterizedTest, localRoutesExclusion) { |
| 4820 | int selectedNetId; |
| 4821 | int sendToAddr; |
| 4822 | bool isSubjectToVpn; |
| 4823 | bool hasAppDefaultNetwork; |
| 4824 | bool testV6; |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4825 | bool differentLocalRoutes; |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4826 | |
| 4827 | std::tie(selectedNetId, sendToAddr, isSubjectToVpn, hasAppDefaultNetwork, testV6, |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4828 | differentLocalRoutes) = GetParam(); |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4829 | |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4830 | setupNetworkRoutesForVpnAndDefaultNetworks( |
| 4831 | SYSTEM_DEFAULT_NETID, APP_DEFAULT_NETID, VPN_NETID, TEST_NETID4, false /* secure */, |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4832 | testV6, differentLocalRoutes, |
| 4833 | // Setup uid ranges for app default and VPN. Configure TEST_UID2 into both app default |
| 4834 | // and VPN to verify the behavior when the uid exists in both network. |
| 4835 | {makeUidRangeParcel(TEST_UID2, TEST_UID1)}, {makeUidRangeParcel(TEST_UID3, TEST_UID2)}); |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4836 | |
| 4837 | int fallthroughFd = sTun.getFdForTesting(); |
| 4838 | int appDefaultFd = sTun2.getFdForTesting(); |
| 4839 | int vpnFd = sTun3.getFdForTesting(); |
| 4840 | |
| 4841 | // Explicitly select network |
| 4842 | setNetworkForProcess(selectedNetId); |
| 4843 | |
| 4844 | int targetUid; |
| 4845 | |
| 4846 | // Setup the expected testing uid |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4847 | if (isSubjectToVpn) { |
| 4848 | if (hasAppDefaultNetwork) { |
| 4849 | targetUid = TEST_UID2; |
| 4850 | } else { |
| 4851 | targetUid = TEST_UID3; |
| 4852 | } |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4853 | } else { |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4854 | if (hasAppDefaultNetwork) { |
| 4855 | targetUid = TEST_UID1; |
| 4856 | } else { |
| 4857 | targetUid = TEST_UID4; // Not in any of the UID ranges. |
| 4858 | } |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4859 | } |
| 4860 | |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4861 | // Get expected interface for the traffic. |
| 4862 | int expectedIface = getTargetIfaceForLocalRoutesExclusion( |
| 4863 | isSubjectToVpn, hasAppDefaultNetwork, differentLocalRoutes, sendToAddr, selectedNetId, |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4864 | fallthroughFd, appDefaultFd, vpnFd); |
| 4865 | |
| 4866 | // Verify the packets are sent to the expected interface. |
| 4867 | Fwmark fwmark; |
| 4868 | if (testV6) { |
| 4869 | in6_addr addr; |
| 4870 | switch (sendToAddr) { |
| 4871 | case SEND_TO_GLOBAL: |
| 4872 | addr = V6_GLOBAL_ADDR; |
| 4873 | break; |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4874 | case SEND_TO_SYSTEM_DEFAULT_LOCAL: |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4875 | addr = V6_LOCAL_ADDR; |
| 4876 | break; |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4877 | case SEND_TO_PER_APP_DEFAULT_LOCAL: |
| 4878 | addr = differentLocalRoutes ? V6_APP_LOCAL_ADDR : V6_LOCAL_ADDR; |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4879 | break; |
| 4880 | default: |
| 4881 | break; |
| 4882 | // should not happen |
| 4883 | } |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4884 | EXPECT_TRUE(sendIPv6PacketFromUid(targetUid, addr, &fwmark, expectedIface)); |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4885 | } else { |
| 4886 | in_addr addr; |
| 4887 | switch (sendToAddr) { |
| 4888 | case SEND_TO_GLOBAL: |
| 4889 | addr = V4_GLOBAL_ADDR; |
| 4890 | break; |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4891 | case SEND_TO_SYSTEM_DEFAULT_LOCAL: |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4892 | addr = V4_LOCAL_ADDR; |
| 4893 | break; |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4894 | case SEND_TO_PER_APP_DEFAULT_LOCAL: |
| 4895 | addr = differentLocalRoutes ? V4_APP_LOCAL_ADDR : V4_LOCAL_ADDR; |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4896 | break; |
| 4897 | default: |
| 4898 | break; |
| 4899 | // should not happen |
| 4900 | } |
| 4901 | |
chiachangwang | 4ad7cde | 2022-11-08 01:42:09 +0000 | [diff] [blame] | 4902 | EXPECT_TRUE(sendIPv4PacketFromUid(targetUid, addr, &fwmark, expectedIface)); |
chiachangwang | d73826d | 2022-08-24 08:51:12 +0000 | [diff] [blame] | 4903 | } |
| 4904 | } |
| 4905 | |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 4906 | TEST_F(NetdBinderTest, NetworkCreate) { |
| 4907 | auto config = makeNativeNetworkConfig(TEST_NETID1, NativeNetworkType::PHYSICAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 4908 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 4909 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
| 4910 | EXPECT_TRUE(mNetd->networkDestroy(config.netId).isOk()); |
| 4911 | |
| 4912 | config.networkType = NativeNetworkType::VIRTUAL; |
| 4913 | config.secure = true; |
| 4914 | config.vpnType = NativeVpnType::OEM; |
| 4915 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
| 4916 | |
| 4917 | // invalid network type |
| 4918 | auto wrongConfig = makeNativeNetworkConfig(TEST_NETID2, static_cast<NativeNetworkType>(-1), |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 4919 | INetd::PERMISSION_NONE, false, false); |
Ken Chen | ab5f347 | 2021-04-04 11:28:06 +0800 | [diff] [blame] | 4920 | EXPECT_EQ(EINVAL, mNetd->networkCreate(wrongConfig).serviceSpecificErrorCode()); |
| 4921 | |
| 4922 | // invalid VPN type |
| 4923 | wrongConfig.networkType = NativeNetworkType::VIRTUAL; |
| 4924 | wrongConfig.vpnType = static_cast<NativeVpnType>(-1); |
| 4925 | EXPECT_EQ(EINVAL, mNetd->networkCreate(wrongConfig).serviceSpecificErrorCode()); |
| 4926 | } |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4927 | |
| 4928 | // Verifies valid and invalid inputs on networkAddUidRangesParcel method. |
| 4929 | TEST_F(NetdBinderTest, UidRangeSubPriority_ValidateInputs) { |
| 4930 | createVpnAndOtherPhysicalNetwork(SYSTEM_DEFAULT_NETID, APP_DEFAULT_NETID, VPN_NETID, |
| 4931 | /*isSecureVPN=*/true); |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 4932 | // Invalid priority -10 on a physical network. |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4933 | NativeUidRangeConfig uidRangeConfig = |
| 4934 | makeNativeUidRangeConfig(APP_DEFAULT_NETID, {makeUidRangeParcel(BASE_UID, BASE_UID)}, |
Patrick Rohr | e2f1b5a | 2022-01-25 21:36:50 +0100 | [diff] [blame] | 4935 | UidRanges::SUB_PRIORITY_HIGHEST - 10); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4936 | binder::Status status = mNetd->networkAddUidRangesParcel(uidRangeConfig); |
| 4937 | EXPECT_FALSE(status.isOk()); |
| 4938 | EXPECT_EQ(EINVAL, status.serviceSpecificErrorCode()); |
| 4939 | |
| 4940 | // Invalid priority 1000 on a physical network. |
Patrick Rohr | e6f198c | 2022-01-25 13:50:31 +0100 | [diff] [blame] | 4941 | uidRangeConfig.subPriority = UidRanges::SUB_PRIORITY_NO_DEFAULT + 1; |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4942 | status = mNetd->networkAddUidRangesParcel(uidRangeConfig); |
| 4943 | EXPECT_FALSE(status.isOk()); |
| 4944 | EXPECT_EQ(EINVAL, status.serviceSpecificErrorCode()); |
| 4945 | |
| 4946 | // Virtual networks support only default priority. |
| 4947 | uidRangeConfig.netId = VPN_NETID; |
| 4948 | uidRangeConfig.subPriority = SUB_PRIORITY_1; |
| 4949 | status = mNetd->networkAddUidRangesParcel(uidRangeConfig); |
| 4950 | EXPECT_FALSE(status.isOk()); |
| 4951 | EXPECT_EQ(EINVAL, status.serviceSpecificErrorCode()); |
| 4952 | |
| 4953 | // For a single network, identical UID ranges with different priorities are allowed. |
| 4954 | uidRangeConfig.netId = APP_DEFAULT_NETID; |
| 4955 | uidRangeConfig.subPriority = SUB_PRIORITY_1; |
| 4956 | EXPECT_TRUE(mNetd->networkAddUidRangesParcel(uidRangeConfig).isOk()); |
| 4957 | uidRangeConfig.subPriority = SUB_PRIORITY_2; |
| 4958 | EXPECT_TRUE(mNetd->networkAddUidRangesParcel(uidRangeConfig).isOk()); |
| 4959 | |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 4960 | // Overlapping ranges is invalid. |
| 4961 | uidRangeConfig.uidRanges = {makeUidRangeParcel(BASE_UID + 1, BASE_UID + 1), |
| 4962 | makeUidRangeParcel(BASE_UID + 1, BASE_UID + 1)}; |
| 4963 | status = mNetd->networkAddUidRangesParcel(uidRangeConfig); |
| 4964 | EXPECT_FALSE(status.isOk()); |
| 4965 | EXPECT_EQ(EINVAL, status.serviceSpecificErrorCode()); |
| 4966 | } |
| 4967 | |
| 4968 | // Examines whether IP rules for app default network with subsidiary priorities are correctly added |
| 4969 | // and removed. |
| 4970 | TEST_F(NetdBinderTest, UidRangeSubPriority_VerifyPhysicalNwIpRules) { |
| 4971 | createPhysicalNetwork(TEST_NETID1, sTun.name()); |
| 4972 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID1, sTun.name(), "::/0", "").isOk()); |
| 4973 | createPhysicalNetwork(TEST_NETID2, sTun2.name()); |
| 4974 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID2, sTun2.name(), "::/0", "").isOk()); |
| 4975 | |
| 4976 | // Adds priority 1 setting |
| 4977 | NativeUidRangeConfig uidRangeConfig1 = makeNativeUidRangeConfig( |
| 4978 | TEST_NETID1, {makeUidRangeParcel(BASE_UID, BASE_UID)}, SUB_PRIORITY_1); |
| 4979 | EXPECT_TRUE(mNetd->networkAddUidRangesParcel(uidRangeConfig1).isOk()); |
| 4980 | verifyAppUidRules({true}, uidRangeConfig1, sTun.name()); |
| 4981 | // Adds priority 2 setting |
| 4982 | NativeUidRangeConfig uidRangeConfig2 = makeNativeUidRangeConfig( |
| 4983 | TEST_NETID2, {makeUidRangeParcel(BASE_UID + 1, BASE_UID + 1)}, SUB_PRIORITY_2); |
| 4984 | EXPECT_TRUE(mNetd->networkAddUidRangesParcel(uidRangeConfig2).isOk()); |
| 4985 | verifyAppUidRules({true}, uidRangeConfig2, sTun2.name()); |
| 4986 | // Adds another priority 2 setting |
| 4987 | NativeUidRangeConfig uidRangeConfig3 = makeNativeUidRangeConfig( |
| 4988 | INetd::UNREACHABLE_NET_ID, {makeUidRangeParcel(BASE_UID + 2, BASE_UID + 2)}, |
| 4989 | SUB_PRIORITY_2); |
| 4990 | EXPECT_TRUE(mNetd->networkAddUidRangesParcel(uidRangeConfig3).isOk()); |
| 4991 | verifyAppUidRules({true}, uidRangeConfig3, ""); |
| 4992 | |
| 4993 | // Removes. |
| 4994 | EXPECT_TRUE(mNetd->networkRemoveUidRangesParcel(uidRangeConfig1).isOk()); |
| 4995 | verifyAppUidRules({false}, uidRangeConfig1, sTun.name()); |
| 4996 | verifyAppUidRules({true}, uidRangeConfig2, sTun2.name()); |
| 4997 | verifyAppUidRules({true}, uidRangeConfig3, ""); |
| 4998 | EXPECT_TRUE(mNetd->networkRemoveUidRangesParcel(uidRangeConfig2).isOk()); |
| 4999 | verifyAppUidRules({false}, uidRangeConfig1, sTun.name()); |
| 5000 | verifyAppUidRules({false}, uidRangeConfig2, sTun2.name()); |
| 5001 | verifyAppUidRules({true}, uidRangeConfig3, ""); |
| 5002 | EXPECT_TRUE(mNetd->networkRemoveUidRangesParcel(uidRangeConfig3).isOk()); |
| 5003 | verifyAppUidRules({false}, uidRangeConfig1, sTun.name()); |
| 5004 | verifyAppUidRules({false}, uidRangeConfig2, sTun2.name()); |
| 5005 | verifyAppUidRules({false}, uidRangeConfig3, ""); |
| 5006 | } |
| 5007 | |
| 5008 | // Verify uid range rules on virtual network. |
| 5009 | TEST_P(VpnParameterizedTest, UidRangeSubPriority_VerifyVpnIpRules) { |
| 5010 | const bool isSecureVPN = GetParam(); |
| 5011 | constexpr int VPN_NETID2 = TEST_NETID2; |
| 5012 | |
| 5013 | // Create 2 VPNs, using sTun and sTun2. |
| 5014 | auto config = makeNativeNetworkConfig(VPN_NETID, NativeNetworkType::VIRTUAL, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 5015 | INetd::PERMISSION_NONE, isSecureVPN, false); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 5016 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
| 5017 | EXPECT_TRUE(mNetd->networkAddInterface(VPN_NETID, sTun.name()).isOk()); |
| 5018 | |
| 5019 | config = makeNativeNetworkConfig(VPN_NETID2, NativeNetworkType::VIRTUAL, INetd::PERMISSION_NONE, |
Chiachang Wang | 6dd4615 | 2022-01-12 10:02:32 +0800 | [diff] [blame] | 5020 | isSecureVPN, false); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 5021 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
| 5022 | EXPECT_TRUE(mNetd->networkAddInterface(VPN_NETID2, sTun2.name()).isOk()); |
| 5023 | |
| 5024 | // Assign uid ranges to different VPNs. Check if rules match. |
| 5025 | NativeUidRangeConfig uidRangeConfig1 = makeNativeUidRangeConfig( |
Patrick Rohr | e2f1b5a | 2022-01-25 21:36:50 +0100 | [diff] [blame] | 5026 | VPN_NETID, {makeUidRangeParcel(BASE_UID, BASE_UID)}, UidRanges::SUB_PRIORITY_HIGHEST); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 5027 | EXPECT_TRUE(mNetd->networkAddUidRangesParcel(uidRangeConfig1).isOk()); |
Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 5028 | verifyVpnUidRules({true}, uidRangeConfig1, sTun.name(), isSecureVPN, false); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 5029 | |
| 5030 | NativeUidRangeConfig uidRangeConfig2 = |
| 5031 | makeNativeUidRangeConfig(VPN_NETID2, {makeUidRangeParcel(BASE_UID + 1, BASE_UID + 1)}, |
Patrick Rohr | e2f1b5a | 2022-01-25 21:36:50 +0100 | [diff] [blame] | 5032 | UidRanges::SUB_PRIORITY_HIGHEST); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 5033 | EXPECT_TRUE(mNetd->networkAddUidRangesParcel(uidRangeConfig2).isOk()); |
Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 5034 | verifyVpnUidRules({true}, uidRangeConfig2, sTun2.name(), isSecureVPN, false); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 5035 | |
| 5036 | // Remove uid configs one-by-one. Check if rules match. |
| 5037 | EXPECT_TRUE(mNetd->networkRemoveUidRangesParcel(uidRangeConfig1).isOk()); |
Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 5038 | verifyVpnUidRules({false}, uidRangeConfig1, sTun.name(), isSecureVPN, false); |
| 5039 | verifyVpnUidRules({true}, uidRangeConfig2, sTun2.name(), isSecureVPN, false); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 5040 | EXPECT_TRUE(mNetd->networkRemoveUidRangesParcel(uidRangeConfig2).isOk()); |
Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 5041 | verifyVpnUidRules({false}, uidRangeConfig1, sTun.name(), isSecureVPN, false); |
| 5042 | verifyVpnUidRules({false}, uidRangeConfig2, sTun2.name(), isSecureVPN, false); |
| 5043 | } |
| 5044 | |
| 5045 | // Verify VPN ip rule on bypassable/secureVPN virtual network with local routes excluded |
| 5046 | TEST_P(VpnParameterizedTest, VerifyVpnIpRules_excludeLocalRoutes) { |
| 5047 | const bool isSecureVPN = GetParam(); |
| 5048 | // Create VPN with local route excluded |
| 5049 | auto config = makeNativeNetworkConfig(VPN_NETID, NativeNetworkType::VIRTUAL, |
| 5050 | INetd::PERMISSION_NONE, isSecureVPN, true); |
| 5051 | EXPECT_TRUE(mNetd->networkCreate(config).isOk()); |
| 5052 | EXPECT_TRUE(mNetd->networkAddInterface(VPN_NETID, sTun.name()).isOk()); |
| 5053 | |
| 5054 | // Assign uid ranges to VPN. Check if rules match. |
| 5055 | NativeUidRangeConfig uidRangeConfig1 = makeNativeUidRangeConfig( |
Patrick Rohr | e2f1b5a | 2022-01-25 21:36:50 +0100 | [diff] [blame] | 5056 | VPN_NETID, {makeUidRangeParcel(BASE_UID, BASE_UID)}, UidRanges::SUB_PRIORITY_HIGHEST); |
Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 5057 | EXPECT_TRUE(mNetd->networkAddUidRangesParcel(uidRangeConfig1).isOk()); |
| 5058 | verifyVpnUidRules({true}, uidRangeConfig1, sTun.name(), isSecureVPN, true); |
| 5059 | |
| 5060 | // Remove uid configs. Check if rules match. |
| 5061 | EXPECT_TRUE(mNetd->networkRemoveUidRangesParcel(uidRangeConfig1).isOk()); |
| 5062 | verifyVpnUidRules({false}, uidRangeConfig1, sTun.name(), isSecureVPN, true); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 5063 | } |
| 5064 | |
| 5065 | // Verify if packets go through the right network when subsidiary priority and VPN works together. |
| 5066 | // |
| 5067 | // Test config: |
| 5068 | // +----------+------------------------+-------------------------------------------+ |
| 5069 | // | Priority | UID | Assigned Network | |
| 5070 | // +----------+------------------------+-------------------------------------------+ |
| 5071 | // | 0 | TEST_UID1 | VPN bypassable (VPN_NETID) | |
| 5072 | // +----------+------------------------+-------------------------------------------+ |
| 5073 | // | 1 | TEST_UID1, TEST_UID2, | Physical Network 1 (APP_DEFAULT_1_NETID) | |
| 5074 | // | 1 | TEST_UID3 | Physical Network 2 (APP_DEFAULT_2_NETID) | |
| 5075 | // | 1 | TEST_UID5 | Unreachable Network (UNREACHABLE_NET_ID) | |
| 5076 | // +----------+------------------------+-------------------------------------------+ |
| 5077 | // | 2 | TEST_UID3 | Physical Network 1 (APP_DEFAULT_1_NETID) | |
| 5078 | // | 2 | TEST_UID4, TEST_UID5 | Physical Network 2 (APP_DEFAULT_2_NETID) | |
| 5079 | // +----------+------------------------+-------------------------------------------+ |
| 5080 | // |
| 5081 | // Expected results: |
| 5082 | // +-----------+------------------------+ |
| 5083 | // | UID | Using Network | |
| 5084 | // +-----------+------------------------+ |
| 5085 | // | TEST_UID1 | VPN | |
| 5086 | // | TEST_UID2 | Physical Network 1 | |
| 5087 | // | TEST_UID3 | Physical Network 2 | |
| 5088 | // | TEST_UID4 | Physical Network 2 | |
| 5089 | // | TEST_UID5 | Unreachable Network | |
| 5090 | // | TEST_UID6 | System Default Network | |
| 5091 | // +-----------+------------------------+ |
| 5092 | // |
| 5093 | // SYSTEM_DEFAULT_NETID uses sTun. |
| 5094 | // APP_DEFAULT_1_NETID uses sTun2. |
| 5095 | // VPN_NETID uses sTun3. |
| 5096 | // APP_DEFAULT_2_NETID uses sTun4. |
| 5097 | // |
| 5098 | TEST_F(NetdBinderTest, UidRangeSubPriority_ImplicitlySelectNetwork) { |
| 5099 | constexpr int APP_DEFAULT_1_NETID = TEST_NETID2; |
| 5100 | constexpr int APP_DEFAULT_2_NETID = TEST_NETID4; |
| 5101 | |
Ken Chen | a43e7ce | 2021-07-08 17:09:42 +0800 | [diff] [blame] | 5102 | static const struct TestData { |
| 5103 | uint32_t subPriority; |
| 5104 | std::vector<UidRangeParcel> uidRanges; |
| 5105 | unsigned int netId; |
| 5106 | } kTestData[] = { |
Patrick Rohr | e2f1b5a | 2022-01-25 21:36:50 +0100 | [diff] [blame] | 5107 | {UidRanges::SUB_PRIORITY_HIGHEST, {makeUidRangeParcel(TEST_UID1)}, VPN_NETID}, |
Ken Chen | a43e7ce | 2021-07-08 17:09:42 +0800 | [diff] [blame] | 5108 | {SUB_PRIORITY_1, |
| 5109 | {makeUidRangeParcel(TEST_UID1), makeUidRangeParcel(TEST_UID2)}, |
| 5110 | APP_DEFAULT_1_NETID}, |
| 5111 | {SUB_PRIORITY_1, {makeUidRangeParcel(TEST_UID3)}, APP_DEFAULT_2_NETID}, |
| 5112 | {SUB_PRIORITY_1, {makeUidRangeParcel(TEST_UID5)}, INetd::UNREACHABLE_NET_ID}, |
| 5113 | {SUB_PRIORITY_2, {makeUidRangeParcel(TEST_UID3)}, APP_DEFAULT_1_NETID}, |
| 5114 | {SUB_PRIORITY_2, |
| 5115 | {makeUidRangeParcel(TEST_UID4), makeUidRangeParcel(TEST_UID5)}, |
| 5116 | APP_DEFAULT_2_NETID}, |
| 5117 | }; |
| 5118 | |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 5119 | // Creates 4 networks. |
| 5120 | createVpnAndOtherPhysicalNetwork(SYSTEM_DEFAULT_NETID, APP_DEFAULT_1_NETID, VPN_NETID, |
| 5121 | /*isSecureVPN=*/false); |
| 5122 | createPhysicalNetwork(APP_DEFAULT_2_NETID, sTun4.name()); |
| 5123 | EXPECT_TRUE(mNetd->networkAddRoute(APP_DEFAULT_2_NETID, sTun4.name(), "::/0", "").isOk()); |
| 5124 | |
Ken Chen | a43e7ce | 2021-07-08 17:09:42 +0800 | [diff] [blame] | 5125 | for (const auto& td : kTestData) { |
| 5126 | NativeUidRangeConfig uidRangeConfig = |
| 5127 | makeNativeUidRangeConfig(td.netId, td.uidRanges, td.subPriority); |
| 5128 | EXPECT_TRUE(mNetd->networkAddUidRangesParcel(uidRangeConfig).isOk()); |
| 5129 | } |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 5130 | |
| 5131 | int systemDefaultFd = sTun.getFdForTesting(); |
| 5132 | int appDefault_1_Fd = sTun2.getFdForTesting(); |
| 5133 | int vpnFd = sTun3.getFdForTesting(); |
| 5134 | int appDefault_2_Fd = sTun4.getFdForTesting(); |
| 5135 | // Verify routings. |
| 5136 | expectPacketSentOnNetId(TEST_UID1, VPN_NETID, vpnFd, IMPLICITLY_SELECT); |
| 5137 | expectPacketSentOnNetId(TEST_UID2, APP_DEFAULT_1_NETID, appDefault_1_Fd, IMPLICITLY_SELECT); |
| 5138 | expectPacketSentOnNetId(TEST_UID3, APP_DEFAULT_2_NETID, appDefault_2_Fd, IMPLICITLY_SELECT); |
| 5139 | expectPacketSentOnNetId(TEST_UID4, APP_DEFAULT_2_NETID, appDefault_2_Fd, IMPLICITLY_SELECT); |
| 5140 | expectUnreachableError(TEST_UID5, INetd::UNREACHABLE_NET_ID, IMPLICITLY_SELECT); |
| 5141 | expectPacketSentOnNetId(TEST_UID6, SYSTEM_DEFAULT_NETID, systemDefaultFd, IMPLICITLY_SELECT); |
| 5142 | |
| 5143 | // Remove test rules from the unreachable network. |
Ken Chen | a43e7ce | 2021-07-08 17:09:42 +0800 | [diff] [blame] | 5144 | for (const auto& td : kTestData) { |
| 5145 | if (td.netId == INetd::UNREACHABLE_NET_ID) { |
| 5146 | NativeUidRangeConfig uidRangeConfig = |
| 5147 | makeNativeUidRangeConfig(td.netId, td.uidRanges, td.subPriority); |
| 5148 | EXPECT_TRUE(mNetd->networkRemoveUidRangesParcel(uidRangeConfig).isOk()); |
| 5149 | } |
| 5150 | } |
Taras Antoshchuk | 35987de | 2021-09-21 17:45:15 +0200 | [diff] [blame] | 5151 | } |
Patrick Rohr | e6f198c | 2022-01-25 13:50:31 +0100 | [diff] [blame] | 5152 | |
| 5153 | class PerAppNetworkPermissionsTest : public NetdBinderTest { |
| 5154 | public: |
| 5155 | int bindSocketToNetwork(int sock, int netId, bool explicitlySelected) { |
| 5156 | ScopedUidChange uidChange(AID_ROOT); |
| 5157 | Fwmark fwmark; |
| 5158 | fwmark.explicitlySelected = explicitlySelected; |
| 5159 | fwmark.netId = netId; |
| 5160 | return setsockopt(sock, SOL_SOCKET, SO_MARK, &(fwmark.intValue), sizeof(fwmark.intValue)); |
| 5161 | } |
| 5162 | |
| 5163 | void changeNetworkPermissionForUid(int netId, int uid, bool add) { |
| 5164 | auto nativeUidRangeConfig = makeNativeUidRangeConfig(netId, {makeUidRangeParcel(uid, uid)}, |
| 5165 | UidRanges::SUB_PRIORITY_NO_DEFAULT); |
| 5166 | ScopedUidChange rootUid(AID_ROOT); |
| 5167 | if (add) { |
| 5168 | EXPECT_TRUE(mNetd->networkAddUidRangesParcel(nativeUidRangeConfig).isOk()); |
| 5169 | } else { |
| 5170 | EXPECT_TRUE(mNetd->networkRemoveUidRangesParcel(nativeUidRangeConfig).isOk()); |
| 5171 | } |
| 5172 | } |
| 5173 | |
| 5174 | protected: |
| 5175 | static inline const sockaddr_in6 TEST_SOCKADDR_IN6 = { |
| 5176 | .sin6_family = AF_INET6, |
| 5177 | .sin6_port = 42, |
| 5178 | .sin6_addr = V6_ADDR, |
| 5179 | }; |
| 5180 | std::array<char, 4096> mTestBuf; |
| 5181 | }; |
| 5182 | |
| 5183 | TEST_F(PerAppNetworkPermissionsTest, HasExplicitAccess) { |
| 5184 | // TEST_NETID1 -> restricted network |
| 5185 | createPhysicalNetwork(TEST_NETID1, sTun.name(), INetd::PERMISSION_SYSTEM); |
| 5186 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID1, sTun.name(), "::/0", "").isOk()); |
| 5187 | |
| 5188 | // Change uid to uid without PERMISSION_SYSTEM |
| 5189 | ScopedUidChange testUid(TEST_UID1); |
| 5190 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 5191 | EXPECT_EQ(bindSocketToNetwork(sock, TEST_NETID1, true /*explicitlySelected*/), 0); |
| 5192 | |
| 5193 | // Test without permissions should fail |
| 5194 | EXPECT_EQ(connect(sock, (sockaddr*)&TEST_SOCKADDR_IN6, sizeof(TEST_SOCKADDR_IN6)), -1); |
| 5195 | |
| 5196 | // Test access with permission succeeds and packet is routed correctly |
| 5197 | changeNetworkPermissionForUid(TEST_NETID1, TEST_UID1, true /*add*/); |
| 5198 | EXPECT_EQ(connect(sock, (sockaddr*)&TEST_SOCKADDR_IN6, sizeof(TEST_SOCKADDR_IN6)), 0); |
| 5199 | EXPECT_EQ(send(sock, "foo", sizeof("foo"), 0), (int)sizeof("foo")); |
| 5200 | EXPECT_GT(read(sTun.getFdForTesting(), mTestBuf.data(), mTestBuf.size()), 0); |
| 5201 | |
| 5202 | // Test removing permissions. |
| 5203 | // Note: Send will still succeed as the destination is cached in |
| 5204 | // sock.sk_dest_cache. Try another connect instead. |
| 5205 | changeNetworkPermissionForUid(TEST_NETID1, TEST_UID1, false /*add*/); |
| 5206 | EXPECT_EQ(-1, connect(sock, (sockaddr*)&TEST_SOCKADDR_IN6, sizeof(TEST_SOCKADDR_IN6))); |
| 5207 | } |
| 5208 | |
| 5209 | TEST_F(PerAppNetworkPermissionsTest, HasImplicitAccess) { |
| 5210 | // TEST_NETID1 -> restricted network |
| 5211 | createPhysicalNetwork(TEST_NETID1, sTun.name(), INetd::PERMISSION_SYSTEM); |
| 5212 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID1, sTun.name(), "::/0", "").isOk()); |
| 5213 | |
| 5214 | // Change uid to uid without PERMISSION_SYSTEM |
| 5215 | ScopedUidChange testUid(TEST_UID1); |
| 5216 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 5217 | EXPECT_EQ(bindSocketToNetwork(sock, TEST_NETID1, false /*explicitlySelected*/), 0); |
| 5218 | |
| 5219 | // Note: we cannot call connect() when implicitly selecting the network as |
| 5220 | // the fwmark would get reset to the default network. |
| 5221 | // Call connect which should bind socket to default network |
| 5222 | EXPECT_EQ(sendto(sock, "foo", sizeof("foo"), 0, (sockaddr*)&TEST_SOCKADDR_IN6, |
| 5223 | sizeof(TEST_SOCKADDR_IN6)), |
| 5224 | -1); |
| 5225 | |
| 5226 | // Test access with permission succeeds and packet is routed correctly |
| 5227 | changeNetworkPermissionForUid(TEST_NETID1, TEST_UID1, true /*add*/); |
| 5228 | EXPECT_EQ(sendto(sock, "foo", sizeof("foo"), 0, (sockaddr*)&TEST_SOCKADDR_IN6, |
| 5229 | sizeof(TEST_SOCKADDR_IN6)), |
| 5230 | (int)sizeof("foo")); |
| 5231 | EXPECT_GT(read(sTun.getFdForTesting(), mTestBuf.data(), mTestBuf.size()), 0); |
| 5232 | } |
| 5233 | |
| 5234 | TEST_F(PerAppNetworkPermissionsTest, DoesNotAffectDefaultNetworkSelection) { |
| 5235 | // TEST_NETID1 -> default network |
| 5236 | // TEST_NETID2 -> restricted network |
| 5237 | createPhysicalNetwork(TEST_NETID1, sTun.name(), INetd::PERMISSION_NONE); |
| 5238 | createPhysicalNetwork(TEST_NETID2, sTun2.name(), INetd::PERMISSION_SYSTEM); |
| 5239 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID1, sTun.name(), "::/0", "").isOk()); |
| 5240 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID2, sTun2.name(), "::/0", "").isOk()); |
| 5241 | mNetd->networkSetDefault(TEST_NETID1); |
| 5242 | |
| 5243 | changeNetworkPermissionForUid(TEST_NETID2, TEST_UID1, true /*add*/); |
| 5244 | |
| 5245 | // Change uid to uid without PERMISSION_SYSTEM |
| 5246 | ScopedUidChange testUid(TEST_UID1); |
| 5247 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 5248 | |
| 5249 | // Connect should select default network |
| 5250 | EXPECT_EQ(connect(sock, (sockaddr*)&TEST_SOCKADDR_IN6, sizeof(TEST_SOCKADDR_IN6)), 0); |
| 5251 | EXPECT_EQ(send(sock, "foo", sizeof("foo"), 0), (int)sizeof("foo")); |
| 5252 | EXPECT_GT(read(sTun.getFdForTesting(), mTestBuf.data(), mTestBuf.size()), 0); |
| 5253 | } |
| 5254 | |
| 5255 | TEST_F(PerAppNetworkPermissionsTest, PermissionDoesNotAffectPerAppDefaultNetworkSelection) { |
| 5256 | // TEST_NETID1 -> restricted app default network |
| 5257 | // TEST_NETID2 -> restricted network |
| 5258 | createPhysicalNetwork(TEST_NETID1, sTun.name(), INetd::PERMISSION_SYSTEM); |
| 5259 | createPhysicalNetwork(TEST_NETID2, sTun2.name(), INetd::PERMISSION_SYSTEM); |
| 5260 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID1, sTun.name(), "::/0", "").isOk()); |
| 5261 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID2, sTun2.name(), "::/0", "").isOk()); |
| 5262 | |
| 5263 | auto nativeUidRangeConfig = makeNativeUidRangeConfig( |
| 5264 | TEST_NETID1, {makeUidRangeParcel(TEST_UID1, TEST_UID1)}, 0 /*subPriority*/); |
| 5265 | EXPECT_TRUE(mNetd->networkAddUidRangesParcel(nativeUidRangeConfig).isOk()); |
| 5266 | changeNetworkPermissionForUid(TEST_NETID2, TEST_UID1, true /*add*/); |
| 5267 | |
| 5268 | // Change uid to uid without PERMISSION_SYSTEM |
| 5269 | ScopedUidChange testUid(TEST_UID1); |
| 5270 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 5271 | |
| 5272 | // Connect should select app default network |
| 5273 | EXPECT_EQ(connect(sock, (sockaddr*)&TEST_SOCKADDR_IN6, sizeof(TEST_SOCKADDR_IN6)), 0); |
| 5274 | EXPECT_EQ(send(sock, "foo", sizeof("foo"), 0), (int)sizeof("foo")); |
| 5275 | EXPECT_GT(read(sTun.getFdForTesting(), mTestBuf.data(), mTestBuf.size()), 0); |
| 5276 | } |
| 5277 | |
| 5278 | TEST_F(PerAppNetworkPermissionsTest, PermissionOnlyAffectsUid) { |
| 5279 | // TEST_NETID1 -> restricted network |
| 5280 | // TEST_NETID2 -> restricted network |
| 5281 | createPhysicalNetwork(TEST_NETID1, sTun.name(), INetd::PERMISSION_SYSTEM); |
| 5282 | createPhysicalNetwork(TEST_NETID2, sTun2.name(), INetd::PERMISSION_SYSTEM); |
| 5283 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID1, sTun.name(), "::/0", "").isOk()); |
| 5284 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID2, sTun2.name(), "::/0", "").isOk()); |
| 5285 | |
| 5286 | // test that neither TEST_UID1, nor TEST_UID2 have access without permission |
| 5287 | { |
| 5288 | // TEST_UID1 |
| 5289 | ScopedUidChange testUid(TEST_UID1); |
| 5290 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 5291 | // TEST_NETID1 |
| 5292 | EXPECT_EQ(bindSocketToNetwork(sock, TEST_NETID1, true /*explicitlySelected*/), 0); |
| 5293 | EXPECT_EQ(connect(sock, (sockaddr*)&TEST_SOCKADDR_IN6, sizeof(TEST_SOCKADDR_IN6)), -1); |
| 5294 | // TEST_NETID2 |
| 5295 | EXPECT_EQ(bindSocketToNetwork(sock, TEST_NETID2, true /*explicitlySelected*/), 0); |
| 5296 | EXPECT_EQ(connect(sock, (sockaddr*)&TEST_SOCKADDR_IN6, sizeof(TEST_SOCKADDR_IN6)), -1); |
| 5297 | } |
| 5298 | { |
| 5299 | // TEST_UID2 |
| 5300 | ScopedUidChange testUid(TEST_UID2); |
| 5301 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 5302 | // TEST_NETID1 |
| 5303 | EXPECT_EQ(bindSocketToNetwork(sock, TEST_NETID1, true /*explicitlySelected*/), 0); |
| 5304 | EXPECT_EQ(connect(sock, (sockaddr*)&TEST_SOCKADDR_IN6, sizeof(TEST_SOCKADDR_IN6)), -1); |
| 5305 | // TEST_NETID2 |
| 5306 | EXPECT_EQ(bindSocketToNetwork(sock, TEST_NETID2, true /*explicitlySelected*/), 0); |
| 5307 | EXPECT_EQ(connect(sock, (sockaddr*)&TEST_SOCKADDR_IN6, sizeof(TEST_SOCKADDR_IN6)), -1); |
| 5308 | } |
| 5309 | |
| 5310 | changeNetworkPermissionForUid(TEST_NETID1, TEST_UID1, true); |
| 5311 | |
| 5312 | // test that TEST_UID1 has access to TEST_UID1 |
| 5313 | { |
| 5314 | // TEST_UID1 |
| 5315 | ScopedUidChange testUid(TEST_UID1); |
| 5316 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 5317 | // TEST_NETID1 |
| 5318 | EXPECT_EQ(bindSocketToNetwork(sock, TEST_NETID1, true /*explicitlySelected*/), 0); |
| 5319 | EXPECT_EQ(connect(sock, (sockaddr*)&TEST_SOCKADDR_IN6, sizeof(TEST_SOCKADDR_IN6)), 0); |
| 5320 | // TEST_NETID2 |
| 5321 | EXPECT_EQ(bindSocketToNetwork(sock, TEST_NETID2, true /*explicitlySelected*/), 0); |
| 5322 | EXPECT_EQ(connect(sock, (sockaddr*)&TEST_SOCKADDR_IN6, sizeof(TEST_SOCKADDR_IN6)), -1); |
| 5323 | } |
| 5324 | { |
| 5325 | // TEST_UID2 |
| 5326 | ScopedUidChange testUid(TEST_UID2); |
| 5327 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 5328 | // TEST_NETID1 |
| 5329 | EXPECT_EQ(bindSocketToNetwork(sock, TEST_NETID1, true /*explicitlySelected*/), 0); |
| 5330 | EXPECT_EQ(connect(sock, (sockaddr*)&TEST_SOCKADDR_IN6, sizeof(TEST_SOCKADDR_IN6)), -1); |
| 5331 | // TEST_NETID2 |
| 5332 | EXPECT_EQ(bindSocketToNetwork(sock, TEST_NETID2, true /*explicitlySelected*/), 0); |
| 5333 | EXPECT_EQ(connect(sock, (sockaddr*)&TEST_SOCKADDR_IN6, sizeof(TEST_SOCKADDR_IN6)), -1); |
| 5334 | } |
| 5335 | } |
paulhu | 7274295 | 2022-02-09 21:24:09 +0800 | [diff] [blame] | 5336 | |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 5337 | // Creates a system default network and 3 enterprise networks for two profiles. Check if network |
| 5338 | // selection in compliance with network allow list settings. |
| 5339 | // |
| 5340 | // +-----------+-----------------------+----------------------------------------+ |
| 5341 | // | UID | UID's default network | UID can select networks | |
| 5342 | // +-----------+-----------------------+----------------------------------------+ |
| 5343 | // | TEST_UID1 | ENTERPRISE_NETID_1 | ENTERPRISE_NETID_1, ENTERPRISE_NETID_2 | |
| 5344 | // | TEST_UID2 | ENTERPRISE_NETID_3 | ENTERPRISE_NETID_3 | |
| 5345 | // +-----------+-----------------------+----------------------------------------+ |
| 5346 | TEST_F(NetdBinderTest, PerProfileNetworkPermission) { |
| 5347 | // creates 4 networks |
| 5348 | createDefaultAndOtherPhysicalNetwork(SYSTEM_DEFAULT_NETID, ENTERPRISE_NETID_1); |
| 5349 | createPhysicalNetwork(ENTERPRISE_NETID_2, sTun3.name()); |
| 5350 | EXPECT_TRUE(mNetd->networkAddRoute(ENTERPRISE_NETID_2, sTun3.name(), "::/0", "").isOk()); |
| 5351 | createPhysicalNetwork(ENTERPRISE_NETID_3, sTun4.name()); |
| 5352 | EXPECT_TRUE(mNetd->networkAddRoute(ENTERPRISE_NETID_3, sTun4.name(), "::/0", "").isOk()); |
| 5353 | |
| 5354 | // profile#1 |
Ken Chen | 04ee609 | 2022-12-26 17:35:33 +0800 | [diff] [blame] | 5355 | // UidRanges::SUB_PRIORITY_HIGHEST + 20 = PREFERENCE_ORDER_PROFILE, which is defined in |
| 5356 | // ConnectivityService.java. The value here doesn't really matter because user allowed network |
| 5357 | // does not depends on specific sub-priority. |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 5358 | NativeUidRangeConfig cfg1 = |
| 5359 | makeNativeUidRangeConfig(ENTERPRISE_NETID_1, {makeUidRangeParcel(TEST_UID1, TEST_UID1)}, |
| 5360 | UidRanges::SUB_PRIORITY_HIGHEST + 20); |
| 5361 | EXPECT_TRUE(mNetd->networkAddUidRangesParcel(cfg1).isOk()); |
| 5362 | |
| 5363 | // profile#2 |
| 5364 | NativeUidRangeConfig cfg2 = |
| 5365 | makeNativeUidRangeConfig(ENTERPRISE_NETID_3, {makeUidRangeParcel(TEST_UID2, TEST_UID2)}, |
| 5366 | UidRanges::SUB_PRIORITY_HIGHEST + 20); |
| 5367 | EXPECT_TRUE(mNetd->networkAddUidRangesParcel(cfg2).isOk()); |
| 5368 | |
| 5369 | // setNetworkAllowlist at once |
| 5370 | // all uids except for TEST_UID2 |
| 5371 | NativeUidRangeConfig nw1UserConfig = makeNativeUidRangeConfig( |
| 5372 | ENTERPRISE_NETID_1, |
| 5373 | {makeUidRangeParcel(0, TEST_UID3), makeUidRangeParcel(TEST_UID1, TEST_UID1)}, |
| 5374 | /*unused*/ 0); |
| 5375 | NativeUidRangeConfig nw2UserConfig = makeNativeUidRangeConfig( |
| 5376 | ENTERPRISE_NETID_2, |
| 5377 | {makeUidRangeParcel(0, TEST_UID3), makeUidRangeParcel(TEST_UID1, TEST_UID1)}, |
| 5378 | /*unused*/ 0); |
| 5379 | // all uids except for TEST_UID1 |
| 5380 | NativeUidRangeConfig nw3UserConfig = makeNativeUidRangeConfig( |
| 5381 | ENTERPRISE_NETID_3, {makeUidRangeParcel(0, TEST_UID2)}, /*unused*/ 0); |
| 5382 | // all uids except for TEST_UID1 and TEST_UID2 |
| 5383 | NativeUidRangeConfig nwDefaultUserConfig = makeNativeUidRangeConfig( |
| 5384 | SYSTEM_DEFAULT_NETID, {makeUidRangeParcel(0, TEST_UID3)}, /*unused*/ 0); |
| 5385 | EXPECT_TRUE(mNetd->setNetworkAllowlist( |
| 5386 | {nw1UserConfig, nw2UserConfig, nw3UserConfig, nwDefaultUserConfig}) |
| 5387 | .isOk()); |
| 5388 | |
| 5389 | { // Can set network for process on allowed networks. |
| 5390 | ScopedUidChange scopedUidChange(TEST_UID1); |
| 5391 | EXPECT_EQ(0, setNetworkForProcess(ENTERPRISE_NETID_1)); |
| 5392 | EXPECT_EQ(0, setNetworkForProcess(ENTERPRISE_NETID_2)); |
| 5393 | // Can not set network for process on not allowed networks. |
| 5394 | EXPECT_EQ(-EACCES, setNetworkForProcess(SYSTEM_DEFAULT_NETID)); |
| 5395 | EXPECT_EQ(-EACCES, setNetworkForProcess(ENTERPRISE_NETID_3)); |
| 5396 | } |
| 5397 | { // Can set network for process on allowed networks. |
| 5398 | ScopedUidChange scopedUidChange(TEST_UID2); |
| 5399 | EXPECT_EQ(0, setNetworkForProcess(ENTERPRISE_NETID_3)); |
| 5400 | // Can not set network for process on not allowed networks. |
| 5401 | EXPECT_EQ(-EACCES, setNetworkForProcess(SYSTEM_DEFAULT_NETID)); |
| 5402 | EXPECT_EQ(-EACCES, setNetworkForProcess(ENTERPRISE_NETID_1)); |
| 5403 | EXPECT_EQ(-EACCES, setNetworkForProcess(ENTERPRISE_NETID_2)); |
| 5404 | } |
| 5405 | { // Root can use whatever network it wants. |
| 5406 | ScopedUidChange scopedUidChange(AID_ROOT); |
| 5407 | EXPECT_EQ(0, setNetworkForProcess(SYSTEM_DEFAULT_NETID)); |
| 5408 | EXPECT_EQ(0, setNetworkForProcess(ENTERPRISE_NETID_1)); |
| 5409 | EXPECT_EQ(0, setNetworkForProcess(ENTERPRISE_NETID_2)); |
| 5410 | EXPECT_EQ(0, setNetworkForProcess(ENTERPRISE_NETID_3)); |
| 5411 | } |
| 5412 | |
| 5413 | // Update setting: remove ENTERPRISE_NETID_2 from profile#1's allowed network list and add it to |
| 5414 | // profile#2's allowed network list. |
| 5415 | // +-----------+-----------------------+----------------------------------------+ |
| 5416 | // | UID | UID's default network | UID can select networks | |
| 5417 | // +-----------+-----------------------+----------------------------------------+ |
| 5418 | // | TEST_UID1 | ENTERPRISE_NETID_1 | ENTERPRISE_NETID_1 | |
| 5419 | // | TEST_UID2 | ENTERPRISE_NETID_3 | ENTERPRISE_NETID_2, ENTERPRISE_NETID_3 | |
| 5420 | // +-----------+-----------------------+----------------------------------------+ |
| 5421 | |
| 5422 | // all uids except for TEST_UID2 |
| 5423 | nw1UserConfig = makeNativeUidRangeConfig( |
| 5424 | ENTERPRISE_NETID_1, |
| 5425 | {makeUidRangeParcel(0, TEST_UID3), makeUidRangeParcel(TEST_UID1, TEST_UID1)}, |
| 5426 | /*unused*/ 0); |
| 5427 | // all uids except for TEST_UID1 |
| 5428 | nw2UserConfig = makeNativeUidRangeConfig(ENTERPRISE_NETID_2, {makeUidRangeParcel(0, TEST_UID2)}, |
| 5429 | /*unused*/ 0); |
| 5430 | nw3UserConfig = makeNativeUidRangeConfig(ENTERPRISE_NETID_3, {makeUidRangeParcel(0, TEST_UID2)}, |
| 5431 | /*unused*/ 0); |
| 5432 | // all uids except for TEST_UID1 and TEST_UID2 |
| 5433 | nwDefaultUserConfig = makeNativeUidRangeConfig( |
| 5434 | SYSTEM_DEFAULT_NETID, {makeUidRangeParcel(0, TEST_UID3)}, /*unused*/ 0); |
| 5435 | EXPECT_TRUE(mNetd->setNetworkAllowlist( |
| 5436 | {nw1UserConfig, nw2UserConfig, nw3UserConfig, nwDefaultUserConfig}) |
| 5437 | .isOk()); |
| 5438 | |
| 5439 | { |
| 5440 | ScopedUidChange scopedUidChange(TEST_UID1); |
| 5441 | EXPECT_EQ(0, setNetworkForProcess(ENTERPRISE_NETID_1)); |
| 5442 | EXPECT_EQ(-EACCES, setNetworkForProcess(SYSTEM_DEFAULT_NETID)); |
| 5443 | EXPECT_EQ(-EACCES, setNetworkForProcess(ENTERPRISE_NETID_2)); |
| 5444 | EXPECT_EQ(-EACCES, setNetworkForProcess(ENTERPRISE_NETID_3)); |
| 5445 | } |
| 5446 | { |
| 5447 | ScopedUidChange scopedUidChange(TEST_UID2); |
| 5448 | EXPECT_EQ(0, setNetworkForProcess(ENTERPRISE_NETID_2)); |
| 5449 | EXPECT_EQ(0, setNetworkForProcess(ENTERPRISE_NETID_3)); |
| 5450 | EXPECT_EQ(-EACCES, setNetworkForProcess(SYSTEM_DEFAULT_NETID)); |
| 5451 | EXPECT_EQ(-EACCES, setNetworkForProcess(ENTERPRISE_NETID_1)); |
| 5452 | } |
| 5453 | |
| 5454 | // UID not restricted by allowed list can select all networks. |
| 5455 | { |
| 5456 | ScopedUidChange scopedUidChange(TEST_UID3); |
| 5457 | EXPECT_EQ(0, setNetworkForProcess(ENTERPRISE_NETID_1)); |
| 5458 | EXPECT_EQ(0, setNetworkForProcess(SYSTEM_DEFAULT_NETID)); |
| 5459 | EXPECT_EQ(0, setNetworkForProcess(ENTERPRISE_NETID_2)); |
| 5460 | EXPECT_EQ(0, setNetworkForProcess(ENTERPRISE_NETID_3)); |
| 5461 | } |
Ken Chen | 04ee609 | 2022-12-26 17:35:33 +0800 | [diff] [blame] | 5462 | |
| 5463 | // Update setting: remove ENTERPRISE_NETID_1 from profile#1's allowed network list |
| 5464 | // +-----------+-----------------------+----------------------------------------+ |
| 5465 | // | UID | UID's default network | UID can select networks | |
| 5466 | // +-----------+-----------------------+----------------------------------------+ |
| 5467 | // | TEST_UID2 | ENTERPRISE_NETID_3 | ENTERPRISE_NETID_2, ENTERPRISE_NETID_3 | |
| 5468 | // +-----------+-----------------------+----------------------------------------+ |
| 5469 | EXPECT_TRUE( |
| 5470 | mNetd->setNetworkAllowlist({nw2UserConfig, nw3UserConfig, nwDefaultUserConfig}).isOk()); |
| 5471 | |
| 5472 | // All UIDs should be able to use ENTERPRISE_NETID_1. |
| 5473 | for (const int uid : {TEST_UID1, TEST_UID2, TEST_UID3}) { |
| 5474 | { |
| 5475 | ScopedUidChange scopedUidChange(uid); |
| 5476 | EXPECT_EQ(0, setNetworkForProcess(ENTERPRISE_NETID_1)); |
| 5477 | } |
| 5478 | } |
Ken Chen | d81356c | 2023-08-24 18:39:26 +0800 | [diff] [blame] | 5479 | } |
| 5480 | |
| 5481 | namespace { |
| 5482 | |
| 5483 | class ScopedIfaceRouteOperation { |
| 5484 | using IfaceCmd = std::tuple<int32_t, const std::string>; |
| 5485 | using RouteCmd = std::tuple<int32_t, const std::string, const std::string, const std::string>; |
| 5486 | |
| 5487 | // Helper type for the visitor. |
| 5488 | template <class... Ts> |
| 5489 | struct overloaded : Ts... { |
| 5490 | using Ts::operator()...; |
| 5491 | }; |
| 5492 | // Explicit deduction guide |
| 5493 | template <class... Ts> |
| 5494 | overloaded(Ts...) -> overloaded<Ts...>; |
| 5495 | |
| 5496 | public: |
| 5497 | ScopedIfaceRouteOperation(sp<INetd> netd) : mNetd(netd) {} |
| 5498 | |
| 5499 | binder::Status addInterface(int32_t netId, const std::string& iface) { |
| 5500 | const binder::Status status = mNetd->networkAddInterface(netId, iface); |
| 5501 | if (status.isOk()) { |
| 5502 | mCmds.push_back(std::make_tuple(netId, iface)); |
| 5503 | } |
| 5504 | return status; |
| 5505 | } |
| 5506 | |
| 5507 | binder::Status addRoute(int32_t netId, const std::string& iface, const std::string& destination, |
| 5508 | const std::string& nextHop) { |
| 5509 | const binder::Status status = mNetd->networkAddRoute(netId, iface, destination, nextHop); |
| 5510 | if (status.isOk()) { |
| 5511 | mCmds.push_back(std::make_tuple(netId, iface, destination, nextHop)); |
| 5512 | } |
| 5513 | return status; |
| 5514 | } |
| 5515 | |
| 5516 | ~ScopedIfaceRouteOperation() { |
| 5517 | // Remove routes and interfaces in reverse order. |
| 5518 | for (std::vector<std::variant<IfaceCmd, RouteCmd>>::reverse_iterator iter = mCmds.rbegin(); |
| 5519 | iter != mCmds.rend(); iter++) { |
| 5520 | // Do corresponding works according to the type of the command pointed by the iter. |
| 5521 | std::visit(overloaded{ |
| 5522 | [&](IfaceCmd& cmd) { |
| 5523 | mNetd->networkRemoveInterface(std::get<0>(cmd), |
| 5524 | std::get<1>(cmd)); |
| 5525 | }, |
| 5526 | [&](RouteCmd& cmd) { |
| 5527 | mNetd->networkRemoveRoute(std::get<0>(cmd), std::get<1>(cmd), |
| 5528 | std::get<2>(cmd), std::get<3>(cmd)); |
| 5529 | }, |
| 5530 | }, |
| 5531 | *iter); |
| 5532 | } |
| 5533 | } |
| 5534 | |
| 5535 | private: |
| 5536 | sp<INetd> mNetd; |
| 5537 | std::vector<std::variant<IfaceCmd, RouteCmd>> mCmds; |
| 5538 | }; |
| 5539 | |
| 5540 | std::optional<sockaddr_in6> getV6LinkLocalAddrFromIfIndex(const unsigned ifIndex) { |
| 5541 | struct ifaddrs* ifAddrList = nullptr; |
| 5542 | sockaddr_in6 linkLocalAddr{}; |
| 5543 | |
| 5544 | if (getifaddrs(&ifAddrList) == -1) return std::nullopt; |
| 5545 | |
| 5546 | for (struct ifaddrs* ifa = ifAddrList; ifa != nullptr; ifa = ifa->ifa_next) { |
| 5547 | if (ifa->ifa_addr != nullptr && ifa->ifa_addr->sa_family == AF_INET6) { |
| 5548 | struct sockaddr_in6* addr = reinterpret_cast<struct sockaddr_in6*>(ifa->ifa_addr); |
| 5549 | if (addr->sin6_scope_id == ifIndex && IN6_IS_ADDR_LINKLOCAL(&(addr->sin6_addr))) { |
| 5550 | linkLocalAddr = *addr; |
| 5551 | freeifaddrs(ifAddrList); |
| 5552 | return linkLocalAddr; |
| 5553 | } |
| 5554 | } |
| 5555 | } |
| 5556 | |
| 5557 | freeifaddrs(ifAddrList); |
| 5558 | return std::nullopt; |
| 5559 | } |
| 5560 | |
| 5561 | int retry_bind(int sockfd, struct sockaddr* addr, socklen_t addrlen) { |
| 5562 | int ret = 0; |
| 5563 | |
| 5564 | for (int retry = 0; retry < 10; retry++) { |
| 5565 | ret = bind(sockfd, addr, addrlen); |
| 5566 | if (ret == 0 || (ret == -1 && errno != EADDRNOTAVAIL)) { |
| 5567 | break; |
| 5568 | } |
| 5569 | usleep(100 * 1000); |
| 5570 | } |
| 5571 | return ret; |
| 5572 | } |
| 5573 | |
| 5574 | } // namespace |
| 5575 | |
| 5576 | TEST_F(NetdBinderTest, V6LinkLocalFwmark) { |
| 5577 | createAndSetDefaultNetwork(TEST_NETID1, sTun.name()); |
| 5578 | |
| 5579 | // Add an interface and route for Local network. |
| 5580 | ScopedIfaceRouteOperation scopedOperation(mNetd); |
| 5581 | EXPECT_TRUE(scopedOperation.addInterface(NetworkController::LOCAL_NET_ID, sTun2.name()).isOk()); |
| 5582 | EXPECT_TRUE( |
| 5583 | scopedOperation.addRoute(NetworkController::LOCAL_NET_ID, sTun2.name(), "fe80::/64", "") |
| 5584 | .isOk()); |
| 5585 | |
| 5586 | // Bind a listening socket to the auto assigned link-local address of the Local network. |
| 5587 | std::optional<sockaddr_in6> v6LinkLocalAddr_1 = getV6LinkLocalAddrFromIfIndex(sTun2.ifindex()); |
| 5588 | ASSERT_TRUE(v6LinkLocalAddr_1.has_value()) << "errno:" << errno; |
| 5589 | socklen_t len = sizeof(v6LinkLocalAddr_1.value()); |
| 5590 | unique_fd s1(socket(AF_INET6, SOCK_STREAM | SOCK_NONBLOCK, 0)); |
| 5591 | ASSERT_EQ(0, bind(s1, reinterpret_cast<sockaddr*>(&v6LinkLocalAddr_1.value()), len)) |
| 5592 | << "errno:" << errno; |
| 5593 | ASSERT_EQ(0, getsockname(s1, reinterpret_cast<sockaddr*>(&v6LinkLocalAddr_1.value()), &len)) |
| 5594 | << "errno:" << errno; |
| 5595 | ASSERT_EQ(0, listen(s1, 10)) << "errno:" << errno; |
| 5596 | |
| 5597 | // Add another v6 link-local address. |
| 5598 | const char* v6LinkLocalAddr_2 = "fe80::ace:d00d"; |
| 5599 | EXPECT_TRUE(mNetd->interfaceAddAddress(sTun2.name(), v6LinkLocalAddr_2, 64).isOk()); |
| 5600 | |
| 5601 | // Bind a client socket on the new added link-local address and connect it to the listen socket. |
| 5602 | // Have different src and dst addresses is needed because we want to test the behavior of fwmark |
| 5603 | // and destroying sockets. The same src and dst addresses are treated as loopbacks and won't be |
| 5604 | // destroyed in any way. |
| 5605 | const struct addrinfo hints = { |
Maciej Żenczykowski | 3d0b37e | 2023-10-22 11:06:01 -0700 | [diff] [blame] | 5606 | .ai_flags = AI_NUMERICHOST, |
Ken Chen | d81356c | 2023-08-24 18:39:26 +0800 | [diff] [blame] | 5607 | .ai_family = AF_INET6, |
| 5608 | .ai_socktype = SOCK_STREAM, |
Ken Chen | d81356c | 2023-08-24 18:39:26 +0800 | [diff] [blame] | 5609 | }; |
| 5610 | struct addrinfo* addrinfoList = nullptr; |
| 5611 | int ret = getaddrinfo(v6LinkLocalAddr_2, nullptr, &hints, &addrinfoList); |
| 5612 | ScopedAddrinfo addrinfoCleanup(addrinfoList); |
| 5613 | ASSERT_EQ(0, ret) << "errno:" << errno; |
| 5614 | |
| 5615 | len = addrinfoList[0].ai_addrlen; |
| 5616 | sockaddr_in6 sin6 = *reinterpret_cast<sockaddr_in6*>(addrinfoList[0].ai_addr); |
| 5617 | sin6.sin6_scope_id = sTun2.ifindex(); |
| 5618 | |
| 5619 | unique_fd c1(socket(AF_INET6, SOCK_STREAM, 0)); |
| 5620 | // Retry in case the newly added address is not ready yet. |
| 5621 | ASSERT_EQ(0, retry_bind(c1, reinterpret_cast<sockaddr*>(&sin6), len)) << "errno:" << errno; |
| 5622 | ASSERT_EQ(0, getsockname(c1, reinterpret_cast<sockaddr*>(&sin6), &len)) << "errno:" << errno; |
| 5623 | ASSERT_EQ(0, connect(c1, reinterpret_cast<sockaddr*>(&v6LinkLocalAddr_1.value()), len)) |
| 5624 | << "errno:" << errno; |
| 5625 | |
| 5626 | // Verify netId in fwmark. |
| 5627 | Fwmark fwmark; |
| 5628 | socklen_t fwmarkLen = sizeof(fwmark.intValue); |
| 5629 | EXPECT_EQ(0, getsockopt(c1, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen)); |
| 5630 | EXPECT_EQ((unsigned)NetworkController::LOCAL_NET_ID, fwmark.netId); |
| 5631 | |
| 5632 | unique_fd a1(accept(s1, nullptr, 0)); |
| 5633 | ASSERT_NE(-1, a1) << "errno:" << errno; |
| 5634 | EXPECT_EQ(0, getsockopt(a1, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen)); |
| 5635 | // TODO: Fix fwmark on the accept socket? |
| 5636 | fwmark.netId = NetworkController::LOCAL_NET_ID; |
| 5637 | EXPECT_EQ(0, setsockopt(a1, SOL_SOCKET, SO_MARK, &fwmark.intValue, sizeof(fwmark.intValue))); |
| 5638 | |
| 5639 | // Change permission on the default network. Client socket should not be destroyed. |
| 5640 | EXPECT_TRUE( |
| 5641 | mNetd->networkSetPermissionForNetwork(TEST_NETID1, INetd::PERMISSION_NETWORK).isOk()); |
| 5642 | |
| 5643 | char buf[1024] = {}; |
| 5644 | EXPECT_EQ(3, write(a1, "foo", 3)) << "errno:" << errno; |
| 5645 | EXPECT_EQ(3, read(c1, buf, sizeof(buf))) << "errno:" << errno; |
Suprabh Shukla | 995728e | 2024-02-07 13:22:49 -0800 | [diff] [blame] | 5646 | } |
Patrick Rohr | 26837ab | 2024-05-14 15:45:10 -0700 | [diff] [blame] | 5647 | |
| 5648 | /** |
| 5649 | * This test sets up three networks: |
| 5650 | * - SYSTEM_DEFAULT_NETID -> sTun |
| 5651 | * - OTHER_NETID -> sTun2 |
| 5652 | * - VPN_NETID -> sTun3 |
| 5653 | */ |
| 5654 | TEST_F(NetdBinderTest, BypassVpnWithNetId) { |
| 5655 | static constexpr unsigned OTHER_NETID = TEST_NETID2; |
| 5656 | static const sockaddr_in6 addr = { |
| 5657 | .sin6_family = AF_INET6, |
| 5658 | .sin6_port = 42, |
| 5659 | .sin6_addr = V6_ADDR, |
| 5660 | }; |
| 5661 | createVpnAndOtherPhysicalNetwork(SYSTEM_DEFAULT_NETID, OTHER_NETID, VPN_NETID, true /*secure*/); |
| 5662 | ASSERT_TRUE(mNetd->networkAddUidRanges(VPN_NETID, {makeUidRangeParcel(TEST_UID1)}).isOk()); |
| 5663 | |
| 5664 | { |
| 5665 | ScopedUidChange change(TEST_UID1); |
| 5666 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM, 0)); |
| 5667 | // No network selected for the socket: data is received on VPN network. |
| 5668 | checkDataReceived(sock, sTun3.getFdForTesting(), (sockaddr*)&addr, sizeof(addr)); |
| 5669 | } |
| 5670 | |
| 5671 | { |
| 5672 | ScopedUidChange change(TEST_UID1); |
| 5673 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM, 0)); |
| 5674 | EXPECT_EQ(0, setNetworkForSocket(VPN_NETID, sock)); |
| 5675 | EXPECT_EQ(-EPERM, setNetworkForSocket(SYSTEM_DEFAULT_NETID, sock)); |
| 5676 | EXPECT_EQ(-EPERM, setNetworkForSocket(OTHER_NETID, sock)); |
| 5677 | |
| 5678 | // No permission to use OTHER_NETID: data is received on VPN network. |
| 5679 | checkDataReceived(sock, sTun3.getFdForTesting(), (sockaddr*)&addr, sizeof(addr)); |
| 5680 | } |
| 5681 | |
| 5682 | mNetd->networkAllowBypassVpnOnNetwork(true /* allow */, TEST_UID1, OTHER_NETID); |
| 5683 | auto guard = android::base::make_scope_guard([this] { |
| 5684 | mNetd->networkAllowBypassVpnOnNetwork(false /* allow */, TEST_UID1, OTHER_NETID); |
| 5685 | }); |
| 5686 | |
| 5687 | { |
| 5688 | ScopedUidChange change(TEST_UID1); |
| 5689 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM, 0)); |
| 5690 | EXPECT_EQ(0, setNetworkForSocket(VPN_NETID, sock)); |
| 5691 | EXPECT_EQ(-EPERM, setNetworkForSocket(SYSTEM_DEFAULT_NETID, sock)); |
| 5692 | EXPECT_EQ(0, setNetworkForSocket(OTHER_NETID, sock)); |
| 5693 | |
| 5694 | // Permission to use OTHER_NETID and OTHER_NETID is selected for socket: data is received on |
| 5695 | // OTHER network. |
| 5696 | checkDataReceived(sock, sTun2.getFdForTesting(), (sockaddr*)&addr, sizeof(addr)); |
| 5697 | } |
| 5698 | } |
| 5699 | |
| 5700 | /** |
| 5701 | * This test sets up three networks: |
| 5702 | * - SYSTEM_DEFAULT_NETID -> sTun |
| 5703 | * - OTHER_NETID -> sTun2 |
| 5704 | * - VPN_NETID -> sTun3 |
| 5705 | */ |
| 5706 | TEST_F(NetdBinderTest, BypassVpnWithNetIdOverlappingRules) { |
| 5707 | static constexpr unsigned OTHER_NETID = TEST_NETID2; |
| 5708 | createVpnAndOtherPhysicalNetwork(SYSTEM_DEFAULT_NETID, OTHER_NETID, VPN_NETID, true /*secure*/); |
| 5709 | ASSERT_TRUE(mNetd->networkAddUidRanges(VPN_NETID, {makeUidRangeParcel(TEST_UID1)}).isOk()); |
| 5710 | |
| 5711 | // Check default network permissions. |
| 5712 | { |
| 5713 | ScopedUidChange change(TEST_UID1); |
| 5714 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM, 0)); |
| 5715 | EXPECT_EQ(0, setNetworkForSocket(VPN_NETID, sock)); |
| 5716 | EXPECT_EQ(-EPERM, setNetworkForSocket(SYSTEM_DEFAULT_NETID, sock)); |
| 5717 | EXPECT_EQ(-EPERM, setNetworkForSocket(OTHER_NETID, sock)); |
| 5718 | } |
| 5719 | |
| 5720 | // Careful: only use EXPECT_*() so the test cleans up after itself on failure. |
| 5721 | // Exempt TEST_UID1 from VPN on OTHER_NETID |
| 5722 | mNetd->networkAllowBypassVpnOnNetwork(true /* allow */, TEST_UID1, OTHER_NETID); |
| 5723 | { |
| 5724 | ScopedUidChange change(TEST_UID1); |
| 5725 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM, 0)); |
| 5726 | EXPECT_EQ(0, setNetworkForSocket(VPN_NETID, sock)); |
| 5727 | EXPECT_EQ(-EPERM, setNetworkForSocket(SYSTEM_DEFAULT_NETID, sock)); |
| 5728 | EXPECT_EQ(0, setNetworkForSocket(OTHER_NETID, sock)); |
| 5729 | } |
| 5730 | |
| 5731 | // Exempt TEST_UID1 from VPN on all networks. |
| 5732 | mNetd->networkSetProtectAllow(TEST_UID1); |
| 5733 | { |
| 5734 | ScopedUidChange change(TEST_UID1); |
| 5735 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM, 0)); |
| 5736 | EXPECT_EQ(0, setNetworkForSocket(VPN_NETID, sock)); |
| 5737 | EXPECT_EQ(0, setNetworkForSocket(SYSTEM_DEFAULT_NETID, sock)); |
| 5738 | EXPECT_EQ(0, setNetworkForSocket(OTHER_NETID, sock)); |
| 5739 | } |
| 5740 | |
| 5741 | // Remove network-specific exemption |
| 5742 | mNetd->networkAllowBypassVpnOnNetwork(false /* allow */, TEST_UID1, OTHER_NETID); |
| 5743 | { |
| 5744 | ScopedUidChange change(TEST_UID1); |
| 5745 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM, 0)); |
| 5746 | EXPECT_EQ(0, setNetworkForSocket(VPN_NETID, sock)); |
| 5747 | EXPECT_EQ(0, setNetworkForSocket(SYSTEM_DEFAULT_NETID, sock)); |
| 5748 | EXPECT_EQ(0, setNetworkForSocket(OTHER_NETID, sock)); |
| 5749 | } |
| 5750 | |
| 5751 | // Remove global exemption |
| 5752 | mNetd->networkSetProtectDeny(TEST_UID1); |
| 5753 | { |
| 5754 | ScopedUidChange change(TEST_UID1); |
| 5755 | unique_fd sock(socket(AF_INET6, SOCK_DGRAM, 0)); |
| 5756 | EXPECT_EQ(0, setNetworkForSocket(VPN_NETID, sock)); |
| 5757 | EXPECT_EQ(-EPERM, setNetworkForSocket(SYSTEM_DEFAULT_NETID, sock)); |
| 5758 | EXPECT_EQ(-EPERM, setNetworkForSocket(OTHER_NETID, sock)); |
| 5759 | } |
| 5760 | } |