From feedf92d3e55f61925ee8eb53ab0afbd219c410c Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:27:44 -0500 Subject: [PATCH] fix(handoff): spawn blocks signals and cancellation across the fork; root flag stored under the stop lock --- .../launcher/session/SessionService.kt | 8 ++++- tools/nativeguest/hook/exec.c | 36 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index 88fc72dbc4..d2abc28469 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -902,7 +902,13 @@ class SessionService : Service() { return } sessionPid = pid - com.droiddeck.launcher.runtime.GuestWorkers.sessionRootUncounted = com.droiddeck.launcher.runtime.GuestWorkers.runsInWorker(pid) + // Under the lock Stop clears it with, and only while this session still runs: a Stop since the + // check above has cleared it already, and this would set it again for no running session. + val uncounted = com.droiddeck.launcher.runtime.GuestWorkers.runsInWorker(pid) + synchronized(stopLock) { + if (gen == sessionGen && SessionState.running) + com.droiddeck.launcher.runtime.GuestWorkers.sessionRootUncounted = uncounted + } if (pid > 1) { raiseTracer(pid, gen) SessionEvents.guestStarted(pid) diff --git a/tools/nativeguest/hook/exec.c b/tools/nativeguest/hook/exec.c index b23781b3de..77eb93f9fa 100644 --- a/tools/nativeguest/hook/exec.c +++ b/tools/nativeguest/hook/exec.c @@ -1210,15 +1210,32 @@ static int ddh_spawn_forked(pid_t *pid, const char *path, const posix_spawn_file if (attr && posix_spawnattr_getflags(attr, &spawn_flags) == 0 && (spawn_flags & (POSIX_SPAWN_SETSCHEDULER | POSIX_SPAWN_SETSCHEDPARAM))) return -1; + // Not cancelled while the child is being started (its report is read, and its failure reaped): + // the pid would be lost with the child still running, and the report's fd left open. + int cancel = PTHREAD_CANCEL_ENABLE; + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel); int report[2]; - if (pipe2(report, O_CLOEXEC) != 0) return -1; + if (pipe2(report, O_CLOEXEC) != 0) { + pthread_setcancelstate(cancel, NULL); + return -1; + } + // Every signal blocked from the fork to the exec, as glibc's posix_spawn() does: a signal to the + // group during a file action (an open of a FIFO that blocks) would otherwise run the caller's + // handler, or end the child, before the program is started. The child gets back the caller's mask, + // or the one the attributes ask for, just before its exec. + sigset_t all, caller; + sigfillset(&all); + pthread_sigmask(SIG_SETMASK, &all, &caller); pid_t child = fork(); if (child < 0) { int err = errno; + pthread_sigmask(SIG_SETMASK, &caller, NULL); close(report[0]); close(report[1]); + pthread_setcancelstate(cancel, NULL); return err; } + if (child > 0) pthread_sigmask(SIG_SETMASK, &caller, NULL); if (child == 0) { call_orig_syscall(SYS_close, (unsigned long)report[0], 0, 0, 0, 0, 0); // Out of the file actions' way: high, and one of the fds they leave open (closefrom(3) is common) @@ -1231,6 +1248,21 @@ static int ddh_spawn_forked(pid_t *pid, const char *path, const posix_spawn_file } int err = ddh_spawn_child_setup(fa, attr); if (!err) { + // A signal still pending when the mask is restored must not run a handler copied from the + // caller: caught signals go back to their default, ignored ones stay ignored (exec keeps + // them), and SIGSYS keeps the hook's handler. + for (int sig = 1; sig < _NSIG; sig++) { + struct sigaction old; + if (sig == SIGSYS || sig == SIGKILL || sig == SIGSTOP) continue; + if (sigaction(sig, NULL, &old) != 0) continue; + if (old.sa_handler == SIG_IGN || old.sa_handler == SIG_DFL) continue; + struct sigaction def = {0}; + def.sa_handler = SIG_DFL; + sigaction(sig, &def, NULL); + } + short flags = 0; + if (attr) posix_spawnattr_getflags(attr, &flags); + if (!(flags & POSIX_SPAWN_SETSIGMASK)) sigprocmask(SIG_SETMASK, &caller, NULL); execve(path, argv, envp); err = errno; } @@ -1246,9 +1278,11 @@ static int ddh_spawn_forked(pid_t *pid, const char *path, const posix_spawn_file if (n == (ssize_t)sizeof(err)) { while (waitpid(child, NULL, 0) < 0 && errno == EINTR) { } + pthread_setcancelstate(cancel, NULL); return err; } if (pid) *pid = child; + pthread_setcancelstate(cancel, NULL); return 0; }