Skip to content

Commit 30e2681

Browse files
committed
Merge tag 'landlock-6.15-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/mic/linux
Pull landlock fixes from Mickaël Salaün: "Fix some Landlock audit issues, add related tests, and updates documentation" * tag 'landlock-6.15-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/mic/linux: landlock: Update log documentation landlock: Fix documentation for landlock_restrict_self(2) landlock: Fix documentation for landlock_create_ruleset(2) selftests/landlock: Add PID tests for audit records selftests/landlock: Factor out audit fixture in audit_test landlock: Log the TGID of the domain creator landlock: Remove incorrect warning
2 parents e72e9e6 + 47ce2af commit 30e2681

File tree

7 files changed

+226
-72
lines changed

7 files changed

+226
-72
lines changed

include/uapi/linux/landlock.h

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,70 @@ struct landlock_ruleset_attr {
5353
__u64 scoped;
5454
};
5555

56-
/*
57-
* sys_landlock_create_ruleset() flags:
56+
/**
57+
* DOC: landlock_create_ruleset_flags
58+
*
59+
* **Flags**
5860
*
59-
* - %LANDLOCK_CREATE_RULESET_VERSION: Get the highest supported Landlock ABI
60-
* version.
61-
* - %LANDLOCK_CREATE_RULESET_ERRATA: Get a bitmask of fixed issues.
61+
* %LANDLOCK_CREATE_RULESET_VERSION
62+
* Get the highest supported Landlock ABI version (starting at 1).
63+
*
64+
* %LANDLOCK_CREATE_RULESET_ERRATA
65+
* Get a bitmask of fixed issues for the current Landlock ABI version.
6266
*/
6367
/* clang-format off */
6468
#define LANDLOCK_CREATE_RULESET_VERSION (1U << 0)
6569
#define LANDLOCK_CREATE_RULESET_ERRATA (1U << 1)
6670
/* clang-format on */
6771

68-
/*
69-
* sys_landlock_restrict_self() flags:
70-
*
71-
* - %LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF: Do not create any log related to the
72-
* enforced restrictions. This should only be set by tools launching unknown
73-
* or untrusted programs (e.g. a sandbox tool, container runtime, system
74-
* service manager). Because programs sandboxing themselves should fix any
75-
* denied access, they should not set this flag to be aware of potential
76-
* issues reported by system's logs (i.e. audit).
77-
* - %LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON: Explicitly ask to continue
78-
* logging denied access requests even after an :manpage:`execve(2)` call.
79-
* This flag should only be set if all the programs than can legitimately be
80-
* executed will not try to request a denied access (which could spam audit
81-
* logs).
82-
* - %LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF: Do not create any log related
83-
* to the enforced restrictions coming from future nested domains created by
84-
* the caller or its descendants. This should only be set according to a
85-
* runtime configuration (i.e. not hardcoded) by programs launching other
86-
* unknown or untrusted programs that may create their own Landlock domains
87-
* and spam logs. The main use case is for container runtimes to enable users
88-
* to mute buggy sandboxed programs for a specific container image. Other use
89-
* cases include sandboxer tools and init systems. Unlike
90-
* %LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF,
91-
* %LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF does not impact the requested
92-
* restriction (if any) but only the future nested domains.
72+
/**
73+
* DOC: landlock_restrict_self_flags
74+
*
75+
* **Flags**
76+
*
77+
* By default, denied accesses originating from programs that sandbox themselves
78+
* are logged via the audit subsystem. Such events typically indicate unexpected
79+
* behavior, such as bugs or exploitation attempts. However, to avoid excessive
80+
* logging, access requests denied by a domain not created by the originating
81+
* program are not logged by default. The rationale is that programs should know
82+
* their own behavior, but not necessarily the behavior of other programs. This
83+
* default configuration is suitable for most programs that sandbox themselves.
84+
* For specific use cases, the following flags allow programs to modify this
85+
* default logging behavior.
86+
*
87+
* The %LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF and
88+
* %LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON flags apply to the newly created
89+
* Landlock domain.
90+
*
91+
* %LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF
92+
* Disables logging of denied accesses originating from the thread creating
93+
* the Landlock domain, as well as its children, as long as they continue
94+
* running the same executable code (i.e., without an intervening
95+
* :manpage:`execve(2)` call). This is intended for programs that execute
96+
* unknown code without invoking :manpage:`execve(2)`, such as script
97+
* interpreters. Programs that only sandbox themselves should not set this
98+
* flag, so users can be notified of unauthorized access attempts via system
99+
* logs.
100+
*
101+
* %LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON
102+
* Enables logging of denied accesses after an :manpage:`execve(2)` call,
103+
* providing visibility into unauthorized access attempts by newly executed
104+
* programs within the created Landlock domain. This flag is recommended
105+
* only when all potential executables in the domain are expected to comply
106+
* with the access restrictions, as excessive audit log entries could make
107+
* it more difficult to identify critical events.
108+
*
109+
* %LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF
110+
* Disables logging of denied accesses originating from nested Landlock
111+
* domains created by the caller or its descendants. This flag should be set
112+
* according to runtime configuration, not hardcoded, to avoid suppressing
113+
* important security events. It is useful for container runtimes or
114+
* sandboxing tools that may launch programs which themselves create
115+
* Landlock domains and could otherwise generate excessive logs. Unlike
116+
* ``LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF``, this flag only affects
117+
* future nested domains, not the one being created. It can also be used
118+
* with a @ruleset_fd value of -1 to mute subdomain logs without creating a
119+
* domain.
93120
*/
94121
/* clang-format off */
95122
#define LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF (1U << 0)

security/landlock/domain.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/path.h>
1717
#include <linux/pid.h>
1818
#include <linux/sched.h>
19+
#include <linux/signal.h>
1920
#include <linux/uidgid.h>
2021

