Skip to content

Commit 6c4a30f

Browse files
committed
Merge: landlock: update landlock-lsm headers for api v5 and v6 in RHEL-9
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/7037 JIRA: https://issues.redhat.com/browse/RHEL-94688 Customer request to update landlock-lsm headers to better match RHEL-10 Omitted-fix: 8d66506 ("bpf: syzkaller found null ptr deref in unix_bpf proto add") Omitted-fix: 16b2f26 ("bpf: sockmap, fix proto update hook to avoid dup calls") Signed-off-by: Ryan Sullivan <[email protected]> Approved-by: Felix Maurer <[email protected]> Approved-by: Jay Shin <[email protected]> Approved-by: Ondrej Mosnáček <[email protected]> Approved-by: CKI KWF Bot <[email protected]> Merged-by: Augusto Caringi <[email protected]>
2 parents 5032de1 + 5ce73a3 commit 6c4a30f

File tree

22 files changed

+1824
-106
lines changed

22 files changed

+1824
-106
lines changed

Documentation/userspace-api/landlock.rst

Lines changed: 42 additions & 2 deletions
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: September 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
@@ -81,6 +81,8 @@ to be explicit about the denied-by-default access rights.
8181
.handled_access_net =
8282
LANDLOCK_ACCESS_NET_BIND_TCP |
8383
LANDLOCK_ACCESS_NET_CONNECT_TCP,
84+
.scoped =
85+
LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET,
8486
};
8587
8688
Because we may not know on which kernel version an application will be
@@ -119,6 +121,10 @@ version, and only use the available subset of access rights:
119121
case 4:
120122
/* Removes LANDLOCK_ACCESS_FS_IOCTL_DEV for ABI < 5 */
121123
ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;
124+
__attribute__((fallthrough));
125+
case 5:
126+
/* Removes LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET for ABI < 6 */
127+
ruleset_attr.scoped &= ~LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET;
122128
}
123129
124130
This enables to create an inclusive ruleset that will contain our rules.
@@ -306,6 +312,33 @@ To be allowed to use :manpage:`ptrace(2)` and related syscalls on a target
306312
process, a sandboxed process should have a subset of the target process rules,
307313
which means the tracee must be in a sub-domain of the tracer.
308314

315+
IPC scoping
316+
-----------
317+
318+
Similar to the implicit `Ptrace restrictions`_, we may want to further restrict
319+
interactions between sandboxes. Each Landlock domain can be explicitly scoped
320+
for a set of actions by specifying it on a ruleset. For example, if a
321+
sandboxed process should not be able to :manpage:`connect(2)` to a
322+
non-sandboxed process through abstract :manpage:`unix(7)` sockets, we can
323+
specify such restriction with ``LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET``.
324+
325+
A sandboxed process can connect to a non-sandboxed process when its domain is
326+
not scoped. If a process's domain is scoped, it can only connect to sockets
327+
created by processes in the same scope.
328+
329+
A connected datagram socket behaves like a stream socket when its domain is
330+
scoped, meaning if the domain is scoped after the socket is connected , it can
331+
still :manpage:`send(2)` data just like a stream socket. However, in the same
332+
scenario, a non-connected datagram socket cannot send data (with
333+
:manpage:`sendto(2)`) outside its scope.
334+
335+
A process with a scoped domain can inherit a socket created by a non-scoped
336+
process. The process cannot connect to this socket since it has a scoped
337+
domain.
338+
339+
IPC scoping does not support exceptions, so if a domain is scoped, no rules can
340+
be added to allow access to resources or processes outside of the scope.
341+
309342
Truncating files
310343
----------------
311344

@@ -404,7 +437,7 @@ Access rights
404437
-------------
405438

406439
.. kernel-doc:: include/uapi/linux/landlock.h
407-
:identifiers: fs_access net_access
440+
:identifiers: fs_access net_access scope
408441

409442
Creating a new ruleset
410443
----------------------
@@ -541,6 +574,13 @@ earlier ABI.
541574
Starting with the Landlock ABI version 5, it is possible to restrict the use of
542575
:manpage:`ioctl(2)` using the new ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right.
543576

577+
Abstract UNIX socket scoping (ABI < 6)
578+
--------------------------------------
579+
580+
Starting with the Landlock ABI version 6, it is possible to restrict
581+
connections to an abstract :manpage:`unix(7)` socket by setting
582+
``LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET`` to the ``scoped`` ruleset attribute.
583+
544584
.. _kernel_support:
545585

546586
Kernel support

include/linux/skmsg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ struct sk_psock {
106106
struct mutex work_mutex;
107107
struct sk_psock_work_state work_state;
108108
struct work_struct work;
109+
struct sock *sk_pair;
109110
struct rcu_work rwork;
110111
};
111112

include/net/af_unix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ static inline void unix_state_lock_nested(struct sock *sk,
8888
spin_lock_nested(&unix_sk(sk)->lock, subclass);
8989
}
9090

91+
#define unix_peer(sk) (unix_sk(sk)->peer)
9192
#define peer_wait peer_wq.wait
9293

9394
long unix_inq_len(struct sock *sk);

