Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | * BpfHandlerTest.cpp - unit tests for BpfHandler.cpp |
| 17 | */ |
| 18 | |
Hungming Chen | 436547e | 2022-02-18 17:52:11 +0800 | [diff] [blame] | 19 | #include <private/android_filesystem_config.h> |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 20 | #include <sys/socket.h> |
| 21 | |
| 22 | #include <gtest/gtest.h> |
| 23 | |
Maciej Żenczykowski | 9c8f740 | 2022-05-31 03:22:32 -0700 | [diff] [blame] | 24 | #define TEST_BPF_MAP |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 25 | #include "BpfHandler.h" |
| 26 | |
| 27 | using namespace android::bpf; // NOLINT(google-build-using-namespace): exempted |
| 28 | |
| 29 | namespace android { |
| 30 | namespace net { |
| 31 | |
| 32 | using base::Result; |
| 33 | |
| 34 | constexpr int TEST_MAP_SIZE = 10; |
| 35 | constexpr int TEST_COOKIE = 1; |
| 36 | constexpr uid_t TEST_UID = 10086; |
| 37 | constexpr uid_t TEST_UID2 = 54321; |
| 38 | constexpr uint32_t TEST_TAG = 42; |
| 39 | constexpr uint32_t TEST_COUNTERSET = 1; |
| 40 | constexpr uint32_t TEST_PER_UID_STATS_ENTRIES_LIMIT = 3; |
| 41 | constexpr uint32_t TEST_TOTAL_UID_STATS_ENTRIES_LIMIT = 7; |
| 42 | |
| 43 | #define ASSERT_VALID(x) ASSERT_TRUE((x).isValid()) |
| 44 | |
| 45 | class BpfHandlerTest : public ::testing::Test { |
| 46 | protected: |
| 47 | BpfHandlerTest() |
| 48 | : mBh(TEST_PER_UID_STATS_ENTRIES_LIMIT, TEST_TOTAL_UID_STATS_ENTRIES_LIMIT) {} |
| 49 | BpfHandler mBh; |
| 50 | BpfMap<uint64_t, UidTagValue> mFakeCookieTagMap; |
| 51 | BpfMap<StatsKey, StatsValue> mFakeStatsMapA; |
Maciej Żenczykowski | 9017a07 | 2022-06-16 14:49:27 -0700 | [diff] [blame] | 52 | BpfMapRO<uint32_t, uint32_t> mFakeConfigurationMap; |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 53 | BpfMap<uint32_t, uint8_t> mFakeUidPermissionMap; |
| 54 | |
| 55 | void SetUp() { |
| 56 | std::lock_guard guard(mBh.mMutex); |
| 57 | ASSERT_EQ(0, setrlimitForTest()); |
| 58 | |
Maciej Żenczykowski | 9c8f740 | 2022-05-31 03:22:32 -0700 | [diff] [blame] | 59 | mFakeCookieTagMap.resetMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 60 | ASSERT_VALID(mFakeCookieTagMap); |
| 61 | |
Maciej Żenczykowski | 9c8f740 | 2022-05-31 03:22:32 -0700 | [diff] [blame] | 62 | mFakeStatsMapA.resetMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 63 | ASSERT_VALID(mFakeStatsMapA); |
| 64 | |
Maciej Żenczykowski | 9017a07 | 2022-06-16 14:49:27 -0700 | [diff] [blame] | 65 | mFakeConfigurationMap.resetMap(BPF_MAP_TYPE_ARRAY, CONFIGURATION_MAP_SIZE); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 66 | ASSERT_VALID(mFakeConfigurationMap); |
| 67 | |
Maciej Żenczykowski | 9c8f740 | 2022-05-31 03:22:32 -0700 | [diff] [blame] | 68 | mFakeUidPermissionMap.resetMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, 0); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 69 | ASSERT_VALID(mFakeUidPermissionMap); |
| 70 | |
Maciej Żenczykowski | 787de01 | 2022-05-31 03:15:12 -0700 | [diff] [blame] | 71 | mBh.mCookieTagMap = mFakeCookieTagMap; |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 72 | ASSERT_VALID(mBh.mCookieTagMap); |
Maciej Żenczykowski | 787de01 | 2022-05-31 03:15:12 -0700 | [diff] [blame] | 73 | mBh.mStatsMapA = mFakeStatsMapA; |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 74 | ASSERT_VALID(mBh.mStatsMapA); |
Maciej Żenczykowski | 787de01 | 2022-05-31 03:15:12 -0700 | [diff] [blame] | 75 | mBh.mConfigurationMap = mFakeConfigurationMap; |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 76 | ASSERT_VALID(mBh.mConfigurationMap); |
| 77 | // Always write to stats map A by default. |
Maciej Żenczykowski | 9017a07 | 2022-06-16 14:49:27 -0700 | [diff] [blame] | 78 | static_assert(SELECT_MAP_A == 0, "bpf map arrays are zero-initialized"); |
| 79 | |
Maciej Żenczykowski | 787de01 | 2022-05-31 03:15:12 -0700 | [diff] [blame] | 80 | mBh.mUidPermissionMap = mFakeUidPermissionMap; |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 81 | ASSERT_VALID(mBh.mUidPermissionMap); |
| 82 | } |
| 83 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 84 | int setUpSocketAndTag(int protocol, uint64_t* cookie, uint32_t tag, uid_t uid, |
| 85 | uid_t realUid) { |
| 86 | int sock = socket(protocol, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 87 | EXPECT_LE(0, sock); |
| 88 | *cookie = getSocketCookie(sock); |
| 89 | EXPECT_NE(NONEXISTENT_COOKIE, *cookie); |
| 90 | EXPECT_EQ(0, mBh.tagSocket(sock, tag, uid, realUid)); |
| 91 | return sock; |
| 92 | } |
| 93 | |
| 94 | void expectUidTag(uint64_t cookie, uid_t uid, uint32_t tag) { |
| 95 | Result<UidTagValue> tagResult = mFakeCookieTagMap.readValue(cookie); |
| 96 | ASSERT_RESULT_OK(tagResult); |
| 97 | EXPECT_EQ(uid, tagResult.value().uid); |
| 98 | EXPECT_EQ(tag, tagResult.value().tag); |
| 99 | } |
| 100 | |
| 101 | void expectNoTag(uint64_t cookie) { EXPECT_FALSE(mFakeCookieTagMap.readValue(cookie).ok()); } |
| 102 | |
| 103 | void populateFakeStats(uint64_t cookie, uint32_t uid, uint32_t tag, StatsKey* key) { |
| 104 | UidTagValue cookieMapkey = {.uid = (uint32_t)uid, .tag = tag}; |
| 105 | EXPECT_RESULT_OK(mFakeCookieTagMap.writeValue(cookie, cookieMapkey, BPF_ANY)); |
| 106 | *key = {.uid = uid, .tag = tag, .counterSet = TEST_COUNTERSET, .ifaceIndex = 1}; |
| 107 | StatsValue statsMapValue = {.rxPackets = 1, .rxBytes = 100}; |
| 108 | EXPECT_RESULT_OK(mFakeStatsMapA.writeValue(*key, statsMapValue, BPF_ANY)); |
| 109 | key->tag = 0; |
| 110 | EXPECT_RESULT_OK(mFakeStatsMapA.writeValue(*key, statsMapValue, BPF_ANY)); |
| 111 | // put tag information back to statsKey |
| 112 | key->tag = tag; |
| 113 | } |
| 114 | |
| 115 | template <class Key, class Value> |
| 116 | void expectMapEmpty(BpfMap<Key, Value>& map) { |
| 117 | auto isEmpty = map.isEmpty(); |
| 118 | EXPECT_RESULT_OK(isEmpty); |
| 119 | EXPECT_TRUE(isEmpty.value()); |
| 120 | } |
| 121 | |
| 122 | void expectTagSocketReachLimit(uint32_t tag, uint32_t uid) { |
| 123 | int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 124 | EXPECT_LE(0, sock); |
| 125 | if (sock < 0) return; |
| 126 | uint64_t sockCookie = getSocketCookie(sock); |
| 127 | EXPECT_NE(NONEXISTENT_COOKIE, sockCookie); |
| 128 | EXPECT_EQ(-EMFILE, mBh.tagSocket(sock, tag, uid, uid)); |
| 129 | expectNoTag(sockCookie); |
| 130 | |
| 131 | // Delete stats entries then tag socket success |
| 132 | StatsKey key = {.uid = uid, .tag = 0, .counterSet = TEST_COUNTERSET, .ifaceIndex = 1}; |
| 133 | ASSERT_RESULT_OK(mFakeStatsMapA.deleteValue(key)); |
| 134 | EXPECT_EQ(0, mBh.tagSocket(sock, tag, uid, uid)); |
| 135 | expectUidTag(sockCookie, uid, tag); |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | TEST_F(BpfHandlerTest, TestTagSocketV4) { |
| 140 | uint64_t sockCookie; |
| 141 | int v4socket = setUpSocketAndTag(AF_INET, &sockCookie, TEST_TAG, TEST_UID, TEST_UID); |
| 142 | expectUidTag(sockCookie, TEST_UID, TEST_TAG); |
| 143 | ASSERT_EQ(0, mBh.untagSocket(v4socket)); |
| 144 | expectNoTag(sockCookie); |
| 145 | expectMapEmpty(mFakeCookieTagMap); |
| 146 | } |
| 147 | |
| 148 | TEST_F(BpfHandlerTest, TestReTagSocket) { |
| 149 | uint64_t sockCookie; |
| 150 | int v4socket = setUpSocketAndTag(AF_INET, &sockCookie, TEST_TAG, TEST_UID, TEST_UID); |
| 151 | expectUidTag(sockCookie, TEST_UID, TEST_TAG); |
| 152 | ASSERT_EQ(0, mBh.tagSocket(v4socket, TEST_TAG + 1, TEST_UID + 1, TEST_UID + 1)); |
| 153 | expectUidTag(sockCookie, TEST_UID + 1, TEST_TAG + 1); |
| 154 | } |
| 155 | |
| 156 | TEST_F(BpfHandlerTest, TestTagTwoSockets) { |
| 157 | uint64_t sockCookie1; |
| 158 | uint64_t sockCookie2; |
| 159 | int v4socket1 = setUpSocketAndTag(AF_INET, &sockCookie1, TEST_TAG, TEST_UID, TEST_UID); |
| 160 | setUpSocketAndTag(AF_INET, &sockCookie2, TEST_TAG, TEST_UID, TEST_UID); |
| 161 | expectUidTag(sockCookie1, TEST_UID, TEST_TAG); |
| 162 | expectUidTag(sockCookie2, TEST_UID, TEST_TAG); |
| 163 | ASSERT_EQ(0, mBh.untagSocket(v4socket1)); |
| 164 | expectNoTag(sockCookie1); |
| 165 | expectUidTag(sockCookie2, TEST_UID, TEST_TAG); |
| 166 | ASSERT_FALSE(mFakeCookieTagMap.getNextKey(sockCookie2).ok()); |
| 167 | } |
| 168 | |
| 169 | TEST_F(BpfHandlerTest, TestTagSocketV6) { |
| 170 | uint64_t sockCookie; |
| 171 | int v6socket = setUpSocketAndTag(AF_INET6, &sockCookie, TEST_TAG, TEST_UID, TEST_UID); |
| 172 | expectUidTag(sockCookie, TEST_UID, TEST_TAG); |
| 173 | ASSERT_EQ(0, mBh.untagSocket(v6socket)); |
| 174 | expectNoTag(sockCookie); |
| 175 | expectMapEmpty(mFakeCookieTagMap); |
| 176 | } |
| 177 | |
| 178 | TEST_F(BpfHandlerTest, TestTagInvalidSocket) { |
| 179 | int invalidSocket = -1; |
| 180 | ASSERT_GT(0, mBh.tagSocket(invalidSocket, TEST_TAG, TEST_UID, TEST_UID)); |
| 181 | expectMapEmpty(mFakeCookieTagMap); |
| 182 | } |
| 183 | |
Hungming Chen | bcc0f5b | 2022-03-07 14:13:49 +0800 | [diff] [blame] | 184 | TEST_F(BpfHandlerTest, TestTagSocketWithUnsupportedFamily) { |
| 185 | int packetSocket = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 186 | EXPECT_LE(0, packetSocket); |
| 187 | EXPECT_NE(NONEXISTENT_COOKIE, getSocketCookie(packetSocket)); |
| 188 | EXPECT_EQ(-EAFNOSUPPORT, mBh.tagSocket(packetSocket, TEST_TAG, TEST_UID, TEST_UID)); |
| 189 | } |
| 190 | |
Hungming Chen | 478c0eb | 2022-03-04 21:16:59 +0800 | [diff] [blame] | 191 | TEST_F(BpfHandlerTest, TestTagSocketWithUnsupportedProtocol) { |
| 192 | int rawSocket = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW); |
| 193 | EXPECT_LE(0, rawSocket); |
Hungming Chen | bcc0f5b | 2022-03-07 14:13:49 +0800 | [diff] [blame] | 194 | EXPECT_NE(NONEXISTENT_COOKIE, getSocketCookie(rawSocket)); |
Hungming Chen | 478c0eb | 2022-03-04 21:16:59 +0800 | [diff] [blame] | 195 | EXPECT_EQ(-EPROTONOSUPPORT, mBh.tagSocket(rawSocket, TEST_TAG, TEST_UID, TEST_UID)); |
| 196 | } |
| 197 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 198 | TEST_F(BpfHandlerTest, TestTagSocketWithoutPermission) { |
| 199 | int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 200 | ASSERT_NE(-1, sock); |
| 201 | ASSERT_EQ(-EPERM, mBh.tagSocket(sock, TEST_TAG, TEST_UID, TEST_UID2)); |
| 202 | expectMapEmpty(mFakeCookieTagMap); |
| 203 | } |
| 204 | |
| 205 | TEST_F(BpfHandlerTest, TestTagSocketWithPermission) { |
| 206 | // Grant permission to real uid. In practice, the uid permission map will be updated by |
| 207 | // TrafficController::setPermissionForUids(). |
| 208 | uid_t realUid = TEST_UID2; |
| 209 | ASSERT_RESULT_OK(mFakeUidPermissionMap.writeValue(realUid, |
| 210 | BPF_PERMISSION_UPDATE_DEVICE_STATS, BPF_ANY)); |
| 211 | |
| 212 | // Tag a socket to a different uid other then realUid. |
| 213 | uint64_t sockCookie; |
| 214 | int v6socket = setUpSocketAndTag(AF_INET6, &sockCookie, TEST_TAG, TEST_UID, realUid); |
| 215 | expectUidTag(sockCookie, TEST_UID, TEST_TAG); |
| 216 | EXPECT_EQ(0, mBh.untagSocket(v6socket)); |
| 217 | expectNoTag(sockCookie); |
| 218 | expectMapEmpty(mFakeCookieTagMap); |
Hungming Chen | 436547e | 2022-02-18 17:52:11 +0800 | [diff] [blame] | 219 | |
| 220 | // Tag a socket to AID_CLAT other then realUid. |
| 221 | int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 222 | ASSERT_NE(-1, sock); |
| 223 | ASSERT_EQ(-EPERM, mBh.tagSocket(sock, TEST_TAG, AID_CLAT, realUid)); |
| 224 | expectMapEmpty(mFakeCookieTagMap); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | TEST_F(BpfHandlerTest, TestUntagInvalidSocket) { |
| 228 | int invalidSocket = -1; |
| 229 | ASSERT_GT(0, mBh.untagSocket(invalidSocket)); |
| 230 | int v4socket = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 231 | ASSERT_GT(0, mBh.untagSocket(v4socket)); |
| 232 | expectMapEmpty(mFakeCookieTagMap); |
| 233 | } |
| 234 | |
| 235 | TEST_F(BpfHandlerTest, TestTagSocketReachLimitFail) { |
| 236 | uid_t uid = TEST_UID; |
| 237 | StatsKey tagStatsMapKey[3]; |
| 238 | for (int i = 0; i < 3; i++) { |
| 239 | uint64_t cookie = TEST_COOKIE + i; |
| 240 | uint32_t tag = TEST_TAG + i; |
| 241 | populateFakeStats(cookie, uid, tag, &tagStatsMapKey[i]); |
| 242 | } |
| 243 | expectTagSocketReachLimit(TEST_TAG, TEST_UID); |
| 244 | } |
| 245 | |
| 246 | TEST_F(BpfHandlerTest, TestTagSocketReachTotalLimitFail) { |
| 247 | StatsKey tagStatsMapKey[4]; |
| 248 | for (int i = 0; i < 4; i++) { |
| 249 | uint64_t cookie = TEST_COOKIE + i; |
| 250 | uint32_t tag = TEST_TAG + i; |
| 251 | uid_t uid = TEST_UID + i; |
| 252 | populateFakeStats(cookie, uid, tag, &tagStatsMapKey[i]); |
| 253 | } |
| 254 | expectTagSocketReachLimit(TEST_TAG, TEST_UID); |
| 255 | } |
| 256 | |
| 257 | } // namespace net |
| 258 | } // namespace android |