Skip to content

Commit 33e65b0

Browse files
committed
landlock: Add AUDIT_LANDLOCK_ACCESS and log ptrace denials
Add a new AUDIT_LANDLOCK_ACCESS record type dedicated to an access request denied by a Landlock domain. AUDIT_LANDLOCK_ACCESS indicates that something unexpected happened. For now, only denied access are logged, which means that any AUDIT_LANDLOCK_ACCESS record is always followed by a SYSCALL record with "success=no". However, log parsers should check this syscall property because this is the only sign that a request was denied. Indeed, we could have "success=yes" if Landlock would support a "permissive" mode. We could also add a new field to AUDIT_LANDLOCK_DOMAIN for this mode (see following commit). By default, the only logged access requests are those coming from the same executed program that enforced the Landlock restriction on itself. In other words, no audit record are created for a task after it called execve(2). This is required to avoid log spam because programs may only be aware of their own restrictions, but not the inherited ones. Following commits will allow to conditionally generate AUDIT_LANDLOCK_ACCESS records according to dedicated landlock_restrict_self(2)'s flags. The AUDIT_LANDLOCK_ACCESS message contains: - the "domain" ID restricting the action on an object, - the "blockers" that are missing to allow the requested access, - a set of fields identifying the related object (e.g. task identified with "opid" and "ocomm"). The blockers are implicit restrictions (e.g. ptrace), or explicit access rights (e.g. filesystem), or explicit scopes (e.g. signal). This field contains a list of at least one element, each separated with a comma. The initial blocker is "ptrace", which describe all implicit Landlock restrictions related to ptrace (e.g. deny tracing of tasks outside a sandbox). Add audit support to ptrace_access_check and ptrace_traceme hooks. For the ptrace_access_check case, we log the current/parent domain and the child task. For the ptrace_traceme case, we log the parent domain and the current/child task. Indeed, the requester and the target are the current task, but the action would be performed by the parent task. Audit event sample: type=LANDLOCK_ACCESS msg=audit(1729738800.349:44): domain=195ba459b blockers=ptrace opid=1 ocomm="systemd" type=SYSCALL msg=audit(1729738800.349:44): arch=c000003e syscall=101 success=no [...] pid=300 auid=0 A following commit adds user documentation. Add KUnit tests to check reading of domain ID relative to layer level. The quick return for non-landlocked tasks is moved from task_ptrace() to each LSM hooks. It is not useful to inline the audit_enabled check because other computation are performed by landlock_log_denial(). Use scoped guards for RCU read-side critical sections. Cc: Günther Noack <gnoack@google.com> Acked-by: Paul Moore <paul@paul-moore.com> Link: https://lore.kernel.org/r/20250320190717.2287696-10-mic@digikod.net Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent 14f6c14 commit 33e65b0

8 files changed

Lines changed: 338 additions & 25 deletions

File tree

include/uapi/linux/audit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* 1100 - 1199 user space trusted application messages
3434
* 1200 - 1299 messages internal to the audit daemon
3535
* 1300 - 1399 audit event messages
36-
* 1400 - 1499 SE Linux use
36+
* 1400 - 1499 access control messages
3737
* 1500 - 1599 kernel LSPP events
3838
* 1600 - 1699 kernel crypto events
3939
* 1700 - 1799 kernel anomaly records
@@ -146,6 +146,7 @@
146146
#define AUDIT_IPE_ACCESS 1420 /* IPE denial or grant */
147147
#define AUDIT_IPE_CONFIG_CHANGE 1421 /* IPE config change */
148148
#define AUDIT_IPE_POLICY_LOAD 1422 /* IPE policy load */
149+
#define AUDIT_LANDLOCK_ACCESS 1423 /* Landlock denial */
149150

150151
#define AUDIT_FIRST_KERN_ANOM_MSG 1700
151152
#define AUDIT_LAST_KERN_ANOM_MSG 1799

security/landlock/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ landlock-y := setup.o syscalls.o object.o ruleset.o \
55

66
landlock-$(CONFIG_INET) += net.o
77

8-
landlock-$(CONFIG_AUDIT) += id.o
8+
landlock-$(CONFIG_AUDIT) += \
9+
id.o \
10+
audit.o \
11+
domain.o

