| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 1 | /* | 
| Maciej Żenczykowski | 283c25a | 2023-10-02 19:43:30 -0700 | [diff] [blame] | 2 | * Copyright (C) 2018-2023 The Android Open Source Project | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Maciej Żenczykowski | 283c25a | 2023-10-02 19:43:30 -0700 | [diff] [blame] | 17 | #define LOG_TAG "NetBpfLoader" | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | #include <errno.h> | 
|  | 20 | #include <fcntl.h> | 
|  | 21 | #include <linux/bpf.h> | 
|  | 22 | #include <linux/elf.h> | 
|  | 23 | #include <log/log.h> | 
|  | 24 | #include <stdint.h> | 
|  | 25 | #include <stdio.h> | 
|  | 26 | #include <stdlib.h> | 
|  | 27 | #include <string.h> | 
|  | 28 | #include <sysexits.h> | 
|  | 29 | #include <sys/stat.h> | 
|  | 30 | #include <sys/utsname.h> | 
|  | 31 | #include <sys/wait.h> | 
|  | 32 | #include <unistd.h> | 
|  | 33 |  | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 34 | #include "BpfSyscallWrappers.h" | 
|  | 35 | #include "bpf/BpfUtils.h" | 
|  | 36 | #include "bpf/bpf_map_def.h" | 
| Maciej Żenczykowski | 40dfe53 | 2023-10-08 20:21:11 -0700 | [diff] [blame] | 37 | #include "loader.h" | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 38 |  | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 39 | #include <cstdlib> | 
|  | 40 | #include <fstream> | 
|  | 41 | #include <iostream> | 
|  | 42 | #include <optional> | 
|  | 43 | #include <string> | 
|  | 44 | #include <unordered_map> | 
|  | 45 | #include <vector> | 
|  | 46 |  | 
|  | 47 | #include <android-base/cmsg.h> | 
|  | 48 | #include <android-base/file.h> | 
| Elliott Hughes | 497e3e0 | 2023-10-09 22:18:08 +0000 | [diff] [blame] | 49 | #include <android-base/properties.h> | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 50 | #include <android-base/strings.h> | 
|  | 51 | #include <android-base/unique_fd.h> | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 52 |  | 
|  | 53 | #define BPF_FS_PATH "/sys/fs/bpf/" | 
|  | 54 |  | 
|  | 55 | // Size of the BPF log buffer for verifier logging | 
|  | 56 | #define BPF_LOAD_LOG_SZ 0xfffff | 
|  | 57 |  | 
|  | 58 | // Unspecified attach type is 0 which is BPF_CGROUP_INET_INGRESS. | 
|  | 59 | #define BPF_ATTACH_TYPE_UNSPEC BPF_CGROUP_INET_INGRESS | 
|  | 60 |  | 
|  | 61 | using android::base::StartsWith; | 
|  | 62 | using android::base::unique_fd; | 
|  | 63 | using std::ifstream; | 
|  | 64 | using std::ios; | 
|  | 65 | using std::optional; | 
|  | 66 | using std::string; | 
|  | 67 | using std::vector; | 
|  | 68 |  | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 69 | namespace android { | 
|  | 70 | namespace bpf { | 
|  | 71 |  | 
|  | 72 | const std::string& getBuildType() { | 
| Elliott Hughes | 497e3e0 | 2023-10-09 22:18:08 +0000 | [diff] [blame] | 73 | static std::string t = android::base::GetProperty("ro.build.type", "unknown"); | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 74 | return t; | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | static unsigned int page_size = static_cast<unsigned int>(getpagesize()); | 
|  | 78 |  | 
|  | 79 | constexpr const char* lookupSelinuxContext(const domain d, const char* const unspecified = "") { | 
|  | 80 | switch (d) { | 
|  | 81 | case domain::unspecified:   return unspecified; | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 82 | case domain::tethering:     return "fs_bpf_tethering"; | 
|  | 83 | case domain::net_private:   return "fs_bpf_net_private"; | 
|  | 84 | case domain::net_shared:    return "fs_bpf_net_shared"; | 
|  | 85 | case domain::netd_readonly: return "fs_bpf_netd_readonly"; | 
|  | 86 | case domain::netd_shared:   return "fs_bpf_netd_shared"; | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 87 | default:                    return "(unrecognized)"; | 
|  | 88 | } | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | domain getDomainFromSelinuxContext(const char s[BPF_SELINUX_CONTEXT_CHAR_ARRAY_SIZE]) { | 
|  | 92 | for (domain d : AllDomains) { | 
|  | 93 | // Not sure how to enforce this at compile time, so abort() bpfloader at boot instead | 
|  | 94 | if (strlen(lookupSelinuxContext(d)) >= BPF_SELINUX_CONTEXT_CHAR_ARRAY_SIZE) abort(); | 
|  | 95 | if (!strncmp(s, lookupSelinuxContext(d), BPF_SELINUX_CONTEXT_CHAR_ARRAY_SIZE)) return d; | 
|  | 96 | } | 
|  | 97 | ALOGW("ignoring unrecognized selinux_context '%-32s'", s); | 
|  | 98 | // We should return 'unrecognized' here, however: returning unspecified will | 
|  | 99 | // result in the system simply using the default context, which in turn | 
|  | 100 | // will allow future expansion by adding more restrictive selinux types. | 
|  | 101 | // Older bpfloader will simply ignore that, and use the less restrictive default. | 
|  | 102 | // This does mean you CANNOT later add a *less* restrictive type than the default. | 
|  | 103 | // | 
|  | 104 | // Note: we cannot just abort() here as this might be a mainline module shipped optional update | 
|  | 105 | return domain::unspecified; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | constexpr const char* lookupPinSubdir(const domain d, const char* const unspecified = "") { | 
|  | 109 | switch (d) { | 
|  | 110 | case domain::unspecified:   return unspecified; | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 111 | case domain::tethering:     return "tethering/"; | 
|  | 112 | case domain::net_private:   return "net_private/"; | 
|  | 113 | case domain::net_shared:    return "net_shared/"; | 
|  | 114 | case domain::netd_readonly: return "netd_readonly/"; | 
|  | 115 | case domain::netd_shared:   return "netd_shared/"; | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 116 | default:                    return "(unrecognized)"; | 
|  | 117 | } | 
|  | 118 | }; | 
|  | 119 |  | 
|  | 120 | domain getDomainFromPinSubdir(const char s[BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE]) { | 
|  | 121 | for (domain d : AllDomains) { | 
|  | 122 | // Not sure how to enforce this at compile time, so abort() bpfloader at boot instead | 
|  | 123 | if (strlen(lookupPinSubdir(d)) >= BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE) abort(); | 
|  | 124 | if (!strncmp(s, lookupPinSubdir(d), BPF_PIN_SUBDIR_CHAR_ARRAY_SIZE)) return d; | 
|  | 125 | } | 
|  | 126 | ALOGE("unrecognized pin_subdir '%-32s'", s); | 
|  | 127 | // pin_subdir affects the object's full pathname, | 
|  | 128 | // and thus using the default would change the location and thus our code's ability to find it, | 
|  | 129 | // hence this seems worth treating as a true error condition. | 
|  | 130 | // | 
|  | 131 | // Note: we cannot just abort() here as this might be a mainline module shipped optional update | 
|  | 132 | // However, our callers will treat this as an error, and stop loading the specific .o, | 
|  | 133 | // which will fail bpfloader if the .o is marked critical. | 
|  | 134 | return domain::unrecognized; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | static string pathToObjName(const string& path) { | 
|  | 138 | // extract everything after the final slash, ie. this is the filename 'foo@1.o' or 'bar.o' | 
|  | 139 | string filename = android::base::Split(path, "/").back(); | 
|  | 140 | // strip off everything from the final period onwards (strip '.o' suffix), ie. 'foo@1' or 'bar' | 
|  | 141 | string name = filename.substr(0, filename.find_last_of('.')); | 
|  | 142 | // strip any potential @1 suffix, this will leave us with just 'foo' or 'bar' | 
|  | 143 | // this can be used to provide duplicate programs (mux based on the bpfloader version) | 
|  | 144 | return name.substr(0, name.find_last_of('@')); | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | typedef struct { | 
|  | 148 | const char* name; | 
|  | 149 | enum bpf_prog_type type; | 
|  | 150 | enum bpf_attach_type expected_attach_type; | 
|  | 151 | } sectionType; | 
|  | 152 |  | 
|  | 153 | /* | 
|  | 154 | * Map section name prefixes to program types, the section name will be: | 
|  | 155 | *   SECTION(<prefix>/<name-of-program>) | 
|  | 156 | * For example: | 
|  | 157 | *   SECTION("tracepoint/sched_switch_func") where sched_switch_funcs | 
|  | 158 | * is the name of the program, and tracepoint is the type. | 
|  | 159 | * | 
|  | 160 | * However, be aware that you should not be directly using the SECTION() macro. | 
|  | 161 | * Instead use the DEFINE_(BPF|XDP)_(PROG|MAP)... & LICENSE/CRITICAL macros. | 
| Maciej Żenczykowski | fcea70f | 2023-10-08 23:35:00 -0700 | [diff] [blame] | 162 | * | 
|  | 163 | * Programs shipped inside the tethering apex should be limited to networking stuff, | 
|  | 164 | * as KPROBE, PERF_EVENT, TRACEPOINT are dangerous to use from mainline updatable code, | 
|  | 165 | * since they are less stable abi/api and may conflict with platform uses of bpf. | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 166 | */ | 
|  | 167 | sectionType sectionNameTypes[] = { | 
|  | 168 | {"bind4/",         BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND}, | 
|  | 169 | {"bind6/",         BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND}, | 
|  | 170 | {"cgroupskb/",     BPF_PROG_TYPE_CGROUP_SKB,       BPF_ATTACH_TYPE_UNSPEC}, | 
|  | 171 | {"cgroupsock/",    BPF_PROG_TYPE_CGROUP_SOCK,      BPF_ATTACH_TYPE_UNSPEC}, | 
|  | 172 | {"connect4/",      BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT}, | 
|  | 173 | {"connect6/",      BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT}, | 
|  | 174 | {"egress/",        BPF_PROG_TYPE_CGROUP_SKB,       BPF_CGROUP_INET_EGRESS}, | 
|  | 175 | {"getsockopt/",    BPF_PROG_TYPE_CGROUP_SOCKOPT,   BPF_CGROUP_GETSOCKOPT}, | 
|  | 176 | {"ingress/",       BPF_PROG_TYPE_CGROUP_SKB,       BPF_CGROUP_INET_INGRESS}, | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 177 | {"lwt_in/",        BPF_PROG_TYPE_LWT_IN,           BPF_ATTACH_TYPE_UNSPEC}, | 
|  | 178 | {"lwt_out/",       BPF_PROG_TYPE_LWT_OUT,          BPF_ATTACH_TYPE_UNSPEC}, | 
|  | 179 | {"lwt_seg6local/", BPF_PROG_TYPE_LWT_SEG6LOCAL,    BPF_ATTACH_TYPE_UNSPEC}, | 
|  | 180 | {"lwt_xmit/",      BPF_PROG_TYPE_LWT_XMIT,         BPF_ATTACH_TYPE_UNSPEC}, | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 181 | {"postbind4/",     BPF_PROG_TYPE_CGROUP_SOCK,      BPF_CGROUP_INET4_POST_BIND}, | 
|  | 182 | {"postbind6/",     BPF_PROG_TYPE_CGROUP_SOCK,      BPF_CGROUP_INET6_POST_BIND}, | 
|  | 183 | {"recvmsg4/",      BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG}, | 
|  | 184 | {"recvmsg6/",      BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG}, | 
|  | 185 | {"schedact/",      BPF_PROG_TYPE_SCHED_ACT,        BPF_ATTACH_TYPE_UNSPEC}, | 
|  | 186 | {"schedcls/",      BPF_PROG_TYPE_SCHED_CLS,        BPF_ATTACH_TYPE_UNSPEC}, | 
|  | 187 | {"sendmsg4/",      BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG}, | 
|  | 188 | {"sendmsg6/",      BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG}, | 
|  | 189 | {"setsockopt/",    BPF_PROG_TYPE_CGROUP_SOCKOPT,   BPF_CGROUP_SETSOCKOPT}, | 
|  | 190 | {"skfilter/",      BPF_PROG_TYPE_SOCKET_FILTER,    BPF_ATTACH_TYPE_UNSPEC}, | 
|  | 191 | {"sockops/",       BPF_PROG_TYPE_SOCK_OPS,         BPF_CGROUP_SOCK_OPS}, | 
|  | 192 | {"sysctl",         BPF_PROG_TYPE_CGROUP_SYSCTL,    BPF_CGROUP_SYSCTL}, | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 193 | {"xdp/",           BPF_PROG_TYPE_XDP,              BPF_ATTACH_TYPE_UNSPEC}, | 
|  | 194 | }; | 
|  | 195 |  | 
|  | 196 | typedef struct { | 
|  | 197 | enum bpf_prog_type type; | 
|  | 198 | enum bpf_attach_type expected_attach_type; | 
|  | 199 | string name; | 
|  | 200 | vector<char> data; | 
|  | 201 | vector<char> rel_data; | 
|  | 202 | optional<struct bpf_prog_def> prog_def; | 
|  | 203 |  | 
|  | 204 | unique_fd prog_fd; /* fd after loading */ | 
|  | 205 | } codeSection; | 
|  | 206 |  | 
|  | 207 | static int readElfHeader(ifstream& elfFile, Elf64_Ehdr* eh) { | 
|  | 208 | elfFile.seekg(0); | 
|  | 209 | if (elfFile.fail()) return -1; | 
|  | 210 |  | 
|  | 211 | if (!elfFile.read((char*)eh, sizeof(*eh))) return -1; | 
|  | 212 |  | 
|  | 213 | return 0; | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | /* Reads all section header tables into an Shdr array */ | 
|  | 217 | static int readSectionHeadersAll(ifstream& elfFile, vector<Elf64_Shdr>& shTable) { | 
|  | 218 | Elf64_Ehdr eh; | 
|  | 219 | int ret = 0; | 
|  | 220 |  | 
|  | 221 | ret = readElfHeader(elfFile, &eh); | 
|  | 222 | if (ret) return ret; | 
|  | 223 |  | 
|  | 224 | elfFile.seekg(eh.e_shoff); | 
|  | 225 | if (elfFile.fail()) return -1; | 
|  | 226 |  | 
|  | 227 | /* Read shdr table entries */ | 
|  | 228 | shTable.resize(eh.e_shnum); | 
|  | 229 |  | 
|  | 230 | if (!elfFile.read((char*)shTable.data(), (eh.e_shnum * eh.e_shentsize))) return -ENOMEM; | 
|  | 231 |  | 
|  | 232 | return 0; | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | /* Read a section by its index - for ex to get sec hdr strtab blob */ | 
|  | 236 | static int readSectionByIdx(ifstream& elfFile, int id, vector<char>& sec) { | 
|  | 237 | vector<Elf64_Shdr> shTable; | 
|  | 238 | int ret = readSectionHeadersAll(elfFile, shTable); | 
|  | 239 | if (ret) return ret; | 
|  | 240 |  | 
|  | 241 | elfFile.seekg(shTable[id].sh_offset); | 
|  | 242 | if (elfFile.fail()) return -1; | 
|  | 243 |  | 
|  | 244 | sec.resize(shTable[id].sh_size); | 
|  | 245 | if (!elfFile.read(sec.data(), shTable[id].sh_size)) return -1; | 
|  | 246 |  | 
|  | 247 | return 0; | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | /* Read whole section header string table */ | 
|  | 251 | static int readSectionHeaderStrtab(ifstream& elfFile, vector<char>& strtab) { | 
|  | 252 | Elf64_Ehdr eh; | 
|  | 253 | int ret = readElfHeader(elfFile, &eh); | 
|  | 254 | if (ret) return ret; | 
|  | 255 |  | 
|  | 256 | ret = readSectionByIdx(elfFile, eh.e_shstrndx, strtab); | 
|  | 257 | if (ret) return ret; | 
|  | 258 |  | 
|  | 259 | return 0; | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | /* Get name from offset in strtab */ | 
|  | 263 | static int getSymName(ifstream& elfFile, int nameOff, string& name) { | 
|  | 264 | int ret; | 
|  | 265 | vector<char> secStrTab; | 
|  | 266 |  | 
|  | 267 | ret = readSectionHeaderStrtab(elfFile, secStrTab); | 
|  | 268 | if (ret) return ret; | 
|  | 269 |  | 
|  | 270 | if (nameOff >= (int)secStrTab.size()) return -1; | 
|  | 271 |  | 
|  | 272 | name = string((char*)secStrTab.data() + nameOff); | 
|  | 273 | return 0; | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | /* Reads a full section by name - example to get the GPL license */ | 
|  | 277 | static int readSectionByName(const char* name, ifstream& elfFile, vector<char>& data) { | 
|  | 278 | vector<char> secStrTab; | 
|  | 279 | vector<Elf64_Shdr> shTable; | 
|  | 280 | int ret; | 
|  | 281 |  | 
|  | 282 | ret = readSectionHeadersAll(elfFile, shTable); | 
|  | 283 | if (ret) return ret; | 
|  | 284 |  | 
|  | 285 | ret = readSectionHeaderStrtab(elfFile, secStrTab); | 
|  | 286 | if (ret) return ret; | 
|  | 287 |  | 
|  | 288 | for (int i = 0; i < (int)shTable.size(); i++) { | 
|  | 289 | char* secname = secStrTab.data() + shTable[i].sh_name; | 
|  | 290 | if (!secname) continue; | 
|  | 291 |  | 
|  | 292 | if (!strcmp(secname, name)) { | 
|  | 293 | vector<char> dataTmp; | 
|  | 294 | dataTmp.resize(shTable[i].sh_size); | 
|  | 295 |  | 
|  | 296 | elfFile.seekg(shTable[i].sh_offset); | 
|  | 297 | if (elfFile.fail()) return -1; | 
|  | 298 |  | 
|  | 299 | if (!elfFile.read((char*)dataTmp.data(), shTable[i].sh_size)) return -1; | 
|  | 300 |  | 
|  | 301 | data = dataTmp; | 
|  | 302 | return 0; | 
|  | 303 | } | 
|  | 304 | } | 
|  | 305 | return -2; | 
|  | 306 | } | 
|  | 307 |  | 
|  | 308 | unsigned int readSectionUint(const char* name, ifstream& elfFile, unsigned int defVal) { | 
|  | 309 | vector<char> theBytes; | 
|  | 310 | int ret = readSectionByName(name, elfFile, theBytes); | 
|  | 311 | if (ret) { | 
|  | 312 | ALOGD("Couldn't find section %s (defaulting to %u [0x%x]).", name, defVal, defVal); | 
|  | 313 | return defVal; | 
|  | 314 | } else if (theBytes.size() < sizeof(unsigned int)) { | 
|  | 315 | ALOGE("Section %s too short (defaulting to %u [0x%x]).", name, defVal, defVal); | 
|  | 316 | return defVal; | 
|  | 317 | } else { | 
|  | 318 | // decode first 4 bytes as LE32 uint, there will likely be more bytes due to alignment. | 
|  | 319 | unsigned int value = static_cast<unsigned char>(theBytes[3]); | 
|  | 320 | value <<= 8; | 
|  | 321 | value += static_cast<unsigned char>(theBytes[2]); | 
|  | 322 | value <<= 8; | 
|  | 323 | value += static_cast<unsigned char>(theBytes[1]); | 
|  | 324 | value <<= 8; | 
|  | 325 | value += static_cast<unsigned char>(theBytes[0]); | 
|  | 326 | ALOGI("Section %s value is %u [0x%x]", name, value, value); | 
|  | 327 | return value; | 
|  | 328 | } | 
|  | 329 | } | 
|  | 330 |  | 
|  | 331 | static int readSectionByType(ifstream& elfFile, int type, vector<char>& data) { | 
|  | 332 | int ret; | 
|  | 333 | vector<Elf64_Shdr> shTable; | 
|  | 334 |  | 
|  | 335 | ret = readSectionHeadersAll(elfFile, shTable); | 
|  | 336 | if (ret) return ret; | 
|  | 337 |  | 
|  | 338 | for (int i = 0; i < (int)shTable.size(); i++) { | 
|  | 339 | if ((int)shTable[i].sh_type != type) continue; | 
|  | 340 |  | 
|  | 341 | vector<char> dataTmp; | 
|  | 342 | dataTmp.resize(shTable[i].sh_size); | 
|  | 343 |  | 
|  | 344 | elfFile.seekg(shTable[i].sh_offset); | 
|  | 345 | if (elfFile.fail()) return -1; | 
|  | 346 |  | 
|  | 347 | if (!elfFile.read((char*)dataTmp.data(), shTable[i].sh_size)) return -1; | 
|  | 348 |  | 
|  | 349 | data = dataTmp; | 
|  | 350 | return 0; | 
|  | 351 | } | 
|  | 352 | return -2; | 
|  | 353 | } | 
|  | 354 |  | 
|  | 355 | static bool symCompare(Elf64_Sym a, Elf64_Sym b) { | 
|  | 356 | return (a.st_value < b.st_value); | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | static int readSymTab(ifstream& elfFile, int sort, vector<Elf64_Sym>& data) { | 
|  | 360 | int ret, numElems; | 
|  | 361 | Elf64_Sym* buf; | 
|  | 362 | vector<char> secData; | 
|  | 363 |  | 
|  | 364 | ret = readSectionByType(elfFile, SHT_SYMTAB, secData); | 
|  | 365 | if (ret) return ret; | 
|  | 366 |  | 
|  | 367 | buf = (Elf64_Sym*)secData.data(); | 
|  | 368 | numElems = (secData.size() / sizeof(Elf64_Sym)); | 
|  | 369 | data.assign(buf, buf + numElems); | 
|  | 370 |  | 
|  | 371 | if (sort) std::sort(data.begin(), data.end(), symCompare); | 
|  | 372 | return 0; | 
|  | 373 | } | 
|  | 374 |  | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 375 | static enum bpf_prog_type getSectionType(string& name) { | 
|  | 376 | for (auto& snt : sectionNameTypes) | 
|  | 377 | if (StartsWith(name, snt.name)) return snt.type; | 
|  | 378 |  | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 379 | return BPF_PROG_TYPE_UNSPEC; | 
|  | 380 | } | 
|  | 381 |  | 
|  | 382 | static enum bpf_attach_type getExpectedAttachType(string& name) { | 
|  | 383 | for (auto& snt : sectionNameTypes) | 
|  | 384 | if (StartsWith(name, snt.name)) return snt.expected_attach_type; | 
|  | 385 | return BPF_ATTACH_TYPE_UNSPEC; | 
|  | 386 | } | 
|  | 387 |  | 
| Maciej Żenczykowski | fcea70f | 2023-10-08 23:35:00 -0700 | [diff] [blame] | 388 | /* | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 389 | static string getSectionName(enum bpf_prog_type type) | 
|  | 390 | { | 
|  | 391 | for (auto& snt : sectionNameTypes) | 
|  | 392 | if (snt.type == type) | 
|  | 393 | return string(snt.name); | 
|  | 394 |  | 
|  | 395 | return "UNKNOWN SECTION NAME " + std::to_string(type); | 
|  | 396 | } | 
| Maciej Żenczykowski | fcea70f | 2023-10-08 23:35:00 -0700 | [diff] [blame] | 397 | */ | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 398 |  | 
|  | 399 | static int readProgDefs(ifstream& elfFile, vector<struct bpf_prog_def>& pd, | 
|  | 400 | size_t sizeOfBpfProgDef) { | 
|  | 401 | vector<char> pdData; | 
|  | 402 | int ret = readSectionByName("progs", elfFile, pdData); | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 403 | if (ret) return ret; | 
|  | 404 |  | 
|  | 405 | if (pdData.size() % sizeOfBpfProgDef) { | 
|  | 406 | ALOGE("readProgDefs failed due to improper sized progs section, %zu %% %zu != 0", | 
|  | 407 | pdData.size(), sizeOfBpfProgDef); | 
|  | 408 | return -1; | 
|  | 409 | }; | 
|  | 410 |  | 
|  | 411 | int progCount = pdData.size() / sizeOfBpfProgDef; | 
|  | 412 | pd.resize(progCount); | 
|  | 413 | size_t trimmedSize = std::min(sizeOfBpfProgDef, sizeof(struct bpf_prog_def)); | 
|  | 414 |  | 
|  | 415 | const char* dataPtr = pdData.data(); | 
|  | 416 | for (auto& p : pd) { | 
|  | 417 | // First we zero initialize | 
|  | 418 | memset(&p, 0, sizeof(p)); | 
|  | 419 | // Then we set non-zero defaults | 
|  | 420 | p.bpfloader_max_ver = DEFAULT_BPFLOADER_MAX_VER;  // v1.0 | 
|  | 421 | // Then we copy over the structure prefix from the ELF file. | 
|  | 422 | memcpy(&p, dataPtr, trimmedSize); | 
|  | 423 | // Move to next struct in the ELF file | 
|  | 424 | dataPtr += sizeOfBpfProgDef; | 
|  | 425 | } | 
|  | 426 | return 0; | 
|  | 427 | } | 
|  | 428 |  | 
|  | 429 | static int getSectionSymNames(ifstream& elfFile, const string& sectionName, vector<string>& names, | 
|  | 430 | optional<unsigned> symbolType = std::nullopt) { | 
|  | 431 | int ret; | 
|  | 432 | string name; | 
|  | 433 | vector<Elf64_Sym> symtab; | 
|  | 434 | vector<Elf64_Shdr> shTable; | 
|  | 435 |  | 
|  | 436 | ret = readSymTab(elfFile, 1 /* sort */, symtab); | 
|  | 437 | if (ret) return ret; | 
|  | 438 |  | 
|  | 439 | /* Get index of section */ | 
|  | 440 | ret = readSectionHeadersAll(elfFile, shTable); | 
|  | 441 | if (ret) return ret; | 
|  | 442 |  | 
|  | 443 | int sec_idx = -1; | 
|  | 444 | for (int i = 0; i < (int)shTable.size(); i++) { | 
|  | 445 | ret = getSymName(elfFile, shTable[i].sh_name, name); | 
|  | 446 | if (ret) return ret; | 
|  | 447 |  | 
|  | 448 | if (!name.compare(sectionName)) { | 
|  | 449 | sec_idx = i; | 
|  | 450 | break; | 
|  | 451 | } | 
|  | 452 | } | 
|  | 453 |  | 
|  | 454 | /* No section found with matching name*/ | 
|  | 455 | if (sec_idx == -1) { | 
|  | 456 | ALOGW("No %s section could be found in elf object", sectionName.c_str()); | 
|  | 457 | return -1; | 
|  | 458 | } | 
|  | 459 |  | 
|  | 460 | for (int i = 0; i < (int)symtab.size(); i++) { | 
|  | 461 | if (symbolType.has_value() && ELF_ST_TYPE(symtab[i].st_info) != symbolType) continue; | 
|  | 462 |  | 
|  | 463 | if (symtab[i].st_shndx == sec_idx) { | 
|  | 464 | string s; | 
|  | 465 | ret = getSymName(elfFile, symtab[i].st_name, s); | 
|  | 466 | if (ret) return ret; | 
|  | 467 | names.push_back(s); | 
|  | 468 | } | 
|  | 469 | } | 
|  | 470 |  | 
|  | 471 | return 0; | 
|  | 472 | } | 
|  | 473 |  | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 474 | /* Read a section by its index - for ex to get sec hdr strtab blob */ | 
| Maciej Żenczykowski | fcea70f | 2023-10-08 23:35:00 -0700 | [diff] [blame] | 475 | static int readCodeSections(ifstream& elfFile, vector<codeSection>& cs, size_t sizeOfBpfProgDef) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 476 | vector<Elf64_Shdr> shTable; | 
|  | 477 | int entries, ret = 0; | 
|  | 478 |  | 
|  | 479 | ret = readSectionHeadersAll(elfFile, shTable); | 
|  | 480 | if (ret) return ret; | 
|  | 481 | entries = shTable.size(); | 
|  | 482 |  | 
|  | 483 | vector<struct bpf_prog_def> pd; | 
|  | 484 | ret = readProgDefs(elfFile, pd, sizeOfBpfProgDef); | 
|  | 485 | if (ret) return ret; | 
|  | 486 | vector<string> progDefNames; | 
|  | 487 | ret = getSectionSymNames(elfFile, "progs", progDefNames); | 
|  | 488 | if (!pd.empty() && ret) return ret; | 
|  | 489 |  | 
|  | 490 | for (int i = 0; i < entries; i++) { | 
|  | 491 | string name; | 
|  | 492 | codeSection cs_temp; | 
|  | 493 | cs_temp.type = BPF_PROG_TYPE_UNSPEC; | 
|  | 494 |  | 
|  | 495 | ret = getSymName(elfFile, shTable[i].sh_name, name); | 
|  | 496 | if (ret) return ret; | 
|  | 497 |  | 
|  | 498 | enum bpf_prog_type ptype = getSectionType(name); | 
|  | 499 |  | 
|  | 500 | if (ptype == BPF_PROG_TYPE_UNSPEC) continue; | 
|  | 501 |  | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 502 | // This must be done before '/' is replaced with '_'. | 
|  | 503 | cs_temp.expected_attach_type = getExpectedAttachType(name); | 
|  | 504 |  | 
|  | 505 | string oldName = name; | 
|  | 506 |  | 
|  | 507 | // convert all slashes to underscores | 
|  | 508 | std::replace(name.begin(), name.end(), '/', '_'); | 
|  | 509 |  | 
|  | 510 | cs_temp.type = ptype; | 
|  | 511 | cs_temp.name = name; | 
|  | 512 |  | 
|  | 513 | ret = readSectionByIdx(elfFile, i, cs_temp.data); | 
|  | 514 | if (ret) return ret; | 
|  | 515 | ALOGD("Loaded code section %d (%s)", i, name.c_str()); | 
|  | 516 |  | 
|  | 517 | vector<string> csSymNames; | 
|  | 518 | ret = getSectionSymNames(elfFile, oldName, csSymNames, STT_FUNC); | 
|  | 519 | if (ret || !csSymNames.size()) return ret; | 
|  | 520 | for (size_t i = 0; i < progDefNames.size(); ++i) { | 
|  | 521 | if (!progDefNames[i].compare(csSymNames[0] + "_def")) { | 
|  | 522 | cs_temp.prog_def = pd[i]; | 
|  | 523 | break; | 
|  | 524 | } | 
|  | 525 | } | 
|  | 526 |  | 
|  | 527 | /* Check for rel section */ | 
|  | 528 | if (cs_temp.data.size() > 0 && i < entries) { | 
|  | 529 | ret = getSymName(elfFile, shTable[i + 1].sh_name, name); | 
|  | 530 | if (ret) return ret; | 
|  | 531 |  | 
|  | 532 | if (name == (".rel" + oldName)) { | 
|  | 533 | ret = readSectionByIdx(elfFile, i + 1, cs_temp.rel_data); | 
|  | 534 | if (ret) return ret; | 
|  | 535 | ALOGD("Loaded relo section %d (%s)", i, name.c_str()); | 
|  | 536 | } | 
|  | 537 | } | 
|  | 538 |  | 
|  | 539 | if (cs_temp.data.size() > 0) { | 
|  | 540 | cs.push_back(std::move(cs_temp)); | 
|  | 541 | ALOGD("Adding section %d to cs list", i); | 
|  | 542 | } | 
|  | 543 | } | 
|  | 544 | return 0; | 
|  | 545 | } | 
|  | 546 |  | 
|  | 547 | static int getSymNameByIdx(ifstream& elfFile, int index, string& name) { | 
|  | 548 | vector<Elf64_Sym> symtab; | 
|  | 549 | int ret = 0; | 
|  | 550 |  | 
|  | 551 | ret = readSymTab(elfFile, 0 /* !sort */, symtab); | 
|  | 552 | if (ret) return ret; | 
|  | 553 |  | 
|  | 554 | if (index >= (int)symtab.size()) return -1; | 
|  | 555 |  | 
|  | 556 | return getSymName(elfFile, symtab[index].st_name, name); | 
|  | 557 | } | 
|  | 558 |  | 
|  | 559 | static bool mapMatchesExpectations(const unique_fd& fd, const string& mapName, | 
|  | 560 | const struct bpf_map_def& mapDef, const enum bpf_map_type type) { | 
| Maciej Żenczykowski | 7e5b09a | 2024-03-11 12:40:06 +0000 | [diff] [blame] | 561 | // bpfGetFd... family of functions require at minimum a 4.14 kernel, | 
|  | 562 | // so on 4.9-T kernels just pretend the map matches our expectations. | 
|  | 563 | // Additionally we'll get almost equivalent test coverage on newer devices/kernels. | 
|  | 564 | // This is because the primary failure mode we're trying to detect here | 
|  | 565 | // is either a source code misconfiguration (which is likely kernel independent) | 
|  | 566 | // or a newly introduced kernel feature/bug (which is unlikely to get backported to 4.9). | 
|  | 567 | if (!isAtLeastKernelVersion(4, 14, 0)) return true; | 
|  | 568 |  | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 569 | // Assuming fd is a valid Bpf Map file descriptor then | 
|  | 570 | // all the following should always succeed on a 4.14+ kernel. | 
|  | 571 | // If they somehow do fail, they'll return -1 (and set errno), | 
|  | 572 | // which should then cause (among others) a key_size mismatch. | 
|  | 573 | int fd_type = bpfGetFdMapType(fd); | 
|  | 574 | int fd_key_size = bpfGetFdKeySize(fd); | 
|  | 575 | int fd_value_size = bpfGetFdValueSize(fd); | 
|  | 576 | int fd_max_entries = bpfGetFdMaxEntries(fd); | 
|  | 577 | int fd_map_flags = bpfGetFdMapFlags(fd); | 
|  | 578 |  | 
|  | 579 | // DEVMAPs are readonly from the bpf program side's point of view, as such | 
|  | 580 | // the kernel in kernel/bpf/devmap.c dev_map_init_map() will set the flag | 
|  | 581 | int desired_map_flags = (int)mapDef.map_flags; | 
|  | 582 | if (type == BPF_MAP_TYPE_DEVMAP || type == BPF_MAP_TYPE_DEVMAP_HASH) | 
|  | 583 | desired_map_flags |= BPF_F_RDONLY_PROG; | 
|  | 584 |  | 
|  | 585 | // The .h file enforces that this is a power of two, and page size will | 
|  | 586 | // also always be a power of two, so this logic is actually enough to | 
|  | 587 | // force it to be a multiple of the page size, as required by the kernel. | 
|  | 588 | unsigned int desired_max_entries = mapDef.max_entries; | 
|  | 589 | if (type == BPF_MAP_TYPE_RINGBUF) { | 
|  | 590 | if (desired_max_entries < page_size) desired_max_entries = page_size; | 
|  | 591 | } | 
|  | 592 |  | 
|  | 593 | // The following checks should *never* trigger, if one of them somehow does, | 
|  | 594 | // it probably means a bpf .o file has been changed/replaced at runtime | 
|  | 595 | // and bpfloader was manually rerun (normally it should only run *once* | 
|  | 596 | // early during the boot process). | 
|  | 597 | // Another possibility is that something is misconfigured in the code: | 
|  | 598 | // most likely a shared map is declared twice differently. | 
|  | 599 | // But such a change should never be checked into the source tree... | 
|  | 600 | if ((fd_type == type) && | 
|  | 601 | (fd_key_size == (int)mapDef.key_size) && | 
|  | 602 | (fd_value_size == (int)mapDef.value_size) && | 
|  | 603 | (fd_max_entries == (int)desired_max_entries) && | 
|  | 604 | (fd_map_flags == desired_map_flags)) { | 
|  | 605 | return true; | 
|  | 606 | } | 
|  | 607 |  | 
|  | 608 | ALOGE("bpf map name %s mismatch: desired/found: " | 
|  | 609 | "type:%d/%d key:%u/%d value:%u/%d entries:%u/%d flags:%u/%d", | 
|  | 610 | mapName.c_str(), type, fd_type, mapDef.key_size, fd_key_size, mapDef.value_size, | 
|  | 611 | fd_value_size, mapDef.max_entries, fd_max_entries, desired_map_flags, fd_map_flags); | 
|  | 612 | return false; | 
|  | 613 | } | 
|  | 614 |  | 
|  | 615 | static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>& mapFds, | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 616 | const char* prefix, const size_t sizeOfBpfMapDef, | 
|  | 617 | const unsigned int bpfloader_ver) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 618 | int ret; | 
|  | 619 | vector<char> mdData; | 
|  | 620 | vector<struct bpf_map_def> md; | 
|  | 621 | vector<string> mapNames; | 
|  | 622 | string objName = pathToObjName(string(elfPath)); | 
|  | 623 |  | 
|  | 624 | ret = readSectionByName("maps", elfFile, mdData); | 
|  | 625 | if (ret == -2) return 0;  // no maps to read | 
|  | 626 | if (ret) return ret; | 
|  | 627 |  | 
|  | 628 | if (mdData.size() % sizeOfBpfMapDef) { | 
|  | 629 | ALOGE("createMaps failed due to improper sized maps section, %zu %% %zu != 0", | 
|  | 630 | mdData.size(), sizeOfBpfMapDef); | 
|  | 631 | return -1; | 
|  | 632 | }; | 
|  | 633 |  | 
|  | 634 | int mapCount = mdData.size() / sizeOfBpfMapDef; | 
|  | 635 | md.resize(mapCount); | 
|  | 636 | size_t trimmedSize = std::min(sizeOfBpfMapDef, sizeof(struct bpf_map_def)); | 
|  | 637 |  | 
|  | 638 | const char* dataPtr = mdData.data(); | 
|  | 639 | for (auto& m : md) { | 
|  | 640 | // First we zero initialize | 
|  | 641 | memset(&m, 0, sizeof(m)); | 
|  | 642 | // Then we set non-zero defaults | 
|  | 643 | m.bpfloader_max_ver = DEFAULT_BPFLOADER_MAX_VER;  // v1.0 | 
|  | 644 | m.max_kver = 0xFFFFFFFFu;                         // matches KVER_INF from bpf_helpers.h | 
|  | 645 | // Then we copy over the structure prefix from the ELF file. | 
|  | 646 | memcpy(&m, dataPtr, trimmedSize); | 
|  | 647 | // Move to next struct in the ELF file | 
|  | 648 | dataPtr += sizeOfBpfMapDef; | 
|  | 649 | } | 
|  | 650 |  | 
|  | 651 | ret = getSectionSymNames(elfFile, "maps", mapNames); | 
|  | 652 | if (ret) return ret; | 
|  | 653 |  | 
|  | 654 | unsigned kvers = kernelVersion(); | 
|  | 655 |  | 
|  | 656 | for (int i = 0; i < (int)mapNames.size(); i++) { | 
|  | 657 | if (md[i].zero != 0) abort(); | 
|  | 658 |  | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 659 | if (bpfloader_ver < md[i].bpfloader_min_ver) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 660 | ALOGI("skipping map %s which requires bpfloader min ver 0x%05x", mapNames[i].c_str(), | 
|  | 661 | md[i].bpfloader_min_ver); | 
|  | 662 | mapFds.push_back(unique_fd()); | 
|  | 663 | continue; | 
|  | 664 | } | 
|  | 665 |  | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 666 | if (bpfloader_ver >= md[i].bpfloader_max_ver) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 667 | ALOGI("skipping map %s which requires bpfloader max ver 0x%05x", mapNames[i].c_str(), | 
|  | 668 | md[i].bpfloader_max_ver); | 
|  | 669 | mapFds.push_back(unique_fd()); | 
|  | 670 | continue; | 
|  | 671 | } | 
|  | 672 |  | 
|  | 673 | if (kvers < md[i].min_kver) { | 
|  | 674 | ALOGI("skipping map %s which requires kernel version 0x%x >= 0x%x", | 
|  | 675 | mapNames[i].c_str(), kvers, md[i].min_kver); | 
|  | 676 | mapFds.push_back(unique_fd()); | 
|  | 677 | continue; | 
|  | 678 | } | 
|  | 679 |  | 
|  | 680 | if (kvers >= md[i].max_kver) { | 
|  | 681 | ALOGI("skipping map %s which requires kernel version 0x%x < 0x%x", | 
|  | 682 | mapNames[i].c_str(), kvers, md[i].max_kver); | 
|  | 683 | mapFds.push_back(unique_fd()); | 
|  | 684 | continue; | 
|  | 685 | } | 
|  | 686 |  | 
|  | 687 | if ((md[i].ignore_on_eng && isEng()) || (md[i].ignore_on_user && isUser()) || | 
|  | 688 | (md[i].ignore_on_userdebug && isUserdebug())) { | 
|  | 689 | ALOGI("skipping map %s which is ignored on %s builds", mapNames[i].c_str(), | 
|  | 690 | getBuildType().c_str()); | 
|  | 691 | mapFds.push_back(unique_fd()); | 
|  | 692 | continue; | 
|  | 693 | } | 
|  | 694 |  | 
|  | 695 | if ((isArm() && isKernel32Bit() && md[i].ignore_on_arm32) || | 
|  | 696 | (isArm() && isKernel64Bit() && md[i].ignore_on_aarch64) || | 
|  | 697 | (isX86() && isKernel32Bit() && md[i].ignore_on_x86_32) || | 
|  | 698 | (isX86() && isKernel64Bit() && md[i].ignore_on_x86_64) || | 
|  | 699 | (isRiscV() && md[i].ignore_on_riscv64)) { | 
|  | 700 | ALOGI("skipping map %s which is ignored on %s", mapNames[i].c_str(), | 
|  | 701 | describeArch()); | 
|  | 702 | mapFds.push_back(unique_fd()); | 
|  | 703 | continue; | 
|  | 704 | } | 
|  | 705 |  | 
|  | 706 | enum bpf_map_type type = md[i].type; | 
| Maciej Żenczykowski | 7e5b09a | 2024-03-11 12:40:06 +0000 | [diff] [blame] | 707 | if (type == BPF_MAP_TYPE_DEVMAP && !isAtLeastKernelVersion(4, 14, 0)) { | 
|  | 708 | // On Linux Kernels older than 4.14 this map type doesn't exist, but it can kind | 
|  | 709 | // of be approximated: ARRAY has the same userspace api, though it is not usable | 
|  | 710 | // by the same ebpf programs.  However, that's okay because the bpf_redirect_map() | 
|  | 711 | // helper doesn't exist on 4.9-T anyway (so the bpf program would fail to load, | 
|  | 712 | // and thus needs to be tagged as 4.14+ either way), so there's nothing useful you | 
|  | 713 | // could do with a DEVMAP anyway (that isn't already provided by an ARRAY)... | 
|  | 714 | // Hence using an ARRAY instead of a DEVMAP simply makes life easier for userspace. | 
|  | 715 | type = BPF_MAP_TYPE_ARRAY; | 
|  | 716 | } | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 717 | if (type == BPF_MAP_TYPE_DEVMAP_HASH && !isAtLeastKernelVersion(5, 4, 0)) { | 
|  | 718 | // On Linux Kernels older than 5.4 this map type doesn't exist, but it can kind | 
|  | 719 | // of be approximated: HASH has the same userspace visible api. | 
|  | 720 | // However it cannot be used by ebpf programs in the same way. | 
|  | 721 | // Since bpf_redirect_map() only requires 4.14, a program using a DEVMAP_HASH map | 
|  | 722 | // would fail to load (due to trying to redirect to a HASH instead of DEVMAP_HASH). | 
|  | 723 | // One must thus tag any BPF_MAP_TYPE_DEVMAP_HASH + bpf_redirect_map() using | 
|  | 724 | // programs as being 5.4+... | 
|  | 725 | type = BPF_MAP_TYPE_HASH; | 
|  | 726 | } | 
|  | 727 |  | 
|  | 728 | // The .h file enforces that this is a power of two, and page size will | 
|  | 729 | // also always be a power of two, so this logic is actually enough to | 
|  | 730 | // force it to be a multiple of the page size, as required by the kernel. | 
|  | 731 | unsigned int max_entries = md[i].max_entries; | 
|  | 732 | if (type == BPF_MAP_TYPE_RINGBUF) { | 
|  | 733 | if (max_entries < page_size) max_entries = page_size; | 
|  | 734 | } | 
|  | 735 |  | 
|  | 736 | domain selinux_context = getDomainFromSelinuxContext(md[i].selinux_context); | 
|  | 737 | if (specified(selinux_context)) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 738 | ALOGI("map %s selinux_context [%-32s] -> %d -> '%s' (%s)", mapNames[i].c_str(), | 
|  | 739 | md[i].selinux_context, selinux_context, lookupSelinuxContext(selinux_context), | 
|  | 740 | lookupPinSubdir(selinux_context)); | 
|  | 741 | } | 
|  | 742 |  | 
|  | 743 | domain pin_subdir = getDomainFromPinSubdir(md[i].pin_subdir); | 
|  | 744 | if (unrecognized(pin_subdir)) return -ENOTDIR; | 
|  | 745 | if (specified(pin_subdir)) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 746 | ALOGI("map %s pin_subdir [%-32s] -> %d -> '%s'", mapNames[i].c_str(), md[i].pin_subdir, | 
|  | 747 | pin_subdir, lookupPinSubdir(pin_subdir)); | 
|  | 748 | } | 
|  | 749 |  | 
|  | 750 | // Format of pin location is /sys/fs/bpf/<pin_subdir|prefix>map_<objName>_<mapName> | 
|  | 751 | // except that maps shared across .o's have empty <objName> | 
|  | 752 | // Note: <objName> refers to the extension-less basename of the .o file (without @ suffix). | 
|  | 753 | string mapPinLoc = string(BPF_FS_PATH) + lookupPinSubdir(pin_subdir, prefix) + "map_" + | 
|  | 754 | (md[i].shared ? "" : objName) + "_" + mapNames[i]; | 
|  | 755 | bool reuse = false; | 
|  | 756 | unique_fd fd; | 
|  | 757 | int saved_errno; | 
|  | 758 |  | 
|  | 759 | if (access(mapPinLoc.c_str(), F_OK) == 0) { | 
|  | 760 | fd.reset(mapRetrieveRO(mapPinLoc.c_str())); | 
|  | 761 | saved_errno = errno; | 
|  | 762 | ALOGD("bpf_create_map reusing map %s, ret: %d", mapNames[i].c_str(), fd.get()); | 
|  | 763 | reuse = true; | 
|  | 764 | } else { | 
|  | 765 | union bpf_attr req = { | 
|  | 766 | .map_type = type, | 
|  | 767 | .key_size = md[i].key_size, | 
|  | 768 | .value_size = md[i].value_size, | 
|  | 769 | .max_entries = max_entries, | 
|  | 770 | .map_flags = md[i].map_flags, | 
|  | 771 | }; | 
| Maciej Żenczykowski | 2283635 | 2024-03-15 19:03:13 +0000 | [diff] [blame] | 772 | if (isAtLeastKernelVersion(4, 14, 0)) | 
|  | 773 | strlcpy(req.map_name, mapNames[i].c_str(), sizeof(req.map_name)); | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 774 | fd.reset(bpf(BPF_MAP_CREATE, req)); | 
|  | 775 | saved_errno = errno; | 
|  | 776 | ALOGD("bpf_create_map name %s, ret: %d", mapNames[i].c_str(), fd.get()); | 
|  | 777 | } | 
|  | 778 |  | 
|  | 779 | if (!fd.ok()) return -saved_errno; | 
|  | 780 |  | 
|  | 781 | // When reusing a pinned map, we need to check the map type/sizes/etc match, but for | 
|  | 782 | // safety (since reuse code path is rare) run these checks even if we just created it. | 
|  | 783 | // We assume failure is due to pinned map mismatch, hence the 'NOT UNIQUE' return code. | 
|  | 784 | if (!mapMatchesExpectations(fd, mapNames[i], md[i], type)) return -ENOTUNIQ; | 
|  | 785 |  | 
|  | 786 | if (!reuse) { | 
|  | 787 | if (specified(selinux_context)) { | 
|  | 788 | string createLoc = string(BPF_FS_PATH) + lookupPinSubdir(selinux_context) + | 
|  | 789 | "tmp_map_" + objName + "_" + mapNames[i]; | 
|  | 790 | ret = bpfFdPin(fd, createLoc.c_str()); | 
|  | 791 | if (ret) { | 
|  | 792 | int err = errno; | 
|  | 793 | ALOGE("create %s -> %d [%d:%s]", createLoc.c_str(), ret, err, strerror(err)); | 
|  | 794 | return -err; | 
|  | 795 | } | 
|  | 796 | ret = renameat2(AT_FDCWD, createLoc.c_str(), | 
|  | 797 | AT_FDCWD, mapPinLoc.c_str(), RENAME_NOREPLACE); | 
|  | 798 | if (ret) { | 
|  | 799 | int err = errno; | 
|  | 800 | ALOGE("rename %s %s -> %d [%d:%s]", createLoc.c_str(), mapPinLoc.c_str(), ret, | 
|  | 801 | err, strerror(err)); | 
|  | 802 | return -err; | 
|  | 803 | } | 
|  | 804 | } else { | 
|  | 805 | ret = bpfFdPin(fd, mapPinLoc.c_str()); | 
|  | 806 | if (ret) { | 
|  | 807 | int err = errno; | 
|  | 808 | ALOGE("pin %s -> %d [%d:%s]", mapPinLoc.c_str(), ret, err, strerror(err)); | 
|  | 809 | return -err; | 
|  | 810 | } | 
|  | 811 | } | 
|  | 812 | ret = chmod(mapPinLoc.c_str(), md[i].mode); | 
|  | 813 | if (ret) { | 
|  | 814 | int err = errno; | 
|  | 815 | ALOGE("chmod(%s, 0%o) = %d [%d:%s]", mapPinLoc.c_str(), md[i].mode, ret, err, | 
|  | 816 | strerror(err)); | 
|  | 817 | return -err; | 
|  | 818 | } | 
|  | 819 | ret = chown(mapPinLoc.c_str(), (uid_t)md[i].uid, (gid_t)md[i].gid); | 
|  | 820 | if (ret) { | 
|  | 821 | int err = errno; | 
|  | 822 | ALOGE("chown(%s, %u, %u) = %d [%d:%s]", mapPinLoc.c_str(), md[i].uid, md[i].gid, | 
|  | 823 | ret, err, strerror(err)); | 
|  | 824 | return -err; | 
|  | 825 | } | 
|  | 826 | } | 
|  | 827 |  | 
|  | 828 | int mapId = bpfGetFdMapId(fd); | 
|  | 829 | if (mapId == -1) { | 
|  | 830 | ALOGE("bpfGetFdMapId failed, ret: %d [%d]", mapId, errno); | 
|  | 831 | } else { | 
|  | 832 | ALOGI("map %s id %d", mapPinLoc.c_str(), mapId); | 
|  | 833 | } | 
|  | 834 |  | 
|  | 835 | mapFds.push_back(std::move(fd)); | 
|  | 836 | } | 
|  | 837 |  | 
|  | 838 | return ret; | 
|  | 839 | } | 
|  | 840 |  | 
|  | 841 | /* For debugging, dump all instructions */ | 
|  | 842 | static void dumpIns(char* ins, int size) { | 
|  | 843 | for (int row = 0; row < size / 8; row++) { | 
|  | 844 | ALOGE("%d: ", row); | 
|  | 845 | for (int j = 0; j < 8; j++) { | 
|  | 846 | ALOGE("%3x ", ins[(row * 8) + j]); | 
|  | 847 | } | 
|  | 848 | ALOGE("\n"); | 
|  | 849 | } | 
|  | 850 | } | 
|  | 851 |  | 
|  | 852 | /* For debugging, dump all code sections from cs list */ | 
|  | 853 | static void dumpAllCs(vector<codeSection>& cs) { | 
|  | 854 | for (int i = 0; i < (int)cs.size(); i++) { | 
|  | 855 | ALOGE("Dumping cs %d, name %s", int(i), cs[i].name.c_str()); | 
|  | 856 | dumpIns((char*)cs[i].data.data(), cs[i].data.size()); | 
|  | 857 | ALOGE("-----------"); | 
|  | 858 | } | 
|  | 859 | } | 
|  | 860 |  | 
|  | 861 | static void applyRelo(void* insnsPtr, Elf64_Addr offset, int fd) { | 
|  | 862 | int insnIndex; | 
|  | 863 | struct bpf_insn *insn, *insns; | 
|  | 864 |  | 
|  | 865 | insns = (struct bpf_insn*)(insnsPtr); | 
|  | 866 |  | 
|  | 867 | insnIndex = offset / sizeof(struct bpf_insn); | 
|  | 868 | insn = &insns[insnIndex]; | 
|  | 869 |  | 
|  | 870 | // Occasionally might be useful for relocation debugging, but pretty spammy | 
|  | 871 | if (0) { | 
|  | 872 | ALOGD("applying relo to instruction at byte offset: %llu, " | 
|  | 873 | "insn offset %d, insn %llx", | 
|  | 874 | (unsigned long long)offset, insnIndex, *(unsigned long long*)insn); | 
|  | 875 | } | 
|  | 876 |  | 
|  | 877 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) { | 
|  | 878 | ALOGE("Dumping all instructions till ins %d", insnIndex); | 
|  | 879 | ALOGE("invalid relo for insn %d: code 0x%x", insnIndex, insn->code); | 
|  | 880 | dumpIns((char*)insnsPtr, (insnIndex + 3) * 8); | 
|  | 881 | return; | 
|  | 882 | } | 
|  | 883 |  | 
|  | 884 | insn->imm = fd; | 
|  | 885 | insn->src_reg = BPF_PSEUDO_MAP_FD; | 
|  | 886 | } | 
|  | 887 |  | 
|  | 888 | static void applyMapRelo(ifstream& elfFile, vector<unique_fd> &mapFds, vector<codeSection>& cs) { | 
|  | 889 | vector<string> mapNames; | 
|  | 890 |  | 
|  | 891 | int ret = getSectionSymNames(elfFile, "maps", mapNames); | 
|  | 892 | if (ret) return; | 
|  | 893 |  | 
|  | 894 | for (int k = 0; k != (int)cs.size(); k++) { | 
|  | 895 | Elf64_Rel* rel = (Elf64_Rel*)(cs[k].rel_data.data()); | 
|  | 896 | int n_rel = cs[k].rel_data.size() / sizeof(*rel); | 
|  | 897 |  | 
|  | 898 | for (int i = 0; i < n_rel; i++) { | 
|  | 899 | int symIndex = ELF64_R_SYM(rel[i].r_info); | 
|  | 900 | string symName; | 
|  | 901 |  | 
|  | 902 | ret = getSymNameByIdx(elfFile, symIndex, symName); | 
|  | 903 | if (ret) return; | 
|  | 904 |  | 
|  | 905 | /* Find the map fd and apply relo */ | 
|  | 906 | for (int j = 0; j < (int)mapNames.size(); j++) { | 
|  | 907 | if (!mapNames[j].compare(symName)) { | 
|  | 908 | applyRelo(cs[k].data.data(), rel[i].r_offset, mapFds[j]); | 
|  | 909 | break; | 
|  | 910 | } | 
|  | 911 | } | 
|  | 912 | } | 
|  | 913 | } | 
|  | 914 | } | 
|  | 915 |  | 
|  | 916 | static int loadCodeSections(const char* elfPath, vector<codeSection>& cs, const string& license, | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 917 | const char* prefix, const unsigned int bpfloader_ver) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 918 | unsigned kvers = kernelVersion(); | 
|  | 919 |  | 
|  | 920 | if (!kvers) { | 
|  | 921 | ALOGE("unable to get kernel version"); | 
|  | 922 | return -EINVAL; | 
|  | 923 | } | 
|  | 924 |  | 
|  | 925 | string objName = pathToObjName(string(elfPath)); | 
|  | 926 |  | 
|  | 927 | for (int i = 0; i < (int)cs.size(); i++) { | 
|  | 928 | unique_fd& fd = cs[i].prog_fd; | 
|  | 929 | int ret; | 
|  | 930 | string name = cs[i].name; | 
|  | 931 |  | 
|  | 932 | if (!cs[i].prog_def.has_value()) { | 
|  | 933 | ALOGE("[%d] '%s' missing program definition! bad bpf.o build?", i, name.c_str()); | 
|  | 934 | return -EINVAL; | 
|  | 935 | } | 
|  | 936 |  | 
|  | 937 | unsigned min_kver = cs[i].prog_def->min_kver; | 
|  | 938 | unsigned max_kver = cs[i].prog_def->max_kver; | 
|  | 939 | ALOGD("cs[%d].name:%s min_kver:%x .max_kver:%x (kvers:%x)", i, name.c_str(), min_kver, | 
|  | 940 | max_kver, kvers); | 
|  | 941 | if (kvers < min_kver) continue; | 
|  | 942 | if (kvers >= max_kver) continue; | 
|  | 943 |  | 
|  | 944 | unsigned bpfMinVer = cs[i].prog_def->bpfloader_min_ver; | 
|  | 945 | unsigned bpfMaxVer = cs[i].prog_def->bpfloader_max_ver; | 
|  | 946 | domain selinux_context = getDomainFromSelinuxContext(cs[i].prog_def->selinux_context); | 
|  | 947 | domain pin_subdir = getDomainFromPinSubdir(cs[i].prog_def->pin_subdir); | 
|  | 948 | // Note: make sure to only check for unrecognized *after* verifying bpfloader | 
|  | 949 | // version limits include this bpfloader's version. | 
|  | 950 |  | 
|  | 951 | ALOGD("cs[%d].name:%s requires bpfloader version [0x%05x,0x%05x)", i, name.c_str(), | 
|  | 952 | bpfMinVer, bpfMaxVer); | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 953 | if (bpfloader_ver < bpfMinVer) continue; | 
|  | 954 | if (bpfloader_ver >= bpfMaxVer) continue; | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 955 |  | 
|  | 956 | if ((cs[i].prog_def->ignore_on_eng && isEng()) || | 
|  | 957 | (cs[i].prog_def->ignore_on_user && isUser()) || | 
|  | 958 | (cs[i].prog_def->ignore_on_userdebug && isUserdebug())) { | 
|  | 959 | ALOGD("cs[%d].name:%s is ignored on %s builds", i, name.c_str(), | 
|  | 960 | getBuildType().c_str()); | 
|  | 961 | continue; | 
|  | 962 | } | 
|  | 963 |  | 
|  | 964 | if ((isArm() && isKernel32Bit() && cs[i].prog_def->ignore_on_arm32) || | 
|  | 965 | (isArm() && isKernel64Bit() && cs[i].prog_def->ignore_on_aarch64) || | 
|  | 966 | (isX86() && isKernel32Bit() && cs[i].prog_def->ignore_on_x86_32) || | 
|  | 967 | (isX86() && isKernel64Bit() && cs[i].prog_def->ignore_on_x86_64) || | 
|  | 968 | (isRiscV() && cs[i].prog_def->ignore_on_riscv64)) { | 
|  | 969 | ALOGD("cs[%d].name:%s is ignored on %s", i, name.c_str(), describeArch()); | 
|  | 970 | continue; | 
|  | 971 | } | 
|  | 972 |  | 
|  | 973 | if (unrecognized(pin_subdir)) return -ENOTDIR; | 
|  | 974 |  | 
|  | 975 | if (specified(selinux_context)) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 976 | ALOGI("prog %s selinux_context [%-32s] -> %d -> '%s' (%s)", name.c_str(), | 
|  | 977 | cs[i].prog_def->selinux_context, selinux_context, | 
|  | 978 | lookupSelinuxContext(selinux_context), lookupPinSubdir(selinux_context)); | 
|  | 979 | } | 
|  | 980 |  | 
|  | 981 | if (specified(pin_subdir)) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 982 | ALOGI("prog %s pin_subdir [%-32s] -> %d -> '%s'", name.c_str(), | 
|  | 983 | cs[i].prog_def->pin_subdir, pin_subdir, lookupPinSubdir(pin_subdir)); | 
|  | 984 | } | 
|  | 985 |  | 
|  | 986 | // strip any potential $foo suffix | 
|  | 987 | // this can be used to provide duplicate programs | 
|  | 988 | // conditionally loaded based on running kernel version | 
|  | 989 | name = name.substr(0, name.find_last_of('$')); | 
|  | 990 |  | 
|  | 991 | bool reuse = false; | 
|  | 992 | // Format of pin location is | 
|  | 993 | // /sys/fs/bpf/<prefix>prog_<objName>_<progName> | 
|  | 994 | string progPinLoc = string(BPF_FS_PATH) + lookupPinSubdir(pin_subdir, prefix) + "prog_" + | 
|  | 995 | objName + '_' + string(name); | 
|  | 996 | if (access(progPinLoc.c_str(), F_OK) == 0) { | 
|  | 997 | fd.reset(retrieveProgram(progPinLoc.c_str())); | 
|  | 998 | ALOGD("New bpf prog load reusing prog %s, ret: %d (%s)", progPinLoc.c_str(), fd.get(), | 
|  | 999 | (!fd.ok() ? std::strerror(errno) : "no error")); | 
|  | 1000 | reuse = true; | 
|  | 1001 | } else { | 
|  | 1002 | vector<char> log_buf(BPF_LOAD_LOG_SZ, 0); | 
|  | 1003 |  | 
|  | 1004 | union bpf_attr req = { | 
|  | 1005 | .prog_type = cs[i].type, | 
|  | 1006 | .kern_version = kvers, | 
|  | 1007 | .license = ptr_to_u64(license.c_str()), | 
|  | 1008 | .insns = ptr_to_u64(cs[i].data.data()), | 
|  | 1009 | .insn_cnt = static_cast<__u32>(cs[i].data.size() / sizeof(struct bpf_insn)), | 
|  | 1010 | .log_level = 1, | 
|  | 1011 | .log_buf = ptr_to_u64(log_buf.data()), | 
|  | 1012 | .log_size = static_cast<__u32>(log_buf.size()), | 
|  | 1013 | .expected_attach_type = cs[i].expected_attach_type, | 
|  | 1014 | }; | 
| Maciej Żenczykowski | 2283635 | 2024-03-15 19:03:13 +0000 | [diff] [blame] | 1015 | if (isAtLeastKernelVersion(4, 14, 0)) | 
|  | 1016 | strlcpy(req.prog_name, cs[i].name.c_str(), sizeof(req.prog_name)); | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 1017 | fd.reset(bpf(BPF_PROG_LOAD, req)); | 
|  | 1018 |  | 
|  | 1019 | ALOGD("BPF_PROG_LOAD call for %s (%s) returned fd: %d (%s)", elfPath, | 
|  | 1020 | cs[i].name.c_str(), fd.get(), (!fd.ok() ? std::strerror(errno) : "no error")); | 
|  | 1021 |  | 
|  | 1022 | if (!fd.ok()) { | 
|  | 1023 | vector<string> lines = android::base::Split(log_buf.data(), "\n"); | 
|  | 1024 |  | 
|  | 1025 | ALOGW("BPF_PROG_LOAD - BEGIN log_buf contents:"); | 
|  | 1026 | for (const auto& line : lines) ALOGW("%s", line.c_str()); | 
|  | 1027 | ALOGW("BPF_PROG_LOAD - END log_buf contents."); | 
|  | 1028 |  | 
|  | 1029 | if (cs[i].prog_def->optional) { | 
|  | 1030 | ALOGW("failed program is marked optional - continuing..."); | 
|  | 1031 | continue; | 
|  | 1032 | } | 
|  | 1033 | ALOGE("non-optional program failed to load."); | 
|  | 1034 | } | 
|  | 1035 | } | 
|  | 1036 |  | 
|  | 1037 | if (!fd.ok()) return fd.get(); | 
|  | 1038 |  | 
|  | 1039 | if (!reuse) { | 
|  | 1040 | if (specified(selinux_context)) { | 
|  | 1041 | string createLoc = string(BPF_FS_PATH) + lookupPinSubdir(selinux_context) + | 
|  | 1042 | "tmp_prog_" + objName + '_' + string(name); | 
|  | 1043 | ret = bpfFdPin(fd, createLoc.c_str()); | 
|  | 1044 | if (ret) { | 
|  | 1045 | int err = errno; | 
|  | 1046 | ALOGE("create %s -> %d [%d:%s]", createLoc.c_str(), ret, err, strerror(err)); | 
|  | 1047 | return -err; | 
|  | 1048 | } | 
|  | 1049 | ret = renameat2(AT_FDCWD, createLoc.c_str(), | 
|  | 1050 | AT_FDCWD, progPinLoc.c_str(), RENAME_NOREPLACE); | 
|  | 1051 | if (ret) { | 
|  | 1052 | int err = errno; | 
|  | 1053 | ALOGE("rename %s %s -> %d [%d:%s]", createLoc.c_str(), progPinLoc.c_str(), ret, | 
|  | 1054 | err, strerror(err)); | 
|  | 1055 | return -err; | 
|  | 1056 | } | 
|  | 1057 | } else { | 
|  | 1058 | ret = bpfFdPin(fd, progPinLoc.c_str()); | 
|  | 1059 | if (ret) { | 
|  | 1060 | int err = errno; | 
|  | 1061 | ALOGE("create %s -> %d [%d:%s]", progPinLoc.c_str(), ret, err, strerror(err)); | 
|  | 1062 | return -err; | 
|  | 1063 | } | 
|  | 1064 | } | 
|  | 1065 | if (chmod(progPinLoc.c_str(), 0440)) { | 
|  | 1066 | int err = errno; | 
|  | 1067 | ALOGE("chmod %s 0440 -> [%d:%s]", progPinLoc.c_str(), err, strerror(err)); | 
|  | 1068 | return -err; | 
|  | 1069 | } | 
|  | 1070 | if (chown(progPinLoc.c_str(), (uid_t)cs[i].prog_def->uid, | 
|  | 1071 | (gid_t)cs[i].prog_def->gid)) { | 
|  | 1072 | int err = errno; | 
|  | 1073 | ALOGE("chown %s %d %d -> [%d:%s]", progPinLoc.c_str(), cs[i].prog_def->uid, | 
|  | 1074 | cs[i].prog_def->gid, err, strerror(err)); | 
|  | 1075 | return -err; | 
|  | 1076 | } | 
|  | 1077 | } | 
|  | 1078 |  | 
|  | 1079 | int progId = bpfGetFdProgId(fd); | 
|  | 1080 | if (progId == -1) { | 
|  | 1081 | ALOGE("bpfGetFdProgId failed, ret: %d [%d]", progId, errno); | 
|  | 1082 | } else { | 
|  | 1083 | ALOGI("prog %s id %d", progPinLoc.c_str(), progId); | 
|  | 1084 | } | 
|  | 1085 | } | 
|  | 1086 |  | 
|  | 1087 | return 0; | 
|  | 1088 | } | 
|  | 1089 |  | 
| Maciej Żenczykowski | a2706bf | 2024-03-18 22:27:22 -0700 | [diff] [blame] | 1090 | int loadProg(const char* const elfPath, bool* const isCritical, const unsigned int bpfloader_ver, | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 1091 | const Location& location) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 1092 | vector<char> license; | 
|  | 1093 | vector<char> critical; | 
|  | 1094 | vector<codeSection> cs; | 
|  | 1095 | vector<unique_fd> mapFds; | 
|  | 1096 | int ret; | 
|  | 1097 |  | 
|  | 1098 | if (!isCritical) return -1; | 
|  | 1099 | *isCritical = false; | 
|  | 1100 |  | 
|  | 1101 | ifstream elfFile(elfPath, ios::in | ios::binary); | 
|  | 1102 | if (!elfFile.is_open()) return -1; | 
|  | 1103 |  | 
|  | 1104 | ret = readSectionByName("critical", elfFile, critical); | 
|  | 1105 | *isCritical = !ret; | 
|  | 1106 |  | 
|  | 1107 | ret = readSectionByName("license", elfFile, license); | 
|  | 1108 | if (ret) { | 
|  | 1109 | ALOGE("Couldn't find license in %s", elfPath); | 
|  | 1110 | return ret; | 
|  | 1111 | } else { | 
|  | 1112 | ALOGD("Loading %s%s ELF object %s with license %s", | 
|  | 1113 | *isCritical ? "critical for " : "optional", *isCritical ? (char*)critical.data() : "", | 
|  | 1114 | elfPath, (char*)license.data()); | 
|  | 1115 | } | 
|  | 1116 |  | 
|  | 1117 | // the following default values are for bpfloader V0.0 format which does not include them | 
|  | 1118 | unsigned int bpfLoaderMinVer = | 
|  | 1119 | readSectionUint("bpfloader_min_ver", elfFile, DEFAULT_BPFLOADER_MIN_VER); | 
|  | 1120 | unsigned int bpfLoaderMaxVer = | 
|  | 1121 | readSectionUint("bpfloader_max_ver", elfFile, DEFAULT_BPFLOADER_MAX_VER); | 
|  | 1122 | unsigned int bpfLoaderMinRequiredVer = | 
|  | 1123 | readSectionUint("bpfloader_min_required_ver", elfFile, 0); | 
|  | 1124 | size_t sizeOfBpfMapDef = | 
|  | 1125 | readSectionUint("size_of_bpf_map_def", elfFile, DEFAULT_SIZEOF_BPF_MAP_DEF); | 
|  | 1126 | size_t sizeOfBpfProgDef = | 
|  | 1127 | readSectionUint("size_of_bpf_prog_def", elfFile, DEFAULT_SIZEOF_BPF_PROG_DEF); | 
|  | 1128 |  | 
|  | 1129 | // inclusive lower bound check | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 1130 | if (bpfloader_ver < bpfLoaderMinVer) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 1131 | ALOGI("BpfLoader version 0x%05x ignoring ELF object %s with min ver 0x%05x", | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 1132 | bpfloader_ver, elfPath, bpfLoaderMinVer); | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 1133 | return 0; | 
|  | 1134 | } | 
|  | 1135 |  | 
|  | 1136 | // exclusive upper bound check | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 1137 | if (bpfloader_ver >= bpfLoaderMaxVer) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 1138 | ALOGI("BpfLoader version 0x%05x ignoring ELF object %s with max ver 0x%05x", | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 1139 | bpfloader_ver, elfPath, bpfLoaderMaxVer); | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 1140 | return 0; | 
|  | 1141 | } | 
|  | 1142 |  | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 1143 | if (bpfloader_ver < bpfLoaderMinRequiredVer) { | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 1144 | ALOGI("BpfLoader version 0x%05x failing due to ELF object %s with required min ver 0x%05x", | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 1145 | bpfloader_ver, elfPath, bpfLoaderMinRequiredVer); | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 1146 | return -1; | 
|  | 1147 | } | 
|  | 1148 |  | 
|  | 1149 | ALOGI("BpfLoader version 0x%05x processing ELF object %s with ver [0x%05x,0x%05x)", | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 1150 | bpfloader_ver, elfPath, bpfLoaderMinVer, bpfLoaderMaxVer); | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 1151 |  | 
|  | 1152 | if (sizeOfBpfMapDef < DEFAULT_SIZEOF_BPF_MAP_DEF) { | 
|  | 1153 | ALOGE("sizeof(bpf_map_def) of %zu is too small (< %d)", sizeOfBpfMapDef, | 
|  | 1154 | DEFAULT_SIZEOF_BPF_MAP_DEF); | 
|  | 1155 | return -1; | 
|  | 1156 | } | 
|  | 1157 |  | 
|  | 1158 | if (sizeOfBpfProgDef < DEFAULT_SIZEOF_BPF_PROG_DEF) { | 
|  | 1159 | ALOGE("sizeof(bpf_prog_def) of %zu is too small (< %d)", sizeOfBpfProgDef, | 
|  | 1160 | DEFAULT_SIZEOF_BPF_PROG_DEF); | 
|  | 1161 | return -1; | 
|  | 1162 | } | 
|  | 1163 |  | 
| Maciej Żenczykowski | fcea70f | 2023-10-08 23:35:00 -0700 | [diff] [blame] | 1164 | ret = readCodeSections(elfFile, cs, sizeOfBpfProgDef); | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 1165 | if (ret) { | 
|  | 1166 | ALOGE("Couldn't read all code sections in %s", elfPath); | 
|  | 1167 | return ret; | 
|  | 1168 | } | 
|  | 1169 |  | 
|  | 1170 | /* Just for future debugging */ | 
|  | 1171 | if (0) dumpAllCs(cs); | 
|  | 1172 |  | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 1173 | ret = createMaps(elfPath, elfFile, mapFds, location.prefix, sizeOfBpfMapDef, bpfloader_ver); | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 1174 | if (ret) { | 
|  | 1175 | ALOGE("Failed to create maps: (ret=%d) in %s", ret, elfPath); | 
|  | 1176 | return ret; | 
|  | 1177 | } | 
|  | 1178 |  | 
|  | 1179 | for (int i = 0; i < (int)mapFds.size(); i++) | 
|  | 1180 | ALOGD("map_fd found at %d is %d in %s", i, mapFds[i].get(), elfPath); | 
|  | 1181 |  | 
|  | 1182 | applyMapRelo(elfFile, mapFds, cs); | 
|  | 1183 |  | 
| Maciej Żenczykowski | 221b248 | 2024-03-18 14:33:10 -0700 | [diff] [blame] | 1184 | ret = loadCodeSections(elfPath, cs, string(license.data()), location.prefix, bpfloader_ver); | 
| Maciej Żenczykowski | 60c159f | 2023-10-02 14:54:48 -0700 | [diff] [blame] | 1185 | if (ret) ALOGE("Failed to load programs, loadCodeSections ret=%d", ret); | 
|  | 1186 |  | 
|  | 1187 | return ret; | 
|  | 1188 | } | 
|  | 1189 |  | 
|  | 1190 | }  // namespace bpf | 
|  | 1191 | }  // namespace android |