Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ jobs:
set -euo pipefail
cargo test --locked -p tinybox-jail --test bwrap_isolation --test bwrap_fd_leak -- --nocapture
cargo test --locked -p tinybox-jail --lib bwrap
bwrap-pasta-live:
name: Bubblewrap private network (pasta) reachability
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262
with:
submodules: recursive
persist-credentials: false
- uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249
- name: Make unprivileged bubblewrap usable
run: |
source scripts/ci/prepare-bwrap.sh
- name: Install passt (pasta) and util-linux (nsenter)
run: |
set -euo pipefail
sudo apt-get install -y --no-install-recommends passt util-linux
pasta --version | head -1
- name: Prove outbound works and host loopback, gateway and abstract sockets do not
env:
TINYBOX_REQUIRE_BWRAP: "1"
TINYBOX_REQUIRE_PASTA: "1"
run: |
set -euo pipefail
cargo test --locked -p tinybox-jail --test bwrap_pasta_live -- --nocapture
cargo test --locked -p tinybox-jail --test bwrap_isolation -- --nocapture
cargo test --locked -p tinybox-jail --test bwrap_fd_leak -- --nocapture
docker-network-live:
name: Docker Egress and denied network connectivity
runs-on: ubuntu-22.04
Expand Down
12 changes: 9 additions & 3 deletions crates/tinybox-jail/src/bwrap/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub(super) struct Options {
/// Bind the jail's grants from descriptors (`--bind-fd`/`--ro-bind-fd`)
/// opened here, instead of by path (see [`jail_args_with_fds`]).
pub bind_fds: bool,
/// Leave the host's `/etc/resolv.conf` out of the baseline: the jail runs
/// in a private network whose resolver is supplied separately.
pub private_resolv: bool,
}

/// The first descriptor a grant is handed to `bwrap` on; the launcher keeps
Expand Down Expand Up @@ -135,8 +138,8 @@ fn replaces_private_mount(path: &Path) -> bool {
}

