8000 Fixed a bug where nested sandboxes would fail if we tried to mount a file with a flag locked by the outer sandbox but not the inner by evanspearman · Pull Request #208 · gominimal/minimal · GitHub
[go: up one dir, main page]

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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ lzma-rs = "0.3"
# Revert back to crates.io dep once 0.18 is released.
nickel-lang-core = { git = "https://github.com/nickel-lang/nickel.git", rev = "3a399233dcb5b5e8610a1245eeb171c29787a903", default-features = false, features = ["format"] }


8000 nix = { version = "0.29", features = ["fs"] }
object = { version = "0.39", default-features = false, features = ["archive", "elf", "read_core", "std"] }
oci-spec = "0.9"
path-absolutize = "3.1"
Expand Down
3 changes: 3 additions & 0 deletions crates/mctx/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ impl From<sandbox2::Error> for Error {
sandbox2::error::ExecutionError::Cancelled => {
Self::Other(anyhow::anyhow!("execution cancelled"))
}
sandbox2::error::ExecutionError::MountError { .. } => {
Self::Other(anyhow::anyhow!(e.to_string()))
}
},
sandbox2::Error::Output(e) => Self::Other(e.into()),
}
Expand Down
1 change: 1 addition & 0 deletions crates/sandbox2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ common.workspace = true

[target.'cfg(target_os = "linux")'.dependencies]
hakoniwa.workspace = true
nix.workspace = true
8 changes: 8 additions & 0 deletions crates/sandbox2/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ pub enum ExecutionError {
stderr: String,
},
SpawnFailed(hakoniwa::Error),
MountError {
msg: &'static str,
path: PathBuf,
},
Cancelled,
}

Expand All @@ -77,6 +81,9 @@ impl fmt::Display for ExecutionError {
Self::SpawnFailed(e) => {
write!(f, "Invocation spawn failed: {}", e)
}
Self::MountError { msg, path } => {
write!(f, "Failed to mount {}: {}", path.display(), msg)
}
Self::Cancelled => {
write!(f, "Execution cancelled")
}
Expand All @@ -89,6 +96,7 @@ impl std::error::Error for ExecutionError {
match self {
Self::InvocationFailed { .. } => None,
Self::SpawnFailed(e) => Some(e),
Self::MountError { .. } => None,
Self::Cancelled => None,
}
}
Expand Down
170 changes: 129 additions & 41 deletions crates/sandbox2/src/lib.rs
< AE48 td id="diff-8079192560465d3b964c34d0ed8d6073e934202f55f1be88691ff5d8a810478bR810" data-line-number="810" class="blob-num blob-num-addition js-linkable-line-number js-blob-rnum">
Original file line number Diff line number Diff line change
Expand Up @@ -338,19 +338,60 @@ impl Container {
}
}

/// Options for [`Sandbox::bind_mount`].
#[derive(Debug, Default, Clone, Copy)]
struct BindOpts {
read_only: bool,
recursive: bool,
}

