Merge "Add option to skip map/prog based on build type"

This commit is contained in:
Maciej Żenczykowski 2022-12-20 08:38:45 +00:00 committed by Gerrit Code Review
commit 4fba4f7a22
4 changed files with 52 additions and 2 deletions

View file

@ -257,6 +257,13 @@ int main(int argc, char** argv) {
(void)argc; (void)argc;
android::base::InitLogging(argv, &android::base::KernelLogger); 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) // 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, // (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) // on 5.13+ it depends on CONFIG_BPF_UNPRIV_DEFAULT_OFF)

View file

@ -39,6 +39,7 @@ cc_library {
shared_libs: [ shared_libs: [
"libbase", "libbase",
"libcutils",
"libutils", "libutils",
"liblog", "liblog",
"libbpf_bcc", "libbpf_bcc",

View file

@ -30,9 +30,9 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
// This is BpfLoader v0.31 // This is BpfLoader v0.32
#define BPFLOADER_VERSION_MAJOR 0u #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) #define BPFLOADER_VERSION ((BPFLOADER_VERSION_MAJOR << 16) | BPFLOADER_VERSION_MINOR)
#include "BpfSyscallWrappers.h" #include "BpfSyscallWrappers.h"
@ -58,6 +58,7 @@
#include <android-base/file.h> #include <android-base/file.h>
#include <android-base/strings.h> #include <android-base/strings.h>
#include <android-base/unique_fd.h> #include <android-base/unique_fd.h>
#include <cutils/properties.h>
#define BPF_FS_PATH "/sys/fs/bpf/" #define BPF_FS_PATH "/sys/fs/bpf/"
@ -75,9 +76,20 @@ using std::optional;
using std::string; using std::string;
using std::vector; 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 android {
namespace bpf { namespace bpf {
const std::string& getBuildType() {
static std::string t = getBuildTypeInternal();
return t;
}
constexpr const char* lookupSelinuxContext(const domain d, const char* const unspecified = "") { constexpr const char* lookupSelinuxContext(const domain d, const char* const unspecified = "") {
switch (d) { switch (d) {
case domain::unspecified: return unspecified; case domain::unspecified: return unspecified;
@ -770,6 +782,14 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>&
continue; 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; enum bpf_map_type type = md[i].type;
if (type == BPF_MAP_TYPE_DEVMAP_HASH && !isAtLeastKernelVersion(5, 4, 0)) { 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 // 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<codeSection>& cs, const
bpfMinVer, bpfMaxVer); bpfMinVer, bpfMaxVer);
if (BPFLOADER_VERSION < bpfMinVer) continue; if (BPFLOADER_VERSION < bpfMinVer) continue;
if (BPFLOADER_VERSION >= bpfMaxVer) 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 (unrecognized(pin_subdir)) return -ENOTDIR;
if (specified(selinux_context)) { if (specified(selinux_context)) {

View file

@ -94,5 +94,19 @@ int loadProg(const char* elfPath, bool* isCritical, const Location &location = {
// Exposed for testing // Exposed for testing
unsigned int readSectionUint(const char* name, std::ifstream& elfFile, unsigned int defVal); 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 bpf
} // namespace android } // namespace android