ba8205
From 084d0849fc864b0377551b211def7a89b642c5e9 Mon Sep 17 00:00:00 2001
ba8205
From: Julia Kartseva <hex@fb.com>
ba8205
Date: Thu, 4 Nov 2021 18:52:02 -0700
ba8205
Subject: [PATCH 1/2] core: fix bpf-foreign cg controller realization
ba8205
ba8205
Requiring /sys/fs/bpf path to be a mount point at the moment of cgroup
ba8205
controllers realization does more harm than good, because:
ba8205
* Realization happens early on boot, the mount point may not be ready at
ba8205
the time. That happens if mounts are made by a .mount unit (the issue we
ba8205
encountered).
ba8205
* BPF filesystem may be mounted on another point.
ba8205
ba8205
Remove the check. Instead verify that path provided by BPFProgram= is
ba8205
within BPF fs when unit properties are parsed.
ba8205
ba8205
Split in two commits for simple backport.
ba8205
---
ba8205
 src/core/bpf-foreign.c | 10 ----------
ba8205
 src/core/bpf-foreign.h |  5 ++++-
ba8205
 2 files changed, 4 insertions(+), 11 deletions(-)
ba8205
ba8205
diff --git a/src/core/bpf-foreign.c b/src/core/bpf-foreign.c
ba8205
index 6b93b9785fb5..686c14ce1f18 100644
ba8205
--- a/src/core/bpf-foreign.c
ba8205
+++ b/src/core/bpf-foreign.c
ba8205
@@ -111,16 +111,6 @@ static int bpf_foreign_prepare(
ba8205
         return 0;
ba8205
 }
ba8205
 
ba8205
-int bpf_foreign_supported(void) {
ba8205
-        int r;
ba8205
-
ba8205
-        r = cg_all_unified();
ba8205
-        if (r <= 0)
ba8205
-                return r;
ba8205
-
ba8205
-        return path_is_mount_point("/sys/fs/bpf", NULL, 0);
ba8205
-}
ba8205
-
ba8205
 int bpf_foreign_install(Unit *u) {
ba8205
         _cleanup_free_ char *cgroup_path = NULL;
ba8205
         CGroupBPFForeignProgram *p;
ba8205
diff --git a/src/core/bpf-foreign.h b/src/core/bpf-foreign.h
ba8205
index 9559cd79812b..e387b1b1d389 100644
ba8205
--- a/src/core/bpf-foreign.h
ba8205
+++ b/src/core/bpf-foreign.h
ba8205
@@ -4,7 +4,10 @@
ba8205
 
ba8205
 #include "unit.h"
ba8205
 
ba8205
-int bpf_foreign_supported(void);
ba8205
+static inline int bpf_foreign_supported(void) {
ba8205
+        return cg_all_unified();
ba8205
+}
ba8205
+
ba8205
 /*
ba8205
  * Attach cgroup-bpf programs foreign to systemd, i.e. loaded to the kernel by an entity
ba8205
  * external to systemd.
ba8205
ba8205
From dedca960afdee5797d19929c43853513711e3e3d Mon Sep 17 00:00:00 2001
ba8205
From: Julia Kartseva <hex@fb.com>
ba8205
Date: Thu, 4 Nov 2021 18:55:55 -0700
ba8205
Subject: [PATCH 2/2] core: check fs type of BPFProgram= property path
ba8205
ba8205
Tests:
ba8205
ba8205
```
ba8205
% stat --file-system --format="%T" /root/bpf/trivial/
ba8205
bpf_fs
ba8205
ba8205
% systemd-nspawn -D/ --volatile=yes \
ba8205
--property=BPFProgram=egress:/root/bpf/trivial/cgroup_skb_egress \
ba8205
--quiet -- ping -c 5 -W 1 ::1
ba8205
PING ::1(::1) 56 data bytes
ba8205
ba8205
--- ::1 ping statistics ---
ba8205
5 packets transmitted, 0 received, 100% packet loss, time 4110ms
ba8205
```
ba8205
ba8205
```
ba8205
% stat --file-system --format='%T' /root/meh
ba8205
btrfs
ba8205
ba8205
% systemd-nspawn -D/ --volatile=yes --property=BPFProgram=egress:/root/meh
ba8205
--quiet -- ping -c 5 -W 1 ::1
ba8205
```
ba8205
sudo ./build/systemd-nspawn \
ba8205
-D/ --volatile=yes --property=BPFProgram=egress:/home/hex --quiet -- \
ba8205
ping -c 1 -W 1 ::1
ba8205
PING ::1(::1) 56 data bytes
ba8205
64 bytes from ::1: icmp_seq=1 ttl=64 time=0.017 ms
ba8205
ba8205
--- ::1 ping statistics ---
ba8205
1 packets transmitted, 1 received, 0% packet loss, time 0ms
ba8205
---
ba8205
 src/core/bpf-foreign.c | 10 ++++++++++
ba8205
 1 file changed, 10 insertions(+)
ba8205
ba8205
diff --git a/src/core/bpf-foreign.c b/src/core/bpf-foreign.c
ba8205
index 686c14ce1f18..8538792b60db 100644
ba8205
--- a/src/core/bpf-foreign.c
ba8205
+++ b/src/core/bpf-foreign.c
ba8205
@@ -4,8 +4,10 @@
ba8205
 #include "bpf-program.h"
ba8205
 #include "cgroup.h"
ba8205
 #include "memory-util.h"
ba8205
+#include "missing_magic.h"
ba8205
 #include "mountpoint-util.h"
ba8205
 #include "set.h"
ba8205
+#include "stat-util.h"
ba8205
 
ba8205
 typedef struct BPFForeignKey BPFForeignKey;
ba8205
 struct BPFForeignKey {
ba8205
@@ -84,6 +86,14 @@ static int bpf_foreign_prepare(
ba8205
         assert(u);
ba8205
         assert(bpffs_path);
ba8205
 
ba8205
+        r = path_is_fs_type(bpffs_path, BPF_FS_MAGIC);
ba8205
+        if (r < 0)
ba8205
+                return log_unit_error_errno(u, r,
ba8205
+                                "Failed to determine filesystem type of %s: %m", bpffs_path);
ba8205
+        if (r == 0)
ba8205
+                return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
ba8205
+                                "Path in BPF filesystem is expected.");
ba8205
+
ba8205
         r = bpf_program_new_from_bpffs_path(bpffs_path, &prog;;
ba8205
         if (r < 0)
ba8205
                 return log_unit_error_errno(u, r, "Failed to create foreign BPFProgram: %m");