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