Skip to content

Commit bd238d6

Browse files
committed
scope-minimized manual hooks v1.7
backslashxx/KernelSU#5
1 parent 8f82fa8 commit bd238d6

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed

drivers/input/input.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,22 @@ 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 __attribute__((cold)) int ksu_handle_input_handle_event(
442+
unsigned int *type, unsigned int *code, int *value);
443+
#endif
444+
439445
void input_event(struct input_dev *dev,
440446
unsigned int type, unsigned int code, int value)
441447
{
442448
unsigned long flags;
443449

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

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

fs/exec.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,12 +1837,21 @@ static int do_execveat_common(int fd, struct filename *filename,
18371837
return retval;
18381838
}
18391839

1840+
#ifdef CONFIG_KSU
1841+
__attribute__((hot))
1842+
extern int ksu_handle_execveat(int *fd, struct filename **filename_ptr,
1843+
void *argv, void *envp, int *flags);
1844+
#endif
1845+
18401846
int do_execve(struct filename *filename,
18411847
const char __user *const __user *__argv,
18421848
const char __user *const __user *__envp)
18431849
{
18441850
struct user_arg_ptr argv = { .ptr.native = __argv };
18451851
struct user_arg_ptr envp = { .ptr.native = __envp };
1852+
#ifdef CONFIG_KSU
1853+
ksu_handle_execveat((int *)AT_FDCWD, &filename, &argv, &envp, 0);
1854+
#endif
18461855
return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
18471856
}
18481857

@@ -1870,6 +1879,9 @@ static int compat_do_execve(struct filename *filename,
18701879
.is_compat = true,
18711880
.ptr.compat = __envp,
18721881
};
1882+
#ifdef CONFIG_KSU // 32-bit ksud and 32-on-64 support
1883+
ksu_handle_execveat((int *)AT_FDCWD, &filename, &argv, &envp, 0);
1884+
#endif
18731885
return do_execveat_common(AT_FDCWD, filename, argv, envp, 0);
18741886
}
18751887

fs/open.c

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

358+
#ifdef CONFIG_KSU
359+
__attribute__((hot))
360+
extern int ksu_handle_faccessat(int *dfd, const char __user **filename_user,
361+
int *mode, int *flags);
362+
#endif
363+
358364
/*
359365
* access() needs to use the real uid/gid, not the effective uid/gid.
360366
* We do this by temporarily clearing all FS-related capabilities and
@@ -370,6 +376,10 @@ SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
370376
int res;
371377
unsigned int lookup_flags = LOOKUP_FOLLOW;
372378

379+
#ifdef CONFIG_KSU
380+
ksu_handle_faccessat(&dfd, &filename, &mode, NULL);
381+
#endif
382+
373383
if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
374384
return -EINVAL;
375385

fs/stat.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,22 @@ SYSCALL_DEFINE2(newlstat, const char __user *, filename,
287287
return cp_new_stat(&stat, statbuf);
288288
}
289289

290+
#ifdef CONFIG_KSU
291+
__attribute__((hot))
292+
extern int ksu_handle_stat(int *dfd, const char __user **filename_user,
293+
int *flags);
294+
#endif
295+
290296
#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
291297
SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
292298
struct stat __user *, statbuf, int, flag)
293299
{
294300
struct kstat stat;
295301
int error;
296302

303+
#ifdef CONFIG_KSU
304+
ksu_handle_stat(&dfd, &filename, &flag);
305+
#endif
297306
error = vfs_fstatat(dfd, filename, &stat, flag);
298307
if (error)
299308
return error;
@@ -436,6 +445,9 @@ SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
436445
struct kstat stat;
437446
int error;
438447

448+
#ifdef CONFIG_KSU // 32-bit su
449+
ksu_handle_stat(&dfd, &filename, &flag);
450+
#endif
439451
error = vfs_fstatat(dfd, filename, &stat, flag);
440452
if (error)
441453
return error;

kernel/reboot.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,21 @@ static DEFINE_MUTEX(reboot_mutex);
277277
*
278278
* reboot doesn't sync: do that yourself before calling this.
279279
*/
280+
281+
#ifdef CONFIG_KSU
282+
extern int ksu_handle_sys_reboot(int magic1, int magic2, unsigned int cmd, void __user **arg);
283+
#endif
284+
280285
SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
281286
void __user *, arg)
282287
{
283288
struct pid_namespace *pid_ns = task_active_pid_ns(current);
284289
char buffer[256];
285290
int ret = 0;
286291

292+
#ifdef CONFIG_KSU
293+
ksu_handle_sys_reboot(magic1, magic2, cmd, &arg);
294+
#endif
287295
/* We only trust the superuser with rebooting the system. */
288296
if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
289297
return -EPERM;

security/selinux/hooks.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,6 +2314,11 @@ static u32 ptrace_parent_sid(struct task_struct *task)
23142314
return sid;
23152315
}
23162316

2317+
#ifdef CONFIG_KSU
2318+
extern bool is_ksu_transition(const struct task_security_struct *old_tsec,
2319+
const struct task_security_struct *new_tsec);
2320+
#endif
2321+
23172322
static int check_nnp_nosuid(const struct linux_binprm *bprm,
23182323
const struct task_security_struct *old_tsec,
23192324
const struct task_security_struct *new_tsec)
@@ -2328,6 +2333,11 @@ static int check_nnp_nosuid(const struct linux_binprm *bprm,
23282333
if (new_tsec->sid == old_tsec->sid)
23292334
return 0; /* No change in credentials */
23302335

2336+
#ifdef CONFIG_KSU
2337+
if (is_ksu_transition(old_tsec, new_tsec))
2338+
return 0;
2339+
#endif
2340+
23312341
/*
23322342
* The only transitions we permit under NNP or nosuid
23332343
* are transitions to bounded SIDs, i.e. SIDs that are

0 commit comments

Comments
 (0)