diff --git a/bpfloader/BpfLoader.cpp b/bpfloader/BpfLoader.cpp index a7748a2..be96c51 100644 --- a/bpfloader/BpfLoader.cpp +++ b/bpfloader/BpfLoader.cpp @@ -257,6 +257,13 @@ int main(int argc, char** argv) { (void)argc; android::base::InitLogging(argv, &android::base::KernelLogger); + // Ensure we can determine the Android build type. + if (!android::bpf::isEng() && !android::bpf::isUser() && !android::bpf::isUserdebug()) { + ALOGE("Failed to determine the build type: got %s, want 'eng', 'user', or 'userdebug'", + android::bpf::getBuildType().c_str()); + return 1; + } + // Linux 5.16-rc1 changed the default to 2 (disabled but changeable), but we need 0 (enabled) // (this writeFile is known to fail on at least 4.19, but always defaults to 0 on pre-5.13, // on 5.13+ it depends on CONFIG_BPF_UNPRIV_DEFAULT_OFF) diff --git a/libbpf_android/Android.bp b/libbpf_android/Android.bp index 1893c6d..dda943d 100644 --- a/libbpf_android/Android.bp +++ b/libbpf_android/Android.bp @@ -39,6 +39,7 @@ cc_library { shared_libs: [ "libbase", + "libcutils", "libutils", "liblog", "libbpf_bcc", diff --git a/libbpf_android/Loader.cpp b/libbpf_android/Loader.cpp index 810cfb2..4a2684d 100644 --- a/libbpf_android/Loader.cpp +++ b/libbpf_android/Loader.cpp @@ -30,9 +30,9 @@ #include #include -// This is BpfLoader v0.31 +// This is BpfLoader v0.32 #define BPFLOADER_VERSION_MAJOR 0u -#define BPFLOADER_VERSION_MINOR 31u +#define BPFLOADER_VERSION_MINOR 32u #define BPFLOADER_VERSION ((BPFLOADER_VERSION_MAJOR << 16) | BPFLOADER_VERSION_MINOR) #include "BpfSyscallWrappers.h" @@ -58,6 +58,7 @@ #include #include #include +#include #define BPF_FS_PATH "/sys/fs/bpf/" @@ -75,9 +76,20 @@ using std::optional; using std::string; using std::vector; +static std::string getBuildTypeInternal() { + char value[PROPERTY_VALUE_MAX] = {}; + (void)property_get("ro.build.type", value, "unknown"); // ignore length + return value; +} + namespace android { namespace bpf { +const std::string& getBuildType() { + static std::string t = getBuildTypeInternal(); + return t; +} + constexpr const char* lookupSelinuxContext(const domain d, const char* const unspecified = "") { switch (d) { case domain::unspecified: return unspecified; @@ -770,6 +782,14 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector& continue; } + if ((md[i].ignore_on_eng && isEng()) || (md[i].ignore_on_user && isUser()) || + (md[i].ignore_on_userdebug && isUserdebug())) { + ALOGI("skipping map %s which is ignored on %s builds", mapNames[i].c_str(), + getBuildType().c_str()); + mapFds.push_back(unique_fd()); + continue; + } + enum bpf_map_type type = md[i].type; if (type == BPF_MAP_TYPE_DEVMAP_HASH && !isAtLeastKernelVersion(5, 4, 0)) { // On Linux Kernels older than 5.4 this map type doesn't exist, but it can kind @@ -1012,6 +1032,14 @@ static int loadCodeSections(const char* elfPath, vector& cs, const bpfMinVer, bpfMaxVer); if (BPFLOADER_VERSION < bpfMinVer) continue; if (BPFLOADER_VERSION >= bpfMaxVer) continue; + + if ((cs[i].prog_def->ignore_on_eng && isEng()) || + (cs[i].prog_def->ignore_on_user && isUser()) || + (cs[i].prog_def->ignore_on_userdebug && isUserdebug())) { + ALOGD("cs[%d].name:%s is ignored on %s builds", i, name.c_str(), + getBuildType().c_str()); + } + if (unrecognized(pin_subdir)) return -ENOTDIR; if (specified(selinux_context)) { diff --git a/libbpf_android/include/libbpf_android.h b/libbpf_android/include/libbpf_android.h index 9a55f3e..31bd539 100644 --- a/libbpf_android/include/libbpf_android.h +++ b/libbpf_android/include/libbpf_android.h @@ -94,5 +94,19 @@ int loadProg(const char* elfPath, bool* isCritical, const Location &location = { // Exposed for testing unsigned int readSectionUint(const char* name, std::ifstream& elfFile, unsigned int defVal); +// Returns the build type string (from ro.build.type). +const std::string& getBuildType(); + +// The following functions classify the 3 Android build types. +inline bool isEng() { + return getBuildType() == "eng"; +} +inline bool isUser() { + return getBuildType() == "user"; +} +inline bool isUserdebug() { + return getBuildType() == "userdebug"; +} + } // namespace bpf } // namespace android