blackship 0.2.1

A FreeBSD jail manager with state machine control
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
//! Running commands inside a container: `jail_attach(2)` between fork and
//! exec, output multiplexed with poll(2) on the calling thread.

// FFI module: jail_attach(2) in pre_exec, process groups, poll(2).
#![allow(unsafe_code)]

use crate::error::{Error, Result};
use std::ffi::CString;
use std::fmt;
use std::io::{self, Read};
use std::os::fd::AsRawFd;
use std::os::unix::process::{CommandExt, ExitStatusExt};
use std::process::{Child, ChildStderr, ChildStdout, Command, Stdio};
use std::time::{Duration, Instant};

/// `PATH` inside the container unless the caller sets one.
const DEFAULT_PATH: &str = "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin";

/// Longest single wait in the supervision loop, so exits are noticed promptly.
const TICK: Duration = Duration::from_millis(50);

/// How long output is still drained after exit, for background holders of the pipes.
const DRAIN_GRACE: Duration = Duration::from_millis(500);

/// Which pipe a chunk of output came from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Stream {
    /// Standard output.
    Stdout,
    /// Standard error.
    Stderr,
}

/// A streaming output callback.
type Sink<'a> = Box<dyn FnMut(Stream, &[u8]) + 'a>;

enum Output<'a> {
    Capture,
    Stream(Sink<'a>),
    Inherit,
}

enum Limit {
    Timeout(Duration),
    Deadline(Instant),
}

/// A command to run in a container.
///
/// ```
/// use blackship::{Exec, Stream};
/// use std::time::Duration;
///
/// let mut log = Vec::new();
/// let step = Exec::shell("cargo test")
///     .cwd("/work")
///     .env("CARGO_TERM_COLOR", "never")
///     .timeout(Duration::from_secs(600))
///     .on_output(|stream, bytes| {
///         if stream == Stream::Stdout {
///             log.extend_from_slice(bytes);
///         }
///     });
/// # drop(step);
/// ```
///
/// Runs as root with a clean environment and stdin on `/dev/null`.
pub struct Exec<'a> {
    argv: Vec<String>,
    env: Vec<(String, String)>,
    cwd: Option<String>,
    limit: Option<Limit>,
    output: Output<'a>,
}

impl<'a> Exec<'a> {
    /// Run `argv` directly; `argv[0]` is looked up on the container's `PATH`.
    pub fn argv<I, S>(argv: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            argv: argv.into_iter().map(Into::into).collect(),
            env: Vec::new(),
            cwd: None,
            limit: None,
            output: Output::Capture,
        }
    }

    /// Run `script` with `/bin/sh -c`.
    pub fn shell(script: impl Into<String>) -> Self {
        Self::argv(["/bin/sh".to_string(), "-c".to_string(), script.into()])
    }

    /// Set an environment variable.
    #[must_use]
    pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.env.push((key.into(), value.into()));
        self
    }

    /// Set several environment variables.
    #[must_use]
    pub fn envs<I, K, V>(mut self, vars: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<String>,
    {
        self.env
            .extend(vars.into_iter().map(|(k, v)| (k.into(), v.into())));
        self
    }

    /// Working directory inside the container; `/` by default.
    #[must_use]
    pub fn cwd(mut self, dir: impl Into<String>) -> Self {
        self.cwd = Some(dir.into());
        self
    }

    /// Give up after `timeout`, counted from when the command starts.
    #[must_use]
    pub const fn timeout(mut self, timeout: Duration) -> Self {
        self.limit = Some(Limit::Timeout(timeout));
        self
    }

    /// Give up at `deadline`, for a budget shared across several commands.
    #[must_use]
    pub const fn deadline(mut self, deadline: Instant) -> Self {
        self.limit = Some(Limit::Deadline(deadline));
        self
    }

    /// Hand output to `sink` as it arrives instead of capturing it.
    #[must_use]
    pub fn on_output(mut self, sink: impl FnMut(Stream, &[u8]) + 'a) -> Self {
        self.output = Output::Stream(Box::new(sink));
        self
    }

    /// Share this process's stdin, stdout and stderr, for interactive use.
    #[must_use]
    pub fn inherit_stdio(mut self) -> Self {
        self.output = Output::Inherit;
        self
    }
}

impl fmt::Debug for Exec<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let output = match self.output {
            Output::Capture => "capture",
            Output::Stream(_) => "stream",
            Output::Inherit => "inherit",
        };
        f.debug_struct("Exec")
            .field("argv", &self.argv)
            .field("env", &self.env)
            .field("cwd", &self.cwd)
            .field("output", &output)
            .finish_non_exhaustive()
    }
}

