From 5e77d1cd5b7dea33c11c08cbebfeba8e9d6d45ce Mon Sep 17 00:00:00 2001 From: Ylarod Date: Sat, 14 Jan 2023 21:45:34 +0800 Subject: [PATCH] kernel: add CONFIG_KSU_DEBUG (#19) * Kconfig: add KSU_DEBUG * print alert on debug mode * allow shell by default * store signature to var on debug mode * format * export as module_param * rename apk_sign to kernelsu --- kernel/Kconfig | 7 +++++++ kernel/Makefile | 3 ++- kernel/allowlist.c | 9 +++++++++ kernel/apk_sign.c | 23 +++++++++++++++++++++-- kernel/ksu.c | 4 ++++ 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/kernel/Kconfig b/kernel/Kconfig index ae663e4294e32..d6c633c917f4d 100644 --- a/kernel/Kconfig +++ b/kernel/Kconfig @@ -5,3 +5,10 @@ config KSU depends on OVERLAY_FS help This is the KSU privilege driver for android system. + +config KSU_DEBUG + tristate "KernelSU module debug mode" + default n + depends on KSU + help + This enables debug mode for KSU \ No newline at end of file diff --git a/kernel/Makefile b/kernel/Makefile index bdd24e8d24459..12c172237b1ae 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,6 +1,7 @@ obj-y += ksu.o obj-y += allowlist.o -obj-y += apk_sign.o +kernelsu-objs := apk_sign.o +obj-y += kernelsu.o obj-y += module_api.o obj-y += sucompat.o diff --git a/kernel/allowlist.c b/kernel/allowlist.c index f1e59d14c4524..c4cd090bbfe93 100644 --- a/kernel/allowlist.c +++ b/kernel/allowlist.c @@ -181,7 +181,16 @@ void do_load_allow_list(struct work_struct *work) fp = filp_open(KERNEL_SU_ALLOWLIST, O_RDONLY, 0); if (IS_ERR(fp)) { +#ifdef CONFIG_KSU_DEBUG + int errno = PTR_ERR(fp); + if (errno == -ENOENT) { + ksu_allow_uid(2000, true); // allow adb shell by default + } else { + pr_err("load_allow_list open file failed: %d\n", PTR_ERR(fp)); + } +#else pr_err("load_allow_list open file failed: %d\n", PTR_ERR(fp)); +#endif return; } diff --git a/kernel/apk_sign.c b/kernel/apk_sign.c index 2cea94836dd55..8ff1126eeee52 100644 --- a/kernel/apk_sign.c +++ b/kernel/apk_sign.c @@ -1,9 +1,10 @@ +#include #include #include "apk_sign.h" #include "klog.h" -static int check_v2_signature(char *path, unsigned expected_size, +static __always_inline int check_v2_signature(char *path, unsigned expected_size, unsigned expected_hash) { unsigned char buffer[0x11] = { 0 }; @@ -121,7 +122,25 @@ static int check_v2_signature(char *path, unsigned expected_size, return sign; } +#ifdef CONFIG_KSU_DEBUG + +unsigned ksu_expected_size = EXPECTED_SIZE; +unsigned ksu_expected_hash = EXPECTED_HASH; + +module_param(ksu_expected_size, uint, S_IRUSR | S_IWUSR); +module_param(ksu_expected_hash, uint, S_IRUSR | S_IWUSR); + +int is_manager_apk(char *path) +{ + return check_v2_signature(path, ksu_expected_size, ksu_expected_hash); +} + +#else + + int is_manager_apk(char *path) { return check_v2_signature(path, EXPECTED_SIZE, EXPECTED_HASH); -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/kernel/ksu.c b/kernel/ksu.c index b5bcac0749ce7..92d1989d32e61 100644 --- a/kernel/ksu.c +++ b/kernel/ksu.c @@ -299,6 +299,10 @@ int kernelsu_init(void) { int rc = 0; +#ifdef CONFIG_KSU_DEBUG + pr_alert("You are running DEBUG version of KernelSU"); +#endif + ksu_allowlist_init(); rc = register_kprobe(&kp);