tino 0.1.24

tino: tiny init process (PID 1) for Docker/Kubernetes containers, written in Rust (tini alternative)
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
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
583
584
585
586
587
use crate::{Context, Error, Result, bail, cli::Cli, logging};
use libc::{_exit, PR_SET_CHILD_SUBREAPER, PR_SET_PDEATHSIG};
use std::{env, ffi::CString};

use super::landlock;
use super::signals;
use super::sys::{Errno, ForkResult, Pid, SigSet, exec_program, fork_process, process_group_of, set_process_group};

#[derive(Default)]
pub(super) struct PrctlOutcome {
    pub subreaper_enabled: bool,
    pub pdeath_set: bool,
}

pub(super) fn configure_prctl(cli: &Cli) -> Result<PrctlOutcome> {
    let mut outcome = PrctlOutcome::default();
    if let Some(sig_name) = &cli.pdeath {
        let sig = signals::signal_by_name(sig_name).ok_or_else(|| {
            Error::msg(format!(
                "invalid signal '{}'; supported values align with `tino --help`",
                sig_name
            ))
        })?;
        // SAFETY: `sig` is a valid signal number and `prctl` is called with documented parameters.
        unsafe {
            if libc::prctl(PR_SET_PDEATHSIG, sig as i32) == -1 {
                bail!("prctl P_DEATHSIG: {}", Errno::last());
            }
        }
        outcome.pdeath_set = true;
    }
    if cli.subreaper {
        // SAFETY: enabling the child subreaper flag is safe for the current process.
        unsafe {
            if libc::prctl(PR_SET_CHILD_SUBREAPER, 1) == -1 {
                let err = Errno::last();
                if err == Errno::EPERM {
                    logging::warn(format_args!(
                        "subreaper capability rejected; continuing without subreaper: {}",
                        err
                    ));
                } else {
                    bail!("prctl SUBREAPER: {}", err);
                }
            } else {
                outcome.subreaper_enabled = true;
            }
        }
    }
    Ok(outcome)
}

const MAX_ENV_EXPANSION_DEPTH: usize = 32;

pub(super) fn resolve_command_args(cmd: &[String], expand_env: bool) -> Result<Vec<String>> {
    if expand_env {
        expand_command_args(cmd)
    } else {
        Ok(cmd.to_vec())
    }
}

pub(super) fn prepare_command(cmd: &[String], expand_env: bool) -> Result<(CString, Vec<CString>)> {
    let args = resolve_command_args(cmd, expand_env)?;
    if args.is_empty() {
        bail!("missing CMD (use --help)");
    }

    let program = CString::new(args[0].as_str())
        .map_err(|_| Error::msg("command argument contains embedded NUL byte"))?;
    let argv = args
        .iter()
        .map(|s| {
            CString::new(s.as_str()).map_err(|_| Error::msg("command argument contains embedded NUL byte"))
        })
        .collect::<std::result::Result<Vec<_>, _>>()?;
    Ok((program, argv))
}

fn expand_command_args(cmd: &[String]) -> Result<Vec<String>> {
    cmd.iter().map(|arg| expand_command_arg(arg)).collect()
}

fn expand_command_arg(arg: &str) -> Result<String> {
    expand_command_arg_with_depth(arg, 0)
        .with_context(|| format!("expand environment references in command argument '{arg}'"))
}

fn expand_command_arg_with_depth(arg: &str, depth: usize) -> Result<String> {
    if depth > MAX_ENV_EXPANSION_DEPTH {
        bail!(
            "environment expansion nesting exceeds {} levels",
            MAX_ENV_EXPANSION_DEPTH
        );
    }

    let mut expanded = String::with_capacity(arg.len());
    let mut idx = 0;
    let bytes = arg.as_bytes();

    while idx < arg.len() {
        let Some(offset) = arg[idx..].find('$') else {
            expanded.push_str(&arg[idx..]);
            break;
        };
        let dollar = idx + offset;
        expanded.push_str(&arg[idx..dollar]);

        if dollar + 1 >= arg.len() {
            expanded.push('$');
            break;
        }

        match bytes[dollar + 1] {
            b'$' => {
                expanded.push('$');
                idx = dollar + 2;
            }
            b'{' => {
                let closing = find_matching_brace(arg, dollar + 2)?;
                let body = &arg[dollar + 2..closing];
                expanded.push_str(&expand_braced_env(body, depth + 1)?);
                idx = closing + 1;
            }
            _ => {
                expanded.push('$');
                idx = dollar + 1;
            }
        }
    }

    Ok(expanded)
}

