eBPF-based security platform
io: tell whether the private bpffs is mounted from the mount itself
Daniel Borkmann2c29d47f6e42
diff --git a/Documentation/reference/files.rst b/Documentation/reference/files.rstindex 4e78cd71..4b2d382f 100644--- a/Documentation/reference/files.rst+++ b/Documentation/reference/files.rst@@ -470,8 +470,6 @@ Kernel and system interfaces- Creating and checking the dm-verity mappings of the images.* - ``/sys/kernel/security/lsm``, ``/proc/filesystems``- Pre-flight: BPF LSM active, bpffs supported.- * - ``/proc/self/mounts``- - Whether the private bpffs is mounted.* - ``/proc/sys/kernel/random/boot_id``- The ``boot_id`` of eventlog records.* - ``/sys/kernel/security/tpm0/binary_bios_measurements``,diff --git a/Documentation/reference/formats/status-json.rst b/Documentation/reference/formats/status-json.rstindex e69c4bab..f101f74d 100644--- a/Documentation/reference/formats/status-json.rst+++ b/Documentation/reference/formats/status-json.rst@@ -493,7 +493,8 @@ assetsdirectory is absent.* - ``bpffs_mounted``- boolean- - ``/run/shirudo/bpffs`` is a mount point.+ - ``/run/shirudo/bpffs`` is the root of a bpffs mount, not+ hidden under another mount.tamper_attempts---------------diff --git a/builtin-init.c b/builtin-init.cindex e04f6b34..180910a9 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -245,29 +245,6 @@ static int init_keyring(void)return 0;}-static int is_bpffs_mounted(const char *path)-{- FILE *f = fopen("/proc/self/mounts", "re");- char line[4096];- int found = 0;-- if (!f)- return -errno;- while (fgets(line, sizeof(line), f)) {- char src[256], target[256], type[256];-- if (sscanf(line, "%255s %255s %255s",- src, target, type) != 3)- continue;- if (strcmp(target, path) == 0 && strcmp(type, "bpf") == 0) {- found = 1;- break;- }- }- fclose(f);- return found;-}-static int init_bpffs(void){struct stat parent, run;@@ -283,7 +260,7 @@ static int init_bpffs(void)pr_err("mkdir %s: %s", RUN_DIR, strerror(-ret));return ret;}- ret = is_bpffs_mounted(BPFFS_DIR);+ ret = bpffs_mounted_at(BPFFS_DIR);if (ret < 0)return ret;if (ret)@@ -314,9 +291,9 @@ static int init_bpffs(void)return ret;}- ret = is_bpffs_mounted(BPFFS_DIR);+ ret = bpffs_mounted_at(BPFFS_DIR);if (ret < 0) {- pr_err("read /proc/self/mounts: %s", strerror(-ret));+ pr_err("check the bpffs at %s: %s", BPFFS_DIR, strerror(-ret));return ret;}if (ret) {diff --git a/builtin-status.c b/builtin-status.cindex f4c48ab3..4fea2e43 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -144,28 +144,6 @@ static int count_dir(const char *dir, bool upatch_only)return count;}-static bool bpffs_is_mounted(void)-{- char line[4096];- bool found = false;- FILE *f;-- f = fopen("/proc/self/mounts", "re");- if (!f)- return false;- while (fgets(line, sizeof(line), f)) {- char *mp = strchr(line, ' '); /* field 2 = mountpoint */-- if (mp && strncmp(mp + 1, BPFFS_DIR " ",- strlen(BPFFS_DIR) + 1) == 0) {- found = true;- break;- }- }- fclose(f);- return found;-}-static void gather_assets(struct asset_view *a){struct stat st;@@ -179,7 +157,7 @@ static void gather_assets(struct asset_view *a)}a->n_upatches = count_dir(UPATCHES_DIR, true);a->n_incoming = count_dir(INCOMING_DIR, false);- a->bpffs_mounted = bpffs_is_mounted();+ a->bpffs_mounted = bpffs_mounted_at(BPFFS_DIR) > 0;}/*diff --git a/testing/unit/io.c b/testing/unit/io.cindex cc5fe194..6fd05108 100644--- a/testing/unit/io.c+++ b/testing/unit/io.c@@ -102,12 +102,23 @@ static void kernel_dev_is_major_over_20_minor_bits(void)EXPECT_EQ(kernel_dev(makedev(0xfff, 0xfffff)), 0xffffffffU);}+static void bpffs_mounted_at_wants_the_root_of_a_bpffs(void)+{+ const char *dir = getenv("TMPDIR");++ EXPECT_EQ(bpffs_mounted_at(dir && dir[0] ? dir : "/tmp"), 0);+ EXPECT_EQ(bpffs_mounted_at("/"), 0);+ EXPECT_EQ(bpffs_mounted_at("/nonexistent/shirudo"), 0);+ EXPECT_EQ(bpffs_mounted_at("/dev/null"), -ENOTDIR);+}+static const struct unit_case cases[] = {UNIT_CASE(lines_len_ends_at_the_last_newline),UNIT_CASE(append_line_adds_one_record),UNIT_CASE(append_line_drops_what_did_not_finish),UNIT_CASE(append_line_takes_one_line_alone),UNIT_CASE(kernel_dev_is_major_over_20_minor_bits),+ UNIT_CASE(bpffs_mounted_at_wants_the_root_of_a_bpffs),{}};diff --git a/util/io.c b/util/io.cindex a95b8d5b..bd12e6a3 100644--- a/util/io.c+++ b/util/io.c@@ -9,10 +9,12 @@#include <stdlib.h>#include <string.h>#include <sys/stat.h>+#include <sys/statfs.h>#include <sys/syscall.h>#include <sys/sysmacros.h>#include <unistd.h>+#include <linux/magic.h>#include <linux/openat2.h>#include <shirudo/misc.h>@@ -274,3 +276,23 @@ __u32 kernel_dev(dev_t dev){return (__u32)major(dev) << 20 | (minor(dev) & 0xfffffU);}++int bpffs_mounted_at(const char *path)+{+ struct statx stx = {};+ struct statfs fs = {};+ int fd, ret;++ fd = open(path, O_PATH | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);+ if (fd < 0)+ return errno == ENOENT ? 0 : -errno;+ if (fstatfs(fd, &fs) ||+ statx(fd, "", AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW, 0, &stx))+ ret = -errno;+ else+ ret = fs.f_type == BPF_FS_MAGIC &&+ stx.stx_attributes_mask & STATX_ATTR_MOUNT_ROOT &&+ stx.stx_attributes & STATX_ATTR_MOUNT_ROOT;+ close(fd);+ return ret;+}diff --git a/util/io.h b/util/io.hindex 777113fc..efe660a3 100644--- a/util/io.h+++ b/util/io.h@@ -50,4 +50,11 @@ int check_trusted(int fd, const char *path, bool secret);/* A st_dev or st_rdev as the kernel encodes the dev_t in s_dev. */__u32 kernel_dev(dev_t dev);+/*+ * 1 when path is the root of a bpf file system mount, which a bpffs+ * mounted on a directory above it or under another mount on top of it is+ * not; 0 when it is not, or there is no such path; else -errno.+ */+int bpffs_mounted_at(const char *path);+#endif /* SHIRUDO_IO_H */