blob: ce710a4feb9f8bea3849ab991f2d2fa8b307c021 [file] [log] [blame]
Joel Fernandesd76a2002018-10-16 13:19:58 -07001/*
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>
Maciej Żenczykowski83f29772020-01-27 03:11:51 -080027#include <sys/stat.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070028#include <sys/utsname.h>
29#include <unistd.h>
30
Maciej Żenczykowski730a3862020-01-27 01:10:48 -080031#include "../progs/include/bpf_map_def.h"
Joel Fernandesd76a2002018-10-16 13:19:58 -070032#include "LoaderUtils.h"
Maciej Żenczykowski07375e22020-02-19 14:23:59 -080033#include "bpf/BpfUtils.h"
Joel Fernandesd76a2002018-10-16 13:19:58 -070034#include "include/libbpf_android.h"
35
36#include <cstdlib>
37#include <fstream>
38#include <iostream>
Connor O'Brien3278a162020-02-13 21:45:22 -080039#include <optional>
Joel Fernandesd76a2002018-10-16 13:19:58 -070040#include <string>
Christopher Ferrisc151c672019-02-01 15:31:26 -080041#include <vector>
Joel Fernandesd76a2002018-10-16 13:19:58 -070042
Steven Moreland4891e612020-01-10 15:35:52 -080043#include <android-base/properties.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070044#include <android-base/strings.h>
Connor O'Brien8d49fc72019-10-24 18:23:49 -070045#include <android-base/unique_fd.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070046
47#define BPF_FS_PATH "/sys/fs/bpf/"
48
49// Size of the BPF log buffer for verifier logging
50#define BPF_LOAD_LOG_SZ 0x1ffff
51
52using android::base::StartsWith;
Connor O'Brien8d49fc72019-10-24 18:23:49 -070053using android::base::unique_fd;
Joel Fernandesd76a2002018-10-16 13:19:58 -070054using std::ifstream;
55using std::ios;
Connor O'Brien3278a162020-02-13 21:45:22 -080056using std::optional;
Christopher Ferrisc151c672019-02-01 15:31:26 -080057using std::string;
Joel Fernandesd76a2002018-10-16 13:19:58 -070058using std::vector;
59
60namespace android {
61namespace bpf {
62
63typedef struct {
64 const char* name;
65 enum bpf_prog_type type;
66} sectionType;
67
68/*
69 * Map section name prefixes to program types, the section name will be:
70 * SEC(<prefix>/<name-of-program>)
71 * For example:
72 * SEC("tracepoint/sched_switch_func") where sched_switch_funcs
73 * is the name of the program, and tracepoint is the type.
74 */
75sectionType sectionNameTypes[] = {
Chenbo Feng5aee2f12018-12-26 16:14:05 -080076 {"kprobe", BPF_PROG_TYPE_KPROBE},
77 {"tracepoint", BPF_PROG_TYPE_TRACEPOINT},
78 {"skfilter", BPF_PROG_TYPE_SOCKET_FILTER},
79 {"cgroupskb", BPF_PROG_TYPE_CGROUP_SKB},
80 {"schedcls", BPF_PROG_TYPE_SCHED_CLS},
81 {"cgroupsock", BPF_PROG_TYPE_CGROUP_SOCK},
Joel Fernandesd76a2002018-10-16 13:19:58 -070082
83 /* End of table */
Chenbo Feng5aee2f12018-12-26 16:14:05 -080084 {"END", BPF_PROG_TYPE_UNSPEC},
Joel Fernandesd76a2002018-10-16 13:19:58 -070085};
86
87typedef struct {
88 enum bpf_prog_type type;
89 string name;
90 vector<char> data;
91 vector<char> rel_data;
Connor O'Brien3278a162020-02-13 21:45:22 -080092 optional<struct bpf_prog_def> prog_def;
Joel Fernandesd76a2002018-10-16 13:19:58 -070093
Connor O'Brien8d49fc72019-10-24 18:23:49 -070094 unique_fd prog_fd; /* fd after loading */
Joel Fernandesd76a2002018-10-16 13:19:58 -070095} codeSection;
96
Joel Fernandesd76a2002018-10-16 13:19:58 -070097static int readElfHeader(ifstream& elfFile, Elf64_Ehdr* eh) {
98 elfFile.seekg(0);
99 if (elfFile.fail()) return -1;
100
101 if (!elfFile.read((char*)eh, sizeof(*eh))) return -1;
102
103 return 0;
104}
105
106/* Reads all section header tables into an Shdr array */
107static int readSectionHeadersAll(ifstream& elfFile, vector<Elf64_Shdr>& shTable) {
108 Elf64_Ehdr eh;
109 int ret = 0;
110
111 ret = readElfHeader(elfFile, &eh);
112 if (ret) return ret;
113
114 elfFile.seekg(eh.e_shoff);
115 if (elfFile.fail()) return -1;
116
117 /* Read shdr table entries */
118 shTable.resize(eh.e_shnum);
119
120 if (!elfFile.read((char*)shTable.data(), (eh.e_shnum * eh.e_shentsize))) return -ENOMEM;
121
122 return 0;
123}
124
125/* Read a section by its index - for ex to get sec hdr strtab blob */
126static int readSectionByIdx(ifstream& elfFile, int id, vector<char>& sec) {
127 vector<Elf64_Shdr> shTable;
128 int entries, ret = 0;
129
130 ret = readSectionHeadersAll(elfFile, shTable);
131 if (ret) return ret;
132 entries = shTable.size();
133
134 elfFile.seekg(shTable[id].sh_offset);
135 if (elfFile.fail()) return -1;
136
137 sec.resize(shTable[id].sh_size);
138 if (!elfFile.read(sec.data(), shTable[id].sh_size)) return -1;
139
140 return 0;
141}
142
143/* Read whole section header string table */
144static int readSectionHeaderStrtab(ifstream& elfFile, vector<char>& strtab) {
145 Elf64_Ehdr eh;
146 int ret = 0;
147
148 ret = readElfHeader(elfFile, &eh);
149 if (ret) return ret;
150
151 ret = readSectionByIdx(elfFile, eh.e_shstrndx, strtab);
152 if (ret) return ret;
153
154 return 0;
155}
156
157/* Get name from offset in strtab */
158static int getSymName(ifstream& elfFile, int nameOff, string& name) {
159 int ret;
160 vector<char> secStrTab;
161
162 ret = readSectionHeaderStrtab(elfFile, secStrTab);
163 if (ret) return ret;
164
165 if (nameOff >= (int)secStrTab.size()) return -1;
166
167 name = string((char*)secStrTab.data() + nameOff);
168 return 0;
169}
170
171/* Reads a full section by name - example to get the GPL license */
172static int readSectionByName(const char* name, ifstream& elfFile, vector<char>& data) {
173 vector<char> secStrTab;
174 vector<Elf64_Shdr> shTable;
175 int ret;
176
177 ret = readSectionHeadersAll(elfFile, shTable);
178 if (ret) return ret;
179
180 ret = readSectionHeaderStrtab(elfFile, secStrTab);
181 if (ret) return ret;
182
183 for (int i = 0; i < (int)shTable.size(); i++) {
184 char* secname = secStrTab.data() + shTable[i].sh_name;
185 if (!secname) continue;
186
187 if (!strcmp(secname, name)) {
188 vector<char> dataTmp;
189 dataTmp.resize(shTable[i].sh_size);
190
191 elfFile.seekg(shTable[i].sh_offset);
192 if (elfFile.fail()) return -1;
193
194 if (!elfFile.read((char*)dataTmp.data(), shTable[i].sh_size)) return -1;
195
196 data = dataTmp;
197 return 0;
198 }
199 }
200 return -2;
201}
202
203static int readSectionByType(ifstream& elfFile, int type, vector<char>& data) {
204 int ret;
205 vector<Elf64_Shdr> shTable;
206
207 ret = readSectionHeadersAll(elfFile, shTable);
208 if (ret) return ret;
209
210 for (int i = 0; i < (int)shTable.size(); i++) {
211 if ((int)shTable[i].sh_type != type) continue;
212
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 return -2;
225}
226
227static bool symCompare(Elf64_Sym a, Elf64_Sym b) {
228 return (a.st_value < b.st_value);
229}
230
231static int readSymTab(ifstream& elfFile, int sort, vector<Elf64_Sym>& data) {
232 int ret, numElems;
233 Elf64_Sym* buf;
234 vector<char> secData;
235
236 ret = readSectionByType(elfFile, SHT_SYMTAB, secData);
237 if (ret) return ret;
238
239 buf = (Elf64_Sym*)secData.data();
240 numElems = (secData.size() / sizeof(Elf64_Sym));
241 data.assign(buf, buf + numElems);
242
243 if (sort) std::sort(data.begin(), data.end(), symCompare);
244 return 0;
245}
246
247static enum bpf_prog_type getSectionType(string& name) {
248 for (int i = 0; sectionNameTypes[i].type != BPF_PROG_TYPE_UNSPEC; i++)
249 if (StartsWith(name, sectionNameTypes[i].name)) return sectionNameTypes[i].type;
250
251 return BPF_PROG_TYPE_UNSPEC;
252}
253
254/* If ever needed
255static string getSectionName(enum bpf_prog_type type)
256{
257 for (int i = 0; sectionNameTypes[i].type != BPF_PROG_TYPE_UNSPEC; i++)
258 if (sectionNameTypes[i].type == type)
259 return std::string(sectionNameTypes[i].name);
260
261 return NULL;
262}
263*/
264
265static bool isRelSection(codeSection& cs, string& name) {
266 for (int i = 0; sectionNameTypes[i].type != BPF_PROG_TYPE_UNSPEC; i++) {
267 sectionType st = sectionNameTypes[i];
268
269 if (st.type != cs.type) continue;
270
271 if (StartsWith(name, std::string(".rel") + st.name + "/"))
272 return true;
273 else
274 return false;
275 }
276 return false;
277}
278
Connor O'Brien3278a162020-02-13 21:45:22 -0800279static int readProgDefs(ifstream& elfFile, vector<struct bpf_prog_def>& pd) {
280 vector<char> pdData;
281 int ret = readSectionByName("progs", elfFile, pdData);
282 if (ret == -2) return 0;
283 if (ret) return ret;
284
285 pd.resize(pdData.size() / sizeof(struct bpf_prog_def));
286 memcpy(pd.data(), pdData.data(), pdData.size());
287 return 0;
288}
289
290static int getSectionSymNames(ifstream& elfFile, const string& sectionName, vector<string>& names) {
291 int ret;
292 string name;
293 vector<Elf64_Sym> symtab;
294 vector<Elf64_Shdr> shTable;
295
296 ret = readSymTab(elfFile, 1 /* sort */, symtab);
297 if (ret) return ret;
298
299 /* Get index of section */
300 ret = readSectionHeadersAll(elfFile, shTable);
301 if (ret) return ret;
302
303 int sec_idx = -1;
304 for (int i = 0; i < (int)shTable.size(); i++) {
305 ret = getSymName(elfFile, shTable[i].sh_name, name);
306 if (ret) return ret;
307
308 if (!name.compare(sectionName)) {
309 sec_idx = i;
310 break;
311 }
312 }
313
314 /* No section found with matching name*/
315 if (sec_idx == -1) {
316 ALOGE("No %s section could be found in elf object\n", sectionName.c_str());
317 return -1;
318 }
319
320 for (int i = 0; i < (int)symtab.size(); i++) {
321 if (symtab[i].st_shndx == sec_idx) {
322 string s;
323 ret = getSymName(elfFile, symtab[i].st_name, s);
324 if (ret) return ret;
325 names.push_back(s);
326 }
327 }
328
329 return 0;
330}
331
Joel Fernandesd76a2002018-10-16 13:19:58 -0700332/* Read a section by its index - for ex to get sec hdr strtab blob */
333static int readCodeSections(ifstream& elfFile, vector<codeSection>& cs) {
334 vector<Elf64_Shdr> shTable;
335 int entries, ret = 0;
336
337 ret = readSectionHeadersAll(elfFile, shTable);
338 if (ret) return ret;
339 entries = shTable.size();
340
Connor O'Brien3278a162020-02-13 21:45:22 -0800341 vector<struct bpf_prog_def> pd;
342 ret = readProgDefs(elfFile, pd);
343 if (ret) return ret;
344 vector<string> progDefNames;
345 ret = getSectionSymNames(elfFile, "progs", progDefNames);
346 if (!pd.empty() && ret) return ret;
347
Joel Fernandesd76a2002018-10-16 13:19:58 -0700348 for (int i = 0; i < entries; i++) {
349 string name;
350 codeSection cs_temp;
351 cs_temp.type = BPF_PROG_TYPE_UNSPEC;
352
353 ret = getSymName(elfFile, shTable[i].sh_name, name);
354 if (ret) return ret;
355
356 enum bpf_prog_type ptype = getSectionType(name);
357 if (ptype != BPF_PROG_TYPE_UNSPEC) {
Connor O'Brien3278a162020-02-13 21:45:22 -0800358 string oldName = name;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700359 deslash(name);
360 cs_temp.type = ptype;
361 cs_temp.name = name;
362
363 ret = readSectionByIdx(elfFile, i, cs_temp.data);
364 if (ret) return ret;
365 ALOGD("Loaded code section %d (%s)\n", i, name.c_str());
Connor O'Brien3278a162020-02-13 21:45:22 -0800366
367 vector<string> csSymNames;
368 ret = getSectionSymNames(elfFile, oldName, csSymNames);
369 if (ret || !csSymNames.size()) return ret;
370 for (size_t i = 0; i < progDefNames.size(); ++i) {
371 if (!progDefNames[i].compare(csSymNames[0] + "_def")) {
372 cs_temp.prog_def = pd[i];
373 break;
374 }
375 }
Joel Fernandesd76a2002018-10-16 13:19:58 -0700376 }
377
378 /* Check for rel section */
379 if (cs_temp.data.size() > 0 && i < entries) {
380 ret = getSymName(elfFile, shTable[i + 1].sh_name, name);
381 if (ret) return ret;
382
383 if (isRelSection(cs_temp, name)) {
384 ret = readSectionByIdx(elfFile, i + 1, cs_temp.rel_data);
385 if (ret) return ret;
386 ALOGD("Loaded relo section %d (%s)\n", i, name.c_str());
387 }
388 }
389
390 if (cs_temp.data.size() > 0) {
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700391 cs.push_back(std::move(cs_temp));
Joel Fernandesd76a2002018-10-16 13:19:58 -0700392 ALOGD("Adding section %d to cs list\n", i);
393 }
394 }
395 return 0;
396}
397
398static int getSymNameByIdx(ifstream& elfFile, int index, string& name) {
399 vector<Elf64_Sym> symtab;
400 int ret = 0;
401
402 ret = readSymTab(elfFile, 0 /* !sort */, symtab);
403 if (ret) return ret;
404
405 if (index >= (int)symtab.size()) return -1;
406
407 return getSymName(elfFile, symtab[index].st_name, name);
408}
409
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700410static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>& mapFds) {
411 int ret;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700412 vector<char> mdData;
413 vector<struct bpf_map_def> md;
414 vector<string> mapNames;
415 string fname = pathToFilename(string(elfPath), true);
416
417 ret = readSectionByName("maps", elfFile, mdData);
Steven Morelandc0905b42019-12-12 14:21:20 -0800418 if (ret == -2) return 0; // no maps to read
Joel Fernandesd76a2002018-10-16 13:19:58 -0700419 if (ret) return ret;
420 md.resize(mdData.size() / sizeof(struct bpf_map_def));
421 memcpy(md.data(), mdData.data(), mdData.size());
422
Connor O'Brien3278a162020-02-13 21:45:22 -0800423 ret = getSectionSymNames(elfFile, "maps", mapNames);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700424 if (ret) return ret;
425
Joel Fernandesd76a2002018-10-16 13:19:58 -0700426 for (int i = 0; i < (int)mapNames.size(); i++) {
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700427 unique_fd fd;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700428 // Format of pin location is /sys/fs/bpf/map_<filename>_<mapname>
429 string mapPinLoc;
430 bool reuse = false;
431
432 mapPinLoc = string(BPF_FS_PATH) + "map_" + fname + "_" + string(mapNames[i]);
433 if (access(mapPinLoc.c_str(), F_OK) == 0) {
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700434 fd.reset(bpf_obj_get(mapPinLoc.c_str()));
435 ALOGD("bpf_create_map reusing map %s, ret: %d\n", mapNames[i].c_str(), fd.get());
Joel Fernandesd76a2002018-10-16 13:19:58 -0700436 reuse = true;
437 } else {
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700438 fd.reset(bpf_create_map(md[i].type, mapNames[i].c_str(), md[i].key_size, md[i].value_size,
439 md[i].max_entries, md[i].map_flags));
440 ALOGD("bpf_create_map name %s, ret: %d\n", mapNames[i].c_str(), fd.get());
Joel Fernandesd76a2002018-10-16 13:19:58 -0700441 }
442
443 if (fd < 0) return fd;
444 if (fd == 0) return -EINVAL;
445
446 if (!reuse) {
447 ret = bpf_obj_pin(fd, mapPinLoc.c_str());
Maciej Żenczykowski83f29772020-01-27 03:11:51 -0800448 if (ret) return -errno;
449 ret = chown(mapPinLoc.c_str(), (uid_t)md[i].uid, (gid_t)md[i].gid);
450 if (ret) return -errno;
451 ret = chmod(mapPinLoc.c_str(), md[i].mode);
452 if (ret) return -errno;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700453 }
454
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700455 mapFds.push_back(std::move(fd));
Joel Fernandesd76a2002018-10-16 13:19:58 -0700456 }
457
458 return ret;
459}
460
461/* For debugging, dump all instructions */
462static void dumpIns(char* ins, int size) {
463 for (int row = 0; row < size / 8; row++) {
464 ALOGE("%d: ", row);
465 for (int j = 0; j < 8; j++) {
466 ALOGE("%3x ", ins[(row * 8) + j]);
467 }
468 ALOGE("\n");
469 }
470}
471
472/* For debugging, dump all code sections from cs list */
473static void dumpAllCs(vector<codeSection>& cs) {
474 for (int i = 0; i < (int)cs.size(); i++) {
475 ALOGE("Dumping cs %d, name %s\n", int(i), cs[i].name.c_str());
476 dumpIns((char*)cs[i].data.data(), cs[i].data.size());
477 ALOGE("-----------\n");
478 }
479}
480
481static void applyRelo(void* insnsPtr, Elf64_Addr offset, int fd) {
482 int insnIndex;
483 struct bpf_insn *insn, *insns;
484
485 insns = (struct bpf_insn*)(insnsPtr);
486
487 insnIndex = offset / sizeof(struct bpf_insn);
488 insn = &insns[insnIndex];
489
490 ALOGD(
491 "applying relo to instruction at byte offset: %d, \
492 insn offset %d , insn %lx\n",
493 (int)offset, (int)insnIndex, *(unsigned long*)insn);
494
495 if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) {
496 ALOGE("Dumping all instructions till ins %d\n", insnIndex);
497 ALOGE("invalid relo for insn %d: code 0x%x\n", insnIndex, insn->code);
498 dumpIns((char*)insnsPtr, (insnIndex + 3) * 8);
499 return;
500 }
501
502 insn->imm = fd;
503 insn->src_reg = BPF_PSEUDO_MAP_FD;
504}
505
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700506static void applyMapRelo(ifstream& elfFile, vector<unique_fd> &mapFds, vector<codeSection>& cs) {
Joel Fernandesd76a2002018-10-16 13:19:58 -0700507 vector<string> mapNames;
508
Connor O'Brien3278a162020-02-13 21:45:22 -0800509 int ret = getSectionSymNames(elfFile, "maps", mapNames);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700510 if (ret) return;
511
512 for (int k = 0; k != (int)cs.size(); k++) {
513 Elf64_Rel* rel = (Elf64_Rel*)(cs[k].rel_data.data());
514 int n_rel = cs[k].rel_data.size() / sizeof(*rel);
515
516 for (int i = 0; i < n_rel; i++) {
517 int symIndex = ELF64_R_SYM(rel[i].r_info);
518 string symName;
519
520 ret = getSymNameByIdx(elfFile, symIndex, symName);
521 if (ret) return;
522
523 /* Find the map fd and apply relo */
524 for (int j = 0; j < (int)mapNames.size(); j++) {
525 if (!mapNames[j].compare(symName)) {
526 applyRelo(cs[k].data.data(), rel[i].r_offset, mapFds[j]);
527 break;
528 }
529 }
530 }
531 }
532}
533
Christopher Ferrisc151c672019-02-01 15:31:26 -0800534static int loadCodeSections(const char* elfPath, vector<codeSection>& cs, const string& license) {
Maciej Żenczykowski07375e22020-02-19 14:23:59 -0800535 unsigned kvers = kernelVersion();
536 int ret, fd;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700537
Maciej Żenczykowski07375e22020-02-19 14:23:59 -0800538 if (!kvers) return -1;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700539
540 string fname = pathToFilename(string(elfPath), true);
541
542 for (int i = 0; i < (int)cs.size(); i++) {
543 string progPinLoc;
544 bool reuse = false;
545
Maciej Żenczykowski07375e22020-02-19 14:23:59 -0800546 if (cs[i].prog_def.has_value()) {
547 if (kvers < cs[i].prog_def->min_kver) continue;
548 if (kvers >= cs[i].prog_def->max_kver) continue;
549 }
550
Joel Fernandesd76a2002018-10-16 13:19:58 -0700551 // Format of pin location is
552 // /sys/fs/bpf/prog_<filename>_<mapname>
553 progPinLoc = string(BPF_FS_PATH) + "prog_" + fname + "_" + cs[i].name;
554 if (access(progPinLoc.c_str(), F_OK) == 0) {
555 fd = bpf_obj_get(progPinLoc.c_str());
556 ALOGD("New bpf prog load reusing prog %s, ret: %d\n", cs[i].name.c_str(), fd);
557 reuse = true;
558 } else {
559 vector<char> log_buf(BPF_LOAD_LOG_SZ, 0);
560
561 fd = bpf_prog_load(cs[i].type, cs[i].name.c_str(), (struct bpf_insn*)cs[i].data.data(),
562 cs[i].data.size(), license.c_str(), kvers, 0,
563 log_buf.data(), log_buf.size());
Steven Moreland804bca02019-12-12 17:21:23 -0800564 ALOGD("bpf_prog_load lib call for %s (%s) returned fd: %d (%s)\n", elfPath,
565 cs[i].name.c_str(), fd, (fd < 0 ? std::strerror(errno) : "no error"));
Joel Fernandesd76a2002018-10-16 13:19:58 -0700566
Maciej Żenczykowski524deef2020-02-11 11:12:37 -0800567 if (fd < 0) {
568 std::vector<std::string> lines = android::base::Split(log_buf.data(), "\n");
569
570 ALOGE("bpf_prog_load - BEGIN log_buf contents:");
571 for (const auto& line : lines) ALOGE("%s", line.c_str());
572 ALOGE("bpf_prog_load - END log_buf contents.");
573 }
Joel Fernandesd76a2002018-10-16 13:19:58 -0700574 }
575
576 if (fd < 0) return fd;
577 if (fd == 0) return -EINVAL;
578
579 if (!reuse) {
580 ret = bpf_obj_pin(fd, progPinLoc.c_str());
Connor O'Brien3278a162020-02-13 21:45:22 -0800581 if (ret) return -errno;
582 if (cs[i].prog_def.has_value()) {
583 if (chown(progPinLoc.c_str(), (uid_t)cs[i].prog_def->uid,
584 (gid_t)cs[i].prog_def->gid)) {
585 return -errno;
586 }
587 }
588 if (chmod(progPinLoc.c_str(), 0440)) return -errno;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700589 }
590
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700591 cs[i].prog_fd.reset(fd);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700592 }
593
594 return 0;
595}
596
597int loadProg(const char* elfPath) {
598 vector<char> license;
599 vector<codeSection> cs;
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700600 vector<unique_fd> mapFds;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700601 int ret;
602
603 ifstream elfFile(elfPath, ios::in | ios::binary);
604 if (!elfFile.is_open()) return -1;
605
606 ret = readSectionByName("license", elfFile, license);
607 if (ret) {
608 ALOGE("Couldn't find license in %s\n", elfPath);
609 return ret;
610 } else {
611 ALOGD("Loading ELF object %s with license %s\n", elfPath, (char*)license.data());
612 }
613
614 ret = readCodeSections(elfFile, cs);
615 if (ret) {
616 ALOGE("Couldn't read all code sections in %s\n", elfPath);
617 return ret;
618 }
619
620 /* Just for future debugging */
621 if (0) dumpAllCs(cs);
622
623 ret = createMaps(elfPath, elfFile, mapFds);
624 if (ret) {
625 ALOGE("Failed to create maps: (ret=%d) in %s\n", ret, elfPath);
626 return ret;
627 }
628
629 for (int i = 0; i < (int)mapFds.size(); i++)
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700630 ALOGD("map_fd found at %d is %d in %s\n", i, mapFds[i].get(), elfPath);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700631
632 applyMapRelo(elfFile, mapFds, cs);
633
634 ret = loadCodeSections(elfPath, cs, string(license.data()));
635 if (ret) ALOGE("Failed to load programs, loadCodeSections ret=%d\n", ret);
636
637 return ret;
638}
639
Steven Moreland4891e612020-01-10 15:35:52 -0800640void waitForProgsLoaded() {
641 while (!android::base::WaitForProperty("bpf.progs_loaded", "1", std::chrono::seconds(5))) {
642 ALOGW("Waited 5s for bpf.progs_loaded, still waiting...");
643 }
644}
645
Joel Fernandesd76a2002018-10-16 13:19:58 -0700646} // namespace bpf
647} // namespace android