blob: f28c5f81c7b7390a4c0643a9b60024c6b1001a61 [file] [log] [blame]
Chenbo Feng837ddfc2018-05-08 13:45:08 -07001/*
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>
Suren Baghdasaryane072a3c2019-01-16 14:36:07 -080032#include <processgroup/processgroup.h>
Chenbo Feng837ddfc2018-05-08 13:45:08 -070033
34#include <android-base/stringprintf.h>
35#include <android-base/strings.h>
36
Chenbo Feng4f6c2372018-04-26 10:37:55 -070037#include "bpf/BpfMap.h"
Chenbo Feng837ddfc2018-05-08 13:45:08 -070038#include "bpf/BpfUtils.h"
Chenbo Fengd6104d12018-10-16 20:29:29 -070039#include "netdbpf/bpf_shared.h"
Chenbo Feng837ddfc2018-05-08 13:45:08 -070040
Steven Morelanda3074542020-01-13 14:13:44 -080041using android::base::Result;
Chenbo Feng837ddfc2018-05-08 13:45:08 -070042
43namespace android {
44namespace 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.
48constexpr uid_t TEST_UID = UID_MAX - 1;
49constexpr uint32_t TEST_TAG = 42;
50constexpr int TEST_COUNTERSET = 1;
51constexpr int DEFAULT_COUNTERSET = 0;
52
53class BpfBasicTest : public testing::Test {
54 protected:
55 BpfBasicTest() {}
56};
57
58TEST_F(BpfBasicTest, TestCgroupMounted) {
59 SKIP_IF_BPF_NOT_SUPPORTED;
60
Suren Baghdasaryane072a3c2019-01-16 14:36:07 -080061 std::string cg2_path;
Maciej Żenczykowski6bacd4f2020-01-21 23:05:26 -080062#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
Suren Baghdasaryane072a3c2019-01-16 14:36:07 -080065 ASSERT_EQ(true, CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cg2_path));
Maciej Żenczykowski6bacd4f2020-01-21 23:05:26 -080066#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
Suren Baghdasaryane072a3c2019-01-16 14:36:07 -080071 ASSERT_EQ(0, access(cg2_path.c_str(), R_OK));
72 ASSERT_EQ(0, access((cg2_path + "/cgroup.controllers").c_str(), R_OK));
Chenbo Feng837ddfc2018-05-08 13:45:08 -070073}
74
75TEST_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));
Chenbo Fengf434e862018-06-27 14:08:39 -070084 ASSERT_EQ(0, access(STATS_MAP_A_PATH, R_OK));
85 ASSERT_EQ(0, access(STATS_MAP_B_PATH, R_OK));
Chenbo Feng837ddfc2018-05-08 13:45:08 -070086 ASSERT_EQ(0, access(IFACE_INDEX_NAME_MAP_PATH, R_OK));
87 ASSERT_EQ(0, access(IFACE_STATS_MAP_PATH, R_OK));
Chenbo Feng703798e2018-06-15 17:07:59 -070088 ASSERT_EQ(0, access(CONFIGURATION_MAP_PATH, R_OK));
89 ASSERT_EQ(0, access(UID_OWNER_MAP_PATH, R_OK));
Chenbo Feng837ddfc2018-05-08 13:45:08 -070090}
91
Chenbo Fengbf660aa2019-02-26 16:12:27 -080092TEST_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
Chenbo Feng837ddfc2018-05-08 13:45:08 -070099TEST_F(BpfBasicTest, TestTagSocket) {
100 SKIP_IF_BPF_NOT_SUPPORTED;
101
Maciej Żenczykowskif879a7f2020-01-20 03:18:06 -0800102 BpfMap<uint64_t, UidTagValue> cookieTagMap(COOKIE_TAG_MAP_PATH);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700103 ASSERT_LE(0, cookieTagMap.getMap());
Bernie Innocentif6918262018-06-11 17:37:35 +0900104 int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
Chenbo Feng837ddfc2018-05-08 13:45:08 -0700105 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));
Steven Morelanda3074542020-01-13 14:13:44 -0800109 Result<UidTagValue> tagResult = cookieTagMap.readValue(cookie);
Bernie Innocentidfd5a432020-02-12 09:21:56 +0900110 ASSERT_RESULT_OK(tagResult);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700111 ASSERT_EQ(TEST_UID, tagResult.value().uid);
112 ASSERT_EQ(TEST_TAG, tagResult.value().tag);
Chenbo Feng837ddfc2018-05-08 13:45:08 -0700113 ASSERT_EQ(0, qtaguid_untagSocket(sock));
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700114 tagResult = cookieTagMap.readValue(cookie);
Bernie Innocentidfd5a432020-02-12 09:21:56 +0900115 ASSERT_FALSE(tagResult.ok());
Steven Morelanda3074542020-01-13 14:13:44 -0800116 ASSERT_EQ(ENOENT, tagResult.error().code());
Chenbo Feng837ddfc2018-05-08 13:45:08 -0700117}
118
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700119TEST_F(BpfBasicTest, TestCloseSocketWithoutUntag) {
120 SKIP_IF_BPF_NOT_SUPPORTED;
121
Maciej Żenczykowskif879a7f2020-01-20 03:18:06 -0800122 BpfMap<uint64_t, UidTagValue> cookieTagMap(COOKIE_TAG_MAP_PATH);
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700123 ASSERT_LE(0, cookieTagMap.getMap());
Bernie Innocentif6918262018-06-11 17:37:35 +0900124 int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700125 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));
Steven Morelanda3074542020-01-13 14:13:44 -0800129 Result<UidTagValue> tagResult = cookieTagMap.readValue(cookie);
Bernie Innocentidfd5a432020-02-12 09:21:56 +0900130 ASSERT_RESULT_OK(tagResult);
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700131 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++) {
Maciej Żenczykowskid0e14f82019-04-24 12:41:17 -0700136 usleep(5000); // 5ms
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700137 tagResult = cookieTagMap.readValue(cookie);
Bernie Innocentidfd5a432020-02-12 09:21:56 +0900138 if (!tagResult.ok()) {
Steven Morelanda3074542020-01-13 14:13:44 -0800139 ASSERT_EQ(ENOENT, tagResult.error().code());
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700140 return;
141 }
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700142 }
Maciej Żenczykowski6ee57d72019-04-23 19:14:09 -0700143 FAIL() << "socket tag still exist after 50ms";
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700144}
145
Chenbo Feng837ddfc2018-05-08 13:45:08 -0700146TEST_F(BpfBasicTest, TestChangeCounterSet) {
147 SKIP_IF_BPF_NOT_SUPPORTED;
148
Maciej Żenczykowskif879a7f2020-01-20 03:18:06 -0800149 BpfMap<uint32_t, uint8_t> uidCounterSetMap(UID_COUNTERSET_MAP_PATH);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700150 ASSERT_LE(0, uidCounterSetMap.getMap());
Chenbo Feng837ddfc2018-05-08 13:45:08 -0700151 ASSERT_EQ(0, qtaguid_setCounterSet(TEST_COUNTERSET, TEST_UID));
152 uid_t uid = TEST_UID;
Steven Morelanda3074542020-01-13 14:13:44 -0800153 Result<uint8_t> counterSetResult = uidCounterSetMap.readValue(uid);
Bernie Innocentidfd5a432020-02-12 09:21:56 +0900154 ASSERT_RESULT_OK(counterSetResult);
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700155 ASSERT_EQ(TEST_COUNTERSET, counterSetResult.value());
Chenbo Feng837ddfc2018-05-08 13:45:08 -0700156 ASSERT_EQ(0, qtaguid_setCounterSet(DEFAULT_COUNTERSET, TEST_UID));
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700157 counterSetResult = uidCounterSetMap.readValue(uid);
Bernie Innocentidfd5a432020-02-12 09:21:56 +0900158 ASSERT_FALSE(counterSetResult.ok());
Steven Morelanda3074542020-01-13 14:13:44 -0800159 ASSERT_EQ(ENOENT, counterSetResult.error().code());
Chenbo Feng837ddfc2018-05-08 13:45:08 -0700160}
161
162TEST_F(BpfBasicTest, TestDeleteTagData) {
163 SKIP_IF_BPF_NOT_SUPPORTED;
164
Maciej Żenczykowskif879a7f2020-01-20 03:18:06 -0800165 BpfMap<StatsKey, StatsValue> statsMapA(STATS_MAP_A_PATH);
Chenbo Fengf434e862018-06-27 14:08:39 -0700166 ASSERT_LE(0, statsMapA.getMap());
Maciej Żenczykowskif879a7f2020-01-20 03:18:06 -0800167 BpfMap<StatsKey, StatsValue> statsMapB(STATS_MAP_B_PATH);
Chenbo Fengf434e862018-06-27 14:08:39 -0700168 ASSERT_LE(0, statsMapB.getMap());
Maciej Żenczykowskif879a7f2020-01-20 03:18:06 -0800169 BpfMap<uint32_t, StatsValue> appUidStatsMap(APP_UID_STATS_MAP_PATH);
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700170 ASSERT_LE(0, appUidStatsMap.getMap());
Chenbo Feng837ddfc2018-05-08 13:45:08 -0700171
172 StatsKey key = {.uid = TEST_UID, .tag = TEST_TAG, .counterSet = TEST_COUNTERSET,
173 .ifaceIndex = 1};
174 StatsValue statsMapValue = {.rxPackets = 1, .rxBytes = 100};
Bernie Innocentidfd5a432020-02-12 09:21:56 +0900175 EXPECT_RESULT_OK(statsMapB.writeValue(key, statsMapValue, BPF_ANY));
Chenbo Feng837ddfc2018-05-08 13:45:08 -0700176 key.tag = 0;
Bernie Innocentidfd5a432020-02-12 09:21:56 +0900177 EXPECT_RESULT_OK(statsMapA.writeValue(key, statsMapValue, BPF_ANY));
178 EXPECT_RESULT_OK(appUidStatsMap.writeValue(TEST_UID, statsMapValue, BPF_ANY));
Chenbo Feng837ddfc2018-05-08 13:45:08 -0700179 ASSERT_EQ(0, qtaguid_deleteTagData(0, TEST_UID));
Steven Morelanda3074542020-01-13 14:13:44 -0800180 Result<StatsValue> statsResult = statsMapA.readValue(key);
Bernie Innocentidfd5a432020-02-12 09:21:56 +0900181 ASSERT_FALSE(statsResult.ok());
Steven Morelanda3074542020-01-13 14:13:44 -0800182 ASSERT_EQ(ENOENT, statsResult.error().code());
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700183 statsResult = appUidStatsMap.readValue(TEST_UID);
Bernie Innocentidfd5a432020-02-12 09:21:56 +0900184 ASSERT_FALSE(statsResult.ok());
Steven Morelanda3074542020-01-13 14:13:44 -0800185 ASSERT_EQ(ENOENT, statsResult.error().code());
Chenbo Feng837ddfc2018-05-08 13:45:08 -0700186 key.tag = TEST_TAG;
Chenbo Fengf434e862018-06-27 14:08:39 -0700187 statsResult = statsMapB.readValue(key);
Bernie Innocentidfd5a432020-02-12 09:21:56 +0900188 ASSERT_FALSE(statsResult.ok());
Steven Morelanda3074542020-01-13 14:13:44 -0800189 ASSERT_EQ(ENOENT, statsResult.error().code());
Chenbo Feng837ddfc2018-05-08 13:45:08 -0700190}
191
192}
193}