diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 389705314a..5bb85b97c7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -195,6 +195,9 @@ jobs: aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \ -o "$D/libblfastpath.so" tools/proot/fastpath/fastpath.c -ldl aarch64-linux-gnu-strip --strip-unneeded "$D/libblfastpath.so" + aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -nostdlib -ffreestanding -fno-stack-protector \ + -fno-tree-loop-distribute-patterns -Wl,-z,defs -o "$D/libblaudit.so" tools/proot/fastpath/audit.c + aarch64-linux-gnu-strip --strip-unneeded "$D/libblaudit.so" # Force-SSBS adapter, preloaded into a game's Wine only when the game asks for it. aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \ -o "$D/libssbs.so" tools/linuxfs/ssbs/ssbs_adapter.c -ldl diff --git a/app/src/main/cpp/fakeinput_steam.cpp b/app/src/main/cpp/fakeinput_steam.cpp index d1c69d2a4d..d88eb5dc41 100644 --- a/app/src/main/cpp/fakeinput_steam.cpp +++ b/app/src/main/cpp/fakeinput_steam.cpp @@ -301,23 +301,43 @@ namespace Logger { int log_enabled; // FAKE_EVDEV_LOG_FILE: the session's pad.log, shared by every process the library is loaded in // (the client, each game), so a pad report has the guest side in one file of the session folder. -// Opened with the raw syscall: open() is this library's own hook. -static int log_fd = -1; +// Opened with the raw syscalls (open() and close() are this library's own hooks), and only once +// there is a line to write: most processes never log one, and each open is a stop in proot on +// shared storage. +static std::atomic log_fd{-1}; +static std::atomic log_unusable{false}; +static char log_path[PATH_MAX]; static char log_name[17]; void init() { log_enabled = getenv("FAKE_EVDEV_LOG") && atoi(getenv("FAKE_EVDEV_LOG")); const char *path = getenv("FAKE_EVDEV_LOG_FILE"); - if (log_enabled && path && *path) - log_fd = static_cast(syscall(SYS_openat, AT_FDCWD, path, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644)); + if (log_enabled && path && *path && strlen(path) < sizeof(log_path)) strcpy(log_path, path); if (prctl(PR_GET_NAME, log_name) != 0) strcpy(log_name, "?"); } +static int log_file() { + int fd = log_fd.load(std::memory_order_acquire); + if (fd >= 0 || !log_path[0] || log_unusable.load(std::memory_order_relaxed)) return fd; + fd = static_cast(syscall(SYS_openat, AT_FDCWD, log_path, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644)); + if (fd < 0) { + log_unusable.store(true, std::memory_order_relaxed); + return -1; + } + int expected = -1; + if (!log_fd.compare_exchange_strong(expected, fd, std::memory_order_acq_rel)) { + syscall(SYS_close, fd); + fd = expected; + } + return fd; +} + void log(const char *message, ...) { if (!log_enabled) return; - if (log_fd < 0) { + int fd = log_file(); + if (fd < 0) { va_list args; va_start(args, message); vfprintf(stderr, message, args); @@ -343,7 +363,7 @@ void log(const char *message, ...) { n = sizeof(line) - 1; line[n - 1] = '\n'; } - ssize_t ignored = write(log_fd, line, n); + ssize_t ignored = write(fd, line, n); (void)ignored; } } // namespace Logger diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index a84ebc66a5..6da8cfbf63 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -42,6 +42,7 @@ object SessionFiles { "libblsession.so" to "usr/local/lib/libblsession.so", "libfakeinput.so" to "usr/local/lib/libfakeinput.so", "libblfastpath.so" to "usr/local/lib/libblfastpath.so", + "libblaudit.so" to "usr/local/lib/libblaudit.so", "libssbs.so" to "usr/local/lib/libssbs.so", "usr/local/bin/droiddeck-session" to "usr/local/bin/droiddeck-session", "usr/local/bin/droiddeck-fonts" to "usr/local/bin/droiddeck-fonts", diff --git a/tools/build_local.sh b/tools/build_local.sh index 646626c397..836a2c52d7 100755 --- a/tools/build_local.sh +++ b/tools/build_local.sh @@ -94,6 +94,9 @@ docker run --rm --platform linux/amd64 \ aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \ -o "$d/libblfastpath.so" tools/proot/fastpath/fastpath.c -ldl aarch64-linux-gnu-strip --strip-unneeded "$d/libblfastpath.so" + aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -nostdlib -ffreestanding -fno-stack-protector \ + -fno-tree-loop-distribute-patterns -Wl,-z,defs -o "$d/libblaudit.so" tools/proot/fastpath/audit.c + aarch64-linux-gnu-strip --strip-unneeded "$d/libblaudit.so" aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \ -o "$d/libssbs.so" tools/linuxfs/ssbs/ssbs_adapter.c -ldl aarch64-linux-gnu-strip --strip-unneeded "$d/libssbs.so" diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-seed-redists b/tools/linuxfs/overlay/usr/local/bin/droiddeck-seed-redists index ef0e5d3418..467a359877 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-seed-redists +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-seed-redists @@ -112,7 +112,8 @@ vcrt() { # dated block it never matched, and every prefix had the whole block appended again on every beat. stamp=$(printf '%s vcrt %s.%s.%s' "$pairs" "$VCRT_MAJOR" "$VCRT_MINOR" "$VCRT_BUILD" | cksum) -block=$(printf '%s\n' "$pairs" | awk -F'\t' -v now="$(date +%s)" ' +printf -v now '%(%s)T' -1 +block=$(printf '%s\n' "$pairs" | awk -F'\t' -v now="$now" ' { path = $1 sub(/^HKEY_LOCAL_MACHINE\\\\/, "", path) @@ -128,7 +129,7 @@ block=$(printf '%s\n' "$pairs" | awk -F'\t' -v now="$(date +%s)" ' } ') block="$block -$(vcrt "$(date +%s)")" +$(vcrt "$now")" # Proton builds a new prefix from its template, and the client runs the install scripts in that # prefix moments later, so the template carries the markers too. Its date is kept: Proton reads a @@ -141,12 +142,14 @@ for reg in "$steam_root"/steamapps/common/Proton*/files/share/default_pfx*/syste /mnt/droiddeck-sd/steamapps/compatdata/*/pfx/system.reg \ /root/Games/*/steamapps/compatdata/*/pfx/system.reg; do [ -f "$reg" ] || continue - seeded=$(dirname "$reg")/.droiddeck-redists - legacy=$(dirname "$reg")/.bannerlator-redists + seeded=${reg%/*}/.droiddeck-redists + legacy=${reg%/*}/.bannerlator-redists if [ -f "$legacy" ] && [ ! -f "$seeded" ]; then mv -f "$legacy" "$seeded" fi - [ "$(cat "$seeded" 2>/dev/null)" = "$stamp" ] && continue + have= + [ -f "$seeded" ] && read -r have < "$seeded" + [ "$have" = "$stamp" ] && continue dated=$(mktemp) || continue touch -r "$reg" "$dated" if printf '%s\n' "$block" >>"$reg"; then diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index 419290c38b..4d5f0c04db 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -29,6 +29,27 @@ if [ -n "${BL_LOG:-}" ] && [ -z "${BL_INSIDE:-}" ]; then echo "== rootfs: $(cat /.version 2>/dev/null || echo unversioned)" fi +# Every program loads the locale LANG names (C.UTF-8) before main(). glibc ships it as a directory, +# which it looks for once per category under two spellings before reading gconv's module lists: +# about 30 opens, each a stop in proot, for every process in the session, a script's dirname or cat +# included. An archive holding it (and gconv's cache) makes that one open. Built again when the +# glibc package changes, or when the archive does (locale-gen rewrites it). +prepare_locale() { + local dir=/usr/lib/locale stamp=/usr/lib/locale/.droiddeck-archive want have= pkg + pkg=(/var/lib/pacman/local/glibc-[0-9]*) + want=${pkg[0]##*/} + [ -f "$stamp" ] && read -r have < "$stamp" + [ "$have" = "$want" ] && [ -f "$dir/locale-archive" ] && [ ! "$dir/locale-archive" -nt "$stamp" ] && return 0 + [ -d "$dir/C.utf8" ] && command -v localedef >/dev/null 2>&1 || return 0 + # localedef names a new archive by hard link, which Android refuses; libblsession renames it. + DROIDDECK_LINK_RENAME=1 localedef --add-to-archive --replace "$dir/C.utf8" >/dev/null 2>&1 || return 0 + rm -f /usr/lib/gconv/gconv-modules.cache + command -v iconvconfig >/dev/null 2>&1 && iconvconfig >/dev/null 2>&1 + printf '%s\n' "$want" > "$stamp" + echo "== locale archive built for $want" +} +[ -z "${BL_INSIDE:-}" ] && prepare_locale + # Until the runtime is stable, Steam's own logs are copied beside the session log at exit so a # failed launch can be handed over as one folder. collect_steam_logs() { @@ -577,23 +598,42 @@ case $mode in esac done ) & - # The library beat. Both helpers are whole programs under proot (the compat one a python - # start and a parse of the client's config), and nearly every beat has nothing new for them. - # The seeder marks Proton's prefix template as well as the prefixes, so a new prefix is born - # marked and every 15 s is soon enough for the rest. The compat pass runs when a directory it - # reads from has changed since its last run - a new manifest, a new depot or tool, the client - # rewriting config.vdf - which [ -nt ] answers without starting anything, and once a minute - # regardless. + # The library beat. Both helpers are whole programs under proot (the seeder some forty of them, + # the compat one a python start and a parse of the client's config), and nearly every beat has + # nothing new for them: a burst of thousands of proot stops every quarter minute, game loads + # included. Each runs only when what it reads has changed, which bash answers without starting + # anything ([ -nt ], globs), and every five minutes regardless. The seeder acts on a prefix (or + # Proton's template for them) it has not marked, and on new install scripts, which arrive with a + # manifest; the compat pass on a new manifest, depot or tool, or the client rewriting config.vdf. ( beat=0 compat_stamp=$HOME/.bl-compat-beat - rm -f "$compat_stamp" - compat_due() { + seed_stamp=$HOME/.bl-seed-beat + rm -f "$compat_stamp" "$seed_stamp" + changed_since() { local d - [ -e "$compat_stamp" ] && [ "$((beat % 12))" -ne 0 ] || return 0 - for d in "$steam_root/steamapps" "$steam_root/steamapps/common" "$steam_root/config" \ - "$steam_root/compatibilitytools.d" /mnt/droiddeck-sd/steamapps /root/Games/*/steamapps; do - [ "$d" -nt "$compat_stamp" ] && return 0 + for d in "${@:2}"; do + [ "$d" -nt "$1" ] && return 0 + done + return 1 + } + compat_due() { + [ -e "$compat_stamp" ] && [ "$((beat % 60))" -ne 0 ] || return 0 + changed_since "$compat_stamp" "${libraries[@]}" "$steam_root/steamapps/common" "$steam_root/config" \ + "$steam_root/compatibilitytools.d" + } + seed_due() { + local reg + [ -e "$seed_stamp" ] && [ "$((beat % 60))" -ne 0 ] || return 0 + changed_since "$seed_stamp" "${libraries[@]}" && return 0 + for reg in "$steam_root"/steamapps/common/Proton*/files/share/default_pfx*/system.reg \ + /mnt/droiddeck-sd/steamapps/common/Proton*/files/share/default_pfx*/system.reg \ + /root/Games/*/steamapps/common/Proton*/files/share/default_pfx*/system.reg \ + "$steam_root"/compatibilitytools.d/*/files/share/default_pfx*/system.reg \ + "$steam_root"/steamapps/compatdata/*/pfx/system.reg \ + /mnt/droiddeck-sd/steamapps/compatdata/*/pfx/system.reg \ + /root/Games/*/steamapps/compatdata/*/pfx/system.reg; do + [ -f "$reg" ] && [ ! -f "${reg%/*}/.droiddeck-redists" ] && return 0 done return 1 } @@ -601,7 +641,11 @@ case $mode in nap 5 beat=$((beat + 1)) [ "$((beat % 3))" -eq 0 ] || continue - droiddeck-seed-redists "$steam_root" >/dev/null 2>&1 || true + libraries=("$steam_root/steamapps" /mnt/droiddeck/steamapps /mnt/droiddeck-sd/steamapps /root/Games/*/steamapps) + if seed_due; then + : > "$seed_stamp" + droiddeck-seed-redists "$steam_root" >/dev/null 2>&1 || true + fi if compat_due; then : > "$compat_stamp" steam-compatibility "$steam_root" >/dev/null 2>&1 || true @@ -839,6 +883,34 @@ BUSCONF # session can never again loop in silence, which reads as a menu opening and closing by itself. steam_updates=0 rm -f "$HOME/.bl-stop-requested" + # The client brings a game it launched to the front only while it counts someone as present, + # which it reads from gamescope's input counter. A game the app launches after a few minutes + # with no touch starts behind the library page and stays there until the next input (device, + # 2026-10-09). The game's window turning up behind the interface is counted as one input, as + # gamescope counts a key. + steam_bring_forward() { + local end=$((SECONDS + 600)) behind=0 line name value focused other n + while [ "$SECONDS" -lt "$end" ]; do + nap 2 + focused= other= + while IFS= read -r line; do + name=${line%%(*} value=${line#*= } + case $name in + GAMESCOPE_FOCUSED_APP) focused=$value ;; + GAMESCOPE_FOCUSABLE_APPS) for n in ${value//,/ }; do [ "$n" = 769 ] || other=1; done ;; + esac + done < <(xprop -root GAMESCOPE_FOCUSED_APP GAMESCOPE_FOCUSABLE_APPS 2>/dev/null) + [ -n "$focused" ] || continue + [ "$focused" = 769 ] || return 0 + if [ -n "$other" ]; then behind=$((behind + 1)); else behind=0; fi + [ "$behind" -ge 2 ] || continue + n=$(xprop -root GAMESCOPE_INPUT_COUNTER 2>/dev/null | sed -n 's/.*= *//p') + case $n in ''|*[!0-9]*) n=0 ;; esac + xprop -root -f GAMESCOPE_INPUT_COUNTER 32c -set GAMESCOPE_INPUT_COUNTER $(((n + 1) % 4294967296)) 2>/dev/null + echo "== steam: the game's window came up behind the interface; counted as an input" + return 0 + done + } if [ -n "${BL_LAUNCH_DIR:-}" ] && [ -d "$BL_LAUNCH_DIR" ]; then rm -f "$BL_LAUNCH_DIR/steam-stop" "$BL_LAUNCH_DIR/steam-game" "$BL_LAUNCH_DIR/steam-game.tmp" ( @@ -862,6 +934,9 @@ BUSCONF }; then echo "== steam: launching requested game $game_id in the running client" "$steam_root/steamrtarm64/steam" "steam://rungameid/$game_id" >/dev/null 2>&1 & + [ -n "${forward_pid:-}" ] && kill "$forward_pid" 2>/dev/null + steam_bring_forward & + forward_pid=$! else echo "== steam: ignored out-of-range game launch request" fi diff --git a/tools/linuxfs/overlay/usr/local/bin/steam-compatibility b/tools/linuxfs/overlay/usr/local/bin/steam-compatibility index af8e3dd9cd..f3eeb33b73 100755 --- a/tools/linuxfs/overlay/usr/local/bin/steam-compatibility +++ b/tools/linuxfs/overlay/usr/local/bin/steam-compatibility @@ -155,11 +155,23 @@ bl_components() { cm_build=$(awk 'NR == 1 { print $2 }' "$base/version" 2>/dev/null) cm_fixed=0 cm_checked=0 + # One sha256sum for every file: each program started here pays a dozen traced library lookups + # under the game's search path before it does anything. + local cm_rows=() cm_files=() row h p + local -A cm_sums=() while IFS=$'\\t' read -r t want comp rel sum src; do [ "$t" = "$cm_tool" ] && [ "$want" = "$cm_build" ] || continue + cm_rows+=("$comp"$'\\t'"$rel"$'\\t'"$sum"$'\\t'"$src") + [ -f "$base/$rel" ] && cm_files+=("$base/$rel") + done < "$cm_list" + if [ ${#cm_files[@]} -gt 0 ]; then + while read -r h p; do cm_sums["$p"]=$h; done < <(sha256sum -- "${cm_files[@]}" 2>/dev/null) + fi + for row in "${cm_rows[@]}"; do + IFS=$'\\t' read -r comp rel sum src <<< "$row" cm_checked=$((cm_checked + 1)) f="$base/$rel" - if [ -f "$f" ] && [ "$(sha256sum "$f" | cut -d' ' -f1)" = "$sum" ]; then continue; fi + if [ -f "$f" ] && [ "${cm_sums[$f]:-}" = "$sum" ]; then continue; fi if [ ! -f "$src" ]; then echo "droiddeck-proton: components: $comp source missing ($src), left as it is" >&2 continue @@ -168,7 +180,7 @@ bl_components() { cp -f "$src" "$f.bl-new" && mv -f "$f.bl-new" "$f" || continue case "$rel" in *.so) chmod 755 "$f" ;; *) chmod 644 "$f" ;; esac cm_fixed=$((cm_fixed + 1)) - done < "$cm_list" + done [ "$cm_checked" -gt 0 ] && echo "droiddeck-proton: components: checked $cm_checked file(s) in $cm_tool for app $cm_app" >&2 if [ "$cm_fixed" -gt 0 ]; then echo "droiddeck-proton: components: re-applied $cm_fixed of $cm_checked file(s) in $cm_tool before this launch" >&2 @@ -347,6 +359,16 @@ for fake in /usr/local/lib/libfakeinput.so /usr/local/lib/libblsession.so; do esac fi done +# ld.so looks every library up along the game's search path (its own directory, Steam's runtime +# and video directories, Proton's), and under proot each miss is a round trip to the tracer. With +# the fast path, the audit module skips the misses it can prove (tools/proot/fastpath/audit.c). +audit=/usr/local/lib/libblaudit.so +if [ -n "${PROOT_FP_KEY:-}" ] && [ -f "$audit" ]; then + case ":${LD_AUDIT:-}:" in + *":$audit:"*) ;; + *) LD_AUDIT="$audit${LD_AUDIT:+:$LD_AUDIT}"; export LD_AUDIT ;; + esac +fi # The depot this tool is named after, newest across the libraries that hold it; the others in the # list stand in only when it is missing. depot= @@ -427,6 +449,14 @@ for fake in /usr/local/lib/libfakeinput.so /usr/local/lib/libblsession.so; do esac fi done +# See droiddeck-proton: library lookups that cannot succeed are skipped with the fast path. +audit=/usr/local/lib/libblaudit.so +if [ -n "${PROOT_FP_KEY:-}" ] && [ -f "$audit" ]; then + case ":${LD_AUDIT:-}:" in + *":$audit:"*) ;; + *) LD_AUDIT="$audit${LD_AUDIT:+:$LD_AUDIT}"; export LD_AUDIT ;; + esac +fi bl_components "$here" bl_directaudio "$here" bl_game_cpus diff --git a/tools/linuxfs/preload/links.c b/tools/linuxfs/preload/links.c index c1304577f0..7a963205b1 100644 --- a/tools/linuxfs/preload/links.c +++ b/tools/linuxfs/preload/links.c @@ -12,16 +12,27 @@ * the lock excludes as before, and the holder removes both names when it is done. Every other * link keeps the real answer: a program that links a file into place and then deletes the * original would be left with a dangling symlink. + * + * That pattern itself is answered for one caller that asks: with DROIDDECK_LINK_RENAME=1 (the + * session sets it for localedef, which names a finished locale archive that way) a refused link + * becomes a rename that will not replace an existing name, which is all the link promised there. */ #define _GNU_SOURCE #include #include #include +#include +#include #include #include static int refused(int error) { return error == EPERM || error == EACCES; } +static int moves_instead(void) { + const char *v = getenv("DROIDDECK_LINK_RENAME"); + return v != NULL && strcmp(v, "1") == 0; +} + static int lock_name(const char *path) { size_t n = path ? strlen(path) : 0; return n > 2 && strcmp(path + n - 2, "-l") == 0; @@ -31,7 +42,9 @@ static int lock_name(const char *path) { __attribute__((visibility("hidden"))) int bl_link_lock(const char *oldpath, const char *newpath, int result) { /* libc's own symlink: dircache.c's wrapper may be holding its lock around this call. */ static int (*real_symlink)(const char *, const char *); - if (result == 0 || !refused(errno) || !lock_name(newpath)) return result; + if (result == 0 || !refused(errno)) return result; + if (moves_instead()) return renameat2(AT_FDCWD, oldpath, AT_FDCWD, newpath, RENAME_NOREPLACE); + if (!lock_name(newpath)) return result; if (real_symlink == NULL) real_symlink = dlsym(RTLD_NEXT, "symlink"); return real_symlink(oldpath, newpath); } @@ -49,6 +62,8 @@ int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, static int (*real_linkat)(int, const char *, int, const char *, int); if (real_linkat == NULL) real_linkat = dlsym(RTLD_NEXT, "linkat"); int result = real_linkat(olddirfd, oldpath, newdirfd, newpath, flags); + if (result != 0 && refused(errno) && flags == 0 && moves_instead()) + return renameat2(olddirfd, oldpath, newdirfd, newpath, RENAME_NOREPLACE); /* A relative old name would resolve against the new name's directory as a symlink's target. */ if (result != 0 && refused(errno) && lock_name(newpath) && oldpath[0] == '/' && flags == 0) return symlinkat(oldpath, newdirfd, newpath); diff --git a/tools/proot/bench/equiv2.py b/tools/proot/bench/equiv2.py new file mode 100644 index 0000000000..397f41b56a --- /dev/null +++ b/tools/proot/bench/equiv2.py @@ -0,0 +1,259 @@ +"""Fast path equivalence, part two: symlinked directories (Wine's dosdevices), "..", trailing +slashes, realpath/getcwd, the write family, Unix sockets, xattrs, statfs, inotify and fstat. + +Run under proot with and without the fast path (run-device.sh fpoff / fastpath) and diff the +output: every line must match. Builds its own tree under /root/fx and /mnt/droiddeck-sd/fx. +""" +import ctypes +import errno +import os +import shutil +import socket +import stat +import sys + +libc = ctypes.CDLL(None, use_errno=True) +libc.realpath.restype = ctypes.c_char_p +libc.realpath.argtypes = [ctypes.c_char_p, ctypes.c_char_p] +libc.getcwd.restype = ctypes.c_char_p +libc.getcwd.argtypes = [ctypes.c_char_p, ctypes.c_size_t] +libc.get_nprocs.restype = ctypes.c_int + +FX = "/root/fx" +SD = "/mnt/droiddeck-sd/fx" + + +def setup(): + for d in (FX, SD): + shutil.rmtree(d, ignore_errors=True) + os.makedirs(FX + "/pfx/drive_c/windows/system32") + os.makedirs(FX + "/pfx/dosdevices") + os.makedirs(FX + "/real/sub/deeper") + os.makedirs(SD + "/data/inner") + with open(FX + "/pfx/drive_c/windows/system32/kernel32.dll", "w") as f: + f.write("MZ" * 10) + with open(FX + "/real/sub/file.txt", "w") as f: + f.write("hello") + with open(SD + "/data/inner/asset.pak", "w") as f: + f.write("pak" * 7) + os.symlink("../drive_c", FX + "/pfx/dosdevices/c:") + os.symlink("/", FX + "/pfx/dosdevices/z:") + os.symlink(SD + "/data", FX + "/pfx/dosdevices/d:") + os.symlink("real/sub", FX + "/rel-dir") + os.symlink(FX + "/real", FX + "/abs-dir") + os.symlink("rel-dir", FX + "/chain1") + os.symlink("chain1", FX + "/chain2") + os.symlink("nowhere", FX + "/dangling") + os.symlink("loop-b", FX + "/loop-a") + os.symlink("loop-a", FX + "/loop-b") + os.symlink("rel-dir/file.txt", FX + "/file-link") + os.symlink("../fx/real/sub/../sub/deeper", FX + "/dotdot-link") + os.symlink("/etc", FX + "/etc-link") + os.symlink("/proc/self", FX + "/proc-link") + os.symlink("/dev/null", FX + "/null-link") + os.symlink("../../../root/fx/real", SD + "/back-to-root") + + +PATHS = [ + "pfx/dosdevices/c:/windows/system32/kernel32.dll", + "pfx/dosdevices/c:/windows/system32/KERNEL32.DLL", + "pfx/dosdevices/c:/windows/system32", + "pfx/dosdevices/c:/windows/system32/", + "pfx/dosdevices/c:/windows/system32/kernel32.dll/", + "pfx/dosdevices/z:/root/fx/real/sub/file.txt", + "pfx/dosdevices/z:/etc/os-release", + "pfx/dosdevices/z:/mnt/droiddeck-sd/fx/data/inner/asset.pak", + "pfx/dosdevices/z:/proc/self/status", + "pfx/dosdevices/z:/nope/x", + "pfx/dosdevices/d:/inner/asset.pak", + "pfx/dosdevices/d:/inner/missing.pak", + "pfx/dosdevices/d:/../back-to-root/sub/file.txt", + "pfx/dosdevices/c:/../dosdevices/c:/windows", + "rel-dir/file.txt", + "rel-dir/../sub/file.txt", + "rel-dir/..", + "abs-dir/sub/deeper", + "chain2/file.txt", + "chain2", + "chain2/", + "dangling", + "dangling/x", + "loop-a", + "loop-a/x", + "file-link", + "file-link/", + "dotdot-link", + "etc-link/os-release", + "proc-link/status", + "null-link", + "real/sub/./file.txt", + "real//sub///file.txt", + "real/sub/file.txt/../file.txt", + "nope", + "nope/deeper", + ".", + "..", + "../fx/real", +] + + +def call(fn): + try: + return repr(fn()) + except OSError as e: + return "E" + errno.errorcode.get(e.errno, str(e.errno)) + + +def realpath(p): + ctypes.set_errno(0) + r = libc.realpath(p.encode(), None) + if r is None: + return "E" + errno.errorcode.get(ctypes.get_errno(), "?") + return r.decode() + + +def mode_of(s): + return (stat.S_IFMT(s.st_mode), s.st_size if stat.S_ISREG(s.st_mode) else 0) + + +def probe(p): + out = [ + "stat=" + call(lambda: mode_of(os.stat(p))), + "lstat=" + call(lambda: mode_of(os.lstat(p))), + "readlink=" + call(lambda: os.readlink(p)), + "read=" + call(lambda: open(p, "rb").read(8)), + "list=" + call(lambda: sorted(os.listdir(p))[:5]), + "access=" + call(lambda: (os.access(p, os.R_OK), os.access(p, os.W_OK, effective_ids=True))), + "realpath=" + realpath(p), + "nofollow=" + call(lambda: os.close(os.open(p, os.O_RDONLY | os.O_NOFOLLOW))), + "statvfs=" + call(lambda: (lambda v: (v.f_bsize, v.f_frsize, v.f_namemax, v.f_flag, v.f_fsid, v.f_files > 0))(os.statvfs(p))), + "xattr=" + call(lambda: os.getxattr(p, "user.DOSATTRIB")), + "lxattr=" + call(lambda: len(os.listxattr(p, follow_symlinks=False)) >= 0), + ] + return " ".join(out) + + +def writes(base): + out = [] + w = base + "/w" + t = lambda label, fn: out.append(f" {label}: {call(fn)}") + t("mkdir", lambda: os.mkdir(w)) + t("mkdir again", lambda: os.mkdir(w)) + t("mkdir under file", lambda: os.mkdir(base + "/real/sub/file.txt/x")) + t("mkdir missing parent", lambda: os.mkdir(w + "/a/b")) + t("create", lambda: os.close(os.open(w + "/f", os.O_CREAT | os.O_WRONLY, 0o640))) + t("excl existing", lambda: os.open(w + "/f", os.O_CREAT | os.O_EXCL | os.O_WRONLY)) + t("symlink", lambda: os.symlink("f", w + "/lf")) + t("symlink exists", lambda: os.symlink("f", w + "/lf")) + t("create through link", lambda: os.close(os.open(w + "/lf", os.O_CREAT | os.O_WRONLY))) + t("dangling create", lambda: (os.symlink("made-by-link", w + "/dl"), os.close(os.open(w + "/dl", os.O_CREAT | os.O_WRONLY)), os.path.lexists(w + "/made-by-link"))[2]) + t("excl through link", lambda: os.open(w + "/lf", os.O_CREAT | os.O_EXCL | os.O_WRONLY)) + t("chmod", lambda: (os.chmod(w + "/lf", 0o600), oct(os.stat(w + "/f").st_mode & 0o777))[1]) + t("utime", lambda: (os.utime(w + "/f", (1000, 2000)), os.stat(w + "/f").st_mtime)[1]) + t("utime link nofollow", lambda: os.utime(w + "/lf", (3, 4), follow_symlinks=False)) + t("futimens", lambda: (lambda fd: (os.utime(fd, (5, 6)), os.close(fd)))(os.open(w + "/f", os.O_RDONLY))) + t("truncate", lambda: (os.truncate(w + "/lf", 7), os.stat(w + "/f").st_size)[1]) + t("rename file", lambda: os.rename(w + "/f", w + "/g")) + t("rename missing", lambda: os.rename(w + "/f", w + "/h")) + t("rename link", lambda: os.rename(w + "/lf", w + "/lg")) + t("readlink renamed", lambda: os.readlink(w + "/lg")) + t("replace", lambda: (open(w + "/tmp", "w").write("new"), os.replace(w + "/tmp", w + "/g"), open(w + "/g").read())[2]) + t("rename dir", lambda: (os.mkdir(w + "/d1"), os.rename(w + "/d1", w + "/d2"))[1]) + t("rename trailing", lambda: os.rename(w + "/d2/", w + "/d3")) + t("unlink", lambda: os.unlink(w + "/g")) + t("unlink missing", lambda: os.unlink(w + "/g")) + t("unlink dir", lambda: os.unlink(w + "/d3")) + t("unlink trailing", lambda: os.unlink(w + "/lg/")) + t("rmdir nonempty", lambda: os.rmdir(w)) + t("rmdir file", lambda: os.rmdir(w + "/lg")) + t("unlink link", lambda: os.unlink(w + "/lg")) + t("rmdir", lambda: os.rmdir(w + "/d3")) + t("listing", lambda: sorted(os.listdir(w))) + for name in os.listdir(w): + call(lambda: os.unlink(w + "/" + name)) + t("rmdir done", lambda: os.rmdir(w)) + return out + + +def sockets(base): + out = [] + path = base + "/s.sock" + try: + os.unlink(path) + except OSError: + pass + srv = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + out.append(" bind " + call(lambda: srv.bind(path))) + if not os.path.exists(path): + # adb's shell may not create sockets under /data/local/tmp; the app can. + srv.close() + return out + out.append(" name " + call(lambda: srv.getsockname())) + out.append(" listen " + call(lambda: srv.listen(1))) + out.append(" is socket " + call(lambda: stat.S_ISSOCK(os.stat(path).st_mode))) + cli = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + out.append(" connect " + call(lambda: cli.connect(path))) + conn, _ = srv.accept() + out.append(" peer " + call(lambda: cli.getpeername())) + cli.sendall(b"ping") + out.append(" recv " + call(lambda: conn.recv(4))) + srv.setblocking(False) + out.append(" accept none " + call(lambda: srv.accept())) + for s in (conn, cli, srv): + s.close() + out.append(" connect missing " + call(lambda: socket.socket(socket.AF_UNIX).connect(base + "/nope.sock"))) + a = socket.socket(socket.AF_UNIX) + out.append(" abstract " + call(lambda: (a.bind("\0fp-equiv"), a.getsockname())[1])) + a.close() + os.unlink(path) + return out + + +def main(): + setup() + for cwd in [FX, "/", FX + "/pfx/dosdevices/c:/windows", FX + "/chain2", SD + "/data", "/mnt/droiddeck-sd"]: + print("== cwd", cwd, call(lambda: os.chdir(cwd)), "getcwd", call(os.getcwd), "libc", libc.getcwd(None, 0)) + for p in PATHS: + print(" ", p, probe(p)) + d = os.open(".", os.O_RDONLY) + for p in PATHS[:12]: + print(" at", p, call(lambda: mode_of(os.stat(p, dir_fd=d))), call(lambda: mode_of(os.stat(p, dir_fd=d, follow_symlinks=False)))) + print(" fstat", call(lambda: stat.S_IFMT(os.fstat(d).st_mode))) + os.close(d) + for base in (FX, SD, FX + "/pfx/dosdevices/d:", FX + "/chain2"): + os.chdir("/") + print("== writes in", base) + print("\n".join(writes(base))) + print("== sockets in", base) + print("\n".join(sockets(base))) + os.chdir(FX) + print("== relative writes") + print("\n".join(writes("."))) + print("== moved cwd") + os.mkdir(FX + "/mv1") + os.chdir(FX + "/mv1") + os.rename(FX + "/mv1", FX + "/mv2") + print(" getcwd", call(os.getcwd), libc.getcwd(None, 0)) + print(" rel stat", call(lambda: mode_of(os.stat(".")))) + os.chdir(FX) + os.rmdir(FX + "/mv2") + print("== deleted cwd") + os.mkdir(FX + "/gone") + os.chdir(FX + "/gone") + os.rmdir(FX + "/gone") + print(" getcwd", call(os.getcwd)) + os.chdir(FX) + print("== inotify") + lib_in = libc.inotify_init1(0) + for p in (FX + "/chain2", FX + "/pfx/dosdevices/c:/windows", FX + "/dangling", FX + "/nope"): + ctypes.set_errno(0) + w = libc.inotify_add_watch(lib_in, p.encode(), 0x100 | 0x200) + print(" ", p, "ok" if w >= 0 else "E" + errno.errorcode.get(ctypes.get_errno(), "?")) + os.close(lib_in) + print("== cpus", libc.get_nprocs() == os.cpu_count(), os.cpu_count() > 0) + for d in (FX, SD): + shutil.rmtree(d, ignore_errors=True) + + +if __name__ == "__main__": + main() diff --git a/tools/proot/bench/equiv3.py b/tools/proot/bench/equiv3.py new file mode 100644 index 0000000000..9f263ba48a --- /dev/null +++ b/tools/proot/bench/equiv3.py @@ -0,0 +1,509 @@ +"""Fast path equivalence, part three: /proc (self, fd and exe links, readlink as proot answers it, +realpath through it, opening through fd links), link text naming host paths, creating in +directories whose host path is long, glue above a binding proot cannot look into, files the +caller may not stat, trailing slashes, shm_open, accept, passwd lookups and the _FORTIFY_SOURCE +entry points. + +Run under proot with and without the fast path (run-device.sh fpoff / fastpath) and diff the +output: every line must match. The rig's rootfs is ROOT; run-device.sh binds /cache/fp/perf and +SHARED at /opt/fpx. +""" +import ctypes +import errno +import os +import shutil +import socket +import stat +import sys + +ROOT = "/data/local/tmp/dd/mini" +SHARED = "/data/local/tmp/dd/fpx" +FX = "/root/fx3" +DEEP = FX + "/a-directory-name-long-enough/to-push-the-host-path/past-what-sun-path-holds/and-then-some" + +libc = ctypes.CDLL(None, use_errno=True) +libc.realpath.restype = ctypes.c_char_p +libc.realpath.argtypes = [ctypes.c_char_p, ctypes.c_char_p] +libc.readlink.restype = ctypes.c_ssize_t +libc.readlink.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_size_t] +libc.__readlink_chk.restype = ctypes.c_ssize_t +libc.__readlink_chk.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_size_t, ctypes.c_size_t] +libc.__readlinkat_chk.restype = ctypes.c_ssize_t +libc.__readlinkat_chk.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_size_t, ctypes.c_size_t] +libc.__getcwd_chk.restype = ctypes.c_char_p +libc.__getcwd_chk.argtypes = [ctypes.c_char_p, ctypes.c_size_t, ctypes.c_size_t] +libc.shm_open.restype = ctypes.c_int +libc.shm_open.argtypes = [ctypes.c_char_p, ctypes.c_int, ctypes.c_uint] +libc.shm_unlink.restype = ctypes.c_int +libc.shm_unlink.argtypes = [ctypes.c_char_p] + + +def norm(s): + return str(s).replace(str(os.getpid()), "PID").replace(str(ctypes.CDLL(None).gettid()), "TID") + + +def err(): + return "E" + errno.errorcode.get(ctypes.get_errno(), "?") + + +def call(fn): + try: + return norm(repr(fn())) + except OSError as e: + return "E" + errno.errorcode.get(e.errno, str(e.errno)) + + +def realpath(p): + ctypes.set_errno(0) + r = libc.realpath(p.encode(), None) + return err() if r is None else norm(r.decode()) + + +def raw_readlink(p, size, chk=False): + buf = ctypes.create_string_buffer(b"#" * (size + 2), size + 2) + ctypes.set_errno(0) + n = libc.__readlink_chk(p.encode(), buf, size, size + 2) if chk else libc.readlink(p.encode(), buf, size) + if n < 0: + return err() + return norm((n, buf.raw[: min(n + 1, size)])) + + +def mode_of(s): + return (stat.S_IFMT(s.st_mode), s.st_size if stat.S_ISREG(s.st_mode) else 0) + + +def opened(p): + def go(): + fd = os.open(p, os.O_RDONLY) + try: + return mode_of(os.fstat(fd)) + finally: + os.close(fd) + return f"open={call(go)} stat={call(lambda: mode_of(os.stat(p)))}" + + +def links(p): + sizes = [1, 5, 4096] + try: + whole = len(os.readlink(p)) + sizes += [whole - 1, whole, whole + 1, whole + 2] + except OSError: + pass + out = [f"readlink={call(lambda: os.readlink(p))}", f"realpath={realpath(p)}", + f"stat={call(lambda: mode_of(os.stat(p)))}", f"lstat={call(lambda: stat.S_IFMT(os.lstat(p).st_mode))}"] + out += [f"buf{n}={raw_readlink(p, n)}" for n in sizes if n > 0] + out.append(f"chk={raw_readlink(p, 64, True)}") + return " ".join(out) + + +def proc_section(): + print("== /proc") + os.makedirs(FX + "/dir", exist_ok=True) + open(FX + "/file", "w").write("x") + open(FX + "/doomed", "w").write("x") + held = { + "rootfs file": os.open(FX + "/file", os.O_RDONLY), + "rootfs dir": os.open(FX + "/dir", os.O_RDONLY), + "bound file": os.open("/proc/version", os.O_RDONLY), + "sdcard": os.open("/sdcard", os.O_RDONLY), + "shared bind": os.open("/mnt/droiddeck-sd", os.O_RDONLY), + "dev null": os.open("/dev/null", os.O_RDONLY), + "deleted": os.open(FX + "/doomed", os.O_RDONLY), + "outside": os.open("/data/local/tmp", os.O_RDONLY), + } + os.unlink(FX + "/doomed") + r, w = os.pipe() + held["pipe"] = r + s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) + held["socket"] = s.fileno() + for label, fd in held.items(): + for p in (f"/proc/self/fd/{fd}", f"/proc/{os.getpid()}/fd/{fd}", f"/proc/thread-self/fd/{fd}"): + print(" ", label, norm(p), links(p)) + d = os.open("/proc/self/fd", os.O_RDONLY) + print(" ", label, "at", call(lambda: os.readlink(str(fd), dir_fd=d))) + os.close(d) + for p in ("/proc/self/exe", f"/proc/{os.getpid()}/exe", "/proc/self/cwd", "/proc/self/root", "/proc/self", + "/proc/thread-self", "/proc/self/task", "/proc/1/exe", "/proc/self/ns/mnt", "/proc/nope/exe", + "/proc/self/fd/9999", "/proc/self/fd/", "/proc/self/fd/foo"): + print(" ", norm(p), links(p)) + for p in ("/proc/self/cwd/file", "/proc/self/cwd/../fx3/dir", "/proc/self/exe/x", "/proc/self/fd/../status", + "/proc/self/./status", "/proc/self/task/../status", "/proc/thread-self/status", "/proc/self/root/etc"): + print(" ", norm(p), "realpath", realpath(p), "stat", call(lambda: mode_of(os.stat(p)))) + for p in ("/proc/self/status", "/proc/version", "/proc/loadavg", "/proc/self/cmdline", "/proc/meminfo"): + print(" ", p, "first", call(lambda: open(p, "rb").read(5)), "access", call(lambda: os.access(p, os.R_OK))) + print(" fd count", call(lambda: len(os.listdir("/proc/self/fd")) > 3)) + for label, fd in list(held.items()) + [("write end", w)]: + for p in (f"/proc/self/fd/{fd}", f"/proc/{os.getpid()}/fd/{fd}", f"/proc/thread-self/fd/{fd}"): + print(" ", label, "through", norm(p), opened(p), "access", call(lambda: os.access(p, os.R_OK)), + "nofollow", call(lambda: os.close(os.open(p, os.O_RDONLY | os.O_NOFOLLOW))), + "below", call(lambda: mode_of(os.stat(p + "/file"))), "slash", call(lambda: mode_of(os.stat(p + "/")))) + for p in ("/proc/mounts", "/proc/net", "/proc/net/unix", "/proc/self/cwd/file", f"/proc/{os.getpid()}/cwd/dir", + "/proc/self/root/etc/os-release", "/proc/self/exe", "/proc/1/fd/0", "/proc/1/fd/0/x", "/proc/1/cwd", + "/proc/1/environ", "/proc/self/task"): + print(" ", norm(p), opened(p), "access", call(lambda: os.access(p, os.F_OK)), + "lstat", call(lambda: stat.S_IFMT(os.lstat(p).st_mode))) + print(" mtab-like", call(lambda: (os.symlink("/proc/self/mounts", FX + "/mtab"), open(FX + "/mtab").read(4))[1])) + for fd in list(held.values()) + [w]: + try: + os.close(fd) + except OSError: + pass + s.detach() + + +def host_text_section(): + print("== link text naming host paths") + cases = { + "in-root": ROOT + "/etc/os-release", + "root itself": ROOT, + "bound sdcard": "/sdcard/Download", + "shared bind": "/data/local/tmp/dd/tmp", + "outside": "/data/local/tmp", + "relative": "../fx3/file", + } + for label, text in cases.items(): + link = FX + "/ht-" + label.replace(" ", "-") + os.symlink(text, link) + print(" ", label, links(link), "read", call(lambda: open(link, "rb").read(4))) + + +def shared_section(): + print("== link text across bindings") + os.makedirs(SHARED + "/sub", exist_ok=True) + open(SHARED + "/target", "w").write("shared") + os.makedirs("/data/local/tmp/dd/fpy", exist_ok=True) + cases = [ + ("/opt/fpx/l-same", SHARED + "/target"), + ("/opt/fpx/l-itself", SHARED), + ("/opt/fpx/l-sub", SHARED + "/sub"), + ("/opt/fpx/l-other-bind", "/data/local/tmp/dd/fpy"), + ("/opt/fpx/l-rootfs", ROOT + "/etc/os-release"), + ("/opt/fpx/l-rel", "target"), + ("/opt/fpx/l-guest", "/opt/fpx/target"), + ("/data/local/tmp/dd/fpy/l-into-shared", SHARED + "/target"), + ("/data/local/tmp/dd/fpy/l-rootfs", ROOT + "/etc/os-release"), + ("/data/local/tmp/dd/fpy/l-root", ROOT), + (FX + "/l-shared", SHARED + "/target"), + (FX + "/l-shared-guest", "/opt/fpx/target"), + ] + for link, text in cases: + try: + os.unlink(link) + except OSError: + pass + os.symlink(text, link) + print(" ", link, links(link), opened(link), "via", call(lambda: mode_of(os.stat(link + "/"))), + "below", call(lambda: mode_of(os.stat(link + "/target")))) + shutil.rmtree("/data/local/tmp/dd/fpy", ignore_errors=True) + for name in os.listdir(SHARED): + if name.startswith("l-"): + os.unlink(SHARED + "/" + name) + + +def unseen_section(): + print("== files the caller may not stat") + for p in ("/proc/1/fd/0", "/proc/1/fd/0/x", "/proc/1/fd", "/proc/1/environ", "/proc/1/mem", "/proc/1/fdinfo/0", + "/proc/sys/vm/overcommit_memory", "/proc/1/task/1/fd/0"): + print(" ", p, "stat", call(lambda: mode_of(os.stat(p))), "lstat", call(lambda: stat.S_IFMT(os.lstat(p).st_mode)), + "access", call(lambda: os.access(p, os.R_OK)), "open", call(lambda: os.close(os.open(p, os.O_RDONLY))), + "xattr", call(lambda: os.listxattr(p)), "chmod", call(lambda: os.chmod(p, 0o600)), + "utime", call(lambda: os.utime(p)), "unlink", call(lambda: os.unlink(p)), "mkdir", call(lambda: os.mkdir(p)), + "readlink", call(lambda: os.readlink(p)), "realpath", realpath(p)) + + +def slash_section(): + print("== trailing slashes") + os.makedirs(FX + "/sl/dir", exist_ok=True) + open(FX + "/sl/file", "w").write("x") + os.symlink("dir", FX + "/sl/ldir") + os.symlink("file", FX + "/sl/lfile") + os.symlink("nowhere", FX + "/sl/ldangling") + for name in ("missing", "dir", "file", "ldir", "lfile", "ldangling", "missing/deeper", "file/deeper"): + p = FX + "/sl/" + name + "/" + print(" ", name + "/", "stat", call(lambda: mode_of(os.stat(p))), "lstat", call(lambda: stat.S_IFMT(os.lstat(p).st_mode)), + "access", call(lambda: os.access(p, os.F_OK)), "open", call(lambda: os.close(os.open(p, os.O_RDONLY))), + "realpath", realpath(p)) + t = lambda label, fn: print(f" {label}: {call(fn)}") + t("create missing/", lambda: os.close(os.open(FX + "/sl/new/", os.O_CREAT | os.O_WRONLY, 0o644))) + t("create dir/", lambda: os.close(os.open(FX + "/sl/dir/", os.O_CREAT | os.O_WRONLY, 0o644))) + t("mkdir new/", lambda: os.mkdir(FX + "/sl/made/")) + t("made", lambda: mode_of(os.stat(FX + "/sl/made"))) + t("mkdir file/", lambda: os.mkdir(FX + "/sl/file/")) + t("rmdir made/", lambda: os.rmdir(FX + "/sl/made/")) + t("unlink file/", lambda: os.unlink(FX + "/sl/file/")) + s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + t("bind sock/", lambda: s.bind(FX + "/sl/sock/")) + t("connect missing/", lambda: s.connect(FX + "/sl/missing/")) + s.close() + for name in ("empty", "empty2", "full/inner"): + os.makedirs(FX + "/sl/" + name, exist_ok=True) + os.chdir(FX + "/sl/empty") + for p in (".", "./", "..", "../empty/.", "../empty/..", "../empty2/"): + t(f"rmdir {p}", lambda: os.rmdir(p)) + t(f"unlink {p}", lambda: os.unlink(p)) + os.chdir(FX) + for name in ("ldir/", "lfile/", "file/", "missing/", "full/", "full//", "empty//", "full/inner/../"): + t(f"rmdir {name}", lambda: os.rmdir(FX + "/sl/" + name)) + r, w = os.pipe() + t("stat at a pipe", lambda: os.stat("x", dir_fd=r)) + t("access at a pipe", lambda: os.access("x", os.F_OK, dir_fd=r)) + t("open at a pipe", lambda: os.open("x", os.O_RDONLY, dir_fd=r)) + t("mkdir at a pipe", lambda: os.mkdir("x", dir_fd=r)) + t("readlink at a pipe", lambda: os.readlink("x", dir_fd=r)) + os.close(r) + os.close(w) + t("listing", lambda: sorted(os.listdir(FX + "/sl"))) + os.symlink("/usr/local/lib/directaudio/linux-wine11/lib/wine/aarch64-windows/winedirectaudio.drv", FX + "/sl/long") + os.symlink(ROOT + "/usr/local/lib/directaudio/linux-wine11/lib/wine/aarch64-windows/x.drv", FX + "/sl/longhost") + os.symlink(ROOT + "x/y", FX + "/sl/rootprefix") + for name in ("long", "longhost", "rootprefix"): + p = FX + "/sl/" + name + print(" ", name, " ".join(f"{n}:{raw_readlink(p, n)}" for n in (1, 2, len(ROOT), len(ROOT) + 1, len(ROOT) + 2, 30, 64, 128))) + + +def accept_section(): + print("== accept") + path = "\0fp-equiv3-listener" + srv = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + srv.bind(path) + srv.listen(4) + named = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + named.bind("\0fp-equiv3-client") + named.connect(path) + plain = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + plain.connect(path) + for label in ("named", "plain"): + c, addr = srv.accept() + print(" ", label, "blocking", srv.getblocking(), "addr", repr(addr), "name", repr(c.getsockname()), + "peer", call(c.getpeername)) + c.close() + srv.setblocking(False) + print(" none waiting", call(srv.accept)) + for x in (srv, named, plain): + x.close() + + +def deep_section(): + print("== long host paths") + os.makedirs(DEEP) + t = lambda label, fn: print(f" {label}: {call(fn)}") + t("stat missing", lambda: os.stat(DEEP + "/missing.dll")) + t("access missing", lambda: os.access(DEEP + "/missing.dll", os.F_OK)) + t("create", lambda: os.close(os.open(DEEP + "/new", os.O_CREAT | os.O_WRONLY, 0o644))) + t("mkdir", lambda: os.mkdir(DEEP + "/sub")) + t("symlink", lambda: os.symlink("new", DEEP + "/sub/link")) + t("stat through", lambda: mode_of(os.stat(DEEP + "/sub/link"))) + t("readlink", lambda: os.readlink(DEEP + "/sub/link")) + t("rename", lambda: os.rename(DEEP + "/new", DEEP + "/renamed")) + t("unlink missing", lambda: os.unlink(DEEP + "/new")) + t("unlink", lambda: os.unlink(DEEP + "/renamed")) + t("realpath", lambda: realpath(DEEP + "/sub/../sub/link")) + t("listing", lambda: sorted(os.listdir(DEEP))) + + +def glue_section(): + print("== glue above a binding") + for p in ("/cache", "/cache/fp", "/cache/fp/perf", "/cache/fp/other", "/cache/other", "/cache/fp/perf/x", + "/cache/fp/.", "/cache/fp/..", "/cache/fp/../fp/perf", "/cache/fp/./perf", "/cache/fp/", "/cache/fp/perf/", + "/sys/class/..", "/sys/class/../class", "/proc/self/..", "/opt/fpx/.."): + print(" ", p, "stat", call(lambda: mode_of(os.stat(p))), "read", call(lambda: open(p, "rb").read(4)), + "realpath", realpath(p)) + + +def shm_section(): + print("== shm_open") + + def shm(name, flags, mode=0o600): + ctypes.set_errno(0) + fd = libc.shm_open(name, flags, mode) + if fd < 0: + return err() + st = os.fstat(fd) + os.close(fd) + return (stat.S_IFMT(st.st_mode), oct(st.st_mode & 0o777), st.st_size) + + def unlink(name): + ctypes.set_errno(0) + return "ok" if libc.shm_unlink(name) == 0 else err() + + os.makedirs("/dev/shm/fp-dir", exist_ok=True) + rw, creat, excl = os.O_RDWR, os.O_CREAT, os.O_EXCL + for label, args in [("create", (b"/fp-eq", rw | creat)), ("again", (b"fp-eq", rw)), ("excl", (b"/fp-eq", rw | creat | excl)), + ("slashes", (b"///fp-eq", os.O_RDONLY)), ("missing", (b"/fp-none", rw)), ("empty", (b"", rw)), + ("only slash", (b"/", rw)), ("inner slash", (b"/a/b", rw | creat)), ("dir", (b"fp-dir", os.O_RDONLY)), + ("long", (b"x" * 300, rw | creat))]: + print(" ", label, shm(*args)) + print(" unlink", unlink(b"/fp-eq"), "again", unlink(b"/fp-eq"), "bad", unlink(b"a/b"), "dir", unlink(b"fp-dir")) + os.rmdir("/dev/shm/fp-dir") + + +def uname_section(): + print("== uname") + print(" uname", tuple(os.uname()), "again", tuple(os.uname()) == tuple(os.uname())) + print(" hostname", socket.gethostname()) + for n in (0, 1, 4, 64, 65, 300): + buf = ctypes.create_string_buffer(b"#" * (n + 1), n + 1) + ctypes.set_errno(0) + r = libc.gethostname(buf, n) + print(f" gethostname {n}", r, err() if r else "", buf.raw[: n + 1]) + sys.stdout.flush() + pid = os.fork() + if pid == 0: + print(" child", tuple(os.uname()) == tuple(os.uname()), socket.gethostname(), flush=True) + os._exit(0) + os.waitpid(pid, 0) + + +def isatty_section(): + print("== isatty") + libc.isatty.argtypes = [ctypes.c_int] + r, w = os.pipe() + fds = {"pipe": r, "dev null": os.open("/dev/null", os.O_RDONLY), "file": os.open("/etc/os-release", os.O_RDONLY), + "dir": os.open("/", os.O_RDONLY), "socket": socket.socket().detach(), "closed": 999, "negative": -1} + try: + m, sl = os.openpty() + fds.update({"pty master": m, "pty slave": sl}) + except OSError as e: + print(" openpty", errno.errorcode.get(e.errno)) + for label, fd in fds.items(): + ctypes.set_errno(0) + print(" ", label, libc.isatty(fd), err(), "os", call(lambda: os.isatty(fd))) + for fd in fds.values(): + try: + os.close(fd) + except OSError: + pass + os.close(w) + + +PASSWD = """# comment + +root:x:0:0:root:/root:/bin/bash +alice:x:1000:1000:Alice A,,,:/home/alice:/bin/sh +bob:*:1001:100::/home/bob: +""" +PASSWD_VARIANTS = { + "plain": ("passwd: files systemd\ngroup: files\n", PASSWD), + "extra colon": ("passwd: files systemd\n", PASSWD + "carol:x:1002:1002::/home/carol:/bin/sh:extra\n"), + "bad uid": ("passwd: files\n", "bad:x:abc:1::/:/bin/sh\n" + PASSWD + "dave:x:1003:1003::/:/bin/sh"), + "big uid": ("passwd: files\n", PASSWD + "eve:x:0010:4294967296::/:/bin/sh\n"), + "compat": ("passwd: files\n", "+@staff\n-bob\n" + PASSWD), + "leading space": ("passwd: files\n", " root:x:0:0:root:/root:/bin/bash\n" + PASSWD), + "duplicate": ("passwd: files\n", "alice:x:1001:1:dup:/:/bin/false\n" + PASSWD), + "cr": ("passwd: files\n", PASSWD.replace("\n", "\r\n")), + "action": ("passwd: files [NOTFOUND=return] systemd\n", PASSWD), + "files second": ("passwd: systemd files\n", PASSWD), + "comment": ("passwd: files # local\n", PASSWD), + "none": ("group: files\n", PASSWD), + "two lines": ("passwd: files\npasswd: systemd\n", PASSWD), +} + + +class Passwd(ctypes.Structure): + _fields_ = [("name", ctypes.c_char_p), ("passwd", ctypes.c_char_p), ("uid", ctypes.c_uint), + ("gid", ctypes.c_uint), ("gecos", ctypes.c_char_p), ("dir", ctypes.c_char_p), + ("shell", ctypes.c_char_p)] + + +def lookup(fn): + try: + return repr(tuple(fn())) + except (KeyError, OSError) as e: + return type(e).__name__ + + +def show_pw(p): + return "NULL" if not p else repr(tuple(getattr(p.contents, f) for f, _ in Passwd._fields_)) + + +def passwd_probe(): + import pwd + libc.getpwnam.restype = libc.getpwuid.restype = ctypes.POINTER(Passwd) + libc.getpwnam.argtypes = [ctypes.c_char_p] + libc.getpwuid.argtypes = [ctypes.c_uint] + names = ["root", "alice", "bob", "carol", "dave", "eve", "nobody", "+@staff", "-bob", ""] + uids = [0, 10, 100, 1000, 1001, 1002, 1003, 65534, 4294967295] + for n in names: + ctypes.set_errno(0) + r = libc.getpwnam(n.encode()) + print(f" getpwnam {n!r}", show_pw(r), err(), lookup(lambda: pwd.getpwnam(n)) if n else "") + for u in uids: + ctypes.set_errno(0) + r = libc.getpwuid(u) + print(f" getpwuid {u}", show_pw(r), err(), lookup(lambda: pwd.getpwuid(u))) + a = libc.getpwnam(b"root") + b = libc.getpwnam(b"alice") + print(" static reused", bool(a) and bool(b) and ctypes.addressof(a.contents) == ctypes.addressof(b.contents), + show_pw(a)) + for fn, key in ((libc.getpwnam_r, b"alice"), (libc.getpwuid_r, 1000), (libc.getpwnam_r, b"nobody")): + for size in (1, 8, 40, 46, 47, 48, 49, 50, 64, 1024): + pw, res, buf = Passwd(), ctypes.POINTER(Passwd)(), ctypes.create_string_buffer(size) + ctypes.set_errno(0) + r = fn(key, ctypes.byref(pw), buf, size, ctypes.byref(res)) + print(f" {fn.__name__} {key!r} {size}", errno.errorcode.get(r, r), err(), show_pw(res)) + sys.stdout.flush() + + +def passwd_section(): + print("== passwd") + saved = {} + for f in ("/etc/passwd", "/etc/nsswitch.conf"): + try: + saved[f] = open(f).read() + except OSError: + saved[f] = None + try: + for label, (conf, passwd) in PASSWD_VARIANTS.items(): + print(" ", label, flush=True) + open("/etc/nsswitch.conf", "w").write(conf) + open("/etc/passwd", "w").write(passwd) + pid = os.fork() + if pid == 0: + os.execv(sys.executable, [sys.executable, __file__, "passwd"]) + os.waitpid(pid, 0) + finally: + for f, text in saved.items(): + if text is None: + os.unlink(f) + else: + open(f, "w").write(text) + + +def fortify_section(): + print("== fortify") + os.chdir(FX + "/dir") + buf = ctypes.create_string_buffer(256) + ctypes.set_errno(0) + r = libc.__getcwd_chk(buf, 256, 256) + print(" getcwd_chk", norm(r.decode()) if r else err()) + ctypes.set_errno(0) + r = libc.__getcwd_chk(buf, 4, 256) + print(" getcwd_chk small", norm(r.decode()) if r else err()) + d = os.open(FX, os.O_RDONLY) + print(" readlinkat_chk", libc.__readlinkat_chk(d, b"mtab", buf, 256, 256), norm(buf.value)) + os.close(d) + os.chdir("/") + + +def main(): + shutil.rmtree(FX, ignore_errors=True) + os.makedirs(FX) + os.chdir(FX) + proc_section() + host_text_section() + shared_section() + unseen_section() + slash_section() + accept_section() + deep_section() + glue_section() + shm_section() + uname_section() + isatty_section() + passwd_section() + fortify_section() + shutil.rmtree(FX, ignore_errors=True) + + +if __name__ == "__main__": + passwd_probe() if sys.argv[1:] == ["passwd"] else main() diff --git a/tools/proot/bench/fpbench.c b/tools/proot/bench/fpbench.c new file mode 100644 index 0000000000..c9b42d8886 --- /dev/null +++ b/tools/proot/bench/fpbench.c @@ -0,0 +1,119 @@ +/* The calls the fast path took over from proot, timed: run under fpoff and fastpath and compare. + * Usage: fpbench [iterations]. Builds a Wine-like prefix there. */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static double now(void) { + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return t.tv_sec * 1e9 + t.tv_nsec; +} + +#define BENCH(name, n, body) \ + do { \ + for (int _w = 0; _w < (n) / 10 + 1; _w++) { body; } \ + double _t0 = now(); \ + for (int _i = 0; _i < (n); _i++) { body; } \ + printf("%-34s %10.0f ns\n", name, (now() - _t0) / (n)); \ + fflush(stdout); \ + } while (0) + +int main(int argc, char **argv) { + const char *d = argc > 1 ? argv[1] : "/root/fpb"; + int n = argc > 2 ? atoi(argv[2]) : 5000; + char p[PATH_MAX], q[2 * PATH_MAX], f[PATH_MAX], dos[PATH_MAX], buf[PATH_MAX]; + struct stat st; + const char *dirs[] = {"", "/pfx", "/pfx/drive_c", "/pfx/drive_c/windows", "/pfx/drive_c/windows/system32", "/pfx/dosdevices"}; + for (size_t i = 0; i < sizeof dirs / sizeof *dirs; i++) { + snprintf(p, sizeof p, "%s%s", d, dirs[i]); + mkdir(p, 0755); + } + snprintf(p, sizeof p, "%s/pfx/drive_c/windows/system32/kernel32.dll", d); + close(open(p, O_CREAT | O_WRONLY, 0644)); + snprintf(p, sizeof p, "%s/pfx/dosdevices/c:", d); + unlink(p); + if (symlink("../drive_c", p) != 0) return 1; + snprintf(p, sizeof p, "%s/pfx/dosdevices/z:", d); + unlink(p); + if (symlink("/", p) != 0) return 1; + snprintf(dos, sizeof dos, "%s/pfx/dosdevices/c:/windows/system32/kernel32.dll", d); + snprintf(f, sizeof f, "%s/pfx/drive_c/windows/system32/kernel32.dll", d); + snprintf(q, sizeof q, "%s/pfx/dosdevices/z:%s", d, f); + + BENCH("stat plain path", n, stat(f, &st)); + BENCH("stat through c: link", n, stat(dos, &st)); + BENCH("stat through z: link", n, stat(q, &st)); + BENCH("open+close through c:", n, close(open(dos, O_RDONLY))); + BENCH("stat missing through c:", n, stat("/root/fpb/pfx/dosdevices/c:/windows/nope.dll", &st)); + BENCH("realpath through c:", n, if (!realpath(dos, buf)) return 1); + BENCH("realpath plain", n, if (!realpath(f, buf)) return 1); + BENCH("getcwd", n, if (!getcwd(buf, sizeof buf)) return 1); + BENCH("fstat", n, { int fd = open(f, O_RDONLY); fstat(fd, &st); close(fd); }); + BENCH("faccessat AT_EACCESS", n, (void)!faccessat(AT_FDCWD, f, R_OK, AT_EACCESS)); + BENCH("getxattr through c:", n, getxattr(dos, "user.DOSATTRIB", buf, sizeof buf)); + BENCH("statvfs", n, { struct statvfs v; statvfs(f, &v); }); + snprintf(p, sizeof p, "%s/w", d); + snprintf(q, sizeof q, "%s/w2", d); + BENCH("mkdir+rmdir", n / 2, { mkdir(p, 0755); rmdir(p); }); + BENCH("create+rename+unlink", n / 2, { close(open(p, O_CREAT | O_WRONLY, 0644)); rename(p, q); unlink(q); }); + BENCH("symlink+unlink", n / 2, { (void)!symlink("x", p); unlink(p); }); + BENCH("utimensat", n, utimensat(AT_FDCWD, f, NULL, 0)); + BENCH("sysconf NPROCESSORS_ONLN", n, sysconf(_SC_NPROCESSORS_ONLN)); + int held = open(f, O_RDONLY); + char fdlink[64]; + snprintf(fdlink, sizeof fdlink, "/proc/self/fd/%d", held); + BENCH("readlink /proc/self/fd/N", n, (void)!readlink(fdlink, buf, sizeof buf)); + BENCH("realpath /proc/self/fd/N", n, if (!realpath(fdlink, buf)) return 1); + BENCH("realpath /proc/self/exe", n, if (!realpath("/proc/self/exe", buf)) return 1); + BENCH("open+close /proc/self/fd/N", n, close(open(fdlink, O_RDONLY))); + BENCH("open+close /proc/mounts", n / 4, close(open("/proc/mounts", O_RDONLY))); + BENCH("access /proc/1/fd/0 (EACCES)", n, (void)!access("/proc/1/fd/0", F_OK)); + snprintf(p, sizeof p, "%s/pfx/drive_c/windows/nope/", d); + BENCH("stat missing dir/", n, stat(p, &st)); + BENCH("open+close /proc/self/status", n, close(open("/proc/self/status", O_RDONLY))); + close(held); + BENCH("shm_open+close", n, close(shm_open("/fpbench", O_RDWR | O_CREAT, 0600))); + shm_unlink("/fpbench"); + snprintf(p, sizeof p, "%s/pfx/drive_c/windows/system32/a-name-long-enough-to-push-the-host-path-past-sun_path.dll", d); + BENCH("stat missing, long host path", n, stat(p, &st)); + BENCH("create+unlink, long host path", n / 2, { close(open(p, O_CREAT | O_WRONLY, 0644)); unlink(p); }); + { + int l = socket(AF_UNIX, SOCK_STREAM, 0); + struct sockaddr_un a = { .sun_family = AF_UNIX }; + memcpy(a.sun_path, "\0fpbench-listener", 18); + if (bind(l, (struct sockaddr *)&a, sizeof a) != 0 || listen(l, 64) != 0) { perror("listener"); return 1; } + BENCH("connect+accept unix (blocking)", n / 4, { + int c = socket(AF_UNIX, SOCK_STREAM, 0); + connect(c, (struct sockaddr *)&a, sizeof a); + close(accept(l, NULL, NULL)); + close(c); + }); + close(l); + } + BENCH("connect abstract unix", n / 4, { + int s = socket(AF_UNIX, SOCK_STREAM, 0); + struct sockaddr_un a = { .sun_family = AF_UNIX }; + memcpy(a.sun_path, "\0fpbench-none", 13); + connect(s, (struct sockaddr *)&a, offsetof(struct sockaddr_un, sun_path) + 13); + close(s); + }); + BENCH("getpwuid", n / 4, getpwuid(0)); + BENCH("uname", n, { struct utsname u; uname(&u); }); + BENCH("isatty", n, isatty(0)); + return 0; +} diff --git a/tools/proot/bench/run-device.sh b/tools/proot/bench/run-device.sh index 058780a0b1..20333318fb 100644 --- a/tools/proot/bench/run-device.sh +++ b/tools/proot/bench/run-device.sh @@ -13,7 +13,7 @@ D=/data/local/tmp/dd V=$1; shift U=$(id -u) REL="\\Linux\\DroidDeck\\$(uname -r)\\$(uname -v)\\aarch64\\localdomain\\-1\\" -BINDS="-b /dev -b /proc -b /sys -b /dev/urandom:/dev/random -b /proc/self/fd:/dev/fd -b /proc/self/fd/0:/dev/stdin -b /proc/self/fd/1:/dev/stdout -b /proc/self/fd/2:/dev/stderr -b $D/tmp:/dev/shm -b $D -b /sdcard -b /data/local/tmp/dd/mini/etc/os-release:/proc/version -b $D/mini/etc/os-release:/proc/loadavg -b $D/mini/etc/os-release:/proc/uptime -b /sys/class/thermal/thermal_zone0/temp:/sys/class/thermal/thermal_zone28/temp -b $D/tmp:/mnt/droiddeck-sd -b $D/tmp:/mnt/droiddeck" +BINDS="-b /dev -b /proc -b /sys -b /dev/urandom:/dev/random -b /proc/self/fd:/dev/fd -b /proc/self/fd/0:/dev/stdin -b /proc/self/fd/1:/dev/stdout -b /proc/self/fd/2:/dev/stderr -b $D/tmp:/dev/shm -b $D -b /sdcard -b /data/local/tmp/dd/mini/etc/os-release:/proc/version -b $D/mini/etc/os-release:/proc/loadavg -b $D/mini/etc/os-release:/proc/uptime -b /sys/class/thermal/thermal_zone0/temp:/sys/class/thermal/thermal_zone28/temp -b $D/tmp:/mnt/droiddeck-sd -b $D/tmp:/mnt/droiddeck -b /cache -b $D/mini/etc/os-release:/cache/fp/perf -b $D/fpx:/opt/fpx" export PROOT_TMP_DIR=$D/tmp case $V in patched) export PROOT_LOADER=$D/loader; exec $D/proot --kill-on-exit --kernel-release="$REL" -i $U:$U -r $D/mini -w /root $BINDS /usr/bin/env -i HOME=/root PATH=/usr/bin LANG=C.UTF-8 "$@" ;; diff --git a/tools/proot/fastpath/audit.c b/tools/proot/fastpath/audit.c new file mode 100644 index 0000000000..01628712e2 --- /dev/null +++ b/tools/proot/fastpath/audit.c @@ -0,0 +1,386 @@ +/* + * libblaudit: an LD_AUDIT module that keeps ld.so from asking proot about libraries that are not + * there. ld.so finds a library by trying to open it in each directory of its search path in turn, + * and under proot every one of those opens is a round trip through the tracer. A game's environment + * puts the game's own directory, Steam's runtime and video directories and Proton's library + * directories ahead of /usr/lib, so most of the opens of every Wine process fail. + * + * la_objsearch() is asked about each candidate before ld.so opens it. One that provably does not + * exist - its parent directory is reached through no symlink, so proot would walk the same path, + * and the name itself is missing there - is skipped. Everything else is left to ld.so, which then + * behaves exactly as it would have. + * + * Built without libc: an audit module is loaded into a link namespace of its own, and a libc there + * would be searched for along that same path. Its calls go through the trampoline page of + * libblfastpath.so, mapped here first (libblfastpath finds it in place), and only in a process traced + * by the proot the environment describes (PROOT_FP_KEY, as libblfastpath checks it). + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define FP_STUB_ADDR 0xffff00000UL /* as in fastpath.c */ +#define FP_STUB_SIZE 4096UL +#define MAX_BINDS 256 +#define PATH 4096 +#define VERIFIED 32 +#define TTL_NS 2000000000LL + +typedef long (*stub_fn)(long, long, long, long, long, long, long); +static stub_fn sys; + +static long sc(long nr, long a, long b, long c, long d) { return sys(nr, a, b, c, d, 0, 0); } + +/* A syscall of its own, before the trampoline exists: only mmap, mprotect and munmap, which proot + * does not stop. */ +static long raw(long nr, long a, long b, long c, long d) { + register long x8 __asm__("x8") = nr; + register long x0 __asm__("x0") = a; + register long x1 __asm__("x1") = b; + register long x2 __asm__("x2") = c; + register long x3 __asm__("x3") = d; + register long x4 __asm__("x4") = -1; + register long x5 __asm__("x5") = 0; + __asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5) : "memory"); + return x0; +} + +/* What __builtin___clear_cache does on aarch64, without libgcc. */ +static void sync_code(char *b, char *e) { + for (char *p = b; p < e; p += 4) __asm__ volatile("dc cvau, %0" : : "r"(p) : "memory"); + __asm__ volatile("dsb ish" : : : "memory"); + for (char *p = b; p < e; p += 4) __asm__ volatile("ic ivau, %0" : : "r"(p) : "memory"); + __asm__ volatile("dsb ish\n\tisb" : : : "memory"); +} + +static size_t len_of(const char *s) { + size_t n = 0; + while (s[n]) n++; + return n; +} + +static int same(const char *a, const char *b, size_t n) { + for (size_t i = 0; i < n; i++) + if (a[i] != b[i]) return 0; + return 1; +} + +static void copy(char *d, const char *s, size_t n) { + for (size_t i = 0; i < n; i++) d[i] = s[i]; +} + +static int under(const char *path, const char *dir, size_t dlen) { + return same(path, dir, dlen) && (path[dlen] == 0 || path[dlen] == '/'); +} + +/* ---------------------------------------------------------------- setup */ + +static char env[65536], tracer_env[32768]; +static size_t env_len; +static const char *root; +static size_t rootlen; +static char root_canon[PATH]; +struct bind { const char *host; size_t hlen; const char *guest; size_t glen; int state; char canon[PATH]; }; +static struct bind binds[MAX_BINDS]; +static int nbinds; +static int ready; + +static long slurp(const char *path, char *buf, size_t size) { + long fd = sc(SYS_openat, AT_FDCWD, (long)path, O_RDONLY | O_CLOEXEC, 0); + if (fd < 0) return -1; + size_t have = 0; + for (long r; have < size - 1 && (r = sc(SYS_read, fd, (long)buf + have, size - 1 - have, 0)) > 0;) have += r; + sc(SYS_close, fd, 0, 0, 0); + buf[have] = 0; + return (long)have; +} + +static const char *var(const char *buf, size_t len, const char *name) { + size_t n = len_of(name); + for (size_t i = 0; i < len; i += len_of(buf + i) + 1) + if (same(buf + i, name, n) && buf[i + n] == '=') return buf + i + n + 1; + return NULL; +} + +/* The canonical form of a host path, as proot's realpath() made it; 0 when it has none. */ +static int canonical(const char *path, char *out) { + long fd = sc(SYS_openat, AT_FDCWD, (long)path, O_PATH | O_CLOEXEC, 0); + if (fd < 0) return 0; + char link[40] = "/proc/self/fd/"; + char digits[20]; + int nd = 0; + for (long v = fd; nd == 0 || v; v /= 10) digits[nd++] = '0' + v % 10; + for (int i = 0; i < nd; i++) link[14 + i] = digits[nd - 1 - i]; + link[14 + nd] = 0; + long n = sc(SYS_readlinkat, AT_FDCWD, (long)link, (long)out, PATH - 1); + sc(SYS_close, fd, 0, 0, 0); + if (n <= 0 || out[0] != '/') return 0; + out[n] = 0; + return 1; +} + +static int traced_by_match(const char *key) { + char status[4096], path[48] = "/proc/"; + long n = slurp("/proc/self/status", status, sizeof status); + const char *line = NULL; + for (long i = 0; n > 0 && i < n; i++) + if (status[i] == '\n' && same(status + i + 1, "TracerPid:", 10)) { line = status + i + 11; break; } + if (!line) return 0; + while (*line == ' ' || *line == '\t') line++; + size_t d = 0; + while (line[d] >= '0' && line[d] <= '9' && d < 16) d++; + if (d == 0 || (d == 1 && line[0] == '0')) return 0; + copy(path + 6, line, d); + copy(path + 6 + d, "/environ", 9); + long have = slurp(path, tracer_env, sizeof tracer_env); + if (have <= 0 || var(tracer_env, have, "PROOT_NO_SECCOMP")) return 0; + const char *theirs = var(tracer_env, have, "PROOT_FASTPATH"); + return theirs && len_of(theirs) == len_of(key) && same(theirs, key, len_of(key)); +} + +static void setup(void) { + long p = raw(SYS_mmap, FP_STUB_ADDR, FP_STUB_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE); + /* Loaded before anything else maps, so whatever is already there is not the trampoline. */ + if (p != (long)FP_STUB_ADDR) { + if (p > 0) raw(SYS_munmap, p, FP_STUB_SIZE, 0, 0); + return; + } + static const uint32_t code[] = { + 0xAA0003E8, 0xAA0103E0, 0xAA0203E1, 0xAA0303E2, 0xAA0403E3, 0xAA0503E4, 0xAA0603E5, + 0xD4000001, 0xD65F03C0, /* mov x8,x0; mov x0..x5,x1..x6; svc #0; ret */ + }; + copy((char *)p, (const char *)code, sizeof code); + sync_code((char *)p, (char *)p + sizeof code); + if (raw(SYS_mprotect, FP_STUB_ADDR, FP_STUB_SIZE, PROT_READ | PROT_EXEC, 0) != 0) { + raw(SYS_munmap, FP_STUB_ADDR, FP_STUB_SIZE, 0, 0); + return; + } + sys = (stub_fn)FP_STUB_ADDR; + long n = slurp("/proc/self/environ", env, sizeof env); + if (n <= 0 || (size_t)n >= sizeof env - 1) return; + env_len = n; + const char *key = var(env, env_len, "PROOT_FP_KEY"), *b = var(env, env_len, "PROOT_FP_BINDS"); + root = var(env, env_len, "PROOT_FP_ROOT"); + if (!key || !*key || !root || !*root || var(env, env_len, "PROOT_FP_OFF") || !traced_by_match(key)) return; + if (!canonical(root, root_canon)) return; + root = root_canon; + rootlen = len_of(root); + if (rootlen <= 1) return; + /* "host[:guest]|..." taken apart in place; a list this cannot hold leaves everything to ld.so. */ + for (char *s = (char *)b; s && *s;) { + if (nbinds == MAX_BINDS) return; + char *end = s; + while (*end && *end != '|') end++; + char *colon = s; + while (colon < end && *colon != ':') colon++; + struct bind *bd = &binds[nbinds]; + bd->host = s; + bd->hlen = colon - s; + bd->guest = colon < end ? colon + 1 : s; + bd->glen = colon < end ? (size_t)(end - colon - 1) : bd->hlen; + if (bd->guest[0] == '/' && bd->glen > 1 && bd->hlen > 0) nbinds++; + int last = *end == 0; + *end = 0; + if (colon < end) *colon = 0; + if (last) break; + s = end + 1; + } + ready = 1; +} + +/* ---------------------------------------------------------------- lookups */ + +/* The host path proot binds there, as it canonicalised it: NULL for one it dropped (or /proc/self). */ +static const char *bind_host(struct bind *bd) { + if (!bd->state) { + bd->state = under(bd->host, "/proc/self", 10) || !canonical(bd->host, bd->canon) ? 2 : 1; + } + return bd->state == 1 ? bd->canon : NULL; +} + +static long long now(void) { + struct timespec ts; + if (sc(SYS_clock_gettime, CLOCK_MONOTONIC_COARSE, (long)&ts, 0, 0) != 0) return 0; + return ts.tv_sec * 1000000000LL + ts.tv_nsec; +} + +/* The host path proot gives a canonical guest path: under the longest binding over it (the last of + * two for the same path), else the rootfs. 0 for a binding proot dropped, or no room. */ +static int to_host(const char *g, char *host, int *bound) { + size_t n = len_of(g); + struct bind *bd = NULL; + for (int i = 0; i < nbinds; i++) + if (under(g, binds[i].guest, binds[i].glen) && (!bd || binds[i].glen >= bd->glen)) bd = &binds[i]; + const char *base = root, *rest = g; + if (bd) { + base = bind_host(bd); + if (!base) return 0; + rest = g + bd->glen; + } else if (n == 1) { + rest = ""; + } + size_t blen = len_of(base), rlen = len_of(rest); + if (blen + rlen >= PATH) return 0; + copy(host, base, blen); + copy(host + blen, rest, rlen + 1); + *bound = bd != NULL; + return 1; +} + +/* Under a directory proot or the kernel answers for in its own way. */ +static int special(const char *g) { + return under(g, "/proc", 5) || under(g, "/dev", 4) || under(g, "/sys", 4); +} + +/* A guest path proot only shows as glue, or one with a binding below it. */ +static int above_binding(const char *g) { + size_t n = len_of(g); + for (int i = 0; i < nbinds; i++) + if (binds[i].glen > n && under(binds[i].guest, g, n)) return 1; + return 0; +} + +/* + * The canonical guest path of the directory `g`, walked as canonicalize() walks it: a symlink in the + * rootfs is followed in guest terms, its text losing the rootfs prefix as detranslate_path() has it. + * -1 when a component is missing, so proot fails the path with ENOENT. Anything this cannot follow + * exactly (a link inside a binding, glue, a component not a directory) gives 0. + */ +static int canonical_dir(const char *g, char *out) { + char path[PATH], next[PATH], host[PATH], text[PATH]; + char st_buf[256] __attribute__((aligned(16))); + struct stat *st = (struct stat *)st_buf; + size_t glen = len_of(g), olen = 0; + int links = 0, bound; + if (glen >= PATH) return 0; + copy(path, g, glen + 1); + out[0] = '/'; + out[1] = 0; + for (char *c = path;;) { + while (*c == '/') c++; + if (!*c) break; + char *e = c; + while (*e && *e != '/') e++; + size_t n = e - c; + if (n == 1 && c[0] == '.') { c = e; continue; } + if (n == 2 && c[0] == '.' && c[1] == '.') { + while (olen > 0 && out[olen] != '/') olen--; + out[olen ? olen : 1] = 0; + c = e; + continue; + } + if (olen + n + 2 >= PATH) return 0; + out[olen] = '/'; + copy(out + olen + 1, c, n); + out[olen + 1 + n] = 0; + if (!to_host(out, host, &bound)) return 0; + long r = sc(SYS_newfstatat, AT_FDCWD, (long)host, (long)st, AT_SYMLINK_NOFOLLOW); + if (r == -ENOENT && !special(out) && !above_binding(out)) return -1; + if (r != 0) return 0; + if (S_ISDIR(st->st_mode)) { + olen += 1 + n; + c = e; + continue; + } + if (!S_ISLNK(st->st_mode) || bound || ++links > 16) return 0; + long t = sc(SYS_readlinkat, AT_FDCWD, (long)host, (long)text, PATH - 1); + if (t <= 0 || t >= PATH - 1) return 0; + text[t] = 0; + const char *guest_text = text; + if (under(text, root, rootlen)) guest_text = text[rootlen] ? text + rootlen : "/"; + size_t tl = len_of(guest_text), rest = len_of(e); + if (tl + rest + 1 >= PATH) return 0; + copy(next, guest_text, tl); + copy(next + tl, e, rest + 1); + copy(path, next, tl + rest + 1); + c = path; + if (guest_text[0] == '/') olen = 0; + out[olen ? olen : 1] = 0; + } + if (olen == 0) out[1] = 0; + return 1; +} + +static struct { char dir[PATH]; char canon[PATH]; long long at; } known[VERIFIED]; +static int next_slot; + +/* canonical_dir(), remembered for a while: ld.so looks in the same few directories for every + * library. */ +static int known_dir(const char *g, char *out) { + size_t n = len_of(g); + long long t = now(); + for (int i = 0; i < VERIFIED; i++) + if (known[i].at && t - known[i].at < TTL_NS && same(known[i].dir, g, n + 1)) { + copy(out, known[i].canon, len_of(known[i].canon) + 1); + return 1; + } + int r = canonical_dir(g, out); + if (r != 1) return r; + copy(known[next_slot].dir, g, n + 1); + copy(known[next_slot].canon, out, len_of(out) + 1); + known[next_slot].at = t; + next_slot = (next_slot + 1) % VERIFIED; + return 1; +} + +/* The guest path certainly names nothing: its directory is one proot reaches the same way, and the + * name is missing there. */ +static int missing(const char *g) { + char dir[PATH], cand[PATH], host[PATH]; + size_t n = len_of(g); + if (g[0] != '/' || n < 2 || n >= PATH - 1) return 0; + size_t slash = n; + while (slash > 0 && g[slash - 1] != '/') slash--; + const char *name = g + slash; + size_t nl = n - slash; + if (nl == 0 || (nl == 1 && name[0] == '.') || (nl == 2 && name[0] == '.' && name[1] == '.')) return 0; + copy(dir, g, slash); + dir[slash > 1 ? slash - 1 : 1] = 0; + int known_as = known_dir(dir, cand); + if (known_as != 1) return known_as < 0; + size_t cl = len_of(cand); + if (cl + nl + 2 >= PATH) return 0; + if (cl > 1) cand[cl++] = '/'; + copy(cand + cl, name, nl + 1); + int bound; + if (special(cand) || above_binding(cand) || !to_host(cand, host, &bound)) + return 0; + char st[256] __attribute__((aligned(16))); + return sc(SYS_newfstatat, AT_FDCWD, (long)host, (long)st, AT_SYMLINK_NOFOLLOW) == -ENOENT; +} + +unsigned int la_version(unsigned int version) { + (void)version; + setup(); + return LAV_CURRENT; +} + +/* + * A candidate skipped here is one whose open "failed" with ld.so's errno as it stands, and ld.so + * gives up on the whole search path unless that is ENOENT or EACCES. So each lookup's first + * candidate is always opened for real: once that has failed, errno says so, and nothing ld.so + * does between candidates changes it unless the search ends anyway. The default directories are + * left alone too: what ld.so meets there is what it reports when a library is nowhere. + */ +static int tried; + +char *la_objsearch(const char *name, uintptr_t *cookie, unsigned int flag) { + (void)cookie; + if (flag == LA_SER_ORIG) { + tried = 0; + return (char *)name; + } + if (!ready || !tried || flag == LA_SER_DEFAULT || !missing(name)) { + tried = 1; + return (char *)name; + } + return NULL; +} diff --git a/tools/proot/fastpath/fastpath.c b/tools/proot/fastpath/fastpath.c index 8720f4d57f..4ac34b8a42 100644 --- a/tools/proot/fastpath/fastpath.c +++ b/tools/proot/fastpath/fastpath.c @@ -7,25 +7,31 @@ * page at a fixed address, which proot's seccomp filter lets through without a stop * (PROOT_FASTPATH, tools/proot/patches/0014). * - * It decides nothing proot would decide differently. The guest path is mapped literally - the - * longest binding whose guest path is a prefix, else the rootfs - and the parent directory is - * opened O_PATH and must come back from /proc/self/fd as exactly that host path, which proves no - * component on the way was a symlink: proot would then have walked the same directories. The - * last component is looked at without following it, and a symlink there goes to proot. So does - * "..", a trailing "/", anything under /proc, any flag not handled here and any failure to - * decide: the real libc call runs, proot traps it and answers as it always has. + * It decides nothing proot would decide differently. A guest path is resolved the way proot's + * canonicalize() does it, one component at a time from the root or the guest directory a + * relative path starts in: each component is mapped through the longest binding whose guest path + * is a prefix of it (else the rootfs) and looked at without following it; a symlink is read and + * its text resolved in its place, ".." drops the last resolved component, and a directory that + * only exists as proot's glue above a binding is walked through. The kernel then gets the host + * path that proot would have given it. In /proc, /proc/self and /proc/thread-self are followed as + * proot follows them, and a link proot emulates is read the way it answers readlink(); anything + * proot keeps state about (a renamed directory, sockets bound at a long path, glue itself), the + * /dev links into /proc, any flag not handled here and any failure to decide go to proot: the real + * libc call runs, proot traps it and answers as it always has. * - * Only syscalls Android's app seccomp policy allows are used (openat, newfstatat, statx, - * faccessat, readlinkat, close) - not openat2 or faccessat2, which it answers with SIGSYS. + * Only syscalls Android's app seccomp policy allows are used - not openat2 or faccessat2, which + * it answers with SIGSYS. * - * Verified parents are remembered for PROOT_FP_TTL_MS (default 2000; 0 = verify every call): a - * parent directory replaced by a symlink within that window would be missed, the same trade the - * session's pathcache.c makes for the Steam client. + * Directories resolved once are remembered for PROOT_FP_TTL_MS (default 2000; 0 = resolve every + * call): a directory replaced by a symlink within that window would be missed, the same trade the + * session's pathcache.c makes for the Steam client. Renames, removals and new symlinks made here + * forget them at once. * * Config (the launcher sets it): PROOT_FP_ROOT=, PROOT_FP_BINDS=; * PROOT_FP_KEY=, with PROOT_FASTPATH= in proot's own environment: nothing is answered * here unless the tracer is that proot (traced_by_match). - * PROOT_FP_OFF=1 disables it; PROOT_FP_STATS=1 reports hits and misses at exit. + * PROOT_FP_OFF=1 disables it; PROOT_FP_STATS=1 reports hits and misses at exit, and + * PROOT_FP_LOG= appends every call left to proot to that file. */ #define _GNU_SOURCE #include @@ -33,18 +39,32 @@ #include #include #include +#include +#include #include #include +#include #include #include #include #include +#include +#include +#include #include +#include #include +#include +#include #include -#include +#include +#include +#include +#include +#include #include #include +#include #define FP_STUB_ADDR 0xffff00000UL /* must match FASTPATH_STUB_ADDR in proot's seccomp.c */ #define FP_STUB_SIZE 4096UL @@ -53,12 +73,14 @@ * guest path into the rootfs instead (an empty mount point), which proot never does. */ #define MAX_BINDS 256 #define FP_SLOW (-100000L) +/* proot gives up at MAXSYMLINKS; a chain this long is left to it, whatever it decides. */ +#define MAX_LINKS 16 typedef long (*stub_fn)(long nr, long a, long b, long c, long d, long e, long f); static stub_fn fp_sys; static int fp_on; -struct bind { char *host; size_t hlen; char *guest; size_t glen; int canon; }; +struct bind { char *host; size_t hlen; char *guest; size_t glen; int canon; int dropped; }; static struct bind binds[MAX_BINDS]; static int nbinds; static char root[PATH_MAX]; @@ -89,17 +111,46 @@ static void stats(void) { if (write(2, line, n) < 0) {} } -/* The canonical form of a host path (bindings may name /sdcard, /data/user/0, ...). */ -static void canonical(char *path, size_t size) { - long fd = sc(SYS_openat, AT_FDCWD, (long)path, O_PATH | O_CLOEXEC, 0, 0); +static const char *miss_log; +static long fp_open(int dirfd, const char *path, int flags, mode_t mode); + +/* A call left to proot. Opened for each line: a program may close or reuse any descriptor kept. */ +static void missed(const char *call, int dirfd, const char *path) { + misses++; + if (!miss_log) return; + char line[2 * PATH_MAX + 128], base[PATH_MAX] = ""; + if (path && path[0] != '/' && dirfd != AT_FDCWD) { + char link[40]; + snprintf(link, sizeof link, "/proc/self/fd/%d", dirfd); + long n = sc(SYS_readlinkat, AT_FDCWD, (long)link, (long)base, sizeof base - 1, 0); + base[n > 0 ? n : 0] = 0; + } + int n = snprintf(line, sizeof line, "%d %s %s %s%s%s\n", getpid(), program_invocation_short_name, call, + base, base[0] ? "/" : "", path ? path : "(null)"); + long fd = fp_open(AT_FDCWD, miss_log, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644); if (fd < 0) return; + sc(SYS_write, fd, (long)line, n < (int)sizeof line ? n : (int)sizeof line - 1, 0, 0); + sc(SYS_close, fd, 0, 0, 0, 0); +} + +/* The canonical form of a host path (bindings may name /sdcard, /data/user/0, ...), as realpath() + * gives it; -errno, leaving the path as it is, when it does not lead to one. */ +static long canonical(char *path, size_t size) { + long fd = sc(SYS_openat, AT_FDCWD, (long)path, O_PATH | O_CLOEXEC, 0, 0); + if (fd < 0) return fd; char link[40], out[PATH_MAX]; snprintf(link, sizeof link, "/proc/self/fd/%ld", fd); long n = sc(SYS_readlinkat, AT_FDCWD, (long)link, (long)out, sizeof out - 1, 0); sc(SYS_close, fd, 0, 0, 0, 0); - if (n > 0 && (size_t)n < size) { out[n] = 0; memcpy(path, out, n + 1); } + if (n <= 0 || out[0] != '/') return -ENOENT; + if ((size_t)n >= size) return -ENAMETOOLONG; + out[n] = 0; + memcpy(path, out, n + 1); + return 0; } +static long tracer; + static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t bind_lock; static void after_fork(void) { pthread_mutex_init(&cache_lock, NULL); pthread_mutex_init(&bind_lock, NULL); } @@ -122,7 +173,7 @@ static int traced_by_match(const char *key) { buf[n] = 0; const char *line = strstr(buf, "\nTracerPid:"); if (!line) return 0; - long tracer = strtol(line + 11, NULL, 10); + tracer = strtol(line + 11, NULL, 10); if (tracer <= 0) return 0; snprintf(path, sizeof path, "/proc/%ld/environ", tracer); if (snprintf(want, sizeof want, "PROOT_FASTPATH=%s", key) >= (int)sizeof want) return 0; @@ -140,6 +191,23 @@ static int traced_by_match(const char *key) { return found; } +/* + * proot canonicalised every binding's host path with realpath() in its own process, so a binding of + * /proc/self/fd/1 (/dev/stdout) names whatever proot's own descriptor 1 is - /dev/null in a session - + * and one onto a pipe was dropped. Read through the tracer's /proc; 0 when that cannot be seen. + */ +static int in_tracer(struct bind *bd) { + char h[PATH_MAX]; + if (snprintf(h, sizeof h, "/proc/%ld%s", tracer, bd->host + 10) >= (int)sizeof h) return 0; + long e = canonical(h, sizeof h); + if (e == -EACCES || e == -EPERM) return 0; + memcpy(bd->host, h, strlen(h) + 1); + bd->hlen = strlen(bd->host); + bd->dropped = e < 0; + bd->canon = 1; + return 1; +} + static void fp_init(void) { const char *r = getenv("PROOT_FP_ROOT"), *b = getenv("PROOT_FP_BINDS"), *t = getenv("PROOT_FP_TTL_MS"); const char *key = getenv("PROOT_FP_KEY"); @@ -165,6 +233,7 @@ static void fp_init(void) { snprintf(root, sizeof root, "%s", r); canonical(root, sizeof root); rootlen = strlen(root); + if (rootlen <= 1) { fp_sys = NULL; return; } const char *s = b; int unheld = 0; for (; s && *s && nbinds < MAX_BINDS;) { @@ -196,30 +265,169 @@ static void fp_init(void) { s = end ? end + 1 : NULL; } if (unheld || (s && *s)) { fp_sys = NULL; return; } + for (int i = 0; i < nbinds; i++) + if (under(binds[i].host, "/proc/self", 10) && !in_tracer(&binds[i])) { fp_sys = NULL; return; } pthread_atfork(NULL, NULL, after_fork); /* registered once per exec */ fp_on = 1; if (getenv("PROOT_FP_STATS")) atexit(stats); + miss_log = getenv("PROOT_FP_LOG"); + if (miss_log && miss_log[0] != '/') miss_log = NULL; } +/* Whether proot holds a binding: one whose host path did not resolve it dropped ("can't sanitize + * binding"). 1, 0, or -1 when that cannot be told now. */ static pthread_mutex_t bind_lock = PTHREAD_MUTEX_INITIALIZER; -static const struct bind *canon(struct bind *bd) { - if (__atomic_load_n(&bd->canon, __ATOMIC_ACQUIRE)) return bd; - if (pthread_mutex_trylock(&bind_lock) != 0) return NULL; - if (!bd->canon) { - canonical(bd->host, PATH_MAX); - bd->hlen = strlen(bd->host); - __atomic_store_n(&bd->canon, 1, __ATOMIC_RELEASE); +static int held(struct bind *bd) { + if (!__atomic_load_n(&bd->canon, __ATOMIC_ACQUIRE)) { + if (pthread_mutex_trylock(&bind_lock) != 0) return -1; + if (!bd->canon) { + bd->dropped = canonical(bd->host, PATH_MAX) < 0; + bd->hlen = strlen(bd->host); + __atomic_store_n(&bd->canon, 1, __ATOMIC_RELEASE); + } + pthread_mutex_unlock(&bind_lock); } - pthread_mutex_unlock(&bind_lock); - return bd; + return !bd->dropped; } -/* ---------------------------------------------------------------- verified parents */ +/* ---------------------------------------------------------------- guest <-> host */ -#define SLOTS 256 -#define SLOT_PATH 240 /* longer directories are verified every time */ -struct slot { long long stamp; unsigned hash; char path[SLOT_PATH]; }; +/* Guest paths never answered here: bindings onto /proc/self, which proot resolves as its own. */ +static int reserved(const char *g) { + return under(g, "/dev/fd", 7) || under(g, "/dev/stdin", 10) || under(g, "/dev/stdout", 11) + || under(g, "/dev/stderr", 11); +} + +/* The binding a canonical guest path is under (the longest guest prefix; of two for the same guest + * path, proot keeps the last), or NULL for the rootfs. -1 when it cannot be told. */ +static int bind_of(const char *g, struct bind **out) { + struct bind *bd = NULL; + for (int i = 0; i < nbinds; i++) { + if (!under(g, binds[i].guest, binds[i].glen) || (bd && binds[i].glen < bd->glen)) continue; + int h = held(&binds[i]); + if (h < 0) return -1; + if (h) bd = &binds[i]; + } + *out = bd; + return 0; +} + +/* The host path proot gives the kernel for a canonical guest path; -1 when it cannot be told. */ +static int to_host(const char *g, char *host, struct bind **through) { + struct bind *bd; + if (bind_of(g, &bd) < 0) return -1; + if (through) *through = bd; + int n = bd ? snprintf(host, PATH_MAX, "%s%s", bd->host, g + bd->glen) + : snprintf(host, PATH_MAX, "%s%s", root, strcmp(g, "/") ? g : ""); + return n < PATH_MAX ? 0 : -1; +} + +/* A guest directory proot only shows as glue: missing in the rootfs, with a binding below it. + * -1 when it cannot be told. */ +static int glue_above_binding(const char *g, size_t len) { + int glue = 0; + for (int i = 0; i < nbinds && glue == 0; i++) + if (binds[i].glen > len && under(binds[i].guest, g, len)) glue = held(&binds[i]); + return glue; +} + +/* + * The k-th guest path proot could name a canonical host path by (the cwd, a dirfd), as its + * detranslation does: inside the rootfs, the rootfs's view of it, whatever is bound; elsewhere the + * binding with the deepest host path - two bindings of the same host path are two names, and which + * one proot uses depends on its list order. A name must also map back to the path, i.e. no longer + * binding hides it. 0, or -1 when there is no k-th. + */ +static int guest_name(const char *h, int k, char *g) { + char back[PATH_MAX]; + size_t deepest = 0; + if (under(h, root, rootlen)) { + if (k > 0 || snprintf(g, PATH_MAX, "%s", h[rootlen] ? h + rootlen : "/") >= PATH_MAX) return -1; + return reserved(g) || to_host(g, back, NULL) < 0 || strcmp(back, h) != 0 ? -1 : 0; + } + for (int i = 0; i < nbinds; i++) { + int in = held(&binds[i]); + if (in < 0) return -1; + if (in && under(h, binds[i].host, binds[i].hlen) && binds[i].hlen > deepest) deepest = binds[i].hlen; + } + for (int i = 0; deepest && i < nbinds; i++) { + if (binds[i].dropped || binds[i].hlen != deepest || !under(h, binds[i].host, binds[i].hlen)) continue; + if (k-- > 0) continue; + if (snprintf(g, PATH_MAX, "%s%s", binds[i].guest, h + binds[i].hlen) >= PATH_MAX) return -1; + return reserved(g) || to_host(g, back, NULL) < 0 || strcmp(back, h) != 0 ? -1 : 0; + } + return -1; +} + +/* The one guest path that names a host path, or -1. */ +static int to_guest(const char *h, char *g, size_t size) { + char other[PATH_MAX]; + if (size < PATH_MAX || guest_name(h, 0, g) < 0) return -1; + for (int k = 1; guest_name(h, k, other) == 0; k++) + if (strcmp(other, g) != 0) return -1; + return 0; +} + +/* A host path or link text proot shows for a host path it read from a /proc link: in guest terms + * when the rootfs or a binding holds it, else as it is (a pipe, a socket, a path outside the guest). */ +static int guest_text(const char *h, char *g) { + int in = h[0] == '/' && under(h, root, rootlen); + for (int i = 0; h[0] == '/' && !in && i < nbinds; i++) { + int live = held(&binds[i]); + if (live < 0) return -1; + in = live && under(h, binds[i].host, binds[i].hlen); + } + if (in) return to_guest(h, g, PATH_MAX); + memcpy(g, h, strlen(h) + 1); + return 0; +} + +/* The binding proot's get_binding(HOST) finds for a host path outside the rootfs: the deepest host + * prefix. -1 for none, -2 when it cannot be told (two at that depth with different guest paths). */ +static int host_bind(const char *h) { + int found = -1; + for (int i = 0; i < nbinds; i++) { + int live = held(&binds[i]); + if (live < 0) return -2; + if (!live || !under(h, binds[i].host, binds[i].hlen)) continue; + if (found < 0 || binds[i].hlen > binds[found].hlen) found = i; + else if (binds[i].hlen == binds[found].hlen && strcmp(binds[i].guest, binds[found].guest) != 0) found = -3; + } + return found == -3 ? -2 : found; +} + +/* + * proot's detranslate_path() for the text of a link outside /proc whose own host path is `at`: + * text into the binding the link itself is in shows under that binding's guest path, other text + * naming the rootfs loses the rootfs prefix. 1 when rewritten, 0 when not, or FP_SLOW. + */ +static long detranslated(char *text, const char *at) { + if (text[0] != '/') return 0; + if (!under(at, root, rootlen) && !under(text, root, rootlen)) { + int own = host_bind(at), to = host_bind(text); + if (own == -2 || to == -2) return FP_SLOW; + if (own >= 0 && to >= 0 && strcmp(binds[own].host, binds[to].host) == 0) { + const struct bind *bd = &binds[to]; + if (strcmp(bd->host, bd->guest) == 0) return 0; + char g[PATH_MAX]; + if (snprintf(g, sizeof g, "%s%s", bd->guest, text + bd->hlen) >= (int)sizeof g) return FP_SLOW; + strcpy(text, g); + return 1; + } + } + if (!under(text, root, rootlen)) return 0; + if (!text[rootlen]) strcpy(text, "/"); + else memmove(text, text + rootlen, strlen(text + rootlen) + 1); + return 1; +} + +/* ---------------------------------------------------------------- resolved directories */ + +#define SLOTS 512 +#define SLOT_PATH 256 /* longer directories are resolved every time */ +struct slot { long long stamp; unsigned hash; unsigned gen; char literal[SLOT_PATH]; char guest[SLOT_PATH]; }; static struct slot cache[SLOTS]; +static unsigned generation; static long long now_ns(void) { struct timespec ts; @@ -233,143 +441,372 @@ static unsigned hash_of(const char *s) { return h; } -static int cached(const char *dir, unsigned h) { +/* Something here changed which paths name which directories. */ +static void forget(void) { __atomic_add_fetch(&generation, 1, __ATOMIC_RELEASE); } + +static int cached(const char *literal, unsigned h, char *guest) { if (ttl_ns <= 0 || pthread_mutex_trylock(&cache_lock) != 0) return 0; struct slot *e = &cache[h % SLOTS]; - int hit = e->stamp && e->hash == h && now_ns() - e->stamp < ttl_ns && strcmp(e->path, dir) == 0; + int hit = e->stamp && e->hash == h && e->gen == __atomic_load_n(&generation, __ATOMIC_ACQUIRE) + && now_ns() - e->stamp < ttl_ns && strcmp(e->literal, literal) == 0; + if (hit) memcpy(guest, e->guest, strlen(e->guest) + 1); pthread_mutex_unlock(&cache_lock); return hit; } -static void remember(const char *dir, unsigned h) { - if (ttl_ns <= 0 || strlen(dir) >= SLOT_PATH || pthread_mutex_trylock(&cache_lock) != 0) return; +static void remember(const char *literal, unsigned h, const char *guest) { + if (ttl_ns <= 0 || strlen(literal) >= SLOT_PATH || strlen(guest) >= SLOT_PATH) return; + unsigned gen = __atomic_load_n(&generation, __ATOMIC_ACQUIRE); + if (pthread_mutex_trylock(&cache_lock) != 0) return; struct slot *e = &cache[h % SLOTS]; e->hash = h; - snprintf(e->path, sizeof e->path, "%s", dir); + e->gen = gen; + strcpy(e->literal, literal); + strcpy(e->guest, guest); e->stamp = now_ns(); pthread_mutex_unlock(&cache_lock); } -/* The host directory `dir` is reached by the kernel without crossing a symlink. */ -static long canonical_dir(const char *dir) { - unsigned h = hash_of(dir); - if (cached(dir, h)) return 1; - long fd = sc(SYS_openat, AT_FDCWD, (long)dir, O_PATH | O_DIRECTORY | O_CLOEXEC, 0, 0); - if (fd < 0) return fd; - char link[40], out[PATH_MAX]; - snprintf(link, sizeof link, "/proc/self/fd/%ld", fd); - long n = sc(SYS_readlinkat, AT_FDCWD, (long)link, (long)out, sizeof out - 1, 0); - sc(SYS_close, fd, 0, 0, 0, 0); - if (n <= 0) return 0; - out[n] = 0; - if (strcmp(out, dir) != 0) return 0; - remember(dir, h); - return 1; +/* ---------------------------------------------------------------- resolution */ + +/* + * Where a resolution ended: the canonical guest path, its host path and, when it exists, its lstat. + * `libc` asks for links to be read as glibc's realpath() reads them, through readlink; `moving` is + * set once the answer depends on the caller (a /proc link), so it is not remembered. `magic`: it + * ends on /proc//fd/N, which proot leaves for the kernel to follow; st is what it leads to. + */ +struct res { + char guest[PATH_MAX]; + char host[PATH_MAX]; + struct stat st; + int exists; + int named; + int libc; + int moving; + int magic; +}; + +/* A directory removed while open: the kernel names it " (deleted)". */ +static int gone(const char *h, size_t n) { + return n > 10 && strcmp(h + n - 10, " (deleted)") == 0; } -/* ---------------------------------------------------------------- guest -> host */ - -/* Guest paths never answered here: proot emulates /proc (self, exe, fd and cwd links). */ -static int reserved(const char *g) { - return under(g, "/proc", 5) || under(g, "/dev/fd", 7) || under(g, "/dev/stdin", 10) - || under(g, "/dev/stdout", 11) || under(g, "/dev/stderr", 11); +/* The guest cwd proot keeps for this process, as the kernel's cwd names it. */ +static long own_cwd(char *g) { + char h[PATH_MAX]; + long n = sc(SYS_readlinkat, AT_FDCWD, (long)"/proc/self/cwd", (long)h, sizeof h - 1, 0); + if (n <= 0 || h[0] != '/') return FP_SLOW; + h[n] = 0; + if (gone(h, n) || to_guest(h, g, PATH_MAX) < 0) return FP_SLOW; + return (long)strlen(g); } -/* The guest path of a host directory (the cwd, a dirfd): the longest host prefix wins. */ -static int host_to_guest(const char *h, char *g, size_t size) { - const struct bind *best = NULL; - for (int i = 0; i < nbinds; i++) - if (under(h, binds[i].host, binds[i].hlen) && (!best || binds[i].hlen > best->hlen)) best = &binds[i]; - if (under(h, root, rootlen) && (!best || rootlen >= best->hlen)) - return snprintf(g, size, "%s", h[rootlen] ? h + rootlen : "/") < (int)size ? 0 : -1; - if (best) return snprintf(g, size, "%s%s", best->guest, h + best->hlen) < (int)size ? 0 : -1; - return -1; +/* The program proot started this process with, asked of proot once: it is its own record. */ +static char exe_text[PATH_MAX]; +static int exe_len; +static long own_exe(char *g) { + int n = __atomic_load_n(&exe_len, __ATOMIC_ACQUIRE); + if (n > 0) { + memcpy(g, exe_text, n + 1); + return n; + } + long t = syscall(SYS_readlinkat, AT_FDCWD, "/proc/self/exe", g, PATH_MAX - 1); + if (t <= 0 || t >= PATH_MAX - 1) return FP_SLOW; + g[t] = 0; + static pthread_mutex_t exe_lock = PTHREAD_MUTEX_INITIALIZER; + if (pthread_mutex_trylock(&exe_lock) == 0) { + if (!exe_len) { + memcpy(exe_text, g, t + 1); + __atomic_store_n(&exe_len, (int)t, __ATOMIC_RELEASE); + } + pthread_mutex_unlock(&exe_lock); + } + return t; } /* - * The host path `path` (relative to dirfd, as the guest means it) names, whose parent directory - * is verified; 0, or FP_SLOW when proot must decide. + * What proot's readlink() answers for the link r names under /proc (readlink_proc2, then + * detranslate_path): exe, cwd and root of a process it traces from its own records, every other + * link as the kernel has it with a host path put in guest terms. `changed` is set when that differs + * from the kernel's text, which is cut to `size` (0: no limit) before proot reads it. */ -static long resolve(int dirfd, const char *path, char *host) { - char guest[PATH_MAX], base[PATH_MAX]; - if (!path || !path[0] || !fp_ready()) return FP_SLOW; - size_t plen = strlen(path); - if (path[plen - 1] == '/' && plen > 1) return FP_SLOW; - if (path[0] == '/') { - if (plen >= sizeof guest) return FP_SLOW; - memcpy(guest, path, plen + 1); - } else { - char link[40], h[PATH_MAX]; - if (dirfd == AT_FDCWD) snprintf(link, sizeof link, "/proc/self/cwd"); - else snprintf(link, sizeof link, "/proc/self/fd/%d", dirfd); - long n = sc(SYS_readlinkat, AT_FDCWD, (long)link, (long)h, sizeof h - 1, 0); - if (n <= 0) return FP_SLOW; - h[n] = 0; - if (host_to_guest(h, base, sizeof base) < 0) return FP_SLOW; - if (snprintf(guest, sizeof guest, "%s/%s", strcmp(base, "/") ? base : "", path) >= (int)sizeof guest) return FP_SLOW; +static long proc_readlink(const struct res *r, char *text, size_t size, int *changed) { + const char *name = strrchr(r->guest, '/') + 1; + size_t blen = name - 1 - r->guest; + char base[32]; + int pid = blen > 6 ? atoi(r->guest + 6) : 0; + int bn = snprintf(base, sizeof base, "/proc/%d", pid); + if (pid != 0 && (size_t)bn == blen && strncmp(r->guest, base, blen) == 0 + && (!strcmp(name, "exe") || !strcmp(name, "cwd") || !strcmp(name, "root"))) { + if (pid != getpid() || !strcmp(name, "root")) return FP_SLOW; + *changed = 1; + return name[0] == 'e' ? own_exe(text) : own_cwd(text); } - /* Normalise "//" and "/./"; ".." goes to proot (it is not lexical once symlinks are involved). */ - char norm[PATH_MAX]; - size_t o = 0; - for (const char *c = guest; *c;) { - while (*c == '/') c++; - if (!*c) break; - const char *e = strchrnul(c, '/'); - size_t len = e - c; - if (len == 1 && c[0] == '.') { c = e; continue; } - if (len == 2 && c[0] == '.' && c[1] == '.') return FP_SLOW; - if (o + len + 2 >= sizeof norm) return FP_SLOW; - norm[o++] = '/'; - memcpy(norm + o, c, len); - o += len; - c = e; - } - if (o == 0) norm[o++] = '/'; - norm[o] = 0; - if (reserved(norm)) return FP_SLOW; - struct bind *bd = NULL; - for (int i = 0; i < nbinds; i++) - if (under(norm, binds[i].guest, binds[i].glen) && (!bd || binds[i].glen > bd->glen)) bd = &binds[i]; - if (bd && !canon(bd)) return FP_SLOW; - size_t base_len = bd ? bd->hlen : rootlen; - int n = bd ? snprintf(host, PATH_MAX, "%s%s", bd->host, norm + bd->glen) - : snprintf(host, PATH_MAX, "%s%s", root, strcmp(norm, "/") ? norm : ""); - if (n >= PATH_MAX) return FP_SLOW; - char *slash = strrchr(host, '/'); - if (!slash || slash == host) return FP_SLOW; - *slash = 0; - long ok = canonical_dir(host); - *slash = '/'; - if (ok == 1) return 0; - if (ok != -ENOENT) return FP_SLOW; - /* The parent is missing. The path is missing for proot too if the deepest directory that does - * exist is canonical and the next component below it is not there at all - not even as a - * symlink proot would follow. Only within the binding (or rootfs) the path was mapped into. */ - char probe[PATH_MAX]; - memcpy(probe, host, n + 1); - for (char *cut = strrchr(probe, '/'); cut && (size_t)(cut - probe) > base_len; ) { - *cut = 0; - char *up = strrchr(probe, '/'); - if (!up || (size_t)(up - probe) < base_len) break; - *up = 0; - long c = canonical_dir(probe[0] ? probe : "/"); - *up = '/'; - if (c == 1) { - struct stat st; - if (sc(SYS_newfstatat, AT_FDCWD, (long)probe, (long)&st, AT_SYMLINK_NOFOLLOW, 0) == -ENOENT) return -ENOENT; - return FP_SLOW; - } - if (c != -ENOENT) return FP_SLOW; - cut = up; - } - return FP_SLOW; + char h[PATH_MAX]; + long t = sc(SYS_readlinkat, AT_FDCWD, (long)r->host, (long)h, sizeof h - 1, 0); + if (t < 0) return t; + if (t >= (long)sizeof h - 1) return FP_SLOW; + if (size && (size_t)t > size) t = size; + h[t] = 0; + if (guest_text(h, text) < 0) return FP_SLOW; + *changed = strcmp(h, text) != 0; + return (long)strlen(text); } -/* The last component, not followed: 0 something that is not a symlink, 1 a symlink, <0 -errno. */ -static long kind(const char *host, struct stat *st) { - long r = sc(SYS_newfstatat, AT_FDCWD, (long)host, (long)st, AT_SYMLINK_NOFOLLOW, 0); - if (r < 0) return r; - return S_ISLNK(st->st_mode) ? 1 : 0; +/* /proc//fd/N. */ +static int fd_link(const char *g) { + const char *p = g + 6; + if (strncmp(g, "/proc/", 6) != 0 || *p < '1' || *p > '9') return 0; + while (*p >= '0' && *p <= '9') p++; + if (strncmp(p, "/fd/", 4) != 0 || p[4] < '0' || p[4] > '9') return 0; + return 1; +} + +/* + * A link met under /proc while resolving, as canonicalize() reads it: /proc/self and + * /proc/thread-self name the caller for proot as for the kernel, the others are what proot's + * readlink() gives. 0 for /proc//fd/N named last, which proot does not follow itself. + */ +static long proc_link(struct res *r, char *text, int final) { + int changed; + if (!under(r->host, "/proc", 5)) return FP_SLOW; + r->moving = 1; + if (!strcmp(r->guest, "/proc/self") || !strcmp(r->guest, "/proc/thread-self")) { + long t = sc(SYS_readlinkat, AT_FDCWD, (long)r->host, (long)text, PATH_MAX - 1, 0); + if (t <= 0 || t >= PATH_MAX - 1) return FP_SLOW; + text[t] = 0; + return t; + } + if (!r->libc && final && fd_link(r->guest)) return 0; + long t = proc_readlink(r, text, 0, &changed); + return t <= 0 ? FP_SLOW : t; +} + +static int pop(char *g) { + char *slash = strrchr(g, '/'); + if (!slash) return -1; + if (slash == g) g[1] = 0; + else *slash = 0; + return 0; +} + +/* The final component is there for the call to meet but could not be looked at (sysfs and /proc + * nodes the app may not stat): proot leaves it to the kernel too. Only callers that act on r->host + * alone ask for it with MAY_BE_UNSEEN; to the others it is FP_SLOW. */ +#define UNSEEN 2 +#define MAY_BE_UNSEEN 2 +/* The final component is proot's glue: a directory, but not one the kernel has. */ +#define GLUE 3 + +/* + * Resolves `text` below r->guest (an existing canonical directory; "/" for an absolute text), as + * canonicalize() does. The final component is followed when `follow` is set; `last`: it is also the + * last of the whole path. 0 with r->exists set, 1 when only the final component is missing, + * UNSEEN, GLUE, -errno, or FP_SLOW. + */ +static long walk(struct res *r, const char *text, int follow, int last) { + char rest[PATH_MAX]; + size_t len = strlen(text); + int links = 0, stated = 0; + if (len >= sizeof rest) return FP_SLOW; + memcpy(rest, text, len + 1); + char *c = rest; + r->exists = 0; + for (;;) { + while (*c == '/') c++; + if (!*c) break; + char *e = strchrnul(c, '/'); + size_t n = e - c; + char *after = e; + while (*after == '/') after++; + int final = *after == 0; + stated = 0; + if (n == 1 && c[0] == '.') { c = e; continue; } + if (n == 2 && c[0] == '.' && c[1] == '.') { pop(r->guest); c = e; continue; } + size_t glen = strlen(r->guest); + if (glen + n + 2 >= PATH_MAX) return FP_SLOW; + if (glen > 1) r->guest[glen++] = '/'; + memcpy(r->guest + glen, c, n); + r->guest[glen + n] = 0; + if (reserved(r->guest)) return FP_SLOW; + struct bind *through; + if (to_host(r->guest, r->host, &through) < 0) return FP_SLOW; + long s = sc(SYS_newfstatat, AT_FDCWD, (long)r->host, (long)&r->st, AT_SYMLINK_NOFOLLOW, 0); + if (s < 0) { + /* A directory above a binding that proot could not look at when it set the binding up (one + * missing, or debugfs) is its glue; any other component it cannot look at is ENOENT unless it + * is the last, which the call itself then meets. */ + int glue = glue_above_binding(r->guest, glen + n); + if (glue < 0) return FP_SLOW; + if (glue) { + if (final) return GLUE; + c = e; + continue; + } + /* glibc's realpath() meets it as its readlink() fails. */ + if (!final || !last) return r->libc ? s : -ENOENT; + return s == -ENOENT ? 1 : UNSEEN; + } + if (S_ISLNK(r->st.st_mode) && (!final || follow)) { + char target[PATH_MAX]; + long t; + if (++links > MAX_LINKS) return FP_SLOW; + if (under(r->guest, "/proc", 5)) { + t = proc_link(r, target, final && last); + if (t == FP_SLOW) return FP_SLOW; + if (t == 0) { + if (sc(SYS_newfstatat, AT_FDCWD, (long)r->host, (long)&r->st, 0, 0) < 0) return FP_SLOW; + r->magic = 1; + r->exists = 1; + return 0; + } + } else { + t = sc(SYS_readlinkat, AT_FDCWD, (long)r->host, (long)target, sizeof target - 1, 0); + if (t <= 0 || t >= (long)sizeof target - 1) return FP_SLOW; + target[t] = 0; + if (detranslated(target, r->host) == FP_SLOW) return FP_SLOW; + t = strlen(target); + } + if (target[0] == '/') strcpy(r->guest, "/"); + else pop(r->guest); + size_t tail = strlen(e); + if ((size_t)t + 1 + tail >= sizeof rest) return FP_SLOW; + memmove(rest + t + 1, e, tail + 1); + memcpy(rest, target, t); + rest[t] = '/'; + c = rest; + continue; + } + if (!final && !S_ISDIR(r->st.st_mode)) return -ENOTDIR; + stated = 1; + c = e; + } + if (!stated) { + /* Ended on the root, ".", ".." or glue: the directory itself. */ + if (to_host(r->guest, r->host, NULL) < 0) return FP_SLOW; + long s = sc(SYS_newfstatat, AT_FDCWD, (long)r->host, (long)&r->st, AT_SYMLINK_NOFOLLOW, 0); + if (s < 0 && strcmp(r->guest, "/") != 0 && glue_above_binding(r->guest, strlen(r->guest))) return FP_SLOW; + if (s < 0) return s == -ENOENT ? FP_SLOW : s; + } + r->exists = 1; + return 0; +} + +/* The host directory a relative path starts in; -ENOTDIR, as proot answers, for a descriptor that + * is no file-system object (a pipe, a socket), or -1. */ +static int base_of(int dirfd, char *h) { + char link[40]; + if (dirfd == AT_FDCWD) snprintf(link, sizeof link, "/proc/self/cwd"); + else snprintf(link, sizeof link, "/proc/self/fd/%d", dirfd); + long n = sc(SYS_readlinkat, AT_FDCWD, (long)link, (long)h, PATH_MAX - 1, 0); + if (n <= 0 || n >= PATH_MAX - 1) return -1; + if (h[0] != '/') return dirfd == AT_FDCWD ? -1 : -ENOTDIR; + h[n] = 0; + return gone(h, n) ? -1 : 0; +} + +/* + * Resolves the guest path `base`/`path` (base NULL for an absolute path). Directories are looked + * up in the cache by their literal absolute path; only a path with ".." in it is resolved in full. + * A trailing "/" asks for an existing directory, as the kernel does; what a missing one creates is + * the caller's to check. + */ +static long resolve_in(const char *base, const char *path, int follow, struct res *r) { + char norm[PATH_MAX]; + size_t o = 0; + int dotdot = 0; + size_t plen = strlen(path); + int trailing = plen > 1 && path[plen - 1] == '/'; + /* "//" and "/./" go; ".." stays, it is not lexical once symlinks are involved. */ + for (int part = base ? 0 : 1; part < 2; part++) { + for (const char *c = part ? path : base; *c;) { + while (*c == '/') c++; + if (!*c) break; + const char *e = strchrnul(c, '/'); + size_t len = e - c; + c = e; + if (len == 1 && e[-1] == '.') continue; + if (len == 2 && e[-1] == '.' && e[-2] == '.') dotdot = 1; + if (o + len + 2 >= sizeof norm) return FP_SLOW; + norm[o++] = '/'; + memcpy(norm + o, e - len, len); + o += len; + } + } + norm[o] = 0; + strcpy(r->guest, "/"); + r->moving = 0; + r->magic = 0; + long w; + char *slash = strrchr(norm, '/'); + if (o == 0 || dotdot || slash == norm) { + w = walk(r, o ? norm : "/", follow || trailing, 1); + } else { + *slash = 0; + unsigned h = hash_of(norm); + if (!cached(norm, h, r->guest)) { + w = walk(r, norm, 1, 0); + if (w < 0) return w; + if (w != GLUE && !S_ISDIR(r->st.st_mode)) return -ENOTDIR; + if (!r->moving) remember(norm, h, r->guest); + } + *slash = '/'; + w = walk(r, slash + 1, follow || trailing, 1); + } + if (w < 0) return w; + if (w == GLUE || (trailing && w != 1 && (w != 0 || !S_ISDIR(r->st.st_mode)))) return FP_SLOW; + return w; +} + +/* + * Resolves `path` relative to dirfd, as the guest means it. When the starting directory has two + * guest names (a library bound twice), proot resolves from the one it was reached by, which is + * not known here: the answer stands only if every name leads to the same place. r->named is set + * when the result's guest path is the one proot would report (realpath). + */ +static __attribute__((noinline)) long same_from_other_names(const char *h, const char *path, int follow, + long rs, const struct res *r) { + char base[PATH_MAX]; + struct res other; + other.libc = r->libc; + for (int k = 1; guest_name(h, k, base) == 0; k++) { + if (resolve_in(base, path, follow, &other) != rs) return FP_SLOW; + if (rs >= 0 && strcmp(other.host, r->host) != 0) return FP_SLOW; + } + return rs; +} + +static long resolve_as(int dirfd, const char *path, int how, int libc, struct res *r) { + int follow = how & 1; + long rs; + char h[PATH_MAX], base[PATH_MAX]; + if (!path || !path[0] || !fp_ready()) return FP_SLOW; + r->named = 1; + r->libc = libc; + if (path[0] == '/') { + rs = resolve_in(NULL, path, follow, r); + } else { + int b = base_of(dirfd, h); + if (b == -ENOTDIR) return b; + if (b < 0 || guest_name(h, 0, base) < 0) return FP_SLOW; + rs = resolve_in(base, path, follow, r); + if (rs != FP_SLOW && guest_name(h, 1, base) == 0) { + r->named = 0; + rs = same_from_other_names(h, path, follow, rs, r); + } + } + return rs == UNSEEN && !(how & MAY_BE_UNSEEN) ? FP_SLOW : rs; +} + +/* `how`: whether to follow a final link (1), and MAY_BE_UNSEEN. */ +static long resolve(int dirfd, const char *path, int how, struct res *r) { + return resolve_as(dirfd, path, how, 0, r); +} + +/* "name/" makes the kernel follow a final link; calls that act on the link itself leave it to proot. */ +static int trailing_slash(const char *path) { + size_t len = path ? strlen(path) : 0; + return len > 1 && path[len - 1] == '/'; } static int ret(long r) { @@ -381,36 +818,29 @@ static int ret(long r) { static __typeof__(type) real_##name; \ if (!real_##name) real_##name = (type)dlsym(RTLD_NEXT, #name) +/* An answer from here (r != FP_SLOW) or the real call. */ +#define ANSWER(r, dirfd, path, real_call) \ + do { long r_ = (r); if (r_ != FP_SLOW) { hits++; return ret(r_); } missed(__func__, dirfd, path); return real_call; } while (0) + /* ---------------------------------------------------------------- open */ static long fp_open(int dirfd, const char *path, int flags, mode_t mode) { - char host[PATH_MAX]; + struct res r; if ((flags & __O_TMPFILE) == __O_TMPFILE) return FP_SLOW; - long rs = resolve(dirfd, path, host); - if (rs != 0) return rs; - /* The last component is never followed here: a symlink there is proot's to resolve. */ - long fd = sc(SYS_openat, AT_FDCWD, (long)host, flags | O_NOFOLLOW, (flags & O_CREAT) ? mode : 0, 0); - if (fd == -ELOOP && !(flags & O_NOFOLLOW)) return FP_SLOW; - /* With O_DIRECTORY the kernel checks for a directory before it gets to the symlink, so a link to - * a directory (opendir() of it) fails ENOTDIR rather than ELOOP. A real non-directory stays here. */ - if (fd == -ENOTDIR && (flags & O_DIRECTORY) && !(flags & O_NOFOLLOW)) { - struct stat st; - if (kind(host, &st) == 1) return FP_SLOW; - } - if (fd >= 0 && (flags & O_PATH) && !(flags & O_NOFOLLOW)) { - struct stat st; - if (sc(SYS_fstat, fd, (long)&st, 0, 0, 0) != 0 || S_ISLNK(st.st_mode)) { - sc(SYS_close, fd, 0, 0, 0, 0); - return FP_SLOW; - } - } - return fd; + int follow = !(flags & O_NOFOLLOW) && !((flags & O_CREAT) && (flags & O_EXCL)); + if ((flags & O_CREAT) && trailing_slash(path)) return FP_SLOW; + long rs = resolve(dirfd, path, follow | MAY_BE_UNSEEN, &r); + if (rs < 0) return rs; + if (rs == 1 && !(flags & O_CREAT)) return -ENOENT; + if (rs == UNSEEN) return sc(SYS_openat, AT_FDCWD, (long)r.host, flags, (flags & O_CREAT) ? mode : 0, 0); + /* Resolved here: the kernel must not follow a link that appeared since in host terms. */ + return sc(SYS_openat, AT_FDCWD, (long)r.host, flags | (follow && !r.magic ? O_NOFOLLOW : 0), (flags & O_CREAT) ? mode : 0, 0); } static int do_openat(int dirfd, const char *path, int flags, mode_t mode, int (*real)(int, const char *, int, ...)) { long r = fp_open(dirfd, path, flags, mode); if (r != FP_SLOW) { hits++; return ret(r); } - misses++; + missed(__func__, dirfd, path); return real(dirfd, path, flags, mode); } @@ -433,25 +863,28 @@ int __open_2(const char *path, int flags) { return open(path, flags); } int __open64_2(const char *path, int flags) { return open(path, flags); } int __openat_2(int d, const char *path, int flags) { return openat(d, path, flags); } int __openat64_2(int d, const char *path, int flags) { return openat(d, path, flags); } +int creat(const char *path, mode_t mode) { return open(path, O_WRONLY | O_CREAT | O_TRUNC, mode); } +int creat64(const char *path, mode_t mode) __attribute__((alias("creat"))); /* ---------------------------------------------------------------- stat family */ static long fp_stat(int dirfd, const char *path, int flags, struct stat *st) { - char host[PATH_MAX]; - if (flags & ~AT_SYMLINK_NOFOLLOW) return FP_SLOW; - long rs = resolve(dirfd, path, host); - if (rs != 0) return rs; - long k = kind(host, st); - if (k == 1 && !(flags & AT_SYMLINK_NOFOLLOW)) return FP_SLOW; - return k < 0 ? k : 0; + struct res r; + /* fstat() is fstatat(fd, "", AT_EMPTY_PATH): nothing to translate. */ + if ((flags & AT_EMPTY_PATH) && path && !path[0] && fp_ready()) + return sc(SYS_newfstatat, dirfd, (long)"", (long)st, flags, 0); + if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT)) return FP_SLOW; + long rs = resolve(dirfd, path, (flags & AT_SYMLINK_NOFOLLOW ? 0 : 1) | MAY_BE_UNSEEN, &r); + if (rs < 0) return rs; + if (rs == 1) return -ENOENT; + if (rs == UNSEEN) return sc(SYS_newfstatat, AT_FDCWD, (long)r.host, (long)st, flags, 0); + *st = r.st; + return 0; } static int stat_common(int dirfd, const char *path, struct stat *st, int flags, int (*real)(int, const char *, struct stat *, int)) { - long r = fp_stat(dirfd, path, flags, st); - if (r != FP_SLOW) { hits++; return ret(r); } - misses++; - return real(dirfd, path, st, flags); + ANSWER(fp_stat(dirfd, path, flags, st), dirfd, path, real(dirfd, path, st, flags)); } int fstatat(int dirfd, const char *path, struct stat *st, int flags) { @@ -470,6 +903,16 @@ int lstat(const char *path, struct stat *st) { } int lstat64(const char *p, struct stat64 *st) { return lstat(p, (struct stat *)st); } +/* glibc turns fstat() into fstatat(fd, "", AT_EMPTY_PATH), which proot traps as a path call; the + * plain syscall is not traced at all. */ +int fstat(int fd, struct stat *st) { + REAL(fstat, int (*)(int, struct stat *)); + if (!fp_ready()) return real_fstat(fd, st); + hits++; + return ret(sc(SYS_fstat, fd, (long)st, 0, 0, 0)); +} +int fstat64(int fd, struct stat64 *st) { return fstat(fd, (struct stat *)st); } + /* * The pre-2.33 entry points. Programs linked against an older glibc - the Steam client, * steamclient.so and filesystem_stdio.so among them - call these rather than stat(), so without @@ -489,70 +932,594 @@ int __lxstat(int ver, const char *p, struct stat *st) { return xstat_common(ver, int __lxstat64(int ver, const char *p, struct stat64 *st) { return xstat_common(ver, AT_FDCWD, p, (struct stat *)st, AT_SYMLINK_NOFOLLOW); } int __fxstatat(int ver, int d, const char *p, struct stat *st, int f) { return xstat_common(ver, d, p, st, f); } int __fxstatat64(int ver, int d, const char *p, struct stat64 *st, int f) { return xstat_common(ver, d, p, (struct stat *)st, f); } +int __fxstat(int ver, int fd, struct stat *st) { + if (ver != FP_STAT_VER) { errno = EINVAL; return -1; } + return fstat(fd, st); +} +int __fxstat64(int ver, int fd, struct stat64 *st) { return __fxstat(ver, fd, (struct stat *)st); } int statx(int dirfd, const char *path, int flags, unsigned mask, struct statx *out) { REAL(statx, int (*)(int, const char *, int, unsigned, struct statx *)); - char host[PATH_MAX]; - long rs = (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_STATX_SYNC_TYPE)) ? FP_SLOW : resolve(dirfd, path, host); - if (rs == -ENOENT) { hits++; return ret(rs); } - if (rs == 0) { - long r = sc(SYS_statx, AT_FDCWD, (long)host, flags | AT_SYMLINK_NOFOLLOW, mask | STATX_TYPE, (long)out); - if (!(r == 0 && S_ISLNK(out->stx_mode) && !(flags & AT_SYMLINK_NOFOLLOW))) { hits++; return ret(r); } + struct res r; + long rs; + if ((flags & AT_EMPTY_PATH) && !path[0] && fp_ready()) { + hits++; + return ret(sc(SYS_statx, dirfd, (long)"", flags, mask, (long)out)); } - misses++; - return real_statx(dirfd, path, flags, mask, out); + rs = (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_STATX_SYNC_TYPE)) ? FP_SLOW + : resolve(dirfd, path, (flags & AT_SYMLINK_NOFOLLOW ? 0 : 1) | MAY_BE_UNSEEN, &r); + if (rs == 1) rs = -ENOENT; + if (rs == 0 || rs == UNSEEN) + rs = sc(SYS_statx, AT_FDCWD, (long)r.host, flags | (rs == 0 && !r.magic ? AT_SYMLINK_NOFOLLOW : 0), mask, (long)out); + ANSWER(rs, dirfd, path, real_statx(dirfd, path, flags, mask, out)); } /* ---------------------------------------------------------------- access */ +/* AT_EACCESS only differs from a plain check in a set-ID program, where glibc does the work itself; + * elsewhere glibc's own fallback is the plain faccessat, and so is this. */ +static int effective_is_real(void) { + return !getauxval(AT_SECURE) && getuid() == geteuid() && getgid() == getegid(); +} + static int access_common(int dirfd, const char *path, int mode, int flags) { REAL(faccessat, int (*)(int, const char *, int, int)); - char host[PATH_MAX]; - struct stat st; - /* faccessat(2) has no flags. AT_EACCESS stays with glibc and proot; AT_SYMLINK_NOFOLLOW (the - * Steam client passes it for every file it reserves) only differs on a symlink, which goes to - * proot as well. */ - long rs = (flags & ~AT_SYMLINK_NOFOLLOW) == 0 ? resolve(dirfd, path, host) : FP_SLOW; - if (rs == -ENOENT) { hits++; return ret(rs); } - if (rs == 0) { - long k = kind(host, &st); - if (k != 1) { - hits++; - if (k < 0) return ret(k); - return ret(sc(SYS_faccessat, AT_FDCWD, (long)host, mode, 0, 0)); - } - } - misses++; + struct res r; + if ((flags & AT_EACCESS) && effective_is_real()) flags &= ~AT_EACCESS; + /* faccessat(2) has no flags; AT_SYMLINK_NOFOLLOW (the Steam client passes it for every file it + * reserves) only differs on a symlink, which goes to proot. */ + long rs = (flags & ~AT_SYMLINK_NOFOLLOW) == 0 + ? resolve(dirfd, path, (flags & AT_SYMLINK_NOFOLLOW ? 0 : 1) | MAY_BE_UNSEEN, &r) : FP_SLOW; + if (rs == 1) rs = -ENOENT; + if (rs == 0 && S_ISLNK(r.st.st_mode)) rs = FP_SLOW; + if (rs == 0 || rs == UNSEEN) rs = sc(SYS_faccessat, AT_FDCWD, (long)r.host, mode, 0, 0); + if (rs != FP_SLOW) { hits++; return ret(rs); } + missed(__func__, dirfd, path); + /* glibc tries faccessat2 first, which Android answers with SIGSYS: a second round trip. */ + if (flags == 0) return (int)syscall(SYS_faccessat, dirfd, path, mode); return real_faccessat(dirfd, path, mode, flags); } int faccessat(int dirfd, const char *path, int mode, int flags) { return access_common(dirfd, path, mode, flags); } int access(const char *path, int mode) { return access_common(AT_FDCWD, path, mode, 0); } +int eaccess(const char *path, int mode) { return access_common(AT_FDCWD, path, mode, AT_EACCESS); } +int euidaccess(const char *path, int mode) __attribute__((alias("eaccess"))); -/* ---------------------------------------------------------------- readlink */ +/* ---------------------------------------------------------------- readlink, realpath, getcwd */ + +/* + * The text proot's readlink() gives for the link r names: the stored text as the kernel cuts it to + * `size` (0: no limit), detranslated. `changed`: proot wrote its own answer rather than leaving the + * kernel's. + */ +static long link_text(const struct res *r, char *text, size_t size, int *changed) { + int in_proc = under(r->guest, "/proc", 5); + *changed = 0; + if (in_proc != under(r->host, "/proc", 5)) return FP_SLOW; + if (in_proc) return proc_readlink(r, text, size, changed); + long t = sc(SYS_readlinkat, AT_FDCWD, (long)r->host, (long)text, PATH_MAX - 1, 0); + if (t < 0) return t; + if (t >= PATH_MAX - 1) return FP_SLOW; + if (size && (size_t)t > size) t = size; + text[t] = 0; + long d = detranslated(text, r->host); + if (d == FP_SLOW) return FP_SLOW; + *changed = (int)d; + return (long)strlen(text); +} -/* The link's own text needs no translation outside /proc: proot stores and returns it as is. */ static ssize_t readlink_common(int dirfd, const char *path, char *buf, size_t size) { REAL(readlinkat, ssize_t (*)(int, const char *, char *, size_t)); - char host[PATH_MAX]; - long rs = resolve(dirfd, path, host); - if (rs == -ENOENT) { hits++; return ret(rs); } - if (rs == 0) { - hits++; - long r = sc(SYS_readlinkat, AT_FDCWD, (long)host, (long)buf, size, 0); - return r < 0 ? ret(r) : r; + struct res r; + char text[PATH_MAX]; + int changed; + long rs = size ? resolve(dirfd, path, 0, &r) : FP_SLOW; + if (rs == 1) rs = -ENOENT; + if (rs == 0) rs = link_text(&r, text, size, &changed); + if (rs == FP_SLOW) { + missed(__func__, dirfd, path); + return real_readlinkat(dirfd, path, buf, size); } - misses++; - return real_readlinkat(dirfd, path, buf, size); + hits++; + if (rs < 0) return ret(rs); + size_t n = rs; + if (!changed) { + n = n < size ? n : size; + memcpy(buf, text, n); + return n; + } + /* As proot writes its answer: with the terminator when it fits, else cut at the buffer (or + * PATH_MAX) and reported as that long. */ + size_t max = size < PATH_MAX ? size : PATH_MAX; + if (n + 1 < max) { + memcpy(buf, text, n + 1); + return n; + } + memcpy(buf, text, max); + return max; } ssize_t readlinkat(int dirfd, const char *path, char *buf, size_t size) { return readlink_common(dirfd, path, buf, size); } ssize_t readlink(const char *path, char *buf, size_t size) { return readlink_common(AT_FDCWD, path, buf, size); } +/* _FORTIFY_SOURCE builds call these, which reach libc's own readlink and getcwd directly; an + * overflow is still the real one's to report. */ +ssize_t __readlink_chk(const char *path, char *buf, size_t size, size_t room) { + REAL(__readlink_chk, ssize_t (*)(const char *, char *, size_t, size_t)); + if (size > room) return real___readlink_chk(path, buf, size, room); + return readlink_common(AT_FDCWD, path, buf, size); +} +ssize_t __readlinkat_chk(int dirfd, const char *path, char *buf, size_t size, size_t room) { + REAL(__readlinkat_chk, ssize_t (*)(int, const char *, char *, size_t, size_t)); + if (size > room) return real___readlinkat_chk(dirfd, path, buf, size, room); + return readlink_common(dirfd, path, buf, size); +} + +/* glibc's realpath() reads every component as a link, each a trapped readlinkat with an exit stop: + * wineserver and Wine resolve their prefix that way for every file they open, and a file's name + * through /proc/self/fd. */ +char *realpath(const char *path, char *resolved) { + REAL(realpath, char *(*)(const char *, char *)); + struct res r; + long rs = resolve_as(AT_FDCWD, path, 1, 1, &r); + if (rs == 0 && !r.named) rs = FP_SLOW; + if (rs == FP_SLOW) { missed(__func__, AT_FDCWD, path); return real_realpath(path, resolved); } + hits++; + if (rs == 1) rs = -ENOENT; + if (rs < 0) { errno = (int)-rs; return NULL; } + size_t n = strlen(r.guest) + 1; + if (!resolved) { + resolved = malloc(n); + if (!resolved) return NULL; + } + memcpy(resolved, r.guest, n); + return resolved; +} +char *__realpath_chk(const char *path, char *resolved, size_t size) { + REAL(__realpath_chk, char *(*)(const char *, char *, size_t)); + if (resolved && size < PATH_MAX) return real___realpath_chk(path, resolved, size); + return realpath(path, resolved); +} +char *canonicalize_file_name(const char *path) { return realpath(path, NULL); } + +/* proot keeps the guest cwd and answers getcwd on the way out; with the fast path the kernel's cwd + * is its host directory, which names it whenever only one guest path does. */ +char *getcwd(char *buf, size_t size) { + REAL(getcwd, char *(*)(char *, size_t)); + char g[PATH_MAX]; + if (!fp_ready() || own_cwd(g) == FP_SLOW) { missed(__func__, AT_FDCWD, "."); return real_getcwd(buf, size); } + hits++; + size_t need = strlen(g) + 1; + if (buf && size == 0) { errno = EINVAL; return NULL; } + if (buf && size < need) { errno = ERANGE; return NULL; } + if (!buf) { + if (size && size < need) { errno = ERANGE; return NULL; } + buf = malloc(size ? size : need); + if (!buf) return NULL; + } + memcpy(buf, g, need); + return buf; +} +char *__getcwd_chk(char *buf, size_t size, size_t room) { + REAL(__getcwd_chk, char *(*)(char *, size_t, size_t)); + if (size > room) return real___getcwd_chk(buf, size, room); + return getcwd(buf, size); +} + +/* ---------------------------------------------------------------- changes */ + +int mkdirat(int dirfd, const char *path, mode_t mode) { + REAL(mkdirat, int (*)(int, const char *, mode_t)); + struct res r; + long rs = resolve(dirfd, path, MAY_BE_UNSEEN, &r); + if (rs == 0 || rs == 1 || rs == UNSEEN) rs = sc(SYS_mkdirat, AT_FDCWD, (long)r.host, mode, 0, 0); + ANSWER(rs, dirfd, path, real_mkdirat(dirfd, path, mode)); +} +int mkdir(const char *path, mode_t mode) { return mkdirat(AT_FDCWD, path, mode); } + +/* The last name in a path is "." or "..", which the kernel refuses to remove. */ +static int ends_in_dots(const char *path) { + size_t n = strlen(path); + while (n > 1 && path[n - 1] == '/') n--; + size_t b = n; + while (b > 0 && path[b - 1] != '/') b--; + return (n - b == 1 && path[b] == '.') || (n - b == 2 && path[b] == '.' && path[b + 1] == '.'); +} + +static long fp_unlink(int dirfd, const char *path, int flags) { + struct res r; + char bare[PATH_MAX]; + int slash = trailing_slash(path); + if ((flags & ~AT_REMOVEDIR) || !path || ends_in_dots(path) || (slash && !(flags & AT_REMOVEDIR))) return FP_SLOW; + if (slash) { + /* rmdir("dir/") removes dir itself, never what a link of that name points to. */ + size_t n = strlen(path); + if (n >= sizeof bare) return FP_SLOW; + while (n > 1 && path[n - 1] == '/') n--; + memcpy(bare, path, n); + bare[n] = 0; + path = bare; + } + long rs = resolve(dirfd, path, MAY_BE_UNSEEN, &r); + if (rs == 1) rs = -ENOENT; + if (slash && rs == UNSEEN) rs = FP_SLOW; + if (slash && rs == 0 && !S_ISDIR(r.st.st_mode)) rs = S_ISLNK(r.st.st_mode) ? FP_SLOW : -ENOTDIR; + if (rs == 0 && (flags & AT_REMOVEDIR) && strcmp(r.guest, "/") == 0) rs = FP_SLOW; + if (rs == 0 || rs == UNSEEN) { + int changes_paths = (flags & AT_REMOVEDIR) || rs == UNSEEN || S_ISLNK(r.st.st_mode); + rs = sc(SYS_unlinkat, AT_FDCWD, (long)r.host, flags, 0, 0); + if (rs == 0 && changes_paths) forget(); + } + return rs; +} + +static int unlink_common(int dirfd, const char *path, int flags) { + REAL(unlinkat, int (*)(int, const char *, int)); + ANSWER(fp_unlink(dirfd, path, flags), dirfd, path, real_unlinkat(dirfd, path, flags)); +} +int unlinkat(int dirfd, const char *path, int flags) { return unlink_common(dirfd, path, flags); } +int unlink(const char *path) { return unlink_common(AT_FDCWD, path, 0); } +int rmdir(const char *path) { return unlink_common(AT_FDCWD, path, AT_REMOVEDIR); } + +/* A renamed directory moves the guest cwd proot keeps for every process under it, so those stay + * with proot; files are what programs rename all the time (write a copy, rename it over). */ +static int rename_common(int olddirfd, const char *old, int newdirfd, const char *new, unsigned flags) { + REAL(renameat2, int (*)(int, const char *, int, const char *, unsigned)); + struct res from, to; + long rs = flags & ~RENAME_NOREPLACE || trailing_slash(old) || trailing_slash(new) ? FP_SLOW : resolve(olddirfd, old, 0, &from); + if (rs == 1) rs = -ENOENT; + if (rs == 0 && S_ISDIR(from.st.st_mode)) rs = FP_SLOW; + if (rs == 0) { + long rt = resolve(newdirfd, new, 0, &to); + if (rt < 0) rs = rt; + else if (rt == 0 && S_ISDIR(to.st.st_mode)) rs = FP_SLOW; + else { + rs = flags ? sc(SYS_renameat2, AT_FDCWD, (long)from.host, AT_FDCWD, (long)to.host, flags) + : sc(SYS_renameat, AT_FDCWD, (long)from.host, AT_FDCWD, (long)to.host, 0); + if (rs == 0) forget(); + } + } + if (rs != FP_SLOW) { hits++; return ret(rs); } + missed(__func__, olddirfd, old); + /* renameat, not renameat2: it is the one proot follows to move the cwd it keeps. */ + if (!flags) return (int)syscall(SYS_renameat, olddirfd, old, newdirfd, new); + return real_renameat2(olddirfd, old, newdirfd, new, flags); +} +int renameat2(int olddirfd, const char *old, int newdirfd, const char *new, unsigned flags) { + return rename_common(olddirfd, old, newdirfd, new, flags); +} +int renameat(int olddirfd, const char *old, int newdirfd, const char *new) { return rename_common(olddirfd, old, newdirfd, new, 0); } +int rename(const char *old, const char *new) { return rename_common(AT_FDCWD, old, AT_FDCWD, new, 0); } + +/* The link's text is stored as given, as proot stores it. */ +int symlinkat(const char *target, int dirfd, const char *path) { + REAL(symlinkat, int (*)(const char *, int, const char *)); + struct res r; + long rs = trailing_slash(path) ? FP_SLOW : resolve(dirfd, path, MAY_BE_UNSEEN, &r); + if (rs == 0 || rs == 1 || rs == UNSEEN) { + rs = sc(SYS_symlinkat, (long)target, AT_FDCWD, (long)r.host, 0, 0); + if (rs == 0) forget(); + } + ANSWER(rs, dirfd, path, real_symlinkat(target, dirfd, path)); +} +int symlink(const char *target, const char *path) { return symlinkat(target, AT_FDCWD, path); } + +int fchmodat(int dirfd, const char *path, mode_t mode, int flags) { + REAL(fchmodat, int (*)(int, const char *, mode_t, int)); + struct res r; + long rs = flags ? FP_SLOW : resolve(dirfd, path, 1 | MAY_BE_UNSEEN, &r); + if (rs == 1) rs = -ENOENT; + if (rs == 0 || rs == UNSEEN) rs = sc(SYS_fchmodat, AT_FDCWD, (long)r.host, mode, 0, 0); + ANSWER(rs, dirfd, path, real_fchmodat(dirfd, path, mode, flags)); +} +int chmod(const char *path, mode_t mode) { return fchmodat(AT_FDCWD, path, mode, 0); } + +static int utimens_common(int dirfd, const char *path, const struct timespec times[2], int flags) { + REAL(utimensat, int (*)(int, const char *, const struct timespec[2], int)); + struct res r; + long rs; + if (!path && fp_ready()) { + rs = sc(SYS_utimensat, dirfd, 0, (long)times, flags, 0); /* futimens() */ + } else { + rs = flags & ~AT_SYMLINK_NOFOLLOW ? FP_SLOW : resolve(dirfd, path, (flags & AT_SYMLINK_NOFOLLOW ? 0 : 1) | MAY_BE_UNSEEN, &r); + if (rs == 1) rs = -ENOENT; + if (rs == 0 || rs == UNSEEN) rs = sc(SYS_utimensat, AT_FDCWD, (long)r.host, (long)times, flags, 0); + } + ANSWER(rs, dirfd, path, real_utimensat(dirfd, path, times, flags)); +} +int utimensat(int dirfd, const char *path, const struct timespec times[2], int flags) { + return utimens_common(dirfd, path, times, flags); +} +int futimens(int fd, const struct timespec times[2]) { return utimens_common(fd, NULL, times, 0); } + +static int utimes_common(int dirfd, const char *path, const struct timeval tv[2], int flags) { + struct timespec ts[2]; + if (tv) { + for (int i = 0; i < 2; i++) { + if (tv[i].tv_usec < 0 || tv[i].tv_usec >= 1000000) { errno = EINVAL; return -1; } + ts[i].tv_sec = tv[i].tv_sec; + ts[i].tv_nsec = tv[i].tv_usec * 1000; + } + } + return utimens_common(dirfd, path, tv ? ts : NULL, flags); +} +int utimes(const char *path, const struct timeval tv[2]) { return utimes_common(AT_FDCWD, path, tv, 0); } +int lutimes(const char *path, const struct timeval tv[2]) { return utimes_common(AT_FDCWD, path, tv, AT_SYMLINK_NOFOLLOW); } +int futimes(int fd, const struct timeval tv[2]) { return utimes_common(fd, NULL, tv, 0); } +int utime(const char *path, const struct utimbuf *times) { + if (!times) return utimens_common(AT_FDCWD, path, NULL, 0); + struct timespec ts[2] = { { times->actime, 0 }, { times->modtime, 0 } }; + return utimens_common(AT_FDCWD, path, ts, 0); +} + +int truncate(const char *path, off_t length) { + REAL(truncate, int (*)(const char *, off_t)); + struct res r; + long rs = resolve(AT_FDCWD, path, 1 | MAY_BE_UNSEEN, &r); + if (rs == 1) rs = -ENOENT; + if (rs == 0 || rs == UNSEEN) rs = sc(SYS_truncate, (long)r.host, length, 0, 0, 0); + ANSWER(rs, AT_FDCWD, path, real_truncate(path, length)); +} +int truncate64(const char *path, off_t length) __attribute__((alias("truncate"))); + +/* proot reports /dev/shm as tmpfs on the way out of statfs; that stays with it. */ +static int statfs_common(const char *path, struct statfs *buf, int (*real)(const char *, struct statfs *)) { + struct res r; + long rs = resolve(AT_FDCWD, path, 1, &r); + if (rs == 1) rs = -ENOENT; + if (rs == 0 && under(r.guest, "/dev/shm", 8)) rs = FP_SLOW; + if (rs == 0) rs = sc(SYS_statfs, (long)r.host, (long)buf, 0, 0, 0); + ANSWER(rs, AT_FDCWD, path, real(path, buf)); +} +int statfs(const char *path, struct statfs *buf) { + REAL(statfs, int (*)(const char *, struct statfs *)); + return statfs_common(path, buf, real_statfs); +} +int statfs64(const char *path, struct statfs64 *buf) { + REAL(statfs64, int (*)(const char *, struct statfs *)); + return statfs_common(path, (struct statfs *)buf, real_statfs64); +} + +#define FP_ST_VALID 0x0020 /* the kernel's ST_VALID: statfs filled in f_flags */ +/* statvfs() is statfs() converted as glibc converts it; without ST_VALID the mount flags would come + * from /proc/mounts, which glibc reads itself. */ +static int statvfs_common(const char *path, struct statvfs *buf, int (*real)(const char *, struct statvfs *)) { + struct res r; + struct statfs fs; + long rs = resolve(AT_FDCWD, path, 1, &r); + if (rs == 1) rs = -ENOENT; + if (rs == 0 && under(r.guest, "/dev/shm", 8)) rs = FP_SLOW; + if (rs == 0) rs = sc(SYS_statfs, (long)r.host, (long)&fs, 0, 0, 0); + if (rs == 0 && !(fs.f_flags & FP_ST_VALID)) rs = FP_SLOW; + if (rs == 0) { + memset(buf, 0, sizeof *buf); + buf->f_bsize = fs.f_bsize; + buf->f_frsize = fs.f_frsize ? fs.f_frsize : fs.f_bsize; + buf->f_blocks = fs.f_blocks; + buf->f_bfree = fs.f_bfree; + buf->f_bavail = fs.f_bavail; + buf->f_files = fs.f_files; + buf->f_ffree = fs.f_ffree; + buf->f_favail = fs.f_ffree; + _Static_assert(sizeof buf->f_fsid == sizeof fs.f_fsid, "fsid layout"); + memcpy(&buf->f_fsid, &fs.f_fsid, sizeof fs.f_fsid); + buf->f_flag = fs.f_flags ^ FP_ST_VALID; + buf->f_namemax = fs.f_namelen; + } + ANSWER(rs, AT_FDCWD, path, real(path, buf)); +} +int statvfs(const char *path, struct statvfs *buf) { + REAL(statvfs, int (*)(const char *, struct statvfs *)); + return statvfs_common(path, buf, real_statvfs); +} +int statvfs64(const char *path, struct statvfs64 *buf) { + REAL(statvfs64, int (*)(const char *, struct statvfs *)); + return statvfs_common(path, (struct statvfs *)buf, real_statvfs64); +} + +/* Reading attributes only: fake_id0 rewrites the permission side of setting them. Wine reads + * user.DOSATTRIB for every file it reports on. */ +static ssize_t getxattr_common(const char *path, const char *name, void *value, size_t size, int follow, + ssize_t (*real)(const char *, const char *, void *, size_t)) { + struct res r; + long rs = resolve(AT_FDCWD, path, follow | MAY_BE_UNSEEN, &r); + if (rs == 1) rs = -ENOENT; + if (rs == 0 || rs == UNSEEN) rs = sc(follow ? SYS_getxattr : SYS_lgetxattr, (long)r.host, (long)name, (long)value, size, 0); + if (rs != FP_SLOW) { hits++; return rs < 0 ? ret(rs) : rs; } + missed(__func__, AT_FDCWD, path); + return real(path, name, value, size); +} +ssize_t getxattr(const char *path, const char *name, void *value, size_t size) { + REAL(getxattr, ssize_t (*)(const char *, const char *, void *, size_t)); + return getxattr_common(path, name, value, size, 1, real_getxattr); +} +ssize_t lgetxattr(const char *path, const char *name, void *value, size_t size) { + REAL(lgetxattr, ssize_t (*)(const char *, const char *, void *, size_t)); + return getxattr_common(path, name, value, size, 0, real_lgetxattr); +} + +static ssize_t listxattr_common(const char *path, char *list, size_t size, int follow, + ssize_t (*real)(const char *, char *, size_t)) { + struct res r; + long rs = resolve(AT_FDCWD, path, follow | MAY_BE_UNSEEN, &r); + if (rs == 1) rs = -ENOENT; + if (rs == 0 || rs == UNSEEN) rs = sc(follow ? SYS_listxattr : SYS_llistxattr, (long)r.host, (long)list, size, 0, 0); + if (rs != FP_SLOW) { hits++; return rs < 0 ? ret(rs) : rs; } + missed(__func__, AT_FDCWD, path); + return real(path, list, size); +} +ssize_t listxattr(const char *path, char *list, size_t size) { + REAL(listxattr, ssize_t (*)(const char *, char *, size_t)); + return listxattr_common(path, list, size, 1, real_listxattr); +} +ssize_t llistxattr(const char *path, char *list, size_t size) { + REAL(llistxattr, ssize_t (*)(const char *, char *, size_t)); + return listxattr_common(path, list, size, 0, real_llistxattr); +} + +int inotify_add_watch(int fd, const char *path, uint32_t mask) { + REAL(inotify_add_watch, int (*)(int, const char *, uint32_t)); + struct res r; + long rs = resolve(AT_FDCWD, path, (mask & IN_DONT_FOLLOW ? 0 : 1) | MAY_BE_UNSEEN, &r); + if (rs == 1) rs = -ENOENT; + if (rs == 0 || rs == UNSEEN) rs = sc(SYS_inotify_add_watch, fd, (long)r.host, mask, 0, 0); + ANSWER(rs, AT_FDCWD, path, real_inotify_add_watch(fd, path, mask)); +} + +/* ---------------------------------------------------------------- Unix sockets */ + +/* + * proot only translates the path of a named Unix socket; every other address (abstract, unnamed, + * IP) passes through untouched, so the call itself needs no stop. A named one is resolved as proot + * would and sent with its host path, as long as that fits sun_path: a longer one proot binds to a + * short temporary name, so that stays with it. + */ +static long socket_address(const struct sockaddr *addr, socklen_t len, int follow, struct sockaddr_un *out, socklen_t *out_len) { + const struct sockaddr_un *un = (const struct sockaddr_un *)addr; + size_t room = sizeof un->sun_path; + if (!addr || len <= offsetof(struct sockaddr_un, sun_path) || addr->sa_family != AF_UNIX || un->sun_path[0] == 0) + return 0; + size_t plen = strnlen(un->sun_path, len - offsetof(struct sockaddr_un, sun_path)); + char path[sizeof un->sun_path + 1]; + if (plen > room) return FP_SLOW; + memcpy(path, un->sun_path, plen); + path[plen] = 0; + if (trailing_slash(path)) return FP_SLOW; + struct res r; + long rs = resolve(AT_FDCWD, path, follow, &r); + if (rs < 0) return rs; + if (rs == 1 && follow) return -ENOENT; + if (r.magic) return FP_SLOW; + size_t hlen = strlen(r.host); + if (hlen >= room) return FP_SLOW; + memset(out, 0, sizeof *out); + out->sun_family = AF_UNIX; + memcpy(out->sun_path, r.host, hlen + 1); + *out_len = offsetof(struct sockaddr_un, sun_path) + hlen + 1; + return 1; +} + +/* accept() and connect() are cancellation points in libc; a raw call is not, so only those that + * cannot block for long come here: a non-blocking socket, a listener with a connection waiting, + * or connecting a Unix one. */ +static int nonblocking(int fd) { + long fl = sc(SYS_fcntl, fd, F_GETFL, 0, 0, 0); + return fl >= 0 && (fl & O_NONBLOCK); +} + +int connect(int fd, const struct sockaddr *addr, socklen_t len) { + REAL(connect, int (*)(int, const struct sockaddr *, socklen_t)); + struct sockaddr_un un; + socklen_t un_len; + long rs = fp_ready() ? socket_address(addr, len, 1, &un, &un_len) : FP_SLOW; + if (rs == 0 && !(addr && addr->sa_family == AF_UNIX) && !nonblocking(fd)) rs = FP_SLOW; + if (rs == 0) rs = sc(SYS_connect, fd, (long)addr, len, 0, 0); + else if (rs == 1) rs = sc(SYS_connect, fd, (long)&un, un_len, 0, 0); + ANSWER(rs, AT_FDCWD, "(connect)", real_connect(fd, addr, len)); +} + +int bind(int fd, const struct sockaddr *addr, socklen_t len) { + REAL(bind, int (*)(int, const struct sockaddr *, socklen_t)); + struct sockaddr_un un; + socklen_t un_len; + long rs = fp_ready() ? socket_address(addr, len, 0, &un, &un_len) : FP_SLOW; + if (rs == 0) rs = sc(SYS_bind, fd, (long)addr, len, 0, 0); + else if (rs == 1) rs = sc(SYS_bind, fd, (long)&un, un_len, 0, 0); + ANSWER(rs, AT_FDCWD, "(bind)", real_bind(fd, addr, len)); +} + +/* An address the kernel reports, as proot reports it: a named Unix socket's host path in guest + * terms. One no guest path names unambiguously is left as is, which proot cannot do better. */ +static void report_address(const struct sockaddr_storage *got, socklen_t got_len, struct sockaddr *addr, socklen_t *len) { + struct sockaddr_storage shown = *got; + socklen_t shown_len = got_len; + const struct sockaddr_un *un = (const struct sockaddr_un *)got; + if (got_len > offsetof(struct sockaddr_un, sun_path) && un->sun_family == AF_UNIX && un->sun_path[0]) { + char g[PATH_MAX], h[sizeof un->sun_path + 1]; + size_t hl = strnlen(un->sun_path, got_len - offsetof(struct sockaddr_un, sun_path)); + memcpy(h, un->sun_path, hl); + h[hl] = 0; + struct sockaddr_un *out = (struct sockaddr_un *)&shown; + if (to_guest(h, g, sizeof g) == 0 && strlen(g) < sizeof out->sun_path) { + memset(out->sun_path, 0, sizeof out->sun_path); + strcpy(out->sun_path, g); + shown_len = offsetof(struct sockaddr_un, sun_path) + strlen(g) + 1; + } + } + memcpy(addr, &shown, *len < shown_len ? *len : shown_len); + *len = shown_len; +} + + +static int pending(int fd) { + struct pollfd p = { .fd = fd, .events = POLLIN }; + struct timespec now = { 0, 0 }; + return sc(SYS_ppoll, (long)&p, 1, (long)&now, 0, 0) == 1 && (p.revents & POLLIN); +} + +static int accept_common(int fd, struct sockaddr *addr, socklen_t *len, int flags) { + REAL(accept4, int (*)(int, struct sockaddr *, socklen_t *, int)); + if (!fp_ready() || (addr && !len) || (!nonblocking(fd) && !pending(fd))) { missed(__func__, AT_FDCWD, "(accept)"); return real_accept4(fd, addr, len, flags); } + hits++; + if (!addr) return ret(sc(SYS_accept4, fd, 0, 0, flags, 0)); + struct sockaddr_storage got; + socklen_t got_len = sizeof got; + long r = sc(SYS_accept4, fd, (long)&got, (long)&got_len, flags, 0); + if (r >= 0) report_address(&got, got_len, addr, len); + return ret(r); +} +int accept4(int fd, struct sockaddr *addr, socklen_t *len, int flags) { return accept_common(fd, addr, len, flags); } +int accept(int fd, struct sockaddr *addr, socklen_t *len) { return accept_common(fd, addr, len, 0); } + +static int name_common(long nr, int fd, struct sockaddr *addr, socklen_t *len, int (*real)(int, struct sockaddr *, socklen_t *)) { + if (!fp_ready() || !addr || !len) { missed(__func__, AT_FDCWD, "(name)"); return real(fd, addr, len); } + hits++; + struct sockaddr_storage got; + socklen_t got_len = sizeof got; + long r = sc(nr, fd, (long)&got, (long)&got_len, 0, 0); + if (r == 0) report_address(&got, got_len, addr, len); + return ret(r); +} +int getsockname(int fd, struct sockaddr *addr, socklen_t *len) { + REAL(getsockname, int (*)(int, struct sockaddr *, socklen_t *)); + return name_common(SYS_getsockname, fd, addr, len, real_getsockname); +} +int getpeername(int fd, struct sockaddr *addr, socklen_t *len) { + REAL(getpeername, int (*)(int, struct sockaddr *, socklen_t *)); + return name_common(SYS_getpeername, fd, addr, len, real_getpeername); +} + /* ---------------------------------------------------------------- libc calls that open internally */ +/* shm_open() and shm_unlink() open /dev/shm/ with libc's internal calls; an overlay asks for + * FEX's statistics that way every frame. The name is checked and the file opened as glibc does. */ +static int shm_path(const char *name, char *path) { + while (*name == '/') name++; + size_t n = strlen(name); + if (n == 0 || n > NAME_MAX || strchr(name, '/')) return -1; + memcpy(path, "/dev/shm/", 9); + memcpy(path + 9, name, n + 1); + return 0; +} + +int shm_open(const char *name, int flags, mode_t mode) { + REAL(shm_open, int (*)(const char *, int, mode_t)); + char path[sizeof "/dev/shm/" + NAME_MAX]; + long rs = shm_path(name, path) == 0 ? fp_open(AT_FDCWD, path, flags | O_NOFOLLOW | O_CLOEXEC, mode) : FP_SLOW; + if (rs == -EISDIR) rs = -EINVAL; + ANSWER(rs, AT_FDCWD, name, real_shm_open(name, flags, mode)); +} + +int shm_unlink(const char *name) { + REAL(shm_unlink, int (*)(const char *)); + char path[sizeof "/dev/shm/" + NAME_MAX]; + long rs = shm_path(name, path) == 0 ? fp_unlink(AT_FDCWD, path, 0) : FP_SLOW; + if (rs == -EPERM) rs = -EACCES; + ANSWER(rs, AT_FDCWD, name, real_shm_unlink(name)); +} + DIR *opendir(const char *path) { REAL(opendir, DIR *(*)(const char *)); long fd = fp_open(AT_FDCWD, path, O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NONBLOCK, 0); - if (fd == FP_SLOW) { misses++; return real_opendir(path); } + if (fd == FP_SLOW) { missed(__func__, AT_FDCWD, path); return real_opendir(path); } hits++; if (fd < 0) { errno = (int)-fd; return NULL; } DIR *d = fdopendir((int)fd); @@ -571,7 +1538,7 @@ static FILE *fopen_common(const char *path, const char *m, FILE *(*real)(const c if (strchr(m, 'e')) flags |= O_CLOEXEC; if (strchr(m, 'x')) flags |= O_EXCL; long fd = fp_open(AT_FDCWD, path, flags, 0666); - if (fd == FP_SLOW) { misses++; return real(path, m); } + if (fd == FP_SLOW) { missed(__func__, AT_FDCWD, path); return real(path, m); } hits++; if (fd < 0) { errno = (int)-fd; return NULL; } FILE *f = fdopen((int)fd, m); @@ -580,3 +1547,243 @@ static FILE *fopen_common(const char *path, const char *m, FILE *(*real)(const c } FILE *fopen(const char *path, const char *m) { REAL(fopen, FILE *(*)(const char *, const char *)); return fopen_common(path, m, real_fopen); } FILE *fopen64(const char *path, const char *m) { REAL(fopen64, FILE *(*)(const char *, const char *)); return fopen_common(path, m, real_fopen64); } + +/* glibc reads /sys/devices/system/cpu/online with its own open for every get_nprocs(), and + * sysconf(_SC_NPROCESSORS_ONLN) is that: overlays and thread pools ask it every frame. */ +static int online_cpus(void) { + long fd = fp_open(AT_FDCWD, "/sys/devices/system/cpu/online", O_RDONLY | O_CLOEXEC, 0); + if (fd < 0) return -1; + char buf[256]; + long n = sc(SYS_read, fd, (long)buf, sizeof buf - 1, 0, 0); + sc(SYS_close, fd, 0, 0, 0, 0); + if (n <= 0) return -1; + buf[n] = 0; + int count = 0; + for (char *p = buf; *p && *p != '\n';) { + char *end; + long lo = strtol(p, &end, 10), hi = lo; + if (end == p) return -1; + if (*end == '-') { p = end + 1; hi = strtol(p, &end, 10); if (end == p || hi < lo) return -1; } + count += (int)(hi - lo + 1); + p = *end == ',' ? end + 1 : end; + } + return count > 0 ? count : -1; +} +int get_nprocs(void) { + REAL(get_nprocs, int (*)(void)); + int n = fp_ready() ? online_cpus() : -1; + if (n > 0) { hits++; return n; } + missed(__func__, AT_FDCWD, "(nprocs)"); + return real_get_nprocs(); +} +long sysconf(int name) { + REAL(sysconf, long (*)(int)); + if (name == _SC_NPROCESSORS_ONLN) return get_nprocs(); + return real_sysconf(name); +} + +/* ---------------------------------------------------------------- uname */ + +/* proot answers uname() with the utsname it started with (--kernel-release), the same for every + * call: asked once, it is every later answer. libX11 asks for each display connection, glibc's + * gethostname() through it. */ +static struct utsname uts; +static int uts_known; +static long own_uname(struct utsname *u) { + if (!fp_ready()) return FP_SLOW; + if (!__atomic_load_n(&uts_known, __ATOMIC_ACQUIRE)) { + struct utsname got; + if (syscall(SYS_uname, &got) != 0) return FP_SLOW; + static pthread_mutex_t uts_lock = PTHREAD_MUTEX_INITIALIZER; + if (pthread_mutex_trylock(&uts_lock) == 0) { + if (!uts_known) { + uts = got; + __atomic_store_n(&uts_known, 1, __ATOMIC_RELEASE); + } + pthread_mutex_unlock(&uts_lock); + } + *u = got; + return 0; + } + *u = uts; + return 0; +} + +int uname(struct utsname *u) { + REAL(uname, int (*)(struct utsname *)); + if (!u) return real_uname(u); + ANSWER(own_uname(u), AT_FDCWD, "(uname)", real_uname(u)); +} + +int gethostname(char *name, size_t len) { + REAL(gethostname, int (*)(char *, size_t)); + struct utsname u; + if (own_uname(&u) == FP_SLOW) { missed(__func__, AT_FDCWD, "(gethostname)"); return real_gethostname(name, len); } + hits++; + size_t n = strlen(u.nodename) + 1; + memcpy(name, u.nodename, len < n ? len : n); + if (n > len) { errno = ENAMETOOLONG; return -1; } + return 0; +} + +/* glibc's isatty() asks with TCGETS2, which proot rewrites to TCGETS (Android allows no termios2 + * request on a tty): one stop each way for every check. libudev and shells check all the time. */ +int isatty(int fd) { + REAL(isatty, int (*)(int)); + struct termios k; + if (!fp_ready()) return real_isatty(fd); + hits++; + long r = sc(SYS_ioctl, fd, TCGETS, (long)&k, 0, 0); + if (r < 0) { errno = (int)-r; return 0; } + return r == 0; +} + +/* ---------------------------------------------------------------- passwd */ + +/* + * glibc's NSS stats /etc/nsswitch.conf and reads /etc/passwd with its own calls on every lookup, + * two stops each: Xwayland looks a user up for every client, every shell for itself. When + * nsswitch.conf sends passwd to "files" first with no action after it, a lookup that finds its + * entry there ends there, so that case is answered here from the same file. Anything else - not + * found, a file nss_files might read differently from this strict reading, a buffer it might call + * too small - goes to glibc. + */ +static long read_small(const char *path, char *buf, size_t size) { + long fd = fp_open(AT_FDCWD, path, O_RDONLY | O_CLOEXEC, 0); + if (fd < 0) return -1; + long have = 0, r = 0; + while ((size_t)have < size && (r = sc(SYS_read, fd, (long)buf + have, size - have, 0, 0)) > 0) have += r; + sc(SYS_close, fd, 0, 0, 0, 0); + return (size_t)have < size && r == 0 ? have : -1; +} + +static int passwd_is_files(char *conf, size_t size) { + long n = read_small("/etc/nsswitch.conf", conf, size - 1); + if (n < 0) return 0; + conf[n] = 0; + int found = 0; + for (char *line = conf; line;) { + char *end = strchr(line, '\n'); + if (end) *end = 0; + if (strncmp(line, "passwd", 6) == 0 && strchr(" \t:", line[6])) { + char *p = line + 6; + while (*p == ' ' || *p == '\t') p++; + if (*p++ != ':' || found++ || strchr(p, '#')) return 0; + while (*p == ' ' || *p == '\t') p++; + if (strncmp(p, "files", 5) != 0 || (p[5] && p[5] != ' ' && p[5] != '\t')) return 0; + for (p += 5; *p == ' ' || *p == '\t'; p++) {} + if (*p == '[') return 0; + } else if (strstr(line, "passwd") && line[0] != '#') { + return 0; + } + line = end ? end + 1 : NULL; + } + return found; +} + +/* The line for `name` (or `uid`, name NULL) split in place into its seven fields; -1 for a file + * read strictly that has none, 0 when this cannot tell. `longest`: the longest line read up to it. */ +static int passwd_line(char *file, const char *name, uid_t uid, char **f, size_t *longest) { + *longest = 0; + for (char *line = file; line && *line;) { + char *end = strchr(line, '\n'); + if (end) *end = 0; + size_t len = strlen(line); + if (len > *longest) *longest = len; + if (line[0] && line[0] != '#') { + int k = 0; + f[k++] = line; + for (char *p = line; *p && k < 8; p++) + if (*p == ':') { *p = 0; if (k < 7) f[k] = p + 1; k++; } + if (k != 7 || !f[0][0] || strchr("+- \t", f[0][0])) return 0; + for (int i = 2; i < 4; i++) { + size_t d = strspn(f[i], "0123456789"); + if (d == 0 || d > 10 || f[i][d] || strtoul(f[i], NULL, 10) > 0xffffffffUL) return 0; + } + if (name ? strcmp(f[0], name) == 0 : (uid_t)strtoul(f[2], NULL, 10) == uid) return 1; + } + line = end ? end + 1 : NULL; + } + return -1; +} + +/* 1 with *pw filled from buf, 0 to leave it to glibc. */ +static int own_passwd(const char *name, uid_t uid, struct passwd *pw, char *buf, size_t buflen) { + char file[16384], *f[7]; + size_t longest; + if (!fp_ready() || !passwd_is_files(file, sizeof file)) return 0; + long n = read_small("/etc/passwd", file, sizeof file - 1); + if (n < 0) return 0; + file[n] = 0; + if (passwd_line(file, name, uid, f, &longest) != 1 || longest + 3 > buflen) return 0; + size_t off = 0; + char *s[7]; + for (int i = 0; i < 7; i++) { + size_t len = strlen(f[i]) + 1; + s[i] = buf + off; + memcpy(s[i], f[i], len); + off += len; + } + pw->pw_name = s[0]; + pw->pw_passwd = s[1]; + pw->pw_uid = (uid_t)strtoul(s[2], NULL, 10); + pw->pw_gid = (gid_t)strtoul(s[3], NULL, 10); + pw->pw_gecos = s[4]; + pw->pw_dir = s[5]; + pw->pw_shell = s[6]; + return 1; +} + +static int passwd_r(const char *name, uid_t uid, struct passwd *pw, char *buf, size_t buflen, struct passwd **result, + int (*real_name)(const char *, struct passwd *, char *, size_t, struct passwd **), + int (*real_uid)(uid_t, struct passwd *, char *, size_t, struct passwd **)) { + if (!own_passwd(name, uid, pw, buf, buflen)) { + missed(name ? "getpwnam" : "getpwuid", AT_FDCWD, "(passwd)"); + return name ? real_name(name, pw, buf, buflen, result) : real_uid(uid, pw, buf, buflen, result); + } + hits++; + *result = pw; + errno = 0; + return 0; +} + +int getpwnam_r(const char *name, struct passwd *pw, char *buf, size_t buflen, struct passwd **result) { + REAL(getpwnam_r, int (*)(const char *, struct passwd *, char *, size_t, struct passwd **)); + return passwd_r(name, 0, pw, buf, buflen, result, real_getpwnam_r, NULL); +} + +int getpwuid_r(uid_t uid, struct passwd *pw, char *buf, size_t buflen, struct passwd **result) { + REAL(getpwuid_r, int (*)(uid_t, struct passwd *, char *, size_t, struct passwd **)); + return passwd_r(NULL, uid, pw, buf, buflen, result, NULL, real_getpwuid_r); +} + +/* glibc's own getpwnam/getpwuid keep one static entry, as these do. */ +static struct passwd static_pw; +static char static_pw_buf[4096]; +static pthread_mutex_t static_pw_lock = PTHREAD_MUTEX_INITIALIZER; + +static struct passwd *passwd_static(const char *name, uid_t uid) { + pthread_mutex_lock(&static_pw_lock); + int own = own_passwd(name, uid, &static_pw, static_pw_buf, sizeof static_pw_buf); + pthread_mutex_unlock(&static_pw_lock); + if (!own) return NULL; + hits++; + errno = 0; + return &static_pw; +} + +struct passwd *getpwnam(const char *name) { + REAL(getpwnam, struct passwd *(*)(const char *)); + struct passwd *pw = passwd_static(name, 0); + if (pw) return pw; + missed(__func__, AT_FDCWD, "(passwd)"); + return real_getpwnam(name); +} + +struct passwd *getpwuid(uid_t uid) { + REAL(getpwuid, struct passwd *(*)(uid_t)); + struct passwd *pw = passwd_static(NULL, uid); + if (pw) return pw; + missed(__func__, AT_FDCWD, "(passwd)"); + return real_getpwuid(uid); +}