Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit 2511836

Browse files
KernelSU: Integrate scope-minimized manual hooks
This commit integrates manual hooks for KernelSU, replacing the kprobe-based approach. This refactors original KSU hooks to replace deep kernel function hooks with targeted hooks. This backports KernelSU pr#1657 and having pr#2084 elements (32-bit sucompat). This transition minimizes the scope of kernel function interception while still maintaining full functionality. references: tiann/KernelSU#1657, tiann/KernelSU#2084, backslashxx/KernelSU#5 https://kernelsu.org/guide/how-to-integrate-for-non-gki.html Co-Authored-by: backslashxx <[email protected]>
1 parent b08a28a commit 2511836

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed

drivers/input/input.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,21 @@ static int input_get_disposition(struct input_dev *dev,
375375
return disposition;
376376
}
377377

378+
#ifdef CONFIG_KSU
379+
extern bool ksu_input_hook __read_mostly;
380+
extern int ksu_handle_input_handle_event(unsigned int *type, unsigned int *code, int *value);
381+
#endif
382+
378383
static void input_handle_event(struct input_dev *dev,
379384
unsigned int type, unsigned int code, int value)
380385
{
381386
int disposition = input_get_disposition(dev, type, code, &value);
382387

388+
#ifdef CONFIG_KSU
389+
if (unlikely(ksu_input_hook))
390+
ksu_handle_input_handle_event(&type, &code, &value);
391+
#endif
392+
383393
if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
384394
add_input_randomness(type, code, value);
385395

fs/devpts/inode.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,10 @@ struct dentry *devpts_pty_new(struct pts_fs_info *fsi, int index, void *priv)
596596
return dentry;
597597
}
598598

599+
#ifdef CONFIG_KSU
600+
extern int ksu_handle_devpts(struct inode*);
601+
#endif
602+
599603
/**
600604
* devpts_get_priv -- get private data for a slave
601605
* @pts_inode: inode of the slave
@@ -604,6 +608,9 @@ struct dentry *devpts_pty_new(struct pts_fs_info *fsi, int index, void *priv)
604608
*/
605609
void *devpts_get_priv(struct dentry *dentry)
606610
{
611+
#ifdef CONFIG_KSU
612+
ksu_handle_devpts(dentry->d_inode);
613+
#endif
607614
if (dentry->d_sb->s_magic != DEVPTS_SUPER_MAGIC)
608615
return NULL;
609616
return dentry->d_fsdata;

fs/exec.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,12 +2011,28 @@ int kernel_execve(const char *kernel_filename,
20112011
return retval;
20122012
}
20132013

2014+
#ifdef CONFIG_KSU
2015+
extern bool ksu_execveat_hook __read_mostly;
2016+
extern int ksu_handle_execveat(int *fd, struct filename **filename_ptr, void *argv,
2017+
void *envp, int *flags);
2018+
extern int ksu_handle_execveat_sucompat(int *fd, struct filename **filename_ptr,
2019+
void *argv, void *envp, int *flags);
2020+
#endif
2021+
20142022
static int do_execve(struct filename *filename,
20152023
const char __user *const __user *__argv,
20162024
const char __user *const __user *__envp)
20172025
{
20182026
struct user_arg_ptr argv = { .ptr.native = __argv };
20192027
struct user_arg_ptr envp = { .ptr.native = __envp };
2028+
2029+
#ifdef CONFIG_KSU
2030+
if (unlikely(ksu_execveat_hook))
2031+
ksu_handle_execveat((int *)AT_FDCWD, &filename, &argv, &envp, 0);
2032+
else
2033+
ksu_handle_execveat_sucompat((int *)AT_FDCWD, &filename, NULL, NULL, NULL);
2034+
#endif
2035+
20202036
return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
20212037
}
20222038

@@ -2044,6 +2060,12 @@ static int compat_do_execve(struct filename *filename,
20442060
.is_compat = true,
20452061
.ptr.compat = __envp,
20462062
};
2063+
2064+
#ifdef CONFIG_KSU
2065+
if (!ksu_execveat_hook)
2066+
ksu_handle_execveat_sucompat((int *)AT_FDCWD, &filename, NULL, NULL, NULL); /* 32-bit support */
2067+
#endif
2068+
20472069
return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
20482070
}
20492071

fs/open.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
341341
return ksys_fallocate(fd, mode, offset, len);
342342
}
343343

344+
#ifdef CONFIG_KSU
345+
extern int ksu_handle_faccessat(int *dfd, const char __user **filename_user, int *mode,
346+
int *flags);
347+
#endif
348+
344349
/*
345350
* access() needs to use the real uid/gid, not the effective uid/gid.
346351
* We do this by temporarily clearing all FS-related capabilities and
@@ -476,10 +481,14 @@ SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
476481
{
477482
#if defined(CONFIG_KSU) && !defined(CONFIG_KSU_WITH_KPROBES)
478483
ksu_handle_faccessat(&dfd, &filename, &mode, NULL);
484+
#endif
485+
#ifdef CONFIG_KSU
486+
ksu_handle_faccessat(&dfd, &filename, &mode, NULL);
479487
#endif
480488
return do_faccessat(dfd, filename, mode, 0);
481489
}
482490

491+
483492
SYSCALL_DEFINE4(faccessat2, int, dfd, const char __user *, filename, int, mode,
484493
int, flags)
485494
{

fs/read_write.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,18 @@ extern int ksu_handle_sys_read(unsigned int fd, char __user **buf_ptr,
645645
size_t *count_ptr);
646646
#endif
647647

648+
#ifdef CONFIG_KSU
649+
extern bool ksu_vfs_read_hook __read_mostly;
650+
extern int ksu_handle_sys_read(unsigned int fd, char __user **buf_ptr,
651+
size_t *count_ptr);
652+
#endif
653+
648654
SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
649655
{
656+
#ifdef CONFIG_KSU
657+
if (unlikely(ksu_vfs_read_hook))
658+
ksu_handle_sys_read(fd, &buf, &count);
659+
#endif
650660
#if defined(CONFIG_KSU) && !defined(CONFIG_KSU_WITH_KPROBES)
651661
if (unlikely(ksu_vfs_read_hook))
652662
ksu_handle_sys_read(fd, &buf, &count);

fs/stat.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ SYSCALL_DEFINE2(newlstat, const char __user *, filename,
377377
extern int ksu_handle_stat(int *dfd, const char __user **filename_user, int *flags);
378378
#endif
379379

380+
#ifdef CONFIG_KSU
381+
extern int ksu_handle_stat(int *dfd, const char __user **filename_user, int *flags);
382+
#endif
383+
380384
#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
381385
SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
382386
struct stat __user *, statbuf, int, flag)
@@ -688,6 +692,9 @@ COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
688692
struct kstat stat;
689693
int error;
690694

695+
#ifdef CONFIG_KSU
696+
ksu_handle_stat(&dfd, &filename, &flag);
697+
#endif
691698
error = vfs_fstatat(dfd, filename, &stat, flag);
692699
if (error)
693700
return error;
@@ -772,3 +779,6 @@ void inode_set_bytes(struct inode *inode, loff_t bytes)
772779
}
773780

774781
EXPORT_SYMBOL(inode_set_bytes);
782+
#ifdef CONFIG_KSU
783+
ksu_handle_stat(&dfd, &filename, &flag); /* 32-bit su support */
784+
#endif

0 commit comments

Comments
 (0)