childflow 0.7.0

A per-command-tree network sandbox for Linux
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
// Copyright (c) 2026 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.

use std::env;
use std::ffi::OsStr;
use std::fs::OpenOptions;
use std::path::Path;

use anyhow::{bail, Result};

use crate::cli::Cli;
use crate::network::NetworkBackend;

const ROOTFUL_REQUIRED_COMMANDS: &[&str] = &["ip", "iptables", "ip6tables"];
const ROOTLESS_INTERNAL_REQUIRED_COMMANDS: &[&str] = &["ip"];
const ROOTLESS_UIDMAP_HELPERS: &[&str] = &["newuidmap", "newgidmap"];
const ROOTFUL_REQUIRED_SYSCTLS: &[&str] = &[
    "/proc/sys/net/ipv4/ip_forward",
    "/proc/sys/net/ipv6/conf/all/forwarding",
];
const ROOTLESS_INTERNAL_NAMESPACE_PATHS: &[&str] = &[
    "/proc/self/ns/user",
    "/proc/self/ns/net",
    "/proc/self/ns/mnt",
];
const ROOTLESS_IDMAP_FILES: &[&str] = &["/etc/subuid", "/etc/subgid"];

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CheckStatus {
    Ok,
    Warning,
    Fatal,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PreflightCheck {
    pub label: String,
    pub status: CheckStatus,
    pub detail: String,
    pub hint: Option<String>,
}

#[derive(Clone, Debug, Default)]
pub struct PreflightReport {
    backend_name: String,
    checks: Vec<PreflightCheck>,
}

impl PreflightReport {
    fn new(backend_name: impl Into<String>) -> Self {
        Self {
            backend_name: backend_name.into(),
            checks: Vec::new(),
        }
    }

    fn push(
        &mut self,
        status: CheckStatus,
        label: impl Into<String>,
        detail: impl Into<String>,
        hint: Option<String>,
    ) {
        self.checks.push(PreflightCheck {
            label: label.into(),
            status,
            detail: detail.into(),
            hint,
        });
    }

    fn push_ok(&mut self, label: impl Into<String>, detail: impl Into<String>) {
        self.push(CheckStatus::Ok, label, detail, None);
    }

    fn push_warning(
        &mut self,
        label: impl Into<String>,
        detail: impl Into<String>,
        hint: impl Into<String>,
    ) {
        self.push(CheckStatus::Warning, label, detail, Some(hint.into()));
    }

    fn push_fatal(
        &mut self,
        label: impl Into<String>,
        detail: impl Into<String>,
        hint: impl Into<String>,
    ) {
        self.push(CheckStatus::Fatal, label, detail, Some(hint.into()));
    }

    pub fn backend_name(&self) -> &str {
        &self.backend_name
    }

    pub fn checks(&self) -> &[PreflightCheck] {
        &self.checks
    }

    pub fn has_fatal(&self) -> bool {
        self.checks
            .iter()
            .any(|check| matches!(check.status, CheckStatus::Fatal))
    }

    pub fn has_warnings(&self) -> bool {
        self.checks
            .iter()
            .any(|check| matches!(check.status, CheckStatus::Warning))
    }

    fn emit_warnings(&self) {
        for check in self
            .checks
            .iter()
            .filter(|check| matches!(check.status, CheckStatus::Warning))
        {
            crate::util::warn(format!("preflight: {}: {}", check.label, check.detail));
        }
    }

    fn finish(self) -> Result<()> {
        self.emit_warnings();

        if !self.has_fatal() {
            return Ok(());
        }

        let fatal = self
            .checks
            .iter()
            .filter(|check| matches!(check.status, CheckStatus::Fatal))
            .map(render_check)
            .collect::<Vec<_>>();

        bail!(
            "preflight checks failed for the `{}` backend:\n{}",
            self.backend_name,
            render_issue_list(&fatal)
        );
    }
}

pub fn run(cli: &Cli) -> Result<()> {
    inspect(cli.selected_backend(), cli.proxy.is_some()).finish()
}

pub fn inspect(backend: NetworkBackend, proxy_requested: bool) -> PreflightReport {
    match backend {
        NetworkBackend::Rootful => inspect_rootful(proxy_requested),
        NetworkBackend::RootlessInternal => inspect_rootless_internal(),
    }
}

fn inspect_rootful(proxy_requested: bool) -> PreflightReport {
    let path_env = env::var_os("PATH").unwrap_or_default();
    let missing_commands = find_missing_commands(ROOTFUL_REQUIRED_COMMANDS, &path_env);
    let unwritable_paths = find_unwritable_paths(ROOTFUL_REQUIRED_SYSCTLS);
    let mut report = PreflightReport::new("rootful");

    if unsafe { nix::libc::geteuid() } == 0 {
        report.push_ok("root privileges", "running as root");
    } else {
        report.push_fatal(
            "root privileges",
            "the `rootful` backend requires root on Linux",
            "rerun with `sudo -- childflow --root ...`, or use the default rootless backend",
        );
    }

    if missing_commands.is_empty() {
        report.push_ok(
            "external commands",
            "found `ip`, `iptables`, and `ip6tables` in PATH",
        );
    } else {
        report.push_fatal(
            "external commands",
            format!("missing required commands: {}", missing_commands.join(", ")),
            "install `iproute2` and an `iptables` / `ip6tables` userspace compatible with the host firewall backend",
        );
    }

    if unwritable_paths.is_empty() {
        report.push_ok(
            "forwarding sysctls",
            "required IPv4 and IPv6 forwarding sysctls are writable",
        );
    } else {
        report.push_fatal(
            "forwarding sysctls",
            format!("required sysctl files are not writable: {}", unwritable_paths.join(", ")),
            "check root privileges, container restrictions, and whether `/proc/sys` is mounted read-write",
        );
    }

    if proxy_requested {
        report.push_warning(
            "transparent proxy prerequisites",
            "transparent proxy mode still depends on Linux TPROXY support (`xt_TPROXY`, `xt_socket`, policy routing, and `IP_TRANSPARENT`) during setup",
            "if proxy startup fails, verify that the host kernel exposes the required TPROXY modules and capabilities",
        );
    }

    report
}

fn inspect_rootless_internal() -> PreflightReport {
    let path_env = env::var_os("PATH").unwrap_or_default();
    let mut report = PreflightReport::new("rootless-internal");

    let missing_commands = find_missing_commands(ROOTLESS_INTERNAL_REQUIRED_COMMANDS, &path_env);
    if missing_commands.is_empty() {
        report.push_ok("external commands", "found `ip` in PATH");
    } else {
        report.push_fatal(
            "external commands",
            format!("missing required commands: {}", missing_commands.join(", ")),
            "install `iproute2` so childflow can configure `tap0`, loopback, and default routes inside the child namespace",
        );
    }

    let missing_namespace_paths = find_missing_paths(ROOTLESS_INTERNAL_NAMESPACE_PATHS);
    if missing_namespace_paths.is_empty() {
        report.push_ok(
            "namespace handles",
            "found `/proc/self/ns/{user,net,mnt}` for rootless namespace setup",
        );
    } else {
        report.push_fatal(
            "namespace handles",
            format!(
                "missing required namespace handles: {}",
                missing_namespace_paths.join(", ")
            ),
            "verify that this Linux environment exposes user, network, and mount namespace handles",
        );
    }

    match parse_proc_u64("/proc/sys/user/max_user_namespaces") {
        Ok(Some(0)) => report.push_fatal(
            "user namespace quota",
            "`/proc/sys/user/max_user_namespaces` is `0`",
            "enable user namespaces before using the default rootless backend",
        ),
        Ok(Some(value)) => report.push_ok(
            "user namespace quota",
            format!("`/proc/sys/user/max_user_namespaces` is set to {value}"),
        ),
        Ok(None) => report.push_warning(
            "user namespace quota",
            "`/proc/sys/user/max_user_namespaces` is unavailable in this environment",
            "namespace availability will be determined during setup; rerun with `CHILDFLOW_DEBUG=1` if startup still fails",
        ),
        Err(err) => report.push_warning(
            "user namespace quota",
            err.to_string(),
            "namespace availability will be determined during setup",
        ),
    }

    if unsafe { nix::libc::geteuid() } != 0 {
        match parse_proc_u64("/proc/sys/kernel/unprivileged_userns_clone") {
            Ok(Some(0)) => report.push_fatal(
                "unprivileged user namespaces",
                "`/proc/sys/kernel/unprivileged_userns_clone` is disabled",
                "enable unprivileged user namespaces or run childflow with enough privileges to set up the namespace another way",
            ),
            Ok(Some(_)) => report.push_ok(
                "unprivileged user namespaces",
                "unprivileged user namespace cloning is enabled",
            ),
            Ok(None) => report.push_ok(
                "unprivileged user namespaces",
                "`/proc/sys/kernel/unprivileged_userns_clone` is unavailable in this environment; namespace setup will be determined during runtime",
            ),
            Err(err) => report.push_warning(
                "unprivileged user namespaces",
                err.to_string(),
                "non-root user-namespace setup may still fail later on this host",
            ),
        }
    } else {
        report.push_ok(
            "unprivileged user namespaces",
            "running as root, so the non-root clone gate does not apply",
        );
    }

    let missing_uidmap_helpers = find_missing_commands(ROOTLESS_UIDMAP_HELPERS, &path_env);
    if unsafe { nix::libc::geteuid() } == 0 {
        report.push_ok(
            "uidmap helpers",
            "running as root, so `newuidmap` / `newgidmap` fallback is not required",
        );
    } else if missing_uidmap_helpers.is_empty() {
        report.push_ok(
            "uidmap helpers",
            "found `newuidmap` and `newgidmap` for fallback user-namespace mapping",
        );
    } else {
        report.push_warning(
            "uidmap helpers",
            format!(
                "missing optional helpers: {}",
                missing_uidmap_helpers.join(", ")
            ),
            "install the `uidmap` package so childflow can fall back to helper-based uid/gid mapping when direct map writes are rejected",
        );
    }

    let missing_idmap_files = ROOTLESS_IDMAP_FILES
        .iter()
        .filter(|path| !Path::new(path).exists())
        .copied()
        .collect::<Vec<_>>();
    if unsafe { nix::libc::geteuid() } == 0 {
        report.push_ok(
            "subuid/subgid files",
            "running as root, so helper-based subordinate id mappings are not required",
        );
    } else if missing_idmap_files.is_empty() {
        report.push_ok(
            "subuid/subgid files",
            "found `/etc/subuid` and `/etc/subgid` for helper-based id mapping",
        );
    } else {
        report.push_warning(
            "subuid/subgid files",
            format!(
                "missing optional subordinate id mapping files: {}",
                missing_idmap_files.join(", ")
            ),
            "create `/etc/subuid` and `/etc/subgid` entries for the current user if helper-based uid/gid mapping is needed on this host",
        );
    }

    match check_tun_device("/dev/net/tun") {
        Ok(()) => report.push_ok(
            "TUN/TAP device",
            "`/dev/net/tun` is present and can be opened for rootless tap setup",
        ),
        Err(err) => report.push_fatal(
            "TUN/TAP device",
            err.to_string(),
            "load the `tun` kernel module, pass the device through to the container or VM, and verify the current user can open it",
        ),
    }

    report
}

fn check_tun_device(path: &str) -> Result<()> {
    let device = Path::new(path);
    if !device.exists() {
        bail!("`{path}` is missing");
    }

    OpenOptions::new()
        .read(true)
        .write(true)
        .open(device)
        .map(|_| ())
        .map_err(|err| anyhow::anyhow!("failed to open `{path}` ({err})"))
}

fn find_unwritable_paths(paths: &[&str]) -> Vec<String> {
    paths
        .iter()
        .filter_map(|path| {
            OpenOptions::new()
                .write(true)
                .open(path)
                .err()
                .map(|err| format!("{path} ({err})"))
        })
        .collect()
}

fn find_missing_paths(paths: &[&str]) -> Vec<String> {
    paths
        .iter()
        .filter(|path| !Path::new(path).exists())
        .map(|path| path.to_string())
        .collect()
}

fn render_issue_list(issues: &[String]) -> String {
    issues
        .iter()
        .map(|issue| format!("- {issue}"))
        .collect::<Vec<_>>()
        .join("\n")
}

fn render_check(check: &PreflightCheck) -> String {
    match &check.hint {
        Some(hint) => format!("{}: {}. Hint: {}", check.label, check.detail, hint),
        None => format!("{}: {}", check.label, check.detail),
    }
}

pub fn find_missing_commands(commands: &[&str], path_env: &OsStr) -> Vec<String> {
    let path_entries = env::split_paths(path_env).collect::<Vec<_>>();

    commands
        .iter()
        .filter(|command| {
            !path_entries
                .iter()
                .any(|dir| command_exists_in_dir(dir, command))
        })
        .map(|command| (*command).to_string())
        .collect()
}

fn parse_proc_u64(path: &str) -> Result<Option<u64>> {
    if !Path::new(path).exists() {
        return Ok(None);
    }

    let raw = std::fs::read_to_string(path)
        .map_err(|err| anyhow::anyhow!("failed to read `{path}` during preflight: {err}"))?;
    let value = raw.trim().parse::<u64>().map_err(|err| {
        anyhow::anyhow!("failed to parse `{path}` as an integer during preflight: {err}")
    })?;
    Ok(Some(value))
}

fn command_exists_in_dir(dir: &Path, command: &str) -> bool {
    dir.join(command).is_file()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    #[test]
    fn find_missing_commands_reports_only_missing_entries() {
        let base = PathBuf::from("/tmp/childflow-preflight-tests");
        let path_env = env::join_paths([base.join("bin-a"), base.join("bin-b")]).unwrap();

        assert_eq!(
            find_missing_commands(&["ip", "iptables"], &path_env),
            vec!["ip".to_string(), "iptables".to_string()]
        );
    }

    #[test]
    fn render_issue_list_formats_each_entry() {
        let rendered = render_issue_list(&["first".into(), "second".into()]);
        assert_eq!(rendered, "- first\n- second");
    }

    #[test]
    fn parse_proc_u64_returns_none_when_file_is_missing() {
        assert_eq!(
            parse_proc_u64("/tmp/childflow-preflight/definitely-missing").unwrap(),
            None
        );
    }

    #[test]
    fn inspect_rootless_report_contains_backend_name() {
        let report = inspect(NetworkBackend::RootlessInternal, false);
        assert_eq!(report.backend_name(), "rootless-internal");
        assert!(!report.checks().is_empty());
    }

    #[test]
    fn preflight_report_finish_succeeds_with_only_warnings() {
        let mut report = PreflightReport::new("rootless-internal");
        report.push_warning("heads up", "warning detail", "warning hint");

        report.finish().unwrap();
    }
}