kernel: core_hook: provide a better reboot handler (#523)

* Revert "feat: try manual reboot hook (#521)"

This reverts commit 1853d9decf.

* kernel: core_hook: provide a better reboot handler

I propose that you pass cmd and arg as reference.
this is so we can have much more extendable use of that pointer

kernel: core_hook: provide sys_reboot handler
- 2e2727d56c

kernel: kp_ksud: add sys_reboot kp hook
- 03285886b0

I'm proposing passing arg as reference to arg pointer and also pass int cmd
we can use it to pass numbers atleast.
for advanced usage, we can use it as a delimiter so we can pass a pointer to array.

example pass a char *array[] which decays to a char ** and then use cmd as the number of array members.
we can pass the pointer of the first member of the array and use cmd as the delimiter (count) of members.

for simpler usecase, heres some that I added.

kernel: core_hook: expose  umount list on sys_reboot interface
- 352de41e4b

kernel: core_hook: expose nuke_ext4_sysfs to sys_reboot interface
- 83fc684ccb

ksud: add cmd for add-try-umount, wipe-umount-list and nuke-ext4-sysfs
- a4eab4b8c3

more usage demos
https://github.com/backslashxx/lkm_template/tree/write-pointer-on-pointer
https://github.com/backslashxx/lkm_template/tree/pointer-reuse

I actually proposed sys_reboot upstream because of this pointer that is very usable.

Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>

---------

Signed-off-by: backslashxx <118538522+backslashxx@users.noreply.github.com>
This commit is contained in:
backslashxx
2025-11-04 19:51:40 +08:00
committed by ShirkNeko
parent d6c6899d28
commit aef96cd93c
2 changed files with 32 additions and 18 deletions

View File

@@ -160,7 +160,6 @@ bool ksu_is_compat __read_mostly = false;
#endif
extern int __ksu_handle_devpts(struct inode *inode); // sucompat.c
extern void ksu_handle_reboot(int magic1, int magic2, void __user * arg); // supercalls.c
#ifdef CONFIG_KSU_MANUAL_SU
static void ksu_try_escalate_for_uid(uid_t uid)
@@ -1288,6 +1287,35 @@ int ksu_handle_setuid(struct cred *new, const struct cred *old)
#endif // #ifdef CONFIG_KSU_SUSFS
// downstream: make sure to pass arg as reference, this can allow us to extend things.
int ksu_handle_sys_reboot(int magic1, int magic2, unsigned int cmd, void __user **arg)
{
if (magic1 != KSU_INSTALL_MAGIC1)
return 0;
#ifdef CONFIG_KSU_DEBUG
pr_info("sys_reboot: intercepted call! magic: 0x%x id: %d\n", magic1, magic2);
#endif
// Check if this is a request to install KSU fd
if (magic2 == KSU_INSTALL_MAGIC2) {
int fd = ksu_install_fd();
pr_info("[%d] install ksu fd: %d\n", current->pid, fd);
// downstream: dereference all arg usage!
if (copy_to_user((void __user *)*arg, &fd, sizeof(fd))) {
pr_err("install ksu fd reply err\n");
}
return 0;
}
// extensions
return 0;
}
// Init functons - kprobe hooks
// 1. Reboot hook for installing fd
@@ -1296,13 +1324,10 @@ static int reboot_handler_pre(struct kprobe *p, struct pt_regs *regs)
struct pt_regs *real_regs = PT_REAL_REGS(regs);
int magic1 = (int)PT_REGS_PARM1(real_regs);
int magic2 = (int)PT_REGS_PARM2(real_regs);
unsigned long arg4;
int cmd = (int)PT_REGS_PARM3(real_regs);
void __user **arg = (void __user **)&PT_REGS_SYSCALL_PARM4(real_regs);
// Check if this is a request to install KSU fd
arg4 = (unsigned long)PT_REGS_SYSCALL_PARM4(real_regs);
ksu_handle_reboot(magic1, magic2, (void __user *) arg4);
return 0;
return ksu_handle_sys_reboot(magic1, magic2, cmd, arg);
}
static struct kprobe reboot_kp = {

View File

@@ -69,17 +69,6 @@ static void init_uid_scanner(void)
}
}
void ksu_handle_reboot(int magic1, int magic2, void __user * arg) {
if (magic1 == KSU_INSTALL_MAGIC1 && magic2 == KSU_INSTALL_MAGIC2) {
int fd = ksu_install_fd();
pr_info("[%d] install ksu fd: %d\n", current->pid, fd);
if (copy_to_user(arg, &fd, sizeof(fd))) {
pr_err("install ksu fd reply err\n");
}
}
}
static int do_grant_root(void __user *arg)
{
// Check if current UID is allowed