fn expand_braced_env(body: &str, depth: usize) -> Result<String> {
    if let Some((name, default)) = split_braced_default(body) {
        if !is_valid_env_name(name) {
            bail!("invalid environment variable name '{name}'");
        }
        resolve_env_value(name, Some(default), depth)
    } else if is_valid_env_name(body) {
        resolve_env_value(body, None, depth)
    } else {
        bail!("unsupported braced environment expansion '${{{body}}}'");
    }
}

fn resolve_env_value(name: &str, default: Option<&str>, depth: usize) -> Result<String> {
    match env::var(name) {
        Ok(value) if default.is_none() || !value.is_empty() => Ok(value),
        Ok(_) | Err(env::VarError::NotPresent) if let Some(fallback) = default => {
            expand_command_arg_with_depth(fallback, depth)
        }
        Ok(_) | Err(env::VarError::NotPresent) => Ok(String::new()),
        Err(env::VarError::NotUnicode(_)) => {
            bail!("environment variable '{name}' contains non-Unicode data")
        }
    }
}

fn find_matching_brace(arg: &str, mut idx: usize) -> Result<usize> {
    let bytes = arg.as_bytes();
    let mut depth = 1usize;

    while idx < arg.len() {
        if bytes[idx] == b'$' && idx + 1 < arg.len() && bytes[idx + 1] == b'{' {
            depth += 1;
            idx += 2;
            continue;
        }
        if bytes[idx] == b'}' {
            depth -= 1;
            if depth == 0 {
                return Ok(idx);
            }
        }
        idx += 1;
    }

    bail!("missing closing '}}'")
}

fn split_braced_default(body: &str) -> Option<(&str, &str)> {
    let bytes = body.as_bytes();
    let mut idx = 0;
    let mut depth = 0usize;

    while idx < body.len() {
        if bytes[idx] == b'$' && idx + 1 < body.len() && bytes[idx + 1] == b'{' {
            depth += 1;
            idx += 2;
            continue;
        }
        if bytes[idx] == b'}' && depth > 0 {
            depth -= 1;
            idx += 1;
            continue;
        }
        if depth == 0 && bytes[idx] == b':' && idx + 1 < body.len() && bytes[idx + 1] == b'-' {
            return Some((&body[..idx], &body[idx + 2..]));
        }
        idx += 1;
    }

    None
}

fn is_valid_env_name(name: &str) -> bool {
    let bytes = name.as_bytes();
    !bytes.is_empty()
        && is_env_name_start(bytes[0])
        && bytes[1..].iter().all(|byte| is_env_name_continue(*byte))
}

fn is_env_name_start(byte: u8) -> bool {
    byte == b'_' || byte.is_ascii_alphabetic()
}

fn is_env_name_continue(byte: u8) -> bool {
    byte == b'_' || byte.is_ascii_alphanumeric()
}

fn child_write(bytes: &[u8]) {
    unsafe {
        let _ = libc::write(
            libc::STDERR_FILENO,
            bytes.as_ptr() as *const libc::c_void,
            bytes.len(),
        );
    }
}

fn child_write_errno(errno: Errno) {
    child_write_u32(errno.raw() as u32);
}

fn child_write_u32(mut value: u32) {
    let mut buf = [0u8; 12];
    let mut idx = buf.len();
    if value == 0 {
        idx -= 1;
        buf[idx] = b'0';
    } else {
        while value > 0 {
            let digit = (value % 10) as u8;
            idx -= 1;
            buf[idx] = b'0' + digit;
            value /= 10;
        }
    }
    child_write(&buf[idx..]);
}

fn child_write_exec_failure_hint(errno: Errno) {
    match errno {
        Errno::ENOENT => {
            child_write(b": file not found; check the path or PATH lookup");
        }
        Errno::EACCES => {
            child_write(b": permission denied or file is not executable");
        }
        Errno::ENOEXEC => {
            child_write(b": file is not a recognized executable format");
        }
        Errno::ENOTDIR => {
            child_write(b": a path component is not a directory");
        }
        Errno::E2BIG => {
            child_write(b": argument list or environment is too large");
        }
        _ => {}
    }
}

fn report_exec_failure(program: &CString, errno: Errno) -> ! {
    std::hint::cold_path();
    child_write(b"tino: execvp failed for ");
    child_write(program.as_bytes());
    child_write_exec_failure_hint(errno);
    child_write(b" (errno ");
    child_write_errno(errno);
    child_write(b")\n");
    unsafe { _exit(127) }
}

fn claim_foreground_tty() {
    // SAFETY: `STDIN_FILENO` is a valid file descriptor constant and the libc calls are used
    // exactly as documented for best-effort tty foreground management.
    unsafe {
        let pgid = libc::getpgrp();
        let _ = libc::tcsetpgrp(libc::STDIN_FILENO, pgid);
    }
}

