fix(nativeguest): bionic shim builds without the signal layer, reap past 64, one-shot SIGCHLD handlers reset the kernel's action

This commit is contained in:
Kurt Himebauch committed 2026-12-02 20:20:03 -05:00
1 parent 8902443848
commit 51f69c68ba
4 files changed
+22 -5

No files matched your search

+7
View File
@@ -87,6 +87,11 @@ static const char *rewrite_path(const char *path, char *out, size_t size) {
return path;
}
// The native hook's SIGCHLD handling (hook/signals.c) is not part of this shim: its programs record their
// first parent for the session's subreapers, and reap nothing themselves.
static void ddnatv_manage_sigchld(void) {}
static void ddh_reap_unclaimed(void) __attribute__((unused));
#include "../nativeguest/hook/kept.c"
#include "../nativeguest/hook/handoff.c"
@@ -315,12 +320,14 @@ pid_t fork(void) {
}
ddh_prepare();
pid_t forker = (pid_t)syscall(SYS_gettid);
pid_t forking = (pid_t)syscall(SYS_getpid);
pid_t pid = orig_fork();
if (pid == 0) {
kept_forked();
kept_close_forked();
ddh_drop_worker_binder();
ddh_forked(forker);
ddh_record_parent((pid_t)syscall(SYS_getpid), forking);
} else if (pid > 0) {
ddh_child_created(pid, 1);
}
+5 -3
View File
@@ -1793,14 +1793,16 @@ static void ddh_reap_unclaimed(void) {
if (!__atomic_load_n(&g_ddh_parents, __ATOMIC_ACQUIRE) && !__atomic_load_n(&g_ddh, __ATOMIC_ACQUIRE)) return;
int saved = errno;
pid_t me = (pid_t)call_orig_syscall(SYS_getpid, 0, 0, 0, 0, 0, 0);
int scan = 0;
// past 64 reaped this way, the rest from /proc too
int scan = 1;
for (int i = 0; i < 64; i++) {
siginfo_t si;
memset(&si, 0, sizeof(si));
if (call_orig_syscall(SYS_waitid, P_ALL, 0, (unsigned long)&si, WEXITED | WNOHANG | WNOWAIT, 0, 0) != 0 || si.si_pid <= 0)
if (call_orig_syscall(SYS_waitid, P_ALL, 0, (unsigned long)&si, WEXITED | WNOHANG | WNOWAIT, 0, 0) != 0 || si.si_pid <= 0) {
scan = 0;
break;
}
if (!ddh_reap_unwaited(me, si.si_pid)) {
scan = 1;
break;
}
}
+6
View File
@@ -185,13 +185,19 @@ 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);
+4 -2
View File
@@ -2392,12 +2392,14 @@ class NativeHookHandoffTest(unittest.TestCase):
env["DD_PARENTS"] = "/run/parents-%s" % self.id().rsplit(".", 1)[-1]
if named:
env["DD_REAP_ADOPTED"] = "other:orphans"
# not a session's own settings, when run inside one
inherited = {k: v for k, v in os.environ.items() if k not in ("DD_PARENTS", "DD_REAP_ADOPTED")}
if self.prefix:
cmd = list(self.prefix) + [arg for key, value in env.items() for arg in ("-E", f"{key}={value}")]
run_env = dict(os.environ)
run_env = inherited
else:
cmd = []
run_env = dict(os.environ, **env)
run_env = dict(inherited, **env)
out = subprocess.run(cmd + [str(probe), mode, *args], env=run_env, capture_output=True, text=True, timeout=60)
result = dict(line.split("\t", 1) for line in out.stdout.splitlines() if "\t" in line)
if mode == "adopt" and result.get("subreaper") != "1":