sbox 0.2.4

Tiny Linux containers implementation
Documentation
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
use std::convert::Infallible;
use std::ffi::CString;
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd, RawFd};
use std::panic::catch_unwind;
use std::path::PathBuf;

use nix::fcntl::OFlag;
use nix::sched::CloneFlags;
use nix::sys::wait::{waitpid, WaitPidFlag};
use nix::unistd::{chdir, dup2, execvpe, fork, sethostname, ForkResult, Gid, Pid, Uid};
use nix::NixPath;

use crate::{
    clone3, close_exec_from, exit_child, new_pipe, pidfd_open, read_ok, read_pid, read_result,
    setup_mount_namespace, write_ok, write_pid, write_result, CloneArgs, CloneResult, Container,
    Error, NetworkHandle, OwnedPid,
};

pub type Signal = nix::sys::signal::Signal;
pub type WaitStatus = nix::sys::wait::WaitStatus;

#[derive(Debug, Default)]
pub struct InitProcessOptions {
    command: Vec<String>,
    environ: Vec<String>,
    work_dir: PathBuf,
    uid: Option<Uid>,
    gid: Option<Gid>,
    cgroup: PathBuf,
    stdin: Option<OwnedFd>,
    stdout: Option<OwnedFd>,
    stderr: Option<OwnedFd>,
}

impl InitProcessOptions {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn command(mut self, command: Vec<String>) -> Self {
        self.command = command;
        self
    }

    pub fn environ(mut self, environ: Vec<String>) -> Self {
        self.environ = environ;
        self
    }

    pub fn work_dir(mut self, work_dir: PathBuf) -> Self {
        self.work_dir = work_dir;
        self
    }

    pub fn user(mut self, uid: Uid, gid: Gid) -> Self {
        self.uid = Some(uid);
        self.gid = Some(gid);
        self
    }

    pub fn cgroup(mut self, cgroup: impl Into<PathBuf>) -> Self {
        self.cgroup = cgroup.into();
        self
    }

    pub fn stdin(mut self, fd: impl Into<OwnedFd>) -> Self {
        self.stdin = Some(fd.into());
        self
    }

    pub fn stdout(mut self, fd: impl Into<OwnedFd>) -> Self {
        self.stdout = Some(fd.into());
        self
    }

    pub fn stderr(mut self, fd: impl Into<OwnedFd>) -> Self {
        self.stderr = Some(fd.into());
        self
    }

