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