Skip to content

Commit 7592a0e

Browse files
esyrldv-alt
andcommitted
Implement landlock_{add_rule,create_ruleset,restrict_self} syscall decoding
Introduced by Linux commit v5.13-rc1~71^2~4. * bundled/linux/include/uapi/linux/landlock.h: New file, copied from headers_install'ed Linux kernel v5.13. * bundled/Makefile.am (EXTRA_DIST): Add it. * src/Makefile.am (libstrace_a_SOURCES): Add landlock.c. * src/landlock.c: New file. * src/linux/generic/syscallent-common.h([BASE_NR + 444], [BASE_NR + 445], [BASE_NR + 446]): New entries, for landlock_create_ruleset, landlock_add_rule, and landlock_restrict_self, respectively. * src/pathtrace.c (pathtrace_match_set): Handle SEN_landlock_add_rule, SEN_landlock_create_ruleset, and SEN_landlock_restrict_self. * src/xlat/landlock_create_ruleset_flags.in: New xlat. * src/xlat/landlock_rule_types.in: Likewise. * src/xlat/landlock_ruleset_access_fs.in: Likewise. * tests/Makefile.am (check_PROGRAMS): Add landlock_create_ruleset-success and landlock_create_ruleset-success-y. (DECODER_TESTS): Add landlock_create_ruleset-success.test. * tests/gen_tests.in (landlock_add_rule, landlock_add_rule-y, landlock_create_ruleset, landlock_create_ruleset-success-y, landlock_create_ruleset-y, landlock_restrict_self, landlock_restrict_self-y): New tests. * tests/landlock_add_rule.c: New file. * tests/landlock_add_rule-y.c: Likewise. * tests/landlock_create_ruleset-success.c: Likewise. * tests/landlock_create_ruleset-success-y.c: Likewise. * tests/landlock_create_ruleset.c: Likewise. * tests/landlock_create_ruleset-y.c: Likewise. * tests/landlock_restrict_self.c: Likewise. * tests/landlock_restrict_self-y.c: Likewise. * tests/landlock_create_ruleset-success.test: New test. * tests/pure_executables.list: Add landlock_add_rule, landlock_add_rule-y, landlock_create_ruleset, landlock_create_ruleset-y, landlock_restrict_self, and landlock_restrict_self-y. * tests/.gitignore: Add landlock_add_rule, landlock_add_rule-y, landlock_create_ruleset, landlock_create_ruleset-success, landlock_create_ruleset-success-y, landlock_create_ruleset-y, landlock_restrict_self, and landlock_restrict_self-y. * NEWS: Mention it. Co-authored-by: Dmitry V. Levin <ldv@strace.io>
1 parent f001287 commit 7592a0e

23 files changed

Lines changed: 704 additions & 0 deletions

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Noteworthy changes in release ?.?? (????-??-??)
99
PTRACE_SETREGS, PTRACE_SETREGS64, PTRACE_GETFPREGS, and PTRACE_SETFPREGS
1010
requests.
1111
* Implemented powerpc System Call Vectored ABI support.
12+
* Implemented decoding of landlock_add_rule, landlock_create_ruleset,
13+
and landlock_restrict_self syscalls introduced in Linux 5.13.
1214
* Enhanced decoding of perf_event_open syscall.
1315
* Updated lists of BPF_*, IORING_*, KEXEC_*, KEY_*, KVM_*, NT_*, PR_*,
1416
PTRACE_*, RTM_*, RTPROT_*, TRAP_*, UFFD_*, UFFDIO_*, and V4L2_* constants.

