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