Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #ifndef LOG_TAG |
| 18 | #define LOG_TAG "bpfloader" |
| 19 | #endif |
| 20 | |
| 21 | #include <arpa/inet.h> |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 22 | #include <dirent.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 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 | |
Steven Moreland | a48639e | 2022-02-07 23:15:48 +0000 | [diff] [blame] | 41 | #include <android-base/logging.h> |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 42 | #include <android-base/macros.h> |
Joel Fernandes | d3ec871 | 2019-01-11 06:22:05 -0500 | [diff] [blame] | 43 | #include <android-base/properties.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 44 | #include <android-base/stringprintf.h> |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 45 | #include <android-base/strings.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 46 | #include <android-base/unique_fd.h> |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 47 | #include <libbpf_android.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 48 | #include <log/log.h> |
Maciej Żenczykowski | 4e4dea0 | 2022-12-10 17:58:59 +0000 | [diff] [blame] | 49 | #include "BpfSyscallWrappers.h" |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 50 | #include "bpf/BpfUtils.h" |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 51 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 52 | using android::base::EndsWith; |
Maciej Żenczykowski | 4181713 | 2022-06-03 08:41:04 -0700 | [diff] [blame] | 53 | using android::bpf::domain; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 54 | using std::string; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 55 | |
Maciej Żenczykowski | 057ef34 | 2022-07-18 03:04:37 -0700 | [diff] [blame] | 56 | bool exists(const char* const path) { |
| 57 | int v = access(path, F_OK); |
| 58 | if (!v) { |
| 59 | ALOGI("%s exists.", path); |
| 60 | return true; |
| 61 | } |
| 62 | if (errno == ENOENT) return false; |
| 63 | ALOGE("FATAL: access(%s, F_OK) -> %d [%d:%s]", path, v, errno, strerror(errno)); |
| 64 | abort(); // can only hit this if permissions (likely selinux) are screwed up |
| 65 | } |
| 66 | |
Maciej Żenczykowski | ae58e7e | 2022-07-01 10:01:44 -0700 | [diff] [blame] | 67 | // Networking-related program types are limited to the Tethering Apex |
| 68 | // to prevent things from breaking due to conflicts on mainline updates |
| 69 | // (exception made for socket filters, ie. xt_bpf for potential use in iptables, |
| 70 | // or for attaching to sockets directly) |
| 71 | constexpr bpf_prog_type kPlatformAllowedProgTypes[] = { |
| 72 | BPF_PROG_TYPE_KPROBE, |
| 73 | BPF_PROG_TYPE_PERF_EVENT, |
| 74 | BPF_PROG_TYPE_SOCKET_FILTER, |
| 75 | BPF_PROG_TYPE_TRACEPOINT, |
Paul Lawrence | 7fb8b54 | 2022-07-19 16:13:49 -0700 | [diff] [blame] | 76 | BPF_PROG_TYPE_UNSPEC, // Will be replaced with fuse bpf program type |
Maciej Żenczykowski | ae58e7e | 2022-07-01 10:01:44 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Yu-Ting Tseng | 9c56a5a | 2024-01-11 20:43:05 -0800 | [diff] [blame] | 79 | constexpr bpf_prog_type kUprobestatsAllowedProgTypes[] = { |
| 80 | BPF_PROG_TYPE_KPROBE, |
| 81 | }; |
| 82 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 83 | // see b/162057235. For arbitrary program types, the concern is that due to the lack of |
| 84 | // SELinux access controls over BPF program attachpoints, we have no way to control the |
| 85 | // attachment of programs to shared resources (or to detect when a shared resource |
| 86 | // has one BPF program replace another that is attached there) |
| 87 | constexpr bpf_prog_type kVendorAllowedProgTypes[] = { |
Stephane Lee | 16c9360 | 2022-03-08 17:27:09 -0800 | [diff] [blame] | 88 | BPF_PROG_TYPE_SOCKET_FILTER, |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
Connor O'Brien | 6c0ce9f | 2022-12-01 20:08:17 -0800 | [diff] [blame] | 91 | const android::bpf::Location locations[] = { |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 92 | // Core operating system |
| 93 | { |
| 94 | .dir = "/system/etc/bpf/", |
| 95 | .prefix = "", |
Maciej Żenczykowski | 4181713 | 2022-06-03 08:41:04 -0700 | [diff] [blame] | 96 | .allowedDomainBitmask = domainToBitmask(domain::platform), |
Maciej Żenczykowski | ae58e7e | 2022-07-01 10:01:44 -0700 | [diff] [blame] | 97 | .allowedProgTypes = kPlatformAllowedProgTypes, |
| 98 | .allowedProgTypesLength = arraysize(kPlatformAllowedProgTypes), |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 99 | }, |
Yu-Ting Tseng | 9c56a5a | 2024-01-11 20:43:05 -0800 | [diff] [blame] | 100 | // uprobestats |
| 101 | { |
| 102 | .dir = "/system/etc/bpf/uprobestats/", |
| 103 | .prefix = "uprobestats/", |
| 104 | .allowedDomainBitmask = domainToBitmask(domain::platform), |
| 105 | .allowedProgTypes = kUprobestatsAllowedProgTypes, |
| 106 | .allowedProgTypesLength = arraysize(kUprobestatsAllowedProgTypes), |
| 107 | }, |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 108 | // Vendor operating system |
| 109 | { |
| 110 | .dir = "/vendor/etc/bpf/", |
| 111 | .prefix = "vendor/", |
Maciej Żenczykowski | 4181713 | 2022-06-03 08:41:04 -0700 | [diff] [blame] | 112 | .allowedDomainBitmask = domainToBitmask(domain::vendor), |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 113 | .allowedProgTypes = kVendorAllowedProgTypes, |
| 114 | .allowedProgTypesLength = arraysize(kVendorAllowedProgTypes), |
| 115 | }, |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 116 | }; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 117 | |
Connor O'Brien | 6c0ce9f | 2022-12-01 20:08:17 -0800 | [diff] [blame] | 118 | int loadAllElfObjects(const android::bpf::Location& location) { |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 119 | int retVal = 0; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 120 | DIR* dir; |
| 121 | struct dirent* ent; |
| 122 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 123 | if ((dir = opendir(location.dir)) != NULL) { |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 124 | while ((ent = readdir(dir)) != NULL) { |
| 125 | string s = ent->d_name; |
| 126 | if (!EndsWith(s, ".o")) continue; |
| 127 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 128 | string progPath(location.dir); |
Hungming Chen | 4b8e982 | 2020-09-10 15:51:59 +0800 | [diff] [blame] | 129 | progPath += s; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 130 | |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 131 | bool critical; |
Connor O'Brien | 6c0ce9f | 2022-12-01 20:08:17 -0800 | [diff] [blame] | 132 | int ret = android::bpf::loadProg(progPath.c_str(), &critical, location); |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 133 | if (ret) { |
| 134 | if (critical) retVal = ret; |
| 135 | ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret)); |
| 136 | } else { |
| 137 | ALOGI("Loaded object: %s", progPath.c_str()); |
| 138 | } |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 139 | } |
| 140 | closedir(dir); |
| 141 | } |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 142 | return retVal; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Maciej Żenczykowski | ebfacde | 2022-12-05 13:33:43 +0000 | [diff] [blame] | 145 | int createSysFsBpfSubDir(const char* const prefix) { |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 146 | if (*prefix) { |
| 147 | mode_t prevUmask = umask(0); |
| 148 | |
| 149 | string s = "/sys/fs/bpf/"; |
| 150 | s += prefix; |
| 151 | |
| 152 | errno = 0; |
| 153 | int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO); |
| 154 | if (ret && errno != EEXIST) { |
Maciej Żenczykowski | ebfacde | 2022-12-05 13:33:43 +0000 | [diff] [blame] | 155 | const int err = errno; |
| 156 | ALOGE("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(err)); |
| 157 | return -err; |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | umask(prevUmask); |
| 161 | } |
Maciej Żenczykowski | ebfacde | 2022-12-05 13:33:43 +0000 | [diff] [blame] | 162 | return 0; |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Maciej Żenczykowski | 03b6caa | 2022-12-03 09:38:05 +0000 | [diff] [blame] | 165 | // Technically 'value' doesn't need to be newline terminated, but it's best |
| 166 | // to include a newline to match 'echo "value" > /proc/sys/...foo' behaviour, |
| 167 | // which is usually how kernel devs test the actual sysctl interfaces. |
| 168 | int writeProcSysFile(const char *filename, const char *value) { |
| 169 | android::base::unique_fd fd(open(filename, O_WRONLY | O_CLOEXEC)); |
| 170 | if (fd < 0) { |
| 171 | const int err = errno; |
| 172 | ALOGE("open('%s', O_WRONLY | O_CLOEXEC) -> %s", filename, strerror(err)); |
| 173 | return -err; |
| 174 | } |
| 175 | int len = strlen(value); |
| 176 | int v = write(fd, value, len); |
| 177 | if (v < 0) { |
| 178 | const int err = errno; |
| 179 | ALOGE("write('%s', '%s', %d) -> %s", filename, value, len, strerror(err)); |
| 180 | return -err; |
| 181 | } |
| 182 | if (v != len) { |
| 183 | // In practice, due to us only using this for /proc/sys/... files, this can't happen. |
| 184 | ALOGE("write('%s', '%s', %d) -> short write [%d]", filename, value, len, v); |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | return 0; |
| 188 | } |
| 189 | |
Steven Moreland | a48639e | 2022-02-07 23:15:48 +0000 | [diff] [blame] | 190 | int main(int argc, char** argv) { |
| 191 | (void)argc; |
| 192 | android::base::InitLogging(argv, &android::base::KernelLogger); |
| 193 | |
Maciej Żenczykowski | 03b6caa | 2022-12-03 09:38:05 +0000 | [diff] [blame] | 194 | // Enable the eBPF JIT -- but do note that on 64-bit kernels it is likely |
Maciej Żenczykowski | 8aa34a7 | 2022-12-13 12:37:03 +0000 | [diff] [blame] | 195 | // already force enabled by the kernel config option BPF_JIT_ALWAYS_ON. |
Maciej Żenczykowski | 052cda1 | 2022-12-12 00:15:53 +0000 | [diff] [blame] | 196 | // (Note: this (open) will fail with ENOENT 'No such file or directory' if |
| 197 | // kernel does not have CONFIG_BPF_JIT=y) |
Maciej Żenczykowski | 8aa34a7 | 2022-12-13 12:37:03 +0000 | [diff] [blame] | 198 | // BPF_JIT is required by R VINTF (which means 4.14/4.19/5.4 kernels), |
| 199 | // but 4.14/4.19 were released with P & Q, and only 5.4 is new in R+. |
Michael Bestas | 30b81fc | 2024-03-11 01:48:26 +0200 | [diff] [blame] | 200 | if (writeProcSysFile("/proc/sys/net/core/bpf_jit_enable", "1\n") && |
| 201 | android::bpf::isAtLeastKernelVersion(4, 14, 0)) return 1; |
Maciej Żenczykowski | 03b6caa | 2022-12-03 09:38:05 +0000 | [diff] [blame] | 202 | |
| 203 | // Enable JIT kallsyms export for privileged users only |
Maciej Żenczykowski | 052cda1 | 2022-12-12 00:15:53 +0000 | [diff] [blame] | 204 | // (Note: this (open) will fail with ENOENT 'No such file or directory' if |
| 205 | // kernel does not have CONFIG_HAVE_EBPF_JIT=y) |
Michael Bestas | 30b81fc | 2024-03-11 01:48:26 +0200 | [diff] [blame] | 206 | if (writeProcSysFile("/proc/sys/net/core/bpf_jit_kallsyms", "1\n") && |
| 207 | android::bpf::isAtLeastKernelVersion(4, 14, 0)) return 1; |
Maciej Żenczykowski | 03b6caa | 2022-12-03 09:38:05 +0000 | [diff] [blame] | 208 | |
Maciej Żenczykowski | 4181713 | 2022-06-03 08:41:04 -0700 | [diff] [blame] | 209 | // Create all the pin subdirectories |
| 210 | // (this must be done first to allow selinux_context and pin_subdir functionality, |
| 211 | // which could otherwise fail with ENOENT during object pinning or renaming, |
| 212 | // due to ordering issues) |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 213 | for (const auto& location : locations) { |
Maciej Żenczykowski | ebfacde | 2022-12-05 13:33:43 +0000 | [diff] [blame] | 214 | if (createSysFsBpfSubDir(location.prefix)) return 1; |
Maciej Żenczykowski | 4181713 | 2022-06-03 08:41:04 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // Load all ELF objects, create programs and maps, and pin them |
| 218 | for (const auto& location : locations) { |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 219 | if (loadAllElfObjects(location) != 0) { |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 220 | ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS FROM %s ===", location.dir); |
Hungming Chen | 4b8e982 | 2020-09-10 15:51:59 +0800 | [diff] [blame] | 221 | ALOGE("If this triggers reliably, you're probably missing kernel options or patches."); |
| 222 | ALOGE("If this triggers randomly, you might be hitting some memory allocation " |
| 223 | "problems or startup script race."); |
| 224 | ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---"); |
| 225 | sleep(20); |
| 226 | return 2; |
| 227 | } |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 228 | } |
Joel Fernandes | d3ec871 | 2019-01-11 06:22:05 -0500 | [diff] [blame] | 229 | |
Maciej Żenczykowski | ab8fd4c | 2023-10-10 16:20:43 -0700 | [diff] [blame] | 230 | if (android::base::SetProperty("bpf.progs_loaded", "1") == false) { |
| 231 | ALOGE("Failed to set bpf.progs_loaded property"); |
| 232 | return 1; |
| 233 | } |
| 234 | |
Joel Fernandes | d3ec871 | 2019-01-11 06:22:05 -0500 | [diff] [blame] | 235 | return 0; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 236 | } |