blob: 31bd539eabb23f04842f9dc47e806e5a5f881b47 [file] [log] [blame]
Joel Fernandesd76a2002018-10-16 13:19:58 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 * Android BPF library - public API
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Maciej Żenczykowski23c436f2021-01-21 14:24:15 -080018#pragma once
Joel Fernandesd76a2002018-10-16 13:19:58 -070019
20#include <libbpf.h>
21#include <linux/bpf.h>
22
Maciej Żenczykowski300c51f2022-12-14 04:18:02 -080023#include <fstream>
24
Joel Fernandesd76a2002018-10-16 13:19:58 -070025namespace android {
26namespace bpf {
Steven Moreland4891e612020-01-10 15:35:52 -080027
Maciej Żenczykowski41817132022-06-03 08:41:04 -070028// Bpf programs may specify per-program & per-map selinux_context and pin_subdir.
29//
30// The BpfLoader needs to convert these bpf.o specified strings into an enum
31// for internal use (to check that valid values were specified for the specific
32// location of the bpf.o file).
33//
34// It also needs to map selinux_context's into pin_subdir's.
35// This is because of how selinux_context is actually implemented via pin+rename.
36//
37// Thus 'domain' enumerates all selinux_context's/pin_subdir's that the BpfLoader
38// is aware of. Thus there currently needs to be a 1:1 mapping between the two.
39//
40enum class domain : int {
41 unrecognized = -1, // invalid for this version of the bpfloader
42 unspecified = 0, // means just use the default for that specific pin location
43 platform, // fs_bpf /sys/fs/bpf
44 tethering, // (S+) fs_bpf_tethering /sys/fs/bpf/tethering
45 net_private, // (T+) fs_bpf_net_private /sys/fs/bpf/net_private
46 net_shared, // (T+) fs_bpf_net_shared /sys/fs/bpf/net_shared
47 netd_readonly, // (T+) fs_bpf_netd_readonly /sys/fs/bpf/netd_readonly
48 netd_shared, // (T+) fs_bpf_netd_shared /sys/fs/bpf/netd_shared
49 vendor, // (T+) fs_bpf_vendor /sys/fs/bpf/vendor
Maciej Żenczykowski9a2093d2022-12-02 13:46:53 +000050 loader, // (U+) fs_bpf_loader /sys/fs/bpf/loader
Maciej Żenczykowski41817132022-06-03 08:41:04 -070051};
52
53// Note: this does not include domain::unrecognized, but does include domain::unspecified
54static constexpr domain AllDomains[] = {
55 domain::unspecified,
56 domain::platform,
57 domain::tethering,
58 domain::net_private,
59 domain::net_shared,
60 domain::netd_readonly,
61 domain::netd_shared,
62 domain::vendor,
Maciej Żenczykowski9a2093d2022-12-02 13:46:53 +000063 domain::loader,
Maciej Żenczykowski41817132022-06-03 08:41:04 -070064};
65
66static constexpr bool unrecognized(domain d) {
67 return d == domain::unrecognized;
68}
69
70// Note: this doesn't handle unrecognized, handle it first.
71static constexpr bool specified(domain d) {
72 return d != domain::unspecified;
73}
74
75static constexpr unsigned long long domainToBitmask(domain d) {
76 return specified(d) ? 1uLL << (static_cast<int>(d) - 1) : 0;
77}
78
79static constexpr bool inDomainBitmask(domain d, unsigned long long v) {
80 return domainToBitmask(d) & v;
81}
82
Connor O'Brien6c0ce9f2022-12-01 20:08:17 -080083struct Location {
84 const char* const dir = "";
85 const char* const prefix = "";
86 unsigned long long allowedDomainBitmask = 0;
87 const bpf_prog_type* allowedProgTypes = nullptr;
88 size_t allowedProgTypesLength = 0;
89};
90
Joel Fernandesd76a2002018-10-16 13:19:58 -070091// BPF loader implementation. Loads an eBPF ELF object
Connor O'Brien6c0ce9f2022-12-01 20:08:17 -080092int loadProg(const char* elfPath, bool* isCritical, const Location &location = {});
Steven Moreland4891e612020-01-10 15:35:52 -080093
Maciej Żenczykowski7ed94ef2021-07-06 01:47:15 -070094// Exposed for testing
95unsigned int readSectionUint(const char* name, std::ifstream& elfFile, unsigned int defVal);
96
Ryan Zukliece89f502022-12-15 16:14:32 -080097// Returns the build type string (from ro.build.type).
98const std::string& getBuildType();
99
100// The following functions classify the 3 Android build types.
101inline bool isEng() {
102 return getBuildType() == "eng";
103}
104inline bool isUser() {
105 return getBuildType() == "user";
106}
107inline bool isUserdebug() {
108 return getBuildType() == "userdebug";
109}
110
Joel Fernandesd76a2002018-10-16 13:19:58 -0700111} // namespace bpf
112} // namespace android