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 | |
Maciej Żenczykowski | a81daca | 2024-05-20 14:33:17 +0000 | [diff] [blame] | 17 | #define LOG_TAG "NetdUpdatable" |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 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; |
Maciej Żenczykowski | 15f9731 | 2024-06-13 14:11:28 -0700 | [diff] [blame] | 37 | using base::WaitForProperty; |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 38 | using bpf::getSocketCookie; |
| 39 | using bpf::retrieveProgram; |
| 40 | using netdutils::Status; |
| 41 | using netdutils::statusFromErrno; |
| 42 | |
| 43 | constexpr int PER_UID_STATS_ENTRIES_LIMIT = 500; |
| 44 | // At most 90% of the stats map may be used by tagged traffic entries. This ensures |
| 45 | // that 10% of the map is always available to count untagged traffic, one entry per UID. |
| 46 | // Otherwise, apps would be able to avoid data usage accounting entirely by filling up the |
| 47 | // map with tagged traffic entries. |
| 48 | constexpr int TOTAL_UID_STATS_ENTRIES_LIMIT = STATS_MAP_SIZE * 0.9; |
| 49 | |
| 50 | static_assert(STATS_MAP_SIZE - TOTAL_UID_STATS_ENTRIES_LIMIT > 100, |
| 51 | "The limit for stats map is to high, stats data may be lost due to overflow"); |
| 52 | |
| 53 | static Status attachProgramToCgroup(const char* programPath, const unique_fd& cgroupFd, |
| 54 | bpf_attach_type type) { |
| 55 | unique_fd cgroupProg(retrieveProgram(programPath)); |
Maciej Żenczykowski | 2582445 | 2023-06-14 10:08:31 +0000 | [diff] [blame] | 56 | if (!cgroupProg.ok()) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 57 | return statusFromErrno(errno, fmt::format("Failed to get program from {}", programPath)); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 58 | } |
| 59 | if (android::bpf::attachProgram(type, cgroupProg, cgroupFd)) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 60 | return statusFromErrno(errno, fmt::format("Program {} attach failed", programPath)); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 61 | } |
| 62 | return netdutils::status::ok; |
| 63 | } |
| 64 | |
Maciej Żenczykowski | c576c0d | 2022-08-07 22:18:15 +0000 | [diff] [blame] | 65 | static Status checkProgramAccessible(const char* programPath) { |
| 66 | unique_fd prog(retrieveProgram(programPath)); |
Maciej Żenczykowski | 2582445 | 2023-06-14 10:08:31 +0000 | [diff] [blame] | 67 | if (!prog.ok()) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 68 | return statusFromErrno(errno, fmt::format("Failed to get program from {}", programPath)); |
Maciej Żenczykowski | c576c0d | 2022-08-07 22:18:15 +0000 | [diff] [blame] | 69 | } |
| 70 | return netdutils::status::ok; |
| 71 | } |
| 72 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 73 | static Status initPrograms(const char* cg2_path) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 74 | if (!cg2_path) return Status("cg2_path is NULL"); |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 75 | |
Maciej Żenczykowski | c2dd01c | 2023-09-01 21:02:36 +0000 | [diff] [blame] | 76 | // This code was mainlined in T, so this should be trivially satisfied. |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 77 | if (!modules::sdklevel::IsAtLeastT()) return Status("S- platform is unsupported"); |
Maciej Żenczykowski | c2dd01c | 2023-09-01 21:02:36 +0000 | [diff] [blame] | 78 | |
| 79 | // 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] | 80 | if (!bpf::isAtLeastKernelVersion(4, 9, 0)) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 81 | return Status("kernel version < 4.9.0 is unsupported"); |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 82 | } |
Maciej Żenczykowski | c2dd01c | 2023-09-01 21:02:36 +0000 | [diff] [blame] | 83 | |
| 84 | // U bumps the kernel requirement up to 4.14 |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 85 | if (modules::sdklevel::IsAtLeastU() && !bpf::isAtLeastKernelVersion(4, 14, 0)) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 86 | return Status("U+ platform with kernel version < 4.14.0 is unsupported"); |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 87 | } |
Maciej Żenczykowski | c2dd01c | 2023-09-01 21:02:36 +0000 | [diff] [blame] | 88 | |
Maciej Żenczykowski | c2dd01c | 2023-09-01 21:02:36 +0000 | [diff] [blame] | 89 | // 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] | 90 | if (modules::sdklevel::IsAtLeastU() && !!strcmp(cg2_path, "/sys/fs/cgroup")) { |
Ken Chen | d6ea75a | 2023-11-28 23:29:47 +0800 | [diff] [blame] | 91 | return Status("U+ platform with cg2_path != /sys/fs/cgroup is unsupported"); |
Ken Chen | 5d14649 | 2023-11-27 17:05:43 +0800 | [diff] [blame] | 92 | } |
Maciej Żenczykowski | 65075bb | 2023-06-01 23:09:14 +0000 | [diff] [blame] | 93 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 94 | 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] | 95 | if (!cg_fd.ok()) { |
| 96 | const int err = errno; |
| 97 | ALOGE("Failed to open the cgroup directory: %s", strerror(err)); |
| 98 | return statusFromErrno(err, "Open the cgroup directory failed"); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 99 | } |
Maciej Żenczykowski | c576c0d | 2022-08-07 22:18:15 +0000 | [diff] [blame] | 100 | RETURN_IF_NOT_OK(checkProgramAccessible(XT_BPF_ALLOWLIST_PROG_PATH)); |
| 101 | RETURN_IF_NOT_OK(checkProgramAccessible(XT_BPF_DENYLIST_PROG_PATH)); |
| 102 | RETURN_IF_NOT_OK(checkProgramAccessible(XT_BPF_EGRESS_PROG_PATH)); |
| 103 | RETURN_IF_NOT_OK(checkProgramAccessible(XT_BPF_INGRESS_PROG_PATH)); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 104 | RETURN_IF_NOT_OK(attachProgramToCgroup(BPF_EGRESS_PROG_PATH, cg_fd, BPF_CGROUP_INET_EGRESS)); |
| 105 | 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] | 106 | |
| 107 | // For the devices that support cgroup socket filter, the socket filter |
| 108 | // should be loaded successfully by bpfloader. So we attach the filter to |
| 109 | // cgroup if the program is pinned properly. |
| 110 | // TODO: delete the if statement once all devices should support cgroup |
| 111 | // socket filter (ie. the minimum kernel version required is 4.14). |
Maciej Żenczykowski | 0309362 | 2023-02-22 22:09:25 +0000 | [diff] [blame] | 112 | if (bpf::isAtLeastKernelVersion(4, 14, 0)) { |
Maciej Żenczykowski | 22db590 | 2024-05-10 06:44:08 -0700 | [diff] [blame] | 113 | RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_INET_CREATE_PROG_PATH, |
| 114 | cg_fd, BPF_CGROUP_INET_SOCK_CREATE)); |
| 115 | } |
| 116 | |
Maciej Żenczykowski | 6e94583 | 2024-09-23 20:20:36 +0000 | [diff] [blame] | 117 | if (bpf::isAtLeastKernelVersion(5, 10, 0)) { |
| 118 | RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_INET_RELEASE_PROG_PATH, |
| 119 | cg_fd, BPF_CGROUP_INET_SOCK_RELEASE)); |
| 120 | } |
| 121 | |
Maciej Żenczykowski | 22db590 | 2024-05-10 06:44:08 -0700 | [diff] [blame] | 122 | if (modules::sdklevel::IsAtLeastV()) { |
Maciej Żenczykowski | 05ebcf0 | 2024-06-18 17:49:19 -0700 | [diff] [blame] | 123 | RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_CONNECT4_PROG_PATH, |
| 124 | cg_fd, BPF_CGROUP_INET4_CONNECT)); |
| 125 | RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_CONNECT6_PROG_PATH, |
| 126 | cg_fd, BPF_CGROUP_INET6_CONNECT)); |
| 127 | RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_UDP4_RECVMSG_PROG_PATH, |
| 128 | cg_fd, BPF_CGROUP_UDP4_RECVMSG)); |
| 129 | RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_UDP6_RECVMSG_PROG_PATH, |
| 130 | cg_fd, BPF_CGROUP_UDP6_RECVMSG)); |
| 131 | RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_UDP4_SENDMSG_PROG_PATH, |
| 132 | cg_fd, BPF_CGROUP_UDP4_SENDMSG)); |
| 133 | RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_UDP6_SENDMSG_PROG_PATH, |
| 134 | cg_fd, BPF_CGROUP_UDP6_SENDMSG)); |
| 135 | |
| 136 | if (bpf::isAtLeastKernelVersion(5, 4, 0)) { |
Maciej Żenczykowski | 22db590 | 2024-05-10 06:44:08 -0700 | [diff] [blame] | 137 | RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_GETSOCKOPT_PROG_PATH, |
| 138 | cg_fd, BPF_CGROUP_GETSOCKOPT)); |
| 139 | RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_SETSOCKOPT_PROG_PATH, |
| 140 | cg_fd, BPF_CGROUP_SETSOCKOPT)); |
Maciej Żenczykowski | 05ebcf0 | 2024-06-18 17:49:19 -0700 | [diff] [blame] | 141 | } |
Lorenzo Colitti | 3505b58 | 2022-10-27 19:36:27 +0900 | [diff] [blame] | 142 | } |
Maciej Żenczykowski | 5b2611d | 2023-10-04 00:44:56 +0000 | [diff] [blame] | 143 | |
Maciej Żenczykowski | 5b2611d | 2023-10-04 00:44:56 +0000 | [diff] [blame] | 144 | if (bpf::isAtLeastKernelVersion(4, 19, 0)) { |
Maciej Żenczykowski | 3ad3794 | 2024-09-19 00:13:04 +0000 | [diff] [blame] | 145 | RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_BIND4_PROG_PATH, |
Maciej Żenczykowski | 3cb494f | 2023-10-04 21:35:41 +0000 | [diff] [blame] | 146 | cg_fd, BPF_CGROUP_INET4_BIND)); |
Maciej Żenczykowski | 3ad3794 | 2024-09-19 00:13:04 +0000 | [diff] [blame] | 147 | RETURN_IF_NOT_OK(attachProgramToCgroup(CGROUP_BIND6_PROG_PATH, |
Maciej Żenczykowski | 3cb494f | 2023-10-04 21:35:41 +0000 | [diff] [blame] | 148 | cg_fd, BPF_CGROUP_INET6_BIND)); |
| 149 | |
| 150 | // This should trivially pass, since we just attached up above, |
| 151 | // but BPF_PROG_QUERY is only implemented on 4.19+ kernels. |
Maciej Żenczykowski | 5b2611d | 2023-10-04 00:44:56 +0000 | [diff] [blame] | 152 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET_EGRESS) <= 0) abort(); |
| 153 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET_INGRESS) <= 0) abort(); |
| 154 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET_SOCK_CREATE) <= 0) abort(); |
Maciej Żenczykowski | 3cb494f | 2023-10-04 21:35:41 +0000 | [diff] [blame] | 155 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET4_BIND) <= 0) abort(); |
| 156 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET6_BIND) <= 0) abort(); |
Maciej Żenczykowski | 5b2611d | 2023-10-04 00:44:56 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Maciej Żenczykowski | 6e94583 | 2024-09-23 20:20:36 +0000 | [diff] [blame] | 159 | if (bpf::isAtLeastKernelVersion(5, 10, 0)) { |
| 160 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET_SOCK_RELEASE) <= 0) abort(); |
| 161 | } |
| 162 | |
Maciej Żenczykowski | 22db590 | 2024-05-10 06:44:08 -0700 | [diff] [blame] | 163 | if (modules::sdklevel::IsAtLeastV()) { |
Maciej Żenczykowski | 05ebcf0 | 2024-06-18 17:49:19 -0700 | [diff] [blame] | 164 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET4_CONNECT) <= 0) abort(); |
| 165 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_INET6_CONNECT) <= 0) abort(); |
| 166 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_UDP4_RECVMSG) <= 0) abort(); |
| 167 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_UDP6_RECVMSG) <= 0) abort(); |
| 168 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_UDP4_SENDMSG) <= 0) abort(); |
| 169 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_UDP6_SENDMSG) <= 0) abort(); |
| 170 | |
| 171 | if (bpf::isAtLeastKernelVersion(5, 4, 0)) { |
Maciej Żenczykowski | 22db590 | 2024-05-10 06:44:08 -0700 | [diff] [blame] | 172 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_GETSOCKOPT) <= 0) abort(); |
| 173 | if (bpf::queryProgram(cg_fd, BPF_CGROUP_SETSOCKOPT) <= 0) abort(); |
Maciej Żenczykowski | 05ebcf0 | 2024-06-18 17:49:19 -0700 | [diff] [blame] | 174 | } |
Maciej Żenczykowski | 22db590 | 2024-05-10 06:44:08 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 177 | return netdutils::status::ok; |
| 178 | } |
| 179 | |
| 180 | BpfHandler::BpfHandler() |
| 181 | : mPerUidStatsEntriesLimit(PER_UID_STATS_ENTRIES_LIMIT), |
| 182 | mTotalUidStatsEntriesLimit(TOTAL_UID_STATS_ENTRIES_LIMIT) {} |
| 183 | |
| 184 | BpfHandler::BpfHandler(uint32_t perUidLimit, uint32_t totalLimit) |
| 185 | : mPerUidStatsEntriesLimit(perUidLimit), mTotalUidStatsEntriesLimit(totalLimit) {} |
| 186 | |
Maciej Żenczykowski | 15f9731 | 2024-06-13 14:11:28 -0700 | [diff] [blame] | 187 | static bool mainlineNetBpfLoadDone() { |
| 188 | return !access("/sys/fs/bpf/netd_shared/mainline_done", F_OK); |
| 189 | } |
| 190 | |
Maciej Żenczykowski | 732a141 | 2024-03-14 00:17:18 -0700 | [diff] [blame] | 191 | // copied with minor changes from waitForProgsLoaded() |
| 192 | // p/m/C's staticlibs/native/bpf_headers/include/bpf/WaitForProgsLoaded.h |
| 193 | static inline void waitForNetProgsLoaded() { |
| 194 | // infinite loop until success with 5/10/20/40/60/60/60... delay |
| 195 | for (int delay = 5;; delay *= 2) { |
| 196 | if (delay > 60) delay = 60; |
Maciej Żenczykowski | 15f9731 | 2024-06-13 14:11:28 -0700 | [diff] [blame] | 197 | if (WaitForProperty("init.svc.mdnsd_netbpfload", "stopped", std::chrono::seconds(delay)) |
| 198 | && mainlineNetBpfLoadDone()) |
Maciej Żenczykowski | 732a141 | 2024-03-14 00:17:18 -0700 | [diff] [blame] | 199 | return; |
Maciej Żenczykowski | 15f9731 | 2024-06-13 14:11:28 -0700 | [diff] [blame] | 200 | ALOGW("Waited %ds for init.svc.mdnsd_netbpfload=stopped, still waiting...", delay); |
Maciej Żenczykowski | 732a141 | 2024-03-14 00:17:18 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Maciej Żenczykowski | 23c02b0 | 2024-10-02 22:48:19 +0000 | [diff] [blame] | 204 | static inline void waitForBpf() { |
Maciej Żenczykowski | 15f9731 | 2024-06-13 14:11:28 -0700 | [diff] [blame] | 205 | // Note: netd *can* be restarted, so this might get called a second time after boot is complete |
| 206 | // at which point we don't need to (and shouldn't) wait for (more importantly start) loading bpf |
| 207 | |
Maciej Żenczykowski | 23d2c1e | 2024-03-28 22:54:01 -0700 | [diff] [blame] | 208 | if (base::GetProperty("bpf.progs_loaded", "") != "1") { |
Maciej Żenczykowski | 15f9731 | 2024-06-13 14:11:28 -0700 | [diff] [blame] | 209 | // AOSP platform netd & mainline don't need this (at least prior to U QPR3), |
| 210 | // but there could be platform provided (xt_)bpf programs that oem/vendor |
| 211 | // modified netd (which calls us during init) depends on... |
| 212 | ALOGI("Waiting for platform BPF programs"); |
Maciej Żenczykowski | 23d2c1e | 2024-03-28 22:54:01 -0700 | [diff] [blame] | 213 | android::bpf::waitForProgsLoaded(); |
Maciej Żenczykowski | 732a141 | 2024-03-14 00:17:18 -0700 | [diff] [blame] | 214 | } |
Maciej Żenczykowski | 15f9731 | 2024-06-13 14:11:28 -0700 | [diff] [blame] | 215 | |
Maciej Żenczykowski | 4e4f872 | 2024-06-15 06:38:08 -0700 | [diff] [blame] | 216 | if (!mainlineNetBpfLoadDone()) { |
Maciej Żenczykowski | 7262899 | 2024-06-14 13:46:11 -0700 | [diff] [blame] | 217 | // We're on < U QPR3 & it's the first time netd is starting up (unless crashlooping) |
| 218 | // |
| 219 | // On U QPR3+ netbpfload is guaranteed to run before the platform bpfloader, |
| 220 | // so waitForProgsLoaded() implies mainlineNetBpfLoadDone(). |
Maciej Żenczykowski | 15f9731 | 2024-06-13 14:11:28 -0700 | [diff] [blame] | 221 | if (!base::SetProperty("ctl.start", "mdnsd_netbpfload")) { |
| 222 | ALOGE("Failed to set property ctl.start=mdnsd_netbpfload, see dmesg for reason."); |
Maciej Żenczykowski | 2afffc2 | 2024-06-15 06:37:03 -0700 | [diff] [blame] | 223 | abort(); |
Maciej Żenczykowski | 15f9731 | 2024-06-13 14:11:28 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Maciej Żenczykowski | 2afffc2 | 2024-06-15 06:37:03 -0700 | [diff] [blame] | 226 | ALOGI("Waiting for Networking BPF programs"); |
| 227 | waitForNetProgsLoaded(); |
| 228 | ALOGI("Networking BPF programs are loaded"); |
Maciej Żenczykowski | 15f9731 | 2024-06-13 14:11:28 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 231 | ALOGI("BPF programs are loaded"); |
Maciej Żenczykowski | 23c02b0 | 2024-10-02 22:48:19 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | Status BpfHandler::init(const char* cg2_path) { |
| 235 | // This wait is effectively a no-op on U QPR3+ devices (as netd starts |
Maciej Żenczykowski | e611324 | 2024-10-03 18:42:18 +0000 | [diff] [blame^] | 236 | // *after* the synchronous 'exec_start bpfloader' which calls NetBpfLoad) |
Maciej Żenczykowski | 23c02b0 | 2024-10-02 22:48:19 +0000 | [diff] [blame] | 237 | // but checking for U QPR3 is hard. |
| 238 | // |
| 239 | // Waiting should not be required on U QPR3+ devices, |
| 240 | // ... |
| 241 | // |
| 242 | // ...unless someone changed 'exec_start bpfloader' to 'start bpfloader' |
| 243 | // in the rc file. |
| 244 | // |
| 245 | // TODO: should be: if (!modules::sdklevel::IsAtLeastW()) |
| 246 | if (android_get_device_api_level() <= __ANDROID_API_V__) waitForBpf(); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 247 | |
| 248 | RETURN_IF_NOT_OK(initPrograms(cg2_path)); |
| 249 | RETURN_IF_NOT_OK(initMaps()); |
| 250 | |
| 251 | return netdutils::status::ok; |
| 252 | } |
| 253 | |
Maciej Żenczykowski | 52018c8 | 2024-06-04 16:05:16 +0000 | [diff] [blame] | 254 | static void mapLockTest(void) { |
| 255 | // The maps must be R/W, and as yet unopened (or more specifically not yet lock'ed). |
| 256 | const char * const m1 = BPF_NETD_PATH "map_netd_lock_array_test_map"; |
| 257 | const char * const m2 = BPF_NETD_PATH "map_netd_lock_hash_test_map"; |
| 258 | |
Maciej Żenczykowski | 7eb7d67 | 2024-06-14 13:55:09 -0700 | [diff] [blame] | 259 | unique_fd fd0(bpf::mapRetrieveExclusiveRW(m1)); if (!fd0.ok()) abort(); // grabs exclusive lock |
Maciej Żenczykowski | 52018c8 | 2024-06-04 16:05:16 +0000 | [diff] [blame] | 260 | |
| 261 | unique_fd fd1(bpf::mapRetrieveExclusiveRW(m2)); if (!fd1.ok()) abort(); // no conflict with fd0 |
| 262 | unique_fd fd2(bpf::mapRetrieveExclusiveRW(m2)); if ( fd2.ok()) abort(); // busy due to fd1 |
| 263 | unique_fd fd3(bpf::mapRetrieveRO(m2)); if (!fd3.ok()) abort(); // no lock taken |
| 264 | unique_fd fd4(bpf::mapRetrieveRW(m2)); if ( fd4.ok()) abort(); // busy due to fd1 |
| 265 | fd1.reset(); // releases exclusive lock |
| 266 | unique_fd fd5(bpf::mapRetrieveRO(m2)); if (!fd5.ok()) abort(); // no lock taken |
| 267 | unique_fd fd6(bpf::mapRetrieveRW(m2)); if (!fd6.ok()) abort(); // now ok |
| 268 | unique_fd fd7(bpf::mapRetrieveRO(m2)); if (!fd7.ok()) abort(); // no lock taken |
| 269 | unique_fd fd8(bpf::mapRetrieveExclusiveRW(m2)); if ( fd8.ok()) abort(); // busy due to fd6 |
Maciej Żenczykowski | 7eb7d67 | 2024-06-14 13:55:09 -0700 | [diff] [blame] | 270 | |
| 271 | fd0.reset(); // releases exclusive lock |
| 272 | unique_fd fd9(bpf::mapRetrieveWO(m1)); if (!fd9.ok()) abort(); // grabs exclusive lock |
Maciej Żenczykowski | 52018c8 | 2024-06-04 16:05:16 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 275 | Status BpfHandler::initMaps() { |
Patrick Rohr | 78ad003 | 2024-06-28 00:25:50 +0000 | [diff] [blame] | 276 | // bpfLock() requires bpfGetFdMapId which is only available on 4.14+ kernels. |
| 277 | if (bpf::isAtLeastKernelVersion(4, 14, 0)) { |
| 278 | mapLockTest(); |
| 279 | } |
Maciej Żenczykowski | 52018c8 | 2024-06-04 16:05:16 +0000 | [diff] [blame] | 280 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 281 | RETURN_IF_NOT_OK(mStatsMapA.init(STATS_MAP_A_PATH)); |
| 282 | RETURN_IF_NOT_OK(mStatsMapB.init(STATS_MAP_B_PATH)); |
| 283 | RETURN_IF_NOT_OK(mConfigurationMap.init(CONFIGURATION_MAP_PATH)); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 284 | RETURN_IF_NOT_OK(mUidPermissionMap.init(UID_PERMISSION_MAP_PATH)); |
Maciej Żenczykowski | b5868a0 | 2022-08-31 04:21:01 +0000 | [diff] [blame] | 285 | // initialized last so mCookieTagMap.isValid() implies everything else is valid too |
| 286 | RETURN_IF_NOT_OK(mCookieTagMap.init(COOKIE_TAG_MAP_PATH)); |
Ken Chen | 322ffcb | 2022-05-23 22:27:40 +0800 | [diff] [blame] | 287 | ALOGI("%s successfully", __func__); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 288 | |
| 289 | return netdutils::status::ok; |
| 290 | } |
| 291 | |
| 292 | bool BpfHandler::hasUpdateDeviceStatsPermission(uid_t uid) { |
| 293 | // This implementation is the same logic as method ActivityManager#checkComponentPermission. |
| 294 | // It implies that the real uid can never be the same as PER_USER_RANGE. |
| 295 | uint32_t appId = uid % PER_USER_RANGE; |
| 296 | auto permission = mUidPermissionMap.readValue(appId); |
| 297 | if (permission.ok() && (permission.value() & BPF_PERMISSION_UPDATE_DEVICE_STATS)) { |
| 298 | return true; |
| 299 | } |
| 300 | return ((appId == AID_ROOT) || (appId == AID_SYSTEM) || (appId == AID_DNS)); |
| 301 | } |
| 302 | |
| 303 | 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] | 304 | if (!mCookieTagMap.isValid()) return -EPERM; |
| 305 | |
| 306 | if (chargeUid != realUid && !hasUpdateDeviceStatsPermission(realUid)) return -EPERM; |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 307 | |
Hungming Chen | 436547e | 2022-02-18 17:52:11 +0800 | [diff] [blame] | 308 | // Note that tagging the socket to AID_CLAT is only implemented in JNI ClatCoordinator. |
| 309 | // The process is not allowed to tag socket to AID_CLAT via tagSocket() which would cause |
| 310 | // process data usage accounting to be bypassed. Tagging AID_CLAT is used for avoiding counting |
| 311 | // CLAT traffic data usage twice. See packages/modules/Connectivity/service/jni/ |
| 312 | // com_android_server_connectivity_ClatCoordinator.cpp |
Maciej Żenczykowski | 4938d40 | 2022-08-14 14:36:20 +0000 | [diff] [blame] | 313 | if (chargeUid == AID_CLAT) return -EPERM; |
Hungming Chen | 436547e | 2022-02-18 17:52:11 +0800 | [diff] [blame] | 314 | |
Hungming Chen | 478c0eb | 2022-03-04 21:16:59 +0800 | [diff] [blame] | 315 | // The socket destroy listener only monitors on the group {INET_TCP, INET_UDP, INET6_TCP, |
| 316 | // INET6_UDP}. Tagging listener unsupported socket causes that the tag can't be removed from |
| 317 | // 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] | 318 | // entries. Note that although tagSocket() of net client has already denied the family which |
| 319 | // is neither AF_INET nor AF_INET6, the family validation is still added here just in case. |
| 320 | // See tagSocket in system/netd/client/NetdClient.cpp and |
| 321 | // TrafficController::makeSkDestroyListener in |
Hungming Chen | 478c0eb | 2022-03-04 21:16:59 +0800 | [diff] [blame] | 322 | // packages/modules/Connectivity/service/native/TrafficController.cpp |
| 323 | // 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] | 324 | int socketFamily; |
| 325 | socklen_t familyLen = sizeof(socketFamily); |
| 326 | if (getsockopt(sockFd, SOL_SOCKET, SO_DOMAIN, &socketFamily, &familyLen)) { |
| 327 | ALOGE("Failed to getsockopt SO_DOMAIN: %s, fd: %d", strerror(errno), sockFd); |
Hungming Chen | 478c0eb | 2022-03-04 21:16:59 +0800 | [diff] [blame] | 328 | return -errno; |
Hungming Chen | bcc0f5b | 2022-03-07 14:13:49 +0800 | [diff] [blame] | 329 | } |
| 330 | if (socketFamily != AF_INET && socketFamily != AF_INET6) { |
| 331 | ALOGE("Unsupported family: %d", socketFamily); |
| 332 | return -EAFNOSUPPORT; |
| 333 | } |
| 334 | |
| 335 | int socketProto; |
| 336 | socklen_t protoLen = sizeof(socketProto); |
| 337 | if (getsockopt(sockFd, SOL_SOCKET, SO_PROTOCOL, &socketProto, &protoLen)) { |
| 338 | ALOGE("Failed to getsockopt SO_PROTOCOL: %s, fd: %d", strerror(errno), sockFd); |
| 339 | return -errno; |
| 340 | } |
| 341 | if (socketProto != IPPROTO_UDP && socketProto != IPPROTO_TCP) { |
| 342 | ALOGE("Unsupported protocol: %d", socketProto); |
| 343 | return -EPROTONOSUPPORT; |
Hungming Chen | 478c0eb | 2022-03-04 21:16:59 +0800 | [diff] [blame] | 344 | } |
| 345 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 346 | uint64_t sock_cookie = getSocketCookie(sockFd); |
Maciej Żenczykowski | d3fe154 | 2023-02-23 03:56:45 +0000 | [diff] [blame] | 347 | if (!sock_cookie) return -errno; |
Maciej Żenczykowski | 4938d40 | 2022-08-14 14:36:20 +0000 | [diff] [blame] | 348 | |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 349 | UidTagValue newKey = {.uid = (uint32_t)chargeUid, .tag = tag}; |
| 350 | |
| 351 | uint32_t totalEntryCount = 0; |
| 352 | uint32_t perUidEntryCount = 0; |
| 353 | // Now we go through the stats map and count how many entries are associated |
| 354 | // 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] | 355 | // the request to prevent the map from overflow. Note though that it isn't really |
| 356 | // safe here to iterate over the map since it might be modified by the system server, |
| 357 | // which might toggle the live stats map and clean it. |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 358 | const auto countUidStatsEntries = [chargeUid, &totalEntryCount, &perUidEntryCount]( |
| 359 | const StatsKey& key, |
Maciej Żenczykowski | 7e2f53e | 2023-09-28 01:08:28 +0000 | [diff] [blame] | 360 | const BpfMapRO<StatsKey, StatsValue>&) { |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 361 | if (key.uid == chargeUid) { |
| 362 | perUidEntryCount++; |
| 363 | } |
| 364 | totalEntryCount++; |
| 365 | return base::Result<void>(); |
| 366 | }; |
| 367 | auto configuration = mConfigurationMap.readValue(CURRENT_STATS_MAP_CONFIGURATION_KEY); |
| 368 | if (!configuration.ok()) { |
Maciej Żenczykowski | 85d4a5e | 2023-08-20 16:41:50 +0000 | [diff] [blame] | 369 | ALOGE("Failed to get current configuration: %s", |
| 370 | strerror(configuration.error().code())); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 371 | return -configuration.error().code(); |
| 372 | } |
| 373 | if (configuration.value() != SELECT_MAP_A && configuration.value() != SELECT_MAP_B) { |
| 374 | ALOGE("unknown configuration value: %d", configuration.value()); |
| 375 | return -EINVAL; |
| 376 | } |
| 377 | |
Maciej Żenczykowski | 7e2f53e | 2023-09-28 01:08:28 +0000 | [diff] [blame] | 378 | BpfMapRO<StatsKey, StatsValue>& currentMap = |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 379 | (configuration.value() == SELECT_MAP_A) ? mStatsMapA : mStatsMapB; |
| 380 | base::Result<void> res = currentMap.iterate(countUidStatsEntries); |
| 381 | if (!res.ok()) { |
Maciej Żenczykowski | 85d4a5e | 2023-08-20 16:41:50 +0000 | [diff] [blame] | 382 | ALOGE("Failed to count the stats entry in map: %s", |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 383 | strerror(res.error().code())); |
| 384 | return -res.error().code(); |
| 385 | } |
| 386 | |
| 387 | if (totalEntryCount > mTotalUidStatsEntriesLimit || |
| 388 | perUidEntryCount > mPerUidStatsEntriesLimit) { |
| 389 | ALOGE("Too many stats entries in the map, total count: %u, chargeUid(%u) count: %u," |
| 390 | " blocking tag request to prevent map overflow", |
| 391 | totalEntryCount, chargeUid, perUidEntryCount); |
| 392 | return -EMFILE; |
| 393 | } |
| 394 | // Update the tag information of a socket to the cookieUidMap. Use BPF_ANY |
| 395 | // 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] | 396 | // 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] | 397 | // 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] | 398 | // 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] | 399 | res = mCookieTagMap.writeValue(sock_cookie, newKey, BPF_ANY); |
| 400 | if (!res.ok()) { |
Maciej Żenczykowski | 85d4a5e | 2023-08-20 16:41:50 +0000 | [diff] [blame] | 401 | ALOGE("Failed to tag the socket: %s", strerror(res.error().code())); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 402 | return -res.error().code(); |
| 403 | } |
Nick Wille | 5076a02 | 2023-06-01 18:39:25 +0000 | [diff] [blame] | 404 | ALOGD("Socket with cookie %" PRIu64 " tagged successfully with tag %" PRIu32 " uid %u " |
| 405 | "and real uid %u", sock_cookie, tag, chargeUid, realUid); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | int BpfHandler::untagSocket(int sockFd) { |
Maciej Żenczykowski | 4938d40 | 2022-08-14 14:36:20 +0000 | [diff] [blame] | 410 | uint64_t sock_cookie = getSocketCookie(sockFd); |
Maciej Żenczykowski | d3fe154 | 2023-02-23 03:56:45 +0000 | [diff] [blame] | 411 | if (!sock_cookie) return -errno; |
Maciej Żenczykowski | 4938d40 | 2022-08-14 14:36:20 +0000 | [diff] [blame] | 412 | |
| 413 | if (!mCookieTagMap.isValid()) return -EPERM; |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 414 | base::Result<void> res = mCookieTagMap.deleteValue(sock_cookie); |
| 415 | if (!res.ok()) { |
Maciej Żenczykowski | e0f5846 | 2022-05-17 13:59:22 -0700 | [diff] [blame] | 416 | ALOGE("Failed to untag socket: %s", strerror(res.error().code())); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 417 | return -res.error().code(); |
| 418 | } |
Nick Wille | 5076a02 | 2023-06-01 18:39:25 +0000 | [diff] [blame] | 419 | ALOGD("Socket with cookie %" PRIu64 " untagged successfully.", sock_cookie); |
Ken Chen | 1647f60 | 2021-10-05 21:55:22 +0800 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | } // namespace net |
| 424 | } // namespace android |