blob: a031dbb9e69d1a5186e37723a692715f0e730f33 [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
Maciej Żenczykowski9c8f7402022-05-31 03:22:32 -070024#define TEST_BPF_MAP
Ken Chen1647f602021-10-05 21:55:22 +080025#include "BpfHandler.h"
26
27using namespace android::bpf; // NOLINT(google-build-using-namespace): exempted
28
29namespace android {
30namespace net {
31
32using base::Result;
33
34constexpr int TEST_MAP_SIZE = 10;
35constexpr int TEST_COOKIE = 1;
36constexpr uid_t TEST_UID = 10086;
37constexpr uid_t TEST_UID2 = 54321;
38constexpr uint32_t TEST_TAG = 42;
39constexpr uint32_t TEST_COUNTERSET = 1;
40constexpr uint32_t TEST_PER_UID_STATS_ENTRIES_LIMIT = 3;
41constexpr uint32_t TEST_TOTAL_UID_STATS_ENTRIES_LIMIT = 7;
42
43#define ASSERT_VALID(x) ASSERT_TRUE((x).isValid())
44
45class 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 Żenczykowski9017a072022-06-16 14:49:27 -070052 BpfMapRO<uint32_t, uint32_t> mFakeConfigurationMap;
Ken Chen1647f602021-10-05 21:55:22 +080053 BpfMap<uint32_t, uint8_t> mFakeUidPermissionMap;
54
55 void SetUp() {
56 std::lock_guard guard(mBh.mMutex);
57 ASSERT_EQ(0, setrlimitForTest());
58
Maciej Żenczykowski9c8f7402022-05-31 03:22:32 -070059 mFakeCookieTagMap.resetMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE);
Ken Chen1647f602021-10-05 21:55:22 +080060 ASSERT_VALID(mFakeCookieTagMap);
61
Maciej Żenczykowski9c8f7402022-05-31 03:22:32 -070062 mFakeStatsMapA.resetMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE);
Ken Chen1647f602021-10-05 21:55:22 +080063 ASSERT_VALID(mFakeStatsMapA);
64
Maciej Żenczykowski9017a072022-06-16 14:49:27 -070065 mFakeConfigurationMap.resetMap(BPF_MAP_TYPE_ARRAY, CONFIGURATION_MAP_SIZE);
Ken Chen1647f602021-10-05 21:55:22 +080066 ASSERT_VALID(mFakeConfigurationMap);
67
Maciej Żenczykowski9c8f7402022-05-31 03:22:32 -070068 mFakeUidPermissionMap.resetMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, 0);
Ken Chen1647f602021-10-05 21:55:22 +080069 ASSERT_VALID(mFakeUidPermissionMap);
70
Maciej Żenczykowski787de012022-05-31 03:15:12 -070071 mBh.mCookieTagMap = mFakeCookieTagMap;
Ken Chen1647f602021-10-05 21:55:22 +080072 ASSERT_VALID(mBh.mCookieTagMap);
Maciej Żenczykowski787de012022-05-31 03:15:12 -070073 mBh.mStatsMapA = mFakeStatsMapA;
Ken Chen1647f602021-10-05 21:55:22 +080074 ASSERT_VALID(mBh.mStatsMapA);
Maciej Żenczykowski787de012022-05-31 03:15:12 -070075 mBh.mConfigurationMap = mFakeConfigurationMap;
Ken Chen1647f602021-10-05 21:55:22 +080076 ASSERT_VALID(mBh.mConfigurationMap);
77 // Always write to stats map A by default.
Maciej Żenczykowski9017a072022-06-16 14:49:27 -070078 static_assert(SELECT_MAP_A == 0, "bpf map arrays are zero-initialized");
79
Maciej Żenczykowski787de012022-05-31 03:15:12 -070080 mBh.mUidPermissionMap = mFakeUidPermissionMap;
Ken Chen1647f602021-10-05 21:55:22 +080081 ASSERT_VALID(mBh.mUidPermissionMap);
82 }
83
Ken Chen1647f602021-10-05 21:55:22 +080084 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
139TEST_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
148TEST_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
156TEST_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
169TEST_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
178TEST_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 Chenbcc0f5b2022-03-07 14:13:49 +0800184TEST_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 Chen478c0eb2022-03-04 21:16:59 +0800191TEST_F(BpfHandlerTest, TestTagSocketWithUnsupportedProtocol) {
192 int rawSocket = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
193 EXPECT_LE(0, rawSocket);
Hungming Chenbcc0f5b2022-03-07 14:13:49 +0800194 EXPECT_NE(NONEXISTENT_COOKIE, getSocketCookie(rawSocket));
Hungming Chen478c0eb2022-03-04 21:16:59 +0800195 EXPECT_EQ(-EPROTONOSUPPORT, mBh.tagSocket(rawSocket, TEST_TAG, TEST_UID, TEST_UID));
196}
197
Ken Chen1647f602021-10-05 21:55:22 +0800198TEST_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
205TEST_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 Chen436547e2022-02-18 17:52:11 +0800219
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 Chen1647f602021-10-05 21:55:22 +0800225}
226
227TEST_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
235TEST_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
246TEST_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