kernel: precise trigger timing of post-fs-data (#118)

* kernel: add report_event cmd

* ksud: report event

* kernel: trigger on_post_fs_data

* ksud: comment unused code

* [skip ci] run clang-format

Signed-off-by: Ylarod <me@ylarod.cn>

* ci: use custom key to sign official bootimgs

* format ksud

* reject non root

* remove

Signed-off-by: Ylarod <me@ylarod.cn>
This commit is contained in:
Ylarod
2023-01-26 11:29:02 +08:00
committed by GitHub
parent 22b66b6672
commit db600d5ea0
19 changed files with 170 additions and 37 deletions

View File

@@ -445,6 +445,7 @@ dependencies = [
"env_logger",
"humansize",
"java-properties",
"libc",
"log",
"regex",
"retry",

View File

@@ -22,6 +22,7 @@ regex = "1.5.4"
encoding = "0.2.33"
retry = "2.0.0"
humansize = "2.0.0"
libc = "0.2"
[profile.release]
strip = true

View File

@@ -1,7 +1,7 @@
use anyhow::Result;
use anyhow::{Ok, Result};
use clap::Parser;
use crate::{event, module, debug, apk_sign};
use crate::{apk_sign, debug, event, module};
/// KernelSU userspace cli
#[derive(Parser, Debug)]
@@ -58,6 +58,9 @@ enum Debug {
apk: String,
},
/// Get kernel version
Version,
/// For testing
Test,
}
@@ -115,17 +118,19 @@ pub fn run() -> Result<()> {
Commands::Sepolicy => todo!(),
Commands::Services => event::on_services(),
Commands::Debug { command } => {
match command {
Debug::SetManager { apk } => debug::set_manager(&apk),
Debug::GetSign { apk } => {
let sign = apk_sign::get_apk_signature(&apk)?;
println!("size: {:#x}, hash: {:#x}", sign.0, sign.1);
Ok(())
},
Debug::Test => todo!(),
Commands::Debug { command } => match command {
Debug::SetManager { apk } => debug::set_manager(&apk),
Debug::GetSign { apk } => {
let sign = apk_sign::get_apk_signature(&apk)?;
println!("size: {:#x}, hash: {:#x}", sign.0, sign.1);
Ok(())
}
}
Debug::Version => {
println!("Kernel Version: {}", crate::ksu::get_version());
Ok(())
}
Debug::Test => todo!(),
},
};
if let Err(e) = &result {

View File

@@ -14,4 +14,4 @@ pub const MODULE_UPDATE_TMP_DIR: &str = concatcp!(WORKING_DIR, "modules_update/"
pub const DISABLE_FILE_NAME: &str = "disable";
pub const UPDATE_FILE_NAME: &str = "update";
pub const REMOVE_FILE_NAME: &str = "remove";
pub const REMOVE_FILE_NAME: &str = "remove";

View File

@@ -89,6 +89,7 @@ pub fn do_systemless_mount(module_dir: &str) -> Result<()> {
}
pub fn on_post_data_fs() -> Result<()> {
crate::ksu::report_post_fs_data();
let module_update_img = defs::MODULE_UPDATE_IMG;
let module_img = defs::MODULE_IMG;
let module_dir = defs::MODULE_DIR;
@@ -151,6 +152,7 @@ pub fn on_services() -> Result<()> {
}
pub fn on_boot_completed() -> Result<()> {
crate::ksu::report_boot_complete();
let module_update_img = Path::new(defs::MODULE_UPDATE_IMG);
let module_img = Path::new(defs::MODULE_IMG);
if module_update_img.exists() {

58
userspace/ksud/src/ksu.rs Normal file
View File

@@ -0,0 +1,58 @@
const KERNEL_SU_OPTION: u32 = 0xDEADBEEF;
// const CMD_GRANT_ROOT: u64 = 0;
// const CMD_BECOME_MANAGER: u64 = 1;
const CMD_GET_VERSION: u64 = 2;
// const CMD_ALLOW_SU: u64 = 3;
// const CMD_DENY_SU: u64 = 4;
// const CMD_GET_ALLOW_LIST: u64 = 5;
// const CMD_GET_DENY_LIST: u64 = 6;
const CMD_REPORT_EVENT: u64 = 7;
const EVENT_POST_FS_DATA: u64 = 1;
const EVENT_BOOT_COMPLETED: u64 = 2;
// pub fn grant_root() -> bool {
// let mut result: i32 = 0;
// unsafe {
// libc::prctl(
// KERNEL_SU_OPTION as i32,
// CMD_GRANT_ROOT,
// 0,
// 0,
// &mut result as *mut _ as *mut libc::c_void,
// );
// }
// return result as u32 == KERNEL_SU_OPTION;
// }
pub fn get_version() -> i32 {
let mut result: i32 = 0;
unsafe {
libc::prctl(
KERNEL_SU_OPTION as i32,
CMD_GET_VERSION,
&mut result as *mut _ as *mut libc::c_void,
);
}
return result;
}
fn report_event(event: u64){
unsafe {
libc::prctl(
KERNEL_SU_OPTION as i32,
CMD_REPORT_EVENT,
event,
);
}
}
pub fn report_post_fs_data(){
report_event(EVENT_POST_FS_DATA);
}
pub fn report_boot_complete(){
report_event(EVENT_BOOT_COMPLETED);
}

View File

@@ -1,11 +1,12 @@
mod cli;
mod event;
mod module;
mod defs;
mod utils;
mod restorecon;
mod debug;
mod apk_sign;
mod cli;
mod debug;
mod defs;
mod event;
mod ksu;
mod module;
mod restorecon;
mod utils;
fn main() -> anyhow::Result<()> {
cli::run()

View File

@@ -109,7 +109,11 @@ fn check_image(img: &str) -> Result<()> {
// 0: no error
// 1: file system errors corrected
// https://man7.org/linux/man-pages/man8/e2fsck.8.html
ensure!(code == Some(0) || code == Some(1), "check image e2fsck exec failed: {}", code.unwrap_or(-1));
ensure!(
code == Some(0) || code == Some(1),
"check image e2fsck exec failed: {}",
code.unwrap_or(-1)
);
Ok(())
}
@@ -120,7 +124,10 @@ fn grow_image_size(img: &str, extra_size: u64) -> Result<()> {
// check image
check_image(img)?;
println!("- Target image size: {}", humansize::format_size(target_size, humansize::DECIMAL));
println!(
"- Target image size: {}",
humansize::format_size(target_size, humansize::DECIMAL)
);
let target_size = target_size / 1024 + 1;
let result = Exec::shell(format!("resize2fs {} {}K", img, target_size))
@@ -331,7 +338,10 @@ pub fn install_module(zip: String) -> Result<()> {
let grow_size_per_m = grow_size / 1024 / 1024 + 1;
println!("- Preparing image");
println!("- Module size: {}", humansize::format_size(zip_uncompressed_size, humansize::DECIMAL));
println!(
"- Module size: {}",
humansize::format_size(zip_uncompressed_size, humansize::DECIMAL)
);
if !modules_img_exist && !modules_update_img_exist {
// if no modules and modules_update, it is brand new installation, we should create a new img

View File

@@ -24,4 +24,4 @@ pub fn restore_syscon(dir: &str) -> Result<()> {
let result = Exec::shell(cmd).join()?;
ensure!(result.success(), "chcon for: {} failed.", dir);
Ok(())
}
}