Files
DroidDeck/tools/nativeguest/hook/signals.c
T
maxjivi05 fe95624af6 Emulate only host-architecture seccomp traps in the guest hook; report the calling thread's seccomp mode in FEX's /proc/self/status
A SIGSYS carrying another architecture's seccomp siginfo now takes its default action when the program has no handler, so a process whose emulated filter traps a syscall while SIGSYS is blocked or ignored dies as on Linux.
2026-12-03 13:01:38 -05:00

349 lines
16 KiB
C

// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
// parts share their static functions and are built with the same command as before).
//
// The SIGSYS handler for system calls Android's seccomp filter blocks, and the signal hooks that
// keep programs from replacing or blocking it.
#ifndef DDNATV_HOST_TEST
#ifndef SYS_SECCOMP
#define SYS_SECCOMP 1
#endif
#if defined(__aarch64__)
#define DDNATV_HOST_AUDIT_ARCH 0xC00000B7U
#else
#define DDNATV_HOST_AUDIT_ARCH 0x40000028U
#endif
static int (*get_real_sigaction(void))(int, const struct sigaction *, struct sigaction *);
// A program's own SIGSYS handler, for the traps of a seccomp filter it installed itself (Firefox's
// and Chromium's sandboxes emulate the calls they trap there). Android's app filter traps with data
// 0; such a sandbox's traps carry their own nonzero data, which the kernel gives as si_errno.
static struct sigaction g_app_sigsys;
static int g_app_sigsys_set = 0;
static void remember_app_sigsys(const struct sigaction *act, struct sigaction *oldact) {
if (oldact) {
if (g_app_sigsys_set) {
*oldact = g_app_sigsys;
} else {
memset(oldact, 0, sizeof(*oldact));
oldact->sa_handler = SIG_DFL;
}
}
if (act) {
g_app_sigsys = *act;
g_app_sigsys_set = act->sa_handler != SIG_DFL && act->sa_handler != SIG_IGN;
}
}
#if defined(__aarch64__)
// An id change the filter refused: root's succeed, an ordinary user's only to the ids it has (as in
// identity.c). setfsuid() and setfsgid() return the id they had.
static long sigsys_id_change(int nr, const unsigned long long *regs, long ret_val) {
if (!app_user_mode()) return ret_val;
uint32_t a = (uint32_t)regs[0], b = (uint32_t)regs[1], c = (uint32_t)regs[2];
switch (nr) {
case 146: return may_set_ids(0, a, UINT32_MAX, UINT32_MAX) ? 0 : -EPERM; // setuid
case 144: return may_set_ids(1, a, UINT32_MAX, UINT32_MAX) ? 0 : -EPERM; // setgid
case 145: return may_set_ids(0, a, b, UINT32_MAX) ? 0 : -EPERM; // setreuid
case 143: return may_set_ids(1, a, b, UINT32_MAX) ? 0 : -EPERM; // setregid
case 147: return may_set_ids(0, a, b, c) ? 0 : -EPERM; // setresuid
case 149: return may_set_ids(1, a, b, c) ? 0 : -EPERM; // setresgid
case 151: return (long)app_uid_now(); // setfsuid
case 152: return (long)app_gid_now(); // setfsgid
case 159: return -EPERM; // setgroups
default: return ret_val;
}
}
#endif
// Intercept SECCOMP blocked syscalls (SIGSYS), resolve sandbox/landlock gracefully, and advance PC
static void ddnatv_sigsys_handler(int sig, siginfo_t *info, void *ctx) {
if (!ctx || !info) return;
int host_trap = info->si_code == SYS_SECCOMP && info->si_errno == 0 && info->si_arch == DDNATV_HOST_AUDIT_ARCH;
if (!host_trap && g_app_sigsys_set) {
if (g_app_sigsys.sa_flags & SA_SIGINFO) g_app_sigsys.sa_sigaction(sig, info, ctx);
else g_app_sigsys.sa_handler(sig);
return;
}
if (!host_trap) {
int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction();
if (real_sig) {
struct sigaction sa_dfl;
memset(&sa_dfl, 0, sizeof(sa_dfl));
sa_dfl.sa_handler = SIG_DFL;
sigemptyset(&sa_dfl.sa_mask);
real_sig(sig, &sa_dfl, NULL);
}
raise(sig);
return;
}
ucontext_t *uctx = (ucontext_t *)ctx;
int sys_nr = info->si_syscall;
long ret_val = is_sigsys_synthetic_success_syscall(sys_nr) ? 0 : -ENOSYS;
#if defined(__aarch64__)
ret_val = sigsys_id_change(sys_nr, uctx->uc_mcontext.regs, ret_val);
#endif
#if defined(__aarch64__)
uctx->uc_mcontext.regs[0] = (uint64_t)ret_val;
if (uctx->uc_mcontext.pc >= 4) {
uint32_t insn_at_pc = 0;
uint32_t insn_before_pc = 0;
memcpy(&insn_at_pc, (const void *)uctx->uc_mcontext.pc, sizeof(insn_at_pc));
memcpy(&insn_before_pc, (const void *)(uctx->uc_mcontext.pc - 4), sizeof(insn_before_pc));
// On SECCOMP_RET_TRAP, the kernel already advanced PC past svc #0 (*(pc - 4) is svc #0).
// Only advance PC if PC itself points to svc #0 and *(pc - 4) does not.
int at_is_svc = ((insn_at_pc & 0xffe0001fU) == 0xd4000001U);
int prev_is_svc = ((insn_before_pc & 0xffe0001fU) == 0xd4000001U);
if (at_is_svc && !prev_is_svc) {
uctx->uc_mcontext.pc += 4;
}
}
#elif defined(__arm__)
uctx->uc_mcontext.arm_r0 = (unsigned long)ret_val;
if (uctx->uc_mcontext.arm_pc >= 4) {
if (uctx->uc_mcontext.arm_cpsr & 0x20) {
uint16_t insn_at_pc = 0;
uint16_t insn_before_pc = 0;
memcpy(&insn_at_pc, (const void *)uctx->uc_mcontext.arm_pc, sizeof(insn_at_pc));
memcpy(&insn_before_pc, (const void *)(uctx->uc_mcontext.arm_pc - 2), sizeof(insn_before_pc));
int at_is_svc = ((insn_at_pc & 0xff00U) == 0xdf00U);
int prev_is_svc = ((insn_before_pc & 0xff00U) == 0xdf00U);
if (at_is_svc && !prev_is_svc) {
uctx->uc_mcontext.arm_pc += 2;
}
} else {
uint32_t insn_at_pc = 0;
uint32_t insn_before_pc = 0;
memcpy(&insn_at_pc, (const void *)uctx->uc_mcontext.arm_pc, sizeof(insn_at_pc));
memcpy(&insn_before_pc, (const void *)(uctx->uc_mcontext.arm_pc - 4), sizeof(insn_before_pc));
int at_is_svc = ((insn_at_pc & 0x0f000000U) == 0x0f000000U);
int prev_is_svc = ((insn_before_pc & 0x0f000000U) == 0x0f000000U);
if (at_is_svc && !prev_is_svc) {
uctx->uc_mcontext.arm_pc += 4;
}
}
}
#elif defined(__x86_64__) && defined(REG_RAX)
uctx->uc_mcontext.gregs[REG_RAX] = (greg_t)ret_val;
if (uctx->uc_mcontext.gregs[REG_RIP] >= 2) {
unsigned char insn_at[2] = {0};
unsigned char insn_prev[2] = {0};
memcpy(insn_at, (const void *)uctx->uc_mcontext.gregs[REG_RIP], sizeof(insn_at));
memcpy(insn_prev, (const void *)(uctx->uc_mcontext.gregs[REG_RIP] - 2), sizeof(insn_prev));
int at_is_sys = (insn_at[0] == 0x0f && insn_at[1] == 0x05);
int prev_is_sys = (insn_prev[0] == 0x0f && insn_prev[1] == 0x05);
if (at_is_sys && !prev_is_sys) {
uctx->uc_mcontext.gregs[REG_RIP] += 2;
}
}
#elif defined(__i386__) && defined(REG_EAX)
uctx->uc_mcontext.gregs[REG_EAX] = (greg_t)ret_val;
if (uctx->uc_mcontext.gregs[REG_EIP] >= 2) {
unsigned char insn_at[2] = {0};
unsigned char insn_prev[2] = {0};
memcpy(insn_at, (const void *)uctx->uc_mcontext.gregs[REG_EIP], sizeof(insn_at));
memcpy(insn_prev, (const void *)(uctx->uc_mcontext.gregs[REG_EIP] - 2), sizeof(insn_prev));
int at_is_sys = (insn_at[0] == 0xcd && insn_at[1] == 0x80);
int prev_is_sys = (insn_prev[0] == 0xcd && insn_prev[1] == 0x80);
if (at_is_sys && !prev_is_sys) {
uctx->uc_mcontext.gregs[REG_EIP] += 2;
}
}
#endif
}
static int (*get_real_sigaction(void))(int, const struct sigaction *, struct sigaction *) {
static int (*real_sigaction)(int, const struct sigaction *, struct sigaction *) = NULL;
if (!real_sigaction) {
real_sigaction = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_NEXT, "sigaction");
if (!real_sigaction) {
real_sigaction = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_DEFAULT, "sigaction");
}
}
return real_sigaction;
}
__attribute__((constructor(101))) static void install_sigsys_handler(void) {
int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction();
if (!real_sig) return;
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = ddnatv_sigsys_handler;
sa.sa_flags = SA_SIGINFO | SA_RESTART;
sigemptyset(&sa.sa_mask);
real_sig(SIGSYS, &sa, NULL);
}
// SIGCHLD in a session with a parents file (handoff.c, DD_PARENTS): the program's action as it set it
// runs from ddnatv_sigchld_handler(), which then reaps the children no wait will come for
// (ddh_reap_unclaimed()). sigaction() shows the program its own action. A program that leaves SIGCHLD
// alone gets the handler only when DD_REAP_ADOPTED names it: with a handler, poll(), sleep() and the
// like return EINTR when a child exits, which a program that never had one may not expect.
static struct sigaction g_app_sigchld;
static int g_sigchld_managed;
static int g_sigchld_on_default;
static void ddh_reap_unclaimed(void);
static int ddh_listed(const char *list, const char *name, char *const argv[]);
static int ddnatv_apply_sigchld(int (*real_sig)(int, const struct sigaction *, struct sigaction *));
static void ddnatv_sigchld_handler(int sig, siginfo_t *info, void *ctx) {
struct sigaction app = g_app_sigchld;
if (app.sa_handler != SIG_DFL && app.sa_handler != SIG_IGN) {
if (app.sa_flags & SA_RESETHAND) {
// as the kernel would: the default from now on, with the handler only for a named program
g_app_sigchld.sa_handler = SIG_DFL;
g_app_sigchld.sa_flags &= ~(SA_SIGINFO | SA_RESETHAND);
int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction();
int saved = errno;
if (real_sig) ddnatv_apply_sigchld(real_sig);
errno = saved;
}
if (app.sa_flags & SA_SIGINFO) app.sa_sigaction(sig, info, ctx);
else app.sa_handler(sig);
}
ddh_reap_unclaimed();
}
// Puts g_app_sigchld into effect: as it is where the kernel reaps every child itself (SIG_IGN,
// SA_NOCLDWAIT) or the program wants no handler and is not named, else through the handler above.
static int ddnatv_apply_sigchld(int (*real_sig)(int, const struct sigaction *, struct sigaction *)) {
const struct sigaction *app = &g_app_sigchld;
int handler = app->sa_handler != SIG_DFL && app->sa_handler != SIG_IGN;
if (app->sa_handler == SIG_IGN || (app->sa_flags & SA_NOCLDWAIT) || (!handler && !g_sigchld_on_default))
return real_sig(SIGCHLD, app, NULL);
struct sigaction k;
memset(&k, 0, sizeof(k));
k.sa_sigaction = ddnatv_sigchld_handler;
k.sa_mask = app->sa_mask;
k.sa_flags = SA_SIGINFO | (handler ? (app->sa_flags & ~(SA_SIGINFO | SA_RESETHAND)) : (SA_RESTART | SA_NOCLDSTOP));
return real_sig(SIGCHLD, &k, NULL);
}
static int ddnatv_sigchld_action(int (*real_sig)(int, const struct sigaction *, struct sigaction *),
const struct sigaction *act, struct sigaction *oldact) {
struct sigaction prev = g_app_sigchld;
if (act) {
g_app_sigchld = *act;
if (ddnatv_apply_sigchld(real_sig) != 0) {
int err = errno;
g_app_sigchld = prev;
errno = err;
return -1;
}
}
if (oldact) *oldact = prev;
return 0;
}
// From the hook's start (handoff.c), once the parents file is open
static void ddnatv_manage_sigchld(void) {
if (!getenv("DD_PARENTS")) return;
int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction();
if (!real_sig || real_sig(SIGCHLD, NULL, &g_app_sigchld) != 0) return;
const char *named = getenv("DD_REAP_ADOPTED");
g_sigchld_on_default = named && ddh_listed(named, program_invocation_short_name, NULL);
if (ddnatv_apply_sigchld(real_sig) == 0) g_sigchld_managed = 1;
}
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) {
int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction();
if (!real_sig) return -1;
if (signum == SIGCHLD && g_sigchld_managed) return ddnatv_sigchld_action(real_sig, act, oldact);
if (signum == SIGSYS) {
if (act && act->sa_sigaction == ddnatv_sigsys_handler) {
return real_sig(signum, act, oldact);
}
remember_app_sigsys(act, oldact);
return 0;
}
return real_sig(signum, act, oldact);
}
int rt_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact, size_t sigsetsize) {
static int (*orig_rt_sigaction)(int, const struct sigaction *, struct sigaction *, size_t) = NULL;
if (!orig_rt_sigaction) orig_rt_sigaction = (int (*)(int, const struct sigaction *, struct sigaction *, size_t))dlsym(RTLD_NEXT, "rt_sigaction");
if (signum == SIGCHLD && g_sigchld_managed) return sigaction(signum, act, oldact);
if (signum == SIGSYS) {
if (act && act->sa_sigaction == ddnatv_sigsys_handler) {
return orig_rt_sigaction ? orig_rt_sigaction(signum, act, oldact, sigsetsize) : 0;
}
remember_app_sigsys(act, oldact);
return 0;
}
return orig_rt_sigaction ? orig_rt_sigaction(signum, act, oldact, sigsetsize) : -1;
}
int __libc_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) {
return sigaction(signum, act, oldact);
}
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler) {
static sighandler_t (*orig_signal)(int, sighandler_t) = NULL;
if (!orig_signal) orig_signal = (sighandler_t (*)(int, sighandler_t))dlsym(RTLD_NEXT, "signal");
if (signum == SIGCHLD && g_sigchld_managed) {
// glibc's signal(): BSD semantics
struct sigaction act, old;
memset(&act, 0, sizeof(act));
act.sa_handler = handler;
act.sa_flags = SA_RESTART;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, SIGCHLD);
return sigaction(SIGCHLD, &act, &old) == 0 ? old.sa_handler : SIG_ERR;
}
if (signum == SIGSYS) {
struct sigaction act, old;
memset(&act, 0, sizeof(act));
act.sa_handler = handler;
remember_app_sigsys(&act, &old);
return old.sa_handler;
}
return orig_signal ? orig_signal(signum, handler) : NULL;
}
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset) {
static int (*orig_sigprocmask)(int, const sigset_t *, sigset_t *) = NULL;
if (!orig_sigprocmask) orig_sigprocmask = (int (*)(int, const sigset_t *, sigset_t *))dlsym(RTLD_NEXT, "sigprocmask");
if (set && (how == SIG_BLOCK || how == SIG_SETMASK)) {
sigset_t mod_set = *set;
sigdelset(&mod_set, SIGSYS);
return orig_sigprocmask ? orig_sigprocmask(how, &mod_set, oldset) : 0;
}
return orig_sigprocmask ? orig_sigprocmask(how, set, oldset) : 0;
}
int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset) {
static int (*orig_pthread_sigmask)(int, const sigset_t *, sigset_t *) = NULL;
if (!orig_pthread_sigmask) orig_pthread_sigmask = (int (*)(int, const sigset_t *, sigset_t *))dlsym(RTLD_NEXT, "pthread_sigmask");
if (set && (how == SIG_BLOCK || how == SIG_SETMASK)) {
sigset_t mod_set = *set;
sigdelset(&mod_set, SIGSYS);
return orig_pthread_sigmask ? orig_pthread_sigmask(how, &mod_set, oldset) : 0;
}
return orig_pthread_sigmask ? orig_pthread_sigmask(how, set, oldset) : 0;
}
int rt_sigprocmask(int how, const sigset_t *set, sigset_t *oldset, size_t sigsetsize) {
static int (*orig_rt_sigprocmask)(int, const sigset_t *, sigset_t *, size_t) = NULL;
if (!orig_rt_sigprocmask) orig_rt_sigprocmask = (int (*)(int, const sigset_t *, sigset_t *, size_t))dlsym(RTLD_NEXT, "rt_sigprocmask");
if (set && (how == SIG_BLOCK || how == SIG_SETMASK)) {
sigset_t mod_set;
memcpy(&mod_set, set, sizeof(mod_set));
sigdelset(&mod_set, SIGSYS);
return orig_rt_sigprocmask ? orig_rt_sigprocmask(how, &mod_set, oldset, sigsetsize) : 0;
}
return orig_rt_sigprocmask ? orig_rt_sigprocmask(how, set, oldset, sigsetsize) : 0;
}
#endif /* !DDNATV_HOST_TEST */