Skip to content

Commit 1176a15

Browse files
committed
landlock: Log scoped denials
Add audit support for unix_stream_connect, unix_may_send, task_kill, and file_send_sigiotask hooks. The related blockers are: - scope.abstract_unix_socket - scope.signal Audit event sample for abstract unix socket: type=LANDLOCK_DENY msg=audit(1729738800.268:30): domain=195ba459b blockers=scope.abstract_unix_socket path=00666F6F Audit event sample for signal: type=LANDLOCK_DENY msg=audit(1729738800.291:31): domain=195ba459b blockers=scope.signal opid=1 ocomm="systemd" Refactor and simplify error handling in LSM hooks. Extend struct landlock_file_security with fown_layer and use it to log the blocking domain. The struct aligned size is still 16 bytes. Cc: Günther Noack <[email protected]> Cc: Tahera Fahimi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mickaël Salaün <[email protected]>
1 parent 9f74411 commit 1176a15

File tree

5 files changed

+97
-18
lines changed

5 files changed

+97
-18
lines changed

security/landlock/audit.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ get_blocker(const enum landlock_request_type type,
7070
if (WARN_ON_ONCE(access_bit >= ARRAY_SIZE(net_access_strings)))
7171
return "unknown";
7272
return net_access_strings[access_bit];
73+
74+
case LANDLOCK_REQUEST_SCOPE_ABSTRACT_UNIX_SOCKET:
75+
WARN_ON_ONCE(access_bit != -1);
76+
return "scope.abstract_unix_socket";
77+
78+
case LANDLOCK_REQUEST_SCOPE_SIGNAL:
79+
WARN_ON_ONCE(access_bit != -1);
80+
return "scope.signal";
7381
}
7482

7583
WARN_ON_ONCE(1);

security/landlock/audit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ enum landlock_request_type {
1919
LANDLOCK_REQUEST_FS_CHANGE_TOPOLOGY,
2020
LANDLOCK_REQUEST_FS_ACCESS,
2121
LANDLOCK_REQUEST_NET_ACCESS,
22+
LANDLOCK_REQUEST_SCOPE_ABSTRACT_UNIX_SOCKET,
23+
LANDLOCK_REQUEST_SCOPE_SIGNAL,
2224
};
2325

2426
/*

security/landlock/fs.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,14 +1798,15 @@ static void hook_file_set_fowner(struct file *file)
17981798
{
17991799
struct landlock_ruleset *prev_dom;
18001800
struct landlock_cred_security fown_subject = {};
1801+
size_t fown_layer = 0;
18011802

18021803
if (control_current_fowner(file_f_owner(file))) {
18031804
static const struct access_masks signal_scope = {
18041805
.scope = LANDLOCK_SCOPE_SIGNAL,
18051806
};
18061807
const struct landlock_cred_security *new_subject =
1807-
landlock_get_applicable_subject(current_cred(),
1808-
signal_scope, NULL);
1808+
landlock_get_applicable_subject(
1809+
current_cred(), signal_scope, &fown_layer);
18091810
if (new_subject) {
18101811
landlock_get_ruleset(new_subject->domain);
18111812
fown_subject = *new_subject;
@@ -1814,6 +1815,9 @@ static void hook_file_set_fowner(struct file *file)
18141815

18151816
prev_dom = landlock_file(file)->fown_subject.domain;
18161817
landlock_file(file)->fown_subject = fown_subject;
1818+
#ifdef CONFIG_AUDIT
1819+
landlock_file(file)->fown_layer = fown_layer;
1820+
#endif /* CONFIG_AUDIT*/
18171821

18181822
/* May be called in an RCU read-side critical section. */
18191823
landlock_put_ruleset_deferred(prev_dom);

security/landlock/fs.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef _SECURITY_LANDLOCK_FS_H
1111
#define _SECURITY_LANDLOCK_FS_H
1212

13+
#include <linux/build_bug.h>
1314
#include <linux/fs.h>
1415
#include <linux/init.h>
1516
#include <linux/rcupdate.h>
@@ -62,6 +63,11 @@ struct landlock_file_security {
6263
* _LANDLOCK_ACCESS_FS_OPTIONAL).
6364
*/
6465
deny_masks_t deny_masks;
66+
/**
67+
* @fown_layer: Layer level of @fown_subject->domain with
68+
* LANDLOCK_SCOPE_SIGNAL.
69+
*/
70+
u8 fown_layer;
6571
#endif /* CONFIG_AUDIT */
6672

6773
/**
@@ -74,6 +80,16 @@ struct landlock_file_security {
7480
struct landlock_cred_security fown_subject;
7581
};
7682

83+
#ifdef CONFIG_AUDIT
84+
85+
/* Makes sure all layers can be identified. */
86+
/* clang-format off */
87+
static_assert((typeof_member(struct landlock_file_security, fown_layer))~0 >=
88+
LANDLOCK_MAX_NUM_LAYERS);
89+
/* clang-format off */
90+
91+
#endif /* CONFIG_AUDIT */
92+
7793
/**
7894
* struct landlock_superblock_security - Superblock security blob
7995
*

security/landlock/task.c

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -266,26 +266,41 @@ static int hook_unix_stream_connect(struct sock *const sock,
266266
struct sock *const other,
267267
struct sock *const newsk)
268268
{
269+
size_t handle_layer;
269270
const struct landlock_cred_security *const subject =
270271
landlock_get_applicable_subject(current_cred(), unix_scope,
271-
NULL);
272+
&handle_layer);
272273

273274
/* Quick return for non-landlocked tasks. */
274275
if (!subject)
275276
return 0;
276277

