From 5eed77355b23a1609d2ffddb2792f9c52d5a7f31 Mon Sep 17 00:00:00 2001 From: chloropine Date: Tue, 17 Nov 2026 23:42:55 -0500 Subject: [PATCH] kernel: Restore late-load ksud on restricted Samsung kernels --- kernel/hook/syscall_event_bridge.c | 18 +++++++++++ kernel/hook/syscall_event_bridge.h | 1 + kernel/hook/syscall_hook_manager.c | 2 ++ kernel/supercall/supercall.c | 26 +++++++++++----- kernel/supercall/supercall.h | 2 ++ userspace/ksud/src/restorecon.rs | 50 ++++++++++++++++++++++++++++-- userspace/ksud/src/utils.rs | 2 +- 7 files changed, 90 insertions(+), 11 deletions(-) diff --git a/kernel/hook/syscall_event_bridge.c b/kernel/hook/syscall_event_bridge.c index 9d2977f167e7..9107ed775b68 100644 --- a/kernel/hook/syscall_event_bridge.c +++ b/kernel/hook/syscall_event_bridge.c @@ -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) { @@ -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; diff --git a/kernel/hook/syscall_event_bridge.h b/kernel/hook/syscall_event_bridge.h index e97bbe9e4b38..37ad044fd44d 100644 --- a/kernel/hook/syscall_event_bridge.h +++ b/kernel/hook/syscall_event_bridge.h @@ -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); diff --git a/kernel/hook/syscall_hook_manager.c b/kernel/hook/syscall_hook_manager.c index e3a656537445..9abbe1486393 100644 --- a/kernel/hook/syscall_hook_manager.c +++ b/kernel/hook/syscall_hook_manager.c @@ -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); @@ -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); diff --git a/kernel/supercall/supercall.c b/kernel/supercall/supercall.c index 6db264956589..2d9a2f2e6f3f 100644 --- a/kernel/supercall/supercall.c +++ b/kernel/supercall/supercall.c @@ -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. @@ -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); } @@ -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"); } diff --git a/kernel/supercall/supercall.h b/kernel/supercall/supercall.h index 4728b17a1a6d..00a70cdc22fd 100644 --- a/kernel/supercall/supercall.h +++ b/kernel/supercall/supercall.h @@ -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); diff --git a/userspace/ksud/src/restorecon.rs b/userspace/ksud/src/restorecon.rs index a7cc81aca8e2..35d41cd0fdba 100644 --- a/userspace/ksud/src/restorecon.rs +++ b/userspace/ksud/src/restorecon.rs @@ -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"; @@ -33,6 +33,50 @@ pub fn lgetfilecon>(path: P) -> Result { Ok(con.to_string()) } +fn is_usable_existing_context(con: &str) -> bool { + !con.is_empty() && con != UNLABEL_CON +} + +pub fn label_ksud>(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>(path: P) -> Result<()> { lsetfilecon(path, SYSTEM_CON) } @@ -59,7 +103,7 @@ fn restore_syscon_if_unlabeled>(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(()) } diff --git a/userspace/ksud/src/utils.rs b/userspace/ksud/src/utils.rs index 33653c511885..86c23729b684 100644 --- a/userspace/ksud/src/utils.rs +++ b/userspace/ksud/src/utils.rs @@ -238,7 +238,7 @@ pub fn install(libadbroot: Option, data_path: Option) -> 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")?;