From a3669d499ad32a69e5b9c66c20ea52fbae24d8f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Bombo?= Date: Tue, 30 Sep 2025 15:48:07 -0500 Subject: [PATCH] agent/rustjail: Fix potentially uninitialized memory read in unsafe code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous code only checked the result of with_nix_path(), not statfs(), thus leading to an uninitialized memory read if statfs() failed. No functional change otherwise. Signed-off-by: Aurélien Bombo --- src/agent/rustjail/src/mount.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/agent/rustjail/src/mount.rs b/src/agent/rustjail/src/mount.rs index 3472bff40a..27c363bf6a 100644 --- a/src/agent/rustjail/src/mount.rs +++ b/src/agent/rustjail/src/mount.rs @@ -5,6 +5,7 @@ use anyhow::{anyhow, Context, Result}; use libc::uid_t; +use nix::errno::Errno; use nix::fcntl::{self, OFlag}; #[cfg(not(test))] use nix::mount; @@ -336,25 +337,19 @@ fn check_proc_mount(m: &Mount) -> Result<()> { if mount_dest == PROC_PATH { // only allow a mount on-top of proc if it's source is "proc" - unsafe { - let mut stats = MaybeUninit::::uninit(); - let mount_source = m.source().as_ref().unwrap().display().to_string(); - if mount_source - .with_nix_path(|path| libc::statfs(path.as_ptr(), stats.as_mut_ptr())) - .is_ok() - { - if stats.assume_init().f_type == PROC_SUPER_MAGIC { - return Ok(()); - } - } else { - return Ok(()); - } + let mount_source = m.source().as_ref().unwrap().display().to_string(); - return Err(anyhow!(format!( + let mut stats = MaybeUninit::::uninit(); + let statfs_ret = mount_source + .with_nix_path(|path| unsafe { libc::statfs(path.as_ptr(), stats.as_mut_ptr()) })?; + + return match Errno::result(statfs_ret) { + Ok(_) if unsafe { stats.assume_init().f_type } == PROC_SUPER_MAGIC => Ok(()), + Ok(_) | Err(_) => Err(anyhow!(format!( "{} cannot be mounted to {} because it is not of type proc", &mount_source, &mount_dest - ))); - } + ))), + }; } if mount_dest.starts_with(PROC_PATH) {