pub(super) fn spawn_child(
    child_mask: SigSet,
    landlock_config: Option<landlock::LandlockConfig>,
    cmd_c: &CString,
    argv_c: &[CString],
) -> Result<Pid> {
    // SAFETY: the forked child only performs async-signal-safe operations before exec or exit.
    match unsafe { fork_process()? } {
        ForkResult::Child => {
            if set_process_group(Pid::from_raw(0), Pid::from_raw(0)).is_err() {
                child_write(b"tino: failed to establish child process group\n");
            }
            claim_foreground_tty();
            if child_mask.thread_set_mask().is_err() {
                child_write(b"tino: failed to restore signal mask in child\n");
                unsafe { _exit(1) }
            }
            if let Some(config) = landlock_config.as_ref()
                && let Err(err) = landlock::apply(config)
            {
                report_landlock_failure(config.warn_only, err);
                if !config.warn_only {
                    unsafe { _exit(1) }
                }
            }
            match exec_program(cmd_c, argv_c) {
                Ok(_) => unsafe { _exit(127) },
                Err(err) => report_exec_failure(cmd_c, err),
            }
        }
        ForkResult::Parent { child } => Ok(child),
    }
}

fn report_landlock_failure(warn_only: bool, err: landlock::LandlockError<'_>) {
    std::hint::cold_path();
    if warn_only {
        child_write(b"tino: access restriction unavailable; continuing (backend landlock: ");
    } else {
        child_write(b"tino: access restriction failed (backend landlock: ");
    }
    match err {
        landlock::LandlockError::NotSupported => {
            child_write(b"not supported (kernel/LSM)");
        }
        landlock::LandlockError::AbiTooOld {
            feature,
            required_abi,
            current_abi,
        } => {
            child_write(feature.as_bytes());
            child_write(b" requires ABI ");
            child_write_u32(required_abi);
            child_write(b" but kernel reports ABI ");
            child_write_u32(current_abi);
        }
        landlock::LandlockError::QueryAbi(errno) => {
            child_write(b"query ABI errno ");
            child_write_errno(errno);
            child_write_seccomp_hint(errno);
        }
        landlock::LandlockError::CreateRuleset(errno) => {
            child_write(b"create ruleset errno ");
            child_write_errno(errno);
            child_write_seccomp_hint(errno);
        }
        landlock::LandlockError::OpenPath { path, errno } => {
            child_write(b"open ");
            child_write(path.to_bytes());
            child_write(b" errno ");
            child_write_errno(errno);
        }
        landlock::LandlockError::AddRule { path, errno } => {
            child_write(b"add rule ");
            child_write(path.to_bytes());
            child_write(b" errno ");
            child_write_errno(errno);
            child_write_seccomp_hint(errno);
        }
        landlock::LandlockError::AddNetPortRule {
            port,
            action,
            errno,
        } => {
            child_write(b"add ");
            child_write(action.as_bytes());
            child_write(b" rule for port ");
            child_write_u32(u32::from(port));
            child_write(b" errno ");
            child_write_errno(errno);
            child_write_seccomp_hint(errno);
        }
        landlock::LandlockError::SetNoNewPrivs(errno) => {
            child_write(b"PR_SET_NO_NEW_PRIVS errno ");
            child_write_errno(errno);
        }
        landlock::LandlockError::RestrictSelf(errno) => {
            child_write(b"restrict self errno ");
            child_write_errno(errno);
            child_write_seccomp_hint(errno);
        }
    }
    child_write(b")\n");
}

fn child_write_seccomp_hint(errno: Errno) {
    if errno == Errno::EPERM || errno == Errno::EACCES {
        child_write(b"; blocked by seccomp?");
    }
}

pub(super) fn manage_process_group(requested: bool, child_pid: Pid) -> bool {
    if !requested {
        return false;
    }
    match set_process_group(child_pid, child_pid) {
        Ok(()) => true,
        Err(Errno::EACCES)
            if let Ok(pgid) = process_group_of(child_pid)
                && pgid == child_pid =>
        {
            true
        }
        Err(Errno::ESRCH) => false,
        Err(err) => {
            logging::warn(format_args!(
                "cannot manage process group (disabling --pgroup-kill): {}",
                err
            ));
            false
        }
    }
}

#[cfg(all(test, target_os = "linux"))]
mod tests {
    use super::*;
    use std::io;

    struct PrctlStateGuard {
        subreaper: libc::c_int,
        pdeath: libc::c_int,
    }