‎bundled/Makefile.am‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ EXTRA_DIST = \
5454
linux/include/uapi/linux/kcmp.h \
5555
linux/include/uapi/linux/kexec.h \
5656
linux/include/uapi/linux/keyctl.h \
57+
linux/include/uapi/linux/landlock.h \
5758
linux/include/uapi/linux/libc-compat.h \
5859
linux/include/uapi/linux/loop.h \
5960
linux/include/uapi/linux/memfd.h \
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2+
/*
3+
* Landlock - User space API
4+
*
5+
* Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
6+
* Copyright © 2018-2020 ANSSI
7+
*/
8+
9+
#ifndef _LINUX_LANDLOCK_H
10+
#define _LINUX_LANDLOCK_H
11+
12+
#include <linux/types.h>
13+
14+
/**
15+
* struct landlock_ruleset_attr - Ruleset definition
16+
*
17+
* Argument of sys_landlock_create_ruleset(). This structure can grow in
18+
* future versions.
19+
*/
20+
struct landlock_ruleset_attr {
21+
/**
22+
* @handled_access_fs: Bitmask of actions (cf. `Filesystem flags`_)
23+
* that is handled by this ruleset and should then be forbidden if no
24+
* rule explicitly allow them. This is needed for backward
25+
* compatibility reasons.
26+
*/
27+
__u64 handled_access_fs;
28+
};
29+
30+
/*
31+
* sys_landlock_create_ruleset() flags:
32+
*
33+
* - %LANDLOCK_CREATE_RULESET_VERSION: Get the highest supported Landlock ABI
34+
* version.
35+
*/
36+
#define LANDLOCK_CREATE_RULESET_VERSION (1U << 0)
37+
38+
/**
39+
* enum landlock_rule_type - Landlock rule type
40+
*
41+
* Argument of sys_landlock_add_rule().
42+
*/
43+
enum landlock_rule_type {
44+
/**
45+
* @LANDLOCK_RULE_PATH_BENEATH: Type of a &struct
46+
* landlock_path_beneath_attr .
47+
*/
48+
LANDLOCK_RULE_PATH_BENEATH = 1,
49+
};
50+
51+
/**
52+
* struct landlock_path_beneath_attr - Path hierarchy definition
53+
*
54+
* Argument of sys_landlock_add_rule().
55+
*/
56+
struct landlock_path_beneath_attr {
57+
/**
58+
* @allowed_access: Bitmask of allowed actions for this file hierarchy
59+
* (cf. `Filesystem flags`_).
60+
*/
61+
__u64 allowed_access;
62+
/**
63+
* @parent_fd: File descriptor, open with ``O_PATH``, which identifies
64+
* the parent directory of a file hierarchy, or just a file.
65+
*/
66+
__s32 parent_fd;
67+
/*
68+
* This struct is packed to avoid trailing reserved members.
69+
* Cf. security/landlock/syscalls.c:build_check_abi()
70+
*/
71+
} __attribute__((packed));
72+
73+
/**
74+
* DOC: fs_access
75+
*
76+
* A set of actions on kernel objects may be defined by an attribute (e.g.
77+
* &struct landlock_path_beneath_attr) including a bitmask of access.
78+
*
79+
* Filesystem flags
80+
* ~~~~~~~~~~~~~~~~
81+
*
82+
* These flags enable to restrict a sandboxed process to a set of actions on
83+
* files and directories. Files or directories opened before the sandboxing
84+
* are not subject to these restrictions.
85+
*
86+
* A file can only receive these access rights:
87+
*
88+
* - %LANDLOCK_ACCESS_FS_EXECUTE: Execute a file.
89+
* - %LANDLOCK_ACCESS_FS_WRITE_FILE: Open a file with write access.
90+
* - %LANDLOCK_ACCESS_FS_READ_FILE: Open a file with read access.
91+
*
92+
* A directory can receive access rights related to files or directories. The
93+
* following access right is applied to the directory itself, and the
94+
* directories beneath it:
95+
*
96+
* - %LANDLOCK_ACCESS_FS_READ_DIR: Open a directory or list its content.
97+
*
98+
* However, the following access rights only apply to the content of a
99+
* directory, not the directory itself:
100+
*
101+
* - %LANDLOCK_ACCESS_FS_REMOVE_DIR: Remove an empty directory or rename one.
102+
* - %LANDLOCK_ACCESS_FS_REMOVE_FILE: Unlink (or rename) a file.
103+
* - %LANDLOCK_ACCESS_FS_MAKE_CHAR: Create (or rename or link) a character
104+
* device.
105+
* - %LANDLOCK_ACCESS_FS_MAKE_DIR: Create (or rename) a directory.
106+
* - %LANDLOCK_ACCESS_FS_MAKE_REG: Create (or rename or link) a regular file.
107+
* - %LANDLOCK_ACCESS_FS_MAKE_SOCK: Create (or rename or link) a UNIX domain
108+
* socket.
109+
* - %LANDLOCK_ACCESS_FS_MAKE_FIFO: Create (or rename or link) a named pipe.
110+
* - %LANDLOCK_ACCESS_FS_MAKE_BLOCK: Create (or rename or link) a block device.
111+
* - %LANDLOCK_ACCESS_FS_MAKE_SYM: Create (or rename or link) a symbolic link.
112+
*
113+
* .. warning::
114+
*
115+
* It is currently not possible to restrict some file-related actions
116+
* accessible through these syscall families: :manpage:`chdir(2)`,
117+
* :manpage:`truncate(2)`, :manpage:`stat(2)`, :manpage:`flock(2)`,
118+
* :manpage:`chmod(2)`, :manpage:`chown(2)`, :manpage:`setxattr(2)`,
119+
* :manpage:`utime(2)`, :manpage:`ioctl(2)`, :manpage:`fcntl(2)`,
120+
* :manpage:`access(2)`.
121+
* Future Landlock evolutions will enable to restrict them.
122+
*/
123+
#define LANDLOCK_ACCESS_FS_EXECUTE (1ULL << 0)
124+
#define LANDLOCK_ACCESS_FS_WRITE_FILE (1ULL << 1)
125+
#define LANDLOCK_ACCESS_FS_READ_FILE (1ULL << 2)
126+
#define LANDLOCK_ACCESS_FS_READ_DIR (1ULL << 3)
127+
#define LANDLOCK_ACCESS_FS_REMOVE_DIR (1ULL << 4)
128+
#define LANDLOCK_ACCESS_FS_REMOVE_FILE (1ULL << 5)
129+
#define LANDLOCK_ACCESS_FS_MAKE_CHAR (1ULL << 6)
130+
#define LANDLOCK_ACCESS_FS_MAKE_DIR (1ULL << 7)
131+
#define LANDLOCK_ACCESS_FS_MAKE_REG (1ULL << 8)
132+
#define LANDLOCK_ACCESS_FS_MAKE_SOCK (1ULL << 9)
133+
#define LANDLOCK_ACCESS_FS_MAKE_FIFO (1ULL << 10)
134+
#define LANDLOCK_ACCESS_FS_MAKE_BLOCK (1ULL << 11)
135+
#define LANDLOCK_ACCESS_FS_MAKE_SYM (1ULL << 12)
136+
137+
#endif /* _LINUX_LANDLOCK_H */