277-
if (is_abstract_socket(other) && sock_is_scoped(other, subject->domain))
278-
return -EPERM;
278+
if (!is_abstract_socket(other))
279+
return 0;
280+
281+
if (!sock_is_scoped(other, subject->domain))
282+
return 0;
279283

280-
return 0;
284+
landlock_log_denial(subject, &(struct landlock_request) {
285+
.type = LANDLOCK_REQUEST_SCOPE_ABSTRACT_UNIX_SOCKET,
286+
.audit = {
287+
.type = LSM_AUDIT_DATA_NET,
288+
.u.net = &(struct lsm_network_audit) {
289+
.sk = other,
290+
},
291+
},
292+
.layer_plus_one = handle_layer + 1,
293+
});
294+
return -EPERM;
281295
}
282296

283297
static int hook_unix_may_send(struct socket *const sock,
284298
struct socket *const other)
285299
{
300+
size_t handle_layer;
286301
const struct landlock_cred_security *const subject =
287302
landlock_get_applicable_subject(current_cred(), unix_scope,
288-
NULL);
303+
&handle_layer);
289304

290305
if (!subject)
291306
return 0;
@@ -297,11 +312,23 @@ static int hook_unix_may_send(struct socket *const sock,
297312
if (unix_peer(sock->sk) == other->sk)
298313
return 0;
299314

300-
if (is_abstract_socket(other->sk) &&
301-
sock_is_scoped(other->sk, subject->domain))
302-
return -EPERM;
315+
if (!is_abstract_socket(other->sk))
316+
return 0;
317+
318+
if (!sock_is_scoped(other->sk, subject->domain))
319+
return 0;
303320

304-
return 0;
321+
landlock_log_denial(subject, &(struct landlock_request) {
322+
.type = LANDLOCK_REQUEST_SCOPE_ABSTRACT_UNIX_SOCKET,
323+
.audit = {
324+
.type = LSM_AUDIT_DATA_NET,
325+
.u.net = &(struct lsm_network_audit) {
326+
.sk = other->sk,
327+
},
328+
},
329+
.layer_plus_one = handle_layer + 1,
330+
});
331+
return -EPERM;
305332
}
306333

307334
static const struct access_masks signal_scope = {
@@ -313,6 +340,7 @@ static int hook_task_kill(struct task_struct *const p,
313340
const struct cred *cred)
314341
{
315342
bool is_scoped;
343+
size_t handle_layer;
316344
const struct landlock_cred_security *subject;
317345

318346
if (!cred) {
@@ -331,7 +359,8 @@ static int hook_task_kill(struct task_struct *const p,
331359
cred = current_cred();
332360
}
333361

334-
subject = landlock_get_applicable_subject(cred, signal_scope, NULL);
362+
subject = landlock_get_applicable_subject(cred, signal_scope,
363+
&handle_layer);
335364

336365
/* Quick return for non-landlocked tasks. */
337366
if (!subject)
@@ -343,10 +372,19 @@ static int hook_task_kill(struct task_struct *const p,
343372
landlock_get_task_domain(p),
344373
signal_scope.scope);
345374
}
346-
if (is_scoped)
347-
return -EPERM;
348375

349-
return 0;
376+
if (!is_scoped)
377+
return 0;
378+
379+
landlock_log_denial(subject, &(struct landlock_request) {
380+
.type = LANDLOCK_REQUEST_SCOPE_SIGNAL,
381+
.audit = {
382+
.type = LSM_AUDIT_DATA_TASK,
383+
.u.tsk = p,
384+
},
385+
.layer_plus_one = handle_layer + 1,
386+
});
387+
return -EPERM;
350388
}
351389

352390
static int hook_file_send_sigiotask(struct task_struct *tsk,
@@ -375,10 +413,21 @@ static int hook_file_send_sigiotask(struct task_struct *tsk,
375413
landlock_get_task_domain(tsk),
376414
signal_scope.scope);
377415
}
378-
if (is_scoped)
379-
return -EPERM;
380416

381-
return 0;
417+
if (!is_scoped)
418+
return 0;
419+
420+
landlock_log_denial(subject, &(struct landlock_request) {
421+
.type = LANDLOCK_REQUEST_SCOPE_SIGNAL,
422+
.audit = {
423+
.type = LSM_AUDIT_DATA_TASK,
424+
.u.tsk = tsk,
425+
},
426+
#ifdef CONFIG_AUDIT
427+
.layer_plus_one = landlock_file(fown->file)->fown_layer + 1,
428+
#endif /* CONFIG_AUDIT */
429+
});
430+
return -EPERM;
382431
}
383432

384433
static struct security_hook_list landlock_hooks[] __ro_after_init = {

0 commit comments

Comments
 (0)