Skip to content

Commit 5f9cada

Browse files
5ec1cffYlarod
authored andcommitted
kernel: handle ksud execve by syscall_hook_manager, use static_key
This should solves conflicts with other kernel module (for example, kpatch-next). TODO: move other ksud syscall hook to syscall_hook_manager
1 parent 68143c9 commit 5f9cada

3 files changed

Lines changed: 19 additions & 16 deletions

File tree

kernel/ksud.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void ksu_handle_execveat_ksud(const char *path, struct user_arg_ptr *argv)
229229
pr_info("exec zygote, /data prepared, second_stage: %d\n", init_second_stage_executed);
230230
on_post_fs_data();
231231
first_zygote = false;
232-
stop_execve_hook();
232+
ksu_stop_ksud_execve_hook();
233233
}
234234
}
235235
}
@@ -435,8 +435,7 @@ bool ksu_is_safe_mode()
435435
return false;
436436
}
437437

438-
static long (*orig_sys_execve)(const struct pt_regs *regs);
439-
static long ksu_sys_execve(const struct pt_regs *regs)
438+
void ksu_execve_hook_ksud(const struct pt_regs *regs)
440439
{
441440
const char __user **filename_user = (const char **)&PT_REGS_PARM1(regs);
442441
const char __user *const __user *__argv = (const char __user *const __user *)PT_REGS_PARM2(regs);
@@ -447,7 +446,7 @@ static long ksu_sys_execve(const struct pt_regs *regs)
447446
const char __user *fn;
448447

449448
if (!filename_user)
450-
goto do_orig;
449+
return;
451450

452451
addr = untagged_addr((unsigned long)*filename_user);
453452
fn = (const char __user *)addr;
@@ -456,13 +455,10 @@ static long ksu_sys_execve(const struct pt_regs *regs)
456455
ret = strncpy_from_user(path, fn, 32);
457456
if (ret < 0) {
458457
pr_err("Access filename failed for execve_handler_pre\n");
459-
goto do_orig;
458+
return;
460459
}
461460

462461
ksu_handle_execveat_ksud(path, &argv);
463-
464-
do_orig:
465-
return orig_sys_execve(regs);
466462
}
467463

468464
static long (*orig_sys_read)(const struct pt_regs *regs);
@@ -539,12 +535,6 @@ static void stop_init_rc_hook()
539535
pr_info("unregister init_rc syscall hook\n");
540536
}
541537

542-
static void stop_execve_hook()
543-
{
544-
ksu_syscall_table_unhook(__NR_execve);
545-
pr_info("unhook sys_execve\n");
546-
}
547-
548538
static void stop_input_hook()
549539
{
550540
static bool input_hook_stopped = false;
@@ -561,7 +551,6 @@ void ksu_ksud_init()
561551
{
562552
int ret;
563553

564-
ksu_syscall_table_hook(__NR_execve, ksu_sys_execve, &orig_sys_execve);
565554
ksu_syscall_table_hook(__NR_read, ksu_sys_read, &orig_sys_read);
566555
ksu_syscall_table_hook(__NR_fstat, ksu_sys_fstat, &orig_sys_fstat);
567556

@@ -573,7 +562,6 @@ void ksu_ksud_init()
573562

574563
void ksu_ksud_exit()
575564
{
576-
stop_execve_hook();
577565
// TODO:
578566
// this should be done before unregister vfs_read_kp
579567
// stop_init_rc_hook();

kernel/ksud.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define __KSU_H_KSUD
33

44
#include <linux/types.h>
5+
#include <asm/syscall.h>
56

67
#define KSUD_PATH "/data/adb/ksud"
78

@@ -20,4 +21,7 @@ extern u32 ksu_file_sid;
2021
extern bool ksu_module_mounted;
2122
extern bool ksu_boot_completed;
2223

24+
void ksu_execve_hook_ksud(const struct pt_regs *regs);
25+
void ksu_stop_ksud_execve_hook();
26+
2327
#endif

kernel/syscall_hook_manager.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "linux/compiler.h"
22
#include "linux/cred.h"
3+
#include "linux/jump_label.h"
34
#include "linux/printk.h"
45
#include "selinux/selinux.h"
56
#include <linux/spinlock.h>
@@ -9,6 +10,7 @@
910
#include <linux/ptrace.h>
1011
#include <linux/slab.h>
1112
#include <trace/events/syscalls.h>
13+
#include <linux/static_key.h>
1214

1315
#include "arch.h"
1416
#include "klog.h" // IWYU pragma: keep
@@ -150,12 +152,21 @@ static long __nocfi ksu_hook_faccessat(int orig_nr, const struct pt_regs *regs)
150152
return ksu_syscall_table[orig_nr](regs);
151153
}
152154

155+
DEFINE_STATIC_KEY_TRUE(ksud_execve_key);
156+
157+
void ksu_stop_ksud_execve_hook()
158+
{
159+
static_branch_disable(&ksud_execve_key);
160+
}
161+
153162
static long __nocfi ksu_hook_execve(int orig_nr, const struct pt_regs *regs)
154163
{
155164
int ret = 0;
156165

157166
const char __user **filename_user = (const char __user **)&PT_REGS_PARM1(regs);
158167
bool current_is_init = is_init(current_cred());
168+
if (static_branch_unlikely(&ksud_execve_key))
169+
ksu_execve_hook_ksud(regs);
159170
if (current->pid != 1 && current_is_init) {
160171
ksu_handle_init_mark_tracker(filename_user);
161172
} else if (ksu_su_compat_enabled) {

0 commit comments

Comments
 (0)