Files
DroidDeck/tools/nativeguest/hook/posix_shm.c
T
Kurt Himebauch 2848484a99 feat: fuck proot and proots mama (#2300)
* feat: experimental native userland that runs the Linux side without proot

* fix(native): keep the visible name of link2symlink binaries

* fix(ci): run the native hook build through bash

* fix(native): address review findings in the hook and the ctl fallback

* fix(ctl): use the broadcast fallback only when content is missing

* fix(native): survive Android's seccomp filter in ld.so, libc and posix_spawn children

* docs(native): how to test under Android's seccomp filter

* fix(session): hide an unlistable /sys/bus/pci/devices so libpci does not abort Chromium's GPU init

* fix(native): nested syscall() redirects no longer loop with libblsession's openat2

* fix(native): controllers reach Steam and the install-script evaluator no longer blocks game moves

* fix(native): write the hook's NUL character literals as escapes

* fix(native): proot fast path turns off and greys out while running without proot

* fix(native): D-Bus logins work and Xwayland still admits root once peers read as root

* fix(native): scripts on session mounts outside the rootfs run with the rootfs shell

* fix(native): games on the SD card library start in their own folder

* fix(native): Wine drive links proot made point at their real places

* fix(native): at-calls, uid lookups and relative programs follow the session's mounts

* fix(native): close-from in spawned programs covers what is open at the spawn

* fix(native): service names resolve and the DNS fallback answers what was asked

* build(native): the hook is built with its Go support and tracked by the preload recipe

* fix(native): drive links into identity mounts stay, and relink and seccomp patch failures are logged

* fix(native): seccomp-sim's filter holds every allowed call

* fix(ctl): the broadcast fallback only stands in for a missing content command

* fix(native): pre-2.33 glibc stat calls present owners as root too

* fix(native): res_init and res_ninit call glibc, so Steam's store resolves names

* fix(ci): a preload lock older than a new output rebuilds that group, and generated headers are not hashed

* fix(native): a second seccomp patch run leaves other system calls alone

* fix(native): the terminal and bottom desktop keep proot's environment in a native session

* fix(native): any number of session mounts, and openat2 relative to a mounted folder

* fix(agent): a native command's stdin is written beside its timeout

* fix(ctl): native rejects --env without = and timeouts outside 0-600 s

* fix(native): the resolver fallback has the network's IPv6 servers, and lookups keep the fallback's validation errors

* fix(native): a spawn whose dup2 source close-from closed fails with EBADF

* fix(native): __getcwd_chk checks its buffer, and seccomp-sim -a takes only call numbers

* feat(native): guests see the session hostname from uname and gethostname

* feat(native): the terminal and bottom desktop run without proot

* fix(components): an origin APK without a newer preload output still proves the rest

* fix(native): seccomp-sim -a rejects empty fields

* fix(native): gethostbyname no longer answers failed lookups with Ubuntu and Debian addresses

* feat(native): store installers, root helpers, Comet and Xodus run without proot

* fix(native): the hook finds libraries, preloads and itself through a sandbox's mounts, and ldconfig -C makes its cache

* feat(native): Flatpak sandboxes run without proot, and apply_extra runs without a runtime

* fix(native): remove, glob and nftw take guest paths, and links in a mount are followed as the guest sees them

* fix(native): Flatpak sandboxes get the session's mounts, their libraries' RUNPATHs, and no rootfs-only settings

* fix(native): gethostbyname asks the network's DNS servers, nftw of / gives single slashes, and the library path fits mapped directories

* fix(native): a native process starts in its working directory's real place

* fix(native): the library path given to ld.so holds whole directories only

* fix(native): Wine drive links keep their guest paths, so proot finds the library after a native session

* fix(native): droiddeckctl native prepares the rootfs with the setting off

* fix(native): a SysV segment stays while any process has it attached

* fix(native): DNS lookups ask the network's own servers before public ones

* fix(native): a program's own seccomp traps reach its own SIGSYS handler

* feat(native): run native sessions under a device's own app seccomp filter for testing

* fix(native): SysV attaches and collection take a lock, a failed mark fails the attach, and collection closes its directory

* fix(native): shmat fails when it cannot take the segment lock

* fix(native): the SysV segment lock is retried when a signal interrupts it

* fix(native): process tools see the session's processes as root's

* fix(native): native processes are named after their program, not ld.so

* fix(native): a session's helpers keep the mode the session started in

* fix(native): a keyed SysV segment stays until IPC_RMID

* fix(native): a rootfs link to an absolute guest path is followed in the guest, so Wine finds its z: drive

* fix(native): a sandbox whose loader can't be patched runs under proot

* fix(native): creating a private SysV segment also collects stale ones

* fix(native): a session stopped during startup forgets its mode

* fix(xbox): a native backend is authorized by its process ancestry

* fix(native): Wine's drive links are followed in the guest from any prefix

* fix(native): only the startup that owns the running session publishes its mode

* fix(native): a SysV segment stays until IPC_RMID even after its maker ends

* fix(native): a sandbox mount nested in another mount gets its own place

* fix(native): a sandbox whose mount places can't be made runs under proot
2026-11-11 09:13:17 -05:00

175 lines
7.3 KiB
C

// Part of libddnatv-hook.so, included by ddnatv-hook.c.
//
// shm_open() and shm_unlink() of glibc open /dev/shm/NAME with a system call of their own, which
// the path hooks never see and Android has no /dev/shm for. They go through open() and unlink()
// here, which map /dev/shm.
#ifndef DDNATV_HOST_TEST
static int posix_shm_path(const char *name, char *path, size_t size) {
if (!name) { errno = EINVAL; return -1; }
while (*name == '/') name++;
size_t len = strlen(name);
if (len == 0 || len > NAME_MAX || strchr(name, '/') || strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
errno = len > NAME_MAX ? ENAMETOOLONG : EINVAL;
return -1;
}
if (snprintf(path, size, "/dev/shm/%s", name) >= (int)size) { errno = ENAMETOOLONG; return -1; }
return 0;
}
int shm_open(const char *name, int oflag, mode_t mode) {
char path[PATH_MAX];
if (posix_shm_path(name, path, sizeof(path)) < 0) return -1;
return open(path, (oflag & (O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) | O_NOFOLLOW | O_CLOEXEC, mode);
}
int shm_unlink(const char *name) {
char path[PATH_MAX];
if (posix_shm_path(name, path, sizeof(path)) < 0) return -1;
return unlink(path);
}
#endif /* !DDNATV_HOST_TEST */
#ifndef DDNATV_HOST_TEST
// glibc's posix_spawn child resets every signal handler to its default, the hook's SIGSYS handler
// included, before it runs the file actions, and "close everything from N" is close_range(), which
// Android's seccomp filter answers with SIGSYS: the child died of it (Python's subprocess with
// close_fds does this). It becomes one close per descriptor, added when the program is spawned so
// that descriptors opened in between and the ones earlier actions make are closed too; the targets
// of actions added after it stay open, as they would. A close of a descriptor that is not open is
// no error to glibc.
#define CLOSEFROM_SLOTS 64
#define CLOSEFROM_KEEP 32
static struct closefrom_entry {
posix_spawn_file_actions_t *actions;
int from; // -1: no close-from yet, only targets of actions seen
int highest; // the highest descriptor an action before close-from makes
int nkeep;
int keep[CLOSEFROM_KEEP];
int closed_to; // the descriptors up to this one are closed by actions already added
int bad_source; // a dup2 after close-from duplicates a descriptor it closed: glibc's EBADF
} g_closefrom[CLOSEFROM_SLOTS];
static pthread_mutex_t g_closefrom_lock = PTHREAD_MUTEX_INITIALIZER;
static struct closefrom_entry *closefrom_entry(posix_spawn_file_actions_t *actions, int create) {
struct closefrom_entry *free_slot = NULL;
for (int i = 0; i < CLOSEFROM_SLOTS; i++) {
if (g_closefrom[i].actions == actions) return &g_closefrom[i];
if (!g_closefrom[i].actions && !free_slot) free_slot = &g_closefrom[i];
}
if (!create || !free_slot) return NULL;
memset(free_slot, 0, sizeof(*free_slot));
free_slot->actions = actions;
free_slot->from = -1;
free_slot->highest = -1;
free_slot->closed_to = -1;
return free_slot;
}
static void closefrom_note_target(posix_spawn_file_actions_t *actions, int fd) {
pthread_mutex_lock(&g_closefrom_lock);
struct closefrom_entry *e = closefrom_entry(actions, 1);
if (e) {
if (e->from < 0) {
if (fd > e->highest) e->highest = fd;
} else if (e->nkeep < CLOSEFROM_KEEP) {
e->keep[e->nkeep++] = fd;
}
}
pthread_mutex_unlock(&g_closefrom_lock);
}
int posix_spawn_file_actions_addclosefrom_np(posix_spawn_file_actions_t *actions, int from) {
if (!actions || from < 0) return EINVAL;
pthread_mutex_lock(&g_closefrom_lock);
struct closefrom_entry *e = closefrom_entry(actions, 1);
if (e && (e->from < 0 || from < e->from)) e->from = from;
pthread_mutex_unlock(&g_closefrom_lock);
return e ? 0 : ENOMEM;
}
int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *actions, int fd, const char *path, int oflag, mode_t mode) {
static int (*orig)(posix_spawn_file_actions_t *, int, const char *, int, mode_t) = NULL;
if (!orig) orig = (int (*)(posix_spawn_file_actions_t *, int, const char *, int, mode_t))dlsym(RTLD_NEXT, "posix_spawn_file_actions_addopen");
int ret = orig ? orig(actions, fd, path, oflag, mode) : ENOSYS;
if (ret == 0) closefrom_note_target(actions, fd);
return ret;
}
int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *actions, int fd, int newfd) {
static int (*orig)(posix_spawn_file_actions_t *, int, int) = NULL;
if (!orig) orig = (int (*)(posix_spawn_file_actions_t *, int, int))dlsym(RTLD_NEXT, "posix_spawn_file_actions_adddup2");
int ret = orig ? orig(actions, fd, newfd) : ENOSYS;
if (ret == 0) {
pthread_mutex_lock(&g_closefrom_lock);
struct closefrom_entry *e = closefrom_entry(actions, 0);
if (e && e->from >= 0 && fd >= e->from) {
int reopened = 0;
for (int k = 0; k < e->nkeep; k++) reopened |= e->keep[k] == fd;
if (!reopened) e->bad_source = 1;
}
pthread_mutex_unlock(&g_closefrom_lock);
closefrom_note_target(actions, newfd);
}
return ret;
}
int posix_spawn_file_actions_init(posix_spawn_file_actions_t *actions) {
static int (*orig)(posix_spawn_file_actions_t *) = NULL;
if (!orig) orig = (int (*)(posix_spawn_file_actions_t *))dlsym(RTLD_NEXT, "posix_spawn_file_actions_init");
pthread_mutex_lock(&g_closefrom_lock);
struct closefrom_entry *e = closefrom_entry(actions, 0);
if (e) e->actions = NULL;
pthread_mutex_unlock(&g_closefrom_lock);
return orig ? orig(actions) : ENOSYS;
}
int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *actions) {
static int (*orig)(posix_spawn_file_actions_t *) = NULL;
if (!orig) orig = (int (*)(posix_spawn_file_actions_t *))dlsym(RTLD_NEXT, "posix_spawn_file_actions_destroy");
pthread_mutex_lock(&g_closefrom_lock);
struct closefrom_entry *e = closefrom_entry(actions, 0);
if (e) e->actions = NULL;
pthread_mutex_unlock(&g_closefrom_lock);
return orig ? orig(actions) : ENOSYS;
}
static int apply_closefrom(posix_spawn_file_actions_t *actions) {
pthread_mutex_lock(&g_closefrom_lock);
struct closefrom_entry *e = closefrom_entry(actions, 0);
if (!e || e->from < 0) {
pthread_mutex_unlock(&g_closefrom_lock);
return 0;
}
if (e->bad_source) {
pthread_mutex_unlock(&g_closefrom_lock);
return EBADF;
}
int highest = e->highest;
DIR *dir = opendir("/proc/self/fd");
if (dir) {
int self = dirfd(dir);
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] < '0' || entry->d_name[0] > '9') continue;
int fd = atoi(entry->d_name);
if (fd != self && fd > highest) highest = fd;
}
closedir(dir);
}
int start = e->from > e->closed_to + 1 ? e->from : e->closed_to + 1;
for (int fd = start; fd <= highest; fd++) {
int kept = 0;
for (int k = 0; k < e->nkeep; k++) kept |= e->keep[k] == fd;
if (!kept && posix_spawn_file_actions_addclose(actions, fd) != 0) break;
e->closed_to = fd;
}
pthread_mutex_unlock(&g_closefrom_lock);
return 0;
}
#else
static int apply_closefrom(posix_spawn_file_actions_t *actions) { (void)actions; return 0; }
#endif