Skip to content

Commit

Permalink
ksud: calc size and hash
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Oct 11, 2023
1 parent e8652d1 commit 4a8df07
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
14 changes: 7 additions & 7 deletions userspace/ksud/src/apk_sign.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{ensure, Result};
use std::io::{Read, Seek, SeekFrom};

pub fn get_apk_signature(apk: &str) -> Result<String> {
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];
Expand Down Expand Up @@ -49,8 +49,8 @@ pub fn get_apk_signature(apk: &str) -> Result<String> {

ensure!(size_of_block == size8, "not a signed apk");

let mut v2_signing: Option<String> = None;
let mut v3_signing: Option<String> = 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;
Expand Down Expand Up @@ -83,8 +83,8 @@ pub fn get_apk_signature(apk: &str) -> Result<String> {
} else {
Err(anyhow::anyhow!(
"Inconsisent signature, v2: {}, v3: {}!",
s1,
s2
s1.1,
s2.1
))
}
}
Expand All @@ -96,7 +96,7 @@ fn calc_cert_sha256(
f: &mut std::fs::File,
size4: &mut [u8; 4],
offset: &mut u32,
) -> Result<String> {
) -> Result<(u32, String)> {
f.read_exact(size4)?; // signer-sequence length
f.read_exact(size4)?; // signer length
f.read_exact(size4)?; // signed data length
Expand All @@ -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)))
}
2 changes: 1 addition & 1 deletion userspace/ksud/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
22 changes: 17 additions & 5 deletions userspace/ksud/src/debug.rs
Original file line number Diff line number Diff line change
@@ -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<u32> {
let content = std::fs::read_to_string(path)?;
let content = content.trim();
let content = content.parse::<u32>()?;
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)?
);

Expand All @@ -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(())
}

0 comments on commit 4a8df07

Please sign in to comment.