From 0a804ba170a01db62c28a675161a469581376b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=9F=E4=BA=8E=E7=94=9F=E6=97=B6=20=E4=BA=A1=E4=BA=8E?= =?UTF-8?q?=E4=BA=A1=E5=88=BB?= <127004703+Tools-cx-app@users.noreply.github.com> Date: Sun, 12 Oct 2025 15:48:24 +0800 Subject: [PATCH] feat: Optimize some codes (ksud) (#465) * chore: make cargo clippy happy Signed-off-by: Tools-app * chore: Optimize import - Format as a standard import Signed-off-by: Tools-app --------- Signed-off-by: Tools-app --- userspace/ksud/Cargo.lock | 74 +++++++++++++++---------------- userspace/ksud/build.rs | 10 ++--- userspace/ksud/src/boot_patch.rs | 65 ++++++++++++--------------- userspace/ksud/src/cli.rs | 14 +++--- userspace/ksud/src/init_event.rs | 48 ++++++++++---------- userspace/ksud/src/kpm.rs | 57 +++++++++++++++--------- userspace/ksud/src/magic_mount.rs | 49 +++++++++++--------- userspace/ksud/src/main.rs | 6 +-- userspace/ksud/src/module.rs | 50 ++++++++++----------- userspace/ksud/src/profile.rs | 7 +-- userspace/ksud/src/restorecon.rs | 18 ++++---- userspace/ksud/src/sepolicy.rs | 3 +- userspace/ksud/src/su.rs | 18 ++++---- userspace/ksud/src/uid_scanner.rs | 7 +-- userspace/ksud/src/utils.rs | 27 +++++------ 15 files changed, 232 insertions(+), 221 deletions(-) diff --git a/userspace/ksud/Cargo.lock b/userspace/ksud/Cargo.lock index 67aaca66..97f38161 100644 --- a/userspace/ksud/Cargo.lock +++ b/userspace/ksud/Cargo.lock @@ -835,6 +835,43 @@ dependencies = [ "libc", ] +[[package]] +name = "ksud" +version = "0.1.0" +dependencies = [ + "android-properties", + "android_logger", + "anyhow", + "chrono", + "clap", + "const_format", + "derive-new", + "encoding_rs", + "env_logger", + "extattr", + "fs4", + "getopts", + "humansize", + "is_executable", + "java-properties", + "jwalk", + "libc", + "log", + "nom", + "notify", + "procfs", + "regex-lite", + "rust-embed", + "rustix 0.38.34", + "serde_json", + "sha1", + "sha256", + "tempfile", + "which", + "zip", + "zip-extensions", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -1837,43 +1874,6 @@ dependencies = [ "lzma-sys", ] -[[package]] -name = "zakozako" -version = "0.1.0" -dependencies = [ - "android-properties", - "android_logger", - "anyhow", - "chrono", - "clap", - "const_format", - "derive-new", - "encoding_rs", - "env_logger", - "extattr", - "fs4", - "getopts", - "humansize", - "is_executable", - "java-properties", - "jwalk", - "libc", - "log", - "nom", - "notify", - "procfs", - "regex-lite", - "rust-embed", - "rustix 0.38.34", - "serde_json", - "sha1", - "sha256", - "tempfile", - "which", - "zip", - "zip-extensions", -] - [[package]] name = "zerocopy" version = "0.8.25" diff --git a/userspace/ksud/build.rs b/userspace/ksud/build.rs index b8337232..22a12b10 100644 --- a/userspace/ksud/build.rs +++ b/userspace/ksud/build.rs @@ -1,8 +1,4 @@ -use std::env; -use std::fs::File; -use std::io::Write; -use std::path::Path; -use std::process::Command; +use std::{env, fs::File, io::Write, path::Path, process::Command}; fn get_git_version() -> Result<(u32, String), std::io::Error> { let output = Command::new("git") @@ -14,7 +10,7 @@ fn get_git_version() -> Result<(u32, String), std::io::Error> { let version_code: u32 = version_code .trim() .parse() - .map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "Failed to parse git count"))?; + .map_err(|_| std::io::Error::other("Failed to parse git count"))?; let version_code = 10000 + 700 + version_code; // For historical reasons let version_name = String::from_utf8( @@ -23,7 +19,7 @@ fn get_git_version() -> Result<(u32, String), std::io::Error> { .output()? .stdout, ) - .map_err(|_| std::io::Error::other("Failed to read git describe stdout"))?; + .map_err(|_| std::io::Error::other("Failed to parse git count"))?; let version_name = version_name.trim_start_matches('v').to_string(); Ok((version_code, version_name)) } diff --git a/userspace/ksud/src/boot_patch.rs b/userspace/ksud/src/boot_patch.rs index 4e4d3e7d..b7168582 100644 --- a/userspace/ksud/src/boot_patch.rs +++ b/userspace/ksud/src/boot_patch.rs @@ -1,22 +1,19 @@ #[cfg(unix)] -use std::os::unix::fs::PermissionsExt; -use std::path::Path; -use std::path::PathBuf; -use std::process::Command; -use std::process::Stdio; +use std::{ + os::unix::fs::PermissionsExt, + path::{Path, PathBuf}, + process::{Command, Stdio}, +}; -use anyhow::Context; -use anyhow::Result; -use anyhow::anyhow; -use anyhow::bail; -use anyhow::ensure; +use anyhow::{Context, Result, anyhow, bail, ensure}; use regex_lite::Regex; use which::which; -use crate::defs; -use crate::defs::BACKUP_FILENAME; -use crate::defs::{KSU_BACKUP_DIR, KSU_BACKUP_FILE_PREFIX}; -use crate::{assets, utils}; +use crate::{ + assets, + defs::{self, BACKUP_FILENAME, KSU_BACKUP_DIR, KSU_BACKUP_FILE_PREFIX}, + utils, +}; #[cfg(target_os = "android")] fn ensure_gki_kernel() -> Result<()> { @@ -118,11 +115,11 @@ fn parse_kmi_from_kernel(kernel: &PathBuf, workdir: &Path) -> Result { let re = Regex::new(r"(?:.* )?(\d+\.\d+)(?:\S+)?(android\d+)").context("Failed to compile regex")?; for s in printable_strings { - if let Some(caps) = re.captures(s) { - if let (Some(kernel_version), Some(android_version)) = (caps.get(1), caps.get(2)) { - let kmi = format!("{}-{}", android_version.as_str(), kernel_version.as_str()); - return Ok(kmi); - } + if let Some(caps) = re.captures(s) + && let (Some(kernel_version), Some(android_version)) = (caps.get(1), caps.get(2)) + { + let kmi = format!("{}-{}", android_version.as_str(), kernel_version.as_str()); + return Ok(kmi); } } println!("- Failed to get KMI version"); @@ -711,10 +708,8 @@ fn do_patch( } #[cfg(target_os = "android")] - if need_backup { - if let Err(e) = do_backup(&magiskboot, workdir, &bootimage) { - println!("- Backup stock image failed: {e}"); - } + if need_backup && let Err(e) = do_backup(&magiskboot, workdir, &bootimage) { + println!("- Backup stock image failed: {e}"); } println!("- Repacking boot image"); @@ -951,16 +946,15 @@ fn find_boot_image( && !is_replace_kernel && vendor_boot_exist && !skip_init - { - if unpack_and_check_init( + && unpack_and_check_init( magiskboot, workdir, &vendor_boot_partition, "vendor_ramdisk/init_boot.cpio", - )? { - println!("- Using vendor_boot partition (vendor_ramdisk/init_boot.cpio)."); - selected_partition = &vendor_boot_partition; - } + )? + { + println!("- Using vendor_boot partition (vendor_ramdisk/init_boot.cpio)."); + selected_partition = &vendor_boot_partition; } // try vendor_boot/vendor_ramdisk/ramdisk.cpio @@ -968,16 +962,15 @@ fn find_boot_image( && !is_replace_kernel && vendor_boot_exist && !skip_init - { - if unpack_and_check_init( + && unpack_and_check_init( magiskboot, workdir, &vendor_boot_partition, "vendor_ramdisk/ramdisk.cpio", - )? { - println!("- Using vendor_boot partition (vendor_ramdisk/ramdisk.cpio)."); - selected_partition = &vendor_boot_partition; - } + )? + { + println!("- Using vendor_boot partition (vendor_ramdisk/ramdisk.cpio)."); + selected_partition = &vendor_boot_partition; } if selected_partition == &boot_partition { @@ -1034,4 +1027,4 @@ rm -f /data/adb/post-fs-data.d/post_ota.sh std::fs::set_permissions(post_ota_sh, std::fs::Permissions::from_mode(0o755))?; Ok(()) -} \ No newline at end of file +} diff --git a/userspace/ksud/src/cli.rs b/userspace/ksud/src/cli.rs index f1a0bce1..2e364044 100644 --- a/userspace/ksud/src/cli.rs +++ b/userspace/ksud/src/cli.rs @@ -7,8 +7,9 @@ use android_logger::Config; #[cfg(target_os = "android")] use log::LevelFilter; -use crate::defs::KSUD_VERBOSE_LOG_FILE; -use crate::{apk_sign, assets, debug, defs, init_event, ksucalls, module, utils}; +use crate::{ + apk_sign, assets, debug, defs, defs::KSUD_VERBOSE_LOG_FILE, init_event, ksucalls, module, utils, +}; /// KernelSU userspace cli #[derive(Parser, Debug)] @@ -288,10 +289,7 @@ mod kpm_cmd { #[derive(Subcommand, Debug)] pub enum Kpm { /// Load a KPM module: load [args] - Load { - path: PathBuf, - args: Option, - }, + Load { path: PathBuf, args: Option }, /// Unload a KPM module: unload Unload { name: String }, /// Get number of loaded modules @@ -420,7 +418,9 @@ pub fn run() -> Result<()> { Commands::Kpm { command } => { use crate::cli::kpm_cmd::Kpm; match command { - Kpm::Load { path, args } => crate::kpm::kpm_load(path.to_str().unwrap(), args.as_deref()), + Kpm::Load { path, args } => { + crate::kpm::kpm_load(path.to_str().unwrap(), args.as_deref()) + } Kpm::Unload { name } => crate::kpm::kpm_unload(&name), Kpm::Num => crate::kpm::kpm_num().map(|_| ()), Kpm::List => crate::kpm::kpm_list(), diff --git a/userspace/ksud/src/init_event.rs b/userspace/ksud/src/init_event.rs index 05784311..76b548df 100644 --- a/userspace/ksud/src/init_event.rs +++ b/userspace/ksud/src/init_event.rs @@ -1,12 +1,16 @@ -use crate::defs::{KSU_MOUNT_SOURCE, NO_MOUNT_PATH, NO_TMPFS_PATH}; -use crate::module::{handle_updated_modules, prune_modules}; -use crate::{assets, defs, ksucalls, restorecon, utils, uid_scanner}; +#[cfg(target_arch = "aarch64")] +use crate::kpm; +use crate::{ + assets, defs, + defs::{KSU_MOUNT_SOURCE, NO_MOUNT_PATH, NO_TMPFS_PATH}, + ksucalls, + module::{handle_updated_modules, prune_modules}, + restorecon, uid_scanner, utils, +}; use anyhow::{Context, Result}; use log::{info, warn}; use rustix::fs::{MountFlags, mount}; use std::path::Path; -#[cfg(target_arch = "aarch64")] -use crate::kpm; pub fn on_post_data_fs() -> Result<()> { ksucalls::report_post_fs_data(); @@ -118,27 +122,25 @@ pub fn on_post_data_fs() -> Result<()> { } // Disable Samsung Activation Verify - if let Some(model) = utils::getprop("ro.product.model") { - if model.starts_with("SM-") { - info!("Disable Samsung Activation for model {}", model); - if Path::new("/system/app/ActivationDevice_V2").exists() { - if let Err(e) = std::fs::create_dir_all("/data/local/tmp/ActivationDevice_V2") { - warn!("Failed to create directory: {}", e); - } else { - if let Err(e) = mount( - "/data/local/tmp/ActivationDevice_V2", - "/system/app/ActivationDevice_V2", - "none", - MountFlags::BIND, - "", - ) { - warn!("Failed to mount ActivationDevice_V2: {}", e); - } - } + if let Some(model) = utils::getprop("ro.product.model") + && model.starts_with("SM-") + { + info!("Disable Samsung Activation for model {}", model); + if Path::new("/system/app/ActivationDevice_V2").exists() { + if let Err(e) = std::fs::create_dir_all("/data/local/tmp/ActivationDevice_V2") { + warn!("Failed to create directory: {}", e); + } else if let Err(e) = mount( + "/data/local/tmp/ActivationDevice_V2", + "/system/app/ActivationDevice_V2", + "none", + MountFlags::BIND, + "", + ) { + warn!("Failed to mount ActivationDevice_V2: {}", e); } } } - + run_stage("post-mount", true); Ok(()) diff --git a/userspace/ksud/src/kpm.rs b/userspace/ksud/src/kpm.rs index 83248b6d..b9bac5f6 100644 --- a/userspace/ksud/src/kpm.rs +++ b/userspace/ksud/src/kpm.rs @@ -1,11 +1,11 @@ -use anyhow::{anyhow, bail, Result}; +use anyhow::{Result, anyhow, bail}; use libc::{c_int, c_ulong, prctl}; use notify::{RecursiveMode, Watcher}; use std::{ ffi::{CStr, CString, OsStr}, - path::{Path, PathBuf}, + fs, os::unix::fs::PermissionsExt, - fs, + path::{Path, PathBuf}, ptr, }; @@ -13,13 +13,13 @@ pub const KPM_DIR: &str = "/data/adb/kpm"; // SukiSU KPM prctl command space const KSU_OPTIONS: c_int = 0xdeadbeef_u32 as c_int; -const SUKISU_KPM_LOAD: c_int = 28; +const SUKISU_KPM_LOAD: c_int = 28; const SUKISU_KPM_UNLOAD: c_int = 29; -const SUKISU_KPM_NUM: c_int = 30; -const SUKISU_KPM_LIST: c_int = 31; -const SUKISU_KPM_INFO: c_int = 32; -const SUKISU_KPM_CONTROL:c_int = 33; -const SUKISU_KPM_VERSION:c_int = 34; +const SUKISU_KPM_NUM: c_int = 30; +const SUKISU_KPM_LIST: c_int = 31; +const SUKISU_KPM_INFO: c_int = 32; +const SUKISU_KPM_CONTROL: c_int = 33; +const SUKISU_KPM_VERSION: c_int = 34; /// Convert raw kernel return code to `Result`. #[inline(always)] @@ -56,7 +56,13 @@ pub fn kpm_unload(name: &str) -> Result<()> { let name_c = CString::new(name)?; let mut rc = -1; unsafe { - prctl(KSU_OPTIONS, SUKISU_KPM_UNLOAD, name_c.as_ptr() as c_ulong, 0, &mut rc as *mut _ as c_ulong); + prctl( + KSU_OPTIONS, + SUKISU_KPM_UNLOAD, + name_c.as_ptr() as c_ulong, + 0, + &mut rc as *mut _ as c_ulong, + ); } check_out(rc)?; Ok(()) @@ -65,7 +71,15 @@ pub fn kpm_unload(name: &str) -> Result<()> { /// Return loaded module count. pub fn kpm_num() -> Result { let mut rc = -1; - unsafe { prctl(KSU_OPTIONS, SUKISU_KPM_NUM, 0, 0, &mut rc as *mut _ as c_ulong) }; + unsafe { + prctl( + KSU_OPTIONS, + SUKISU_KPM_NUM, + 0, + 0, + &mut rc as *mut _ as c_ulong, + ) + }; let n = check_out(rc)?; println!("{n}"); Ok(n) @@ -122,7 +136,7 @@ pub fn kpm_control(name: &str, args: &str) -> Result { &mut rc as *mut _ as c_ulong, ); } - check_out(rc).map(|v| v as i32) + check_out(rc) } /// Print loader version string. @@ -243,12 +257,11 @@ pub fn remove_all_kpms() -> Result<()> { } for entry in fs::read_dir(dir)? { let p = entry?.path(); - if p.extension() == Some(OsStr::new("kpm")) { - if let Some(name) = p.file_stem().and_then(|s| s.to_str()) { - if let Err(e) = unload_kpm(name) { - log::error!("KPM: unload {name} failed: {e}"); - } - } + if p.extension() == Some(OsStr::new("kpm")) + && let Some(name) = p.file_stem().and_then(|s| s.to_str()) + && let Err(e) = unload_kpm(name) + { + log::error!("KPM: unload {name} failed: {e}"); } } Ok(()) @@ -282,5 +295,9 @@ pub fn load_kpm_modules() -> Result<()> { /// Convert zero-padded kernel buffer to owned String. fn buf2str(buf: &[u8]) -> String { // SAFETY: buffer is always NUL-terminated by kernel. - unsafe { CStr::from_ptr(buf.as_ptr().cast()).to_string_lossy().into_owned() } -} \ No newline at end of file + unsafe { + CStr::from_ptr(buf.as_ptr().cast()) + .to_string_lossy() + .into_owned() + } +} diff --git a/userspace/ksud/src/magic_mount.rs b/userspace/ksud/src/magic_mount.rs index 7404dd71..e5f26820 100644 --- a/userspace/ksud/src/magic_mount.rs +++ b/userspace/ksud/src/magic_mount.rs @@ -1,22 +1,28 @@ -use crate::defs::{DISABLE_FILE_NAME, KSU_MOUNT_SOURCE, MODULE_DIR, SKIP_MOUNT_FILE_NAME}; -use crate::magic_mount::NodeFileType::{Directory, RegularFile, Symlink, Whiteout}; -use crate::restorecon::{lgetfilecon, lsetfilecon}; -use crate::utils::{ensure_dir_exists, get_work_dir}; +use std::{ + cmp::PartialEq, + collections::{HashMap, hash_map::Entry}, + fs::{self, DirEntry, FileType, create_dir, create_dir_all, read_dir, read_link}, + os::unix::fs::{FileTypeExt, symlink}, + path::{Path, PathBuf}, +}; + use anyhow::{Context, Result, bail}; use extattr::lgetxattr; -use rustix::fs::{ - Gid, MetadataExt, Mode, MountFlags, MountPropagationFlags, Uid, UnmountFlags, bind_mount, - chmod, chown, mount, move_mount, remount, unmount, +use rustix::{ + fs::{ + Gid, MetadataExt, Mode, MountFlags, MountPropagationFlags, Uid, UnmountFlags, bind_mount, + chmod, chown, mount, move_mount, remount, unmount, + }, + mount::mount_change, + path::Arg, +}; + +use crate::{ + defs::{DISABLE_FILE_NAME, KSU_MOUNT_SOURCE, MODULE_DIR, SKIP_MOUNT_FILE_NAME}, + magic_mount::NodeFileType::{Directory, RegularFile, Symlink, Whiteout}, + restorecon::{lgetfilecon, lsetfilecon}, + utils::{ensure_dir_exists, get_work_dir}, }; -use rustix::mount::mount_change; -use rustix::path::Arg; -use std::cmp::PartialEq; -use std::collections::HashMap; -use std::collections::hash_map::Entry; -use std::fs; -use std::fs::{DirEntry, FileType, create_dir, create_dir_all, read_dir, read_link}; -use std::os::unix::fs::{FileTypeExt, symlink}; -use std::path::{Path, PathBuf}; const REPLACE_DIR_XATTR: &str = "trusted.overlay.opaque"; @@ -98,12 +104,11 @@ impl Node { }; if let Some(file_type) = file_type { let mut replace = false; - if file_type == Directory { - if let Ok(v) = lgetxattr(&path, REPLACE_DIR_XATTR) { - if String::from_utf8_lossy(&v) == "y" { - replace = true; - } - } + if file_type == Directory + && let Ok(v) = lgetxattr(&path, REPLACE_DIR_XATTR) + && String::from_utf8_lossy(&v) == "y" + { + replace = true; } return Some(Node { name: name.to_string(), diff --git a/userspace/ksud/src/main.rs b/userspace/ksud/src/main.rs index bb20c67a..55aaf2a3 100644 --- a/userspace/ksud/src/main.rs +++ b/userspace/ksud/src/main.rs @@ -5,6 +5,8 @@ mod cli; mod debug; mod defs; mod init_event; +#[cfg(target_arch = "aarch64")] +mod kpm; mod ksucalls; #[cfg(target_os = "android")] mod magic_mount; @@ -13,10 +15,8 @@ mod profile; mod restorecon; mod sepolicy; mod su; -mod utils; mod uid_scanner; -#[cfg(target_arch = "aarch64")] -mod kpm; +mod utils; fn main() -> anyhow::Result<()> { cli::run() diff --git a/userspace/ksud/src/module.rs b/userspace/ksud/src/module.rs index f84f316a..04de0e76 100644 --- a/userspace/ksud/src/module.rs +++ b/userspace/ksud/src/module.rs @@ -1,18 +1,6 @@ -#[allow(clippy::wildcard_imports)] -use crate::utils::*; -use crate::{ - assets, defs, ksucalls, - restorecon::{restore_syscon, setsyscon}, - sepolicy, -}; - -use anyhow::{Context, Result, anyhow, bail, ensure}; -use const_format::concatcp; -use is_executable::is_executable; -use java_properties::PropertiesIter; -use log::{info, warn}; - use std::fs::{copy, rename}; +#[cfg(unix)] +use std::os::unix::{prelude::PermissionsExt, process::CommandExt}; use std::{ collections::HashMap, env::var as env_var, @@ -22,11 +10,23 @@ use std::{ process::Command, str::FromStr, }; + +use anyhow::{Context, Result, anyhow, bail, ensure}; +use const_format::concatcp; +use is_executable::is_executable; +use java_properties::PropertiesIter; +use log::{info, warn}; use zip_extensions::zip_extract_file_to_memory; -use crate::defs::{MODULE_DIR, MODULE_UPDATE_DIR, UPDATE_FILE_NAME}; -#[cfg(unix)] -use std::os::unix::{prelude::PermissionsExt, process::CommandExt}; +#[allow(clippy::wildcard_imports)] +use crate::{ + assets, + defs::{self, MODULE_DIR, MODULE_UPDATE_DIR, UPDATE_FILE_NAME}, + ksucalls, + restorecon::{restore_syscon, setsyscon}, + sepolicy, + utils::*, +}; const INSTALLER_CONTENT: &str = include_str!("./installer.sh"); const INSTALL_MODULE_SCRIPT: &str = concatcp!( @@ -256,10 +256,10 @@ pub fn prune_modules() -> Result<()> { info!("remove module: {}", module.display()); let uninstaller = module.join("uninstall.sh"); - if uninstaller.exists() { - if let Err(e) = exec_script(uninstaller, true) { - warn!("Failed to exec uninstaller: {}", e); - } + if uninstaller.exists() + && let Err(e) = exec_script(uninstaller, true) + { + warn!("Failed to exec uninstaller: {}", e); } if let Err(e) = remove_dir_all(module) { @@ -283,10 +283,10 @@ pub fn handle_updated_modules() -> Result<()> { if let Some(name) = module.file_name() { let old_dir = modules_root.join(name); - if old_dir.exists() { - if let Err(e) = remove_dir_all(&old_dir) { - log::error!("Failed to remove old {}: {}", old_dir.display(), e); - } + if old_dir.exists() + && let Err(e) = remove_dir_all(&old_dir) + { + log::error!("Failed to remove old {}: {}", old_dir.display(), e); } if let Err(e) = rename(module, &old_dir) { log::error!("Failed to move new module {}: {}", module.display(), e); diff --git a/userspace/ksud/src/profile.rs b/userspace/ksud/src/profile.rs index e2b3f350..0584f948 100644 --- a/userspace/ksud/src/profile.rs +++ b/userspace/ksud/src/profile.rs @@ -1,8 +1,9 @@ -use crate::utils::ensure_dir_exists; -use crate::{defs, sepolicy}; -use anyhow::{Context, Result}; use std::path::Path; +use anyhow::{Context, Result}; + +use crate::{defs, sepolicy, utils::ensure_dir_exists}; + pub fn set_sepolicy(pkg: String, policy: String) -> Result<()> { ensure_dir_exists(defs::PROFILE_SELINUX_DIR)?; let policy_file = Path::new(defs::PROFILE_SELINUX_DIR).join(pkg); diff --git a/userspace/ksud/src/restorecon.rs b/userspace/ksud/src/restorecon.rs index 91ccfb11..eb0f35f9 100644 --- a/userspace/ksud/src/restorecon.rs +++ b/userspace/ksud/src/restorecon.rs @@ -1,12 +1,13 @@ -use crate::defs; -use anyhow::Result; -use jwalk::{Parallelism::Serial, WalkDir}; use std::path::Path; +use anyhow::Result; #[cfg(any(target_os = "linux", target_os = "android"))] use anyhow::{Context, Ok}; #[cfg(any(target_os = "linux", target_os = "android"))] use extattr::{Flags as XattrFlags, lsetxattr}; +use jwalk::{Parallelism::Serial, WalkDir}; + +use crate::defs; pub const SYSTEM_CON: &str = "u:object_r:system_file:s0"; pub const ADB_CON: &str = "u:object_r:adb_data_file:s0"; @@ -63,12 +64,11 @@ pub fn restore_syscon>(dir: P) -> Result<()> { fn restore_modules_con>(dir: P) -> Result<()> { for dir_entry in WalkDir::new(dir).parallelism(Serial) { - if let Some(path) = dir_entry.ok().map(|dir_entry| dir_entry.path()) { - if let Result::Ok(con) = lgetfilecon(&path) { - if con == ADB_CON || con == UNLABEL_CON || con.is_empty() { - lsetfilecon(&path, SYSTEM_CON)?; - } - } + if let Some(path) = dir_entry.ok().map(|dir_entry| dir_entry.path()) + && let Result::Ok(con) = lgetfilecon(&path) + && (con == ADB_CON || con == UNLABEL_CON || con.is_empty()) + { + lsetfilecon(&path, SYSTEM_CON)?; } } Ok(()) diff --git a/userspace/ksud/src/sepolicy.rs b/userspace/ksud/src/sepolicy.rs index b38ad2c1..897e9d9a 100644 --- a/userspace/ksud/src/sepolicy.rs +++ b/userspace/ksud/src/sepolicy.rs @@ -1,3 +1,5 @@ +use std::{ffi, path::Path, vec}; + use anyhow::{Result, bail}; use derive_new::new; use nom::{ @@ -7,7 +9,6 @@ use nom::{ character::complete::{space0, space1}, combinator::map, }; -use std::{ffi, path::Path, vec}; type SeObject<'a> = Vec<&'a str>; diff --git a/userspace/ksud/src/su.rs b/userspace/ksud/src/su.rs index 7f31c688..9cd3afbb 100644 --- a/userspace/ksud/src/su.rs +++ b/userspace/ksud/src/su.rs @@ -1,22 +1,20 @@ -use anyhow::{Ok, Result}; -use getopts::Options; -use std::env; #[cfg(unix)] use std::os::unix::process::CommandExt; -use std::path::PathBuf; -use std::{ffi::CStr, process::Command}; - -use crate::{ - defs, - utils::{self, umask}, -}; +use std::{env, ffi::CStr, path::PathBuf, process::Command}; +use anyhow::{Ok, Result}; +use getopts::Options; #[cfg(any(target_os = "linux", target_os = "android"))] use rustix::{ process::getuid, thread::{Gid, Uid, set_thread_res_gid, set_thread_res_uid}, }; +use crate::{ + defs, + utils::{self, umask}, +}; + #[cfg(any(target_os = "linux", target_os = "android"))] pub fn grant_root(global_mnt: bool) -> Result<()> { rustix::process::ksu_grant_root()?; diff --git a/userspace/ksud/src/uid_scanner.rs b/userspace/ksud/src/uid_scanner.rs index 7083d774..fa2b7eba 100644 --- a/userspace/ksud/src/uid_scanner.rs +++ b/userspace/ksud/src/uid_scanner.rs @@ -1,16 +1,17 @@ -use anyhow::Result; -use log::{info, warn}; use std::{ fs, io::Write, os::unix::{ - fs::{symlink, PermissionsExt}, + fs::{PermissionsExt, symlink}, process::CommandExt, }, path::Path, process::{Command, Stdio}, }; +use anyhow::Result; +use log::{info, warn}; + pub fn start_uid_scanner_daemon() -> Result<()> { const SCANNER_PATH: &str = "/data/adb/uid_scanner"; const LINK_DIR: &str = "/data/adb/ksu/bin"; diff --git a/userspace/ksud/src/utils.rs b/userspace/ksud/src/utils.rs index e31c3387..bc77c32f 100644 --- a/userspace/ksud/src/utils.rs +++ b/userspace/ksud/src/utils.rs @@ -1,28 +1,25 @@ -use anyhow::{Context, Error, Ok, Result, bail}; +#[cfg(unix)] +use std::os::unix::prelude::PermissionsExt; use std::{ fs::{self, File, OpenOptions, create_dir_all, remove_file, write}, + fs::{Permissions, set_permissions}, io::{ ErrorKind::{AlreadyExists, NotFound}, Write, }, - path::Path, + path::{Path, PathBuf}, process::Command, }; -use crate::{assets, boot_patch, defs, ksucalls, module, restorecon}; -#[allow(unused_imports)] -use std::fs::{Permissions, set_permissions}; -#[cfg(unix)] -use std::os::unix::prelude::PermissionsExt; - -use std::path::PathBuf; - +use anyhow::{Context, Error, Ok, Result, bail}; #[cfg(any(target_os = "linux", target_os = "android"))] use rustix::{ process, thread::{LinkNameSpaceType, move_into_link_name_space}, }; +use crate::{assets, boot_patch, defs, ksucalls, module, restorecon}; + pub fn ensure_clean_dir(dir: impl AsRef) -> Result<()> { let path = dir.as_ref(); log::debug!("ensure_clean_dir: {}", path.display()); @@ -74,11 +71,11 @@ pub fn ensure_binary>( ) })?)?; - if let Err(e) = remove_file(path.as_ref()) { - if e.kind() != NotFound { - return Err(Error::from(e)) - .with_context(|| format!("failed to unlink {}", path.as_ref().display())); - } + if let Err(e) = remove_file(path.as_ref()) + && e.kind() != NotFound + { + return Err(Error::from(e)) + .with_context(|| format!("failed to unlink {}", path.as_ref().display())); } write(&path, contents)?;