diff --git a/src/core/bpf-restrict-fsaccess.c b/src/core/bpf-restrict-fsaccess.c index e2d5cbf955..b3891f741a 100644 --- a/src/core/bpf-restrict-fsaccess.c +++ b/src/core/bpf-restrict-fsaccess.c @@ -39,7 +39,6 @@ const char* const restrict_fsaccess_link_names[_RESTRICT_FILESYSTEM_ACCESS_LINK_ #if BPF_FRAMEWORK && HAVE_LSM_INTEGRITY_TYPE #include "bpf-util.h" -#include "bpf-link.h" #include "restrict-fsaccess-skel.h" static struct restrict_fsaccess_bpf *restrict_fsaccess_bpf_free(struct restrict_fsaccess_bpf *obj) { @@ -164,42 +163,6 @@ int bpf_restrict_fsaccess_prepare(struct restrict_fsaccess_bpf **ret) { return 0; } -bool bpf_restrict_fsaccess_supported(void) { - _cleanup_(restrict_fsaccess_bpf_freep) struct restrict_fsaccess_bpf *obj = NULL; - static int supported = -1; - int r; - - if (supported >= 0) - return supported; - if (dlopen_bpf(LOG_WARNING) < 0) - return (supported = false); - - r = lsm_supported("bpf"); - if (r == -ENOPKG) { - log_debug_errno(r, "bpf-restrict-fsaccess: securityfs not mounted, BPF LSM not available."); - return (supported = false); - } - if (r < 0) { - log_warning_errno(r, "bpf-restrict-fsaccess: Can't determine whether the BPF LSM module is used: %m"); - return (supported = false); - } - if (r == 0) { - log_info("bpf-restrict-fsaccess: BPF LSM hook not enabled in the kernel, not supported."); - return (supported = false); - } - - r = bpf_restrict_fsaccess_prepare(&obj); - if (r < 0) - return (supported = false); - - if (!bpf_can_link_lsm_program(obj->progs.restrict_fsaccess_bprm_check)) { - log_warning("bpf-restrict-fsaccess: Failed to link program; assuming BPF LSM is not available."); - return (supported = false); - } - - return (supported = true); -} - /* Partial deserialization (some FDs but not all) is fatal: continuing * would leave enforcement incomplete. */ static int restrict_fsaccess_have_deserialized_fds(Manager *m) { @@ -405,9 +368,18 @@ int bpf_restrict_fsaccess_setup(Manager *m) { } /* Fresh setup: verify BPF LSM is available */ - if (!bpf_restrict_fsaccess_supported()) + r = dlopen_bpf(LOG_WARNING); + if (r < 0) + return r; + + r = lsm_supported("bpf"); + if (r == -ENOPKG) + return log_warning_errno(r, "bpf-restrict-fsaccess: securityfs not mounted, BPF LSM not available."); + if (r < 0) + return log_warning_errno(r, "bpf-restrict-fsaccess: Can't determine whether the BPF LSM module is used: %m"); + if (r == 0) return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), - "bpf-restrict-fsaccess: BPF LSM is not available."); + "bpf-restrict-fsaccess: BPF LSM hook not enabled in the kernel, not supported."); /* Require dm-verity signature enforcement */ if (!dm_verity_require_signatures()) @@ -533,10 +505,6 @@ bool dm_verity_require_signatures(void) { return false; } -bool bpf_restrict_fsaccess_supported(void) { - return false; -} - int bpf_restrict_fsaccess_setup(Manager *m) { if (!MANAGER_IS_SYSTEM(m) || m->restrict_filesystem_access <= RESTRICT_FILESYSTEM_ACCESS_NO) return 0; diff --git a/src/core/bpf-restrict-fsaccess.h b/src/core/bpf-restrict-fsaccess.h index cefdcb9ee9..0a8cb45c8f 100644 --- a/src/core/bpf-restrict-fsaccess.h +++ b/src/core/bpf-restrict-fsaccess.h @@ -34,7 +34,6 @@ enum { extern const char* const restrict_fsaccess_link_names[_RESTRICT_FILESYSTEM_ACCESS_LINK_MAX]; bool dm_verity_require_signatures(void); -bool bpf_restrict_fsaccess_supported(void); int bpf_restrict_fsaccess_setup(Manager *m); int bpf_restrict_fsaccess_prepare(struct restrict_fsaccess_bpf **ret); int bpf_restrict_fsaccess_populate_guard(struct restrict_fsaccess_bpf *obj); diff --git a/src/test/test-bpf-restrict-fsaccess.c b/src/test/test-bpf-restrict-fsaccess.c index 6df10728eb..df07222478 100644 --- a/src/test/test-bpf-restrict-fsaccess.c +++ b/src/test/test-bpf-restrict-fsaccess.c @@ -24,10 +24,12 @@ #include #include +#include "bpf-link.h" #include "bpf-restrict-fsaccess.h" #include "devnum-util.h" #include "fd-util.h" #include "log.h" +#include "lsm-util.h" #include "string-util.h" #include "tests.h" @@ -161,16 +163,28 @@ static int do_attach(void) { } static int do_check(void) { - if (!bpf_restrict_fsaccess_supported()) { - log_error("BPF LSM is not available"); - return -EOPNOTSUPP; - } + int r; + + r = dlopen_bpf(LOG_WARNING); + if (r < 0) + return r; + + r = lsm_supported("bpf"); + if (r <= 0) + return log_error_errno(r < 0 ? r : EOPNOTSUPP, "BPF LSM is not available: %m"); log_info("BPF LSM: supported"); - if (!dm_verity_require_signatures()) { - log_error("dm-verity require_signatures is not enabled"); - return -ENOKEY; - } + _cleanup_(restrict_fsaccess_bpf_freep) struct restrict_fsaccess_bpf *obj = NULL; + r = bpf_restrict_fsaccess_prepare(&obj); + if (r < 0) + return r; + + if (!bpf_can_link_lsm_program(obj->progs.restrict_fsaccess_bprm_check)) + return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), + "bpf-restrict-fsaccess: Failed to link program."); + + if (!dm_verity_require_signatures()) + return log_error_errno(SYNTHETIC_ERRNO(ENOKEY), "dm-verity require_signatures is not enabled."); log_info("dm-verity require_signatures: enabled"); return 0;