Skip to content
Open
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
130 changes: 112 additions & 18 deletions lkm/supercall/accctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
#include <linux/errno.h>
#include <linux/pid.h>
#include <linux/sched.h>
#include <linux/sched/signal.h>
#include <linux/seccomp.h>
#include <linux/spinlock.h>
#include <linux/thread_info.h>
#include <linux/version.h>
#include <linux/sched/task.h>
#include <linux/task_work.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <scdefs.h>
Expand All @@ -40,15 +46,55 @@ 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 {
u32 osid;
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");
Expand All @@ -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])
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -174,39 +219,88 @@ 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) {
logke("task_su: no task pid %d\n", pid);
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;
}