fix(handoff): spawn blocks signals and cancellation across the fork; root flag stored under the stop lock

This commit is contained in:
Kurt Himebauch committed 2026-12-01 10:27:44 -05:00
1 parent 9f5c35f088
commit feedf92d3e
2 files changed
+42 -2

No files matched your search

@@ -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)
+35 -1
View File
@@ -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;
}