Skip to content

Commit 9fa2375

Browse files
committed
Merge tag 'landlock-6.11-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/mic/linux
Pull landlock updates from Mickaël Salaün: "This simplifies code and improves documentation" * tag 'landlock-6.11-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/mic/linux: landlock: Various documentation improvements landlock: Clarify documentation for struct landlock_ruleset_attr landlock: Use bit-fields for storing handled layer access masks
2 parents 8326f5e + f4b89d8 commit 9fa2375

File tree

6 files changed

+56
-59
lines changed

6 files changed

+56
-59
lines changed

Documentation/userspace-api/landlock.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Landlock: unprivileged access control
88
=====================================
99

1010
:Author: Mickaël Salaün
11-
:Date: April 2024
11+
:Date: July 2024
1212

1313
The goal of Landlock is to enable to restrict ambient rights (e.g. global
1414
filesystem or network access) for a set of processes. Because Landlock

include/uapi/linux/landlock.h

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,36 @@
1212
#include <linux/types.h>
1313

1414
/**
15-
* struct landlock_ruleset_attr - Ruleset definition
15+
* struct landlock_ruleset_attr - Ruleset definition.
1616
*
17-
* Argument of sys_landlock_create_ruleset(). This structure can grow in
18-
* future versions.
17+
* Argument of sys_landlock_create_ruleset().
18+
*
19+
* This structure defines a set of *handled access rights*, a set of actions on
20+
* different object types, which should be denied by default when the ruleset is
21+
* enacted. Vice versa, access rights that are not specifically listed here are
22+
* not going to be denied by this ruleset when it is enacted.
23+
*
24+
* For historical reasons, the %LANDLOCK_ACCESS_FS_REFER right is always denied
25+
* by default, even when its bit is not set in @handled_access_fs. In order to
26+
* add new rules with this access right, the bit must still be set explicitly
27+
* (cf. `Filesystem flags`_).
28+
*
29+
* The explicit listing of *handled access rights* is required for backwards
30+
* compatibility reasons. In most use cases, processes that use Landlock will
31+
* *handle* a wide range or all access rights that they know about at build time
32+
* (and that they have tested with a kernel that supported them all).
33+
*
34+
* This structure can grow in future Landlock versions.
1935
*/
2036
struct landlock_ruleset_attr {
2137
/**
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: it is a deny-by-default list that should
25-
* contain as much Landlock access rights as possible. Indeed, all
26-
* Landlock filesystem access rights that are not part of
27-
* handled_access_fs are allowed. This is needed for backward
28-
* compatibility reasons. One exception is the
29-
* %LANDLOCK_ACCESS_FS_REFER access right, which is always implicitly
30-
* handled, but must still be explicitly handled to add new rules with
31-
* this access right.
38+
* @handled_access_fs: Bitmask of handled filesystem actions
39+
* (cf. `Filesystem flags`_).
3240
*/
3341
__u64 handled_access_fs;
3442
/**
35-
* @handled_access_net: Bitmask of actions (cf. `Network flags`_)
36-
* that is handled by this ruleset and should then be forbidden if no
37-
* rule explicitly allow them.
43+
* @handled_access_net: Bitmask of handled network actions (cf. `Network
44+
* flags`_).
3845
*/
3946
__u64 handled_access_net;
4047
};
@@ -97,20 +104,21 @@ struct landlock_path_beneath_attr {
97104
*/
98105
struct landlock_net_port_attr {
99106
/**
100-
* @allowed_access: Bitmask of allowed access network for a port
107+
* @allowed_access: Bitmask of allowed network actions for a port
101108
* (cf. `Network flags`_).
102109
*/
103110
__u64 allowed_access;
104111
/**
105112
* @port: Network port in host endianness.
106113
*
107-
* It should be noted that port 0 passed to :manpage:`bind(2)` will
108-
* bind to an available port from a specific port range. This can be
109-
* configured thanks to the ``/proc/sys/net/ipv4/ip_local_port_range``
110-
* sysctl (also used for IPv6). A Landlock rule with port 0 and the
111-
* ``LANDLOCK_ACCESS_NET_BIND_TCP`` right means that requesting to bind
112-
* on port 0 is allowed and it will automatically translate to binding
113-
* on the related port range.
114+
* It should be noted that port 0 passed to :manpage:`bind(2)` will bind
115+
* to an available port from the ephemeral port range. This can be
116+
* configured with the ``/proc/sys/net/ipv4/ip_local_port_range`` sysctl
117+
* (also used for IPv6).
118+
*
119+
* A Landlock rule with port 0 and the ``LANDLOCK_ACCESS_NET_BIND_TCP``
120+
* right means that requesting to bind on port 0 is allowed and it will
121+
* automatically translate to binding on the related port range.
114122
*/
115123
__u64 port;
116124
};
@@ -131,10 +139,10 @@ struct landlock_net_port_attr {
131139
* The following access rights apply only to files:
132140
*
133141
* - %LANDLOCK_ACCESS_FS_EXECUTE: Execute a file.
134-
* - %LANDLOCK_ACCESS_FS_WRITE_FILE: Open a file with write access. Note that
135-
* you might additionally need the %LANDLOCK_ACCESS_FS_TRUNCATE right in order
136-
* to overwrite files with :manpage:`open(2)` using ``O_TRUNC`` or
137-
* :manpage:`creat(2)`.
142+
* - %LANDLOCK_ACCESS_FS_WRITE_FILE: Open a file with write access. When
143+
* opening files for writing, you will often additionally need the
144+
* %LANDLOCK_ACCESS_FS_TRUNCATE right. In many cases, these system calls
145+
* truncate existing files when overwriting them (e.g., :manpage:`creat(2)`).
138146
* - %LANDLOCK_ACCESS_FS_READ_FILE: Open a file with read access.
139147
* - %LANDLOCK_ACCESS_FS_TRUNCATE: Truncate a file with :manpage:`truncate(2)`,
140148
* :manpage:`ftruncate(2)`, :manpage:`creat(2)`, or :manpage:`open(2)` with
@@ -256,7 +264,7 @@ struct landlock_net_port_attr {
256264
* These flags enable to restrict a sandboxed process to a set of network
257265
* actions. This is supported since the Landlock ABI version 4.
258266
*
259-
* TCP sockets with allowed actions:
267+
* The following access rights apply to TCP port numbers:
260268
*
261269
* - %LANDLOCK_ACCESS_NET_BIND_TCP: Bind a TCP socket to a local port.
262270
* - %LANDLOCK_ACCESS_NET_CONNECT_TCP: Connect an active TCP socket to

security/landlock/limits.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
#define LANDLOCK_LAST_ACCESS_FS LANDLOCK_ACCESS_FS_IOCTL_DEV
2222
#define LANDLOCK_MASK_ACCESS_FS ((LANDLOCK_LAST_ACCESS_FS << 1) - 1)
2323
#define LANDLOCK_NUM_ACCESS_FS __const_hweight64(LANDLOCK_MASK_ACCESS_FS)
24-
#define LANDLOCK_SHIFT_ACCESS_FS 0
2524

2625
#define LANDLOCK_LAST_ACCESS_NET LANDLOCK_ACCESS_NET_CONNECT_TCP
2726
#define LANDLOCK_MASK_ACCESS_NET ((LANDLOCK_LAST_ACCESS_NET << 1) - 1)
2827
#define LANDLOCK_NUM_ACCESS_NET __const_hweight64(LANDLOCK_MASK_ACCESS_NET)
29-
#define LANDLOCK_SHIFT_ACCESS_NET LANDLOCK_NUM_ACCESS_FS
3028

3129
/* clang-format on */
3230

security/landlock/ruleset.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,9 @@ static void build_check_ruleset(void)
169169
.num_rules = ~0,
170170
.num_layers = ~0,
171171
};
172-
typeof(ruleset.access_masks[0]) access_masks = ~0;
173172

174173
BUILD_BUG_ON(ruleset.num_rules < LANDLOCK_MAX_NUM_RULES);
175174
BUILD_BUG_ON(ruleset.num_layers < LANDLOCK_MAX_NUM_LAYERS);
176-
BUILD_BUG_ON(access_masks <
177-
((LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS) |
178-
(LANDLOCK_MASK_ACCESS_NET << LANDLOCK_SHIFT_ACCESS_NET)));
179175
}
180176

181177
/**

security/landlock/ruleset.h

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
3939
static_assert(sizeof(unsigned long) >= sizeof(access_mask_t));
4040

4141
/* Ruleset access masks. */
42-
typedef u32 access_masks_t;
43-
/* Makes sure all ruleset access rights can be stored. */
44-
static_assert(BITS_PER_TYPE(access_masks_t) >=
45-
LANDLOCK_NUM_ACCESS_FS + LANDLOCK_NUM_ACCESS_NET);
42+
struct access_masks {
43+
access_mask_t fs : LANDLOCK_NUM_ACCESS_FS;
44+
access_mask_t net : LANDLOCK_NUM_ACCESS_NET;
45+
};
4646

4747
typedef u16 layer_mask_t;
4848
/* Makes sure all layers can be checked. */
@@ -226,7 +226,7 @@ struct landlock_ruleset {
226226
* layers are set once and never changed for the
227227
* lifetime of the ruleset.
228228
*/
229-
access_masks_t access_masks[];
229+
struct access_masks access_masks[];
230230
};
231231
};
232232
};
@@ -265,8 +265,7 @@ landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset,
265265