‎src/Makefile.am‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ libstrace_a_SOURCES = \
168168
keyctl_kdf_params.h \
169169
kill_save_errno.h \
170170
kvm.c \
171+
landlock.c \
171172
largefile_wrappers.h \
172173
ldt.c \
173174
link.c \

‎src/landlock.c‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2021 Eugene Syromyatnikov <evgsyr@gmail.com>
3+
* All rights reserved.
4+
*
5+
* SPDX-License-Identifier: LGPL-2.1-or-later
6+
*/
7+
8+
#include "defs.h"
9+
10+
#include <linux/landlock.h>
11+
12+
#include "xlat/landlock_create_ruleset_flags.h"
13+
#include "xlat/landlock_rule_types.h"
14+
#include "xlat/landlock_ruleset_access_fs.h"
15+
16+
static void
17+
print_landlock_ruleset_attr(struct tcb *tcp, const kernel_ulong_t addr,
18+
const kernel_ulong_t size)
19+
{
20+
struct {
21+
uint64_t handled_access_fs;
22+
} attr;
23+
24+
if (size < offsetofend(typeof(attr), handled_access_fs)) {
25+
printaddr(addr);
26+
return;
27+
}
28+
29+
if (umoven_or_printaddr(tcp, addr, MIN(size, sizeof(attr)), &attr))
30+
return;
31+
32+
tprint_struct_begin();
33+
PRINT_FIELD_FLAGS(attr, handled_access_fs, landlock_ruleset_access_fs,
34+
"LANDLOCK_ACCESS_FS_???");
35+
36+
if (size > offsetofend(typeof(attr), handled_access_fs)) {
37+
tprint_arg_next();
38+
tprint_more_data_follows();
39+
}
40+
41+
tprint_struct_end();
42+
}
43+
44+
SYS_FUNC(landlock_create_ruleset)
45+
{
46+
kernel_ulong_t attr = tcp->u_arg[0];
47+
kernel_ulong_t size = tcp->u_arg[1];
48+
unsigned int flags = tcp->u_arg[2];
49+
int fd_flag = flags & LANDLOCK_CREATE_RULESET_VERSION ? 0 : RVAL_FD;
50+
51+
/* attr */
52+
print_landlock_ruleset_attr(tcp, attr, size);
53+
tprint_arg_next();
54+
55+
/* size */
56+
PRINT_VAL_U(size);
57+
tprint_arg_next();
58+
59+
/* flags */
60+
printflags(landlock_create_ruleset_flags, flags,
61+
"LANDLOCK_CREATE_RULESET_???");
62+
63+
return RVAL_DECODED | fd_flag;
64+
}
65+
66+
static void
67+
print_landlock_path_beneath_attr(struct tcb *tcp, const kernel_ulong_t addr)
68+
{
69+
struct {
70+
uint64_t allowed_access;
71+
int32_t parent_fd;
72+
} ATTRIBUTE_PACKED attr;
73+
74+
if (umove_or_printaddr(tcp, addr, &attr))
75+
return;
76+
77+
tprint_struct_begin();
78+
PRINT_FIELD_FLAGS(attr, allowed_access, landlock_ruleset_access_fs,
79+
"LANDLOCK_ACCESS_FS_???");
80+
tprint_struct_next();
81+
PRINT_FIELD_FD(attr, parent_fd, tcp);
82+
tprint_struct_end();
83+
}
84+
85+
SYS_FUNC(landlock_add_rule)
86+
{
87+
unsigned int rule_type = tcp->u_arg[1];
88+
89+
/* ruleset_fd */
90+
printfd(tcp, tcp->u_arg[0]);
91+
tprint_arg_next();
92+
93+
/* rule_type */
94+
printxval(landlock_rule_types, rule_type, "LANDLOCK_RULE_???");
95+
tprint_arg_next();
96+
97+
/* rule_attr */
98+
switch (rule_type) {
99+
case LANDLOCK_RULE_PATH_BENEATH:
100+
print_landlock_path_beneath_attr(tcp, tcp->u_arg[2]);
101+
break;
102+
103+
default:
104+
printaddr(tcp->u_arg[2]);
105+
}
106+
tprint_arg_next();
107+
108+
/* flags */
109+
PRINT_VAL_X((unsigned int) tcp->u_arg[3]);
110+
111+
return RVAL_DECODED;
112+
}
113+
114+
SYS_FUNC(landlock_restrict_self)
115+
{
116+
/* ruleset_fd */
117+
printfd(tcp, tcp->u_arg[0]);
118+
tprint_arg_next();
119+
120+
/* flags */
121+
PRINT_VAL_X((unsigned int) tcp->u_arg[1]);
122+
123+
return RVAL_DECODED;
124+
}

