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