include/uapi/linux/landlock.h

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,44 @@
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;
47+
/**
48+
* @scoped: Bitmask of scopes (cf. `Scope flags`_)
49+
* restricting a Landlock domain from accessing outside
50+
* resources (e.g. IPCs).
51+
*/
52+
__u64 scoped;
4053
};
4154

4255
/*
@@ -97,20 +110,21 @@ struct landlock_path_beneath_attr {
97110
*/
98111
struct landlock_net_port_attr {
99112
/**
100-
* @allowed_access: Bitmask of allowed access network for a port
113+
* @allowed_access: Bitmask of allowed network actions for a port
101114
* (cf. `Network flags`_).
102115
*/
103116
__u64 allowed_access;
104117
/**
105118
* @port: Network port in host endianness.
106119
*
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.
120+
* It should be noted that port 0 passed to :manpage:`bind(2)` will bind
121+
* to an available port from the ephemeral port range. This can be
122+
* configured with the ``/proc/sys/net/ipv4/ip_local_port_range`` sysctl
123+
* (also used for IPv6).
124+
*
125+
* A Landlock rule with port 0 and the ``LANDLOCK_ACCESS_NET_BIND_TCP``
126+
* right means that requesting to bind on port 0 is allowed and it will
127+
* automatically translate to binding on the related port range.
114128
*/
115129
__u64 port;
116130
};
@@ -131,10 +145,10 @@ struct landlock_net_port_attr {
131145
* The following access rights apply only to files:
132146
*
133147
* - %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)`.
148+
* - %LANDLOCK_ACCESS_FS_WRITE_FILE: Open a file with write access. When
149+
* opening files for writing, you will often additionally need the
150+
* %LANDLOCK_ACCESS_FS_TRUNCATE right. In many cases, these system calls
151+
* truncate existing files when overwriting them (e.g., :manpage:`creat(2)`).
138152
* - %LANDLOCK_ACCESS_FS_READ_FILE: Open a file with read access.
139153
* - %LANDLOCK_ACCESS_FS_TRUNCATE: Truncate a file with :manpage:`truncate(2)`,
140154
* :manpage:`ftruncate(2)`, :manpage:`creat(2)`, or :manpage:`open(2)` with
@@ -256,7 +270,7 @@ struct landlock_net_port_attr {
256270
* These flags enable to restrict a sandboxed process to a set of network
257271
* actions. This is supported since the Landlock ABI version 4.
258272
*
259-
* TCP sockets with allowed actions:
273+
* The following access rights apply to TCP port numbers:
260274
*
261275
* - %LANDLOCK_ACCESS_NET_BIND_TCP: Bind a TCP socket to a local port.
262276
* - %LANDLOCK_ACCESS_NET_CONNECT_TCP: Connect an active TCP socket to
@@ -266,4 +280,25 @@ struct landlock_net_port_attr {
266280
#define LANDLOCK_ACCESS_NET_BIND_TCP (1ULL << 0)
267281
#define LANDLOCK_ACCESS_NET_CONNECT_TCP (1ULL << 1)
268282
/* clang-format on */
283+
284+
/**
285+
* DOC: scope
286+
*
287+
* Scope flags
288+
* ~~~~~~~~~~~
289+
*
290+
* These flags enable to isolate a sandboxed process from a set of IPC actions.
291+
* Setting a flag for a ruleset will isolate the Landlock domain to forbid
292+
* connections to resources outside the domain.
293+
*
294+
* Scopes:
295+
*
296+
* - %LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET: Restrict a sandboxed process from
297+
* connecting to an abstract UNIX socket created by a process outside the
298+
* related Landlock domain (e.g. a parent domain or a non-sandboxed process).
299+
*/
300+
/* clang-format off */
301+
#define LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET (1ULL << 0)
302+
/* clang-format on*/
303+
269304
#endif /* _UAPI_LINUX_LANDLOCK_H */

net/core/skmsg.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,8 @@ static void sk_psock_destroy(struct work_struct *work)
833833

834834
if (psock->sk_redir)
835835
sock_put(psock->sk_redir);
836+
if (psock->sk_pair)
837+
sock_put(psock->sk_pair);
836838
sock_put(psock->sk);
837839
kfree(psock);
838840
}

net/unix/af_unix.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,6 @@ static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
205205
}
206206
#endif /* CONFIG_SECURITY_NETWORK */
207207

208-
#define unix_peer(sk) (unix_sk(sk)->peer)
209-
210208
static inline int unix_our_peer(struct sock *sk, struct sock *osk)
211209
{
212210
return unix_peer(osk) == sk;

net/unix/unix_bpf.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,17 @@ int unix_dgram_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool re
160160

161161
int unix_stream_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
162162
{
163+
struct sock *sk_pair;
164+
163165
if (restore) {
164166
sk->sk_write_space = psock->saved_write_space;
165167
sock_replace_proto(sk, psock->sk_proto);
166168
return 0;
167169
}
168170

171+
sk_pair = unix_peer(sk);
172+
sock_hold(sk_pair);
173+
psock->sk_pair = sk_pair;
169174
unix_stream_bpf_check_needs_rebuild(psock->sk_proto);
170175
sock_replace_proto(sk, &unix_stream_bpf_prot);
171176
return 0;

0 commit comments

Comments
 (0)