diff options
Diffstat (limited to 'init/src/mounts')
| -rw-r--r-- | init/src/mounts/fstab.rs | 23 | ||||
| -rw-r--r-- | init/src/mounts/rescue.rs | 46 |
2 files changed, 64 insertions, 5 deletions
diff --git a/init/src/mounts/fstab.rs b/init/src/mounts/fstab.rs index b8708e2..4d9b474 100644 --- a/init/src/mounts/fstab.rs +++ b/init/src/mounts/fstab.rs @@ -1,6 +1,8 @@ use crate::log::{log_critical_error, log_success, log_warning}; use libc::syscall; use std::ffi::CString; +use std::fs::{create_dir, metadata}; +use std::os::unix::fs::MetadataExt; use std::{fmt, fs}; #[derive(Debug)] @@ -114,7 +116,28 @@ impl FstabEntry { Ok((flags, data)) } + fn check_mount_point_permissions(path: &str) -> Result<(), Box<dyn std::error::Error>> { + if !std::path::Path::new(path).exists() { + create_dir(path)?; + } + + let meta = metadata(path)?; + if !meta.is_dir() { + return Err(format!("Mount point {} is not a directory", path).into()); + } + let uid = meta.uid(); + if uid != 0 { + log_warning(&format!("Warning: Mount point {} not owned by root", path)); + } + + Ok(()) + } + pub fn mount(&self) -> Result<(), Box<dyn std::error::Error>> { + if let Err(e) = Self::check_mount_point_permissions(&self.mountpoint) { + log_warning(&format!("Permission check failed for {}: {}", self.mountpoint, e)); + } + log_success(&format!( "Started mounting {} from {}", self.mountpoint, self.source diff --git a/init/src/mounts/rescue.rs b/init/src/mounts/rescue.rs index 078984d..2238297 100644 --- a/init/src/mounts/rescue.rs +++ b/init/src/mounts/rescue.rs @@ -1,6 +1,29 @@ -use crate::log::log_success; +use crate::log::{log_success, log_warning}; use std::ffi::CString; -use std::fs::create_dir; +use std::fs::{create_dir, metadata}; +use std::os::unix::fs::MetadataExt; + +fn check_mount_point_permissions(path: &str) -> Result<(), Box<dyn std::error::Error>> { + if !std::path::Path::new(path).exists() { + create_dir(path)?; + } + + let meta = metadata(path)?; + + if !meta.is_dir() { + return Err(format!("Mount point {} is not a directory", path).into()); + } + + // TODO + // let mode = meta.mode(); + + let uid = meta.uid(); + if uid != 0 { + log_warning(&format!("Warning: Mount point {} not owned by root", path)); + } + + Ok(()) +} pub fn mount_system() -> Result<(), Box<dyn std::error::Error>> { let mounts: &[(&str, &str, Option<&str>)] = &[ @@ -13,11 +36,24 @@ pub fn mount_system() -> Result<(), Box<dyn std::error::Error>> { unsafe { for &(target, fstype, source) in mounts { + if let Err(e) = check_mount_point_permissions(target) { + log_warning(&format!("Permission check failed for {}: {}", target, e)); + } + let target_c = CString::new(target)?; let fstype_c = CString::new(fstype)?; - let source_c = source.map(|s| CString::new(s).unwrap()); - - let _ = create_dir(target); + // let source_c = source.map(|s| CString::new(s).map_err(|e| )); + let source_c = match source { + Some(s) => match CString::new(s) { + Ok(c_string) => Some(c_string), + Err(null_err) => { + log_warning(&format!("Source string contains NULL bytes (\\0), skipping: {}", null_err)); + continue; + } + }, + None => None + }; + let source_ptr = source_c.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()); |
