ksud: support global mnt for debug su

This commit is contained in:
weishu
2024-02-23 17:57:40 +08:00
parent 8e448767a5
commit cbc04ff6df
2 changed files with 34 additions and 5 deletions

View File

@@ -105,7 +105,11 @@ enum Debug {
}, },
/// Root Shell /// Root Shell
Su, Su {
/// switch to gloabl mount namespace
#[arg(short, long, default_value = "false")]
global_mnt: bool,
},
/// Get kernel version /// Get kernel version
Version, Version,
@@ -306,7 +310,7 @@ pub fn run() -> Result<()> {
println!("Kernel Version: {}", crate::ksu::get_version()); println!("Kernel Version: {}", crate::ksu::get_version());
Ok(()) Ok(())
} }
Debug::Su => crate::ksu::grant_root(), Debug::Su { global_mnt } => crate::ksu::grant_root(global_mnt),
Debug::Mount => event::mount_systemlessly(defs::MODULE_DIR), Debug::Mount => event::mount_systemlessly(defs::MODULE_DIR),
Debug::Xcp { src, dst } => { Debug::Xcp { src, dst } => {
utils::copy_sparse_file(src, dst)?; utils::copy_sparse_file(src, dst)?;

View File

@@ -22,9 +22,34 @@ const EVENT_BOOT_COMPLETED: u64 = 2;
const EVENT_MODULE_MOUNTED: u64 = 3; const EVENT_MODULE_MOUNTED: u64 = 3;
#[cfg(any(target_os = "linux", target_os = "android"))] #[cfg(any(target_os = "linux", target_os = "android"))]
pub fn grant_root() -> Result<()> { pub fn grant_root(global_mnt: bool) -> Result<()> {
rustix::process::ksu_grant_root()?; const KERNEL_SU_OPTION: u32 = 0xDEAD_BEEF;
Ok(()) const CMD_GRANT_ROOT: u64 = 0;
let mut result: u32 = 0;
unsafe {
#[allow(clippy::cast_possible_wrap)]
libc::prctl(
KERNEL_SU_OPTION as i32, // supposed to overflow
CMD_GRANT_ROOT,
0,
0,
std::ptr::addr_of_mut!(result).cast::<libc::c_void>(),
);
}
anyhow::ensure!(result == KERNEL_SU_OPTION, "grant root failed");
let mut command = std::process::Command::new("sh");
let command = unsafe {
command.pre_exec(move || {
if global_mnt {
let _ = utils::switch_mnt_ns(1);
let _ = utils::unshare_mnt_ns();
}
std::result::Result::Ok(())
})
};
Err(command.exec().into())
} }
#[cfg(not(any(target_os = "linux", target_os = "android")))] #[cfg(not(any(target_os = "linux", target_os = "android")))]