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