4141#endif
4242#include "async.h"
4343#include "barrier.h"
44+ #include "bpf-lsm.h"
4445#include "cap-list.h"
4546#include "capability-util.h"
4647#include "cgroup-setup.h"
@@ -1685,6 +1686,29 @@ static int apply_restrict_namespaces(const Unit *u, const ExecContext *c) {
16851686 return seccomp_restrict_namespaces (c -> restrict_namespaces );
16861687}
16871688
1689+ #if HAVE_LIBBPF
1690+ static bool skip_lsm_bpf_unsupported (const Unit * u , const char * msg ) {
1691+ if (lsm_bpf_supported ())
1692+ return false;
1693+
1694+ log_unit_debug (u , "LSM BPF not supported, skipping %s" , msg );
1695+ return true;
1696+ }
1697+
1698+ static int apply_restrict_filesystems (Unit * u , const ExecContext * c ) {
1699+ assert (u );
1700+ assert (c );
1701+
1702+ if (!exec_context_restrict_filesystems_set (c ))
1703+ return 0 ;
1704+
1705+ if (skip_lsm_bpf_unsupported (u , "RestrictFileSystems=" ))
1706+ return 0 ;
1707+
1708+ return lsm_bpf_unit_restrict_filesystems (u , c -> restrict_filesystems , c -> restrict_filesystems_allow_list );
1709+ }
1710+ #endif
1711+
16881712static int apply_lock_personality (const Unit * u , const ExecContext * c ) {
16891713 unsigned long personality ;
16901714 int r ;
@@ -3813,7 +3837,7 @@ static int exec_child(
38133837 /* In case anything used libc syslog(), close this here, too */
38143838 closelog ();
38153839
3816- int keep_fds [n_fds + 2 ];
3840+ int keep_fds [n_fds + 3 ];
38173841 memcpy_safe (keep_fds , fds , n_fds * sizeof (int ));
38183842 n_keep_fds = n_fds ;
38193843
@@ -3823,6 +3847,24 @@ static int exec_child(
38233847 return log_unit_error_errno (unit , r , "Failed to shift fd and set FD_CLOEXEC: %m" );
38243848 }
38253849
3850+ #if HAVE_LIBBPF
3851+ if (MANAGER_IS_SYSTEM (unit -> manager ) && lsm_bpf_supported ()) {
3852+ int bpf_map_fd = -1 ;
3853+
3854+ bpf_map_fd = lsm_bpf_map_restrict_fs_fd (unit );
3855+ if (bpf_map_fd < 0 ) {
3856+ * exit_status = EXIT_FDS ;
3857+ return log_unit_error_errno (unit , r , "Failed to get restrict filesystems BPF map fd: %m" );
3858+ }
3859+
3860+ r = add_shifted_fd (keep_fds , ELEMENTSOF (keep_fds ), & n_keep_fds , bpf_map_fd , & bpf_map_fd );
3861+ if (r < 0 ) {
3862+ * exit_status = EXIT_FDS ;
3863+ return log_unit_error_errno (unit , r , "Failed to shift fd and set FD_CLOEXEC: %m" );
3864+ }
3865+ }
3866+ #endif
3867+
38263868 r = close_remaining_fds (params , runtime , dcreds , user_lookup_fd , socket_fd , keep_fds , n_keep_fds );
38273869 if (r < 0 ) {
38283870 * exit_status = EXIT_FDS ;
@@ -4682,6 +4724,15 @@ static int exec_child(
46824724 return log_unit_error_errno (unit , r , "Failed to apply system call filters: %m" );
46834725 }
46844726#endif
4727+
4728+ #if HAVE_LIBBPF
4729+ r = apply_restrict_filesystems (unit , context );
4730+ if (r < 0 ) {
4731+ * exit_status = EXIT_BPF ;
4732+ return log_unit_error_errno (unit , r , "Failed to restrict filesystems: %m" );
4733+ }
4734+ #endif
4735+
46854736 }
46864737
46874738 if (!strv_isempty (context -> unset_environment )) {
@@ -4967,6 +5018,8 @@ void exec_context_done(ExecContext *c) {
49675018 c -> apparmor_profile = mfree (c -> apparmor_profile );
49685019 c -> smack_process_label = mfree (c -> smack_process_label );
49695020
5021+ c -> restrict_filesystems = set_free (c -> restrict_filesystems );
5022+
49705023 c -> syscall_filter = hashmap_free (c -> syscall_filter );
49715024 c -> syscall_archs = set_free (c -> syscall_archs );
49725025 c -> address_families = set_free (c -> address_families );
@@ -5734,6 +5787,12 @@ void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix) {
57345787 prefix , strna (s ));
57355788 }
57365789
5790+ #if HAVE_LIBBPF
5791+ if (exec_context_restrict_filesystems_set (c ))
5792+ SET_FOREACH (e , c -> restrict_filesystems )
5793+ fprintf (f , "%sRestrictFileSystems: %s\n" , prefix , * e );
5794+ #endif
5795+
57375796 if (c -> network_namespace_path )
57385797 fprintf (f ,
57395798 "%sNetworkNamespacePath: %s\n" ,
0 commit comments