Files
DroidDeck/tools/nativeguest
MaxsTechReview 872c5e1834 fix(native): reopen refused memfds and answer links in the form they are named (#3734)
* fix(native): reopen refused memfds and answer links in the form they are named

Android's app policy refuses the open permission on memory files, so a
read-only reopen of an own memfd through /proc/self/fd/N,
/proc/thread-self/fd/N, /proc/<own pid or thread>/fd/N or /dev/fd/N
failed with EACCES while read, write and mmap of the descriptor still
worked. The hook now answers such a refused read-only open with a new
memfd holding the same bytes, name, mode and seals. Writable opens,
other processes' descriptors and regular files stay refused. FEX routes
guest openat through libc syscall(), so x86 guests get the same answer.

readlink() stripped the rootfs prefix from every target, so a link
stored with a real target (Proton's s: drive) read as /root/... while
getcwd(), /proc/self/fd and /proc/self/maps give the real path, and a
path resolved through the link no longer matched the working directory.
A link named by its real rootfs path, and /proc/<pid>/cwd, now keep the
real target, as realpath() of a real path does; guest and relative names
keep the guest form.

* fix(native): keep the memfd copy read-only, leave openat2 errors native, keep guest form for remapped link names

The copy that answers a refused read-only memfd reopen is now sealed
against writes and resizing, so it cannot be written through as a
read-only descriptor could not. openat2 reads its open_how only after
the kernel refused the open with EACCES, so an unreadable pointer gets
the kernel's EFAULT. readlinkat and fstatat keep the guest form for a
relative name the hook remapped to a real path; only a name the caller
supplied in real form keeps the real target.

* fix(native): pin the memfd being copied and refuse copies that cannot match it

* Copy a refused memfd only when the path and the descriptor name one file
2026-12-05 02:54:50 -05:00
..

Native userland (no proot)

libddnatv-hook.so is an LD_PRELOAD library that lets the glibc rootfs run directly on Android: it maps the guest's absolute paths into the rootfs (plus the mounts proot used to provide, from DDNATV_BINDS), starts every program through the rootfs's ld.so, and answers the system calls Android's seccomp policy kills a process for (SIGSYS handler). There is no tracer. The app side is runtime/NativeGuest.kt; the setting is Performance > "Run without proot (experimental)".

The hook is derived from the Apache-2.0 libcortex-hook of ssfuisu/cortex (see LICENSE), renamed (cortex -> ddnatv, same length: the code compares environment names by length) and extended with:

  • DDNATV_BINDS mounts, DDNATV_PRELOAD (what /etc/ld.so.preload does for proot), and mapping of guest paths inside LD_PRELOAD;
  • POSIX shm_open/shm_unlink through /dev/shm;
  • starting Wine without its static preloader, which opens files with raw system calls.

Build: tools/nativeguest/build.sh out.so with an aarch64 glibc cross compiler.

Root or an ordinary user

Every program sees itself as root by default, which apt, dpkg and the session expect. DDNATV_USER=app in a process's environment makes it, and everything it starts, the app's own Android user instead, named deck (home /root): its ids, its files' owners, /proc/*/status and peer credentials are the kernel's, and it may not change its ids. DDNATV_BECOME=root or =app in a started program's environment switches that program and its children; the hook drops both variables and puts DDNATV_USER=app first in the environment, where a peer's mode is read from /proc/<pid>/environ. A set-user-ID program owned by the app (shown as root's) starts as root.

droiddeck-user [COMMAND] starts a shell or command that way, sudo and su return to root, and Performance > "Desktop runs as a user" starts the whole Plasma session as deck. Statically linked Go programs are patched once on disk and still see root.

Testing under Android's seccomp filter

Some devices (eng builds, setenforce 0 ROMs) run apps with no seccomp filter, so a hook that passes there can still die with SIGSYS on a stock phone (GUEST_EXIT 159). seccomp-sim.c applies a filter that answers the calls Android blocks with SIGSYS, then runs the program:

aarch64-linux-gnu-gcc -static -O2 -o seccomp-sim tools/nativeguest/seccomp-sim.c
# copy it into the rootfs, then name it for every native launch:
echo /usr/local/bin/seccomp-sim > <rootfs>/etc/droiddeck/seccomp-sim

grep Seccomp: /proc/<pid>/status should then show 2 for the session's processes. Empty the file to turn it off. seccomp-sim -a NR,NR PROGRAM leaves the listed calls allowed, to find which one kills a program.