Files
DroidDeck/tools/nativeguest/hook/signals.c
T
Kurt Himebauch 2848484a99 feat: fuck proot and proots mama (#2300)
* feat: experimental native userland that runs the Linux side without proot

* fix(native): keep the visible name of link2symlink binaries

* fix(ci): run the native hook build through bash

* fix(native): address review findings in the hook and the ctl fallback

* fix(ctl): use the broadcast fallback only when content is missing

* fix(native): survive Android's seccomp filter in ld.so, libc and posix_spawn children

* docs(native): how to test under Android's seccomp filter

* fix(session): hide an unlistable /sys/bus/pci/devices so libpci does not abort Chromium's GPU init

* fix(native): nested syscall() redirects no longer loop with libblsession's openat2

* fix(native): controllers reach Steam and the install-script evaluator no longer blocks game moves

* fix(native): write the hook's NUL character literals as escapes

* fix(native): proot fast path turns off and greys out while running without proot

* fix(native): D-Bus logins work and Xwayland still admits root once peers read as root

* fix(native): scripts on session mounts outside the rootfs run with the rootfs shell

* fix(native): games on the SD card library start in their own folder

* fix(native): Wine drive links proot made point at their real places

* fix(native): at-calls, uid lookups and relative programs follow the session's mounts

* fix(native): close-from in spawned programs covers what is open at the spawn

* fix(native): service names resolve and the DNS fallback answers what was asked

* build(native): the hook is built with its Go support and tracked by the preload recipe

* fix(native): drive links into identity mounts stay, and relink and seccomp patch failures are logged

* fix(native): seccomp-sim's filter holds every allowed call

* fix(ctl): the broadcast fallback only stands in for a missing content command

* fix(native): pre-2.33 glibc stat calls present owners as root too

* fix(native): res_init and res_ninit call glibc, so Steam's store resolves names

* fix(ci): a preload lock older than a new output rebuilds that group, and generated headers are not hashed

* fix(native): a second seccomp patch run leaves other system calls alone

* fix(native): the terminal and bottom desktop keep proot's environment in a native session

* fix(native): any number of session mounts, and openat2 relative to a mounted folder

* fix(agent): a native command's stdin is written beside its timeout

* fix(ctl): native rejects --env without = and timeouts outside 0-600 s

* fix(native): the resolver fallback has the network's IPv6 servers, and lookups keep the fallback's validation errors

* fix(native): a spawn whose dup2 source close-from closed fails with EBADF

* fix(native): __getcwd_chk checks its buffer, and seccomp-sim -a takes only call numbers

* feat(native): guests see the session hostname from uname and gethostname

* feat(native): the terminal and bottom desktop run without proot

* fix(components): an origin APK without a newer preload output still proves the rest

* fix(native): seccomp-sim -a rejects empty fields

* fix(native): gethostbyname no longer answers failed lookups with Ubuntu and Debian addresses

* feat(native): store installers, root helpers, Comet and Xodus run without proot

* fix(native): the hook finds libraries, preloads and itself through a sandbox's mounts, and ldconfig -C makes its cache

* feat(native): Flatpak sandboxes run without proot, and apply_extra runs without a runtime

* fix(native): remove, glob and nftw take guest paths, and links in a mount are followed as the guest sees them

* fix(native): Flatpak sandboxes get the session's mounts, their libraries' RUNPATHs, and no rootfs-only settings

* fix(native): gethostbyname asks the network's DNS servers, nftw of / gives single slashes, and the library path fits mapped directories

* fix(native): a native process starts in its working directory's real place

* fix(native): the library path given to ld.so holds whole directories only

* fix(native): Wine drive links keep their guest paths, so proot finds the library after a native session

* fix(native): droiddeckctl native prepares the rootfs with the setting off

* fix(native): a SysV segment stays while any process has it attached

* fix(native): DNS lookups ask the network's own servers before public ones

* fix(native): a program's own seccomp traps reach its own SIGSYS handler

* feat(native): run native sessions under a device's own app seccomp filter for testing

* fix(native): SysV attaches and collection take a lock, a failed mark fails the attach, and collection closes its directory

* fix(native): shmat fails when it cannot take the segment lock

* fix(native): the SysV segment lock is retried when a signal interrupts it

* fix(native): process tools see the session's processes as root's

* fix(native): native processes are named after their program, not ld.so

* fix(native): a session's helpers keep the mode the session started in

* fix(native): a keyed SysV segment stays until IPC_RMID

* fix(native): a rootfs link to an absolute guest path is followed in the guest, so Wine finds its z: drive

* fix(native): a sandbox whose loader can't be patched runs under proot

* fix(native): creating a private SysV segment also collects stale ones

* fix(native): a session stopped during startup forgets its mode

* fix(xbox): a native backend is authorized by its process ancestry

* fix(native): Wine's drive links are followed in the guest from any prefix

* fix(native): only the startup that owns the running session publishes its mode

* fix(native): a SysV segment stays until IPC_RMID even after its maker ends

* fix(native): a sandbox mount nested in another mount gets its own place

* fix(native): a sandbox whose mount places can't be made runs under proot
2026-11-11 09:13:17 -05:00

234 lines
10 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
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;
}
}
// 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;
if (info->si_code != SYS_SECCOMP) {
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;
}
if (info->si_errno != 0 && 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;
}
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__)
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);
}
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 == 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 == 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 == 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 */