    impl PrctlStateGuard {
        fn capture() -> Self {
            let mut subreaper = 0;
            let mut pdeath = 0;
            // SAFETY: we pass valid pointers to store the current prctl state.
            let ret = unsafe {
                libc::prctl(
                    libc::PR_GET_CHILD_SUBREAPER,
                    &mut subreaper as *mut libc::c_int,
                )
            };
            assert_eq!(
                ret,
                0,
                "PR_GET_CHILD_SUBREAPER failed: {}",
                io::Error::last_os_error()
            );
            // SAFETY: pointer references a valid mutable integer on our stack.
            let ret =
                unsafe { libc::prctl(libc::PR_GET_PDEATHSIG, &mut pdeath as *mut libc::c_int) };
            assert_eq!(
                ret,
                0,
                "PR_GET_PDEATHSIG failed: {}",
                io::Error::last_os_error()
            );
            Self { subreaper, pdeath }
        }
    }

    impl Drop for PrctlStateGuard {
        fn drop(&mut self) {
            // SAFETY: we restore the previously captured values; best-effort errors are ignored.
            unsafe {
                libc::prctl(libc::PR_SET_CHILD_SUBREAPER, self.subreaper);
                libc::prctl(libc::PR_SET_PDEATHSIG, self.pdeath);
            }
        }
    }

    fn base_cli() -> Cli {
        Cli {
            cmd: vec!["/bin/true".into()],
            ..Cli::default()
        }
    }

    #[test]
    fn configure_prctl_sets_pdeathsig() {
        let guard = PrctlStateGuard::capture();
        let mut cli = base_cli();
        cli.pdeath = Some("SIGUSR1".into());

        let outcome = configure_prctl(&cli).expect("configure prctl with pdeath");
        assert!(
            outcome.pdeath_set,
            "expected pdeath signal to be configured"
        );

        let mut current = guard.pdeath;
        // SAFETY: pointer references a valid mutable integer for prctl output.
        let ret = unsafe { libc::prctl(libc::PR_GET_PDEATHSIG, &mut current as *mut libc::c_int) };
        assert_eq!(
            ret,
            0,
            "PR_GET_PDEATHSIG failed: {}",
            io::Error::last_os_error()
        );
        assert_eq!(current, libc::SIGUSR1);
    }

    #[test]
    fn configure_prctl_handles_subreaper_capability() {
        let guard = PrctlStateGuard::capture();
        let mut cli = base_cli();
        cli.subreaper = true;

        let outcome = configure_prctl(&cli).expect("configure prctl with subreaper flag");

        let mut current = guard.subreaper;
        // SAFETY: pointer references a valid mutable integer for prctl output.
        let ret = unsafe {
            libc::prctl(
                libc::PR_GET_CHILD_SUBREAPER,
                &mut current as *mut libc::c_int,
            )
        };
        assert_eq!(
            ret,
            0,
            "PR_GET_CHILD_SUBREAPER failed: {}",
            io::Error::last_os_error()
        );
        if outcome.subreaper_enabled {
            assert_eq!(current, 1, "subreaper flag expected to be enabled");
        } else {
            assert_eq!(
                current, guard.subreaper,
                "subreaper state should be unchanged when capability is denied"
            );
        }
    }

    #[test]
    fn expand_command_arg_supports_defaults_and_escapes() {
        let expanded = expand_command_arg(
            "port=${__TINO_TEST_MISSING_PORT_123456__:-8900},literal=$${HOME},missing=${__TINO_TEST_MISSING_VALUE_123456__}",
        )
        .expect("expand env with defaults and escapes");

        assert_eq!(expanded, "port=8900,literal=${HOME},missing=");
    }

    #[test]
    fn expand_command_arg_supports_nested_defaults() {
        let expanded = expand_command_arg(
            "${__TINO_TEST_MISSING_PRIMARY_123456__:-${__TINO_TEST_MISSING_FALLBACK_123456__:-8900}}",
        )
        .expect("expand nested default");

        assert_eq!(expanded, "8900");
    }

    #[test]
    fn expand_command_arg_rejects_invalid_syntax() {
        let err =
            expand_command_arg("${__TINO_TEST_MISSING_PORT_123456__").expect_err("missing brace");
        let message = format!("{err:#}");
        assert!(
            message.contains("missing closing '}'"),
            "unexpected error: {message}"
        );

        let err = expand_command_arg("${NAME:+value}").expect_err("unsupported operator");
        let message = format!("{err:#}");
        assert!(
            message.contains("unsupported braced environment expansion"),
            "unexpected error: {message}"
        );
    }

    #[test]
    fn expand_command_arg_leaves_unbraced_dollar_names_unchanged() {
        let expanded = expand_command_arg("$SERVICE_PORT ${SERVICE_PORT:-8900}")
            .expect("expand env with unbraced name");

        assert_eq!(expanded, "$SERVICE_PORT 8900");
    }
}