Skip to content

Commit

Permalink
Fix a stray FD leaking in containers when using preserve-fd
Browse files Browse the repository at this point in the history
Signed-off-by: Aidan Hobson Sayers <[email protected]>
  • Loading branch information
aidanhs committed Aug 24, 2024
1 parent 97df099 commit 97574b1
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions crates/libcontainer/src/syscall/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use nix::fcntl::{open, OFlag};
use nix::mount::{mount, umount2, MntFlags, MsFlags};
use nix::sched::{unshare, CloneFlags};
use nix::sys::stat::{mknod, Mode, SFlag};
use nix::unistd::{chown, chroot, fchdir, pivot_root, sethostname, Gid, Uid};
use nix::unistd::{chown, chroot, close, fchdir, pivot_root, sethostname, Gid, Uid};
use oci_spec::runtime::PosixRlimit;

use super::{Result, Syscall, SyscallError};
Expand Down Expand Up @@ -233,7 +233,7 @@ impl Syscall for LinuxSyscall {
fn pivot_rootfs(&self, path: &Path) -> Result<()> {
// open the path as directory and read only
let newroot =
open(path, OFlag::O_DIRECTORY | OFlag::O_RDONLY, Mode::empty()).map_err(|errno| {
open(path, OFlag::O_DIRECTORY | OFlag::O_RDONLY | OFlag::O_CLOEXEC, Mode::empty()).map_err(|errno| {
tracing::error!(?errno, ?path, "failed to open the new root for pivot root");
errno
})?;
Expand Down Expand Up @@ -279,6 +279,11 @@ impl Syscall for LinuxSyscall {
errno
})?;

close(newroot).map_err(|errno| {
tracing::error!(?errno, ?newroot, "failed to close new root directory");
errno
})?;

Ok(())
}

Expand Down

0 comments on commit 97574b1

Please sign in to comment.