/// How a command ended.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Exit {
    /// Exited with this status code.
    Code(i32),
    /// Killed by this signal.
    Signal(i32),
    /// Ran past its timeout or deadline; everything in the container was killed.
    TimedOut,
}

impl Exit {
    /// Whether the command exited zero.
    #[must_use]
    pub fn success(self) -> bool {
        self == Self::Code(0)
    }

    /// The exit code, when the command exited on its own.
    #[must_use]
    pub const fn code(self) -> Option<i32> {
        match self {
            Self::Code(code) => Some(code),
            _ => None,
        }
    }
}

/// What a finished command left behind.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExecOutput {
    /// How it ended.
    pub exit: Exit,
    /// Captured stdout; empty when streamed or inherited.
    pub stdout: Vec<u8>,
    /// Captured stderr; empty when streamed or inherited.
    pub stderr: Vec<u8>,
    /// Wall-clock time from start to exit.
    pub duration: Duration,
}

/// Run `exec` inside jail `jid`.
pub fn run(jid: i32, exec: Exec<'_>) -> Result<ExecOutput> {
    let Exec {
        argv,
        env,
        cwd,
        limit,
        mut output,
    } = exec;
    let Some(program) = argv.first() else {
        return Err(Error::InvalidArgument("exec needs a command".into()));
    };
    let cwd = CString::new(cwd.as_deref().unwrap_or("/"))?;
    let inherit = matches!(output, Output::Inherit);

    let mut cmd = Command::new(program);
    cmd.args(&argv[1..])
        .env_clear()
        .env("PATH", DEFAULT_PATH)
        .env("HOME", "/root")
        .env("USER", "root")
        .env("SHELL", "/bin/sh");
    if inherit {
        if let Some(term) = std::env::var_os("TERM") {
            cmd.env("TERM", term);
        }
        cmd.stdin(Stdio::inherit())
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit());
    } else {
        // Own process group so a timeout can kill the whole tree at once.
        cmd.process_group(0)
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
    }
    cmd.envs(env);

    // SAFETY: only async-signal-safe syscalls on memory allocated before fork.
    unsafe {
        cmd.pre_exec(move || {
            if libc::jail_attach(jid) != 0 {
                return Err(io::Error::last_os_error());
            }
            if libc::chdir(cwd.as_ptr()) != 0 {
                return Err(io::Error::last_os_error());
            }
            crate::proc::harden_fds();
            Ok(())
        });
    }

    let started = Instant::now();
    let deadline = limit.map(|limit| match limit {
        Limit::Timeout(timeout) => started + timeout,
        Limit::Deadline(deadline) => deadline,
    });
    let mut child = cmd
        .spawn()
        .map_err(|e| Error::JailExecFailed(format!("cannot run '{program}' in jail {jid}: {e}")))?;

    let mut stdout = Vec::new();
    let mut stderr = Vec::new();
    let status = if inherit {
        wait(&mut child, jid, deadline)?
    } else {
        let mut sink = |stream: Stream, bytes: &[u8]| match &mut output {
            Output::Stream(callback) => callback(stream, bytes),
            _ => match stream {
                Stream::Stdout => stdout.extend_from_slice(bytes),
                Stream::Stderr => stderr.extend_from_slice(bytes),
            },
        };
        supervise(&mut child, jid, deadline, &mut sink)?
    };

    let exit = match status {
        Waited::TimedOut => Exit::TimedOut,
        Waited::Exited(status) => match (status.code(), status.signal()) {
            (Some(code), _) => Exit::Code(code),
            (None, Some(signal)) => Exit::Signal(signal),
            (None, None) => Exit::Code(-1),
        },
    };
    Ok(ExecOutput {
        exit,
        stdout,
        stderr,
        duration: started.elapsed(),
    })
}

enum Waited {
    Exited(std::process::ExitStatus),
    TimedOut,
}

/// Pump both pipes into `sink` until they close, the command exits, or the deadline.
fn supervise(
    child: &mut Child,
    jid: i32,
    deadline: Option<Instant>,
    sink: &mut dyn FnMut(Stream, &[u8]),
) -> Result<Waited> {
    let mut pipes = Pipes {
        stdout: child.stdout.take(),
        stderr: child.stderr.take(),
    };
    let mut exited = None;
    let mut drain_until: Option<Instant> = None;

    while pipes.any_open() {
        let now = Instant::now();
        if exited.is_none() {
            if let Some(status) = child.try_wait()? {
                exited = Some(status);
                drain_until = Some(now + DRAIN_GRACE);
            } else if deadline.is_some_and(|d| now >= d) {
                kill_everything(child, jid);
                return Ok(Waited::TimedOut);
            }
        }
        if drain_until.is_some_and(|d| now >= d) {
            break;
        }

        let wake = [deadline.filter(|_| exited.is_none()), drain_until]
            .into_iter()
            .flatten()
            .map(|at| at.saturating_duration_since(now))
            .fold(TICK, Duration::min);
        pipes.pump(wake, sink)?;
    }
    drop(pipes);

    match exited {
        Some(status) => Ok(Waited::Exited(status)),
        None => wait(child, jid, deadline),
    }
}

