blob: ed3507ac38209c8af1e7dbe8209ba7b96971c02e [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
187domain getDomainFromPinSubdir(const char s[BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE]) {
188 for (domain d : AllDomains) {
189 // Not sure how to enforce this at compile time, so abort() bpfloader at boot instead
190 if (strlen(lookupPinSubdir(d)) >= BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE) abort();
191 if (!strncmp(s, lookupPinSubdir(d), BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE)) return d;
192 }
193 ALOGE("unrecognized pin_subdir '%-32s'", s);
Maciej Żenczykowski6641f2f2024-08-07 15:34:24 -0700194 // Note: we *can* just abort() here as we only load bpf .o files shipped
195 // in the same mainline module / apex as NetBpfLoad itself.
196 abort();
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700197}
198
199static string pathToObjName(const string& path) {
200 // extract everything after the final slash, ie. this is the filename 'foo@1.o' or 'bar.o'
Maciej Żenczykowski8a767282024-09-04 10:56:55 -0700201 string filename = Split(path, "/").back();
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700202 // strip off everything from the final period onwards (strip '.o' suffix), ie. 'foo@1' or 'bar'
203 string name = filename.substr(0, filename.find_last_of('.'));
204 // strip any potential @1 suffix, this will leave us with just 'foo' or 'bar'
205 // this can be used to provide duplicate programs (mux based on the bpfloader version)
206 return name.substr(0, name.find_last_of('@'));
207}
208
209typedef struct {
210 const char* name;
211 enum bpf_prog_type type;
Maciej Żenczykowski346831c2024-08-12 17:49:10 +0000212 enum bpf_attach_type attach_type;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700213} sectionType;
214
215/*
216 * Map section name prefixes to program types, the section name will be:
217 * SECTION(<prefix>/<name-of-program>)
218 * For example:
219 * SECTION("tracepoint/sched_switch_func") where sched_switch_funcs
220 * is the name of the program, and tracepoint is the type.
221 *
222 * However, be aware that you should not be directly using the SECTION() macro.
Maciej Żenczykowski3a085152024-09-18 23:45:52 +0000223 * Instead use the DEFINE_(BPF|XDP)_(PROG|MAP)... & LICENSE macros.
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700224 *
225 * Programs shipped inside the tethering apex should be limited to networking stuff,
226 * as KPROBE, PERF_EVENT, TRACEPOINT are dangerous to use from mainline updatable code,
227 * since they are less stable abi/api and may conflict with platform uses of bpf.
228 */
229sectionType sectionNameTypes[] = {
230 {"bind4/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND},
231 {"bind6/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND},
Maciej Żenczykowski346831c2024-08-12 17:49:10 +0000232 {"cgroupskb/", BPF_PROG_TYPE_CGROUP_SKB},
233 {"cgroupsock/", BPF_PROG_TYPE_CGROUP_SOCK},
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700234 {"cgroupsockcreate/", BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE},
235 {"cgroupsockrelease/", BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET_SOCK_RELEASE},
236 {"connect4/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT},
237 {"connect6/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT},
238 {"egress/", BPF_PROG_TYPE_CGROUP_SKB, BPF_CGROUP_INET_EGRESS},
239 {"getsockopt/", BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT},
240 {"ingress/", BPF_PROG_TYPE_CGROUP_SKB, BPF_CGROUP_INET_INGRESS},
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700241 {"postbind4/", BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND},
242 {"postbind6/", BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET6_POST_BIND},
243 {"recvmsg4/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG},
244 {"recvmsg6/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG},
Maciej Żenczykowski346831c2024-08-12 17:49:10 +0000245 {"schedact/", BPF_PROG_TYPE_SCHED_ACT},
246 {"schedcls/", BPF_PROG_TYPE_SCHED_CLS},
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700247 {"sendmsg4/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG},
248 {"sendmsg6/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG},
249 {"setsockopt/", BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT},
Maciej Żenczykowski346831c2024-08-12 17:49:10 +0000250 {"skfilter/", BPF_PROG_TYPE_SOCKET_FILTER},
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700251 {"sockops/", BPF_PROG_TYPE_SOCK_OPS, BPF_CGROUP_SOCK_OPS},
252 {"sysctl", BPF_PROG_TYPE_CGROUP_SYSCTL, BPF_CGROUP_SYSCTL},
Maciej Żenczykowski346831c2024-08-12 17:49:10 +0000253 {"xdp/", BPF_PROG_TYPE_XDP},
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700254};
255
256typedef struct {
257 enum bpf_prog_type type;
Maciej Żenczykowski346831c2024-08-12 17:49:10 +0000258 enum bpf_attach_type attach_type;
Motomu Utsumi8645b6e2025-07-23 12:04:50 +0900259 string name; // The canonicalized section name.
260 string program_name;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700261 vector<char> data;
262 vector<char> rel_data;
263 optional<struct bpf_prog_def> prog_def;
264
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000265 unique_fd prog_fd; // fd after loading
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700266} codeSection;
267
268static int readElfHeader(ifstream& elfFile, Elf64_Ehdr* eh) {
269 elfFile.seekg(0);
270 if (elfFile.fail()) return -1;
271
272 if (!elfFile.read((char*)eh, sizeof(*eh))) return -1;
273
274 return 0;
275}
276
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000277// Reads all section header tables into an Shdr array
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700278static int readSectionHeadersAll(ifstream& elfFile, vector<Elf64_Shdr>& shTable) {
279 Elf64_Ehdr eh;
280 int ret = 0;
281
282 ret = readElfHeader(elfFile, &eh);
283 if (ret) return ret;
284
285 elfFile.seekg(eh.e_shoff);
286 if (elfFile.fail()) return -1;
287
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000288 // Read shdr table entries
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700289 shTable.resize(eh.e_shnum);
290
291 if (!elfFile.read((char*)shTable.data(), (eh.e_shnum * eh.e_shentsize))) return -ENOMEM;
292
293 return 0;
294}
295
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000296// Read a section by its index - for ex to get sec hdr strtab blob
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700297static int readSectionByIdx(ifstream& elfFile, int id, vector<char>& sec) {
298 vector<Elf64_Shdr> shTable;
299 int ret = readSectionHeadersAll(elfFile, shTable);
300 if (ret) return ret;
301
302 elfFile.seekg(shTable[id].sh_offset);
303 if (elfFile.fail()) return -1;
304
305 sec.resize(shTable[id].sh_size);
306 if (!elfFile.read(sec.data(), shTable[id].sh_size)) return -1;
307
308 return 0;
309}
310
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000311// Read whole section header string table
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700312static int readSectionHeaderStrtab(ifstream& elfFile, vector<char>& strtab) {
313 Elf64_Ehdr eh;
314 int ret = readElfHeader(elfFile, &eh);
315 if (ret) return ret;
316
317 ret = readSectionByIdx(elfFile, eh.e_shstrndx, strtab);
318 if (ret) return ret;
319
320 return 0;
321}
322
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000323// Get name from offset in strtab
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700324static int getSymName(ifstream& elfFile, int nameOff, string& name) {
325 int ret;
326 vector<char> secStrTab;
327
328 ret = readSectionHeaderStrtab(elfFile, secStrTab);
329 if (ret) return ret;
330
331 if (nameOff >= (int)secStrTab.size()) return -1;
332
333 name = string((char*)secStrTab.data() + nameOff);
334 return 0;
335}
336
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000337// Reads a full section by name - example to get the GPL license
Motomu Utsumi99a7b732025-07-17 10:59:49 +0900338template <typename T>
339static int readSectionByName(const char* name, ifstream& elfFile, vector<T>& data) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700340 vector<char> secStrTab;
341 vector<Elf64_Shdr> shTable;
342 int ret;
343
344 ret = readSectionHeadersAll(elfFile, shTable);
345 if (ret) return ret;
346
347 ret = readSectionHeaderStrtab(elfFile, secStrTab);
348 if (ret) return ret;
349
350 for (int i = 0; i < (int)shTable.size(); i++) {
351 char* secname = secStrTab.data() + shTable[i].sh_name;
352 if (!secname) continue;
353
354 if (!strcmp(secname, name)) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700355 elfFile.seekg(shTable[i].sh_offset);
356 if (elfFile.fail()) return -1;
357
Motomu Utsumi99a7b732025-07-17 10:59:49 +0900358 if (shTable[i].sh_size % sizeof(T)) return -1;
359 data.resize(shTable[i].sh_size / sizeof(T));
360 if (!elfFile.read(reinterpret_cast<char*>(data.data()), shTable[i].sh_size))
361 return -1;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700362
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700363 return 0;
364 }
365 }
366 return -2;
367}
368
Maciej Żenczykowski213c9222024-08-15 15:52:43 -0700369unsigned int readSectionUint(const char* name, ifstream& elfFile) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700370 vector<char> theBytes;
371 int ret = readSectionByName(name, elfFile, theBytes);
372 if (ret) {
Maciej Żenczykowski213c9222024-08-15 15:52:43 -0700373 ALOGE("Couldn't find section %s.", name);
374 abort();
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700375 } else if (theBytes.size() < sizeof(unsigned int)) {
Maciej Żenczykowski213c9222024-08-15 15:52:43 -0700376 ALOGE("Section %s is too short.", name);
377 abort();
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700378 } else {
379 // decode first 4 bytes as LE32 uint, there will likely be more bytes due to alignment.
380 unsigned int value = static_cast<unsigned char>(theBytes[3]);
381 value <<= 8;
382 value += static_cast<unsigned char>(theBytes[2]);
383 value <<= 8;
384 value += static_cast<unsigned char>(theBytes[1]);
385 value <<= 8;
386 value += static_cast<unsigned char>(theBytes[0]);
Maciej Żenczykowskidbdd90f2024-08-22 23:42:58 +0000387 ALOGD("Section %s value is %u [0x%x]", name, value, value);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700388 return value;
389 }
390}
391
392static int readSectionByType(ifstream& elfFile, int type, vector<char>& data) {
393 int ret;
394 vector<Elf64_Shdr> shTable;
395
396 ret = readSectionHeadersAll(elfFile, shTable);
397 if (ret) return ret;
398
399 for (int i = 0; i < (int)shTable.size(); i++) {
400 if ((int)shTable[i].sh_type != type) continue;
401
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700402 elfFile.seekg(shTable[i].sh_offset);
403 if (elfFile.fail()) return -1;
404
Motomu Utsumi1ae10a02025-07-17 10:45:19 +0900405 data.resize(shTable[i].sh_size);
406 if (!elfFile.read(data.data(), shTable[i].sh_size)) return -1;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700407
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700408 return 0;
409 }
410 return -2;
411}
412
413static bool symCompare(Elf64_Sym a, Elf64_Sym b) {
414 return (a.st_value < b.st_value);
415}
416
417static int readSymTab(ifstream& elfFile, int sort, vector<Elf64_Sym>& data) {
418 int ret, numElems;
419 Elf64_Sym* buf;
420 vector<char> secData;
421
422 ret = readSectionByType(elfFile, SHT_SYMTAB, secData);
423 if (ret) return ret;
424
425 buf = (Elf64_Sym*)secData.data();
426 numElems = (secData.size() / sizeof(Elf64_Sym));
427 data.assign(buf, buf + numElems);
428
429 if (sort) std::sort(data.begin(), data.end(), symCompare);
430 return 0;
431}
432
433static enum bpf_prog_type getSectionType(string& name) {
434 for (auto& snt : sectionNameTypes)
435 if (StartsWith(name, snt.name)) return snt.type;
436
437 return BPF_PROG_TYPE_UNSPEC;
438}
439
Maciej Żenczykowskie666d852024-08-15 15:03:38 -0700440static int readProgDefs(ifstream& elfFile, vector<struct bpf_prog_def>& pd) {
Motomu Utsumi99a7b732025-07-17 10:59:49 +0900441 return readSectionByName("progs", elfFile, pd);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700442}
443
444static int getSectionSymNames(ifstream& elfFile, const string& sectionName, vector<string>& names,
445 optional<unsigned> symbolType = std::nullopt) {
446 int ret;
447 string name;
448 vector<Elf64_Sym> symtab;
449 vector<Elf64_Shdr> shTable;
450
451 ret = readSymTab(elfFile, 1 /* sort */, symtab);
452 if (ret) return ret;
453
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000454 // Get index of section
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700455 ret = readSectionHeadersAll(elfFile, shTable);
456 if (ret) return ret;
457
458 int sec_idx = -1;
459 for (int i = 0; i < (int)shTable.size(); i++) {
460 ret = getSymName(elfFile, shTable[i].sh_name, name);
461 if (ret) return ret;
462
463 if (!name.compare(sectionName)) {
464 sec_idx = i;
465 break;
466 }
467 }
468
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000469 // No section found with matching name
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700470 if (sec_idx == -1) {
471 ALOGW("No %s section could be found in elf object", sectionName.c_str());
472 return -1;
473 }
474
475 for (int i = 0; i < (int)symtab.size(); i++) {
476 if (symbolType.has_value() && ELF_ST_TYPE(symtab[i].st_info) != symbolType) continue;
477
478 if (symtab[i].st_shndx == sec_idx) {
479 string s;
480 ret = getSymName(elfFile, symtab[i].st_name, s);
481 if (ret) return ret;
482 names.push_back(s);
483 }
484 }
485
486 return 0;
487}
488
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000489// Read a section by its index - for ex to get sec hdr strtab blob
Maciej Żenczykowskie666d852024-08-15 15:03:38 -0700490static int readCodeSections(ifstream& elfFile, vector<codeSection>& cs) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700491 vector<Elf64_Shdr> shTable;
492 int entries, ret = 0;
493
494 ret = readSectionHeadersAll(elfFile, shTable);
495 if (ret) return ret;
496 entries = shTable.size();
497
498 vector<struct bpf_prog_def> pd;
Maciej Żenczykowskie666d852024-08-15 15:03:38 -0700499 ret = readProgDefs(elfFile, pd);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700500 if (ret) return ret;
501 vector<string> progDefNames;
502 ret = getSectionSymNames(elfFile, "progs", progDefNames);
503 if (!pd.empty() && ret) return ret;
504
505 for (int i = 0; i < entries; i++) {
506 string name;
507 codeSection cs_temp;
508 cs_temp.type = BPF_PROG_TYPE_UNSPEC;
509
510 ret = getSymName(elfFile, shTable[i].sh_name, name);
511 if (ret) return ret;
512
513 enum bpf_prog_type ptype = getSectionType(name);
514
515 if (ptype == BPF_PROG_TYPE_UNSPEC) continue;
516
517 // This must be done before '/' is replaced with '_'.
Maciej Żenczykowski346831c2024-08-12 17:49:10 +0000518 for (auto& snt : sectionNameTypes)
519 if (StartsWith(name, snt.name)) cs_temp.attach_type = snt.attach_type;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700520
521 string oldName = name;
522
523 // convert all slashes to underscores
524 std::replace(name.begin(), name.end(), '/', '_');
525
526 cs_temp.type = ptype;
527 cs_temp.name = name;
528
529 ret = readSectionByIdx(elfFile, i, cs_temp.data);
530 if (ret) return ret;
531 ALOGV("Loaded code section %d (%s)", i, name.c_str());
532
533 vector<string> csSymNames;
534 ret = getSectionSymNames(elfFile, oldName, csSymNames, STT_FUNC);
535 if (ret || !csSymNames.size()) return ret;
Motomu Utsumi8645b6e2025-07-23 12:04:50 +0900536 cs_temp.program_name = csSymNames[0];
Motomu Utsumi62b1c882025-03-21 15:13:00 +0900537 for (size_t j = 0; j < progDefNames.size(); ++j) {
538 if (!progDefNames[j].compare(csSymNames[0] + "_def")) {
539 cs_temp.prog_def = pd[j];
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700540 break;
541 }
542 }
543
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +0000544 // Check for rel section
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700545 if (cs_temp.data.size() > 0 && i < entries) {
546 ret = getSymName(elfFile, shTable[i + 1].sh_name, name);
547 if (ret) return ret;
548
549 if (name == (".rel" + oldName)) {
550 ret = readSectionByIdx(elfFile, i + 1, cs_temp.rel_data);
551 if (ret) return ret;
552 ALOGV("Loaded relo section %d (%s)", i, name.c_str());
553 }
554 }
555
556 if (cs_temp.data.size() > 0) {
557 cs.push_back(std::move(cs_temp));
558 ALOGV("Adding section %d to cs list", i);
559 }
560 }
561 return 0;
562}
563
564static int getSymNameByIdx(ifstream& elfFile, int index, string& name) {
565 vector<Elf64_Sym> symtab;
566 int ret = 0;
567
568 ret = readSymTab(elfFile, 0 /* !sort */, symtab);
569 if (ret) return ret;
570
571 if (index >= (int)symtab.size()) return -1;
572
573 return getSymName(elfFile, symtab[index].st_name, name);
574}
575
576static bool mapMatchesExpectations(const unique_fd& fd, const string& mapName,
577 const struct bpf_map_def& mapDef, const enum bpf_map_type type) {
578 // bpfGetFd... family of functions require at minimum a 4.14 kernel,
579 // so on 4.9-T kernels just pretend the map matches our expectations.
580 // Additionally we'll get almost equivalent test coverage on newer devices/kernels.
581 // This is because the primary failure mode we're trying to detect here
582 // is either a source code misconfiguration (which is likely kernel independent)
583 // or a newly introduced kernel feature/bug (which is unlikely to get backported to 4.9).
584 if (!isAtLeastKernelVersion(4, 14, 0)) return true;
585
586 // Assuming fd is a valid Bpf Map file descriptor then
587 // all the following should always succeed on a 4.14+ kernel.
588 // If they somehow do fail, they'll return -1 (and set errno),
589 // which should then cause (among others) a key_size mismatch.
590 int fd_type = bpfGetFdMapType(fd);
591 int fd_key_size = bpfGetFdKeySize(fd);
592 int fd_value_size = bpfGetFdValueSize(fd);
593 int fd_max_entries = bpfGetFdMaxEntries(fd);
594 int fd_map_flags = bpfGetFdMapFlags(fd);
595
596 // DEVMAPs are readonly from the bpf program side's point of view, as such
597 // the kernel in kernel/bpf/devmap.c dev_map_init_map() will set the flag
598 int desired_map_flags = (int)mapDef.map_flags;
599 if (type == BPF_MAP_TYPE_DEVMAP || type == BPF_MAP_TYPE_DEVMAP_HASH)
600 desired_map_flags |= BPF_F_RDONLY_PROG;
601
602 // The .h file enforces that this is a power of two, and page size will
603 // also always be a power of two, so this logic is actually enough to
604 // force it to be a multiple of the page size, as required by the kernel.
605 unsigned int desired_max_entries = mapDef.max_entries;
606 if (type == BPF_MAP_TYPE_RINGBUF) {
607 if (desired_max_entries < page_size) desired_max_entries = page_size;
608 }
609
610 // The following checks should *never* trigger, if one of them somehow does,
611 // it probably means a bpf .o file has been changed/replaced at runtime
612 // and bpfloader was manually rerun (normally it should only run *once*
613 // early during the boot process).
614 // Another possibility is that something is misconfigured in the code:
615 // most likely a shared map is declared twice differently.
616 // But such a change should never be checked into the source tree...
617 if ((fd_type == type) &&
618 (fd_key_size == (int)mapDef.key_size) &&
619 (fd_value_size == (int)mapDef.value_size) &&
620 (fd_max_entries == (int)desired_max_entries) &&
621 (fd_map_flags == desired_map_flags)) {
622 return true;
623 }
624
Maciej Żenczykowski4b3937b2025-05-29 01:24:47 -0700625 ALOGE("bpf map name %s mismatch: desired/found (errno: %d): "
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700626 "type:%d/%d key:%u/%d value:%u/%d entries:%u/%d flags:%u/%d",
Maciej Żenczykowski4b3937b2025-05-29 01:24:47 -0700627 mapName.c_str(), errno, type, fd_type, mapDef.key_size, fd_key_size,
628 mapDef.value_size, fd_value_size, mapDef.max_entries, fd_max_entries,
629 desired_map_flags, fd_map_flags);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700630 return false;
631}
632
Motomu Utsumib3d3c2a2025-03-18 15:06:34 +0900633static int setBtfDatasecSize(ifstream &elfFile, struct btf *btf,
634 struct btf_type *bt) {
635 const char *name = btf__name_by_offset(btf, bt->name_off);
636 if (!name) {
637 ALOGE("Couldn't resolve section name, errno: %d", errno);
638 return -errno;
639 }
640
641 vector<char> data;
642 int ret = readSectionByName(name, elfFile, data);
643 if (ret) {
644 ALOGE("Couldn't read section %s, ret: %d", name, ret);
645 return ret;
646 }
647 bt->size = data.size();
648 return 0;
649}
650
Motomu Utsumiefe33312025-03-18 15:08:15 +0900651static int getSymOffsetByName(ifstream &elfFile, const char *name, int *off) {
652 vector<Elf64_Sym> symtab;
653 int ret = readSymTab(elfFile, 1 /* sort */, symtab);
654 if (ret) return ret;
655 for (int i = 0; i < (int)symtab.size(); i++) {
656 string s;
657 ret = getSymName(elfFile, symtab[i].st_name, s);
658 if (ret) continue;
659 if (!strcmp(s.c_str(), name)) {
660 *off = symtab[i].st_value;
661 return 0;
662 }
663 }
664 return -1;
665}
666
667static int setBtfVarOffset(ifstream &elfFile, struct btf *btf,
668 struct btf_type *datasecBt) {
669 int i, vars = btf_vlen(datasecBt);
670 struct btf_var_secinfo *vsi;
671 const char *datasecName = btf__name_by_offset(btf, datasecBt->name_off);
672 if (!datasecName) {
673 ALOGE("Couldn't resolve section name, errno: %d", errno);
674 return -errno;
675 }
676
677 for (i = 0, vsi = btf_var_secinfos(datasecBt); i < vars; i++, vsi++) {
678 const struct btf_type *varBt = btf__type_by_id(btf, vsi->type);
679 if (!varBt || !btf_is_var(varBt)) {
680 ALOGE("Found non VAR kind btf_type, section: %s id: %d", datasecName,
681 vsi->type);
682 return -1;
683 }
684
685 const struct btf_var *var = btf_var(varBt);
686 if (var->linkage == BTF_VAR_STATIC) continue;
687
688 const char *varName = btf__name_by_offset(btf, varBt->name_off);
689 if (!varName) {
690 ALOGE("Failed to resolve var name, section: %s", datasecName);
691 return -1;
692 }
693
694 int off;
695 int ret = getSymOffsetByName(elfFile, varName, &off);
696 if (ret) {
697 ALOGE("No offset found in symbol table, section: %s, var: %s, ret: %d",
698 datasecName, varName, ret);
699 return ret;
700 }
701 vsi->offset = off;
702 }
703 return 0;
704}
705
Motomu Utsumi4a3b5372025-06-04 09:15:36 +0900706#define BTF_INFO_ENC(kind, kind_flag, vlen) \
707 ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
708#define BTF_INT_ENC(encoding, bits_offset, nr_bits) \
709 ((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
710
711static int sanitizeBtf(struct btf *btf) {
712 for (unsigned int i = 1; i < btf__type_cnt(btf); ++i) {
713 struct btf_type *bt = (struct btf_type *)btf__type_by_id(btf, i);
714
715 // Replace BTF_KIND_VAR (5.2+) with BTF_KIND_INT (4.18+)
716 if (btf_is_var(bt)) {
717 bt->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
718 // using size = 1 is the safest choice, 4 will be too
719 // big and cause kernel BTF validation failure if
720 // original variable took less than 4 bytes
721 bt->size = 1;
722 *(int *)(bt + 1) = BTF_INT_ENC(0, 0, 8);
723 continue;
724 }
725
726 // Replace BTF_KIND_FUNC_PROTO (5.0+) with BTF_KIND_ENUM (4.18+)
727 if (btf_is_func_proto(bt)) {
728 int vlen = btf_vlen(bt);
729 bt->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
730 bt->size = sizeof(__u32); // kernel enforced
731 continue;
732 }
733
734 // Replace BTF_KIND_FUNC (5.0+) with BTF_KIND_TYPEDEF (4.18+)
735 if (btf_is_func(bt)) {
736 bt->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
737 continue;
738 }
739
740 // Replace BTF_KIND_DATASEC (5.2+) with BTF_KIND_STRUCT (4.18+)
741 if (btf_is_datasec(bt)) {
742 const struct btf_var_secinfo *v = btf_var_secinfos(bt);
743 struct btf_member *m = btf_members(bt);
744 char *name;
745
746 name = (char *)btf__name_by_offset(btf, bt->name_off);
747 while (*name) {
748 if (*name == '.' || *name == '?') *name = '_';
749 name++;
750 }
751
752 int vlen = btf_vlen(bt);
753 bt->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
754 for (int j = 0; j < vlen; j++, v++, m++) {
755 // order of field assignments is important
756 m->offset = v->offset * 8;
757 m->type = v->type;
758 // preserve variable name as member name
759 const struct btf_type *vt = btf__type_by_id(btf, v->type);
760 m->name_off = vt->name_off;
761 }
762 }
763 }
764 return 0;
765}
766
Motomu Utsumi21536e52025-03-18 15:09:27 +0900767static int loadBtf(ifstream &elfFile, struct btf *btf) {
768 int ret;
769 for (unsigned int i = 1; i < btf__type_cnt(btf); ++i) {
770 struct btf_type *bt = (struct btf_type *)btf__type_by_id(btf, i);
771 if (!btf_is_datasec(bt)) continue;
772 ret = setBtfDatasecSize(elfFile, btf, bt);
773 if (ret) return ret;
774 ret = setBtfVarOffset(elfFile, btf, bt);
775 if (ret) return ret;
776 }
777
Motomu Utsumi4a3b5372025-06-04 09:15:36 +0900778 if (!isAtLeastKernelVersion(5, 10, 0)) {
779 // Likely unnecessary on kernel 5.4 but untested.
780 sanitizeBtf(btf);
781 }
782
Motomu Utsumi21536e52025-03-18 15:09:27 +0900783 ret = btf__load_into_kernel(btf);
784 if (ret) {
785 if (errno != EINVAL) {
786 ALOGE("btf__load_into_kernel failed, errno: %d", errno);
787 return ret;
788 };
789 // For BTF_KIND_FUNC, newer kernels can read the BTF_INFO_VLEN bits of
790 // struct btf_type to distinguish static vs. global vs. extern
791 // functions, but older kernels enforce that only the BTF_INFO_KIND bits
792 // can be set. Retry with non-BTF_INFO_KIND bits zeroed out to handle
793 // this case.
794 for (unsigned int i = 1; i < btf__type_cnt(btf); ++i) {
795 struct btf_type *bt = (struct btf_type *)btf__type_by_id(btf, i);
796 if (btf_is_func(bt)) {
797 bt->info = (BTF_INFO_KIND(bt->info)) << 24;
798 }
799 }
800 ret = btf__load_into_kernel(btf);
801 if (ret) {
802 ALOGE("btf__load_into_kernel retry failed, errno: %d", errno);
803 return ret;
804 };
805 }
806 return 0;
807}
808
Motomu Utsumi59b20992025-03-18 15:10:19 +0900809int getKeyValueTids(const struct btf *btf, const char *mapName,
810 uint32_t expectedKeySize, uint32_t expectedValueSize,
811 uint32_t *keyTypeId, uint32_t *valueTypeId) {
812 const struct btf_type *kvBt;
813 const struct btf_member *key, *value;
814 const size_t max_name = 256;
815 char kvTypeName[max_name];
816 int64_t keySize, valueSize;
Motomu Utsumia6ffae22025-03-21 11:00:10 +0900817 int32_t kvId;
Motomu Utsumi59b20992025-03-18 15:10:19 +0900818
819 if (snprintf(kvTypeName, max_name, "____btf_map_%s", mapName) == max_name) {
820 ALOGE("____btf_map_%s is too long", mapName);
821 return -1;
822 }
823
824 kvId = btf__find_by_name(btf, kvTypeName);
825 if (kvId < 0) {
826 ALOGE("section not found, map: %s typeName: %s", mapName, kvTypeName);
827 return -1;
828 }
829
830 kvBt = btf__type_by_id(btf, kvId);
831 if (!kvBt) {
832 ALOGE("Couldn't find BTF type, map: %s id: %u", mapName, kvId);
833 return -1;
834 }
835
836 if (!btf_is_struct(kvBt) || btf_vlen(kvBt) < 2) {
837 ALOGE("Non Struct kind or invalid vlen, map: %s id: %u", mapName, kvId);
838 return -1;
839 }
840
841 key = btf_members(kvBt);
842 value = key + 1;
843
844 keySize = btf__resolve_size(btf, key->type);
845 if (keySize < 0) {
846 ALOGE("Couldn't get key size, map: %s errno: %d", mapName, errno);
847 return -1;
848 }
849
850 valueSize = btf__resolve_size(btf, value->type);
851 if (valueSize < 0) {
852 ALOGE("Couldn't get value size, map: %s errno: %d", mapName, errno);
853 return -1;
854 }
855
856 if (expectedKeySize != keySize || expectedValueSize != valueSize) {
857 ALOGE("Key value size mismatch, map: %s key size: %d expected key size: "
858 "%d value size: %d expected value size: %d",
859 mapName, (uint32_t)keySize, expectedKeySize, (uint32_t)valueSize,
860 expectedValueSize);
861 return -1;
862 }
863
864 *keyTypeId = key->type;
865 *valueTypeId = value->type;
866
867 return 0;
868}
869
Motomu Utsumi1a5cc5b2025-03-18 15:25:13 +0900870static bool isBtfSupported(enum bpf_map_type type) {
871 return type != BPF_MAP_TYPE_DEVMAP_HASH && type != BPF_MAP_TYPE_RINGBUF;
872}
873
Motomu Utsumi52a3ba72025-07-25 10:41:53 +0900874static int pinMap(const borrowed_fd& fd, const string& mapName, const struct bpf_map_def& mapDef,
Motomu Utsumi77b0b252025-07-15 13:36:20 +0900875 const string& objName, const string& mapPinLoc) {
876 int ret;
877 domain selinux_context = getDomainFromSelinuxContext(mapDef.selinux_context);
878 if (specified(selinux_context)) {
879 ALOGV("map %s selinux_context [%-32s] -> %d -> '%s' (%s)", mapName.c_str(),
880 mapDef.selinux_context, static_cast<int>(selinux_context),
881 lookupSelinuxContext(selinux_context), lookupPinSubdir(selinux_context));
882
883 string createLoc = string(BPF_FS_PATH) + lookupPinSubdir(selinux_context) +
884 "tmp_map_" + objName + "_" + mapName;
885 ret = bpfFdPin(fd, createLoc.c_str());
886 if (ret) {
887 const int err = errno;
888 ALOGE("create %s -> %d [%d:%s]", createLoc.c_str(), ret, err, strerror(err));
889 return -err;
890 }
891 ret = renameat2(AT_FDCWD, createLoc.c_str(),
892 AT_FDCWD, mapPinLoc.c_str(), RENAME_NOREPLACE);
893 if (ret) {
894 const int err = errno;
895 ALOGE("rename %s %s -> %d [%d:%s]", createLoc.c_str(), mapPinLoc.c_str(), ret,
896 err, strerror(err));
897 return -err;
898 }
899 } else {
900 ret = bpfFdPin(fd, mapPinLoc.c_str());
901 if (ret) {
902 const int err = errno;
903 ALOGE("pin %s -> %d [%d:%s]", mapPinLoc.c_str(), ret, err, strerror(err));
904 return -err;
905 }
906 }
907 ret = chmod(mapPinLoc.c_str(), mapDef.mode);
908 if (ret) {
909 const int err = errno;
910 ALOGE("chmod(%s, 0%o) = %d [%d:%s]", mapPinLoc.c_str(), mapDef.mode, ret, err,
911 strerror(err));
912 return -err;
913 }
914 ret = chown(mapPinLoc.c_str(), (uid_t)mapDef.uid, (gid_t)mapDef.gid);
915 if (ret) {
916 const int err = errno;
917 ALOGE("chown(%s, %u, %u) = %d [%d:%s]", mapPinLoc.c_str(), mapDef.uid, mapDef.gid,
918 ret, err, strerror(err));
919 return -err;
920 }
921
922 if (isAtLeastKernelVersion(4, 14, 0)) {
923 int mapId = bpfGetFdMapId(fd);
924 if (mapId == -1) {
925 const int err = errno;
926 ALOGE("bpfGetFdMapId failed, errno: %d", err);
927 return -err;
928 }
929 ALOGI("map %s id %d", mapPinLoc.c_str(), mapId);
930 }
931 return 0;
932}
933
Motomu Utsumie5dcaf72025-07-01 14:44:18 +0900934static int readMapNames(ifstream& elfFile, vector<string>& mapNames) {
935 int ret = getSectionSymNames(elfFile, ".android_maps", mapNames);
936 if (ret) return ret;
937
938 const string suffix = "_def";
939 for (string& name : mapNames) {
940 if (EndsWith(name, suffix)) {
941 name.erase(name.length() - suffix.length());
942 } else {
943 ALOGE("Failed to get map names, invalid symbol in .android_maps: %s", name.c_str());
944 return 1;
945 }
946 }
947 return 0;
948}
949
Motomu Utsumic80abe12025-07-18 10:14:37 +0900950static bool isMapTypeSupported(enum bpf_map_type type) {
951 if (type == BPF_MAP_TYPE_LPM_TRIE && !isAtLeastKernelVersion(4, 14, 0)) {
952 // On Linux Kernels older than 4.14 this map type doesn't exist - autoskip.
953 return false;
954 }
955 return true;
956}
957
958static enum bpf_map_type sanitizeMapType(enum bpf_map_type type) {
959 if (type == BPF_MAP_TYPE_DEVMAP && !isAtLeastKernelVersion(4, 14, 0)) {
960 // On Linux Kernels older than 4.14 this map type doesn't exist, but it can kind
961 // of be approximated: ARRAY has the same userspace api, though it is not usable
962 // by the same ebpf programs. However, that's okay because the bpf_redirect_map()
963 // helper doesn't exist on 4.9-T anyway (so the bpf program would fail to load,
964 // and thus needs to be tagged as 4.14+ either way), so there's nothing useful you
965 // could do with a DEVMAP anyway (that isn't already provided by an ARRAY)...
966 // Hence using an ARRAY instead of a DEVMAP simply makes life easier for userspace.
967 return BPF_MAP_TYPE_ARRAY;
968 }
969 if (type == BPF_MAP_TYPE_DEVMAP_HASH && !isAtLeastKernelVersion(5, 4, 0)) {
970 // On Linux Kernels older than 5.4 this map type doesn't exist, but it can kind
971 // of be approximated: HASH has the same userspace visible api.
972 // However it cannot be used by ebpf programs in the same way.
973 // Since bpf_redirect_map() only requires 4.14, a program using a DEVMAP_HASH map
974 // would fail to load (due to trying to redirect to a HASH instead of DEVMAP_HASH).
975 // One must thus tag any BPF_MAP_TYPE_DEVMAP_HASH + bpf_redirect_map() using
976 // programs as being 5.4+...
977 return BPF_MAP_TYPE_HASH;
978 }
979 // No sanitization is required.
980 return type;
981}
982
Maciej Żenczykowski586d4622025-07-28 15:12:00 -0700983static string buildMapPinLoc(const domain pin_subdir, const string& objName, const string& mapName) {
Motomu Utsumibf407f62025-07-24 13:08:34 +0900984 // Format of pin location is /sys/fs/bpf/<pin_subdir|prefix>map_<objName>_<mapName>
Motomu Utsumibf407f62025-07-24 13:08:34 +0900985 // Note: <objName> refers to the extension-less basename of the .o file (without @ suffix).
Maciej Żenczykowski16a24482025-07-28 15:32:09 -0700986 return string(BPF_FS_PATH) + lookupPinSubdir(pin_subdir) + "map_" +
Maciej Żenczykowskic1f92e52025-07-28 13:54:10 -0700987 objName + "_" + mapName;
Motomu Utsumibf407f62025-07-24 13:08:34 +0900988}
989
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700990static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>& mapFds,
Maciej Żenczykowski960c3372025-07-28 15:09:22 -0700991 const unsigned int bpfloader_ver) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700992 int ret;
Motomu Utsumi99a7b732025-07-17 10:59:49 +0900993 vector<char> btfData;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700994 vector<struct bpf_map_def> md;
995 vector<string> mapNames;
996 string objName = pathToObjName(string(elfPath));
997
Motomu Utsumi99a7b732025-07-17 10:59:49 +0900998 ret = readSectionByName(".android_maps", elfFile, md);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -0700999 if (ret == -2) return 0; // no maps to read
1000 if (ret) return ret;
1001
Motomu Utsumie5dcaf72025-07-01 14:44:18 +09001002 ret = readMapNames(elfFile, mapNames);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001003 if (ret) return ret;
1004
Motomu Utsumi6f1cecc2025-03-19 19:49:08 +09001005 struct btf *btf = NULL;
Patrick Rohr03b97432025-04-17 08:37:01 -07001006 auto btfGuard = base::make_scope_guard([&btf] { if (btf) btf__free(btf); });
Motomu Utsumi4a3b5372025-06-04 09:15:36 +09001007 if (isAtLeastKernelVersion(4, 19, 0)) {
Motomu Utsumi6f1cecc2025-03-19 19:49:08 +09001008 // On Linux Kernels older than 4.18 BPF_BTF_LOAD command doesn't exist.
1009 ret = readSectionByName(".BTF", elfFile, btfData);
1010 if (ret) {
1011 ALOGE("Failed to read .BTF section, ret:%d", ret);
1012 return ret;
1013 }
Motomu Utsumi62b1c882025-03-21 15:13:00 +09001014 btf = btf__new(btfData.data(), btfData.size());
Motomu Utsumi6f1cecc2025-03-19 19:49:08 +09001015 if (btf == NULL) {
1016 ALOGE("btf__new failed, errno: %d", errno);
1017 return -errno;
1018 }
Motomu Utsumi1a5cc5b2025-03-18 15:25:13 +09001019
Motomu Utsumi6f1cecc2025-03-19 19:49:08 +09001020 ret = loadBtf(elfFile, btf);
1021 if (ret) return ret;
1022 }
Motomu Utsumi1a5cc5b2025-03-18 15:25:13 +09001023
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001024 unsigned kvers = kernelVersion();
1025
1026 for (int i = 0; i < (int)mapNames.size(); i++) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001027 if (bpfloader_ver < md[i].bpfloader_min_ver) {
Maciej Żenczykowskidbdd90f2024-08-22 23:42:58 +00001028 ALOGD("skipping map %s which requires bpfloader min ver 0x%05x", mapNames[i].c_str(),
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001029 md[i].bpfloader_min_ver);
1030 mapFds.push_back(unique_fd());
1031 continue;
1032 }
1033
1034 if (bpfloader_ver >= md[i].bpfloader_max_ver) {
Maciej Żenczykowskidbdd90f2024-08-22 23:42:58 +00001035 ALOGD("skipping map %s which requires bpfloader max ver 0x%05x", mapNames[i].c_str(),
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001036 md[i].bpfloader_max_ver);
1037 mapFds.push_back(unique_fd());
1038 continue;
1039 }
1040
1041 if (kvers < md[i].min_kver) {
Maciej Żenczykowskidbdd90f2024-08-22 23:42:58 +00001042 ALOGD("skipping map %s which requires kernel version 0x%x >= 0x%x",
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001043 mapNames[i].c_str(), kvers, md[i].min_kver);
1044 mapFds.push_back(unique_fd());
1045 continue;
1046 }
1047
1048 if (kvers >= md[i].max_kver) {
Maciej Żenczykowskidbdd90f2024-08-22 23:42:58 +00001049 ALOGD("skipping map %s which requires kernel version 0x%x < 0x%x",
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001050 mapNames[i].c_str(), kvers, md[i].max_kver);
1051 mapFds.push_back(unique_fd());
1052 continue;
1053 }
1054
Motomu Utsumic80abe12025-07-18 10:14:37 +09001055 if (!isMapTypeSupported(md[i].type)) {
1056 ALOGD("skipping unsupported map type(%d): %s", md[i].type, mapNames[i].c_str());
Maciej Żenczykowski87019832025-02-03 22:04:26 -08001057 mapFds.push_back(unique_fd());
1058 continue;
1059 }
Motomu Utsumic80abe12025-07-18 10:14:37 +09001060 enum bpf_map_type type = sanitizeMapType(md[i].type);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001061
1062 // The .h file enforces that this is a power of two, and page size will
1063 // also always be a power of two, so this logic is actually enough to
1064 // force it to be a multiple of the page size, as required by the kernel.
1065 unsigned int max_entries = md[i].max_entries;
1066 if (type == BPF_MAP_TYPE_RINGBUF) {
1067 if (max_entries < page_size) max_entries = page_size;
1068 }
1069
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001070 domain pin_subdir = getDomainFromPinSubdir(md[i].pin_subdir);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001071 if (specified(pin_subdir)) {
Maciej Żenczykowski27b535a2024-08-15 19:46:46 +00001072 ALOGV("map %s pin_subdir [%-32s] -> %d -> '%s'", mapNames[i].c_str(), md[i].pin_subdir,
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001073 static_cast<int>(pin_subdir), lookupPinSubdir(pin_subdir));
1074 }
1075
Maciej Żenczykowski586d4622025-07-28 15:12:00 -07001076 string mapPinLoc = buildMapPinLoc(pin_subdir, objName, mapNames[i]);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001077 unique_fd fd;
1078 int saved_errno;
1079
1080 if (access(mapPinLoc.c_str(), F_OK) == 0) {
1081 fd.reset(mapRetrieveRO(mapPinLoc.c_str()));
1082 saved_errno = errno;
1083 ALOGD("bpf_create_map reusing map %s, ret: %d", mapNames[i].c_str(), fd.get());
Maciej Żenczykowskib71cd4f2025-05-20 06:41:01 -07001084 abort();
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001085 } else {
1086 union bpf_attr req = {
1087 .map_type = type,
1088 .key_size = md[i].key_size,
1089 .value_size = md[i].value_size,
1090 .max_entries = max_entries,
Maciej Żenczykowskic1a9f4a2025-01-20 12:09:13 -08001091 .map_flags = md[i].map_flags,
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001092 };
1093 if (isAtLeastKernelVersion(4, 15, 0))
1094 strlcpy(req.map_name, mapNames[i].c_str(), sizeof(req.map_name));
Motomu Utsumi1a5cc5b2025-03-18 15:25:13 +09001095
Motomu Utsumi6f1cecc2025-03-19 19:49:08 +09001096 bool haveBtf = btf && isBtfSupported(type);
Maciej Żenczykowski9fef9302025-03-18 20:09:34 -07001097 if (haveBtf) {
Motomu Utsumi1a5cc5b2025-03-18 15:25:13 +09001098 uint32_t kTid, vTid;
1099 ret = getKeyValueTids(btf, mapNames[i].c_str(), md[i].key_size,
1100 md[i].value_size, &kTid, &vTid);
1101 if (ret) return ret;
1102 req.btf_fd = btf__fd(btf);
1103 req.btf_key_type_id = kTid;
1104 req.btf_value_type_id = vTid;
Motomu Utsumi1a5cc5b2025-03-18 15:25:13 +09001105 }
1106
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001107 fd.reset(bpf(BPF_MAP_CREATE, req));
1108 saved_errno = errno;
Maciej Żenczykowski8597e3a2024-08-28 15:42:01 -07001109 if (fd.ok()) {
Maciej Żenczykowski9fef9302025-03-18 20:09:34 -07001110 ALOGD("bpf_create_map[%s] btf:%d -> %d",
1111 mapNames[i].c_str(), haveBtf, fd.get());
Maciej Żenczykowski8597e3a2024-08-28 15:42:01 -07001112 } else {
Maciej Żenczykowski9fef9302025-03-18 20:09:34 -07001113 ALOGE("bpf_create_map[%s] btf:%d -> %d errno:%d",
1114 mapNames[i].c_str(), haveBtf, fd.get(), saved_errno);
Maciej Żenczykowski8597e3a2024-08-28 15:42:01 -07001115 }
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001116 }
1117
1118 if (!fd.ok()) return -saved_errno;
1119
1120 // When reusing a pinned map, we need to check the map type/sizes/etc match, but for
1121 // safety (since reuse code path is rare) run these checks even if we just created it.
1122 // We assume failure is due to pinned map mismatch, hence the 'NOT UNIQUE' return code.
1123 if (!mapMatchesExpectations(fd, mapNames[i], md[i], type)) return -ENOTUNIQ;
1124
Motomu Utsumi77b0b252025-07-15 13:36:20 +09001125 ret = pinMap(fd, mapNames[i], md[i], objName, mapPinLoc);
1126 if (ret) return ret;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001127
1128 mapFds.push_back(std::move(fd));
1129 }
1130
1131 return ret;
1132}
1133
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001134static void applyRelo(void* insnsPtr, Elf64_Addr offset, int fd) {
1135 int insnIndex;
1136 struct bpf_insn *insn, *insns;
1137
1138 insns = (struct bpf_insn*)(insnsPtr);
1139
1140 insnIndex = offset / sizeof(struct bpf_insn);
1141 insn = &insns[insnIndex];
1142
1143 // Occasionally might be useful for relocation debugging, but pretty spammy
1144 if (0) {
1145 ALOGV("applying relo to instruction at byte offset: %llu, "
1146 "insn offset %d, insn %llx",
1147 (unsigned long long)offset, insnIndex, *(unsigned long long*)insn);
1148 }
1149
1150 if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001151 ALOGE("invalid relo for insn %d: code 0x%x", insnIndex, insn->code);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001152 return;
1153 }
1154
1155 insn->imm = fd;
1156 insn->src_reg = BPF_PSEUDO_MAP_FD;
1157}
1158
1159static void applyMapRelo(ifstream& elfFile, vector<unique_fd> &mapFds, vector<codeSection>& cs) {
1160 vector<string> mapNames;
1161
Motomu Utsumie5dcaf72025-07-01 14:44:18 +09001162 int ret = readMapNames(elfFile, mapNames);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001163 if (ret) return;
1164
1165 for (int k = 0; k != (int)cs.size(); k++) {
1166 Elf64_Rel* rel = (Elf64_Rel*)(cs[k].rel_data.data());
1167 int n_rel = cs[k].rel_data.size() / sizeof(*rel);
1168
1169 for (int i = 0; i < n_rel; i++) {
1170 int symIndex = ELF64_R_SYM(rel[i].r_info);
1171 string symName;
1172
1173 ret = getSymNameByIdx(elfFile, symIndex, symName);
1174 if (ret) return;
1175
Maciej Żenczykowskib4bade92024-08-14 23:06:54 +00001176 // Find the map fd and apply relo
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001177 for (int j = 0; j < (int)mapNames.size(); j++) {
1178 if (!mapNames[j].compare(symName)) {
1179 applyRelo(cs[k].data.data(), rel[i].r_offset, mapFds[j]);
1180 break;
1181 }
1182 }
1183 }
1184 }
1185}
1186
Motomu Utsumi5dbe6562025-07-23 12:45:40 +09001187static int pinProg(const borrowed_fd& fd, string& name, const struct bpf_prog_def& progDef,
Motomu Utsumi597f3af2025-07-16 14:23:46 +09001188 const string& objName, string& progPinLoc) {
1189 int ret;
1190 domain selinux_context = getDomainFromSelinuxContext(progDef.selinux_context);
1191 if (specified(selinux_context)) {
1192 ALOGV("prog %s selinux_context [%-32s] -> %d -> '%s' (%s)", name.c_str(),
1193 progDef.selinux_context, static_cast<int>(selinux_context),
1194 lookupSelinuxContext(selinux_context), lookupPinSubdir(selinux_context));
1195 string createLoc = string(BPF_FS_PATH) + lookupPinSubdir(selinux_context) +
1196 "tmp_prog_" + objName + '_' + string(name);
1197 ret = bpfFdPin(fd, createLoc.c_str());
1198 if (ret) {
1199 const int err = errno;
1200 ALOGE("create %s -> %d [%d:%s]", createLoc.c_str(), ret, err, strerror(err));
1201 return -err;
1202 }
1203 ret = renameat2(AT_FDCWD, createLoc.c_str(),
1204 AT_FDCWD, progPinLoc.c_str(), RENAME_NOREPLACE);
1205 if (ret) {
1206 const int err = errno;
1207 ALOGE("rename %s %s -> %d [%d:%s]", createLoc.c_str(), progPinLoc.c_str(), ret,
1208 err, strerror(err));
1209 return -err;
1210 }
1211 } else {
1212 ret = bpfFdPin(fd, progPinLoc.c_str());
1213 if (ret) {
1214 const int err = errno;
1215 ALOGE("create %s -> %d [%d:%s]", progPinLoc.c_str(), ret, err, strerror(err));
1216 return -err;
1217 }
1218 }
1219 if (chmod(progPinLoc.c_str(), 0440)) {
1220 const int err = errno;
1221 ALOGE("chmod %s 0440 -> [%d:%s]", progPinLoc.c_str(), err, strerror(err));
1222 return -err;
1223 }
1224 if (chown(progPinLoc.c_str(), (uid_t)progDef.uid,
1225 (gid_t)progDef.gid)) {
1226 const int err = errno;
1227 ALOGE("chown %s %d %d -> [%d:%s]", progPinLoc.c_str(), progDef.uid,
1228 progDef.gid, err, strerror(err));
1229 return -err;
1230 }
1231 return 0;
1232}
1233
Motomu Utsumi52a3ba72025-07-25 10:41:53 +09001234static int validateProg(const borrowed_fd& fd, string& progPinLoc,
1235 const unsigned int bpfloader_ver) {
Motomu Utsumi1d25bb32025-07-16 14:26:59 +09001236 if (!isAtLeastKernelVersion(4, 14, 0)) {
1237 return 0;
1238 }
1239 int progId = bpfGetFdProgId(fd);
1240 if (progId == -1) {
1241 const int err = errno;
1242 ALOGE("bpfGetFdProgId failed, errno: %d", err);
1243 return -err;
1244 }
1245
1246 int jitLen = bpfGetFdJitProgLen(fd);
1247 if (jitLen == -1) {
1248 const int err = errno;
1249 ALOGE("bpfGetFdJitProgLen failed, ret: %d", err);
1250 return -err;
1251 }
1252
1253 int xlatLen = bpfGetFdXlatProgLen(fd);
1254 if (xlatLen == -1) {
1255 const int err = errno;
1256 ALOGE("bpfGetFdXlatProgLen failed, ret: %d", err);
1257 return -err;
1258 }
1259 ALOGI("prog %s id %d len jit:%d xlat:%d", progPinLoc.c_str(), progId, jitLen, xlatLen);
1260
1261 if (!jitLen && bpfloader_ver >= BPFLOADER_MAINLINE_25Q2_VERSION) {
1262 ALOGE("Kernel eBPF JIT failure for %s", progPinLoc.c_str());
1263 return -ENOTSUP;
1264 }
1265 return 0;
1266}
1267
Maciej Żenczykowskic0136b12025-07-28 15:35:29 -07001268static string buildProgPinLoc(const domain pin_subdir, const string& objName, const string& name) {
Motomu Utsumif8977e92025-07-24 13:57:35 +09001269 // Format of pin location is
1270 // /sys/fs/bpf/<prefix>prog_<objName>_<progName>
Maciej Żenczykowski16a24482025-07-28 15:32:09 -07001271 return string(BPF_FS_PATH) + lookupPinSubdir(pin_subdir) + "prog_" +
Motomu Utsumif8977e92025-07-24 13:57:35 +09001272 objName + '_' + string(name);
1273}
1274
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001275static int loadCodeSections(const char* elfPath, vector<codeSection>& cs, const string& license,
Maciej Żenczykowski05da6b02025-07-28 15:34:43 -07001276 const unsigned int bpfloader_ver) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001277 unsigned kvers = kernelVersion();
1278
1279 if (!kvers) {
1280 ALOGE("unable to get kernel version");
1281 return -EINVAL;
1282 }
1283
1284 string objName = pathToObjName(string(elfPath));
1285
1286 for (int i = 0; i < (int)cs.size(); i++) {
1287 unique_fd& fd = cs[i].prog_fd;
1288 int ret;
1289 string name = cs[i].name;
1290
1291 if (!cs[i].prog_def.has_value()) {
1292 ALOGE("[%d] '%s' missing program definition! bad bpf.o build?", i, name.c_str());
1293 return -EINVAL;
1294 }
1295
1296 unsigned min_kver = cs[i].prog_def->min_kver;
1297 unsigned max_kver = cs[i].prog_def->max_kver;
1298 ALOGD("cs[%d].name:%s min_kver:%x .max_kver:%x (kvers:%x)", i, name.c_str(), min_kver,
1299 max_kver, kvers);
1300 if (kvers < min_kver) continue;
1301 if (kvers >= max_kver) continue;
1302
1303 unsigned bpfMinVer = cs[i].prog_def->bpfloader_min_ver;
1304 unsigned bpfMaxVer = cs[i].prog_def->bpfloader_max_ver;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001305 domain pin_subdir = getDomainFromPinSubdir(cs[i].prog_def->pin_subdir);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001306
1307 ALOGD("cs[%d].name:%s requires bpfloader version [0x%05x,0x%05x)", i, name.c_str(),
1308 bpfMinVer, bpfMaxVer);
1309 if (bpfloader_ver < bpfMinVer) continue;
1310 if (bpfloader_ver >= bpfMaxVer) continue;
1311
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001312 if (specified(pin_subdir)) {
Maciej Żenczykowski27b535a2024-08-15 19:46:46 +00001313 ALOGV("prog %s pin_subdir [%-32s] -> %d -> '%s'", name.c_str(),
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001314 cs[i].prog_def->pin_subdir, static_cast<int>(pin_subdir),
1315 lookupPinSubdir(pin_subdir));
1316 }
1317
1318 // strip any potential $foo suffix
1319 // this can be used to provide duplicate programs
1320 // conditionally loaded based on running kernel version
1321 name = name.substr(0, name.find_last_of('$'));
1322
1323 bool reuse = false;
Maciej Żenczykowskic0136b12025-07-28 15:35:29 -07001324 string progPinLoc = buildProgPinLoc(pin_subdir, objName, name);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001325 if (access(progPinLoc.c_str(), F_OK) == 0) {
1326 fd.reset(retrieveProgram(progPinLoc.c_str()));
1327 ALOGD("New bpf prog load reusing prog %s, ret: %d (%s)", progPinLoc.c_str(), fd.get(),
Maciej Żenczykowski37ba9392025-02-13 16:00:26 -08001328 !fd.ok() ? std::strerror(errno) : "ok");
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001329 reuse = true;
1330 } else {
Maciej Żenczykowskiea1d8f62024-09-05 09:38:14 -07001331 static char log_buf[1 << 20]; // 1 MiB logging buffer
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001332
1333 union bpf_attr req = {
1334 .prog_type = cs[i].type,
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001335 .insn_cnt = static_cast<__u32>(cs[i].data.size() / sizeof(struct bpf_insn)),
Maciej Żenczykowski52be6a82024-08-26 17:18:39 -07001336 .insns = ptr_to_u64(cs[i].data.data()),
1337 .license = ptr_to_u64(license.c_str()),
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001338 .log_level = 1,
Maciej Żenczykowskiea1d8f62024-09-05 09:38:14 -07001339 .log_size = sizeof(log_buf),
1340 .log_buf = ptr_to_u64(log_buf),
Maciej Żenczykowski346831c2024-08-12 17:49:10 +00001341 .expected_attach_type = cs[i].attach_type,
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001342 };
1343 if (isAtLeastKernelVersion(4, 15, 0))
1344 strlcpy(req.prog_name, cs[i].name.c_str(), sizeof(req.prog_name));
1345 fd.reset(bpf(BPF_PROG_LOAD, req));
1346
Maciej Żenczykowskiea1d8f62024-09-05 09:38:14 -07001347 // Kernel should have NULL terminated the log buffer, but force it anyway for safety
1348 log_buf[sizeof(log_buf) - 1] = 0;
1349
1350 // Strip out final newline if present
1351 int log_chars = strlen(log_buf);
1352 if (log_chars && log_buf[log_chars - 1] == '\n') log_buf[--log_chars] = 0;
1353
1354 bool log_oneline = !strchr(log_buf, '\n');
1355
1356 ALOGD("BPF_PROG_LOAD call for %s (%s) returned '%s' fd: %d (%s)", elfPath,
1357 cs[i].name.c_str(), log_oneline ? log_buf : "{multiline}",
Maciej Żenczykowski37ba9392025-02-13 16:00:26 -08001358 fd.get(), !fd.ok() ? std::strerror(errno) : "ok");
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001359
1360 if (!fd.ok()) {
Maciej Żenczykowskiea1d8f62024-09-05 09:38:14 -07001361 // kernel NULL terminates log_buf, so this checks for non-empty string
Maciej Żenczykowskif1259922025-07-15 14:57:23 -07001362 if (log_buf[0] && !isUser()) {
Maciej Żenczykowskiea1d8f62024-09-05 09:38:14 -07001363 vector<string> lines = Split(log_buf, "\n");
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001364
Maciej Żenczykowski9f8b17e2024-08-29 12:07:35 -07001365 ALOGW("BPF_PROG_LOAD - BEGIN log_buf contents:");
1366 for (const auto& line : lines) ALOGW("%s", line.c_str());
1367 ALOGW("BPF_PROG_LOAD - END log_buf contents.");
1368 }
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001369
1370 if (cs[i].prog_def->optional) {
Maciej Żenczykowskibfc0b612024-08-28 17:45:25 -07001371 ALOGW("failed program %s is marked optional - continuing...",
1372 cs[i].name.c_str());
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001373 continue;
1374 }
Maciej Żenczykowskibfc0b612024-08-28 17:45:25 -07001375 ALOGE("non-optional program %s failed to load.", cs[i].name.c_str());
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001376 }
1377 }
1378
1379 if (!fd.ok()) return fd.get();
1380
1381 if (!reuse) {
Motomu Utsumi597f3af2025-07-16 14:23:46 +09001382 ret = pinProg(fd, name, cs[i].prog_def.value(), objName, progPinLoc);
1383 if (ret) return ret;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001384 }
Motomu Utsumi1d25bb32025-07-16 14:26:59 +09001385 ret = validateProg(fd, progPinLoc, bpfloader_ver);
1386 if (ret) return ret;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001387 }
1388
1389 return 0;
1390}
1391
Motomu Utsumidbada872025-07-23 12:50:34 +09001392static int prepareLoadMaps(const struct bpf_object* obj, const vector<struct bpf_map_def>& md,
1393 const vector<string>& mapNames, const unsigned int bpfloader_ver) {
Motomu Utsumif1b1afb2025-07-23 12:10:52 +09001394 unsigned kvers = kernelVersion();
1395
1396 for (int i = 0; i < (int)mapNames.size(); i++) {
1397 struct bpf_map* m = bpf_object__find_map_by_name(obj, mapNames[i].c_str());
1398 if (!m) {
1399 ALOGE("bpf_object does not contain map: %s", mapNames[i].c_str());
1400 return -1;
1401 }
1402
1403 if (bpfloader_ver < md[i].bpfloader_min_ver || bpfloader_ver >= md[i].bpfloader_max_ver) {
1404 ALOGD("skipping map %s: bpfloader 0x%05x is outside required range [0x%05x, 0x%05x)",
1405 mapNames[i].c_str(), bpfloader_ver,
1406 md[i].bpfloader_min_ver, md[i].bpfloader_max_ver);
1407 bpf_map__set_autocreate(m, false);
1408 continue;
1409 }
1410
1411 if (kvers < md[i].min_kver || kvers >= md[i].max_kver) {
1412 ALOGD("skipping map %s: kernel version 0x%x is outside required range [0x%x, 0x%x)",
1413 mapNames[i].c_str(), kvers, md[i].min_kver, md[i].max_kver);
1414 bpf_map__set_autocreate(m, false);
1415 continue;
1416 }
1417
1418 if (!isMapTypeSupported(md[i].type)) {
1419 ALOGD("skipping unsupported map type(%d): %s", md[i].type, mapNames[i].c_str());
1420 bpf_map__set_autocreate(m, false);
1421 continue;
1422 }
1423
1424 bpf_map__set_type(m, sanitizeMapType(md[i].type));
1425 bpf_map__set_map_flags(m, md[i].map_flags);
1426 }
1427 return 0;
1428}
1429
Motomu Utsumidbada872025-07-23 12:50:34 +09001430static int prepareLoadProgs(const struct bpf_object* obj, const vector<codeSection>& cs,
1431 const unsigned int bpfloader_ver) {
Motomu Utsumi3cdcc472025-07-23 12:22:14 +09001432 unsigned kvers = kernelVersion();
1433
1434 for (int i = 0; i < (int)cs.size(); i++) {
1435 string name = cs[i].name;
1436 if (!cs[i].prog_def.has_value()) {
1437 ALOGE("[%d] '%s' missing program definition! bad bpf.o build?", i, name.c_str());
1438 return -EINVAL;
1439 }
1440 string program_name = cs[i].program_name;
1441 struct bpf_program* prog = bpf_object__find_program_by_name(obj, program_name.c_str());
1442 if (!prog) {
1443 ALOGE("bpf_object does not contain program: %s", cs[i].program_name.c_str());
1444 return -1;
1445 }
1446
1447 unsigned min_kver = cs[i].prog_def->min_kver;
1448 unsigned max_kver = cs[i].prog_def->max_kver;
1449 if (kvers < min_kver || kvers >= max_kver) {
1450 ALOGD("skipping prog %s: kernel version 0x%x is outside required range [0x%x, 0x%x)",
1451 name.c_str(), kvers, min_kver, max_kver);
1452 bpf_program__set_autoload(prog, false);
1453 continue;
1454 }
1455
1456 unsigned bpfMinVer = cs[i].prog_def->bpfloader_min_ver;
1457 unsigned bpfMaxVer = cs[i].prog_def->bpfloader_max_ver;
1458 if (bpfloader_ver < bpfMinVer || bpfloader_ver >= bpfMaxVer) {
1459 ALOGD("skipping prog %s: bpfloader 0x%05x is outside required range [0x%05x, 0x%05x)",
1460 name.c_str(), bpfloader_ver, bpfMinVer, bpfMaxVer);
1461 bpf_program__set_autoload(prog, false);
1462 continue;
1463 }
1464
1465 if (cs[i].prog_def->optional) {
1466 // TODO: Support optional program
1467 ALOGE("Optional program cannot be loaded by libbpf");
1468 return -1;
1469 }
1470
1471 bpf_program__set_type(prog, cs[i].type);
1472 bpf_program__set_expected_attach_type(prog, cs[i].attach_type);
1473 }
1474 return 0;
1475}
1476
Motomu Utsumidbada872025-07-23 12:50:34 +09001477static int pinMaps(const char* const elfPath, const struct bpf_object* obj,
Maciej Żenczykowski7ce493d2025-07-28 15:13:07 -07001478 const vector<struct bpf_map_def>& md, const vector<string>& mapNames) {
Motomu Utsumicd5bdf52025-07-23 12:32:08 +09001479 int ret;
1480 string objName = pathToObjName(string(elfPath));
1481
1482 for (int i = 0; i < (int)mapNames.size(); i++) {
1483 struct bpf_map* m = bpf_object__find_map_by_name(obj, mapNames[i].c_str());
1484 if (!m) {
1485 ALOGE("bpf_object does not contain map: %s", mapNames[i].c_str());
1486 return -1;
1487 }
1488 // This map was skipped
1489 if (!bpf_map__autocreate(m)) continue;
1490
1491 domain pin_subdir = getDomainFromPinSubdir(md[i].pin_subdir);
1492 if (specified(pin_subdir)) {
1493 ALOGE("map %s pin_subdir [%-32s] -> %d -> '%s'", mapNames[i].c_str(), md[i].pin_subdir,
1494 static_cast<int>(pin_subdir), lookupPinSubdir(pin_subdir));
1495 return -1;
1496 }
Maciej Żenczykowski586d4622025-07-28 15:12:00 -07001497 string mapPinLoc = buildMapPinLoc(pin_subdir, objName, mapNames[i]);
Motomu Utsumicd5bdf52025-07-23 12:32:08 +09001498 if (access(mapPinLoc.c_str(), F_OK) == 0) {
1499 ALOGE("Reusing map is not supported: %s", mapNames[i].c_str());
1500 return -1;
1501 }
1502
1503 ret = pinMap(bpf_map__fd(m), mapNames[i], md[i], objName, mapPinLoc);
1504 if (ret) return ret;
1505 }
1506 return 0;
1507}
1508
Motomu Utsumidbada872025-07-23 12:50:34 +09001509static int pinProgs(const char* const elfPath, const struct bpf_object * obj,
Maciej Żenczykowskif8e88362025-07-28 15:33:00 -07001510 const vector<codeSection>& cs, const unsigned int bpfloader_ver) {
Motomu Utsumi5dbe6562025-07-23 12:45:40 +09001511 int ret;
1512 string objName = pathToObjName(string(elfPath));
1513
1514 for (int i = 0; i < (int)cs.size(); i++) {
1515 string program_name = cs[i].program_name;
1516 struct bpf_program* prog = bpf_object__find_program_by_name(obj, program_name.c_str());
1517 if (!prog) {
1518 ALOGE("bpf_object does not contain program: %s", program_name.c_str());
1519 return -1;
1520 }
1521 // This program was skipped
1522 if (!bpf_program__autoload(prog)) continue;
1523
1524 string name = cs[i].name;
1525 name = name.substr(0, name.find_last_of('$'));
1526 domain pin_subdir = getDomainFromPinSubdir(cs[i].prog_def->pin_subdir);
1527 if (specified(pin_subdir)) {
1528 ALOGE("prog %s pin_subdir [%-32s] -> %d -> '%s'", name.c_str(),
1529 cs[i].prog_def->pin_subdir, static_cast<int>(pin_subdir),
1530 lookupPinSubdir(pin_subdir));
1531 return -1;
1532 }
Maciej Żenczykowskic0136b12025-07-28 15:35:29 -07001533 string progPinLoc = buildProgPinLoc(pin_subdir, objName, name);
Motomu Utsumi5dbe6562025-07-23 12:45:40 +09001534 if (access(progPinLoc.c_str(), F_OK) == 0) {
1535 // TODO: Skip loading lower priority program
1536 ALOGI("Higher priority program is already pinned, skip pinning %s", cs[i].name.c_str());
1537 continue;
1538 }
1539
1540 int fd = bpf_program__fd(prog);
1541 ret = pinProg(fd, name, cs[i].prog_def.value(), objName, progPinLoc);
1542 if (ret) return ret;
1543 ret = validateProg(fd, progPinLoc, bpfloader_ver);
1544 if (ret) return ret;
1545 }
1546 return 0;
1547}
1548
Maciej Żenczykowskiff3b4182025-07-28 15:02:07 -07001549static int loadProgByLibbpf(const char* const elfPath, const unsigned int bpfloader_ver) {
Motomu Utsumidbada872025-07-23 12:50:34 +09001550 int ret;
1551 vector<string> mapNames;
1552 vector<struct bpf_map_def> md;
1553 vector<codeSection> cs;
1554
1555 ifstream elfFile(elfPath, ios::in | ios::binary);
1556 if (!elfFile.is_open()) return -1;
1557
1558 LIBBPF_OPTS(bpf_object_open_opts, opts,
1559 .bpf_token_path = "",
1560 );
1561 struct bpf_object* obj = bpf_object__open_file(elfPath, &opts);
1562 if (!obj) return -1;
1563 auto objGuard = base::make_scope_guard([&obj] { bpf_object__close(obj); });
1564
1565 ret = readSectionByName(".android_maps", elfFile, md);
1566 if (ret) return ret;
1567
1568 ret = readMapNames(elfFile, mapNames);
1569 if (ret) return ret;
1570
1571 ret = prepareLoadMaps(obj, md, mapNames, bpfloader_ver);
1572 if (ret) return ret;
1573
1574 ret = readCodeSections(elfFile, cs);
1575 if (ret && ret != -ENOENT) return ret;
1576
1577 ret = prepareLoadProgs(obj, cs, bpfloader_ver);
1578 if (ret) return ret;
1579
1580 ret = bpf_object__load(obj);
1581 if (ret) return ret;
1582
Maciej Żenczykowski7ce493d2025-07-28 15:13:07 -07001583 ret = pinMaps(elfPath, obj, md, mapNames);
Motomu Utsumidbada872025-07-23 12:50:34 +09001584 if (ret) return ret;
1585
Maciej Żenczykowskif8e88362025-07-28 15:33:00 -07001586 ret = pinProgs(elfPath, obj, cs, bpfloader_ver);
Motomu Utsumidbada872025-07-23 12:50:34 +09001587 if (ret) return ret;
1588
1589 return 0;
1590}
1591
Maciej Żenczykowski12751c52025-07-28 15:06:10 -07001592int loadProg(const char* const elfPath, const unsigned int bpfloader_ver) {
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001593 vector<char> license;
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001594 vector<codeSection> cs;
1595 vector<unique_fd> mapFds;
1596 int ret;
1597
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001598 ifstream elfFile(elfPath, ios::in | ios::binary);
1599 if (!elfFile.is_open()) return -1;
1600
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001601 ret = readSectionByName("license", elfFile, license);
1602 if (ret) {
1603 ALOGE("Couldn't find license in %s", elfPath);
1604 return ret;
1605 } else {
Maciej Żenczykowski3a085152024-09-18 23:45:52 +00001606 ALOGD("Loading ELF object %s with license %s",
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001607 elfPath, (char*)license.data());
1608 }
1609
Maciej Żenczykowskic4a1cae2025-07-15 14:23:51 -07001610 ALOGD("BpfLoader ver 0x%05x processing ELF object %s", bpfloader_ver, elfPath);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001611
Maciej Żenczykowski960c3372025-07-28 15:09:22 -07001612 ret = createMaps(elfPath, elfFile, mapFds, bpfloader_ver);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001613 if (ret) {
1614 ALOGE("Failed to create maps: (ret=%d) in %s", ret, elfPath);
1615 return ret;
1616 }
1617
1618 for (int i = 0; i < (int)mapFds.size(); i++)
1619 ALOGV("map_fd found at %d is %d in %s", i, mapFds[i].get(), elfPath);
1620
Maciej Żenczykowski1e487172024-09-05 09:27:35 -07001621 ret = readCodeSections(elfFile, cs);
Maciej Żenczykowski66893bf2025-05-06 02:59:22 -07001622 if (ret == -ENOENT) return 0;
Maciej Żenczykowski1e487172024-09-05 09:27:35 -07001623 if (ret) {
1624 ALOGE("Couldn't read all code sections in %s", elfPath);
1625 return ret;
1626 }
1627
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001628 applyMapRelo(elfFile, mapFds, cs);
1629
Maciej Żenczykowski05da6b02025-07-28 15:34:43 -07001630 ret = loadCodeSections(elfPath, cs, string(license.data()), bpfloader_ver);
Maciej Żenczykowski6e1b4252024-08-07 15:03:44 -07001631 if (ret) ALOGE("Failed to load programs, loadCodeSections ret=%d", ret);
1632
1633 return ret;
1634}
1635
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07001636static bool exists(const char* const path) {
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001637 int v = access(path, F_OK);
Maciej Żenczykowski731acfe2024-04-30 10:09:57 +00001638 if (!v) return true;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001639 if (errno == ENOENT) return false;
1640 ALOGE("FATAL: access(%s, F_OK) -> %d [%d:%s]", path, v, errno, strerror(errno));
1641 abort(); // can only hit this if permissions (likely selinux) are screwed up
1642}
1643
Maciej Żenczykowski78fa8612024-08-26 17:22:25 -07001644#define APEXROOT "/apex/com.android.tethering"
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001645#define BPFROOT APEXROOT "/etc/bpf/mainline/"
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001646
Maciej Żenczykowski58464e62025-07-28 14:58:56 -07001647static int loadObject(const unsigned int bpfloader_ver,
Motomu Utsumie50e55d2025-07-23 12:59:23 +09001648 const char* const fname, const bool useLibbpf = false) {
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001649 string progPath = string(BPFROOT) + fname;
Maciej Żenczykowskiff3b4182025-07-28 15:02:07 -07001650 int ret = useLibbpf ? loadProgByLibbpf(progPath.c_str(), bpfloader_ver) :
Maciej Żenczykowski12751c52025-07-28 15:06:10 -07001651 loadProg(progPath.c_str(), bpfloader_ver);
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001652 if (ret) {
Motomu Utsumie50e55d2025-07-23 12:59:23 +09001653 ALOGE("Failed to load object: %s, ret: %s, libbpf: %d",
1654 progPath.c_str(), std::strerror(-ret), useLibbpf);
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001655 return 1;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001656 }
Motomu Utsumie50e55d2025-07-23 12:59:23 +09001657 ALOGD("Loaded object: %s, libbpf: %d", progPath.c_str(), useLibbpf);
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001658 return 0;
1659}
1660
1661static int loadAllObjects(const unsigned int bpfloader_ver) {
1662 // S+ Tethering mainline module (network_stack): tether offload
1663 // loads under /sys/fs/bpf/tethering:
Maciej Żenczykowski58464e62025-07-28 14:58:56 -07001664 if (loadObject(bpfloader_ver, "offload.o")) return 1;
1665 if (loadObject(bpfloader_ver, "test.o")) return 1;
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001666 if (isAtLeastT) {
1667 // T+ Tethering mainline module loads under:
1668 // /sys/fs/bpf/net_shared: shared with netd & system server
Maciej Żenczykowski58464e62025-07-28 14:58:56 -07001669 if (loadObject(bpfloader_ver, "clatd.o")) return 1;
1670 if (loadObject(bpfloader_ver, "dscpPolicy.o")) return 1;
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001671
1672 // /sys/fs/bpf/netd_shared: shared with netd & system server
1673 // - netutils_wrapper (for iptables xt_bpf) has access to programs
1674
1675 // WARNING: Android T+ non-updatable netd depends on both of the
1676 // 'netd_shared' & 'netd' strings for xt_bpf programs it loads
Maciej Żenczykowski58464e62025-07-28 14:58:56 -07001677 if (loadObject(bpfloader_ver, "netd.o")) return 1;
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07001678
1679 // /sys/fs/bpf/netd_readonly: shared with netd & system server
1680 // - netutils_wrapper has no access, netd has read only access
1681
1682 // /sys/fs/bpf/net_private: not shared, just network_stack
1683 }
1684 return 0;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001685}
1686
Maciej Żenczykowski57de4bf2025-05-12 17:52:04 -07001687static int createDir(const char* const dir) {
1688 mode_t prevUmask = umask(0);
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001689
Maciej Żenczykowski57de4bf2025-05-12 17:52:04 -07001690 errno = 0;
1691 int ret = mkdir(dir, S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
1692 if (ret && errno != EEXIST) {
1693 const int err = errno;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001694 umask(prevUmask);
Maciej Żenczykowski57de4bf2025-05-12 17:52:04 -07001695 ALOGE("Failed to create directory: %s, ret: %s", dir, std::strerror(err));
1696 return -err;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001697 }
Maciej Żenczykowski57de4bf2025-05-12 17:52:04 -07001698
1699 umask(prevUmask);
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001700 return 0;
1701}
1702
1703// Technically 'value' doesn't need to be newline terminated, but it's best
1704// to include a newline to match 'echo "value" > /proc/sys/...foo' behaviour,
1705// which is usually how kernel devs test the actual sysctl interfaces.
Maciej Żenczykowskic9b0a832025-07-22 12:30:32 -07001706static int writeFile(const char *filename, const char *value) {
Maciej Żenczykowski8a767282024-09-04 10:56:55 -07001707 unique_fd fd(open(filename, O_WRONLY | O_CLOEXEC));
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001708 if (fd < 0) {
1709 const int err = errno;
1710 ALOGE("open('%s', O_WRONLY | O_CLOEXEC) -> %s", filename, strerror(err));
1711 return -err;
1712 }
1713 int len = strlen(value);
1714 int v = write(fd, value, len);
1715 if (v < 0) {
1716 const int err = errno;
1717 ALOGE("write('%s', '%s', %d) -> %s", filename, value, len, strerror(err));
1718 return -err;
1719 }
1720 if (v != len) {
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001721 ALOGE("write('%s', '%s', %d) -> short write [%d]", filename, value, len, v);
1722 return -EINVAL;
1723 }
1724 return 0;
1725}
1726
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -08001727#define APEX_MOUNT_POINT "/apex/com.android.tethering"
Maciej Żenczykowski2fe2db52024-02-07 01:23:58 +00001728const char * const platformBpfLoader = "/system/bin/bpfloader";
Yu-Ting Tseng9b15fa02024-10-28 11:16:35 -07001729const char *const uprobestatsBpfLoader =
1730 "/apex/com.android.uprobestats/bin/uprobestatsbpfload";
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -08001731
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07001732static int logTetheringApexVersion(void) {
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -08001733 char * found_blockdev = NULL;
1734 FILE * f = NULL;
1735 char buf[4096];
1736
1737 f = fopen("/proc/mounts", "re");
1738 if (!f) return 1;
1739
1740 // /proc/mounts format: block_device [space] mount_point [space] other stuff... newline
1741 while (fgets(buf, sizeof(buf), f)) {
1742 char * blockdev = buf;
1743 char * space = strchr(blockdev, ' ');
1744 if (!space) continue;
1745 *space = '\0';
1746 char * mntpath = space + 1;
1747 space = strchr(mntpath, ' ');
1748 if (!space) continue;
1749 *space = '\0';
1750 if (strcmp(mntpath, APEX_MOUNT_POINT)) continue;
1751 found_blockdev = strdup(blockdev);
1752 break;
1753 }
1754 fclose(f);
1755 f = NULL;
1756
1757 if (!found_blockdev) return 2;
Maciej Żenczykowski5c057ed2024-04-30 11:59:13 +00001758 ALOGV("Found Tethering Apex mounted from blockdev %s", found_blockdev);
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -08001759
1760 f = fopen("/proc/mounts", "re");
1761 if (!f) { free(found_blockdev); return 3; }
1762
1763 while (fgets(buf, sizeof(buf), f)) {
1764 char * blockdev = buf;
1765 char * space = strchr(blockdev, ' ');
1766 if (!space) continue;
1767 *space = '\0';
1768 char * mntpath = space + 1;
1769 space = strchr(mntpath, ' ');
1770 if (!space) continue;
1771 *space = '\0';
1772 if (strcmp(blockdev, found_blockdev)) continue;
1773 if (strncmp(mntpath, APEX_MOUNT_POINT "@", strlen(APEX_MOUNT_POINT "@"))) continue;
1774 char * at = strchr(mntpath, '@');
1775 if (!at) continue;
1776 char * ver = at + 1;
1777 ALOGI("Tethering APEX version %s", ver);
1778 }
1779 fclose(f);
1780 free(found_blockdev);
1781 return 0;
1782}
Maciej Żenczykowski2fe2db52024-02-07 01:23:58 +00001783
Maciej Żenczykowski68eab892024-05-24 03:17:59 -07001784static bool hasGSM() {
Maciej Żenczykowski8a767282024-09-04 10:56:55 -07001785 static string ph = GetProperty("gsm.current.phone-type", "");
Maciej Żenczykowski68eab892024-05-24 03:17:59 -07001786 static bool gsm = (ph != "");
1787 static bool logged = false;
1788 if (!logged) {
1789 logged = true;
1790 ALOGI("hasGSM(gsm.current.phone-type='%s'): %s", ph.c_str(), gsm ? "true" : "false");
1791 }
1792 return gsm;
1793}
1794
1795static bool isTV() {
1796 if (hasGSM()) return false; // TVs don't do GSM
1797
Maciej Żenczykowski8a767282024-09-04 10:56:55 -07001798 static string key = GetProperty("ro.oem.key1", "");
Maciej Żenczykowski68eab892024-05-24 03:17:59 -07001799 static bool tv = StartsWith(key, "ATV00");
1800 static bool logged = false;
1801 if (!logged) {
1802 logged = true;
1803 ALOGI("isTV(ro.oem.key1='%s'): %s.", key.c_str(), tv ? "true" : "false");
1804 }
1805 return tv;
1806}
1807
Maciej Żenczykowski6e6b2092024-06-24 23:57:41 +00001808static bool isWear() {
Maciej Żenczykowski8a767282024-09-04 10:56:55 -07001809 static string wearSdkStr = GetProperty("ro.cw_build.wear_sdk.version", "");
1810 static int wearSdkInt = GetIntProperty("ro.cw_build.wear_sdk.version", 0);
1811 static string buildChars = GetProperty("ro.build.characteristics", "");
1812 static vector<string> v = Tokenize(buildChars, ",");
Maciej Żenczykowski6e6b2092024-06-24 23:57:41 +00001813 static bool watch = (std::find(v.begin(), v.end(), "watch") != v.end());
1814 static bool wear = (wearSdkInt > 0) || watch;
1815 static bool logged = false;
1816 if (!logged) {
1817 logged = true;
1818 ALOGI("isWear(ro.cw_build.wear_sdk.version=%d[%s] ro.build.characteristics='%s'): %s",
1819 wearSdkInt, wearSdkStr.c_str(), buildChars.c_str(), wear ? "true" : "false");
1820 }
1821 return wear;
1822}
1823
Motomu Utsumi712088d2025-03-18 14:52:02 +09001824static int libbpfPrint(enum libbpf_print_level lvl, const char *const formatStr,
1825 va_list argList) {
Motomu Utsumia84eb0b2025-07-23 13:26:19 +09001826#ifndef NETBPFLOAD_VERBOSE_LOG
1827 if (lvl != LIBBPF_WARN) return 0;
1828#endif
Motomu Utsumi712088d2025-03-18 14:52:02 +09001829 int32_t prio;
1830 switch (lvl) {
1831 case LIBBPF_WARN:
1832 prio = ANDROID_LOG_WARN;
1833 break;
1834 case LIBBPF_INFO:
1835 prio = ANDROID_LOG_INFO;
1836 break;
1837 case LIBBPF_DEBUG:
1838 prio = ANDROID_LOG_DEBUG;
1839 break;
1840 }
Motomu Utsumi47fcb862025-07-28 11:19:54 +09001841 if (!formatStr) {
1842 LOG_PRI(prio, LOG_TAG, "libbpf (null format string)");
1843 return 0;
1844 }
1845
1846 // Print each line to avoid being truncated.
1847 char *s = NULL;
1848 int ret = vasprintf(&s, formatStr, argList);
1849 if (ret == -1) {
1850 LOG_PRI(prio, LOG_TAG, "libbpf (format failure)");
1851 return 0;
1852 }
Motomu Utsumi712088d2025-03-18 14:52:02 +09001853 int len = strlen(s);
1854 if (len && s[len - 1] == '\n')
1855 s[len - 1] = 0;
Motomu Utsumi47fcb862025-07-28 11:19:54 +09001856 vector<string> lines = Split(s, "\n");
1857 for (const auto& line : lines) LOG_PRI(prio, LOG_TAG, "%s", line.c_str());
Motomu Utsumi712088d2025-03-18 14:52:02 +09001858 free(s);
1859 return 0;
1860}
1861
Maciej Żenczykowski6d151ef2024-04-30 23:55:57 -07001862static int doLoad(char** argv, char * const envp[]) {
Maciej Żenczykowski0b477492025-03-04 22:12:42 -08001863 if (!isAtLeastS) {
1864 ALOGE("Impossible - not reachable on Android <S.");
1865 // for safety, we don't fail, this is a just-in-case workaround
1866 // for any possible busted 'optimized' start everything vendor init hacks on R
1867 return 0;
1868 }
Motomu Utsumi712088d2025-03-18 14:52:02 +09001869 libbpf_set_print(libbpfPrint);
Maciej Żenczykowski0b477492025-03-04 22:12:42 -08001870
Maciej Żenczykowski15f97312024-06-13 14:11:28 -07001871 const bool runningAsRoot = !getuid(); // true iff U QPR3 or V+
Maciej Żenczykowski7b95d992024-06-13 18:18:11 -07001872
Maciej Żenczykowskidb9171f2025-01-14 16:22:46 -08001873 const int first_api_level = GetIntProperty("ro.board.first_api_level", api_level);
Maciej Żenczykowski1c2187a2024-09-03 16:03:45 -07001874
Maciej Żenczykowski03ef12c2024-02-10 21:34:22 +00001875 // last in U QPR2 beta1
1876 const bool has_platform_bpfloader_rc = exists("/system/etc/init/bpfloader.rc");
1877 // first in U QPR2 beta~2
1878 const bool has_platform_netbpfload_rc = exists("/system/etc/init/netbpfload.rc");
1879
Maciej Żenczykowski62956142024-06-13 15:32:57 -07001880 // Version of Network BpfLoader depends on the Android OS version
Maciej Żenczykowski8c097782025-03-04 13:11:56 -08001881 unsigned int bpfloader_ver = BPFLOADER_MAINLINE_S_VERSION; // [42u]
Maciej Żenczykowski1a3b54f2024-06-13 15:35:46 -07001882 if (isAtLeastT) ++bpfloader_ver; // [43] BPFLOADER_MAINLINE_T_VERSION
1883 if (isAtLeastU) ++bpfloader_ver; // [44] BPFLOADER_MAINLINE_U_VERSION
1884 if (runningAsRoot) ++bpfloader_ver; // [45] BPFLOADER_MAINLINE_U_QPR3_VERSION
1885 if (isAtLeastV) ++bpfloader_ver; // [46] BPFLOADER_MAINLINE_V_VERSION
Maciej Żenczykowski98975122025-01-14 14:57:24 -08001886 if (isAtLeast25Q2) ++bpfloader_ver; // [47] BPFLOADER_MAINLINE_25Q2_VERSION
Maciej Żenczykowskic5b9f5e2025-05-06 02:35:27 -07001887 if (isAtLeast25Q3) ++bpfloader_ver; // [48] BPFLOADER_MAINLINE_25Q3_VERSION
1888 if (isAtLeast25Q4) ++bpfloader_ver; // [49] BPFLOADER_MAINLINE_25Q4_VERSION
1889 if (isAtLeast26Q1) ++bpfloader_ver; // [50] BPFLOADER_MAINLINE_26Q1_VERSION
1890 if (isAtLeast26Q2) ++bpfloader_ver; // [51] BPFLOADER_MAINLINE_26Q2_VERSION
Maciej Żenczykowski62956142024-06-13 15:32:57 -07001891
Motomu Utsumia7693582025-02-05 17:40:08 +09001892 ALOGI("NetBpfLoad v0.%u (%s) api:%d/%d kver:%07x (%s) libbpf: v%u.%u "
1893 "uid:%d rc:%d%d",
Maciej Żenczykowskidb9171f2025-01-14 16:22:46 -08001894 bpfloader_ver, argv[0], android_get_device_api_level(), api_level,
Motomu Utsumia7693582025-02-05 17:40:08 +09001895 kernelVersion(), describeArch(), libbpf_major_version(),
1896 libbpf_minor_version(), getuid(), has_platform_bpfloader_rc,
1897 has_platform_netbpfload_rc);
Maciej Żenczykowski041be522023-10-23 23:34:52 -07001898
Maciej Żenczykowski03ef12c2024-02-10 21:34:22 +00001899 if (!has_platform_bpfloader_rc && !has_platform_netbpfload_rc) {
1900 ALOGE("Unable to find platform's bpfloader & netbpfload init scripts.");
1901 return 1;
1902 }
1903
1904 if (has_platform_bpfloader_rc && has_platform_netbpfload_rc) {
1905 ALOGE("Platform has *both* bpfloader & netbpfload init scripts.");
1906 return 1;
1907 }
1908
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -08001909 logTetheringApexVersion();
1910
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +00001911 // both S and T require kernel 4.9 (and eBpf support)
Maciej Żenczykowski0b477492025-03-04 22:12:42 -08001912 if (!isAtLeastKernelVersion(4, 9, 0)) {
1913 ALOGE("Android S & T require kernel 4.9.");
Maciej Żenczykowski041be522023-10-23 23:34:52 -07001914 return 1;
1915 }
1916
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +00001917 // U bumps the kernel requirement up to 4.14
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07001918 if (isAtLeastU && !isAtLeastKernelVersion(4, 14, 0)) {
Maciej Żenczykowski041be522023-10-23 23:34:52 -07001919 ALOGE("Android U requires kernel 4.14.");
1920 return 1;
1921 }
1922
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +00001923 // V bumps the kernel requirement up to 4.19
1924 // see also: //system/netd/tests/kernel_test.cpp TestKernel419
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07001925 if (isAtLeastV && !isAtLeastKernelVersion(4, 19, 0)) {
Maciej Żenczykowski041be522023-10-23 23:34:52 -07001926 ALOGE("Android V requires kernel 4.19.");
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001927 return 1;
1928 }
1929
Maciej Żenczykowskidb9171f2025-01-14 16:22:46 -08001930 // 25Q2 bumps the kernel requirement up to 5.4
Maciej Żenczykowski76f66b62024-09-27 02:46:00 +00001931 // see also: //system/netd/tests/kernel_test.cpp TestKernel54
Maciej Żenczykowski98975122025-01-14 14:57:24 -08001932 if (isAtLeast25Q2 && !isAtLeastKernelVersion(5, 4, 0)) {
1933 ALOGE("Android 25Q2 requires kernel 5.4.");
Maciej Żenczykowski76f66b62024-09-27 02:46:00 +00001934 return 1;
1935 }
1936
Maciej Żenczykowskiec1115e2025-05-06 04:51:09 -07001937 // 25Q4 bumps the kernel requirement up to 5.10
1938 // see also: //system/netd/tests/kernel_test.cpp TestKernel510
1939 if (isAtLeast25Q4 && !isAtLeastKernelVersion(5, 10, 0)) {
1940 ALOGE("Android 25Q4 requires kernel 5.10.");
1941 return 1;
1942 }
1943
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +00001944 // Technically already required by U, but only enforce on V+
1945 // see also: //system/netd/tests/kernel_test.cpp TestKernel64Bit
1946 if (isAtLeastV && isKernel32Bit() && isAtLeastKernelVersion(5, 16, 0)) {
1947 ALOGE("Android V+ platform with 32 bit kernel version >= 5.16.0 is unsupported");
1948 if (!isTV()) return 1;
1949 }
1950
Maciej Żenczykowski127715a2025-02-10 21:52:01 -08001951 if (isKernel32Bit() && isAtLeast25Q2) {
1952 ALOGE("Android 25Q2 requires 64 bit kernel.");
1953 return 1;
1954 }
1955
Maciej Żenczykowski9b6a9942024-09-03 16:08:35 -07001956 // 6.6 is highest version supported by Android V, so this is effectively W+ (sdk=36+)
1957 if (isKernel32Bit() && isAtLeastKernelVersion(6, 7, 0)) {
1958 ALOGE("Android platform with 32 bit kernel version >= 6.7.0 is unsupported");
1959 return 1;
1960 }
1961
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +00001962 // Various known ABI layout issues, particularly wrt. bpf and ipsec/xfrm.
1963 if (isAtLeastV && isKernel32Bit() && isX86()) {
Maciej Żenczykowski7f6a4262024-02-17 00:42:42 +00001964 ALOGE("Android V requires X86 kernel to be 64-bit.");
Maciej Żenczykowski68eab892024-05-24 03:17:59 -07001965 if (!isTV()) return 1;
Maciej Żenczykowski7f6a4262024-02-17 00:42:42 +00001966 }
1967
Maciej Żenczykowskic982a4b2024-04-25 23:04:09 -07001968 if (isAtLeastV) {
1969 bool bad = false;
1970
1971 if (!isLtsKernel()) {
Maciej Żenczykowski76f66b62024-09-27 02:46:00 +00001972 ALOGW("Android V+ only supports LTS kernels.");
Maciej Żenczykowskic982a4b2024-04-25 23:04:09 -07001973 bad = true;
1974 }
1975
1976#define REQUIRE(maj, min, sub) \
1977 if (isKernelVersion(maj, min) && !isAtLeastKernelVersion(maj, min, sub)) { \
Maciej Żenczykowski76f66b62024-09-27 02:46:00 +00001978 ALOGW("Android V+ requires %d.%d kernel to be %d.%d.%d+.", maj, min, maj, min, sub); \
Maciej Żenczykowskic982a4b2024-04-25 23:04:09 -07001979 bad = true; \
1980 }
1981
1982 REQUIRE(4, 19, 236)
1983 REQUIRE(5, 4, 186)
1984 REQUIRE(5, 10, 199)
1985 REQUIRE(5, 15, 136)
1986 REQUIRE(6, 1, 57)
1987 REQUIRE(6, 6, 0)
Maciej Żenczykowski06f38e32024-12-11 07:12:59 -08001988 REQUIRE(6, 12, 0)
Maciej Żenczykowskic982a4b2024-04-25 23:04:09 -07001989
1990#undef REQUIRE
1991
Maciej Żenczykowski4a0838c2024-06-14 20:22:20 +00001992 if (bad) {
Maciej Żenczykowskic982a4b2024-04-25 23:04:09 -07001993 ALOGE("Unsupported kernel version (%07x).", kernelVersion());
1994 }
1995 }
1996
Maciej Żenczykowski726b58f2024-09-03 15:42:46 -07001997 /* Android 14/U should only launch on 64-bit kernels
1998 * T launches on 5.10/5.15
1999 * U launches on 5.15/6.1
2000 * So >=5.16 implies isKernel64Bit()
2001 *
2002 * We thus added a test to V VTS which requires 5.16+ devices to use 64-bit kernels.
2003 *
2004 * Starting with Android V, which is the first to support a post 6.1 Linux Kernel,
2005 * we also require 64-bit userspace.
2006 *
2007 * There are various known issues with 32-bit userspace talking to various
2008 * kernel interfaces (especially CAP_NET_ADMIN ones) on a 64-bit kernel.
2009 * Some of these have userspace or kernel workarounds/hacks.
2010 * Some of them don't...
2011 * We're going to be removing the hacks.
2012 * (for example "ANDROID: xfrm: remove in_compat_syscall() checks").
2013 * Note: this check/enforcement only applies to *system* userspace code,
2014 * it does not affect unprivileged apps, the 32-on-64 compatibility
2015 * problems are AFAIK limited to various CAP_NET_ADMIN protected interfaces.
2016 *
2017 * Additionally the 32-bit kernel jit support is poor,
2018 * and 32-bit userspace on 64-bit kernel bpf ringbuffer compatibility is broken.
Lorenzo Colittid95c0c62024-12-04 15:16:15 +09002019 * Note, however, that TV and Wear devices will continue to support 32-bit userspace
2020 * on ARM64.
Maciej Żenczykowski726b58f2024-09-03 15:42:46 -07002021 */
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07002022 if (isUserspace32bit() && isAtLeastKernelVersion(6, 2, 0)) {
Maciej Żenczykowski1c2187a2024-09-03 16:03:45 -07002023 // Stuff won't work reliably, but...
Lorenzo Colittid95c0c62024-12-04 15:16:15 +09002024 if (isArm() && (isTV() || isWear())) {
2025 // exempt Arm TV or Wear devices (arm32 ABI is far less problematic than x86-32)
2026 ALOGW("[Arm TV/Wear] 32-bit userspace unsupported on 6.2+ kernels.");
Maciej Żenczykowski73238632025-02-24 14:50:09 -08002027 } else if (first_api_level <= 33 /*T*/ && isArm()) {
Maciej Żenczykowski1c2187a2024-09-03 16:03:45 -07002028 // also exempt Arm devices upgrading with major kernel rev from T-
2029 // might possibly be better for them to run with a newer kernel...
2030 ALOGW("[Arm KernelUpRev] 32-bit userspace unsupported on 6.2+ kernels.");
2031 } else if (isArm()) {
2032 ALOGE("[Arm] 64-bit userspace required on 6.2+ kernels (%d).", first_api_level);
2033 return 1;
2034 } else { // x86 since RiscV cannot be 32-bit
2035 ALOGE("[x86] 64-bit userspace required on 6.2+ kernels.");
2036 return 1;
2037 }
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002038 }
2039
Maciej Żenczykowski1da19452025-05-16 11:07:51 -07002040 // Linux 6.12 was an LTS released at the end of 2024 (Nov 17),
2041 // and was first supported by Android 16 / 25Q2 (released in June 2025).
2042 // The next Linux LTS should be released near the end of 2025,
2043 // and will likely be 6.18.
2044 // Since officially Android only supports LTS, 6.13+ really means 6.18+,
2045 // and won't be supported before 2026, most likely Android 17 / 26Q2.
2046 // 6.13+ (implying 26Q2+) requires 64-bit userspace.
2047 if (isUserspace32bit() && isAtLeastKernelVersion(6, 13, 0)) {
2048 // due to previous check only reachable on Arm && (<=T kernel uprev || TV || Wear)
2049 ALOGE("64-bit userspace required on 6.13+ kernels.");
2050 return 1;
2051 }
2052
Maciej Żenczykowski25e26222025-03-20 23:25:39 -07002053 if (isAtLeast25Q2) {
2054 FILE * f = fopen("/system/etc/init/netbpfload.rc", "re");
2055 if (!f) {
2056 ALOGE("failure opening /system/etc/init/netbpfload.rc");
2057 return 1;
2058 }
2059 int y = -1, q = -1, a = -1, b = -1, c = -1;
2060 int v = fscanf(f, "# %d %d %d %d %d #", &y, &q, &a, &b, &c);
2061 ALOGI("detected %d of 5: %dQ%d api:%d.%d.%d", v, y, q, a, b, c);
2062 fclose(f);
Maciej Żenczykowskicb555722025-05-01 06:12:00 -07002063 if (v != 5) return 1;
2064 if (y < 2025 || y > 2099) return 1;
2065 if (q < 1 || q > 4) return 1;
2066 if (a < 36) return 1;
2067 if (b < 0 || b > 4) return 1;
2068 if (c < 0) return 1;
Maciej Żenczykowski25e26222025-03-20 23:25:39 -07002069 }
2070
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002071 // Ensure we can determine the Android build type.
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07002072 if (!isEng() && !isUser() && !isUserdebug()) {
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002073 ALOGE("Failed to determine the build type: got %s, want 'eng', 'user', or 'userdebug'",
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07002074 getBuildType().c_str());
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002075 return 1;
2076 }
2077
Maciej Żenczykowski48e476b2024-06-13 14:06:49 -07002078 if (runningAsRoot) {
2079 // Note: writing this proc file requires being root (always the case on V+)
2080
Maciej Żenczykowskif33f1282023-10-24 04:41:54 -07002081 // Linux 5.16-rc1 changed the default to 2 (disabled but changeable),
2082 // but we need 0 (enabled)
2083 // (this writeFile is known to fail on at least 4.19, but always defaults to 0 on
2084 // pre-5.13, on 5.13+ it depends on CONFIG_BPF_UNPRIV_DEFAULT_OFF)
Maciej Żenczykowskic9b0a832025-07-22 12:30:32 -07002085 if (writeFile("/proc/sys/kernel/unprivileged_bpf_disabled", "0\n") &&
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07002086 isAtLeastKernelVersion(5, 13, 0)) return 1;
Maciej Żenczykowski732a1412024-03-14 00:17:18 -07002087 }
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002088
Maciej Żenczykowski732a1412024-03-14 00:17:18 -07002089 if (isAtLeastU) {
Maciej Żenczykowski48e476b2024-06-13 14:06:49 -07002090 // Note: writing these proc files requires CAP_NET_ADMIN
2091 // and sepolicy which is only present on U+,
2092 // on Android T and earlier versions they're written from the 'load_bpf_programs'
2093 // trigger (ie. by init itself) instead.
2094
Maciej Żenczykowskif33f1282023-10-24 04:41:54 -07002095 // Enable the eBPF JIT -- but do note that on 64-bit kernels it is likely
2096 // already force enabled by the kernel config option BPF_JIT_ALWAYS_ON.
2097 // (Note: this (open) will fail with ENOENT 'No such file or directory' if
2098 // kernel does not have CONFIG_BPF_JIT=y)
2099 // BPF_JIT is required by R VINTF (which means 4.14/4.19/5.4 kernels),
2100 // 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 -07002101 if (writeFile("/proc/sys/net/core/bpf_jit_enable", "1\n")) return 1;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002102
Maciej Żenczykowskif33f1282023-10-24 04:41:54 -07002103 // Enable JIT kallsyms export for privileged users only
2104 // (Note: this (open) will fail with ENOENT 'No such file or directory' if
2105 // kernel does not have CONFIG_HAVE_EBPF_JIT=y)
Maciej Żenczykowskic9b0a832025-07-22 12:30:32 -07002106 if (writeFile("/proc/sys/net/core/bpf_jit_kallsyms", "1\n")) return 1;
Maciej Żenczykowskif33f1282023-10-24 04:41:54 -07002107 }
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002108
Maciej Żenczykowski18523cb2025-06-07 05:59:44 -07002109 if (runningAsRoot) { // implies U QPR3+ and kernel 4.14+
2110 // There should not be any programs or maps yet
2111 errno = 0;
2112 uint32_t progId = bpfGetNextProgId(0); // expect 0 with errno == ENOENT
2113 if (progId || errno != ENOENT) {
2114 ALOGE("bpfGetNextProgId(zero) returned %u (errno %d)", progId, errno);
2115 return 1;
2116 }
2117 errno = 0;
2118 uint32_t mapId = bpfGetNextMapId(0); // expect 0 with errno == ENOENT
2119 if (mapId || errno != ENOENT) {
2120 ALOGE("bpfGetNextMapId(zero) returned %u (errno %d)", mapId, errno);
2121 return 1;
2122 }
2123 } else if (isAtLeastKernelVersion(4, 14, 0)) { // implies S through U QPR2
2124 // bpfGetNext{Prog,Map}Id require 4.14+
2125 // furthermore since we're not running as root, we're not the initial
2126 // platform bpfloader, so there may already be some maps & programs.
2127 uint32_t mapId = 0;
2128 while (true) {
2129 errno = 0;
2130 uint32_t next = bpfGetNextMapId(mapId);
2131 if (!next && errno == ENOENT) break;
2132 if (next <= mapId) {
2133 ALOGE("bpfGetNextMapId(%u) returned %u errno %d", mapId, next, errno);
2134 return 1;
2135 }
2136 mapId = next;
2137 }
2138 // mapId is now the last map id, creating a new map should change that
2139 unique_fd map(createMap(BPF_MAP_TYPE_ARRAY, sizeof(int), sizeof(int), 1, 0));
2140 errno = 0;
2141 uint32_t next = bpfGetNextMapId(mapId);
2142 if (next <= mapId) {
2143 // We should fail here on Xiaomi S 4.14.180 due to kernel uapi bug,
2144 // which causes bpfGetNextMapId to behave as bpfGetNextProgId,
2145 // and thus it should return 0 with errno == ENOENT.
2146 ALOGE("bpfGetNextMapId(final %d) returned %d errno %d", mapId, next, errno);
Maciej Żenczykowskif7eb2bf2025-06-10 01:56:48 -07002147 if (next || errno != ENOENT) return 1;
2148 if (isAtLeastT || isAtLeastKernelVersion(4, 20, 0)) return 1;
2149 // implies Android S with 4.14 or 4.19 kernel
2150 ALOGW("Enabling bpfCmdFixupIsNeeded.");
2151 bpfCmdFixupIsNeeded = true;
Maciej Żenczykowski18523cb2025-06-07 05:59:44 -07002152 }
2153 } else { // implies S/T with 4.9 kernel
2154 // nothing we can do.
2155 }
2156
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002157 // Create all the pin subdirectories
2158 // (this must be done first to allow selinux_context and pin_subdir functionality,
2159 // which could otherwise fail with ENOENT during object pinning or renaming,
2160 // due to ordering issues)
Maciej Żenczykowski57de4bf2025-05-12 17:52:04 -07002161 if (createDir("/sys/fs/bpf/tethering")) return 1;
2162 // This is technically T+ but S also needs it for the 'mainline_done' file.
2163 if (createDir("/sys/fs/bpf/netd_shared")) return 1;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002164
Maciej Żenczykowski0b477492025-03-04 22:12:42 -08002165 if (isAtLeastT) {
Maciej Żenczykowski57de4bf2025-05-12 17:52:04 -07002166 if (createDir("/sys/fs/bpf/netd_readonly")) return 1;
2167 if (createDir("/sys/fs/bpf/net_shared")) return 1;
2168 if (createDir("/sys/fs/bpf/net_private")) return 1;
2169
2170 // This one is primarily meant for triggering genfscon rules.
2171 if (createDir("/sys/fs/bpf/loader")) return 1;
Maciej Żenczykowski0b477492025-03-04 22:12:42 -08002172 }
Maciej Żenczykowskia9209da2024-02-29 02:01:20 +00002173
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002174 // Load all ELF objects, create programs and maps, and pin them
Maciej Żenczykowski82ce2ca2025-05-14 14:49:28 -07002175 if (loadAllObjects(bpfloader_ver)) {
2176 ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS ===");
2177 ALOGE("If this triggers reliably, you're probably missing kernel options or patches.");
2178 ALOGE("If this triggers randomly, you might be hitting some memory allocation "
2179 "problems or startup script race.");
2180 ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---");
2181 sleep(20);
2182 return 2;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002183 }
2184
Maciej Żenczykowski83d5ad12025-06-17 00:48:44 -07002185 {
2186 // Create a trivial bpf map: a two element array [int->int]
2187 unique_fd map(createMap(BPF_MAP_TYPE_ARRAY, sizeof(int), sizeof(int), 2, 0));
2188
2189 int zero = 0;
2190 int kernel_bugs = bpfCmdFixupIsNeeded;
2191 if (writeToMapEntry(map, &zero, &kernel_bugs, BPF_ANY)) {
2192 ALOGE("Failure to write into index 0 of kernel bugs array.");
2193 return 1;
2194 }
2195
2196 int one = 1;
2197 int value = 123;
2198 if (writeToMapEntry(map, &one, &value, BPF_ANY)) {
2199 ALOGE("Critical kernel bug - failure to write into index 1 of 2 element bpf map array.");
2200 if (isAtLeastT) return 1;
2201 }
2202
2203 int ret = bpfFdPin(map, "/sys/fs/bpf/tethering/map_kernel_bugs");
2204 if (ret) {
2205 const int err = errno;
2206 ALOGE("pin -> %d [%d:%s]", ret, err, strerror(err));
2207 return -err;
2208 }
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002209 }
2210
Maciej Żenczykowski15f97312024-06-13 14:11:28 -07002211 // leave a flag that we're done
Maciej Żenczykowski57de4bf2025-05-12 17:52:04 -07002212 if (createDir("/sys/fs/bpf/netd_shared/mainline_done")) return 1;
Maciej Żenczykowski58c18222023-10-20 14:40:16 -07002213
Maciej Żenczykowski15f97312024-06-13 14:11:28 -07002214 // platform bpfloader will only succeed when run as root
2215 if (!runningAsRoot) {
2216 // unreachable on U QPR3+ which always runs netbpfload as root
2217
2218 ALOGI("mainline done, no need to transfer control to platform bpf loader.");
2219 return 0;
Maciej Żenczykowski732a1412024-03-14 00:17:18 -07002220 }
2221
Maciej Żenczykowski15f97312024-06-13 14:11:28 -07002222 // unreachable before U QPR3
Yu-Ting Tsengcb19e1b2024-12-10 14:55:04 -08002223 if (exists(uprobestatsBpfLoader)) {
Yu-Ting Tseng9b15fa02024-10-28 11:16:35 -07002224 ALOGI("done, transferring control to uprobestatsbpfload.");
2225 const char *args[] = {
2226 uprobestatsBpfLoader,
2227 NULL,
2228 };
2229 execve(args[0], (char **)args, envp);
Yu-Ting Tsengcb19e1b2024-12-10 14:55:04 -08002230 ALOGI("unable to execute uprobestatsbpfload, transferring control to "
2231 "platform bpfloader.");
Yu-Ting Tseng9b15fa02024-10-28 11:16:35 -07002232 }
Maciej Żenczykowski15f97312024-06-13 14:11:28 -07002233
2234 // platform BpfLoader *needs* to run as root
2235 const char * args[] = { platformBpfLoader, NULL, };
2236 execve(args[0], (char**)args, envp);
2237 ALOGE("FATAL: execve('%s'): %d[%s]", platformBpfLoader, errno, strerror(errno));
2238 return 1;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07002239}
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07002240
2241} // namespace bpf
2242} // namespace android
2243
Maciej Żenczykowski6d151ef2024-04-30 23:55:57 -07002244int main(int argc, char** argv, char * const envp[]) {
Maciej Żenczykowski0b477492025-03-04 22:12:42 -08002245 if (android::bpf::isAtLeastT) {
2246 InitLogging(argv, &KernelLogger);
2247 } else {
2248 // S lacks the sepolicy to make non-root uid KernelLogger viable
2249 InitLogging(argv);
2250 }
Maciej Żenczykowski6d151ef2024-04-30 23:55:57 -07002251
2252 if (argc == 2 && !strcmp(argv[1], "done")) {
2253 // we're being re-exec'ed from platform bpfloader to 'finalize' things
Maciej Żenczykowski8a767282024-09-04 10:56:55 -07002254 if (!SetProperty("bpf.progs_loaded", "1")) {
Maciej Żenczykowski6d151ef2024-04-30 23:55:57 -07002255 ALOGE("Failed to set bpf.progs_loaded property to 1.");
2256 return 125;
2257 }
Maciej Żenczykowski66f16292024-05-06 23:52:33 -07002258 ALOGI("success.");
Maciej Żenczykowski6d151ef2024-04-30 23:55:57 -07002259 return 0;
2260 }
2261
2262 return android::bpf::doLoad(argv, envp);
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -07002263}