Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 1 | /* |
Maciej Żenczykowski | 283c25a | 2023-10-02 19:43:30 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017-2023 The Android Open Source Project |
Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 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 | #ifndef LOG_TAG |
Maciej Żenczykowski | 283c25a | 2023-10-02 19:43:30 -0700 | [diff] [blame] | 18 | #define LOG_TAG "NetBpfLoad" |
Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 19 | #endif |
| 20 | |
| 21 | #include <arpa/inet.h> |
| 22 | #include <dirent.h> |
| 23 | #include <elf.h> |
| 24 | #include <error.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <inttypes.h> |
| 27 | #include <linux/bpf.h> |
| 28 | #include <linux/unistd.h> |
| 29 | #include <net/if.h> |
| 30 | #include <stdint.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | #include <sys/mman.h> |
| 37 | #include <sys/socket.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/types.h> |
| 40 | |
| 41 | #include <android-base/logging.h> |
| 42 | #include <android-base/macros.h> |
| 43 | #include <android-base/properties.h> |
| 44 | #include <android-base/stringprintf.h> |
| 45 | #include <android-base/strings.h> |
| 46 | #include <android-base/unique_fd.h> |
| 47 | #include <libbpf_android.h> |
| 48 | #include <log/log.h> |
| 49 | #include <netdutils/Misc.h> |
| 50 | #include <netdutils/Slice.h> |
| 51 | #include "BpfSyscallWrappers.h" |
| 52 | #include "bpf/BpfUtils.h" |
| 53 | |
| 54 | using android::base::EndsWith; |
| 55 | using android::bpf::domain; |
| 56 | using std::string; |
| 57 | |
| 58 | bool exists(const char* const path) { |
| 59 | int v = access(path, F_OK); |
| 60 | if (!v) { |
| 61 | ALOGI("%s exists.", path); |
| 62 | return true; |
| 63 | } |
| 64 | if (errno == ENOENT) return false; |
| 65 | ALOGE("FATAL: access(%s, F_OK) -> %d [%d:%s]", path, v, errno, strerror(errno)); |
| 66 | abort(); // can only hit this if permissions (likely selinux) are screwed up |
| 67 | } |
| 68 | |
| 69 | constexpr unsigned long long kTetheringApexDomainBitmask = |
| 70 | domainToBitmask(domain::tethering) | |
| 71 | domainToBitmask(domain::net_private) | |
| 72 | domainToBitmask(domain::net_shared) | |
| 73 | domainToBitmask(domain::netd_readonly) | |
| 74 | domainToBitmask(domain::netd_shared); |
| 75 | |
| 76 | // Programs shipped inside the tethering apex should be limited to networking stuff, |
| 77 | // as KPROBE, PERF_EVENT, TRACEPOINT are dangerous to use from mainline updatable code, |
| 78 | // since they are less stable abi/api and may conflict with platform uses of bpf. |
| 79 | constexpr bpf_prog_type kTetheringApexAllowedProgTypes[] = { |
| 80 | BPF_PROG_TYPE_CGROUP_SKB, |
| 81 | BPF_PROG_TYPE_CGROUP_SOCK, |
| 82 | BPF_PROG_TYPE_CGROUP_SOCKOPT, |
| 83 | BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 84 | BPF_PROG_TYPE_CGROUP_SYSCTL, |
| 85 | BPF_PROG_TYPE_LWT_IN, |
| 86 | BPF_PROG_TYPE_LWT_OUT, |
| 87 | BPF_PROG_TYPE_LWT_SEG6LOCAL, |
| 88 | BPF_PROG_TYPE_LWT_XMIT, |
| 89 | BPF_PROG_TYPE_SCHED_ACT, |
| 90 | BPF_PROG_TYPE_SCHED_CLS, |
| 91 | BPF_PROG_TYPE_SOCKET_FILTER, |
| 92 | BPF_PROG_TYPE_SOCK_OPS, |
| 93 | BPF_PROG_TYPE_XDP, |
| 94 | }; |
| 95 | |
Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 96 | |
| 97 | const android::bpf::Location locations[] = { |
| 98 | // S+ Tethering mainline module (network_stack): tether offload |
| 99 | { |
| 100 | .dir = "/apex/com.android.tethering/etc/bpf/", |
| 101 | .prefix = "tethering/", |
| 102 | .allowedDomainBitmask = kTetheringApexDomainBitmask, |
| 103 | .allowedProgTypes = kTetheringApexAllowedProgTypes, |
| 104 | .allowedProgTypesLength = arraysize(kTetheringApexAllowedProgTypes), |
| 105 | }, |
| 106 | // T+ Tethering mainline module (shared with netd & system server) |
| 107 | // netutils_wrapper (for iptables xt_bpf) has access to programs |
| 108 | { |
| 109 | .dir = "/apex/com.android.tethering/etc/bpf/netd_shared/", |
| 110 | .prefix = "netd_shared/", |
| 111 | .allowedDomainBitmask = kTetheringApexDomainBitmask, |
| 112 | .allowedProgTypes = kTetheringApexAllowedProgTypes, |
| 113 | .allowedProgTypesLength = arraysize(kTetheringApexAllowedProgTypes), |
| 114 | }, |
| 115 | // T+ Tethering mainline module (shared with netd & system server) |
| 116 | // netutils_wrapper has no access, netd has read only access |
| 117 | { |
| 118 | .dir = "/apex/com.android.tethering/etc/bpf/netd_readonly/", |
| 119 | .prefix = "netd_readonly/", |
| 120 | .allowedDomainBitmask = kTetheringApexDomainBitmask, |
| 121 | .allowedProgTypes = kTetheringApexAllowedProgTypes, |
| 122 | .allowedProgTypesLength = arraysize(kTetheringApexAllowedProgTypes), |
| 123 | }, |
| 124 | // T+ Tethering mainline module (shared with system server) |
| 125 | { |
| 126 | .dir = "/apex/com.android.tethering/etc/bpf/net_shared/", |
| 127 | .prefix = "net_shared/", |
| 128 | .allowedDomainBitmask = kTetheringApexDomainBitmask, |
| 129 | .allowedProgTypes = kTetheringApexAllowedProgTypes, |
| 130 | .allowedProgTypesLength = arraysize(kTetheringApexAllowedProgTypes), |
| 131 | }, |
| 132 | // T+ Tethering mainline module (not shared, just network_stack) |
| 133 | { |
| 134 | .dir = "/apex/com.android.tethering/etc/bpf/net_private/", |
| 135 | .prefix = "net_private/", |
| 136 | .allowedDomainBitmask = kTetheringApexDomainBitmask, |
| 137 | .allowedProgTypes = kTetheringApexAllowedProgTypes, |
| 138 | .allowedProgTypesLength = arraysize(kTetheringApexAllowedProgTypes), |
| 139 | }, |
Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | int loadAllElfObjects(const android::bpf::Location& location) { |
| 143 | int retVal = 0; |
| 144 | DIR* dir; |
| 145 | struct dirent* ent; |
| 146 | |
| 147 | if ((dir = opendir(location.dir)) != NULL) { |
| 148 | while ((ent = readdir(dir)) != NULL) { |
| 149 | string s = ent->d_name; |
| 150 | if (!EndsWith(s, ".o")) continue; |
| 151 | |
| 152 | string progPath(location.dir); |
| 153 | progPath += s; |
| 154 | |
| 155 | bool critical; |
| 156 | int ret = android::bpf::loadProg(progPath.c_str(), &critical, location); |
| 157 | if (ret) { |
| 158 | if (critical) retVal = ret; |
| 159 | ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret)); |
| 160 | } else { |
| 161 | ALOGI("Loaded object: %s", progPath.c_str()); |
| 162 | } |
| 163 | } |
| 164 | closedir(dir); |
| 165 | } |
| 166 | return retVal; |
| 167 | } |
| 168 | |
| 169 | int createSysFsBpfSubDir(const char* const prefix) { |
| 170 | if (*prefix) { |
| 171 | mode_t prevUmask = umask(0); |
| 172 | |
| 173 | string s = "/sys/fs/bpf/"; |
| 174 | s += prefix; |
| 175 | |
| 176 | errno = 0; |
| 177 | int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO); |
| 178 | if (ret && errno != EEXIST) { |
| 179 | const int err = errno; |
| 180 | ALOGE("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(err)); |
| 181 | return -err; |
| 182 | } |
| 183 | |
| 184 | umask(prevUmask); |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | // Technically 'value' doesn't need to be newline terminated, but it's best |
| 190 | // to include a newline to match 'echo "value" > /proc/sys/...foo' behaviour, |
| 191 | // which is usually how kernel devs test the actual sysctl interfaces. |
| 192 | int writeProcSysFile(const char *filename, const char *value) { |
| 193 | android::base::unique_fd fd(open(filename, O_WRONLY | O_CLOEXEC)); |
| 194 | if (fd < 0) { |
| 195 | const int err = errno; |
| 196 | ALOGE("open('%s', O_WRONLY | O_CLOEXEC) -> %s", filename, strerror(err)); |
| 197 | return -err; |
| 198 | } |
| 199 | int len = strlen(value); |
| 200 | int v = write(fd, value, len); |
| 201 | if (v < 0) { |
| 202 | const int err = errno; |
| 203 | ALOGE("write('%s', '%s', %d) -> %s", filename, value, len, strerror(err)); |
| 204 | return -err; |
| 205 | } |
| 206 | if (v != len) { |
| 207 | // In practice, due to us only using this for /proc/sys/... files, this can't happen. |
| 208 | ALOGE("write('%s', '%s', %d) -> short write [%d]", filename, value, len, v); |
| 209 | return -EINVAL; |
| 210 | } |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | int main(int argc, char** argv) { |
| 215 | (void)argc; |
| 216 | android::base::InitLogging(argv, &android::base::KernelLogger); |
| 217 | |
| 218 | if (!android::bpf::isAtLeastKernelVersion(4, 19, 0)) { |
| 219 | ALOGE("Android U QPR2 requires kernel 4.19."); |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | if (android::bpf::isUserspace32bit() && android::bpf::isAtLeastKernelVersion(6, 2, 0)) { |
| 224 | /* Android 14/U should only launch on 64-bit kernels |
| 225 | * T launches on 5.10/5.15 |
| 226 | * U launches on 5.15/6.1 |
| 227 | * So >=5.16 implies isKernel64Bit() |
| 228 | * |
| 229 | * We thus added a test to V VTS which requires 5.16+ devices to use 64-bit kernels. |
| 230 | * |
| 231 | * Starting with Android V, which is the first to support a post 6.1 Linux Kernel, |
| 232 | * we also require 64-bit userspace. |
| 233 | * |
| 234 | * There are various known issues with 32-bit userspace talking to various |
| 235 | * kernel interfaces (especially CAP_NET_ADMIN ones) on a 64-bit kernel. |
| 236 | * Some of these have userspace or kernel workarounds/hacks. |
| 237 | * Some of them don't... |
| 238 | * We're going to be removing the hacks. |
| 239 | * |
| 240 | * Additionally the 32-bit kernel jit support is poor, |
| 241 | * and 32-bit userspace on 64-bit kernel bpf ringbuffer compatibility is broken. |
| 242 | */ |
| 243 | ALOGE("64-bit userspace required on 6.2+ kernels."); |
| 244 | return 1; |
| 245 | } |
| 246 | |
| 247 | // Ensure we can determine the Android build type. |
| 248 | if (!android::bpf::isEng() && !android::bpf::isUser() && !android::bpf::isUserdebug()) { |
| 249 | ALOGE("Failed to determine the build type: got %s, want 'eng', 'user', or 'userdebug'", |
| 250 | android::bpf::getBuildType().c_str()); |
| 251 | return 1; |
| 252 | } |
| 253 | |
| 254 | // Linux 5.16-rc1 changed the default to 2 (disabled but changeable), but we need 0 (enabled) |
| 255 | // (this writeFile is known to fail on at least 4.19, but always defaults to 0 on pre-5.13, |
| 256 | // on 5.13+ it depends on CONFIG_BPF_UNPRIV_DEFAULT_OFF) |
| 257 | if (writeProcSysFile("/proc/sys/kernel/unprivileged_bpf_disabled", "0\n") && |
| 258 | android::bpf::isAtLeastKernelVersion(5, 13, 0)) return 1; |
| 259 | |
| 260 | // Enable the eBPF JIT -- but do note that on 64-bit kernels it is likely |
| 261 | // already force enabled by the kernel config option BPF_JIT_ALWAYS_ON. |
| 262 | // (Note: this (open) will fail with ENOENT 'No such file or directory' if |
| 263 | // kernel does not have CONFIG_BPF_JIT=y) |
| 264 | // BPF_JIT is required by R VINTF (which means 4.14/4.19/5.4 kernels), |
| 265 | // but 4.14/4.19 were released with P & Q, and only 5.4 is new in R+. |
| 266 | if (writeProcSysFile("/proc/sys/net/core/bpf_jit_enable", "1\n")) return 1; |
| 267 | |
| 268 | // Enable JIT kallsyms export for privileged users only |
| 269 | // (Note: this (open) will fail with ENOENT 'No such file or directory' if |
| 270 | // kernel does not have CONFIG_HAVE_EBPF_JIT=y) |
| 271 | if (writeProcSysFile("/proc/sys/net/core/bpf_jit_kallsyms", "1\n")) return 1; |
| 272 | |
| 273 | // Create all the pin subdirectories |
| 274 | // (this must be done first to allow selinux_context and pin_subdir functionality, |
| 275 | // which could otherwise fail with ENOENT during object pinning or renaming, |
| 276 | // due to ordering issues) |
| 277 | for (const auto& location : locations) { |
| 278 | if (createSysFsBpfSubDir(location.prefix)) return 1; |
| 279 | } |
| 280 | |
| 281 | // Note: there's no actual src dir for fs_bpf_loader .o's, |
| 282 | // so it is not listed in 'locations[].prefix'. |
| 283 | // This is because this is primarily meant for triggering genfscon rules, |
| 284 | // and as such this will likely always be the case. |
| 285 | // Thus we need to manually create the /sys/fs/bpf/loader subdirectory. |
| 286 | if (createSysFsBpfSubDir("loader")) return 1; |
| 287 | |
| 288 | // Load all ELF objects, create programs and maps, and pin them |
| 289 | for (const auto& location : locations) { |
| 290 | if (loadAllElfObjects(location) != 0) { |
| 291 | ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS FROM %s ===", location.dir); |
| 292 | ALOGE("If this triggers reliably, you're probably missing kernel options or patches."); |
| 293 | ALOGE("If this triggers randomly, you might be hitting some memory allocation " |
| 294 | "problems or startup script race."); |
| 295 | ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---"); |
| 296 | sleep(20); |
| 297 | return 2; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | int key = 1; |
| 302 | int value = 123; |
| 303 | android::base::unique_fd map( |
| 304 | android::bpf::createMap(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value), 2, 0)); |
| 305 | if (android::bpf::writeToMapEntry(map, &key, &value, BPF_ANY)) { |
| 306 | ALOGE("Critical kernel bug - failure to write into index 1 of 2 element bpf map array."); |
| 307 | return 1; |
| 308 | } |
| 309 | |
| 310 | if (android::base::SetProperty("bpf.progs_loaded", "1") == false) { |
| 311 | ALOGE("Failed to set bpf.progs_loaded property"); |
| 312 | return 1; |
| 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |