diff --git a/userspace/ksud/src/apk_sign.rs b/userspace/ksud/src/apk_sign.rs index 356cdc6cb2c2..bf325a56ed4c 100644 --- a/userspace/ksud/src/apk_sign.rs +++ b/userspace/ksud/src/apk_sign.rs @@ -1,7 +1,7 @@ use anyhow::{ensure, Result}; use std::io::{Read, Seek, SeekFrom}; -pub fn get_apk_signature(apk: &str) -> Result { +pub fn get_apk_signature(apk: &str) -> Result<(u32, String)> { let mut buffer = [0u8; 0x10]; let mut size4 = [0u8; 4]; let mut size8 = [0u8; 8]; @@ -49,8 +49,8 @@ pub fn get_apk_signature(apk: &str) -> Result { ensure!(size_of_block == size8, "not a signed apk"); - let mut v2_signing: Option = None; - let mut v3_signing: Option = None; + let mut v2_signing: Option<(u32, String)> = None; + let mut v3_signing: Option<(u32, String)> = None; loop { let mut id = [0u8; 4]; let mut offset = 4u32; @@ -83,8 +83,8 @@ pub fn get_apk_signature(apk: &str) -> Result { } else { Err(anyhow::anyhow!( "Inconsisent signature, v2: {}, v3: {}!", - s1, - s2 + s1.1, + s2.1 )) } } @@ -96,7 +96,7 @@ fn calc_cert_sha256( f: &mut std::fs::File, size4: &mut [u8; 4], offset: &mut u32, -) -> Result { +) -> Result<(u32, String)> { f.read_exact(size4)?; // signer-sequence length f.read_exact(size4)?; // signer length f.read_exact(size4)?; // signed data length @@ -116,5 +116,5 @@ fn calc_cert_sha256( f.read_exact(&mut cert)?; *offset += cert_len; - Ok(sha256::digest(&cert)) + Ok((cert_len, sha256::digest(&cert))) } diff --git a/userspace/ksud/src/cli.rs b/userspace/ksud/src/cli.rs index 5d020c3be1b7..410195a9e05a 100644 --- a/userspace/ksud/src/cli.rs +++ b/userspace/ksud/src/cli.rs @@ -228,7 +228,7 @@ pub fn run() -> Result<()> { Debug::SetManager { apk } => debug::set_manager(&apk), Debug::GetSign { apk } => { let sign = apk_sign::get_apk_signature(&apk)?; - println!("hash: {}", sign); + println!("size: {:#x}, hash: {}", sign.0, sign.1); Ok(()) } Debug::Version => { diff --git a/userspace/ksud/src/debug.rs b/userspace/ksud/src/debug.rs index 83dafcea5ee3..8a3e0b5fb9b5 100644 --- a/userspace/ksud/src/debug.rs +++ b/userspace/ksud/src/debug.rs @@ -1,23 +1,35 @@ use anyhow::{ensure, Context, Ok, Result}; -use std::{path::Path, process::Command}; +use std::{path::{Path, PathBuf}, process::Command}; use crate::apk_sign::get_apk_signature; const KERNEL_PARAM_PATH: &str = "/sys/module/kernelsu"; -fn set_kernel_param(hash: String) -> Result<()> { +fn read_u32(path: &PathBuf) -> Result { + let content = std::fs::read_to_string(path)?; + let content = content.trim(); + let content = content.parse::()?; + Ok(content) +} + +fn set_kernel_param(size: u32, hash: String) -> Result<()> { let kernel_param_path = Path::new(KERNEL_PARAM_PATH).join("parameters"); + + let expeced_size_path = kernel_param_path.join("ksu_expected_size"); let expeced_hash_path = kernel_param_path.join("ksu_expected_hash"); println!( - "before hash: {}", + "before size: {:#x} hash: {}", + read_u32(&expeced_size_path)?, std::fs::read_to_string(&expeced_hash_path)? ); + std::fs::write(&expeced_size_path, size.to_string())?; std::fs::write(&expeced_hash_path, hash)?; println!( - "after hash: {}", + "after size: {:#x} hash: {}", + read_u32(&expeced_size_path)?, std::fs::read_to_string(&expeced_hash_path)? ); @@ -42,6 +54,6 @@ pub fn set_manager(pkg: &str) -> Result<()> { let path = get_apk_path(pkg).with_context(|| format!("{pkg} does not exist!"))?; let sign = get_apk_signature(&path)?; - set_kernel_param(sign)?; + set_kernel_param(sign.0, sign.1)?; Ok(()) }