blob: bc72811ddf12ced0f6b1442d03a22dc4db88805a [file] [log] [blame]
Chenbo Feng75b410b2018-10-10 15:01:19 -07001/*
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 Fernandesd76a2002018-10-16 13:19:58 -070022#include <dirent.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070023#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 Morelanda48639e2022-02-07 23:15:48 +000041#include <android-base/logging.h>
Steven Moreland0f10f3f2019-12-12 14:22:34 -080042#include <android-base/macros.h>
Joel Fernandesd3ec8712019-01-11 06:22:05 -050043#include <android-base/properties.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070044#include <android-base/stringprintf.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070045#include <android-base/strings.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070046#include <android-base/unique_fd.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070047#include <libbpf_android.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070048#include <log/log.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070049#include <netdutils/Misc.h>
50#include <netdutils/Slice.h>
51#include "bpf/BpfUtils.h"
Chenbo Feng75b410b2018-10-10 15:01:19 -070052
Joel Fernandesd76a2002018-10-16 13:19:58 -070053using android::base::EndsWith;
Maciej Żenczykowski41817132022-06-03 08:41:04 -070054using android::bpf::domain;
Joel Fernandesd76a2002018-10-16 13:19:58 -070055using std::string;
Chenbo Feng75b410b2018-10-10 15:01:19 -070056
Maciej Żenczykowski41817132022-06-03 08:41:04 -070057constexpr unsigned long long kTetheringApexDomainBitmask =
58 domainToBitmask(domain::tethering) |
59 domainToBitmask(domain::net_private) |
60 domainToBitmask(domain::net_shared) |
61 domainToBitmask(domain::netd_readonly) |
62 domainToBitmask(domain::netd_shared);
63
Steven Moreland0f10f3f2019-12-12 14:22:34 -080064// see b/162057235. For arbitrary program types, the concern is that due to the lack of
65// SELinux access controls over BPF program attachpoints, we have no way to control the
66// attachment of programs to shared resources (or to detect when a shared resource
67// has one BPF program replace another that is attached there)
68constexpr bpf_prog_type kVendorAllowedProgTypes[] = {
Stephane Lee16c93602022-03-08 17:27:09 -080069 BPF_PROG_TYPE_SOCKET_FILTER,
Steven Moreland0f10f3f2019-12-12 14:22:34 -080070};
71
72struct Location {
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -080073 const char* const dir;
74 const char* const prefix;
Maciej Żenczykowski41817132022-06-03 08:41:04 -070075 unsigned long long allowedDomainBitmask;
Steven Moreland0f10f3f2019-12-12 14:22:34 -080076 const bpf_prog_type* allowedProgTypes = nullptr;
77 size_t allowedProgTypesLength = 0;
78};
79
80const Location locations[] = {
Maciej Żenczykowski32c0b8f2022-06-16 18:58:22 -070081 // S+ Tethering mainline module (network_stack): tether offload
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -080082 {
83 .dir = "/apex/com.android.tethering/etc/bpf/",
84 .prefix = "tethering/",
Maciej Żenczykowski41817132022-06-03 08:41:04 -070085 .allowedDomainBitmask = kTetheringApexDomainBitmask,
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -080086 },
Maciej Żenczykowski32c0b8f2022-06-16 18:58:22 -070087 // T+ Tethering mainline module (shared with netd & system server)
88 // netutils_wrapper (for iptables xt_bpf) has access to programs
Maciej Żenczykowski7acf9382022-05-12 08:47:05 +000089 {
90 .dir = "/apex/com.android.tethering/etc/bpf/netd_shared/",
91 .prefix = "netd_shared/",
Maciej Żenczykowski41817132022-06-03 08:41:04 -070092 .allowedDomainBitmask = kTetheringApexDomainBitmask,
Maciej Żenczykowski7acf9382022-05-12 08:47:05 +000093 },
Maciej Żenczykowski32c0b8f2022-06-16 18:58:22 -070094 // T+ Tethering mainline module (shared with netd & system server)
95 // netutils_wrapper has no access, netd has read only access
96 {
97 .dir = "/apex/com.android.tethering/etc/bpf/netd_readonly/",
98 .prefix = "netd_readonly/",
Maciej Żenczykowski41817132022-06-03 08:41:04 -070099 .allowedDomainBitmask = kTetheringApexDomainBitmask,
Maciej Żenczykowski32c0b8f2022-06-16 18:58:22 -0700100 },
101 // T+ Tethering mainline module (shared with system server)
Ken Chen6d697842022-01-17 17:22:34 +0800102 {
103 .dir = "/apex/com.android.tethering/etc/bpf/net_shared/",
Maciej Żenczykowski2a36ce42022-04-21 06:31:33 -0700104 .prefix = "net_shared/",
Maciej Żenczykowski41817132022-06-03 08:41:04 -0700105 .allowedDomainBitmask = kTetheringApexDomainBitmask,
Ken Chen6d697842022-01-17 17:22:34 +0800106 },
Maciej Żenczykowski32c0b8f2022-06-16 18:58:22 -0700107 // T+ Tethering mainline module (not shared, just network_stack)
Maciej Żenczykowski7acf9382022-05-12 08:47:05 +0000108 {
109 .dir = "/apex/com.android.tethering/etc/bpf/net_private/",
110 .prefix = "net_private/",
Maciej Żenczykowski41817132022-06-03 08:41:04 -0700111 .allowedDomainBitmask = kTetheringApexDomainBitmask,
Maciej Żenczykowski7acf9382022-05-12 08:47:05 +0000112 },
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800113 // Core operating system
114 {
115 .dir = "/system/etc/bpf/",
116 .prefix = "",
Maciej Żenczykowski41817132022-06-03 08:41:04 -0700117 .allowedDomainBitmask = domainToBitmask(domain::platform),
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800118 },
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800119 // Vendor operating system
120 {
121 .dir = "/vendor/etc/bpf/",
122 .prefix = "vendor/",
Maciej Żenczykowski41817132022-06-03 08:41:04 -0700123 .allowedDomainBitmask = domainToBitmask(domain::vendor),
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800124 .allowedProgTypes = kVendorAllowedProgTypes,
125 .allowedProgTypesLength = arraysize(kVendorAllowedProgTypes),
126 },
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800127};
Chenbo Feng75b410b2018-10-10 15:01:19 -0700128
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800129int loadAllElfObjects(const Location& location) {
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700130 int retVal = 0;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700131 DIR* dir;
132 struct dirent* ent;
133
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800134 if ((dir = opendir(location.dir)) != NULL) {
Joel Fernandesd76a2002018-10-16 13:19:58 -0700135 while ((ent = readdir(dir)) != NULL) {
136 string s = ent->d_name;
137 if (!EndsWith(s, ".o")) continue;
138
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800139 string progPath(location.dir);
Hungming Chen4b8e9822020-09-10 15:51:59 +0800140 progPath += s;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700141
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700142 bool critical;
Maciej Żenczykowski41817132022-06-03 08:41:04 -0700143 int ret = android::bpf::loadProg(progPath.c_str(), &critical,
144 location.prefix,
145 location.allowedDomainBitmask,
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800146 location.allowedProgTypes,
147 location.allowedProgTypesLength);
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700148 if (ret) {
149 if (critical) retVal = ret;
150 ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret));
151 } else {
152 ALOGI("Loaded object: %s", progPath.c_str());
153 }
Joel Fernandesd76a2002018-10-16 13:19:58 -0700154 }
155 closedir(dir);
156 }
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700157 return retVal;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700158}
159
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800160void createSysFsBpfSubDir(const char* const prefix) {
161 if (*prefix) {
162 mode_t prevUmask = umask(0);
163
164 string s = "/sys/fs/bpf/";
165 s += prefix;
166
167 errno = 0;
168 int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
169 if (ret && errno != EEXIST) {
170 ALOGW("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(errno));
171 }
172
173 umask(prevUmask);
174 }
175}
176
Steven Morelanda48639e2022-02-07 23:15:48 +0000177int main(int argc, char** argv) {
178 (void)argc;
179 android::base::InitLogging(argv, &android::base::KernelLogger);
180
Maciej Żenczykowski41817132022-06-03 08:41:04 -0700181 // Create all the pin subdirectories
182 // (this must be done first to allow selinux_context and pin_subdir functionality,
183 // which could otherwise fail with ENOENT during object pinning or renaming,
184 // due to ordering issues)
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800185 for (const auto& location : locations) {
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800186 createSysFsBpfSubDir(location.prefix);
Maciej Żenczykowski41817132022-06-03 08:41:04 -0700187 }
188
189 // Load all ELF objects, create programs and maps, and pin them
190 for (const auto& location : locations) {
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800191 if (loadAllElfObjects(location) != 0) {
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800192 ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS FROM %s ===", location.dir);
Hungming Chen4b8e9822020-09-10 15:51:59 +0800193 ALOGE("If this triggers reliably, you're probably missing kernel options or patches.");
194 ALOGE("If this triggers randomly, you might be hitting some memory allocation "
195 "problems or startup script race.");
196 ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---");
197 sleep(20);
198 return 2;
199 }
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700200 }
Joel Fernandesd3ec8712019-01-11 06:22:05 -0500201
202 if (android::base::SetProperty("bpf.progs_loaded", "1") == false) {
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700203 ALOGE("Failed to set bpf.progs_loaded property");
Joel Fernandesd3ec8712019-01-11 06:22:05 -0500204 return 1;
205 }
206
207 return 0;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700208}