‎src/linux/generic/syscallent-common.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@
2727
[BASE_NR + 440] = { 5, TD, SEN(process_madvise), "process_madvise" },
2828
[BASE_NR + 441] = { 6, TD, SEN(epoll_pwait2), "epoll_pwait2" },
2929
[BASE_NR + 442] = { 5, TD|TF, SEN(mount_setattr), "mount_setattr" },
30+
/* 443 is reserved for quotactl_fd */
31+
[BASE_NR + 444] = { 3, TD, SEN(landlock_create_ruleset), "landlock_create_ruleset" },
32+
[BASE_NR + 445] = { 4, TD, SEN(landlock_add_rule), "landlock_add_rule" },
33+
[BASE_NR + 446] = { 2, TD, SEN(landlock_restrict_self), "landlock_restrict_self" },

‎src/pathtrace.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ pathtrace_match_set(struct tcb *tcp, struct path_set *set)
372372
case SEN_io_uring_enter:
373373
case SEN_io_uring_register:
374374
case SEN_io_uring_setup:
375+
case SEN_landlock_add_rule:
376+
case SEN_landlock_create_ruleset:
377+
case SEN_landlock_restrict_self:
375378
case SEN_memfd_create:
376379
case SEN_mq_getsetattr:
377380
case SEN_mq_notify:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#unconditional
2+
LANDLOCK_CREATE_RULESET_VERSION

‎src/xlat/landlock_rule_types.in‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#unconditional
2+
#value_indexed
3+
LANDLOCK_RULE_PATH_BENEATH
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#unconditional
2+
LANDLOCK_ACCESS_FS_EXECUTE
3+
LANDLOCK_ACCESS_FS_WRITE_FILE
4+
LANDLOCK_ACCESS_FS_READ_FILE
5+
LANDLOCK_ACCESS_FS_READ_DIR
6+
LANDLOCK_ACCESS_FS_REMOVE_DIR
7+
LANDLOCK_ACCESS_FS_REMOVE_FILE
8+
LANDLOCK_ACCESS_FS_MAKE_CHAR
9+
LANDLOCK_ACCESS_FS_MAKE_DIR
10+
LANDLOCK_ACCESS_FS_MAKE_REG
11+
LANDLOCK_ACCESS_FS_MAKE_SOCK
12+
LANDLOCK_ACCESS_FS_MAKE_FIFO
13+
LANDLOCK_ACCESS_FS_MAKE_BLOCK
14+
LANDLOCK_ACCESS_FS_MAKE_SYM

0 commit comments

Comments
 (0)