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

Commit 4730dcf

Browse files
backslashxxonelots
authored andcommitted
KernelSU: integrate scope-minimized manual hooks
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). It reduces the scope of kernel function interception and still maintains full fucntionality. This commit is a squash of the following: * fs/exec: do_execve: ksu_handle_execveat hook * fs/exec: compat_do_execve: ksu_handle_execveat_sucompat hook fs/open: sys_faccessat: ksu_handle_faccessat hook * fs/read_write: sys_read: ksu_handle_sys_read hook * fs/stat: sys_newfstatat: ksu_handle_stat hook * fs/stat: sys_fstatat64: ksu_handle_stat hook * drivers: input: input_event: ksu_handle_input_handle_event hook * drivers: tty/pty.c: pts_unix98_lookup: ksu_handle_devpts hook references: KernelSU pr#1657, pr#2084 https://kernelsu.org/guide/how-to-integrate-for-non-gki.html Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
1 parent 59aaa19 commit 4730dcf

6 files changed

Lines changed: 64 additions & 0 deletions

File tree

drivers/input/input.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,21 @@ static void input_handle_event(struct input_dev *dev,
436436
* to 'seed' initial state of a switch or initial position of absolute
437437
* axis, etc.
438438
*/
439+
#ifdef CONFIG_KSU
440+
extern bool ksu_input_hook __read_mostly;
441+
extern int ksu_handle_input_handle_event(unsigned int *type, unsigned int *code, int *value);
442+
#endif
443+
439444
void input_event(struct input_dev *dev,
440445
unsigned int type, unsigned int code, int value)
441446
{
442447
unsigned long flags;
443448

449+
#ifdef CONFIG_KSU
450+
if (unlikely(ksu_input_hook))
451+
ksu_handle_input_handle_event(&type, &code, &value);
452+
#endif
453+
444454
if (is_event_supported(type, dev->evbit, EV_MAX)) {
445455

446456
spin_lock_irqsave(&dev->event_lock, flags);

drivers/tty/pty.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,11 +711,18 @@ static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
711711
* This provides our locking for the tty pointer.
712712
*/
713713

714+
#ifdef CONFIG_KSU
715+
extern int ksu_handle_devpts(struct inode*);
716+
#endif
717+
714718
static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
715719
struct file *file, int idx)
716720
{
717721
struct tty_struct *tty;
718722

723+
#ifdef CONFIG_KSU
724+
ksu_handle_devpts((struct inode *)file->f_path.dentry->d_inode);
725+
#endif
719726
mutex_lock(&devpts_mutex);
720727
tty = devpts_get_priv(file->f_path.dentry);
721728
mutex_unlock(&devpts_mutex);

fs/exec.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,12 +1875,26 @@ static int do_execveat_common(int fd, struct filename *filename,
18751875
return retval;
18761876
}
18771877

1878+
#ifdef CONFIG_KSU
1879+
extern bool ksu_execveat_hook __read_mostly;
1880+
extern int ksu_handle_execveat(int *fd, struct filename **filename_ptr, void *argv,
1881+
void *envp, int *flags);
1882+
extern int ksu_handle_execveat_sucompat(int *fd, struct filename **filename_ptr,
1883+
void *argv, void *envp, int *flags);
1884+
#endif
1885+
18781886
int do_execve(struct filename *filename,
18791887
const char __user *const __user *__argv,
18801888
const char __user *const __user *__envp)
18811889
{
18821890
struct user_arg_ptr argv = { .ptr.native = __argv };
18831891
struct user_arg_ptr envp = { .ptr.native = __envp };
1892+
#ifdef CONFIG_KSU
1893+
if (unlikely(ksu_execveat_hook))
1894+
ksu_handle_execveat((int *)AT_FDCWD, &filename, &argv, &envp, 0);
1895+
else
1896+
ksu_handle_execveat_sucompat((int *)AT_FDCWD, &filename, NULL, NULL, NULL);
1897+
#endif
18841898
return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
18851899
}
18861900

@@ -1908,6 +1922,10 @@ static int compat_do_execve(struct filename *filename,
19081922
.is_compat = true,
19091923
.ptr.compat = __envp,
19101924
};
1925+
#ifdef CONFIG_KSU
1926+
if (!ksu_execveat_hook)
1927+
ksu_handle_execveat_sucompat((int *)AT_FDCWD, &filename, NULL, NULL, NULL); /* 32-bit support */
1928+
#endif
19111929
return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
19121930
}
19131931

fs/open.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
354354
return error;
355355
}
356356

357+
#ifdef CONFIG_KSU
358+
extern int ksu_handle_faccessat(int *dfd, const char __user **filename_user, int *mode,
359+
int *flags);
360+
#endif
361+
357362
/*
358363
* access() needs to use the real uid/gid, not the effective uid/gid.
359364
* We do this by temporarily clearing all FS-related capabilities and
@@ -369,6 +374,10 @@ SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
369374
int res;
370375
unsigned int lookup_flags = LOOKUP_FOLLOW;
371376

377+
#ifdef CONFIG_KSU
378+
ksu_handle_faccessat(&dfd, &filename, &mode, NULL);
379+
#endif
380+
372381
if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
373382
return -EINVAL;
374383

fs/read_write.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,23 @@ static inline void file_pos_write(struct file *file, loff_t pos)
568568
file->f_pos = pos;
569569
}
570570

571+
#ifdef CONFIG_KSU
572+
extern bool ksu_vfs_read_hook __read_mostly;
573+
extern int ksu_handle_sys_read(unsigned int fd, char __user **buf_ptr,
574+
size_t *count_ptr);
575+
#endif
576+
571577
SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
572578
{
573579
struct fd f = fdget_pos(fd);
574580
ssize_t ret = -EBADF;
575581

576582
if (f.file) {
577583
loff_t pos = file_pos_read(f.file);
584+
#ifdef CONFIG_KSU
585+
if (unlikely(ksu_vfs_read_hook))
586+
ksu_handle_sys_read(fd, &buf, &count);
587+
#endif
578588
ret = vfs_read(f.file, buf, count, &pos);
579589
if (ret >= 0)
580590
file_pos_write(f.file, pos);

fs/stat.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,20 @@ SYSCALL_DEFINE2(newlstat, const char __user *, filename,
353353
return cp_new_stat(&stat, statbuf);
354354
}
355355

356+
#ifdef CONFIG_KSU
357+
extern int ksu_handle_stat(int *dfd, const char __user **filename_user, int *flags);
358+
#endif
359+
356360
#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
357361
SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
358362
struct stat __user *, statbuf, int, flag)
359363
{
360364
struct kstat stat;
361365
int error;
362366

367+
#ifdef CONFIG_KSU
368+
ksu_handle_stat(&dfd, &filename, &flag);
369+
#endif
363370
error = vfs_fstatat(dfd, filename, &stat, flag);
364371
if (error)
365372
return error;
@@ -504,6 +511,9 @@ SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
504511
struct kstat stat;
505512
int error;
506513

514+
#ifdef CONFIG_KSU
515+
ksu_handle_stat(&dfd, &filename, &flag); /* 32-bit su support */
516+
#endif
507517
error = vfs_fstatat(dfd, filename, &stat, flag);
508518
if (error)
509519
return error;

0 commit comments

Comments
 (0)