mirror of
https://github.com/tiann/KernelSU.git
synced 2025-02-20 11:43:32 +08:00
Some checks failed
Build Manager / build-lkm (push) Waiting to run
Build Manager / build-ksud (macos-latest, aarch64-apple-darwin) (push) Blocked by required conditions
Build Manager / build-ksud (macos-latest, x86_64-apple-darwin) (push) Blocked by required conditions
Build Manager / build-ksud (ubuntu-latest, aarch64-linux-android) (push) Blocked by required conditions
Build Manager / build-ksud (ubuntu-latest, aarch64-unknown-linux-musl) (push) Blocked by required conditions
Build Manager / build-ksud (ubuntu-latest, x86_64-linux-android) (push) Blocked by required conditions
Build Manager / build-ksud (ubuntu-latest, x86_64-pc-windows-gnu) (push) Blocked by required conditions
Build Manager / build-ksud (ubuntu-latest, x86_64-unknown-linux-musl) (push) Blocked by required conditions
Build Manager / build-manager (push) Blocked by required conditions
Build Kernel - Android 12 / build-kernel (2024-05, 209) (push) Has been cancelled
Build Kernel - Android 12 / build-kernel (2024-08, 218) (push) Has been cancelled
Build Kernel - Android 12 / build-kernel (2024-11, 226) (push) Has been cancelled
Build Kernel - Android 12 / check-build-kernel (push) Has been cancelled
Build Kernel - Android 13 / build-kernel (2024-05, 148, 5.15) (push) Has been cancelled
Build Kernel - Android 13 / build-kernel (2024-05, 209, 5.10) (push) Has been cancelled
Build Kernel - Android 13 / build-kernel (2024-06, 210, 5.10) (push) Has been cancelled
Build Kernel - Android 13 / build-kernel (2024-07, 149, 5.15) (push) Has been cancelled
Build Kernel - Android 13 / build-kernel (2024-07, 214, 5.10) (push) Has been cancelled
Build Kernel - Android 13 / build-kernel (2024-08, 151, 5.15) (push) Has been cancelled
Build Kernel - Android 13 / build-kernel (2024-08, 218, 5.10) (push) Has been cancelled
Build Kernel - Android 13 / build-kernel (2024-09, 153, 5.15) (push) Has been cancelled
Build Kernel - Android 13 / build-kernel (2024-11, 167, 5.15) (push) Has been cancelled
Build Kernel - Android 13 / build-kernel (2024-11, 223, 5.10) (push) Has been cancelled
Build Kernel - Android 13 / check-build-kernel (2024-11, 167, 5.15) (push) Has been cancelled
Build Kernel - Android 13 / check-build-kernel (2024-11, 223, 5.10) (push) Has been cancelled
Build Kernel - Android 14 / build-kernel (2024-05, 148, 5.15) (push) Has been cancelled
Build Kernel - Android 14 / build-kernel (2024-05, 75, 6.1) (push) Has been cancelled
Build Kernel - Android 14 / build-kernel (2024-06, 149, 5.15) (push) Has been cancelled
Build Kernel - Android 14 / build-kernel (2024-06, 78, 6.1) (push) Has been cancelled
Build Kernel - Android 14 / build-kernel (2024-07, 153, 5.15) (push) Has been cancelled
Build Kernel - Android 14 / build-kernel (2024-07, 84, 6.1) (push) Has been cancelled
Build Kernel - Android 14 / build-kernel (2024-08, 158, 5.15) (push) Has been cancelled
Build Kernel - Android 14 / build-kernel (2024-08, 90, 6.1) (push) Has been cancelled
Build Kernel - Android 14 / build-kernel (2024-09, 164, 5.15) (push) Has been cancelled
Build Kernel - Android 14 / build-kernel (2024-09, 93, 6.1) (push) Has been cancelled
Build Kernel - Android 14 / build-kernel (2024-10, 99, 6.1) (push) Has been cancelled
Build Kernel - Android 14 / build-kernel (2024-11, 112, 6.1) (push) Has been cancelled
Build Kernel - Android 14 / build-kernel (2024-11, 167, 5.15) (push) Has been cancelled
Build Kernel - Android 14 / build-kernel (2024-12, 115, 6.1) (push) Has been cancelled
Build Kernel - Android 14 / check-build-kernel (2024-11, 167, 5.15) (push) Has been cancelled
Build Kernel - Android 14 / check-build-kernel (2024-12, 115, 6.1) (push) Has been cancelled
Build Kernel - Android 15 / build-kernel (2024-08, 30, 6.6) (push) Has been cancelled
Build Kernel - Android 15 / build-kernel (2024-09, 46, 6.6) (push) Has been cancelled
Build Kernel - Android 15 / build-kernel (2024-10, 50, 6.6) (push) Has been cancelled
Build Kernel - Android 15 / build-kernel (2024-11, 56, 6.6) (push) Has been cancelled
Build Kernel - Android 15 / build-kernel (2024-12, 57, 6.6) (push) Has been cancelled
Build Kernel - Android 15 / check-build-kernel (2024-12, 57, 6.6) (push) Has been cancelled
Build Kernel - ChromeOS ARCVM / Build ChromeOS ARCVM kernel (arm64, build.config.gki.aarch64, arm64_arcvm_defconfig, Image) (push) Has been cancelled
Build Kernel - ChromeOS ARCVM / Build ChromeOS ARCVM kernel (x86_64, build.config.gki.x86_64, x86_64_arcvm_defconfig, bzImage) (push) Has been cancelled
Build Kernel - AVD / build-kernel (aarch64, android-15-avd_aarch64.xml, android-15-avd_aarch64) (push) Has been cancelled
Build Kernel - AVD / build-kernel (x86_64, android-14-avd_x86_64.xml, android-14-avd_x86_64) (push) Has been cancelled
Build Kernel - AVD / build-kernel (x86_64, android-15-avd_x86_64.xml, android-15-avd_x86_64) (push) Has been cancelled
Build Kernel - WSA / build (arm64, 5.15.104.1) (push) Has been cancelled
Build Kernel - WSA / build (arm64, 5.15.104.2) (push) Has been cancelled
Build Kernel - WSA / build (arm64, 5.15.104.3) (push) Has been cancelled
Build Kernel - WSA / build (arm64, 5.15.104.4) (push) Has been cancelled
Build Kernel - WSA / build (arm64, 5.15.94.2) (push) Has been cancelled
Build Kernel - WSA / build (x86_64, 5.15.104.1) (push) Has been cancelled
Build Kernel - WSA / build (x86_64, 5.15.104.2) (push) Has been cancelled
Build Kernel - WSA / build (x86_64, 5.15.104.3) (push) Has been cancelled
Build Kernel - WSA / build (x86_64, 5.15.104.4) (push) Has been cancelled
Build Kernel - WSA / build (x86_64, 5.15.94.2) (push) Has been cancelled
Build Kernel - WSA / check_build (arm64) (push) Has been cancelled
Build Kernel - WSA / check_build (x86_64) (push) Has been cancelled
Build Kernel - Android 12 / upload-artifacts (push) Has been cancelled
Build Kernel - Android 13 / upload-artifacts (push) Has been cancelled
Build Kernel - Android 14 / upload-artifacts (push) Has been cancelled
Build Kernel - Android 15 / upload-artifacts (push) Has been cancelled
280 lines
6.6 KiB
C
280 lines
6.6 KiB
C
#include <linux/dcache.h>
|
|
#include <linux/security.h>
|
|
#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>
|
|
#include <linux/sched/task_stack.h>
|
|
|
|
#include "objsec.h"
|
|
#include "allowlist.h"
|
|
#include "arch.h"
|
|
#include "klog.h" // IWYU pragma: keep
|
|
#include "ksud.h"
|
|
#include "kernel_compat.h"
|
|
|
|
#define SU_PATH "/system/bin/su"
|
|
#define SH_PATH "/system/bin/sh"
|
|
|
|
extern void escape_to_root();
|
|
|
|
static void __user *userspace_stack_buffer(const void *d, size_t len)
|
|
{
|
|
/* To avoid having to mmap a page in userspace, just write below the stack
|
|
* pointer. */
|
|
char __user *p = (void __user *)current_user_stack_pointer() - len;
|
|
|
|
return copy_to_user(p, d, len) ? NULL : p;
|
|
}
|
|
|
|
static char __user *sh_user_path(void)
|
|
{
|
|
static const char sh_path[] = "/system/bin/sh";
|
|
|
|
return userspace_stack_buffer(sh_path, sizeof(sh_path));
|
|
}
|
|
|
|
static char __user *ksud_user_path(void)
|
|
{
|
|
static const char ksud_path[] = KSUD_PATH;
|
|
|
|
return userspace_stack_buffer(ksud_path, sizeof(ksud_path));
|
|
}
|
|
|
|
int ksu_handle_faccessat(int *dfd, const char __user **filename_user, int *mode,
|
|
int *__unused_flags)
|
|
{
|
|
const char su[] = SU_PATH;
|
|
|
|
if (!ksu_is_allow_uid(current_uid().val)) {
|
|
return 0;
|
|
}
|
|
|
|
char path[sizeof(su) + 1];
|
|
memset(path, 0, sizeof(path));
|
|
ksu_strncpy_from_user_nofault(path, *filename_user, sizeof(path));
|
|
|
|
if (unlikely(!memcmp(path, su, sizeof(su)))) {
|
|
pr_info("faccessat su->sh!\n");
|
|
*filename_user = sh_user_path();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int ksu_handle_stat(int *dfd, const char __user **filename_user, int *flags)
|
|
{
|
|
// const char sh[] = SH_PATH;
|
|
const char su[] = SU_PATH;
|
|
|
|
if (!ksu_is_allow_uid(current_uid().val)) {
|
|
return 0;
|
|
}
|
|
|
|
if (unlikely(!filename_user)) {
|
|
return 0;
|
|
}
|
|
|
|
char path[sizeof(su) + 1];
|
|
memset(path, 0, sizeof(path));
|
|
// Remove this later!! we use syscall hook, so this will never happen!!!!!
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0) && 0
|
|
// it becomes a `struct filename *` after 5.18
|
|
// https://elixir.bootlin.com/linux/v5.18/source/fs/stat.c#L216
|
|
const char sh[] = SH_PATH;
|
|
struct filename *filename = *((struct filename **)filename_user);
|
|
if (IS_ERR(filename)) {
|
|
return 0;
|
|
}
|
|
if (likely(memcmp(filename->name, su, sizeof(su))))
|
|
return 0;
|
|
pr_info("vfs_statx su->sh!\n");
|
|
memcpy((void *)filename->name, sh, sizeof(sh));
|
|
#else
|
|
ksu_strncpy_from_user_nofault(path, *filename_user, sizeof(path));
|
|
|
|
if (unlikely(!memcmp(path, su, sizeof(su)))) {
|
|
pr_info("newfstatat su->sh!\n");
|
|
*filename_user = sh_user_path();
|
|
}
|
|
#endif
|
|
|
|
return 0;
|
|
}
|
|
|
|
// the call from execve_handler_pre won't provided correct value for __never_use_argument, use them after fix execve_handler_pre, keeping them for consistence for manually patched code
|
|
int ksu_handle_execveat_sucompat(int *fd, struct filename **filename_ptr,
|
|
void *__never_use_argv, void *__never_use_envp,
|
|
int *__never_use_flags)
|
|
{
|
|
struct filename *filename;
|
|
const char sh[] = KSUD_PATH;
|
|
const char su[] = SU_PATH;
|
|
|
|
if (unlikely(!filename_ptr))
|
|
return 0;
|
|
|
|
filename = *filename_ptr;
|
|
if (IS_ERR(filename)) {
|
|
return 0;
|
|
}
|
|
|
|
if (likely(memcmp(filename->name, su, sizeof(su))))
|
|
return 0;
|
|
|
|
if (!ksu_is_allow_uid(current_uid().val))
|
|
return 0;
|
|
|
|
pr_info("do_execveat_common su found\n");
|
|
memcpy((void *)filename->name, sh, sizeof(sh));
|
|
|
|
escape_to_root();
|
|
|
|
return 0;
|
|
}
|
|
|
|
int ksu_handle_execve_sucompat(int *fd, const char __user **filename_user,
|
|
void *__never_use_argv, void *__never_use_envp,
|
|
int *__never_use_flags)
|
|
{
|
|
const char su[] = SU_PATH;
|
|
char path[sizeof(su) + 1];
|
|
|
|
if (unlikely(!filename_user))
|
|
return 0;
|
|
|
|
memset(path, 0, sizeof(path));
|
|
ksu_strncpy_from_user_nofault(path, *filename_user, sizeof(path));
|
|
|
|
if (likely(memcmp(path, su, sizeof(su))))
|
|
return 0;
|
|
|
|
if (!ksu_is_allow_uid(current_uid().val))
|
|
return 0;
|
|
|
|
pr_info("sys_execve su found\n");
|
|
*filename_user = ksud_user_path();
|
|
|
|
escape_to_root();
|
|
|
|
return 0;
|
|
}
|
|
|
|
int ksu_handle_devpts(struct inode *inode)
|
|
{
|
|
if (!current->mm) {
|
|
return 0;
|
|
}
|
|
|
|
uid_t uid = current_uid().val;
|
|
if (uid % 100000 < 10000) {
|
|
// not untrusted_app, ignore it
|
|
return 0;
|
|
}
|
|
|
|
if (!ksu_is_allow_uid(uid))
|
|
return 0;
|
|
|
|
if (ksu_devpts_sid) {
|
|
struct inode_security_struct *sec = selinux_inode(inode);
|
|
if (sec) {
|
|
sec->sid = ksu_devpts_sid;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int faccessat_handler_pre(struct kprobe *p, struct pt_regs *regs)
|
|
{
|
|
struct pt_regs *real_regs = PT_REAL_REGS(regs);
|
|
int *dfd = (int *)&PT_REGS_PARM1(real_regs);
|
|
const char __user **filename_user =
|
|
(const char **)&PT_REGS_PARM2(real_regs);
|
|
int *mode = (int *)&PT_REGS_PARM3(real_regs);
|
|
|
|
return ksu_handle_faccessat(dfd, filename_user, mode, NULL);
|
|
}
|
|
|
|
static int newfstatat_handler_pre(struct kprobe *p, struct pt_regs *regs)
|
|
{
|
|
struct pt_regs *real_regs = PT_REAL_REGS(regs);
|
|
int *dfd = (int *)&PT_REGS_PARM1(real_regs);
|
|
const char __user **filename_user =
|
|
(const char **)&PT_REGS_PARM2(real_regs);
|
|
int *flags = (int *)&PT_REGS_SYSCALL_PARM4(real_regs);
|
|
|
|
return ksu_handle_stat(dfd, filename_user, flags);
|
|
}
|
|
|
|
static int execve_handler_pre(struct kprobe *p, struct pt_regs *regs)
|
|
{
|
|
struct pt_regs *real_regs = PT_REAL_REGS(regs);
|
|
const char __user **filename_user =
|
|
(const char **)&PT_REGS_PARM1(real_regs);
|
|
|
|
return ksu_handle_execve_sucompat(AT_FDCWD, filename_user, NULL, NULL,
|
|
NULL);
|
|
}
|
|
|
|
static int pts_unix98_lookup_pre(struct kprobe *p, struct pt_regs *regs)
|
|
{
|
|
struct inode *inode;
|
|
struct file *file = (struct file *)PT_REGS_PARM2(regs);
|
|
inode = file->f_path.dentry->d_inode;
|
|
|
|
return ksu_handle_devpts(inode);
|
|
}
|
|
|
|
static struct kprobe *init_kprobe(const char *name,
|
|
kprobe_pre_handler_t handler)
|
|
{
|
|
struct kprobe *kp = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
|
|
if (!kp)
|
|
return NULL;
|
|
kp->symbol_name = name;
|
|
kp->pre_handler = handler;
|
|
|
|
int ret = register_kprobe(kp);
|
|
pr_info("sucompat: register_%s kprobe: %d\n", name, ret);
|
|
if (ret) {
|
|
kfree(kp);
|
|
return NULL;
|
|
}
|
|
|
|
return kp;
|
|
}
|
|
|
|
static void destroy_kprobe(struct kprobe **kp_ptr)
|
|
{
|
|
struct kprobe *kp = *kp_ptr;
|
|
if (!kp)
|
|
return;
|
|
unregister_kprobe(kp);
|
|
synchronize_rcu();
|
|
kfree(kp);
|
|
*kp_ptr = NULL;
|
|
}
|
|
|
|
static struct kprobe *su_kps[4];
|
|
|
|
// sucompat: permited process can execute 'su' to gain root access.
|
|
void ksu_sucompat_init()
|
|
{
|
|
su_kps[0] = init_kprobe(SYS_EXECVE_SYMBOL, execve_handler_pre);
|
|
su_kps[1] = init_kprobe(SYS_FACCESSAT_SYMBOL, faccessat_handler_pre);
|
|
su_kps[2] = init_kprobe(SYS_NEWFSTATAT_SYMBOL, newfstatat_handler_pre);
|
|
su_kps[3] = init_kprobe("pts_unix98_lookup", pts_unix98_lookup_pre);
|
|
}
|
|
|
|
void ksu_sucompat_exit()
|
|
{
|
|
for (int i = 0; i < ARRAY_SIZE(su_kps); i++) {
|
|
destroy_kprobe(&su_kps[i]);
|
|
}
|
|
}
|