Files
DroidDeck/tools/nativeguest/hook/kept.c
T
The412Banner 0ef9c7924a refactor(hook): kept descriptors in their own part, a handoff matched by another program
The descriptors close(), close_range() and closefrom() leave open move from
privileged.c to hook/kept.c (unchanged), so the bionic runtime's exec shim can
build the handoff with them. ddh_try_as() matches DD_HANDOFF against one
program and sends another: the bionic shim sends "/system/bin/linker64 PROGRAM
ARGS" and lists it by PROGRAM. ddh_try() is ddh_try_as() with both the same.
2026-11-30 17:28:27 -05:00

151 lines
7.3 KiB
C

// Part of libddnatv-hook.so, included by ddnatv-hook.c before privileged.c, and of the bionic
// runtime's exec shim (tools/bionic-runtime/handoff.c): the descriptors close(), close_range() and
// closefrom() leave open.
// The ARM translator of x86 devices and emulators (ndk_translation, houdini) keeps memory files of
// its own open in the process (ndk_translation at descriptor 3) and needs them whenever it
// translates more code: a program that closes every descriptor it did not open (sshd at start,
// closefrom(3)) made it abort later ("CHECK failed" in mmap_posix.cc). Its memory files open when
// the hook starts survive close(), close_range() and closefrom(); phones that run ARM code
// themselves have none.
#define DDNATV_MAX_KEPT_FDS 12
static int g_kept_fds[DDNATV_MAX_KEPT_FDS]; // ascending
static int g_kept_fd_count = 0;
// Of those, the ones also kept from close-on-exec: a worker process's binder fds (hook/handoff.c),
// which must outlive the program's own execs.
static int g_cloexec_kept_fds[8]; // ascending
static int g_cloexec_kept_count = 0;
// The list can change while the program runs (hook/handoff.c keeps and lets go of its own fds): odd
// while a change is under way, which kept_change_begin() and kept_change_end() bracket. Readers take
// a whole copy (kept_snapshot()), so that another thread's close() or close_range() never works
// from a list half moved, which could close a kept fd.
static unsigned g_kept_seq;
static pthread_mutex_t g_kept_lock = PTHREAD_MUTEX_INITIALIZER;
static __thread uint64_t t_kept_mask;
static int kept_snapshot(int *out) {
for (;;) {
unsigned seq = __atomic_load_n(&g_kept_seq, __ATOMIC_ACQUIRE);
if (seq & 1) continue;
int n = __atomic_load_n(&g_kept_fd_count, __ATOMIC_RELAXED);
if (n > DDNATV_MAX_KEPT_FDS) n = DDNATV_MAX_KEPT_FDS;
for (int i = 0; i < n; i++) out[i] = __atomic_load_n(&g_kept_fds[i], __ATOMIC_RELAXED);
__atomic_thread_fence(__ATOMIC_ACQUIRE);
if (__atomic_load_n(&g_kept_seq, __ATOMIC_RELAXED) == seq) return n;
}
}
static void kept_fork_prepare(void) { pthread_mutex_lock(&g_kept_lock); }
static void kept_fork_done(void) { pthread_mutex_unlock(&g_kept_lock); }
static void kept_register_fork(void) { pthread_atfork(kept_fork_prepare, kept_fork_done, kept_fork_done); }
// Every signal blocked meanwhile, so that no handler on this thread reads the list half changed (it
// would wait for the change forever) or changes it too; a fork() waits for the change to end, so a
// child never starts with one under way.
static void kept_change_begin(void) {
static pthread_once_t once = PTHREAD_ONCE_INIT;
pthread_once(&once, kept_register_fork);
uint64_t all = ~0ull;
call_orig_syscall(SYS_rt_sigprocmask, SIG_SETMASK, (unsigned long)&all, (unsigned long)&t_kept_mask, 8, 0, 0);
pthread_mutex_lock(&g_kept_lock);
__atomic_store_n(&g_kept_seq, g_kept_seq + 1, __ATOMIC_RELAXED);
__atomic_thread_fence(__ATOMIC_RELEASE);
}
static void kept_change_end(void) {
__atomic_store_n(&g_kept_seq, g_kept_seq + 1, __ATOMIC_RELEASE);
pthread_mutex_unlock(&g_kept_lock);
call_orig_syscall(SYS_rt_sigprocmask, SIG_SETMASK, (unsigned long)&t_kept_mask, 0, 8, 0, 0);
}
static int is_kept_fd(int fd) {
int kept[DDNATV_MAX_KEPT_FDS];
int n = kept_snapshot(kept);
for (int i = 0; i < n; i++) {
if (kept[i] == fd) return 1;
}
return 0;
}
// Closes under way (close(), close_range()), counted by the epoch they began in: one that looked at
// the list before a fd was added to it may still close that fd. A thread that makes and keeps a fd
// while others run (hook/handoff.c's table) moves the epoch on afterwards, waits for the closes of
// the one before (kept_settle()), and only then trusts the fd to be its own still.
static unsigned g_close_epoch;
// Per epoch, generation << 32 | count, one word: every change is one compare-and-swap, so a fork from
// a handler that interrupted a close's begin or end never splits one in two. A forked child moves the
// generation on and starts from none (kept_close_forked()): an end whose generation is gone counts
// nothing, and a begin whose swap failed takes the child's.
static uint64_t g_closers[2];
static void kept_close_end(uint64_t ticket);
// The close's ticket: generation << 1 | epoch
static uint64_t kept_close_begin(void) {
for (;;) {
unsigned epoch = __atomic_load_n(&g_close_epoch, __ATOMIC_SEQ_CST) & 1;
uint64_t word = __atomic_load_n(&g_closers[epoch], __ATOMIC_SEQ_CST);
if (!__atomic_compare_exchange_n(&g_closers[epoch], &word, word + 1, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
continue;
uint64_t ticket = (word >> 32) << 1 | epoch;
if ((__atomic_load_n(&g_close_epoch, __ATOMIC_SEQ_CST) & 1) == epoch) return ticket;
kept_close_end(ticket); // the epoch moved on meanwhile: counted in the new one instead
}
}
static void kept_close_end(uint64_t ticket) {
unsigned epoch = (unsigned)(ticket & 1);
uint64_t word = __atomic_load_n(&g_closers[epoch], __ATOMIC_SEQ_CST);
while ((word >> 32) == (ticket >> 1) && (word & 0xffffffffu) != 0 &&
!__atomic_compare_exchange_n(&g_closers[epoch], &word, word - 1, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
}
}
// In a forked child: the closes the parent's threads were making are not the child's to wait for.
// (The forking thread's own, a close a handler interrupted to fork, then goes uncounted.)
static void kept_close_forked(void) {
for (int epoch = 0; epoch < 2; epoch++) {
uint64_t word = __atomic_load_n(&g_closers[epoch], __ATOMIC_SEQ_CST);
__atomic_store_n(&g_closers[epoch], ((word >> 32) + 1) << 32, __ATOMIC_SEQ_CST);
}
}
static int kept_drained(unsigned epoch) {
for (int i = 0; i < 1000; i++) {
if ((__atomic_load_n(&g_closers[epoch], __ATOMIC_SEQ_CST) & 0xffffffffu) == 0) return 1;
struct timespec pause = {0, 1000 * 1000};
nanosleep(&pause, NULL);
}
return 0;
}
// 1 once every close that began before now has ended; 0 after a second without (one interrupted by
// a signal whose handler is the caller, say): then nothing it made since can be trusted. The epoch
// moves on only once the closes of the one before it are over too, so the two counts never mix one
// that is still under way from an earlier settle that gave up with the current one.
static int kept_settle(void) {
unsigned current = __atomic_load_n(&g_close_epoch, __ATOMIC_SEQ_CST) & 1;
if (!kept_drained(current ^ 1)) return 0;
unsigned old = __atomic_fetch_add(&g_close_epoch, 1, __ATOMIC_SEQ_CST) & 1;
return kept_drained(old);
}
// Calls fn for each part of [first, last] that leaves out the descriptors in kept (ascending);
// the first nonzero answer of fn is returned, else 0.
static int for_each_range_without(unsigned int first, unsigned int last, const int *kept, int nkept,
int (*fn)(unsigned int, unsigned int, void *), void *arg) {
unsigned int from = first;
for (int i = 0; i < nkept && from <= last; i++) {
if (kept[i] < 0 || (unsigned int)kept[i] < from || (unsigned int)kept[i] > last) continue;
unsigned int k = (unsigned int)kept[i];
if (k > from) {
int r = fn(from, k - 1, arg);
if (r != 0) return r;
}
if (k == last) return 0;
from = k + 1;
}
return from <= last ? fn(from, last, arg) : 0;
}