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     std::string cg2_path;
60     ASSERT_EQ(true, CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cg2_path));
61     ASSERT_EQ(0, access(cg2_path.c_str(), R_OK));
62     ASSERT_EQ(0, access((cg2_path + "/cgroup.controllers").c_str(), R_OK));
63 }
64 
TEST_F(BpfBasicTest,TestTrafficControllerSetUp)65 TEST_F(BpfBasicTest, TestTrafficControllerSetUp) {
66     ASSERT_EQ(0, access(BPF_EGRESS_PROG_PATH, R_OK));
67     ASSERT_EQ(0, access(BPF_INGRESS_PROG_PATH, R_OK));
68     ASSERT_EQ(0, access(XT_BPF_INGRESS_PROG_PATH, R_OK));
69     ASSERT_EQ(0, access(XT_BPF_EGRESS_PROG_PATH, R_OK));
70     ASSERT_EQ(0, access(COOKIE_TAG_MAP_PATH, R_OK));
71     ASSERT_EQ(0, access(UID_COUNTERSET_MAP_PATH, R_OK));
72     ASSERT_EQ(0, access(STATS_MAP_A_PATH, R_OK));
73     ASSERT_EQ(0, access(STATS_MAP_B_PATH, R_OK));
74     ASSERT_EQ(0, access(IFACE_INDEX_NAME_MAP_PATH, R_OK));
75     ASSERT_EQ(0, access(IFACE_STATS_MAP_PATH, R_OK));
76     ASSERT_EQ(0, access(CONFIGURATION_MAP_PATH, R_OK));
77     ASSERT_EQ(0, access(UID_OWNER_MAP_PATH, R_OK));
78 }
79 
TEST_F(BpfBasicTest,TestSocketFilterSetUp)80 TEST_F(BpfBasicTest, TestSocketFilterSetUp) {
81     SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED;
82 
83     ASSERT_EQ(0, access(CGROUP_SOCKET_PROG_PATH, R_OK));
84     ASSERT_EQ(0, access(UID_PERMISSION_MAP_PATH, R_OK));
85 }
86 
TEST_F(BpfBasicTest,TestTagSocket)87 TEST_F(BpfBasicTest, TestTagSocket) {
88     BpfMap<uint64_t, UidTagValue> cookieTagMap(COOKIE_TAG_MAP_PATH);
89     ASSERT_LE(0, cookieTagMap.getMap());
90     int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
91     ASSERT_LE(0, sock);
92     uint64_t cookie = getSocketCookie(sock);
93     ASSERT_NE(NONEXISTENT_COOKIE, cookie);
94     ASSERT_EQ(0, qtaguid_tagSocket(sock, TEST_TAG, TEST_UID));
95     Result<UidTagValue> tagResult = cookieTagMap.readValue(cookie);
96     ASSERT_RESULT_OK(tagResult);
97     ASSERT_EQ(TEST_UID, tagResult.value().uid);
98     ASSERT_EQ(TEST_TAG, tagResult.value().tag);
99     ASSERT_EQ(0, qtaguid_untagSocket(sock));
100     tagResult = cookieTagMap.readValue(cookie);
101     ASSERT_FALSE(tagResult.ok());
102     ASSERT_EQ(ENOENT, tagResult.error().code());
103 }
104 
TEST_F(BpfBasicTest,TestCloseSocketWithoutUntag)105 TEST_F(BpfBasicTest, TestCloseSocketWithoutUntag) {
106     BpfMap<uint64_t, UidTagValue> cookieTagMap(COOKIE_TAG_MAP_PATH);
107     ASSERT_LE(0, cookieTagMap.getMap());
108     int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
109     ASSERT_LE(0, sock);
110     uint64_t cookie = getSocketCookie(sock);
111     ASSERT_NE(NONEXISTENT_COOKIE, cookie);
112     ASSERT_EQ(0, qtaguid_tagSocket(sock, TEST_TAG, TEST_UID));
113     Result<UidTagValue> tagResult = cookieTagMap.readValue(cookie);
114     ASSERT_RESULT_OK(tagResult);
115     ASSERT_EQ(TEST_UID, tagResult.value().uid);
116     ASSERT_EQ(TEST_TAG, tagResult.value().tag);
117     ASSERT_EQ(0, close(sock));
118     // Check map periodically until sk destroy handler have done its job.
119     for (int i = 0; i < 10; i++) {
120         usleep(5000);  // 5ms
121         tagResult = cookieTagMap.readValue(cookie);
122         if (!tagResult.ok()) {
123             ASSERT_EQ(ENOENT, tagResult.error().code());
124             return;
125         }
126     }
127     FAIL() << "socket tag still exist after 50ms";
128 }
129 
TEST_F(BpfBasicTest,TestChangeCounterSet)130 TEST_F(BpfBasicTest, TestChangeCounterSet) {
131     BpfMap<uint32_t, uint8_t> uidCounterSetMap(UID_COUNTERSET_MAP_PATH);
132     ASSERT_LE(0, uidCounterSetMap.getMap());
133     ASSERT_EQ(0, qtaguid_setCounterSet(TEST_COUNTERSET, TEST_UID));
134     uid_t uid = TEST_UID;
135     Result<uint8_t> counterSetResult = uidCounterSetMap.readValue(uid);
136     ASSERT_RESULT_OK(counterSetResult);
137     ASSERT_EQ(TEST_COUNTERSET, counterSetResult.value());
138     ASSERT_EQ(0, qtaguid_setCounterSet(DEFAULT_COUNTERSET, TEST_UID));
139     counterSetResult = uidCounterSetMap.readValue(uid);
140     ASSERT_FALSE(counterSetResult.ok());
141     ASSERT_EQ(ENOENT, counterSetResult.error().code());
142 }
143 
TEST_F(BpfBasicTest,TestDeleteTagData)144 TEST_F(BpfBasicTest, TestDeleteTagData) {
145     BpfMap<StatsKey, StatsValue> statsMapA(STATS_MAP_A_PATH);
146     ASSERT_LE(0, statsMapA.getMap());
147     BpfMap<StatsKey, StatsValue> statsMapB(STATS_MAP_B_PATH);
148     ASSERT_LE(0, statsMapB.getMap());
149     BpfMap<uint32_t, StatsValue> appUidStatsMap(APP_UID_STATS_MAP_PATH);
150     ASSERT_LE(0, appUidStatsMap.getMap());
151 
152     StatsKey key = {.uid = TEST_UID, .tag = TEST_TAG, .counterSet = TEST_COUNTERSET,
153                     .ifaceIndex = 1};
154     StatsValue statsMapValue = {.rxPackets = 1, .rxBytes = 100};
155     EXPECT_RESULT_OK(statsMapB.writeValue(key, statsMapValue, BPF_ANY));
156     key.tag = 0;
157     EXPECT_RESULT_OK(statsMapA.writeValue(key, statsMapValue, BPF_ANY));
158     EXPECT_RESULT_OK(appUidStatsMap.writeValue(TEST_UID, statsMapValue, BPF_ANY));
159     ASSERT_EQ(0, qtaguid_deleteTagData(0, TEST_UID));
160     Result<StatsValue> statsResult = statsMapA.readValue(key);
161     ASSERT_FALSE(statsResult.ok());
162     ASSERT_EQ(ENOENT, statsResult.error().code());
163     statsResult = appUidStatsMap.readValue(TEST_UID);
164     ASSERT_FALSE(statsResult.ok());
165     ASSERT_EQ(ENOENT, statsResult.error().code());
166     key.tag = TEST_TAG;
167     statsResult = statsMapB.readValue(key);
168     ASSERT_FALSE(statsResult.ok());
169     ASSERT_EQ(ENOENT, statsResult.error().code());
170 }
171 
172 }
173 }
174