From efbc07fde3466d70431d3cdb824f60032f8ce8a0 Mon Sep 17 00:00:00 2001 From: weishu Date: Sat, 27 Apr 2024 09:55:16 +0800 Subject: [PATCH] kernel: use library import --- kernel/allowlist.c | 22 +++++----- kernel/allowlist.h | 2 +- kernel/apk_sign.c | 34 ++++++++------- kernel/apk_sign.h | 2 +- kernel/arch.h | 2 +- kernel/core_hook.c | 61 ++++++++++++++------------ kernel/core_hook.h | 2 +- kernel/include/ksu_hook.h | 4 +- kernel/kernel_compat.c | 18 ++++---- kernel/kernel_compat.h | 6 +-- kernel/ksu.c | 10 ++--- kernel/ksu.h | 4 +- kernel/ksud.c | 92 +++++++++++++++++++++++---------------- kernel/ksud.h | 2 + kernel/manager.h | 4 +- kernel/selinux/rules.c | 6 +-- kernel/selinux/sepolicy.c | 10 ++--- kernel/selinux/sepolicy.h | 2 +- kernel/sucompat.c | 20 ++++----- kernel/throne_tracker.c | 16 +++---- 20 files changed, 171 insertions(+), 148 deletions(-) diff --git a/kernel/allowlist.c b/kernel/allowlist.c index 00777e62..4fbba935 100644 --- a/kernel/allowlist.c +++ b/kernel/allowlist.c @@ -1,17 +1,17 @@ -#include "ksu.h" -#include "linux/compiler.h" -#include "linux/fs.h" -#include "linux/gfp.h" -#include "linux/kernel.h" -#include "linux/list.h" -#include "linux/printk.h" -#include "linux/slab.h" -#include "linux/types.h" -#include "linux/version.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0) -#include "linux/compiler_types.h" +#include #endif +#include "ksu.h" #include "klog.h" // IWYU pragma: keep #include "selinux/selinux.h" #include "kernel_compat.h" diff --git a/kernel/allowlist.h b/kernel/allowlist.h index 298624bc..e89bf71f 100644 --- a/kernel/allowlist.h +++ b/kernel/allowlist.h @@ -1,7 +1,7 @@ #ifndef __KSU_H_ALLOWLIST #define __KSU_H_ALLOWLIST -#include "linux/types.h" +#include #include "ksu.h" void ksu_allowlist_init(void); diff --git a/kernel/apk_sign.c b/kernel/apk_sign.c index c2a55623..ba8b73f2 100644 --- a/kernel/apk_sign.c +++ b/kernel/apk_sign.c @@ -1,21 +1,23 @@ -#include "linux/err.h" -#include "linux/fs.h" -#include "linux/gfp.h" -#include "linux/kernel.h" -#include "linux/moduleparam.h" +#include +#include +#include +#include +#include +#include +#ifdef CONFIG_KSU_DEBUG +#include +#endif +#include +#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0) +#include +#else +#include +#endif #include "apk_sign.h" #include "klog.h" // IWYU pragma: keep #include "kernel_compat.h" -#include "crypto/hash.h" -#include "linux/slab.h" -#include "linux/version.h" -#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0) -#include "crypto/sha2.h" -#else -#include "crypto/sha.h" -#endif struct sdesc { struct shash_desc shash; @@ -229,7 +231,7 @@ static __always_inline bool check_v2_signature(char *path, goto clean; } - int loop_count = 0; + int loop_count = 0; while (loop_count++ < 10) { uint32_t id; uint32_t offset; @@ -307,8 +309,8 @@ static struct kernel_param_ops expected_size_ops = { .get = param_get_uint, }; -module_param_cb(ksu_debug_manager_uid, &expected_size_ops, &ksu_debug_manager_uid, - S_IRUSR | S_IWUSR); +module_param_cb(ksu_debug_manager_uid, &expected_size_ops, + &ksu_debug_manager_uid, S_IRUSR | S_IWUSR); #endif diff --git a/kernel/apk_sign.h b/kernel/apk_sign.h index ebd78a8b..bed501c4 100644 --- a/kernel/apk_sign.h +++ b/kernel/apk_sign.h @@ -1,7 +1,7 @@ #ifndef __KSU_H_APK_V2_SIGN #define __KSU_H_APK_V2_SIGN -#include "linux/types.h" +#include bool is_manager_apk(char *path); diff --git a/kernel/arch.h b/kernel/arch.h index f671b51b..f36ec5f5 100644 --- a/kernel/arch.h +++ b/kernel/arch.h @@ -1,7 +1,7 @@ #ifndef __KSU_H_ARCH #define __KSU_H_ARCH -#include "linux/version.h" +#include #if defined(__aarch64__) diff --git a/kernel/core_hook.c b/kernel/core_hook.c index 617c1bc8..71265736 100644 --- a/kernel/core_hook.c +++ b/kernel/core_hook.c @@ -1,32 +1,36 @@ -#include "linux/capability.h" -#include "linux/cred.h" -#include "linux/dcache.h" -#include "linux/err.h" -#include "linux/init.h" -#include "linux/init_task.h" -#include "linux/irqflags.h" -#include "linux/kallsyms.h" -#include "linux/kernel.h" -#include "linux/kprobes.h" -#include "linux/list.h" -#include "linux/lsm_hooks.h" -#include "linux/mm.h" -#include "linux/mm_types.h" -#include "linux/nsproxy.h" -#include "linux/path.h" -#include "linux/printk.h" -#include "linux/sched.h" -#include "linux/security.h" -#include "linux/stddef.h" -#include "linux/types.h" -#include "linux/uaccess.h" -#include "linux/uidgid.h" -#include "linux/version.h" -#include "linux/mount.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include -#include "linux/fs.h" -#include "linux/namei.h" -#include "linux/rcupdate.h" +#include +#include + +#ifdef MODULE +#include +#include +#include +#include +#include +#endif #include "allowlist.h" #include "arch.h" @@ -34,7 +38,6 @@ #include "klog.h" // IWYU pragma: keep #include "ksu.h" #include "ksud.h" -#include "linux/vmalloc.h" #include "manager.h" #include "selinux/selinux.h" #include "throne_tracker.h" diff --git a/kernel/core_hook.h b/kernel/core_hook.h index 8e8bfc2c..616951e8 100644 --- a/kernel/core_hook.h +++ b/kernel/core_hook.h @@ -1,7 +1,7 @@ #ifndef __KSU_H_KSU_CORE #define __KSU_H_KSU_CORE -#include "linux/init.h" +#include void __init ksu_core_init(void); void ksu_core_exit(void); diff --git a/kernel/include/ksu_hook.h b/kernel/include/ksu_hook.h index d4a2cddb..ea0b04d3 100644 --- a/kernel/include/ksu_hook.h +++ b/kernel/include/ksu_hook.h @@ -1,8 +1,8 @@ #ifndef __KSU_H_KSHOOK #define __KSU_H_KSHOOK -#include "linux/fs.h" -#include "linux/types.h" +#include +#include // For sucompat diff --git a/kernel/kernel_compat.c b/kernel/kernel_compat.c index 1a94f36f..b242bc63 100644 --- a/kernel/kernel_compat.c +++ b/kernel/kernel_compat.c @@ -1,19 +1,19 @@ -#include "linux/version.h" -#include "linux/fs.h" -#include "linux/nsproxy.h" +#include +#include +#include #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0) -#include "linux/sched/task.h" +#include #else -#include "linux/sched.h" +#include #endif -#include "linux/uaccess.h" +#include #include "klog.h" // IWYU pragma: keep #include "kernel_compat.h" // Add check Huawei Device #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0) || defined(CONFIG_IS_HW_HISI) -#include "linux/key.h" -#include "linux/errno.h" -#include "linux/cred.h" +#include +#include +#include struct key *init_session_keyring = NULL; static inline int install_session_keyring(struct key *keyring) diff --git a/kernel/kernel_compat.h b/kernel/kernel_compat.h index 8638378b..ba998185 100644 --- a/kernel/kernel_compat.h +++ b/kernel/kernel_compat.h @@ -1,10 +1,10 @@ #ifndef __KSU_H_KERNEL_COMPAT #define __KSU_H_KERNEL_COMPAT -#include "linux/fs.h" -#include "linux/key.h" -#include "linux/version.h" +#include +#include #include "ss/policydb.h" +#include "linux/key.h" /* * Adapt to Huawei HISI kernel without affecting other kernels , diff --git a/kernel/ksu.c b/kernel/ksu.c index 22d0672e..3639edc2 100644 --- a/kernel/ksu.c +++ b/kernel/ksu.c @@ -1,8 +1,8 @@ -#include "linux/export.h" -#include "linux/fs.h" -#include "linux/kobject.h" -#include "linux/module.h" -#include "linux/workqueue.h" +#include +#include +#include +#include +#include #include "allowlist.h" #include "arch.h" diff --git a/kernel/ksu.h b/kernel/ksu.h index b98c0fd1..35d1b14d 100644 --- a/kernel/ksu.h +++ b/kernel/ksu.h @@ -1,8 +1,8 @@ #ifndef __KSU_H_KSU #define __KSU_H_KSU -#include "linux/types.h" -#include "linux/workqueue.h" +#include +#include #define KERNEL_SU_VERSION KSU_VERSION #define KERNEL_SU_OPTION 0xDEADBEEF diff --git a/kernel/ksud.c b/kernel/ksud.c index 59171afa..efa16398 100644 --- a/kernel/ksud.c +++ b/kernel/ksud.c @@ -1,25 +1,24 @@ -#include "asm/current.h" -#include "linux/compat.h" -#include "linux/cred.h" -#include "linux/dcache.h" -#include "linux/err.h" -#include "linux/file.h" -#include "linux/fs.h" -#include "linux/version.h" +#include +#include +#include +#include +#include +#include +#include +#include #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0) -#include "linux/input-event-codes.h" +#include #else -#include "uapi/linux/input.h" +#include #endif #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0) -#include "linux/aio.h" +#include #endif -#include "linux/kprobes.h" -#include "linux/printk.h" -#include "linux/types.h" -#include "linux/uaccess.h" -#include "linux/version.h" -#include "linux/workqueue.h" +#include +#include +#include +#include +#include #include "allowlist.h" #include "arch.h" @@ -117,7 +116,7 @@ static const char __user *get_user_arg_ptr(struct user_arg_ptr argv, int nr) * count() counts the number of strings in array ARGV. */ - /* +/* * Make sure old GCC compiler can use __maybe_unused, * Test passed in 4.4.x ~ 4.9.x when use GCC. */ @@ -150,7 +149,8 @@ static int __maybe_unused count(struct user_arg_ptr argv, int max) // IMPORTANT NOTE: the call from execve_handler_pre WON'T provided correct value for envp and flags in GKI version int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr, - struct user_arg_ptr *argv, struct user_arg_ptr *envp, int *flags) + struct user_arg_ptr *argv, + struct user_arg_ptr *envp, int *flags) { #ifndef CONFIG_KPROBES if (!ksu_execveat_hook) { @@ -177,7 +177,8 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr, } if (unlikely(!memcmp(filename->name, system_bin_init, - sizeof(system_bin_init) - 1) && argv)) { + sizeof(system_bin_init) - 1) && + argv)) { // /system/bin/init executed int argc = count(*argv, MAX_ARG_STRINGS); pr_info("/system/bin/init argc: %d\n", argc); @@ -185,8 +186,10 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr, const char __user *p = get_user_arg_ptr(*argv, 1); if (p && !IS_ERR(p)) { char first_arg[16]; - ksu_strncpy_from_user_nofault(first_arg, p, sizeof(first_arg)); - pr_info("/system/bin/init first arg: %s\n", first_arg); + ksu_strncpy_from_user_nofault( + first_arg, p, sizeof(first_arg)); + pr_info("/system/bin/init first arg: %s\n", + first_arg); if (!strcmp(first_arg, "second_stage")) { pr_info("/system/bin/init second_stage executed\n"); apply_kernelsu_rules(); @@ -198,7 +201,8 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr, } } } else if (unlikely(!memcmp(filename->name, old_system_init, - sizeof(old_system_init) - 1) && argv)) { + sizeof(old_system_init) - 1) && + argv)) { // /init executed int argc = count(*argv, MAX_ARG_STRINGS); pr_info("/init argc: %d\n", argc); @@ -207,7 +211,8 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr, const char __user *p = get_user_arg_ptr(*argv, 1); if (p && !IS_ERR(p)) { char first_arg[16]; - ksu_strncpy_from_user_nofault(first_arg, p, sizeof(first_arg)); + ksu_strncpy_from_user_nofault( + first_arg, p, sizeof(first_arg)); pr_info("/init first arg: %s\n", first_arg); if (!strcmp(first_arg, "--second-stage")) { pr_info("/init second_stage executed\n"); @@ -224,13 +229,15 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr, if (envc > 0) { int n; for (n = 1; n <= envc; n++) { - const char __user *p = get_user_arg_ptr(*envp, n); + const char __user *p = + get_user_arg_ptr(*envp, n); if (!p || IS_ERR(p)) { continue; } char env[256]; // Reading environment variable strings from user space - if (ksu_strncpy_from_user_nofault(env, p, sizeof(env)) < 0) + if (ksu_strncpy_from_user_nofault( + env, p, sizeof(env)) < 0) continue; // Parsing environment variable names and values char *env_name = env; @@ -241,10 +248,14 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr, *env_value = '\0'; env_value++; // Check if the environment variable name and value are matching - if (!strcmp(env_name, "INIT_SECOND_STAGE") && (!strcmp(env_value, "1") || !strcmp(env_value, "true"))) { + if (!strcmp(env_name, + "INIT_SECOND_STAGE") && + (!strcmp(env_value, "1") || + !strcmp(env_value, "true"))) { pr_info("/init second_stage executed\n"); apply_kernelsu_rules(); - init_second_stage_executed = true; + init_second_stage_executed = + true; ksu_android_ns_fs_check(); } } @@ -252,10 +263,11 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr, } } - if (unlikely(first_app_process && - !memcmp(filename->name, app_process, sizeof(app_process) - 1))) { + if (unlikely(first_app_process && !memcmp(filename->name, app_process, + sizeof(app_process) - 1))) { first_app_process = false; - pr_info("exec app_process, /data prepared, second_stage: %d\n", init_second_stage_executed); + pr_info("exec app_process, /data prepared, second_stage: %d\n", + init_second_stage_executed); on_post_fs_data(); // we keep this for old ksud stop_execve_hook(); } @@ -274,7 +286,8 @@ static ssize_t read_proxy(struct file *file, char __user *buf, size_t count, bool first_read = file->f_pos == 0; ssize_t ret = orig_read(file, buf, count, pos); if (first_read) { - pr_info("read_proxy append %ld + %ld\n", ret, read_count_append); + pr_info("read_proxy append %ld + %ld\n", ret, + read_count_append); ret += read_count_append; } return ret; @@ -386,7 +399,8 @@ int ksu_handle_vfs_read(struct file **file_ptr, char __user **buf_ptr, return 0; } -int ksu_handle_sys_read(unsigned int fd, char __user **buf_ptr, size_t *count_ptr) +int ksu_handle_sys_read(unsigned int fd, char __user **buf_ptr, + size_t *count_ptr) { struct file *file = fget(fd); if (!file) { @@ -475,7 +489,8 @@ static int execve_handler_pre(struct kprobe *p, struct pt_regs *regs) static int sys_execve_handler_pre(struct kprobe *p, struct pt_regs *regs) { struct pt_regs *real_regs = PT_REAL_REGS(regs); - const char __user **filename_user = (const char **)&PT_REGS_PARM1(real_regs); + const char __user **filename_user = + (const char **)&PT_REGS_PARM1(real_regs); const char __user *const __user *__argv = (const char __user *const __user *)PT_REGS_PARM2(real_regs); struct user_arg_ptr argv = { .ptr.native = __argv }; @@ -495,7 +510,8 @@ static int sys_execve_handler_pre(struct kprobe *p, struct pt_regs *regs) } // remove this later! -__maybe_unused static int vfs_read_handler_pre(struct kprobe *p, struct pt_regs *regs) +__maybe_unused static int vfs_read_handler_pre(struct kprobe *p, + struct pt_regs *regs) { struct file **file_ptr = (struct file **)&PT_REGS_PARM1(regs); char __user **buf_ptr = (char **)&PT_REGS_PARM2(regs); @@ -510,7 +526,7 @@ static int sys_read_handler_pre(struct kprobe *p, struct pt_regs *regs) struct pt_regs *real_regs = PT_REAL_REGS(regs); unsigned int fd = PT_REGS_PARM1(real_regs); char __user **buf_ptr = (char __user **)&PT_REGS_PARM2(real_regs); - size_t count_ptr = (size_t *) &PT_REGS_PARM3(real_regs); + size_t count_ptr = (size_t *)&PT_REGS_PARM3(real_regs); return ksu_handle_sys_read(fd, buf_ptr, count_ptr); } @@ -559,7 +575,6 @@ static struct kprobe input_event_kp = { .pre_handler = input_handle_event_handler_pre, }; - static void do_stop_vfs_read_hook(struct work_struct *work) { unregister_kprobe(&vfs_read_kp); @@ -635,7 +650,8 @@ void ksu_ksud_init() #endif } -void ksu_ksud_exit() { +void ksu_ksud_exit() +{ #ifdef CONFIG_KPROBES unregister_kprobe(&execve_kp); // this should be done before unregister vfs_read_kp diff --git a/kernel/ksud.h b/kernel/ksud.h index 5a32a527..eafb3147 100644 --- a/kernel/ksud.h +++ b/kernel/ksud.h @@ -1,6 +1,8 @@ #ifndef __KSU_H_KSUD #define __KSU_H_KSUD +#include + #define KSUD_PATH "/data/adb/ksud" void on_post_fs_data(void); diff --git a/kernel/manager.h b/kernel/manager.h index 6f915108..be5bbced 100644 --- a/kernel/manager.h +++ b/kernel/manager.h @@ -1,8 +1,8 @@ #ifndef __KSU_H_KSU_MANAGER #define __KSU_H_KSU_MANAGER -#include "linux/cred.h" -#include "linux/types.h" +#include +#include #define KSU_INVALID_UID -1 diff --git a/kernel/selinux/rules.c b/kernel/selinux/rules.c index 2ae960af..7c3f7b33 100644 --- a/kernel/selinux/rules.c +++ b/kernel/selinux/rules.c @@ -1,6 +1,6 @@ -#include "linux/uaccess.h" -#include "linux/types.h" -#include "linux/version.h" +#include +#include +#include #include "../klog.h" // IWYU pragma: keep #include "selinux.h" diff --git a/kernel/selinux/sepolicy.c b/kernel/selinux/sepolicy.c index 8d8d9831..acdc45ad 100644 --- a/kernel/selinux/sepolicy.c +++ b/kernel/selinux/sepolicy.c @@ -1,9 +1,9 @@ -#include "sepolicy.h" -#include "linux/gfp.h" -#include "linux/printk.h" -#include "linux/slab.h" -#include "linux/version.h" +#include +#include +#include +#include +#include "sepolicy.h" #include "../klog.h" // IWYU pragma: keep #include "ss/symtab.h" #include "../kernel_compat.h" // Add check Huawei Device diff --git a/kernel/selinux/sepolicy.h b/kernel/selinux/sepolicy.h index a5071236..675d1499 100644 --- a/kernel/selinux/sepolicy.h +++ b/kernel/selinux/sepolicy.h @@ -1,7 +1,7 @@ #ifndef __KSU_H_SEPOLICY #define __KSU_H_SEPOLICY -#include "linux/types.h" +#include #include "ss/policydb.h" diff --git a/kernel/sucompat.c b/kernel/sucompat.c index 44f30982..f43c2063 100644 --- a/kernel/sucompat.c +++ b/kernel/sucompat.c @@ -1,15 +1,15 @@ -#include "asm/current.h" -#include "linux/cred.h" -#include "linux/err.h" -#include "linux/fs.h" -#include "linux/kprobes.h" -#include "linux/types.h" -#include "linux/uaccess.h" -#include "linux/version.h" +#include +#include +#include +#include +#include +#include +#include +#include #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) -#include "linux/sched/task_stack.h" +#include #else -#include "linux/sched.h" +#include #endif #include "allowlist.h" diff --git a/kernel/throne_tracker.c b/kernel/throne_tracker.c index 90181b7e..b931b60f 100644 --- a/kernel/throne_tracker.c +++ b/kernel/throne_tracker.c @@ -1,11 +1,11 @@ -#include "linux/err.h" -#include "linux/fs.h" -#include "linux/list.h" -#include "linux/slab.h" -#include "linux/string.h" -#include "linux/types.h" -#include "linux/version.h" -#include "linux/workqueue.h" +#include +#include +#include +#include +#include +#include +#include +#include #include "allowlist.h" #include "klog.h" // IWYU pragma: keep