Skip to content

Commit

Permalink
Before starting the intermediate process, close all non-stdio open files
Browse files Browse the repository at this point in the history
Signed-off-by: utam0k <k0ma@utam0k.jp>
  • Loading branch information
utam0k committed Jan 31, 2024
1 parent e178bbd commit 6c9f997
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion crates/libcontainer/src/process/container_init_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,8 @@ fn sync_seccomp(
fn verify_cwd() -> Result<()> {
let cwd = unistd::getcwd().map_err(|err| {
if let nix::errno::Errno::ENOENT = err {
// https://man7.org/linux/man-pages/man2/getcwd.2.html
// ENOENT The current working directory has been unlinked.
InitProcessError::InvalidCwd(err)
} else {
InitProcessError::NixOther(err)
Expand All @@ -855,7 +857,7 @@ fn verify_cwd() -> Result<()> {
return Err(InitProcessError::InvalidCwd(nix::errno::Errno::ENOENT));
}

return Ok(());
Ok(())
}

#[cfg(test)]
Expand Down
12 changes: 12 additions & 0 deletions crates/libcontainer/src/process/container_main_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
fork::{self, CloneCb},
intel_rdt::setup_intel_rdt,
},
syscall::SyscallError,
user_ns::UserNamespaceConfig,
};
use nix::sys::wait::{waitpid, WaitStatus};
Expand All @@ -29,6 +30,8 @@ pub enum ProcessError {
#[error("failed seccomp listener")]
#[cfg(feature = "libseccomp")]
SeccompListener(#[from] crate::process::seccomp_listener::SeccompListenerError),
#[error("failed syscall")]
SyscallOther(#[source] SyscallError),
}

type Result<T> = std::result::Result<T, ProcessError>;
Expand Down Expand Up @@ -69,6 +72,15 @@ pub fn container_main_process(container_args: &ContainerArgs) -> Result<(Pid, bo
})
};

// Before starting the intermediate process, mark all non-stdio open files as O_CLOEXEC
// to ensure we don't leak any file descriptors to the intermediate process.
// Please refer to XXXXXXX(TODO(utam0k): fill in) for more details.
let syscall = container_args.syscall.create_syscall();
syscall.close_range(0).map_err(|err| {
tracing::error!(?err, "failed to cleanup extra fds");
ProcessError::SyscallOther(err)
})?;

let intermediate_pid = fork::container_clone(cb).map_err(|err| {
tracing::error!("failed to fork intermediate process: {}", err);
ProcessError::IntermediateProcessFailed(err)
Expand Down

0 comments on commit 6c9f997

Please # to comment.