2023-01-25 21:53:19 +08:00
|
|
|
#include "asm/current.h"
|
|
|
|
#include "linux/cred.h"
|
|
|
|
#include "linux/err.h"
|
|
|
|
#include "linux/fs.h"
|
|
|
|
#include "linux/kprobes.h"
|
|
|
|
#include "linux/types.h"
|
|
|
|
#include "linux/uaccess.h"
|
|
|
|
#include "linux/version.h"
|
2023-01-22 11:32:28 +08:00
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
|
2023-01-25 21:53:19 +08:00
|
|
|
#include "linux/sched/task_stack.h"
|
2023-01-22 11:32:28 +08:00
|
|
|
#else
|
2023-01-25 21:53:19 +08:00
|
|
|
#include "linux/sched.h"
|
2023-01-22 11:32:28 +08:00
|
|
|
#endif
|
2022-12-14 14:55:29 +07:00
|
|
|
|
|
|
|
#include "allowlist.h"
|
2023-01-25 21:53:19 +08:00
|
|
|
#include "arch.h"
|
2023-01-25 22:24:00 +08:00
|
|
|
#include "klog.h" // IWYU pragma: keep
|
2023-04-03 20:29:10 +08:00
|
|
|
#include "ksud.h"
|
2022-12-14 14:55:29 +07:00
|
|
|
|
|
|
|
#define SU_PATH "/system/bin/su"
|
|
|
|
#define SH_PATH "/system/bin/sh"
|
|
|
|
|
2022-12-19 17:14:38 +07:00
|
|
|
extern void escape_to_root();
|
2022-12-14 14:55:29 +07:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
static void __user *userspace_stack_buffer(const void *d, size_t len)
|
|
|
|
{
|
2023-01-25 21:53:19 +08:00
|
|
|
/* To avoid having to mmap a page in userspace, just write below the stack
|
|
|
|
* pointer. */
|
2022-12-14 14:55:29 +07:00
|
|
|
char __user *p = (void __user *)current_user_stack_pointer() - len;
|
|
|
|
|
|
|
|
return copy_to_user(p, d, len) ? NULL : p;
|
|
|
|
}
|
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
static char __user *sh_user_path(void)
|
|
|
|
{
|
2022-12-14 14:55:29 +07:00
|
|
|
static const char sh_path[] = "/system/bin/sh";
|
|
|
|
|
|
|
|
return userspace_stack_buffer(sh_path, sizeof(sh_path));
|
|
|
|
}
|
|
|
|
|
2023-01-19 13:31:55 +07:00
|
|
|
int ksu_handle_faccessat(int *dfd, const char __user **filename_user, int *mode,
|
|
|
|
int *flags)
|
2022-12-27 18:21:10 +07:00
|
|
|
{
|
|
|
|
struct filename *filename;
|
|
|
|
const char su[] = SU_PATH;
|
2022-12-14 14:55:29 +07:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
if (!ksu_is_allow_uid(current_uid().val)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-12-14 14:55:29 +07:00
|
|
|
|
2023-01-19 13:31:55 +07:00
|
|
|
filename = getname(*filename_user);
|
2022-12-14 14:55:29 +07:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
if (IS_ERR(filename)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-06-16 20:53:15 +09:00
|
|
|
if (unlikely(!memcmp(filename->name, su, sizeof(su)))) {
|
2022-12-27 18:21:10 +07:00
|
|
|
pr_info("faccessat su->sh!\n");
|
2023-01-19 13:31:55 +07:00
|
|
|
*filename_user = sh_user_path();
|
2022-12-27 18:21:10 +07:00
|
|
|
}
|
2022-12-14 14:55:29 +07:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
putname(filename);
|
2022-12-14 16:26:43 +07:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
return 0;
|
2022-12-14 14:55:29 +07:00
|
|
|
}
|
|
|
|
|
2023-01-19 13:31:55 +07:00
|
|
|
int ksu_handle_stat(int *dfd, const char __user **filename_user, int *flags)
|
2022-12-27 18:21:10 +07:00
|
|
|
{
|
|
|
|
// const char sh[] = SH_PATH;
|
|
|
|
struct filename *filename;
|
|
|
|
const char su[] = SU_PATH;
|
2022-12-14 14:55:29 +07:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
if (!ksu_is_allow_uid(current_uid().val)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-12-14 14:55:29 +07:00
|
|
|
|
2023-06-16 20:53:15 +09:00
|
|
|
if (unlikely(!filename_user)) {
|
2023-01-19 13:31:55 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = getname(*filename_user);
|
2022-12-14 14:55:29 +07:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
if (IS_ERR(filename)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-06-16 20:53:15 +09:00
|
|
|
if (unlikely(!memcmp(filename->name, su, sizeof(su)))) {
|
2022-12-27 18:21:10 +07:00
|
|
|
pr_info("newfstatat su->sh!\n");
|
2023-01-19 13:31:55 +07:00
|
|
|
*filename_user = sh_user_path();
|
2022-12-27 18:21:10 +07:00
|
|
|
}
|
2022-12-14 14:55:29 +07:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
putname(filename);
|
2022-12-14 16:26:43 +07:00
|
|
|
|
2022-12-27 18:21:10 +07:00
|
|
|
return 0;
|
2022-12-14 14:55:29 +07:00
|
|
|
}
|
|
|
|
|
2023-01-25 21:53:19 +08:00
|
|
|
int ksu_handle_execveat_sucompat(int *fd, struct filename **filename_ptr,
|
|
|
|
void *argv, void *envp, int *flags)
|
2022-12-27 18:21:10 +07:00
|
|
|
{
|
|
|
|
struct filename *filename;
|
2023-04-03 20:29:10 +08:00
|
|
|
const char sh[] = KSUD_PATH;
|
2022-12-27 18:21:10 +07:00
|
|
|
const char su[] = SU_PATH;
|
|
|
|
|
2023-06-16 20:53:15 +09:00
|
|
|
if (unlikely(!filename_ptr))
|
2023-01-19 13:31:55 +07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
filename = *filename_ptr;
|
2022-12-27 18:21:10 +07:00
|
|
|
if (IS_ERR(filename)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-16 20:53:15 +09:00
|
|
|
if (likely(memcmp(filename->name, su, sizeof(su))))
|
2022-12-27 18:21:10 +07:00
|
|
|
return 0;
|
|
|
|
|
2023-06-16 20:53:15 +09:00
|
|
|
if (!ksu_is_allow_uid(current_uid().val))
|
|
|
|
return 0;
|
2022-12-27 18:21:10 +07:00
|
|
|
|
2023-06-16 20:53:15 +09:00
|
|
|
pr_info("do_execveat_common su found\n");
|
|
|
|
memcpy((void *)filename->name, sh, sizeof(sh));
|
|
|
|
|
|
|
|
escape_to_root();
|
2022-12-27 18:21:10 +07:00
|
|
|
|
|
|
|
return 0;
|
2022-12-14 14:55:29 +07:00
|
|
|
}
|
|
|
|
|
2023-01-25 21:53:19 +08:00
|
|
|
#ifdef CONFIG_KPROBES
|
2023-01-01 23:54:54 +07:00
|
|
|
|
2023-01-19 13:31:55 +07:00
|
|
|
static int faccessat_handler_pre(struct kprobe *p, struct pt_regs *regs)
|
|
|
|
{
|
|
|
|
int *dfd = (int *)PT_REGS_PARM1(regs);
|
|
|
|
const char __user **filename_user = (const char **)&PT_REGS_PARM2(regs);
|
|
|
|
int *mode = (int *)&PT_REGS_PARM3(regs);
|
|
|
|
int *flags = (int *)&PT_REGS_PARM4(regs);
|
|
|
|
|
|
|
|
return ksu_handle_faccessat(dfd, filename_user, mode, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int newfstatat_handler_pre(struct kprobe *p, struct pt_regs *regs)
|
|
|
|
{
|
2023-02-01 19:48:36 +08:00
|
|
|
int *dfd = (int *)&PT_REGS_PARM1(regs);
|
2023-01-19 13:31:55 +07:00
|
|
|
const char __user **filename_user = (const char **)&PT_REGS_PARM2(regs);
|
2023-02-02 23:38:04 +08:00
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
|
|
|
|
// static int vfs_statx(int dfd, const char __user *filename, int flags, struct kstat *stat, u32 request_mask)
|
2023-01-19 13:31:55 +07:00
|
|
|
int *flags = (int *)&PT_REGS_PARM3(regs);
|
2023-02-01 19:48:36 +08:00
|
|
|
#else
|
|
|
|
// int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,int flag)
|
|
|
|
int *flags = (int *)&PT_REGS_PARM4(regs);
|
|
|
|
#endif
|
2023-01-19 13:31:55 +07:00
|
|
|
|
|
|
|
return ksu_handle_stat(dfd, filename_user, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://elixir.bootlin.com/linux/v5.10.158/source/fs/exec.c#L1864
|
|
|
|
static int execve_handler_pre(struct kprobe *p, struct pt_regs *regs)
|
|
|
|
{
|
|
|
|
int *fd = (int *)&PT_REGS_PARM1(regs);
|
|
|
|
struct filename **filename_ptr =
|
|
|
|
(struct filename **)&PT_REGS_PARM2(regs);
|
|
|
|
void *argv = (void *)&PT_REGS_PARM3(regs);
|
|
|
|
void *envp = (void *)&PT_REGS_PARM4(regs);
|
|
|
|
int *flags = (int *)&PT_REGS_PARM5(regs);
|
|
|
|
|
2023-01-25 21:53:19 +08:00
|
|
|
return ksu_handle_execveat_sucompat(fd, filename_ptr, argv, envp,
|
|
|
|
flags);
|
2023-01-19 13:31:55 +07:00
|
|
|
}
|
|
|
|
|
2022-12-14 14:55:29 +07:00
|
|
|
static struct kprobe faccessat_kp = {
|
2023-01-15 15:34:15 +08:00
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 17, 0)
|
2022-12-27 18:21:10 +07:00
|
|
|
.symbol_name = "do_faccessat",
|
2023-01-15 15:34:15 +08:00
|
|
|
#else
|
|
|
|
.symbol_name = "sys_faccessat",
|
|
|
|
#endif
|
2022-12-27 18:21:10 +07:00
|
|
|
.pre_handler = faccessat_handler_pre,
|
2022-12-14 14:55:29 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct kprobe newfstatat_kp = {
|
2023-02-02 23:38:04 +08:00
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
|
2022-12-27 18:21:10 +07:00
|
|
|
.symbol_name = "vfs_statx",
|
2023-02-01 19:48:36 +08:00
|
|
|
#else
|
2023-02-02 23:38:04 +08:00
|
|
|
.symbol_name = "vfs_fstatat",
|
2023-02-01 19:48:36 +08:00
|
|
|
#endif
|
2022-12-27 18:21:10 +07:00
|
|
|
.pre_handler = newfstatat_handler_pre,
|
2022-12-14 14:55:29 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct kprobe execve_kp = {
|
2023-01-10 23:20:32 +08:00
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 9, 0)
|
2022-12-27 18:21:10 +07:00
|
|
|
.symbol_name = "do_execveat_common",
|
2023-02-02 23:38:04 +08:00
|
|
|
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0)
|
2023-01-15 17:05:08 +08:00
|
|
|
.symbol_name = "__do_execve_file",
|
2023-02-02 23:38:04 +08:00
|
|
|
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)
|
2023-02-01 19:48:36 +08:00
|
|
|
.symbol_name = "do_execveat_common",
|
2023-01-15 17:05:08 +08:00
|
|
|
#endif
|
2022-12-27 18:21:10 +07:00
|
|
|
.pre_handler = execve_handler_pre,
|
2022-12-14 14:55:29 +07:00
|
|
|
};
|
|
|
|
|
2023-01-25 21:53:19 +08:00
|
|
|
#endif
|
2023-01-01 23:54:54 +07:00
|
|
|
|
2022-12-14 14:55:29 +07:00
|
|
|
// sucompat: permited process can execute 'su' to gain root access.
|
2023-01-25 21:53:19 +08:00
|
|
|
void ksu_enable_sucompat()
|
2022-12-27 18:21:10 +07:00
|
|
|
{
|
2023-01-25 21:53:19 +08:00
|
|
|
#ifdef CONFIG_KPROBES
|
2022-12-27 18:21:10 +07:00
|
|
|
int ret;
|
|
|
|
ret = register_kprobe(&execve_kp);
|
2023-01-25 21:53:19 +08:00
|
|
|
pr_info("sucompat: execve_kp: %d\n", ret);
|
2022-12-27 18:21:10 +07:00
|
|
|
ret = register_kprobe(&newfstatat_kp);
|
2023-01-25 21:53:19 +08:00
|
|
|
pr_info("sucompat: newfstatat_kp: %d\n", ret);
|
2022-12-27 18:21:10 +07:00
|
|
|
ret = register_kprobe(&faccessat_kp);
|
2023-01-25 21:53:19 +08:00
|
|
|
pr_info("sucompat: faccessat_kp: %d\n", ret);
|
|
|
|
#endif
|
2022-12-19 19:38:20 +07:00
|
|
|
}
|