security/landlock/audit.c

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Landlock - Audit helpers
4+
*
5+
* Copyright © 2023-2025 Microsoft Corporation
6+
*/
7+
8+
#include <kunit/test.h>
9+
#include <linux/audit.h>
10+
#include <linux/lsm_audit.h>
11+
12+
#include "audit.h"
13+
#include "cred.h"
14+
#include "domain.h"
15+
#include "limits.h"
16+
#include "ruleset.h"
17+
18+
static const char *get_blocker(const enum landlock_request_type type)
19+
{
20+
switch (type) {
21+
case LANDLOCK_REQUEST_PTRACE:
22+
return "ptrace";
23+
}
24+
25+
WARN_ON_ONCE(1);
26+
return "unknown";
27+
}
28+
29+
static void log_blockers(struct audit_buffer *const ab,
30+
const enum landlock_request_type type)
31+
{
32+
audit_log_format(ab, "%s", get_blocker(type));
33+
}
34+
35+
static struct landlock_hierarchy *
36+
get_hierarchy(const struct landlock_ruleset *const domain, const size_t layer)
37+
{
38+
struct landlock_hierarchy *hierarchy = domain->hierarchy;
39+
ssize_t i;
40+
41+
if (WARN_ON_ONCE(layer >= domain->num_layers))
42+
return hierarchy;
43+
44+
for (i = domain->num_layers - 1; i > layer; i--) {
45+
if (WARN_ON_ONCE(!hierarchy->parent))
46+
break;
47+
48+
hierarchy = hierarchy->parent;
49+
}
50+
51+
return hierarchy;
52+
}
53+
54+
#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST
55+
56+
static void test_get_hierarchy(struct kunit *const test)
57+
{
58+
struct landlock_hierarchy dom0_hierarchy = {
59+
.id = 10,
60+
};
61+
struct landlock_hierarchy dom1_hierarchy = {
62+
.parent = &dom0_hierarchy,
63+
.id = 20,
64+
};
65+
struct landlock_hierarchy dom2_hierarchy = {
66+
.parent = &dom1_hierarchy,
67+
.id = 30,
68+
};
69+
struct landlock_ruleset dom2 = {
70+
.hierarchy = &dom2_hierarchy,
71+
.num_layers = 3,
72+
};
73+
74+
KUNIT_EXPECT_EQ(test, 10, get_hierarchy(&dom2, 0)->id);
75+
KUNIT_EXPECT_EQ(test, 20, get_hierarchy(&dom2, 1)->id);
76+
KUNIT_EXPECT_EQ(test, 30, get_hierarchy(&dom2, 2)->id);
77+
KUNIT_EXPECT_EQ(test, 30, get_hierarchy(&dom2, -1)->id);
78+
}
79+
80+
#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */
81+
82+
static bool is_valid_request(const struct landlock_request *const request)
83+
{
84+
if (WARN_ON_ONCE(request->layer_plus_one > LANDLOCK_MAX_NUM_LAYERS))
85+
return false;
86+
87+
if (WARN_ON_ONCE(!request->layer_plus_one))
88+
return false;
89+
90+
return true;
91+
}
92+
93+
/**
94+
* landlock_log_denial - Create audit records related to a denial
95+
*
96+
* @subject: The Landlock subject's credential denying an action.
97+
* @request: Detail of the user space request.
98+
*/
99+
void landlock_log_denial(const struct landlock_cred_security *const subject,
100+
const struct landlock_request *const request)
101+
{
102+
struct audit_buffer *ab;
103+
struct landlock_hierarchy *youngest_denied;
104+
size_t youngest_layer;
105+
106+
if (WARN_ON_ONCE(!subject || !subject->domain ||
107+
!subject->domain->hierarchy || !request))
108+
return;
109+
110+
if (!is_valid_request(request))
111+
return;
112+
113+
if (!audit_enabled)
114+
return;
115+
116+
youngest_layer = request->layer_plus_one - 1;
117+
youngest_denied = get_hierarchy(subject->domain, youngest_layer);
118+
119+
/* Ignores denials after an execution. */
120+
if (!(subject->domain_exec & (1 << youngest_layer)))
121+
return;
122+
123+
/* Uses consistent allocation flags wrt common_lsm_audit(). */
124+
ab = audit_log_start(audit_context(), GFP_ATOMIC | __GFP_NOWARN,
125+
AUDIT_LANDLOCK_ACCESS);
126+
if (!ab)
127+
return;
128+
129+
audit_log_format(ab, "domain=%llx blockers=", youngest_denied->id);
130+
log_blockers(ab, request->type);
131+
audit_log_lsm_data(ab, &request->audit);
132+
audit_log_end(ab);
133+
}
134+
135+
#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST
136+
137+
static struct kunit_case test_cases[] = {
138+
/* clang-format off */
139+
KUNIT_CASE(test_get_hierarchy),
140+
{}
141+
/* clang-format on */
142+
};
143+
144+
static struct kunit_suite test_suite = {
145+
.name = "landlock_audit",
146+
.test_cases = test_cases,
147+
};
148+
149+
kunit_test_suite(test_suite);
150+
151+
#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */

