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 | 7acf938 | 2022-05-12 08:47:05 +0000 | [diff] [blame^] | 33 | // This is BpfLoader v0.13 |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 34 | #define BPFLOADER_VERSION_MAJOR 0u |
Maciej Żenczykowski | 7acf938 | 2022-05-12 08:47:05 +0000 | [diff] [blame^] | 35 | #define BPFLOADER_VERSION_MINOR 13u |
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 | f7c0d99 | 2021-01-21 16:01:04 -0800 | [diff] [blame] | 76 | static string pathToFilename(const string& path, bool noext = false) { |
| 77 | vector<string> spath = android::base::Split(path, "/"); |
| 78 | string ret = spath.back(); |
| 79 | |
| 80 | if (noext) { |
| 81 | size_t lastindex = ret.find_last_of('.'); |
| 82 | return ret.substr(0, lastindex); |
| 83 | } |
| 84 | return ret; |
| 85 | } |
| 86 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 87 | typedef struct { |
| 88 | const char* name; |
| 89 | enum bpf_prog_type type; |
Tyler Wear | 4e2f460 | 2022-02-03 09:46:01 -0800 | [diff] [blame] | 90 | enum bpf_attach_type expected_attach_type; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 91 | } sectionType; |
| 92 | |
| 93 | /* |
| 94 | * Map section name prefixes to program types, the section name will be: |
Maciej Żenczykowski | 3adb1d5 | 2021-10-22 19:27:10 -0700 | [diff] [blame] | 95 | * SECTION(<prefix>/<name-of-program>) |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 96 | * For example: |
Maciej Żenczykowski | 3adb1d5 | 2021-10-22 19:27:10 -0700 | [diff] [blame] | 97 | * SECTION("tracepoint/sched_switch_func") where sched_switch_funcs |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 98 | * is the name of the program, and tracepoint is the type. |
Maciej Żenczykowski | 3adb1d5 | 2021-10-22 19:27:10 -0700 | [diff] [blame] | 99 | * |
| 100 | * However, be aware that you should not be directly using the SECTION() macro. |
| 101 | * Instead use the DEFINE_(BPF|XDP)_(PROG|MAP)... & LICENSE/CRITICAL macros. |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 102 | */ |
| 103 | sectionType sectionNameTypes[] = { |
Tyler Wear | 4e2f460 | 2022-02-03 09:46:01 -0800 | [diff] [blame] | 104 | {"bind4/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND}, |
| 105 | {"bind6/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND}, |
| 106 | {"cgroupskb/", BPF_PROG_TYPE_CGROUP_SKB, BPF_ATTACH_TYPE_UNSPEC}, |
| 107 | {"cgroupsock/", BPF_PROG_TYPE_CGROUP_SOCK, BPF_ATTACH_TYPE_UNSPEC}, |
| 108 | {"kprobe/", BPF_PROG_TYPE_KPROBE, BPF_ATTACH_TYPE_UNSPEC}, |
| 109 | {"schedact/", BPF_PROG_TYPE_SCHED_ACT, BPF_ATTACH_TYPE_UNSPEC}, |
| 110 | {"schedcls/", BPF_PROG_TYPE_SCHED_CLS, BPF_ATTACH_TYPE_UNSPEC}, |
| 111 | {"skfilter/", BPF_PROG_TYPE_SOCKET_FILTER, BPF_ATTACH_TYPE_UNSPEC}, |
| 112 | {"tracepoint/", BPF_PROG_TYPE_TRACEPOINT, BPF_ATTACH_TYPE_UNSPEC}, |
| 113 | {"xdp/", BPF_PROG_TYPE_XDP, BPF_ATTACH_TYPE_UNSPEC}, |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | typedef struct { |
| 117 | enum bpf_prog_type type; |
Tyler Wear | 4e2f460 | 2022-02-03 09:46:01 -0800 | [diff] [blame] | 118 | enum bpf_attach_type expected_attach_type; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 119 | string name; |
| 120 | vector<char> data; |
| 121 | vector<char> rel_data; |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 122 | optional<struct bpf_prog_def> prog_def; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 123 | |
Connor O'Brien | 8d49fc7 | 2019-10-24 18:23:49 -0700 | [diff] [blame] | 124 | unique_fd prog_fd; /* fd after loading */ |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 125 | } codeSection; |
| 126 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 127 | static int readElfHeader(ifstream& elfFile, Elf64_Ehdr* eh) { |
| 128 | elfFile.seekg(0); |
| 129 | if (elfFile.fail()) return -1; |
| 130 | |
| 131 | if (!elfFile.read((char*)eh, sizeof(*eh))) return -1; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | /* Reads all section header tables into an Shdr array */ |
| 137 | static int readSectionHeadersAll(ifstream& elfFile, vector<Elf64_Shdr>& shTable) { |
| 138 | Elf64_Ehdr eh; |
| 139 | int ret = 0; |
| 140 | |
| 141 | ret = readElfHeader(elfFile, &eh); |
| 142 | if (ret) return ret; |
| 143 | |
| 144 | elfFile.seekg(eh.e_shoff); |
| 145 | if (elfFile.fail()) return -1; |
| 146 | |
| 147 | /* Read shdr table entries */ |
| 148 | shTable.resize(eh.e_shnum); |
| 149 | |
| 150 | if (!elfFile.read((char*)shTable.data(), (eh.e_shnum * eh.e_shentsize))) return -ENOMEM; |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | /* Read a section by its index - for ex to get sec hdr strtab blob */ |
| 156 | static int readSectionByIdx(ifstream& elfFile, int id, vector<char>& sec) { |
| 157 | vector<Elf64_Shdr> shTable; |
Maciej Żenczykowski | d56ec05 | 2021-01-15 00:27:04 -0800 | [diff] [blame] | 158 | int ret = readSectionHeadersAll(elfFile, shTable); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 159 | if (ret) return ret; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 160 | |
| 161 | elfFile.seekg(shTable[id].sh_offset); |
| 162 | if (elfFile.fail()) return -1; |
| 163 | |
| 164 | sec.resize(shTable[id].sh_size); |
| 165 | if (!elfFile.read(sec.data(), shTable[id].sh_size)) return -1; |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* Read whole section header string table */ |
| 171 | static int readSectionHeaderStrtab(ifstream& elfFile, vector<char>& strtab) { |
| 172 | Elf64_Ehdr eh; |
Maciej Żenczykowski | d56ec05 | 2021-01-15 00:27:04 -0800 | [diff] [blame] | 173 | int ret = readElfHeader(elfFile, &eh); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 174 | if (ret) return ret; |
| 175 | |
| 176 | ret = readSectionByIdx(elfFile, eh.e_shstrndx, strtab); |
| 177 | if (ret) return ret; |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | /* Get name from offset in strtab */ |
| 183 | static int getSymName(ifstream& elfFile, int nameOff, string& name) { |
| 184 | int ret; |
| 185 | vector<char> secStrTab; |
| 186 | |
| 187 | ret = readSectionHeaderStrtab(elfFile, secStrTab); |
| 188 | if (ret) return ret; |
| 189 | |
| 190 | if (nameOff >= (int)secStrTab.size()) return -1; |
| 191 | |
| 192 | name = string((char*)secStrTab.data() + nameOff); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | /* Reads a full section by name - example to get the GPL license */ |
| 197 | static int readSectionByName(const char* name, ifstream& elfFile, vector<char>& data) { |
| 198 | vector<char> secStrTab; |
| 199 | vector<Elf64_Shdr> shTable; |
| 200 | int ret; |
| 201 | |
| 202 | ret = readSectionHeadersAll(elfFile, shTable); |
| 203 | if (ret) return ret; |
| 204 | |
| 205 | ret = readSectionHeaderStrtab(elfFile, secStrTab); |
| 206 | if (ret) return ret; |
| 207 | |
| 208 | for (int i = 0; i < (int)shTable.size(); i++) { |
| 209 | char* secname = secStrTab.data() + shTable[i].sh_name; |
| 210 | if (!secname) continue; |
| 211 | |
| 212 | if (!strcmp(secname, name)) { |
| 213 | vector<char> dataTmp; |
| 214 | dataTmp.resize(shTable[i].sh_size); |
| 215 | |
| 216 | elfFile.seekg(shTable[i].sh_offset); |
| 217 | if (elfFile.fail()) return -1; |
| 218 | |
| 219 | if (!elfFile.read((char*)dataTmp.data(), shTable[i].sh_size)) return -1; |
| 220 | |
| 221 | data = dataTmp; |
| 222 | return 0; |
| 223 | } |
| 224 | } |
| 225 | return -2; |
| 226 | } |
| 227 | |
Maciej Żenczykowski | 7ed94ef | 2021-07-06 01:47:15 -0700 | [diff] [blame] | 228 | unsigned int readSectionUint(const char* name, ifstream& elfFile, unsigned int defVal) { |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 229 | vector<char> theBytes; |
| 230 | int ret = readSectionByName(name, elfFile, theBytes); |
| 231 | if (ret) { |
| 232 | ALOGD("Couldn't find section %s (defaulting to %u [0x%x]).\n", name, defVal, defVal); |
| 233 | return defVal; |
| 234 | } else if (theBytes.size() < sizeof(unsigned int)) { |
| 235 | ALOGE("Section %s too short (defaulting to %u [0x%x]).\n", name, defVal, defVal); |
| 236 | return defVal; |
| 237 | } else { |
| 238 | // decode first 4 bytes as LE32 uint, there will likely be more bytes due to alignment. |
| 239 | unsigned int value = static_cast<unsigned char>(theBytes[3]); |
| 240 | value <<= 8; |
| 241 | value += static_cast<unsigned char>(theBytes[2]); |
| 242 | value <<= 8; |
| 243 | value += static_cast<unsigned char>(theBytes[1]); |
| 244 | value <<= 8; |
| 245 | value += static_cast<unsigned char>(theBytes[0]); |
| 246 | ALOGI("Section %s value is %u [0x%x]\n", name, value, value); |
| 247 | return value; |
| 248 | } |
| 249 | } |
| 250 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 251 | static int readSectionByType(ifstream& elfFile, int type, vector<char>& data) { |
| 252 | int ret; |
| 253 | vector<Elf64_Shdr> shTable; |
| 254 | |
| 255 | ret = readSectionHeadersAll(elfFile, shTable); |
| 256 | if (ret) return ret; |
| 257 | |
| 258 | for (int i = 0; i < (int)shTable.size(); i++) { |
| 259 | if ((int)shTable[i].sh_type != type) continue; |
| 260 | |
| 261 | vector<char> dataTmp; |
| 262 | dataTmp.resize(shTable[i].sh_size); |
| 263 | |
| 264 | elfFile.seekg(shTable[i].sh_offset); |
| 265 | if (elfFile.fail()) return -1; |
| 266 | |
| 267 | if (!elfFile.read((char*)dataTmp.data(), shTable[i].sh_size)) return -1; |
| 268 | |
| 269 | data = dataTmp; |
| 270 | return 0; |
| 271 | } |
| 272 | return -2; |
| 273 | } |
| 274 | |
| 275 | static bool symCompare(Elf64_Sym a, Elf64_Sym b) { |
| 276 | return (a.st_value < b.st_value); |
| 277 | } |
| 278 | |
| 279 | static int readSymTab(ifstream& elfFile, int sort, vector<Elf64_Sym>& data) { |
| 280 | int ret, numElems; |
| 281 | Elf64_Sym* buf; |
| 282 | vector<char> secData; |
| 283 | |
| 284 | ret = readSectionByType(elfFile, SHT_SYMTAB, secData); |
| 285 | if (ret) return ret; |
| 286 | |
| 287 | buf = (Elf64_Sym*)secData.data(); |
| 288 | numElems = (secData.size() / sizeof(Elf64_Sym)); |
| 289 | data.assign(buf, buf + numElems); |
| 290 | |
| 291 | if (sort) std::sort(data.begin(), data.end(), symCompare); |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static enum bpf_prog_type getSectionType(string& name) { |
Maciej Żenczykowski | 2b20313 | 2021-11-18 15:13:36 -0800 | [diff] [blame] | 296 | for (auto& snt : sectionNameTypes) |
| 297 | if (StartsWith(name, snt.name)) return snt.type; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 298 | |
Paul Lawrence | 9548f9f | 2021-11-09 16:32:43 +0000 | [diff] [blame] | 299 | // TODO Remove this code when fuse-bpf is upstream and this BPF_PROG_TYPE_FUSE is fixed |
| 300 | if (StartsWith(name, "fuse/")) { |
| 301 | int result = BPF_PROG_TYPE_UNSPEC; |
| 302 | ifstream("/sys/fs/fuse/bpf_prog_type_fuse") >> result; |
| 303 | return static_cast<bpf_prog_type>(result); |
| 304 | } |
| 305 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 306 | return BPF_PROG_TYPE_UNSPEC; |
| 307 | } |
| 308 | |
Tyler Wear | 4e2f460 | 2022-02-03 09:46:01 -0800 | [diff] [blame] | 309 | static enum bpf_attach_type getExpectedAttachType(string& name) { |
| 310 | for (auto& snt : sectionNameTypes) |
| 311 | if (StartsWith(name, snt.name)) return snt.expected_attach_type; |
| 312 | return BPF_ATTACH_TYPE_UNSPEC; |
| 313 | } |
| 314 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 315 | static string getSectionName(enum bpf_prog_type type) |
| 316 | { |
Maciej Żenczykowski | 2b20313 | 2021-11-18 15:13:36 -0800 | [diff] [blame] | 317 | for (auto& snt : sectionNameTypes) |
| 318 | if (snt.type == type) |
| 319 | return string(snt.name); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 320 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 321 | return "UNKNOWN SECTION NAME " + std::to_string(type); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 322 | } |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 323 | |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 324 | static int readProgDefs(ifstream& elfFile, vector<struct bpf_prog_def>& pd, |
| 325 | size_t sizeOfBpfProgDef) { |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 326 | vector<char> pdData; |
| 327 | int ret = readSectionByName("progs", elfFile, pdData); |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 328 | // Older file formats do not require a 'progs' section at all. |
| 329 | // (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] | 330 | if (ret == -2) return 0; |
| 331 | if (ret) return ret; |
| 332 | |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 333 | if (pdData.size() % sizeOfBpfProgDef) { |
| 334 | ALOGE("readProgDefs failed due to improper sized progs section, %zu %% %zu != 0\n", |
| 335 | pdData.size(), sizeOfBpfProgDef); |
| 336 | return -1; |
| 337 | }; |
| 338 | |
| 339 | int progCount = pdData.size() / sizeOfBpfProgDef; |
| 340 | pd.resize(progCount); |
| 341 | size_t trimmedSize = std::min(sizeOfBpfProgDef, sizeof(struct bpf_prog_def)); |
| 342 | |
| 343 | const char* dataPtr = pdData.data(); |
| 344 | for (auto& p : pd) { |
| 345 | // First we zero initialize |
| 346 | memset(&p, 0, sizeof(p)); |
| 347 | // Then we set non-zero defaults |
| 348 | p.bpfloader_max_ver = DEFAULT_BPFLOADER_MAX_VER; // v1.0 |
| 349 | // Then we copy over the structure prefix from the ELF file. |
| 350 | memcpy(&p, dataPtr, trimmedSize); |
| 351 | // Move to next struct in the ELF file |
| 352 | dataPtr += sizeOfBpfProgDef; |
| 353 | } |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
Connor O'Brien | 0ea4c6b | 2022-01-14 00:18:11 -0800 | [diff] [blame] | 357 | static int getSectionSymNames(ifstream& elfFile, const string& sectionName, vector<string>& names, |
| 358 | optional<unsigned> symbolType = std::nullopt) { |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 359 | int ret; |
| 360 | string name; |
| 361 | vector<Elf64_Sym> symtab; |
| 362 | vector<Elf64_Shdr> shTable; |
| 363 | |
| 364 | ret = readSymTab(elfFile, 1 /* sort */, symtab); |
| 365 | if (ret) return ret; |
| 366 | |
| 367 | /* Get index of section */ |
| 368 | ret = readSectionHeadersAll(elfFile, shTable); |
| 369 | if (ret) return ret; |
| 370 | |
| 371 | int sec_idx = -1; |
| 372 | for (int i = 0; i < (int)shTable.size(); i++) { |
| 373 | ret = getSymName(elfFile, shTable[i].sh_name, name); |
| 374 | if (ret) return ret; |
| 375 | |
| 376 | if (!name.compare(sectionName)) { |
| 377 | sec_idx = i; |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /* No section found with matching name*/ |
| 383 | if (sec_idx == -1) { |
Maciej Żenczykowski | 21f34cb | 2020-07-20 18:44:33 -0700 | [diff] [blame] | 384 | ALOGW("No %s section could be found in elf object\n", sectionName.c_str()); |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 385 | return -1; |
| 386 | } |
| 387 | |
| 388 | for (int i = 0; i < (int)symtab.size(); i++) { |
Connor O'Brien | 0ea4c6b | 2022-01-14 00:18:11 -0800 | [diff] [blame] | 389 | if (symbolType.has_value() && ELF_ST_TYPE(symtab[i].st_info) != symbolType) continue; |
| 390 | |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 391 | if (symtab[i].st_shndx == sec_idx) { |
| 392 | string s; |
| 393 | ret = getSymName(elfFile, symtab[i].st_name, s); |
| 394 | if (ret) return ret; |
| 395 | names.push_back(s); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 402 | static bool IsAllowed(bpf_prog_type type, const bpf_prog_type* allowed, size_t numAllowed) { |
| 403 | if (allowed == nullptr) return true; |
| 404 | |
| 405 | for (size_t i = 0; i < numAllowed; i++) { |
| 406 | if (type == allowed[i]) return true; |
| 407 | } |
| 408 | |
| 409 | return false; |
| 410 | } |
| 411 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 412 | /* 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] | 413 | static int readCodeSections(ifstream& elfFile, vector<codeSection>& cs, size_t sizeOfBpfProgDef, |
| 414 | const bpf_prog_type* allowed, size_t numAllowed) { |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 415 | vector<Elf64_Shdr> shTable; |
| 416 | int entries, ret = 0; |
| 417 | |
| 418 | ret = readSectionHeadersAll(elfFile, shTable); |
| 419 | if (ret) return ret; |
| 420 | entries = shTable.size(); |
| 421 | |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 422 | vector<struct bpf_prog_def> pd; |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 423 | ret = readProgDefs(elfFile, pd, sizeOfBpfProgDef); |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 424 | if (ret) return ret; |
| 425 | vector<string> progDefNames; |
| 426 | ret = getSectionSymNames(elfFile, "progs", progDefNames); |
| 427 | if (!pd.empty() && ret) return ret; |
| 428 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 429 | for (int i = 0; i < entries; i++) { |
| 430 | string name; |
| 431 | codeSection cs_temp; |
| 432 | cs_temp.type = BPF_PROG_TYPE_UNSPEC; |
| 433 | |
| 434 | ret = getSymName(elfFile, shTable[i].sh_name, name); |
| 435 | if (ret) return ret; |
| 436 | |
| 437 | enum bpf_prog_type ptype = getSectionType(name); |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 438 | |
Tyler Wear | 4e2f460 | 2022-02-03 09:46:01 -0800 | [diff] [blame] | 439 | if (ptype == BPF_PROG_TYPE_UNSPEC) continue; |
Maciej Żenczykowski | f7c0d99 | 2021-01-21 16:01:04 -0800 | [diff] [blame] | 440 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 441 | if (!IsAllowed(ptype, allowed, numAllowed)) { |
| 442 | ALOGE("Program type %s not permitted here", getSectionName(ptype).c_str()); |
| 443 | return -1; |
| 444 | } |
| 445 | |
Tyler Wear | 4e2f460 | 2022-02-03 09:46:01 -0800 | [diff] [blame] | 446 | // This must be done before '/' is replaced with '_'. |
| 447 | cs_temp.expected_attach_type = getExpectedAttachType(name); |
Maciej Żenczykowski | f7c0d99 | 2021-01-21 16:01:04 -0800 | [diff] [blame] | 448 | |
Tyler Wear | 4e2f460 | 2022-02-03 09:46:01 -0800 | [diff] [blame] | 449 | string oldName = name; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 450 | |
Tyler Wear | 4e2f460 | 2022-02-03 09:46:01 -0800 | [diff] [blame] | 451 | // convert all slashes to underscores |
| 452 | std::replace(name.begin(), name.end(), '/', '_'); |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 453 | |
Tyler Wear | 4e2f460 | 2022-02-03 09:46:01 -0800 | [diff] [blame] | 454 | cs_temp.type = ptype; |
| 455 | cs_temp.name = name; |
| 456 | |
| 457 | ret = readSectionByIdx(elfFile, i, cs_temp.data); |
| 458 | if (ret) return ret; |
| 459 | ALOGD("Loaded code section %d (%s)\n", i, name.c_str()); |
| 460 | |
| 461 | vector<string> csSymNames; |
| 462 | ret = getSectionSymNames(elfFile, oldName, csSymNames, STT_FUNC); |
| 463 | if (ret || !csSymNames.size()) return ret; |
| 464 | for (size_t i = 0; i < progDefNames.size(); ++i) { |
| 465 | if (!progDefNames[i].compare(csSymNames[0] + "_def")) { |
| 466 | cs_temp.prog_def = pd[i]; |
| 467 | break; |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 468 | } |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /* Check for rel section */ |
| 472 | if (cs_temp.data.size() > 0 && i < entries) { |
| 473 | ret = getSymName(elfFile, shTable[i + 1].sh_name, name); |
| 474 | if (ret) return ret; |
| 475 | |
Tyler Wear | 4e2f460 | 2022-02-03 09:46:01 -0800 | [diff] [blame] | 476 | if (name == (".rel" + oldName)) { |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 477 | ret = readSectionByIdx(elfFile, i + 1, cs_temp.rel_data); |
| 478 | if (ret) return ret; |
| 479 | ALOGD("Loaded relo section %d (%s)\n", i, name.c_str()); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | if (cs_temp.data.size() > 0) { |
Connor O'Brien | 8d49fc7 | 2019-10-24 18:23:49 -0700 | [diff] [blame] | 484 | cs.push_back(std::move(cs_temp)); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 485 | ALOGD("Adding section %d to cs list\n", i); |
| 486 | } |
| 487 | } |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | static int getSymNameByIdx(ifstream& elfFile, int index, string& name) { |
| 492 | vector<Elf64_Sym> symtab; |
| 493 | int ret = 0; |
| 494 | |
| 495 | ret = readSymTab(elfFile, 0 /* !sort */, symtab); |
| 496 | if (ret) return ret; |
| 497 | |
| 498 | if (index >= (int)symtab.size()) return -1; |
| 499 | |
| 500 | return getSymName(elfFile, symtab[index].st_name, name); |
| 501 | } |
| 502 | |
Connor O'Brien | 35425e5 | 2022-01-18 21:41:16 -0800 | [diff] [blame] | 503 | static bool waitpidTimeout(pid_t pid, int timeoutMs) { |
| 504 | // Add SIGCHLD to the signal set. |
| 505 | sigset_t child_mask, original_mask; |
| 506 | sigemptyset(&child_mask); |
| 507 | sigaddset(&child_mask, SIGCHLD); |
| 508 | if (sigprocmask(SIG_BLOCK, &child_mask, &original_mask) == -1) return false; |
| 509 | |
| 510 | // Wait for a SIGCHLD notification. |
| 511 | errno = 0; |
| 512 | timespec ts = {0, timeoutMs * 1000000}; |
| 513 | int wait_result = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, nullptr, &ts)); |
| 514 | |
| 515 | // Restore the original signal set. |
| 516 | sigprocmask(SIG_SETMASK, &original_mask, nullptr); |
| 517 | |
| 518 | if (wait_result == -1) return false; |
| 519 | |
| 520 | int status; |
| 521 | return TEMP_FAILURE_RETRY(waitpid(pid, &status, WNOHANG)) == pid; |
| 522 | } |
| 523 | |
| 524 | static std::optional<unique_fd> getMapBtfInfo(const char* elfPath, |
| 525 | std::unordered_map<string, std::pair<uint32_t, uint32_t>> &btfTypeIds) { |
| 526 | unique_fd bpfloaderSocket, btfloaderSocket; |
| 527 | if (!android::base::Socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, &bpfloaderSocket, |
| 528 | &btfloaderSocket)) { |
| 529 | return {}; |
| 530 | } |
| 531 | |
| 532 | unique_fd pipeRead, pipeWrite; |
| 533 | if (!android::base::Pipe(&pipeRead, &pipeWrite, O_NONBLOCK)) { |
| 534 | return {}; |
| 535 | } |
| 536 | |
| 537 | pid_t pid = fork(); |
| 538 | if (pid < 0) return {}; |
| 539 | if (!pid) { |
| 540 | bpfloaderSocket.reset(); |
| 541 | pipeRead.reset(); |
| 542 | auto socketFdStr = std::to_string(btfloaderSocket.release()); |
| 543 | auto pipeFdStr = std::to_string(pipeWrite.release()); |
| 544 | |
| 545 | if (execl("/system/bin/btfloader", "/system/bin/btfloader", socketFdStr.c_str(), |
| 546 | pipeFdStr.c_str(), elfPath, NULL) == -1) { |
| 547 | ALOGW("exec btfloader failed with errno %d (%s)\n", errno, strerror(errno)); |
| 548 | exit(EX_UNAVAILABLE); |
| 549 | } |
| 550 | } |
| 551 | btfloaderSocket.reset(); |
| 552 | pipeWrite.reset(); |
| 553 | if (!waitpidTimeout(pid, 100)) { |
| 554 | kill(pid, SIGKILL); |
| 555 | return {}; |
| 556 | } |
| 557 | |
| 558 | unique_fd btfFd; |
| 559 | if (android::base::ReceiveFileDescriptors(bpfloaderSocket, nullptr, 0, &btfFd)) return {}; |
| 560 | |
| 561 | std::string btfTypeIdStr; |
| 562 | if (!android::base::ReadFdToString(pipeRead, &btfTypeIdStr)) return {}; |
| 563 | if (btfFd.get() < 0) return {}; |
| 564 | |
| 565 | const auto mapTypeIdLines = android::base::Split(btfTypeIdStr, "\n"); |
| 566 | for (const auto &line : mapTypeIdLines) { |
| 567 | const auto vec = android::base::Split(line, " "); |
| 568 | // Splitting on newline will give us one empty line |
| 569 | if (vec.size() != 3) continue; |
| 570 | const int kTid = atoi(vec[1].c_str()); |
| 571 | const int vTid = atoi(vec[2].c_str()); |
| 572 | if (!kTid || !vTid) return {}; |
| 573 | btfTypeIds[vec[0]] = std::make_pair(kTid, vTid); |
| 574 | } |
| 575 | return btfFd; |
| 576 | } |
| 577 | |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 578 | static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>& mapFds, |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 579 | const char* prefix, size_t sizeOfBpfMapDef) { |
Connor O'Brien | 8d49fc7 | 2019-10-24 18:23:49 -0700 | [diff] [blame] | 580 | int ret; |
Connor O'Brien | 35425e5 | 2022-01-18 21:41:16 -0800 | [diff] [blame] | 581 | vector<char> mdData, btfData; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 582 | vector<struct bpf_map_def> md; |
| 583 | vector<string> mapNames; |
Connor O'Brien | 35425e5 | 2022-01-18 21:41:16 -0800 | [diff] [blame] | 584 | std::unordered_map<string, std::pair<uint32_t, uint32_t>> btfTypeIdMap; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 585 | string fname = pathToFilename(string(elfPath), true); |
| 586 | |
| 587 | ret = readSectionByName("maps", elfFile, mdData); |
Steven Moreland | c0905b4 | 2019-12-12 14:21:20 -0800 | [diff] [blame] | 588 | if (ret == -2) return 0; // no maps to read |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 589 | if (ret) return ret; |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 590 | |
| 591 | if (mdData.size() % sizeOfBpfMapDef) { |
| 592 | ALOGE("createMaps failed due to improper sized maps section, %zu %% %zu != 0\n", |
| 593 | mdData.size(), sizeOfBpfMapDef); |
| 594 | return -1; |
| 595 | }; |
| 596 | |
| 597 | int mapCount = mdData.size() / sizeOfBpfMapDef; |
| 598 | md.resize(mapCount); |
| 599 | size_t trimmedSize = std::min(sizeOfBpfMapDef, sizeof(struct bpf_map_def)); |
| 600 | |
| 601 | const char* dataPtr = mdData.data(); |
| 602 | for (auto& m : md) { |
| 603 | // First we zero initialize |
| 604 | memset(&m, 0, sizeof(m)); |
| 605 | // Then we set non-zero defaults |
| 606 | m.bpfloader_max_ver = DEFAULT_BPFLOADER_MAX_VER; // v1.0 |
Maciej Żenczykowski | 36c53ba | 2021-07-05 15:20:34 -0700 | [diff] [blame] | 607 | m.max_kver = 0xFFFFFFFFu; // matches KVER_INF from bpf_helpers.h |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 608 | // Then we copy over the structure prefix from the ELF file. |
| 609 | memcpy(&m, dataPtr, trimmedSize); |
| 610 | // Move to next struct in the ELF file |
| 611 | dataPtr += sizeOfBpfMapDef; |
| 612 | } |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 613 | |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 614 | ret = getSectionSymNames(elfFile, "maps", mapNames); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 615 | if (ret) return ret; |
| 616 | |
Connor O'Brien | 35425e5 | 2022-01-18 21:41:16 -0800 | [diff] [blame] | 617 | std::optional<unique_fd> btfFd; |
| 618 | if (!readSectionByName(".BTF", elfFile, btfData)) { |
| 619 | btfFd = getMapBtfInfo(elfPath, btfTypeIdMap); |
| 620 | } |
| 621 | |
Maciej Żenczykowski | 36c53ba | 2021-07-05 15:20:34 -0700 | [diff] [blame] | 622 | unsigned kvers = kernelVersion(); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 623 | |
Maciej Żenczykowski | 36c53ba | 2021-07-05 15:20:34 -0700 | [diff] [blame] | 624 | for (int i = 0; i < (int)mapNames.size(); i++) { |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 625 | if (BPFLOADER_VERSION < md[i].bpfloader_min_ver) { |
| 626 | ALOGI("skipping map %s which requires bpfloader min ver 0x%05x\n", mapNames[i].c_str(), |
| 627 | md[i].bpfloader_min_ver); |
Maciej Żenczykowski | a21256d | 2021-07-02 00:40:55 -0700 | [diff] [blame] | 628 | mapFds.push_back(unique_fd()); |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 629 | continue; |
| 630 | } |
| 631 | |
| 632 | if (BPFLOADER_VERSION >= md[i].bpfloader_max_ver) { |
| 633 | ALOGI("skipping map %s which requires bpfloader max ver 0x%05x\n", mapNames[i].c_str(), |
| 634 | md[i].bpfloader_max_ver); |
Maciej Żenczykowski | a21256d | 2021-07-02 00:40:55 -0700 | [diff] [blame] | 635 | mapFds.push_back(unique_fd()); |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 636 | continue; |
| 637 | } |
| 638 | |
Maciej Żenczykowski | 36c53ba | 2021-07-05 15:20:34 -0700 | [diff] [blame] | 639 | if (kvers < md[i].min_kver) { |
| 640 | ALOGI("skipping map %s which requires kernel version 0x%x >= 0x%x\n", |
| 641 | mapNames[i].c_str(), kvers, md[i].min_kver); |
| 642 | mapFds.push_back(unique_fd()); |
| 643 | continue; |
| 644 | } |
| 645 | |
| 646 | if (kvers >= md[i].max_kver) { |
| 647 | ALOGI("skipping map %s which requires kernel version 0x%x < 0x%x\n", |
| 648 | mapNames[i].c_str(), kvers, md[i].max_kver); |
| 649 | mapFds.push_back(unique_fd()); |
| 650 | continue; |
| 651 | } |
| 652 | |
| 653 | // Format of pin location is /sys/fs/bpf/<prefix>map_<filename>_<mapname> |
| 654 | string mapPinLoc = |
| 655 | string(BPF_FS_PATH) + prefix + "map_" + fname + "_" + string(mapNames[i]); |
| 656 | bool reuse = false; |
| 657 | unique_fd fd; |
| 658 | int saved_errno; |
| 659 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 660 | if (access(mapPinLoc.c_str(), F_OK) == 0) { |
Connor O'Brien | 8d49fc7 | 2019-10-24 18:23:49 -0700 | [diff] [blame] | 661 | fd.reset(bpf_obj_get(mapPinLoc.c_str())); |
Maciej Żenczykowski | 2c37213 | 2021-03-01 23:09:54 -0800 | [diff] [blame] | 662 | saved_errno = errno; |
Connor O'Brien | 8d49fc7 | 2019-10-24 18:23:49 -0700 | [diff] [blame] | 663 | ALOGD("bpf_create_map reusing map %s, ret: %d\n", mapNames[i].c_str(), fd.get()); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 664 | reuse = true; |
| 665 | } else { |
Maciej Żenczykowski | cb358de | 2021-03-04 07:27:38 -0800 | [diff] [blame] | 666 | enum bpf_map_type type = md[i].type; |
| 667 | if (type == BPF_MAP_TYPE_DEVMAP && !isAtLeastKernelVersion(4, 14, 0)) { |
| 668 | // On Linux Kernels older than 4.14 this map type doesn't exist, but it can kind |
| 669 | // of be approximated: ARRAY has the same userspace api, though it is not usable |
| 670 | // by the same ebpf programs. However, that's okay because the bpf_redirect_map() |
| 671 | // helper doesn't exist on 4.9 anyway (so the bpf program would fail to load, |
| 672 | // and thus needs to be tagged as 4.14+ either way), so there's nothing useful you |
| 673 | // could do with a DEVMAP anyway (that isn't already provided by an ARRAY)... |
| 674 | // Hence using an ARRAY instead of a DEVMAP simply makes life easier for userspace. |
| 675 | type = BPF_MAP_TYPE_ARRAY; |
| 676 | } |
| 677 | if (type == BPF_MAP_TYPE_DEVMAP_HASH && !isAtLeastKernelVersion(5, 4, 0)) { |
| 678 | // On Linux Kernels older than 5.4 this map type doesn't exist, but it can kind |
| 679 | // of be approximated: HASH has the same userspace visible api. |
| 680 | // However it cannot be used by ebpf programs in the same way. |
| 681 | // Since bpf_redirect_map() only requires 4.14, a program using a DEVMAP_HASH map |
| 682 | // would fail to load (due to trying to redirect to a HASH instead of DEVMAP_HASH). |
| 683 | // One must thus tag any BPF_MAP_TYPE_DEVMAP_HASH + bpf_redirect_map() using |
| 684 | // programs as being 5.4+... |
| 685 | type = BPF_MAP_TYPE_HASH; |
| 686 | } |
Connor O'Brien | 35425e5 | 2022-01-18 21:41:16 -0800 | [diff] [blame] | 687 | struct bpf_create_map_attr attr = { |
| 688 | .name = mapNames[i].c_str(), |
| 689 | .map_type = type, |
| 690 | .map_flags = md[i].map_flags, |
| 691 | .key_size = md[i].key_size, |
| 692 | .value_size = md[i].value_size, |
| 693 | .max_entries = md[i].max_entries, |
| 694 | }; |
| 695 | if (btfFd.has_value() && btfTypeIdMap.find(mapNames[i]) != btfTypeIdMap.end()) { |
| 696 | attr.btf_fd = btfFd->get(); |
| 697 | attr.btf_key_type_id = btfTypeIdMap.at(mapNames[i]).first; |
| 698 | attr.btf_value_type_id = btfTypeIdMap.at(mapNames[i]).second; |
| 699 | } |
| 700 | fd.reset(bcc_create_map_xattr(&attr, true)); |
Maciej Żenczykowski | 2c37213 | 2021-03-01 23:09:54 -0800 | [diff] [blame] | 701 | saved_errno = errno; |
Connor O'Brien | 8d49fc7 | 2019-10-24 18:23:49 -0700 | [diff] [blame] | 702 | ALOGD("bpf_create_map name %s, ret: %d\n", mapNames[i].c_str(), fd.get()); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Maciej Żenczykowski | 2c37213 | 2021-03-01 23:09:54 -0800 | [diff] [blame] | 705 | if (fd < 0) return -saved_errno; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 706 | |
| 707 | if (!reuse) { |
| 708 | ret = bpf_obj_pin(fd, mapPinLoc.c_str()); |
Maciej Żenczykowski | 83f2977 | 2020-01-27 03:11:51 -0800 | [diff] [blame] | 709 | if (ret) return -errno; |
| 710 | ret = chown(mapPinLoc.c_str(), (uid_t)md[i].uid, (gid_t)md[i].gid); |
| 711 | if (ret) return -errno; |
| 712 | ret = chmod(mapPinLoc.c_str(), md[i].mode); |
| 713 | if (ret) return -errno; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Connor O'Brien | 8d49fc7 | 2019-10-24 18:23:49 -0700 | [diff] [blame] | 716 | mapFds.push_back(std::move(fd)); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | return ret; |
| 720 | } |
| 721 | |
| 722 | /* For debugging, dump all instructions */ |
| 723 | static void dumpIns(char* ins, int size) { |
| 724 | for (int row = 0; row < size / 8; row++) { |
| 725 | ALOGE("%d: ", row); |
| 726 | for (int j = 0; j < 8; j++) { |
| 727 | ALOGE("%3x ", ins[(row * 8) + j]); |
| 728 | } |
| 729 | ALOGE("\n"); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /* For debugging, dump all code sections from cs list */ |
| 734 | static void dumpAllCs(vector<codeSection>& cs) { |
| 735 | for (int i = 0; i < (int)cs.size(); i++) { |
| 736 | ALOGE("Dumping cs %d, name %s\n", int(i), cs[i].name.c_str()); |
| 737 | dumpIns((char*)cs[i].data.data(), cs[i].data.size()); |
| 738 | ALOGE("-----------\n"); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | static void applyRelo(void* insnsPtr, Elf64_Addr offset, int fd) { |
| 743 | int insnIndex; |
| 744 | struct bpf_insn *insn, *insns; |
| 745 | |
| 746 | insns = (struct bpf_insn*)(insnsPtr); |
| 747 | |
| 748 | insnIndex = offset / sizeof(struct bpf_insn); |
| 749 | insn = &insns[insnIndex]; |
| 750 | |
| 751 | ALOGD( |
| 752 | "applying relo to instruction at byte offset: %d, \ |
| 753 | insn offset %d , insn %lx\n", |
| 754 | (int)offset, (int)insnIndex, *(unsigned long*)insn); |
| 755 | |
| 756 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) { |
| 757 | ALOGE("Dumping all instructions till ins %d\n", insnIndex); |
| 758 | ALOGE("invalid relo for insn %d: code 0x%x\n", insnIndex, insn->code); |
| 759 | dumpIns((char*)insnsPtr, (insnIndex + 3) * 8); |
| 760 | return; |
| 761 | } |
| 762 | |
| 763 | insn->imm = fd; |
| 764 | insn->src_reg = BPF_PSEUDO_MAP_FD; |
| 765 | } |
| 766 | |
Connor O'Brien | 8d49fc7 | 2019-10-24 18:23:49 -0700 | [diff] [blame] | 767 | static void applyMapRelo(ifstream& elfFile, vector<unique_fd> &mapFds, vector<codeSection>& cs) { |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 768 | vector<string> mapNames; |
| 769 | |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 770 | int ret = getSectionSymNames(elfFile, "maps", mapNames); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 771 | if (ret) return; |
| 772 | |
| 773 | for (int k = 0; k != (int)cs.size(); k++) { |
| 774 | Elf64_Rel* rel = (Elf64_Rel*)(cs[k].rel_data.data()); |
| 775 | int n_rel = cs[k].rel_data.size() / sizeof(*rel); |
| 776 | |
| 777 | for (int i = 0; i < n_rel; i++) { |
| 778 | int symIndex = ELF64_R_SYM(rel[i].r_info); |
| 779 | string symName; |
| 780 | |
| 781 | ret = getSymNameByIdx(elfFile, symIndex, symName); |
| 782 | if (ret) return; |
| 783 | |
| 784 | /* Find the map fd and apply relo */ |
| 785 | for (int j = 0; j < (int)mapNames.size(); j++) { |
| 786 | if (!mapNames[j].compare(symName)) { |
| 787 | applyRelo(cs[k].data.data(), rel[i].r_offset, mapFds[j]); |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 795 | static int loadCodeSections(const char* elfPath, vector<codeSection>& cs, const string& license, |
| 796 | const char* prefix) { |
Maciej Żenczykowski | 07375e2 | 2020-02-19 14:23:59 -0800 | [diff] [blame] | 797 | unsigned kvers = kernelVersion(); |
| 798 | int ret, fd; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 799 | |
Maciej Żenczykowski | 07375e2 | 2020-02-19 14:23:59 -0800 | [diff] [blame] | 800 | if (!kvers) return -1; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 801 | |
| 802 | string fname = pathToFilename(string(elfPath), true); |
| 803 | |
| 804 | for (int i = 0; i < (int)cs.size(); i++) { |
Maciej Żenczykowski | 681f604 | 2020-04-21 15:34:18 -0700 | [diff] [blame] | 805 | string name = cs[i].name; |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 806 | unsigned bpfMinVer = DEFAULT_BPFLOADER_MIN_VER; // v0.0 |
| 807 | unsigned bpfMaxVer = DEFAULT_BPFLOADER_MAX_VER; // v1.0 |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 808 | |
Maciej Żenczykowski | 07375e2 | 2020-02-19 14:23:59 -0800 | [diff] [blame] | 809 | if (cs[i].prog_def.has_value()) { |
Maciej Żenczykowski | 681f604 | 2020-04-21 15:34:18 -0700 | [diff] [blame] | 810 | unsigned min_kver = cs[i].prog_def->min_kver; |
| 811 | unsigned max_kver = cs[i].prog_def->max_kver; |
| 812 | ALOGD("cs[%d].name:%s min_kver:%x .max_kver:%x (kvers:%x)\n", i, name.c_str(), min_kver, |
| 813 | max_kver, kvers); |
| 814 | if (kvers < min_kver) continue; |
| 815 | if (kvers >= max_kver) continue; |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 816 | |
| 817 | bpfMinVer = cs[i].prog_def->bpfloader_min_ver; |
| 818 | bpfMaxVer = cs[i].prog_def->bpfloader_max_ver; |
Maciej Żenczykowski | 07375e2 | 2020-02-19 14:23:59 -0800 | [diff] [blame] | 819 | } |
| 820 | |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 821 | ALOGD("cs[%d].name:%s requires bpfloader version [0x%05x,0x%05x)\n", i, name.c_str(), |
| 822 | bpfMinVer, bpfMaxVer); |
| 823 | if (BPFLOADER_VERSION < bpfMinVer) continue; |
| 824 | if (BPFLOADER_VERSION >= bpfMaxVer) continue; |
| 825 | |
Maciej Żenczykowski | 681f604 | 2020-04-21 15:34:18 -0700 | [diff] [blame] | 826 | // strip any potential $foo suffix |
| 827 | // this can be used to provide duplicate programs |
| 828 | // conditionally loaded based on running kernel version |
Maciej Żenczykowski | 428843d | 2020-04-23 12:43:44 -0700 | [diff] [blame] | 829 | name = name.substr(0, name.find_last_of('$')); |
Maciej Żenczykowski | 681f604 | 2020-04-21 15:34:18 -0700 | [diff] [blame] | 830 | |
| 831 | bool reuse = false; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 832 | // Format of pin location is |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 833 | // /sys/fs/bpf/<prefix>prog_<filename>_<mapname> |
| 834 | string progPinLoc = BPF_FS_PATH; |
| 835 | progPinLoc += prefix; |
| 836 | progPinLoc += "prog_"; |
Maciej Żenczykowski | 6c7871b | 2020-04-23 12:46:00 -0700 | [diff] [blame] | 837 | progPinLoc += fname; |
| 838 | progPinLoc += '_'; |
| 839 | progPinLoc += name; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 840 | if (access(progPinLoc.c_str(), F_OK) == 0) { |
Maciej Żenczykowski | aa295c8 | 2020-06-16 17:02:48 -0700 | [diff] [blame] | 841 | fd = retrieveProgram(progPinLoc.c_str()); |
| 842 | ALOGD("New bpf prog load reusing prog %s, ret: %d (%s)\n", progPinLoc.c_str(), fd, |
| 843 | (fd < 0 ? std::strerror(errno) : "no error")); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 844 | reuse = true; |
| 845 | } else { |
| 846 | vector<char> log_buf(BPF_LOAD_LOG_SZ, 0); |
| 847 | |
Tyler Wear | 4e2f460 | 2022-02-03 09:46:01 -0800 | [diff] [blame] | 848 | struct bpf_load_program_attr attr = { |
| 849 | .prog_type = cs[i].type, |
| 850 | .name = name.c_str(), |
| 851 | .insns = (struct bpf_insn*)cs[i].data.data(), |
| 852 | .license = license.c_str(), |
| 853 | .log_level = 0, |
| 854 | .expected_attach_type = cs[i].expected_attach_type, |
| 855 | }; |
| 856 | |
| 857 | fd = bcc_prog_load_xattr(&attr, cs[i].data.size(), log_buf.data(), log_buf.size(), |
| 858 | true); |
| 859 | |
Steven Moreland | 804bca0 | 2019-12-12 17:21:23 -0800 | [diff] [blame] | 860 | ALOGD("bpf_prog_load lib call for %s (%s) returned fd: %d (%s)\n", elfPath, |
| 861 | 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] | 862 | |
Maciej Żenczykowski | 524deef | 2020-02-11 11:12:37 -0800 | [diff] [blame] | 863 | if (fd < 0) { |
Maciej Żenczykowski | f7c0d99 | 2021-01-21 16:01:04 -0800 | [diff] [blame] | 864 | vector<string> lines = android::base::Split(log_buf.data(), "\n"); |
Maciej Żenczykowski | 524deef | 2020-02-11 11:12:37 -0800 | [diff] [blame] | 865 | |
Maciej Żenczykowski | aa295c8 | 2020-06-16 17:02:48 -0700 | [diff] [blame] | 866 | ALOGW("bpf_prog_load - BEGIN log_buf contents:"); |
| 867 | for (const auto& line : lines) ALOGW("%s", line.c_str()); |
| 868 | ALOGW("bpf_prog_load - END log_buf contents."); |
| 869 | |
| 870 | if (cs[i].prog_def->optional) { |
| 871 | ALOGW("failed program is marked optional - continuing..."); |
| 872 | continue; |
| 873 | } |
| 874 | ALOGE("non-optional program failed to load."); |
Maciej Żenczykowski | 524deef | 2020-02-11 11:12:37 -0800 | [diff] [blame] | 875 | } |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | if (fd < 0) return fd; |
| 879 | if (fd == 0) return -EINVAL; |
| 880 | |
| 881 | if (!reuse) { |
| 882 | ret = bpf_obj_pin(fd, progPinLoc.c_str()); |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 883 | if (ret) return -errno; |
Stephane Lee | 16c9360 | 2022-03-08 17:27:09 -0800 | [diff] [blame] | 884 | if (chmod(progPinLoc.c_str(), 0440)) return -errno; |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 885 | if (cs[i].prog_def.has_value()) { |
| 886 | if (chown(progPinLoc.c_str(), (uid_t)cs[i].prog_def->uid, |
| 887 | (gid_t)cs[i].prog_def->gid)) { |
| 888 | return -errno; |
| 889 | } |
| 890 | } |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Connor O'Brien | 8d49fc7 | 2019-10-24 18:23:49 -0700 | [diff] [blame] | 893 | cs[i].prog_fd.reset(fd); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 899 | int loadProg(const char* elfPath, bool* isCritical, const char* prefix, |
| 900 | const bpf_prog_type* allowed, size_t numAllowed) { |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 901 | vector<char> license; |
Maciej Żenczykowski | 4ba8c1c | 2020-06-10 15:49:31 -0700 | [diff] [blame] | 902 | vector<char> critical; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 903 | vector<codeSection> cs; |
Connor O'Brien | 8d49fc7 | 2019-10-24 18:23:49 -0700 | [diff] [blame] | 904 | vector<unique_fd> mapFds; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 905 | int ret; |
| 906 | |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 907 | if (!isCritical) return -1; |
| 908 | *isCritical = false; |
| 909 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 910 | ifstream elfFile(elfPath, ios::in | ios::binary); |
| 911 | if (!elfFile.is_open()) return -1; |
| 912 | |
Maciej Żenczykowski | 4ba8c1c | 2020-06-10 15:49:31 -0700 | [diff] [blame] | 913 | ret = readSectionByName("critical", elfFile, critical); |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 914 | *isCritical = !ret; |
Maciej Żenczykowski | 4ba8c1c | 2020-06-10 15:49:31 -0700 | [diff] [blame] | 915 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 916 | ret = readSectionByName("license", elfFile, license); |
| 917 | if (ret) { |
| 918 | ALOGE("Couldn't find license in %s\n", elfPath); |
| 919 | return ret; |
| 920 | } else { |
Maciej Żenczykowski | 4ba8c1c | 2020-06-10 15:49:31 -0700 | [diff] [blame] | 921 | ALOGD("Loading %s%s ELF object %s with license %s\n", |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 922 | *isCritical ? "critical for " : "optional", *isCritical ? (char*)critical.data() : "", |
Maciej Żenczykowski | 4ba8c1c | 2020-06-10 15:49:31 -0700 | [diff] [blame] | 923 | elfPath, (char*)license.data()); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 926 | // the following default values are for bpfloader V0.0 format which does not include them |
| 927 | unsigned int bpfLoaderMinVer = |
| 928 | readSectionUint("bpfloader_min_ver", elfFile, DEFAULT_BPFLOADER_MIN_VER); |
| 929 | unsigned int bpfLoaderMaxVer = |
| 930 | readSectionUint("bpfloader_max_ver", elfFile, DEFAULT_BPFLOADER_MAX_VER); |
| 931 | size_t sizeOfBpfMapDef = |
| 932 | readSectionUint("size_of_bpf_map_def", elfFile, DEFAULT_SIZEOF_BPF_MAP_DEF); |
| 933 | size_t sizeOfBpfProgDef = |
| 934 | readSectionUint("size_of_bpf_prog_def", elfFile, DEFAULT_SIZEOF_BPF_PROG_DEF); |
| 935 | |
| 936 | // inclusive lower bound check |
| 937 | if (BPFLOADER_VERSION < bpfLoaderMinVer) { |
| 938 | ALOGI("BpfLoader version 0x%05x ignoring ELF object %s with min ver 0x%05x\n", |
| 939 | BPFLOADER_VERSION, elfPath, bpfLoaderMinVer); |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | // exclusive upper bound check |
| 944 | if (BPFLOADER_VERSION >= bpfLoaderMaxVer) { |
| 945 | ALOGI("BpfLoader version 0x%05x ignoring ELF object %s with max ver 0x%05x\n", |
| 946 | BPFLOADER_VERSION, elfPath, bpfLoaderMaxVer); |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | ALOGI("BpfLoader version 0x%05x processing ELF object %s with ver [0x%05x,0x%05x)\n", |
| 951 | BPFLOADER_VERSION, elfPath, bpfLoaderMinVer, bpfLoaderMaxVer); |
| 952 | |
| 953 | if (sizeOfBpfMapDef < DEFAULT_SIZEOF_BPF_MAP_DEF) { |
| 954 | ALOGE("sizeof(bpf_map_def) of %zu is too small (< %d)\n", sizeOfBpfMapDef, |
| 955 | DEFAULT_SIZEOF_BPF_MAP_DEF); |
| 956 | return -1; |
| 957 | } |
| 958 | |
| 959 | if (sizeOfBpfProgDef < DEFAULT_SIZEOF_BPF_PROG_DEF) { |
| 960 | ALOGE("sizeof(bpf_prog_def) of %zu is too small (< %d)\n", sizeOfBpfProgDef, |
| 961 | DEFAULT_SIZEOF_BPF_PROG_DEF); |
| 962 | return -1; |
| 963 | } |
| 964 | |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 965 | ret = readCodeSections(elfFile, cs, sizeOfBpfProgDef, allowed, numAllowed); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 966 | if (ret) { |
| 967 | ALOGE("Couldn't read all code sections in %s\n", elfPath); |
| 968 | return ret; |
| 969 | } |
| 970 | |
| 971 | /* Just for future debugging */ |
| 972 | if (0) dumpAllCs(cs); |
| 973 | |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 974 | ret = createMaps(elfPath, elfFile, mapFds, prefix, sizeOfBpfMapDef); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 975 | if (ret) { |
| 976 | ALOGE("Failed to create maps: (ret=%d) in %s\n", ret, elfPath); |
| 977 | return ret; |
| 978 | } |
| 979 | |
| 980 | for (int i = 0; i < (int)mapFds.size(); i++) |
Connor O'Brien | 8d49fc7 | 2019-10-24 18:23:49 -0700 | [diff] [blame] | 981 | ALOGD("map_fd found at %d is %d in %s\n", i, mapFds[i].get(), elfPath); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 982 | |
| 983 | applyMapRelo(elfFile, mapFds, cs); |
| 984 | |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 985 | ret = loadCodeSections(elfPath, cs, string(license.data()), prefix); |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 986 | if (ret) ALOGE("Failed to load programs, loadCodeSections ret=%d\n", ret); |
| 987 | |
| 988 | return ret; |
| 989 | } |
| 990 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 991 | } // namespace bpf |
| 992 | } // namespace android |