| 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 |  | 
| Maciej Żenczykowski | f33f128 | 2023-10-24 04:41:54 -0700 | [diff] [blame] | 41 | #include <android/api-level.h> | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 42 | #include <android-base/logging.h> | 
|  | 43 | #include <android-base/macros.h> | 
|  | 44 | #include <android-base/properties.h> | 
|  | 45 | #include <android-base/stringprintf.h> | 
|  | 46 | #include <android-base/strings.h> | 
|  | 47 | #include <android-base/unique_fd.h> | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 48 | #include <log/log.h> | 
| Maciej Żenczykowski | 40dfe53 | 2023-10-08 20:21:11 -0700 | [diff] [blame] | 49 |  | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 50 | #include "BpfSyscallWrappers.h" | 
|  | 51 | #include "bpf/BpfUtils.h" | 
| Maciej Żenczykowski | 40dfe53 | 2023-10-08 20:21:11 -0700 | [diff] [blame] | 52 | #include "loader.h" | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 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 |  | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 69 |  | 
|  | 70 | const android::bpf::Location locations[] = { | 
|  | 71 | // S+ Tethering mainline module (network_stack): tether offload | 
|  | 72 | { | 
|  | 73 | .dir = "/apex/com.android.tethering/etc/bpf/", | 
|  | 74 | .prefix = "tethering/", | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 75 | }, | 
|  | 76 | // T+ Tethering mainline module (shared with netd & system server) | 
|  | 77 | // netutils_wrapper (for iptables xt_bpf) has access to programs | 
|  | 78 | { | 
|  | 79 | .dir = "/apex/com.android.tethering/etc/bpf/netd_shared/", | 
|  | 80 | .prefix = "netd_shared/", | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 81 | }, | 
|  | 82 | // T+ Tethering mainline module (shared with netd & system server) | 
|  | 83 | // netutils_wrapper has no access, netd has read only access | 
|  | 84 | { | 
|  | 85 | .dir = "/apex/com.android.tethering/etc/bpf/netd_readonly/", | 
|  | 86 | .prefix = "netd_readonly/", | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 87 | }, | 
|  | 88 | // T+ Tethering mainline module (shared with system server) | 
|  | 89 | { | 
|  | 90 | .dir = "/apex/com.android.tethering/etc/bpf/net_shared/", | 
|  | 91 | .prefix = "net_shared/", | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 92 | }, | 
|  | 93 | // T+ Tethering mainline module (not shared, just network_stack) | 
|  | 94 | { | 
|  | 95 | .dir = "/apex/com.android.tethering/etc/bpf/net_private/", | 
|  | 96 | .prefix = "net_private/", | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 97 | }, | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 98 | }; | 
|  | 99 |  | 
|  | 100 | int loadAllElfObjects(const android::bpf::Location& location) { | 
|  | 101 | int retVal = 0; | 
|  | 102 | DIR* dir; | 
|  | 103 | struct dirent* ent; | 
|  | 104 |  | 
|  | 105 | if ((dir = opendir(location.dir)) != NULL) { | 
|  | 106 | while ((ent = readdir(dir)) != NULL) { | 
|  | 107 | string s = ent->d_name; | 
|  | 108 | if (!EndsWith(s, ".o")) continue; | 
|  | 109 |  | 
|  | 110 | string progPath(location.dir); | 
|  | 111 | progPath += s; | 
|  | 112 |  | 
|  | 113 | bool critical; | 
|  | 114 | int ret = android::bpf::loadProg(progPath.c_str(), &critical, location); | 
|  | 115 | if (ret) { | 
|  | 116 | if (critical) retVal = ret; | 
|  | 117 | ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret)); | 
|  | 118 | } else { | 
|  | 119 | ALOGI("Loaded object: %s", progPath.c_str()); | 
|  | 120 | } | 
|  | 121 | } | 
|  | 122 | closedir(dir); | 
|  | 123 | } | 
|  | 124 | return retVal; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | int createSysFsBpfSubDir(const char* const prefix) { | 
|  | 128 | if (*prefix) { | 
|  | 129 | mode_t prevUmask = umask(0); | 
|  | 130 |  | 
|  | 131 | string s = "/sys/fs/bpf/"; | 
|  | 132 | s += prefix; | 
|  | 133 |  | 
|  | 134 | errno = 0; | 
|  | 135 | int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO); | 
|  | 136 | if (ret && errno != EEXIST) { | 
|  | 137 | const int err = errno; | 
|  | 138 | ALOGE("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(err)); | 
|  | 139 | return -err; | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | umask(prevUmask); | 
|  | 143 | } | 
|  | 144 | return 0; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | // Technically 'value' doesn't need to be newline terminated, but it's best | 
|  | 148 | // to include a newline to match 'echo "value" > /proc/sys/...foo' behaviour, | 
|  | 149 | // which is usually how kernel devs test the actual sysctl interfaces. | 
|  | 150 | int writeProcSysFile(const char *filename, const char *value) { | 
|  | 151 | android::base::unique_fd fd(open(filename, O_WRONLY | O_CLOEXEC)); | 
|  | 152 | if (fd < 0) { | 
|  | 153 | const int err = errno; | 
|  | 154 | ALOGE("open('%s', O_WRONLY | O_CLOEXEC) -> %s", filename, strerror(err)); | 
|  | 155 | return -err; | 
|  | 156 | } | 
|  | 157 | int len = strlen(value); | 
|  | 158 | int v = write(fd, value, len); | 
|  | 159 | if (v < 0) { | 
|  | 160 | const int err = errno; | 
|  | 161 | ALOGE("write('%s', '%s', %d) -> %s", filename, value, len, strerror(err)); | 
|  | 162 | return -err; | 
|  | 163 | } | 
|  | 164 | if (v != len) { | 
|  | 165 | // In practice, due to us only using this for /proc/sys/... files, this can't happen. | 
|  | 166 | ALOGE("write('%s', '%s', %d) -> short write [%d]", filename, value, len, v); | 
|  | 167 | return -EINVAL; | 
|  | 168 | } | 
|  | 169 | return 0; | 
|  | 170 | } | 
|  | 171 |  | 
| Maciej Żenczykowski | b60599b | 2024-02-09 12:30:52 -0800 | [diff] [blame] | 172 | #define APEX_MOUNT_POINT "/apex/com.android.tethering" | 
| Maciej Żenczykowski | 2fe2db5 | 2024-02-07 01:23:58 +0000 | [diff] [blame] | 173 | const char * const platformBpfLoader = "/system/bin/bpfloader"; | 
|  | 174 | const char * const platformNetBpfLoad = "/system/bin/netbpfload"; | 
| Maciej Żenczykowski | b60599b | 2024-02-09 12:30:52 -0800 | [diff] [blame] | 175 | const char * const apexNetBpfLoad = APEX_MOUNT_POINT "/bin/netbpfload"; | 
|  | 176 |  | 
|  | 177 | int logTetheringApexVersion(void) { | 
|  | 178 | char * found_blockdev = NULL; | 
|  | 179 | FILE * f = NULL; | 
|  | 180 | char buf[4096]; | 
|  | 181 |  | 
|  | 182 | f = fopen("/proc/mounts", "re"); | 
|  | 183 | if (!f) return 1; | 
|  | 184 |  | 
|  | 185 | // /proc/mounts format: block_device [space] mount_point [space] other stuff... newline | 
|  | 186 | while (fgets(buf, sizeof(buf), f)) { | 
|  | 187 | char * blockdev = buf; | 
|  | 188 | char * space = strchr(blockdev, ' '); | 
|  | 189 | if (!space) continue; | 
|  | 190 | *space = '\0'; | 
|  | 191 | char * mntpath = space + 1; | 
|  | 192 | space = strchr(mntpath, ' '); | 
|  | 193 | if (!space) continue; | 
|  | 194 | *space = '\0'; | 
|  | 195 | if (strcmp(mntpath, APEX_MOUNT_POINT)) continue; | 
|  | 196 | found_blockdev = strdup(blockdev); | 
|  | 197 | break; | 
|  | 198 | } | 
|  | 199 | fclose(f); | 
|  | 200 | f = NULL; | 
|  | 201 |  | 
|  | 202 | if (!found_blockdev) return 2; | 
|  | 203 | ALOGD("Found Tethering Apex mounted from blockdev %s", found_blockdev); | 
|  | 204 |  | 
|  | 205 | f = fopen("/proc/mounts", "re"); | 
|  | 206 | if (!f) { free(found_blockdev); return 3; } | 
|  | 207 |  | 
|  | 208 | while (fgets(buf, sizeof(buf), f)) { | 
|  | 209 | char * blockdev = buf; | 
|  | 210 | char * space = strchr(blockdev, ' '); | 
|  | 211 | if (!space) continue; | 
|  | 212 | *space = '\0'; | 
|  | 213 | char * mntpath = space + 1; | 
|  | 214 | space = strchr(mntpath, ' '); | 
|  | 215 | if (!space) continue; | 
|  | 216 | *space = '\0'; | 
|  | 217 | if (strcmp(blockdev, found_blockdev)) continue; | 
|  | 218 | if (strncmp(mntpath, APEX_MOUNT_POINT "@", strlen(APEX_MOUNT_POINT "@"))) continue; | 
|  | 219 | char * at = strchr(mntpath, '@'); | 
|  | 220 | if (!at) continue; | 
|  | 221 | char * ver = at + 1; | 
|  | 222 | ALOGI("Tethering APEX version %s", ver); | 
|  | 223 | } | 
|  | 224 | fclose(f); | 
|  | 225 | free(found_blockdev); | 
|  | 226 | return 0; | 
|  | 227 | } | 
| Maciej Żenczykowski | 2fe2db5 | 2024-02-07 01:23:58 +0000 | [diff] [blame] | 228 |  | 
| Maciej Żenczykowski | 58c1822 | 2023-10-20 14:40:16 -0700 | [diff] [blame] | 229 | int main(int argc, char** argv, char * const envp[]) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 230 | (void)argc; | 
|  | 231 | android::base::InitLogging(argv, &android::base::KernelLogger); | 
|  | 232 |  | 
| Maciej Żenczykowski | 041be52 | 2023-10-23 23:34:52 -0700 | [diff] [blame] | 233 | ALOGI("NetBpfLoad '%s' starting...", argv[0]); | 
| Maciej Żenczykowski | f33f128 | 2023-10-24 04:41:54 -0700 | [diff] [blame] | 234 |  | 
| Maciej Żenczykowski | 041be52 | 2023-10-23 23:34:52 -0700 | [diff] [blame] | 235 | // true iff we are running from the module | 
| Maciej Żenczykowski | 2fe2db5 | 2024-02-07 01:23:58 +0000 | [diff] [blame] | 236 | const bool is_mainline = !strcmp(argv[0], apexNetBpfLoad); | 
| Maciej Żenczykowski | 041be52 | 2023-10-23 23:34:52 -0700 | [diff] [blame] | 237 |  | 
|  | 238 | // true iff we are running from the platform | 
| Maciej Żenczykowski | 2fe2db5 | 2024-02-07 01:23:58 +0000 | [diff] [blame] | 239 | const bool is_platform = !strcmp(argv[0], platformNetBpfLoad); | 
| Maciej Żenczykowski | 041be52 | 2023-10-23 23:34:52 -0700 | [diff] [blame] | 240 |  | 
|  | 241 | const int device_api_level = android_get_device_api_level(); | 
|  | 242 | const bool isAtLeastT = (device_api_level >= __ANDROID_API_T__); | 
|  | 243 | const bool isAtLeastU = (device_api_level >= __ANDROID_API_U__); | 
|  | 244 | const bool isAtLeastV = (device_api_level >= __ANDROID_API_V__); | 
|  | 245 |  | 
| Maciej Żenczykowski | 03ef12c | 2024-02-10 21:34:22 +0000 | [diff] [blame] | 246 | // last in U QPR2 beta1 | 
|  | 247 | const bool has_platform_bpfloader_rc = exists("/system/etc/init/bpfloader.rc"); | 
|  | 248 | // first in U QPR2 beta~2 | 
|  | 249 | const bool has_platform_netbpfload_rc = exists("/system/etc/init/netbpfload.rc"); | 
|  | 250 |  | 
|  | 251 | ALOGI("NetBpfLoad api:%d/%d kver:%07x platform:%d mainline:%d rc:%d%d", | 
| Maciej Żenczykowski | 041be52 | 2023-10-23 23:34:52 -0700 | [diff] [blame] | 252 | android_get_application_target_sdk_version(), device_api_level, | 
| Maciej Żenczykowski | 03ef12c | 2024-02-10 21:34:22 +0000 | [diff] [blame] | 253 | android::bpf::kernelVersion(), is_platform, is_mainline, | 
|  | 254 | has_platform_bpfloader_rc, has_platform_netbpfload_rc); | 
| Maciej Żenczykowski | 041be52 | 2023-10-23 23:34:52 -0700 | [diff] [blame] | 255 |  | 
|  | 256 | if (!is_platform && !is_mainline) { | 
|  | 257 | ALOGE("Unable to determine if we're platform or mainline netbpfload."); | 
|  | 258 | return 1; | 
|  | 259 | } | 
|  | 260 |  | 
| Maciej Żenczykowski | 2fe2db5 | 2024-02-07 01:23:58 +0000 | [diff] [blame] | 261 | if (is_platform) { | 
|  | 262 | const char * args[] = { apexNetBpfLoad, NULL, }; | 
|  | 263 | execve(args[0], (char**)args, envp); | 
|  | 264 | ALOGW("exec '%s' fail: %d[%s]", apexNetBpfLoad, errno, strerror(errno)); | 
|  | 265 | } | 
|  | 266 |  | 
| Maciej Żenczykowski | 03ef12c | 2024-02-10 21:34:22 +0000 | [diff] [blame] | 267 | if (!has_platform_bpfloader_rc && !has_platform_netbpfload_rc) { | 
|  | 268 | ALOGE("Unable to find platform's bpfloader & netbpfload init scripts."); | 
|  | 269 | return 1; | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | if (has_platform_bpfloader_rc && has_platform_netbpfload_rc) { | 
|  | 273 | ALOGE("Platform has *both* bpfloader & netbpfload init scripts."); | 
|  | 274 | return 1; | 
|  | 275 | } | 
|  | 276 |  | 
| Maciej Żenczykowski | b60599b | 2024-02-09 12:30:52 -0800 | [diff] [blame] | 277 | logTetheringApexVersion(); | 
|  | 278 |  | 
| Maciej Żenczykowski | 03ef12c | 2024-02-10 21:34:22 +0000 | [diff] [blame] | 279 | if (is_mainline && has_platform_bpfloader_rc && !has_platform_netbpfload_rc) { | 
|  | 280 | // Tethering apex shipped initrc file causes us to reach here | 
|  | 281 | // but we're not ready to correctly handle anything before U QPR2 | 
|  | 282 | // in which the 'bpfloader' vs 'netbpfload' split happened | 
|  | 283 | const char * args[] = { platformBpfLoader, NULL, }; | 
|  | 284 | execve(args[0], (char**)args, envp); | 
|  | 285 | ALOGE("exec '%s' fail: %d[%s]", platformBpfLoader, errno, strerror(errno)); | 
|  | 286 | return 1; | 
|  | 287 | } | 
|  | 288 |  | 
| Maciej Żenczykowski | 041be52 | 2023-10-23 23:34:52 -0700 | [diff] [blame] | 289 | if (isAtLeastT && !android::bpf::isAtLeastKernelVersion(4, 9, 0)) { | 
|  | 290 | ALOGE("Android T requires kernel 4.9."); | 
|  | 291 | return 1; | 
|  | 292 | } | 
|  | 293 |  | 
|  | 294 | if (isAtLeastU && !android::bpf::isAtLeastKernelVersion(4, 14, 0)) { | 
|  | 295 | ALOGE("Android U requires kernel 4.14."); | 
|  | 296 | return 1; | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | if (isAtLeastV && !android::bpf::isAtLeastKernelVersion(4, 19, 0)) { | 
|  | 300 | ALOGE("Android V requires kernel 4.19."); | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 301 | return 1; | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | if (android::bpf::isUserspace32bit() && android::bpf::isAtLeastKernelVersion(6, 2, 0)) { | 
|  | 305 | /* Android 14/U should only launch on 64-bit kernels | 
|  | 306 | *   T launches on 5.10/5.15 | 
|  | 307 | *   U launches on 5.15/6.1 | 
|  | 308 | * So >=5.16 implies isKernel64Bit() | 
|  | 309 | * | 
|  | 310 | * We thus added a test to V VTS which requires 5.16+ devices to use 64-bit kernels. | 
|  | 311 | * | 
|  | 312 | * Starting with Android V, which is the first to support a post 6.1 Linux Kernel, | 
|  | 313 | * we also require 64-bit userspace. | 
|  | 314 | * | 
|  | 315 | * There are various known issues with 32-bit userspace talking to various | 
|  | 316 | * kernel interfaces (especially CAP_NET_ADMIN ones) on a 64-bit kernel. | 
|  | 317 | * Some of these have userspace or kernel workarounds/hacks. | 
|  | 318 | * Some of them don't... | 
|  | 319 | * We're going to be removing the hacks. | 
|  | 320 | * | 
|  | 321 | * Additionally the 32-bit kernel jit support is poor, | 
|  | 322 | * and 32-bit userspace on 64-bit kernel bpf ringbuffer compatibility is broken. | 
|  | 323 | */ | 
|  | 324 | ALOGE("64-bit userspace required on 6.2+ kernels."); | 
|  | 325 | return 1; | 
|  | 326 | } | 
|  | 327 |  | 
|  | 328 | // Ensure we can determine the Android build type. | 
|  | 329 | if (!android::bpf::isEng() && !android::bpf::isUser() && !android::bpf::isUserdebug()) { | 
|  | 330 | ALOGE("Failed to determine the build type: got %s, want 'eng', 'user', or 'userdebug'", | 
|  | 331 | android::bpf::getBuildType().c_str()); | 
|  | 332 | return 1; | 
|  | 333 | } | 
|  | 334 |  | 
| Maciej Żenczykowski | f33f128 | 2023-10-24 04:41:54 -0700 | [diff] [blame] | 335 | if (isAtLeastU) { | 
|  | 336 | // Linux 5.16-rc1 changed the default to 2 (disabled but changeable), | 
|  | 337 | // but we need 0 (enabled) | 
|  | 338 | // (this writeFile is known to fail on at least 4.19, but always defaults to 0 on | 
|  | 339 | // pre-5.13, on 5.13+ it depends on CONFIG_BPF_UNPRIV_DEFAULT_OFF) | 
|  | 340 | if (writeProcSysFile("/proc/sys/kernel/unprivileged_bpf_disabled", "0\n") && | 
|  | 341 | android::bpf::isAtLeastKernelVersion(5, 13, 0)) return 1; | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 342 |  | 
| Maciej Żenczykowski | f33f128 | 2023-10-24 04:41:54 -0700 | [diff] [blame] | 343 | // Enable the eBPF JIT -- but do note that on 64-bit kernels it is likely | 
|  | 344 | // already force enabled by the kernel config option BPF_JIT_ALWAYS_ON. | 
|  | 345 | // (Note: this (open) will fail with ENOENT 'No such file or directory' if | 
|  | 346 | //  kernel does not have CONFIG_BPF_JIT=y) | 
|  | 347 | // BPF_JIT is required by R VINTF (which means 4.14/4.19/5.4 kernels), | 
|  | 348 | // but 4.14/4.19 were released with P & Q, and only 5.4 is new in R+. | 
|  | 349 | if (writeProcSysFile("/proc/sys/net/core/bpf_jit_enable", "1\n")) return 1; | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 350 |  | 
| Maciej Żenczykowski | f33f128 | 2023-10-24 04:41:54 -0700 | [diff] [blame] | 351 | // Enable JIT kallsyms export for privileged users only | 
|  | 352 | // (Note: this (open) will fail with ENOENT 'No such file or directory' if | 
|  | 353 | //  kernel does not have CONFIG_HAVE_EBPF_JIT=y) | 
|  | 354 | if (writeProcSysFile("/proc/sys/net/core/bpf_jit_kallsyms", "1\n")) return 1; | 
|  | 355 | } | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 356 |  | 
|  | 357 | // Create all the pin subdirectories | 
|  | 358 | // (this must be done first to allow selinux_context and pin_subdir functionality, | 
|  | 359 | //  which could otherwise fail with ENOENT during object pinning or renaming, | 
|  | 360 | //  due to ordering issues) | 
|  | 361 | for (const auto& location : locations) { | 
|  | 362 | if (createSysFsBpfSubDir(location.prefix)) return 1; | 
|  | 363 | } | 
|  | 364 |  | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 365 | // Load all ELF objects, create programs and maps, and pin them | 
|  | 366 | for (const auto& location : locations) { | 
|  | 367 | if (loadAllElfObjects(location) != 0) { | 
|  | 368 | ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS FROM %s ===", location.dir); | 
|  | 369 | ALOGE("If this triggers reliably, you're probably missing kernel options or patches."); | 
|  | 370 | ALOGE("If this triggers randomly, you might be hitting some memory allocation " | 
|  | 371 | "problems or startup script race."); | 
|  | 372 | ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---"); | 
|  | 373 | sleep(20); | 
|  | 374 | return 2; | 
|  | 375 | } | 
|  | 376 | } | 
|  | 377 |  | 
|  | 378 | int key = 1; | 
|  | 379 | int value = 123; | 
|  | 380 | android::base::unique_fd map( | 
|  | 381 | android::bpf::createMap(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value), 2, 0)); | 
|  | 382 | if (android::bpf::writeToMapEntry(map, &key, &value, BPF_ANY)) { | 
|  | 383 | ALOGE("Critical kernel bug - failure to write into index 1 of 2 element bpf map array."); | 
|  | 384 | return 1; | 
|  | 385 | } | 
|  | 386 |  | 
| Maciej Żenczykowski | 58c1822 | 2023-10-20 14:40:16 -0700 | [diff] [blame] | 387 | ALOGI("done, transferring control to platform bpfloader."); | 
|  | 388 |  | 
| Maciej Żenczykowski | 2fe2db5 | 2024-02-07 01:23:58 +0000 | [diff] [blame] | 389 | const char * args[] = { platformBpfLoader, NULL, }; | 
|  | 390 | execve(args[0], (char**)args, envp); | 
|  | 391 | ALOGE("FATAL: execve('%s'): %d[%s]", platformBpfLoader, errno, strerror(errno)); | 
| Maciej Żenczykowski | 58c1822 | 2023-10-20 14:40:16 -0700 | [diff] [blame] | 392 | return 1; | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 393 | } |