security/landlock/audit.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Landlock - Audit helpers
4+
*
5+
* Copyright © 2023-2025 Microsoft Corporation
6+
*/
7+
8+
#ifndef _SECURITY_LANDLOCK_AUDIT_H
9+
#define _SECURITY_LANDLOCK_AUDIT_H
10+
11+
#include <linux/audit.h>
12+
#include <linux/lsm_audit.h>
13+
14+
#include "cred.h"
15+
16+
enum landlock_request_type {
17+
LANDLOCK_REQUEST_PTRACE = 1,
18+
};
19+
20+
/*
21+
* We should be careful to only use a variable of this type for
22+
* landlock_log_denial(). This way, the compiler can remove it entirely if
23+
* CONFIG_AUDIT is not set.
24+
*/
25+
struct landlock_request {
26+
/* Mandatory fields. */
27+
enum landlock_request_type type;
28+
struct common_audit_data audit;
29+
30+
/**
31+
* layer_plus_one: First layer level that denies the request + 1. The
32+
* extra one is useful to detect uninitialized field.
33+
*/
34+
size_t layer_plus_one;
35+
};
36+
37+
#ifdef CONFIG_AUDIT
38+
39+
void landlock_log_denial(const struct landlock_cred_security *const subject,
40+
const struct landlock_request *const request);
41+
42+
#else /* CONFIG_AUDIT */
43+
44+
static inline void
45+
landlock_log_denial(const struct landlock_cred_security *const subject,
46+
const struct landlock_request *const request)
47+
{
48+
}
49+
50+
#endif /* CONFIG_AUDIT */
51+
52+
#endif /* _SECURITY_LANDLOCK_AUDIT_H */

security/landlock/domain.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Landlock - Domain management
4+
*
5+
* Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
6+
* Copyright © 2018-2020 ANSSI
7+
* Copyright © 2024-2025 Microsoft Corporation
8+
*/
9+
10+
#include "domain.h"
11+
#include "id.h"
12+
13+
#ifdef CONFIG_AUDIT
14+
15+
/**
16+
* landlock_init_hierarchy_log - Partially initialize landlock_hierarchy
17+
*
18+
* @hierarchy: The hierarchy to initialize.
19+
*
20+
* @hierarchy->parent and @hierarchy->usage should already be set.
21+
*/
22+
int landlock_init_hierarchy_log(struct landlock_hierarchy *const hierarchy)
23+
{
24+
hierarchy->id = landlock_get_id_range(1);
25+
return 0;
26+
}
27+
28+
#endif /* CONFIG_AUDIT */

security/landlock/domain.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
* Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
66
* Copyright © 2018-2020 ANSSI
7+
* Copyright © 2024-2025 Microsoft Corporation
78
*/
89

910
#ifndef _SECURITY_LANDLOCK_DOMAIN_H
@@ -26,8 +27,29 @@ struct landlock_hierarchy {
2627
* domain.
2728
*/
2829
refcount_t usage;
30+
31+
#ifdef CONFIG_AUDIT
32+
/**
33+
* @id: Landlock domain ID, sets once at domain creation time.
34+
*/
35+
u64 id;
36+
#endif /* CONFIG_AUDIT */
2937
};
3038

39+
#ifdef CONFIG_AUDIT
40+
41+
int landlock_init_hierarchy_log(struct landlock_hierarchy *const hierarchy);
42+
43+
#else /* CONFIG_AUDIT */
44+
45+
static inline int
46+
landlock_init_hierarchy_log(struct landlock_hierarchy *const hierarchy)
47+
{
48+
return 0;
49+
}
50+
51+
#endif /* CONFIG_AUDIT */
52+
3153
static inline void
3254
landlock_get_hierarchy(struct landlock_hierarchy *const hierarchy)
3355
{

security/landlock/ruleset.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/workqueue.h>
2424

2525
#include "access.h"
26+
#include "audit.h"
2627
#include "domain.h"
2728
#include "limits.h"
2829
#include "object.h"
@@ -505,6 +506,7 @@ static void free_ruleset_work(struct work_struct *const work)
505506
free_ruleset(ruleset);
506507
}
507508

509+
/* Only called by hook_cred_free(). */
508510
void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset)
509511
{
510512
if (ruleset && refcount_dec_and_test(&ruleset->usage)) {
@@ -564,6 +566,10 @@ landlock_merge_ruleset(struct landlock_ruleset *const parent,
564566
if (err)
565567
return ERR_PTR(err);
566568

569+
err = landlock_init_hierarchy_log(new_dom->hierarchy);
570+
if (err)
571+
return ERR_PTR(err);
572+
567573
return no_free_ptr(new_dom);
568574
}
569575

0 commit comments

Comments
 (0)