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
use std::{ffi::OsStr, os::fd::RawFd};
use nix::sys::{resource::rlim_t, signal::Signal};
use crate::{
landlock::{AccessFs, AccessNet},
sandbox::RawIoctlMap,
unshare::{ffi_util::ToCString, Command},
};
impl Command {
/// Allow child process to daemonize. By default we run equivalent of
/// `set_parent_death_signal(SIGKILL)`. See the `set_parent_death_signal`
/// for better explanation.
pub fn allow_daemonize(&mut self) -> &mut Command {
self.config.death_sig = None;
self
}
/// Set a signal that is sent to a process when it's parent is dead.
/// This is by default set to `SIGKILL`. And you should keep it that way
/// unless you know what you are doing.
///
/// Particularly you should consider the following choices:
///
/// 1. Instead of setting ``PDEATHSIG`` to some other signal, send signal
/// yourself and wait until child gracefully finishes.
///
/// 2. Instead of daemonizing use ``systemd``/``upstart``/whatever system
/// init script to run your service
///
/// Another issue with this option is that it works only with immediate
/// child. To better control all descendant processes you may need the
/// following:
///
/// 1. The `prctl(PR_SET_CHILD_SUBREAPER..)` in parent which allows to
/// "catch" descendant processes.
///
/// 2. The pid namespaces
///
/// The former is out of scope of this library. The latter works by
/// ``cmd.unshare(Namespace::Pid)``, but you may need to setup mount points
/// and other important things (which are out of scope too).
///
/// To reset this behavior use ``allow_daemonize()``.
pub fn set_parent_death_signal(&mut self, sig: Signal) -> &mut Command {
self.config.death_sig = Some(sig);
self
}
/// Keep signal mask intact after executing child, keeps also ignored
/// signals
///
/// By default signal mask is empty and all signals are reset to the
/// `SIG_DFL` value right before `execve()` syscall.
///
/// This is only useful if started process is aware of the issue and sets
/// sigmasks to some reasonable value. When used wisely it may avoid some
/// race conditions when signal is sent after child is cloned but before
/// child have been able to establish it's state.
pub fn keep_sigmask(&mut self) -> &mut Command {
self.config.restore_sigmask = false;
self
}
/// Set the argument zero for the process
///
/// By default argument zero is same as path to the program to run. You
/// may set it to a short name of the command or to something else to
/// pretend there is a symlink to a program (for example to run `gzip` as
/// `gunzip`).
pub fn arg0<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
if let Some(ref mut exe_args) = self.exe_args {
exe_args[0] = arg.to_cstring();
} else {
self.exe_args = Some(vec![arg.to_cstring()]);
}
self
}
/// Deny reading the timestamp counter (x86 only)
pub fn deny_tsc(&mut self, deny: bool) -> &mut Command {
self.config.deny_tsc = deny;
self
}
/// Makes process keeps capabilities (only CAP_SYS_PTRACE is dropped atm).
pub fn keep(&mut self, keep: bool) -> &mut Command {
self.config.keep = keep;
self
}
/// STOPs process before exec, so e.g. a ptracer can attach.
pub fn stop(&mut self, stop: bool) -> &mut Command {
self.config.stop = stop;
self
}
/// Disable Speculative Store Bypass mitigations for seccomp(2) filters.
pub fn ssb(&mut self, ssb: bool) -> &mut Command {
self.config.ssb = ssb;
self
}
/// Enable append-only mitigations.
///
/// Currently only disables pwritev2(2) with the RWF_NOAPPEND flag.
pub fn append_only(&mut self, append_only: bool) -> &mut Command {
self.config.append_only = append_only;
self
}
/// Enable kernel pointer in syscall arguments mitigations.
pub fn restrict_kptr(&mut self, restrict_kptr: bool) -> &mut Command {
self.config.restrict_kptr = restrict_kptr;
self
}
/// Hint whether prlimit(2) is enabled by seccomp(2) so resource limits can be applied late.
pub fn restrict_prlimit(&mut self, restrict_prlimit: bool) -> &mut Command {
self.config.restrict_prlimit = restrict_prlimit;
self
}
/// Specify pseudoterminal file descriptor.
pub fn pty(&mut self, fd: Option<RawFd>) -> &mut Command {
self.pty_fd = fd;
self
}
/// Specify ioctl(2) denylist.
pub fn ioctl_denylist(&mut self, denylist: Option<RawIoctlMap>) -> &mut Command {
self.ioctl_denylist = denylist;
self
}
/// Specify RLIMIT_AS rlimit(2).
pub fn rlimit_as(&mut self, lim: Option<rlim_t>) -> &mut Command {
self.config.rlimit_as = lim;
self
}
/// Specify RLIMIT_CORE rlimit(2).
pub fn rlimit_core(&mut self, lim: Option<rlim_t>) -> &mut Command {
self.config.rlimit_core = lim;
self
}
/// Specify RLIMIT_CPU rlimit(2).
pub fn rlimit_cpu(&mut self, lim: Option<rlim_t>) -> &mut Command {
self.config.rlimit_cpu = lim;
self
}
/// Specify RLIMIT_DATA rlimit(2).
pub fn rlimit_data(&mut self, lim: Option<rlim_t>) -> &mut Command {
self.config.rlimit_data = lim;
self
}
/// Specify RLIMIT_FSIZE rlimit(2).
pub fn rlimit_fsize(&mut self, lim: Option<rlim_t>) -> &mut Command {
self.config.rlimit_fsize = lim;
self
}
/// Specify RLIMIT_MEMLOCK rlimit(2).
pub fn rlimit_memlock(&mut self, lim: Option<rlim_t>) -> &mut Command {
self.config.rlimit_memlock = lim;
self
}
/// Specify RLIMIT_MSGQUEUE rlimit(2).
pub fn rlimit_msgqueue(&mut self, lim: Option<rlim_t>) -> &mut Command {
self.config.rlimit_msgqueue = lim;
self
}
/// Specify RLIMIT_NICE rlimit(2).
pub fn rlimit_nice(&mut self, lim: Option<rlim_t>) -> &mut Command {
self.config.rlimit_nice = lim;
self
}
/// Specify RLIMIT_NOFILE rlimit(2).
pub fn rlimit_nofile(&mut self, lim: Option<rlim_t>) -> &mut Command {
self.config.rlimit_nofile = lim;
self
}
/// Specify RLIMIT_NPROC rlimit(2).
pub fn rlimit_nproc(&mut self, lim: Option<rlim_t>) -> &mut Command {
self.config.rlimit_nproc = lim;
self
}
/// Specify RLIMIT_RTPRIO rlimit(2).
pub fn rlimit_rtprio(&mut self, lim: Option<rlim_t>) -> &mut Command {
self.config.rlimit_rtprio = lim;
self
}
/// Specify RLIMIT_RTTIME rlimit(2).
pub fn rlimit_rttime(&mut self, lim: Option<rlim_t>) -> &mut Command {
self.config.rlimit_rttime = lim;
self
}
/// Specify RLIMIT_SIGPENDING rlimit(2).
pub fn rlimit_sigpending(&mut self, lim: Option<rlim_t>) -> &mut Command {
self.config.rlimit_sigpending = lim;
self
}
/// Specify RLIMIT_STACK rlimit(2).
pub fn rlimit_stack(&mut self, lim: Option<rlim_t>) -> &mut Command {
self.config.rlimit_stack = lim;
self
}
/// Specify handled landlock(7) filesystem access rights.
pub fn landlock_access_fs(&mut self, access_fs: AccessFs) -> &mut Command {
self.config.landlock_access_fs = access_fs;
self
}
/// Specify handled landlock(7) network access rights.
pub fn landlock_access_net(&mut self, access_net: AccessNet) -> &mut Command {
self.config.landlock_access_net = access_net;
self
}
/// Specify whether landlock(7) UnixAbstractSocket scoping should be enabled.
pub fn landlock_scoped_abs(&mut self, scoped_abs: bool) -> &mut Command {
self.config.landlock_scoped_abs = scoped_abs;
self
}
}