diff --git a/userspace/ksud/src/init_event.rs b/userspace/ksud/src/init_event.rs index 76b548df..0632461f 100644 --- a/userspace/ksud/src/init_event.rs +++ b/userspace/ksud/src/init_event.rs @@ -7,6 +7,7 @@ use crate::{ module::{handle_updated_modules, prune_modules}, restorecon, uid_scanner, utils, }; +use crate::utils::find_tmp_path; use anyhow::{Context, Result}; use log::{info, warn}; use rustix::fs::{MountFlags, mount}; @@ -86,11 +87,15 @@ pub fn on_post_data_fs() -> Result<()> { warn!("KPM: Failed to load KPM modules: {}", e); } + let tmpfs_path = find_tmp_path(); + // for compatibility + let no_mount = Path::new(NO_TMPFS_PATH).exists() || Path::new(NO_MOUNT_PATH).exists(); + // mount temp dir - if !Path::new(NO_TMPFS_PATH).exists() { + if !no_mount { if let Err(e) = mount( KSU_MOUNT_SOURCE, - utils::get_tmp_path(), + &tmpfs_path, "tmpfs", MountFlags::empty(), "", @@ -113,9 +118,10 @@ pub fn on_post_data_fs() -> Result<()> { } // mount module systemlessly by magic mount - if !Path::new(NO_MOUNT_PATH).exists() { - if let Err(e) = mount_modules_systemlessly() { - warn!("do systemless mount failed: {}", e); + #[cfg(target_os = "android")] + if !no_mount { + if let Err(e) = crate::magic_mount::magic_mount(&tmpfs_path) { + warn!("do systemless mount failed: {e}"); } } else { info!("no mount requested"); @@ -148,7 +154,7 @@ pub fn on_post_data_fs() -> Result<()> { #[cfg(target_os = "android")] pub fn mount_modules_systemlessly() -> Result<()> { - crate::magic_mount::magic_mount() + crate::magic_mount::magic_mount(&find_tmp_path()) } #[cfg(not(target_os = "android"))] diff --git a/userspace/ksud/src/magic_mount.rs b/userspace/ksud/src/magic_mount.rs index e5f26820..90c5b2bd 100644 --- a/userspace/ksud/src/magic_mount.rs +++ b/userspace/ksud/src/magic_mount.rs @@ -21,7 +21,7 @@ use crate::{ defs::{DISABLE_FILE_NAME, KSU_MOUNT_SOURCE, MODULE_DIR, SKIP_MOUNT_FILE_NAME}, magic_mount::NodeFileType::{Directory, RegularFile, Symlink, Whiteout}, restorecon::{lgetfilecon, lsetfilecon}, - utils::{ensure_dir_exists, get_work_dir}, + utils::get_work_dir, }; const REPLACE_DIR_XATTR: &str = "trusted.overlay.opaque"; @@ -435,10 +435,10 @@ fn do_magic_mount, WP: AsRef>( Ok(()) } -pub fn magic_mount() -> Result<()> { +pub fn magic_mount(tmp_path: &String) -> Result<()> { if let Some(root) = collect_module_files()? { log::debug!("collected: {:#?}", root); - let tmp_dir = PathBuf::from(get_work_dir()); + let tmp_dir = Path::new(tmp_path).join("workdir"); ensure_dir_exists(&tmp_dir)?; mount(KSU_MOUNT_SOURCE, &tmp_dir, "tmpfs", MountFlags::empty(), "").context("mount tmp")?; mount_change(&tmp_dir, MountPropagationFlags::PRIVATE).context("make tmp private")?; diff --git a/userspace/ksud/src/utils.rs b/userspace/ksud/src/utils.rs index 5bd54e4b..68a13728 100644 --- a/userspace/ksud/src/utils.rs +++ b/userspace/ksud/src/utils.rs @@ -183,7 +183,7 @@ fn is_ok_empty(dir: &str) -> bool { } } -pub fn get_tmp_path() -> String { +pub fn find_tmp_path() -> String { let dirs = ["/debug_ramdisk", "/patch_hw", "/oem", "/root", "/sbin"]; // find empty directory @@ -195,11 +195,6 @@ pub fn get_tmp_path() -> String { "".to_string() } -pub fn get_work_dir() -> String { - let tmp_path = get_tmp_path(); - format!("{}/workdir/", tmp_path) -} - #[cfg(target_os = "android")] fn link_ksud_to_bin() -> Result<()> { let ksu_bin = PathBuf::from(defs::DAEMON_PATH);