    pub fn start(self, container: &Container) -> Result<InitProcess, Error> {
        let uid = self.uid.unwrap_or(Uid::from(0));
        if !container.user_mapper.is_uid_mapped(uid) {
            return Err(format!("User {} is not mapped", uid).into());
        }
        let gid = self.gid.unwrap_or(Gid::from(0));
        if !container.user_mapper.is_gid_mapped(gid) {
            return Err(format!("Group {} is not mapped", gid).into());
        }
        let work_dir = if !self.work_dir.is_empty() {
            self.work_dir
        } else {
            "/".into()
        };
        let command = self.command;
        let environ = self.environ;
        let cgroup = if self.cgroup.is_empty() {
            None
        } else {
            let cgroup = container.cgroup.child(self.cgroup)?;
            cgroup.create()?;
            Some(cgroup)
        };
        let stdin = self.stdin;
        let stdout = self.stdout;
        let stderr = self.stderr;
        let dev_null = if stdin.is_none() || stdout.is_none() || stderr.is_none() {
            let raw_fd =
                nix::fcntl::open("/dev/null", OFlag::O_RDWR, nix::sys::stat::Mode::empty())?;
            Some(unsafe { OwnedFd::from_raw_fd(raw_fd) })
        } else {
            None
        };
        let cgroup_file = container.cgroup.open()?;
        let pipe = new_pipe()?;
        let child_pipe = new_pipe()?;
        let mut clone_args = CloneArgs::default();
        clone_args.flag_newuser();
        clone_args.flag_newns();
        clone_args.flag_newpid();
        clone_args.flag_newnet();
        clone_args.flag_newipc();
        clone_args.flag_newuts();
        clone_args.flag_newtime();
        clone_args.flag_newcgroup();
        clone_args.flag_into_cgroup(&cgroup_file);
        match unsafe { clone3(&clone_args) }
            .map_err(|v| format!("Cannot start init process: {v}"))?
        {
            CloneResult::Child => {
                let _ = catch_unwind(move || {
                    drop(cgroup_file);
                    let rx = pipe.rx();
                    let tx = child_pipe.tx();
                    exit_child(move || -> Result<Infallible, Error> {
                        // Await parent process is initialized pid.
                        read_ok(rx)?;
                        // Unlock parent process.
                        write_result(
                            tx,
                            move || -> Result<(), Error> {
                                // Setup mount namespace.
                                setup_mount_namespace(container)
                                    .map_err(|v| format!("Cannot setup mount namespace: {v}"))?;
                                // Setup uts namespace.
                                sethostname(&container.hostname)
                                    .map_err(|v| format!("Cannot setup hostname: {v}"))?;
                                // Setup network.
                                if let Some(v) = &container.network_manager {
                                    v.set_network()?;
                                }
                                // Setup stdio.
                                dup2(
                                    stdin.as_ref().or(dev_null.as_ref()).unwrap().as_raw_fd(),
                                    RawFd::from(0),
                                )?;
                                dup2(
                                    stdout.as_ref().or(dev_null.as_ref()).unwrap().as_raw_fd(),
                                    RawFd::from(1),
                                )?;
                                dup2(
                                    stderr.as_ref().or(dev_null.as_ref()).unwrap().as_raw_fd(),
                                    RawFd::from(2),
                                )?;
                                // Close file descriptors.
                                close_exec_from(3)?;
                                // Setup workdir.
                                chdir(&work_dir)
                                    .map_err(|v| format!("Cannot change directory: {v}"))?;
                                // Setup user.
                                container
                                    .user_mapper
                                    .set_user(uid, gid)
                                    .map_err(|v| format!("Cannot set current user: {v}"))?;
                                Ok(())
                            }(),
                        )??;
                        // Prepare exec arguments.
                        let filename = CString::new(command[0].as_bytes())?;
                        let argv = Result::<Vec<_>, _>::from_iter(
                            command.iter().map(|v| CString::new(v.as_bytes())),
                        )?;
                        let envp = Result::<Vec<_>, _>::from_iter(
                            environ.iter().map(|v| CString::new(v.as_bytes())),
                        )?;
                        // Run process.
                        Ok(execvpe(&filename, &argv, &envp)?)
                    }())
                });
                unsafe { nix::libc::_exit(2) }
            }
            CloneResult::Parent { child } => {
                let child = unsafe { OwnedPid::from_raw(child) };
                // Close cgroup file descriptor.
                drop(cgroup_file);
                // Close stdio descriptors.
                drop(stdin);
                drop(stdout);
                drop(stderr);
                drop(dev_null);
                // Setup pipes.
                let rx = child_pipe.rx();
                let tx = pipe.tx();
                // Map user.
                container
                    .user_mapper
                    .run_map_user(child.as_raw())
                    .map_err(|v| format!("Cannot setup user namespace: {v}"))?;
                // Setup init cgroup.
                if let Some(cgroup) = cgroup {
                    cgroup
                        .add_process(child.as_raw())
                        .map_err(|v| format!("Cannot add process to cgroup: {v}"))?;
                }
                // Setup network namespace.
                let network_handle = match &container.network_manager {
                    Some(v) => v.run_network(child.as_raw())?,
                    None => None,
                };
                // Unlock child process.
                write_ok(tx)?;
                // Await child process result.
                read_result(rx)??;
                Ok(InitProcess {
                    pid: child.into_raw(),
                    _network_handle: network_handle,
                })
            }
        }
    }
}

pub struct InitProcess {
    pid: Pid,
    _network_handle: Option<Box<dyn NetworkHandle>>,
}

