Skip to content

Commit

Permalink
ksud: support global mnt for debug su
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Feb 23, 2024
1 parent 8e44876 commit cbc04ff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
8 changes: 6 additions & 2 deletions userspace/ksud/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ enum Debug {
},

/// Root Shell
Su,
Su {
/// switch to gloabl mount namespace
#[arg(short, long, default_value = "false")]
global_mnt: bool,
},

/// Get kernel version
Version,
Expand Down Expand Up @@ -306,7 +310,7 @@ pub fn run() -> Result<()> {
println!("Kernel Version: {}", crate::ksu::get_version());
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::Xcp { src, dst } => {
utils::copy_sparse_file(src, dst)?;
Expand Down
31 changes: 28 additions & 3 deletions userspace/ksud/src/ksu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,34 @@ const EVENT_BOOT_COMPLETED: u64 = 2;
const EVENT_MODULE_MOUNTED: u64 = 3;

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn grant_root() -> Result<()> {
rustix::process::ksu_grant_root()?;
Ok(())
pub fn grant_root(global_mnt: bool) -> Result<()> {
const KERNEL_SU_OPTION: u32 = 0xDEAD_BEEF;
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")))]
Expand Down

0 comments on commit cbc04ff

Please sign in to comment.