| 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> | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 47 | #include <log/log.h> | 
| Maciej Żenczykowski | 40dfe53 | 2023-10-08 20:21:11 -0700 | [diff] [blame] | 48 |  | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 49 | #include "BpfSyscallWrappers.h" | 
|  | 50 | #include "bpf/BpfUtils.h" | 
| Maciej Żenczykowski | 40dfe53 | 2023-10-08 20:21:11 -0700 | [diff] [blame] | 51 | #include "loader.h" | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 52 |  | 
|  | 53 | using android::base::EndsWith; | 
|  | 54 | using android::bpf::domain; | 
|  | 55 | using std::string; | 
|  | 56 |  | 
|  | 57 | bool exists(const char* const path) { | 
|  | 58 | int v = access(path, F_OK); | 
|  | 59 | if (!v) { | 
|  | 60 | ALOGI("%s exists.", path); | 
|  | 61 | return true; | 
|  | 62 | } | 
|  | 63 | if (errno == ENOENT) return false; | 
|  | 64 | ALOGE("FATAL: access(%s, F_OK) -> %d [%d:%s]", path, v, errno, strerror(errno)); | 
|  | 65 | abort();  // can only hit this if permissions (likely selinux) are screwed up | 
|  | 66 | } | 
|  | 67 |  | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 68 |  | 
|  | 69 | const android::bpf::Location locations[] = { | 
|  | 70 | // S+ Tethering mainline module (network_stack): tether offload | 
|  | 71 | { | 
|  | 72 | .dir = "/apex/com.android.tethering/etc/bpf/", | 
|  | 73 | .prefix = "tethering/", | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 74 | }, | 
|  | 75 | // T+ Tethering mainline module (shared with netd & system server) | 
|  | 76 | // netutils_wrapper (for iptables xt_bpf) has access to programs | 
|  | 77 | { | 
|  | 78 | .dir = "/apex/com.android.tethering/etc/bpf/netd_shared/", | 
|  | 79 | .prefix = "netd_shared/", | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 80 | }, | 
|  | 81 | // T+ Tethering mainline module (shared with netd & system server) | 
|  | 82 | // netutils_wrapper has no access, netd has read only access | 
|  | 83 | { | 
|  | 84 | .dir = "/apex/com.android.tethering/etc/bpf/netd_readonly/", | 
|  | 85 | .prefix = "netd_readonly/", | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 86 | }, | 
|  | 87 | // T+ Tethering mainline module (shared with system server) | 
|  | 88 | { | 
|  | 89 | .dir = "/apex/com.android.tethering/etc/bpf/net_shared/", | 
|  | 90 | .prefix = "net_shared/", | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 91 | }, | 
|  | 92 | // T+ Tethering mainline module (not shared, just network_stack) | 
|  | 93 | { | 
|  | 94 | .dir = "/apex/com.android.tethering/etc/bpf/net_private/", | 
|  | 95 | .prefix = "net_private/", | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 96 | }, | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 97 | }; | 
|  | 98 |  | 
|  | 99 | int loadAllElfObjects(const android::bpf::Location& location) { | 
|  | 100 | int retVal = 0; | 
|  | 101 | DIR* dir; | 
|  | 102 | struct dirent* ent; | 
|  | 103 |  | 
|  | 104 | if ((dir = opendir(location.dir)) != NULL) { | 
|  | 105 | while ((ent = readdir(dir)) != NULL) { | 
|  | 106 | string s = ent->d_name; | 
|  | 107 | if (!EndsWith(s, ".o")) continue; | 
|  | 108 |  | 
|  | 109 | string progPath(location.dir); | 
|  | 110 | progPath += s; | 
|  | 111 |  | 
|  | 112 | bool critical; | 
|  | 113 | int ret = android::bpf::loadProg(progPath.c_str(), &critical, location); | 
|  | 114 | if (ret) { | 
|  | 115 | if (critical) retVal = ret; | 
|  | 116 | ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret)); | 
|  | 117 | } else { | 
|  | 118 | ALOGI("Loaded object: %s", progPath.c_str()); | 
|  | 119 | } | 
|  | 120 | } | 
|  | 121 | closedir(dir); | 
|  | 122 | } | 
|  | 123 | return retVal; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | int createSysFsBpfSubDir(const char* const prefix) { | 
|  | 127 | if (*prefix) { | 
|  | 128 | mode_t prevUmask = umask(0); | 
|  | 129 |  | 
|  | 130 | string s = "/sys/fs/bpf/"; | 
|  | 131 | s += prefix; | 
|  | 132 |  | 
|  | 133 | errno = 0; | 
|  | 134 | int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO); | 
|  | 135 | if (ret && errno != EEXIST) { | 
|  | 136 | const int err = errno; | 
|  | 137 | ALOGE("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(err)); | 
|  | 138 | return -err; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | umask(prevUmask); | 
|  | 142 | } | 
|  | 143 | return 0; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | // Technically 'value' doesn't need to be newline terminated, but it's best | 
|  | 147 | // to include a newline to match 'echo "value" > /proc/sys/...foo' behaviour, | 
|  | 148 | // which is usually how kernel devs test the actual sysctl interfaces. | 
|  | 149 | int writeProcSysFile(const char *filename, const char *value) { | 
|  | 150 | android::base::unique_fd fd(open(filename, O_WRONLY | O_CLOEXEC)); | 
|  | 151 | if (fd < 0) { | 
|  | 152 | const int err = errno; | 
|  | 153 | ALOGE("open('%s', O_WRONLY | O_CLOEXEC) -> %s", filename, strerror(err)); | 
|  | 154 | return -err; | 
|  | 155 | } | 
|  | 156 | int len = strlen(value); | 
|  | 157 | int v = write(fd, value, len); | 
|  | 158 | if (v < 0) { | 
|  | 159 | const int err = errno; | 
|  | 160 | ALOGE("write('%s', '%s', %d) -> %s", filename, value, len, strerror(err)); | 
|  | 161 | return -err; | 
|  | 162 | } | 
|  | 163 | if (v != len) { | 
|  | 164 | // In practice, due to us only using this for /proc/sys/... files, this can't happen. | 
|  | 165 | ALOGE("write('%s', '%s', %d) -> short write [%d]", filename, value, len, v); | 
|  | 166 | return -EINVAL; | 
|  | 167 | } | 
|  | 168 | return 0; | 
|  | 169 | } | 
|  | 170 |  | 
| Maciej Żenczykowski | 58c1822 | 2023-10-20 14:40:16 -0700 | [diff] [blame] | 171 | int main(int argc, char** argv, char * const envp[]) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 172 | (void)argc; | 
|  | 173 | android::base::InitLogging(argv, &android::base::KernelLogger); | 
|  | 174 |  | 
|  | 175 | if (!android::bpf::isAtLeastKernelVersion(4, 19, 0)) { | 
|  | 176 | ALOGE("Android U QPR2 requires kernel 4.19."); | 
|  | 177 | return 1; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | if (android::bpf::isUserspace32bit() && android::bpf::isAtLeastKernelVersion(6, 2, 0)) { | 
|  | 181 | /* Android 14/U should only launch on 64-bit kernels | 
|  | 182 | *   T launches on 5.10/5.15 | 
|  | 183 | *   U launches on 5.15/6.1 | 
|  | 184 | * So >=5.16 implies isKernel64Bit() | 
|  | 185 | * | 
|  | 186 | * We thus added a test to V VTS which requires 5.16+ devices to use 64-bit kernels. | 
|  | 187 | * | 
|  | 188 | * Starting with Android V, which is the first to support a post 6.1 Linux Kernel, | 
|  | 189 | * we also require 64-bit userspace. | 
|  | 190 | * | 
|  | 191 | * There are various known issues with 32-bit userspace talking to various | 
|  | 192 | * kernel interfaces (especially CAP_NET_ADMIN ones) on a 64-bit kernel. | 
|  | 193 | * Some of these have userspace or kernel workarounds/hacks. | 
|  | 194 | * Some of them don't... | 
|  | 195 | * We're going to be removing the hacks. | 
|  | 196 | * | 
|  | 197 | * Additionally the 32-bit kernel jit support is poor, | 
|  | 198 | * and 32-bit userspace on 64-bit kernel bpf ringbuffer compatibility is broken. | 
|  | 199 | */ | 
|  | 200 | ALOGE("64-bit userspace required on 6.2+ kernels."); | 
|  | 201 | return 1; | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | // Ensure we can determine the Android build type. | 
|  | 205 | if (!android::bpf::isEng() && !android::bpf::isUser() && !android::bpf::isUserdebug()) { | 
|  | 206 | ALOGE("Failed to determine the build type: got %s, want 'eng', 'user', or 'userdebug'", | 
|  | 207 | android::bpf::getBuildType().c_str()); | 
|  | 208 | return 1; | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | // Linux 5.16-rc1 changed the default to 2 (disabled but changeable), but we need 0 (enabled) | 
|  | 212 | // (this writeFile is known to fail on at least 4.19, but always defaults to 0 on pre-5.13, | 
|  | 213 | // on 5.13+ it depends on CONFIG_BPF_UNPRIV_DEFAULT_OFF) | 
|  | 214 | if (writeProcSysFile("/proc/sys/kernel/unprivileged_bpf_disabled", "0\n") && | 
|  | 215 | android::bpf::isAtLeastKernelVersion(5, 13, 0)) return 1; | 
|  | 216 |  | 
|  | 217 | // Enable the eBPF JIT -- but do note that on 64-bit kernels it is likely | 
|  | 218 | // already force enabled by the kernel config option BPF_JIT_ALWAYS_ON. | 
|  | 219 | // (Note: this (open) will fail with ENOENT 'No such file or directory' if | 
|  | 220 | //  kernel does not have CONFIG_BPF_JIT=y) | 
|  | 221 | // BPF_JIT is required by R VINTF (which means 4.14/4.19/5.4 kernels), | 
|  | 222 | // but 4.14/4.19 were released with P & Q, and only 5.4 is new in R+. | 
|  | 223 | if (writeProcSysFile("/proc/sys/net/core/bpf_jit_enable", "1\n")) return 1; | 
|  | 224 |  | 
|  | 225 | // Enable JIT kallsyms export for privileged users only | 
|  | 226 | // (Note: this (open) will fail with ENOENT 'No such file or directory' if | 
|  | 227 | //  kernel does not have CONFIG_HAVE_EBPF_JIT=y) | 
|  | 228 | if (writeProcSysFile("/proc/sys/net/core/bpf_jit_kallsyms", "1\n")) return 1; | 
|  | 229 |  | 
|  | 230 | // Create all the pin subdirectories | 
|  | 231 | // (this must be done first to allow selinux_context and pin_subdir functionality, | 
|  | 232 | //  which could otherwise fail with ENOENT during object pinning or renaming, | 
|  | 233 | //  due to ordering issues) | 
|  | 234 | for (const auto& location : locations) { | 
|  | 235 | if (createSysFsBpfSubDir(location.prefix)) return 1; | 
|  | 236 | } | 
|  | 237 |  | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 238 | // Load all ELF objects, create programs and maps, and pin them | 
|  | 239 | for (const auto& location : locations) { | 
|  | 240 | if (loadAllElfObjects(location) != 0) { | 
|  | 241 | ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS FROM %s ===", location.dir); | 
|  | 242 | ALOGE("If this triggers reliably, you're probably missing kernel options or patches."); | 
|  | 243 | ALOGE("If this triggers randomly, you might be hitting some memory allocation " | 
|  | 244 | "problems or startup script race."); | 
|  | 245 | ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---"); | 
|  | 246 | sleep(20); | 
|  | 247 | return 2; | 
|  | 248 | } | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | int key = 1; | 
|  | 252 | int value = 123; | 
|  | 253 | android::base::unique_fd map( | 
|  | 254 | android::bpf::createMap(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value), 2, 0)); | 
|  | 255 | if (android::bpf::writeToMapEntry(map, &key, &value, BPF_ANY)) { | 
|  | 256 | ALOGE("Critical kernel bug - failure to write into index 1 of 2 element bpf map array."); | 
|  | 257 | return 1; | 
|  | 258 | } | 
|  | 259 |  | 
| Maciej Żenczykowski | 58c1822 | 2023-10-20 14:40:16 -0700 | [diff] [blame] | 260 | ALOGI("done, transferring control to platform bpfloader."); | 
|  | 261 |  | 
|  | 262 | const char * args[] = { "/system/bin/bpfloader", NULL, }; | 
|  | 263 | if (execve(args[0], (char**)args, envp)) { | 
|  | 264 | ALOGE("FATAL: execve('/system/bin/bpfloader'): %d[%s]", errno, strerror(errno)); | 
|  | 265 | } | 
|  | 266 |  | 
|  | 267 | return 1; | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 268 | } |