From f963e40a5fed6e89529e83ac3b20d0f9d7f378b9 Mon Sep 17 00:00:00 2001 From: 5ec1cff <56485584+5ec1cff@users.noreply.github.com> Date: Mon, 17 Apr 2023 12:05:13 +0800 Subject: [PATCH] ksud: fix bind mount failed again (#395) This fixes https://github.com/Dr-TSNG/ZygiskOnKernelSU/issues/18 --- userspace/ksud/src/event.rs | 4 +++- userspace/ksud/src/mount.rs | 15 ++++----------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/userspace/ksud/src/event.rs b/userspace/ksud/src/event.rs index 6c97101a..0c34b3db 100644 --- a/userspace/ksud/src/event.rs +++ b/userspace/ksud/src/event.rs @@ -164,11 +164,13 @@ pub fn on_post_data_fs() -> Result<()> { warn!("load system.prop failed: {}", e); } - // mount moduke systemlessly by overlay + // mount module systemlessly by overlay if let Err(e) = mount_systemlessly(module_dir) { warn!("do systemless mount failed: {}", e); } + std::env::set_current_dir("/").with_context(|| "failed to chdir to /")?; + Ok(()) } diff --git a/userspace/ksud/src/mount.rs b/userspace/ksud/src/mount.rs index 704849e1..424f02e8 100644 --- a/userspace/ksud/src/mount.rs +++ b/userspace/ksud/src/mount.rs @@ -11,12 +11,6 @@ use crate::defs::KSU_OVERLAY_SOURCE; use log::{info, warn}; #[cfg(any(target_os = "linux", target_os = "android"))] use procfs::process::Process; -#[cfg(any(target_os = "linux", target_os = "android"))] -use std::fs::File; -#[cfg(any(target_os = "linux", target_os = "android"))] -use std::os::fd::AsRawFd; -#[cfg(any(target_os = "linux", target_os = "android"))] -use std::os::unix::fs::OpenOptionsExt; use std::path::Path; pub struct AutoMountExt4 { @@ -32,6 +26,7 @@ impl AutoMountExt4 { let result = Mount::builder() .fstype(FilesystemType::from("ext4")) .flags(MountFlags::empty()) + .create_loop(true) .mount(src, mnt) .map(|mount| { Ok(Self { @@ -106,6 +101,7 @@ fn mount_image(src: &str, target: &str, autodrop: bool) -> Result<()> { if autodrop { Mount::builder() .fstype(FilesystemType::from("ext4")) + .create_loop(true) .mount_autodrop(src, target, UnmountFlags::empty()) .with_context(|| format!("Failed to do mount: {src} -> {target}"))?; } else { @@ -225,11 +221,8 @@ fn mount_overlay_child( #[cfg(any(target_os = "linux", target_os = "android"))] pub fn mount_overlay(root: &String, module_roots: &Vec) -> Result<()> { info!("mount overlay for {}", root); - let stock_root = File::options() - .read(true) - .custom_flags(libc::O_PATH) - .open(root)?; - let stock_root = format!("/proc/self/fd/{}", stock_root.as_raw_fd()); + std::env::set_current_dir(root).with_context(|| format!("failed to chdir to {root}"))?; + let stock_root = "."; // collect child mounts before mounting the root let mounts = Process::myself()?