/// The baseline mounts: system directories (or their symlinks) and the short
/// `/etc` allow-list.
fn baseline_mounts() -> Vec<Mount> {
/// `/etc` allow-list, without `/etc/resolv.conf` when `private_resolv`.
fn baseline_mounts(private_resolv: bool) -> Vec<Mount> {
let mut mounts: Vec<Mount> = Vec::new();
for dir in SYSTEM_DIRS {
let path = PathBuf::from(dir);
Expand All @@ -154,6 +157,9 @@ fn baseline_mounts() -> Vec<Mount> {
});
}
for entry in ETC_ENTRIES {
if private_resolv && *entry == "/etc/resolv.conf" {
continue;
}
mounts.push(Mount {
path: PathBuf::from(entry),
kind: Kind::Bind { writable: false },
Expand Down Expand Up @@ -217,7 +223,7 @@ pub(super) fn jail_args_with_fds(
options: Options,
) -> io::Result<JailArgs> {
check_paths(jail, cwd)?;
let mut mounts = baseline_mounts();
let mut mounts = baseline_mounts(options.private_resolv);
for (path, writable) in std::iter::once((&jail.root, true))
.chain(jail.read_write.iter().map(|p| (p, true)))
.chain(jail.read_only.iter().map(|p| (p, false)))
Expand Down
3 changes: 3 additions & 0 deletions crates/tinybox-jail/src/bwrap/args_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::*;
const OPTS: Options = Options {
disable_userns: true,
bind_fds: false,
private_resolv: false,
};

fn build(jail: &Jail) -> io::Result<Vec<String>> {
Expand Down Expand Up @@ -57,6 +58,7 @@ fn disable_userns_is_omitted_when_the_probe_found_no_support() -> io::Result<()>
Options {
disable_userns: false,
bind_fds: false,
private_resolv: false,
},
)?;
assert!(!argv.iter().any(|a| a == "--disable-userns"));
Expand Down Expand Up @@ -354,6 +356,7 @@ fn a_denial_through_a_baseline_symlink_also_covers_the_link_target() -> io::Resu
const FD_OPTS: Options = Options {
disable_userns: true,
bind_fds: true,
private_resolv: false,
};

fn strings(argv: &[OsString]) -> Vec<String> {
Expand Down
27 changes: 25 additions & 2 deletions crates/tinybox-jail/src/bwrap/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,41 @@ impl ArgsFile {
}
}

/// Namespaces the launcher joins before it runs: `nsenter` enters the user
/// and network namespace open on these child descriptors (by descriptor, not
/// by pid, so a reused pid cannot redirect it), and keeps every other
/// descriptor. The script then closes these two, as it closes everything
/// outside its keep list.
#[derive(Debug, Clone, Copy)]
pub(super) struct Enter<'a> {
pub nsenter: &'a Path,
pub user_fd: i32,
pub net_fd: i32,
}

/// The launcher command: an empty environment, the script, `bwrap`, the file,
/// how many grant descriptors to keep, then the command to run inside the
/// sandbox and its arguments.
/// sandbox and its arguments. With `enter`, the shell runs under `nsenter`.
pub(super) fn command(
enter: Option<Enter<'_>>,
shell: &str,
bwrap: &Path,
args: &ArgsFile,
grant_fds: usize,
program: &std::ffi::OsStr,
program_args: &[OsString],
) -> Command {
let mut launcher = Command::new(shell);
let mut launcher = match enter {
Some(enter) => {
let mut nsenter = Command::new(enter.nsenter);
nsenter
.arg(format!("--user=/proc/self/fd/{}", enter.user_fd))
.arg(format!("--net=/proc/self/fd/{}", enter.net_fd))
.args(["--preserve-credentials", "--", shell]);
nsenter
}
None => Command::new(shell),
};
launcher
.env_clear()
.arg("-c")
Expand Down
64 changes: 59 additions & 5 deletions crates/tinybox-jail/src/bwrap/launcher_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ fn run(shell: &str, body: &str, items: &[&str]) -> io::Result<String> {
let bwrap = stand_in(dir.path(), body)?;
let argv: Vec<OsString> = items.iter().map(OsString::from).collect();
let file = ArgsFile::create(&argv, &tmp())?;
let out = command(shell, &bwrap, &file, 0, "prog".as_ref(), &["a".into()]).output()?;
let out = command(
None,
shell,
&bwrap,
&file,
0,
"prog".as_ref(),
&["a".into()],
)
.output()?;
file.discard();
Ok(String::from_utf8_lossy(&out.stdout).into_owned())
}
Expand Down Expand Up @@ -81,7 +90,7 @@ fn the_args_file_is_gone_once_the_launcher_has_started_bwrap() -> io::Result<()>
let file = ArgsFile::create(&["x".into()], &tmp())?;
let path = file.path().to_path_buf();
assert!(
command(shell(), &bwrap, &file, 0, "prog".as_ref(), &[])
command(None, shell(), &bwrap, &file, 0, "prog".as_ref(), &[])
.status()?
.success()
);
Expand All @@ -94,7 +103,7 @@ fn the_launcher_runs_with_an_empty_environment() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let bwrap = stand_in(dir.path(), "env")?;
let file = ArgsFile::create(&[], &tmp())?;
let out = command(shell(), &bwrap, &file, 0, "prog".as_ref(), &[]).output()?;
let out = command(None, shell(), &bwrap, &file, 0, "prog".as_ref(), &[]).output()?;
file.discard();
let text = String::from_utf8_lossy(&out.stdout);
assert!(
Expand Down Expand Up @@ -137,7 +146,7 @@ fn a_launcher_that_cannot_open_the_args_file_fails_closed_without_running_bwrap(
dir: dir.path().join("gone"),
file: dir.path().join("gone").join("args"),
};
let status = command(shell(), &bwrap, &missing, 0, "prog".as_ref(), &[]).status()?;
let status = command(None, shell(), &bwrap, &missing, 0, "prog".as_ref(), &[]).status()?;
assert_eq!(status.code(), Some(126));
assert!(!marker.exists(), "bwrap must not run without its arguments");
Ok(())
Expand Down Expand Up @@ -211,7 +220,7 @@ fn the_grant_descriptors_reach_bwrap_and_nothing_else_does() -> io::Result<()> {
rustix::io::fcntl_setfd(&stray, rustix::io::FdFlags::empty())?;
let bwrap = stand_in(dir.path(), "ls -l /proc/self/fd")?;
let file = ArgsFile::create(&[], &tmp())?;
let mut launcher = command(shell, &bwrap, &file, 2, "prog".as_ref(), &[]);
let mut launcher = command(None, shell, &bwrap, &file, 2, "prog".as_ref(), &[]);
let mappings = vec![
command_fds::FdMapping {
parent_fd: std::fs::File::open(granted.path())?.into(),
Expand Down Expand Up @@ -241,3 +250,48 @@ fn the_grant_descriptors_reach_bwrap_and_nothing_else_does() -> io::Result<()> {
);
Ok(())
}

#[test]
fn entering_namespaces_puts_nsenter_in_front_by_descriptor() -> io::Result<()> {
let file = ArgsFile::create(&[], &tmp())?;
let enter = Enter {
nsenter: Path::new("/usr/bin/nsenter"),
user_fd: 14,
net_fd: 15,
};
let launcher = command(
Some(enter),
"/bin/bash",
Path::new("/usr/bin/bwrap"),
&file,
4,
"prog".as_ref(),
&[],
);
file.discard();
assert_eq!(launcher.get_program(), "/usr/bin/nsenter");
assert_eq!(
launcher.get_envs().count(),
0,
"no environment reaches nsenter"
);
let argv: Vec<String> = launcher
.get_args()
.map(|a| a.to_string_lossy().into_owned())
.collect();
assert_eq!(
&argv[..6],
[
"--user=/proc/self/fd/14",
"--net=/proc/self/fd/15",
"--preserve-credentials",
"--",
"/bin/bash",
"-c",
]
);
// The launcher keeps only the 4 descriptors from 10, so the namespace
// descriptors on 14 and 15 are closed before bwrap runs.
assert_eq!(argv.get(9).map(String::as_str), Some("4"));
Ok(())
}
Loading
Loading