diff --git a/tools/nativeguest/hook/handoff.c b/tools/nativeguest/hook/handoff.c index 4b13a1f7e1..dfa53c2503 100644 --- a/tools/nativeguest/hook/handoff.c +++ b/tools/nativeguest/hook/handoff.c @@ -316,7 +316,7 @@ static void ddh_keep(int fd) { // then fails, the move is undone (ddh_parent_pidfd_settle()). Returns the number moved from, or -1. static int ddh_parent_pidfd_moves(int target); static void ddh_parent_pidfd_settle(int from, int done); -static int ddh_parent_pidfd_lock(void); +static int ddh_parent_pidfd_lock(int target); static void ddh_parent_pidfd_unlock(int locked); static void ddh_drop_worker_binder(void) { @@ -376,34 +376,49 @@ static int ddh_parent_pidfd_moves(int target) { static void ddh_parent_pidfd_settle(int from, int done) { if (from < 0 || done) return; - // the dup failed and left the number as it was: the pidfd is still there. The copy's slot in - // the kept list goes first, so that a full list has room for the original again. + // The dup failed and left the number as it was. If it still is the pidfd (no other thread's + // dup2() has put a file there since the move), the original is tracked again and the copy goes, + // its kept slot first so that a full list has room; otherwise the copy stays the pidfd. + char path[32], target[32]; + snprintf(path, sizeof(path), "/proc/self/fd/%d", from); + int saved = errno; + long len = syscall(SYS_readlinkat, AT_FDCWD, path, target, sizeof(target) - 1); + errno = saved; + if (len <= 0) return; + target[len] = 0; + if (strcmp(target, "anon_inode:[pidfd]") != 0) return; int moved = __atomic_exchange_n(&g_ddh_parent_pidfd, from, __ATOMIC_ACQ_REL); if (moved >= 0 && moved != from) ddh_unkeep(moved); ddh_keep(from); if (moved >= 0 && moved != from) { - int saved = errno; + saved = errno; syscall(SYS_close, moved); errno = saved; } } -// Held from the move through the dup to the settling, in a handed-off program (the one process with -// the pidfd): another thread's dup2() onto the number in between would otherwise be taken for the -// pidfd when a failed dup is undone. Released in a forked child. +// Held from the move through the dup to the settling when a dup2()/dup3() targets the pidfd's +// number (no other call takes it), with every signal blocked: a handler that calls dup2() itself +// cannot run while it is held. ddh_parent_lives() only tries it. Released in a forked child. static pthread_mutex_t g_ddh_pidfd_lock = PTHREAD_MUTEX_INITIALIZER; +static __thread uint64_t t_ddh_pidfd_mask; static void ddh_pidfd_hold(void) { pthread_mutex_lock(&g_ddh_pidfd_lock); } static void ddh_pidfd_release(void) { pthread_mutex_unlock(&g_ddh_pidfd_lock); } -static int ddh_parent_pidfd_lock(void) { - if (__atomic_load_n(&g_ddh_parent_pidfd, __ATOMIC_ACQUIRE) < 0) return 0; +static int ddh_parent_pidfd_lock(int target) { + int fd = __atomic_load_n(&g_ddh_parent_pidfd, __ATOMIC_ACQUIRE); + if (fd < 0 || target != fd) return 0; + uint64_t all = ~0ull; + call_orig_syscall(SYS_rt_sigprocmask, SIG_SETMASK, (unsigned long)&all, (unsigned long)&t_ddh_pidfd_mask, 8, 0, 0); ddh_pidfd_hold(); return 1; } static void ddh_parent_pidfd_unlock(int locked) { - if (locked) ddh_pidfd_release(); + if (!locked) return; + ddh_pidfd_release(); + call_orig_syscall(SYS_rt_sigprocmask, SIG_SETMASK, (unsigned long)&t_ddh_pidfd_mask, 0, 8, 0, 0); } // The line of /proc/PID/cgroup that names the app's per-process cgroup ("...:/uid_U/pid_P"). @@ -1846,7 +1861,10 @@ static pid_t ddh_logical_pgrp(void) { return ddh_logical_group(getpgrp()); } static int ddh_parent_lives(pid_t parent, long child_start) { // The dup2()/dup3() wrappers move the pidfd only under this lock, so no move falls between the // look and the poll; when another thread holds it, /proc answers instead. - int locked = __atomic_load_n(&g_ddh_parent_pidfd, __ATOMIC_ACQUIRE) >= 0 && pthread_mutex_trylock(&g_ddh_pidfd_lock) == 0; + uint64_t all = ~0ull, mask = 0; + int blocked = __atomic_load_n(&g_ddh_parent_pidfd, __ATOMIC_ACQUIRE) >= 0 && + call_orig_syscall(SYS_rt_sigprocmask, SIG_SETMASK, (unsigned long)&all, (unsigned long)&mask, 8, 0, 0) == 0; + int locked = blocked && pthread_mutex_trylock(&g_ddh_pidfd_lock) == 0; int fd = locked ? __atomic_load_n(&g_ddh_parent_pidfd, __ATOMIC_ACQUIRE) : -1; if (parent == g_ddh_report_parent && fd >= 0) { // No new fd, and exited counts whether reaped or not. Only while the number still is that @@ -1865,9 +1883,11 @@ static int ddh_parent_lives(pid_t parent, long child_start) { int clear = n >= 0 && len2 > 0 && strcmp(before, "anon_inode:[pidfd]") == 0 && strcmp(after, before) == 0 && __atomic_load_n(&g_ddh_parent_pidfd, __ATOMIC_ACQUIRE) == fd && !(p.revents & (POLLNVAL | POLLERR)); pthread_mutex_unlock(&g_ddh_pidfd_lock); + call_orig_syscall(SYS_rt_sigprocmask, SIG_SETMASK, (unsigned long)&mask, 0, 8, 0, 0); if (clear) return !(p.revents & POLLIN); - } else if (locked) { - pthread_mutex_unlock(&g_ddh_pidfd_lock); + } else { + if (locked) pthread_mutex_unlock(&g_ddh_pidfd_lock); + if (blocked) call_orig_syscall(SYS_rt_sigprocmask, SIG_SETMASK, (unsigned long)&mask, 0, 8, 0, 0); } char path[48], stat[512]; snprintf(path, sizeof(path), "/proc/%d/stat", (int)parent); diff --git a/tools/nativeguest/hook/net.c b/tools/nativeguest/hook/net.c index 9c29fe2db9..44c9a789cd 100644 --- a/tools/nativeguest/hook/net.c +++ b/tools/nativeguest/hook/net.c @@ -251,7 +251,7 @@ int close(int fd) { int dup2(int oldfd, int newfd) { static int (*orig_dup2)(int, int) = NULL; if (!orig_dup2) orig_dup2 = (int (*)(int, int))dlsym(RTLD_NEXT, "dup2"); - int pidfd_from = -1, locked = ddh_parent_pidfd_lock(); + int pidfd_from = -1, locked = ddh_parent_pidfd_lock(oldfd != newfd ? newfd : -1); if (oldfd != newfd) { pidfd_from = ddh_parent_pidfd_moves(newfd); clear_dns_redirect(newfd); @@ -269,7 +269,7 @@ int dup2(int oldfd, int newfd) { int dup3(int oldfd, int newfd, int flags) { static int (*orig_dup3)(int, int, int) = NULL; if (!orig_dup3) orig_dup3 = (int (*)(int, int, int))dlsym(RTLD_NEXT, "dup3"); - int pidfd_from = -1, locked = ddh_parent_pidfd_lock(); + int pidfd_from = -1, locked = ddh_parent_pidfd_lock(oldfd != newfd ? newfd : -1); if (oldfd != newfd) { pidfd_from = ddh_parent_pidfd_moves(newfd); clear_dns_redirect(newfd);