landlock: Fix LOG_SUBDOMAINS_OFF inheritance across fork()
Subsystems (4)
Mickaël Salaün
maintainerPATCH v2 1/2 landlock: Fix LOG_SUBDOMAINS_OFF inheritance across fork()
hook_cred_transfer() only copies the Landlock security blob when the source credential has a domain. This is inconsistent with landlock_restrict_self() which can set LOG_SUBDOMAINS_OFF on a credential without creating a domain (via the ruleset_fd=-1 path): the field is committed but not preserved across fork() because the child's prepare_creds() calls hook_cred_transfer() which skips the copy when domain is NULL.
This breaks the documented use case where a process mutes subdomain logs before forking sandboxed children: the children lose the muting and their domains produce unexpected audit records.
Fix this by unconditionally copying the Landlock credential blob.
---
Changes since v1:
https://lore.kernel.org/r/[email protected]
- Improve subject.
- Add Reviewed-by Günther.
--- security/landlock/cred.c | 6 +- tools/testing/selftests/landlock/audit_test.c | 88 +++++++++++++++++++ 2 files changed, 90 insertions(+), 4 deletions(-)
security/landlock/cred.c +2 -4
@@ -22,10 +22,8 @@ static void hook_cred_transfer(struct cred *const new,const struct landlock_cred_security *const old_llcred =landlock_cred (old); -if (old_llcred->domain) { -landlock_get_ruleset (old_llcred->domain); - *landlock_cred (new) = *old_llcred; - } +landlock_get_ruleset (old_llcred->domain); + *landlock_cred (new) = *old_llcred; } static int hook_cred_prepare(struct cred *const new,
tools/testing/selftests/landlock/audit_test.c +88 -0
94 lines not on this page · raw message
Signature · 1 line
2.53.0
Mickaël Salaün
maintainerPATCH v2 2/2 landlock: Allow TSYNC with LOG_SUBDOMAINS_OFF and fd=-1
LANDLOCK_RESTRICT_SELF_TSYNC does not allow LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF with ruleset_fd=-1, preventing a multithreaded process from atomically propagating subdomain log muting to all threads without creating a domain layer. Relax the fd=-1 condition to accept TSYNC alongside LOG_SUBDOMAINS_OFF, and update the documentation accordingly.
Add flag validation tests for all TSYNC combinations with ruleset_fd=-1, and audit tests verifying both transition directions: muting via TSYNC (logged to not logged) and override via TSYNC (not logged to logged).
---
Changes since v1:
https://lore.kernel.org/r/[email protected]
- Zero-initialize struct thread_data to avoid flaky uninitialized mute_subdomains field (pointed out by Günther Noack).
- Drop capabilities and set no_new_privs in tsync_without_ruleset fixture setup instead of relying on ambient CAP_SYS_ADMIN (pointed out by Günther Noack).
- Add Reviewed-by Günther.
--- include/uapi/linux/landlock.h | 4 +- security/landlock/syscalls.c | 14 +- tools/testing/selftests/landlock/audit_test.c | 233 ++++++++++++++++++ tools/testing/selftests/landlock/tsync_test.c | 77 ++++++ 4 files changed, 322 insertions(+), 6 deletions(-)
include/uapi/linux/landlock.h +3 -1
@@ -116,7 +116,9 @@ struct landlock_ruleset_attr {* ``LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF``, this flag only affects * future nested domains, not the one being created. It can also be used * with a @ruleset_fd value of -1 to mute subdomain logs without creating a -* domain. +* domain. When combined with %LANDLOCK_RESTRICT_SELF_TSYNC and a +* @ruleset_fd value of -1, this configuration is propagated to all threads +* of the current process. * * The following flag supports policy enforcement in multithreaded processes: *
security/landlock/syscalls.c +9 -5
@@ -512,10 +512,13 @@ SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32,/* * It is allowed to set LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF with -* -1 as ruleset_fd, but no other flag must be set. +* -1 as ruleset_fd, optionally combined with +* LANDLOCK_RESTRICT_SELF_TSYNC to propagate this configuration to all +* threads. No other flag must be set. */ if (!(ruleset_fd ==-1 && - flags ==LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF )) { + (flags & ~LANDLOCK_RESTRICT_SELF_TSYNC ) == +LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF )) {/* Gets and checks the ruleset. */ ruleset =get_ruleset_from_fd (ruleset_fd,FMODE_CAN_READ );if (IS_ERR (ruleset))
@@ -537,9 +540,10 @@ SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32,/* * The only case when a ruleset may not be set is if -* LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF is set and ruleset_fd is -1. -* We could optimize this case by not calling commit_creds() if this flag -* was already set, but it is not worth the complexity. +* LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF is set (optionally with +* LANDLOCK_RESTRICT_SELF_TSYNC) and ruleset_fd is -1. We could +* optimize this case by not calling commit_creds() if this flag was +* already set, but it is not worth the complexity. */ if (ruleset) {/*
tools/testing/selftests/landlock/audit_test.c +233 -0
245 lines not on this page · raw message
tools/testing/selftests/landlock/tsync_test.c +77 -0
81 lines not on this page · raw message
Signature · 1 line
2.53.0
Quoted 8 lines · Mickaël Salaün
hook_cred_transfer()only copies the Landlock security blob when the source credential has a domain. This is inconsistent withlandlock_restrict_self()which can setLOG_SUBDOMAINS_OFFon a credential without creating a domain (via the ruleset_fd=-1 path): the field is committed but not preserved acrossfork()because the child'sprepare_creds()callshook_cred_transfer()which skips the copy when domain is NULL.
Quoted 5 lines
This breaks the documented use case where a process mutes subdomain logs before forking sandboxed children: the children lose the muting and their domains produce unexpected audit records.
Fix this by unconditionally copying the Landlock credential blob.
As before, LGTM for both patches. Thanks for the fixes!
–Günther