blob: d8771c109e73daddff458c9f42cde02d810260b0 [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>
27#include <sys/utsname.h>
28#include <unistd.h>
29
30#include "LoaderUtils.h"
31#include "include/libbpf_android.h"
32
33#include <cstdlib>
34#include <fstream>
35#include <iostream>
36#include <string>
37
38#include <android-base/strings.h>
39
40#define BPF_FS_PATH "/sys/fs/bpf/"
41
42// Size of the BPF log buffer for verifier logging
43#define BPF_LOAD_LOG_SZ 0x1ffff
44
45using android::base::StartsWith;
46using std::ifstream;
47using std::ios;
48using std::vector;
49
50namespace android {
51namespace bpf {
52
53typedef struct {
54 const char* name;
55 enum bpf_prog_type type;
56} sectionType;
57
58/*
59 * Map section name prefixes to program types, the section name will be:
60 * SEC(<prefix>/<name-of-program>)
61 * For example:
62 * SEC("tracepoint/sched_switch_func") where sched_switch_funcs
63 * is the name of the program, and tracepoint is the type.
64 */
65sectionType sectionNameTypes[] = {
66 { "kprobe", BPF_PROG_TYPE_KPROBE },
67 { "tracepoint", BPF_PROG_TYPE_TRACEPOINT },
68 { "skfilter", BPF_PROG_TYPE_SOCKET_FILTER },
69 { "cgroupskb", BPF_PROG_TYPE_CGROUP_SKB },
Maciej Żenczykowskiffa7aad2019-01-11 17:46:35 -080070 { "schedcls", BPF_PROG_TYPE_SCHED_CLS },
Joel Fernandesd76a2002018-10-16 13:19:58 -070071
72 /* End of table */
73 { "END", BPF_PROG_TYPE_UNSPEC },
74};
75
76typedef struct {
77 enum bpf_prog_type type;
78 string name;
79 vector<char> data;
80 vector<char> rel_data;
81
82 int prog_fd; /* fd after loading */
83} codeSection;
84
85/* Common with the eBPF C program */
86struct bpf_map_def {
87 enum bpf_map_type type;
88 unsigned int key_size;
89 unsigned int value_size;
90 unsigned int max_entries;
91 unsigned int map_flags;
92 unsigned int inner_map_idx;
93 unsigned int numa_node;
94};
95
96static int readElfHeader(ifstream& elfFile, Elf64_Ehdr* eh) {
97 elfFile.seekg(0);
98 if (elfFile.fail()) return -1;
99
100 if (!elfFile.read((char*)eh, sizeof(*eh))) return -1;
101
102 return 0;
103}
104
105/* Reads all section header tables into an Shdr array */
106static int readSectionHeadersAll(ifstream& elfFile, vector<Elf64_Shdr>& shTable) {
107 Elf64_Ehdr eh;
108 int ret = 0;
109
110 ret = readElfHeader(elfFile, &eh);
111 if (ret) return ret;
112
113 elfFile.seekg(eh.e_shoff);
114 if (elfFile.fail()) return -1;
115
116 /* Read shdr table entries */
117 shTable.resize(eh.e_shnum);
118
119 if (!elfFile.read((char*)shTable.data(), (eh.e_shnum * eh.e_shentsize))) return -ENOMEM;
120
121 return 0;
122}
123
124/* Read a section by its index - for ex to get sec hdr strtab blob */
125static int readSectionByIdx(ifstream& elfFile, int id, vector<char>& sec) {
126 vector<Elf64_Shdr> shTable;
127 int entries, ret = 0;
128
129 ret = readSectionHeadersAll(elfFile, shTable);
130 if (ret) return ret;
131 entries = shTable.size();
132
133 elfFile.seekg(shTable[id].sh_offset);
134 if (elfFile.fail()) return -1;
135
136 sec.resize(shTable[id].sh_size);
137 if (!elfFile.read(sec.data(), shTable[id].sh_size)) return -1;
138
139 return 0;
140}
141
142/* Read whole section header string table */
143static int readSectionHeaderStrtab(ifstream& elfFile, vector<char>& strtab) {
144 Elf64_Ehdr eh;
145 int ret = 0;
146
147 ret = readElfHeader(elfFile, &eh);
148 if (ret) return ret;
149
150 ret = readSectionByIdx(elfFile, eh.e_shstrndx, strtab);
151 if (ret) return ret;
152
153 return 0;
154}
155
156/* Get name from offset in strtab */
157static int getSymName(ifstream& elfFile, int nameOff, string& name) {
158 int ret;
159 vector<char> secStrTab;
160
161 ret = readSectionHeaderStrtab(elfFile, secStrTab);
162 if (ret) return ret;
163
164 if (nameOff >= (int)secStrTab.size()) return -1;
165
166 name = string((char*)secStrTab.data() + nameOff);
167 return 0;
168}
169
170/* Reads a full section by name - example to get the GPL license */
171static int readSectionByName(const char* name, ifstream& elfFile, vector<char>& data) {
172 vector<char> secStrTab;
173 vector<Elf64_Shdr> shTable;
174 int ret;
175
176 ret = readSectionHeadersAll(elfFile, shTable);
177 if (ret) return ret;
178
179 ret = readSectionHeaderStrtab(elfFile, secStrTab);
180 if (ret) return ret;
181
182 for (int i = 0; i < (int)shTable.size(); i++) {
183 char* secname = secStrTab.data() + shTable[i].sh_name;
184 if (!secname) continue;
185
186 if (!strcmp(secname, name)) {
187 vector<char> dataTmp;
188 dataTmp.resize(shTable[i].sh_size);
189
190 elfFile.seekg(shTable[i].sh_offset);
191 if (elfFile.fail()) return -1;
192
193 if (!elfFile.read((char*)dataTmp.data(), shTable[i].sh_size)) return -1;
194
195 data = dataTmp;
196 return 0;
197 }
198 }
199 return -2;
200}
201
202static int readSectionByType(ifstream& elfFile, int type, vector<char>& data) {
203 int ret;
204 vector<Elf64_Shdr> shTable;
205
206 ret = readSectionHeadersAll(elfFile, shTable);
207 if (ret) return ret;
208
209 for (int i = 0; i < (int)shTable.size(); i++) {
210 if ((int)shTable[i].sh_type != type) continue;
211
212 vector<char> dataTmp;
213 dataTmp.resize(shTable[i].sh_size);
214
215 elfFile.seekg(shTable[i].sh_offset);
216 if (elfFile.fail()) return -1;
217
218 if (!elfFile.read((char*)dataTmp.data(), shTable[i].sh_size)) return -1;
219
220 data = dataTmp;
221 return 0;
222 }
223 return -2;
224}
225
226static bool symCompare(Elf64_Sym a, Elf64_Sym b) {
227 return (a.st_value < b.st_value);
228}
229
230static int readSymTab(ifstream& elfFile, int sort, vector<Elf64_Sym>& data) {
231 int ret, numElems;
232 Elf64_Sym* buf;
233 vector<char> secData;
234
235 ret = readSectionByType(elfFile, SHT_SYMTAB, secData);
236 if (ret) return ret;
237
238 buf = (Elf64_Sym*)secData.data();
239 numElems = (secData.size() / sizeof(Elf64_Sym));
240 data.assign(buf, buf + numElems);
241
242 if (sort) std::sort(data.begin(), data.end(), symCompare);
243 return 0;
244}
245
246static enum bpf_prog_type getSectionType(string& name) {
247 for (int i = 0; sectionNameTypes[i].type != BPF_PROG_TYPE_UNSPEC; i++)
248 if (StartsWith(name, sectionNameTypes[i].name)) return sectionNameTypes[i].type;
249
250 return BPF_PROG_TYPE_UNSPEC;
251}
252
253/* If ever needed
254static string getSectionName(enum bpf_prog_type type)
255{
256 for (int i = 0; sectionNameTypes[i].type != BPF_PROG_TYPE_UNSPEC; i++)
257 if (sectionNameTypes[i].type == type)
258 return std::string(sectionNameTypes[i].name);
259
260 return NULL;
261}
262*/
263
264static bool isRelSection(codeSection& cs, string& name) {
265 for (int i = 0; sectionNameTypes[i].type != BPF_PROG_TYPE_UNSPEC; i++) {
266 sectionType st = sectionNameTypes[i];
267
268 if (st.type != cs.type) continue;
269
270 if (StartsWith(name, std::string(".rel") + st.name + "/"))
271 return true;
272 else
273 return false;
274 }
275 return false;
276}
277
278/* Read a section by its index - for ex to get sec hdr strtab blob */
279static int readCodeSections(ifstream& elfFile, vector<codeSection>& cs) {
280 vector<Elf64_Shdr> shTable;
281 int entries, ret = 0;
282
283 ret = readSectionHeadersAll(elfFile, shTable);
284 if (ret) return ret;
285 entries = shTable.size();
286
287 for (int i = 0; i < entries; i++) {
288 string name;
289 codeSection cs_temp;
290 cs_temp.type = BPF_PROG_TYPE_UNSPEC;
291
292 ret = getSymName(elfFile, shTable[i].sh_name, name);
293 if (ret) return ret;
294
295 enum bpf_prog_type ptype = getSectionType(name);
296 if (ptype != BPF_PROG_TYPE_UNSPEC) {
297 deslash(name);
298 cs_temp.type = ptype;
299 cs_temp.name = name;
300
301 ret = readSectionByIdx(elfFile, i, cs_temp.data);
302 if (ret) return ret;
303 ALOGD("Loaded code section %d (%s)\n", i, name.c_str());
304 }
305
306 /* Check for rel section */
307 if (cs_temp.data.size() > 0 && i < entries) {
308 ret = getSymName(elfFile, shTable[i + 1].sh_name, name);
309 if (ret) return ret;
310
311 if (isRelSection(cs_temp, name)) {
312 ret = readSectionByIdx(elfFile, i + 1, cs_temp.rel_data);
313 if (ret) return ret;
314 ALOGD("Loaded relo section %d (%s)\n", i, name.c_str());
315 }
316 }
317
318 if (cs_temp.data.size() > 0) {
319 cs.push_back(cs_temp);
320 ALOGD("Adding section %d to cs list\n", i);
321 }
322 }
323 return 0;
324}
325
326static int getSymNameByIdx(ifstream& elfFile, int index, string& name) {
327 vector<Elf64_Sym> symtab;
328 int ret = 0;
329
330 ret = readSymTab(elfFile, 0 /* !sort */, symtab);
331 if (ret) return ret;
332
333 if (index >= (int)symtab.size()) return -1;
334
335 return getSymName(elfFile, symtab[index].st_name, name);
336}
337
338static int getMapNames(ifstream& elfFile, vector<string>& names) {
339 int ret;
340 string mapName;
341 vector<Elf64_Sym> symtab;
342 vector<Elf64_Shdr> shTable;
343
344 ret = readSymTab(elfFile, 1 /* sort */, symtab);
345 if (ret) return ret;
346
347 /* Get index of maps section */
348 ret = readSectionHeadersAll(elfFile, shTable);
349 if (ret) return ret;
350
351 int maps_idx = -1;
352 for (int i = 0; i < (int)shTable.size(); i++) {
353 ret = getSymName(elfFile, shTable[i].sh_name, mapName);
354 if (ret) return ret;
355
356 if (!mapName.compare("maps")) {
357 maps_idx = i;
358 break;
359 }
360 }
361
362 /* No maps found */
363 if (maps_idx == -1) {
364 ALOGE("No maps could be found in elf object\n");
365 return -1;
366 }
367
368 for (int i = 0; i < (int)symtab.size(); i++) {
369 if (symtab[i].st_shndx == maps_idx) {
370 string s;
371 ret = getSymName(elfFile, symtab[i].st_name, s);
372 if (ret) return ret;
373 names.push_back(s);
374 }
375 }
376
377 return 0;
378}
379
380static int createMaps(const char* elfPath, ifstream& elfFile, vector<int>& mapFds) {
381 int ret, fd;
382 vector<char> mdData;
383 vector<struct bpf_map_def> md;
384 vector<string> mapNames;
385 string fname = pathToFilename(string(elfPath), true);
386
387 ret = readSectionByName("maps", elfFile, mdData);
388 if (ret) return ret;
389 md.resize(mdData.size() / sizeof(struct bpf_map_def));
390 memcpy(md.data(), mdData.data(), mdData.size());
391
392 ret = getMapNames(elfFile, mapNames);
393 if (ret) return ret;
394
395 mapFds.resize(mapNames.size());
396
397 for (int i = 0; i < (int)mapNames.size(); i++) {
398 // Format of pin location is /sys/fs/bpf/map_<filename>_<mapname>
399 string mapPinLoc;
400 bool reuse = false;
401
402 mapPinLoc = string(BPF_FS_PATH) + "map_" + fname + "_" + string(mapNames[i]);
403 if (access(mapPinLoc.c_str(), F_OK) == 0) {
404 fd = bpf_obj_get(mapPinLoc.c_str());
405 ALOGD("bpf_create_map reusing map %s, ret: %d\n", mapNames[i].c_str(), fd);
406 reuse = true;
407 } else {
408 fd = bpf_create_map(md[i].type, mapNames[i].c_str(), md[i].key_size, md[i].value_size,
409 md[i].max_entries, md[i].map_flags);
410 ALOGD("bpf_create_map name %s, ret: %d\n", mapNames[i].c_str(), fd);
411 }
412
413 if (fd < 0) return fd;
414 if (fd == 0) return -EINVAL;
415
416 if (!reuse) {
417 ret = bpf_obj_pin(fd, mapPinLoc.c_str());
418 if (ret < 0) return ret;
419 }
420
421 mapFds[i] = fd;
422 }
423
424 return ret;
425}
426
427/* For debugging, dump all instructions */
428static void dumpIns(char* ins, int size) {
429 for (int row = 0; row < size / 8; row++) {
430 ALOGE("%d: ", row);
431 for (int j = 0; j < 8; j++) {
432 ALOGE("%3x ", ins[(row * 8) + j]);
433 }
434 ALOGE("\n");
435 }
436}
437
438/* For debugging, dump all code sections from cs list */
439static void dumpAllCs(vector<codeSection>& cs) {
440 for (int i = 0; i < (int)cs.size(); i++) {
441 ALOGE("Dumping cs %d, name %s\n", int(i), cs[i].name.c_str());
442 dumpIns((char*)cs[i].data.data(), cs[i].data.size());
443 ALOGE("-----------\n");
444 }
445}
446
447static void applyRelo(void* insnsPtr, Elf64_Addr offset, int fd) {
448 int insnIndex;
449 struct bpf_insn *insn, *insns;
450
451 insns = (struct bpf_insn*)(insnsPtr);
452
453 insnIndex = offset / sizeof(struct bpf_insn);
454 insn = &insns[insnIndex];
455
456 ALOGD(
457 "applying relo to instruction at byte offset: %d, \
458 insn offset %d , insn %lx\n",
459 (int)offset, (int)insnIndex, *(unsigned long*)insn);
460
461 if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) {
462 ALOGE("Dumping all instructions till ins %d\n", insnIndex);
463 ALOGE("invalid relo for insn %d: code 0x%x\n", insnIndex, insn->code);
464 dumpIns((char*)insnsPtr, (insnIndex + 3) * 8);
465 return;
466 }
467
468 insn->imm = fd;
469 insn->src_reg = BPF_PSEUDO_MAP_FD;
470}
471
472static void applyMapRelo(ifstream& elfFile, vector<int> mapFds, vector<codeSection>& cs) {
473 vector<string> mapNames;
474
475 int ret = getMapNames(elfFile, mapNames);
476 if (ret) return;
477
478 for (int k = 0; k != (int)cs.size(); k++) {
479 Elf64_Rel* rel = (Elf64_Rel*)(cs[k].rel_data.data());
480 int n_rel = cs[k].rel_data.size() / sizeof(*rel);
481
482 for (int i = 0; i < n_rel; i++) {
483 int symIndex = ELF64_R_SYM(rel[i].r_info);
484 string symName;
485
486 ret = getSymNameByIdx(elfFile, symIndex, symName);
487 if (ret) return;
488
489 /* Find the map fd and apply relo */
490 for (int j = 0; j < (int)mapNames.size(); j++) {
491 if (!mapNames[j].compare(symName)) {
492 applyRelo(cs[k].data.data(), rel[i].r_offset, mapFds[j]);
493 break;
494 }
495 }
496 }
497 }
498}
499
500static int loadCodeSections(const char* elfPath, vector<codeSection>& cs, string license) {
501 int ret, fd, kvers;
502
503 if ((kvers = getMachineKvers()) < 0) return -1;
504
505 string fname = pathToFilename(string(elfPath), true);
506
507 for (int i = 0; i < (int)cs.size(); i++) {
508 string progPinLoc;
509 bool reuse = false;
510
511 // Format of pin location is
512 // /sys/fs/bpf/prog_<filename>_<mapname>
513 progPinLoc = string(BPF_FS_PATH) + "prog_" + fname + "_" + cs[i].name;
514 if (access(progPinLoc.c_str(), F_OK) == 0) {
515 fd = bpf_obj_get(progPinLoc.c_str());
516 ALOGD("New bpf prog load reusing prog %s, ret: %d\n", cs[i].name.c_str(), fd);
517 reuse = true;
518 } else {
519 vector<char> log_buf(BPF_LOAD_LOG_SZ, 0);
520
521 fd = bpf_prog_load(cs[i].type, cs[i].name.c_str(), (struct bpf_insn*)cs[i].data.data(),
522 cs[i].data.size(), license.c_str(), kvers, 0,
523 log_buf.data(), log_buf.size());
524 ALOGD("New bpf core prog_load for %s (%s) returned: %d\n", elfPath, cs[i].name.c_str(),
525 fd);
526
527 if (fd <= 0)
528 ALOGE("bpf_prog_load: log_buf contents: %s\n", (char *)log_buf.data());
529 }
530
531 if (fd < 0) return fd;
532 if (fd == 0) return -EINVAL;
533
534 if (!reuse) {
535 ret = bpf_obj_pin(fd, progPinLoc.c_str());
536 if (ret < 0) return ret;
537 }
538
539 cs[i].prog_fd = fd;
540 }
541
542 return 0;
543}
544
545int loadProg(const char* elfPath) {
546 vector<char> license;
547 vector<codeSection> cs;
548 vector<int> mapFds;
549 int ret;
550
551 ifstream elfFile(elfPath, ios::in | ios::binary);
552 if (!elfFile.is_open()) return -1;
553
554 ret = readSectionByName("license", elfFile, license);
555 if (ret) {
556 ALOGE("Couldn't find license in %s\n", elfPath);
557 return ret;
558 } else {
559 ALOGD("Loading ELF object %s with license %s\n", elfPath, (char*)license.data());
560 }
561
562 ret = readCodeSections(elfFile, cs);
563 if (ret) {
564 ALOGE("Couldn't read all code sections in %s\n", elfPath);
565 return ret;
566 }
567
568 /* Just for future debugging */
569 if (0) dumpAllCs(cs);
570
571 ret = createMaps(elfPath, elfFile, mapFds);
572 if (ret) {
573 ALOGE("Failed to create maps: (ret=%d) in %s\n", ret, elfPath);
574 return ret;
575 }
576
577 for (int i = 0; i < (int)mapFds.size(); i++)
578 ALOGD("map_fd found at %d is %d in %s\n", i, mapFds[i], elfPath);
579
580 applyMapRelo(elfFile, mapFds, cs);
581
582 ret = loadCodeSections(elfPath, cs, string(license.data()));
583 if (ret) ALOGE("Failed to load programs, loadCodeSections ret=%d\n", ret);
584
585 return ret;
586}
587
588} // namespace bpf
589} // namespace android