Skip to content

Commit 97b2411

Browse files
authored
Revert "kernel: remove unused CONFIG guard becuase GKI kernel enable kprobe by default" (#2495)
follow up to #2475 (comment)
1 parent 203cd4d commit 97b2411

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

kernel/core_hook.c

+2
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,9 @@ void __init ksu_core_init(void)
886886

887887
void ksu_core_exit(void)
888888
{
889+
#ifdef CONFIG_KPROBES
889890
pr_info("ksu_core_kprobe_exit\n");
890891
// we dont use this now
891892
// ksu_kprobe_exit();
893+
#endif
892894
}

kernel/ksu.c

+6
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ int __init kernelsu_init(void)
5757

5858
ksu_throne_tracker_init();
5959

60+
#ifdef CONFIG_KPROBES
6061
ksu_sucompat_init();
6162
ksu_ksud_init();
63+
#else
64+
pr_alert("KPROBES is disabled, KernelSU may not work, please check https://kernelsu.org/guide/how-to-integrate-for-non-gki.html");
65+
#endif
6266

6367
#ifdef MODULE
6468
#ifndef CONFIG_KSU_DEBUG
@@ -76,8 +80,10 @@ void kernelsu_exit(void)
7680

7781
destroy_workqueue(ksu_workqueue);
7882

83+
#ifdef CONFIG_KPROBES
7984
ksu_ksud_exit();
8085
ksu_sucompat_exit();
86+
#endif
8187

8288
ksu_core_exit();
8389
}

kernel/ksud.c

+44
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ static void stop_vfs_read_hook();
4747
static void stop_execve_hook();
4848
static void stop_input_hook();
4949

50+
#ifdef CONFIG_KPROBES
5051
static struct work_struct stop_vfs_read_work;
5152
static struct work_struct stop_execve_hook_work;
5253
static struct work_struct stop_input_hook_work;
54+
#else
55+
bool ksu_vfs_read_hook __read_mostly = true;
56+
bool ksu_execveat_hook __read_mostly = true;
57+
bool ksu_input_hook __read_mostly = true;
58+
#endif
5359

5460
u32 ksu_devpts_sid;
5561

