Files
DroidDeck/tools/nativeguest/gosys/gosys_abi.h
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

80 lines
3.8 KiB
C

// What the hook and the code it adds to a static Go program (gosys.c) agree on.
//
// A Go program built without cgo makes its system calls itself, so the preloaded hook never sees
// them and Ubuntu's paths (/etc/resolv.conf, /usr/bin/git, ...) mean nothing to it. Before such a
// program starts, the hook adds a small piece of position-independent code to its file (see
// go_programs.c): the program's one system call function, internal/runtime/syscall.Syscall6, jumps
// to it, and it maps paths into the rootfs like the hook does, then makes the call.
//
// This header has no dependencies beyond <stdint.h>, so that the freestanding code and the hook,
// which are built with different flags, can both include it.
#ifndef DDNATV_GOSYS_ABI_H
#define DDNATV_GOSYS_ABI_H
#include <stdint.h>
#ifndef GOSYS_VERSION
#define GOSYS_VERSION 3u // raised when the code or its header changes: patched programs are patched again
#endif
// The scratch memory for rewritten paths: a small state area and 64 slots, in zero-filled memory the
// patcher appends to the program's data segment (so nothing is allocated per call). A slot belongs to
// the thread that took it until its call is over: a word in the state area for each slot says
// which goroutine (x28) has it. A slot that a vfork child kept by a successful execve is given back
// at the next call of the goroutine that made the child (the state area counts the execve calls
// that have not returned, [8]).
// (A ring of slots shared by every call was tried first: a thread that was descheduled for a
// moment between taking a slot and the call let sixteen others go round the ring and overwrite its
// path, so a Go program opened another file than it asked for.)
#define GOSYS_SLOTS 64u
#define GOSYS_SLOT_SIZE 32768u // two paths (4K each), an argv array (8K), an environment array (16K)
#define GOSYS_STATE_SIZE 1024u // [4] rewriting mode, [8] execve calls on their way, [64 + 8 * slot] who has the slot (0: nobody)
#define GOSYS_SCRATCH_SIZE (GOSYS_STATE_SIZE + GOSYS_SLOTS * GOSYS_SLOT_SIZE)
#define GOSYS_THUNK_BYTES 256u // room for the small code that connects a call site to the code
#define GOSYS_ROOT_MAX 448
#define GOSYS_PATH_MAX 448
#define GOSYS_LIBS_MAX 1024
struct gosys_hdr {
char magic[8]; // "CXGOSYS1"
uint32_t version; // GOSYS_VERSION
uint32_t flags;
int64_t scratch_rel; // scratch memory, relative to this header (the same in any load address)
uint32_t root_len; // the rootfs, as Android sees it
uint32_t ldso_len; // the rootfs's dynamic linker
uint32_t hook_len; // libddnatv-hook.so; 0: none
uint32_t libs_len; // the rootfs's library directories, ':' separated
uint32_t runner_len; // the rootfs's bash, or env without it; 0: Ubuntu programs cannot be started
uint32_t reserved;
char root[GOSYS_ROOT_MAX];
char ldso[GOSYS_PATH_MAX];
char hook[GOSYS_PATH_MAX];
char libs[GOSYS_LIBS_MAX];
char runner[GOSYS_PATH_MAX];
};
#define GOSYS_FLAG_RUNNER_IS_BASH 1u // gosys_hdr.flags
// The last 64 bytes of the added code: where it is, and what the patcher changed to add it.
struct gosys_trailer {
char magic[8]; // "CXGOEND1"
uint32_t version;
uint32_t total_size; // the added code, header to trailer
uint64_t text_orig_filesz; // the segment that holds it: its size before
uint64_t rw_orig_memsz; // the data segment: its size before
uint32_t text_index; // their numbers in the program header table
uint32_t rw_index;
uint32_t text_orig_flags;
uint32_t reserved;
uint64_t site_file_off; // the instruction that was replaced (a "svc #0")
uint64_t reserved2;
};
_Static_assert(sizeof(struct gosys_trailer) == 64, "the trailer is 64 bytes");
_Static_assert(sizeof(struct gosys_hdr) % 16 == 0, "the header keeps the code after it aligned");
#endif