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