@@ -144,6 +150,11 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
144150
struct user_arg_ptr *argv,
145151
struct user_arg_ptr *envp, int *flags)
146152
{
153+
#ifndef CONFIG_KPROBES
154+
if (!ksu_execveat_hook) {
155+
return 0;
156+
}
157+
#endif
147158
struct filename *filename;
148159

149160
static const char app_process[] = "/system/bin/app_process";
@@ -295,6 +306,11 @@ static ssize_t read_iter_proxy(struct kiocb *iocb, struct iov_iter *to)
295306
int ksu_handle_vfs_read(struct file **file_ptr, char __user **buf_ptr,
296307
size_t *count_ptr, loff_t **pos)
297308
{
309+
#ifndef CONFIG_KPROBES
310+
if (!ksu_vfs_read_hook) {
311+
return 0;
312+
}
313+
#endif
298314
struct file *file;
299315
char __user *buf;
300316
size_t count;
@@ -403,6 +419,11 @@ static bool is_volumedown_enough(unsigned int count)
403419
int ksu_handle_input_handle_event(unsigned int *type, unsigned int *code,
404420
int *value)
405421
{
422+
#ifndef CONFIG_KPROBES
423+
if (!ksu_input_hook) {
424+
return 0;
425+
}
426+
#endif
406427
if (*type == EV_KEY && *code == KEY_VOLUMEDOWN) {
407428
int val = *value;
408429
pr_info("KEY_VOLUMEDOWN val: %d\n", val);
@@ -440,6 +461,8 @@ bool ksu_is_safe_mode()
440461
return false;
441462
}
442463

464+
#ifdef CONFIG_KPROBES
465+
443466
static int sys_execve_handler_pre(struct kprobe *p, struct pt_regs *regs)
444467
{
445468
struct pt_regs *real_regs = PT_REAL_REGS(regs);
@@ -492,6 +515,7 @@ static struct kprobe vfs_read_kp = {
492515
.pre_handler = sys_read_handler_pre,
493516
};
494517

518+
495519
static struct kprobe input_event_kp = {
496520
.symbol_name = "input_event",
497521
.pre_handler = input_handle_event_handler_pre,
@@ -511,17 +535,28 @@ static void do_stop_input_hook(struct work_struct *work)
511535
{
512536
unregister_kprobe(&input_event_kp);
513537
}
538+
#endif
514539

515540
static void stop_vfs_read_hook()
516541
{
542+
#ifdef CONFIG_KPROBES
517543
bool ret = schedule_work(&stop_vfs_read_work);
518544
pr_info("unregister vfs_read kprobe: %d!\n", ret);
545+
#else
546+
ksu_vfs_read_hook = false;
547+
pr_info("stop vfs_read_hook\n");
548+
#endif
519549
}
520550

521551
static void stop_execve_hook()
522552
{
553+
#ifdef CONFIG_KPROBES
523554
bool ret = schedule_work(&stop_execve_hook_work);
524555
pr_info("unregister execve kprobe: %d!\n", ret);
556+
#else
557+
ksu_execveat_hook = false;
558+
pr_info("stop execve_hook\n");
559+
#endif
525560
}
526561

527562
static void stop_input_hook()
@@ -531,13 +566,19 @@ static void stop_input_hook()
531566
return;
532567
}
533568
input_hook_stopped = true;
569+
#ifdef CONFIG_KPROBES
534570
bool ret = schedule_work(&stop_input_hook_work);
535571
pr_info("unregister input kprobe: %d!\n", ret);
572+
#else
573+
ksu_input_hook = false;
574+
pr_info("stop input_hook\n");
575+
#endif
536576
}
537577

538578
// ksud: module support
539579
void ksu_ksud_init()
540580
{
581+
#ifdef CONFIG_KPROBES
541582
int ret;
542583

543584
ret = register_kprobe(&execve_kp);
@@ -552,12 +593,15 @@ void ksu_ksud_init()
552593
INIT_WORK(&stop_vfs_read_work, do_stop_vfs_read_hook);
553594
INIT_WORK(&stop_execve_hook_work, do_stop_execve_hook);
554595
INIT_WORK(&stop_input_hook_work, do_stop_input_hook);
596+
#endif
555597
}
556598

557599
void ksu_ksud_exit()
558600
{
601+
#ifdef CONFIG_KPROBES
559602
unregister_kprobe(&execve_kp);
560603
// this should be done before unregister vfs_read_kp
561604
// unregister_kprobe(&vfs_read_kp);
562605
unregister_kprobe(&input_event_kp);
606+
#endif
563607
}

kernel/sucompat.c

+7
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ int ksu_handle_devpts(struct inode *inode)
189189
return 0;
190190
}
191191

192+
#ifdef CONFIG_KPROBES
193+
192194
static int faccessat_handler_pre(struct kprobe *p, struct pt_regs *regs)
193195
{
194196
struct pt_regs *real_regs = PT_REAL_REGS(regs);
@@ -261,19 +263,24 @@ static void destroy_kprobe(struct kprobe **kp_ptr)
261263
}
262264

263265
static struct kprobe *su_kps[4];
266+
#endif
264267

265268
// sucompat: permited process can execute 'su' to gain root access.
266269
void ksu_sucompat_init()
267270
{
271+
#ifdef CONFIG_KPROBES
268272
su_kps[0] = init_kprobe(SYS_EXECVE_SYMBOL, execve_handler_pre);
269273
su_kps[1] = init_kprobe(SYS_FACCESSAT_SYMBOL, faccessat_handler_pre);
270274
su_kps[2] = init_kprobe(SYS_NEWFSTATAT_SYMBOL, newfstatat_handler_pre);
271275
su_kps[3] = init_kprobe("pts_unix98_lookup", pts_unix98_lookup_pre);
276+
#endif
272277
}
273278

274279
void ksu_sucompat_exit()
275280
{
281+
#ifdef CONFIG_KPROBES
276282
for (int i = 0; i < ARRAY_SIZE(su_kps); i++) {
277283
destroy_kprobe(&su_kps[i]);
278284
}
285+
#endif
279286
}

0 commit comments

Comments
 (0)