impl InitProcess {
    pub fn as_pid(&self) -> Pid {
        self.pid
    }

    pub fn wait(&mut self) -> Result<WaitStatus, Error> {
        Ok(waitpid(self.pid, Some(WaitPidFlag::__WALL))?)
    }

    pub fn options() -> InitProcessOptions {
        InitProcessOptions::new()
    }
}

#[derive(Debug, Default)]
pub struct ProcessOptions {
    command: Vec<String>,
    environ: Vec<String>,
    work_dir: PathBuf,
    uid: Option<Uid>,
    gid: Option<Gid>,
    cgroup: PathBuf,
    stdin: Option<OwnedFd>,
    stdout: Option<OwnedFd>,
    stderr: Option<OwnedFd>,
}

impl ProcessOptions {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn command(mut self, command: Vec<String>) -> Self {
        self.command = command;
        self
    }

    pub fn environ(mut self, environ: Vec<String>) -> Self {
        self.environ = environ;
        self
    }

    pub fn work_dir(mut self, work_dir: impl Into<PathBuf>) -> Self {
        self.work_dir = work_dir.into();
        self
    }

    pub fn user(mut self, uid: impl Into<Uid>, gid: impl Into<Gid>) -> Self {
        self.uid = Some(uid.into());
        self.gid = Some(gid.into());
        self
    }

    pub fn cgroup(mut self, cgroup: impl Into<PathBuf>) -> Self {
        self.cgroup = cgroup.into();
        self
    }

    pub fn stdin(mut self, fd: impl Into<OwnedFd>) -> Self {
        self.stdin = Some(fd.into());
        self
    }

    pub fn stdout(mut self, fd: impl Into<OwnedFd>) -> Self {
        self.stdout = Some(fd.into());
        self
    }

    pub fn stderr(mut self, fd: impl Into<OwnedFd>) -> Self {
        self.stderr = Some(fd.into());
        self
    }

