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::base::Result;
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 class BpfBasicTest : public testing::Test {
54   protected:
BpfBasicTest()55     BpfBasicTest() {}
56 };
57 
TEST_F(BpfBasicTest,TestCgroupMounted)58 TEST_F(BpfBasicTest, TestCgroupMounted) {
59     SKIP_IF_BPF_NOT_SUPPORTED;
60 
61     std::string cg2_path;
62 #if 0
63     // This is the correct way to fetch cg2_path, but it occasionally hits ASAN
64     // problems due to memory allocated in non ASAN code being freed later by us
65     ASSERT_EQ(true, CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cg2_path));
66 #else
67     ASSERT_EQ(true, CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, nullptr));
68     // Constant derived from //system/core/libprocessgroup/profiles/cgroups.json
69     cg2_path = "/dev/cg2_bpf";
70 #endif
71     ASSERT_EQ(0, access(cg2_path.c_str(), R_OK));
72     ASSERT_EQ(0, access((cg2_path + "/cgroup.controllers").c_str(), R_OK));
73 }
74 
TEST_F(BpfBasicTest,TestTrafficControllerSetUp)75 TEST_F(BpfBasicTest, TestTrafficControllerSetUp) {
76     SKIP_IF_BPF_NOT_SUPPORTED;
77 
78     ASSERT_EQ(0, access(BPF_EGRESS_PROG_PATH, R_OK));
79     ASSERT_EQ(0, access(BPF_INGRESS_PROG_PATH, R_OK));
80     ASSERT_EQ(0, access(XT_BPF_INGRESS_PROG_PATH, R_OK));
81     ASSERT_EQ(0, access(XT_BPF_EGRESS_PROG_PATH, R_OK));
82     ASSERT_EQ(0, access(COOKIE_TAG_MAP_PATH, R_OK));
83     ASSERT_EQ(0, access(UID_COUNTERSET_MAP_PATH, R_OK));
84     ASSERT_EQ(0, access(STATS_MAP_A_PATH, R_OK));
85     ASSERT_EQ(0, access(STATS_MAP_B_PATH, R_OK));
86     ASSERT_EQ(0, access(IFACE_INDEX_NAME_MAP_PATH, R_OK));
87     ASSERT_EQ(0, access(IFACE_STATS_MAP_PATH, R_OK));
88     ASSERT_EQ(0, access(CONFIGURATION_MAP_PATH, R_OK));
89     ASSERT_EQ(0, access(UID_OWNER_MAP_PATH, R_OK));
90 }
91 
TEST_F(BpfBasicTest,TestSocketFilterSetUp)92 TEST_F(BpfBasicTest, TestSocketFilterSetUp) {
93     SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED;
94 
95     ASSERT_EQ(0, access(CGROUP_SOCKET_PROG_PATH, R_OK));
96     ASSERT_EQ(0, access(UID_PERMISSION_MAP_PATH, R_OK));
97 }
98 
TEST_F(BpfBasicTest,TestTagSocket)99 TEST_F(BpfBasicTest, TestTagSocket) {
100     SKIP_IF_BPF_NOT_SUPPORTED;
101 
102     BpfMap<uint64_t, UidTagValue> cookieTagMap(COOKIE_TAG_MAP_PATH);
103     ASSERT_LE(0, cookieTagMap.getMap());
104     int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
105     ASSERT_LE(0, sock);
106     uint64_t cookie = getSocketCookie(sock);
107     ASSERT_NE(NONEXISTENT_COOKIE, cookie);
108     ASSERT_EQ(0, qtaguid_tagSocket(sock, TEST_TAG, TEST_UID));
109     Result<UidTagValue> tagResult = cookieTagMap.readValue(cookie);
110     ASSERT_RESULT_OK(tagResult);
111     ASSERT_EQ(TEST_UID, tagResult.value().uid);
112     ASSERT_EQ(TEST_TAG, tagResult.value().tag);
113     ASSERT_EQ(0, qtaguid_untagSocket(sock));
114     tagResult = cookieTagMap.readValue(cookie);
115     ASSERT_FALSE(tagResult.ok());
116     ASSERT_EQ(ENOENT, tagResult.error().code());
117 }
118 
TEST_F(BpfBasicTest,TestCloseSocketWithoutUntag)119 TEST_F(BpfBasicTest, TestCloseSocketWithoutUntag) {
120     SKIP_IF_BPF_NOT_SUPPORTED;
121 
122     BpfMap<uint64_t, UidTagValue> cookieTagMap(COOKIE_TAG_MAP_PATH);
123     ASSERT_LE(0, cookieTagMap.getMap());
124     int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
125     ASSERT_LE(0, sock);
126     uint64_t cookie = getSocketCookie(sock);
127     ASSERT_NE(NONEXISTENT_COOKIE, cookie);
128     ASSERT_EQ(0, qtaguid_tagSocket(sock, TEST_TAG, TEST_UID));
129     Result<UidTagValue> tagResult = cookieTagMap.readValue(cookie);
130     ASSERT_RESULT_OK(tagResult);
131     ASSERT_EQ(TEST_UID, tagResult.value().uid);
132     ASSERT_EQ(TEST_TAG, tagResult.value().tag);
133     ASSERT_EQ(0, close(sock));
134     // Check map periodically until sk destroy handler have done its job.
135     for (int i = 0; i < 10; i++) {
136         usleep(5000);  // 5ms
137         tagResult = cookieTagMap.readValue(cookie);
138         if (!tagResult.ok()) {
139             ASSERT_EQ(ENOENT, tagResult.error().code());
140             return;
141         }
142     }
143     FAIL() << "socket tag still exist after 50ms";
144 }
145 
TEST_F(BpfBasicTest,TestChangeCounterSet)146 TEST_F(BpfBasicTest, TestChangeCounterSet) {
147     SKIP_IF_BPF_NOT_SUPPORTED;
148 
149     BpfMap<uint32_t, uint8_t> uidCounterSetMap(UID_COUNTERSET_MAP_PATH);
150     ASSERT_LE(0, uidCounterSetMap.getMap());
151     ASSERT_EQ(0, qtaguid_setCounterSet(TEST_COUNTERSET, TEST_UID));
152     uid_t uid = TEST_UID;
153     Result<uint8_t> counterSetResult = uidCounterSetMap.readValue(uid);
154     ASSERT_RESULT_OK(counterSetResult);
155     ASSERT_EQ(TEST_COUNTERSET, counterSetResult.value());
156     ASSERT_EQ(0, qtaguid_setCounterSet(DEFAULT_COUNTERSET, TEST_UID));
157     counterSetResult = uidCounterSetMap.readValue(uid);
158     ASSERT_FALSE(counterSetResult.ok());
159     ASSERT_EQ(ENOENT, counterSetResult.error().code());
160 }
161 
TEST_F(BpfBasicTest,TestDeleteTagData)162 TEST_F(BpfBasicTest, TestDeleteTagData) {
163     SKIP_IF_BPF_NOT_SUPPORTED;
164 
165     BpfMap<StatsKey, StatsValue> statsMapA(STATS_MAP_A_PATH);
166     ASSERT_LE(0, statsMapA.getMap());
167     BpfMap<StatsKey, StatsValue> statsMapB(STATS_MAP_B_PATH);
168     ASSERT_LE(0, statsMapB.getMap());
169     BpfMap<uint32_t, StatsValue> appUidStatsMap(APP_UID_STATS_MAP_PATH);
170     ASSERT_LE(0, appUidStatsMap.getMap());
171 
172     StatsKey key = {.uid = TEST_UID, .tag = TEST_TAG, .counterSet = TEST_COUNTERSET,
173                     .ifaceIndex = 1};
174     StatsValue statsMapValue = {.rxPackets = 1, .rxBytes = 100};
175     EXPECT_RESULT_OK(statsMapB.writeValue(key, statsMapValue, BPF_ANY));
176     key.tag = 0;
177     EXPECT_RESULT_OK(statsMapA.writeValue(key, statsMapValue, BPF_ANY));
178     EXPECT_RESULT_OK(appUidStatsMap.writeValue(TEST_UID, statsMapValue, BPF_ANY));
179     ASSERT_EQ(0, qtaguid_deleteTagData(0, TEST_UID));
180     Result<StatsValue> statsResult = statsMapA.readValue(key);
181     ASSERT_FALSE(statsResult.ok());
182     ASSERT_EQ(ENOENT, statsResult.error().code());
183     statsResult = appUidStatsMap.readValue(TEST_UID);
184     ASSERT_FALSE(statsResult.ok());
185     ASSERT_EQ(ENOENT, statsResult.error().code());
186     key.tag = TEST_TAG;
187     statsResult = statsMapB.readValue(key);
188     ASSERT_FALSE(statsResult.ok());
189     ASSERT_EQ(ENOENT, statsResult.error().code());
190 }
191 
192 }
193 }
194