TrafficControllerTest.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * Copyright 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * TrafficControllerTest.cpp - unit tests for TrafficController.cpp
  17. */
  18. #include <string>
  19. #include <vector>
  20. #include <fcntl.h>
  21. #include <inttypes.h>
  22. #include <linux/inet_diag.h>
  23. #include <linux/sock_diag.h>
  24. #include <sys/socket.h>
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27. #include <gtest/gtest.h>
  28. #include <android-base/stringprintf.h>
  29. #include <android-base/strings.h>
  30. #include <netdutils/MockSyscalls.h>
  31. #include "netdutils/StatusOr.h"
  32. #include "FirewallController.h"
  33. #include "TrafficController.h"
  34. #include "bpf/BpfUtils.h"
  35. using namespace android::bpf; // NOLINT(google-build-using-namespace): grandfathered
  36. namespace android {
  37. namespace net {
  38. using netdutils::isOk;
  39. using netdutils::StatusOr;
  40. constexpr int TEST_MAP_SIZE = 10;
  41. constexpr int TEST_COOKIE = 1;
  42. constexpr uid_t TEST_UID = 10086;
  43. constexpr uid_t TEST_UID2 = 54321;
  44. constexpr uid_t TEST_UID3 = 98765;
  45. constexpr uint32_t TEST_TAG = 42;
  46. constexpr uint32_t TEST_COUNTERSET = 1;
  47. constexpr uint32_t DEFAULT_COUNTERSET = 0;
  48. constexpr uint32_t TEST_PER_UID_STATS_ENTRIES_LIMIT = 3;
  49. constexpr uint32_t TEST_TOTAL_UID_STATS_ENTRIES_LIMIT = 7;
  50. #define ASSERT_VALID(x) ASSERT_TRUE((x).isValid())
  51. class TrafficControllerTest : public ::testing::Test {
  52. protected:
  53. TrafficControllerTest()
  54. : mTc(TEST_PER_UID_STATS_ENTRIES_LIMIT, TEST_TOTAL_UID_STATS_ENTRIES_LIMIT) {}
  55. TrafficController mTc;
  56. BpfMap<uint64_t, UidTag> mFakeCookieTagMap;
  57. BpfMap<uint32_t, uint8_t> mFakeUidCounterSetMap;
  58. BpfMap<uint32_t, StatsValue> mFakeAppUidStatsMap;
  59. BpfMap<StatsKey, StatsValue> mFakeStatsMapA;
  60. BpfMap<uint32_t, uint8_t> mFakeConfigurationMap;
  61. BpfMap<uint32_t, UidOwnerValue> mFakeUidOwnerMap;
  62. BpfMap<uint32_t, uint8_t> mFakeUidPermissionMap;
  63. void SetUp() {
  64. std::lock_guard guard(mTc.mMutex);
  65. SKIP_IF_BPF_NOT_SUPPORTED;
  66. ASSERT_EQ(0, setrlimitForTest());
  67. mFakeCookieTagMap.reset(createMap(BPF_MAP_TYPE_HASH, sizeof(uint64_t),
  68. sizeof(struct UidTag), TEST_MAP_SIZE, 0));
  69. ASSERT_VALID(mFakeCookieTagMap);
  70. mFakeUidCounterSetMap.reset(
  71. createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t), sizeof(uint8_t), TEST_MAP_SIZE, 0));
  72. ASSERT_VALID(mFakeUidCounterSetMap);
  73. mFakeAppUidStatsMap.reset(createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t),
  74. sizeof(struct StatsValue), TEST_MAP_SIZE, 0));
  75. ASSERT_VALID(mFakeAppUidStatsMap);
  76. mFakeStatsMapA.reset(createMap(BPF_MAP_TYPE_HASH, sizeof(struct StatsKey),
  77. sizeof(struct StatsValue), TEST_MAP_SIZE, 0));
  78. ASSERT_VALID(mFakeStatsMapA);
  79. mFakeConfigurationMap.reset(
  80. createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t), sizeof(uint8_t), 1, 0));
  81. ASSERT_VALID(mFakeConfigurationMap);
  82. mFakeUidOwnerMap.reset(createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t), sizeof(UidOwnerValue),
  83. TEST_MAP_SIZE, 0));
  84. ASSERT_VALID(mFakeUidOwnerMap);
  85. mFakeUidPermissionMap.reset(
  86. createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t), sizeof(uint8_t), TEST_MAP_SIZE, 0));
  87. ASSERT_VALID(mFakeUidPermissionMap);
  88. mTc.mCookieTagMap.reset(dupFd(mFakeCookieTagMap.getMap()));
  89. ASSERT_VALID(mTc.mCookieTagMap);
  90. mTc.mUidCounterSetMap.reset(dupFd(mFakeUidCounterSetMap.getMap()));
  91. ASSERT_VALID(mTc.mUidCounterSetMap);
  92. mTc.mAppUidStatsMap.reset(dupFd(mFakeAppUidStatsMap.getMap()));
  93. ASSERT_VALID(mTc.mAppUidStatsMap);
  94. mTc.mStatsMapA.reset(dupFd(mFakeStatsMapA.getMap()));
  95. ASSERT_VALID(mTc.mStatsMapA);
  96. mTc.mConfigurationMap.reset(dupFd(mFakeConfigurationMap.getMap()));
  97. ASSERT_VALID(mTc.mConfigurationMap);
  98. // Always write to stats map A by default.
  99. ASSERT_TRUE(isOk(mTc.mConfigurationMap.writeValue(CURRENT_STATS_MAP_CONFIGURATION_KEY,
  100. SELECT_MAP_A, BPF_ANY)));
  101. mTc.mUidOwnerMap.reset(dupFd(mFakeUidOwnerMap.getMap()));
  102. ASSERT_VALID(mTc.mUidOwnerMap);
  103. mTc.mUidPermissionMap.reset(dupFd(mFakeUidPermissionMap.getMap()));
  104. ASSERT_VALID(mTc.mUidPermissionMap);
  105. mTc.mPrivilegedUser.clear();
  106. }
  107. int dupFd(const android::base::unique_fd& mapFd) {
  108. return fcntl(mapFd.get(), F_DUPFD_CLOEXEC, 0);
  109. }
  110. int setUpSocketAndTag(int protocol, uint64_t* cookie, uint32_t tag, uid_t uid,
  111. uid_t callingUid) {
  112. int sock = socket(protocol, SOCK_STREAM | SOCK_CLOEXEC, 0);
  113. EXPECT_LE(0, sock);
  114. *cookie = getSocketCookie(sock);
  115. EXPECT_NE(NONEXISTENT_COOKIE, *cookie);
  116. EXPECT_EQ(0, mTc.tagSocket(sock, tag, uid, callingUid));
  117. return sock;
  118. }
  119. void expectUidTag(uint64_t cookie, uid_t uid, uint32_t tag) {
  120. StatusOr<UidTag> tagResult = mFakeCookieTagMap.readValue(cookie);
  121. EXPECT_TRUE(isOk(tagResult));
  122. EXPECT_EQ(uid, tagResult.value().uid);
  123. EXPECT_EQ(tag, tagResult.value().tag);
  124. }
  125. void expectNoTag(uint64_t cookie) { EXPECT_FALSE(isOk(mFakeCookieTagMap.readValue(cookie))); }
  126. void populateFakeStats(uint64_t cookie, uint32_t uid, uint32_t tag, StatsKey* key) {
  127. UidTag cookieMapkey = {.uid = (uint32_t)uid, .tag = tag};
  128. EXPECT_TRUE(isOk(mFakeCookieTagMap.writeValue(cookie, cookieMapkey, BPF_ANY)));
  129. *key = {.uid = uid, .tag = tag, .counterSet = TEST_COUNTERSET, .ifaceIndex = 1};
  130. StatsValue statsMapValue = {.rxPackets = 1, .rxBytes = 100};
  131. uint8_t counterSet = TEST_COUNTERSET;
  132. EXPECT_TRUE(isOk(mFakeUidCounterSetMap.writeValue(uid, counterSet, BPF_ANY)));
  133. EXPECT_TRUE(isOk(mFakeStatsMapA.writeValue(*key, statsMapValue, BPF_ANY)));
  134. key->tag = 0;
  135. EXPECT_TRUE(isOk(mFakeStatsMapA.writeValue(*key, statsMapValue, BPF_ANY)));
  136. EXPECT_TRUE(isOk(mFakeAppUidStatsMap.writeValue(uid, statsMapValue, BPF_ANY)));
  137. // put tag information back to statsKey
  138. key->tag = tag;
  139. }
  140. void checkUidOwnerRuleForChain(ChildChain chain, UidOwnerMatchType match) {
  141. uint32_t uid = TEST_UID;
  142. EXPECT_EQ(0, mTc.changeUidOwnerRule(chain, uid, DENY, BLACKLIST));
  143. StatusOr<UidOwnerValue> value = mFakeUidOwnerMap.readValue(uid);
  144. EXPECT_TRUE(isOk(value));
  145. EXPECT_TRUE(value.value().rule & match);
  146. uid = TEST_UID2;
  147. EXPECT_EQ(0, mTc.changeUidOwnerRule(chain, uid, ALLOW, WHITELIST));
  148. value = mFakeUidOwnerMap.readValue(uid);
  149. EXPECT_TRUE(isOk(value));
  150. EXPECT_TRUE(value.value().rule & match);
  151. EXPECT_EQ(0, mTc.changeUidOwnerRule(chain, uid, DENY, WHITELIST));
  152. value = mFakeUidOwnerMap.readValue(uid);
  153. EXPECT_FALSE(isOk(value));
  154. EXPECT_EQ(ENOENT, value.status().code());
  155. uid = TEST_UID;
  156. EXPECT_EQ(0, mTc.changeUidOwnerRule(chain, uid, ALLOW, BLACKLIST));
  157. value = mFakeUidOwnerMap.readValue(uid);
  158. EXPECT_FALSE(isOk(value));
  159. EXPECT_EQ(ENOENT, value.status().code());
  160. uid = TEST_UID3;
  161. EXPECT_EQ(-ENOENT, mTc.changeUidOwnerRule(chain, uid, ALLOW, BLACKLIST));
  162. value = mFakeUidOwnerMap.readValue(uid);
  163. EXPECT_FALSE(isOk(value));
  164. EXPECT_EQ(ENOENT, value.status().code());
  165. }
  166. void checkEachUidValue(const std::vector<int32_t>& uids, UidOwnerMatchType match) {
  167. for (uint32_t uid : uids) {
  168. StatusOr<UidOwnerValue> value = mFakeUidOwnerMap.readValue(uid);
  169. EXPECT_TRUE(isOk(value));
  170. EXPECT_TRUE(value.value().rule & match);
  171. }
  172. std::set<uint32_t> uidSet(uids.begin(), uids.end());
  173. const auto checkNoOtherUid = [&uidSet](const int32_t& key,
  174. const BpfMap<uint32_t, UidOwnerValue>&) {
  175. EXPECT_NE(uidSet.end(), uidSet.find(key));
  176. return netdutils::status::ok;
  177. };
  178. EXPECT_TRUE(isOk(mFakeUidOwnerMap.iterate(checkNoOtherUid)));
  179. }
  180. void checkUidMapReplace(const std::string& name, const std::vector<int32_t>& uids,
  181. UidOwnerMatchType match) {
  182. bool isWhitelist = true;
  183. EXPECT_EQ(0, mTc.replaceUidOwnerMap(name, isWhitelist, uids));
  184. checkEachUidValue(uids, match);
  185. isWhitelist = false;
  186. EXPECT_EQ(0, mTc.replaceUidOwnerMap(name, isWhitelist, uids));
  187. checkEachUidValue(uids, match);
  188. }
  189. void expectUidOwnerMapValues(const std::vector<std::string>& appStrUids, uint8_t expectedRule,
  190. uint32_t expectedIif) {
  191. for (const std::string& strUid : appStrUids) {
  192. uint32_t uid = stoi(strUid);
  193. StatusOr<UidOwnerValue> value = mFakeUidOwnerMap.readValue(uid);
  194. EXPECT_TRUE(isOk(value));
  195. EXPECT_EQ(expectedRule, value.value().rule)
  196. << "Expected rule for UID " << uid << " to be " << expectedRule << ", but was "
  197. << value.value().rule;
  198. EXPECT_EQ(expectedIif, value.value().iif)
  199. << "Expected iif for UID " << uid << " to be " << expectedIif << ", but was "
  200. << value.value().iif;
  201. }
  202. }
  203. template <class Key, class Value>
  204. void expectMapEmpty(BpfMap<Key, Value>& map) {
  205. auto isEmpty = map.isEmpty();
  206. EXPECT_TRUE(isOk(isEmpty));
  207. EXPECT_TRUE(isEmpty.value());
  208. }
  209. void expectUidPermissionMapValues(const std::vector<uid_t>& appUids, uint8_t expectedValue) {
  210. for (uid_t uid : appUids) {
  211. StatusOr<uint8_t> value = mFakeUidPermissionMap.readValue(uid);
  212. EXPECT_TRUE(isOk(value));
  213. EXPECT_EQ(expectedValue, value.value())
  214. << "Expected value for UID " << uid << " to be " << expectedValue
  215. << ", but was " << value.value();
  216. }
  217. }
  218. void expectPrivilegedUserSet(const std::vector<uid_t>& appUids) {
  219. std::lock_guard guard(mTc.mMutex);
  220. EXPECT_EQ(appUids.size(), mTc.mPrivilegedUser.size());
  221. for (uid_t uid : appUids) {
  222. EXPECT_NE(mTc.mPrivilegedUser.end(), mTc.mPrivilegedUser.find(uid));
  223. }
  224. }
  225. void expectPrivilegedUserSetEmpty() {
  226. std::lock_guard guard(mTc.mMutex);
  227. EXPECT_TRUE(mTc.mPrivilegedUser.empty());
  228. }
  229. void addPrivilegedUid(uid_t uid) {
  230. std::vector privilegedUid = {uid};
  231. mTc.setPermissionForUids(INetd::PERMISSION_UPDATE_DEVICE_STATS, privilegedUid);
  232. }
  233. void removePrivilegedUid(uid_t uid) {
  234. std::vector privilegedUid = {uid};
  235. mTc.setPermissionForUids(INetd::PERMISSION_NONE, privilegedUid);
  236. }
  237. void expectFakeStatsUnchanged(uint64_t cookie, uint32_t tag, uint32_t uid,
  238. StatsKey tagStatsMapKey) {
  239. StatusOr<UidTag> cookieMapResult = mFakeCookieTagMap.readValue(cookie);
  240. EXPECT_TRUE(isOk(cookieMapResult));
  241. EXPECT_EQ(uid, cookieMapResult.value().uid);
  242. EXPECT_EQ(tag, cookieMapResult.value().tag);
  243. StatusOr<uint8_t> counterSetResult = mFakeUidCounterSetMap.readValue(uid);
  244. EXPECT_TRUE(isOk(counterSetResult));
  245. EXPECT_EQ(TEST_COUNTERSET, counterSetResult.value());
  246. StatusOr<StatsValue> statsMapResult = mFakeStatsMapA.readValue(tagStatsMapKey);
  247. EXPECT_TRUE(isOk(statsMapResult));
  248. EXPECT_EQ((uint64_t)1, statsMapResult.value().rxPackets);
  249. EXPECT_EQ((uint64_t)100, statsMapResult.value().rxBytes);
  250. tagStatsMapKey.tag = 0;
  251. statsMapResult = mFakeStatsMapA.readValue(tagStatsMapKey);
  252. EXPECT_TRUE(isOk(statsMapResult));
  253. EXPECT_EQ((uint64_t)1, statsMapResult.value().rxPackets);
  254. EXPECT_EQ((uint64_t)100, statsMapResult.value().rxBytes);
  255. auto appStatsResult = mFakeAppUidStatsMap.readValue(uid);
  256. EXPECT_TRUE(isOk(appStatsResult));
  257. EXPECT_EQ((uint64_t)1, appStatsResult.value().rxPackets);
  258. EXPECT_EQ((uint64_t)100, appStatsResult.value().rxBytes);
  259. }
  260. void expectTagSocketReachLimit(uint32_t tag, uint32_t uid) {
  261. int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
  262. EXPECT_LE(0, sock);
  263. if (sock < 0) return;
  264. uint64_t sockCookie = getSocketCookie(sock);
  265. EXPECT_NE(NONEXISTENT_COOKIE, sockCookie);
  266. EXPECT_EQ(-EMFILE, mTc.tagSocket(sock, tag, uid, uid));
  267. expectNoTag(sockCookie);
  268. // Delete stats entries then tag socket success
  269. EXPECT_EQ(0, mTc.deleteTagData(0, uid, 0));
  270. EXPECT_EQ(0, mTc.tagSocket(sock, tag, uid, uid));
  271. expectUidTag(sockCookie, uid, tag);
  272. }
  273. };
  274. TEST_F(TrafficControllerTest, TestTagSocketV4) {
  275. SKIP_IF_BPF_NOT_SUPPORTED;
  276. uint64_t sockCookie;
  277. int v4socket = setUpSocketAndTag(AF_INET, &sockCookie, TEST_TAG, TEST_UID, TEST_UID);
  278. expectUidTag(sockCookie, TEST_UID, TEST_TAG);
  279. ASSERT_EQ(0, mTc.untagSocket(v4socket));
  280. expectNoTag(sockCookie);
  281. expectMapEmpty(mFakeCookieTagMap);
  282. }
  283. TEST_F(TrafficControllerTest, TestReTagSocket) {
  284. SKIP_IF_BPF_NOT_SUPPORTED;
  285. uint64_t sockCookie;
  286. int v4socket = setUpSocketAndTag(AF_INET, &sockCookie, TEST_TAG, TEST_UID, TEST_UID);
  287. expectUidTag(sockCookie, TEST_UID, TEST_TAG);
  288. ASSERT_EQ(0, mTc.tagSocket(v4socket, TEST_TAG + 1, TEST_UID + 1, TEST_UID + 1));
  289. expectUidTag(sockCookie, TEST_UID + 1, TEST_TAG + 1);
  290. }
  291. TEST_F(TrafficControllerTest, TestTagTwoSockets) {
  292. SKIP_IF_BPF_NOT_SUPPORTED;
  293. uint64_t sockCookie1;
  294. uint64_t sockCookie2;
  295. int v4socket1 = setUpSocketAndTag(AF_INET, &sockCookie1, TEST_TAG, TEST_UID, TEST_UID);
  296. setUpSocketAndTag(AF_INET, &sockCookie2, TEST_TAG, TEST_UID, TEST_UID);
  297. expectUidTag(sockCookie1, TEST_UID, TEST_TAG);
  298. expectUidTag(sockCookie2, TEST_UID, TEST_TAG);
  299. ASSERT_EQ(0, mTc.untagSocket(v4socket1));
  300. expectNoTag(sockCookie1);
  301. expectUidTag(sockCookie2, TEST_UID, TEST_TAG);
  302. ASSERT_FALSE(isOk(mFakeCookieTagMap.getNextKey(sockCookie2)));
  303. }
  304. TEST_F(TrafficControllerTest, TestTagSocketV6) {
  305. SKIP_IF_BPF_NOT_SUPPORTED;
  306. uint64_t sockCookie;
  307. int v6socket = setUpSocketAndTag(AF_INET6, &sockCookie, TEST_TAG, TEST_UID, TEST_UID);
  308. expectUidTag(sockCookie, TEST_UID, TEST_TAG);
  309. ASSERT_EQ(0, mTc.untagSocket(v6socket));
  310. expectNoTag(sockCookie);
  311. expectMapEmpty(mFakeCookieTagMap);
  312. }
  313. TEST_F(TrafficControllerTest, TestTagInvalidSocket) {
  314. SKIP_IF_BPF_NOT_SUPPORTED;
  315. int invalidSocket = -1;
  316. ASSERT_GT(0, mTc.tagSocket(invalidSocket, TEST_TAG, TEST_UID, TEST_UID));
  317. expectMapEmpty(mFakeCookieTagMap);
  318. }
  319. TEST_F(TrafficControllerTest, TestTagSocketWithoutPermission) {
  320. SKIP_IF_BPF_NOT_SUPPORTED;
  321. int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
  322. ASSERT_NE(-1, sock);
  323. ASSERT_EQ(-EPERM, mTc.tagSocket(sock, TEST_TAG, TEST_UID, TEST_UID2));
  324. expectMapEmpty(mFakeCookieTagMap);
  325. }
  326. TEST_F(TrafficControllerTest, TestTagSocketWithPermission) {
  327. SKIP_IF_BPF_NOT_SUPPORTED;
  328. // Grant permission to calling uid.
  329. std::vector<uid_t> callingUid = {TEST_UID2};
  330. mTc.setPermissionForUids(INetd::PERMISSION_UPDATE_DEVICE_STATS, callingUid);
  331. // Tag a socket to a different uid other then callingUid.
  332. uint64_t sockCookie;
  333. int v6socket = setUpSocketAndTag(AF_INET6, &sockCookie, TEST_TAG, TEST_UID, TEST_UID2);
  334. expectUidTag(sockCookie, TEST_UID, TEST_TAG);
  335. EXPECT_EQ(0, mTc.untagSocket(v6socket));
  336. expectNoTag(sockCookie);
  337. expectMapEmpty(mFakeCookieTagMap);
  338. // Clean up the permission
  339. mTc.setPermissionForUids(INetd::PERMISSION_NONE, callingUid);
  340. expectPrivilegedUserSetEmpty();
  341. }
  342. TEST_F(TrafficControllerTest, TestUntagInvalidSocket) {
  343. SKIP_IF_BPF_NOT_SUPPORTED;
  344. int invalidSocket = -1;
  345. ASSERT_GT(0, mTc.untagSocket(invalidSocket));
  346. int v4socket = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  347. ASSERT_GT(0, mTc.untagSocket(v4socket));
  348. expectMapEmpty(mFakeCookieTagMap);
  349. }
  350. TEST_F(TrafficControllerTest, TestTagSocketReachLimitFail) {
  351. SKIP_IF_BPF_NOT_SUPPORTED;
  352. uid_t uid = TEST_UID;
  353. StatsKey tagStatsMapKey[4];
  354. for (int i = 0; i < 3; i++) {
  355. uint64_t cookie = TEST_COOKIE + i;
  356. uint32_t tag = TEST_TAG + i;
  357. populateFakeStats(cookie, uid, tag, &tagStatsMapKey[i]);
  358. }
  359. expectTagSocketReachLimit(TEST_TAG, TEST_UID);
  360. }
  361. TEST_F(TrafficControllerTest, TestTagSocketReachTotalLimitFail) {
  362. SKIP_IF_BPF_NOT_SUPPORTED;
  363. StatsKey tagStatsMapKey[4];
  364. for (int i = 0; i < 4; i++) {
  365. uint64_t cookie = TEST_COOKIE + i;
  366. uint32_t tag = TEST_TAG + i;
  367. uid_t uid = TEST_UID + i;
  368. populateFakeStats(cookie, uid, tag, &tagStatsMapKey[i]);
  369. }
  370. expectTagSocketReachLimit(TEST_TAG, TEST_UID);
  371. }
  372. TEST_F(TrafficControllerTest, TestSetCounterSet) {
  373. SKIP_IF_BPF_NOT_SUPPORTED;
  374. uid_t callingUid = TEST_UID2;
  375. addPrivilegedUid(callingUid);
  376. ASSERT_EQ(0, mTc.setCounterSet(TEST_COUNTERSET, TEST_UID, callingUid));
  377. uid_t uid = TEST_UID;
  378. StatusOr<uint8_t> counterSetResult = mFakeUidCounterSetMap.readValue(uid);
  379. ASSERT_TRUE(isOk(counterSetResult));
  380. ASSERT_EQ(TEST_COUNTERSET, counterSetResult.value());
  381. ASSERT_EQ(0, mTc.setCounterSet(DEFAULT_COUNTERSET, TEST_UID, callingUid));
  382. ASSERT_FALSE(isOk(mFakeUidCounterSetMap.readValue(uid)));
  383. expectMapEmpty(mFakeUidCounterSetMap);
  384. }
  385. TEST_F(TrafficControllerTest, TestSetCounterSetWithoutPermission) {
  386. SKIP_IF_BPF_NOT_SUPPORTED;
  387. ASSERT_EQ(-EPERM, mTc.setCounterSet(TEST_COUNTERSET, TEST_UID, TEST_UID2));
  388. uid_t uid = TEST_UID;
  389. ASSERT_FALSE(isOk(mFakeUidCounterSetMap.readValue(uid)));
  390. expectMapEmpty(mFakeUidCounterSetMap);
  391. }
  392. TEST_F(TrafficControllerTest, TestSetInvalidCounterSet) {
  393. SKIP_IF_BPF_NOT_SUPPORTED;
  394. uid_t callingUid = TEST_UID2;
  395. addPrivilegedUid(callingUid);
  396. ASSERT_GT(0, mTc.setCounterSet(OVERFLOW_COUNTERSET, TEST_UID, callingUid));
  397. uid_t uid = TEST_UID;
  398. ASSERT_FALSE(isOk(mFakeUidCounterSetMap.readValue(uid)));
  399. expectMapEmpty(mFakeUidCounterSetMap);
  400. }
  401. TEST_F(TrafficControllerTest, TestDeleteTagDataWithoutPermission) {
  402. SKIP_IF_BPF_NOT_SUPPORTED;
  403. uint64_t cookie = 1;
  404. uid_t uid = TEST_UID;
  405. uint32_t tag = TEST_TAG;
  406. StatsKey tagStatsMapKey;
  407. populateFakeStats(cookie, uid, tag, &tagStatsMapKey);
  408. ASSERT_EQ(-EPERM, mTc.deleteTagData(0, TEST_UID, TEST_UID2));
  409. expectFakeStatsUnchanged(cookie, tag, uid, tagStatsMapKey);
  410. }
  411. TEST_F(TrafficControllerTest, TestDeleteTagData) {
  412. SKIP_IF_BPF_NOT_SUPPORTED;
  413. uid_t callingUid = TEST_UID2;
  414. addPrivilegedUid(callingUid);
  415. uint64_t cookie = 1;
  416. uid_t uid = TEST_UID;
  417. uint32_t tag = TEST_TAG;
  418. StatsKey tagStatsMapKey;
  419. populateFakeStats(cookie, uid, tag, &tagStatsMapKey);
  420. ASSERT_EQ(0, mTc.deleteTagData(TEST_TAG, TEST_UID, callingUid));
  421. ASSERT_FALSE(isOk(mFakeCookieTagMap.readValue(cookie)));
  422. StatusOr<uint8_t> counterSetResult = mFakeUidCounterSetMap.readValue(uid);
  423. ASSERT_TRUE(isOk(counterSetResult));
  424. ASSERT_EQ(TEST_COUNTERSET, counterSetResult.value());
  425. ASSERT_FALSE(isOk(mFakeStatsMapA.readValue(tagStatsMapKey)));
  426. tagStatsMapKey.tag = 0;
  427. StatusOr<StatsValue> statsMapResult = mFakeStatsMapA.readValue(tagStatsMapKey);
  428. ASSERT_TRUE(isOk(statsMapResult));
  429. ASSERT_EQ((uint64_t)1, statsMapResult.value().rxPackets);
  430. ASSERT_EQ((uint64_t)100, statsMapResult.value().rxBytes);
  431. auto appStatsResult = mFakeAppUidStatsMap.readValue(TEST_UID);
  432. ASSERT_TRUE(isOk(appStatsResult));
  433. ASSERT_EQ((uint64_t)1, appStatsResult.value().rxPackets);
  434. ASSERT_EQ((uint64_t)100, appStatsResult.value().rxBytes);
  435. }
  436. TEST_F(TrafficControllerTest, TestDeleteAllUidData) {
  437. SKIP_IF_BPF_NOT_SUPPORTED;
  438. uid_t callingUid = TEST_UID2;
  439. addPrivilegedUid(callingUid);
  440. uint64_t cookie = 1;
  441. uid_t uid = TEST_UID;
  442. uint32_t tag = TEST_TAG;
  443. StatsKey tagStatsMapKey;
  444. populateFakeStats(cookie, uid, tag, &tagStatsMapKey);
  445. ASSERT_EQ(0, mTc.deleteTagData(0, TEST_UID, callingUid));
  446. ASSERT_FALSE(isOk(mFakeCookieTagMap.readValue(cookie)));
  447. ASSERT_FALSE(isOk(mFakeUidCounterSetMap.readValue(uid)));
  448. ASSERT_FALSE(isOk(mFakeStatsMapA.readValue(tagStatsMapKey)));
  449. tagStatsMapKey.tag = 0;
  450. ASSERT_FALSE(isOk(mFakeStatsMapA.readValue(tagStatsMapKey)));
  451. ASSERT_FALSE(isOk(mFakeAppUidStatsMap.readValue(TEST_UID)));
  452. }
  453. TEST_F(TrafficControllerTest, TestDeleteDataWithTwoTags) {
  454. SKIP_IF_BPF_NOT_SUPPORTED;
  455. uid_t callingUid = TEST_UID2;
  456. addPrivilegedUid(callingUid);
  457. uint64_t cookie1 = 1;
  458. uint64_t cookie2 = 2;
  459. uid_t uid = TEST_UID;
  460. uint32_t tag1 = TEST_TAG;
  461. uint32_t tag2 = TEST_TAG + 1;
  462. StatsKey tagStatsMapKey1;
  463. StatsKey tagStatsMapKey2;
  464. populateFakeStats(cookie1, uid, tag1, &tagStatsMapKey1);
  465. populateFakeStats(cookie2, uid, tag2, &tagStatsMapKey2);
  466. ASSERT_EQ(0, mTc.deleteTagData(TEST_TAG, TEST_UID, callingUid));
  467. ASSERT_FALSE(isOk(mFakeCookieTagMap.readValue(cookie1)));
  468. StatusOr<UidTag> cookieMapResult = mFakeCookieTagMap.readValue(cookie2);
  469. ASSERT_TRUE(isOk(cookieMapResult));
  470. ASSERT_EQ(TEST_UID, cookieMapResult.value().uid);
  471. ASSERT_EQ(TEST_TAG + 1, cookieMapResult.value().tag);
  472. StatusOr<uint8_t> counterSetResult = mFakeUidCounterSetMap.readValue(uid);
  473. ASSERT_TRUE(isOk(counterSetResult));
  474. ASSERT_EQ(TEST_COUNTERSET, counterSetResult.value());
  475. ASSERT_FALSE(isOk(mFakeStatsMapA.readValue(tagStatsMapKey1)));
  476. StatusOr<StatsValue> statsMapResult = mFakeStatsMapA.readValue(tagStatsMapKey2);
  477. ASSERT_TRUE(isOk(statsMapResult));
  478. ASSERT_EQ((uint64_t)1, statsMapResult.value().rxPackets);
  479. ASSERT_EQ((uint64_t)100, statsMapResult.value().rxBytes);
  480. }
  481. TEST_F(TrafficControllerTest, TestDeleteDataWithTwoUids) {
  482. SKIP_IF_BPF_NOT_SUPPORTED;
  483. uid_t callingUid = TEST_UID2;
  484. addPrivilegedUid(callingUid);
  485. uint64_t cookie1 = 1;
  486. uint64_t cookie2 = 2;
  487. uid_t uid1 = TEST_UID;
  488. uid_t uid2 = TEST_UID + 1;
  489. uint32_t tag = TEST_TAG;
  490. StatsKey tagStatsMapKey1;
  491. StatsKey tagStatsMapKey2;
  492. populateFakeStats(cookie1, uid1, tag, &tagStatsMapKey1);
  493. populateFakeStats(cookie2, uid2, tag, &tagStatsMapKey2);
  494. // Delete the stats of one of the uid. Check if it is properly collected by
  495. // removedStats.
  496. ASSERT_EQ(0, mTc.deleteTagData(0, uid2, callingUid));
  497. ASSERT_FALSE(isOk(mFakeCookieTagMap.readValue(cookie2)));
  498. StatusOr<uint8_t> counterSetResult = mFakeUidCounterSetMap.readValue(uid1);
  499. ASSERT_TRUE(isOk(counterSetResult));
  500. ASSERT_EQ(TEST_COUNTERSET, counterSetResult.value());
  501. ASSERT_FALSE(isOk(mFakeUidCounterSetMap.readValue(uid2)));
  502. ASSERT_FALSE(isOk(mFakeStatsMapA.readValue(tagStatsMapKey2)));
  503. tagStatsMapKey2.tag = 0;
  504. ASSERT_FALSE(isOk(mFakeStatsMapA.readValue(tagStatsMapKey2)));
  505. ASSERT_FALSE(isOk(mFakeAppUidStatsMap.readValue(uid2)));
  506. tagStatsMapKey1.tag = 0;
  507. StatusOr<StatsValue> statsMapResult = mFakeStatsMapA.readValue(tagStatsMapKey1);
  508. ASSERT_TRUE(isOk(statsMapResult));
  509. ASSERT_EQ((uint64_t)1, statsMapResult.value().rxPackets);
  510. ASSERT_EQ((uint64_t)100, statsMapResult.value().rxBytes);
  511. auto appStatsResult = mFakeAppUidStatsMap.readValue(uid1);
  512. ASSERT_TRUE(isOk(appStatsResult));
  513. ASSERT_EQ((uint64_t)1, appStatsResult.value().rxPackets);
  514. ASSERT_EQ((uint64_t)100, appStatsResult.value().rxBytes);
  515. // Delete the stats of the other uid.
  516. ASSERT_EQ(0, mTc.deleteTagData(0, uid1, callingUid));
  517. ASSERT_FALSE(isOk(mFakeStatsMapA.readValue(tagStatsMapKey1)));
  518. ASSERT_FALSE(isOk(mFakeAppUidStatsMap.readValue(uid1)));
  519. }
  520. TEST_F(TrafficControllerTest, TestUpdateOwnerMapEntry) {
  521. SKIP_IF_BPF_NOT_SUPPORTED;
  522. uint32_t uid = TEST_UID;
  523. ASSERT_TRUE(isOk(mTc.updateOwnerMapEntry(STANDBY_MATCH, uid, DENY, BLACKLIST)));
  524. StatusOr<UidOwnerValue> value = mFakeUidOwnerMap.readValue(uid);
  525. ASSERT_TRUE(isOk(value));
  526. ASSERT_TRUE(value.value().rule & STANDBY_MATCH);
  527. ASSERT_TRUE(isOk(mTc.updateOwnerMapEntry(DOZABLE_MATCH, uid, ALLOW, WHITELIST)));
  528. value = mFakeUidOwnerMap.readValue(uid);
  529. ASSERT_TRUE(isOk(value));
  530. ASSERT_TRUE(value.value().rule & DOZABLE_MATCH);
  531. ASSERT_TRUE(isOk(mTc.updateOwnerMapEntry(DOZABLE_MATCH, uid, DENY, WHITELIST)));
  532. value = mFakeUidOwnerMap.readValue(uid);
  533. ASSERT_TRUE(isOk(value));
  534. ASSERT_FALSE(value.value().rule & DOZABLE_MATCH);
  535. ASSERT_TRUE(isOk(mTc.updateOwnerMapEntry(STANDBY_MATCH, uid, ALLOW, BLACKLIST)));
  536. ASSERT_FALSE(isOk(mFakeUidOwnerMap.readValue(uid)));
  537. uid = TEST_UID2;
  538. ASSERT_FALSE(isOk(mTc.updateOwnerMapEntry(STANDBY_MATCH, uid, ALLOW, BLACKLIST)));
  539. ASSERT_FALSE(isOk(mFakeUidOwnerMap.readValue(uid)));
  540. }
  541. TEST_F(TrafficControllerTest, TestChangeUidOwnerRule) {
  542. SKIP_IF_BPF_NOT_SUPPORTED;
  543. checkUidOwnerRuleForChain(DOZABLE, DOZABLE_MATCH);
  544. checkUidOwnerRuleForChain(STANDBY, STANDBY_MATCH);
  545. checkUidOwnerRuleForChain(POWERSAVE, POWERSAVE_MATCH);
  546. ASSERT_EQ(-EINVAL, mTc.changeUidOwnerRule(NONE, TEST_UID, ALLOW, WHITELIST));
  547. ASSERT_EQ(-EINVAL, mTc.changeUidOwnerRule(INVALID_CHAIN, TEST_UID, ALLOW, WHITELIST));
  548. }
  549. TEST_F(TrafficControllerTest, TestReplaceUidOwnerMap) {
  550. SKIP_IF_BPF_NOT_SUPPORTED;
  551. std::vector<int32_t> uids = {TEST_UID, TEST_UID2, TEST_UID3};
  552. checkUidMapReplace("fw_dozable", uids, DOZABLE_MATCH);
  553. checkUidMapReplace("fw_standby", uids, STANDBY_MATCH);
  554. checkUidMapReplace("fw_powersave", uids, POWERSAVE_MATCH);
  555. ASSERT_EQ(-EINVAL, mTc.replaceUidOwnerMap("unknow", true, uids));
  556. }
  557. TEST_F(TrafficControllerTest, TestReplaceSameChain) {
  558. SKIP_IF_BPF_NOT_SUPPORTED;
  559. std::vector<int32_t> uids = {TEST_UID, TEST_UID2, TEST_UID3};
  560. checkUidMapReplace("fw_dozable", uids, DOZABLE_MATCH);
  561. std::vector<int32_t> newUids = {TEST_UID2, TEST_UID3};
  562. checkUidMapReplace("fw_dozable", newUids, DOZABLE_MATCH);
  563. }
  564. TEST_F(TrafficControllerTest, TestBlacklistUidMatch) {
  565. SKIP_IF_BPF_NOT_SUPPORTED;
  566. std::vector<std::string> appStrUids = {"1000", "1001", "10012"};
  567. ASSERT_TRUE(isOk(mTc.updateUidOwnerMap(appStrUids, BandwidthController::IptJumpReject,
  568. BandwidthController::IptOpInsert)));
  569. expectUidOwnerMapValues(appStrUids, PENALTY_BOX_MATCH, 0);
  570. ASSERT_TRUE(isOk(mTc.updateUidOwnerMap(appStrUids, BandwidthController::IptJumpReject,
  571. BandwidthController::IptOpDelete)));
  572. expectMapEmpty(mFakeUidOwnerMap);
  573. }
  574. TEST_F(TrafficControllerTest, TestWhitelistUidMatch) {
  575. SKIP_IF_BPF_NOT_SUPPORTED;
  576. std::vector<std::string> appStrUids = {"1000", "1001", "10012"};
  577. ASSERT_TRUE(isOk(mTc.updateUidOwnerMap(appStrUids, BandwidthController::IptJumpReturn,
  578. BandwidthController::IptOpInsert)));
  579. expectUidOwnerMapValues(appStrUids, HAPPY_BOX_MATCH, 0);
  580. ASSERT_TRUE(isOk(mTc.updateUidOwnerMap(appStrUids, BandwidthController::IptJumpReturn,
  581. BandwidthController::IptOpDelete)));
  582. expectMapEmpty(mFakeUidOwnerMap);
  583. }
  584. TEST_F(TrafficControllerTest, TestReplaceMatchUid) {
  585. SKIP_IF_BPF_NOT_SUPPORTED;
  586. std::vector<std::string> appStrUids = {"1000", "1001", "10012"};
  587. // Add appStrUids to the blacklist and expect that their values are all PENALTY_BOX_MATCH.
  588. ASSERT_TRUE(isOk(mTc.updateUidOwnerMap(appStrUids, BandwidthController::IptJumpReject,
  589. BandwidthController::IptOpInsert)));
  590. expectUidOwnerMapValues(appStrUids, PENALTY_BOX_MATCH, 0);
  591. // Add the same UIDs to the whitelist and expect that we get PENALTY_BOX_MATCH |
  592. // HAPPY_BOX_MATCH.
  593. ASSERT_TRUE(isOk(mTc.updateUidOwnerMap(appStrUids, BandwidthController::IptJumpReturn,
  594. BandwidthController::IptOpInsert)));
  595. expectUidOwnerMapValues(appStrUids, HAPPY_BOX_MATCH | PENALTY_BOX_MATCH, 0);
  596. // Remove the same UIDs from the whitelist and check the PENALTY_BOX_MATCH is still there.
  597. ASSERT_TRUE(isOk(mTc.updateUidOwnerMap(appStrUids, BandwidthController::IptJumpReturn,
  598. BandwidthController::IptOpDelete)));
  599. expectUidOwnerMapValues(appStrUids, PENALTY_BOX_MATCH, 0);
  600. // Remove the same UIDs from the blacklist and check the map is empty.
  601. ASSERT_TRUE(isOk(mTc.updateUidOwnerMap(appStrUids, BandwidthController::IptJumpReject,
  602. BandwidthController::IptOpDelete)));
  603. ASSERT_FALSE(isOk(mFakeUidOwnerMap.getFirstKey()));
  604. }
  605. TEST_F(TrafficControllerTest, TestDeleteWrongMatchSilentlyFails) {
  606. SKIP_IF_BPF_NOT_SUPPORTED;
  607. std::vector<std::string> appStrUids = {"1000", "1001", "10012"};
  608. // If the uid does not exist in the map, trying to delete a rule about it will fail.
  609. ASSERT_FALSE(isOk(mTc.updateUidOwnerMap(appStrUids, BandwidthController::IptJumpReturn,
  610. BandwidthController::IptOpDelete)));
  611. expectMapEmpty(mFakeUidOwnerMap);
  612. // Add blacklist rules for appStrUids.
  613. ASSERT_TRUE(isOk(mTc.updateUidOwnerMap(appStrUids, BandwidthController::IptJumpReturn,
  614. BandwidthController::IptOpInsert)));
  615. expectUidOwnerMapValues(appStrUids, HAPPY_BOX_MATCH, 0);
  616. // Delete (non-existent) blacklist rules for appStrUids, and check that this silently does
  617. // nothing if the uid is in the map but does not have blacklist match. This is required because
  618. // NetworkManagementService will try to remove a uid from blacklist after adding it to the
  619. // whitelist and if the remove fails it will not update the uid status.
  620. ASSERT_TRUE(isOk(mTc.updateUidOwnerMap(appStrUids, BandwidthController::IptJumpReject,
  621. BandwidthController::IptOpDelete)));
  622. expectUidOwnerMapValues(appStrUids, HAPPY_BOX_MATCH, 0);
  623. }
  624. TEST_F(TrafficControllerTest, TestAddUidInterfaceFilteringRules) {
  625. SKIP_IF_BPF_NOT_SUPPORTED;
  626. int iif0 = 15;
  627. ASSERT_TRUE(isOk(mTc.addUidInterfaceRules(iif0, {1000, 1001})));
  628. expectUidOwnerMapValues({"1000", "1001"}, IIF_MATCH, iif0);
  629. // Add some non-overlapping new uids. They should coexist with existing rules
  630. int iif1 = 16;
  631. ASSERT_TRUE(isOk(mTc.addUidInterfaceRules(iif1, {2000, 2001})));
  632. expectUidOwnerMapValues({"1000", "1001"}, IIF_MATCH, iif0);
  633. expectUidOwnerMapValues({"2000", "2001"}, IIF_MATCH, iif1);
  634. // Overwrite some existing uids
  635. int iif2 = 17;
  636. ASSERT_TRUE(isOk(mTc.addUidInterfaceRules(iif2, {1000, 2000})));
  637. expectUidOwnerMapValues({"1001"}, IIF_MATCH, iif0);
  638. expectUidOwnerMapValues({"2001"}, IIF_MATCH, iif1);
  639. expectUidOwnerMapValues({"1000", "2000"}, IIF_MATCH, iif2);
  640. }
  641. TEST_F(TrafficControllerTest, TestRemoveUidInterfaceFilteringRules) {
  642. SKIP_IF_BPF_NOT_SUPPORTED;
  643. int iif0 = 15;
  644. int iif1 = 16;
  645. ASSERT_TRUE(isOk(mTc.addUidInterfaceRules(iif0, {1000, 1001})));
  646. ASSERT_TRUE(isOk(mTc.addUidInterfaceRules(iif1, {2000, 2001})));
  647. expectUidOwnerMapValues({"1000", "1001"}, IIF_MATCH, iif0);
  648. expectUidOwnerMapValues({"2000", "2001"}, IIF_MATCH, iif1);
  649. // Rmove some uids
  650. ASSERT_TRUE(isOk(mTc.removeUidInterfaceRules({1001, 2001})));
  651. expectUidOwnerMapValues({"1000"}, IIF_MATCH, iif0);
  652. expectUidOwnerMapValues({"2000"}, IIF_MATCH, iif1);
  653. checkEachUidValue({1000, 2000}, IIF_MATCH); // Make sure there are only two uids remaining
  654. // Remove non-existent uids shouldn't fail
  655. ASSERT_TRUE(isOk(mTc.removeUidInterfaceRules({2000, 3000})));
  656. expectUidOwnerMapValues({"1000"}, IIF_MATCH, iif0);
  657. checkEachUidValue({1000}, IIF_MATCH); // Make sure there are only one uid remaining
  658. // Remove everything
  659. ASSERT_TRUE(isOk(mTc.removeUidInterfaceRules({1000})));
  660. expectMapEmpty(mFakeUidOwnerMap);
  661. }
  662. TEST_F(TrafficControllerTest, TestUidInterfaceFilteringRulesCoexistWithExistingMatches) {
  663. SKIP_IF_BPF_NOT_SUPPORTED;
  664. // Set up existing PENALTY_BOX_MATCH rules
  665. ASSERT_TRUE(isOk(mTc.updateUidOwnerMap({"1000", "1001", "10012"},
  666. BandwidthController::IptJumpReject,
  667. BandwidthController::IptOpInsert)));
  668. expectUidOwnerMapValues({"1000", "1001", "10012"}, PENALTY_BOX_MATCH, 0);
  669. // Add some partially-overlapping uid owner rules and check result
  670. int iif1 = 32;
  671. ASSERT_TRUE(isOk(mTc.addUidInterfaceRules(iif1, {10012, 10013, 10014})));
  672. expectUidOwnerMapValues({"1000", "1001"}, PENALTY_BOX_MATCH, 0);
  673. expectUidOwnerMapValues({"10012"}, PENALTY_BOX_MATCH | IIF_MATCH, iif1);
  674. expectUidOwnerMapValues({"10013", "10014"}, IIF_MATCH, iif1);
  675. // Removing some PENALTY_BOX_MATCH rules should not change uid interface rule
  676. ASSERT_TRUE(isOk(mTc.updateUidOwnerMap({"1001", "10012"}, BandwidthController::IptJumpReject,
  677. BandwidthController::IptOpDelete)));
  678. expectUidOwnerMapValues({"1000"}, PENALTY_BOX_MATCH, 0);
  679. expectUidOwnerMapValues({"10012", "10013", "10014"}, IIF_MATCH, iif1);
  680. // Remove all uid interface rules
  681. ASSERT_TRUE(isOk(mTc.removeUidInterfaceRules({10012, 10013, 10014})));
  682. expectUidOwnerMapValues({"1000"}, PENALTY_BOX_MATCH, 0);
  683. // Make sure these are the only uids left
  684. checkEachUidValue({1000}, PENALTY_BOX_MATCH);
  685. }
  686. TEST_F(TrafficControllerTest, TestUidInterfaceFilteringRulesCoexistWithNewMatches) {
  687. SKIP_IF_BPF_NOT_SUPPORTED;
  688. int iif1 = 56;
  689. // Set up existing uid interface rules
  690. ASSERT_TRUE(isOk(mTc.addUidInterfaceRules(iif1, {10001, 10002})));
  691. expectUidOwnerMapValues({"10001", "10002"}, IIF_MATCH, iif1);
  692. // Add some partially-overlapping doze rules
  693. EXPECT_EQ(0, mTc.replaceUidOwnerMap("fw_dozable", true, {10002, 10003}));
  694. expectUidOwnerMapValues({"10001"}, IIF_MATCH, iif1);
  695. expectUidOwnerMapValues({"10002"}, DOZABLE_MATCH | IIF_MATCH, iif1);
  696. expectUidOwnerMapValues({"10003"}, DOZABLE_MATCH, 0);
  697. // Introduce a third rule type (powersave) on various existing UIDs
  698. EXPECT_EQ(0, mTc.replaceUidOwnerMap("fw_powersave", true, {10000, 10001, 10002, 10003}));
  699. expectUidOwnerMapValues({"10000"}, POWERSAVE_MATCH, 0);
  700. expectUidOwnerMapValues({"10001"}, POWERSAVE_MATCH | IIF_MATCH, iif1);
  701. expectUidOwnerMapValues({"10002"}, POWERSAVE_MATCH | DOZABLE_MATCH | IIF_MATCH, iif1);
  702. expectUidOwnerMapValues({"10003"}, POWERSAVE_MATCH | DOZABLE_MATCH, 0);
  703. // Remove all doze rules
  704. EXPECT_EQ(0, mTc.replaceUidOwnerMap("fw_dozable", true, {}));
  705. expectUidOwnerMapValues({"10000"}, POWERSAVE_MATCH, 0);
  706. expectUidOwnerMapValues({"10001"}, POWERSAVE_MATCH | IIF_MATCH, iif1);
  707. expectUidOwnerMapValues({"10002"}, POWERSAVE_MATCH | IIF_MATCH, iif1);
  708. expectUidOwnerMapValues({"10003"}, POWERSAVE_MATCH, 0);
  709. // Remove all powersave rules, expect ownerMap to only have uid interface rules left
  710. EXPECT_EQ(0, mTc.replaceUidOwnerMap("fw_powersave", true, {}));
  711. expectUidOwnerMapValues({"10001", "10002"}, IIF_MATCH, iif1);
  712. // Make sure these are the only uids left
  713. checkEachUidValue({10001, 10002}, IIF_MATCH);
  714. }
  715. TEST_F(TrafficControllerTest, TestGrantInternetPermission) {
  716. SKIP_IF_BPF_NOT_SUPPORTED;
  717. std::vector<uid_t> appUids = {TEST_UID, TEST_UID2, TEST_UID3};
  718. mTc.setPermissionForUids(INetd::PERMISSION_INTERNET, appUids);
  719. expectMapEmpty(mFakeUidPermissionMap);
  720. expectPrivilegedUserSetEmpty();
  721. }
  722. TEST_F(TrafficControllerTest, TestRevokeInternetPermission) {
  723. SKIP_IF_BPF_NOT_SUPPORTED;
  724. std::vector<uid_t> appUids = {TEST_UID, TEST_UID2, TEST_UID3};
  725. mTc.setPermissionForUids(INetd::PERMISSION_NONE, appUids);
  726. expectUidPermissionMapValues(appUids, INetd::PERMISSION_NONE);
  727. }
  728. TEST_F(TrafficControllerTest, TestPermissionUninstalled) {
  729. SKIP_IF_BPF_NOT_SUPPORTED;
  730. std::vector<uid_t> appUids = {TEST_UID, TEST_UID2, TEST_UID3};
  731. mTc.setPermissionForUids(INetd::PERMISSION_UPDATE_DEVICE_STATS, appUids);
  732. expectUidPermissionMapValues(appUids, INetd::PERMISSION_UPDATE_DEVICE_STATS);
  733. expectPrivilegedUserSet(appUids);
  734. std::vector<uid_t> uidToRemove = {TEST_UID};
  735. mTc.setPermissionForUids(INetd::PERMISSION_UNINSTALLED, uidToRemove);
  736. std::vector<uid_t> uidRemain = {TEST_UID3, TEST_UID2};
  737. expectUidPermissionMapValues(uidRemain, INetd::PERMISSION_UPDATE_DEVICE_STATS);
  738. expectPrivilegedUserSet(uidRemain);
  739. mTc.setPermissionForUids(INetd::PERMISSION_UNINSTALLED, uidRemain);
  740. expectMapEmpty(mFakeUidPermissionMap);
  741. expectPrivilegedUserSetEmpty();
  742. }
  743. TEST_F(TrafficControllerTest, TestGrantUpdateStatsPermission) {
  744. SKIP_IF_BPF_NOT_SUPPORTED;
  745. std::vector<uid_t> appUids = {TEST_UID, TEST_UID2, TEST_UID3};
  746. mTc.setPermissionForUids(INetd::PERMISSION_UPDATE_DEVICE_STATS, appUids);
  747. expectUidPermissionMapValues(appUids, INetd::PERMISSION_UPDATE_DEVICE_STATS);
  748. expectPrivilegedUserSet(appUids);
  749. mTc.setPermissionForUids(INetd::PERMISSION_NONE, appUids);
  750. expectPrivilegedUserSetEmpty();
  751. expectUidPermissionMapValues(appUids, INetd::PERMISSION_NONE);
  752. }
  753. TEST_F(TrafficControllerTest, TestRevokeUpdateStatsPermission) {
  754. SKIP_IF_BPF_NOT_SUPPORTED;
  755. std::vector<uid_t> appUids = {TEST_UID, TEST_UID2, TEST_UID3};
  756. mTc.setPermissionForUids(INetd::PERMISSION_UPDATE_DEVICE_STATS, appUids);
  757. expectPrivilegedUserSet(appUids);
  758. std::vector<uid_t> uidToRemove = {TEST_UID};
  759. mTc.setPermissionForUids(INetd::PERMISSION_NONE, uidToRemove);
  760. std::vector<uid_t> uidRemain = {TEST_UID3, TEST_UID2};
  761. expectPrivilegedUserSet(uidRemain);
  762. mTc.setPermissionForUids(INetd::PERMISSION_NONE, uidRemain);
  763. expectPrivilegedUserSetEmpty();
  764. }
  765. TEST_F(TrafficControllerTest, TestGrantWrongPermission) {
  766. SKIP_IF_BPF_NOT_SUPPORTED;
  767. std::vector<uid_t> appUids = {TEST_UID, TEST_UID2, TEST_UID3};
  768. mTc.setPermissionForUids(INetd::PERMISSION_NONE, appUids);
  769. expectPrivilegedUserSetEmpty();
  770. expectUidPermissionMapValues(appUids, INetd::PERMISSION_NONE);
  771. }
  772. TEST_F(TrafficControllerTest, TestGrantDuplicatePermissionSlientlyFail) {
  773. SKIP_IF_BPF_NOT_SUPPORTED;
  774. std::vector<uid_t> appUids = {TEST_UID, TEST_UID2, TEST_UID3};
  775. mTc.setPermissionForUids(INetd::PERMISSION_INTERNET, appUids);
  776. expectMapEmpty(mFakeUidPermissionMap);
  777. std::vector<uid_t> uidToAdd = {TEST_UID};
  778. mTc.setPermissionForUids(INetd::PERMISSION_INTERNET, uidToAdd);
  779. expectPrivilegedUserSetEmpty();
  780. mTc.setPermissionForUids(INetd::PERMISSION_NONE, appUids);
  781. expectUidPermissionMapValues(appUids, INetd::PERMISSION_NONE);
  782. mTc.setPermissionForUids(INetd::PERMISSION_UPDATE_DEVICE_STATS, appUids);
  783. expectPrivilegedUserSet(appUids);
  784. mTc.setPermissionForUids(INetd::PERMISSION_UPDATE_DEVICE_STATS, uidToAdd);
  785. expectPrivilegedUserSet(appUids);
  786. mTc.setPermissionForUids(INetd::PERMISSION_NONE, appUids);
  787. expectPrivilegedUserSetEmpty();
  788. }
  789. } // namespace net
  790. } // namespace android