    pub fn start(
        self,
        container: &Container,
        init_process: &InitProcess,
    ) -> Result<Process, Error> {
        let uid = self.uid.unwrap_or(Uid::from(0));
        if !container.user_mapper.is_uid_mapped(uid) {
            return Err(format!("User {} is not mapped", uid).into());
        }
        let gid = self.gid.unwrap_or(Gid::from(0));
        if !container.user_mapper.is_gid_mapped(gid) {
            return Err(format!("Group {} is not mapped", gid).into());
        }
        let work_dir = if !self.work_dir.is_empty() {
            self.work_dir
        } else {
            "/".into()
        };
        let cgroup = if self.cgroup.is_empty() {
            None
        } else {
            let cgroup = container.cgroup.child(self.cgroup)?;
            cgroup.create()?;
            Some(cgroup)
        };
        let command = self.command;
        let environ = self.environ;
        let stdin = self.stdin;
        let stdout = self.stdout;
        let stderr = self.stderr;
        let dev_null = if stdin.is_none() || stdout.is_none() || stderr.is_none() {
            let raw_fd =
                nix::fcntl::open("/dev/null", OFlag::O_RDWR, nix::sys::stat::Mode::empty())?;
            Some(unsafe { OwnedFd::from_raw_fd(raw_fd) })
        } else {
            None
        };
        let pid_pipe = new_pipe()?;
        match unsafe { fork() }? {
            ForkResult::Child => {
                let _ = catch_unwind(move || -> Result<(), Error> {
                    let pid_tx = pid_pipe.tx();
                    let cgroup_file = match cgroup {
                        Some(v) => v.open(),
                        None => container.cgroup.open(),
                    }?;
                    // Enter namespaces.
                    let pidfd = pidfd_open(init_process.pid)?;
                    let flags = CloneFlags::CLONE_NEWUSER
                        | CloneFlags::CLONE_NEWNS
                        | CloneFlags::CLONE_NEWPID
                        | CloneFlags::CLONE_NEWNET
                        | CloneFlags::CLONE_NEWIPC
                        | CloneFlags::CLONE_NEWUTS
                        | CloneFlags::from_bits_retain(nix::libc::CLONE_NEWTIME);
                    nix::sched::setns(&pidfd, flags)
                        .map_err(|v| format!("Cannot enter init namespaces: {v}"))?;
                    let pipe = new_pipe()?;
                    let mut clone_args = CloneArgs::default();
                    clone_args.flag_parent();
                    clone_args.flag_into_cgroup(&cgroup_file);
                    match unsafe { clone3(&clone_args) }? {
                        CloneResult::Child => {
                            let _ = catch_unwind(move || -> Result<Infallible, Error> {
                                drop(cgroup_file);
                                drop(pid_tx);
                                let tx = pipe.tx();
                                // Unlock parent process.
                                write_result(
                                    tx,
                                    move || -> Result<(), Error> {
                                        // Setup cgroup namespace.
                                        nix::sched::setns(pidfd, CloneFlags::CLONE_NEWCGROUP)
                                            .map_err(|v| {
                                                format!("Cannot enter cgroup namespace: {v}")
                                            })?;
                                        // Setup stdio.
                                        dup2(
                                            stdin
                                                .as_ref()
                                                .or(dev_null.as_ref())
                                                .unwrap()
                                                .as_raw_fd(),
                                            RawFd::from(0),
                                        )?;
                                        dup2(
                                            stdout
                                                .as_ref()
                                                .or(dev_null.as_ref())
                                                .unwrap()
                                                .as_raw_fd(),
                                            RawFd::from(1),
                                        )?;
                                        dup2(
                                            stderr
                                                .as_ref()
                                                .or(dev_null.as_ref())
                                                .unwrap()
                                                .as_raw_fd(),
                                            RawFd::from(2),
                                        )?;
                                        // Close file descriptors.
                                        close_exec_from(3)?;
                                        // Setup workdir.
                                        chdir(&work_dir).map_err(|v| {
                                            format!("Cannot change work directory: {v}")
                                        })?;
                                        // Setup user.
                                        container.user_mapper.set_user(uid, gid)
                                    }(),
                                )??;
                                // Prepare exec arguments.
                                let filename = CString::new(command[0].as_bytes())?;
                                let argv = Result::<Vec<_>, _>::from_iter(
                                    command.iter().map(|v| CString::new(v.as_bytes())),
                                )?;
                                let envp = Result::<Vec<_>, _>::from_iter(
                                    environ.iter().map(|v| CString::new(v.as_bytes())),
                                )?;
                                // Run process.
                                Ok(execvpe(&filename, &argv, &envp)?)
                            });
                            unsafe { nix::libc::_exit(2) }
                        }
                        CloneResult::Parent { child } => {
                            exit_child(move || -> Result<(), Error> {
                                // Close stdio descriptors.
                                drop(stdin);
                                drop(stdout);
                                drop(stderr);
                                drop(dev_null);
                                // Send child pid to parent process.
                                write_pid(pid_tx, child)?;
                                // Await child process is started.
                                read_result(pipe.rx())?
                            }())
                        }
                    }
                });
                unsafe { nix::libc::_exit(2) }
            }
            ForkResult::Parent { child } => {
                let child = unsafe { OwnedPid::from_raw(child) };
                // Close stdio descriptors.
                drop(stdin);
                drop(stdout);
                drop(stderr);
                drop(dev_null);
                // Setup pipes.
                let rx = pid_pipe.rx();
                // Read subchild pid.
                let sibling = unsafe { OwnedPid::from_raw(read_pid(rx)?) };
                // Wait for child exit.
                child.wait_success()?;
                // Return process.
                Ok(Process {
                    pid: sibling.into_raw(),
                })
            }
        }
    }
}

pub struct Process {
    pid: Pid,
}

impl Process {
    pub fn as_pid(&self) -> Pid {
        self.pid
    }

    pub fn wait(&mut self) -> Result<WaitStatus, Error> {
        Ok(waitpid(self.pid, Some(WaitPidFlag::__WALL))?)
    }

    pub fn options() -> ProcessOptions {
        ProcessOptions::new()
    }
}