kernel: Rewrite the kernel source code (#554)

* clean unused header

* on_module_mounted in ksud.c

* refact: use app_profile

* unified hook manager

* add zygote to hook target

* move reboot hook to supercall.c

* refactor: kernel_umount setuid_hook

* update mark rules, add init mark tracker

* remove reboot from check_syscall_fastpath

* update setuid_hook, remove uneeded sucompat enable

* log freely

* kernel: Migrate kprobe hook configuration items

* kernel: fix build

* cli: add ksud debug mark

* Fix rustfmt warning

---------

Co-authored-by: Ylarod <me@ylarod.cn>
Co-authored-by: Wang Han <416810799@qq.com>
Co-authored-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>
This commit is contained in:
ShirkNeko
2025-11-09 01:14:26 +08:00
committed by GitHub
parent 46b9f5fb4b
commit 548258f922
32 changed files with 1782 additions and 1282 deletions

View File

@@ -188,6 +188,39 @@ enum Debug {
/// For testing
Test,
/// Process mark management
Mark {
#[command(subcommand)]
command: MarkCommand,
},
}
#[derive(clap::Subcommand, Debug)]
enum MarkCommand {
/// Get mark status for a process (or all)
Get {
/// target pid (0 for total count)
#[arg(default_value = "0")]
pid: i32,
},
/// Mark a process
Mark {
/// target pid (0 for all processes)
#[arg(default_value = "0")]
pid: i32,
},
/// Unmark a process
Unmark {
/// target pid (0 for all processes)
#[arg(default_value = "0")]
pid: i32,
},
/// Refresh mark for all running processes
Refresh,
}
#[derive(clap::Subcommand, Debug)]
@@ -473,6 +506,12 @@ pub fn run() -> Result<()> {
Debug::Su { global_mnt } => crate::su::grant_root(global_mnt),
Debug::Mount => init_event::mount_modules_systemlessly(),
Debug::Test => assets::ensure_binaries(false),
Debug::Mark { command } => match command {
MarkCommand::Get { pid } => debug::mark_get(pid),
MarkCommand::Mark { pid } => debug::mark_set(pid),
MarkCommand::Unmark { pid } => debug::mark_unset(pid),
MarkCommand::Refresh => debug::mark_refresh(),
},
},
Commands::BootPatch {

View File

@@ -1,9 +1,11 @@
use anyhow::{Context, Ok, Result, ensure};
use anyhow::{Context, Ok, Result, bail, ensure};
use std::{
path::{Path, PathBuf},
process::Command,
};
use crate::ksucalls;
const KERNEL_PARAM_PATH: &str = "/sys/module/kernelsu";
fn read_u32(path: &PathBuf) -> Result<u32> {
@@ -50,3 +52,47 @@ pub fn set_manager(pkg: &str) -> Result<()> {
let _ = Command::new("am").args(["force-stop", pkg]).status();
Ok(())
}
/// Get mark status for a process
pub fn mark_get(pid: i32) -> Result<()> {
let result = ksucalls::mark_get(pid)?;
if pid == 0 {
bail!("Please specify a pid to get its mark status");
} else {
println!(
"Process {} mark status: {}",
pid,
if result != 0 { "marked" } else { "unmarked" }
);
}
Ok(())
}
/// Mark a process
pub fn mark_set(pid: i32) -> Result<()> {
ksucalls::mark_set(pid)?;
if pid == 0 {
println!("All processes marked successfully");
} else {
println!("Process {} marked successfully", pid);
}
Ok(())
}
/// Unmark a process
pub fn mark_unset(pid: i32) -> Result<()> {
ksucalls::mark_unset(pid)?;
if pid == 0 {
println!("All processes unmarked successfully");
} else {
println!("Process {} unmarked successfully", pid);
}
Ok(())
}
/// Refresh mark for all running processes
pub fn mark_refresh() -> Result<()> {
ksucalls::mark_refresh()?;
println!("Refreshed mark for all running processes");
Ok(())
}

View File

@@ -16,6 +16,7 @@ const KSU_IOCTL_CHECK_SAFEMODE: u32 = 0x80004b05; // _IOC(_IOC_READ, 'K', 5, 0)
const KSU_IOCTL_GET_FEATURE: u32 = 0xc0004b0d; // _IOC(_IOC_READ|_IOC_WRITE, 'K', 13, 0)
const KSU_IOCTL_SET_FEATURE: u32 = 0x40004b0e; // _IOC(_IOC_WRITE, 'K', 14, 0)
const KSU_IOCTL_GET_WRAPPER_FD: u32 = 0x40004b0f; // _IOC(_IOC_WRITE, 'K', 15, 0)
const KSU_IOCTL_MANAGE_MARK: u32 = 0xc0004b10; // _IOC(_IOC_READ|_IOC_WRITE, 'K', 16, 0)
#[allow(dead_code)]
const KSU_IOCTL_KPM: u32 = 0xc0004bc8; // _IOC(_IOC_READ|_IOC_WRITE, 'K', 200, 0)
#[allow(dead_code)]
@@ -68,6 +69,20 @@ struct GetWrapperFdCmd {
flags: u32,
}
#[repr(C)]
#[derive(Clone, Copy, Default)]
struct ManageMarkCmd {
operation: u32,
pid: i32,
result: u32,
}
// Mark operation constants
const KSU_MARK_GET: u32 = 1;
const KSU_MARK_MARK: u32 = 2;
const KSU_MARK_UNMARK: u32 = 3;
const KSU_MARK_REFRESH: u32 = 4;
// Global driver fd cache
#[cfg(any(target_os = "linux", target_os = "android"))]
static DRIVER_FD: OnceLock<RawFd> = OnceLock::new();
@@ -240,6 +255,50 @@ pub fn get_wrapped_fd(fd: RawFd) -> std::io::Result<RawFd> {
Ok(result)
}
/// Get mark status for a process (pid=0 returns total marked count)
pub fn mark_get(pid: i32) -> std::io::Result<u32> {
let mut cmd = ManageMarkCmd {
operation: KSU_MARK_GET,
pid,
result: 0,
};
ksuctl(KSU_IOCTL_MANAGE_MARK, &mut cmd as *mut _)?;
Ok(cmd.result)
}
/// Mark a process (pid=0 marks all processes)
pub fn mark_set(pid: i32) -> std::io::Result<()> {
let mut cmd = ManageMarkCmd {
operation: KSU_MARK_MARK,
pid,
result: 0,
};
ksuctl(KSU_IOCTL_MANAGE_MARK, &mut cmd as *mut _)?;
Ok(())
}
/// Unmark a process (pid=0 unmarks all processes)
pub fn mark_unset(pid: i32) -> std::io::Result<()> {
let mut cmd = ManageMarkCmd {
operation: KSU_MARK_UNMARK,
pid,
result: 0,
};
ksuctl(KSU_IOCTL_MANAGE_MARK, &mut cmd as *mut _)?;
Ok(())
}
/// Refresh mark for all running processes
pub fn mark_refresh() -> std::io::Result<()> {
let mut cmd = ManageMarkCmd {
operation: KSU_MARK_REFRESH,
pid: 0,
result: 0,
};
ksuctl(KSU_IOCTL_MANAGE_MARK, &mut cmd as *mut _)?;
Ok(())
}
#[repr(C)]
#[derive(Clone, Copy, Default)]
#[allow(dead_code)]