Skip to content

Commit 20ffabb

Browse files
tiannbackslashxx
authored andcommitted
kernel: backport support for sucompat disable/enable
I have no idea if this is needed or any useful for manual fs hooks users. Upstream, this is likely to, well, simply disable hooking of those functions, to remove and mitigate timing issues as some detection methods can actually differentiate these. This is done like how vfs_read_hook, input_hook and execve_hook is disabled. While this is not exactly the same thing, this *CAN* achieve the same results. The complete disabling of all KernelSU hooks. While this is probably not so useful for us, honestly, I have no idea for shit what I am doing, but lets still port it for the sake of feature parity. adapted from upstream: kernel: add support for disable sucompat - tiann@2096bd7 kernel: Allow to re-enable sucompat - tiann@4593ae8 Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
1 parent a3d3e93 commit 20ffabb

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

kernel/core_hook.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ static bool ksu_module_mounted = false;
3333

3434
extern int handle_sepolicy(unsigned long arg3, void __user *arg4);
3535

36+
static bool ksu_su_compat_enabled = true;
37+
extern void ksu_sucompat_init();
38+
extern void ksu_sucompat_exit();
39+
3640
static inline bool is_allow_su()
3741
{
3842
if (is_manager()) {
@@ -284,8 +288,9 @@ int ksu_handle_prctl(int option, unsigned long arg2, unsigned long arg3,
284288
if (copy_to_user(arg3, &version, sizeof(version))) {
285289
pr_err("prctl reply error, cmd: %lu\n", arg2);
286290
}
287-
u32 is_lkm = 0x0;
288-
if (arg4 && copy_to_user(arg4, &is_lkm, sizeof(is_lkm))) {
291+
u32 version_flags = 0;
292+
version_flags |= 0x0;
293+
if (arg4 && copy_to_user(arg4, &version_flags, sizeof(version_flags))) {
289294
pr_err("prctl reply error, cmd: %lu\n", arg2);
290295
}
291296
return 0;
@@ -432,6 +437,34 @@ int ksu_handle_prctl(int option, unsigned long arg2, unsigned long arg3,
432437
return 0;
433438
}
434439

440+
if (arg2 == CMD_IS_SU_ENABLED) {
441+
if (copy_to_user(arg3, &ksu_su_compat_enabled,
442+
sizeof(ksu_su_compat_enabled))) {
443+
pr_err("copy su compat failed\n");
444+
return 0;
445+
}
446+
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {
447+
pr_err("prctl reply error, cmd: %lu\n", arg2);
448+
}
449+
return 0;
450+
}
451+
if (arg2 == CMD_ENABLE_SU) {
452+
bool enabled = (arg3 != 0);
453+
if (enabled == ksu_su_compat_enabled) {
454+
pr_info("cmd enable su but no need to change.\n");
455+
return 0;
456+
}
457+
if (enabled) {
458+
ksu_sucompat_init();
459+
} else {
460+
ksu_sucompat_exit();
461+
}
462+
ksu_su_compat_enabled = enabled;
463+
if (copy_to_user(result, &reply_ok, sizeof(reply_ok))) {
464+
pr_err("prctl reply error, cmd: %lu\n", arg2);
465+
}
466+
return 0;
467+
}
435468
return 0;
436469
}
437470

kernel/ksu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#define CMD_SET_APP_PROFILE 11
2222
#define CMD_UID_GRANTED_ROOT 12
2323
#define CMD_UID_SHOULD_UMOUNT 13
24+
#define CMD_IS_SU_ENABLED 14
25+
#define CMD_ENABLE_SU 15
2426

2527
#define EVENT_POST_FS_DATA 1
2628
#define EVENT_BOOT_COMPLETED 2

kernel/sucompat.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525

2626
extern void escape_to_root();
2727

28+
bool ksu_faccessat_hook __read_mostly = true;
29+
bool ksu_stat_hook __read_mostly = true;
30+
bool ksu_execve_sucompat_hook __read_mostly = true;
31+
bool ksu_execveat_sucompat_hook __read_mostly = true;
32+
bool ksu_devpts_hook __read_mostly = true;
33+
2834
static void __user *userspace_stack_buffer(const void *d, size_t len)
2935
{
3036
/* To avoid having to mmap a page in userspace, just write below the stack
@@ -51,8 +57,13 @@ static char __user *ksud_user_path(void)
5157
int ksu_handle_faccessat(int *dfd, const char __user **filename_user, int *mode,
5258
int *__unused_flags)
5359
{
60+
5461
const char su[] = SU_PATH;
5562

63+
if (!ksu_faccessat_hook) {
64+
return 0;
65+
}
66+
5667
if (!ksu_is_allow_uid(current_uid().val)) {
5768
return 0;
5869
}
@@ -74,6 +85,10 @@ int ksu_handle_stat(int *dfd, const char __user **filename_user, int *flags)
7485
// const char sh[] = SH_PATH;
7586
const char su[] = SU_PATH;
7687

88+
if (!ksu_stat_hook) {
89+
return 0;
90+
}
91+
7792
if (!ksu_is_allow_uid(current_uid().val)) {
7893
return 0;
7994
}
@@ -118,6 +133,10 @@ int ksu_handle_execveat_sucompat(int *fd, struct filename **filename_ptr,
118133
const char sh[] = KSUD_PATH;
119134
const char su[] = SU_PATH;
120135

136+
if (!ksu_execveat_sucompat_hook){
137+
return 0;
138+
}
139+
121140
if (unlikely(!filename_ptr))
122141
return 0;
123142

@@ -147,6 +166,10 @@ int ksu_handle_execve_sucompat(int *fd, const char __user **filename_user,
147166
const char su[] = SU_PATH;
148167
char path[sizeof(su) + 1];
149168

169+
if (!ksu_execve_sucompat_hook){
170+
return 0;
171+
}
172+
150173
if (unlikely(!filename_user))
151174
return 0;
152175

@@ -169,6 +192,10 @@ int ksu_handle_execve_sucompat(int *fd, const char __user **filename_user,
169192

170193
int ksu_handle_devpts(struct inode *inode)
171194
{
195+
if (!ksu_devpts_hook) {
196+
return 0;
197+
}
198+
172199
if (!current->mm) {
173200
return 0;
174201
}
@@ -196,3 +223,24 @@ int ksu_handle_devpts(struct inode *inode)
196223

197224
return 0;
198225
}
226+
227+
void ksu_sucompat_init()
228+
{
229+
ksu_faccessat_hook = true;
230+
ksu_stat_hook = true;
231+
ksu_execve_sucompat_hook = true;
232+
ksu_execveat_sucompat_hook = true;
233+
ksu_devpts_hook = true;
234+
pr_info("ksu_sucompat_init: hooks enabled: execve/execveat_su, faccessat, stat, devpts\n");
235+
}
236+
237+
void ksu_sucompat_exit()
238+
{
239+
ksu_faccessat_hook = false;
240+
ksu_stat_hook = false;
241+
ksu_execve_sucompat_hook = false;
242+
ksu_execveat_sucompat_hook = false;
243+
ksu_devpts_hook = false;
244+
pr_info("ksu_sucompat_exit: hooks disabled: execve/execveat_su, faccessat, stat, devpts\n");
245+
246+
}

0 commit comments

Comments
 (0)