blob: 62245106c763e5f2ebac00befe8a91278b7bc4d0 [file] [log] [blame]
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -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
18#pragma once
19
20#include <linux/bpf.h>
21
22#include <fstream>
23
24namespace android {
25namespace bpf {
26
27// Bpf programs may specify per-program & per-map selinux_context and pin_subdir.
28//
29// The BpfLoader needs to convert these bpf.o specified strings into an enum
30// for internal use (to check that valid values were specified for the specific
31// location of the bpf.o file).
32//
33// It also needs to map selinux_context's into pin_subdir's.
34// This is because of how selinux_context is actually implemented via pin+rename.
35//
36// Thus 'domain' enumerates all selinux_context's/pin_subdir's that the BpfLoader
37// is aware of. Thus there currently needs to be a 1:1 mapping between the two.
38//
39enum class domain : int {
40 unrecognized = -1, // invalid for this version of the bpfloader
41 unspecified = 0, // means just use the default for that specific pin location
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070042 tethering, // (S+) fs_bpf_tethering /sys/fs/bpf/tethering
43 net_private, // (T+) fs_bpf_net_private /sys/fs/bpf/net_private
44 net_shared, // (T+) fs_bpf_net_shared /sys/fs/bpf/net_shared
45 netd_readonly, // (T+) fs_bpf_netd_readonly /sys/fs/bpf/netd_readonly
46 netd_shared, // (T+) fs_bpf_netd_shared /sys/fs/bpf/netd_shared
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070047};
48
49// Note: this does not include domain::unrecognized, but does include domain::unspecified
50static constexpr domain AllDomains[] = {
51 domain::unspecified,
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070052 domain::tethering,
53 domain::net_private,
54 domain::net_shared,
55 domain::netd_readonly,
56 domain::netd_shared,
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070057};
58
59static constexpr bool unrecognized(domain d) {
60 return d == domain::unrecognized;
61}
62
63// Note: this doesn't handle unrecognized, handle it first.
64static constexpr bool specified(domain d) {
65 return d != domain::unspecified;
66}
67
68static constexpr unsigned long long domainToBitmask(domain d) {
69 return specified(d) ? 1uLL << (static_cast<int>(d) - 1) : 0;
70}
71
72static constexpr bool inDomainBitmask(domain d, unsigned long long v) {
73 return domainToBitmask(d) & v;
74}
75
76struct Location {
77 const char* const dir = "";
78 const char* const prefix = "";
79 unsigned long long allowedDomainBitmask = 0;
80 const bpf_prog_type* allowedProgTypes = nullptr;
81 size_t allowedProgTypesLength = 0;
82};
83
84// BPF loader implementation. Loads an eBPF ELF object
85int loadProg(const char* elfPath, bool* isCritical, const Location &location = {});
86
87// Exposed for testing
88unsigned int readSectionUint(const char* name, std::ifstream& elfFile, unsigned int defVal);
89
90// Returns the build type string (from ro.build.type).
91const std::string& getBuildType();
92
93// The following functions classify the 3 Android build types.
94inline bool isEng() {
95 return getBuildType() == "eng";
96}
97inline bool isUser() {
98 return getBuildType() == "user";
99}
100inline bool isUserdebug() {
101 return getBuildType() == "userdebug";
102}
103
104} // namespace bpf
105} // namespace android