2122
#include "access.h"
@@ -99,8 +100,7 @@ static struct landlock_details *get_current_details(void)
99100
return ERR_PTR(-ENOMEM);
100101

101102
memcpy(details->exe_path, path_str, path_size);
102-
WARN_ON_ONCE(current_cred() != current_real_cred());
103-
details->pid = get_pid(task_pid(current));
103+
details->pid = get_pid(task_tgid(current));
104104
details->uid = from_kuid(&init_user_ns, current_uid());
105105
get_task_comm(details->comm, current);
106106
return details;

security/landlock/domain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ int landlock_init_hierarchy_log(struct landlock_hierarchy *const hierarchy);
130130
static inline void
131131
landlock_free_hierarchy_details(struct landlock_hierarchy *const hierarchy)
132132
{
133-
if (WARN_ON_ONCE(!hierarchy || !hierarchy->details))
133+
if (!hierarchy || !hierarchy->details)
134134
return;
135135

136136
put_pid(hierarchy->details->pid);

security/landlock/syscalls.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,16 @@ const int landlock_abi_version = 7;
169169
* the new ruleset.
170170
* @size: Size of the pointed &struct landlock_ruleset_attr (needed for
171171
* backward and forward compatibility).
172-
* @flags: Supported value:
172+
* @flags: Supported values:
173+
*
173174
* - %LANDLOCK_CREATE_RULESET_VERSION
174175
* - %LANDLOCK_CREATE_RULESET_ERRATA
175176
*
176177
* This system call enables to create a new Landlock ruleset, and returns the
177178
* related file descriptor on success.
178179
*
179-
* If @flags is %LANDLOCK_CREATE_RULESET_VERSION and @attr is NULL and @size is
180-
* 0, then the returned value is the highest supported Landlock ABI version
181-
* (starting at 1).
182-
*
183-
* If @flags is %LANDLOCK_CREATE_RULESET_ERRATA and @attr is NULL and @size is
184-
* 0, then the returned value is a bitmask of fixed issues for the current
185-
* Landlock ABI version.
180+
* If %LANDLOCK_CREATE_RULESET_VERSION or %LANDLOCK_CREATE_RULESET_ERRATA is
181+
* set, then @attr must be NULL and @size must be 0.
186182
*
187183
* Possible returned errors are:
188184
*
@@ -191,6 +187,9 @@ const int landlock_abi_version = 7;
191187
* - %E2BIG: @attr or @size inconsistencies;
192188
* - %EFAULT: @attr or @size inconsistencies;
193189
* - %ENOMSG: empty &landlock_ruleset_attr.handled_access_fs.
190+
*
191+
* .. kernel-doc:: include/uapi/linux/landlock.h
192+
* :identifiers: landlock_create_ruleset_flags
194193
*/
195194
SYSCALL_DEFINE3(landlock_create_ruleset,
196195
const struct landlock_ruleset_attr __user *const, attr,
@@ -452,18 +451,15 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
452451
* @ruleset_fd: File descriptor tied to the ruleset to merge with the target.
453452
* @flags: Supported values:
454453
*
455-
* - %LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF
456-
* - %LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON
457-
* - %LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF
454+
* - %LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF
455+
* - %LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON
456+
* - %LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF
458457
*
459458
* This system call enables to enforce a Landlock ruleset on the current
460459
* thread. Enforcing a ruleset requires that the task has %CAP_SYS_ADMIN in its
461460
* namespace or is running with no_new_privs. This avoids scenarios where
462461
* unprivileged tasks can affect the behavior of privileged children.
463462
*
464-
* It is allowed to only pass the %LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF
465-
* flag with a @ruleset_fd value of -1.
466-
*
467463
* Possible returned errors are:
468464
*
469465
* - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
@@ -475,6 +471,9 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
475471
* %CAP_SYS_ADMIN in its namespace.
476472
* - %E2BIG: The maximum number of stacked rulesets is reached for the current
477473
* thread.
474+
*
475+
* .. kernel-doc:: include/uapi/linux/landlock.h
476+
* :identifiers: landlock_restrict_self_flags
478477
*/
479478
SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32,
480479
flags)

tools/testing/selftests/landlock/audit.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,22 @@ static int audit_match_record(int audit_fd, const __u16 type,
300300
return err;
301301
}
302302

303-
static int __maybe_unused matches_log_domain_allocated(int audit_fd,
303+
static int __maybe_unused matches_log_domain_allocated(int audit_fd, pid_t pid,
304304
__u64 *domain_id)
305305
{
306-
return audit_match_record(
307-
audit_fd, AUDIT_LANDLOCK_DOMAIN,
308-
REGEX_LANDLOCK_PREFIX
309-
" status=allocated mode=enforcing pid=[0-9]\\+ uid=[0-9]\\+"
310-
" exe=\"[^\"]\\+\" comm=\".*_test\"$",
311-
domain_id);
306+
static const char log_template[] = REGEX_LANDLOCK_PREFIX
307+
" status=allocated mode=enforcing pid=%d uid=[0-9]\\+"
308+
" exe=\"[^\"]\\+\" comm=\".*_test\"$";
309+
char log_match[sizeof(log_template) + 10];
310+
int log_match_len;
311+
312+
log_match_len =
313+
snprintf(log_match, sizeof(log_match), log_template, pid);
314+
if (log_match_len > sizeof(log_match))
315+
return -E2BIG;
316+
317+
return audit_match_record(audit_fd, AUDIT_LANDLOCK_DOMAIN, log_match,
318+
domain_id);
312319
}
313320

314321
static int __maybe_unused matches_log_domain_deallocated(

0 commit comments

Comments
 (0)