1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//! Landlock: a thread that gives up access to files, to ports and to other
//! processes, without any privilege.
//!
//! A *ruleset* names the kinds of access it is about (it *handles* them),
//! and holds rules that each allow some of them somewhere: under a
//! directory, on a port. A thread that restricts itself with a ruleset is
//! from then on refused every access the ruleset handles and no rule
//! allows. What the ruleset does not handle is as it was: a ruleset that
//! handles writing alone leaves reading to the permissions of files.
//!
//! ```no_run
//! # #[cfg(any(target_os = "linux", target_os = "fullrust"))] {
//! use oscall::fs::{self, OpenOptions};
//! use oscall::process::linux::{self, landlock};
//! use oscall::process::linux::landlock::{AccessFs, AccessNet, RestrictFlags, Scope};
//!
//! // Nothing is written from here on, except under `/tmp`.
//! let abi = landlock::abi_version()?;
//! let writing = AccessFs::WRITING & AccessFs::for_abi(abi);
//! let ruleset = landlock::create_ruleset(writing, AccessNet::empty(), Scope::empty())?;
//! let tmp = fs::open("/tmp", &OpenOptions::new().read(true).directory(true))?;
//! landlock::add_path_rule(&ruleset, &tmp, writing)?;
//! linux::set_no_new_privileges()?;
//! landlock::restrict_self(&ruleset, RestrictFlags::empty())?;
//! # }
//! # Ok::<(), oscall::Error>(())
//! ```
//!
//! # What a restriction is attached to
//!
//! As a [`seccomp`](super::seccomp) filter: to the calling **thread**, and
//! the threads and processes it creates afterwards, for good, across
//! `execve`. Restricting again adds a layer: an access is allowed when
//! every layer allows it (16 layers at most, `E2BIG` beyond). And like a
//! filter it wants a thread that may not gain privileges
//! ([`set_no_new_privileges`](super::set_no_new_privileges)), or
//! `CAP_SYS_ADMIN`. [`ConfineExt::landlock`](super::ConfineExt::landlock)
//! restricts a child alone, before its program starts.
//!
//! Files that are open stay open, with what they could do: a restriction
//! is about what is opened from then on. (Truncating and the `ioctl`s of
//! devices, which are done on open files, are decided when the file is
//! opened.)
//!
//! A thread that may not open files for writing may not open the null
//! device either: spawning a child with
//! [`Stdio::Null`](crate::process::Stdio::Null) then fails, unless a rule
//! allows `/dev/null`.
//!
//! # Versions
//!
//! Landlock grew, and says how far by a number: [`abi_version`]. A kernel
//! refuses ([`InvalidInput`](crate::ErrorKind::InvalidInput)) a ruleset
//! that handles a kind of access it does not know, since it could not
//! enforce it; [`AccessFs::for_abi`], [`AccessNet::for_abi`] and
//! [`Scope::for_abi`] are what a version knows, to mask a ruleset with.
//! What an older kernel cannot restrict is then not restricted: a program
//! decides whether that is good enough for it.
//!
//! | Version | Linux | What it added |
//! |---|---|---|
//! | 1 | 5.13 | files and directories: [`AccessFs::EXECUTE`] to [`AccessFs::MAKE_SYM`] |
//! | 2 | 5.19 | [`AccessFs::REFER`] |
//! | 3 | 6.2 | [`AccessFs::TRUNCATE`] |
//! | 4 | 6.7 | TCP ports: [`AccessNet`] |
//! | 5 | 6.10 | [`AccessFs::IOCTL_DEV`] |
//! | 6 | 6.12 | [`Scope`] |
//! | 7 | 6.15 | [`RestrictFlags`] |
//!
//! # What is needed of the system
//!
//! A kernel built with Landlock (`CONFIG_SECURITY_LANDLOCK`) and started
//! with it among its security modules (`/sys/kernel/security/lsm` lists
//! them): [`abi_version`] fails with
//! [`Unsupported`](crate::ErrorKind::Unsupported) otherwise, as it does
//! under an emulator that lacks the calls (qemu-user).
use crate::;
crateflags!
crateflags!
crateflags!
crateflags!
/// The version of Landlock the running kernel has, 1 or more:
/// `landlock_create_ruleset` with `LANDLOCK_CREATE_RULESET_VERSION`. See
/// the [module documentation](self#versions).
///
/// # Errors
///
/// [`Unsupported`](crate::ErrorKind::Unsupported) for a kernel without
/// Landlock (`ENOSYS`), one that has it switched off (`EOPNOTSUPP`), and
/// an emulator that lacks the call.
/// The fixes the running kernel has for mistakes found in Landlock, one
/// bit each: `landlock_create_ruleset` with
/// `LANDLOCK_CREATE_RULESET_ERRATA` (Linux 6.15, and the older kernels
/// the fixes were brought to). The kernel's documentation lists them.
///
/// # Errors
///
/// As [`abi_version`]; [`InvalidInput`](crate::ErrorKind::InvalidInput)
/// on a kernel that does not answer the question.
/// Creates a ruleset that handles these kinds of access, with no rule
/// yet: `landlock_create_ruleset` (Linux 5.13). Restricting a thread with
/// it as it is denies all of them, everywhere.
///
/// The handle is close-on-exec, and is what [`add_path_rule`],
/// [`add_port_rule`], [`restrict_self`] and
/// [`ConfineExt::landlock`](super::ConfineExt::landlock) take. It can be
/// kept, and used for several threads or children.
///
/// # Errors
///
/// [`InvalidInput`](crate::ErrorKind::InvalidInput) for a right the
/// kernel does not know (mask with [`AccessFs::for_abi`] and its kin);
/// `ENOMSG` for a ruleset that handles nothing; `E2BIG` for network
/// rights or scopes on a kernel older than they are;
/// [`Unsupported`](crate::ErrorKind::Unsupported) as for
/// [`abi_version`].
/// Adds to `ruleset` a rule that allows the access `allowed` to the file
/// `parent` is open on, and, when it is a directory, to everything under
/// it: `landlock_add_rule` with `LANDLOCK_RULE_PATH_BENEATH`.
///
/// `parent` is any handle of the file or directory, whatever it was
/// opened for (`O_PATH` is enough). The rule is about that file, wherever
/// it is later moved or mounted, not about its name.
///
/// # Errors
///
/// [`InvalidInput`](crate::ErrorKind::InvalidInput) for rights the
/// ruleset does not handle, and for rights about directories on a file
/// that is not one; `ENOMSG` for no right at all;
/// [`BadHandle`](crate::ErrorKind::BadHandle) for a handle that is not
/// open, or a `ruleset` that is not one (`EBADFD`).
/// Adds to `ruleset` a rule that allows the access `allowed` to the TCP
/// port `port`: `landlock_add_rule` with `LANDLOCK_RULE_NET_PORT`
/// (version 4). The port is the number as programs write it, not in
/// network byte order.
///
/// Binding to port 0 asks the system for any free port of its dynamic
/// range: a rule for port 0 allows that, and so every port of that range.
///
/// # Errors
///
/// [`InvalidInput`](crate::ErrorKind::InvalidInput) for rights the
/// ruleset does not handle, and on a kernel older than the rule.
Sized>
/// Restricts the calling **thread** with `ruleset`, for good:
/// `landlock_restrict_self`. See the
/// [module documentation](self#what-a-restriction-is-attached-to) for
/// what that means, and for what the caller must have done first.
///
/// The ruleset is copied: rules added to it later are for the threads
/// restricted later.
///
/// # Errors
///
/// [`PermissionDenied`](crate::ErrorKind::PermissionDenied) without
/// [`set_no_new_privileges`](super::set_no_new_privileges) nor
/// `CAP_SYS_ADMIN`; `E2BIG` for a seventeenth layer;
/// [`InvalidInput`](crate::ErrorKind::InvalidInput) for flags the kernel
/// does not know.
Sized>