// Sandbox usage
impl<C: Channel> Sandbox<C> {
fn bind_mount(
path: &Path,
container_path: &str,
opts: BindOpts,
container: &mut hakoniwa::Container,
) -> Result<(), Error> {
let mut flags = hakoniwa::MountOptions::BIND
| hakoniwa::MountOptions::NOSUID
| locked_mount_flags(path);
if opts.recursive {
flags |= hakoniwa::MountOptions::REC;
}
if opts.read_only {
flags |= hakoniwa::MountOptions::RDONLY;
}
container.mount(
path.to_str().ok_or_else(|| {
Error::Execution(ExecutionError::MountError {
msg: "Unable to convert path to unicode string",
path: path.to_path_buf(),
})
})?,
container_path,
"",
flags,
);
Ok(())
}

pub fn new_container(&self) -> Result<Container, Error> {
let mut container = hakoniwa::Container::new();
container
.rootfs(self.rootfs())
.unwrap()
.devfsmount("/dev")
.tmpfsmount("/tmp")
.bindmount_rw(self.state_dir.to_str().unwrap(), "/state")
.bindmount_rw(self.base_dir.join("run").to_str().unwrap(), "/run")
.unshare(hakoniwa::Namespace::Cgroup);

let rec = BindOpts {
recursive: true,
read_only: false,
};
Self::bind_mount(&self.state_dir, "/state", rec, &mut container)?;
Self::bind_mount(&self.base_dir.join("run"), "/run", rec, &mut container)?;

if self.needs_bin_symlink()? {
container.symlink("/usr/bin", "/bin");
}
Expand All @@ -364,52 +405,35 @@ impl<C: Channel> Sandbox<C> {
// Mount in the working directory
match &self.config.wd {
WdSetup::Isolated { .. } => {
container.bindmount_rw(self.base_dir.join("build").to_str().unwrap(), "/build")
Self::bind_mount(&self.base_dir.join("build"), "/build", rec, &mut container)?;
}
WdSetup::BoundDir {
path,
read_only: false,
fs_mappings: _,
} => container.bindmount_rw(
path.to_str().unwrap(),
format!(
"/{}",
self.config.wd.bound_dir_sandbox_cwd().to_str().unwrap()
)
.as_str(),
),
WdSetup::BoundDir {
path,
read_only: true,
fs_mappings: _,
} => container.bindmount_ro(
path.to_str().unwrap(),
format!(
path, read_only, ..
} => {
let container_path = format!(
"/{}",
self.config.wd.bound_dir_sandbox_cwd().to_str().unwrap()
)
.as_str(),
),
};
);
let opts = BindOpts {
recursive: true,
read_only: *read_only,
};
Self::bind_mount(path, &container_path, opts, &mut container)?;
}
}
// Mount in any file mappings
if let WdSetup::BoundDir { fs_mappings, .. } = &self.config.wd {
for m in fs_mappings {
if m.is_file {
container.mount(
&m.host_path,
&m.path_in_sandbox(),
"",
if m.read_only {
hakoniwa::MountOptions::BIND | hakoniwa::MountOptions::RDONLY
} else {
hakoniwa::MountOptions::BIND
},
);
} else if m.read_only {
container.bindmount_ro(&m.host_path, &m.path_in_sandbox());
} else {
container.bindmount_rw(&m.host_path, &m.path_in_sandbox());
}
let opts = BindOpts {
recursive: !m.is_file,
read_only: m.read_only,
};
Self::bind_mount(
Path::new(&m.host_path),
&m.path_in_sandbox(),
opts,
&mut container,
)?;
}
}

Expand Down Expand Up @@ -768,3 +792,67 @@ fn systemd_probably_works() -> bool {
}
false
}

/// Returns the kernel-locked mount flags for the mount containing `path`.
///
/// In a user namespace, remounting a bind mount requires preserving all
/// flags that the kernel has locked (CL_UNPRIVILEGED). If the remount
/// omits a locked flag the kernel returns EPERM. By proactively reading
/// these flags and including them in the mount options we hand to
/// hakoniwa, the remount succeeds even in nested sandboxes—without
/// resorting to MountFallback (which can silently drop requested
/// restrictions like RDONLY).
fn locked_mount_flags(path: &Path) -> hakoniwa::MountOptions {
use nix::sys::statfs::statfs;
use nix::sys::statvfs::FsFlags;

let stat = match statfs(path) {
Ok(s) => s,
Err(e) => {
tracing::warn!(
"statfs({}) failed: {e}, not adding locked mount flags",
path.to_string_lossy()
);
return hakoniwa::MountOptions::empty();
}
};

let flags = stat.flags();
let mut opts = hakoniwa::MountOptions::empty();
if flags.contains(FsFlags::ST_RDONLY) {
opts |= hakoniwa::MountOptions::RDONLY;
}
if flags.contains(FsFlags::ST_NOSUID) {
opts |= hakoniwa::MountOptions::NOSUID;
}
if flags.contains(FsFlags::ST_NODEV) {
opts |= hakoniwa::MountOptions::NODEV;
}
if flags.contains(FsFlags::ST_NOEXEC) {
opts |= hakoniwa::MountOptions::NOEXEC;
}
opts
}

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

// /proc is mounted with nosuid,nodev on essentially every Linux distro;
// if either stops showing up we've broken the FsFlags → MountOptions
// mapping and a nested sandbox would silently lose those locked flags
// again. NOEXEC is also common but skipped here since it's not
// universal.
#[test]
fn locked_mount_flags_reads_proc_flags() {
let opts = locked_mount_flags(Path::new("/proc"));
assert!(opts.contains(hakoniwa::MountOptions::NOSUID));
assert!(opts.contains(hakoniwa::MountOptions::NODEV));
}

#[test]
fn locked_mount_flags_empty_on_statfs_failure() {
let opts = locked_mount_flags(Path::new("/nonexistent-path-for-statfs-test"));
assert!(opts.is_empty());
}
}
Loading
0