Skip to content

Commit

Permalink
apd: add kpm load shell
Browse files Browse the repository at this point in the history
  • Loading branch information
Admirepowered committed Dec 8, 2024
1 parent d1200d0 commit b0ecb05
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
37 changes: 36 additions & 1 deletion apd/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use android_logger::Config;
use log::LevelFilter;

use crate::{defs, event, module, supercall, utils};

use std::ffi::{CString, CStr};
/// APatch cli
#[derive(Parser, Debug)]
#[command(author, version = defs::VERSION_CODE, about, long_about = None)]
Expand All @@ -30,6 +30,11 @@ enum Commands {
#[command(subcommand)]
command: Module,
},
/// Manage Kernel Patch modules
Kpm {
#[command(subcommand)]
command: Kpmsub,
},

/// Trigger `post-fs-data` event
PostFsData,
Expand Down Expand Up @@ -79,6 +84,17 @@ enum Module {
/// list all modules
List,
}
#[derive(clap::Subcommand, Debug)]
enum Kpmsub {
/// Load Kernelpath module
Load {
// super_key
key: String,
// kpm module path
path: String
},

}

pub fn run() -> Result<()> {
#[cfg(target_os = "android")]
Expand Down Expand Up @@ -118,6 +134,25 @@ pub fn run() -> Result<()> {

Commands::UidListener => event::start_uid_listener(),

Commands::Kpm { command } => {
match command {
Kpmsub::Load { key,path } => {
let key_cstr = CString::new(key).map_err(|_| anyhow::anyhow!("Invalid key string"))?;
let path_cstr = CString::new(path).map_err(|_| anyhow::anyhow!("Invalid path string"))?;
let ret = supercall::sc_kpm_load(key_cstr.as_c_str(),path_cstr.as_c_str(),None,std::ptr::null_mut());
if ret < 0 {
Err(anyhow::anyhow!("System call failed with error code {}", ret))
} else {
Ok(())
}

}
_ => Err(anyhow::anyhow!("Unsupported command")),

}

}

Commands::Module { command } => {
#[cfg(any(target_os = "linux", target_os = "android"))]
{
Expand Down
16 changes: 16 additions & 0 deletions apd/src/supercall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const SUPERCALL_SU_NUMS: c_long = 0x1102;
const SUPERCALL_SU_LIST: c_long = 0x1103;
const SUPERCALL_SU_RESET_PATH: c_long = 0x1111;
const SUPERCALL_SU_GET_SAFEMODE: c_long = 0x1112;
const SUPERCALL_KPM_LOAD: c_long = 0x1020;

const SUPERCALL_SCONTEXT_LEN: usize = 0x60;

Expand Down Expand Up @@ -144,7 +145,22 @@ fn sc_su(key: &CStr, profile: &SuProfile) -> c_long {
) as c_long
}
}
pub fn sc_kpm_load(key: &CStr, path: &CStr, args: Option<&CStr>, reserved: *mut c_void) -> c_long {
if key.to_bytes().is_empty() || path.to_bytes().is_empty() {
return (-EINVAL).into();
}

unsafe {
syscall(
__NR_SUPERCALL,
key.as_ptr(),
ver_and_cmd(SUPERCALL_KPM_LOAD),
path.as_ptr(),
args.map_or(std::ptr::null(), |a| a.as_ptr()),
reserved,
) as c_long
}
}
fn sc_su_reset_path(key: &CStr, path: &CStr) -> c_long {
if key.to_bytes().is_empty() || path.to_bytes().is_empty() {
return (-EINVAL).into();
Expand Down

0 comments on commit b0ecb05

Please sign in to comment.