Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions kernel/hook/syscall_event_bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "hook/syscall_hook.h"
#include "hook/syscall_event_bridge.h"
#include "feature/adb_root.h"
#include "supercall/supercall.h"
#include "uapi/supercall.h"

static int ksu_handle_init_mark_tracker(const char __user **filename_user)
{
Expand Down Expand Up @@ -121,6 +123,22 @@ long __nocfi ksu_hook_execveat(int orig_nr, const struct pt_regs *regs)
return ksu_hook_execve_common(orig_nr, regs, true);
}

long __nocfi ksu_hook_reboot(int orig_nr, const struct pt_regs *regs)
{
int magic1 = (int)PT_REGS_SYSCALL_PARM1(regs);
int magic2 = (int)PT_REGS_PARM2(regs);

if (magic1 == KSU_INSTALL_MAGIC1 && magic2 == KSU_INSTALL_MAGIC2) {
int __user *outp = (int __user *)(unsigned long)PT_REGS_SYSCALL_PARM4(regs);
int ret = ksu_install_fd_to_user(outp);

pr_info("[%d] install ksu fd through syscall dispatcher: %d\n", current->pid, ret);
return ret;
}

return ksu_syscall_table[orig_nr](regs);
}

long __nocfi ksu_hook_setresuid(int orig_nr, const struct pt_regs *regs)
{
uid_t old_uid = current_uid().val;
Expand Down
1 change: 1 addition & 0 deletions kernel/hook/syscall_event_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ long ksu_hook_newfstatat(int orig_nr, const struct pt_regs *regs);
long ksu_hook_faccessat(int orig_nr, const struct pt_regs *regs);
long ksu_hook_execve(int orig_nr, const struct pt_regs *regs);
long ksu_hook_execveat(int orig_nr, const struct pt_regs *regs);
long ksu_hook_reboot(int orig_nr, const struct pt_regs *regs);
long ksu_hook_setresuid(int orig_nr, const struct pt_regs *regs);

void ksu_stop_ksud_execve_hook(void);
Expand Down
2 changes: 2 additions & 0 deletions kernel/hook/syscall_hook_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ void __init ksu_syscall_hook_manager_init(void)
ksu_register_syscall_hook(__NR_setresuid, ksu_hook_setresuid);
ksu_register_syscall_hook(__NR_execve, ksu_hook_execve);
ksu_register_syscall_hook(__NR_execveat, ksu_hook_execveat);
ksu_register_syscall_hook(__NR_reboot, ksu_hook_reboot);
ksu_register_syscall_hook(__NR_newfstatat, ksu_hook_newfstatat);
ksu_register_syscall_hook(__NR_faccessat, ksu_hook_faccessat);

Expand Down Expand Up @@ -172,6 +173,7 @@ void __exit ksu_syscall_hook_manager_exit(void)
ksu_unregister_syscall_hook(__NR_setresuid);
ksu_unregister_syscall_hook(__NR_execve);
ksu_unregister_syscall_hook(__NR_execveat);
ksu_unregister_syscall_hook(__NR_reboot);
ksu_unregister_syscall_hook(__NR_newfstatat);
ksu_unregister_syscall_hook(__NR_faccessat);

Expand Down
26 changes: 19 additions & 7 deletions kernel/supercall/supercall.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ int ksu_install_fd(void)
return ksu_install_fd_with_permissions(O_CLOEXEC, 0);
}

int ksu_install_fd_to_user(int __user *outp)
{
int fd = ksu_install_fd();

if (fd < 0)
return fd;

if (copy_to_user(outp, &fd, sizeof(fd))) {
pr_err("install ksu fd reply err\n");
ksu_close_fd(fd);
return -EFAULT;
}

return 0;
}