266266
/* Should already be checked in sys_landlock_create_ruleset(). */
267267
WARN_ON_ONCE(fs_access_mask != fs_mask);
268-
ruleset->access_masks[layer_level] |=
269-
(fs_mask << LANDLOCK_SHIFT_ACCESS_FS);
268+
ruleset->access_masks[layer_level].fs |= fs_mask;
270269
}
271270

272271
static inline void
@@ -278,17 +277,14 @@ landlock_add_net_access_mask(struct landlock_ruleset *const ruleset,
278277

279278
/* Should already be checked in sys_landlock_create_ruleset(). */
280279
WARN_ON_ONCE(net_access_mask != net_mask);
281-
ruleset->access_masks[layer_level] |=
282-
(net_mask << LANDLOCK_SHIFT_ACCESS_NET);
280+
ruleset->access_masks[layer_level].net |= net_mask;
283281
}
284282

285283
static inline access_mask_t
286284
landlock_get_raw_fs_access_mask(const struct landlock_ruleset *const ruleset,
287285
const u16 layer_level)
288286
{
289-
return (ruleset->access_masks[layer_level] >>
290-
LANDLOCK_SHIFT_ACCESS_FS) &
291-
LANDLOCK_MASK_ACCESS_FS;
287+
return ruleset->access_masks[layer_level].fs;
292288
}
293289

