Files
DroidDeck/tools/nativeguest/hook/env.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

310 lines
11 KiB
C

// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
// parts share their static functions and are built with the same command as before).
//
// The environment of started programs: Android programs get it without the hook, rootfs
// programs with it.
#ifndef DDNATV_HOST_TEST
static const char *ddnatv_hook_library(char *buf, size_t bufsz);
static char **clean_env_for_system(char *const envp[]) {
int count = 0;
while (envp && envp[count]) count++;
char **new_env = calloc(count + 3, sizeof(char *));
int dst = 0;
int has_path = 0;
for (int i = 0; i < count; i++) {
char first = envp[i][0];
if (!((first == 'L' && (strncmp(envp[i], "LD_PRELOAD=", 11) == 0 ||
strncmp(envp[i], "LD_LIBRARY_PATH=", 16) == 0)) ||
(first == 'G' && strncmp(envp[i], "GLIBC_TUNABLES=", 15) == 0))) {
if (first == 'P' && strncmp(envp[i], "PATH=", 5) == 0) {
has_path = 1;
size_t plen = strlen(envp[i]);
char *new_path = malloc(plen + 64);
if (new_path) {
snprintf(new_path, plen + 64, "PATH=/system/bin:/system/xbin:/sbin:/vendor/bin:%s", envp[i] + 5);
new_env[dst++] = new_path;
} else {
new_env[dst++] = envp[i];
}
} else {
new_env[dst++] = envp[i];
}
}
}
if (!has_path) {
new_env[dst++] = strdup("PATH=/system/bin:/system/xbin:/sbin:/vendor/bin");
}
new_env[dst] = NULL;
return new_env;
}
// LD_PRELOAD with every absolute path that is not in the rootfs yet mapped into it. Entries are
// separated by ':' or ' '. free() the result.
static char *map_preload_paths(const char *value) {
if (!value) return NULL;
size_t cap = strlen(value) + (g_ddnatv_root_len + 1) * 16 + 16;
char *out = malloc(cap);
if (!out) return NULL;
size_t used = 0;
out[0] = '\0';
const char *p = value;
while (*p) {
size_t len = strcspn(p, ": ");
if (len > 0 && len < PATH_MAX) {
char entry[PATH_MAX];
memcpy(entry, p, len);
entry[len] = '\0';
char buf[PATH_MAX];
const char *mapped = entry[0] == '/' ? rewrite_path_in_root(entry, buf, sizeof(buf)) : entry;
size_t mlen = strlen(mapped);
if (used + mlen + 2 >= cap) break;
if (used) out[used++] = ':';
memcpy(out + used, mapped, mlen);
used += mlen;
out[used] = '\0';
}
p += len;
if (*p) p++;
}
return out;
}
static char **prepare_ddnatv_env(char *const envp[], const char *real_exe) {
init_ddnatv_hook();
int count = 0;
int has_preload = 0;
int has_ld_library_path = 0;
int has_root = 0;
int has_tunables = 0;
int has_path = 0;
int has_tmp = 0;
int has_threads_max = 0;
int has_xz_opt = 0;
int has_xz_defaults = 0;
int has_frontend = 0;
int has_debconf_frontend = 0;
int has_debconf_seen = 0;
const char *chosen_real_exe = (real_exe && real_exe[0] != '\0') ? real_exe : (g_real_exe[0] != '\0' ? g_real_exe : NULL);
// Every variable is looked at on every exec, so only the names that start with its first letter
// are compared (this was 13 strncmp() calls for each of the 30 or so variables)
int has_nt = 0;
int has_binds = 0;
int has_hostname = 0;
int has_link_root = 0;
int has_preload_list = 0;
const char *preload_value = NULL;
while (envp && envp[count]) {
const char *e = envp[count];
switch (e[0]) {
case 'L':
if (strncmp(e, "LD_PRELOAD=", 11) == 0) { has_preload = 1; preload_value = e + 11; }
else if (strncmp(e, "LD_LIBRARY_PATH=", 16) == 0) has_ld_library_path = 1;
break;
case 'G':
if (strncmp(e, "GLIBC_TUNABLES=", 15) == 0) has_tunables = 1;
break;
case 'P':
if (strncmp(e, "PATH=", 5) == 0) has_path = 1;
break;
case 'T':
if (strncmp(e, "TMPDIR=", 7) == 0) has_tmp = 1;
break;
case 'X':
if (strncmp(e, "XZ_OPT=", 7) == 0) has_xz_opt = 1;
else if (strncmp(e, "XZ_DEFAULTS=", 12) == 0) has_xz_defaults = 1;
break;
case 'D':
if (strncmp(e, "DDNATV_ROOT=", 12) == 0) has_root = 1;
else if (strncmp(e, "DDNATV_NT=", 10) == 0) has_nt = 1;
else if (strncmp(e, "DDNATV_BINDS=", 13) == 0) has_binds = 1;
else if (strncmp(e, "DDNATV_PRELOAD=", 15) == 0) has_preload_list = 1;
else if (strncmp(e, "DDNATV_HOSTNAME=", 16) == 0) has_hostname = 1;
else if (strncmp(e, "DDNATV_LINK_ROOT=", 17) == 0) has_link_root = 1;
else if (strncmp(e, "DPKG_DEB_THREADS_MAX=", 21) == 0) has_threads_max = 1;
else if (strncmp(e, "DEBIAN_FRONTEND=", 16) == 0) has_frontend = 1;
else if (strncmp(e, "DEBCONF_FRONTEND=", 17) == 0) has_debconf_frontend = 1;
else if (strncmp(e, "DEBCONF_NONINTERACTIVE_SEEN=", 28) == 0) has_debconf_seen = 1;
break;
default:
break;
}
count++;
}
char **new_env = calloc(count + 24, sizeof(char *));
int dst = 0;
for (int i = 0; i < count; i++) {
if (has_preload && envp[i][0] == 'L' && strncmp(envp[i], "LD_PRELOAD=", 11) == 0) {
// ld.so opens these itself: the guest's paths are mapped to the rootfs, and the libraries every
// program loads (DDNATV_PRELOAD) come first
char *mapped = map_preload_paths(preload_value);
if (mapped) {
size_t chain = g_preload_host ? strlen(g_preload_host) : 0;
int has_chain = chain > 0 && strstr(mapped, g_preload_host) != NULL;
size_t len = 11 + chain + 1 + strlen(mapped) + 1;
char *str = malloc(len);
if (str) {
if (chain > 0 && !has_chain) snprintf(str, len, "LD_PRELOAD=%s:%s", g_preload_host, mapped);
else snprintf(str, len, "LD_PRELOAD=%s", mapped);
new_env[dst++] = str;
free(mapped);
continue;
}
free(mapped);
}
}
// If we have an explicit real executable, drop the inherited entry to replace with the fresh target
if (chosen_real_exe && envp[i][0] == 'D' && strncmp(envp[i], "DDNATV_REAL_EXE=", 16) == 0) {
continue;
}
new_env[dst++] = envp[i];
}
// Exact-size strings: this runs before every exec, often in a freshly forked child where each
// newly touched page is a copy-on-write fault.
if (chosen_real_exe) {
size_t len = strlen(chosen_real_exe);
char *str = malloc(16 + len + 1);
if (str) {
memcpy(str, "DDNATV_REAL_EXE=", 16);
memcpy(str + 16, chosen_real_exe, len + 1);
new_env[dst++] = str;
}
}
char hook_buf[PATH_MAX];
const char *hook = (!has_preload && g_ddnatv_root[0] != '\0') ? ddnatv_hook_library(hook_buf, sizeof(hook_buf)) : NULL;
if (hook) {
size_t hook_len = strlen(hook);
size_t host_len = g_preload_host ? strlen(g_preload_host) + 1 : 0;
size_t size = 11 + host_len + hook_len + 1;
char *str = malloc(size);
if (str) {
size_t n = ddnatv_append(str, size, 0, "LD_PRELOAD=", 11);
if (host_len) {
n = ddnatv_append(str, size, n, g_preload_host, host_len - 1);
n = ddnatv_append(str, size, n, ":", 1);
}
ddnatv_append(str, size, n, hook, hook_len);
new_env[dst++] = str;
}
}
if (!has_preload_list && g_preload_guest) {
size_t len = strlen(g_preload_guest);
char *str = malloc(15 + len + 1);
if (str) {
memcpy(str, "DDNATV_PRELOAD=", 15);
memcpy(str + 15, g_preload_guest, len + 1);
new_env[dst++] = str;
}
}
if (!has_ld_library_path && g_ddnatv_root[0] != '\0') {
size_t size = 16 + 1;
for (size_t i = 0; i < sizeof(k_rootfs_lib_dirs) / sizeof(k_rootfs_lib_dirs[0]); i++) {
char dir[PATH_MAX];
size += 1 + strlen(rewrite_path_in_root(k_rootfs_lib_dirs[i], dir, sizeof(dir)));
}
char *str = malloc(size);
if (str) {
size_t n = ddnatv_append(str, size, 0, "LD_LIBRARY_PATH=", 16);
append_rootfs_lib_dirs(str, size, n, 0);
new_env[dst++] = str;
}
}
if (!has_root && g_ddnatv_root[0] != '\0') {
char *str = malloc(PATH_MAX + 16);
if (str) {
snprintf(str, PATH_MAX + 16, "DDNATV_ROOT=%s", g_ddnatv_root);
new_env[dst++] = str;
}
}
if (!has_link_root && g_link_root_len) {
char *str = malloc(17 + g_link_root_len + 1);
if (str) {
memcpy(str, "DDNATV_LINK_ROOT=", 17);
memcpy(str + 17, g_link_root, g_link_root_len + 1);
new_env[dst++] = str;
}
}
if (!has_hostname && g_hostname[0]) {
char *str = malloc(16 + sizeof(g_hostname));
if (str) {
snprintf(str, 16 + sizeof(g_hostname), "DDNATV_HOSTNAME=%s", g_hostname);
new_env[dst++] = str;
}
}
if (!has_binds && g_binds_raw) {
size_t len = strlen(g_binds_raw);
char *str = malloc(13 + len + 1);
if (str) {
memcpy(str, "DDNATV_BINDS=", 13);
memcpy(str + 13, g_binds_raw, len + 1);
new_env[dst++] = str;
}
}
// This process looked for the ARM translator's files and found none: the programs it starts
// need not look again (it takes a look at /proc/self/fd in each of them, see privileged.c)
if (!has_nt && g_no_translator) {
new_env[dst++] = strdup("DDNATV_NT=0");
}
if (!has_tunables) {
new_env[dst++] = strdup("GLIBC_TUNABLES=glibc.pthread.rseq=0");
}
if (!has_path) {
new_env[dst++] = strdup("PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
}
if (!has_tmp && g_ddnatv_root[0] != '\0') {
char *str = malloc(PATH_MAX + 16);
if (str) {
snprintf(str, PATH_MAX + 16, "TMPDIR=%s/tmp", g_ddnatv_root);
new_env[dst++] = str;
}
}
if (!has_threads_max) {
new_env[dst++] = strdup("DPKG_DEB_THREADS_MAX=1");
}
if (!has_xz_opt) {
new_env[dst++] = strdup("XZ_OPT=-T1");
}
if (!has_xz_defaults) {
new_env[dst++] = strdup("XZ_DEFAULTS=-T1");
}
if (!has_frontend) {
new_env[dst++] = strdup("DEBIAN_FRONTEND=noninteractive");
}
if (!has_debconf_frontend) {
new_env[dst++] = strdup("DEBCONF_FRONTEND=noninteractive");
}
if (!has_debconf_seen) {
new_env[dst++] = strdup("DEBCONF_NONINTERACTIVE_SEEN=true");
}
new_env[dst] = NULL;
return new_env;
}
static void free_modified_env(char **modified_env, char *const orig_env[]) {
if (!modified_env) return;
for (int i = 0; modified_env[i] != NULL; i++) {
int is_orig = 0;
if (orig_env) {
for (int j = 0; orig_env[j] != NULL; j++) {
if (modified_env[i] == orig_env[j]) {
is_orig = 1;
break;
}
}
}
if (!is_orig) {
free(modified_env[i]);
}
}
free(modified_env);
}
#endif /* !DDNATV_HOST_TEST */