• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 
17 #include <string>
18 
19 #include <fcntl.h>
20 #include <inttypes.h>
21 #include <limits.h>
22 #include <linux/inet_diag.h>
23 #include <linux/sock_diag.h>
24 #include <net/if.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 
29 #include <gtest/gtest.h>
30 
31 #include <cutils/qtaguid.h>
32 #include <processgroup/processgroup.h>
33 
34 #include <android-base/stringprintf.h>
35 #include <android-base/strings.h>
36 
37 #include "bpf/BpfMap.h"
38 #include "bpf/BpfUtils.h"
39 #include "netdbpf/bpf_shared.h"
40 
41 using android::netdutils::StatusOr;
42 
43 namespace android {
44 namespace bpf {
45 
46 // Use the upper limit of uid to avoid conflict with real app uids. We can't use UID_MAX because
47 // it's -1, which is INVALID_UID.
48 constexpr uid_t TEST_UID = UID_MAX - 1;
49 constexpr uint32_t TEST_TAG = 42;
50 constexpr int TEST_COUNTERSET = 1;
51 constexpr int DEFAULT_COUNTERSET = 0;
52 
53 #define SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED                                            \
54     do {                                                                              \
55         if (android::bpf::getBpfSupportLevel() != android::bpf::BpfLevel::EXTENDED) { \
56             GTEST_LOG_(INFO) << "This test is skipped since extended bpf feature"     \
57                              << "not supported\n";                                    \
58             return;                                                                   \
59         }                                                                             \
60     } while (0)
61 
62 class BpfBasicTest : public testing::Test {
63   protected:
BpfBasicTest()64     BpfBasicTest() {}
65 };
66 
TEST_F(BpfBasicTest,TestCgroupMounted)67 TEST_F(BpfBasicTest, TestCgroupMounted) {
68     SKIP_IF_BPF_NOT_SUPPORTED;
69 
70     std::string cg2_path;
71     ASSERT_EQ(true, CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cg2_path));
72     ASSERT_EQ(0, access(cg2_path.c_str(), R_OK));
73     ASSERT_EQ(0, access((cg2_path + "/cgroup.controllers").c_str(), R_OK));
74 }
75 
TEST_F(BpfBasicTest,TestTrafficControllerSetUp)76 TEST_F(BpfBasicTest, TestTrafficControllerSetUp) {
77     SKIP_IF_BPF_NOT_SUPPORTED;
78 
79     ASSERT_EQ(0, access(BPF_EGRESS_PROG_PATH, R_OK));
80     ASSERT_EQ(0, access(BPF_INGRESS_PROG_PATH, R_OK));
81     ASSERT_EQ(0, access(XT_BPF_INGRESS_PROG_PATH, R_OK));
82     ASSERT_EQ(0, access(XT_BPF_EGRESS_PROG_PATH, R_OK));
83     ASSERT_EQ(0, access(COOKIE_TAG_MAP_PATH, R_OK));
84     ASSERT_EQ(0, access(UID_COUNTERSET_MAP_PATH, R_OK));
85     ASSERT_EQ(0, access(STATS_MAP_A_PATH, R_OK));
86     ASSERT_EQ(0, access(STATS_MAP_B_PATH, R_OK));
87     ASSERT_EQ(0, access(IFACE_INDEX_NAME_MAP_PATH, R_OK));
88     ASSERT_EQ(0, access(IFACE_STATS_MAP_PATH, R_OK));
89     ASSERT_EQ(0, access(CONFIGURATION_MAP_PATH, R_OK));
90     ASSERT_EQ(0, access(UID_OWNER_MAP_PATH, R_OK));
91 }
92 
TEST_F(BpfBasicTest,TestSocketFilterSetUp)93 TEST_F(BpfBasicTest, TestSocketFilterSetUp) {
94     SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED;
95 
96     ASSERT_EQ(0, access(CGROUP_SOCKET_PROG_PATH, R_OK));
97     ASSERT_EQ(0, access(UID_PERMISSION_MAP_PATH, R_OK));
98 }
99 
TEST_F(BpfBasicTest,TestTagSocket)100 TEST_F(BpfBasicTest, TestTagSocket) {
101     SKIP_IF_BPF_NOT_SUPPORTED;
102 
103     BpfMap<uint64_t, UidTag> cookieTagMap(mapRetrieve(COOKIE_TAG_MAP_PATH, 0));
104     ASSERT_LE(0, cookieTagMap.getMap());
105     int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
106     ASSERT_LE(0, sock);
107     uint64_t cookie = getSocketCookie(sock);
108     ASSERT_NE(NONEXISTENT_COOKIE, cookie);
109     ASSERT_EQ(0, qtaguid_tagSocket(sock, TEST_TAG, TEST_UID));
110     StatusOr<UidTag> tagResult = cookieTagMap.readValue(cookie);
111     ASSERT_TRUE(isOk(tagResult));
112     ASSERT_EQ(TEST_UID, tagResult.value().uid);
113     ASSERT_EQ(TEST_TAG, tagResult.value().tag);
114     ASSERT_EQ(0, qtaguid_untagSocket(sock));
115     tagResult = cookieTagMap.readValue(cookie);
116     ASSERT_FALSE(isOk(tagResult));
117     ASSERT_EQ(ENOENT, tagResult.status().code());
118 }
119 
TEST_F(BpfBasicTest,TestCloseSocketWithoutUntag)120 TEST_F(BpfBasicTest, TestCloseSocketWithoutUntag) {
121     SKIP_IF_BPF_NOT_SUPPORTED;
122 
123     BpfMap<uint64_t, UidTag> cookieTagMap(mapRetrieve(COOKIE_TAG_MAP_PATH, 0));
124     ASSERT_LE(0, cookieTagMap.getMap());
125     int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
126     ASSERT_LE(0, sock);
127     uint64_t cookie = getSocketCookie(sock);
128     ASSERT_NE(NONEXISTENT_COOKIE, cookie);
129     ASSERT_EQ(0, qtaguid_tagSocket(sock, TEST_TAG, TEST_UID));
130     StatusOr<UidTag> tagResult = cookieTagMap.readValue(cookie);
131     ASSERT_TRUE(isOk(tagResult));
132     ASSERT_EQ(TEST_UID, tagResult.value().uid);
133     ASSERT_EQ(TEST_TAG, tagResult.value().tag);
134     ASSERT_EQ(0, close(sock));
135     // Check map periodically until sk destroy handler have done its job.
136     for (int i = 0; i < 10; i++) {
137         usleep(5000);  // 5ms
138         tagResult = cookieTagMap.readValue(cookie);
139         if (!isOk(tagResult)) {
140             ASSERT_EQ(ENOENT, tagResult.status().code());
141             return;
142         }
143     }
144     FAIL() << "socket tag still exist after 50ms";
145 }
146 
TEST_F(BpfBasicTest,TestChangeCounterSet)147 TEST_F(BpfBasicTest, TestChangeCounterSet) {
148     SKIP_IF_BPF_NOT_SUPPORTED;
149 
150     BpfMap<uint32_t, uint8_t> uidCounterSetMap(mapRetrieve(UID_COUNTERSET_MAP_PATH, 0));
151     ASSERT_LE(0, uidCounterSetMap.getMap());
152     ASSERT_EQ(0, qtaguid_setCounterSet(TEST_COUNTERSET, TEST_UID));
153     uid_t uid = TEST_UID;
154     StatusOr<uint8_t> counterSetResult = uidCounterSetMap.readValue(uid);
155     ASSERT_TRUE(isOk(counterSetResult));
156     ASSERT_EQ(TEST_COUNTERSET, counterSetResult.value());
157     ASSERT_EQ(0, qtaguid_setCounterSet(DEFAULT_COUNTERSET, TEST_UID));
158     counterSetResult = uidCounterSetMap.readValue(uid);
159     ASSERT_FALSE(isOk(counterSetResult));
160     ASSERT_EQ(ENOENT, counterSetResult.status().code());
161 }
162 
TEST_F(BpfBasicTest,TestDeleteTagData)163 TEST_F(BpfBasicTest, TestDeleteTagData) {
164     SKIP_IF_BPF_NOT_SUPPORTED;
165 
166     BpfMap<StatsKey, StatsValue> statsMapA(mapRetrieve(STATS_MAP_A_PATH, 0));
167     ASSERT_LE(0, statsMapA.getMap());
168     BpfMap<StatsKey, StatsValue> statsMapB(mapRetrieve(STATS_MAP_B_PATH, 0));
169     ASSERT_LE(0, statsMapB.getMap());
170     BpfMap<uint32_t, StatsValue> appUidStatsMap(mapRetrieve(APP_UID_STATS_MAP_PATH, 0));
171     ASSERT_LE(0, appUidStatsMap.getMap());
172 
173     StatsKey key = {.uid = TEST_UID, .tag = TEST_TAG, .counterSet = TEST_COUNTERSET,
174                     .ifaceIndex = 1};
175     StatsValue statsMapValue = {.rxPackets = 1, .rxBytes = 100};
176     EXPECT_TRUE(isOk(statsMapB.writeValue(key, statsMapValue, BPF_ANY)));
177     key.tag = 0;
178     EXPECT_TRUE(isOk(statsMapA.writeValue(key, statsMapValue, BPF_ANY)));
179     EXPECT_TRUE(isOk(appUidStatsMap.writeValue(TEST_UID, statsMapValue, BPF_ANY)));
180     ASSERT_EQ(0, qtaguid_deleteTagData(0, TEST_UID));
181     StatusOr<StatsValue> statsResult = statsMapA.readValue(key);
182     ASSERT_FALSE(isOk(statsResult));
183     ASSERT_EQ(ENOENT, statsResult.status().code());
184     statsResult = appUidStatsMap.readValue(TEST_UID);
185     ASSERT_FALSE(isOk(statsResult));
186     ASSERT_EQ(ENOENT, statsResult.status().code());
187     key.tag = TEST_TAG;
188     statsResult = statsMapB.readValue(key);
189     ASSERT_FALSE(isOk(statsResult));
190     ASSERT_EQ(ENOENT, statsResult.status().code());
191 }
192 
193 }
194 }
195