/// Wait for the command alone, honouring the deadline.
fn wait(child: &mut Child, jid: i32, deadline: Option<Instant>) -> Result<Waited> {
    let Some(deadline) = deadline else {
        return Ok(Waited::Exited(child.wait()?));
    };
    loop {
        if let Some(status) = child.try_wait()? {
            return Ok(Waited::Exited(status));
        }
        let now = Instant::now();
        if now >= deadline {
            kill_everything(child, jid);
            return Ok(Waited::TimedOut);
        }
        std::thread::sleep(TICK.min(deadline - now));
    }
}

/// Kill the command's process group, then everything in the jail, and reap it.
fn kill_everything(child: &mut Child, jid: i32) {
    if let Ok(pgid) = libc::pid_t::try_from(child.id()) {
        // SAFETY: negative pid signals the command's own process group.
        unsafe { libc::kill(-pgid, libc::SIGKILL) };
    }
    let _ = child.kill(); // usually already dead from the group SIGKILL
    kill_jail_processes(jid);
    let _ = child.wait(); // reap only; the exit status of a killed child is moot
}

/// SIGKILL everything inside jail `jid`, sweeping until it is empty.
pub fn kill_jail_processes(jid: i32) {
    let jid = jid.to_string();
    for _ in 0..50 {
        // Best-effort; nothing may be left.
        let _ = Command::new("/bin/pkill")
            .args(["-KILL", "-j", &jid])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status();
        let survivors = Command::new("/bin/pgrep")
            .args(["-j", &jid])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status();
        // pgrep exits 1 when nothing matched; anything else means look again.
        if survivors.map_or(true, |s| s.code() == Some(1)) {
            return;
        }
        std::thread::sleep(Duration::from_millis(20));
    }
}

struct Pipes {
    stdout: Option<ChildStdout>,
    stderr: Option<ChildStderr>,
}

impl Pipes {
    const fn any_open(&self) -> bool {
        self.stdout.is_some() || self.stderr.is_some()
    }

    /// Wait up to `wait`, then read one chunk from each ready pipe.
    fn pump(&mut self, wait: Duration, sink: &mut dyn FnMut(Stream, &[u8])) -> Result<()> {
        let mut fds = Vec::with_capacity(2);
        if let Some(out) = &self.stdout {
            fds.push(pollfd(out.as_raw_fd()));
        }
        if let Some(err) = &self.stderr {
            fds.push(pollfd(err.as_raw_fd()));
        }
        let timeout = libc::c_int::try_from(wait.as_millis()).unwrap_or(libc::c_int::MAX);
        let nfds = libc::nfds_t::try_from(fds.len()).unwrap_or(0);
        // SAFETY: `fds` is a live, correctly sized array of pollfd.
        let ready = unsafe { libc::poll(fds.as_mut_ptr(), nfds, timeout) };
        if ready < 0 {
            let err = io::Error::last_os_error();
            return if err.kind() == io::ErrorKind::Interrupted {
                Ok(())
            } else {
                Err(err.into())
            };
        }

        let mut buf = [0u8; 16 * 1024];
        for pfd in fds {
            if pfd.revents == 0 {
                continue;
            }
            if self
                .stdout
                .as_ref()
                .is_some_and(|p| p.as_raw_fd() == pfd.fd)
            {
                read_chunk(&mut self.stdout, Stream::Stdout, &mut buf, sink);
            } else {
                read_chunk(&mut self.stderr, Stream::Stderr, &mut buf, sink);
            }
        }
        Ok(())
    }
}

const fn pollfd(fd: libc::c_int) -> libc::pollfd {
    libc::pollfd {
        fd,
        events: libc::POLLIN,
        revents: 0,
    }
}

/// One read from a pipe poll(2) marked ready; EOF or an error closes it.
fn read_chunk<R: Read>(
    pipe: &mut Option<R>,
    stream: Stream,
    buf: &mut [u8],
    sink: &mut dyn FnMut(Stream, &[u8]),
) {
    let Some(reader) = pipe else {
        return;
    };
    match reader.read(buf) {
        Ok(0) => *pipe = None,
        Ok(n) => sink(stream, &buf[..n]),
        Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
        Err(_) => *pipe = None,
    }
}

#[cfg(test)]
mod tests;