Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2022, 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 | #define LOG_TAG "BpfHandler" |
| 18 | |
| 19 | #include "BpfHandler.h" |
| 20 | |
| 21 | #include <linux/bpf.h> |
Nick Wille | 5076a02 | 2023-06-01 18:39:25 +0000 | [diff] [blame] | 22 | #include <inttypes.h> |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 23 | |
| 24 | #include <android-base/unique_fd.h> |
Maciej Żenczykowski | 65075bb | 2023-06-01 23:09:14 +0000 | [diff] [blame] | 25 | #include <android-modules-utils/sdk_level.h> |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 26 | #include <bpf/WaitForProgsLoaded.h> |
| 27 | #include <log/log.h> |
| 28 | #include <netdutils/UidConstants.h> |
| 29 | #include <private/android_filesystem_config.h> |
| 30 | |
| 31 | #include "BpfSyscallWrappers.h" |
| 32 | |
| 33 | namespace android { |
| 34 | namespace net { |
| 35 | |
| 36 | using base::unique_fd; |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 37 | using bpf::getSocketCookie; |
| 38 | using bpf::retrieveProgram; |
| 39 | using netdutils::Status; |
| 40 | using netdutils::statusFromErrno; |
| 41 | |
| 42 | constexpr int PER_UID_STATS_ENTRIES_LIMIT = 500; |
| 43 | // At most 90% of the stats map may be used by tagged traffic entries. This ensures |
| 44 | // that 10% of the map is always available to count untagged traffic, one entry per UID. |
| 45 | // Otherwise, apps would be able to avoid data usage accounting entirely by filling up the |
| 46 | // map with tagged traffic entries. |
| 47 | constexpr int TOTAL_UID_STATS_ENTRIES_LIMIT = STATS_MAP_SIZE * 0.9; |
| 48 | |
| 49 | static_assert(STATS_MAP_SIZE - TOTAL_UID_STATS_ENTRIES_LIMIT > 100, |
| 50 | "The limit for stats map is to high, stats data may be lost due to overflow"); |
| 51 | |
| 52 | static Status attachProgramToCgroup(const char* programPath, const unique_fd& cgroupFd, |
| 53 | bpf_attach_type type) { |
| 54 | unique_fd cgroupProg(retrieveProgram(programPath)); |
Maciej Żenczykowski | 2582445 | 2023-06-14 10:08:31 +0000 | [diff] [blame] | 55 | if (!cgroupProg.ok()) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 56 | return statusFromErrno(errno, fmt::format("Failed to get program from {}", programPath)); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 57 | } |
| 58 | if (android::bpf::attachProgram(type, cgroupProg, cgroupFd)) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 59 | return statusFromErrno(errno, fmt::format("Program {} attach failed", programPath)); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 60 | } |
| 61 | return netdutils::status::ok; |
| 62 | } |
| 63 | |
Maciej Żenczykowski | c576c0d | 2022-08-07 22:18:15 +0000 | [diff] [blame] | 64 | static Status checkProgramAccessible(const char* programPath) { |
| 65 | unique_fd prog(retrieveProgram(programPath)); |
Maciej Żenczykowski | 2582445 | 2023-06-14 10:08:31 +0000 | [diff] [blame] | 66 | if (!prog.ok()) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 67 | return statusFromErrno(errno, fmt::format("Failed to get program from {}", programPath)); |
Maciej Żenczykowski | c576c0d | 2022-08-07 22:18:15 +0000 | [diff] [blame] | 68 | } |
| 69 | return netdutils::status::ok; |
| 70 | } |
| 71 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 72 | static Status initPrograms(const char* cg2_path) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 73 | if (!cg2_path) return Status("cg2_path is NULL"); |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 74 | |
Maciej Żenczykowski | c2dd01c | 2023-09-01 21:02:36 +0000 | [diff] [blame] | 75 | // This code was mainlined in T, so this should be trivially satisfied. |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 76 | if (!modules::sdklevel::IsAtLeastT()) return Status("S- platform is unsupported"); |
Maciej Żenczykowski | c2dd01c | 2023-09-01 21:02:36 +0000 | [diff] [blame] | 77 | |
| 78 | // S requires eBPF support which was only added in 4.9, so this should be satisfied. |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 79 | if (!bpf::isAtLeastKernelVersion(4, 9, 0)) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 80 | return Status("kernel version < 4.9.0 is unsupported"); |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 81 | } |
Maciej Żenczykowski | c2dd01c | 2023-09-01 21:02:36 +0000 | [diff] [blame] | 82 | |
| 83 | // U bumps the kernel requirement up to 4.14 |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 84 | if (modules::sdklevel::IsAtLeastU() && !bpf::isAtLeastKernelVersion(4, 14, 0)) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 85 | return Status("U+ platform with kernel version < 4.14.0 is unsupported"); |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 86 | } |
Maciej Żenczykowski | c2dd01c | 2023-09-01 21:02:36 +0000 | [diff] [blame] | 87 | |
Maciej Żenczykowski | e061385 | 2023-09-29 04:02:10 +0000 | [diff] [blame] | 88 | if (modules::sdklevel::IsAtLeastV()) { |
| 89 | // V bumps the kernel requirement up to 4.19 |
| 90 | // see also: //system/netd/tests/kernel_test.cpp TestKernel419 |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 91 | if (!bpf::isAtLeastKernelVersion(4, 19, 0)) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 92 | return Status("V+ platform with kernel version < 4.19.0 is unsupported"); |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 93 | } |
Maciej Żenczykowski | e061385 | 2023-09-29 04:02:10 +0000 | [diff] [blame] | 94 | |
| 95 | // Technically already required by U, but only enforce on V+ |
| 96 | // see also: //system/netd/tests/kernel_test.cpp TestKernel64Bit |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 97 | if (bpf::isKernel32Bit() && bpf::isAtLeastKernelVersion(5, 16, 0)) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 98 | return Status("V+ platform with 32 bit kernel, version >= 5.16.0 is unsupported"); |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 99 | } |
Maciej Żenczykowski | e061385 | 2023-09-29 04:02:10 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Linux 6.1 is highest version supported by U, starting with V new kernels, |
| 103 | // ie. 6.2+ we are dropping various kernel/system userspace 32-on-64 hacks |
| 104 | // (for example "ANDROID: xfrm: remove in_compat_syscall() checks"). |
| 105 | // Note: this check/enforcement only applies to *system* userspace code, |
| 106 | // it does not affect unprivileged apps, the 32-on-64 compatibility |
| 107 | // problems are AFAIK limited to various CAP_NET_ADMIN protected interfaces. |
| 108 | // see also: //system/bpf/bpfloader/BpfLoader.cpp main() |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 109 | if (bpf::isUserspace32bit() && bpf::isAtLeastKernelVersion(6, 2, 0)) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 110 | return Status("32 bit userspace with Kernel version >= 6.2.0 is unsupported"); |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 111 | } |
Maciej Żenczykowski | c2dd01c | 2023-09-01 21:02:36 +0000 | [diff] [blame] | 112 | |
| 113 | // U mandates this mount point (though it should also be the case on T) |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 114 | if (modules::sdklevel::IsAtLeastU() && !!strcmp(cg2_path, "/sys/fs/cgroup")) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 115 | return Status("U+ platform with cg2_path != /sys/fs/cgroup is unsupported"); |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 116 | } |
Maciej Żenczykowski | 65075bb | 2023-06-01 23:09:14 +0000 | [diff] [blame] | 117 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 118 | unique_fd cg_fd(open(cg2_path, O_DIRECTORY | O_RDONLY | O_CLOEXEC)); |
Maciej Żenczykowski | 2582445 | 2023-06-14 10:08:31 +0000 | [diff] [blame] | 119 | if (!cg_fd.ok()) { |
| 120 | const int err = errno; |
| 121 | ALOGE("Failed to open the cgroup directory: %s", strerror(err)); |
| 122 | return statusFromErrno(err, "Open the cgroup directory failed"); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 123 | } |
Maciej Żenczykowski | c576c0d | 2022-08-07 22:18:15 +0000 | [diff] [blame] | 124 | RETURN_IF_NOT_OK(checkProgramAccessible(XT_BPF_ALLOWLIST_PROG_PATH)); |
| 125 | RETURN_IF_NOT_OK(checkProgramAccessible(XT_BPF_DENYLIST_PROG_PATH)); |
| 126 | RETURN_IF_NOT_OK(checkProgramAccessible(XT_BPF_EGRESS_PROG_PATH)); |
| 127 | RETURN_IF_NOT_OK(checkProgramAccessible(XT_BPF_INGRESS_PROG_PATH)); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 128 | RETURN_IF_NOT_OK(attachProgramToCgroup(BPF_EGRESS_PROG_PATH, cg_fd, BPF_CGROUP_INET_EGRESS)); |
| 129 | RETURN_IF_NOT_OK(attachProgramToCgroup(BPF_INGRESS_PROG_PATH, cg_fd, BPF_CGROUP_INET_INGRESS)); |
Lorenzo Colitti | 3505b58 | 2022-10-27 19:36:27 +0900 | [diff] [blame] | 130 | |
| 131 | // For the devices that support cgroup socket filter, the socket filter |
| 132 | // should be loaded successfully by bpfloader. So we attach the filter to |
| 133 | // cgroup if the program is pinned properly. |
| 134 | // TODO: delete the if statement once all devices should support cgroup |
| 135 | // socket filter (ie. the minimum kernel version required is 4.14). |
Maciej Żenczykowski | 0309362 | 2023-02-22 22:09:25 +0000 | [diff] [blame] | 136 | if (bpf::isAtLeastKernelVersion(4, 14, 0)) { |
Lorenzo Colitti | 3505b58 | 2022-10-27 19:36:27 +0900 | [diff] [blame] | 137 | RETURN_IF_NOT_OK( |
| 138 | attachProgramToCgroup(CGROUP_SOCKET_PROG_PATH, cg_fd, BPF_CGROUP_INET_SOCK_CREATE)); |
| 139 | } |
Maciej Żenczykowski | 5b2611d | 2023-10-04 00:44:56 +0000 | [diff] [blame] | 140 | |
Maciej Żenczykowski | 5b2611d | 2023-10-04 00:44:56 +0000 | [diff] [blame] | 141 | if (bpf::isAtLeastKernelVersion(4, 19, 0)) { |
Maciej Żenczykowski | 3cb494f | 2023-10-04 21:35:41 +0000 | [diff] [blame] | 142 | RETURN_IF_NOT_OK(attachProgramToCgroup( |
| 143 | "/sys/fs/bpf/netd_readonly/prog_block_bind4_block_port", |
| 144 | cg_fd, BPF_CGROUP_INET4_BIND)); |
| 145 | RETURN_IF_NOT_OK(attachProgramToCgroup( |
| 146 | "/sys/fs/bpf/netd_readonly/prog_block_bind6_block_port", |
| 147 | cg_fd, BPF_CGROUP_INET6_BIND)); |
| 148 | |
| 149 | // This should trivially pass, since we just attached up above, |
| 150 | // but BPF_PROG_QUERY is only implemented on 4.19+ kernels. |
Maciej Żenczykowski | 5b2611d | 2023-10-04 00:44:56 +0000 | [diff] [blame] | 151 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET_EGRESS) <= 0) abort(); |
| 152 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET_INGRESS) <= 0) abort(); |
| 153 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET_SOCK_CREATE) <= 0) abort(); |
Maciej Żenczykowski | 3cb494f | 2023-10-04 21:35:41 +0000 | [diff] [blame] | 154 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET4_BIND) <= 0) abort(); |
| 155 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET6_BIND) <= 0) abort(); |
Maciej Żenczykowski | 5b2611d | 2023-10-04 00:44:56 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 158 | return netdutils::status::ok; |
| 159 | } |
| 160 | |
| 161 | BpfHandler::BpfHandler() |
| 162 | : mPerUidStatsEntriesLimit(PER_UID_STATS_ENTRIES_LIMIT), |
| 163 | mTotalUidStatsEntriesLimit(TOTAL_UID_STATS_ENTRIES_LIMIT) {} |
| 164 | |
| 165 | BpfHandler::BpfHandler(uint32_t perUidLimit, uint32_t totalLimit) |
| 166 | : mPerUidStatsEntriesLimit(perUidLimit), mTotalUidStatsEntriesLimit(totalLimit) {} |
| 167 | |
Maciej Żenczykowski | 732a141 | 2024-03-14 00:17:18 -0700 | [diff] [blame] | 168 | // copied with minor changes from waitForProgsLoaded() |
| 169 | // p/m/C's staticlibs/native/bpf_headers/include/bpf/WaitForProgsLoaded.h |
| 170 | static inline void waitForNetProgsLoaded() { |
| 171 | // infinite loop until success with 5/10/20/40/60/60/60... delay |
| 172 | for (int delay = 5;; delay *= 2) { |
| 173 | if (delay > 60) delay = 60; |
| 174 | if (base::WaitForProperty("init.svc.bpfloader", "stopped", std::chrono::seconds(delay)) |
| 175 | && !access("/sys/fs/bpf/netd_shared", F_OK)) |
| 176 | return; |
| 177 | ALOGW("Waited %ds for init.svc.bpfloader=stopped, still waiting...", delay); |
| 178 | } |
| 179 | } |
| 180 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 181 | Status BpfHandler::init(const char* cg2_path) { |
Maciej Żenczykowski | 23d2c1e | 2024-03-28 22:54:01 -0700 | [diff] [blame^] | 182 | if (base::GetProperty("bpf.progs_loaded", "") != "1") { |
| 183 | // Make sure BPF programs are loaded before doing anything |
| 184 | ALOGI("Waiting for BPF programs"); |
Maciej Żenczykowski | 732a141 | 2024-03-14 00:17:18 -0700 | [diff] [blame] | 185 | |
Maciej Żenczykowski | 23d2c1e | 2024-03-28 22:54:01 -0700 | [diff] [blame^] | 186 | if (true || !modules::sdklevel::IsAtLeastV()) { |
| 187 | waitForNetProgsLoaded(); |
| 188 | ALOGI("Networking BPF programs are loaded"); |
Maciej Żenczykowski | 732a141 | 2024-03-14 00:17:18 -0700 | [diff] [blame] | 189 | |
Maciej Żenczykowski | 23d2c1e | 2024-03-28 22:54:01 -0700 | [diff] [blame^] | 190 | if (!base::SetProperty("ctl.start", "mdnsd_loadbpf")) { |
| 191 | ALOGE("Failed to set property ctl.start=mdnsd_loadbpf, see dmesg for reason."); |
| 192 | abort(); |
| 193 | } |
| 194 | |
| 195 | ALOGI("Waiting for remaining BPF programs"); |
Maciej Żenczykowski | 732a141 | 2024-03-14 00:17:18 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Maciej Żenczykowski | 23d2c1e | 2024-03-28 22:54:01 -0700 | [diff] [blame^] | 198 | android::bpf::waitForProgsLoaded(); |
Maciej Żenczykowski | 732a141 | 2024-03-14 00:17:18 -0700 | [diff] [blame] | 199 | } |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 200 | ALOGI("BPF programs are loaded"); |
| 201 | |
| 202 | RETURN_IF_NOT_OK(initPrograms(cg2_path)); |
| 203 | RETURN_IF_NOT_OK(initMaps()); |
| 204 | |
| 205 | return netdutils::status::ok; |
| 206 | } |
| 207 | |
| 208 | Status BpfHandler::initMaps() { |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 209 | RETURN_IF_NOT_OK(mStatsMapA.init(STATS_MAP_A_PATH)); |
| 210 | RETURN_IF_NOT_OK(mStatsMapB.init(STATS_MAP_B_PATH)); |
| 211 | RETURN_IF_NOT_OK(mConfigurationMap.init(CONFIGURATION_MAP_PATH)); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 212 | RETURN_IF_NOT_OK(mUidPermissionMap.init(UID_PERMISSION_MAP_PATH)); |
Maciej Żenczykowski | b5868a0 | 2022-08-31 04:21:01 +0000 | [diff] [blame] | 213 | // initialized last so mCookieTagMap.isValid() implies everything else is valid too |
| 214 | RETURN_IF_NOT_OK(mCookieTagMap.init(COOKIE_TAG_MAP_PATH)); |
Ken Chen | 322ffcb | 2022-05-23 22:27:40 +0800 | [diff] [blame] | 215 | ALOGI("%s successfully", __func__); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 216 | |
| 217 | return netdutils::status::ok; |
| 218 | } |
| 219 | |
| 220 | bool BpfHandler::hasUpdateDeviceStatsPermission(uid_t uid) { |
| 221 | // This implementation is the same logic as method ActivityManager#checkComponentPermission. |
| 222 | // It implies that the real uid can never be the same as PER_USER_RANGE. |
| 223 | uint32_t appId = uid % PER_USER_RANGE; |
| 224 | auto permission = mUidPermissionMap.readValue(appId); |
| 225 | if (permission.ok() && (permission.value() & BPF_PERMISSION_UPDATE_DEVICE_STATS)) { |
| 226 | return true; |
| 227 | } |
| 228 | return ((appId == AID_ROOT) || (appId == AID_SYSTEM) || (appId == AID_DNS)); |
| 229 | } |
| 230 | |
| 231 | int BpfHandler::tagSocket(int sockFd, uint32_t tag, uid_t chargeUid, uid_t realUid) { |
Maciej Żenczykowski | 4938d40 | 2022-08-14 14:36:20 +0000 | [diff] [blame] | 232 | if (!mCookieTagMap.isValid()) return -EPERM; |
| 233 | |
| 234 | if (chargeUid != realUid && !hasUpdateDeviceStatsPermission(realUid)) return -EPERM; |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 235 | |
Hungming Chen | 436547e | 2022-02-18 17:52:11 +0800 | [diff] [blame] | 236 | // Note that tagging the socket to AID_CLAT is only implemented in JNI ClatCoordinator. |
| 237 | // The process is not allowed to tag socket to AID_CLAT via tagSocket() which would cause |
| 238 | // process data usage accounting to be bypassed. Tagging AID_CLAT is used for avoiding counting |
| 239 | // CLAT traffic data usage twice. See packages/modules/Connectivity/service/jni/ |
| 240 | // com_android_server_connectivity_ClatCoordinator.cpp |
Maciej Żenczykowski | 4938d40 | 2022-08-14 14:36:20 +0000 | [diff] [blame] | 241 | if (chargeUid == AID_CLAT) return -EPERM; |
Hungming Chen | 436547e | 2022-02-18 17:52:11 +0800 | [diff] [blame] | 242 | |
Hungming Chen | 478c0eb | 2022-03-04 21:16:59 +0800 | [diff] [blame] | 243 | // The socket destroy listener only monitors on the group {INET_TCP, INET_UDP, INET6_TCP, |
| 244 | // INET6_UDP}. Tagging listener unsupported socket causes that the tag can't be removed from |
| 245 | // tag map automatically. Eventually, the tag map may run out of space because of dead tag |
Hungming Chen | bcc0f5b | 2022-03-07 14:13:49 +0800 | [diff] [blame] | 246 | // entries. Note that although tagSocket() of net client has already denied the family which |
| 247 | // is neither AF_INET nor AF_INET6, the family validation is still added here just in case. |
| 248 | // See tagSocket in system/netd/client/NetdClient.cpp and |
| 249 | // TrafficController::makeSkDestroyListener in |
Hungming Chen | 478c0eb | 2022-03-04 21:16:59 +0800 | [diff] [blame] | 250 | // packages/modules/Connectivity/service/native/TrafficController.cpp |
| 251 | // TODO: remove this once the socket destroy listener can detect more types of socket destroy. |
Hungming Chen | bcc0f5b | 2022-03-07 14:13:49 +0800 | [diff] [blame] | 252 | int socketFamily; |
| 253 | socklen_t familyLen = sizeof(socketFamily); |
| 254 | if (getsockopt(sockFd, SOL_SOCKET, SO_DOMAIN, &socketFamily, &familyLen)) { |
| 255 | ALOGE("Failed to getsockopt SO_DOMAIN: %s, fd: %d", strerror(errno), sockFd); |
Hungming Chen | 478c0eb | 2022-03-04 21:16:59 +0800 | [diff] [blame] | 256 | return -errno; |
Hungming Chen | bcc0f5b | 2022-03-07 14:13:49 +0800 | [diff] [blame] | 257 | } |
| 258 | if (socketFamily != AF_INET && socketFamily != AF_INET6) { |
| 259 | ALOGE("Unsupported family: %d", socketFamily); |
| 260 | return -EAFNOSUPPORT; |
| 261 | } |
| 262 | |
| 263 | int socketProto; |
| 264 | socklen_t protoLen = sizeof(socketProto); |
| 265 | if (getsockopt(sockFd, SOL_SOCKET, SO_PROTOCOL, &socketProto, &protoLen)) { |
| 266 | ALOGE("Failed to getsockopt SO_PROTOCOL: %s, fd: %d", strerror(errno), sockFd); |
| 267 | return -errno; |
| 268 | } |
| 269 | if (socketProto != IPPROTO_UDP && socketProto != IPPROTO_TCP) { |
| 270 | ALOGE("Unsupported protocol: %d", socketProto); |
| 271 | return -EPROTONOSUPPORT; |
Hungming Chen | 478c0eb | 2022-03-04 21:16:59 +0800 | [diff] [blame] | 272 | } |
| 273 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 274 | uint64_t sock_cookie = getSocketCookie(sockFd); |
Maciej Żenczykowski | d3fe154 | 2023-02-23 03:56:45 +0000 | [diff] [blame] | 275 | if (!sock_cookie) return -errno; |
Maciej Żenczykowski | 4938d40 | 2022-08-14 14:36:20 +0000 | [diff] [blame] | 276 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 277 | UidTagValue newKey = {.uid = (uint32_t)chargeUid, .tag = tag}; |
| 278 | |
| 279 | uint32_t totalEntryCount = 0; |
| 280 | uint32_t perUidEntryCount = 0; |
| 281 | // Now we go through the stats map and count how many entries are associated |
| 282 | // with chargeUid. If the uid entry hit the limit for each chargeUid, we block |
Maciej Żenczykowski | b5868a0 | 2022-08-31 04:21:01 +0000 | [diff] [blame] | 283 | // the request to prevent the map from overflow. Note though that it isn't really |
| 284 | // safe here to iterate over the map since it might be modified by the system server, |
| 285 | // which might toggle the live stats map and clean it. |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 286 | const auto countUidStatsEntries = [chargeUid, &totalEntryCount, &perUidEntryCount]( |
| 287 | const StatsKey& key, |
Maciej Żenczykowski | 7e2f53e | 2023-09-28 01:08:28 +0000 | [diff] [blame] | 288 | const BpfMapRO<StatsKey, StatsValue>&) { |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 289 | if (key.uid == chargeUid) { |
| 290 | perUidEntryCount++; |
| 291 | } |
| 292 | totalEntryCount++; |
| 293 | return base::Result<void>(); |
| 294 | }; |
| 295 | auto configuration = mConfigurationMap.readValue(CURRENT_STATS_MAP_CONFIGURATION_KEY); |
| 296 | if (!configuration.ok()) { |
Maciej Żenczykowski | 85d4a5e | 2023-08-20 16:41:50 +0000 | [diff] [blame] | 297 | ALOGE("Failed to get current configuration: %s", |
| 298 | strerror(configuration.error().code())); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 299 | return -configuration.error().code(); |
| 300 | } |
| 301 | if (configuration.value() != SELECT_MAP_A && configuration.value() != SELECT_MAP_B) { |
| 302 | ALOGE("unknown configuration value: %d", configuration.value()); |
| 303 | return -EINVAL; |
| 304 | } |
| 305 | |
Maciej Żenczykowski | 7e2f53e | 2023-09-28 01:08:28 +0000 | [diff] [blame] | 306 | BpfMapRO<StatsKey, StatsValue>& currentMap = |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 307 | (configuration.value() == SELECT_MAP_A) ? mStatsMapA : mStatsMapB; |
| 308 | base::Result<void> res = currentMap.iterate(countUidStatsEntries); |
| 309 | if (!res.ok()) { |
Maciej Żenczykowski | 85d4a5e | 2023-08-20 16:41:50 +0000 | [diff] [blame] | 310 | ALOGE("Failed to count the stats entry in map: %s", |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 311 | strerror(res.error().code())); |
| 312 | return -res.error().code(); |
| 313 | } |
| 314 | |
| 315 | if (totalEntryCount > mTotalUidStatsEntriesLimit || |
| 316 | perUidEntryCount > mPerUidStatsEntriesLimit) { |
| 317 | ALOGE("Too many stats entries in the map, total count: %u, chargeUid(%u) count: %u," |
| 318 | " blocking tag request to prevent map overflow", |
| 319 | totalEntryCount, chargeUid, perUidEntryCount); |
| 320 | return -EMFILE; |
| 321 | } |
| 322 | // Update the tag information of a socket to the cookieUidMap. Use BPF_ANY |
| 323 | // flag so it will insert a new entry to the map if that value doesn't exist |
Maciej Żenczykowski | b5868a0 | 2022-08-31 04:21:01 +0000 | [diff] [blame] | 324 | // yet and update the tag if there is already a tag stored. Since the eBPF |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 325 | // program in kernel only read this map, and is protected by rcu read lock. It |
Maciej Żenczykowski | b5868a0 | 2022-08-31 04:21:01 +0000 | [diff] [blame] | 326 | // should be fine to concurrently update the map while eBPF program is running. |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 327 | res = mCookieTagMap.writeValue(sock_cookie, newKey, BPF_ANY); |
| 328 | if (!res.ok()) { |
Maciej Żenczykowski | 85d4a5e | 2023-08-20 16:41:50 +0000 | [diff] [blame] | 329 | ALOGE("Failed to tag the socket: %s", strerror(res.error().code())); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 330 | return -res.error().code(); |
| 331 | } |
Nick Wille | 5076a02 | 2023-06-01 18:39:25 +0000 | [diff] [blame] | 332 | ALOGD("Socket with cookie %" PRIu64 " tagged successfully with tag %" PRIu32 " uid %u " |
| 333 | "and real uid %u", sock_cookie, tag, chargeUid, realUid); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | int BpfHandler::untagSocket(int sockFd) { |
Maciej Żenczykowski | 4938d40 | 2022-08-14 14:36:20 +0000 | [diff] [blame] | 338 | uint64_t sock_cookie = getSocketCookie(sockFd); |
Maciej Żenczykowski | d3fe154 | 2023-02-23 03:56:45 +0000 | [diff] [blame] | 339 | if (!sock_cookie) return -errno; |
Maciej Żenczykowski | 4938d40 | 2022-08-14 14:36:20 +0000 | [diff] [blame] | 340 | |
| 341 | if (!mCookieTagMap.isValid()) return -EPERM; |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 342 | base::Result<void> res = mCookieTagMap.deleteValue(sock_cookie); |
| 343 | if (!res.ok()) { |
Maciej Żenczykowski | e0f5846 | 2022-05-17 13:59:22 -0700 | [diff] [blame] | 344 | ALOGE("Failed to untag socket: %s", strerror(res.error().code())); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 345 | return -res.error().code(); |
| 346 | } |
Nick Wille | 5076a02 | 2023-06-01 18:39:25 +0000 | [diff] [blame] | 347 | ALOGD("Socket with cookie %" PRIu64 " untagged successfully.", sock_cookie); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | } // namespace net |
| 352 | } // namespace android |