294290
static inline access_mask_t
@@ -304,9 +300,7 @@ static inline access_mask_t
304300
landlock_get_net_access_mask(const struct landlock_ruleset *const ruleset,
305301
const u16 layer_level)
306302
{
307-
return (ruleset->access_masks[layer_level] >>
308-
LANDLOCK_SHIFT_ACCESS_NET) &
309-
LANDLOCK_MASK_ACCESS_NET;
303+
return ruleset->access_masks[layer_level].net;
310304
}
311305

312306
bool landlock_unmask_layers(const struct landlock_rule *const rule,

security/landlock/syscalls.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,7 @@ static int add_rule_net_port(struct landlock_ruleset *ruleset,
378378
* with the new rule.
379379
* @rule_type: Identify the structure type pointed to by @rule_attr:
380380
* %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT.
381-
* @rule_attr: Pointer to a rule (only of type &struct
382-
* landlock_path_beneath_attr for now).
381+
* @rule_attr: Pointer to a rule (matching the @rule_type).
383382
* @flags: Must be 0.
384383
*
385384
* This system call enables to define a new rule and add it to an existing
@@ -390,18 +389,20 @@ static int add_rule_net_port(struct landlock_ruleset *ruleset,
390389
* - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
391390
* - %EAFNOSUPPORT: @rule_type is %LANDLOCK_RULE_NET_PORT but TCP/IP is not
392391
* supported by the running kernel;
393-
* - %EINVAL: @flags is not 0, or inconsistent access in the rule (i.e.
392+
* - %EINVAL: @flags is not 0;
393+
* - %EINVAL: The rule accesses are inconsistent (i.e.
394394
* &landlock_path_beneath_attr.allowed_access or
395-
* &landlock_net_port_attr.allowed_access is not a subset of the
396-
* ruleset handled accesses), or &landlock_net_port_attr.port is
397-
* greater than 65535;
398-
* - %ENOMSG: Empty accesses (e.g. &landlock_path_beneath_attr.allowed_access);
395+
* &landlock_net_port_attr.allowed_access is not a subset of the ruleset
396+
* handled accesses)
397+
* - %EINVAL: &landlock_net_port_attr.port is greater than 65535;
398+
* - %ENOMSG: Empty accesses (e.g. &landlock_path_beneath_attr.allowed_access is
399+
* 0);
399400
* - %EBADF: @ruleset_fd is not a file descriptor for the current thread, or a
400401
* member of @rule_attr is not a file descriptor as expected;
401402
* - %EBADFD: @ruleset_fd is not a ruleset file descriptor, or a member of
402403
* @rule_attr is not the expected file descriptor type;
403404
* - %EPERM: @ruleset_fd has no write access to the underlying ruleset;
404-
* - %EFAULT: @rule_attr inconsistency.
405+
* - %EFAULT: @rule_attr was not a valid address.
405406
*/
406407
SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
407408
const enum landlock_rule_type, rule_type,

0 commit comments

Comments
 (0)