BpfLoader - create /sys/fs/bpf/loader

we also take this opportunity to enforce that all the
directory creations actually succeed (there really
is no reason why that could fail though)

Test: TreeHugger, manually inspected /sys/fs/bpf contents on cuttlefish
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Icd7c1e6eba5736a58cf81476ddafc70df2807dd4
diff --git a/bpfloader/BpfLoader.cpp b/bpfloader/BpfLoader.cpp
index fd261b5..c29c97e 100644
--- a/bpfloader/BpfLoader.cpp
+++ b/bpfloader/BpfLoader.cpp
@@ -218,7 +218,7 @@
     return retVal;
 }
 
-void createSysFsBpfSubDir(const char* const prefix) {
+int createSysFsBpfSubDir(const char* const prefix) {
     if (*prefix) {
         mode_t prevUmask = umask(0);
 
@@ -228,11 +228,14 @@
         errno = 0;
         int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
         if (ret && errno != EEXIST) {
-            ALOGE("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(errno));
+            const int err = errno;
+            ALOGE("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(err));
+            return -err;
         }
 
         umask(prevUmask);
     }
+    return 0;
 }
 
 int main(int argc, char** argv) {
@@ -244,8 +247,8 @@
     // (due to genfscon rules) have fs_bpf_tethering selinux context, which is restricted
     // to the network_stack process only (which is where out of process tethering runs)
     if (isInProcessTethering() && !exists("/sys/fs/bpf/tethering")) {
-        createSysFsBpfSubDir(/* /sys/fs/bpf/ */ "net_shared");
-        createSysFsBpfSubDir(/* /sys/fs/bpf/ */ "net_shared/tethering");
+        if (createSysFsBpfSubDir(/* /sys/fs/bpf/ */ "net_shared")) return 1;
+        if (createSysFsBpfSubDir(/* /sys/fs/bpf/ */ "net_shared/tethering")) return 1;
 
         /* /sys/fs/bpf/tethering -> net_shared/tethering */
         if (symlink("net_shared/tethering", "/sys/fs/bpf/tethering")) {
@@ -259,9 +262,16 @@
     //  which could otherwise fail with ENOENT during object pinning or renaming,
     //  due to ordering issues)
     for (const auto& location : locations) {
-        createSysFsBpfSubDir(location.prefix);
+        if (createSysFsBpfSubDir(location.prefix)) return 1;
     }
 
+    // Note: there's no actual src dir for fs_bpf_loader .o's,
+    // so it is not listed in 'locations[].prefix'.
+    // This is because this is primarily meant for triggering genfscon rules,
+    // and as such this will likely always be the case.
+    // Thus we need to manually create the /sys/fs/bpf/loader subdirectory.
+    if (createSysFsBpfSubDir("loader")) return 1;
+
     // Load all ELF objects, create programs and maps, and pin them
     for (const auto& location : locations) {
         if (loadAllElfObjects(location) != 0) {
diff --git a/libbpf_android/Loader.cpp b/libbpf_android/Loader.cpp
index 0d7483e..5b621ae 100644
--- a/libbpf_android/Loader.cpp
+++ b/libbpf_android/Loader.cpp
@@ -30,9 +30,9 @@
 #include <sys/wait.h>
 #include <unistd.h>
 
-// This is BpfLoader v0.30
+// This is BpfLoader v0.31
 #define BPFLOADER_VERSION_MAJOR 0u
-#define BPFLOADER_VERSION_MINOR 30u
+#define BPFLOADER_VERSION_MINOR 31u
 #define BPFLOADER_VERSION ((BPFLOADER_VERSION_MAJOR << 16) | BPFLOADER_VERSION_MINOR)
 
 #include "bpf/BpfUtils.h"