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> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 49 | #include <netdutils/Misc.h> |
| 50 | #include <netdutils/Slice.h> |
| 51 | #include "bpf/BpfUtils.h" |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 52 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 53 | using android::base::EndsWith; |
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 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 56 | // see b/162057235. For arbitrary program types, the concern is that due to the lack of |
| 57 | // SELinux access controls over BPF program attachpoints, we have no way to control the |
| 58 | // attachment of programs to shared resources (or to detect when a shared resource |
| 59 | // has one BPF program replace another that is attached there) |
| 60 | constexpr bpf_prog_type kVendorAllowedProgTypes[] = { |
Stephane Lee | 16c9360 | 2022-03-08 17:27:09 -0800 | [diff] [blame] | 61 | BPF_PROG_TYPE_SOCKET_FILTER, |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | struct Location { |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 65 | const char* const dir; |
| 66 | const char* const prefix; |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 67 | const bpf_prog_type* allowedProgTypes = nullptr; |
| 68 | size_t allowedProgTypesLength = 0; |
| 69 | }; |
| 70 | |
| 71 | const Location locations[] = { |
Ken Chen | 6d69784 | 2022-01-17 17:22:34 +0800 | [diff] [blame] | 72 | // Tethering mainline module: tether offload |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 73 | { |
| 74 | .dir = "/apex/com.android.tethering/etc/bpf/", |
| 75 | .prefix = "tethering/", |
| 76 | }, |
Maciej Żenczykowski | 7acf938 | 2022-05-12 08:47:05 +0000 | [diff] [blame] | 77 | // Tethering mainline module (shared with netd & system server) |
| 78 | { |
| 79 | .dir = "/apex/com.android.tethering/etc/bpf/netd_shared/", |
| 80 | .prefix = "netd_shared/", |
| 81 | }, |
| 82 | // Tethering mainline module (shared with system server) |
Ken Chen | 6d69784 | 2022-01-17 17:22:34 +0800 | [diff] [blame] | 83 | { |
| 84 | .dir = "/apex/com.android.tethering/etc/bpf/net_shared/", |
Maciej Żenczykowski | 2a36ce4 | 2022-04-21 06:31:33 -0700 | [diff] [blame] | 85 | .prefix = "net_shared/", |
Ken Chen | 6d69784 | 2022-01-17 17:22:34 +0800 | [diff] [blame] | 86 | }, |
Maciej Żenczykowski | 7acf938 | 2022-05-12 08:47:05 +0000 | [diff] [blame] | 87 | // Tethering mainline module (not shared) |
| 88 | { |
| 89 | .dir = "/apex/com.android.tethering/etc/bpf/net_private/", |
| 90 | .prefix = "net_private/", |
| 91 | }, |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 92 | // Core operating system |
| 93 | { |
| 94 | .dir = "/system/etc/bpf/", |
| 95 | .prefix = "", |
| 96 | }, |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 97 | // Vendor operating system |
| 98 | { |
| 99 | .dir = "/vendor/etc/bpf/", |
| 100 | .prefix = "vendor/", |
| 101 | .allowedProgTypes = kVendorAllowedProgTypes, |
| 102 | .allowedProgTypesLength = arraysize(kVendorAllowedProgTypes), |
| 103 | }, |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 104 | }; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 105 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 106 | int loadAllElfObjects(const Location& location) { |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 107 | int retVal = 0; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 108 | DIR* dir; |
| 109 | struct dirent* ent; |
| 110 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 111 | if ((dir = opendir(location.dir)) != NULL) { |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 112 | while ((ent = readdir(dir)) != NULL) { |
| 113 | string s = ent->d_name; |
| 114 | if (!EndsWith(s, ".o")) continue; |
| 115 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 116 | string progPath(location.dir); |
Hungming Chen | 4b8e982 | 2020-09-10 15:51:59 +0800 | [diff] [blame] | 117 | progPath += s; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 118 | |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 119 | bool critical; |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 120 | int ret = android::bpf::loadProg(progPath.c_str(), &critical, location.prefix, |
| 121 | location.allowedProgTypes, |
| 122 | location.allowedProgTypesLength); |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 123 | if (ret) { |
| 124 | if (critical) retVal = ret; |
| 125 | ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret)); |
| 126 | } else { |
| 127 | ALOGI("Loaded object: %s", progPath.c_str()); |
| 128 | } |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 129 | } |
| 130 | closedir(dir); |
| 131 | } |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 132 | return retVal; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 135 | void createSysFsBpfSubDir(const char* const prefix) { |
| 136 | if (*prefix) { |
| 137 | mode_t prevUmask = umask(0); |
| 138 | |
| 139 | string s = "/sys/fs/bpf/"; |
| 140 | s += prefix; |
| 141 | |
| 142 | errno = 0; |
| 143 | int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO); |
| 144 | if (ret && errno != EEXIST) { |
| 145 | ALOGW("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(errno)); |
| 146 | } |
| 147 | |
| 148 | umask(prevUmask); |
| 149 | } |
| 150 | } |
| 151 | |
Steven Moreland | a48639e | 2022-02-07 23:15:48 +0000 | [diff] [blame] | 152 | int main(int argc, char** argv) { |
| 153 | (void)argc; |
| 154 | android::base::InitLogging(argv, &android::base::KernelLogger); |
| 155 | |
Maciej Żenczykowski | 567dc56 | 2020-06-10 15:56:07 -0700 | [diff] [blame] | 156 | // Load all ELF objects, create programs and maps, and pin them |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 157 | for (const auto& location : locations) { |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 158 | createSysFsBpfSubDir(location.prefix); |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 159 | if (loadAllElfObjects(location) != 0) { |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 160 | ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS FROM %s ===", location.dir); |
Hungming Chen | 4b8e982 | 2020-09-10 15:51:59 +0800 | [diff] [blame] | 161 | ALOGE("If this triggers reliably, you're probably missing kernel options or patches."); |
| 162 | ALOGE("If this triggers randomly, you might be hitting some memory allocation " |
| 163 | "problems or startup script race."); |
| 164 | ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---"); |
| 165 | sleep(20); |
| 166 | return 2; |
| 167 | } |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 168 | } |
Joel Fernandes | d3ec871 | 2019-01-11 06:22:05 -0500 | [diff] [blame] | 169 | |
| 170 | if (android::base::SetProperty("bpf.progs_loaded", "1") == false) { |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 171 | ALOGE("Failed to set bpf.progs_loaded property"); |
Joel Fernandes | d3ec871 | 2019-01-11 06:22:05 -0500 | [diff] [blame] | 172 | return 1; |
| 173 | } |
| 174 | |
| 175 | return 0; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 176 | } |