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
#![deny(missing_docs)]
use std::path::Path;
use std::path::PathBuf;
use std::str;
use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
use base::*;
use libc::c_ulong;
use minijail::Minijail;
use once_cell::sync::Lazy;
use crate::crosvm::config::JailConfig;
static EMBEDDED_BPFS: Lazy<std::collections::HashMap<&str, Vec<u8>>> =
Lazy::new(|| include!(concat!(env!("OUT_DIR"), "/bpf_includes.in")));
pub const MAX_OPEN_FILES_DEFAULT: u64 = 1024;
const MAX_OPEN_FILES_FOR_GPU: u64 = 32768;
pub(super) enum RunAsUser {
Unspecified,
CurrentUser,
#[cfg(all(feature = "gpu", feature = "virgl_renderer_next"))]
Root,
}
pub(super) struct SandboxConfig<'a> {
pub(super) limit_caps: bool,
log_failures: bool,
seccomp_policy_path: Option<PathBuf>,
seccomp_policy_name: &'a str,
pub(super) ugid_map: Option<(&'a str, &'a str)>,
pub(super) remount_mode: Option<c_ulong>,
pub(super) bind_mounts: bool,
pub(super) run_as: RunAsUser,
}
impl<'a> SandboxConfig<'a> {
pub(super) fn new(jail_config: &JailConfig, policy: &'a str) -> Self {
let policy_path = jail_config
.seccomp_policy_dir
.as_ref()
.map(|dir| dir.join(policy));
Self {
limit_caps: true,
log_failures: jail_config.seccomp_log_failures,
seccomp_policy_path: policy_path,
seccomp_policy_name: policy,
ugid_map: None,
remount_mode: None,
bind_mounts: false,
run_as: RunAsUser::Unspecified,
}
}
}
pub(crate) struct ScopedMinijail(pub Minijail);
impl Drop for ScopedMinijail {
fn drop(&mut self) {
let _ = self.0.kill();
}
}
pub(super) fn create_base_minijail(root: &Path, max_open_files: u64) -> Result<Minijail> {
if !root.is_dir() {
bail!("{:?} is not a directory, cannot create jail", root);
}
let mut jail = Minijail::new().context("failed to jail device")?;
if root != Path::new("/") {
jail.namespace_vfs();
jail.enter_pivot_root(root)
.context("failed to pivot root device")?;
}
jail.set_rlimit(libc::RLIMIT_NOFILE as i32, max_open_files, max_open_files)
.context("error setting max open files")?;
Ok(jail)
}
pub(super) fn create_sandbox_minijail(
root: &Path,
max_open_files: u64,
config: &SandboxConfig,
) -> Result<Minijail> {
let mut jail = create_base_minijail(root, max_open_files)?;
jail.namespace_pids();
jail.namespace_user();
jail.namespace_user_disable_setgroups();
if config.limit_caps {
jail.use_caps(0);
}
match config.run_as {
RunAsUser::Unspecified => {
if config.bind_mounts && config.ugid_map.is_none() {
add_current_user_to_jail(&mut jail)?;
}
}
RunAsUser::CurrentUser => {
add_current_user_to_jail(&mut jail)?;
}
#[cfg(all(feature = "gpu", feature = "virgl_renderer_next"))]
RunAsUser::Root => {
let crosvm_uid = geteuid();
let crosvm_gid = getegid();
jail.uidmap(&format!("0 {0} 1", crosvm_uid))
.context("error setting UID map")?;
jail.gidmap(&format!("0 {0} 1", crosvm_gid))
.context("error setting GID map")?;
}
}
if config.bind_mounts {
jail.mount_with_data(
Path::new("none"),
Path::new("/"),
"tmpfs",
(libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC) as usize,
"size=67108864",
)?;
}
if let Some((uid_map, gid_map)) = config.ugid_map {
jail.uidmap(uid_map).context("error setting UID map")?;
jail.gidmap(gid_map).context("error setting GID map")?;
}
jail.namespace_vfs();
jail.namespace_net();
jail.no_new_privs();
if let Some(seccomp_policy_path) = &config.seccomp_policy_path {
let bpf_policy_file = seccomp_policy_path.with_extension("bpf");
if bpf_policy_file.exists() && !config.log_failures {
jail.parse_seccomp_program(&bpf_policy_file)
.with_context(|| {
format!(
"failed to parse precompiled seccomp policy: {}",
bpf_policy_file.display()
)
})?;
} else {
jail.set_seccomp_filter_tsync();
if config.log_failures {
jail.log_seccomp_filter_failures();
}
let bpf_policy_file = seccomp_policy_path.with_extension("policy");
jail.parse_seccomp_filters(&bpf_policy_file)
.with_context(|| {
format!(
"failed to parse seccomp policy: {}",
bpf_policy_file.display()
)
})?;
}
} else {
let bpf_program = EMBEDDED_BPFS
.get(&config.seccomp_policy_name)
.with_context(|| {
format!(
"failed to find embedded seccomp policy: {}",
&config.seccomp_policy_name
)
})?;
jail.parse_seccomp_bytes(bpf_program).with_context(|| {
format!(
"failed to parse embedded seccomp policy: {}",
&config.seccomp_policy_name
)
})?;
}
jail.use_seccomp_filter();
jail.run_as_init();
if let Some(mode) = config.remount_mode {
jail.set_remount_mode(mode);
}
Ok(jail)
}
pub(super) fn simple_jail(
jail_config: &Option<JailConfig>,
policy: &str,
) -> Result<Option<Minijail>> {
if let Some(jail_config) = jail_config {
let config = SandboxConfig::new(jail_config, policy);
Ok(Some(create_sandbox_minijail(
&jail_config.pivot_root,
MAX_OPEN_FILES_DEFAULT,
&config,
)?))
} else {
Ok(None)
}
}
pub(super) fn create_gpu_minijail(root: &Path, config: &SandboxConfig) -> Result<Minijail> {
let mut jail = create_sandbox_minijail(root, MAX_OPEN_FILES_FOR_GPU, config)?;
let sys_dev_char_path = Path::new("/sys/dev/char");
jail.mount_bind(sys_dev_char_path, sys_dev_char_path, false)?;
let sys_devices_path = Path::new("/sys/devices");
jail.mount_bind(sys_devices_path, sys_devices_path, false)?;
let drm_dri_path = Path::new("/dev/dri");
if drm_dri_path.exists() {
jail.mount_bind(drm_dri_path, drm_dri_path, false)?;
}
let mali0_path = Path::new("/dev/mali0");
if mali0_path.exists() {
jail.mount_bind(mali0_path, mali0_path, true)?;
}
let pvr_sync_path = Path::new("/dev/pvr_sync");
if pvr_sync_path.exists() {
jail.mount_bind(pvr_sync_path, pvr_sync_path, true)?;
}
let udmabuf_path = Path::new("/dev/udmabuf");
if udmabuf_path.exists() {
jail.mount_bind(udmabuf_path, udmabuf_path, true)?;
}
jail_mount_bind_if_exists(
&mut jail,
&[
"/usr/lib",
"/usr/lib64",
"/lib",
"/lib64",
"/usr/share/drirc.d",
"/usr/share/glvnd",
"/usr/share/vulkan",
],
)?;
let proc_path = Path::new("/proc");
jail.mount(
proc_path,
proc_path,
"proc",
(libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC | libc::MS_RDONLY) as usize,
)?;
let perfetto_path = Path::new("/run/perfetto");
if perfetto_path.exists() {
jail.mount_bind(perfetto_path, perfetto_path, true)?;
}
Ok(jail)
}
pub(super) fn jail_mount_bind_if_exists<P: AsRef<std::ffi::OsStr>>(
jail: &mut Minijail,
dirs: &[P],
) -> Result<()> {
for dir in dirs {
let dir_path = Path::new(dir);
if dir_path.exists() {
jail.mount_bind(dir_path, dir_path, false)?;
}
}
Ok(())
}
fn add_current_user_to_jail(jail: &mut Minijail) -> Result<()> {
let crosvm_uid = geteuid();
let crosvm_gid = getegid();
jail.uidmap(&format!("{0} {0} 1", crosvm_uid))
.context("error setting UID map")?;
jail.gidmap(&format!("{0} {0} 1", crosvm_gid))
.context("error setting GID map")?;
if crosvm_uid != 0 {
jail.change_uid(crosvm_uid);
}
if crosvm_gid != 0 {
jail.change_gid(crosvm_gid);
}
Ok(())
}