blob: 6696cfe746f910982895a93082e18553c28687f2 [file] [log] [blame]
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001/*
Maciej Żenczykowski49140b92024-08-07 15:06:07 -07002 * Copyright (C) 2018-2024 The Android Open Source Project
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07003 *
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
Maciej Żenczykowski49140b92024-08-07 15:06:07 -070017#define LOG_TAG "NetBpfLoad"
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -070018
Elliott Hughescd7f3bf2025-05-22 16:37:33 -040019#include <algorithm>
Maciej Żenczykowski49140b92024-08-07 15:06:07 -070020#include <arpa/inet.h>
Motomu Utsumib3d3c2a2025-03-18 15:06:34 +090021#include <bpf/btf.h>
Motomu Utsumia7693582025-02-05 17:40:08 +090022#include <bpf/libbpf.h>
Maciej Żenczykowski49140b92024-08-07 15:06:07 -070023#include <dirent.h>
24#include <elf.h>
25#include <errno.h>
26#include <error.h>
27#include <fcntl.h>
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -070028#include <fstream>
Maciej Żenczykowski49140b92024-08-07 15:06:07 -070029#include <inttypes.h>
30#include <iostream>
Maciej Żenczykowski49140b92024-08-07 15:06:07 -070031#include <linux/unistd.h>
32#include <log/log.h>
33#include <net/if.h>
34#include <optional>
35#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <string>
40#include <sys/mman.h>
41#include <sys/socket.h>
42#include <sys/stat.h>
43#include <sys/types.h>
44#include <sys/utsname.h>
45#include <sys/wait.h>
46#include <sysexits.h>
47#include <unistd.h>
48#include <unordered_map>
49#include <vector>
50
51#include <android-base/cmsg.h>
52#include <android-base/file.h>
53#include <android-base/logging.h>
54#include <android-base/macros.h>
55#include <android-base/properties.h>
Motomu Utsumi1a5cc5b2025-03-18 15:25:13 +090056#include <android-base/scopeguard.h>
Maciej Żenczykowski49140b92024-08-07 15:06:07 -070057#include <android-base/stringprintf.h>
58#include <android-base/strings.h>
59#include <android-base/unique_fd.h>
60#include <android/api-level.h>
61
Maciej Żenczykowskif7eb2bf2025-06-10 01:56:48 -070062#define BPF_SUPPORT_CMD_FIXUP
Maciej Żenczykowski49140b92024-08-07 15:06:07 -070063#include "BpfSyscallWrappers.h"
64#include "bpf/BpfUtils.h"
Maciej Żenczykowskid6028352024-08-19 15:20:04 -070065#include "bpf_map_def.h"
Maciej Żenczykowski49140b92024-08-07 15:06:07 -070066
Maciej Żenczykowski2d52f8c2024-09-25 22:14:04 +000067// The following matches bpf_helpers.h, which is only for inclusion in bpf code
Maciej Żenczykowski8c097782025-03-04 13:11:56 -080068#define BPFLOADER_MAINLINE_S_VERSION 42u
Maciej Żenczykowski199fd352025-02-13 15:17:08 -080069#define BPFLOADER_MAINLINE_25Q2_VERSION 47u
Maciej Żenczykowski2d52f8c2024-09-25 22:14:04 +000070
Motomu Utsumi52a3ba72025-07-25 10:41:53 +090071using android::base::borrowed_fd;
Maciej Żenczykowski49140b92024-08-07 15:06:07 -070072using android::base::EndsWith;
Maciej Żenczykowski8a767282024-09-04 10:56:55 -070073using android::base::GetIntProperty;
74using android::base::GetProperty;
75using android::base::InitLogging;
76using android::base::KernelLogger;
77using android::base::SetProperty;
78using android::base::Split;
Maciej Żenczykowski49140b92024-08-07 15:06:07 -070079using android::base::StartsWith;
Maciej Żenczykowski8a767282024-09-04 10:56:55 -070080using android::base::Tokenize;
Maciej Żenczykowski49140b92024-08-07 15:06:07 -070081using android::base::unique_fd;
82using std::ifstream;
83using std::ios;
84using std::optional;
85using std::string;
86using std::vector;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -070087
88namespace android {
89namespace bpf {
90
91// Bpf programs may specify per-program & per-map selinux_context and pin_subdir.
92//
93// The BpfLoader needs to convert these bpf.o specified strings into an enum
94// for internal use (to check that valid values were specified for the specific
95// location of the bpf.o file).
96//
97// It also needs to map selinux_context's into pin_subdir's.
98// This is because of how selinux_context is actually implemented via pin+rename.
99//
100// Thus 'domain' enumerates all selinux_context's/pin_subdir's that the BpfLoader
101// is aware of. Thus there currently needs to be a 1:1 mapping between the two.
102//
103enum class domain : int {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700104 unspecified = 0, // means just use the default for that specific pin location
105 tethering, // (S+) fs_bpf_tethering /sys/fs/bpf/tethering
106 net_private, // (T+) fs_bpf_net_private /sys/fs/bpf/net_private
107 net_shared, // (T+) fs_bpf_net_shared /sys/fs/bpf/net_shared
108 netd_readonly, // (T+) fs_bpf_netd_readonly /sys/fs/bpf/netd_readonly
109 netd_shared, // (T+) fs_bpf_netd_shared /sys/fs/bpf/netd_shared
Maciej Żenczykowski1ec8d7d2024-09-04 16:44:04 -0700110 loader, // (U+) fs_bpf_loader /sys/fs/bpf/loader
111 // on T due to lack of sepolicy/genfscon rules it behaves simply as 'fs_bpf'
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700112};
113
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700114static constexpr domain AllDomains[] = {
115 domain::unspecified,
116 domain::tethering,
117 domain::net_private,
118 domain::net_shared,
119 domain::netd_readonly,
120 domain::netd_shared,
Maciej Żenczykowski1ec8d7d2024-09-04 16:44:04 -0700121 domain::loader,
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700122};
123
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700124static constexpr bool specified(domain d) {
125 return d != domain::unspecified;
126}
127
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700128// Returns the build type string (from ro.build.type).
Maciej Żenczykowski49140b92024-08-07 15:06:07 -0700129const std::string& getBuildType() {
Maciej Żenczykowski8a767282024-09-04 10:56:55 -0700130 static std::string t = GetProperty("ro.build.type", "unknown");
Maciej Żenczykowski49140b92024-08-07 15:06:07 -0700131 return t;
132}
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700133
134// The following functions classify the 3 Android build types.
135inline bool isEng() {
136 return getBuildType() == "eng";
137}
Maciej Żenczykowski49140b92024-08-07 15:06:07 -0700138
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700139inline bool isUser() {
140 return getBuildType() == "user";
141}
Maciej Żenczykowski49140b92024-08-07 15:06:07 -0700142
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700143inline bool isUserdebug() {
144 return getBuildType() == "userdebug";
145}
146
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700147#define BPF_FS_PATH "/sys/fs/bpf/"
148
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700149static unsigned int page_size = static_cast<unsigned int>(getpagesize());
150
Maciej Żenczykowskid9fa1c02024-08-07 15:46:11 -0700151constexpr const char* lookupSelinuxContext(const domain d) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700152 switch (d) {
Maciej Żenczykowskid9fa1c02024-08-07 15:46:11 -0700153 case domain::unspecified: return "";
Maciej Żenczykowski868fc842025-07-28 12:33:03 -0700154 case domain::tethering: return "tethering/";
155 case domain::net_private: return "net_private/";
156 case domain::net_shared: return "net_shared/";
157 case domain::netd_readonly: return "netd_readonly/";
158 case domain::netd_shared: return "netd_shared/";
159 case domain::loader: return "loader/";
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700160 }
161}
162
Maciej Żenczykowski07d1a892025-07-28 12:44:40 -0700163domain getDomainFromSelinuxContext(const char s[BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE]) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700164 for (domain d : AllDomains) {
165 // Not sure how to enforce this at compile time, so abort() bpfloader at boot instead
Maciej Żenczykowski07d1a892025-07-28 12:44:40 -0700166 if (strlen(lookupSelinuxContext(d)) >= BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE) abort();
167 if (!strncmp(s, lookupSelinuxContext(d), BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE)) return d;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700168 }
Maciej Żenczykowski6641f2f2024-08-07 15:34:24 -0700169 ALOGE("unrecognized selinux_context '%-32s'", s);
170 // Note: we *can* just abort() here as we only load bpf .o files shipped
171 // in the same mainline module / apex as NetBpfLoad itself.
172 abort();
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700173}
174
Maciej Żenczykowski16a24482025-07-28 15:32:09 -0700175constexpr const char* lookupPinSubdir(const domain d) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700176 switch (d) {
Maciej Żenczykowski16a24482025-07-28 15:32:09 -0700177 case domain::unspecified: return "";
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700178 case domain::tethering: return "tethering/";
179 case domain::net_private: return "net_private/";
180 case domain::net_shared: return "net_shared/";
181 case domain::netd_readonly: return "netd_readonly/";
182 case domain::netd_shared: return "netd_shared/";
Maciej Żenczykowski1ec8d7d2024-09-04 16:44:04 -0700183 case domain::loader: return "loader/";
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700184 }
185};
186
Maciej Żenczykowski1fdc3032025-07-28 16:18:20 -0700187void validatePinDir(const char s[BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE]) {
188 if (!s[0]) abort();
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700189 for (domain d : AllDomains) {
190 // Not sure how to enforce this at compile time, so abort() bpfloader at boot instead
191 if (strlen(lookupPinSubdir(d)) >= BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE) abort();
Maciej Żenczykowski1fdc3032025-07-28 16:18:20 -0700192 if (!strncmp(s, lookupPinSubdir(d), BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE)) return;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700193 }
194 ALOGE("unrecognized pin_subdir '%-32s'", s);
Maciej Żenczykowski6641f2f2024-08-07 15:34:24 -0700195 // Note: we *can* just abort() here as we only load bpf .o files shipped
196 // in the same mainline module / apex as NetBpfLoad itself.
197 abort();
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700198}
199
200static string pathToObjName(const string& path) {
201 // extract everything after the final slash, ie. this is the filename 'foo@1.o' or 'bar.o'
Maciej Żenczykowski8a767282024-09-04 10:56:55 -0700202 string filename = Split(path, "/").back();
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700203 // strip off everything from the final period onwards (strip '.o' suffix), ie. 'foo@1' or 'bar'
204 string name = filename.substr(0, filename.find_last_of('.'));
205 // strip any potential @1 suffix, this will leave us with just 'foo' or 'bar'
206 // this can be used to provide duplicate programs (mux based on the bpfloader version)
207 return name.substr(0, name.find_last_of('@'));
208}
209
210typedef struct {
211 const char* name;
212 enum bpf_prog_type type;
Maciej Żenczykowski346831c2024-08-12 17:49:10 +0000213 enum bpf_attach_type attach_type;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700214} sectionType;
215
216/*
217 * Map section name prefixes to program types, the section name will be:
218 * SECTION(<prefix>/<name-of-program>)
219 * For example:
220 * SECTION("tracepoint/sched_switch_func") where sched_switch_funcs
221 * is the name of the program, and tracepoint is the type.
222 *
223 * However, be aware that you should not be directly using the SECTION() macro.
Maciej Żenczykowski3a085152024-09-18 23:45:52 +0000224 * Instead use the DEFINE_(BPF|XDP)_(PROG|MAP)... & LICENSE macros.
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700225 *
226 * Programs shipped inside the tethering apex should be limited to networking stuff,
227 * as KPROBE, PERF_EVENT, TRACEPOINT are dangerous to use from mainline updatable code,
228 * since they are less stable abi/api and may conflict with platform uses of bpf.
229 */
230sectionType sectionNameTypes[] = {
231 {"bind4/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND},
232 {"bind6/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND},
Maciej Żenczykowski346831c2024-08-12 17:49:10 +0000233 {"cgroupskb/", BPF_PROG_TYPE_CGROUP_SKB},
234 {"cgroupsock/", BPF_PROG_TYPE_CGROUP_SOCK},
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700235 {"cgroupsockcreate/", BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE},
236 {"cgroupsockrelease/", BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET_SOCK_RELEASE},
237 {"connect4/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT},
238 {"connect6/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT},
239 {"egress/", BPF_PROG_TYPE_CGROUP_SKB, BPF_CGROUP_INET_EGRESS},
240 {"getsockopt/", BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT},
241 {"ingress/", BPF_PROG_TYPE_CGROUP_SKB, BPF_CGROUP_INET_INGRESS},
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700242 {"postbind4/", BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND},
243 {"postbind6/", BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET6_POST_BIND},
244 {"recvmsg4/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG},
245 {"recvmsg6/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG},
Maciej Żenczykowski346831c2024-08-12 17:49:10 +0000246 {"schedact/", BPF_PROG_TYPE_SCHED_ACT},
247 {"schedcls/", BPF_PROG_TYPE_SCHED_CLS},
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700248 {"sendmsg4/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG},
249 {"sendmsg6/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG},
250 {"setsockopt/", BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT},
Maciej Żenczykowski346831c2024-08-12 17:49:10 +0000251 {"skfilter/", BPF_PROG_TYPE_SOCKET_FILTER},
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700252 {"sockops/", BPF_PROG_TYPE_SOCK_OPS, BPF_CGROUP_SOCK_OPS},
253 {"sysctl", BPF_PROG_TYPE_CGROUP_SYSCTL, BPF_CGROUP_SYSCTL},
Maciej Żenczykowski346831c2024-08-12 17:49:10 +0000254 {"xdp/", BPF_PROG_TYPE_XDP},
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700255};
256
257typedef struct {
258 enum bpf_prog_type type;
Maciej Żenczykowski346831c2024-08-12 17:49:10 +0000259 enum bpf_attach_type attach_type;
Motomu Utsumi8645b6e2025-07-23 12:04:50 +0900260 string name; // The canonicalized section name.
261 string program_name;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700262 vector<char> data;
263 vector<char> rel_data;
264 optional<struct bpf_prog_def> prog_def;
265
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000266 unique_fd prog_fd; // fd after loading
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700267} codeSection;
268
269static int readElfHeader(ifstream& elfFile, Elf64_Ehdr* eh) {
270 elfFile.seekg(0);
271 if (elfFile.fail()) return -1;
272
273 if (!elfFile.read((char*)eh, sizeof(*eh))) return -1;
274
275 return 0;
276}
277
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000278// Reads all section header tables into an Shdr array
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700279static int readSectionHeadersAll(ifstream& elfFile, vector<Elf64_Shdr>& shTable) {
280 Elf64_Ehdr eh;
281 int ret = 0;
282
283 ret = readElfHeader(elfFile, &eh);
284 if (ret) return ret;
285
286 elfFile.seekg(eh.e_shoff);
287 if (elfFile.fail()) return -1;
288
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000289 // Read shdr table entries
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700290 shTable.resize(eh.e_shnum);
291
292 if (!elfFile.read((char*)shTable.data(), (eh.e_shnum * eh.e_shentsize))) return -ENOMEM;
293
294 return 0;
295}
296
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000297// Read a section by its index - for ex to get sec hdr strtab blob
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700298static int readSectionByIdx(ifstream& elfFile, int id, vector<char>& sec) {
299 vector<Elf64_Shdr> shTable;
300 int ret = readSectionHeadersAll(elfFile, shTable);
301 if (ret) return ret;
302
303 elfFile.seekg(shTable[id].sh_offset);
304 if (elfFile.fail()) return -1;
305
306 sec.resize(shTable[id].sh_size);
307 if (!elfFile.read(sec.data(), shTable[id].sh_size)) return -1;
308
309 return 0;
310}
311
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000312// Read whole section header string table
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700313static int readSectionHeaderStrtab(ifstream& elfFile, vector<char>& strtab) {
314 Elf64_Ehdr eh;
315 int ret = readElfHeader(elfFile, &eh);
316 if (ret) return ret;
317
318 ret = readSectionByIdx(elfFile, eh.e_shstrndx, strtab);
319 if (ret) return ret;
320
321 return 0;
322}
323
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000324// Get name from offset in strtab
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700325static int getSymName(ifstream& elfFile, int nameOff, string& name) {
326 int ret;
327 vector<char> secStrTab;
328
329 ret = readSectionHeaderStrtab(elfFile, secStrTab);
330 if (ret) return ret;
331
332 if (nameOff >= (int)secStrTab.size()) return -1;
333
334 name = string((char*)secStrTab.data() + nameOff);
335 return 0;
336}
337
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000338// Reads a full section by name - example to get the GPL license
Motomu Utsumi99a7b732025-07-17 10:59:49 +0900339template <typename T>
340static int readSectionByName(const char* name, ifstream& elfFile, vector<T>& data) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700341 vector<char> secStrTab;
342 vector<Elf64_Shdr> shTable;
343 int ret;
344
345 ret = readSectionHeadersAll(elfFile, shTable);
346 if (ret) return ret;
347
348 ret = readSectionHeaderStrtab(elfFile, secStrTab);
349 if (ret) return ret;
350
351 for (int i = 0; i < (int)shTable.size(); i++) {
352 char* secname = secStrTab.data() + shTable[i].sh_name;
353 if (!secname) continue;
354
355 if (!strcmp(secname, name)) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700356 elfFile.seekg(shTable[i].sh_offset);
357 if (elfFile.fail()) return -1;
358
Motomu Utsumi99a7b732025-07-17 10:59:49 +0900359 if (shTable[i].sh_size % sizeof(T)) return -1;
360 data.resize(shTable[i].sh_size / sizeof(T));
361 if (!elfFile.read(reinterpret_cast<char*>(data.data()), shTable[i].sh_size))
362 return -1;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700363
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700364 return 0;
365 }
366 }
367 return -2;
368}
369
Maciej Żenczykowski213c9222024-08-15 15:52:43 -0700370unsigned int readSectionUint(const char* name, ifstream& elfFile) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700371 vector<char> theBytes;
372 int ret = readSectionByName(name, elfFile, theBytes);
373 if (ret) {
Maciej Żenczykowski213c9222024-08-15 15:52:43 -0700374 ALOGE("Couldn't find section %s.", name);
375 abort();
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700376 } else if (theBytes.size() < sizeof(unsigned int)) {
Maciej Żenczykowski213c9222024-08-15 15:52:43 -0700377 ALOGE("Section %s is too short.", name);
378 abort();
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700379 } else {
380 // decode first 4 bytes as LE32 uint, there will likely be more bytes due to alignment.
381 unsigned int value = static_cast<unsigned char>(theBytes[3]);
382 value <<= 8;
383 value += static_cast<unsigned char>(theBytes[2]);
384 value <<= 8;
385 value += static_cast<unsigned char>(theBytes[1]);
386 value <<= 8;
387 value += static_cast<unsigned char>(theBytes[0]);
Maciej Żenczykowskidbdd90f2024-08-22 23:42:58 +0000388 ALOGD("Section %s value is %u [0x%x]", name, value, value);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700389 return value;
390 }
391}
392
393static int readSectionByType(ifstream& elfFile, int type, vector<char>& data) {
394 int ret;
395 vector<Elf64_Shdr> shTable;
396
397 ret = readSectionHeadersAll(elfFile, shTable);
398 if (ret) return ret;
399
400 for (int i = 0; i < (int)shTable.size(); i++) {
401 if ((int)shTable[i].sh_type != type) continue;
402
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700403 elfFile.seekg(shTable[i].sh_offset);
404 if (elfFile.fail()) return -1;
405
Motomu Utsumi1ae10a02025-07-17 10:45:19 +0900406 data.resize(shTable[i].sh_size);
407 if (!elfFile.read(data.data(), shTable[i].sh_size)) return -1;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700408
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700409 return 0;
410 }
411 return -2;
412}
413
414static bool symCompare(Elf64_Sym a, Elf64_Sym b) {
415 return (a.st_value < b.st_value);
416}
417
418static int readSymTab(ifstream& elfFile, int sort, vector<Elf64_Sym>& data) {
419 int ret, numElems;
420 Elf64_Sym* buf;
421 vector<char> secData;
422
423 ret = readSectionByType(elfFile, SHT_SYMTAB, secData);
424 if (ret) return ret;
425
426 buf = (Elf64_Sym*)secData.data();
427 numElems = (secData.size() / sizeof(Elf64_Sym));
428 data.assign(buf, buf + numElems);
429
430 if (sort) std::sort(data.begin(), data.end(), symCompare);
431 return 0;
432}
433
434static enum bpf_prog_type getSectionType(string& name) {
435 for (auto& snt : sectionNameTypes)
436 if (StartsWith(name, snt.name)) return snt.type;
437
438 return BPF_PROG_TYPE_UNSPEC;
439}
440
Maciej Żenczykowskie666d852024-08-15 15:03:38 -0700441static int readProgDefs(ifstream& elfFile, vector<struct bpf_prog_def>& pd) {
Motomu Utsumi99a7b732025-07-17 10:59:49 +0900442 return readSectionByName("progs", elfFile, pd);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700443}
444
445static int getSectionSymNames(ifstream& elfFile, const string& sectionName, vector<string>& names,
446 optional<unsigned> symbolType = std::nullopt) {
447 int ret;
448 string name;
449 vector<Elf64_Sym> symtab;
450 vector<Elf64_Shdr> shTable;
451
452 ret = readSymTab(elfFile, 1 /* sort */, symtab);
453 if (ret) return ret;
454
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000455 // Get index of section
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700456 ret = readSectionHeadersAll(elfFile, shTable);
457 if (ret) return ret;
458
459 int sec_idx = -1;
460 for (int i = 0; i < (int)shTable.size(); i++) {
461 ret = getSymName(elfFile, shTable[i].sh_name, name);
462 if (ret) return ret;
463
464 if (!name.compare(sectionName)) {
465 sec_idx = i;
466 break;
467 }
468 }
469
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000470 // No section found with matching name
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700471 if (sec_idx == -1) {
472 ALOGW("No %s section could be found in elf object", sectionName.c_str());
473 return -1;
474 }
475
476 for (int i = 0; i < (int)symtab.size(); i++) {
477 if (symbolType.has_value() && ELF_ST_TYPE(symtab[i].st_info) != symbolType) continue;
478
479 if (symtab[i].st_shndx == sec_idx) {
480 string s;
481 ret = getSymName(elfFile, symtab[i].st_name, s);
482 if (ret) return ret;
483 names.push_back(s);
484 }
485 }
486
487 return 0;
488}
489
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000490// Read a section by its index - for ex to get sec hdr strtab blob
Maciej Żenczykowskie666d852024-08-15 15:03:38 -0700491static int readCodeSections(ifstream& elfFile, vector<codeSection>& cs) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700492 vector<Elf64_Shdr> shTable;
493 int entries, ret = 0;
494
495 ret = readSectionHeadersAll(elfFile, shTable);
496 if (ret) return ret;
497 entries = shTable.size();
498
499 vector<struct bpf_prog_def> pd;
Maciej Żenczykowskie666d852024-08-15 15:03:38 -0700500 ret = readProgDefs(elfFile, pd);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700501 if (ret) return ret;
502 vector<string> progDefNames;
503 ret = getSectionSymNames(elfFile, "progs", progDefNames);
504 if (!pd.empty() && ret) return ret;
505
506 for (int i = 0; i < entries; i++) {
507 string name;
508 codeSection cs_temp;
509 cs_temp.type = BPF_PROG_TYPE_UNSPEC;
510
511 ret = getSymName(elfFile, shTable[i].sh_name, name);
512 if (ret) return ret;
513
514 enum bpf_prog_type ptype = getSectionType(name);
515
516 if (ptype == BPF_PROG_TYPE_UNSPEC) continue;
517
518 // This must be done before '/' is replaced with '_'.
Maciej Żenczykowski346831c2024-08-12 17:49:10 +0000519 for (auto& snt : sectionNameTypes)
520 if (StartsWith(name, snt.name)) cs_temp.attach_type = snt.attach_type;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700521
522 string oldName = name;
523
524 // convert all slashes to underscores
525 std::replace(name.begin(), name.end(), '/', '_');
526
527 cs_temp.type = ptype;
528 cs_temp.name = name;
529
530 ret = readSectionByIdx(elfFile, i, cs_temp.data);
531 if (ret) return ret;
532 ALOGV("Loaded code section %d (%s)", i, name.c_str());
533
534 vector<string> csSymNames;
535 ret = getSectionSymNames(elfFile, oldName, csSymNames, STT_FUNC);
536 if (ret || !csSymNames.size()) return ret;
Motomu Utsumi8645b6e2025-07-23 12:04:50 +0900537 cs_temp.program_name = csSymNames[0];
Motomu Utsumi62b1c882025-03-21 15:13:00 +0900538 for (size_t j = 0; j < progDefNames.size(); ++j) {
539 if (!progDefNames[j].compare(csSymNames[0] + "_def")) {
540 cs_temp.prog_def = pd[j];
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700541 break;
542 }
543 }
544
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000545 // Check for rel section
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700546 if (cs_temp.data.size() > 0 && i < entries) {
547 ret = getSymName(elfFile, shTable[i + 1].sh_name, name);
548 if (ret) return ret;
549
550 if (name == (".rel" + oldName)) {
551 ret = readSectionByIdx(elfFile, i + 1, cs_temp.rel_data);
552 if (ret) return ret;
553 ALOGV("Loaded relo section %d (%s)", i, name.c_str());
554 }
555 }
556
557 if (cs_temp.data.size() > 0) {
558 cs.push_back(std::move(cs_temp));
559 ALOGV("Adding section %d to cs list", i);
560 }
561 }
562 return 0;
563}
564
565static int getSymNameByIdx(ifstream& elfFile, int index, string& name) {
566 vector<Elf64_Sym> symtab;
567 int ret = 0;
568
569 ret = readSymTab(elfFile, 0 /* !sort */, symtab);
570 if (ret) return ret;
571
572 if (index >= (int)symtab.size()) return -1;
573
574 return getSymName(elfFile, symtab[index].st_name, name);
575}
576
577static bool mapMatchesExpectations(const unique_fd& fd, const string& mapName,
578 const struct bpf_map_def& mapDef, const enum bpf_map_type type) {
579 // bpfGetFd... family of functions require at minimum a 4.14 kernel,
580 // so on 4.9-T kernels just pretend the map matches our expectations.
581 // Additionally we'll get almost equivalent test coverage on newer devices/kernels.
582 // This is because the primary failure mode we're trying to detect here
583 // is either a source code misconfiguration (which is likely kernel independent)
584 // or a newly introduced kernel feature/bug (which is unlikely to get backported to 4.9).
585 if (!isAtLeastKernelVersion(4, 14, 0)) return true;
586
587 // Assuming fd is a valid Bpf Map file descriptor then
588 // all the following should always succeed on a 4.14+ kernel.
589 // If they somehow do fail, they'll return -1 (and set errno),
590 // which should then cause (among others) a key_size mismatch.
591 int fd_type = bpfGetFdMapType(fd);
592 int fd_key_size = bpfGetFdKeySize(fd);
593 int fd_value_size = bpfGetFdValueSize(fd);
594 int fd_max_entries = bpfGetFdMaxEntries(fd);
595 int fd_map_flags = bpfGetFdMapFlags(fd);
596
597 // DEVMAPs are readonly from the bpf program side's point of view, as such
598 // the kernel in kernel/bpf/devmap.c dev_map_init_map() will set the flag
599 int desired_map_flags = (int)mapDef.map_flags;
600 if (type == BPF_MAP_TYPE_DEVMAP || type == BPF_MAP_TYPE_DEVMAP_HASH)
601 desired_map_flags |= BPF_F_RDONLY_PROG;
602
603 // The .h file enforces that this is a power of two, and page size will
604 // also always be a power of two, so this logic is actually enough to
605 // force it to be a multiple of the page size, as required by the kernel.
606 unsigned int desired_max_entries = mapDef.max_entries;
607 if (type == BPF_MAP_TYPE_RINGBUF) {
608 if (desired_max_entries < page_size) desired_max_entries = page_size;
609 }
610
611 // The following checks should *never* trigger, if one of them somehow does,
612 // it probably means a bpf .o file has been changed/replaced at runtime
613 // and bpfloader was manually rerun (normally it should only run *once*
614 // early during the boot process).
615 // Another possibility is that something is misconfigured in the code:
616 // most likely a shared map is declared twice differently.
617 // But such a change should never be checked into the source tree...
618 if ((fd_type == type) &&
619 (fd_key_size == (int)mapDef.key_size) &&
620 (fd_value_size == (int)mapDef.value_size) &&
621 (fd_max_entries == (int)desired_max_entries) &&
622 (fd_map_flags == desired_map_flags)) {
623 return true;
624 }
625
Maciej Żenczykowski4b3937b2025-05-29 01:24:47 -0700626 ALOGE("bpf map name %s mismatch: desired/found (errno: %d): "
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700627 "type:%d/%d key:%u/%d value:%u/%d entries:%u/%d flags:%u/%d",
Maciej Żenczykowski4b3937b2025-05-29 01:24:47 -0700628 mapName.c_str(), errno, type, fd_type, mapDef.key_size, fd_key_size,
629 mapDef.value_size, fd_value_size, mapDef.max_entries, fd_max_entries,
630 desired_map_flags, fd_map_flags);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700631 return false;
632}
633
Motomu Utsumib3d3c2a2025-03-18 15:06:34 +0900634static int setBtfDatasecSize(ifstream &elfFile, struct btf *btf,
635 struct btf_type *bt) {
636 const char *name = btf__name_by_offset(btf, bt->name_off);
637 if (!name) {
638 ALOGE("Couldn't resolve section name, errno: %d", errno);
639 return -errno;
640 }
641
642 vector<char> data;
643 int ret = readSectionByName(name, elfFile, data);
644 if (ret) {
645 ALOGE("Couldn't read section %s, ret: %d", name, ret);
646 return ret;
647 }
648 bt->size = data.size();
649 return 0;
650}
651
Motomu Utsumiefe33312025-03-18 15:08:15 +0900652static int getSymOffsetByName(ifstream &elfFile, const char *name, int *off) {
653 vector<Elf64_Sym> symtab;
654 int ret = readSymTab(elfFile, 1 /* sort */, symtab);
655 if (ret) return ret;
656 for (int i = 0; i < (int)symtab.size(); i++) {
657 string s;
658 ret = getSymName(elfFile, symtab[i].st_name, s);
659 if (ret) continue;
660 if (!strcmp(s.c_str(), name)) {
661 *off = symtab[i].st_value;
662 return 0;
663 }
664 }
665 return -1;
666}
667
668static int setBtfVarOffset(ifstream &elfFile, struct btf *btf,
669 struct btf_type *datasecBt) {
670 int i, vars = btf_vlen(datasecBt);
671 struct btf_var_secinfo *vsi;
672 const char *datasecName = btf__name_by_offset(btf, datasecBt->name_off);
673 if (!datasecName) {
674 ALOGE("Couldn't resolve section name, errno: %d", errno);
675 return -errno;
676 }
677
678 for (i = 0, vsi = btf_var_secinfos(datasecBt); i < vars; i++, vsi++) {
679 const struct btf_type *varBt = btf__type_by_id(btf, vsi->type);
680 if (!varBt || !btf_is_var(varBt)) {
681 ALOGE("Found non VAR kind btf_type, section: %s id: %d", datasecName,
682 vsi->type);
683 return -1;
684 }
685
686 const struct btf_var *var = btf_var(varBt);
687 if (var->linkage == BTF_VAR_STATIC) continue;
688
689 const char *varName = btf__name_by_offset(btf, varBt->name_off);
690 if (!varName) {
691 ALOGE("Failed to resolve var name, section: %s", datasecName);
692 return -1;
693 }
694
695 int off;
696 int ret = getSymOffsetByName(elfFile, varName, &off);
697 if (ret) {
698 ALOGE("No offset found in symbol table, section: %s, var: %s, ret: %d",
699 datasecName, varName, ret);
700 return ret;
701 }
702 vsi->offset = off;
703 }
704 return 0;
705}
706
Motomu Utsumi4a3b5372025-06-04 09:15:36 +0900707#define BTF_INFO_ENC(kind, kind_flag, vlen) \
708 ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
709#define BTF_INT_ENC(encoding, bits_offset, nr_bits) \
710 ((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
711
712static int sanitizeBtf(struct btf *btf) {
713 for (unsigned int i = 1; i < btf__type_cnt(btf); ++i) {
714 struct btf_type *bt = (struct btf_type *)btf__type_by_id(btf, i);
715
716 // Replace BTF_KIND_VAR (5.2+) with BTF_KIND_INT (4.18+)
717 if (btf_is_var(bt)) {
718 bt->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
719 // using size = 1 is the safest choice, 4 will be too
720 // big and cause kernel BTF validation failure if
721 // original variable took less than 4 bytes
722 bt->size = 1;
723 *(int *)(bt + 1) = BTF_INT_ENC(0, 0, 8);
724 continue;
725 }
726
727 // Replace BTF_KIND_FUNC_PROTO (5.0+) with BTF_KIND_ENUM (4.18+)
728 if (btf_is_func_proto(bt)) {
729 int vlen = btf_vlen(bt);
730 bt->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
731 bt->size = sizeof(__u32); // kernel enforced
732 continue;
733 }
734
735 // Replace BTF_KIND_FUNC (5.0+) with BTF_KIND_TYPEDEF (4.18+)
736 if (btf_is_func(bt)) {
737 bt->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
738 continue;
739 }
740
741 // Replace BTF_KIND_DATASEC (5.2+) with BTF_KIND_STRUCT (4.18+)
742 if (btf_is_datasec(bt)) {
743 const struct btf_var_secinfo *v = btf_var_secinfos(bt);
744 struct btf_member *m = btf_members(bt);
745 char *name;
746
747 name = (char *)btf__name_by_offset(btf, bt->name_off);
748 while (*name) {
749 if (*name == '.' || *name == '?') *name = '_';
750 name++;
751 }
752
753 int vlen = btf_vlen(bt);
754 bt->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
755 for (int j = 0; j < vlen; j++, v++, m++) {
756 // order of field assignments is important
757 m->offset = v->offset * 8;
758 m->type = v->type;
759 // preserve variable name as member name
760 const struct btf_type *vt = btf__type_by_id(btf, v->type);
761 m->name_off = vt->name_off;
762 }
763 }
764 }
765 return 0;
766}
767
Motomu Utsumi21536e52025-03-18 15:09:27 +0900768static int loadBtf(ifstream &elfFile, struct btf *btf) {
769 int ret;
770 for (unsigned int i = 1; i < btf__type_cnt(btf); ++i) {
771 struct btf_type *bt = (struct btf_type *)btf__type_by_id(btf, i);
772 if (!btf_is_datasec(bt)) continue;
773 ret = setBtfDatasecSize(elfFile, btf, bt);
774 if (ret) return ret;
775 ret = setBtfVarOffset(elfFile, btf, bt);
776 if (ret) return ret;
777 }
778
Motomu Utsumi4a3b5372025-06-04 09:15:36 +0900779 if (!isAtLeastKernelVersion(5, 10, 0)) {
780 // Likely unnecessary on kernel 5.4 but untested.
781 sanitizeBtf(btf);
782 }
783
Motomu Utsumi21536e52025-03-18 15:09:27 +0900784 ret = btf__load_into_kernel(btf);
785 if (ret) {
786 if (errno != EINVAL) {
787 ALOGE("btf__load_into_kernel failed, errno: %d", errno);
788 return ret;
789 };
790 // For BTF_KIND_FUNC, newer kernels can read the BTF_INFO_VLEN bits of
791 // struct btf_type to distinguish static vs. global vs. extern
792 // functions, but older kernels enforce that only the BTF_INFO_KIND bits
793 // can be set. Retry with non-BTF_INFO_KIND bits zeroed out to handle
794 // this case.
795 for (unsigned int i = 1; i < btf__type_cnt(btf); ++i) {
796 struct btf_type *bt = (struct btf_type *)btf__type_by_id(btf, i);
797 if (btf_is_func(bt)) {
798 bt->info = (BTF_INFO_KIND(bt->info)) << 24;
799 }
800 }
801 ret = btf__load_into_kernel(btf);
802 if (ret) {
803 ALOGE("btf__load_into_kernel retry failed, errno: %d", errno);
804 return ret;
805 };
806 }
807 return 0;
808}
809
Motomu Utsumi59b20992025-03-18 15:10:19 +0900810int getKeyValueTids(const struct btf *btf, const char *mapName,
811 uint32_t expectedKeySize, uint32_t expectedValueSize,
812 uint32_t *keyTypeId, uint32_t *valueTypeId) {
813 const struct btf_type *kvBt;
814 const struct btf_member *key, *value;
815 const size_t max_name = 256;
816 char kvTypeName[max_name];
817 int64_t keySize, valueSize;
Motomu Utsumia6ffae22025-03-21 11:00:10 +0900818 int32_t kvId;
Motomu Utsumi59b20992025-03-18 15:10:19 +0900819
820 if (snprintf(kvTypeName, max_name, "____btf_map_%s", mapName) == max_name) {
821 ALOGE("____btf_map_%s is too long", mapName);
822 return -1;
823 }
824
825 kvId = btf__find_by_name(btf, kvTypeName);
826 if (kvId < 0) {
827 ALOGE("section not found, map: %s typeName: %s", mapName, kvTypeName);
828 return -1;
829 }
830
831 kvBt = btf__type_by_id(btf, kvId);
832 if (!kvBt) {
833 ALOGE("Couldn't find BTF type, map: %s id: %u", mapName, kvId);
834 return -1;
835 }
836
837 if (!btf_is_struct(kvBt) || btf_vlen(kvBt) < 2) {
838 ALOGE("Non Struct kind or invalid vlen, map: %s id: %u", mapName, kvId);
839 return -1;
840 }
841
842 key = btf_members(kvBt);
843 value = key + 1;
844
845 keySize = btf__resolve_size(btf, key->type);
846 if (keySize < 0) {
847 ALOGE("Couldn't get key size, map: %s errno: %d", mapName, errno);
848 return -1;
849 }
850
851 valueSize = btf__resolve_size(btf, value->type);
852 if (valueSize < 0) {
853 ALOGE("Couldn't get value size, map: %s errno: %d", mapName, errno);
854 return -1;
855 }
856
857 if (expectedKeySize != keySize || expectedValueSize != valueSize) {
858 ALOGE("Key value size mismatch, map: %s key size: %d expected key size: "
859 "%d value size: %d expected value size: %d",
860 mapName, (uint32_t)keySize, expectedKeySize, (uint32_t)valueSize,
861 expectedValueSize);
862 return -1;
863 }
864
865 *keyTypeId = key->type;
866 *valueTypeId = value->type;
867
868 return 0;
869}
870
Motomu Utsumi1a5cc5b2025-03-18 15:25:13 +0900871static bool isBtfSupported(enum bpf_map_type type) {
872 return type != BPF_MAP_TYPE_DEVMAP_HASH && type != BPF_MAP_TYPE_RINGBUF;
873}
874
Motomu Utsumi52a3ba72025-07-25 10:41:53 +0900875static int pinMap(const borrowed_fd& fd, const string& mapName, const struct bpf_map_def& mapDef,
Motomu Utsumi77b0b252025-07-15 13:36:20 +0900876 const string& objName, const string& mapPinLoc) {
877 int ret;
878 domain selinux_context = getDomainFromSelinuxContext(mapDef.selinux_context);
879 if (specified(selinux_context)) {
880 ALOGV("map %s selinux_context [%-32s] -> %d -> '%s' (%s)", mapName.c_str(),
881 mapDef.selinux_context, static_cast<int>(selinux_context),
882 lookupSelinuxContext(selinux_context), lookupPinSubdir(selinux_context));
883
884 string createLoc = string(BPF_FS_PATH) + lookupPinSubdir(selinux_context) +
885 "tmp_map_" + objName + "_" + mapName;
886 ret = bpfFdPin(fd, createLoc.c_str());
887 if (ret) {
888 const int err = errno;
889 ALOGE("create %s -> %d [%d:%s]", createLoc.c_str(), ret, err, strerror(err));
890 return -err;
891 }
892 ret = renameat2(AT_FDCWD, createLoc.c_str(),
893 AT_FDCWD, mapPinLoc.c_str(), RENAME_NOREPLACE);
894 if (ret) {
895 const int err = errno;
896 ALOGE("rename %s %s -> %d [%d:%s]", createLoc.c_str(), mapPinLoc.c_str(), ret,
897 err, strerror(err));
898 return -err;
899 }
900 } else {
901 ret = bpfFdPin(fd, mapPinLoc.c_str());
902 if (ret) {
903 const int err = errno;
904 ALOGE("pin %s -> %d [%d:%s]", mapPinLoc.c_str(), ret, err, strerror(err));
905 return -err;
906 }
907 }
908 ret = chmod(mapPinLoc.c_str(), mapDef.mode);
909 if (ret) {
910 const int err = errno;
911 ALOGE("chmod(%s, 0%o) = %d [%d:%s]", mapPinLoc.c_str(), mapDef.mode, ret, err,
912 strerror(err));
913 return -err;
914 }
915 ret = chown(mapPinLoc.c_str(), (uid_t)mapDef.uid, (gid_t)mapDef.gid);
916 if (ret) {
917 const int err = errno;
918 ALOGE("chown(%s, %u, %u) = %d [%d:%s]", mapPinLoc.c_str(), mapDef.uid, mapDef.gid,
919 ret, err, strerror(err));
920 return -err;
921 }
922
923 if (isAtLeastKernelVersion(4, 14, 0)) {
924 int mapId = bpfGetFdMapId(fd);
925 if (mapId == -1) {
926 const int err = errno;
927 ALOGE("bpfGetFdMapId failed, errno: %d", err);
928 return -err;
929 }
930 ALOGI("map %s id %d", mapPinLoc.c_str(), mapId);
931 }
932 return 0;
933}
934
Motomu Utsumie5dcaf72025-07-01 14:44:18 +0900935static int readMapNames(ifstream& elfFile, vector<string>& mapNames) {
936 int ret = getSectionSymNames(elfFile, ".android_maps", mapNames);
937 if (ret) return ret;
938
939 const string suffix = "_def";
940 for (string& name : mapNames) {
941 if (EndsWith(name, suffix)) {
942 name.erase(name.length() - suffix.length());
943 } else {
944 ALOGE("Failed to get map names, invalid symbol in .android_maps: %s", name.c_str());
945 return 1;
946 }
947 }
948 return 0;
949}
950
Motomu Utsumic80abe12025-07-18 10:14:37 +0900951static bool isMapTypeSupported(enum bpf_map_type type) {
952 if (type == BPF_MAP_TYPE_LPM_TRIE && !isAtLeastKernelVersion(4, 14, 0)) {
953 // On Linux Kernels older than 4.14 this map type doesn't exist - autoskip.
954 return false;
955 }
956 return true;
957}
958
959static enum bpf_map_type sanitizeMapType(enum bpf_map_type type) {
960 if (type == BPF_MAP_TYPE_DEVMAP && !isAtLeastKernelVersion(4, 14, 0)) {
961 // On Linux Kernels older than 4.14 this map type doesn't exist, but it can kind
962 // of be approximated: ARRAY has the same userspace api, though it is not usable
963 // by the same ebpf programs. However, that's okay because the bpf_redirect_map()
964 // helper doesn't exist on 4.9-T anyway (so the bpf program would fail to load,
965 // and thus needs to be tagged as 4.14+ either way), so there's nothing useful you
966 // could do with a DEVMAP anyway (that isn't already provided by an ARRAY)...
967 // Hence using an ARRAY instead of a DEVMAP simply makes life easier for userspace.
968 return BPF_MAP_TYPE_ARRAY;
969 }
970 if (type == BPF_MAP_TYPE_DEVMAP_HASH && !isAtLeastKernelVersion(5, 4, 0)) {
971 // On Linux Kernels older than 5.4 this map type doesn't exist, but it can kind
972 // of be approximated: HASH has the same userspace visible api.
973 // However it cannot be used by ebpf programs in the same way.
974 // Since bpf_redirect_map() only requires 4.14, a program using a DEVMAP_HASH map
975 // would fail to load (due to trying to redirect to a HASH instead of DEVMAP_HASH).
976 // One must thus tag any BPF_MAP_TYPE_DEVMAP_HASH + bpf_redirect_map() using
977 // programs as being 5.4+...
978 return BPF_MAP_TYPE_HASH;
979 }
980 // No sanitization is required.
981 return type;
982}
983
Maciej Żenczykowski1fdc3032025-07-28 16:18:20 -0700984static string buildMapPinLoc(const char pin_subdir[BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE],
985 const string& objName, const string& mapName) {
986 validatePinDir(pin_subdir);
987 // Format of pin location is /sys/fs/bpf/<pin_subdir>map_<objName>_<mapName>
Motomu Utsumibf407f62025-07-24 13:08:34 +0900988 // Note: <objName> refers to the extension-less basename of the .o file (without @ suffix).
Maciej Żenczykowski1fdc3032025-07-28 16:18:20 -0700989 return string(BPF_FS_PATH) + pin_subdir + "map_" + objName + "_" + mapName;
Motomu Utsumibf407f62025-07-24 13:08:34 +0900990}
991
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700992static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>& mapFds,
Maciej Żenczykowski960c3372025-07-28 15:09:22 -0700993 const unsigned int bpfloader_ver) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700994 int ret;
Motomu Utsumi99a7b732025-07-17 10:59:49 +0900995 vector<char> btfData;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700996 vector<struct bpf_map_def> md;
997 vector<string> mapNames;
998 string objName = pathToObjName(string(elfPath));
999
Motomu Utsumi99a7b732025-07-17 10:59:49 +09001000 ret = readSectionByName(".android_maps", elfFile, md);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001001 if (ret == -2) return 0; // no maps to read
1002 if (ret) return ret;
1003
Motomu Utsumie5dcaf72025-07-01 14:44:18 +09001004 ret = readMapNames(elfFile, mapNames);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001005 if (ret) return ret;
1006
Motomu Utsumi6f1cecc2025-03-19 19:49:08 +09001007 struct btf *btf = NULL;
Patrick Rohr03b97432025-04-17 08:37:01 -07001008 auto btfGuard = base::make_scope_guard([&btf] { if (btf) btf__free(btf); });
Motomu Utsumi4a3b5372025-06-04 09:15:36 +09001009 if (isAtLeastKernelVersion(4, 19, 0)) {
Motomu Utsumi6f1cecc2025-03-19 19:49:08 +09001010 // On Linux Kernels older than 4.18 BPF_BTF_LOAD command doesn't exist.
1011 ret = readSectionByName(".BTF", elfFile, btfData);
1012 if (ret) {
1013 ALOGE("Failed to read .BTF section, ret:%d", ret);
1014 return ret;
1015 }
Motomu Utsumi62b1c882025-03-21 15:13:00 +09001016 btf = btf__new(btfData.data(), btfData.size());
Motomu Utsumi6f1cecc2025-03-19 19:49:08 +09001017 if (btf == NULL) {
1018 ALOGE("btf__new failed, errno: %d", errno);
1019 return -errno;
1020 }
Motomu Utsumi1a5cc5b2025-03-18 15:25:13 +09001021
Motomu Utsumi6f1cecc2025-03-19 19:49:08 +09001022 ret = loadBtf(elfFile, btf);
1023 if (ret) return ret;
1024 }
Motomu Utsumi1a5cc5b2025-03-18 15:25:13 +09001025
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001026 unsigned kvers = kernelVersion();
1027
1028 for (int i = 0; i < (int)mapNames.size(); i++) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001029 if (bpfloader_ver < md[i].bpfloader_min_ver) {
Maciej Żenczykowskidbdd90f2024-08-22 23:42:58 +00001030 ALOGD("skipping map %s which requires bpfloader min ver 0x%05x", mapNames[i].c_str(),
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001031 md[i].bpfloader_min_ver);
1032 mapFds.push_back(unique_fd());
1033 continue;
1034 }
1035
1036 if (bpfloader_ver >= md[i].bpfloader_max_ver) {
Maciej Żenczykowskidbdd90f2024-08-22 23:42:58 +00001037 ALOGD("skipping map %s which requires bpfloader max ver 0x%05x", mapNames[i].c_str(),
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001038 md[i].bpfloader_max_ver);
1039 mapFds.push_back(unique_fd());
1040 continue;
1041 }
1042
1043 if (kvers < md[i].min_kver) {
Maciej Żenczykowskidbdd90f2024-08-22 23:42:58 +00001044 ALOGD("skipping map %s which requires kernel version 0x%x >= 0x%x",
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001045 mapNames[i].c_str(), kvers, md[i].min_kver);
1046 mapFds.push_back(unique_fd());
1047 continue;
1048 }
1049
1050 if (kvers >= md[i].max_kver) {
Maciej Żenczykowskidbdd90f2024-08-22 23:42:58 +00001051 ALOGD("skipping map %s which requires kernel version 0x%x < 0x%x",
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001052 mapNames[i].c_str(), kvers, md[i].max_kver);
1053 mapFds.push_back(unique_fd());
1054 continue;
1055 }
1056
Motomu Utsumic80abe12025-07-18 10:14:37 +09001057 if (!isMapTypeSupported(md[i].type)) {
1058 ALOGD("skipping unsupported map type(%d): %s", md[i].type, mapNames[i].c_str());
Maciej Żenczykowski87019832025-02-03 22:04:26 -08001059 mapFds.push_back(unique_fd());
1060 continue;
1061 }
Motomu Utsumic80abe12025-07-18 10:14:37 +09001062 enum bpf_map_type type = sanitizeMapType(md[i].type);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001063
1064 // The .h file enforces that this is a power of two, and page size will
1065 // also always be a power of two, so this logic is actually enough to
1066 // force it to be a multiple of the page size, as required by the kernel.
1067 unsigned int max_entries = md[i].max_entries;
1068 if (type == BPF_MAP_TYPE_RINGBUF) {
1069 if (max_entries < page_size) max_entries = page_size;
1070 }
1071
Maciej Żenczykowski1fdc3032025-07-28 16:18:20 -07001072 string mapPinLoc = buildMapPinLoc(md[i].pin_subdir, objName, mapNames[i]);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001073 unique_fd fd;
1074 int saved_errno;
1075
1076 if (access(mapPinLoc.c_str(), F_OK) == 0) {
1077 fd.reset(mapRetrieveRO(mapPinLoc.c_str()));
1078 saved_errno = errno;
1079 ALOGD("bpf_create_map reusing map %s, ret: %d", mapNames[i].c_str(), fd.get());
Maciej Żenczykowskib71cd4f2025-05-20 06:41:01 -07001080 abort();
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001081 } else {
1082 union bpf_attr req = {
1083 .map_type = type,
1084 .key_size = md[i].key_size,
1085 .value_size = md[i].value_size,
1086 .max_entries = max_entries,
Maciej Żenczykowskic1a9f4a2025-01-20 12:09:13 -08001087 .map_flags = md[i].map_flags,
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001088 };
1089 if (isAtLeastKernelVersion(4, 15, 0))
1090 strlcpy(req.map_name, mapNames[i].c_str(), sizeof(req.map_name));
Motomu Utsumi1a5cc5b2025-03-18 15:25:13 +09001091
Motomu Utsumi6f1cecc2025-03-19 19:49:08 +09001092 bool haveBtf = btf && isBtfSupported(type);
Maciej Żenczykowski9fef9302025-03-18 20:09:34 -07001093 if (haveBtf) {
Motomu Utsumi1a5cc5b2025-03-18 15:25:13 +09001094 uint32_t kTid, vTid;
1095 ret = getKeyValueTids(btf, mapNames[i].c_str(), md[i].key_size,
1096 md[i].value_size, &kTid, &vTid);
1097 if (ret) return ret;
1098 req.btf_fd = btf__fd(btf);
1099 req.btf_key_type_id = kTid;
1100 req.btf_value_type_id = vTid;
Motomu Utsumi1a5cc5b2025-03-18 15:25:13 +09001101 }
1102
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001103 fd.reset(bpf(BPF_MAP_CREATE, req));
1104 saved_errno = errno;
Maciej Żenczykowski8597e3a2024-08-28 15:42:01 -07001105 if (fd.ok()) {
Maciej Żenczykowski9fef9302025-03-18 20:09:34 -07001106 ALOGD("bpf_create_map[%s] btf:%d -> %d",
1107 mapNames[i].c_str(), haveBtf, fd.get());
Maciej Żenczykowski8597e3a2024-08-28 15:42:01 -07001108 } else {
Maciej Żenczykowski9fef9302025-03-18 20:09:34 -07001109 ALOGE("bpf_create_map[%s] btf:%d -> %d errno:%d",
1110 mapNames[i].c_str(), haveBtf, fd.get(), saved_errno);
Maciej Żenczykowski8597e3a2024-08-28 15:42:01 -07001111 }
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001112 }
1113
1114 if (!fd.ok()) return -saved_errno;
1115
1116 // When reusing a pinned map, we need to check the map type/sizes/etc match, but for
1117 // safety (since reuse code path is rare) run these checks even if we just created it.
1118 // We assume failure is due to pinned map mismatch, hence the 'NOT UNIQUE' return code.
1119 if (!mapMatchesExpectations(fd, mapNames[i], md[i], type)) return -ENOTUNIQ;
1120
Motomu Utsumi77b0b252025-07-15 13:36:20 +09001121 ret = pinMap(fd, mapNames[i], md[i], objName, mapPinLoc);
1122 if (ret) return ret;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001123
1124 mapFds.push_back(std::move(fd));
1125 }
1126
1127 return ret;
1128}
1129
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001130static void applyRelo(void* insnsPtr, Elf64_Addr offset, int fd) {
1131 int insnIndex;
1132 struct bpf_insn *insn, *insns;
1133
1134 insns = (struct bpf_insn*)(insnsPtr);
1135
1136 insnIndex = offset / sizeof(struct bpf_insn);
1137 insn = &insns[insnIndex];
1138
1139 // Occasionally might be useful for relocation debugging, but pretty spammy
1140 if (0) {
1141 ALOGV("applying relo to instruction at byte offset: %llu, "
1142 "insn offset %d, insn %llx",
1143 (unsigned long long)offset, insnIndex, *(unsigned long long*)insn);
1144 }
1145
1146 if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001147 ALOGE("invalid relo for insn %d: code 0x%x", insnIndex, insn->code);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001148 return;
1149 }
1150
1151 insn->imm = fd;
1152 insn->src_reg = BPF_PSEUDO_MAP_FD;
1153}
1154
1155static void applyMapRelo(ifstream& elfFile, vector<unique_fd> &mapFds, vector<codeSection>& cs) {
1156 vector<string> mapNames;
1157
Motomu Utsumie5dcaf72025-07-01 14:44:18 +09001158 int ret = readMapNames(elfFile, mapNames);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001159 if (ret) return;
1160
1161 for (int k = 0; k != (int)cs.size(); k++) {
1162 Elf64_Rel* rel = (Elf64_Rel*)(cs[k].rel_data.data());
1163 int n_rel = cs[k].rel_data.size() / sizeof(*rel);
1164
1165 for (int i = 0; i < n_rel; i++) {
1166 int symIndex = ELF64_R_SYM(rel[i].r_info);
1167 string symName;
1168
1169 ret = getSymNameByIdx(elfFile, symIndex, symName);
1170 if (ret) return;
1171
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +00001172 // Find the map fd and apply relo
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001173 for (int j = 0; j < (int)mapNames.size(); j++) {
1174 if (!mapNames[j].compare(symName)) {
1175 applyRelo(cs[k].data.data(), rel[i].r_offset, mapFds[j]);
1176 break;
1177 }
1178 }
1179 }
1180 }
1181}
1182
Motomu Utsumi5dbe6562025-07-23 12:45:40 +09001183static int pinProg(const borrowed_fd& fd, string& name, const struct bpf_prog_def& progDef,
Motomu Utsumi597f3af2025-07-16 14:23:46 +09001184 const string& objName, string& progPinLoc) {
1185 int ret;
1186 domain selinux_context = getDomainFromSelinuxContext(progDef.selinux_context);
1187 if (specified(selinux_context)) {
1188 ALOGV("prog %s selinux_context [%-32s] -> %d -> '%s' (%s)", name.c_str(),
1189 progDef.selinux_context, static_cast<int>(selinux_context),
1190 lookupSelinuxContext(selinux_context), lookupPinSubdir(selinux_context));
1191 string createLoc = string(BPF_FS_PATH) + lookupPinSubdir(selinux_context) +
1192 "tmp_prog_" + objName + '_' + string(name);
1193 ret = bpfFdPin(fd, createLoc.c_str());
1194 if (ret) {
1195 const int err = errno;
1196 ALOGE("create %s -> %d [%d:%s]", createLoc.c_str(), ret, err, strerror(err));
1197 return -err;
1198 }
1199 ret = renameat2(AT_FDCWD, createLoc.c_str(),
1200 AT_FDCWD, progPinLoc.c_str(), RENAME_NOREPLACE);
1201 if (ret) {
1202 const int err = errno;
1203 ALOGE("rename %s %s -> %d [%d:%s]", createLoc.c_str(), progPinLoc.c_str(), ret,
1204 err, strerror(err));
1205 return -err;
1206 }
1207 } else {
1208 ret = bpfFdPin(fd, progPinLoc.c_str());
1209 if (ret) {
1210 const int err = errno;
1211 ALOGE("create %s -> %d [%d:%s]", progPinLoc.c_str(), ret, err, strerror(err));
1212 return -err;
1213 }
1214 }
1215 if (chmod(progPinLoc.c_str(), 0440)) {
1216 const int err = errno;
1217 ALOGE("chmod %s 0440 -> [%d:%s]", progPinLoc.c_str(), err, strerror(err));
1218 return -err;
1219 }
1220 if (chown(progPinLoc.c_str(), (uid_t)progDef.uid,
1221 (gid_t)progDef.gid)) {
1222 const int err = errno;
1223 ALOGE("chown %s %d %d -> [%d:%s]", progPinLoc.c_str(), progDef.uid,
1224 progDef.gid, err, strerror(err));
1225 return -err;
1226 }
1227 return 0;
1228}
1229
Motomu Utsumi52a3ba72025-07-25 10:41:53 +09001230static int validateProg(const borrowed_fd& fd, string& progPinLoc,
1231 const unsigned int bpfloader_ver) {
Motomu Utsumi1d25bb32025-07-16 14:26:59 +09001232 if (!isAtLeastKernelVersion(4, 14, 0)) {
1233 return 0;
1234 }
1235 int progId = bpfGetFdProgId(fd);
1236 if (progId == -1) {
1237 const int err = errno;
1238 ALOGE("bpfGetFdProgId failed, errno: %d", err);
1239 return -err;
1240 }
1241
1242 int jitLen = bpfGetFdJitProgLen(fd);
1243 if (jitLen == -1) {
1244 const int err = errno;
1245 ALOGE("bpfGetFdJitProgLen failed, ret: %d", err);
1246 return -err;
1247 }
1248
1249 int xlatLen = bpfGetFdXlatProgLen(fd);
1250 if (xlatLen == -1) {
1251 const int err = errno;
1252 ALOGE("bpfGetFdXlatProgLen failed, ret: %d", err);
1253 return -err;
1254 }
1255 ALOGI("prog %s id %d len jit:%d xlat:%d", progPinLoc.c_str(), progId, jitLen, xlatLen);
1256
1257 if (!jitLen && bpfloader_ver >= BPFLOADER_MAINLINE_25Q2_VERSION) {
1258 ALOGE("Kernel eBPF JIT failure for %s", progPinLoc.c_str());
1259 return -ENOTSUP;
1260 }
1261 return 0;
1262}
1263
Maciej Żenczykowski1fdc3032025-07-28 16:18:20 -07001264static string buildProgPinLoc(const char pin_subdir[BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE],
1265 const string& objName, const string& name) {
1266 validatePinDir(pin_subdir);
1267 // Format of pin location is /sys/fs/bpf/<prefix>prog_<objName>_<progName>
1268 return string(BPF_FS_PATH) + pin_subdir + "prog_" + objName + '_' + string(name);
Motomu Utsumif8977e92025-07-24 13:57:35 +09001269}
1270
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001271static int loadCodeSections(const char* elfPath, vector<codeSection>& cs, const string& license,
Maciej Żenczykowski05da6b02025-07-28 15:34:43 -07001272 const unsigned int bpfloader_ver) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001273 unsigned kvers = kernelVersion();
1274
1275 if (!kvers) {
1276 ALOGE("unable to get kernel version");
1277 return -EINVAL;
1278 }
1279
1280 string objName = pathToObjName(string(elfPath));
1281
1282 for (int i = 0; i < (int)cs.size(); i++) {
1283 unique_fd& fd = cs[i].prog_fd;
1284 int ret;
1285 string name = cs[i].name;
1286
1287 if (!cs[i].prog_def.has_value()) {
1288 ALOGE("[%d] '%s' missing program definition! bad bpf.o build?", i, name.c_str());
1289 return -EINVAL;
1290 }
1291
1292 unsigned min_kver = cs[i].prog_def->min_kver;
1293 unsigned max_kver = cs[i].prog_def->max_kver;
1294 ALOGD("cs[%d].name:%s min_kver:%x .max_kver:%x (kvers:%x)", i, name.c_str(), min_kver,
1295 max_kver, kvers);
1296 if (kvers < min_kver) continue;
1297 if (kvers >= max_kver) continue;
1298
1299 unsigned bpfMinVer = cs[i].prog_def->bpfloader_min_ver;
1300 unsigned bpfMaxVer = cs[i].prog_def->bpfloader_max_ver;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001301
1302 ALOGD("cs[%d].name:%s requires bpfloader version [0x%05x,0x%05x)", i, name.c_str(),
1303 bpfMinVer, bpfMaxVer);
1304 if (bpfloader_ver < bpfMinVer) continue;
1305 if (bpfloader_ver >= bpfMaxVer) continue;
1306
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001307 // strip any potential $foo suffix
1308 // this can be used to provide duplicate programs
1309 // conditionally loaded based on running kernel version
1310 name = name.substr(0, name.find_last_of('$'));
1311
1312 bool reuse = false;
Maciej Żenczykowski1fdc3032025-07-28 16:18:20 -07001313 string progPinLoc = buildProgPinLoc(cs[i].prog_def->pin_subdir, objName, name);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001314 if (access(progPinLoc.c_str(), F_OK) == 0) {
1315 fd.reset(retrieveProgram(progPinLoc.c_str()));
1316 ALOGD("New bpf prog load reusing prog %s, ret: %d (%s)", progPinLoc.c_str(), fd.get(),
Maciej Żenczykowski37ba9392025-02-13 16:00:26 -08001317 !fd.ok() ? std::strerror(errno) : "ok");
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001318 reuse = true;
1319 } else {
Maciej Żenczykowskiea1d8f62024-09-05 09:38:14 -07001320 static char log_buf[1 << 20]; // 1 MiB logging buffer
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001321
1322 union bpf_attr req = {
1323 .prog_type = cs[i].type,
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001324 .insn_cnt = static_cast<__u32>(cs[i].data.size() / sizeof(struct bpf_insn)),
Maciej Żenczykowski52be6a82024-08-26 17:18:39 -07001325 .insns = ptr_to_u64(cs[i].data.data()),
1326 .license = ptr_to_u64(license.c_str()),
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001327 .log_level = 1,
Maciej Żenczykowskiea1d8f62024-09-05 09:38:14 -07001328 .log_size = sizeof(log_buf),
1329 .log_buf = ptr_to_u64(log_buf),
Maciej Żenczykowski346831c2024-08-12 17:49:10 +00001330 .expected_attach_type = cs[i].attach_type,
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001331 };
1332 if (isAtLeastKernelVersion(4, 15, 0))
1333 strlcpy(req.prog_name, cs[i].name.c_str(), sizeof(req.prog_name));
1334 fd.reset(bpf(BPF_PROG_LOAD, req));
1335
Maciej Żenczykowskiea1d8f62024-09-05 09:38:14 -07001336 // Kernel should have NULL terminated the log buffer, but force it anyway for safety
1337 log_buf[sizeof(log_buf) - 1] = 0;
1338
1339 // Strip out final newline if present
1340 int log_chars = strlen(log_buf);
1341 if (log_chars && log_buf[log_chars - 1] == '\n') log_buf[--log_chars] = 0;
1342
1343 bool log_oneline = !strchr(log_buf, '\n');
1344
1345 ALOGD("BPF_PROG_LOAD call for %s (%s) returned '%s' fd: %d (%s)", elfPath,
1346 cs[i].name.c_str(), log_oneline ? log_buf : "{multiline}",
Maciej Żenczykowski37ba9392025-02-13 16:00:26 -08001347 fd.get(), !fd.ok() ? std::strerror(errno) : "ok");
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001348
1349 if (!fd.ok()) {
Maciej Żenczykowskiea1d8f62024-09-05 09:38:14 -07001350 // kernel NULL terminates log_buf, so this checks for non-empty string
Maciej Żenczykowskif1259922025-07-15 14:57:23 -07001351 if (log_buf[0] && !isUser()) {
Maciej Żenczykowskiea1d8f62024-09-05 09:38:14 -07001352 vector<string> lines = Split(log_buf, "\n");
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001353
Maciej Żenczykowski9f8b17e2024-08-29 12:07:35 -07001354 ALOGW("BPF_PROG_LOAD - BEGIN log_buf contents:");
1355 for (const auto& line : lines) ALOGW("%s", line.c_str());
1356 ALOGW("BPF_PROG_LOAD - END log_buf contents.");
1357 }
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001358
1359 if (cs[i].prog_def->optional) {
Maciej Żenczykowskibfc0b612024-08-28 17:45:25 -07001360 ALOGW("failed program %s is marked optional - continuing...",
1361 cs[i].name.c_str());
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001362 continue;
1363 }
Maciej Żenczykowskibfc0b612024-08-28 17:45:25 -07001364 ALOGE("non-optional program %s failed to load.", cs[i].name.c_str());
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001365 }
1366 }
1367
1368 if (!fd.ok()) return fd.get();
1369
1370 if (!reuse) {
Motomu Utsumi597f3af2025-07-16 14:23:46 +09001371 ret = pinProg(fd, name, cs[i].prog_def.value(), objName, progPinLoc);
1372 if (ret) return ret;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001373 }
Motomu Utsumi1d25bb32025-07-16 14:26:59 +09001374 ret = validateProg(fd, progPinLoc, bpfloader_ver);
1375 if (ret) return ret;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001376 }
1377
1378 return 0;
1379}
1380
Motomu Utsumidbada872025-07-23 12:50:34 +09001381static int prepareLoadMaps(const struct bpf_object* obj, const vector<struct bpf_map_def>& md,
1382 const vector<string>& mapNames, const unsigned int bpfloader_ver) {
Motomu Utsumif1b1afb2025-07-23 12:10:52 +09001383 unsigned kvers = kernelVersion();
1384
1385 for (int i = 0; i < (int)mapNames.size(); i++) {
1386 struct bpf_map* m = bpf_object__find_map_by_name(obj, mapNames[i].c_str());
1387 if (!m) {
1388 ALOGE("bpf_object does not contain map: %s", mapNames[i].c_str());
1389 return -1;
1390 }
1391
1392 if (bpfloader_ver < md[i].bpfloader_min_ver || bpfloader_ver >= md[i].bpfloader_max_ver) {
1393 ALOGD("skipping map %s: bpfloader 0x%05x is outside required range [0x%05x, 0x%05x)",
1394 mapNames[i].c_str(), bpfloader_ver,
1395 md[i].bpfloader_min_ver, md[i].bpfloader_max_ver);
1396 bpf_map__set_autocreate(m, false);
1397 continue;
1398 }
1399
1400 if (kvers < md[i].min_kver || kvers >= md[i].max_kver) {
1401 ALOGD("skipping map %s: kernel version 0x%x is outside required range [0x%x, 0x%x)",
1402 mapNames[i].c_str(), kvers, md[i].min_kver, md[i].max_kver);
1403 bpf_map__set_autocreate(m, false);
1404 continue;
1405 }
1406
1407 if (!isMapTypeSupported(md[i].type)) {
1408 ALOGD("skipping unsupported map type(%d): %s", md[i].type, mapNames[i].c_str());
1409 bpf_map__set_autocreate(m, false);
1410 continue;
1411 }
1412
1413 bpf_map__set_type(m, sanitizeMapType(md[i].type));
1414 bpf_map__set_map_flags(m, md[i].map_flags);
1415 }
1416 return 0;
1417}
1418
Motomu Utsumidbada872025-07-23 12:50:34 +09001419static int prepareLoadProgs(const struct bpf_object* obj, const vector<codeSection>& cs,
1420 const unsigned int bpfloader_ver) {
Motomu Utsumi3cdcc472025-07-23 12:22:14 +09001421 unsigned kvers = kernelVersion();
1422
1423 for (int i = 0; i < (int)cs.size(); i++) {
1424 string name = cs[i].name;
1425 if (!cs[i].prog_def.has_value()) {
1426 ALOGE("[%d] '%s' missing program definition! bad bpf.o build?", i, name.c_str());
1427 return -EINVAL;
1428 }
1429 string program_name = cs[i].program_name;
1430 struct bpf_program* prog = bpf_object__find_program_by_name(obj, program_name.c_str());
1431 if (!prog) {
1432 ALOGE("bpf_object does not contain program: %s", cs[i].program_name.c_str());
1433 return -1;
1434 }
1435
1436 unsigned min_kver = cs[i].prog_def->min_kver;
1437 unsigned max_kver = cs[i].prog_def->max_kver;
1438 if (kvers < min_kver || kvers >= max_kver) {
1439 ALOGD("skipping prog %s: kernel version 0x%x is outside required range [0x%x, 0x%x)",
1440 name.c_str(), kvers, min_kver, max_kver);
1441 bpf_program__set_autoload(prog, false);
1442 continue;
1443 }
1444
1445 unsigned bpfMinVer = cs[i].prog_def->bpfloader_min_ver;
1446 unsigned bpfMaxVer = cs[i].prog_def->bpfloader_max_ver;
1447 if (bpfloader_ver < bpfMinVer || bpfloader_ver >= bpfMaxVer) {
1448 ALOGD("skipping prog %s: bpfloader 0x%05x is outside required range [0x%05x, 0x%05x)",
1449 name.c_str(), bpfloader_ver, bpfMinVer, bpfMaxVer);
1450 bpf_program__set_autoload(prog, false);
1451 continue;
1452 }
1453
1454 if (cs[i].prog_def->optional) {
1455 // TODO: Support optional program
1456 ALOGE("Optional program cannot be loaded by libbpf");
1457 return -1;
1458 }
1459
1460 bpf_program__set_type(prog, cs[i].type);
1461 bpf_program__set_expected_attach_type(prog, cs[i].attach_type);
1462 }
1463 return 0;
1464}
1465
Motomu Utsumidbada872025-07-23 12:50:34 +09001466static int pinMaps(const char* const elfPath, const struct bpf_object* obj,
Maciej Żenczykowski7ce493d2025-07-28 15:13:07 -07001467 const vector<struct bpf_map_def>& md, const vector<string>& mapNames) {
Motomu Utsumicd5bdf52025-07-23 12:32:08 +09001468 int ret;
1469 string objName = pathToObjName(string(elfPath));
1470
1471 for (int i = 0; i < (int)mapNames.size(); i++) {
1472 struct bpf_map* m = bpf_object__find_map_by_name(obj, mapNames[i].c_str());
1473 if (!m) {
1474 ALOGE("bpf_object does not contain map: %s", mapNames[i].c_str());
1475 return -1;
1476 }
1477 // This map was skipped
1478 if (!bpf_map__autocreate(m)) continue;
1479
Maciej Żenczykowski1fdc3032025-07-28 16:18:20 -07001480 string mapPinLoc = buildMapPinLoc(md[i].pin_subdir, objName, mapNames[i]);
Motomu Utsumicd5bdf52025-07-23 12:32:08 +09001481 if (access(mapPinLoc.c_str(), F_OK) == 0) {
1482 ALOGE("Reusing map is not supported: %s", mapNames[i].c_str());
1483 return -1;
1484 }
1485
1486 ret = pinMap(bpf_map__fd(m), mapNames[i], md[i], objName, mapPinLoc);
1487 if (ret) return ret;
1488 }
1489 return 0;
1490}
1491
Motomu Utsumidbada872025-07-23 12:50:34 +09001492static int pinProgs(const char* const elfPath, const struct bpf_object * obj,
Maciej Żenczykowskif8e88362025-07-28 15:33:00 -07001493 const vector<codeSection>& cs, const unsigned int bpfloader_ver) {
Motomu Utsumi5dbe6562025-07-23 12:45:40 +09001494 int ret;
1495 string objName = pathToObjName(string(elfPath));
1496
1497 for (int i = 0; i < (int)cs.size(); i++) {
1498 string program_name = cs[i].program_name;
1499 struct bpf_program* prog = bpf_object__find_program_by_name(obj, program_name.c_str());
1500 if (!prog) {
1501 ALOGE("bpf_object does not contain program: %s", program_name.c_str());
1502 return -1;
1503 }
1504 // This program was skipped
1505 if (!bpf_program__autoload(prog)) continue;
1506
1507 string name = cs[i].name;
1508 name = name.substr(0, name.find_last_of('$'));
Maciej Żenczykowski1fdc3032025-07-28 16:18:20 -07001509 string progPinLoc = buildProgPinLoc(cs[i].prog_def->pin_subdir, objName, name);
Motomu Utsumi5dbe6562025-07-23 12:45:40 +09001510 if (access(progPinLoc.c_str(), F_OK) == 0) {
1511 // TODO: Skip loading lower priority program
1512 ALOGI("Higher priority program is already pinned, skip pinning %s", cs[i].name.c_str());
1513 continue;
1514 }
1515
1516 int fd = bpf_program__fd(prog);
1517 ret = pinProg(fd, name, cs[i].prog_def.value(), objName, progPinLoc);
1518 if (ret) return ret;
1519 ret = validateProg(fd, progPinLoc, bpfloader_ver);
1520 if (ret) return ret;
1521 }
1522 return 0;
1523}
1524
Maciej Żenczykowskiff3b4182025-07-28 15:02:07 -07001525static int loadProgByLibbpf(const char* const elfPath, const unsigned int bpfloader_ver) {
Motomu Utsumidbada872025-07-23 12:50:34 +09001526 int ret;
1527 vector<string> mapNames;
1528 vector<struct bpf_map_def> md;
1529 vector<codeSection> cs;
1530
1531 ifstream elfFile(elfPath, ios::in | ios::binary);
1532 if (!elfFile.is_open()) return -1;
1533
1534 LIBBPF_OPTS(bpf_object_open_opts, opts,
1535 .bpf_token_path = "",
1536 );
1537 struct bpf_object* obj = bpf_object__open_file(elfPath, &opts);
1538 if (!obj) return -1;
1539 auto objGuard = base::make_scope_guard([&obj] { bpf_object__close(obj); });
1540
1541 ret = readSectionByName(".android_maps", elfFile, md);
1542 if (ret) return ret;
1543
1544 ret = readMapNames(elfFile, mapNames);
1545 if (ret) return ret;
1546
1547 ret = prepareLoadMaps(obj, md, mapNames, bpfloader_ver);
1548 if (ret) return ret;
1549
1550 ret = readCodeSections(elfFile, cs);
1551 if (ret && ret != -ENOENT) return ret;
1552
1553 ret = prepareLoadProgs(obj, cs, bpfloader_ver);
1554 if (ret) return ret;
1555
1556 ret = bpf_object__load(obj);
1557 if (ret) return ret;
1558
Maciej Żenczykowski7ce493d2025-07-28 15:13:07 -07001559 ret = pinMaps(elfPath, obj, md, mapNames);
Motomu Utsumidbada872025-07-23 12:50:34 +09001560 if (ret) return ret;
1561
Maciej Żenczykowskif8e88362025-07-28 15:33:00 -07001562 ret = pinProgs(elfPath, obj, cs, bpfloader_ver);
Motomu Utsumidbada872025-07-23 12:50:34 +09001563 if (ret) return ret;
1564
1565 return 0;
1566}
1567
Maciej Żenczykowski12751c52025-07-28 15:06:10 -07001568int loadProg(const char* const elfPath, const unsigned int bpfloader_ver) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001569 vector<char> license;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001570 vector<codeSection> cs;
1571 vector<unique_fd> mapFds;
1572 int ret;
1573
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001574 ifstream elfFile(elfPath, ios::in | ios::binary);
1575 if (!elfFile.is_open()) return -1;
1576
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001577 ret = readSectionByName("license", elfFile, license);
1578 if (ret) {
1579 ALOGE("Couldn't find license in %s", elfPath);
1580 return ret;
1581 } else {
Maciej Żenczykowski3a085152024-09-18 23:45:52 +00001582 ALOGD("Loading ELF object %s with license %s",
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001583 elfPath, (char*)license.data());
1584 }
1585
Maciej Żenczykowskic4a1cae2025-07-15 14:23:51 -07001586 ALOGD("BpfLoader ver 0x%05x processing ELF object %s", bpfloader_ver, elfPath);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001587
Maciej Żenczykowski960c3372025-07-28 15:09:22 -07001588 ret = createMaps(elfPath, elfFile, mapFds, bpfloader_ver);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001589 if (ret) {
1590 ALOGE("Failed to create maps: (ret=%d) in %s", ret, elfPath);
1591 return ret;
1592 }
1593
1594 for (int i = 0; i < (int)mapFds.size(); i++)
1595 ALOGV("map_fd found at %d is %d in %s", i, mapFds[i].get(), elfPath);
1596
Maciej Żenczykowski1e487172024-09-05 09:27:35 -07001597 ret = readCodeSections(elfFile, cs);
Maciej Żenczykowski66893bf2025-05-06 02:59:22 -07001598 if (ret == -ENOENT) return 0;
Maciej Żenczykowski1e487172024-09-05 09:27:35 -07001599 if (ret) {
1600 ALOGE("Couldn't read all code sections in %s", elfPath);
1601 return ret;
1602 }
1603
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001604 applyMapRelo(elfFile, mapFds, cs);
1605
Maciej Żenczykowski05da6b02025-07-28 15:34:43 -07001606 ret = loadCodeSections(elfPath, cs, string(license.data()), bpfloader_ver);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001607 if (ret) ALOGE("Failed to load programs, loadCodeSections ret=%d", ret);
1608
1609 return ret;
1610}
1611
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07001612static bool exists(const char* const path) {
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001613 int v = access(path, F_OK);
Maciej Żenczykowski731acfe2024-04-30 10:09:57 +00001614 if (!v) return true;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001615 if (errno == ENOENT) return false;
1616 ALOGE("FATAL: access(%s, F_OK) -> %d [%d:%s]", path, v, errno, strerror(errno));
1617 abort(); // can only hit this if permissions (likely selinux) are screwed up
1618}
1619
Maciej Żenczykowski78fa8612024-08-26 17:22:25 -07001620#define APEXROOT "/apex/com.android.tethering"
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001621#define BPFROOT APEXROOT "/etc/bpf/mainline/"
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001622
Maciej Żenczykowski58464e62025-07-28 14:58:56 -07001623static int loadObject(const unsigned int bpfloader_ver,
Motomu Utsumie50e55d2025-07-23 12:59:23 +09001624 const char* const fname, const bool useLibbpf = false) {
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001625 string progPath = string(BPFROOT) + fname;
Maciej Żenczykowskiff3b4182025-07-28 15:02:07 -07001626 int ret = useLibbpf ? loadProgByLibbpf(progPath.c_str(), bpfloader_ver) :
Maciej Żenczykowski12751c52025-07-28 15:06:10 -07001627 loadProg(progPath.c_str(), bpfloader_ver);
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001628 if (ret) {
Motomu Utsumie50e55d2025-07-23 12:59:23 +09001629 ALOGE("Failed to load object: %s, ret: %s, libbpf: %d",
1630 progPath.c_str(), std::strerror(-ret), useLibbpf);
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001631 return 1;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001632 }
Motomu Utsumie50e55d2025-07-23 12:59:23 +09001633 ALOGD("Loaded object: %s, libbpf: %d", progPath.c_str(), useLibbpf);
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001634 return 0;
1635}
1636
1637static int loadAllObjects(const unsigned int bpfloader_ver) {
1638 // S+ Tethering mainline module (network_stack): tether offload
1639 // loads under /sys/fs/bpf/tethering:
Maciej Żenczykowski58464e62025-07-28 14:58:56 -07001640 if (loadObject(bpfloader_ver, "offload.o")) return 1;
Motomu Utsumi4efd5442025-07-29 19:37:13 +09001641 if (loadObject(bpfloader_ver, "test.o", isAtLeast25Q3)) return 1;
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001642 if (isAtLeastT) {
1643 // T+ Tethering mainline module loads under:
1644 // /sys/fs/bpf/net_shared: shared with netd & system server
Motomu Utsumif5044b72025-07-29 19:43:40 +09001645 if (loadObject(bpfloader_ver, "clatd.o", isAtLeast25Q3)) return 1;
Maciej Żenczykowski58464e62025-07-28 14:58:56 -07001646 if (loadObject(bpfloader_ver, "dscpPolicy.o")) return 1;
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001647
1648 // /sys/fs/bpf/netd_shared: shared with netd & system server
1649 // - netutils_wrapper (for iptables xt_bpf) has access to programs
1650
1651 // WARNING: Android T+ non-updatable netd depends on both of the
1652 // 'netd_shared' & 'netd' strings for xt_bpf programs it loads
Maciej Żenczykowski58464e62025-07-28 14:58:56 -07001653 if (loadObject(bpfloader_ver, "netd.o")) return 1;
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001654
1655 // /sys/fs/bpf/netd_readonly: shared with netd & system server
1656 // - netutils_wrapper has no access, netd has read only access
1657
1658 // /sys/fs/bpf/net_private: not shared, just network_stack
1659 }
1660 return 0;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001661}
1662
Maciej Żenczykowski57de4bf2025-05-12 17:52:04 -07001663static int createDir(const char* const dir) {
1664 mode_t prevUmask = umask(0);
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001665
Maciej Żenczykowski57de4bf2025-05-12 17:52:04 -07001666 errno = 0;
1667 int ret = mkdir(dir, S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
1668 if (ret && errno != EEXIST) {
1669 const int err = errno;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001670 umask(prevUmask);
Maciej Żenczykowski57de4bf2025-05-12 17:52:04 -07001671 ALOGE("Failed to create directory: %s, ret: %s", dir, std::strerror(err));
1672 return -err;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001673 }
Maciej Żenczykowski57de4bf2025-05-12 17:52:04 -07001674
1675 umask(prevUmask);
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001676 return 0;
1677}
1678
1679// Technically 'value' doesn't need to be newline terminated, but it's best
1680// to include a newline to match 'echo "value" > /proc/sys/...foo' behaviour,
1681// which is usually how kernel devs test the actual sysctl interfaces.
Maciej Żenczykowskic9b0a832025-07-22 12:30:32 -07001682static int writeFile(const char *filename, const char *value) {
Maciej Żenczykowski8a767282024-09-04 10:56:55 -07001683 unique_fd fd(open(filename, O_WRONLY | O_CLOEXEC));
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001684 if (fd < 0) {
1685 const int err = errno;
1686 ALOGE("open('%s', O_WRONLY | O_CLOEXEC) -> %s", filename, strerror(err));
1687 return -err;
1688 }
1689 int len = strlen(value);
1690 int v = write(fd, value, len);
1691 if (v < 0) {
1692 const int err = errno;
1693 ALOGE("write('%s', '%s', %d) -> %s", filename, value, len, strerror(err));
1694 return -err;
1695 }
1696 if (v != len) {
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001697 ALOGE("write('%s', '%s', %d) -> short write [%d]", filename, value, len, v);
1698 return -EINVAL;
1699 }
1700 return 0;
1701}
1702
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -08001703#define APEX_MOUNT_POINT "/apex/com.android.tethering"
Maciej Żenczykowski2fe2db52024-02-07 01:23:58 +00001704const char * const platformBpfLoader = "/system/bin/bpfloader";
Yu-Ting Tseng9b15fa02024-10-28 11:16:35 -07001705const char *const uprobestatsBpfLoader =
1706 "/apex/com.android.uprobestats/bin/uprobestatsbpfload";
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -08001707
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07001708static int logTetheringApexVersion(void) {
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -08001709 char * found_blockdev = NULL;
1710 FILE * f = NULL;
1711 char buf[4096];
1712
1713 f = fopen("/proc/mounts", "re");
1714 if (!f) return 1;
1715
1716 // /proc/mounts format: block_device [space] mount_point [space] other stuff... newline
1717 while (fgets(buf, sizeof(buf), f)) {
1718 char * blockdev = buf;
1719 char * space = strchr(blockdev, ' ');
1720 if (!space) continue;
1721 *space = '\0';
1722 char * mntpath = space + 1;
1723 space = strchr(mntpath, ' ');
1724 if (!space) continue;
1725 *space = '\0';
1726 if (strcmp(mntpath, APEX_MOUNT_POINT)) continue;
1727 found_blockdev = strdup(blockdev);
1728 break;
1729 }
1730 fclose(f);
1731 f = NULL;
1732
1733 if (!found_blockdev) return 2;
Maciej Żenczykowski5c057ed2024-04-30 11:59:13 +00001734 ALOGV("Found Tethering Apex mounted from blockdev %s", found_blockdev);
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -08001735
1736 f = fopen("/proc/mounts", "re");
1737 if (!f) { free(found_blockdev); return 3; }
1738
1739 while (fgets(buf, sizeof(buf), f)) {
1740 char * blockdev = buf;
1741 char * space = strchr(blockdev, ' ');
1742 if (!space) continue;
1743 *space = '\0';
1744 char * mntpath = space + 1;
1745 space = strchr(mntpath, ' ');
1746 if (!space) continue;
1747 *space = '\0';
1748 if (strcmp(blockdev, found_blockdev)) continue;
1749 if (strncmp(mntpath, APEX_MOUNT_POINT "@", strlen(APEX_MOUNT_POINT "@"))) continue;
1750 char * at = strchr(mntpath, '@');
1751 if (!at) continue;
1752 char * ver = at + 1;
1753 ALOGI("Tethering APEX version %s", ver);
1754 }
1755 fclose(f);
1756 free(found_blockdev);
1757 return 0;
1758}
Maciej Żenczykowski2fe2db52024-02-07 01:23:58 +00001759
Maciej Żenczykowski68eab892024-05-24 03:17:59 -07001760static bool hasGSM() {
Maciej Żenczykowski8a767282024-09-04 10:56:55 -07001761 static string ph = GetProperty("gsm.current.phone-type", "");
Maciej Żenczykowski68eab892024-05-24 03:17:59 -07001762 static bool gsm = (ph != "");
1763 static bool logged = false;
1764 if (!logged) {
1765 logged = true;
1766 ALOGI("hasGSM(gsm.current.phone-type='%s'): %s", ph.c_str(), gsm ? "true" : "false");
1767 }
1768 return gsm;
1769}
1770
1771static bool isTV() {
1772 if (hasGSM()) return false; // TVs don't do GSM
1773
Maciej Żenczykowski8a767282024-09-04 10:56:55 -07001774 static string key = GetProperty("ro.oem.key1", "");
Maciej Żenczykowski68eab892024-05-24 03:17:59 -07001775 static bool tv = StartsWith(key, "ATV00");
1776 static bool logged = false;
1777 if (!logged) {
1778 logged = true;
1779 ALOGI("isTV(ro.oem.key1='%s'): %s.", key.c_str(), tv ? "true" : "false");
1780 }
1781 return tv;
1782}
1783
Maciej Żenczykowski6e6b2092024-06-24 23:57:41 +00001784static bool isWear() {
Maciej Żenczykowski8a767282024-09-04 10:56:55 -07001785 static string wearSdkStr = GetProperty("ro.cw_build.wear_sdk.version", "");
1786 static int wearSdkInt = GetIntProperty("ro.cw_build.wear_sdk.version", 0);
1787 static string buildChars = GetProperty("ro.build.characteristics", "");
1788 static vector<string> v = Tokenize(buildChars, ",");
Maciej Żenczykowski6e6b2092024-06-24 23:57:41 +00001789 static bool watch = (std::find(v.begin(), v.end(), "watch") != v.end());
1790 static bool wear = (wearSdkInt > 0) || watch;
1791 static bool logged = false;
1792 if (!logged) {
1793 logged = true;
1794 ALOGI("isWear(ro.cw_build.wear_sdk.version=%d[%s] ro.build.characteristics='%s'): %s",
1795 wearSdkInt, wearSdkStr.c_str(), buildChars.c_str(), wear ? "true" : "false");
1796 }
1797 return wear;
1798}
1799
Motomu Utsumi712088d2025-03-18 14:52:02 +09001800static int libbpfPrint(enum libbpf_print_level lvl, const char *const formatStr,
1801 va_list argList) {
Motomu Utsumia84eb0b2025-07-23 13:26:19 +09001802#ifndef NETBPFLOAD_VERBOSE_LOG
1803 if (lvl != LIBBPF_WARN) return 0;
1804#endif
Motomu Utsumi712088d2025-03-18 14:52:02 +09001805 int32_t prio;
1806 switch (lvl) {
1807 case LIBBPF_WARN:
1808 prio = ANDROID_LOG_WARN;
1809 break;
1810 case LIBBPF_INFO:
1811 prio = ANDROID_LOG_INFO;
1812 break;
1813 case LIBBPF_DEBUG:
1814 prio = ANDROID_LOG_DEBUG;
1815 break;
1816 }
Motomu Utsumi47fcb862025-07-28 11:19:54 +09001817 if (!formatStr) {
1818 LOG_PRI(prio, LOG_TAG, "libbpf (null format string)");
1819 return 0;
1820 }
1821
1822 // Print each line to avoid being truncated.
1823 char *s = NULL;
1824 int ret = vasprintf(&s, formatStr, argList);
1825 if (ret == -1) {
1826 LOG_PRI(prio, LOG_TAG, "libbpf (format failure)");
1827 return 0;
1828 }
Motomu Utsumi712088d2025-03-18 14:52:02 +09001829 int len = strlen(s);
1830 if (len && s[len - 1] == '\n')
1831 s[len - 1] = 0;
Motomu Utsumi47fcb862025-07-28 11:19:54 +09001832 vector<string> lines = Split(s, "\n");
1833 for (const auto& line : lines) LOG_PRI(prio, LOG_TAG, "%s", line.c_str());
Motomu Utsumi712088d2025-03-18 14:52:02 +09001834 free(s);
1835 return 0;
1836}
1837
Maciej Żenczykowski6d151ef2024-04-30 23:55:57 -07001838static int doLoad(char** argv, char * const envp[]) {
Maciej Żenczykowski0b477492025-03-04 22:12:42 -08001839 if (!isAtLeastS) {
1840 ALOGE("Impossible - not reachable on Android <S.");
1841 // for safety, we don't fail, this is a just-in-case workaround
1842 // for any possible busted 'optimized' start everything vendor init hacks on R
1843 return 0;
1844 }
Motomu Utsumi712088d2025-03-18 14:52:02 +09001845 libbpf_set_print(libbpfPrint);
Maciej Żenczykowski0b477492025-03-04 22:12:42 -08001846
Maciej Żenczykowski15f97312024-06-13 14:11:28 -07001847 const bool runningAsRoot = !getuid(); // true iff U QPR3 or V+
Maciej Żenczykowski7b95d992024-06-13 18:18:11 -07001848
Maciej Żenczykowskidb9171f2025-01-14 16:22:46 -08001849 const int first_api_level = GetIntProperty("ro.board.first_api_level", api_level);
Maciej Żenczykowski1c2187a2024-09-03 16:03:45 -07001850
Maciej Żenczykowski03ef12c2024-02-10 21:34:22 +00001851 // last in U QPR2 beta1
1852 const bool has_platform_bpfloader_rc = exists("/system/etc/init/bpfloader.rc");
1853 // first in U QPR2 beta~2
1854 const bool has_platform_netbpfload_rc = exists("/system/etc/init/netbpfload.rc");
1855
Maciej Żenczykowski62956142024-06-13 15:32:57 -07001856 // Version of Network BpfLoader depends on the Android OS version
Maciej Żenczykowski8c097782025-03-04 13:11:56 -08001857 unsigned int bpfloader_ver = BPFLOADER_MAINLINE_S_VERSION; // [42u]
Maciej Żenczykowski1a3b54f2024-06-13 15:35:46 -07001858 if (isAtLeastT) ++bpfloader_ver; // [43] BPFLOADER_MAINLINE_T_VERSION
1859 if (isAtLeastU) ++bpfloader_ver; // [44] BPFLOADER_MAINLINE_U_VERSION
1860 if (runningAsRoot) ++bpfloader_ver; // [45] BPFLOADER_MAINLINE_U_QPR3_VERSION
1861 if (isAtLeastV) ++bpfloader_ver; // [46] BPFLOADER_MAINLINE_V_VERSION
Maciej Żenczykowski98975122025-01-14 14:57:24 -08001862 if (isAtLeast25Q2) ++bpfloader_ver; // [47] BPFLOADER_MAINLINE_25Q2_VERSION
Maciej Żenczykowskic5b9f5e2025-05-06 02:35:27 -07001863 if (isAtLeast25Q3) ++bpfloader_ver; // [48] BPFLOADER_MAINLINE_25Q3_VERSION
1864 if (isAtLeast25Q4) ++bpfloader_ver; // [49] BPFLOADER_MAINLINE_25Q4_VERSION
1865 if (isAtLeast26Q1) ++bpfloader_ver; // [50] BPFLOADER_MAINLINE_26Q1_VERSION
1866 if (isAtLeast26Q2) ++bpfloader_ver; // [51] BPFLOADER_MAINLINE_26Q2_VERSION
Maciej Żenczykowski62956142024-06-13 15:32:57 -07001867
Motomu Utsumia7693582025-02-05 17:40:08 +09001868 ALOGI("NetBpfLoad v0.%u (%s) api:%d/%d kver:%07x (%s) libbpf: v%u.%u "
1869 "uid:%d rc:%d%d",
Maciej Żenczykowskidb9171f2025-01-14 16:22:46 -08001870 bpfloader_ver, argv[0], android_get_device_api_level(), api_level,
Motomu Utsumia7693582025-02-05 17:40:08 +09001871 kernelVersion(), describeArch(), libbpf_major_version(),
1872 libbpf_minor_version(), getuid(), has_platform_bpfloader_rc,
1873 has_platform_netbpfload_rc);
Maciej Żenczykowski041be522023-10-23 23:34:52 -07001874
Maciej Żenczykowski03ef12c2024-02-10 21:34:22 +00001875 if (!has_platform_bpfloader_rc && !has_platform_netbpfload_rc) {
1876 ALOGE("Unable to find platform's bpfloader & netbpfload init scripts.");
1877 return 1;
1878 }
1879
1880 if (has_platform_bpfloader_rc && has_platform_netbpfload_rc) {
1881 ALOGE("Platform has *both* bpfloader & netbpfload init scripts.");
1882 return 1;
1883 }
1884
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -08001885 logTetheringApexVersion();
1886
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +00001887 // both S and T require kernel 4.9 (and eBpf support)
Maciej Żenczykowski0b477492025-03-04 22:12:42 -08001888 if (!isAtLeastKernelVersion(4, 9, 0)) {
1889 ALOGE("Android S & T require kernel 4.9.");
Maciej Żenczykowski041be522023-10-23 23:34:52 -07001890 return 1;
1891 }
1892
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +00001893 // U bumps the kernel requirement up to 4.14
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07001894 if (isAtLeastU && !isAtLeastKernelVersion(4, 14, 0)) {
Maciej Żenczykowski041be522023-10-23 23:34:52 -07001895 ALOGE("Android U requires kernel 4.14.");
1896 return 1;
1897 }
1898
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +00001899 // V bumps the kernel requirement up to 4.19
1900 // see also: //system/netd/tests/kernel_test.cpp TestKernel419
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07001901 if (isAtLeastV && !isAtLeastKernelVersion(4, 19, 0)) {
Maciej Żenczykowski041be522023-10-23 23:34:52 -07001902 ALOGE("Android V requires kernel 4.19.");
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001903 return 1;
1904 }
1905
Maciej Żenczykowskidb9171f2025-01-14 16:22:46 -08001906 // 25Q2 bumps the kernel requirement up to 5.4
Maciej Żenczykowski76f66b62024-09-27 02:46:00 +00001907 // see also: //system/netd/tests/kernel_test.cpp TestKernel54
Maciej Żenczykowski98975122025-01-14 14:57:24 -08001908 if (isAtLeast25Q2 && !isAtLeastKernelVersion(5, 4, 0)) {
1909 ALOGE("Android 25Q2 requires kernel 5.4.");
Maciej Żenczykowski76f66b62024-09-27 02:46:00 +00001910 return 1;
1911 }
1912
Maciej Żenczykowskiec1115e2025-05-06 04:51:09 -07001913 // 25Q4 bumps the kernel requirement up to 5.10
1914 // see also: //system/netd/tests/kernel_test.cpp TestKernel510
1915 if (isAtLeast25Q4 && !isAtLeastKernelVersion(5, 10, 0)) {
1916 ALOGE("Android 25Q4 requires kernel 5.10.");
1917 return 1;
1918 }
1919
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +00001920 // Technically already required by U, but only enforce on V+
1921 // see also: //system/netd/tests/kernel_test.cpp TestKernel64Bit
1922 if (isAtLeastV && isKernel32Bit() && isAtLeastKernelVersion(5, 16, 0)) {
1923 ALOGE("Android V+ platform with 32 bit kernel version >= 5.16.0 is unsupported");
1924 if (!isTV()) return 1;
1925 }
1926
Maciej Żenczykowski127715a2025-02-10 21:52:01 -08001927 if (isKernel32Bit() && isAtLeast25Q2) {
1928 ALOGE("Android 25Q2 requires 64 bit kernel.");
1929 return 1;
1930 }
1931
Maciej Żenczykowski9b6a9942024-09-03 16:08:35 -07001932 // 6.6 is highest version supported by Android V, so this is effectively W+ (sdk=36+)
1933 if (isKernel32Bit() && isAtLeastKernelVersion(6, 7, 0)) {
1934 ALOGE("Android platform with 32 bit kernel version >= 6.7.0 is unsupported");
1935 return 1;
1936 }
1937
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +00001938 // Various known ABI layout issues, particularly wrt. bpf and ipsec/xfrm.
1939 if (isAtLeastV && isKernel32Bit() && isX86()) {
Maciej Żenczykowski7f6a4262024-02-17 00:42:42 +00001940 ALOGE("Android V requires X86 kernel to be 64-bit.");
Maciej Żenczykowski68eab892024-05-24 03:17:59 -07001941 if (!isTV()) return 1;
Maciej Żenczykowski7f6a4262024-02-17 00:42:42 +00001942 }
1943
Maciej Żenczykowskic982a4b2024-04-25 23:04:09 -07001944 if (isAtLeastV) {
1945 bool bad = false;
1946
1947 if (!isLtsKernel()) {
Maciej Żenczykowski76f66b62024-09-27 02:46:00 +00001948 ALOGW("Android V+ only supports LTS kernels.");
Maciej Żenczykowskic982a4b2024-04-25 23:04:09 -07001949 bad = true;
1950 }
1951
1952#define REQUIRE(maj, min, sub) \
1953 if (isKernelVersion(maj, min) && !isAtLeastKernelVersion(maj, min, sub)) { \
Maciej Żenczykowski76f66b62024-09-27 02:46:00 +00001954 ALOGW("Android V+ requires %d.%d kernel to be %d.%d.%d+.", maj, min, maj, min, sub); \
Maciej Żenczykowskic982a4b2024-04-25 23:04:09 -07001955 bad = true; \
1956 }
1957
1958 REQUIRE(4, 19, 236)
1959 REQUIRE(5, 4, 186)
1960 REQUIRE(5, 10, 199)
1961 REQUIRE(5, 15, 136)
1962 REQUIRE(6, 1, 57)
1963 REQUIRE(6, 6, 0)
Maciej Żenczykowski06f38e32024-12-11 07:12:59 -08001964 REQUIRE(6, 12, 0)
Maciej Żenczykowskic982a4b2024-04-25 23:04:09 -07001965
1966#undef REQUIRE
1967
Maciej Żenczykowski4a0838c2024-06-14 20:22:20 +00001968 if (bad) {
Maciej Żenczykowskic982a4b2024-04-25 23:04:09 -07001969 ALOGE("Unsupported kernel version (%07x).", kernelVersion());
1970 }
1971 }
1972
Maciej Żenczykowski726b58f2024-09-03 15:42:46 -07001973 /* Android 14/U should only launch on 64-bit kernels
1974 * T launches on 5.10/5.15
1975 * U launches on 5.15/6.1
1976 * So >=5.16 implies isKernel64Bit()
1977 *
1978 * We thus added a test to V VTS which requires 5.16+ devices to use 64-bit kernels.
1979 *
1980 * Starting with Android V, which is the first to support a post 6.1 Linux Kernel,
1981 * we also require 64-bit userspace.
1982 *
1983 * There are various known issues with 32-bit userspace talking to various
1984 * kernel interfaces (especially CAP_NET_ADMIN ones) on a 64-bit kernel.
1985 * Some of these have userspace or kernel workarounds/hacks.
1986 * Some of them don't...
1987 * We're going to be removing the hacks.
1988 * (for example "ANDROID: xfrm: remove in_compat_syscall() checks").
1989 * Note: this check/enforcement only applies to *system* userspace code,
1990 * it does not affect unprivileged apps, the 32-on-64 compatibility
1991 * problems are AFAIK limited to various CAP_NET_ADMIN protected interfaces.
1992 *
1993 * Additionally the 32-bit kernel jit support is poor,
1994 * and 32-bit userspace on 64-bit kernel bpf ringbuffer compatibility is broken.
Lorenzo Colittid95c0c62024-12-04 15:16:15 +09001995 * Note, however, that TV and Wear devices will continue to support 32-bit userspace
1996 * on ARM64.
Maciej Żenczykowski726b58f2024-09-03 15:42:46 -07001997 */
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07001998 if (isUserspace32bit() && isAtLeastKernelVersion(6, 2, 0)) {
Maciej Żenczykowski1c2187a2024-09-03 16:03:45 -07001999 // Stuff won't work reliably, but...
Lorenzo Colittid95c0c62024-12-04 15:16:15 +09002000 if (isArm() && (isTV() || isWear())) {
2001 // exempt Arm TV or Wear devices (arm32 ABI is far less problematic than x86-32)
2002 ALOGW("[Arm TV/Wear] 32-bit userspace unsupported on 6.2+ kernels.");
Maciej Żenczykowski73238632025-02-24 14:50:09 -08002003 } else if (first_api_level <= 33 /*T*/ && isArm()) {
Maciej Żenczykowski1c2187a2024-09-03 16:03:45 -07002004 // also exempt Arm devices upgrading with major kernel rev from T-
2005 // might possibly be better for them to run with a newer kernel...
2006 ALOGW("[Arm KernelUpRev] 32-bit userspace unsupported on 6.2+ kernels.");
2007 } else if (isArm()) {
2008 ALOGE("[Arm] 64-bit userspace required on 6.2+ kernels (%d).", first_api_level);
2009 return 1;
2010 } else { // x86 since RiscV cannot be 32-bit
2011 ALOGE("[x86] 64-bit userspace required on 6.2+ kernels.");
2012 return 1;
2013 }
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002014 }
2015
Maciej Żenczykowski1da19452025-05-16 11:07:51 -07002016 // Linux 6.12 was an LTS released at the end of 2024 (Nov 17),
2017 // and was first supported by Android 16 / 25Q2 (released in June 2025).
2018 // The next Linux LTS should be released near the end of 2025,
2019 // and will likely be 6.18.
2020 // Since officially Android only supports LTS, 6.13+ really means 6.18+,
2021 // and won't be supported before 2026, most likely Android 17 / 26Q2.
2022 // 6.13+ (implying 26Q2+) requires 64-bit userspace.
2023 if (isUserspace32bit() && isAtLeastKernelVersion(6, 13, 0)) {
2024 // due to previous check only reachable on Arm && (<=T kernel uprev || TV || Wear)
2025 ALOGE("64-bit userspace required on 6.13+ kernels.");
2026 return 1;
2027 }
2028
Maciej Żenczykowski25e26222025-03-20 23:25:39 -07002029 if (isAtLeast25Q2) {
2030 FILE * f = fopen("/system/etc/init/netbpfload.rc", "re");
2031 if (!f) {
2032 ALOGE("failure opening /system/etc/init/netbpfload.rc");
2033 return 1;
2034 }
2035 int y = -1, q = -1, a = -1, b = -1, c = -1;
2036 int v = fscanf(f, "# %d %d %d %d %d #", &y, &q, &a, &b, &c);
2037 ALOGI("detected %d of 5: %dQ%d api:%d.%d.%d", v, y, q, a, b, c);
2038 fclose(f);
Maciej Żenczykowskicb555722025-05-01 06:12:00 -07002039 if (v != 5) return 1;
2040 if (y < 2025 || y > 2099) return 1;
2041 if (q < 1 || q > 4) return 1;
2042 if (a < 36) return 1;
2043 if (b < 0 || b > 4) return 1;
2044 if (c < 0) return 1;
Maciej Żenczykowski25e26222025-03-20 23:25:39 -07002045 }
2046
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002047 // Ensure we can determine the Android build type.
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07002048 if (!isEng() && !isUser() && !isUserdebug()) {
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002049 ALOGE("Failed to determine the build type: got %s, want 'eng', 'user', or 'userdebug'",
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07002050 getBuildType().c_str());
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002051 return 1;
2052 }
2053
Maciej Żenczykowski48e476b2024-06-13 14:06:49 -07002054 if (runningAsRoot) {
2055 // Note: writing this proc file requires being root (always the case on V+)
2056
Maciej Żenczykowskif33f1282023-10-24 04:41:54 -07002057 // Linux 5.16-rc1 changed the default to 2 (disabled but changeable),
2058 // but we need 0 (enabled)
2059 // (this writeFile is known to fail on at least 4.19, but always defaults to 0 on
2060 // pre-5.13, on 5.13+ it depends on CONFIG_BPF_UNPRIV_DEFAULT_OFF)
Maciej Żenczykowskic9b0a832025-07-22 12:30:32 -07002061 if (writeFile("/proc/sys/kernel/unprivileged_bpf_disabled", "0\n") &&
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07002062 isAtLeastKernelVersion(5, 13, 0)) return 1;
Maciej Żenczykowski732a1412024-03-14 00:17:18 -07002063 }
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002064
Maciej Żenczykowski732a1412024-03-14 00:17:18 -07002065 if (isAtLeastU) {
Maciej Żenczykowski48e476b2024-06-13 14:06:49 -07002066 // Note: writing these proc files requires CAP_NET_ADMIN
2067 // and sepolicy which is only present on U+,
2068 // on Android T and earlier versions they're written from the 'load_bpf_programs'
2069 // trigger (ie. by init itself) instead.
2070
Maciej Żenczykowskif33f1282023-10-24 04:41:54 -07002071 // Enable the eBPF JIT -- but do note that on 64-bit kernels it is likely
2072 // already force enabled by the kernel config option BPF_JIT_ALWAYS_ON.
2073 // (Note: this (open) will fail with ENOENT 'No such file or directory' if
2074 // kernel does not have CONFIG_BPF_JIT=y)
2075 // BPF_JIT is required by R VINTF (which means 4.14/4.19/5.4 kernels),
2076 // but 4.14/4.19 were released with P & Q, and only 5.4 is new in R+.
Maciej Żenczykowskic9b0a832025-07-22 12:30:32 -07002077 if (writeFile("/proc/sys/net/core/bpf_jit_enable", "1\n")) return 1;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002078
Maciej Żenczykowskif33f1282023-10-24 04:41:54 -07002079 // Enable JIT kallsyms export for privileged users only
2080 // (Note: this (open) will fail with ENOENT 'No such file or directory' if
2081 // kernel does not have CONFIG_HAVE_EBPF_JIT=y)
Maciej Żenczykowskic9b0a832025-07-22 12:30:32 -07002082 if (writeFile("/proc/sys/net/core/bpf_jit_kallsyms", "1\n")) return 1;
Maciej Żenczykowskif33f1282023-10-24 04:41:54 -07002083 }
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002084
Maciej Żenczykowski18523cb2025-06-07 05:59:44 -07002085 if (runningAsRoot) { // implies U QPR3+ and kernel 4.14+
2086 // There should not be any programs or maps yet
2087 errno = 0;
2088 uint32_t progId = bpfGetNextProgId(0); // expect 0 with errno == ENOENT
2089 if (progId || errno != ENOENT) {
2090 ALOGE("bpfGetNextProgId(zero) returned %u (errno %d)", progId, errno);
2091 return 1;
2092 }
2093 errno = 0;
2094 uint32_t mapId = bpfGetNextMapId(0); // expect 0 with errno == ENOENT
2095 if (mapId || errno != ENOENT) {
2096 ALOGE("bpfGetNextMapId(zero) returned %u (errno %d)", mapId, errno);
2097 return 1;
2098 }
2099 } else if (isAtLeastKernelVersion(4, 14, 0)) { // implies S through U QPR2
2100 // bpfGetNext{Prog,Map}Id require 4.14+
2101 // furthermore since we're not running as root, we're not the initial
2102 // platform bpfloader, so there may already be some maps & programs.
2103 uint32_t mapId = 0;
2104 while (true) {
2105 errno = 0;
2106 uint32_t next = bpfGetNextMapId(mapId);
2107 if (!next && errno == ENOENT) break;
2108 if (next <= mapId) {
2109 ALOGE("bpfGetNextMapId(%u) returned %u errno %d", mapId, next, errno);
2110 return 1;
2111 }
2112 mapId = next;
2113 }
2114 // mapId is now the last map id, creating a new map should change that
2115 unique_fd map(createMap(BPF_MAP_TYPE_ARRAY, sizeof(int), sizeof(int), 1, 0));
2116 errno = 0;
2117 uint32_t next = bpfGetNextMapId(mapId);
2118 if (next <= mapId) {
2119 // We should fail here on Xiaomi S 4.14.180 due to kernel uapi bug,
2120 // which causes bpfGetNextMapId to behave as bpfGetNextProgId,
2121 // and thus it should return 0 with errno == ENOENT.
2122 ALOGE("bpfGetNextMapId(final %d) returned %d errno %d", mapId, next, errno);
Maciej Żenczykowskif7eb2bf2025-06-10 01:56:48 -07002123 if (next || errno != ENOENT) return 1;
2124 if (isAtLeastT || isAtLeastKernelVersion(4, 20, 0)) return 1;
2125 // implies Android S with 4.14 or 4.19 kernel
2126 ALOGW("Enabling bpfCmdFixupIsNeeded.");
2127 bpfCmdFixupIsNeeded = true;
Maciej Żenczykowski18523cb2025-06-07 05:59:44 -07002128 }
2129 } else { // implies S/T with 4.9 kernel
2130 // nothing we can do.
2131 }
2132
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002133 // Create all the pin subdirectories
2134 // (this must be done first to allow selinux_context and pin_subdir functionality,
2135 // which could otherwise fail with ENOENT during object pinning or renaming,
2136 // due to ordering issues)
Maciej Żenczykowski57de4bf2025-05-12 17:52:04 -07002137 if (createDir("/sys/fs/bpf/tethering")) return 1;
2138 // This is technically T+ but S also needs it for the 'mainline_done' file.
2139 if (createDir("/sys/fs/bpf/netd_shared")) return 1;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002140
Maciej Żenczykowski0b477492025-03-04 22:12:42 -08002141 if (isAtLeastT) {
Maciej Żenczykowski57de4bf2025-05-12 17:52:04 -07002142 if (createDir("/sys/fs/bpf/netd_readonly")) return 1;
2143 if (createDir("/sys/fs/bpf/net_shared")) return 1;
2144 if (createDir("/sys/fs/bpf/net_private")) return 1;
2145
2146 // This one is primarily meant for triggering genfscon rules.
2147 if (createDir("/sys/fs/bpf/loader")) return 1;
Maciej Żenczykowski0b477492025-03-04 22:12:42 -08002148 }
Maciej Żenczykowskia9209da2024-02-29 02:01:20 +00002149
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002150 // Load all ELF objects, create programs and maps, and pin them
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07002151 if (loadAllObjects(bpfloader_ver)) {
2152 ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS ===");
2153 ALOGE("If this triggers reliably, you're probably missing kernel options or patches.");
2154 ALOGE("If this triggers randomly, you might be hitting some memory allocation "
2155 "problems or startup script race.");
2156 ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---");
2157 sleep(20);
2158 return 2;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002159 }
2160
Maciej Żenczykowski83d5ad12025-06-17 00:48:44 -07002161 {
2162 // Create a trivial bpf map: a two element array [int->int]
2163 unique_fd map(createMap(BPF_MAP_TYPE_ARRAY, sizeof(int), sizeof(int), 2, 0));
2164
2165 int zero = 0;
2166 int kernel_bugs = bpfCmdFixupIsNeeded;
2167 if (writeToMapEntry(map, &zero, &kernel_bugs, BPF_ANY)) {
2168 ALOGE("Failure to write into index 0 of kernel bugs array.");
2169 return 1;
2170 }
2171
2172 int one = 1;
2173 int value = 123;
2174 if (writeToMapEntry(map, &one, &value, BPF_ANY)) {
2175 ALOGE("Critical kernel bug - failure to write into index 1 of 2 element bpf map array.");
2176 if (isAtLeastT) return 1;
2177 }
2178
2179 int ret = bpfFdPin(map, "/sys/fs/bpf/tethering/map_kernel_bugs");
2180 if (ret) {
2181 const int err = errno;
2182 ALOGE("pin -> %d [%d:%s]", ret, err, strerror(err));
2183 return -err;
2184 }
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002185 }
2186
Maciej Żenczykowski15f97312024-06-13 14:11:28 -07002187 // leave a flag that we're done
Maciej Żenczykowski57de4bf2025-05-12 17:52:04 -07002188 if (createDir("/sys/fs/bpf/netd_shared/mainline_done")) return 1;
Maciej Żenczykowski58c18222023-10-20 14:40:16 -07002189
Maciej Żenczykowski15f97312024-06-13 14:11:28 -07002190 // platform bpfloader will only succeed when run as root
2191 if (!runningAsRoot) {
2192 // unreachable on U QPR3+ which always runs netbpfload as root
2193
2194 ALOGI("mainline done, no need to transfer control to platform bpf loader.");
2195 return 0;
Maciej Żenczykowski732a1412024-03-14 00:17:18 -07002196 }
2197
Maciej Żenczykowski15f97312024-06-13 14:11:28 -07002198 // unreachable before U QPR3
Yu-Ting Tsengcb19e1b2024-12-10 14:55:04 -08002199 if (exists(uprobestatsBpfLoader)) {
Yu-Ting Tseng9b15fa02024-10-28 11:16:35 -07002200 ALOGI("done, transferring control to uprobestatsbpfload.");
2201 const char *args[] = {
2202 uprobestatsBpfLoader,
2203 NULL,
2204 };
2205 execve(args[0], (char **)args, envp);
Yu-Ting Tsengcb19e1b2024-12-10 14:55:04 -08002206 ALOGI("unable to execute uprobestatsbpfload, transferring control to "
2207 "platform bpfloader.");
Yu-Ting Tseng9b15fa02024-10-28 11:16:35 -07002208 }
Maciej Żenczykowski15f97312024-06-13 14:11:28 -07002209
2210 // platform BpfLoader *needs* to run as root
2211 const char * args[] = { platformBpfLoader, NULL, };
2212 execve(args[0], (char**)args, envp);
2213 ALOGE("FATAL: execve('%s'): %d[%s]", platformBpfLoader, errno, strerror(errno));
2214 return 1;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002215}
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07002216
2217} // namespace bpf
2218} // namespace android
2219
Maciej Żenczykowski6d151ef2024-04-30 23:55:57 -07002220int main(int argc, char** argv, char * const envp[]) {
Maciej Żenczykowski0b477492025-03-04 22:12:42 -08002221 if (android::bpf::isAtLeastT) {
2222 InitLogging(argv, &KernelLogger);
2223 } else {
2224 // S lacks the sepolicy to make non-root uid KernelLogger viable
2225 InitLogging(argv);
2226 }
Maciej Żenczykowski6d151ef2024-04-30 23:55:57 -07002227
2228 if (argc == 2 && !strcmp(argv[1], "done")) {
2229 // we're being re-exec'ed from platform bpfloader to 'finalize' things
Maciej Żenczykowski8a767282024-09-04 10:56:55 -07002230 if (!SetProperty("bpf.progs_loaded", "1")) {
Maciej Żenczykowski6d151ef2024-04-30 23:55:57 -07002231 ALOGE("Failed to set bpf.progs_loaded property to 1.");
2232 return 125;
2233 }
Maciej Żenczykowski66f16292024-05-06 23:52:33 -07002234 ALOGI("success.");
Maciej Żenczykowski6d151ef2024-04-30 23:55:57 -07002235 return 0;
2236 }
2237
2238 return android::bpf::doLoad(argv, envp);
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07002239}