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[] = { |
Maciej Żenczykowski | 32c0b8f | 2022-06-16 18:58:22 -0700 | [diff] [blame] | 72 | // S+ Tethering mainline module (network_stack): 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 | 32c0b8f | 2022-06-16 18:58:22 -0700 | [diff] [blame] | 77 | // T+ Tethering mainline module (shared with netd & system server) |
| 78 | // netutils_wrapper (for iptables xt_bpf) has access to programs |
Maciej Żenczykowski | 7acf938 | 2022-05-12 08:47:05 +0000 | [diff] [blame] | 79 | { |
| 80 | .dir = "/apex/com.android.tethering/etc/bpf/netd_shared/", |
| 81 | .prefix = "netd_shared/", |
| 82 | }, |
Maciej Żenczykowski | 32c0b8f | 2022-06-16 18:58:22 -0700 | [diff] [blame] | 83 | // T+ Tethering mainline module (shared with netd & system server) |
| 84 | // netutils_wrapper has no access, netd has read only access |
| 85 | { |
| 86 | .dir = "/apex/com.android.tethering/etc/bpf/netd_readonly/", |
| 87 | .prefix = "netd_readonly/", |
| 88 | }, |
| 89 | // T+ Tethering mainline module (shared with system server) |
Ken Chen | 6d69784 | 2022-01-17 17:22:34 +0800 | [diff] [blame] | 90 | { |
| 91 | .dir = "/apex/com.android.tethering/etc/bpf/net_shared/", |
Maciej Żenczykowski | 2a36ce4 | 2022-04-21 06:31:33 -0700 | [diff] [blame] | 92 | .prefix = "net_shared/", |
Ken Chen | 6d69784 | 2022-01-17 17:22:34 +0800 | [diff] [blame] | 93 | }, |
Maciej Żenczykowski | 32c0b8f | 2022-06-16 18:58:22 -0700 | [diff] [blame] | 94 | // T+ Tethering mainline module (not shared, just network_stack) |
Maciej Żenczykowski | 7acf938 | 2022-05-12 08:47:05 +0000 | [diff] [blame] | 95 | { |
| 96 | .dir = "/apex/com.android.tethering/etc/bpf/net_private/", |
| 97 | .prefix = "net_private/", |
| 98 | }, |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 99 | // Core operating system |
| 100 | { |
| 101 | .dir = "/system/etc/bpf/", |
| 102 | .prefix = "", |
| 103 | }, |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 104 | // Vendor operating system |
| 105 | { |
| 106 | .dir = "/vendor/etc/bpf/", |
| 107 | .prefix = "vendor/", |
| 108 | .allowedProgTypes = kVendorAllowedProgTypes, |
| 109 | .allowedProgTypesLength = arraysize(kVendorAllowedProgTypes), |
| 110 | }, |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 111 | }; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 112 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 113 | int loadAllElfObjects(const Location& location) { |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 114 | int retVal = 0; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 115 | DIR* dir; |
| 116 | struct dirent* ent; |
| 117 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 118 | if ((dir = opendir(location.dir)) != NULL) { |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 119 | while ((ent = readdir(dir)) != NULL) { |
| 120 | string s = ent->d_name; |
| 121 | if (!EndsWith(s, ".o")) continue; |
| 122 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 123 | string progPath(location.dir); |
Hungming Chen | 4b8e982 | 2020-09-10 15:51:59 +0800 | [diff] [blame] | 124 | progPath += s; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 125 | |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 126 | bool critical; |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 127 | int ret = android::bpf::loadProg(progPath.c_str(), &critical, location.prefix, |
| 128 | location.allowedProgTypes, |
| 129 | location.allowedProgTypesLength); |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 130 | if (ret) { |
| 131 | if (critical) retVal = ret; |
| 132 | ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret)); |
| 133 | } else { |
| 134 | ALOGI("Loaded object: %s", progPath.c_str()); |
| 135 | } |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 136 | } |
| 137 | closedir(dir); |
| 138 | } |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 139 | return retVal; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 142 | void createSysFsBpfSubDir(const char* const prefix) { |
| 143 | if (*prefix) { |
| 144 | mode_t prevUmask = umask(0); |
| 145 | |
| 146 | string s = "/sys/fs/bpf/"; |
| 147 | s += prefix; |
| 148 | |
| 149 | errno = 0; |
| 150 | int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO); |
| 151 | if (ret && errno != EEXIST) { |
| 152 | ALOGW("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(errno)); |
| 153 | } |
| 154 | |
| 155 | umask(prevUmask); |
| 156 | } |
| 157 | } |
| 158 | |
Steven Moreland | a48639e | 2022-02-07 23:15:48 +0000 | [diff] [blame] | 159 | int main(int argc, char** argv) { |
| 160 | (void)argc; |
| 161 | android::base::InitLogging(argv, &android::base::KernelLogger); |
| 162 | |
Maciej Żenczykowski | 567dc56 | 2020-06-10 15:56:07 -0700 | [diff] [blame] | 163 | // Load all ELF objects, create programs and maps, and pin them |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 164 | for (const auto& location : locations) { |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 165 | createSysFsBpfSubDir(location.prefix); |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 166 | if (loadAllElfObjects(location) != 0) { |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 167 | ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS FROM %s ===", location.dir); |
Hungming Chen | 4b8e982 | 2020-09-10 15:51:59 +0800 | [diff] [blame] | 168 | ALOGE("If this triggers reliably, you're probably missing kernel options or patches."); |
| 169 | ALOGE("If this triggers randomly, you might be hitting some memory allocation " |
| 170 | "problems or startup script race."); |
| 171 | ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---"); |
| 172 | sleep(20); |
| 173 | return 2; |
| 174 | } |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 175 | } |
Joel Fernandes | d3ec871 | 2019-01-11 06:22:05 -0500 | [diff] [blame] | 176 | |
| 177 | if (android::base::SetProperty("bpf.progs_loaded", "1") == false) { |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 178 | ALOGE("Failed to set bpf.progs_loaded property"); |
Joel Fernandes | d3ec871 | 2019-01-11 06:22:05 -0500 | [diff] [blame] | 179 | return 1; |
| 180 | } |
| 181 | |
| 182 | return 0; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 183 | } |