From 103217755b959ef438389288525c369dfe06c40e Mon Sep 17 00:00:00 2001 From: rsuntk Date: Thu, 19 Jun 2025 21:33:52 +0700 Subject: [PATCH] kernel: allow GKI kprobes to use LSM hook for ksu_handle_devpts * Also minor changes on comments Signed-off-by: rsuntk --- kernel/Makefile | 7 +++++-- kernel/core_hook.c | 12 +++--------- kernel/sucompat.c | 9 +++++++-- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/kernel/Makefile b/kernel/Makefile index 9e1c3fde..a61b11ba 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -102,10 +102,13 @@ endif KSU_HAS_DEVPTS_INODE := $(shell grep -q "ksu_handle_devpts" $(srctree)/fs/devpts/inode.c; echo $$?) KSU_HAS_PTS_UNIX98 := $(shell grep -q "ksu_handle_devpts" $(srctree)/drivers/tty/pty.c; echo $$?) -# It's impossible to have them both! neither KSU_HAS_DEVPTS_INODE/PTS_UNIX98 will detect 1 value! -# If both of them weren't exist, KSU_HAS_DEVPTS_HANDLER value gonna be 2. +# Codes: +# - 0: Both of them exist (impossible) +# - 1: Neither fs/devpts/inode.c or drivers/tty/pty.c have ksu_handle_devpts +# - 2: Both of them did not exist $(eval KSU_DEVPTS_HANDLER_EXIST=$(shell expr $(KSU_HAS_DEVPTS_INODE) + $(KSU_HAS_PTS_UNIX98))) ifeq ($(KSU_DEVPTS_HANDLER_EXIST),1) +$(info -- KernelSU: Kernel have devpts handler) ccflags-y += -DKSU_HAS_DEVPTS_HANDLER endif diff --git a/kernel/core_hook.c b/kernel/core_hook.c index f906538a..6c8ae272 100644 --- a/kernel/core_hook.c +++ b/kernel/core_hook.c @@ -1460,13 +1460,8 @@ static int ksu_task_fix_setuid(struct cred *new, const struct cred *old, return ksu_handle_setuid(new, old); } -/* - * Keep in mind, since kprobes already have pre handler, we must - * guard it with CONFIG_KSU_KPROBES_HOOK, although it is possible to - * disable kprobes pre handler, but this is way more simple. - * However, if you wanna use LSM hooks, feel free to fork. - */ -#if !defined(KSU_HAS_DEVPTS_HANDLER) && !defined(CONFIG_KSU_KPROBES_HOOK) +#ifndef MODULE +#ifndef KSU_HAS_DEVPTS_HANDLER extern int ksu_handle_devpts(struct inode *inode); static int ksu_inode_permission(struct inode *inode, int mask) { @@ -1480,12 +1475,11 @@ static int ksu_inode_permission(struct inode *inode, int mask) } #endif -#ifndef MODULE static struct security_hook_list ksu_hooks[] = { LSM_HOOK_INIT(task_prctl, ksu_task_prctl), LSM_HOOK_INIT(inode_rename, ksu_inode_rename), LSM_HOOK_INIT(task_fix_setuid, ksu_task_fix_setuid), -#if !defined(KSU_HAS_DEVPTS_HANDLER) && !defined(CONFIG_KSU_KPROBES_HOOK) +#ifndef KSU_HAS_DEVPTS_HANDLER LSM_HOOK_INIT(inode_permission, ksu_inode_permission), #endif #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0) || \ diff --git a/kernel/sucompat.c b/kernel/sucompat.c index ad74bf97..f81ccce5 100644 --- a/kernel/sucompat.c +++ b/kernel/sucompat.c @@ -303,6 +303,8 @@ static int execve_handler_pre(struct kprobe *p, struct pt_regs *regs) NULL); } +#ifdef MODULE +static struct kprobe *su_kps[6]; static int pts_unix98_lookup_pre(struct kprobe *p, struct pt_regs *regs) { struct inode *inode; @@ -315,6 +317,9 @@ static int pts_unix98_lookup_pre(struct kprobe *p, struct pt_regs *regs) return ksu_handle_devpts(inode); } +#else +static struct kprobe *su_kps[5]; +#endif static struct kprobe *init_kprobe(const char *name, kprobe_pre_handler_t handler) @@ -345,8 +350,6 @@ static void destroy_kprobe(struct kprobe **kp_ptr) kfree(kp); *kp_ptr = NULL; } - -static struct kprobe *su_kps[6]; #endif // sucompat: permited process can execute 'su' to gain root access. @@ -358,7 +361,9 @@ void ksu_sucompat_init() su_kps[2] = init_kprobe(SYS_FACCESSAT_SYMBOL, faccessat_handler_pre); su_kps[3] = init_kprobe(SYS_NEWFSTATAT_SYMBOL, newfstatat_handler_pre); su_kps[4] = init_kprobe(SYS_FSTATAT64_SYMBOL, newfstatat_handler_pre); +#ifdef MODULE su_kps[5] = init_kprobe("pts_unix98_lookup", pts_unix98_lookup_pre); +#endif #else ksu_sucompat_hook_state = true; pr_info("ksu_sucompat init\n");