kata-sys-util: fix issues where umount2 couldn't get the correct path

Strings in Rust don't have \0 at the end, but C does, which leads to `umount2`
in the libc can't get the correct path. Besides, calling `nix::mount::umount2`
to avoid using an unsafe block is a robust solution.

Fixes: #5871

Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
This commit is contained in:
Xuewei Niu 2022-12-09 17:07:26 +08:00 committed by Xuewei Niu
parent 5ef7ed72ae
commit 8079a9732d
2 changed files with 11 additions and 12 deletions

8
src/libs/Cargo.lock generated
View File

@ -40,6 +40,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "base64"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "bitflags"
version = "1.2.1"
@ -420,6 +426,8 @@ dependencies = [
name = "kata-types"
version = "0.1.0"
dependencies = [
"anyhow",
"base64",
"bitmask-enum",
"byte-unit",
"glob",

View File

@ -43,8 +43,6 @@
use std::fmt::Debug;
use std::fs;
use std::io::{self, BufRead};
use std::os::raw::c_char;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt};
use std::os::unix::io::AsRawFd;
use std::path::{Path, PathBuf};
@ -760,18 +758,11 @@ pub fn umount_all<P: AsRef<Path>>(mountpoint: P, lazy_umount: bool) -> Result<()
// Counterpart of nix::umount2, with support of `UMOUNT_FOLLOW`.
fn umount2<P: AsRef<Path>>(path: P, lazy_umount: bool) -> std::io::Result<()> {
let path_ptr = path.as_ref().as_os_str().as_bytes().as_ptr() as *const c_char;
let mut flags = MntFlags::UMOUNT_NOFOLLOW.bits();
let mut flags = MntFlags::UMOUNT_NOFOLLOW;
if lazy_umount {
flags |= MntFlags::MNT_DETACH.bits();
}
// Safe because parameter is valid and we have checked the reuslt.
if unsafe { libc::umount2(path_ptr, flags) } < 0 {
Err(io::Error::last_os_error())
} else {
Ok(())
flags |= MntFlags::MNT_DETACH;
}
nix::mount::umount2(path.as_ref(), flags).map_err(io::Error::from)
}
#[cfg(test)]