diff --git a/lkm/supercall/accctl.c b/lkm/supercall/accctl.c index 0e93f5ada..1f26c373a 100644 --- a/lkm/supercall/accctl.c +++ b/lkm/supercall/accctl.c @@ -15,7 +15,13 @@ #include #include #include +#include +#include +#include +#include +#include #include +#include #include #include #include @@ -40,6 +46,11 @@ struct kp_lsm_blob_sizes { static struct kp_lsm_blob_sizes *kp_selinux_blob_sizes; +typedef int (*kp_task_work_add_t)(struct task_struct *task, struct callback_head *work, + enum task_work_notify_mode mode); + +static kp_task_work_add_t kp_task_work_add; + /* task_security_struct { osid, sid, ... } — RANDSTRUCT is off, so sid is the * second u32 (offset 4). */ struct kp_task_sec { @@ -47,8 +58,43 @@ struct kp_task_sec { u32 sid; }; +/* + * Disable seccomp enforcement for the current task without manually releasing + * its filter. The filter reference remains owned by current and is released + * through the normal task-exit path. + * + * This deliberately avoids calling seccomp_filter_release(): its internal + * teardown contract differs across kernel versions and Android GKI backports, + * and cannot be classified reliably from a generic version check. + */ +static bool kp_disable_current_seccomp(void) +{ + struct sighand_struct *sighand = current->sighand; + + if (!sighand) { + logkw("seccomp disable skipped: current has no sighand\n"); + return false; + } + + spin_lock_irq(&sighand->siglock); +#if defined(CONFIG_GENERIC_ENTRY) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0) + clear_syscall_work(SECCOMP); +#else + clear_thread_flag(TIF_SECCOMP); +#endif + current->seccomp.mode = SECCOMP_MODE_DISABLED; + spin_unlock_irq(&sighand->siglock); + + logkw("seccomp mode disabled; filter retained until task exit\n"); + return true; +} + int kp_accctl_init(void) { + kp_task_work_add = (kp_task_work_add_t)kp_resolve_symbol_variant("task_work_add"); + if (!kp_task_work_add) + logke("failed to resolve task_work_add; task_su disabled\n"); + kp_selinux_blob_sizes = (struct kp_lsm_blob_sizes *)kp_resolve_symbol("selinux_blob_sizes"); if (!kp_selinux_blob_sizes) { logke("failed to resolve selinux_blob_sizes; selinux translabel disabled\n"); @@ -64,7 +110,7 @@ int kp_accctl_init(void) static int kp_selinux_set_cred_context(struct cred *new, const char *sctx) { struct kp_task_sec *tsec; - u32 sid; + u32 sid = 0; int rc; if (!kp_selinux_blob_sizes || !sctx || !sctx[0]) @@ -139,9 +185,6 @@ int kp_commit_su(uid_t to_uid, const char *sctx) { int rc; - /* Disable seccomp on the caller, matching KP's commit_common_su. */ - current_thread_info()->flags &= ~_TIF_SECCOMP; - /* Empty sctx: default to the magisk domain (KP initializes all_allow_sctx * to ALL_ALLOW_SCONTEXT_MAGISK). The u:r:kernel:s0 domain cannot connect * sockets or run ART (seen: libsu RootServer ClassNotFoundException after @@ -152,6 +195,7 @@ int kp_commit_su(uid_t to_uid, const char *sctx) if (kp_selinux_blob_sizes) { rc = commit_common_su(to_uid, ALL_ALLOW_SCONTEXT_MAGISK); if (!rc) { + kp_disable_current_seccomp(); logki("commit_su: to_uid=%u magisk domain\n", to_uid); return 0; } @@ -161,6 +205,7 @@ int kp_commit_su(uid_t to_uid, const char *sctx) if (!new) return -ENOMEM; commit_creds(new); + kp_disable_current_seccomp(); logki("commit_su: to_uid=%u kernel cred (u:r:kernel:s0)\n", to_uid); return 0; } @@ -174,16 +219,55 @@ int kp_commit_su(uid_t to_uid, const char *sctx) if (!new) return -ENOMEM; commit_creds(new); + kp_disable_current_seccomp(); return 0; } + kp_disable_current_seccomp(); return 0; } +struct kp_task_su_work { + struct callback_head work; + uid_t to_uid; + char *sctx; +}; + +/* Run credential changes in the target task's own context. */ +__attribute__((no_sanitize("cfi"))) +static void kp_task_su_workfn(struct callback_head *head) +{ + struct kp_task_su_work *work = container_of(head, struct kp_task_su_work, work); + struct cred *new; + int rc; + + if (current->flags & PF_EXITING) + goto out; + + rc = commit_common_su(work->to_uid, work->sctx); + if (rc) { + logkw("task_su: credential setup failed (%d), kernel cred fallback\n", rc); + new = prepare_kernel_cred(NULL); + if (new) + commit_creds(new); + else + logke("task_su: kernel cred fallback failed\n"); + } + + logki("task_su: pid %d -> uid %u\n", task_pid_nr(current), work->to_uid); +out: + kfree(work->sctx); + kfree(work); +} + +__attribute__((no_sanitize("cfi"))) int kp_task_su(pid_t pid, uid_t to_uid, const char *sctx) { struct task_struct *task; - const struct cred *old; - struct cred *new; + struct kp_task_su_work *work; + int rc; + + if (!kp_task_work_add) + return -ENOSYS; task = find_get_task_by_vpid(pid); if (!task) { @@ -191,22 +275,32 @@ int kp_task_su(pid_t pid, uid_t to_uid, const char *sctx) return -ESRCH; } - new = prepare_creds(); - if (!new) { + work = kzalloc(sizeof(*work), GFP_KERNEL); + if (!work) { put_task_struct(task); return -ENOMEM; } - su_cred(new, to_uid); - (void)sctx; - - rcu_read_lock(); - old = task->cred; - rcu_assign_pointer(task->cred, new); - rcu_assign_pointer(task->real_cred, new); - rcu_read_unlock(); - put_cred(old); + work->to_uid = to_uid; + if (sctx && sctx[0]) { + work->sctx = kstrdup(sctx, GFP_KERNEL); + if (!work->sctx) { + kfree(work); + put_task_struct(task); + return -ENOMEM; + } + } + init_task_work(&work->work, kp_task_su_workfn); + /* TWA_SIGNAL requests prompt execution when the target is in the kernel. */ + rc = kp_task_work_add(task, &work->work, TWA_SIGNAL); put_task_struct(task); - logki("task_su: pid %d -> uid %u\n", pid, to_uid); + if (rc) { + kfree(work->sctx); + kfree(work); + logkw("task_su: failed to queue work for pid %d: %d\n", pid, rc); + return rc; + } + + logki("task_su: credential update queued for pid %d -> uid %u\n", pid, to_uid); return 0; }