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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! What Linux alone has, and what containing a process is made of there:
//! namespaces, the identifier maps of a user namespace, process
//! descriptors (pidfds), starting a child in a cgroup, and a few process
//! attributes (`prctl`).
//!
//! # Namespaces
//!
//! A namespace is the view a process has of one kind of system resource:
//! [`Namespaces`] names the kinds. A child is started in new ones with
//! [`CommandExt::namespaces`]; [`unshare`] gives new ones to the caller,
//! and [`setns`] moves it into those of another process.
//!
//! Creating a namespace needs `CAP_SYS_ADMIN`, except a **user namespace**,
//! which anyone may create where the system allows it, and whose creator
//! has every capability *inside* it: a process asks for
//! [`Namespaces::USER`] together with the others, and the kernel creates
//! the user namespace first. In a new user namespace no identifier means
//! anything until its maps are written ([`CommandExt::uid_map`],
//! [`write_uid_map`]): the process is `nobody` (65534) to itself, and loses
//! its capabilities when it runs a program.
//!
//! Systems restrict this, and answer
//! [`PermissionDenied`](crate::ErrorKind::PermissionDenied) unless said
//! otherwise:
//!
//! - `user.max_user_namespaces` may be 0: `ENOSPC`,
//! [`StorageFull`](crate::ErrorKind::StorageFull).
//! - Debian's `kernel.unprivileged_userns_clone` switches user namespaces
//! off for who has no privilege.
//! - Ubuntu's AppArmor restriction
//! (`kernel.apparmor_restrict_unprivileged_userns`) lets a process
//! without privilege create its namespaces, and
//! its parent write maps of one identifier, but denies the process the
//! *use* of the capabilities it has there: it is root of its namespace
//! and may not set the host name, nor create another namespace later.
//! What the `clone` that creates the child does is done; what the child
//! would do itself is refused.
//! - The default system call filter of a container runtime (Docker)
//! refuses every call that takes a namespace.
//!
//! # Memory and other limits: cgroups
//!
//! The limits of `unix::set_limit` are per process, and the one on memory
//! ([`AddressSpace`](super::unix::Resource::AddressSpace)) counts address
//! space, not memory in use. What limits the memory, the processor time or
//! the number of processes of a child *and everything it starts* is a
//! control group (cgroup v2): a directory under `/sys/fs/cgroup` whose
//! files are the settings (`memory.max`, `pids.max`, `cpu.max`...) and
//! which [`fs`](crate::fs) creates, reads and writes like any other.
//! [`CommandExt::cgroup`] starts a child in one, so that not one
//! instruction of the program runs outside it. oscall has no more than
//! that: no cgroup manager.
//!
//! # Process descriptors
//!
//! A pidfd names one process for as long as it is open, whatever becomes of
//! its identifier: [`pidfd_open`], [`pidfd_send_signal`], [`pidfd_wait`].
//! A [`Child`](super::Child) holds one and lends it
//! ([`AsHandle`]). A pidfd is readable once its process has exited:
//! [`event::unix::register`](crate::event::unix::register) takes it, with
//! [`Interest::READABLE`](crate::event::Interest::READABLE), for any
//! process (a [`Child`](super::Child) has
//! [`Poller::register_child`](crate::event::Poller::register_child)).
//!
//! # What is needed of the kernel
//!
//! oscall supports Linux 5.10 and later, which has everything here but the
//! time namespace (5.6) on kernels built without it. An emulator or a
//! system call filter may lack a call all the same, and it then fails with
//! [`Unsupported`](crate::ErrorKind::Unsupported) (`ENOSYS`): qemu-user
//! has no `clone3`, and refuses to create a child in new namespaces
//! (`EINVAL`).
//!
//! # Not covered
//!
//! Mounting (`mount`, `umount2`, `pivot_root`, `chroot`, the new mount
//! API), capabilities (`capset`), `seccomp`, and reading the host name:
//! [`raw::linux`](crate::raw::linux) makes those calls.
use crate::;
use ;
use crateBorrowedHandle;
crateflags!
/// Gives the calling **thread** new namespaces of the kinds in
/// `namespaces`, which it stops sharing with the rest of the system:
/// `unshare`.
///
/// Namespaces belong to threads, not to processes. The threads and
/// processes created afterwards by this thread are in its namespaces; the
/// other threads of the process stay where they were. Two kinds do not
/// take the caller along:
///
/// - [`PID`](Namespaces::PID): the caller keeps its identifier and its
/// namespace. The next process it creates is process 1 of the new one
/// (see [`CommandExt::namespaces`] for what that means), and when that
/// one has exited no other can be created: spawning then fails with
/// [`OutOfMemory`](crate::ErrorKind::OutOfMemory) (`ENOMEM`).
/// - [`TIME`](Namespaces::TIME): likewise, the processes created
/// afterwards are in the new namespace, as is the caller once it runs
/// another program.
///
/// # Errors
///
/// [`PermissionDenied`](crate::ErrorKind::PermissionDenied) without
/// `CAP_SYS_ADMIN` in the user namespace of the caller, which a new
/// [`USER`](Namespaces::USER) namespace asked for in the same call gives
/// (see the [module documentation](self#namespaces) for what may forbid
/// that one). [`InvalidInput`](crate::ErrorKind::InvalidInput) for
/// [`USER`](Namespaces::USER) in a process that has several threads: the
/// kernel refuses it, and a Rust test, which runs on a thread of its own,
/// is such a process.
/// Moves the calling thread into existing namespaces: `setns`.
///
/// `handle` is either:
///
/// - a namespace: one of the files of `/proc/<pid>/ns` (`uts`, `net`,
/// `mnt`, `ipc`, `user`, `pid`, `cgroup`, `time`), opened for reading
/// with [`fs`](crate::fs). `namespaces` is then the kind it must be, or
/// empty for any;
/// - a pidfd ([`pidfd_open`], a [`Child`](super::Child)): the thread joins
/// the namespaces of that process, of all the kinds in `namespaces` or of
/// none (Linux 5.8).
///
/// As with [`unshare`], [`PID`](Namespaces::PID) and
/// [`TIME`](Namespaces::TIME) change where the processes created afterwards
/// are, not the caller.
///
/// # Errors
///
/// [`PermissionDenied`](crate::ErrorKind::PermissionDenied) without
/// `CAP_SYS_ADMIN` in the user namespace that owns the target, and in the
/// caller's own. [`InvalidInput`](crate::ErrorKind::InvalidInput) when the
/// handle is not of the kind asked for, for a pidfd with no kind, and to
/// join a [`USER`](Namespaces::USER) namespace from a process that has
/// several threads, or the one the caller is in already.
Sized>
/// Sets the host name of the UTS namespace of the caller: `sethostname`.
/// For a namespace of one's own ([`Namespaces::UTS`]); in the initial one
/// this renames the machine.
///
/// The name is at most 64 bytes; `/proc/sys/kernel/hostname` reads it
/// back.
///
/// # Errors
///
/// [`PermissionDenied`](crate::ErrorKind::PermissionDenied) without
/// `CAP_SYS_ADMIN` in the user namespace that owns the UTS namespace;
/// [`InvalidInput`](crate::ErrorKind::InvalidInput) if the name is too
/// long.
/// A range of a user namespace's identifier map: one line of `uid_map` or
/// `gid_map`. The `count` identifiers from `inside`, as the processes of
/// the namespace see them, are the `count` identifiers from `outside` in
/// the namespace of whoever writes the map.
///
/// `IdMap { inside: 0, outside: uid, count: 1 }` makes the process root in
/// its namespace, and itself to the rest of the system.
/// Writes the user map of the user namespace the process `pid` is in:
/// `/proc/<pid>/uid_map`. `pid` may be the caller
/// ([`id`](super::id)) after [`unshare`], or a child created in a new
/// namespace; [`CommandExt::uid_map`] does it for a child before its
/// program starts.
///
/// A map is written once, whole, and never changes. The writer must be in
/// the namespace or in its parent. Without `CAP_SETUID` in the parent
/// namespace, the map can only be one range of one identifier, `outside`
/// being the effective user of the writer (who must be the user that
/// created the namespace). A process that writes its own map is always in
/// that case, root included: its capabilities are those of the new
/// namespace. Larger maps are written from outside, by the parent.
///
/// # Errors
///
/// [`PermissionDenied`](crate::ErrorKind::PermissionDenied) when the rules
/// above are not met, or the map was already written;
/// [`InvalidInput`](crate::ErrorKind::InvalidInput) for an empty map, one
/// whose text is not shorter than 4096 bytes, and what the kernel finds
/// wrong with it (overlapping ranges, a count of 0, more than 340 ranges);
/// [`NotFound`](crate::ErrorKind::NotFound) when there is no such process,
/// or no `/proc`.
/// Writes the group map of the user namespace the process `pid` is in:
/// `/proc/<pid>/gid_map`. As [`write_uid_map`], with `CAP_SETGID` and the
/// effective group of the writer, and one more rule: without the
/// capability, `setgroups` must have been denied first
/// ([`allow_setgroups`]).
/// Says whether the processes of the user namespace the process `pid` is
/// in may call `setgroups`, which drops supplementary groups:
/// `/proc/<pid>/setgroups`, `allow` (the default) or `deny`.
///
/// Dropping a group can *give* access, to files that deny it to the group:
/// this is why a process without privilege must deny `setgroups` before it
/// may write a group map. It can only be set before the group map is
/// written, and once denied stays so, in that namespace and those created
/// inside it.
/// Opens a descriptor of the process `pid`: `pidfd_open` (Linux 5.3).
///
/// The descriptor names that process even after it has exited and its
/// identifier was given to another. That holds from the moment it is
/// opened: `pid` itself is only safe to use while it cannot have been
/// reused, which is the case of a child not yet waited for, and of the
/// caller.
///
/// The handle is close-on-exec, becomes readable when the process exits
/// (see the [module documentation](self#process-descriptors)), and is
/// what [`pidfd_send_signal`], [`pidfd_wait`] and [`setns`] take.
///
/// # Errors
///
/// [`NoSuchProcess`](crate::ErrorKind::NoSuchProcess);
/// [`InvalidInput`](crate::ErrorKind::InvalidInput) if `pid` is 0 or does
/// not fit in the system's type, or names a thread that is not the first
/// of its process; [`Unsupported`](crate::ErrorKind::Unsupported) under an
/// emulator that lacks the call.
/// Sends a signal to the process of a pidfd: `pidfd_send_signal`
/// (Linux 5.1). `signal` is the system's number, as for
/// `unix::send_signal`; 0 sends nothing and checks that the process is
/// there.
///
/// # Errors
///
/// [`NoSuchProcess`](crate::ErrorKind::NoSuchProcess) once the process has
/// been waited for (one that has exited and was not yet collected takes
/// the signal without an error);
/// [`PermissionDenied`](crate::ErrorKind::PermissionDenied);
/// [`BadHandle`](crate::ErrorKind::BadHandle) if the handle is not a
/// pidfd; [`InvalidInput`](crate::ErrorKind::InvalidInput) if the system
/// has no such signal.
Sized>
/// Waits for the process of a pidfd, which must be a child of the caller,
/// to exit, and collects it: `waitid(P_PIDFD)` (Linux 5.4).
///
/// The status of a child can be collected once. A
/// [`Child`](super::Child) for the same process then finds it gone (see
/// there): wait for a child through one or the other.
///
/// # Errors
///
/// `ECHILD` when the process is not a child of the caller, or was
/// collected already; [`Interrupted`](crate::ErrorKind::Interrupted) when
/// a signal handler runs during the wait.
Sized>
/// As [`pidfd_wait`], without waiting: `None` if the process is still
/// running.
Sized>
/// Forbids the calling **thread** to gain privileges by running a program:
/// `prctl(PR_SET_NO_NEW_PRIVS)`. From then on set-user-ID and set-group-ID
/// bits and file capabilities are ignored by `execve`. It cannot be
/// undone, and the threads and processes created afterwards by this thread
/// inherit it; the other threads of the process are not affected.
///
/// A process without privilege needs this before it may install a
/// `seccomp` filter. [`CommandExt::no_new_privileges`] sets it for a child
/// alone.
/// Whether the calling thread may no longer gain privileges
/// (`PR_GET_NO_NEW_PRIVS`): see [`set_no_new_privileges`].
/// Makes the calling process the one that adopts its orphaned descendants,
/// or stops it being so: `prctl(PR_SET_CHILD_SUBREAPER)`.
///
/// When a process exits, its children are normally adopted by process 1.
/// With this they go to the nearest ancestor that asked for them, which is
/// told when they exit (`SIGCHLD`) and must wait for them, as for its own
/// children: this is how a supervisor keeps track of every process a
/// program left behind. oscall has no call that waits for *any* child:
/// `waitid(P_ALL)` through [`raw::linux`](crate::raw::linux) does, and a
/// pidfd opened for a known identifier.
///
/// The setting is not inherited by children.
/// Whether the calling process adopts its orphaned descendants
/// (`PR_GET_CHILD_SUBREAPER`): see [`set_child_subreaper`].
/// Says whether the calling process may be dumped and inspected:
/// `prctl(PR_SET_DUMPABLE)`.
///
/// A process that is not dumpable writes no core dump, cannot be attached
/// to with `ptrace` by a process without privilege, and its files under
/// `/proc/<pid>` belong to root: other processes of the same user, and
/// the process itself where it is not root, can no longer open most of
/// them (which includes `/proc/self/environ`, `maps`, `mem` and the
/// namespace files of `ns`). The kernel clears the flag by itself when a
/// process changes identity, and sets it again when a program is run.
/// Whether the calling process may be dumped (`PR_GET_DUMPABLE`): see
/// [`set_dumpable`]. `false` also for a process the system only lets root
/// dump (`fs.suid_dumpable = 2`).
/// What [`CommandExt`] sets.
pub
/// More to set on a [`Command`] on Linux.