1 /*
2  * Copyright (C) 2018-2023 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 #pragma once
18 
19 #include <linux/bpf.h>
20 
21 #include <fstream>
22 
23 namespace android {
24 namespace 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 //
38 enum 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
41     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
46 };
47 
48 // Note: this does not include domain::unrecognized, but does include domain::unspecified
49 static constexpr domain AllDomains[] = {
50     domain::unspecified,
51     domain::tethering,
52     domain::net_private,
53     domain::net_shared,
54     domain::netd_readonly,
55     domain::netd_shared,
56 };
57 
unrecognized(domain d)58 static constexpr bool unrecognized(domain d) {
59     return d == domain::unrecognized;
60 }
61 
62 // Note: this doesn't handle unrecognized, handle it first.
specified(domain d)63 static constexpr bool specified(domain d) {
64     return d != domain::unspecified;
65 }
66 
67 struct Location {
68     const char* const dir = "";
69     const char* const prefix = "";
70 };
71 
72 // BPF loader implementation. Loads an eBPF ELF object
73 int loadProg(const char* elfPath, bool* isCritical, const unsigned int bpfloader_ver,
74              const Location &location = {});
75 
76 // Exposed for testing
77 unsigned int readSectionUint(const char* name, std::ifstream& elfFile, unsigned int defVal);
78 
79 // Returns the build type string (from ro.build.type).
80 const std::string& getBuildType();
81 
82 // The following functions classify the 3 Android build types.
isEng()83 inline bool isEng() {
84     return getBuildType() == "eng";
85 }
isUser()86 inline bool isUser() {
87     return getBuildType() == "user";
88 }
isUserdebug()89 inline bool isUserdebug() {
90     return getBuildType() == "userdebug";
91 }
92 
93 }  // namespace bpf
94 }  // namespace android
95