int ksu_install_su_fd(void)
{
// This descriptor must be installed after the exec into ksud.
Expand All @@ -102,13 +118,9 @@ bool ksu_is_su_session_fd(const struct file *filp)
static void ksu_install_fd_tw_func(struct callback_head *cb)
{
struct ksu_install_fd_tw *tw = container_of(cb, struct ksu_install_fd_tw, cb);
int fd = ksu_install_fd();
int ret = ksu_install_fd_to_user(tw->outp);

pr_info("[%d] install ksu fd: %d\n", current->pid, fd);
if (copy_to_user(tw->outp, &fd, sizeof(fd))) {
pr_err("install ksu fd reply err\n");
ksu_close_fd(fd);
}
pr_info("[%d] install ksu fd through reboot kprobe: %d\n", current->pid, ret);

kfree(tw);
}
Expand Down Expand Up @@ -152,7 +164,7 @@ void __init ksu_supercalls_init(void)

rc = register_kprobe(&reboot_kp);
if (rc) {
pr_err("reboot kprobe failed: %d\n", rc);
pr_warn("reboot kprobe failed: %d; syscall-dispatch fallback remains available\n", rc);
} else {
pr_info("reboot kprobe registered successfully\n");
}
Expand Down
2 changes: 2 additions & 0 deletions kernel/supercall/supercall.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct ksu_ioctl_cmd_map {

// Install KSU fd to current process
int ksu_install_fd(void);
// Install a KSU fd and return its number through a userspace pointer.
int ksu_install_fd_to_user(int __user *outp);
// Install a KSU fd that authorizes operations required while starting su.
int ksu_install_su_fd(void);
bool ksu_is_su_session_fd(const struct file *filp);
Expand Down
50 changes: 47 additions & 3 deletions userspace/ksud/src/restorecon.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::defs;
use anyhow::Result;
use anyhow::{Context, Result, bail};
use jwalk::{Parallelism::Serial, WalkDir};
use std::path::Path;

use anyhow::{Context, Ok};
use anyhow::Ok;
use extattr::{Flags as XattrFlags, lsetxattr};

pub const SYSTEM_CON: &str = "u:object_r:system_file:s0";
Expand Down Expand Up @@ -33,6 +33,50 @@ pub fn lgetfilecon<P: AsRef<Path>>(path: P) -> Result<String> {
Ok(con.to_string())
}

fn is_usable_existing_context(con: &str) -> bool {
!con.is_empty() && con != UNLABEL_CON
}

pub fn label_ksud<P: AsRef<Path>>(path: P) -> Result<()> {
let path = path.as_ref();
let inherited = lgetfilecon(path).context("Failed to read inherited ksud SELinux context")?;

match lsetxattr(path, SELINUX_XATTR, KSU_CON, XattrFlags::empty()) {
std::result::Result::Ok(()) => {}
Err(error) if error.0 == libc::EINVAL => {
// Samsung SEPF can reject the dynamically added ksu_file type.
// A newly copied file already has a context from the active policy,
// so retain that context instead of forcing another vendor type.
if is_usable_existing_context(&inherited) {
log::warn!(
"SELinux policy rejected {KSU_CON}; retaining inherited context {inherited}"
);
} else {
lsetfilecon(path, SYSTEM_CON).with_context(|| {
format!(
"ksu_file is unavailable and inherited context {inherited:?} is unusable; fallback to {SYSTEM_CON} failed"
)
})?;
}
}
Err(error) => {
return Err(error).with_context(|| {
format!(
"Failed to change SELinux context for {} to {KSU_CON}",
path.display()
)
});
}
}

let actual = lgetfilecon(path).context("Failed to verify ksud SELinux context")?;
if !is_usable_existing_context(&actual) {
bail!("ksud has unusable SELinux context {actual:?}");
}
log::info!("ksud SELinux label: requested={KSU_CON}, inherited={inherited}, actual={actual}");
Ok(())
}

pub fn setsyscon<P: AsRef<Path>>(path: P) -> Result<()> {
lsetfilecon(path, SYSTEM_CON)
}
Expand All @@ -59,7 +103,7 @@ fn restore_syscon_if_unlabeled<P: AsRef<Path>>(dir: P) -> Result<()> {
}

pub fn restorecon() -> Result<()> {
lsetfilecon(defs::DAEMON_PATH, KSU_CON)?;
label_ksud(defs::DAEMON_PATH)?;
restore_syscon_if_unlabeled(defs::MODULE_DIR)?;
Ok(())
}
2 changes: 1 addition & 1 deletion userspace/ksud/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ pub fn install(libadbroot: Option<PathBuf>, data_path: Option<PathBuf>) -> Resul
"/proc/self/exe",
defs::DAEMON_PATH,
)?;
restorecon::lsetfilecon(defs::DAEMON_PATH, restorecon::KSU_CON)?;
restorecon::label_ksud(defs::DAEMON_PATH)?;
// install binary assets
assets::ensure_binaries(false).with_context(|| "Failed to extract assets")?;

Expand Down