From 9c3183412d3f7cd56974ed26dd8a6a0e05c59654 Mon Sep 17 00:00:00 2001 From: Hiroyuki Moriya <41197469+Gekko0114@users.noreply.github.com> Date: Fri, 7 Jun 2024 08:22:05 +0000 Subject: [PATCH] is_proc_handl --- experiment/selinux/Cargo.toml | 2 +- experiment/selinux/src/selinux.rs | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/experiment/selinux/Cargo.toml b/experiment/selinux/Cargo.toml index 52c68cad3c..fd95ffba7c 100644 --- a/experiment/selinux/Cargo.toml +++ b/experiment/selinux/Cargo.toml @@ -12,4 +12,4 @@ autoexamples = true keywords = ["youki", "container", "selinux"] [dependencies] -nix = { version = "0.29.0", features = ["process"] } +nix = { version = "0.29.0", features = ["process", "fs"] } diff --git a/experiment/selinux/src/selinux.rs b/experiment/selinux/src/selinux.rs index 9671770c5f..74f73de7c0 100644 --- a/experiment/selinux/src/selinux.rs +++ b/experiment/selinux/src/selinux.rs @@ -1,9 +1,12 @@ use std::sync::Once; use crate::xattr::*; use nix::unistd::gettid; +use nix::sys::statfs; +use nix::errno::Errno; use std::path::PathBuf; use std::fs::File; use std::io::{self, Read}; +use std::os::fd::{AsFd, AsRawFd}; const XATTR_NAME_SELINUX: &str = "security.selinux"; const ERR_EMPTY_PATH: &str = "empty path"; @@ -203,8 +206,24 @@ pub fn write_con(fpath: &str, val: &str) -> Result<(), std::io::Error> { panic!("not implemented yet"); } -pub fn is_proc_handle(file: &File) -> Result<(), std::io::Error> { - panic!("not implemented yet"); +pub fn is_proc_handle(file: &File) -> Result<(), std::io::Error> { + loop { + match statfs::fstatfs(file.as_fd()) { + Ok(stat) if stat.filesystem_type() == statfs::PROC_SUPER_MAGIC => break, + Ok(_) => { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, format!("file {} is not on procfs", file.as_raw_fd()) + )); + }, + Err(Errno::EINTR) => continue, + Err(err) => { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + format!("fstatfs failed: {}", err))) + } + } + } + Ok(()) } pub fn read_con_fd(file: &mut File) -> Result {