Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
CanerKaraca23 authored Mar 11, 2024
2 parents ecaed35 + d77988c commit 939aca1
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 104 deletions.
12 changes: 8 additions & 4 deletions userspace/ksud/src/boot_patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,14 @@ pub fn patch(
.status()?;
ensure!(status.success(), "magiskboot unpack failed");

let status = do_cpio_cmd(&magiskboot, workding_dir.path(), "exists init");
if status.is_ok() {
// init exist, backup it.
do_cpio_cmd(&magiskboot, workding_dir.path(), "mv init init.real")?;
let is_kernelsu_patched =
do_cpio_cmd(&magiskboot, workding_dir.path(), "exists kernelsu.ko").is_ok();
if !is_kernelsu_patched {
// kernelsu.ko is not exist, backup init if necessary
let status = do_cpio_cmd(&magiskboot, workding_dir.path(), "exists init");
if status.is_ok() {
do_cpio_cmd(&magiskboot, workding_dir.path(), "mv init init.real")?;
}
}

do_cpio_cmd(&magiskboot, workding_dir.path(), "add 0755 init init")?;
Expand Down
18 changes: 9 additions & 9 deletions userspace/ksud/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use android_logger::Config;
#[cfg(target_os = "android")]
use log::LevelFilter;

use crate::{apk_sign, debug, defs, event, module, utils};
use crate::{apk_sign, debug, defs, init_event, ksucalls, module, utils};

/// KernelSU userspace cli
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -239,16 +239,16 @@ pub fn run() -> Result<()> {
// the kernel executes su with argv[0] = "su" and replace it with us
let arg0 = std::env::args().next().unwrap_or_default();
if arg0 == "su" || arg0 == "/system/bin/su" {
return crate::ksu::root_shell();
return crate::su::root_shell();
}

let cli = Args::parse();

log::info!("command: {:?}", cli.command);

let result = match cli.command {
Commands::PostFsData => event::on_post_data_fs(),
Commands::BootCompleted => event::on_boot_completed(),
Commands::PostFsData => init_event::on_post_data_fs(),
Commands::BootCompleted => init_event::on_boot_completed(),

Commands::Module { command } => {
#[cfg(any(target_os = "linux", target_os = "android"))]
Expand All @@ -265,13 +265,13 @@ pub fn run() -> Result<()> {
Module::Shrink => module::shrink_ksu_images(),
}
}
Commands::Install => event::install(),
Commands::Install => utils::install(),
Commands::Sepolicy { command } => match command {
Sepolicy::Patch { sepolicy } => crate::sepolicy::live_patch(&sepolicy),
Sepolicy::Apply { file } => crate::sepolicy::apply_file(file),
Sepolicy::Check { sepolicy } => crate::sepolicy::check_rule(&sepolicy),
},
Commands::Services => event::on_services(),
Commands::Services => init_event::on_services(),
Commands::Profile { command } => match command {
Profile::GetSepolicy { package } => crate::profile::get_sepolicy(package),
Profile::SetSepolicy { package, policy } => {
Expand All @@ -291,11 +291,11 @@ pub fn run() -> Result<()> {
Ok(())
}
Debug::Version => {
println!("Kernel Version: {}", crate::ksu::get_version());
println!("Kernel Version: {}", ksucalls::get_version());
Ok(())
}
Debug::Su { global_mnt } => crate::ksu::grant_root(global_mnt),
Debug::Mount => event::mount_systemlessly(defs::MODULE_DIR),
Debug::Su { global_mnt } => crate::su::grant_root(global_mnt),
Debug::Mount => init_event::mount_modules_systemlessly(defs::MODULE_DIR),
Debug::Xcp {
src,
dst,
Expand Down
56 changes: 16 additions & 40 deletions userspace/ksud/src/event.rs → userspace/ksud/src/init_event.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use anyhow::{bail, Context, Result};
use log::{info, warn};
#[cfg(target_os = "android")]
use std::path::PathBuf;
use std::{collections::HashMap, path::Path};

use crate::module::prune_modules;
use crate::{
assets, defs, mount, restorecon,
utils::{self, ensure_clean_dir, ensure_dir_exists},
assets, defs, ksucalls, mount, restorecon,
utils::{self, ensure_clean_dir},
};

fn mount_partition(partition_name: &str, lowerdir: &Vec<String>) -> Result<()> {
Expand Down Expand Up @@ -35,7 +33,7 @@ fn mount_partition(partition_name: &str, lowerdir: &Vec<String>) -> Result<()> {
mount::mount_overlay(&partition, lowerdir, workdir, upperdir)
}

pub fn mount_systemlessly(module_dir: &str) -> Result<()> {
pub fn mount_modules_systemlessly(module_dir: &str) -> Result<()> {
// construct overlay mount params
let dir = std::fs::read_dir(module_dir);
let Ok(dir) = dir else {
Expand Down Expand Up @@ -99,12 +97,14 @@ pub fn mount_systemlessly(module_dir: &str) -> Result<()> {
}

pub fn on_post_data_fs() -> Result<()> {
crate::ksu::report_post_fs_data();
ksucalls::report_post_fs_data();

utils::umask(0);

#[cfg(unix)]
let _ = catch_bootlog();
let _ = catch_bootlog("logcat", vec!["logcat"]);
#[cfg(unix)]
let _ = catch_bootlog("dmesg", vec!["dmesg", "-w"]);

if utils::has_magisk() {
warn!("Magisk detected, skip post-fs-data!");
Expand Down Expand Up @@ -162,7 +162,7 @@ pub fn on_post_data_fs() -> Result<()> {
.with_context(|| "mount module image failed".to_string())?;

// tell kernel that we've mount the module, so that it can do some optimization
crate::ksu::report_module_mounted();
ksucalls::report_module_mounted();

// if we are in safe mode, we should disable all modules
if safe_mode {
Expand Down Expand Up @@ -207,7 +207,7 @@ pub fn on_post_data_fs() -> Result<()> {
}

// mount module systemlessly by overlay
if let Err(e) = mount_systemlessly(module_dir) {
if let Err(e) = mount_modules_systemlessly(module_dir) {
warn!("do systemless mount failed: {}", e);
}

Expand Down Expand Up @@ -247,7 +247,7 @@ pub fn on_services() -> Result<()> {
}

pub fn on_boot_completed() -> Result<()> {
crate::ksu::report_boot_complete();
ksucalls::report_boot_complete();
info!("on_boot_completed triggered!");
let module_update_img = Path::new(defs::MODULE_UPDATE_IMG);
let module_img = Path::new(defs::MODULE_IMG);
Expand All @@ -266,45 +266,24 @@ pub fn on_boot_completed() -> Result<()> {
Ok(())
}

pub fn install() -> Result<()> {
ensure_dir_exists(defs::ADB_DIR)?;
std::fs::copy("/proc/self/exe", defs::DAEMON_PATH)?;
restorecon::lsetfilecon(defs::DAEMON_PATH, restorecon::ADB_CON)?;
// install binary assets
assets::ensure_binaries(false).with_context(|| "Failed to extract assets")?;

#[cfg(target_os = "android")]
link_ksud_to_bin()?;

Ok(())
}

#[cfg(target_os = "android")]
fn link_ksud_to_bin() -> Result<()> {
let ksu_bin = PathBuf::from(defs::DAEMON_PATH);
let ksu_bin_link = PathBuf::from(defs::DAEMON_LINK_PATH);
if ksu_bin.exists() && !ksu_bin_link.exists() {
std::os::unix::fs::symlink(&ksu_bin, &ksu_bin_link)?;
}
Ok(())
}

#[cfg(unix)]
fn catch_bootlog() -> Result<()> {
fn catch_bootlog(logname: &str, command: Vec<&str>) -> Result<()> {
use std::os::unix::process::CommandExt;
use std::process::Stdio;

let logdir = Path::new(defs::LOG_DIR);
utils::ensure_dir_exists(logdir)?;
let bootlog = logdir.join("boot.log");
let oldbootlog = logdir.join("boot.old.log");
let bootlog = logdir.join(format!("{logname}.log"));
let oldbootlog = logdir.join(format!("{logname}.old.log"));

if bootlog.exists() {
std::fs::rename(&bootlog, oldbootlog)?;
}

let bootlog = std::fs::File::create(bootlog)?;

let mut args = vec!["-s", "9", "30s"];
args.extend_from_slice(&command);
// timeout -s 9 30s logcat > boot.log
let result = unsafe {
std::process::Command::new("timeout")
Expand All @@ -313,10 +292,7 @@ fn catch_bootlog() -> Result<()> {
utils::switch_cgroups();
Ok(())
})
.arg("-s")
.arg("9")
.arg("30s")
.arg("logcat")
.args(args)
.stdout(Stdio::from(bootlog))
.spawn()
};
Expand Down
43 changes: 43 additions & 0 deletions userspace/ksud/src/ksucalls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const EVENT_POST_FS_DATA: u64 = 1;
const EVENT_BOOT_COMPLETED: u64 = 2;
const EVENT_MODULE_MOUNTED: u64 = 3;

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn get_version() -> i32 {
rustix::process::ksu_get_version()
}

#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub fn get_version() -> i32 {
0
}

#[cfg(any(target_os = "linux", target_os = "android"))]
fn report_event(event: u64) {
rustix::process::ksu_report_event(event)
}

#[cfg(not(any(target_os = "linux", target_os = "android")))]
fn report_event(_event: u64) {}

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn check_kernel_safemode() -> bool {
rustix::process::ksu_check_kernel_safemode()
}

#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub fn check_kernel_safemode() -> bool {
false
}

pub fn report_post_fs_data() {
report_event(EVENT_POST_FS_DATA);
}

pub fn report_boot_complete() {
report_event(EVENT_BOOT_COMPLETED);
}

pub fn report_module_mounted() {
report_event(EVENT_MODULE_MOUNTED);
}
5 changes: 3 additions & 2 deletions userspace/ksud/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ mod boot_patch;
mod cli;
mod debug;
mod defs;
mod event;
mod ksu;
mod init_event;
mod ksucalls;
mod module;
mod mount;
mod profile;
mod restorecon;
mod sepolicy;
mod su;
mod utils;

fn main() -> anyhow::Result<()> {
Expand Down
6 changes: 3 additions & 3 deletions userspace/ksud/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[allow(clippy::wildcard_imports)]
use crate::utils::*;
use crate::{
assets, defs, mount,
assets, defs, ksucalls, mount,
restorecon::{restore_syscon, setsyscon},
sepolicy, utils,
};
Expand Down Expand Up @@ -53,7 +53,7 @@ fn exec_install_script(module_file: &str) -> Result<()> {
),
)
.env("KSU", "true")
.env("KSU_KERNEL_VER_CODE", crate::ksu::get_version().to_string())
.env("KSU_KERNEL_VER_CODE", ksucalls::get_version().to_string())
.env("KSU_VER", defs::VERSION_NAME)
.env("KSU_VER_CODE", defs::VERSION_CODE)
.env("OUTFD", "1")
Expand Down Expand Up @@ -178,7 +178,7 @@ fn exec_script<T: AsRef<Path>>(path: T, wait: bool) -> Result<()> {
.arg(path.as_ref())
.env("ASH_STANDALONE", "1")
.env("KSU", "true")
.env("KSU_KERNEL_VER_CODE", crate::ksu::get_version().to_string())
.env("KSU_KERNEL_VER_CODE", ksucalls::get_version().to_string())
.env("KSU_VER_CODE", defs::VERSION_CODE)
.env("KSU_VER", defs::VERSION_NAME)
.env(
Expand Down
44 changes: 0 additions & 44 deletions userspace/ksud/src/ksu.rs → userspace/ksud/src/su.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ use rustix::{
thread::{set_thread_res_gid, set_thread_res_uid, Gid, Uid},
};

const EVENT_POST_FS_DATA: u64 = 1;
const EVENT_BOOT_COMPLETED: u64 = 2;
const EVENT_MODULE_MOUNTED: u64 = 3;

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn grant_root(global_mnt: bool) -> Result<()> {
const KERNEL_SU_OPTION: u32 = 0xDEAD_BEEF;
Expand Down Expand Up @@ -303,43 +299,3 @@ fn add_path_to_env(path: &str) -> Result<()> {
env::set_var("PATH", new_path_env);
Ok(())
}

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn get_version() -> i32 {
rustix::process::ksu_get_version()
}

#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub fn get_version() -> i32 {
0
}

#[cfg(any(target_os = "linux", target_os = "android"))]
fn report_event(event: u64) {
rustix::process::ksu_report_event(event)
}

#[cfg(not(any(target_os = "linux", target_os = "android")))]
fn report_event(_event: u64) {}

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn check_kernel_safemode() -> bool {
rustix::process::ksu_check_kernel_safemode()
}

#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub fn check_kernel_safemode() -> bool {
false
}

pub fn report_post_fs_data() {
report_event(EVENT_POST_FS_DATA);
}

pub fn report_boot_complete() {
report_event(EVENT_BOOT_COMPLETED);
}

pub fn report_module_mounted() {
report_event(EVENT_MODULE_MOUNTED);
}
Loading

0 comments on commit 939aca1

Please sign in to comment.