mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 13:55:58 +00:00
Merge remote-tracking branch 'droiddeck/main' into maint/appimage-x64-wayland-runtime
This commit is contained in:
commit
0d6c18b300
562 files changed
+71029
-1963
No files matched your search
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
# Opens a URL in Android's browser on the bottom screen.
|
||||
url=$1
|
||||
[ -n "$url" ] || exit 1
|
||||
serial=${ADB_SERIAL:-127.0.0.1:5555}
|
||||
adb connect "$serial" >/dev/null 2>&1
|
||||
quoted=$(printf "%s" "$url" | sed "s/'/'\\\\''/g")
|
||||
adb -s "$serial" shell "am start --display 2 -a android.intent.action.VIEW -d '$quoted'" >/dev/null 2>&1 ||
|
||||
echo "Open this on any device: $url" >&2
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
# Claude Code in the DroidDeck checkout, with sign-in links opened in Android's browser.
|
||||
export ADB_SERIAL=${ADB_SERIAL:-127.0.0.1:5555} PATH="$HOME/.local/bin:$PATH" BROWSER="$HOME/.local/bin/android-open"
|
||||
adb connect "$ADB_SERIAL" >/dev/null 2>&1
|
||||
cd "$HOME/DroidDeck" && exec claude "$@"
|
||||
Executable
+52
@@ -0,0 +1,52 @@
|
||||
#!/bin/bash
|
||||
# Starts GitHub's device login, floats its code over Android's browser and opens the approval page.
|
||||
url=https://github.com/login/device
|
||||
inbox=${DD_AGENT_INBOX:-}
|
||||
|
||||
# DroidDeck takes $inbox/device-code within a few seconds when it can float the code.
|
||||
float_code() {
|
||||
[ -n "$inbox" ] && [ -d "$inbox" ] || return 1
|
||||
rm -f "$inbox/device-code.result"
|
||||
printf '%s %s\n' "$1" "$url" > "$inbox/device-code.part" && mv -f "$inbox/device-code.part" "$inbox/device-code" || return 1
|
||||
i=0
|
||||
while [ $i -lt 40 ] && [ -e "$inbox/device-code" ]; do
|
||||
sleep 0.2
|
||||
i=$((i + 1))
|
||||
done
|
||||
if [ -e "$inbox/device-code" ]; then
|
||||
rm -f "$inbox/device-code"
|
||||
return 1
|
||||
fi
|
||||
# DroidDeck writes "ok" or "failed" once it has tried to open the page; no answer counts as failed.
|
||||
i=0
|
||||
while [ $i -lt 50 ] && [ ! -e "$inbox/device-code.result" ]; do
|
||||
sleep 0.2
|
||||
i=$((i + 1))
|
||||
done
|
||||
result=$(cat "$inbox/device-code.result" 2>/dev/null)
|
||||
rm -f "$inbox/device-code.result"
|
||||
[ "$result" = ok ]
|
||||
}
|
||||
|
||||
coproc GH { BROWSER=true gh auth login --hostname github.com --git-protocol https --web 2>&1; }
|
||||
gh_pid=$GH_PID
|
||||
exec {ghout}<&"${GH[0]}" {ghin}>&"${GH[1]}"
|
||||
shown=
|
||||
while IFS= read -r line <&"$ghout"; do
|
||||
code=$(grep -oE '[A-Z0-9]{4}-[A-Z0-9]{4}' <<<"$line" | head -1)
|
||||
# gh's own prompts would push the code out of the one line the setup card shows.
|
||||
case "$line" in
|
||||
*"one-time code"* | *"Press Enter"* | *"Open this URL"*) ;;
|
||||
*) printf '%s\n' "$line" ;;
|
||||
esac
|
||||
if [ -n "$code" ] && [ -z "$shown" ]; then
|
||||
shown=1
|
||||
echo >&"$ghin"
|
||||
printf 'GitHub device code: %s · github.com/login/device\n' "$code"
|
||||
float_code "$code" || android-open "$url"
|
||||
fi
|
||||
done
|
||||
wait "$gh_pid"
|
||||
status=$?
|
||||
[ -n "$shown" ] && [ -n "$inbox" ] && [ -d "$inbox" ] && : > "$inbox/device-code"
|
||||
exit $status
|
||||
Executable
+245
@@ -0,0 +1,245 @@
|
||||
#!/bin/bash
|
||||
# Builds this checkout as a Lab build of the installed DroidDeck and restarts the app on it.
|
||||
# Usage: dd-lab-build [--no-restart | --prepare] [--explain]
|
||||
# --prepare compiles the installed commit without making a Lab build, so later builds only
|
||||
# compile what changed. Setup runs it once; it takes the longest.
|
||||
# --explain logs why each task ran and Kotlin's build report, to find out what makes a build slow.
|
||||
set -euo pipefail
|
||||
pkg=${DROIDDECK_PACKAGE:-}
|
||||
if [ -z "$pkg" ] && [[ ${DD_AGENT_INBOX:-} =~ ^/data/(user/[0-9]+|data)/([^/]+)/files/agent$ ]]; then
|
||||
pkg=${BASH_REMATCH[2]}
|
||||
fi
|
||||
pkg=${pkg:-com.droiddeck.launcher}
|
||||
serial=${ADB_SERIAL:-127.0.0.1:5555}
|
||||
mode=build
|
||||
explain=()
|
||||
for arg in "$@"; do
|
||||
case $arg in
|
||||
--package=*) pkg=${arg#--package=} ;;
|
||||
--no-restart) mode=no-restart ;;
|
||||
--prepare) mode=prepare ;;
|
||||
--explain) explain=(--info -Pkotlin.build.report.output=file) ;;
|
||||
esac
|
||||
done
|
||||
started=$(date +%s)
|
||||
cache=$HOME/.cache
|
||||
progress_file=$cache/dd-lab-build.json
|
||||
times_file=$cache/dd-lab-build-times
|
||||
mkdir -p "$cache"
|
||||
# The Agent tab reads this file to show how far the build has got and how long is left.
|
||||
progress() { # stage [permille task done total eta]
|
||||
local task=${3:-}; task=${task//\\/}; task=${task//\"/\'}; task=${task//$'\n'/ }
|
||||
printf '{"stage":"%s","mode":"%s","permille":%d,"task":"%s","done":%d,"total":%d,"eta":%d,"elapsed":%d,"pid":%d}\n' \
|
||||
"$1" "$mode" "${2:-0}" "$task" "${4:-0}" "${5:-0}" "${6:--1}" $(( $(date +%s) - started )) $$ > "$progress_file.part.$BASHPID"
|
||||
mv "$progress_file.part.$BASHPID" "$progress_file"
|
||||
}
|
||||
fail() { progress failed 0 "$*"; echo "dd-lab-build: $*" >&2; exit 1; }
|
||||
trap 'progress failed 0 stopped; exit 143' INT TERM
|
||||
trap 'progress failed 0 "a command failed on line $LINENO"' ERR
|
||||
[[ $pkg =~ ^[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z][A-Za-z0-9_]*)+$ ]] || fail "invalid Android package: $pkg"
|
||||
export DROIDDECK_PACKAGE=$pkg
|
||||
device() { adb -s "$serial" shell "$@" | tr -d '\r'; }
|
||||
|
||||
# The app's agent loses every process it started when its turn ends, a build included, so it asks
|
||||
# the app to run the build in a process of its own and follows that build here.
|
||||
agent=${DD_AGENT_INBOX:-/data/user/0/$pkg/files/agent}
|
||||
if [ -n "${DD_AGENT:-}" ] && [ -z "${DD_LAB_DETACHED:-}" ] && [ -d "$agent" ]; then
|
||||
trap - ERR
|
||||
log=$cache/dd-lab-build.log
|
||||
rm -f "$progress_file"
|
||||
python3 -c 'import json,os,sys; print(json.dumps({"cwd": os.getcwd(), "args": [*sys.argv[1:], "--package=" + os.environ["DROIDDECK_PACKAGE"]]}))' "$@" > "$agent/lab-request.part"
|
||||
mv "$agent/lab-request.part" "$agent/lab-request"
|
||||
echo "DroidDeck runs this build itself, so it keeps going if this turn ends. Its log follows."
|
||||
for _ in $(seq 1 30); do [ -f "$progress_file" ] && break; sleep 1; done
|
||||
[ -f "$progress_file" ] || { echo "dd-lab-build: DroidDeck did not start the build; is it running?" >&2; exit 1; }
|
||||
tail -n +1 -F "$log" 2>/dev/null & follow=$!
|
||||
result=1
|
||||
while sleep 2; do
|
||||
stage=$(sed -n 's/.*"stage":"\([a-z]*\)".*/\1/p' "$progress_file")
|
||||
pid=$(sed -n 's/.*"pid":\([0-9]*\).*/\1/p' "$progress_file")
|
||||
case $stage in
|
||||
ready|prepared) result=0; break ;;
|
||||
failed) break ;;
|
||||
esac
|
||||
[ -n "$pid" ] && [ -d "/proc/$pid" ] || break
|
||||
done
|
||||
sleep 1; kill $follow 2>/dev/null || true
|
||||
exit $result
|
||||
fi
|
||||
|
||||
progress checking
|
||||
repo=$(git rev-parse --show-toplevel) || fail "run it inside the DroidDeck checkout"
|
||||
cd "$repo"
|
||||
adb connect "$serial" >/dev/null 2>&1 || true
|
||||
|
||||
state=$(python3 tools/droiddeckctl state 2>/dev/null | sed -n '/^{/,$p') || fail "could not read DroidDeck's state; is it running and is adb connected?"
|
||||
# A Lab build reports its own label, so the APK's commit comes from apkCommit when the app has it.
|
||||
label=$(python3 -c 'import json,sys; s=json.load(sys.stdin); print(s.get("apkCommit") or s["build"].rsplit(" at ", 1)[-1])' <<<"$state") ||
|
||||
fail "DroidDeck's state did not say which build is installed"
|
||||
installed_label=$(python3 -c 'import json,re,sys; print(re.sub(r"^(?:\w+ \w+ · |lab on )+", "", json.load(sys.stdin)["build"]))' <<<"$state")
|
||||
if ! base=$(git rev-parse --verify --quiet "$label^{commit}"); then
|
||||
progress fetching 0 "the installed commit"
|
||||
git fetch --quiet --tags origin >/dev/null 2>&1 || true
|
||||
git fetch --quiet origin main >/dev/null 2>&1 || true
|
||||
base=$(git rev-parse --verify --quiet "$label^{commit}") || fail "the installed build ($label) is not in this checkout or on GitHub's main; fetch it first"
|
||||
fi
|
||||
|
||||
if [ $mode = prepare ]; then
|
||||
# Compiling the installed commit itself gives later builds of branches on it incremental caches.
|
||||
if ! git merge-base --is-ancestor "$base" HEAD; then
|
||||
git diff --quiet && git diff --cached --quiet || fail "HEAD does not build on the installed commit and has uncommitted changes"
|
||||
was=$(git branch --show-current)
|
||||
git checkout --quiet --detach "$base"
|
||||
echo "Checked out the installed commit ${base:0:9} to prepare it${was:+; $was is unchanged}"
|
||||
fi
|
||||
else
|
||||
git diff --quiet && git diff --cached --quiet || fail "commit your changes first"
|
||||
git merge-base --is-ancestor "$base" HEAD || fail "HEAD does not build on the installed commit ${base:0:9}; branch from it"
|
||||
blocked=$(git diff --name-only "$base" HEAD | grep -E 'AndroidManifest\.xml$|/cpp/|/jniLibs/|\.rs$|(^|/)build\.gradle$|/App\.kt$|/AppLanguage\.kt$|/lab/LabOverlay\.kt$' || true)
|
||||
[ -z "$blocked" ] || fail "these need a new APK, not a Lab build: $(echo $blocked)"
|
||||
fi
|
||||
apk=$(device pm path $pkg | sed -n 's/^package://p' | grep 'base\.apk$' | head -1)
|
||||
[ -n "$apk" ] || fail "DroidDeck is not installed"
|
||||
|
||||
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-arm64 ANDROID_HOME=$HOME/android-sdk
|
||||
# BuildConfig describes the installed base, not each Lab commit; its commit changing would make the
|
||||
# Kotlin compile redo the whole app (the overlay's own label names the Lab build). Updates reads the
|
||||
# installed variant (non-adreno) from the build label.
|
||||
export DROIDDECK_SOURCE_COMMIT=$base DROIDDECK_BUILD_LABEL="lab on $installed_label" DROIDDECK_BUILD_TREE_STATE=clean GITHUB_HEAD_REF=lab
|
||||
printf 'sdk.dir=%s\n' "$ANDROID_HOME" > local.properties
|
||||
if [ ! -d "$ANDROID_HOME/platforms/android-34" ]; then
|
||||
progress sdk
|
||||
(set +o pipefail; yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses >/dev/null)
|
||||
"$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "platforms;android-34" "build-tools;35.0.0" >/dev/null
|
||||
fi
|
||||
|
||||
# A full Kotlin compile of the app needs about 2.5 GB of heap and Gradle about 1 GB; smaller devices get
|
||||
# what they can spare. Both daemons end with this script's process when the build is done.
|
||||
total_mb=$(awk '/^MemTotal:/ {print int($2 / 1024)}' /proc/meminfo)
|
||||
heap=$(( total_mb / 2 )); [ $heap -gt 4096 ] && heap=4096; [ $heap -lt 2048 ] && heap=2048
|
||||
heap=${DD_LAB_HEAP_MB:-$heap}
|
||||
kotlin_heap=$(( heap * 2 / 3 )); gradle_heap=$(( heap - kotlin_heap ))
|
||||
|
||||
# Only the Kotlin daemon compiles incrementally; in-process compiles always redo the whole app, and the
|
||||
# caches they leave behind make the daemon's first incremental compile fail.
|
||||
if [ "$(cat "$cache/dd-lab-build-strategy" 2>/dev/null)" != daemon ]; then
|
||||
rm -rf app/build/kotlin/compileReleaseKotlin
|
||||
echo daemon > "$cache/dd-lab-build-strategy"
|
||||
fi
|
||||
|
||||
# Google publishes aapt2 for x86-64 Linux only; setup wraps it in qemu-user at ~/aapt2/aapt2.
|
||||
gradle=(./gradlew :app:processReleaseResources :app:mergeDexRelease --max-workers=2 -PskipRust=true -x buildVkBridgeServer
|
||||
-Pandroid.aapt2FromMavenOverride=$HOME/aapt2/aapt2 -Pkotlin.compiler.execution.strategy=daemon
|
||||
-Pkotlin.daemon.jvmargs=-Xmx${kotlin_heap}m -Dorg.gradle.jvmargs=-Xmx${gradle_heap}m --console=plain)
|
||||
progress planning
|
||||
plan_file=$cache/dd-lab-build-plan
|
||||
plan_key=$(
|
||||
{
|
||||
printf '%s\n' "$repo" "${gradle[@]}"
|
||||
git ls-files -z -- '*gradle*' | xargs -0 -r sha256sum
|
||||
} | sha256sum | cut -d ' ' -f1
|
||||
)
|
||||
if [ "$(head -n 1 "$plan_file" 2>/dev/null)" != "$plan_key" ]; then
|
||||
plan_output=$("${gradle[@]}" --dry-run) || fail "Gradle planning failed: $plan_output"
|
||||
{ printf '%s\n' "$plan_key"; awk '/^:/ {print $1}' <<<"$plan_output"; } > "$plan_file.part"
|
||||
mv "$plan_file.part" "$plan_file"
|
||||
fi
|
||||
mapfile -t plan < <(tail -n +2 "$plan_file")
|
||||
total=${#plan[@]}
|
||||
|
||||
# Each task's estimate is how long it took last time; a full compile of the app takes about 15 minutes.
|
||||
declare -A estimate
|
||||
[ -f "$times_file" ] && while read -r t s; do estimate[$t]=$s; done < "$times_file"
|
||||
estimate_of() { local e=${estimate[$1]:-}; [ -n "$e" ] && echo "$e" || { [[ $1 == *compileReleaseKotlin ]] && echo 900 || echo 2; }; }
|
||||
planned=0
|
||||
for t in "${plan[@]}"; do planned=$(( planned + $(estimate_of "$t") )); done
|
||||
[ $planned -gt 0 ] || planned=1
|
||||
|
||||
current=$(mktemp)
|
||||
measured=$(mktemp)
|
||||
# "<tasks started> <task> <its start> <estimated seconds of tasks done> <its estimate>"
|
||||
echo "0 starting $(date +%s) 0 0" > "$current"
|
||||
report() {
|
||||
local n task since done_s cur_est now part permille left
|
||||
read -r n task since done_s cur_est < "$current"
|
||||
now=$(date +%s)
|
||||
part=$(( now - since )); [ $part -gt $(( cur_est * 95 / 100 )) ] && part=$(( cur_est * 95 / 100 ))
|
||||
permille=$(( (done_s + part) * 1000 / planned )); [ $permille -gt 990 ] && permille=990
|
||||
left=$(( planned - done_s - part )); [ $left -lt 0 ] && left=0
|
||||
progress building "$permille" "$task" "$n" "$total" "$left"
|
||||
echo "$n $task $(( (now - started) / 60 )) $left"
|
||||
}
|
||||
(
|
||||
beat=0
|
||||
while sleep 5; do
|
||||
set -- $(report)
|
||||
beat=$(( beat + 1 ))
|
||||
[ $(( beat % 12 )) = 0 ] && echo "[build $1/$total] still on $2, $3m in, about $(( $4 / 60 + 1 ))m left"
|
||||
done
|
||||
) &
|
||||
heartbeat=$!
|
||||
trap 'kill $heartbeat 2>/dev/null || true; rm -f "$current" "$measured"' EXIT
|
||||
set +o pipefail
|
||||
"${gradle[@]}" ${explain[@]+"${explain[@]}"} 2>&1 | {
|
||||
n=0 done_s=0 task="" since=0 cur_est=0 ran=0
|
||||
while IFS= read -r line; do
|
||||
printf '%s\n' "$line"
|
||||
case $line in
|
||||
"> Task "*)
|
||||
now=$(date +%s)
|
||||
# A task's line comes when it starts, or when it ends for one that did no work.
|
||||
if [ -n "$task" ]; then
|
||||
done_s=$(( done_s + cur_est ))
|
||||
[ $ran = 1 ] && echo "$task $(( now - since ))" >> "$measured"
|
||||
fi
|
||||
rest=${line#> Task }; task=${rest%% *}
|
||||
case $rest in *" "*) ran=0 ;; *) ran=1 ;; esac
|
||||
n=$((n + 1)); since=$now; cur_est=$(estimate_of "$task")
|
||||
echo "$n $task $since $done_s $cur_est" > "$current"
|
||||
[ $ran = 1 ] && report >/dev/null && echo "[build $n/$total] $task" ;;
|
||||
esac
|
||||
done
|
||||
if [ -n "$task" ] && [ $ran = 1 ]; then echo "$task $(( $(date +%s) - since ))" >> "$measured"; fi
|
||||
}
|
||||
status=${PIPESTATUS[0]}
|
||||
set -o pipefail
|
||||
kill $heartbeat 2>/dev/null || true
|
||||
[ "$status" = 0 ] || fail "Gradle failed (exit $status); the reason is above"
|
||||
# Tasks that did no work this time keep their earlier time.
|
||||
{ [ -f "$times_file" ] && cat "$times_file"; cat "$measured"; } | awk '{t[$1] = $2} END {for (k in t) print k, t[k]}' > "$times_file.part"
|
||||
mv "$times_file.part" "$times_file"
|
||||
# The app reads which installed commit the build caches are warm for.
|
||||
echo "$base" > "$cache/dd-lab-build-base"
|
||||
|
||||
if [ $mode = prepare ]; then
|
||||
progress prepared 1000
|
||||
echo "Prepared: Lab builds of branches on ${base:0:9} now compile only what changed ($(( ($(date +%s) - started) / 60 ))m)"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
progress packaging 995
|
||||
linked=$(ls app/build/intermediates/linked_resources_binary_format/release/processReleaseResources/*.ap_ | head -1)
|
||||
work=$(mktemp -d)
|
||||
cp "$linked" "$work/overlay.apk"
|
||||
(cd app/build/intermediates/dex/release/mergeDexRelease && zip -q "$work/overlay.apk" classes*.dex)
|
||||
name="$(git branch --show-current) @ $(git rev-parse --short HEAD)"
|
||||
python3 -c 'import json,sys,time; print(json.dumps({"label": sys.argv[1], "baseCommit": sys.argv[2], "baseApk": sys.argv[3], "built": int(time.time())}))' \
|
||||
"$name" "$label" "$apk" > "$work/overlay.json"
|
||||
|
||||
lab=/data/user/0/$pkg/files/lab
|
||||
mkdir -p "$lab"
|
||||
rm -rf "$lab/next.part" "$lab/next"
|
||||
mv "$work" "$lab/next.part"
|
||||
mv "$lab/next.part" "$lab/next"
|
||||
progress ready 1000 "$name"
|
||||
echo "Lab build ready: $name ($(du -h "$lab/next/overlay.apk" | cut -f1)) in $(( ($(date +%s) - started) / 60 ))m"
|
||||
[ $mode = build ] || exit 0
|
||||
|
||||
# Classes in an app image load before the Lab build can, so the app is recompiled without one.
|
||||
device cmd package compile -m verify -f $pkg >/dev/null
|
||||
progress restarting 1000 "$name"
|
||||
echo "Restarting DroidDeck on the Lab build; it shows a notification when it is back"
|
||||
# The app's own agent ends with the app, so it is told to carry on once the app is back.
|
||||
if [ -n "${DD_AGENT:-}" ]; then mkdir -p "$agent" && : > "$agent/resume"; fi
|
||||
device "nohup sh -c 'sleep 3; monkey -p $pkg -c android.intent.category.LAUNCHER 1' >/dev/null 2>&1 &"
|
||||
for pid in $(device pidof $pkg); do kill "$pid" 2>/dev/null || true; done
|
||||
Executable
+79
@@ -0,0 +1,79 @@
|
||||
#!/bin/bash
|
||||
# Sets up the on-device agent inside DroidDeck's Linux userland: Claude Code, git and gh, loopback
|
||||
# adb for droiddeckctl, and the toolchain dd-lab-build uses to build Lab builds on the device.
|
||||
# Run it from the DroidDeck terminal, inside a checkout of the DroidDeck repository.
|
||||
set -euo pipefail
|
||||
here=$(cd "$(dirname "$0")" && pwd)
|
||||
export DEBIAN_FRONTEND=noninteractive
|
||||
tmp=$(mktemp -d)
|
||||
trap 'rm -rf "$tmp"' EXIT
|
||||
|
||||
apt-get update
|
||||
apt-get install -y --no-install-recommends git gh adb python3 zip unzip curl openjdk-17-jdk-headless qemu-user zipalign apksigner
|
||||
|
||||
command -v claude >/dev/null || [ -x "$HOME/.local/bin/claude" ] ||
|
||||
{ curl -fsSL -o "$tmp/install-claude.sh" https://claude.ai/install.sh && bash "$tmp/install-claude.sh"; }
|
||||
|
||||
mkdir -p "$HOME/.local/bin"
|
||||
for tool in dd-agent dd-lab-build dd-gh-login android-open; do install -m755 "$here/$tool" "$HOME/.local/bin/$tool"; done
|
||||
# DroidDeck passes the live loopback adb port; a fixed one here would go stale whenever wireless debugging is toggled.
|
||||
sed -i 's|ADB_SERIAL=127.0.0.1:5555 |ADB_SERIAL="${ADB_SERIAL:-127.0.0.1:5555}" |' "$HOME/.bashrc" 2>/dev/null || true
|
||||
grep -q 'droiddeck-agent-path' "$HOME/.bashrc" 2>/dev/null ||
|
||||
printf '\n# droiddeck-agent-path\nexport PATH="$HOME/.local/bin:$PATH" ADB_SERIAL="${ADB_SERIAL:-127.0.0.1:5555}" BROWSER="$HOME/.local/bin/android-open"\n' >> "$HOME/.bashrc"
|
||||
|
||||
# Google publishes aapt2 for x86-64 Linux only; it runs here under qemu-user with Ubuntu's x86-64 runtime.
|
||||
x86=$HOME/x86root
|
||||
mkdir -p "$HOME/aapt2/x86" "$x86/etc"
|
||||
if [ ! -x "$HOME/aapt2/x86/aapt2" ]; then
|
||||
agp=$(sed -n "s/.*com.android.tools.build:gradle:\([0-9.]*\).*/\1/p" "$here/../../build.gradle" | head -1)
|
||||
[ -n "$agp" ] || { echo "setup: no Android Gradle plugin version in build.gradle" >&2; exit 1; }
|
||||
aapt2=$(curl -fsSL "https://dl.google.com/android/maven2/com/android/tools/build/aapt2/maven-metadata.xml" |
|
||||
{ grep -o "<version>$agp-[0-9]*</version>" || true; } | tail -1 | sed 's/<[^>]*>//g')
|
||||
[ -n "$aapt2" ] || { echo "setup: Google publishes no aapt2 for AGP $agp" >&2; exit 1; }
|
||||
curl -fsSL -o "$tmp/aapt2.jar" "https://dl.google.com/android/maven2/com/android/tools/build/aapt2/$aapt2/aapt2-$aapt2-linux.jar"
|
||||
unzip -o -q "$tmp/aapt2.jar" aapt2 -d "$HOME/aapt2/x86"
|
||||
chmod +x "$HOME/aapt2/x86/aapt2"
|
||||
fi
|
||||
if [ ! -e "$x86/lib64/ld-linux-x86-64.so.2" ]; then
|
||||
codename=$(. /etc/os-release && echo "$VERSION_CODENAME")
|
||||
curl -fsSL "https://archive.ubuntu.com/ubuntu/dists/$codename/main/binary-amd64/Packages.gz" | gunzip > "$tmp/amd64-packages"
|
||||
for package in libc6 libgcc-s1 libstdc++6 zlib1g; do
|
||||
read -r file sum < <(awk -v p="$package" '$0 == "Package: " p {found = 1} found && /^Filename:/ {f = $2}
|
||||
found && /^SHA256:/ {s = $2} found && /^$/ {exit} END {print f, s}' "$tmp/amd64-packages")
|
||||
[ -n "$sum" ] || { echo "setup: $package is not in Ubuntu $codename's amd64 index" >&2; exit 1; }
|
||||
curl -fsSL -o "$tmp/$package.deb" "https://archive.ubuntu.com/ubuntu/$file"
|
||||
echo "$sum $tmp/$package.deb" | sha256sum -c --quiet -
|
||||
dpkg-deb -x "$tmp/$package.deb" "$x86"
|
||||
done
|
||||
ln -sfn usr/lib64 "$x86/lib64"
|
||||
ln -sfn usr/lib "$x86/lib"
|
||||
: > "$x86/etc/ld.so.preload"
|
||||
fi
|
||||
# AGP only accepts a custom aapt2 whose file is named aapt2.
|
||||
printf '#!/bin/sh\nexec qemu-x86_64 -L %s %s/aapt2/x86/aapt2 "$@"\n' "$x86" "$HOME" > "$HOME/aapt2/aapt2"
|
||||
chmod +x "$HOME/aapt2/aapt2"
|
||||
"$HOME/aapt2/aapt2" version
|
||||
|
||||
sdk=$HOME/android-sdk
|
||||
if [ ! -x "$sdk/cmdline-tools/latest/bin/sdkmanager" ]; then
|
||||
mkdir -p "$sdk/cmdline-tools"
|
||||
curl -fsSL -o "$tmp/cmdline-tools.zip" https://dl.google.com/android/repository/commandlinetools-linux-13114758_latest.zip
|
||||
unzip -q -o "$tmp/cmdline-tools.zip" -d "$sdk/cmdline-tools"
|
||||
rm -rf "$sdk/cmdline-tools/latest"
|
||||
mv "$sdk/cmdline-tools/cmdline-tools" "$sdk/cmdline-tools/latest"
|
||||
fi
|
||||
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-arm64
|
||||
(set +o pipefail; yes | "$sdk/cmdline-tools/latest/bin/sdkmanager" --licenses >/dev/null)
|
||||
"$sdk/cmdline-tools/latest/bin/sdkmanager" "platforms;android-34" "build-tools;35.0.0" >/dev/null
|
||||
|
||||
adb connect 127.0.0.1:5555 || true
|
||||
# The first build compiles the whole app; doing it now makes the agent's Lab builds incremental.
|
||||
if [ "${DD_SKIP_PREPARE:-}" != 1 ]; then
|
||||
echo "Approve the USB debugging prompt if one appears."
|
||||
if timeout 120 adb -s 127.0.0.1:5555 wait-for-device; then
|
||||
"$here/dd-lab-build" --prepare || echo "The first Lab build will compile the whole app"
|
||||
else
|
||||
echo "adb did not connect, so the first Lab build will compile the whole app"
|
||||
fi
|
||||
fi
|
||||
echo "Done. Run dd-agent."
|
||||
@@ -133,6 +133,7 @@ docker run --rm --platform "${docker_platform}" \
|
||||
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"
|
||||
bash tools/nativeguest/build.sh "$d/libddnatv-hook.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"
|
||||
@@ -424,6 +425,22 @@ if ! restore_step comet "${comet_key}" "${comet_out}"; then
|
||||
DROIDDECK_BUILD_IMAGE="${image_name}" bash tools/comet/build-local.sh
|
||||
save_step comet "${comet_key}" "${comet_out}"
|
||||
fi
|
||||
if [[ "${DROIDDECK_SKIP_RUST:-0}" != 1 ]]; then
|
||||
upc_out="${linuxfs_dir}/usr/local/lib/droiddeck-ubisoft"
|
||||
upc_key=$(inputs_hash "x86_64-pc-windows-gnu" tools/ubisoft-launcher/src tools/ubisoft-launcher/Cargo.toml tools/ubisoft-launcher/Cargo.lock tools/ubisoft-launcher/build.sh)
|
||||
if ! restore_step ubisoft "${upc_key}" "${upc_out}"; then
|
||||
command -v x86_64-w64-mingw32-gcc >/dev/null 2>&1 || { echo "x86_64-w64-mingw32-gcc is required for the Ubisoft launcher service (droiddeck-upc.exe)." >&2; exit 1; }
|
||||
CARGO_TARGET_DIR="${cache_dir}/ubisoft-target" bash tools/ubisoft-launcher/build.sh "${upc_out}"
|
||||
save_step ubisoft "${upc_key}" "${upc_out}"
|
||||
fi
|
||||
rockstar_out="${repo_root}/app/src/main/resources/rockstar/helpers"
|
||||
rockstar_key=$(inputs_hash "windows-gnu" tools/rockstar-launcher/src tools/rockstar-launcher/Cargo.toml tools/rockstar-launcher/Cargo.lock tools/rockstar-launcher/build.sh)
|
||||
if ! restore_step rockstar "${rockstar_key}" "${rockstar_out}"; then
|
||||
command -v i686-w64-mingw32-gcc >/dev/null 2>&1 || { echo "i686-w64-mingw32-gcc is required for the Rockstar launcher service." >&2; exit 1; }
|
||||
CARGO_TARGET_DIR="${cache_dir}/rockstar-target" bash tools/rockstar-launcher/build.sh "${rockstar_out}"
|
||||
save_step rockstar "${rockstar_key}" "${rockstar_out}"
|
||||
fi
|
||||
fi
|
||||
python3 tools/powervr/fetch.py
|
||||
bash tools/packs/build.sh app/build/packs
|
||||
python3 tools/release/runtime_inputs.py record
|
||||
|
||||
@@ -31,15 +31,22 @@ install_packages() {
|
||||
if install_packages; then
|
||||
exit 0
|
||||
fi
|
||||
echo "::warning::Runner mirror failed or timed out; retrying with the official Ubuntu mirror."
|
||||
[ -f "$ubuntu_sources" ] || exit 1
|
||||
sources=$(mktemp --suffix=.sources)
|
||||
trap 'rm -f "$sources"' EXIT
|
||||
mirror=https://archive.ubuntu.com/ubuntu
|
||||
# The last mirror of each list is outside Canonical's network, so it still answers when runners cannot reach Canonical.
|
||||
mirrors=(https://archive.ubuntu.com/ubuntu https://mirrors.edge.kernel.org/ubuntu)
|
||||
case "$(dpkg --print-architecture)" in
|
||||
arm64) mirror=https://ports.ubuntu.com/ubuntu-ports ;;
|
||||
arm64) mirrors=(https://ports.ubuntu.com/ubuntu-ports https://mirrors.ocf.berkeley.edu/ubuntu-ports) ;;
|
||||
esac
|
||||
sed -E "s@^URIs:.*@URIs: $mirror@" "$ubuntu_sources" > "$sources"
|
||||
chmod 644 "$sources"
|
||||
options[1]="Dir::Etc::sourcelist=$sources"
|
||||
install_packages
|
||||
status=1
|
||||
for mirror in "${mirrors[@]}"; do
|
||||
echo "::warning::Ubuntu mirror failed or timed out; retrying with $mirror."
|
||||
sed -E "s@^URIs:.*@URIs: $mirror@" "$ubuntu_sources" > "$sources"
|
||||
chmod 644 "$sources"
|
||||
options[1]="Dir::Etc::sourcelist=$sources"
|
||||
status=0
|
||||
install_packages || status=$?
|
||||
[ "$status" = 0 ] && exit 0
|
||||
done
|
||||
exit "$status"
|
||||
@@ -12,6 +12,7 @@ aarch64-linux-gnu-strip --strip-unneeded "$D/libblsession.so"
|
||||
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"
|
||||
bash tools/nativeguest/build.sh "$D/libddnatv-hook.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"
|
||||
|
||||
@@ -51,6 +51,7 @@ def source_files(group, root=ROOT, commit=None, patterns=None):
|
||||
if not paths:
|
||||
raise ValueError('No preload inputs found')
|
||||
result = {}
|
||||
listed = set(paths)
|
||||
pending = list(paths)
|
||||
while pending:
|
||||
path = pending.pop()
|
||||
@@ -58,7 +59,13 @@ def source_files(group, root=ROOT, commit=None, patterns=None):
|
||||
continue
|
||||
if path.startswith('../') or path.startswith('/'):
|
||||
raise ValueError('Preload include escapes repository')
|
||||
content = read(path).replace(b'\r\n', b'\n')
|
||||
try:
|
||||
content = read(path).replace(b'\r\n', b'\n')
|
||||
except (OSError, subprocess.CalledProcessError):
|
||||
# a header the build generates (gosys/gosys_blob.h, included behind __has_include)
|
||||
if path in listed:
|
||||
raise
|
||||
continue
|
||||
result[path] = sha(content)
|
||||
for include in re.findall(rb'^\s*#\s*include\s*"([^"\n]+)"', content, re.M):
|
||||
pending.append(posixpath.normpath(posixpath.join(posixpath.dirname(path), include.decode())))
|
||||
@@ -126,13 +133,15 @@ def artifact_bytes(origin, cache):
|
||||
if len(names) != 1:
|
||||
raise ValueError('Expected one original APK')
|
||||
with ZipFile(io.BytesIO(archive.read(names[0]))) as apk:
|
||||
return {name: apk.read('assets/linuxfs/' + name) for name in FILES}
|
||||
present = set(apk.namelist())
|
||||
return {name: apk.read('assets/linuxfs/' + name) for name in FILES if 'assets/linuxfs/' + name in present}
|
||||
|
||||
|
||||
def validate_lock(data):
|
||||
if data.get('version') != 1 or set(data['groups']) != set(INPUTS):
|
||||
raise ValueError('Unsupported preload snapshot')
|
||||
if set(data['outputs']) != FILES:
|
||||
# A lock made before a recipe gained an output lacks it; that group is rebuilt (restore)
|
||||
if not set(data['outputs']) <= FILES:
|
||||
raise ValueError('Unexpected preload output set')
|
||||
for digest in [data['archive_sha256'], *data['outputs'].values()]:
|
||||
if len(digest) != 64 or any(c not in '0123456789abcdef' for c in digest):
|
||||
@@ -160,7 +169,7 @@ def producer_sources(group, commit, root=ROOT):
|
||||
|
||||
def prove_origin(data, cache, root=ROOT):
|
||||
content = artifact_bytes(data['origin'], cache)
|
||||
if {name: sha(value) for name, value in content.items()} != data['outputs']:
|
||||
if {name: sha(value) for name, value in content.items() if name in data['outputs']} != data['outputs']:
|
||||
raise ValueError('Snapshot binaries differ from the original main APK')
|
||||
commit = data['origin']['commit']
|
||||
run('git', 'fetch', '--no-tags', 'origin', commit, root=root)
|
||||
@@ -183,7 +192,7 @@ def unpack(content, data):
|
||||
total = 0
|
||||
with tarfile.open(fileobj=io.BytesIO(content), mode='r:gz') as archive:
|
||||
for entry in archive:
|
||||
if entry.name not in FILES or entry.name in result or not entry.isfile() or entry.size > 32 * 1024 * 1024:
|
||||
if entry.name not in data['outputs'] or entry.name in result or not entry.isfile() or entry.size > 32 * 1024 * 1024:
|
||||
raise ValueError('Unsafe or unexpected preload archive entry')
|
||||
total += entry.size
|
||||
if total > 64 * 1024 * 1024:
|
||||
@@ -192,7 +201,7 @@ def unpack(content, data):
|
||||
if sha(value) != data['outputs'][entry.name]:
|
||||
raise ValueError('Preload binary checksum mismatch')
|
||||
result[entry.name] = value
|
||||
if set(result) != FILES:
|
||||
if set(result) != set(data['outputs']):
|
||||
raise ValueError('Incomplete preload archive')
|
||||
return result
|
||||
|
||||
@@ -203,6 +212,8 @@ def restore(root=ROOT):
|
||||
matching = {group: source_files(group, root) == data['groups'][group] for group in INPUTS}
|
||||
matching['arm64'] &= recipe(root) == data['arm64_recipe']
|
||||
matching['x86'] &= x86_recipe(root) == data.get('x86_recipe')
|
||||
for group in matching:
|
||||
matching[group] &= all(name in data['outputs'] for name in OUTPUTS[group])
|
||||
if os.environ.get('FORCE_PRELOAD_REBUILD') == 'true':
|
||||
matching = dict.fromkeys(INPUTS, False)
|
||||
if any(matching.values()):
|
||||
|
||||
@@ -164,7 +164,8 @@
|
||||
"tools/components/build-arm64-preloads.sh",
|
||||
"tools/linuxfs/preload",
|
||||
"app/src/main/cpp/fakeinput_steam.cpp",
|
||||
"tools/components/recipes/arm64-preloads.sh"
|
||||
"tools/components/recipes/arm64-preloads.sh",
|
||||
"tools/nativeguest"
|
||||
],
|
||||
"outputs": [
|
||||
"app/src/main/assets/linuxfs/libfakeinput.so",
|
||||
@@ -172,7 +173,8 @@
|
||||
"app/src/main/assets/linuxfs/libblfastpath.so",
|
||||
"app/src/main/assets/linuxfs/libblaudit.so",
|
||||
"app/src/main/assets/linuxfs/libssbs.so",
|
||||
"app/src/main/assets/linuxfs/usr/local/bin/droiddeck-clipboard"
|
||||
"app/src/main/assets/linuxfs/usr/local/bin/droiddeck-clipboard",
|
||||
"app/src/main/assets/linuxfs/libddnatv-hook.so"
|
||||
],
|
||||
"packages": [
|
||||
"gcc-aarch64-linux-gnu",
|
||||
@@ -187,7 +189,8 @@
|
||||
"tools/quest/kgsl_compat.h",
|
||||
"tools/proot/fastpath",
|
||||
"tools/linuxfs/ssbs",
|
||||
"tools/linuxfs/clipboard"
|
||||
"tools/linuxfs/clipboard",
|
||||
"tools/nativeguest"
|
||||
],
|
||||
"snapshot_outputs": [
|
||||
"libfakeinput.so",
|
||||
@@ -195,7 +198,8 @@
|
||||
"libblfastpath.so",
|
||||
"libblaudit.so",
|
||||
"libssbs.so",
|
||||
"usr/local/bin/droiddeck-clipboard"
|
||||
"usr/local/bin/droiddeck-clipboard",
|
||||
"libddnatv-hook.so"
|
||||
]
|
||||
},
|
||||
"preload-snapshot": {
|
||||
|
||||
+40
-2
@@ -101,6 +101,10 @@ def encode_request(request: dict) -> str:
|
||||
|
||||
def receiver_call(adb: str, serial: str, method: str, arg: str | None, request: dict | None, timeout: float) -> dict:
|
||||
"""The agent bridge through a broadcast, for shells without the `content` command."""
|
||||
# Its receiver is registered by the running app; a broadcast does not start the app the way `content call` does
|
||||
running = run_adb(adb, serial, "shell", f"pidof {PACKAGE}", timeout=30)
|
||||
if not running.stdout.decode(errors="replace").strip():
|
||||
raise ControlError(f"{PACKAGE} is not running and this shell has no `content` command to start it; open DroidDeck once", "APP_NOT_RUNNING")
|
||||
call_id = uuid.uuid4().hex[:16]
|
||||
remote = f"/sdcard/Android/data/{PACKAGE}/files/agent-calls/{call_id}.json"
|
||||
command = ["shell", "am", "broadcast", "-a", f"{PACKAGE}.AGENT_CALL", "-p", PACKAGE,
|
||||
@@ -132,7 +136,8 @@ def provider_call(adb: str, serial: str, method: str, arg: str | None = None, re
|
||||
command.extend(["--extra", f"request:s:{encoded}"])
|
||||
result = run_adb(adb, serial, *command, timeout=timeout)
|
||||
output = (result.stdout + result.stderr).decode(errors="replace")
|
||||
if "content: inaccessible or not found" in output:
|
||||
# Only the shell's own answer for a missing `content` binary, never a command's output that quotes it
|
||||
if result.returncode == 127 and "Result:" not in output and "content: inaccessible or not found" in result.stderr.decode(errors="replace") + output:
|
||||
return receiver_call(adb, serial, method, arg, request, timeout)
|
||||
response = _content_result(output)
|
||||
if result.returncode != 0 and response.get("ok") is not False:
|
||||
@@ -166,6 +171,9 @@ def start_agent_activity(adb: str, serial: str, request: dict) -> None:
|
||||
output = (result.stdout + result.stderr).decode(errors="replace")
|
||||
if result.returncode != 0 or "status: ok" not in output.lower():
|
||||
raise ControlError(output.strip() or "The agent start Activity did not launch", "ACTIVITY_START_FAILED")
|
||||
if "activity not started" in output.lower():
|
||||
raise ControlError("Android handed the start request to an existing DroidDeck task instead of starting it: "
|
||||
+ output.strip(), "ACTIVITY_START_FAILED")
|
||||
|
||||
|
||||
def confirm_start(adb: str, serial: str, request: dict, previous: dict) -> dict:
|
||||
@@ -563,7 +571,7 @@ def unpack_logs(archive: Path, destination: Path, session_id: str) -> None:
|
||||
def require_commands(adb: str, serial: str) -> dict:
|
||||
access = checked(provider_call(adb, serial, "access"), "ACCESS_FAILED")
|
||||
if access.get("commands") is not True:
|
||||
raise ControlError("Debugging tools are off: turn on Setup > Diagnostics > Debugging tools on the device",
|
||||
raise ControlError("Debugging tools are off: turn on Setup > Developer > Debugging tools on the device",
|
||||
"AGENT_COMMANDS_DISABLED")
|
||||
return access
|
||||
|
||||
@@ -781,6 +789,28 @@ def execute(args: argparse.Namespace, adb: str, serial: str) -> dict:
|
||||
f"{(response.get('stderr') or {}).get('text', '')[-400:]}", "GUEST_EXIT")
|
||||
return add_serial(response, serial)
|
||||
|
||||
if command == "native":
|
||||
argv = args.argv[1:] if args.argv[:1] == ["--"] else args.argv
|
||||
if not argv:
|
||||
raise ControlError("native needs a command after --", "INVALID_ARGUMENTS")
|
||||
if not 0 < args.timeout <= 600:
|
||||
raise ControlError("--timeout must be more than 0 and at most 600 seconds", "INVALID_ARGUMENTS")
|
||||
env = {}
|
||||
for item in args.env or []:
|
||||
key, sep, value = item.partition("=")
|
||||
if not sep:
|
||||
raise ControlError(f"--env takes KEY=VALUE, not {item}", "INVALID_ARGUMENTS")
|
||||
env[key] = value
|
||||
request = {"argv": argv, "timeout": args.timeout, "env": env}
|
||||
if args.cwd:
|
||||
request["cwd"] = args.cwd
|
||||
if args.stdin is not None:
|
||||
request["stdin"] = args.stdin
|
||||
if args.preload:
|
||||
request["preload"] = args.preload
|
||||
response = checked(provider_call(adb, serial, "native", request=request, timeout=args.timeout + 40), "NATIVE_FAILED")
|
||||
return add_serial(response, serial)
|
||||
|
||||
if command == "cdp":
|
||||
if args.cdp_op == "targets":
|
||||
request = {"op": "targets"}
|
||||
@@ -1015,6 +1045,14 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
guest.add_argument("--check", action="store_true", help="fail unless the command exits 0")
|
||||
guest.add_argument("argv", nargs=argparse.REMAINDER)
|
||||
|
||||
native = commands.add_parser("native", help="run a command in the rootfs without proot (debugging tools)")
|
||||
native.add_argument("--timeout", type=float, default=30)
|
||||
native.add_argument("--env", action="append", metavar="KEY=VALUE")
|
||||
native.add_argument("--cwd")
|
||||
native.add_argument("--stdin")
|
||||
native.add_argument("--preload", action="append", help="guest path of another library to preload before the hook")
|
||||
native.add_argument("argv", nargs=argparse.REMAINDER)
|
||||
|
||||
cdp = commands.add_parser("cdp", help="the Steam client's DevTools (debugging tools)")
|
||||
cdp_ops = cdp.add_subparsers(dest="cdp_op", required=True)
|
||||
cdp_ops.add_parser("targets", help="list DevTools targets")
|
||||
|
||||
@@ -41,6 +41,9 @@ FEX_WAIT = 120
|
||||
START_GRACE = 90
|
||||
POLL = 2
|
||||
STOP_WAIT = 5
|
||||
DRIVES = "defghijklmnopqruvwxy"
|
||||
PRODUCT_DB = ("ProgramData", "Battle.net", "Agent", "product.db")
|
||||
INSTALL_FOLDER = (1, 3, 1)
|
||||
CLOSE_GRACE = 15
|
||||
HANDOFF_WAIT = 120
|
||||
GAME_SETTLE = 20
|
||||
@@ -85,6 +88,8 @@ COUNTRY = re.compile(r"[A-Z]{3}")
|
||||
REGION = re.compile(r"[A-Z]{2}")
|
||||
REPORT = "BNET"
|
||||
ETA_LIMIT = 30 * 86400
|
||||
STALL = 600
|
||||
MOVING = ("download_current", "download_total", "progress", "playable_progress", "state", "phase", "paused", "error")
|
||||
DONE_STATE = 1004
|
||||
REPORTED = ("progress", "playable_progress", "download_rate", "download_remaining", "estimated_time_in_sec", "state",
|
||||
"playable", "paused", "download_complete", "installed", "error")
|
||||
@@ -115,6 +120,12 @@ function field(text, name) {
|
||||
return m ? m[1] : "";
|
||||
}
|
||||
|
||||
function job(text) {
|
||||
if (/"error"\s*:\s*[1-9]/.test(text)) return false;
|
||||
var bare = text.replace(/"extended_status"\s*:\s*\{[^}]*\}/, "");
|
||||
return /"state"\s*:\s*\d+/.test(bare) || /"download_current"\s*:/.test(bare);
|
||||
}
|
||||
|
||||
function install() {
|
||||
// Blizzard's China repository moved to cn.patch.battlenet.com.cn; cn.patch.battle.net only redirects away from it.
|
||||
var patch = zone == "cn" ? "cn.patch.battlenet.com.cn" : zone + ".patch.battle.net";
|
||||
@@ -123,7 +134,7 @@ function install() {
|
||||
save(".form", form);
|
||||
if (form.indexOf('"' + lang + '"') < 0) lang = "enUS";
|
||||
var locale = lang.toLowerCase();
|
||||
call("POST", "/install/" + uid, '{"account_country":"' + country + '","finalized":true,"game_dir":"' + dir + '","geo_ip_country":"' +
|
||||
call("POST", "/install/" + uid, '{"account_country":"' + country + '","finalized":true,"game_dir":"' + dir.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '","geo_ip_country":"' +
|
||||
region + '","language":["' + locale + '"],"selected_asset_locale":"' + locale + '","selected_locale":"' + locale +
|
||||
'","shortcut":"none","tome_torrent":""}');
|
||||
watch("/install/" + uid);
|
||||
@@ -137,7 +148,7 @@ function watch(path) {
|
||||
if (/"error"\s*:\s*[1-9]/.test(s)) break;
|
||||
var state = /"state"\s*:\s*(\d+)/.exec(s.replace(/"extended_status"\s*:\s*\{[^}]*\}/, "")), complete = /"download_complete"\s*:\s*true/.test(s);
|
||||
state = state ? +state[1] : 0;
|
||||
if (state == 1000 || state == 1003 || !complete) started = true;
|
||||
if (state == 1000 || state == 1003 || job(s) && !complete) started = true;
|
||||
if (started && (state == 1004 || complete && /"playable"\s*:\s*true/.test(s))) break;
|
||||
if (!started && state == 1004 && i >= 20) break;
|
||||
if (!started && i >= 180) {
|
||||
@@ -226,8 +237,72 @@ def installed(root):
|
||||
return os.path.isfile(launcher(root))
|
||||
|
||||
|
||||
def install_path(library):
|
||||
return "Z:" + library.rstrip("/")
|
||||
def dosdevices(root):
|
||||
return os.path.join(prefix(root), "pfx", "dosdevices")
|
||||
|
||||
|
||||
def volume_root(path):
|
||||
device = os.stat(path).st_dev
|
||||
while path != "/":
|
||||
parent = os.path.dirname(path)
|
||||
if os.stat(parent).st_dev != device:
|
||||
break
|
||||
path = parent
|
||||
return path
|
||||
|
||||
|
||||
def inside(path, folder):
|
||||
return path == folder or path.startswith(folder.rstrip("/") + "/")
|
||||
|
||||
|
||||
def mapped_drive(root, path, volume):
|
||||
folder = dosdevices(root)
|
||||
try:
|
||||
names = sorted(os.listdir(folder))
|
||||
except OSError:
|
||||
return None
|
||||
for name in names:
|
||||
link = os.path.join(folder, name)
|
||||
if len(name) == 2 and name[0].lower() in DRIVES and name[1] == ":" and os.path.islink(link):
|
||||
target = os.path.realpath(link)
|
||||
if inside(target, volume) and inside(path, target):
|
||||
return name[0].lower(), target
|
||||
return None
|
||||
|
||||
|
||||
def map_drive(root, volume):
|
||||
folder = dosdevices(root)
|
||||
for letter in DRIVES:
|
||||
link = os.path.join(folder, letter + ":")
|
||||
if not os.path.lexists(link):
|
||||
try:
|
||||
os.makedirs(folder, exist_ok=True)
|
||||
os.symlink(volume, link)
|
||||
except OSError as error:
|
||||
say("%s not mapped to a Wine drive: %s" % (volume, error))
|
||||
return None
|
||||
say("Wine drive %s: is %s" % (letter.upper(), volume))
|
||||
return letter, volume
|
||||
say("%s not mapped to a Wine drive: every letter is taken" % volume)
|
||||
return None
|
||||
|
||||
|
||||
def install_path(root, library):
|
||||
library = library.rstrip("/") or "/"
|
||||
try:
|
||||
path = os.path.realpath(library)
|
||||
volume = volume_root(path)
|
||||
except OSError:
|
||||
return "Z:" + library
|
||||
if volume == "/":
|
||||
return "Z:" + library
|
||||
drive = mapped_drive(root, path, volume)
|
||||
if drive is None and not wine_processes(os.path.join(prefix(root), "pfx")):
|
||||
drive = map_drive(root, volume)
|
||||
if drive is None:
|
||||
return "Z:" + library
|
||||
letter, target = drive
|
||||
return letter.upper() + ":\\" + path[len(target):].strip("/").replace("/", "\\")
|
||||
|
||||
|
||||
def write_config(root, library=None):
|
||||
@@ -254,8 +329,8 @@ def write_config(root, library=None):
|
||||
client["HardwareAcceleration"] = "false"
|
||||
changes.append("acceleration")
|
||||
if library and isinstance(client.setdefault("Install", {}), dict) \
|
||||
and str(client["Install"].get("DefaultInstallPath", "")).rstrip("/\\") != install_path(library):
|
||||
client["Install"]["DefaultInstallPath"] = install_path(library)
|
||||
and str(client["Install"].get("DefaultInstallPath", "")).rstrip("/\\") != install_path(root, library).rstrip("\\"):
|
||||
client["Install"]["DefaultInstallPath"] = install_path(root, library)
|
||||
changes.append("library")
|
||||
games = config.setdefault("Games", {})
|
||||
for uid in CLIENT_32_BIT if isinstance(games, dict) else ():
|
||||
@@ -417,13 +492,106 @@ def wallpaper(root):
|
||||
|
||||
|
||||
def windows_path(root, path):
|
||||
found = re.match(r"(?i)^([cz]):[\\/](.*)$", path.strip())
|
||||
found = re.match(r"(?i)^([a-z]):[\\/](.*)$", path.strip())
|
||||
if not found:
|
||||
return None
|
||||
parts = [part for part in re.split(r"[\\/]+", found.group(2)) if part]
|
||||
if not parts or ".." in parts:
|
||||
if ".." in parts:
|
||||
return None
|
||||
return os.path.join(drive_c(root) if found.group(1).lower() == "c" else "/", *parts)
|
||||
letter = found.group(1).lower()
|
||||
link = os.path.join(dosdevices(root), letter + ":")
|
||||
if letter == "c":
|
||||
target = drive_c(root)
|
||||
elif os.path.islink(link):
|
||||
target = os.path.realpath(link)
|
||||
elif letter == "z":
|
||||
target = "/"
|
||||
else:
|
||||
return None
|
||||
return os.path.join(target, *parts)
|
||||
|
||||
|
||||
def varint(data, at):
|
||||
value = shift = 0
|
||||
while at < len(data) and shift < 64:
|
||||
byte = data[at]
|
||||
at += 1
|
||||
value |= (byte & 0x7F) << shift
|
||||
if not byte & 0x80:
|
||||
return value, at
|
||||
shift += 7
|
||||
raise ValueError("varint")
|
||||
|
||||
|
||||
def encode_varint(value):
|
||||
out = bytearray()
|
||||
while value > 0x7F:
|
||||
out.append(value & 0x7F | 0x80)
|
||||
value >>= 7
|
||||
out.append(value)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def rewrite_field(data, path, change):
|
||||
out, at = bytearray(), 0
|
||||
while at < len(data):
|
||||
start = at
|
||||
tag, at = varint(data, at)
|
||||
kind = tag & 7
|
||||
if kind == 0:
|
||||
at = varint(data, at)[1]
|
||||
elif kind == 1:
|
||||
at += 8
|
||||
elif kind == 5:
|
||||
at += 4
|
||||
elif kind == 2:
|
||||
size, body = varint(data, at)
|
||||
at = body + size
|
||||
else:
|
||||
raise ValueError("wire type")
|
||||
if at > len(data):
|
||||
raise ValueError("length")
|
||||
if kind == 2 and tag >> 3 == path[0]:
|
||||
value = data[body:at]
|
||||
value = change(value) if len(path) == 1 else rewrite_field(value, path[1:], change)
|
||||
out += encode_varint(tag) + encode_varint(len(value)) + value
|
||||
else:
|
||||
out += data[start:at]
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def drive_folder(root, value):
|
||||
try:
|
||||
text = value.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
return value
|
||||
if not re.match(r"(?i)^z:[\\/]", text):
|
||||
return value
|
||||
folder = windows_path(root, text)
|
||||
if not folder or not os.path.isdir(folder):
|
||||
return value
|
||||
moved = install_path(root, folder)
|
||||
return value if moved.startswith("Z:") else moved.replace("\\", "/").encode("utf-8")
|
||||
|
||||
|
||||
def move_installs(root):
|
||||
path = os.path.join(drive_c(root), *PRODUCT_DB)
|
||||
try:
|
||||
with open(path, "rb") as f:
|
||||
data = f.read()
|
||||
moved = rewrite_field(data, INSTALL_FOLDER, lambda value: drive_folder(root, value))
|
||||
if moved == data or wine_processes(os.path.join(prefix(root), "pfx")):
|
||||
return False
|
||||
with open(path, "rb") as f:
|
||||
if f.read() != data:
|
||||
return False
|
||||
except (OSError, ValueError):
|
||||
return False
|
||||
with open(path + ".tmp", "wb") as f:
|
||||
f.write(moved)
|
||||
shutil.copymode(path, path + ".tmp")
|
||||
os.replace(path + ".tmp", path)
|
||||
return True
|
||||
|
||||
|
||||
def wow_folders(root):
|
||||
@@ -533,6 +701,11 @@ def configure(root, library=None):
|
||||
except OSError as error:
|
||||
say("Battle.net's config not written: %s" % error)
|
||||
return
|
||||
try:
|
||||
if move_installs(root):
|
||||
say("Battle.net's installed games on a mapped volume now use its Wine drive")
|
||||
except OSError as error:
|
||||
say("Battle.net's installed games not moved to their Wine drive: %s" % error)
|
||||
try:
|
||||
wallpaper(root)
|
||||
except OSError as error:
|
||||
@@ -1211,7 +1384,7 @@ def summary(status):
|
||||
return out
|
||||
|
||||
|
||||
def agent_job(root, action, uid, product="", folder="", locale="enUS", country="USA", region="US", poll=None, wait=None):
|
||||
def agent_job(root, action, uid, product="", folder="", locale="enUS", country="USA", region="US", poll=None, wait=None, stall=None):
|
||||
pfx = os.path.join(prefix(root), "pfx")
|
||||
if not installed(root) or agent_exe(root) is None:
|
||||
emit("ERROR", "client")
|
||||
@@ -1227,7 +1400,6 @@ def agent_job(root, action, uid, product="", folder="", locale="enUS", country="
|
||||
say("%s not made: %s" % (folder, error))
|
||||
emit("ERROR", "folder")
|
||||
return 2
|
||||
windows = "Z:" + folder
|
||||
told = None
|
||||
while True:
|
||||
processes = wine_processes(pfx)
|
||||
@@ -1244,6 +1416,8 @@ def agent_job(root, action, uid, product="", folder="", locale="enUS", country="
|
||||
time.sleep(POLL if poll is None else poll)
|
||||
with lock:
|
||||
stop_leftovers(pfx)
|
||||
if action == "install":
|
||||
windows = install_path(root, folder)
|
||||
emit("STATE", "starting")
|
||||
base = os.path.join(CACHE, "agent-%s" % uid)
|
||||
for suffix in (".json", ".game", ".form", ".error"):
|
||||
@@ -1266,6 +1440,7 @@ def agent_job(root, action, uid, product="", folder="", locale="enUS", country="
|
||||
os.replace(output, output + ".1")
|
||||
except OSError:
|
||||
pass
|
||||
before = {pid for pid, _ in wine_processes(pfx)}
|
||||
with open(output, "ab") as log:
|
||||
try:
|
||||
child = subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=log, stderr=log, env=env)
|
||||
@@ -1273,6 +1448,9 @@ def agent_job(root, action, uid, product="", folder="", locale="enUS", country="
|
||||
emit("ERROR", "stopped gamescope: %s" % error)
|
||||
return 1
|
||||
seen = None
|
||||
mark = None
|
||||
moved = time.monotonic()
|
||||
stalled = False
|
||||
while True:
|
||||
done = child.poll() is not None
|
||||
status = read_status(base + ".json")
|
||||
@@ -1281,12 +1459,27 @@ def agent_job(root, action, uid, product="", folder="", locale="enUS", country="
|
||||
if brief != seen:
|
||||
emit("PROGRESS", json.dumps(brief, sort_keys=True))
|
||||
seen = brief
|
||||
signature = tuple(brief.get(key) for key in MOVING)
|
||||
if signature != mark:
|
||||
mark = signature
|
||||
moved = time.monotonic()
|
||||
if done:
|
||||
break
|
||||
if action != "uninstall" and time.monotonic() - moved > (STALL if stall is None else stall):
|
||||
say("the %s of %s made no progress for %d s; stopping it" % (action, uid, STALL if stall is None else stall))
|
||||
stalled = True
|
||||
child.kill()
|
||||
child.wait()
|
||||
break
|
||||
time.sleep(POLL if poll is None else poll)
|
||||
leftovers = wine_processes(pfx)
|
||||
if leftovers and idle(leftovers):
|
||||
stop(leftovers)
|
||||
elif stalled:
|
||||
stop([(pid, argv) for pid, argv in leftovers if pid not in before and program(argv) == AGENT])
|
||||
if stalled:
|
||||
emit("ERROR", "stalled")
|
||||
return 6
|
||||
answer = read_status(base + ".game")
|
||||
game = answer or {}
|
||||
status = read_status(base + ".json") or {}
|
||||
|
||||
@@ -4,7 +4,6 @@ import collections
|
||||
import fcntl
|
||||
import filecmp
|
||||
import http.client
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
@@ -72,6 +71,14 @@ APP_ID = "com.epicgames.eglv2"
|
||||
DBUS_NAME = APP_ID + ".SingleInstance"
|
||||
DBUS_PATH = "/" + APP_ID.replace(".", "/") + "/SingleInstance"
|
||||
DBUS_METHOD = "org.SingleInstance.DBus.ExecuteCallback"
|
||||
APP_BIN = "usr/bin/EpicGamesLauncher"
|
||||
DEB_ICON = "usr/share/icons/hicolor/256x256@2/apps/EpicGamesLauncher.png"
|
||||
ICON = "epic-games-launcher.png"
|
||||
DESKTOP = "desktop"
|
||||
DESKTOP_KEEP = ("usr/lib/x86_64-linux-gnu/", "usr/share/glib-2.0/schemas/")
|
||||
DESKTOP_LIB = DESKTOP + "/usr/lib/x86_64-linux-gnu"
|
||||
WEBKIT_DIR = "webkit2gtk-4.1"
|
||||
WEBKIT_LIB = "libwebkit2gtk-4.1.so.0"
|
||||
SKIPPED = ("lib/i386-linux-gnu", "lib32", "libx32", "i686-pc-linux-gnu", "share/doc", "include")
|
||||
SETTINGS_SEED = {
|
||||
"minimizeToTray": False,
|
||||
@@ -254,6 +261,35 @@ def inside(root, path):
|
||||
return real == root or real.startswith(root + os.sep)
|
||||
|
||||
|
||||
def unpack_member(archive, member, staging, root, name, link):
|
||||
target = os.path.join(staging, name)
|
||||
if not inside(root, os.path.dirname(target)):
|
||||
return False
|
||||
if member.isdir():
|
||||
os.makedirs(target, exist_ok=True)
|
||||
return False
|
||||
os.makedirs(os.path.dirname(target), exist_ok=True)
|
||||
if not inside(root, os.path.dirname(target)):
|
||||
return False
|
||||
if member.issym():
|
||||
remove(target)
|
||||
os.symlink(link(target, member.linkname), target)
|
||||
elif member.islnk():
|
||||
source = safe_name(member.linkname)
|
||||
source = os.path.join(staging, source) if source else None
|
||||
if source and os.path.isfile(source) and inside(root, source):
|
||||
remove(target)
|
||||
shutil.copy2(source, target)
|
||||
elif member.isfile():
|
||||
remove(target)
|
||||
with archive.extractfile(member) as src, open(target, "wb") as out:
|
||||
shutil.copyfileobj(src, out, 1 << 20)
|
||||
os.chmod(target, (member.mode & 0o777) | 0o600)
|
||||
else:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def extract_runtime(tarball, dest):
|
||||
staging = dest + ".staging"
|
||||
remove(staging)
|
||||
@@ -271,32 +307,12 @@ def extract_runtime(tarball, dest):
|
||||
continue
|
||||
if top == "files" and rest and skipped(rest):
|
||||
continue
|
||||
target = os.path.join(staging, name)
|
||||
if not inside(root, os.path.dirname(target)):
|
||||
continue
|
||||
if member.isdir():
|
||||
os.makedirs(target, exist_ok=True)
|
||||
continue
|
||||
os.makedirs(os.path.dirname(target), exist_ok=True)
|
||||
if not inside(root, os.path.dirname(target)):
|
||||
continue
|
||||
if member.issym():
|
||||
remove(target)
|
||||
os.symlink(rooted_link(files_root, target, member.linkname) if top == "files" else member.linkname, target)
|
||||
elif member.islnk():
|
||||
source = safe_name(member.linkname)
|
||||
source = os.path.join(staging, source) if source else None
|
||||
if source and os.path.isfile(source) and inside(root, source):
|
||||
remove(target)
|
||||
shutil.copy2(source, target)
|
||||
elif member.isfile():
|
||||
remove(target)
|
||||
with archive.extractfile(member) as src, open(target, "wb") as out:
|
||||
shutil.copyfileobj(src, out, 1 << 20)
|
||||
os.chmod(target, (member.mode & 0o777) | 0o600)
|
||||
if top == "files":
|
||||
link = lambda target, linkname: rooted_link(files_root, target, linkname)
|
||||
else:
|
||||
continue
|
||||
count += 1
|
||||
link = lambda target, linkname: linkname
|
||||
if unpack_member(archive, member, staging, root, name, link):
|
||||
count += 1
|
||||
if not os.path.isfile(os.path.join(files_root, "lib", "x86_64-linux-gnu", "libc.so.6")) and \
|
||||
not os.path.islink(os.path.join(files_root, "lib", "x86_64-linux-gnu", "libc.so.6")):
|
||||
remove(staging)
|
||||
@@ -306,63 +322,129 @@ def extract_runtime(tarball, dest):
|
||||
return staging, count
|
||||
|
||||
|
||||
def ar_members(path):
|
||||
@contextlib.contextmanager
|
||||
def deb_data(path, reason):
|
||||
with open(path, "rb") as f:
|
||||
if f.read(8) != b"!<arch>\n":
|
||||
raise Failure("gles-format")
|
||||
raise Failure(reason)
|
||||
while True:
|
||||
header = f.read(60)
|
||||
if len(header) < 60:
|
||||
return
|
||||
raise Failure(reason)
|
||||
name = header[:16].decode("ascii", "replace").strip().rstrip("/")
|
||||
size = int(header[48:58].decode("ascii").strip())
|
||||
data = f.read(size)
|
||||
if size % 2:
|
||||
f.read(1)
|
||||
yield name, data
|
||||
if name.startswith("data.tar"):
|
||||
with tarfile.open(fileobj=f, mode="r|*") as archive:
|
||||
yield archive
|
||||
return
|
||||
f.seek(size + size % 2, os.SEEK_CUR)
|
||||
|
||||
|
||||
def is_deb(path):
|
||||
try:
|
||||
with open(path, "rb") as f:
|
||||
return f.read(21) == b"!<arch>\ndebian-binary"
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
|
||||
def install_gles(deb, files_root):
|
||||
libdir = os.path.join(files_root, "lib", "x86_64-linux-gnu")
|
||||
os.makedirs(libdir, exist_ok=True)
|
||||
for name, data in ar_members(deb):
|
||||
if not name.startswith("data.tar"):
|
||||
continue
|
||||
with tarfile.open(fileobj=io.BytesIO(data), mode="r:*") as archive:
|
||||
for member in archive:
|
||||
base = os.path.basename(member.name)
|
||||
if not base.startswith("libGLESv2.so"):
|
||||
continue
|
||||
target = os.path.join(libdir, base)
|
||||
remove(target)
|
||||
if member.issym():
|
||||
os.symlink(os.path.basename(member.linkname), target)
|
||||
elif member.isfile():
|
||||
with archive.extractfile(member) as src, open(target, "wb") as out:
|
||||
shutil.copyfileobj(src, out)
|
||||
os.chmod(target, 0o644)
|
||||
if os.path.exists(os.path.join(libdir, "libGLESv2.so.2")):
|
||||
return
|
||||
raise Failure("gles-missing")
|
||||
with deb_data(deb, "gles-format") as archive:
|
||||
for member in archive:
|
||||
base = os.path.basename(member.name)
|
||||
if not base.startswith("libGLESv2.so"):
|
||||
continue
|
||||
target = os.path.join(libdir, base)
|
||||
remove(target)
|
||||
if member.issym():
|
||||
os.symlink(os.path.basename(member.linkname), target)
|
||||
elif member.isfile():
|
||||
with archive.extractfile(member) as src, open(target, "wb") as out:
|
||||
shutil.copyfileobj(src, out)
|
||||
os.chmod(target, 0o644)
|
||||
if not os.path.exists(os.path.join(libdir, "libGLESv2.so.2")):
|
||||
raise Failure("gles-missing")
|
||||
|
||||
|
||||
def extract_app(image, dest):
|
||||
def extract_deb(deb, dest, wanted=None):
|
||||
root = os.path.realpath(dest)
|
||||
|
||||
def link(target, linkname):
|
||||
if not linkname.startswith("/"):
|
||||
return linkname
|
||||
return os.path.relpath(os.path.join(dest, linkname.lstrip("/")), os.path.dirname(target))
|
||||
|
||||
count = 0
|
||||
with deb_data(deb, "app-format") as archive:
|
||||
for member in archive:
|
||||
name = safe_name(member.name)
|
||||
if name is None or (wanted is not None and not wanted(name)):
|
||||
continue
|
||||
if unpack_member(archive, member, dest, root, name, link):
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
def extract_app(image, dest, desktop=()):
|
||||
staging = dest + ".staging"
|
||||
remove(staging)
|
||||
os.makedirs(staging)
|
||||
status = subprocess.call([os.path.join(BIN_DIR, "droiddeck-fex"), "extract", os.path.abspath(image)],
|
||||
cwd=staging, stdout=subprocess.DEVNULL)
|
||||
root = os.path.realpath(os.path.join(staging, "squashfs-root"))
|
||||
if status != 0 or not app_complete(root):
|
||||
try:
|
||||
if not is_deb(image):
|
||||
raise Failure("app-format")
|
||||
extract_deb(image, staging)
|
||||
layer = os.path.join(staging, DESKTOP)
|
||||
if not inside(os.path.realpath(staging), layer):
|
||||
raise Failure("app-extract")
|
||||
os.makedirs(layer, exist_ok=True)
|
||||
for package in desktop:
|
||||
if not is_deb(package):
|
||||
raise Failure("desktop-format")
|
||||
extract_deb(package, layer, lambda name: any(name.startswith(prefix) for prefix in DESKTOP_KEEP))
|
||||
icon = os.path.join(staging, DEB_ICON)
|
||||
if os.path.isfile(icon):
|
||||
remove(os.path.join(staging, ICON))
|
||||
shutil.copyfile(icon, os.path.join(staging, ICON))
|
||||
if not app_complete(staging):
|
||||
raise Failure("app-extract")
|
||||
except Exception:
|
||||
remove(staging)
|
||||
raise Failure("app-extract")
|
||||
raise
|
||||
remove(dest)
|
||||
os.rename(root, dest)
|
||||
remove(staging)
|
||||
os.rename(staging, dest)
|
||||
|
||||
|
||||
def app_complete(root):
|
||||
return os.path.isfile(os.path.join(root, "AppRun")) and os.path.isfile(os.path.join(root, "usr", "bin", "EpicGamesLauncher"))
|
||||
if not os.path.isfile(os.path.join(root, APP_BIN)):
|
||||
return False
|
||||
return os.path.isfile(os.path.join(root, "AppRun")) or os.path.isfile(os.path.join(root, DESKTOP_LIB, WEBKIT_LIB))
|
||||
|
||||
|
||||
def app_command(app_dir):
|
||||
apprun = os.path.join(app_dir, "AppRun")
|
||||
return apprun if os.path.isfile(apprun) else os.path.join(app_dir, APP_BIN)
|
||||
|
||||
|
||||
def desktop_libs(app_dir):
|
||||
path = os.path.join(app_dir, DESKTOP_LIB)
|
||||
return path if os.path.isdir(path) else None
|
||||
|
||||
|
||||
def link_webkit(files, libs):
|
||||
target = os.path.join(files, "lib", "x86_64-linux-gnu", WEBKIT_DIR)
|
||||
if os.path.isdir(target) and not os.path.islink(target):
|
||||
return
|
||||
source = os.path.join(libs, WEBKIT_DIR) if libs else None
|
||||
if source is None or not os.path.isdir(source):
|
||||
remove(target)
|
||||
return
|
||||
link = os.path.relpath(source, os.path.dirname(target))
|
||||
if os.path.islink(target) and os.readlink(target) == link:
|
||||
return
|
||||
remove(target)
|
||||
os.symlink(link, target)
|
||||
|
||||
|
||||
def ready(path):
|
||||
@@ -396,8 +478,11 @@ def install(options):
|
||||
image = options.get("app")
|
||||
if not image or not os.path.isfile(image):
|
||||
raise Failure("no-app-image")
|
||||
say("unpacking the Epic Games Launcher %s" % app)
|
||||
extract_app(image, app_dir)
|
||||
desktop = options.get("desktop") or []
|
||||
if not all(os.path.isfile(package) for package in desktop):
|
||||
raise Failure("no-desktop-package")
|
||||
say("unpacking the Epic Games Launcher %s (%d desktop packages)" % (app, len(desktop)))
|
||||
extract_app(image, app_dir, desktop)
|
||||
mark_ready(app_dir)
|
||||
if not ready(runtime_dir):
|
||||
tarball, deb = options.get("runtime"), options.get("gles")
|
||||
@@ -729,11 +814,16 @@ def fex_environment(fex, files, env):
|
||||
return result
|
||||
|
||||
|
||||
def client_environment(base, fex, files, display, bus, broker, a11y=None, hidden=False):
|
||||
def client_environment(base, fex, files, display, bus, broker, a11y=None, hidden=False, libs=None):
|
||||
env = {name: base[name] for name in KEPT_ENV if base.get(name)}
|
||||
env.setdefault("HOME", HOME)
|
||||
env.setdefault("LANG", "C.UTF-8")
|
||||
env["PATH"] = SHIMS + ":/usr/bin:/bin"
|
||||
if libs:
|
||||
env["LD_LIBRARY_PATH"] = libs
|
||||
data = os.path.normpath(os.path.join(os.path.dirname(libs), "..", "share"))
|
||||
env["XDG_DATA_DIRS"] = data + ":/usr/local/share:/usr/share"
|
||||
env["GSETTINGS_SCHEMA_DIR"] = os.path.join(data, "glib-2.0", "schemas")
|
||||
env["DISPLAY"] = display
|
||||
env["DBUS_SESSION_BUS_ADDRESS"] = bus
|
||||
env[BROKER_ENV] = broker
|
||||
@@ -1238,6 +1328,8 @@ class Session:
|
||||
seed_settings()
|
||||
install_shims()
|
||||
install_preload(files)
|
||||
libs = desktop_libs(app_dir)
|
||||
link_webkit(files, libs)
|
||||
fex_module()["install_fonts"](files)
|
||||
clear_playing()
|
||||
sync_installations()
|
||||
@@ -1254,8 +1346,8 @@ class Session:
|
||||
path = broker_path()
|
||||
self.broker = Broker(path, self.mode, base)
|
||||
write_json(instance_file(), {"pid": os.getpid(), "mode": self.mode, "bus": bus, "broker": path})
|
||||
env = client_environment(base, fex, files, display, bus, path, self.a11y, hidden=self.mode == "hidden")
|
||||
self.command = [fex["bin"], os.path.join(app_dir, "AppRun")]
|
||||
env = client_environment(base, fex, files, display, bus, path, self.a11y, hidden=self.mode == "hidden", libs=libs)
|
||||
self.command = [fex["bin"], app_command(app_dir)]
|
||||
self.env = env
|
||||
say("starting the Epic Games Launcher %s (%s, display %s)" % (current.get("app"), self.mode, display))
|
||||
self.start_client()
|
||||
@@ -1471,12 +1563,14 @@ def forward(bus, url):
|
||||
return False
|
||||
|
||||
|
||||
def parse_options(args, flags=()):
|
||||
def parse_options(args, flags=(), lists=()):
|
||||
options, rest = {}, []
|
||||
while args:
|
||||
arg = args.pop(0)
|
||||
if arg.startswith("--") and arg[2:] in flags:
|
||||
options[arg[2:]] = True
|
||||
elif arg.startswith("--") and arg[2:] in lists and args:
|
||||
options.setdefault(arg[2:], []).append(args.pop(0))
|
||||
elif arg.startswith("--") and args:
|
||||
options[arg[2:]] = args.pop(0)
|
||||
else:
|
||||
@@ -1830,7 +1924,7 @@ def main(argv):
|
||||
if action == "xdg-open":
|
||||
return shim_open(args)
|
||||
if action == "install":
|
||||
options, _ = parse_options(args)
|
||||
options, _ = parse_options(args, lists=("desktop",))
|
||||
try:
|
||||
return install(options)
|
||||
except (ValueError, tarfile.TarError, OSError) as error:
|
||||
|
||||
@@ -10,24 +10,46 @@ import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
GAME_TU_DEBUG = "DROIDDECK_GAME_TU_DEBUG"
|
||||
GAME_PREFIX = "DROIDDECK_GAME_"
|
||||
GAME_SPLIT_KEYS = ("TU_DEBUG", "MESA_NO_ERROR")
|
||||
CHROMIUM_FILES = {"chrome-sandbox", "chrome_100_percent.pak", "v8_context_snapshot.bin"}
|
||||
D3D9_DEFAULT_MEMORY_MB = 4096
|
||||
SYNC_KEYS = ("BL_SYNC", "BL_SYNC_FALLBACK", "BL_FSYNC_FIRST", "BL_SYNC_MIN_NOFILE", "PROTON_NO_ESYNC", "PROTON_NO_FSYNC", "PROTON_NO_NTSYNC", "WINEESYNC")
|
||||
SEED_REDISTS = "/usr/local/bin/droiddeck-seed-redists"
|
||||
PACK_KEYS = ("WINESERVER", "BL_SYNC_PACK", "WINEESYNC", "PROTON_NO_ESYNC", "PROTON_NO_FSYNC")
|
||||
|
||||
|
||||
# Prefixes several games share; the game itself is the Steam app being launched.
|
||||
SHARED_PREFIXES = {"2929927072"}
|
||||
|
||||
|
||||
def app_number(value):
|
||||
if not re.fullmatch(r"-?[0-9]+", value or ""):
|
||||
return None
|
||||
number = int(value)
|
||||
if not -(2**31) <= number < 2**32 or number == 0:
|
||||
return None
|
||||
return str(number % 2**32)
|
||||
|
||||
|
||||
def game_id(env):
|
||||
prefix = env.get("STEAM_COMPAT_DATA_PATH", "").rstrip("/")
|
||||
if not prefix:
|
||||
return None
|
||||
value = prefix.rsplit("/", 1)[-1]
|
||||
if not re.fullmatch(r"-?[0-9]+", value):
|
||||
found = app_number(prefix.rsplit("/", 1)[-1])
|
||||
if found in SHARED_PREFIXES:
|
||||
return app_number(env.get("SteamAppId", "")) or shortcut_number(env.get("SteamGameId", "")) or found
|
||||
return found
|
||||
|
||||
|
||||
def shortcut_number(value):
|
||||
"""The shortcut appid inside Steam's 64-bit shortcut game id `(appid << 32) | 0x02000000`."""
|
||||
if not re.fullmatch(r"[0-9]{1,20}", value or ""):
|
||||
return None
|
||||
number = int(value)
|
||||
if not -(2**31) <= number < 2**32 or number == 0:
|
||||
if number >= 2**64 or number & 0xFFFFFFFF != 0x02000000:
|
||||
return None
|
||||
return str(number % 2**32)
|
||||
return app_number(str(number >> 32))
|
||||
|
||||
|
||||
DOTNET_FEX = {
|
||||
@@ -124,6 +146,12 @@ def deep_rock_gdk_raytracing(game):
|
||||
return {}, ["-noraytracing", "-ini:Engine:[/Script/Engine.RendererSettings]:r.RayTracing=False"], "Deep Rock Galactic GDK: ray tracing off (D3D12 pipeline creation fails on Thor)"
|
||||
|
||||
|
||||
@fix
|
||||
def ff7_rebirth_feature_level(game):
|
||||
if game.stem in ("ff7rebirth", "ff7rebirth_"):
|
||||
return {"VKD3D_FEATURE_LEVEL": "12_2"}, [], "Final Fantasy VII Rebirth: D3D12 feature level 12_2 (the device-reported level fails its DX12 check)"
|
||||
|
||||
|
||||
@fix
|
||||
def arkham_city_x87(game):
|
||||
if "batmanac.exe" in game.files:
|
||||
@@ -358,16 +386,19 @@ def chromium_app(folder):
|
||||
|
||||
|
||||
def game_gpu(env, keep=False):
|
||||
if GAME_TU_DEBUG not in env:
|
||||
if not any(GAME_PREFIX + key in env for key in GAME_SPLIT_KEYS):
|
||||
return env
|
||||
result = env.copy()
|
||||
value = result.pop(GAME_TU_DEBUG)
|
||||
if keep:
|
||||
return result
|
||||
if value:
|
||||
result["TU_DEBUG"] = value
|
||||
else:
|
||||
result.pop("TU_DEBUG", None)
|
||||
for key in GAME_SPLIT_KEYS:
|
||||
if GAME_PREFIX + key not in result:
|
||||
continue
|
||||
value = result.pop(GAME_PREFIX + key)
|
||||
if keep:
|
||||
continue
|
||||
if value:
|
||||
result[key] = value
|
||||
else:
|
||||
result.pop(key, None)
|
||||
return result
|
||||
|
||||
|
||||
@@ -376,7 +407,9 @@ def vram_cap(env):
|
||||
if not re.fullmatch(r"[1-9][0-9]{0,6}", value):
|
||||
return env
|
||||
result = env.copy()
|
||||
cap = "dxgi.maxDeviceMemory = %d" % int(value)
|
||||
cap = "dxgi.maxDeviceMemory = {0}; dxgi.maxSharedMemory = {0}".format(int(value))
|
||||
if int(value) < D3D9_DEFAULT_MEMORY_MB:
|
||||
cap += "; d3d9.maxAvailableMemory = {0}".format(int(value))
|
||||
own = result.get("DXVK_CONFIG", "").strip().strip(";").strip()
|
||||
result["DXVK_CONFIG"] = cap + "; " + own if own else cap
|
||||
result["DROIDDECK_VRAM_CAP"] = "1"
|
||||
@@ -1250,6 +1283,10 @@ def main():
|
||||
if scripts and install_scripts_done(scripts, env):
|
||||
print("droiddeck-game-env: install scripts already run in this prefix; evaluator skipped", file=sys.stderr)
|
||||
return 0
|
||||
if scripts and not env.get("DBUS_SESSION_BUS_ADDRESS"):
|
||||
# libdbus would autolaunch a session bus under the evaluator's reaper, which then waits for it
|
||||
# forever: Steam keeps seeing the app as running and cannot move or update it
|
||||
env["DBUS_SESSION_BUS_ADDRESS"] = "disabled:"
|
||||
try:
|
||||
digest = scripts_digest(scripts) if scripts else None
|
||||
except OSError:
|
||||
|
||||
@@ -17,6 +17,7 @@ PROTON_RUN = os.path.join(BIN, "droiddeck-proton-run")
|
||||
APPIMAGE_RUN = os.path.join(BIN, "droiddeck-appimage-run")
|
||||
SCRIPT_RUN = os.path.join(BIN, "droiddeck-script-run")
|
||||
STEAM_LAUNCH = os.path.join(BIN, "droiddeck-steam-launch")
|
||||
STORE_LAUNCH = os.path.join(BIN, "droiddeck-store-launch")
|
||||
PROGRAMS = os.path.join(BIN, "droiddeck-desktop-programs")
|
||||
CACHE = os.path.join(HOME, ".cache", "droiddeck")
|
||||
IMPORTS = {"appimage": ("/opt/appimages/user", "app"), "folder": ("/opt/droiddeck-apps", "entry")}
|
||||
@@ -272,12 +273,32 @@ def steam_title(path):
|
||||
return None
|
||||
|
||||
|
||||
def exe_prefix(path):
|
||||
try:
|
||||
run = runpy.run_path(PROTON_RUN)
|
||||
root = os.environ.get("STEAM_COMPAT_CLIENT_INSTALL_PATH") or run["STEAM_ROOT"]
|
||||
target = run["locate"](path, root)
|
||||
return ["--prefix", target.appid, "--compatdata", target.compatdata]
|
||||
except (OSError, ValueError, KeyError, IndexError):
|
||||
return None
|
||||
|
||||
|
||||
def store_launcher(path):
|
||||
try:
|
||||
return runpy.run_path(STORE_LAUNCH)["store_launcher"](path)
|
||||
except (OSError, ValueError, KeyError):
|
||||
return None
|
||||
|
||||
|
||||
def command(path, args, what, window=False, appid=None):
|
||||
folder = os.path.dirname(path)
|
||||
if what == "steam":
|
||||
return [STEAM_LAUNCH, "desktop", appid], folder
|
||||
if what == "windows":
|
||||
return [PROTON_RUN] + (["--window"] if window else []) + [path] + args, folder
|
||||
launcher = None if args else store_launcher(path)
|
||||
prefix = exe_prefix(path) if launcher else None
|
||||
keep = prefix or []
|
||||
return [PROTON_RUN] + (["--window"] if window else []) + keep + [launcher or path] + args, folder
|
||||
if what == "x86 script":
|
||||
return [FEX, "run", "--mode", "on", "--for", folder, "--"] + interpreter(path) + [path] + args, folder
|
||||
if what == "script":
|
||||
|
||||
+266
@@ -0,0 +1,266 @@
|
||||
#!/usr/bin/python3
|
||||
import os
|
||||
import signal
|
||||
import struct
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
MAGIC = 0x46484444
|
||||
HEADER = 128
|
||||
POLL = 0.5
|
||||
SETTLE = 3.0
|
||||
WINDOW = 1.0
|
||||
LATE = 1.5
|
||||
PACED_LATE_SHARE = 0.02
|
||||
RELEASE_LATE_SHARE = 0.05
|
||||
SLOWER = 1.04
|
||||
MAX_INTERVAL = 1e9 / 30
|
||||
SPREAD = 0.08
|
||||
BACKOFF = 30.0
|
||||
BACKOFF_MAX = 600.0
|
||||
|
||||
|
||||
def log(message):
|
||||
print("droiddeck-pace-cores: " + message, flush=True)
|
||||
|
||||
|
||||
def parse_cores(text):
|
||||
cores = set()
|
||||
for part in text.strip().split(","):
|
||||
if not part:
|
||||
continue
|
||||
first, _, last = part.partition("-")
|
||||
cores.update(range(int(first), int(last or first) + 1))
|
||||
return cores
|
||||
|
||||
|
||||
def efficient_cores(cpu=Path("/sys/devices/system/cpu"), own=None):
|
||||
try:
|
||||
online = parse_cores((cpu / "online").read_text())
|
||||
except (OSError, ValueError):
|
||||
online = set(range(os.cpu_count() or 0))
|
||||
online &= own if own is not None else os.sched_getaffinity(0)
|
||||
for name in ("cpu_capacity", "cpufreq/cpuinfo_max_freq"):
|
||||
try:
|
||||
speed = {core: int((cpu / f"cpu{core}/{name}").read_text()) for core in online}
|
||||
except (OSError, ValueError):
|
||||
continue
|
||||
top = max(speed.values(), default=0)
|
||||
return {core for core, value in speed.items() if value < top}
|
||||
return set()
|
||||
|
||||
|
||||
class Frames:
|
||||
"""gamescope's per-frame feed for the HUD: frame intervals of the focused app, its pid and whether Steam is in front."""
|
||||
|
||||
def __init__(self, path):
|
||||
self.path = Path(path)
|
||||
self.count = None
|
||||
|
||||
def read(self):
|
||||
try:
|
||||
data = self.path.read_bytes()
|
||||
except OSError:
|
||||
return None
|
||||
if len(data) < HEADER or struct.unpack_from("<I", data, 0)[0] != MAGIC:
|
||||
return None
|
||||
capacity, pid = struct.unpack_from("<II", data, 8)
|
||||
count, = struct.unpack_from("<Q", data, 16)
|
||||
steam, = struct.unpack_from("<I", data, 40)
|
||||
last = self.count if self.count is not None else count
|
||||
self.count = count
|
||||
first = max(last, count - capacity)
|
||||
intervals = [struct.unpack_from("<Q", data, HEADER + (i % capacity) * 16)[0] for i in range(first, count)]
|
||||
return pid, bool(steam), intervals
|
||||
|
||||
|
||||
def median(values):
|
||||
ordered = sorted(values)
|
||||
return ordered[len(ordered) // 2]
|
||||
|
||||
|
||||
def late_share(intervals, period):
|
||||
return sum(1 for value in intervals if value > LATE * period) / len(intervals)
|
||||
|
||||
|
||||
def paced_interval(intervals):
|
||||
"""The interval of a game a cap holds at 30 fps or more; a starved or uncapped game spreads."""
|
||||
if len(intervals) < 2:
|
||||
return None
|
||||
ordered = sorted(intervals)
|
||||
held = ordered[len(ordered) // 2]
|
||||
if held > 1.04 * MAX_INTERVAL:
|
||||
return None
|
||||
if ordered[len(ordered) // 10] < (1 - SPREAD) * held or ordered[len(ordered) * 9 // 10] > (1 + SPREAD) * held:
|
||||
return None
|
||||
if late_share(intervals, held) > PACED_LATE_SHARE:
|
||||
return None
|
||||
return held
|
||||
|
||||
|
||||
class Cores:
|
||||
|
||||
def get(self, tid):
|
||||
return os.sched_getaffinity(tid)
|
||||
|
||||
def set(self, tid, cpus):
|
||||
os.sched_setaffinity(tid, cpus)
|
||||
|
||||
|
||||
class Parker:
|
||||
"""Keeps one process's threads off the fastest cores and puts back what each had."""
|
||||
|
||||
def __init__(self, efficient, proc=Path("/proc"), cores=None):
|
||||
self.efficient = efficient
|
||||
self.proc = proc
|
||||
self.cores = cores or Cores()
|
||||
self.pid = None
|
||||
self.before = {}
|
||||
self.present = None
|
||||
self.own = set()
|
||||
|
||||
def tasks(self, pid):
|
||||
try:
|
||||
return [int(tid) for tid in os.listdir(self.proc / str(pid) / "task") if tid.isdigit()]
|
||||
except OSError:
|
||||
return []
|
||||
|
||||
def park(self, pid):
|
||||
self.pid = pid
|
||||
tids = self.tasks(pid)
|
||||
if self.present is None:
|
||||
self.present = set(tids)
|
||||
masks = {}
|
||||
for tid in tids:
|
||||
if tid not in self.before:
|
||||
try:
|
||||
masks[tid] = self.cores.get(tid)
|
||||
except OSError:
|
||||
continue
|
||||
# A mask the game gave one of its own threads may be passed on by it, not by a parked thread.
|
||||
self.own.update(frozenset(now) for tid, now in masks.items() if tid in self.present)
|
||||
inherited = {}
|
||||
for before, parked in self.before.values():
|
||||
inherited[frozenset(parked)] = inherited.get(frozenset(parked), set()) | before
|
||||
for tid, now in masks.items():
|
||||
try:
|
||||
wanted = now & self.efficient
|
||||
if wanted and wanted != now:
|
||||
self.cores.set(tid, wanted)
|
||||
self.before[tid] = (now, wanted)
|
||||
elif tid not in self.present and frozenset(now) in inherited and frozenset(now) not in self.own:
|
||||
self.before[tid] = (inherited[frozenset(now)], now)
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
def release(self):
|
||||
for tid, (before, parked) in self.before.items():
|
||||
try:
|
||||
if self.cores.get(tid) == parked:
|
||||
self.cores.set(tid, before)
|
||||
except OSError:
|
||||
continue
|
||||
self.pid = None
|
||||
self.present = None
|
||||
self.own.clear()
|
||||
self.before.clear()
|
||||
|
||||
|
||||
class Governor:
|
||||
def __init__(self, frames, parker, clock=time.monotonic):
|
||||
self.frames = frames
|
||||
self.parker = parker
|
||||
self.clock = clock
|
||||
self.pid = None
|
||||
self.window = []
|
||||
self.since = None
|
||||
self.held = None
|
||||
self.blocked = {}
|
||||
|
||||
def tick(self):
|
||||
read = self.frames.read()
|
||||
if read is None:
|
||||
return
|
||||
pid, steam, intervals = read
|
||||
now = self.clock()
|
||||
if steam or not pid or pid != self.pid:
|
||||
if self.parker.pid is not None:
|
||||
self.release(now, "focus left the game", backoff=False)
|
||||
self.pid = None if steam else pid
|
||||
self.window, self.since = [], now
|
||||
return
|
||||
self.window.extend((now, value) for value in intervals)
|
||||
if self.parker.pid is None and not intervals:
|
||||
self.window, self.since = [], now
|
||||
return
|
||||
if self.parker.pid is None:
|
||||
self.consider(now)
|
||||
else:
|
||||
self.watch(now)
|
||||
|
||||
def consider(self, now):
|
||||
self.window = [entry for entry in self.window if entry[0] >= now - SETTLE]
|
||||
if now - self.since < SETTLE or now < self.blocked.get(self.pid, (0, 0))[0]:
|
||||
return
|
||||
intervals = [value for _, value in self.window]
|
||||
if sum(intervals) < 0.9 * SETTLE * 1e9:
|
||||
return
|
||||
held = paced_interval(intervals)
|
||||
if held is None:
|
||||
return
|
||||
self.held = held
|
||||
self.parker.park(self.pid)
|
||||
if self.parker.before:
|
||||
self.window = []
|
||||
self.since = now
|
||||
log(f"pid {self.pid} holds {1e9 / held:.0f} fps, {len(self.parker.before)} thread(s) off the fastest cores")
|
||||
else:
|
||||
self.parker.release()
|
||||
|
||||
def watch(self, now):
|
||||
self.parker.park(self.pid)
|
||||
self.window = [entry for entry in self.window if entry[0] >= now - WINDOW]
|
||||
intervals = [value for _, value in self.window]
|
||||
if now - self.since < WINDOW:
|
||||
return
|
||||
if len(intervals) < 0.5 * WINDOW * 1e9 / self.held:
|
||||
self.release(now, "frames stalled")
|
||||
return
|
||||
if late_share(intervals, self.held) > RELEASE_LATE_SHARE or median(intervals) > SLOWER * self.held:
|
||||
self.release(now, "frames fell behind")
|
||||
|
||||
def release(self, now, why, backoff=True):
|
||||
pid = self.parker.pid
|
||||
self.parker.release()
|
||||
if backoff:
|
||||
_, wait = self.blocked.get(pid, (0, BACKOFF / 2))
|
||||
wait = min(wait * 2, BACKOFF_MAX)
|
||||
self.blocked[pid] = (now + wait, wait)
|
||||
log(f"pid {pid} back on every core: {why}; not again for {wait:.0f} s")
|
||||
else:
|
||||
log(f"pid {pid} back on every core: {why}")
|
||||
self.window, self.since = [], now
|
||||
|
||||
def stop(self, *_):
|
||||
self.parker.release()
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
def main(argv):
|
||||
if len(argv) != 1:
|
||||
print("usage: droiddeck-pace-cores <gamescope HUD frames file>", file=sys.stderr)
|
||||
return 2
|
||||
efficient = efficient_cores()
|
||||
if not efficient:
|
||||
return 0
|
||||
governor = Governor(Frames(argv[0]), Parker(efficient))
|
||||
for sig in (signal.SIGTERM, signal.SIGHUP, signal.SIGINT):
|
||||
signal.signal(sig, governor.stop)
|
||||
while True:
|
||||
governor.tick()
|
||||
time.sleep(POLL)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main(sys.argv[1:]))
|
||||
@@ -315,8 +315,8 @@ def locate(program, root):
|
||||
os.path.dirname(program), root, "own")
|
||||
|
||||
|
||||
def fixed(program, root, appid):
|
||||
return Target(appid, os.path.join(root, "steamapps", "compatdata", str(appid)),
|
||||
def fixed(program, root, appid, compatdata=None):
|
||||
return Target(appid, compatdata or os.path.join(root, "steamapps", "compatdata", str(appid)),
|
||||
os.path.dirname(program), root, "fixed")
|
||||
|
||||
|
||||
@@ -451,6 +451,7 @@ def arguments(program, args, desktop=None, compatdata=None):
|
||||
def main(argv):
|
||||
window = False
|
||||
prefix = None
|
||||
compatdata = None
|
||||
verb = VERB
|
||||
game_id = None
|
||||
desktop = None
|
||||
@@ -461,6 +462,8 @@ def main(argv):
|
||||
window = True
|
||||
elif option == "--prefix" and argv and prefix_id(argv[0]):
|
||||
prefix = argv.pop(0)
|
||||
elif option == "--compatdata" and argv and os.path.isabs(argv[0]):
|
||||
compatdata = argv.pop(0)
|
||||
elif option == "--verb" and argv and argv[0] in VERBS:
|
||||
verb = argv.pop(0)
|
||||
elif option == "--game-id" and argv and argv[0].isdigit() and argv[0] != "0":
|
||||
@@ -484,7 +487,7 @@ def main(argv):
|
||||
say("%s is not an app id" % app)
|
||||
return 2
|
||||
if not argv and app is None:
|
||||
say("usage: droiddeck-proton-run [--window] [--prefix ID] [--verb run] [--game-id ID] [--desktop WxH] PROGRAM [ARGS...]")
|
||||
say("usage: droiddeck-proton-run [--window] [--prefix ID [--compatdata DIR]] [--verb run] [--game-id ID] [--desktop WxH] PROGRAM [ARGS...]")
|
||||
return 2
|
||||
root = os.environ.get("STEAM_COMPAT_CLIENT_INSTALL_PATH") or STEAM_ROOT
|
||||
if options is None:
|
||||
@@ -509,7 +512,7 @@ def main(argv):
|
||||
if app is not None and int(app) >= STEAM_APP_LIMIT:
|
||||
target = shortcut_target(app, program, start, root)
|
||||
elif prefix:
|
||||
target = fixed(program, root, prefix)
|
||||
target = fixed(program, root, prefix, compatdata)
|
||||
else:
|
||||
target = locate(program, root)
|
||||
available = tools(root)
|
||||
|
||||
@@ -56,18 +56,75 @@ libraries="$steam_root"
|
||||
for library in $LIBRARIES; do libraries="$libraries
|
||||
$library"; done
|
||||
libraries=$(printf '%s\n%s\n' "$libraries" "$(sed -n 's/^[[:space:]]*"path"[[:space:]]*"\(.*\)"[[:space:]]*$/\1/p' "$steam_root/steamapps/libraryfolders.vdf" 2>/dev/null)" | awk 'NF && !seen[$0]++')
|
||||
sig=$(while IFS= read -r library; do ls -l --time-style=+%s "$library"/steamapps/appmanifest_*.acf 2>/dev/null; done <<<"$libraries" | cksum)
|
||||
if [ "$(head -n 1 "$cache" 2>/dev/null)" != "$sig" ]; then
|
||||
{
|
||||
printf '%s\n' "$sig"
|
||||
while IFS= read -r library; do
|
||||
[ -d "$library/steamapps/common" ] || continue
|
||||
find "$library/steamapps/common" -mindepth 2 -maxdepth 5 -iname 'installscript*.vdf' \
|
||||
-not -path '*/Steamworks Shared/*' 2>/dev/null
|
||||
done <<<"$libraries" | sort -u
|
||||
} >"$cache.tmp" && mv -f "$cache.tmp" "$cache"
|
||||
games=$(while IFS= read -r library; do
|
||||
library="$library" awk '
|
||||
function flush() { if (dir != "") printf "%s|%s|%s|%s\t%s/steamapps/common/%s\n", file, build, updated, size, ENVIRON["library"], dir }
|
||||
FNR == 1 { flush(); file = FILENAME; dir = build = updated = size = "" }
|
||||
{
|
||||
n = split($0, part, "\"")
|
||||
for (i = 2; i + 2 <= n; i += 2) {
|
||||
name = tolower(part[i])
|
||||
if (name == "installdir" && dir == "") dir = part[i + 2]
|
||||
else if (name == "buildid" && build == "") build = part[i + 2]
|
||||
else if (name == "lastupdated" && updated == "") updated = part[i + 2]
|
||||
else if (name == "sizeondisk" && size == "") size = part[i + 2]
|
||||
else continue
|
||||
i += 2
|
||||
}
|
||||
}
|
||||
END { flush() }
|
||||
' "$library"/steamapps/appmanifest_*.acf 2>/dev/null
|
||||
done <<<"$libraries")
|
||||
legacy=
|
||||
[ -s "$cache" ] && ! grep -q $'\t' "$cache" && legacy=$(tail -n +2 "$cache"; echo .)
|
||||
declare -A scanned=() current=()
|
||||
read_scanned() {
|
||||
scanned=()
|
||||
while IFS=$'\t' read -r key _; do
|
||||
[ -n "$key" ] && scanned[$key]=1
|
||||
done < <(grep $'\t' "$cache" 2>/dev/null)
|
||||
}
|
||||
read_scanned
|
||||
later=
|
||||
fresh=()
|
||||
while IFS=$'\t' read -r key dir; do
|
||||
[ -n "$key" ] || continue
|
||||
current[$key]=1
|
||||
[ -z "${scanned[$key]:-}" ] || continue
|
||||
if [ -z "$legacy" ] && [ -n "${DROIDDECK_SEED_SCRIPTS:-}${DROIDDECK_SEED_LATER:-}" ]; then
|
||||
[ -z "${DROIDDECK_SEED_LATER:-}" ] || later=1
|
||||
continue
|
||||
fi
|
||||
[ -d "$dir" ] || continue
|
||||
scanned[$key]=1
|
||||
if [ -n "$legacy" ] && [ "$cache" -nt "${key%%|*}" ]; then
|
||||
found=$(printf '%s\n' "${legacy%.}" | dir="$dir" awk 'index($0, ENVIRON["dir"] "/") == 1')
|
||||
else
|
||||
found=$(find "$dir" -mindepth 1 -maxdepth 4 -iname 'installscript*.vdf' \
|
||||
-not -path '*/Steamworks Shared/*' 2>/dev/null) || continue
|
||||
found=$(printf '%s\n' "$found" | sort -u)
|
||||
fi
|
||||
fresh+=("$key" "$found")
|
||||
done <<<"$games"
|
||||
exec 9>"$cache.lock" && command -v flock >/dev/null 2>&1 && flock 9
|
||||
if [ ${#fresh[@]} -gt 0 ]; then
|
||||
read_scanned
|
||||
for ((i = 0; i < ${#fresh[@]}; i += 2)); do
|
||||
[ -z "${scanned[${fresh[i]}]:-}" ] || continue
|
||||
printf '%s\n' "${fresh[i + 1]}" | key="${fresh[i]}" awk '{ print ENVIRON["key"] "\t" $0 }' >>"$cache"
|
||||
done
|
||||
fi
|
||||
game_scripts() { { tail -n +2 "$cache" 2>/dev/null; printf '%s\n' "${DROIDDECK_SEED_SCRIPTS:-}"; } | sed '/^$/d' | tr '\n' '\0'; }
|
||||
if [ -n "$later" ]; then
|
||||
(DROIDDECK_SEED_LATER= DROIDDECK_SEED_SCRIPTS= DROIDDECK_SEED_DEFERRED=1 setsid "$0" "$steam_root" >/dev/null 2>&1 9>&- &)
|
||||
fi
|
||||
if [ -e "$cache" ]; then
|
||||
keys=$(printf '%s\n' "${!current[@]}") awk -F'\t' '
|
||||
BEGIN { n = split(ENVIRON["keys"], key, "\n"); for (i = 1; i <= n; i++) want[key[i]] = 1 }
|
||||
NF > 1 && ($1 in want)
|
||||
' "$cache" >"$cache.tmp" && { cmp -s "$cache.tmp" "$cache" && rm -f "$cache.tmp" || mv -f "$cache.tmp" "$cache"; }
|
||||
fi
|
||||
[ -z "${DROIDDECK_SEED_DEFERRED:-}" ] || ! pgrep -x wineserver >/dev/null 2>&1 || exit 0
|
||||
game_scripts() { { cut -s -f2- "$cache" 2>/dev/null; printf '%s\n' "${DROIDDECK_SEED_SCRIPTS:-}"; } | sed '/^$/d' | tr '\n' '\0'; }
|
||||
game_pairs=$(game_scripts | read_scripts)
|
||||
[ -n "$(printf '%s' "$game_pairs" | tr -d '[:space:]')" ] && pairs=$(printf '%s\n%s\n' "$pairs" "$game_pairs" | sort -u | sed '/^$/d')
|
||||
declare -A vulkan=()
|
||||
|
||||
@@ -132,8 +132,7 @@ clear_stale_tmp() {
|
||||
[ -z "${BL_INSIDE:-}" ] && clear_stale_tmp
|
||||
if [ -z "${BL_INSIDE:-}" ]; then
|
||||
if [ "${BL_ZRAM:-0}" = 1 ]; then printf 0 > /tmp/.bl-zram; else rm -f /tmp/.bl-zram; fi
|
||||
rm -f /tmp/.bl-clock
|
||||
head -c 8 /dev/zero > /tmp/.bl-clock
|
||||
dd if=/dev/zero of=/tmp/.bl-clock bs=8 count=1 conv=notrunc status=none
|
||||
fi
|
||||
|
||||
steam_devtools() {
|
||||
@@ -512,6 +511,12 @@ if [ -z "${BL_INSIDE:-}" ]; then
|
||||
echo "== gamescope: games at ${nested}x${nested_h} stretched to the ${width}x${height} output"
|
||||
fi
|
||||
fi
|
||||
if [ "$mode" = run ] && [ "${BL_GAME_WIDTH:-0}" -gt 0 ] && [ "${BL_GAME_HEIGHT:-0}" -gt 0 ]; then
|
||||
nested=$BL_GAME_WIDTH
|
||||
nested_h=$BL_GAME_HEIGHT
|
||||
game_size=1
|
||||
echo "== gamescope: the game runs at ${nested}x${nested_h}, scaled to the ${width}x${height} output with its proportions kept"
|
||||
fi
|
||||
backend=${BL_GAMESCOPE_BACKEND:-wayland}
|
||||
case "$backend" in
|
||||
wayland) ;;
|
||||
@@ -529,7 +534,7 @@ if [ -z "${BL_INSIDE:-}" ]; then
|
||||
echo "== gamescope: composites every frame (Mali bridge: the Steam UI flickers on separate planes)"
|
||||
fi
|
||||
args=(--backend "$backend" --expose-wayland -f -W "$width" -H "$height" -w "$nested" -h "$nested_h")
|
||||
if [ "$nested" != "$width" ] || [ "$nested_h" != "$height" ]; then
|
||||
if [ "${game_size:-0}" != 1 ] && { [ "$nested" != "$width" ] || [ "$nested_h" != "$height" ]; }; then
|
||||
args+=(-S stretch)
|
||||
export BL_STRETCHED=1
|
||||
fi
|
||||
@@ -812,19 +817,30 @@ case $mode in
|
||||
fi
|
||||
}
|
||||
bl_zram_usage() {
|
||||
local _p _k _v _u _rss=0 _swap=0
|
||||
local _p _k _v _u _resident _anonymous _rss=0 _anon=0 _swap=0
|
||||
for _p in "$@"; do
|
||||
_resident=0 _anonymous=-1
|
||||
while read -r _k _v _u; do
|
||||
case $_k in VmRSS:) _rss=$((_rss + _v)) ;; VmSwap:) _swap=$((_swap + _v)) ;; esac
|
||||
case $_v in ''|*[!0-9]*) continue ;; esac
|
||||
case $_k in
|
||||
VmRSS:) _resident=$_v ;;
|
||||
RssAnon:) _anonymous=$_v ;;
|
||||
VmSwap:) _swap=$((_swap + _v)) ;;
|
||||
esac
|
||||
done 2>/dev/null < "/proc/$_p/status"
|
||||
[ "$_anonymous" -ge 0 ] || _anonymous=$_resident
|
||||
_rss=$((_rss + _resident)) _anon=$((_anon + _anonymous))
|
||||
done
|
||||
_bl_rss=$_rss
|
||||
_bl_rss=$_rss _bl_anon=$_anon
|
||||
_bl_swap=$_swap
|
||||
echo "$# processes, resident $((_rss / 1024)) MB, in zram $((_swap / 1024)) MB"
|
||||
}
|
||||
bl_memory_read() {
|
||||
local _k _v _u _kind _fields _avg
|
||||
_bl_mem_total=0 _bl_available=-1 _bl_swap_total=0 _bl_swap_free=-1 _bl_psi=0
|
||||
local _k _v _u _kind _fields _avg _whole _frac _gpu_bytes _growth _saved_growth _saved_fall
|
||||
_saved_growth=${_bl_preserved_growth:-0} _saved_fall=${_bl_preserved_fall:-0}
|
||||
_bl_preserved_growth=0 _bl_preserved_fall=0
|
||||
_bl_mem_total=0 _bl_available=-1 _bl_swap_total=0 _bl_swap_free=-1
|
||||
_bl_psi_some=0 _bl_psi_full=0 _bl_gpu=0 _bl_unevictable=0 _bl_shmem=0
|
||||
while read -r _k _v _u; do
|
||||
case $_v in ''|*[!0-9]*) continue ;; esac
|
||||
case $_k in
|
||||
@@ -832,15 +848,50 @@ case $mode in
|
||||
MemAvailable:) _bl_available=$_v ;;
|
||||
SwapTotal:) _bl_swap_total=$_v ;;
|
||||
SwapFree:) _bl_swap_free=$_v ;;
|
||||
Unevictable:) _bl_unevictable=$_v ;;
|
||||
Shmem:) _bl_shmem=$_v ;;
|
||||
esac
|
||||
done 2>/dev/null < /proc/meminfo
|
||||
if [ -r /proc/pressure/memory ]; then
|
||||
while read -r _kind _fields; do
|
||||
[ "$_kind" = some ] || continue
|
||||
_avg=${_fields#avg10=} _avg=${_avg%% *} _avg=${_avg%%.*}
|
||||
case $_avg in ''|*[!0-9]*) ;; *) _bl_psi=$((10#$_avg)) ;; esac
|
||||
[ "$_kind" = some ] || [ "$_kind" = full ] || continue
|
||||
_avg=${_fields#avg10=} _avg=${_avg%% *}
|
||||
_whole=${_avg%%.*} _frac=${_avg#*.}
|
||||
[ "$_frac" = "$_avg" ] && _frac=00
|
||||
_frac=${_frac}00
|
||||
_frac=${_frac:0:2}
|
||||
case $_whole$_frac in ''|*[!0-9]*) continue ;; esac
|
||||
if [ "$_kind" = some ]; then
|
||||
_bl_psi_some=$((10#$_whole * 100 + 10#$_frac))
|
||||
else
|
||||
_bl_psi_full=$((10#$_whole * 100 + 10#$_frac))
|
||||
fi
|
||||
done 2>/dev/null < /proc/pressure/memory
|
||||
fi
|
||||
if [ -r /sys/class/kgsl/kgsl/page_alloc ]; then
|
||||
read -r _gpu_bytes _u < /sys/class/kgsl/kgsl/page_alloc
|
||||
case $_gpu_bytes in ''|*[!0-9]*) _gpu_bytes=0 ;; esac
|
||||
_bl_gpu=$((_gpu_bytes / 1024))
|
||||
fi
|
||||
_bl_psi=$_bl_psi_some
|
||||
[ "$_bl_psi_full" -gt "$_bl_psi" ] && _bl_psi=$_bl_psi_full
|
||||
if [ "${_bl_sample_at:-}" = "" ]; then
|
||||
_bl_growth=0 _bl_fall=0
|
||||
_bl_sample_at=$SECONDS _bl_sample_available=$_bl_available _bl_sample_gpu=$_bl_gpu
|
||||
_bl_sample_unevictable=$_bl_unevictable _bl_sample_shmem=$_bl_shmem
|
||||
elif [ "$SECONDS" -gt "$_bl_sample_at" ]; then
|
||||
_bl_growth=0 _bl_fall=0
|
||||
_growth=$((_bl_gpu - _bl_sample_gpu)); [ "$_growth" -gt 0 ] && _bl_growth=$((_bl_growth + _growth))
|
||||
_growth=$((_bl_unevictable - _bl_sample_unevictable)); [ "$_growth" -gt 0 ] && _bl_growth=$((_bl_growth + _growth))
|
||||
_growth=$((_bl_shmem - _bl_sample_shmem)); [ "$_growth" -gt 0 ] && _bl_growth=$((_bl_growth + _growth))
|
||||
_bl_fall=$((_bl_sample_available - _bl_available))
|
||||
[ "$_saved_growth" -gt "$_bl_growth" ] && _bl_growth=$_saved_growth
|
||||
[ "$_saved_fall" -gt "$_bl_fall" ] && _bl_fall=$_saved_fall
|
||||
_bl_sample_at=$SECONDS _bl_sample_available=$_bl_available _bl_sample_gpu=$_bl_gpu
|
||||
_bl_sample_unevictable=$_bl_unevictable _bl_sample_shmem=$_bl_shmem
|
||||
fi
|
||||
_bl_real_available=$_bl_available
|
||||
[ "$_bl_real_available" -ge 0 ] && _bl_real_available=$((_bl_real_available - _bl_growth))
|
||||
_bl_headroom=$((_bl_mem_total / 5))
|
||||
[ "$_bl_headroom" -ge $((512 * 1024)) ] || _bl_headroom=$((512 * 1024))
|
||||
[ "$_bl_headroom" -le $((1536 * 1024)) ] || _bl_headroom=$((1536 * 1024))
|
||||
@@ -852,25 +903,29 @@ case $mode in
|
||||
}
|
||||
bl_memory_tight() {
|
||||
[ "$_bl_mem_total" -gt 0 ] && [ "$_bl_available" -ge 0 ] &&
|
||||
{ [ "$_bl_available" -lt "$_bl_headroom" ] ||
|
||||
{ [ "$_bl_psi" -ge 1 ] && [ "$_bl_available" -lt $((_bl_mem_total / 3)) ]; }; }
|
||||
{ [ "$_bl_real_available" -lt "$_bl_headroom" ] ||
|
||||
{ [ "$_bl_psi" -ge 100 ] && [ "$_bl_real_available" -lt $((_bl_mem_total / 3)) ]; } ||
|
||||
{ [ "$_bl_fall" -ge $((256 * 1024)) ] && [ "$_bl_real_available" -lt $((_bl_headroom * 2)) ]; }; }
|
||||
}
|
||||
bl_client_can_pageout() {
|
||||
bl_memory_read
|
||||
bl_zram_usage "${_bl_pids[@]}" > /dev/null
|
||||
[ "$_bl_mem_total" -gt 0 ] && [ "$_bl_swap_total" -gt 0 ] &&
|
||||
[ $((_bl_swap_free - _bl_rss)) -ge "$_bl_swap_reserve" ]
|
||||
[ "$_bl_mem_total" -gt 0 ] && bl_zram_has_room
|
||||
}
|
||||
bl_zram_has_room() {
|
||||
[ "$_bl_swap_total" -gt 0 ] &&
|
||||
[ $((_bl_swap_free - _bl_anon - ${_bl_client_reserve:-0})) -ge "$_bl_swap_reserve" ]
|
||||
}
|
||||
bl_client_can_readback() {
|
||||
bl_memory_read
|
||||
bl_zram_usage "${_bl_pids[@]}" > /dev/null
|
||||
[ "$_bl_mem_total" -gt 0 ] && ! bl_memory_tight &&
|
||||
[ "$_bl_available" -ge $((_bl_swap * 2)) ] &&
|
||||
[ $((_bl_available - _bl_swap)) -ge "$_bl_headroom" ]
|
||||
[ "$_bl_mem_total" -gt 0 ] && ! bl_memory_tight && ! bl_memory_critical &&
|
||||
[ "$_bl_real_available" -ge $((_bl_swap * 2)) ] &&
|
||||
[ $((_bl_real_available - _bl_swap)) -ge "$_bl_headroom" ]
|
||||
}
|
||||
bl_client_pressure() {
|
||||
[ "${BL_ZRAM_CLIENT:-1}" = 1 ] && [ "$_bl_game" = 1 ] &&
|
||||
[ "${#_bl_pids[@]}" -gt 0 ] && bl_memory_tight || return 0
|
||||
[ "${#_bl_pids[@]}" -gt 0 ] && { bl_memory_tight || bl_memory_critical; } || return 0
|
||||
[ "$_bl_pressure_at" -lt 0 ] ||
|
||||
[ $((SECONDS - _bl_pressure_at)) -ge "$_bl_pressure_backoff" ] || return 0
|
||||
[ "$_bl_packed" -lt 0 ] || [ $((SECONDS - _bl_packed)) -ge 30 ] || return 0
|
||||
@@ -880,12 +935,29 @@ case $mode in
|
||||
return 0
|
||||
fi
|
||||
[ "$_bl_rss" -ge $((128 * 1024)) ] || return 0
|
||||
local _before=$_bl_rss
|
||||
bl_zram_send 1 "memory pressure, compressing the client (available $((_bl_available / 1024)) MB, swap free $((_bl_swap_free / 1024)) MB)" client
|
||||
_bl_pressure_before=$_bl_rss
|
||||
if [ "${1:-}" = defer ]; then
|
||||
_bl_client_reserve=$_bl_anon
|
||||
_bl_client_pending=1
|
||||
fi
|
||||
bl_zram_send 1 "memory pressure, compressing the client (available $((_bl_available / 1024)) MB, swap free $((_bl_swap_free / 1024)) MB)" client "${1:-}"
|
||||
_bl_packed=$SECONDS
|
||||
if [ "${1:-}" = defer ]; then
|
||||
_bl_client_targets=("${_bl_zram_targets[@]}")
|
||||
else
|
||||
bl_client_pressure_result
|
||||
fi
|
||||
}
|
||||
bl_client_pressure_finish() {
|
||||
[ "${_bl_client_pending:-0}" = 1 ] || return 0
|
||||
bl_zram_finish 1 "${_bl_client_targets[@]}"
|
||||
bl_client_pressure_result "${_bl_client_targets[@]}"
|
||||
_bl_client_pending=0 _bl_client_reserve=0 _bl_client_targets=()
|
||||
}
|
||||
bl_client_pressure_result() {
|
||||
[ "${_bl_pageout_complete:-0}" = 1 ] || return 0
|
||||
bl_zram_usage "${_bl_pids[@]}" > /dev/null
|
||||
if [ $((_before - _bl_rss)) -lt $((16 * 1024)) ]; then
|
||||
if [ "$#" -gt 0 ]; then bl_zram_usage "$@" > /dev/null; else bl_zram_usage "${_bl_pids[@]}" > /dev/null; fi
|
||||
if [ $((_bl_pressure_before - _bl_rss)) -lt $((16 * 1024)) ]; then
|
||||
_bl_pressure_backoff=$((_bl_pressure_backoff * 2 > 300 ? 300 : _bl_pressure_backoff * 2))
|
||||
else
|
||||
_bl_pressure_backoff=30
|
||||
@@ -893,15 +965,13 @@ case $mode in
|
||||
}
|
||||
bl_memory_critical() {
|
||||
[ "$_bl_mem_total" -gt 0 ] && [ "$_bl_available" -ge 0 ] &&
|
||||
{ [ "$_bl_available" -lt "$_bl_tier_headroom" ] ||
|
||||
{ [ "$_bl_psi" -ge 1 ] && [ "$_bl_available" -lt $((_bl_tier_headroom * 3 / 2)) ]; }; }
|
||||
}
|
||||
bl_low_ram() {
|
||||
[ "$_bl_mem_total" -gt 0 ] && [ "$_bl_mem_total" -le $((13 * 1024 * 1024)) ]
|
||||
{ [ "$_bl_real_available" -lt "$_bl_tier_headroom" ] ||
|
||||
{ [ "$_bl_psi" -ge 100 ] && [ "$_bl_real_available" -lt $((_bl_tier_headroom * 3 / 2)) ]; } ||
|
||||
{ [ "$_bl_fall" -ge $((256 * 1024)) ] && [ "$_bl_real_available" -lt $((_bl_tier_headroom * 2)) ]; }; }
|
||||
}
|
||||
bl_session_pressure() {
|
||||
bl_memory_read
|
||||
[ "$_bl_game" = 1 ] && bl_low_ram && bl_memory_critical || return 0
|
||||
[ "$_bl_game" = 1 ] && bl_memory_critical || return 0
|
||||
local _wait=$_bl_tier_backoff _before _pids _targets
|
||||
[ "$_bl_available" -ge $((_bl_headroom / 2)) ] || [ "$_bl_tier_pushed" -lt 0 ] || _wait=30
|
||||
[ "$_bl_tier_at" -lt 0 ] || [ $((SECONDS - _bl_tier_at)) -ge "$_wait" ] || return 0
|
||||
@@ -917,21 +987,18 @@ case $mode in
|
||||
fi
|
||||
fi
|
||||
_bl_tier_at=$SECONDS _bl_tier_pushed=0
|
||||
bl_zram_pick game
|
||||
bl_zram_pick session
|
||||
bl_zram_usage "${_pids[@]}" > /dev/null
|
||||
[ "$_bl_rss" -ge $((256 * 1024)) ] || return 0
|
||||
if [ "$_bl_swap_total" -le 0 ] || [ $((_bl_swap_free - _bl_rss)) -lt "$_bl_swap_reserve" ]; then
|
||||
if ! bl_zram_has_room; then
|
||||
step "zRAM: low-memory page-out skipped, swap headroom too small ($((_bl_swap_free / 1024)) MB free)"
|
||||
return 0
|
||||
fi
|
||||
_targets=("${_pids[@]}") _before=$_bl_swap
|
||||
bl_zram_send 1 "low memory (available $((_bl_available / 1024)) MB), compressing idle game memory" game
|
||||
bl_zram_send 1 "low memory (available $((_bl_real_available / 1024)) MB), compressing idle game memory" session
|
||||
bl_zram_usage "${_targets[@]}" > /dev/null
|
||||
_bl_tier_pushed=$((_bl_swap - _before))
|
||||
bl_tier_note "${_targets[@]}"
|
||||
if [ "${_bl_pageout_complete:-0}" = 1 ] && [ "${BL_ZRAM_CLIENT:-1}" = 1 ] && [ "${#_bl_pids[@]}" -gt 0 ]; then
|
||||
_bl_pressure_at=$SECONDS _bl_packed=$SECONDS
|
||||
fi
|
||||
}
|
||||
bl_proc_start() {
|
||||
local _s=
|
||||
@@ -962,7 +1029,7 @@ case $mode in
|
||||
}
|
||||
bl_watch_nap() {
|
||||
local _i
|
||||
if [ "$_bl_game" = 1 ] && bl_low_ram && [ "$_bl_available" -ge 0 ] && [ "$_bl_available" -lt $((_bl_tier_headroom * 2)) ]; then
|
||||
if [ "$_bl_game" = 1 ] && [ "$_bl_available" -ge 0 ] && [ "$_bl_real_available" -lt $((_bl_tier_headroom * 2)) ]; then
|
||||
for _i in 1 2 3 4 5; do
|
||||
nap 1
|
||||
bl_memory_read
|
||||
@@ -1018,10 +1085,16 @@ case $mode in
|
||||
for _p in "${_pids[@]}"; do rm -f "/tmp/.bl-zram.ack.$_p"; done
|
||||
printf '%s' "$_mode" > /tmp/.bl-zram
|
||||
[ "${#_pids[@]}" = 0 ] || kill -URG "${_pids[@]}" 2>/dev/null
|
||||
if [ "$_mode" = 1 ]; then bl_zram_wait "${_pids[@]}"; else nap 5; fi
|
||||
_bl_zram_targets=("${_pids[@]}")
|
||||
[ "${4:-}" != defer ] || return 0
|
||||
bl_zram_finish "$_mode" "${_pids[@]}"
|
||||
}
|
||||
bl_zram_finish() {
|
||||
local _mode=$1
|
||||
shift
|
||||
if [ "$_mode" = 1 ]; then bl_zram_wait "$@"; else nap 5; fi
|
||||
bl_zram_scan
|
||||
bl_zram_pick "$_which"
|
||||
step "zRAM: done ($(bl_zram_usage "${_pids[@]}"))"
|
||||
step "zRAM: done ($(bl_zram_usage "$@"))"
|
||||
}
|
||||
bl_game_focused() {
|
||||
if [ "${BL_DESKTOP:-}" = 1 ] || [ -z "${BL_LAUNCH_DIR:-}" ]; then
|
||||
@@ -1048,6 +1121,8 @@ case $mode in
|
||||
}
|
||||
_bl_since=-1 _bl_since_apps= _bl_packed=-1 _bl_measured=-1 _bl_unpack=0 _bl_capped=-1 _bl_backoff=30
|
||||
_bl_pressure_at=-1 _bl_pressure_backoff=30 _bl_tier_at=-1 _bl_tier_backoff=30 _bl_tier_pushed=0 _bl_tier_back=0 _bl_tier_targets=() _bl_tier_swaps=() _bl_tier_starts=()
|
||||
_bl_client_pending=0 _bl_client_reserve=0 _bl_client_targets=()
|
||||
_bl_sample_at= _bl_sample_available=0 _bl_sample_gpu=0 _bl_sample_unevictable=0 _bl_sample_shmem=0
|
||||
while kill -0 $$ 2>/dev/null; do
|
||||
bl_zram_scan
|
||||
bl_memory_read
|
||||
@@ -1074,8 +1149,11 @@ case $mode in
|
||||
_bl_backoff=30
|
||||
fi
|
||||
fi
|
||||
_bl_preserved_growth=$_bl_growth _bl_preserved_fall=$_bl_fall
|
||||
bl_client_pressure defer
|
||||
_bl_preserved_growth=$_bl_growth _bl_preserved_fall=$_bl_fall
|
||||
bl_session_pressure
|
||||
bl_client_pressure
|
||||
bl_client_pressure_finish
|
||||
if [ "$_bl_game" = 1 ] && bl_game_focused; then
|
||||
if [ "$_bl_since" -lt 0 ] || [ "$_bl_since_apps" != "$_bl_apps" ]; then
|
||||
_bl_since=$SECONDS _bl_since_apps=$_bl_apps
|
||||
@@ -1105,6 +1183,10 @@ case $mode in
|
||||
&& [ "${BL_STEAM_UI:-}" != desktop ] && [ -x /usr/local/bin/droiddeck-launcher-cores ]; then
|
||||
/usr/local/bin/droiddeck-launcher-cores "$BL_LAUNCH_DIR/agent/focus.json" 2>&1 &
|
||||
fi
|
||||
if [ "${BL_PACE_CORES:-1}" != 0 ] && [ -n "${DROIDDECK_HUD_FRAMES:-}" ] && [ -z "${BL_DESKTOP:-}" ] \
|
||||
&& [ -x /usr/local/bin/droiddeck-pace-cores ]; then
|
||||
/usr/local/bin/droiddeck-pace-cores "$DROIDDECK_HUD_FRAMES" 2>&1 &
|
||||
fi
|
||||
|
||||
if [ "$steam_x64" = 0 ]; then
|
||||
step "registering the app's game library with Steam"
|
||||
@@ -1118,7 +1200,7 @@ case $mode in
|
||||
step "adding the app's own games to the client's library"
|
||||
droiddeck-steam-shortcuts "$steam_root" "$BL_ADDED_GAMES" || true
|
||||
fi
|
||||
droiddeck-seed-redists "$steam_root" || true
|
||||
DROIDDECK_SEED_LATER=1 droiddeck-seed-redists "$steam_root" || true
|
||||
else
|
||||
_bl_x64_tool=$(droiddeck-steam-x64 --default-tool)
|
||||
if [ -n "$_bl_x64_tool" ]; then
|
||||
@@ -1132,7 +1214,7 @@ case $mode in
|
||||
if [ -z "${BL_DESKTOP:-}" ] && [ "${BL_STEAM_UI:-}" != desktop ]; then
|
||||
droiddeck-steam-ui-scale "$steam_root" "${BL_WIDTH:-1280}" "${BL_HEIGHT:-720}" || true
|
||||
fi
|
||||
droiddeck-seed-redists "$steam_root" || true
|
||||
DROIDDECK_SEED_LATER=1 droiddeck-seed-redists "$steam_root" || true
|
||||
fi
|
||||
if [ -n "${BL_SHADER_PRECACHE:-}" ]; then
|
||||
droiddeck-steam-shaders "$BL_SHADER_PRECACHE" "$XDG_DATA_HOME/Steam" \
|
||||
@@ -1396,6 +1478,8 @@ case $mode in
|
||||
decky_stop() { [ -z "$decky_child" ] || kill "$decky_child" 2>/dev/null; [ -z "$decky_child" ] || wait "$decky_child" 2>/dev/null; exit 0; }
|
||||
trap decky_stop TERM INT HUP
|
||||
decky_exits=0
|
||||
# The guest has no "deck" user, which Decky otherwise requires at startup.
|
||||
export UNPRIVILEGED_USER=root
|
||||
while :; do
|
||||
if [ "$decky_arch" = x86_64 ]; then /usr/local/bin/droiddeck-fex run --mode on -- "$decky_loader" >>"$HOME/homebrew/services/PluginLoader.log" 2>&1 &
|
||||
else "$decky_loader" >>"$HOME/homebrew/services/PluginLoader.log" 2>&1 &
|
||||
@@ -1458,6 +1542,9 @@ case $mode in
|
||||
_bl_cef=(-cef-disable-gpu -cef-disable-gpu-compositing)
|
||||
echo "== steam: browser UI in software (Mali bridge)"
|
||||
elif [ "${_bl_cef[0]}" = -cef-force-gpu ]; then
|
||||
# With Steam's own web helper layout the Deck UI's footer and overlays flicker on the bridge
|
||||
# (Mali-G78 MP14). ANGLE on Vulkan without Chromium's Vulkan compositing draws them steadily.
|
||||
: "${BL_VKBRIDGE_CEF_ANGLE=vulkan}" "${BL_VKBRIDGE_CEF_VULKAN=0}"
|
||||
_bl_cef=(-cef-force-gpu -cef-use-gl=angle -cef-use-angle="${BL_VKBRIDGE_CEF_ANGLE:-vulkan}")
|
||||
fi
|
||||
[ "${BL_VKBRIDGE_CEF_LOG:-0}" = 1 ] && _bl_cef+=(-cef-enable-logging=stderr -cef-v=1)
|
||||
|
||||
@@ -71,7 +71,11 @@ def store_game(game):
|
||||
|
||||
def manifest_ids(steamapps, name):
|
||||
matches = []
|
||||
for manifest in Path(steamapps).glob("appmanifest_*.acf"):
|
||||
try:
|
||||
manifests = list(Path(steamapps).glob("appmanifest_*.acf"))
|
||||
except OSError:
|
||||
return matches
|
||||
for manifest in manifests:
|
||||
try:
|
||||
install = re.search(r'"installdir"\s*"([^"\\]*)"', manifest.read_text(), re.I)
|
||||
if install and install[1] == name:
|
||||
@@ -81,13 +85,21 @@ def manifest_ids(steamapps, name):
|
||||
return matches
|
||||
|
||||
|
||||
def identify(game):
|
||||
def manifest_identity(game):
|
||||
folder = Path(game["folder"])
|
||||
steamapps = folder.parent.parent
|
||||
for library in ([steamapps] if folder.parent.name == "common" and steamapps.name == "steamapps" else []) + [folder.parent / WINNATIVE]:
|
||||
matches = [appid for appid in manifest_ids(library, folder.name) if appid != SPACEWAR]
|
||||
if matches:
|
||||
return matches[0] if len(matches) == 1 else None
|
||||
return (matches[0], library) if len(matches) == 1 else None
|
||||
return False
|
||||
|
||||
|
||||
def identify(game):
|
||||
folder = Path(game["folder"])
|
||||
found = manifest_identity(game)
|
||||
if found is not False:
|
||||
return found[0] if found is not None else None
|
||||
ids = set()
|
||||
for parent in {folder, Path(game["exe"]).parent}:
|
||||
try:
|
||||
@@ -508,6 +520,57 @@ def release(steam_root, game, stale, reason):
|
||||
print(f"droiddeck-steam-games: {game.get('name', appid)} {reason}; it is no longer Steam app {appid}")
|
||||
|
||||
|
||||
PREFIX_LINKS = ".droiddeck-prefix-links.json"
|
||||
|
||||
|
||||
def steam_prefix(steam_root, game):
|
||||
identity = manifest_identity(game)
|
||||
if not identity or not identity[0]:
|
||||
return None, None
|
||||
appid, steamapps = identity
|
||||
installed = steamapps == Path(game["folder"]).parent.parent
|
||||
for library in [steamapps.parent] if installed else dict.fromkeys([steamapps.parent] + libraries(steam_root)):
|
||||
prefix = library / "steamapps/compatdata" / str(appid)
|
||||
if (prefix / "pfx/drive_c").is_dir():
|
||||
return appid, str(prefix)
|
||||
return appid, None
|
||||
|
||||
|
||||
def share_prefix(steam_root, game):
|
||||
shortcut = Path(steam_root) / "steamapps/compatdata" / str(game["appid"] & 0xFFFFFFFF)
|
||||
record = Path(steam_root) / "steamapps" / PREFIX_LINKS
|
||||
links = read_json(record, {})
|
||||
if not isinstance(links, dict):
|
||||
links = {}
|
||||
before = dict(links)
|
||||
key = str(shortcut)
|
||||
name = game.get("name", "game")
|
||||
appid, target = steam_prefix(steam_root, game)
|
||||
try:
|
||||
current = os.readlink(shortcut) if os.path.islink(shortcut) else None
|
||||
if current is not None and links.get(key) == current and current != target:
|
||||
if target is not None or (not os.path.exists(shortcut) and Path(current).parent.parent.is_dir()):
|
||||
shortcut.unlink()
|
||||
current = None
|
||||
if current is None:
|
||||
links.pop(key, None)
|
||||
if current is None and os.path.lexists(shortcut):
|
||||
if target is not None:
|
||||
print(f"droiddeck-steam-games: {name} keeps its own prefix; Steam app {appid} saves are in {target}")
|
||||
elif current is None and target is not None:
|
||||
shortcut.parent.mkdir(parents=True, exist_ok=True)
|
||||
shortcut.symlink_to(target)
|
||||
links[key] = target
|
||||
print(f"droiddeck-steam-games: {name} shares the saves prefix of Steam app {appid}")
|
||||
except OSError as error:
|
||||
print(f"droiddeck-steam-games: {name} keeps its own prefix ({error})")
|
||||
if links != before:
|
||||
try:
|
||||
save_json(record, links)
|
||||
except OSError as error:
|
||||
print(f"droiddeck-steam-games: prefix links not recorded ({error})")
|
||||
|
||||
|
||||
def route(steam_root, acct, games):
|
||||
snapshot = read_json(Path(acct) / "config" / STATE, {})
|
||||
if not isinstance(snapshot, dict):
|
||||
@@ -557,6 +620,7 @@ def route(steam_root, acct, games):
|
||||
elif named and seeded(steamapps, game, appid):
|
||||
routes[str(game["appid"])] = appid
|
||||
continue
|
||||
share_prefix(steam_root, game)
|
||||
shortcuts.append(game)
|
||||
try:
|
||||
park(steamapps)
|
||||
|
||||
@@ -449,7 +449,11 @@ mkdir -p "$srt_libs"
|
||||
for d in "$STEAM_X64"/steamrt64/pv-runtime/steam-runtime-steamrt/steamrt3c_platform_*/files/lib/x86_64-linux-gnu \
|
||||
"$STEAM_X64"/steamrt64/pv-runtime/steam-runtime-steamrt/steamrt3c_platform_*/files/usr/lib/x86_64-linux-gnu; do
|
||||
[ -d "$d" ] || continue
|
||||
for f in "$d"/*.so*; do [ -e "$srt_libs/$(basename "$f")" ] || ln -s "$f" "$srt_libs/"; done
|
||||
for f in "$d"/*.so*; do
|
||||
[ -e "$f" ] || [ -L "$f" ] || continue
|
||||
s="$srt_libs/$(basename "$f")"
|
||||
{ [ -e "$s" ] || [ -L "$s" ]; } || ln -s "$f" "$srt_libs/"
|
||||
done
|
||||
done
|
||||
FEX_ROOTFS=$ROOTFS /usr/local/bin/FEX "$ROOTFS/usr/sbin/ldconfig.real" -n "$srt_libs" >>"$LOG" 2>&1 || step "ldconfig on $srt_libs failed"
|
||||
sniper_compat=$X64_HOME/sniper-compat
|
||||
@@ -458,6 +462,7 @@ mkdir -p "$sniper_compat"
|
||||
hb=$(ls "$srt_libs"/libharfbuzz.so.0.* 2>/dev/null | head -n 1)
|
||||
if [ -n "$hb" ]; then ln -s "$hb" "$sniper_compat/libharfbuzz.so.0"; else step "warning: no HarfBuzz in Valve's runtime for the newer-runtime titles"; fi
|
||||
for f in "$srt_libs"/*; do
|
||||
[ -e "$f" ] || [ -L "$f" ] || continue
|
||||
n=$(basename "$f")
|
||||
if [ -e "$ROOTFS/usr/lib/x86_64-linux-gnu/$n" ] || [ -e "$ROOTFS/lib/x86_64-linux-gnu/$n" ]; then rm -f "$f"; fi
|
||||
done
|
||||
|
||||
@@ -5,6 +5,7 @@ import binascii
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shlex
|
||||
import shutil
|
||||
import sys
|
||||
import time
|
||||
@@ -47,14 +48,14 @@ def sidecar_at(folder):
|
||||
return sidecar
|
||||
|
||||
|
||||
def find_store_game(argv):
|
||||
def find_store_game(argv, depth=MAX_DEPTH):
|
||||
for raw in argv:
|
||||
arg = raw.strip().strip('"').strip("'").replace("\\", "/")
|
||||
low = arg.lower()
|
||||
if not (low.endswith(LAUNCHER) or low.endswith(".exe") or low.endswith(".bat")) or "/" not in arg:
|
||||
continue
|
||||
folder = Path(arg).parent
|
||||
for _ in range(MAX_DEPTH):
|
||||
for _ in range(depth):
|
||||
sidecar = sidecar_at(folder)
|
||||
if sidecar is not None:
|
||||
return folder, sidecar
|
||||
@@ -64,6 +65,21 @@ def find_store_game(argv):
|
||||
return None
|
||||
|
||||
|
||||
def store_launcher(path):
|
||||
real = os.path.realpath(path)
|
||||
for start in dict.fromkeys([os.path.abspath(path), real]):
|
||||
found = find_store_game([start], depth=len(Path(start).parts))
|
||||
if found is None:
|
||||
continue
|
||||
folder, sidecar = found
|
||||
launcher, exe = str(sidecar.get("launcher") or ""), str(sidecar.get("exe") or "")
|
||||
if not launcher or not exe or (folder / launcher).is_symlink() or not (folder / launcher).is_file():
|
||||
continue
|
||||
if os.path.realpath(folder / exe).lower() == real.lower():
|
||||
return str(folder / launcher)
|
||||
return None
|
||||
|
||||
|
||||
def drop_stale(folder, now=None):
|
||||
code = folder / EPIC_CODE
|
||||
try:
|
||||
@@ -270,12 +286,22 @@ def cloud_on(sidecar):
|
||||
def exited(argv, code=0):
|
||||
found = find_store_game(argv)
|
||||
launch_dir = os.environ.get("BL_LAUNCH_DIR", "")
|
||||
if found is not None and launch_dir and str(found[1].get("store")) == "rockstar":
|
||||
ask(Path(launch_dir) / "stores", {"op": "rockstar-exited", "store": "rockstar", "id": str(found[1].get("id"))}, timeout=1.0)
|
||||
compat_data = os.environ.get("STEAM_COMPAT_DATA_PATH")
|
||||
if compat_data:
|
||||
(Path(compat_data) / "pfx" / ROCKSTAR_FOLDER / "launch.json").unlink(missing_ok=True)
|
||||
return 0
|
||||
if found is not None and str(found[1].get("store")) == "epic" and os.environ.get(EPIC_HANDLED) != "1" \
|
||||
and os.access(EPIC_CLIENT, os.X_OK):
|
||||
try:
|
||||
subprocess.run([EPIC_CLIENT, "exited", "--code", str(code)], stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, timeout=10)
|
||||
except (OSError, subprocess.TimeoutExpired):
|
||||
pass
|
||||
if found is not None and launch_dir and str(found[1].get("store")) == "ea":
|
||||
ask(Path(launch_dir) / "stores", {"op": "ea-exited", "store": "ea", "id": str(found[1].get("id"))}, timeout=1.0)
|
||||
log("ea %s exited" % found[1].get("id"))
|
||||
return 0
|
||||
if found is None or not launch_dir or not cloud_on(found[1]):
|
||||
return 0
|
||||
store, game = str(found[1].get("store")), str(found[1].get("id"))
|
||||
@@ -284,6 +310,443 @@ def exited(argv, code=0):
|
||||
return 0
|
||||
|
||||
|
||||
UBISOFT_DIR = "drive_c/Program Files (x86)/Ubisoft/Ubisoft Game Launcher"
|
||||
UBISOFT_WINDOWS_DIR = "C:\\Program Files (x86)\\Ubisoft\\Ubisoft Game Launcher\\"
|
||||
UBISOFT_INSTALLER_URL = "https://static3.cdn.ubi.com/orbit/launcher_installer/UbisoftConnectInstaller.exe"
|
||||
ROCKSTAR_SERVICE = Path("/usr/local/lib/droiddeck-rockstar")
|
||||
ROCKSTAR_FOLDER = "drive_c/Program Files/Rockstar Games/Launcher"
|
||||
ROCKSTAR_TITLES = {"gta3": "Grand Theft Auto III", "gtavc": "Grand Theft Auto: Vice City", "gtasa": "GTA: San Andreas",
|
||||
"gta4": "Grand Theft Auto IV", "lanoire": "LANoireMTL"}
|
||||
|
||||
|
||||
def rockstar_sdk(folder, sidecar):
|
||||
if str(sidecar.get("id")) in ("gta4", "lanoire"):
|
||||
return True
|
||||
root = folder.resolve()
|
||||
exe = (folder / str(sidecar.get("exe") or "").replace("\\", "/")).resolve()
|
||||
if not exe.is_relative_to(root):
|
||||
return False
|
||||
for directory in {root, exe.parent}:
|
||||
if directory.is_dir() and any(p.is_file() and not p.is_symlink() and p.name.lower() in ("socialclub.dll", "socialclub64.dll")
|
||||
for p in directory.iterdir()):
|
||||
return True
|
||||
if str(sidecar.get("id")) in ROCKSTAR_TITLES:
|
||||
return False
|
||||
level = [root]
|
||||
for _ in range(4):
|
||||
children = []
|
||||
for directory in level:
|
||||
for path in directory.iterdir():
|
||||
if path.is_symlink():
|
||||
continue
|
||||
if path.name.lower() == "title.rgl" and path.is_file() and path.resolve().is_relative_to(root) and 112 <= path.stat().st_size <= 1048576:
|
||||
with path.open("rb") as source:
|
||||
if source.read(4) == b"RGLM":
|
||||
return True
|
||||
if len(children) < 256 and path.is_dir() and path.resolve().is_relative_to(root):
|
||||
children.append(path)
|
||||
level = children
|
||||
return False
|
||||
|
||||
|
||||
def rockstar_registry(folder, title, compat_data, proton, sdk_arch=None):
|
||||
pfx = Path(compat_data) / "pfx"
|
||||
launcher = r"C:\Program Files\Rockstar Games\Launcher"
|
||||
values = [(r"Rockstar Games\Launcher", launcher)]
|
||||
if title:
|
||||
values.append(("Rockstar Games\\" + title, windows_path(str(folder))))
|
||||
entries = [("Software\\" + view + key, value) for view in ("", "Wow6432Node\\") for key, value in values]
|
||||
if sdk_arch:
|
||||
entries.append((r"Software\Rockstar Games\Rockstar Games Social Club", r"C:\Program Files\Rockstar Games\Social Club"))
|
||||
program = "Program Files (x86)" if sdk_arch == "x86" else "Program Files"
|
||||
entries.append((r"Software\Wow6432Node\Rockstar Games\Rockstar Games Social Club", "C:\\" + program + r"\Rockstar Games\Social Club"))
|
||||
reg = pfx / "system.reg"
|
||||
if all(has_value(reg, "[" + key.replace("\\", "\\\\") + "]", "InstallFolder", value) for key, value in entries):
|
||||
return True
|
||||
if wait_for_server(pfx, 2):
|
||||
return all(set_value(reg, "[" + key.replace("\\", "\\\\") + "]", "InstallFolder", value) for key, value in entries)
|
||||
with tempfile.TemporaryDirectory(prefix="droiddeck-rockstar-") as temp:
|
||||
path = Path(temp) / "install.reg"
|
||||
text = "Windows Registry Editor Version 5.00\r\n\r\n"
|
||||
text += "\r\n".join('[HKEY_LOCAL_MACHINE\\%s]\r\n"InstallFolder"="%s"\r\n' % (key, reg_escape(value)) for key, value in entries)
|
||||
path.write_text(text, encoding="utf-16")
|
||||
result = subprocess.run([proton, "run", "reg.exe", "import", windows_path(str(path))],
|
||||
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=45)
|
||||
return result.returncode == 0
|
||||
|
||||
|
||||
def rockstar_runtime(folder, exe, compat_data, arch):
|
||||
if arch == "x86":
|
||||
rockstar_runtime(folder, exe, compat_data, "x64")
|
||||
pfx = Path(compat_data) / "pfx"
|
||||
target = pfx / "drive_c" / ("Program Files (x86)" if arch == "x86" else "Program Files") / "Rockstar Games/Social Club"
|
||||
if not target.resolve().is_relative_to(pfx.resolve()):
|
||||
raise ValueError("invalid Social Club destination")
|
||||
required = ("socialclub.dll", "socialclub.pak") + (("SocialClubHelper.exe",) if arch == "x64" else ())
|
||||
marker = target / ".droiddeck-installing"
|
||||
if all((target / name).is_file() for name in required) and not marker.exists():
|
||||
return "present"
|
||||
root = folder.resolve()
|
||||
installers = [base / sub / "Social-Club-Setup.exe" for base in (root, exe.parent)
|
||||
for sub in ("Redistributables", "Installers")]
|
||||
installer = next((p for p in installers if p.is_file() and p.resolve().is_relative_to(root) and p.stat().st_size <= 256 * 1024 * 1024), None)
|
||||
if installer is None:
|
||||
return "game-runtime"
|
||||
tool = shutil.which("7z")
|
||||
if not tool:
|
||||
return "no-unpacker"
|
||||
listing = subprocess.run([tool, "l", "-t7z", "-slt", "-ba", str(installer)], capture_output=True,
|
||||
stdin=subprocess.DEVNULL, timeout=30)
|
||||
if listing.returncode:
|
||||
return "installer-unreadable"
|
||||
entries, size = [], 0
|
||||
for block in listing.stdout.decode("utf-8", "replace").split("\n\n"):
|
||||
fields = dict(line.split(" = ", 1) for line in block.splitlines() if " = " in line)
|
||||
name = fields.get("Path", "").replace("\\", "/")
|
||||
if not name.startswith(arch + "/"):
|
||||
continue
|
||||
if ".." in name.split("/") or ":" in name or fields.get("Symbolic Link") or fields.get("Hard Link"):
|
||||
raise ValueError("invalid Social Club archive path")
|
||||
entry_size = int(fields.get("Size", "0"))
|
||||
if entry_size < 0:
|
||||
raise ValueError("invalid Social Club member size")
|
||||
size += entry_size
|
||||
entries.append(name)
|
||||
if not entries or len(entries) > 2048 or size > 768 * 1024 * 1024:
|
||||
raise ValueError("invalid Social Club archive size")
|
||||
with tempfile.TemporaryDirectory(prefix="droiddeck-socialclub-") as temporary:
|
||||
result = subprocess.run([tool, "x", "-t7z", "-y", "-o" + temporary, str(installer), arch + "/*"],
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL, timeout=180)
|
||||
source = Path(temporary) / arch
|
||||
if result.returncode or not all((source / name).is_file() for name in required):
|
||||
return "installer-incomplete"
|
||||
target.mkdir(parents=True, exist_ok=True)
|
||||
if marker.is_symlink():
|
||||
raise ValueError("invalid Social Club install marker")
|
||||
marker.touch()
|
||||
for path in source.rglob("*"):
|
||||
if path.is_symlink() or not path.resolve().is_relative_to(source.resolve()):
|
||||
raise ValueError("invalid Social Club archive member")
|
||||
if not path.is_file():
|
||||
continue
|
||||
output = target / path.relative_to(source)
|
||||
if not output.resolve().is_relative_to(pfx.resolve()):
|
||||
raise ValueError("invalid Social Club output path")
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
with tempfile.NamedTemporaryFile(dir=output.parent, delete=False) as stage:
|
||||
stage_path = Path(stage.name)
|
||||
try:
|
||||
with path.open("rb") as stream:
|
||||
shutil.copyfileobj(stream, stage)
|
||||
except BaseException:
|
||||
stage_path.unlink(missing_ok=True)
|
||||
raise
|
||||
os.replace(stage_path, output)
|
||||
marker.unlink()
|
||||
return "installed"
|
||||
|
||||
|
||||
def prepare_rockstar(folder, sidecar, compat_data, launch_dir, proton="", ask_app=None):
|
||||
if not compat_data:
|
||||
return "no-prefix"
|
||||
target = Path(compat_data) / "pfx" / ROCKSTAR_FOLDER
|
||||
if not target.resolve().is_relative_to((Path(compat_data) / "pfx").resolve()):
|
||||
return "invalid-service-path"
|
||||
config = target / "launch.json"
|
||||
config.unlink(missing_ok=True)
|
||||
legacy = str(sidecar.get("id")) in ROCKSTAR_TITLES
|
||||
sdk = rockstar_sdk(folder, sidecar)
|
||||
title = ROCKSTAR_TITLES.get(str(sidecar.get("id")))
|
||||
if not launch_dir:
|
||||
return "no-channel"
|
||||
root = folder.resolve()
|
||||
exe = (folder / str(sidecar.get("exe") or "").replace("\\", "/")).resolve()
|
||||
if not exe.is_relative_to(root):
|
||||
return "invalid-game-path"
|
||||
workdir = (folder / str(sidecar["workdir"])).resolve() if sidecar.get("workdir") is not None else exe.parent
|
||||
if not workdir.is_relative_to(root) or not exe.is_file() or legacy and not any((p / "MTLX.dll").is_file() for p in (root, exe.parent)):
|
||||
return "invalid-game-path"
|
||||
args = sidecar.get("args") or []
|
||||
if not isinstance(args, list) or any(not isinstance(arg, str) or "\0" in arg for arg in args):
|
||||
return "invalid-game-arguments"
|
||||
with exe.open("rb") as source:
|
||||
if source.read(2) != b"MZ":
|
||||
return "invalid-game-exe"
|
||||
source.seek(0x3c)
|
||||
raw = source.read(4)
|
||||
if len(raw) != 4:
|
||||
return "invalid-game-exe"
|
||||
source.seek(int.from_bytes(raw, "little"))
|
||||
signature = source.read(6)
|
||||
if len(signature) != 6 or signature[:4] != b"PE\0\0" or signature[4:] not in (b"\x4c\x01", b"\x64\x86"):
|
||||
return "invalid-game-exe"
|
||||
service = ROCKSTAR_SERVICE / ("Launcher32.exe" if signature[4:] == b"\x4c\x01" else "Launcher64.exe")
|
||||
if not service.is_file():
|
||||
return "no-service"
|
||||
made = "present" if (Path(compat_data) / "pfx/user.reg").is_file() else create_prefix(compat_data, proton)
|
||||
if made not in ("created", "present"):
|
||||
return "prefix-" + made
|
||||
arch = "x86" if signature[4:] == b"\x4c\x01" else "x64"
|
||||
if sdk:
|
||||
log("rockstar runtime: " + rockstar_runtime(folder, exe, compat_data, arch))
|
||||
if not rockstar_registry(folder, title, compat_data, proton, sdk_arch=arch if sdk else None):
|
||||
return "registry-unavailable"
|
||||
target.mkdir(parents=True, exist_ok=True)
|
||||
with tempfile.NamedTemporaryFile(prefix="Launcher-", suffix=".exe", dir=target, delete=False) as temporary:
|
||||
temporary_path = Path(temporary.name)
|
||||
try:
|
||||
with service.open("rb") as source:
|
||||
shutil.copyfileobj(source, temporary)
|
||||
except BaseException:
|
||||
temporary_path.unlink(missing_ok=True)
|
||||
raise
|
||||
os.replace(temporary_path, target / "Launcher.exe")
|
||||
answer = (ask_app or ask)(Path(launch_dir) / "stores", {"op": "rockstar-launch", "store": "rockstar", "id": str(sidecar.get("id"))}, timeout=120.0)
|
||||
if not isinstance(answer, dict) or not answer.get("ok"):
|
||||
return "session-" + (str(answer.get("reason") or "refused") if isinstance(answer, dict) else "timeout")
|
||||
if legacy and (not isinstance(answer.get("material"), str) or not re.fullmatch(r"[0-9A-Fa-f]{640}", answer["material"])):
|
||||
return "invalid-material"
|
||||
expires = answer.get("expiresAt")
|
||||
if not isinstance(expires, int) or not time.time() * 1000 < expires <= time.time() * 1000 + 120_000:
|
||||
return "invalid-expiry"
|
||||
launch = {"folder": windows_path(str(root)), "exe": windows_path(str(exe)), "workdir": windows_path(str(workdir)),
|
||||
"args": args, "expiresAt": expires}
|
||||
if legacy:
|
||||
launch.update(pipe=answer.get("pipe"), material=answer["material"])
|
||||
if sdk:
|
||||
session = answer.get("session")
|
||||
if not isinstance(session, dict) or not isinstance(session.get("accountId"), str) \
|
||||
or not re.fullmatch(r"[1-9][0-9]{0,19}", session["accountId"]) or int(session["accountId"]) > 2**64 - 1 \
|
||||
or any(not isinstance(session.get(key), str) or not session[key] or "\0" in session[key]
|
||||
or len(session[key].encode("utf-8")) > limit for key, limit in (("identity", 512), ("ticket", 49152))):
|
||||
return "invalid-sdk-session"
|
||||
launch["session"] = {key: session[key] for key in ("accountId", "identity", "ticket")}
|
||||
if "titleId" in session:
|
||||
if not isinstance(session["titleId"], str) or not re.fullmatch(r"[a-z][a-z0-9_]{0,63}", session["titleId"]):
|
||||
return "invalid-sdk-title"
|
||||
launch["session"]["titleId"] = session["titleId"]
|
||||
if len(json.dumps(launch).encode("utf-8")) > 65536:
|
||||
return "handoff-too-large"
|
||||
with tempfile.NamedTemporaryFile(mode="w", prefix="launch-", suffix=".json", dir=target, delete=False) as temporary:
|
||||
temporary_path = Path(temporary.name)
|
||||
try:
|
||||
json.dump(launch, temporary)
|
||||
except BaseException:
|
||||
temporary_path.unlink(missing_ok=True)
|
||||
raise
|
||||
os.replace(temporary_path, config)
|
||||
return "ready"
|
||||
|
||||
|
||||
UBISOFT_CACHE = Path("/root/.cache/droiddeck/ubisoft")
|
||||
UBISOFT_LAUNCH = ".droiddeck-ubisoft.json"
|
||||
UBISOFT_SERVICE = Path("/usr/local/lib/droiddeck-ubisoft/droiddeck-upc.exe")
|
||||
UBISOFT_ASK_WAIT = 45.0
|
||||
UBISOFT_EXTRACT_WAIT = 300
|
||||
# Ubisoft's own game-side runtime from the Connect installer, unmodified: the API and aux DLLs the
|
||||
# game's loader opens from InstallDir, the starter it re-executes into and its imports, and the overlay
|
||||
# DLLs uplay_r1.dll loads even with the overlay off (its startup fails without them).
|
||||
UBISOFT_RUNTIME = ("uplay_r1.dll", "uplay_r164.dll", "uplay_aux_r1.dll", "uplay_aux_r164.dll",
|
||||
"uplay_r2.dll", "uplay_r264.dll", "ubiorbitapi_r2.dll", "ubiorbitapi_r264.dll",
|
||||
"UbisoftGameLauncher.exe", "UbisoftGameLauncher64.exe", "discord-rpc.x86.dll",
|
||||
"discord-rpc.x64.dll", "steam_api.dll", "steam_api64.dll", "overlay.dll", "overlay64.dll")
|
||||
UBISOFT_VIEWS = ("[Software\\\\Ubisoft\\\\Launcher%s]", "[Software\\\\Wow6432Node\\\\Ubisoft\\\\Launcher%s]")
|
||||
MIN_INSTALLER = 1 << 20
|
||||
|
||||
|
||||
def ubisoft_runtime_dir(compat_data):
|
||||
return Path(compat_data) / "pfx" / UBISOFT_DIR
|
||||
|
||||
|
||||
def ubisoft_runtime_ready(compat_data):
|
||||
folder = ubisoft_runtime_dir(compat_data)
|
||||
return all((folder / name).is_file() for name in UBISOFT_RUNTIME)
|
||||
|
||||
|
||||
def fetch_installer(url=UBISOFT_INSTALLER_URL, folder=UBISOFT_CACHE):
|
||||
target = folder / "UbisoftConnectInstaller.exe"
|
||||
if target.is_file() and target.stat().st_size >= MIN_INSTALLER:
|
||||
return target
|
||||
import urllib.request
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
part = target.with_name(target.name + ".part")
|
||||
request = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
|
||||
with urllib.request.urlopen(request, timeout=60) as reply, open(part, "wb") as out:
|
||||
shutil.copyfileobj(reply, out)
|
||||
if part.stat().st_size < MIN_INSTALLER:
|
||||
part.unlink()
|
||||
raise OSError("installer too small")
|
||||
os.replace(part, target)
|
||||
return target
|
||||
|
||||
|
||||
def install_runtime(compat_data, wait=UBISOFT_EXTRACT_WAIT):
|
||||
if ubisoft_runtime_ready(compat_data):
|
||||
return "present"
|
||||
if not (Path(compat_data) / "pfx").is_dir():
|
||||
return "no-prefix"
|
||||
try:
|
||||
installer = fetch_installer()
|
||||
except OSError as error:
|
||||
return "download-failed-" + type(error).__name__
|
||||
target = ubisoft_runtime_dir(compat_data)
|
||||
target.mkdir(parents=True, exist_ok=True)
|
||||
with tempfile.TemporaryDirectory(prefix="droiddeck-ubisoft-", dir=str(UBISOFT_CACHE)) as tmp:
|
||||
try:
|
||||
subprocess.run(["7z", "e", "-y", "-o" + tmp, str(installer)] + list(UBISOFT_RUNTIME), stdin=subprocess.DEVNULL,
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=wait)
|
||||
except subprocess.TimeoutExpired:
|
||||
return "extract-timeout"
|
||||
except OSError:
|
||||
return "no-7z"
|
||||
missing = [name for name in UBISOFT_RUNTIME if not (Path(tmp) / name).is_file()]
|
||||
if missing:
|
||||
return "extract-missing-" + ",".join(missing)
|
||||
for name in UBISOFT_RUNTIME:
|
||||
shutil.copyfile(Path(tmp) / name, target / name)
|
||||
if not ubisoft_runtime_ready(compat_data):
|
||||
return "copy-failed"
|
||||
try:
|
||||
installer.unlink()
|
||||
except OSError:
|
||||
pass
|
||||
return "installed"
|
||||
|
||||
|
||||
def ubisoft_product(sidecar):
|
||||
extra = sidecar.get("extra") if isinstance(sidecar.get("extra"), dict) else {}
|
||||
try:
|
||||
return int(str(extra.get("productId") or ""))
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def ubisoft_install_dir(folder):
|
||||
return windows_path(str(folder)).replace("\\", "/").rstrip("/") + "/"
|
||||
|
||||
|
||||
def register_ubisoft(folder, product, compat_data):
|
||||
reg = Path(compat_data) / "pfx" / "system.reg"
|
||||
if not reg.is_file():
|
||||
return "no-registry"
|
||||
if not wait_for_server(reg.parent):
|
||||
return "server-running"
|
||||
for view in UBISOFT_VIEWS:
|
||||
set_value(reg, view % "", "InstallDir", UBISOFT_WINDOWS_DIR)
|
||||
set_value(reg, view % ("\\\\Installs\\\\%d" % product), "InstallDir", ubisoft_install_dir(folder))
|
||||
return "registered %d" % product
|
||||
|
||||
|
||||
UBISOFT_HIVES = {"HKEY_LOCAL_MACHINE": "system.reg", "HKEY_CURRENT_USER": "user.reg"}
|
||||
|
||||
|
||||
def manifest_value(values, folder, language):
|
||||
"""The value Connect writes for a manifest registry entry: the install folder, the entry for the
|
||||
language being played, or the only entry there is."""
|
||||
entries = [v for v in values if isinstance(v, dict)]
|
||||
if not entries:
|
||||
return None
|
||||
names = [str(v.get("name") or "") for v in entries]
|
||||
if "$INSTALL_DIR" in names:
|
||||
return windows_path(str(folder)).rstrip("\\") + "\\"
|
||||
for v in entries:
|
||||
if str(v.get("value") or "").lower() == language.lower():
|
||||
return str(v.get("value"))
|
||||
prefix = language.split("-")[0].lower()
|
||||
for v in entries:
|
||||
if str(v.get("value") or "").lower().split("-")[0] == prefix:
|
||||
return str(v.get("value"))
|
||||
return str(entries[0].get("value") or entries[0].get("name") or "")
|
||||
|
||||
|
||||
def apply_manifest_registry(compat_data, registry, folder, language):
|
||||
written = 0
|
||||
for item in registry or []:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
path = str(item.get("key") or "").replace("/", "\\")
|
||||
hive, _, rest = path.partition("\\")
|
||||
target = UBISOFT_HIVES.get(hive.upper())
|
||||
if not target or "\\" not in rest:
|
||||
continue
|
||||
subkey, _, name = rest.rpartition("\\")
|
||||
value = manifest_value(item.get("values"), folder, language)
|
||||
if value is None or not name:
|
||||
continue
|
||||
reg = Path(compat_data) / "pfx" / target
|
||||
parts = subkey.split("\\")
|
||||
views = [parts]
|
||||
if target == "system.reg" and len(parts) > 1 and parts[0].lower() == "software" and parts[1].lower() != "wow6432node":
|
||||
views.append(parts[:1] + ["Wow6432Node"] + parts[1:])
|
||||
for view in views:
|
||||
key = "[" + "\\\\".join(view) + "]"
|
||||
if set_value(reg, key, name, value):
|
||||
written += 1
|
||||
return written
|
||||
|
||||
|
||||
def ubisoft_launch(folder, sidecar, product, compat_data, answer):
|
||||
"""The launch file droiddeck-upc.exe reads: the game, the account and this launch's ticket."""
|
||||
account = answer.get("account") if isinstance(answer.get("account"), dict) else {}
|
||||
user = str(account.get("userId") or "user")
|
||||
saves = Path(compat_data) / "pfx" / "drive_c/users/steamuser/Saved Games/Ubisoft" / user / str(product)
|
||||
saves.mkdir(parents=True, exist_ok=True)
|
||||
exe = str(sidecar.get("exe") or "").replace("\\", "/")
|
||||
workdir = folder / str(sidecar.get("workdir")) if sidecar.get("workdir") else folder
|
||||
UBISOFT_CACHE.mkdir(parents=True, exist_ok=True)
|
||||
return {
|
||||
"productId": product,
|
||||
"exe": windows_path(str(folder / exe)),
|
||||
"args": " ".join(str(a) for a in sidecar.get("args") or []),
|
||||
"workdir": windows_path(str(workdir)),
|
||||
"account": account,
|
||||
"ticket": str(answer.get("ticket") or ""),
|
||||
"owned": [int(i) for i in answer.get("owned") or [] if str(i).isdigit()],
|
||||
"products": [p for p in answer.get("products") or [] if isinstance(p, dict) and isinstance(p.get("id"), int)],
|
||||
"appId": str(answer.get("appId") or ""),
|
||||
"chunks": [c for c in answer.get("chunks") or [] if isinstance(c, dict) and isinstance(c.get("id"), int)],
|
||||
"saves": "C:/users/steamuser/Saved Games/Ubisoft/%s/%d/" % (user, product),
|
||||
"language": str(answer.get("language") or "en-US"),
|
||||
"bridge": windows_path(str(UBISOFT_CACHE / ("bridge-%d" % product))),
|
||||
"log": windows_path(str(UBISOFT_CACHE / ("upc-%d.log" % product))),
|
||||
}
|
||||
|
||||
|
||||
def prepare_ubisoft(folder, sidecar, compat_data, launch_dir, proton="", ask_app=None):
|
||||
launch = folder / UBISOFT_LAUNCH
|
||||
try:
|
||||
launch.unlink()
|
||||
except OSError:
|
||||
pass
|
||||
if not compat_data:
|
||||
return "no-prefix"
|
||||
product = ubisoft_product(sidecar)
|
||||
if product is None:
|
||||
return "no-product"
|
||||
if not UBISOFT_SERVICE.is_file():
|
||||
return "no-service"
|
||||
made = create_prefix(compat_data, proton) if not (Path(compat_data) / "pfx/user.reg").is_file() else "present"
|
||||
if made not in ("present", "created"):
|
||||
return "prefix " + made
|
||||
runtime = install_runtime(compat_data)
|
||||
if not ubisoft_runtime_ready(compat_data):
|
||||
return "runtime " + runtime
|
||||
registry = register_ubisoft(folder, product, compat_data)
|
||||
if not launch_dir:
|
||||
return "runtime %s, %s, no-channel" % (runtime, registry)
|
||||
answer = (ask_app or ask)(Path(launch_dir) / "stores", {"op": "ubisoft-launch", "store": "ubisoft", "id": str(sidecar.get("id"))},
|
||||
timeout=UBISOFT_ASK_WAIT)
|
||||
if not isinstance(answer, dict) or not answer.get("ok") or not answer.get("ticket"):
|
||||
reason = "timeout" if answer is None else str(answer.get("reason") or "refused")
|
||||
return "runtime %s, %s, session %s" % (runtime, registry, reason)
|
||||
written = 0
|
||||
if answer.get("registry") and wait_for_server(Path(compat_data) / "pfx"):
|
||||
written = apply_manifest_registry(compat_data, answer.get("registry"), folder, str(answer.get("language") or "en-US"))
|
||||
tmp = launch.with_name(launch.name + ".tmp")
|
||||
tmp.write_text(json.dumps(ubisoft_launch(folder, sidecar, product, compat_data, answer)))
|
||||
os.replace(tmp, launch)
|
||||
return "runtime %s, %s, %d manifest value(s), session ready" % (runtime, registry, written)
|
||||
|
||||
COMET_DIR = Path("/usr/local/lib/droiddeck-comet")
|
||||
|
||||
|
||||
@@ -613,6 +1076,344 @@ def apply_gog_registry(folder, compat_data, proton, timeout=90, installation=Non
|
||||
return "import-failed" + note
|
||||
|
||||
|
||||
EA_HARDWARE = ".droiddeck-ea-hw.json"
|
||||
EA_CLIENT = "C:\\Program Files (x86)\\Origin\\Origin.exe"
|
||||
EA_WMI = (
|
||||
("boardManufacturer", "Win32_BaseBoard", "Manufacturer"),
|
||||
("boardSerial", "Win32_BaseBoard", "SerialNumber"),
|
||||
("biosManufacturer", "Win32_BIOS", "Manufacturer"),
|
||||
("biosSerial", "Win32_BIOS", "SerialNumber"),
|
||||
("osInstallDate", "Win32_OperatingSystem", "InstallDate"),
|
||||
("osSerial", "Win32_OperatingSystem", "SerialNumber"),
|
||||
("gpuPnpId", "Win32_VideoController", "PNPDeviceID"),
|
||||
("cpuManufacturer", "Win32_Processor", "Manufacturer"),
|
||||
("processorId", "Win32_Processor", "ProcessorId"),
|
||||
("brand", "Win32_Processor", "Name"),
|
||||
)
|
||||
EA_ROOTS = {"HKLM": "HKEY_LOCAL_MACHINE", "HKCU": "HKEY_CURRENT_USER"}
|
||||
|
||||
|
||||
def decode_output(data):
|
||||
if data[:2] in (b"\xff\xfe", b"\xfe\xff") or (len(data) > 1 and data[1:2] == b"\x00"):
|
||||
return data.decode("utf-16", "replace")
|
||||
return data.decode("utf-8", "replace")
|
||||
|
||||
|
||||
def wmi_value(text):
|
||||
rows = [row.strip() for row in text.replace("\r", "\n").split("\n") if row.strip()]
|
||||
return rows[1] if len(rows) > 1 else ""
|
||||
|
||||
|
||||
CPUID_LEAVES = (0, 1, 0x80000002, 0x80000003, 0x80000004)
|
||||
|
||||
|
||||
def cpuid_exe():
|
||||
"""A 32-bit console program that writes the raw CPUID leaves an x86 game sees under the emulator."""
|
||||
import struct
|
||||
base, rva = 0x400000, 0x1000
|
||||
buf, written = base + rva + 0x180, base + rva + 0x1D0
|
||||
iat = base + rva + 0x138
|
||||
code = b"\x53\x57\xbf" + struct.pack("<I", buf)
|
||||
for leaf in CPUID_LEAVES:
|
||||
code += b"\xb8" + struct.pack("<I", leaf) + b"\x31\xc9\x0f\xa2\x89\x07\x89\x5f\x04\x89\x4f\x08\x89\x57\x0c\x83\xc7\x10"
|
||||
code += b"\x6a\xf5\xff\x15" + struct.pack("<I", iat)
|
||||
code += b"\x6a\x00\x68" + struct.pack("<I", written) + b"\x6a\x50\x68" + struct.pack("<I", buf) + b"\x50\xff\x15" + struct.pack("<I", iat + 4)
|
||||
code += b"\x6a\x00\xff\x15" + struct.pack("<I", iat + 8)
|
||||
section = bytearray(0x200)
|
||||
section[:len(code)] = code
|
||||
names = [(0x148, b"GetStdHandle"), (0x158, b"WriteFile"), (0x164, b"ExitProcess")]
|
||||
struct.pack_into("<IIIII", section, 0x100, rva + 0x128, 0, 0, rva + 0x172, rva + 0x138)
|
||||
for i, (at, name) in enumerate(names):
|
||||
struct.pack_into("<I", section, 0x128 + 4 * i, rva + at)
|
||||
struct.pack_into("<I", section, 0x138 + 4 * i, rva + at)
|
||||
section[at + 2:at + 2 + len(name)] = name
|
||||
section[0x172:0x172 + 12] = b"KERNEL32.dll"
|
||||
header = bytearray(0x200)
|
||||
header[0:2] = b"MZ"
|
||||
struct.pack_into("<I", header, 0x3C, 0x40)
|
||||
header[0x40:0x44] = b"PE\0\0"
|
||||
struct.pack_into("<HHIIIHH", header, 0x44, 0x14C, 1, 0, 0, 0, 0xE0, 0x0103)
|
||||
opt = struct.pack("<HBBIIIIIIIIIHHHHHHIIIIHHIIIIII", 0x10B, 0, 0, 0x200, 0, 0, rva, rva, rva, base, 0x1000, 0x200,
|
||||
4, 0, 0, 0, 4, 0, 0, 0x2000, 0x200, 0, 3, 0, 0x100000, 0x1000, 0x100000, 0x1000, 0, 16)
|
||||
dirs = bytearray(128)
|
||||
struct.pack_into("<II", dirs, 8, rva + 0x100, 40)
|
||||
struct.pack_into("<II", dirs, 96, rva + 0x138, 16)
|
||||
header[0x58:0x58 + 0xE0] = opt + bytes(dirs)
|
||||
header[0x138:0x138 + 40] = struct.pack("<8sIIIIIIHHI", b".text", 0x1000, rva, 0x200, 0x200, 0, 0, 0, 0, 0xE0000060)
|
||||
return bytes(header) + bytes(section)
|
||||
|
||||
|
||||
EA_STUB_DIR = "Program Files (x86)/Origin"
|
||||
EA_STUB = "C:\\Program Files (x86)\\Origin\\Origin.exe"
|
||||
|
||||
|
||||
def origin_stub_exe():
|
||||
"""A 32-bit program that only sleeps: the process older EA SDKs look for while a game runs."""
|
||||
import struct
|
||||
base, rva = 0x400000, 0x1000
|
||||
iat = base + rva + 0x138
|
||||
code = b"\x6a\xff\xff\x15" + struct.pack("<I", iat) + b"\xeb\xf6"
|
||||
section = bytearray(0x200)
|
||||
section[:len(code)] = code
|
||||
struct.pack_into("<IIIII", section, 0x100, rva + 0x128, 0, 0, rva + 0x172, rva + 0x138)
|
||||
struct.pack_into("<I", section, 0x128, rva + 0x148)
|
||||
struct.pack_into("<I", section, 0x138, rva + 0x148)
|
||||
section[0x14A:0x14A + 5] = b"Sleep"
|
||||
section[0x172:0x172 + 12] = b"KERNEL32.dll"
|
||||
header = bytearray(cpuid_exe()[:0x200])
|
||||
struct.pack_into("<H", header, 0x58 + 68, 2)
|
||||
return bytes(header) + bytes(section)
|
||||
|
||||
|
||||
def install_origin_stub(compat_data):
|
||||
if not compat_data:
|
||||
return "no-prefix"
|
||||
target = Path(compat_data) / "pfx/drive_c" / EA_STUB_DIR / "Origin.exe"
|
||||
data = origin_stub_exe()
|
||||
try:
|
||||
if target.is_file() and target.read_bytes() == data:
|
||||
return "ready"
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
target.write_bytes(data)
|
||||
return "written"
|
||||
except OSError:
|
||||
return "failed"
|
||||
|
||||
|
||||
def cpuid_values(raw):
|
||||
import struct
|
||||
if len(raw) < 80:
|
||||
return {}
|
||||
regs = [struct.unpack_from("<IIII", raw, 16 * i) for i in range(5)]
|
||||
vendor = struct.pack("<III", regs[0][1], regs[0][3], regs[0][2]).split(b"\0")[0].decode("latin-1")
|
||||
brand = b"".join(struct.pack("<IIII", *r) for r in regs[2:]).split(b"\0")[0].decode("latin-1")
|
||||
return {"cpuVendor": vendor, "cpuEax": "%08X" % regs[1][0], "cpuEdx": "%08X" % regs[1][3],
|
||||
"cpuEcx": "%08x" % regs[1][2], "cpuBrand": brand}
|
||||
|
||||
|
||||
EA_PROBE_FIELDS = ("boardManufacturer", "biosManufacturer", "osInstallDate", "osSerial", "volumeSerial", "hostname",
|
||||
"cpuVendor", "cpuEax", "cpuEdx", "cpuEcx")
|
||||
|
||||
|
||||
def probe_complete(values):
|
||||
return all(str(values.get(key) or "").strip() for key in EA_PROBE_FIELDS)
|
||||
|
||||
|
||||
def ea_hardware(compat_data, proton, timeout=240):
|
||||
pfx = Path(compat_data) / "pfx"
|
||||
cache = pfx / EA_HARDWARE
|
||||
try:
|
||||
cached = json.loads(cache.read_text())
|
||||
if isinstance(cached, dict) and probe_complete(cached):
|
||||
return cached, "cached"
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
if not proton or not Path(proton).is_file():
|
||||
return {}, "no-proton"
|
||||
values = {}
|
||||
with tempfile.TemporaryDirectory(prefix="droiddeck-ea-") as tmp:
|
||||
folder = Path(tmp)
|
||||
lines = ["@echo off"]
|
||||
for key, cls, prop in EA_WMI:
|
||||
lines.append('wmic path %s get %s > "%s"' % (cls, prop, windows_path(str(folder / (key + ".txt")))))
|
||||
lines.append('vol C: > "%s"' % windows_path(str(folder / "volume.txt")))
|
||||
lines.append('hostname > "%s"' % windows_path(str(folder / "hostname.txt")))
|
||||
(folder / "cpuid.exe").write_bytes(cpuid_exe())
|
||||
lines.append('"%s" > "%s"' % (windows_path(str(folder / "cpuid.exe")), windows_path(str(folder / "cpuid.bin"))))
|
||||
bat = folder / "probe.bat"
|
||||
bat.write_bytes(("\r\n".join(lines) + "\r\n").encode())
|
||||
try:
|
||||
subprocess.run([proton, "run", "cmd.exe", "/d", "/c", windows_path(str(bat))], stdin=subprocess.DEVNULL,
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=timeout)
|
||||
except subprocess.TimeoutExpired:
|
||||
return {}, "timeout"
|
||||
except OSError:
|
||||
return {}, "failed"
|
||||
for key, _, _ in EA_WMI:
|
||||
try:
|
||||
values[key] = wmi_value(decode_output((folder / (key + ".txt")).read_bytes()))
|
||||
except OSError:
|
||||
values[key] = ""
|
||||
try:
|
||||
found = re.search(r"([0-9A-Fa-f]{4})-([0-9A-Fa-f]{4})", decode_output((folder / "volume.txt").read_bytes()))
|
||||
values["volumeSerial"] = (found.group(1) + found.group(2)).lower() if found else ""
|
||||
except OSError:
|
||||
values["volumeSerial"] = ""
|
||||
try:
|
||||
values["hostname"] = decode_output((folder / "hostname.txt").read_bytes()).strip().splitlines()[0].strip()
|
||||
except (OSError, IndexError):
|
||||
values["hostname"] = ""
|
||||
try:
|
||||
values.update(cpuid_values((folder / "cpuid.bin").read_bytes()))
|
||||
except OSError:
|
||||
pass
|
||||
if probe_complete(values):
|
||||
try:
|
||||
cache.write_text(json.dumps(values))
|
||||
except OSError:
|
||||
pass
|
||||
return values, "probed"
|
||||
return values, "incomplete"
|
||||
|
||||
|
||||
def ea_registry_entries(folder, sidecar):
|
||||
install = windows_path(str(folder)).rstrip("\\") + "\\"
|
||||
extra = sidecar.get("extra") if isinstance(sidecar.get("extra"), dict) else {}
|
||||
title = str(extra.get("displayName") or sidecar.get("title") or folder.name).strip()
|
||||
locale = str(extra.get("locale") or "en_US")
|
||||
entries = [
|
||||
("HKEY_LOCAL_MACHINE\\SOFTWARE\\Electronic Arts\\EA Desktop", "InstallSuccessful", "true"),
|
||||
("HKEY_LOCAL_MACHINE\\SOFTWARE\\Electronic Arts\\Origin", "InstallSuccessful", "true"),
|
||||
("HKEY_LOCAL_MACHINE\\SOFTWARE\\Electronic Arts\\Origin", "ClientPath", EA_CLIENT),
|
||||
("HKEY_LOCAL_MACHINE\\SOFTWARE\\Origin", "ClientPath", EA_CLIENT),
|
||||
]
|
||||
for content in [c.strip() for c in str(extra.get("contentIds") or extra.get("contentId") or "").split(",")]:
|
||||
if re.fullmatch(r"[A-Za-z0-9._-]{1,64}", content):
|
||||
key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Origin Games\\" + content
|
||||
entries += [(key, "DisplayName", title), (key, "Install Dir", install), (key, "Locale", locale)]
|
||||
try:
|
||||
keys = json.loads(extra.get("registry") or "[]")
|
||||
except ValueError:
|
||||
keys = []
|
||||
for pair in keys if isinstance(keys, list) else []:
|
||||
if not isinstance(pair, list) or len(pair) != 2:
|
||||
continue
|
||||
key, name = str(pair[0]), str(pair[1])
|
||||
root, _, rest = key.partition("\\")
|
||||
key = EA_ROOTS.get(root.upper(), root) + ("\\" + rest if rest else "")
|
||||
if key.upper().startswith(("HKEY_LOCAL_MACHINE\\", "HKEY_CURRENT_USER\\")) and name and "\n" not in key + name:
|
||||
entries.append((key, name, install))
|
||||
if title:
|
||||
entries.append((key, "DisplayName", title))
|
||||
entries.append((key, "Locale", locale))
|
||||
return entries
|
||||
|
||||
|
||||
def ea_registry(folder, sidecar, compat_data, proton, timeout=90):
|
||||
if not compat_data or not proton or not Path(proton).is_file():
|
||||
return "no-proton"
|
||||
pfx = Path(compat_data) / "pfx"
|
||||
text = "Windows Registry Editor Version 5.00\r\n\r\n" + "".join(
|
||||
'[%s]\r\n"%s"="%s"\r\n\r\n' % (key, reg_escape(name), reg_escape(value)) for key, name, value in ea_registry_entries(folder, sidecar))
|
||||
stamp = hashlib.sha256(text.encode("utf-8")).hexdigest()
|
||||
marker = pfx / (".droiddeck-ea-registry-" + re.sub(r"[^A-Za-z0-9._-]", "_", str(sidecar.get("id"))))
|
||||
try:
|
||||
if marker.is_file() and marker.read_text() == stamp and (pfx / "system.reg").is_file():
|
||||
return "ready"
|
||||
with tempfile.TemporaryDirectory(prefix="droiddeck-ea-") as tmp:
|
||||
source = Path(tmp) / "registry.reg"
|
||||
source.write_bytes(b"\xff\xfe" + text.encode("utf-16-le"))
|
||||
executable = pfx / "drive_c/windows/system32/reg.exe"
|
||||
for view in ("/reg:32", "/reg:64"):
|
||||
run = subprocess.run([proton, "run", str(executable) if executable.is_file() else "reg.exe", "import", windows_path(str(source)), view],
|
||||
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=timeout)
|
||||
if run.returncode:
|
||||
return "import-failed"
|
||||
marker.write_text(stamp)
|
||||
return "written"
|
||||
except subprocess.TimeoutExpired:
|
||||
return "timeout"
|
||||
except OSError:
|
||||
return "import-failed"
|
||||
|
||||
|
||||
def find_case(folder, relative):
|
||||
current = Path(folder)
|
||||
for part in [p for p in relative.replace("\\", "/").split("/") if p and p != "."]:
|
||||
exact = current / part
|
||||
if exact.exists():
|
||||
current = exact
|
||||
continue
|
||||
try:
|
||||
current = next(c for c in current.iterdir() if c.name.lower() == part.lower())
|
||||
except (OSError, StopIteration):
|
||||
return None
|
||||
return current if current != Path(folder) else None
|
||||
|
||||
|
||||
def touchup_args(raw, locale, install):
|
||||
raw = raw.replace("{locale}", locale)
|
||||
raw = raw.replace('"{installLocation}"', '"%s"' % install).replace("{installLocation}", install)
|
||||
try:
|
||||
return shlex.split(raw.replace("\\", "\\\\"))
|
||||
except ValueError:
|
||||
return raw.split()
|
||||
|
||||
|
||||
def ea_touchup(folder, sidecar, compat_data, proton, timeout=900):
|
||||
extra = sidecar.get("extra") if isinstance(sidecar.get("extra"), dict) else {}
|
||||
relative = str(extra.get("touchup") or "")
|
||||
if not relative:
|
||||
return "none"
|
||||
exe = find_case(folder, relative)
|
||||
if exe is None or not exe.is_file():
|
||||
return "missing"
|
||||
if not compat_data or not proton or not Path(proton).is_file():
|
||||
return "no-proton"
|
||||
pfx = Path(compat_data) / "pfx"
|
||||
stamp = str(sidecar.get("installVersion") or "") + " " + str(sidecar.get("installedAt") or "")
|
||||
marker = pfx / (".droiddeck-ea-touchup-" + re.sub(r"[^A-Za-z0-9._-]", "_", str(sidecar.get("id"))))
|
||||
try:
|
||||
if marker.is_file() and marker.read_text() == stamp:
|
||||
return "done"
|
||||
except OSError:
|
||||
pass
|
||||
args = touchup_args(str(extra.get("touchupArgs") or ""), str(extra.get("locale") or "en_US"), windows_path(str(folder)).rstrip("\\"))
|
||||
try:
|
||||
run = subprocess.run([proton, "run", str(exe)] + args, cwd=str(exe.parent), stdin=subprocess.DEVNULL,
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=timeout)
|
||||
result = "ok" if run.returncode == 0 else "exit-%d" % run.returncode
|
||||
except subprocess.TimeoutExpired:
|
||||
result = "timeout"
|
||||
except OSError:
|
||||
return "failed"
|
||||
try:
|
||||
marker.write_text(stamp)
|
||||
except OSError:
|
||||
pass
|
||||
return result
|
||||
|
||||
|
||||
def write_licenses(compat_data, licenses):
|
||||
if not compat_data:
|
||||
return 0
|
||||
root = Path(compat_data) / "pfx/drive_c"
|
||||
written = 0
|
||||
for item in licenses or []:
|
||||
relative = str(item.get("path") or "") if isinstance(item, dict) else ""
|
||||
if not relative or relative.startswith("/") or ".." in relative.split("/"):
|
||||
continue
|
||||
target = root / relative
|
||||
try:
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
target.write_bytes(base64.b64decode(item.get("data") or ""))
|
||||
written += 1
|
||||
except (OSError, binascii.Error):
|
||||
pass
|
||||
return written
|
||||
|
||||
|
||||
def prepare_ea(folder, sidecar, compat_data, launch_dir):
|
||||
game = str(sidecar.get("id"))
|
||||
proton = os.environ.get("BL_STORE_PROTON", "")
|
||||
made = create_prefix(compat_data, proton)
|
||||
ready = made in ("present", "created") or (compat_data and (Path(compat_data) / "pfx/user.reg").is_file())
|
||||
stub = install_origin_stub(compat_data) if ready else "no-prefix"
|
||||
hardware, probe = ea_hardware(compat_data, proton) if ready else ({}, "no-prefix")
|
||||
answer = None
|
||||
if launch_dir:
|
||||
answer = ask(Path(launch_dir) / "stores", {"op": "ea-prepare", "store": "ea", "id": game, "hardware": hardware}, timeout=40.0)
|
||||
sidecar = sidecar_at(Path(folder)) or sidecar
|
||||
log("ea %s prefix=%s stub=%s hardware=%s registry=%s touchup=%s" % (
|
||||
game, made, stub, probe, ea_registry(folder, sidecar, compat_data, proton), ea_touchup(folder, sidecar, compat_data, proton)))
|
||||
if answer is None:
|
||||
log("ea %s session=no reason=%s" % (game, "timeout" if launch_dir else "no-channel"))
|
||||
return
|
||||
written = write_licenses(compat_data, answer.get("licenses"))
|
||||
log("ea %s session=%s reason=%s lsx=%s licence=%s files=%d" % (
|
||||
game, "yes" if answer.get("ok") else "no", answer.get("reason", ""), answer.get("lsx"), answer.get("license", ""), written))
|
||||
|
||||
|
||||
def gog_support_actions(folder):
|
||||
found = []
|
||||
for script in sorted(Path(folder).glob(GOG_SCRIPTS)):
|
||||
@@ -771,6 +1572,28 @@ def main(argv):
|
||||
prepare_gog(sidecar, compat_data, launch_dir)
|
||||
log("gog registry: " + apply_gog_registry(folder, compat_data, os.environ.get("BL_STORE_PROTON", "")))
|
||||
log("gog support data: " + gog_support_data(folder, compat_data, os.environ.get("BL_STORE_PROTON", "")))
|
||||
if store == "rockstar":
|
||||
if game not in ROCKSTAR_TITLES and not rockstar_sdk(folder, sidecar):
|
||||
if compat_data:
|
||||
(Path(compat_data) / "pfx" / ROCKSTAR_FOLDER / "launch.json").unlink(missing_ok=True)
|
||||
return STORE_GAME
|
||||
try:
|
||||
state = prepare_rockstar(folder, sidecar, compat_data, launch_dir, os.environ.get("BL_STORE_PROTON", ""))
|
||||
except (OSError, ValueError, subprocess.TimeoutExpired):
|
||||
state = "preparation-failed"
|
||||
log("rockstar %s: %s" % (game, state))
|
||||
if state != "ready" and launch_dir:
|
||||
ask(Path(launch_dir) / "stores", {"op": "rockstar-exited", "store": "rockstar", "id": game}, timeout=1.0)
|
||||
return STORE_GAME if state == "ready" else RAN_FAILED
|
||||
if store == "ubisoft":
|
||||
try:
|
||||
log("ubisoft %s: %s" % (game, prepare_ubisoft(folder, sidecar, compat_data, launch_dir, os.environ.get("BL_STORE_PROTON", ""))))
|
||||
except OSError as e:
|
||||
try:
|
||||
(folder / UBISOFT_LAUNCH).unlink()
|
||||
except OSError:
|
||||
pass
|
||||
log("ubisoft %s: not prepared (%s), starting directly" % (game, e))
|
||||
if store == "epic":
|
||||
handled = os.environ.get(EPIC_HANDLED) == "1" and (folder / EPIC_ARGS).is_file()
|
||||
if not handled:
|
||||
@@ -808,6 +1631,9 @@ def main(argv):
|
||||
got = bool(answer.get("code"))
|
||||
log("epic launch id=%s code=%s reason=%s" % (game, "yes" if got else "no", answer.get("reason", "")))
|
||||
log("epic prefix=%s %s" % (prefix_id or "?", provision_prefix(compat_data)))
|
||||
if store == "ea":
|
||||
prepare_ea(folder, sidecar, compat_data, launch_dir)
|
||||
return STORE_GAME
|
||||
if not cloud_on(sidecar):
|
||||
if store in CLOUD_STORES:
|
||||
log("cloud %s %s down result=skipped reason=off" % (store, game))
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/python3
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@@ -75,8 +76,16 @@ def pe_has_arm64ec_metadata(path):
|
||||
return False
|
||||
|
||||
|
||||
# Only x64 code and members of the same runtime family import these, so an x64 copy loads in an
|
||||
# ARM64EC prefix, and games check for a newer version than Wine's builtins report.
|
||||
VC_RUNTIME = re.compile(r"(msvc[pr]\d.*|vcruntime\d.*|vcomp\d*|vcamp\d.*|vccorlib\d.*|concrt\d.*|mfcm?\d.*)\.dll",
|
||||
re.IGNORECASE)
|
||||
|
||||
|
||||
def machine_compatible(path, machines, machine=None):
|
||||
machine = pe_machine(path) if machine is None else machine
|
||||
if machine == 0x8664 and 0xA641 in machines and VC_RUNTIME.fullmatch(Path(path).name):
|
||||
return True
|
||||
if machine == 0x8664 and pe_has_arm64ec_metadata(path):
|
||||
return 0xA641 in machines
|
||||
return machine in machines
|
||||
|
||||
@@ -152,6 +152,13 @@ bl_components() {
|
||||
else
|
||||
cm_app=""
|
||||
fi
|
||||
if [ "$cm_app" = 2929927072 ] && [[ "${SteamAppId:-}" =~ ^[0-9]{1,10}$ ]] && (( 10#$SteamAppId > 0 && 10#$SteamAppId <= 4294967295 )); then
|
||||
cm_app=$((10#$SteamAppId))
|
||||
elif [ "$cm_app" = 2929927072 ] && [[ "${SteamGameId:-}" =~ ^[1-9][0-9]{9,19}$ ]] &&
|
||||
{ [ ${#SteamGameId} -lt 20 ] || [[ ! "$SteamGameId" > 18446744073709551615 ]]; } &&
|
||||
(( (10#$SteamGameId & 0xFFFFFFFF) == 0x02000000 && ((10#$SteamGameId >> 32) & 0xFFFFFFFF) != 0 )); then
|
||||
cm_app=$(( (10#$SteamGameId >> 32) & 0xFFFFFFFF ))
|
||||
fi
|
||||
if [ -z "$cm_app" ]; then
|
||||
echo "droiddeck-proton: components: invalid game prefix, left as it is" >&2
|
||||
return 0
|
||||
@@ -1533,7 +1540,7 @@ def register_default(config_path, apps=(), protect=(), default=None, catalog=Non
|
||||
auto = followers(state, current)
|
||||
known = {app for app in state.get("owned", ()) if isinstance(app, str)} if isinstance(state.get("owned"), list) else set()
|
||||
pinned = {app: current[app] for app in set(owned) - known
|
||||
if current.get(app) in {TOOL, previous} | gone and app not in auto}
|
||||
if app in current and current.get(app) in {TOOL, previous} | gone and app not in auto}
|
||||
if pinned and isinstance(auto, dict):
|
||||
state["auto"] = dict(auto, **pinned)
|
||||
state["owned"] = sorted(set(owned), key=app_order)
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
|
||||
typedef int (*spawn_fn)(pid_t *, const char *, const posix_spawn_file_actions_t *, const posix_spawnattr_t *,
|
||||
char *const[], char *const[]);
|
||||
__attribute__((visibility("hidden")))
|
||||
char *const *bl_webhelper_argv(const char *path, char *const argv[], char *const envp[]);
|
||||
|
||||
#if defined(__aarch64__) || defined(BINFMT_TEST)
|
||||
|
||||
@@ -141,21 +143,26 @@ __attribute__((visibility("hidden"))) int bl_binfmt_spawn(pid_t *pid, const char
|
||||
}
|
||||
|
||||
int execvpe(const char *file, char *const argv[], char *const envp[]) {
|
||||
char resolved[PATH_MAX];
|
||||
const char *path = lookup(file, resolved, sizeof resolved) ? resolved : file;
|
||||
char *const *adjusted = bl_webhelper_argv(path, argv, envp);
|
||||
static int (*real)(const char *, char *const[], char *const[]);
|
||||
if (!real) real = (int (*)(const char *, char *const[], char *const[]))dlsym(RTLD_NEXT, "execvpe");
|
||||
bl_binfmt_before_search(file, argv, envp);
|
||||
real(file, argv, envp);
|
||||
return bl_binfmt_after_search(file, argv, envp, errno);
|
||||
bl_binfmt_before_search(file, (char *const *)adjusted, envp);
|
||||
real(file, (char *const *)adjusted, envp);
|
||||
return bl_binfmt_after_search(file, (char *const *)adjusted, envp, errno);
|
||||
}
|
||||
|
||||
int posix_spawnp(pid_t *pid, const char *file, const posix_spawn_file_actions_t *fa, const posix_spawnattr_t *attr,
|
||||
char *const argv[], char *const envp[]) {
|
||||
char path[PATH_MAX];
|
||||
int found = lookup(file, path, sizeof path);
|
||||
char *const *adjusted = bl_webhelper_argv(found ? path : file, argv, envp);
|
||||
static spawn_fn real, spawn;
|
||||
if (!real) real = (spawn_fn)dlsym(RTLD_NEXT, "posix_spawnp");
|
||||
if (!spawn) spawn = (spawn_fn)dlsym(RTLD_NEXT, "posix_spawn");
|
||||
char path[PATH_MAX];
|
||||
if (!handing_off() || !lookup(file, path, sizeof path)) return real(pid, file, fa, attr, argv, envp);
|
||||
return bl_binfmt_spawn(pid, path, fa, attr, argv, envp, spawn);
|
||||
if (!handing_off() || !found) return real(pid, file, fa, attr, (char *const *)adjusted, envp);
|
||||
return bl_binfmt_spawn(pid, path, fa, attr, (char *const *)adjusted, envp, spawn);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
@@ -81,7 +81,6 @@ static void cp_init(void) {
|
||||
struct stat st;
|
||||
void *map;
|
||||
int fd;
|
||||
if (strcmp(program_invocation_short_name, "plasmashell") != 0) return;
|
||||
if (!cp_find_cond_bit()) return;
|
||||
fd = open(CP_CTL, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return;
|
||||
|
||||
@@ -0,0 +1,291 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define DESCRIPTOR "user.wine.sd"
|
||||
#define STORE "/.droiddeck-descriptors"
|
||||
#define ENTRY_MAX (PATH_MAX * 2)
|
||||
|
||||
int bl_is_wineserver(void) __attribute__((visibility("hidden")));
|
||||
|
||||
static int store_seen;
|
||||
|
||||
static int kept(const char *name) {
|
||||
return name != NULL && strcmp(name, DESCRIPTOR) == 0 && bl_is_wineserver();
|
||||
}
|
||||
|
||||
static int store_root(char *out, size_t size) {
|
||||
const char *prefix = getenv("WINEPREFIX");
|
||||
int n;
|
||||
|
||||
if (prefix == NULL || prefix[0] != '/') return -1;
|
||||
n = snprintf(out, size, "%s" STORE, prefix);
|
||||
return n > 0 && (size_t)n < size ? n : -1;
|
||||
}
|
||||
|
||||
static int store_exists(void) {
|
||||
char root[PATH_MAX];
|
||||
|
||||
if (store_seen) return 1;
|
||||
if (store_root(root, sizeof(root)) < 0) return 0;
|
||||
store_seen = access(root, F_OK) == 0;
|
||||
return store_seen;
|
||||
}
|
||||
|
||||
static int entry_for(const char *path, char *out, size_t size, const char *suffix, int create) {
|
||||
int used = store_root(out, size);
|
||||
const char *part = path, *end;
|
||||
|
||||
if (used < 0 || path[0] != '/') return -1;
|
||||
if (create && mkdir(out, 0700) != 0 && errno != EEXIST) return -1;
|
||||
while (*part == '/') part++;
|
||||
if (*part == '\0') return -1;
|
||||
for (;;) {
|
||||
size_t length;
|
||||
const char *tail;
|
||||
|
||||
end = strchr(part, '/');
|
||||
length = end ? (size_t)(end - part) : strlen(part);
|
||||
tail = end ? ".d" : suffix;
|
||||
if (length + strlen(tail) > NAME_MAX || used + length + strlen(tail) + 2 > size) return -1;
|
||||
out[used++] = '/';
|
||||
memcpy(out + used, part, length);
|
||||
used += (int)length;
|
||||
strcpy(out + used, tail);
|
||||
used += (int)strlen(tail);
|
||||
if (!end) break;
|
||||
if (create && mkdir(out, 0700) != 0 && errno != EEXIST) return -1;
|
||||
part = end + 1;
|
||||
}
|
||||
if (create) store_seen = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int path_of(int fd, char *out, size_t size) {
|
||||
char link[40];
|
||||
ssize_t n;
|
||||
|
||||
snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
|
||||
n = readlink(link, out, size - 1);
|
||||
if (n <= 0 || out[0] != '/') return -1;
|
||||
out[n] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int resolved(const char *path, char *out, size_t size) {
|
||||
int fd, result;
|
||||
|
||||
if (path == NULL) return -1;
|
||||
fd = open(path, O_PATH | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (fd < 0) return -1;
|
||||
result = path_of(fd, out, size);
|
||||
close(fd);
|
||||
return result;
|
||||
}
|
||||
|
||||
static int entry_of(int fd, char *out, size_t size, int create) {
|
||||
char path[PATH_MAX];
|
||||
|
||||
if (path_of(fd, path, sizeof(path)) != 0) return -1;
|
||||
return entry_for(path, out, size, ".sd", create);
|
||||
}
|
||||
|
||||
static ssize_t stored_read(int fd, void *value, size_t size) {
|
||||
char entry[ENTRY_MAX];
|
||||
struct stat st;
|
||||
ssize_t got = 0;
|
||||
int file;
|
||||
|
||||
if (entry_of(fd, entry, sizeof(entry), 0) != 0) return -2;
|
||||
file = open(entry, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (file < 0) {
|
||||
if (errno != ENOENT) return -2;
|
||||
errno = ENODATA;
|
||||
return -1;
|
||||
}
|
||||
if (fstat(file, &st) != 0) {
|
||||
close(file);
|
||||
return -2;
|
||||
}
|
||||
if (size == 0) {
|
||||
close(file);
|
||||
return (ssize_t)st.st_size;
|
||||
}
|
||||
if ((size_t)st.st_size > size) {
|
||||
close(file);
|
||||
errno = ERANGE;
|
||||
return -1;
|
||||
}
|
||||
while (got < st.st_size) {
|
||||
ssize_t n = read(file, (char *)value + got, (size_t)(st.st_size - got));
|
||||
if (n <= 0) {
|
||||
close(file);
|
||||
return -2;
|
||||
}
|
||||
got += n;
|
||||
}
|
||||
close(file);
|
||||
return got;
|
||||
}
|
||||
|
||||
static int stored_write(int fd, const void *value, size_t size) {
|
||||
char entry[ENTRY_MAX], fresh[ENTRY_MAX + 8];
|
||||
size_t put = 0;
|
||||
int file;
|
||||
|
||||
if (entry_of(fd, entry, sizeof(entry), 1) != 0) return -1;
|
||||
snprintf(fresh, sizeof(fresh), "%s.new", entry);
|
||||
file = open(fresh, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, 0600);
|
||||
if (file < 0) return -1;
|
||||
while (put < size) {
|
||||
ssize_t n = write(file, (const char *)value + put, size - put);
|
||||
if (n <= 0) {
|
||||
close(file);
|
||||
unlinkat(AT_FDCWD, fresh, 0);
|
||||
return -1;
|
||||
}
|
||||
put += (size_t)n;
|
||||
}
|
||||
if (close(file) != 0 || renameat(AT_FDCWD, fresh, AT_FDCWD, entry) != 0) {
|
||||
unlinkat(AT_FDCWD, fresh, 0);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t fgetxattr(int fd, const char *name, void *value, size_t size) {
|
||||
static ssize_t (*next)(int, const char *, void *, size_t);
|
||||
ssize_t result;
|
||||
|
||||
if (next == NULL) next = dlsym(RTLD_NEXT, "fgetxattr");
|
||||
if (next == NULL) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
result = next(fd, name, value, size);
|
||||
if (result >= 0 || errno != ENOTSUP || !kept(name)) return result;
|
||||
result = stored_read(fd, value, size);
|
||||
if (result == -2) {
|
||||
errno = ENOTSUP;
|
||||
return -1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int fsetxattr(int fd, const char *name, const void *value, size_t size, int flags) {
|
||||
static int (*next)(int, const char *, const void *, size_t, int);
|
||||
int saved = errno, result;
|
||||
|
||||
if (next == NULL) next = dlsym(RTLD_NEXT, "fsetxattr");
|
||||
if (next == NULL) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
result = next(fd, name, value, size, flags);
|
||||
if (result == 0 || errno != ENOTSUP || !kept(name)) return result;
|
||||
if (stored_write(fd, value, size) != 0) {
|
||||
errno = ENOTSUP;
|
||||
return -1;
|
||||
}
|
||||
errno = saved;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fremovexattr(int fd, const char *name) {
|
||||
static int (*next)(int, const char *);
|
||||
char entry[ENTRY_MAX];
|
||||
int saved = errno, result;
|
||||
|
||||
if (next == NULL) next = dlsym(RTLD_NEXT, "fremovexattr");
|
||||
if (next == NULL) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
result = next(fd, name);
|
||||
if (result == 0 || errno != ENOTSUP || !kept(name)) return result;
|
||||
if (entry_of(fd, entry, sizeof(entry), 0) != 0) {
|
||||
errno = ENOTSUP;
|
||||
return -1;
|
||||
}
|
||||
if (unlinkat(AT_FDCWD, entry, 0) != 0) {
|
||||
errno = errno == ENOENT ? ENODATA : ENOTSUP;
|
||||
return -1;
|
||||
}
|
||||
errno = saved;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tracked(const char *path, char *out, size_t size) {
|
||||
int saved = errno, found;
|
||||
|
||||
found = bl_is_wineserver() && store_exists() && resolved(path, out, size) == 0;
|
||||
errno = saved;
|
||||
return found;
|
||||
}
|
||||
|
||||
static void forget(const char *path, int folder) {
|
||||
char entry[ENTRY_MAX];
|
||||
int saved = errno;
|
||||
|
||||
if (entry_for(path, entry, sizeof(entry), ".sd", 0) == 0) unlinkat(AT_FDCWD, entry, 0);
|
||||
if (folder && entry_for(path, entry, sizeof(entry), ".d", 0) == 0) unlinkat(AT_FDCWD, entry, AT_REMOVEDIR);
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
static void follow(const char *from, const char *to) {
|
||||
static const char *const kinds[] = {".sd", ".d"};
|
||||
char old[ENTRY_MAX], new[ENTRY_MAX];
|
||||
int saved = errno;
|
||||
|
||||
for (size_t i = 0; i < 2; i++) {
|
||||
if (entry_for(from, old, sizeof(old), kinds[i], 0) != 0) continue;
|
||||
if (access(old, F_OK) != 0) {
|
||||
if (i == 0 && entry_for(to, new, sizeof(new), kinds[i], 0) == 0) unlinkat(AT_FDCWD, new, 0);
|
||||
continue;
|
||||
}
|
||||
if (entry_for(to, new, sizeof(new), kinds[i], 1) != 0 || renameat(AT_FDCWD, old, AT_FDCWD, new) != 0) {
|
||||
if (i == 0) unlinkat(AT_FDCWD, old, 0);
|
||||
}
|
||||
}
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden")))
|
||||
int bl_descriptor_unlink(const char *path, int (*real)(const char *)) {
|
||||
char target[PATH_MAX];
|
||||
int known = tracked(path, target, sizeof(target)), result = real(path);
|
||||
|
||||
if (result == 0 && known) forget(target, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden")))
|
||||
int bl_descriptor_rmdir(const char *path, int (*real)(const char *)) {
|
||||
char target[PATH_MAX];
|
||||
int known = tracked(path, target, sizeof(target)), result = real(path);
|
||||
|
||||
if (result == 0 && known) forget(target, 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden")))
|
||||
int bl_descriptor_remove(const char *path, int (*real)(const char *)) {
|
||||
return bl_descriptor_rmdir(path, real);
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden")))
|
||||
int bl_descriptor_rename(const char *from, const char *to, int (*real)(const char *, const char *)) {
|
||||
char old[PATH_MAX], new[PATH_MAX];
|
||||
int known = tracked(from, old, sizeof(old)), result = real(from, to);
|
||||
|
||||
if (result == 0 && known && tracked(to, new, sizeof(new))) follow(old, new);
|
||||
return result;
|
||||
}
|
||||
@@ -83,6 +83,19 @@ static int notify = -1;
|
||||
static int forked;
|
||||
static struct { dev_t dev; int fuse; } devs[8];
|
||||
static int ndevs;
|
||||
// Libraries preloaded after this one can call back in while a lock is held (the native userland
|
||||
// hook's inotify_add_watch calls mkdir() by name); such calls go straight to the next library.
|
||||
static __thread int holding;
|
||||
|
||||
static void take(pthread_mutex_t *m) {
|
||||
pthread_mutex_lock(m);
|
||||
holding++;
|
||||
}
|
||||
|
||||
static void give(pthread_mutex_t *m) {
|
||||
holding--;
|
||||
pthread_mutex_unlock(m);
|
||||
}
|
||||
|
||||
static long long now_ns(void) {
|
||||
struct timespec t;
|
||||
@@ -109,32 +122,32 @@ static int enabled(void) {
|
||||
}
|
||||
|
||||
static int eligible(const char *path) {
|
||||
if (path == NULL || path[0] != '/' || !enabled()) return 0;
|
||||
if (holding || path == NULL || path[0] != '/' || !enabled()) return 0;
|
||||
const char *s = strstr(path, "/steamapps");
|
||||
return s && (s[10] == '/' || s[10] == 0);
|
||||
}
|
||||
|
||||
static int on_fuse(dev_t dev, const char *path) {
|
||||
pthread_mutex_lock(&lock);
|
||||
take(&lock);
|
||||
for (int i = 0; i < ndevs; i++) {
|
||||
if (devs[i].dev == dev) {
|
||||
int fuse = devs[i].fuse;
|
||||
pthread_mutex_unlock(&lock);
|
||||
give(&lock);
|
||||
return fuse;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
give(&lock);
|
||||
struct statfs fs;
|
||||
int saved = errno;
|
||||
int fuse = statfs(path, &fs) == 0 && (unsigned long)fs.f_type == FUSE_MAGIC;
|
||||
errno = saved;
|
||||
pthread_mutex_lock(&lock);
|
||||
take(&lock);
|
||||
if (ndevs < 8) {
|
||||
devs[ndevs].dev = dev;
|
||||
devs[ndevs].fuse = fuse;
|
||||
ndevs++;
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
give(&lock);
|
||||
return fuse;
|
||||
}
|
||||
|
||||
@@ -404,7 +417,7 @@ __attribute__((constructor)) static void resolve(void) {
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
static DIR *hit(const struct stat *st, struct handle *h, long long now) {
|
||||
pthread_mutex_lock(&lock);
|
||||
take(&lock);
|
||||
if (!changing) pump(NULL, 0);
|
||||
int slot = find(st->st_dev, st->st_ino);
|
||||
if (slot >= 0 && same(table[slot], st) && now - table[slot]->filled < DIR_TTL_NS && records(table[slot]) == 0) {
|
||||
@@ -413,7 +426,7 @@ static DIR *hit(const struct stat *st, struct handle *h, long long now) {
|
||||
h->snap->used = now;
|
||||
DIR *d = wrap(h);
|
||||
if (d) {
|
||||
pthread_mutex_unlock(&lock);
|
||||
give(&lock);
|
||||
return d;
|
||||
}
|
||||
unref(h->snap);
|
||||
@@ -421,7 +434,7 @@ static DIR *hit(const struct stat *st, struct handle *h, long long now) {
|
||||
} else if (slot >= 0) {
|
||||
drop(slot);
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
give(&lock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -462,11 +475,11 @@ static int above(struct handle *h, const char *path) {
|
||||
}
|
||||
|
||||
static int track(struct handle *h, const char *path) {
|
||||
pthread_mutex_lock(&lock);
|
||||
take(&lock);
|
||||
h->wd = watch(path, WATCHED);
|
||||
DIR *d = h->wd >= 0 && above(h, path) ? wrap(h) : NULL;
|
||||
if (d == NULL) let_go(h->wd, h->up, h->nup);
|
||||
pthread_mutex_unlock(&lock);
|
||||
give(&lock);
|
||||
return d != NULL;
|
||||
}
|
||||
|
||||
@@ -486,11 +499,11 @@ static void filling(struct handle *h, DIR *real, const struct stat *st, long lon
|
||||
}
|
||||
|
||||
static void untrack(struct handle *h) {
|
||||
pthread_mutex_lock(&lock);
|
||||
take(&lock);
|
||||
int i = registered((DIR *)h);
|
||||
if (i >= 0) __atomic_store_n(&handles[i], NULL, __ATOMIC_RELEASE);
|
||||
let_go(h->wd, h->up, h->nup);
|
||||
pthread_mutex_unlock(&lock);
|
||||
give(&lock);
|
||||
}
|
||||
|
||||
DIR *opendir(const char *path) {
|
||||
@@ -553,11 +566,11 @@ static void finish(struct handle *h) {
|
||||
h->fill = NULL;
|
||||
int current = stat(h->path, &st) == 0 && same(l, &st);
|
||||
if (current) l->folds = folds(h->path, &st);
|
||||
pthread_mutex_lock(&lock);
|
||||
take(&lock);
|
||||
if (!changing) pump(NULL, 0);
|
||||
if (current && !__atomic_load_n(&h->dirty, __ATOMIC_RELAXED)) install(l);
|
||||
else destroy(l);
|
||||
pthread_mutex_unlock(&lock);
|
||||
give(&lock);
|
||||
}
|
||||
|
||||
static struct dirent64 *entry(struct handle *h) {
|
||||
@@ -624,7 +637,8 @@ int readdir_r(DIR *d, struct dirent *out, struct dirent **result) {
|
||||
|
||||
int closedir(DIR *d) {
|
||||
NEXT(closedir);
|
||||
pthread_mutex_lock(&lock);
|
||||
if (holding && registered(d) < 0) return next_closedir(d);
|
||||
take(&lock);
|
||||
int i = registered(d);
|
||||
struct handle *h = i >= 0 ? handles[i] : NULL;
|
||||
if (h) {
|
||||
@@ -632,7 +646,7 @@ int closedir(DIR *d) {
|
||||
if (h->snap) unref(h->snap);
|
||||
let_go(h->wd, h->up, h->nup);
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
give(&lock);
|
||||
if (h == NULL) return next_closedir(d);
|
||||
int r = 0;
|
||||
if (h->real) r = next_closedir(h->real);
|
||||
@@ -704,10 +718,10 @@ static int watching(const char *path) {
|
||||
}
|
||||
|
||||
static void begin(void) {
|
||||
pthread_mutex_lock(&lock);
|
||||
take(&lock);
|
||||
pump(NULL, 0);
|
||||
changing = 1;
|
||||
pthread_mutex_unlock(&lock);
|
||||
give(&lock);
|
||||
}
|
||||
|
||||
static void before(struct side *s) {
|
||||
@@ -796,10 +810,10 @@ static int patch(struct side *s, int what, uint32_t event, int ok, int error, co
|
||||
}
|
||||
|
||||
static void end(struct expect *x, int nx) {
|
||||
pthread_mutex_lock(&lock);
|
||||
take(&lock);
|
||||
pump(x, nx);
|
||||
changing = 0;
|
||||
pthread_mutex_unlock(&lock);
|
||||
give(&lock);
|
||||
}
|
||||
|
||||
int bl_dircache_create(int dirfd, const char *path, int flags, mode_t mode,
|
||||
@@ -809,17 +823,17 @@ int bl_dircache_create(int dirfd, const char *path, int flags, mode_t mode,
|
||||
struct side s;
|
||||
if (!(flags & O_CREAT) || (dirfd != AT_FDCWD && (path == NULL || path[0] != '/')) || !watching(path) || !split(path, &s))
|
||||
return real(dirfd, path, flags, mode);
|
||||
pthread_mutex_lock(&oplock);
|
||||
take(&oplock);
|
||||
begin();
|
||||
before(&s);
|
||||
int fd = real(dirfd, path, flags, mode);
|
||||
int saved = errno;
|
||||
struct expect x;
|
||||
pthread_mutex_lock(&lock);
|
||||
take(&lock);
|
||||
int nx = patch(&s, ADDED, IN_CREATE, fd >= 0, saved, path, &x);
|
||||
pthread_mutex_unlock(&lock);
|
||||
give(&lock);
|
||||
end(&x, nx);
|
||||
pthread_mutex_unlock(&oplock);
|
||||
give(&oplock);
|
||||
errno = saved;
|
||||
return fd;
|
||||
}
|
||||
@@ -829,26 +843,34 @@ int bl_dircache_create(int dirfd, const char *path, int flags, mode_t mode,
|
||||
NEXT(name); \
|
||||
struct side s; \
|
||||
if (!watching(path) || !split(path, &s)) return call; \
|
||||
pthread_mutex_lock(&oplock); \
|
||||
take(&oplock); \
|
||||
begin(); \
|
||||
before(&s); \
|
||||
int r = call; \
|
||||
int saved = errno; \
|
||||
struct expect x; \
|
||||
pthread_mutex_lock(&lock); \
|
||||
take(&lock); \
|
||||
int nx = patch(&s, what, event, r == 0, saved, what == ADDED ? path : NULL, &x); \
|
||||
if (r == 0 && what == REMOVED) forget_below(path); \
|
||||
pthread_mutex_unlock(&lock); \
|
||||
give(&lock); \
|
||||
end(&x, nx); \
|
||||
pthread_mutex_unlock(&oplock); \
|
||||
give(&oplock); \
|
||||
errno = saved; \
|
||||
return r; \
|
||||
}
|
||||
|
||||
ONE_PATH(mkdir, ADDED, IN_CREATE, next_mkdir(path, mode), mode_t mode)
|
||||
ONE_PATH(rmdir, REMOVED, IN_DELETE, next_rmdir(path))
|
||||
ONE_PATH(unlink, REMOVED, IN_DELETE, next_unlink(path))
|
||||
ONE_PATH(remove, REMOVED, IN_DELETE, next_remove(path))
|
||||
int bl_descriptor_unlink(const char *path, int (*real)(const char *)) __attribute__((visibility("hidden")));
|
||||
int bl_descriptor_rmdir(const char *path, int (*real)(const char *)) __attribute__((visibility("hidden")));
|
||||
int bl_descriptor_remove(const char *path, int (*real)(const char *)) __attribute__((visibility("hidden")));
|
||||
int bl_descriptor_rename(const char *from, const char *to, int (*real)(const char *, const char *))
|
||||
__attribute__((visibility("hidden")));
|
||||
|
||||
static int moved(const char *from, const char *to) { return bl_descriptor_rename(from, to, next_rename); }
|
||||
|
||||
ONE_PATH(rmdir, REMOVED, IN_DELETE, bl_descriptor_rmdir(path, next_rmdir))
|
||||
ONE_PATH(unlink, REMOVED, IN_DELETE, bl_descriptor_unlink(path, next_unlink))
|
||||
ONE_PATH(remove, REMOVED, IN_DELETE, bl_descriptor_remove(path, next_remove))
|
||||
|
||||
#define TWO_PATHS(name, gone, event, call) \
|
||||
int name(const char *from, const char *to) { \
|
||||
@@ -856,7 +878,7 @@ ONE_PATH(remove, REMOVED, IN_DELETE, next_remove(path))
|
||||
struct side a, b; \
|
||||
int track_a = gone && watching(from) && split(from, &a), track_b = watching(to) && split(to, &b); \
|
||||
if (!track_a && !track_b) return call(from, to); \
|
||||
pthread_mutex_lock(&oplock); \
|
||||
take(&oplock); \
|
||||
begin(); \
|
||||
if (track_a) before(&a); \
|
||||
if (track_b) before(&b); \
|
||||
@@ -864,16 +886,16 @@ ONE_PATH(remove, REMOVED, IN_DELETE, next_remove(path))
|
||||
int saved = errno; \
|
||||
struct expect x[2]; \
|
||||
int nx = 0; \
|
||||
pthread_mutex_lock(&lock); \
|
||||
take(&lock); \
|
||||
if (track_a) nx += patch(&a, REMOVED, IN_MOVED_FROM, r == 0, saved, NULL, &x[nx]); \
|
||||
if (track_b) { \
|
||||
if (track_a && strcmp(a.dir, b.dir) == 0) b.known = stat(b.dir, &b.before) == 0; \
|
||||
nx += patch(&b, ADDED, event, r == 0, saved, to, &x[nx]); \
|
||||
} \
|
||||
if (r == 0 && gone) { forget_below(from); forget_below(to); } \
|
||||
pthread_mutex_unlock(&lock); \
|
||||
give(&lock); \
|
||||
end(x, nx); \
|
||||
pthread_mutex_unlock(&oplock); \
|
||||
give(&oplock); \
|
||||
errno = saved; \
|
||||
return r; \
|
||||
}
|
||||
@@ -881,7 +903,7 @@ ONE_PATH(remove, REMOVED, IN_DELETE, next_remove(path))
|
||||
int bl_link_lock(const char *oldpath, const char *newpath, int result) __attribute__((visibility("hidden")));
|
||||
static int link_or_lock(const char *from, const char *to) { return bl_link_lock(from, to, next_link(from, to)); }
|
||||
|
||||
TWO_PATHS(rename, 1, IN_MOVED_TO, next_rename)
|
||||
TWO_PATHS(rename, 1, IN_MOVED_TO, moved)
|
||||
TWO_PATHS(link, 0, IN_CREATE, link_or_lock)
|
||||
TWO_PATHS(symlink, 0, IN_CREATE, next_symlink)
|
||||
|
||||
@@ -923,13 +945,13 @@ int bl_dircache_absent(const char *path) {
|
||||
for (const char *c = s.name; *c; c++)
|
||||
if ((unsigned char)*c >= 0x80) return 0;
|
||||
long long now = now_ns();
|
||||
pthread_mutex_lock(&lock);
|
||||
take(&lock);
|
||||
int absent = 0;
|
||||
if (!changing) {
|
||||
pump(NULL, 0);
|
||||
absent = listed_absent(s.dir, s.name, now);
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
give(&lock);
|
||||
return absent;
|
||||
}
|
||||
|
||||
|
||||
@@ -271,7 +271,8 @@ __attribute__((visibility("hidden"))) void bl_binfmt_before(const char *path, ch
|
||||
__attribute__((visibility("hidden"))) int bl_binfmt_retry(const char *path, char *const argv[], char *const envp[], int error);
|
||||
__attribute__((visibility("hidden"))) void bl_binfmt_before_search(const char *file, char *const argv[], char *const envp[]);
|
||||
__attribute__((visibility("hidden"))) int bl_binfmt_after_search(const char *file, char *const argv[], char *const envp[], int error);
|
||||
__attribute__((visibility("hidden"))) char *const *bl_webhelper_argv(const char *path, char *const argv[]);
|
||||
__attribute__((visibility("hidden")))
|
||||
char *const *bl_webhelper_argv(const char *path, char *const argv[], char *const envp[]);
|
||||
__attribute__((visibility("hidden"))) int bl_binfmt_spawn(pid_t *pid, const char *path, const posix_spawn_file_actions_t *fa,
|
||||
const posix_spawnattr_t *attr, char *const argv[], char *const envp[],
|
||||
spawn_fn real);
|
||||
@@ -341,7 +342,7 @@ static char **web_helper_redirect(const char *path, char *const argv[], const ch
|
||||
}
|
||||
|
||||
int execve(const char *path, char *const argv_in[], char *const envp[]) {
|
||||
char *const *argv = bl_webhelper_argv(path, argv_in);
|
||||
char *const *argv = bl_webhelper_argv(path, argv_in, envp);
|
||||
static int (*real)(const char *, char *const[], char *const[]);
|
||||
if (!real) real = (int (*)(const char *, char *const[], char *const[]))dlsym(RTLD_NEXT, "execve");
|
||||
maybe_answer_lsof(path, argv);
|
||||
@@ -372,7 +373,7 @@ static int answer_lsof_via_spawn(const char *path, char *const argv[], pid_t *pi
|
||||
}
|
||||
|
||||
int execvp(const char *file, char *const argv_in[]) {
|
||||
char *const *argv = bl_webhelper_argv(file, argv_in);
|
||||
char *const *argv = bl_webhelper_argv(file, argv_in, environ);
|
||||
static int (*real)(const char *, char *const[]);
|
||||
if (!real) real = (int (*)(const char *, char *const[]))dlsym(RTLD_NEXT, "execvp");
|
||||
maybe_answer_lsof(file, argv);
|
||||
@@ -382,7 +383,7 @@ int execvp(const char *file, char *const argv_in[]) {
|
||||
}
|
||||
|
||||
int execv(const char *path, char *const argv_in[]) {
|
||||
char *const *argv = bl_webhelper_argv(path, argv_in);
|
||||
char *const *argv = bl_webhelper_argv(path, argv_in, environ);
|
||||
static int (*real)(const char *, char *const[]);
|
||||
if (!real) real = (int (*)(const char *, char *const[]))dlsym(RTLD_NEXT, "execv");
|
||||
maybe_answer_lsof(path, argv);
|
||||
@@ -406,5 +407,6 @@ int posix_spawn(pid_t *pid, const char *path, const posix_spawn_file_actions_t *
|
||||
}
|
||||
static spawn_fn real;
|
||||
if (!real) real = (spawn_fn)dlsym(RTLD_NEXT, "posix_spawn");
|
||||
return bl_binfmt_spawn(pid, path, fa, attr, argv, envp, real);
|
||||
char *const *adjusted = bl_webhelper_argv(path, argv, envp);
|
||||
return bl_binfmt_spawn(pid, path, fa, attr, (char *const *)adjusted, envp, real);
|
||||
}
|
||||
@@ -12,7 +12,8 @@
|
||||
#define FAT_MAGIC 0x4d44
|
||||
#define EXFAT_MAGIC 0x2011bab0
|
||||
|
||||
static int is_wineserver(void) {
|
||||
__attribute__((visibility("hidden")))
|
||||
int bl_is_wineserver(void) {
|
||||
static int cached = -1;
|
||||
char exe[PATH_MAX];
|
||||
const char *base;
|
||||
@@ -43,7 +44,7 @@ void bl_storage_owner(int fd, struct stat *st) {
|
||||
int saved = errno;
|
||||
uid_t uid;
|
||||
|
||||
if (st != NULL && is_wineserver()) {
|
||||
if (st != NULL && bl_is_wineserver()) {
|
||||
uid = getuid();
|
||||
if (st->st_uid != uid && has_no_unix_owner(fd)) st->st_uid = uid;
|
||||
}
|
||||
@@ -60,7 +61,7 @@ int fchmod(int fd, mode_t mode) {
|
||||
return -1;
|
||||
}
|
||||
result = next_fchmod(fd, mode);
|
||||
if (result != 0 && errno == EPERM && is_wineserver()) {
|
||||
if (result != 0 && errno == EPERM && bl_is_wineserver()) {
|
||||
if (has_no_unix_owner(fd)) {
|
||||
errno = saved;
|
||||
return 0;
|
||||
|
||||
@@ -81,7 +81,10 @@ __attribute__((visibility("hidden"))) int bl_status_without_tracer(const char *p
|
||||
long pid;
|
||||
if ((flags & O_ACCMODE) != O_RDONLY || (pid = status_pid(path)) < 0 || is_web_helper(caller) || bl_from_ntdll(caller))
|
||||
return -1;
|
||||
int in = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
|
||||
// through the next open(): the native userland's hook gives the app's ids as root's there
|
||||
static int (*next_open)(const char *, int, ...) = NULL;
|
||||
if (!next_open) next_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open");
|
||||
int in = next_open ? next_open(path, O_RDONLY | O_CLOEXEC) : (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
|
||||
if (in < 0) goto done;
|
||||
for (;;) {
|
||||
ssize_t n = read(in, text + len, sizeof(text) - 1 - len);
|
||||
|
||||
@@ -12,7 +12,14 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
extern char **environ;
|
||||
|
||||
__attribute__((constructor)) static void bl_venus_keep_kopper(void) {
|
||||
static const char name[] = "LIBGL_KOPPER_DISABLE=";
|
||||
const char *debug = getenv("VN_DEBUG");
|
||||
if (debug && strstr(debug, "vtest")) unsetenv("LIBGL_KOPPER_DISABLE");
|
||||
if (!debug || !strstr(debug, "vtest") || !environ) return;
|
||||
char **out = environ;
|
||||
for (char **in = environ; *in; in++)
|
||||
if (strncmp(*in, name, sizeof name - 1)) *out++ = *in;
|
||||
*out = NULL;
|
||||
}
|
||||
@@ -26,6 +26,14 @@ static char *join_features(const char *have, const char *add) {
|
||||
return strcat(out, add);
|
||||
}
|
||||
|
||||
static int electron_node_mode(char *const envp[]) {
|
||||
static const char variable[] = "ELECTRON_RUN_AS_NODE=";
|
||||
for (int i = 0; envp && envp[i]; i++)
|
||||
if (!strncmp(envp[i], variable, sizeof variable - 1))
|
||||
return envp[i][sizeof variable - 1] != 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char *const *chromium_argv(const char *path, char *const argv[]) {
|
||||
const char *slash = path ? strrchr(path, '/') : NULL;
|
||||
if (!slash || !argv || !argv[0]) return argv;
|
||||
@@ -48,7 +56,9 @@ static char *const *chromium_argv(const char *path, char *const argv[]) {
|
||||
return out;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) char *const *bl_webhelper_argv(const char *path, char *const argv[]) {
|
||||
__attribute__((visibility("hidden")))
|
||||
char *const *bl_webhelper_argv(const char *path, char *const argv[], char *const envp[]) {
|
||||
if (electron_node_mode(envp)) return argv;
|
||||
if (!is_webhelper_browser(path, argv)) return chromium_argv(path, argv);
|
||||
const char *spec = getenv("BL_WEBHELPER_ARGS");
|
||||
if (!spec || !*spec) spec = WEBHELPER_DEFAULT;
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,34 @@
|
||||
# Native userland (no proot)
|
||||
|
||||
`libddnatv-hook.so` is an `LD_PRELOAD` library that lets the glibc rootfs run directly on Android:
|
||||
it maps the guest's absolute paths into the rootfs (plus the mounts proot used to provide, from
|
||||
`DDNATV_BINDS`), starts every program through the rootfs's `ld.so`, and answers the system calls
|
||||
Android's seccomp policy kills a process for (`SIGSYS` handler). There is no tracer. The app side
|
||||
is `runtime/NativeGuest.kt`; the setting is Performance > "Run without proot (experimental)".
|
||||
|
||||
The hook is derived from the Apache-2.0 `libcortex-hook` of
|
||||
[ssfuisu/cortex](https://github.com/ssfuisu/cortex) (see `LICENSE`), renamed (`cortex` -> `ddnatv`,
|
||||
same length: the code compares environment names by length) and extended with:
|
||||
|
||||
- `DDNATV_BINDS` mounts, `DDNATV_PRELOAD` (what `/etc/ld.so.preload` does for proot), and mapping of
|
||||
guest paths inside `LD_PRELOAD`;
|
||||
- POSIX `shm_open`/`shm_unlink` through `/dev/shm`;
|
||||
- starting Wine without its static preloader, which opens files with raw system calls.
|
||||
|
||||
Build: `tools/nativeguest/build.sh out.so` with an aarch64 glibc cross compiler.
|
||||
|
||||
## Testing under Android's seccomp filter
|
||||
|
||||
Some devices (eng builds, `setenforce 0` ROMs) run apps with no seccomp filter, so a hook that
|
||||
passes there can still die with SIGSYS on a stock phone (`GUEST_EXIT 159`). `seccomp-sim.c` applies
|
||||
a filter that answers the calls Android blocks with SIGSYS, then runs the program:
|
||||
|
||||
```
|
||||
aarch64-linux-gnu-gcc -static -O2 -o seccomp-sim tools/nativeguest/seccomp-sim.c
|
||||
# copy it into the rootfs, then name it for every native launch:
|
||||
echo /usr/local/bin/seccomp-sim > <rootfs>/etc/droiddeck/seccomp-sim
|
||||
```
|
||||
|
||||
`grep Seccomp: /proc/<pid>/status` should then show `2` for the session's processes. Empty the file
|
||||
to turn it off. `seccomp-sim -a NR,NR PROGRAM` leaves the listed calls allowed, to find which one
|
||||
kills a program.
|
||||
@@ -0,0 +1,50 @@
|
||||
// Writes the seccomp filter Android's zygote gives an app (from the device's libandroid_runtime.so)
|
||||
// to a file, as the raw sock_filter array, for seccomp-sim -f on devices whose zygote skips it
|
||||
// (a permissive SELinux). A bionic program, built with the NDK and run from adb shell:
|
||||
// aarch64-linux-android33-clang -O2 -rdynamic -o app-seccomp-dump app-seccomp-dump.c -ldl
|
||||
// app-seccomp-dump OUT
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <linux/filter.h>
|
||||
#include <linux/seccomp.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static const char *g_out;
|
||||
static int g_dumped;
|
||||
|
||||
// the runtime's own prctl() call lands here first
|
||||
__attribute__((visibility("default"))) int prctl(int option, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, option);
|
||||
unsigned long a2 = va_arg(ap, unsigned long), a3 = va_arg(ap, unsigned long);
|
||||
unsigned long a4 = va_arg(ap, unsigned long), a5 = va_arg(ap, unsigned long);
|
||||
va_end(ap);
|
||||
if (option == PR_SET_SECCOMP && a2 == SECCOMP_MODE_FILTER && g_out) {
|
||||
const struct sock_fprog *prog = (const struct sock_fprog *)a3;
|
||||
FILE *f = fopen(g_out, "wb");
|
||||
if (f) {
|
||||
fwrite(prog->filter, sizeof(prog->filter[0]), prog->len, f);
|
||||
fclose(f);
|
||||
fprintf(stderr, "app-seccomp-dump: %u instructions to %s\n", prog->len, g_out);
|
||||
g_dumped = 1;
|
||||
}
|
||||
return 0; // not installed in this process
|
||||
}
|
||||
return (int)syscall(SYS_prctl, option, a2, a3, a4, a5);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 2) { fprintf(stderr, "usage: %s OUT\n", argv[0]); return 2; }
|
||||
g_out = argv[1];
|
||||
void *rt = dlopen("libandroid_runtime.so", RTLD_NOW);
|
||||
if (!rt) { fprintf(stderr, "%s\n", dlerror()); return 2; }
|
||||
int (*set_app_filter)(void) = (int (*)(void))dlsym(rt, "_Z22set_app_seccomp_filterv");
|
||||
if (!set_app_filter) { fprintf(stderr, "no set_app_seccomp_filter\n"); return 2; }
|
||||
set_app_filter();
|
||||
return g_dumped ? 0 : 1;
|
||||
}
|
||||
Executable
+82
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env bash
|
||||
# Builds the code the native userland adds to static Go programs (gosys/gosys.c) into the C header
|
||||
# the hook includes (gosys/gosys_blob.h, not kept in the repository; build.sh makes it each time).
|
||||
#
|
||||
# The code is AArch64 whatever the machine this runs on: it is compiled freestanding with an
|
||||
# aarch64 cross compiler (gcc-aarch64-linux-gnu: the one that builds the hook; or clang with lld).
|
||||
#
|
||||
# usage: tools/nativeguest/build-gosys.sh [output header]
|
||||
set -euo pipefail
|
||||
|
||||
here=$(cd "$(dirname "$0")" && pwd)
|
||||
src=$here/gosys
|
||||
out=${1:-$src/gosys_blob.h}
|
||||
|
||||
if command -v aarch64-linux-gnu-gcc >/dev/null 2>&1; then
|
||||
CC=(aarch64-linux-gnu-gcc)
|
||||
LD=(aarch64-linux-gnu-ld)
|
||||
OBJCOPY=(aarch64-linux-gnu-objcopy)
|
||||
NM=(aarch64-linux-gnu-nm)
|
||||
OBJDUMP=(aarch64-linux-gnu-objdump)
|
||||
elif command -v clang >/dev/null 2>&1 && command -v ld.lld >/dev/null 2>&1; then
|
||||
CC=(clang --target=aarch64-linux-gnu)
|
||||
LD=(ld.lld)
|
||||
OBJCOPY=(llvm-objcopy)
|
||||
NM=(llvm-nm)
|
||||
OBJDUMP=(llvm-objdump)
|
||||
else
|
||||
echo "build-gosys.sh: needs aarch64-linux-gnu-gcc (apt install gcc-aarch64-linux-gnu) or clang with lld" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
tmp=$(mktemp -d)
|
||||
trap 'rm -rf "$tmp"' EXIT
|
||||
|
||||
"${CC[@]}" -std=gnu11 -O2 -Wall -Wextra -Werror \
|
||||
-ffreestanding -fno-builtin -nostdlib -fPIE -fvisibility=hidden -fno-stack-protector \
|
||||
-fno-asynchronous-unwind-tables -fno-unwind-tables -fno-tree-loop-distribute-patterns \
|
||||
-mcmodel=tiny -mgeneral-regs-only -mno-outline-atomics -ffixed-x18 -ffixed-x28 \
|
||||
-c "$src/gosys.c" -o "$tmp/gosys.o"
|
||||
"${LD[@]}" -T "$src/gosys.ld" -nostdlib -o "$tmp/gosys.elf" "$tmp/gosys.o"
|
||||
"${OBJCOPY[@]}" -O binary "$tmp/gosys.elf" "$tmp/gosys.bin"
|
||||
|
||||
# It must stand on its own: nothing undefined, no writable data, and nothing that depends on the
|
||||
# address it is loaded at (adrp assumes the page offset of the link address; adr does not)
|
||||
if [ -n "$("${NM[@]}" -u "$tmp/gosys.elf")" ]; then
|
||||
echo "build-gosys.sh: gosys.c needs symbols it does not define:" >&2
|
||||
"${NM[@]}" -u "$tmp/gosys.elf" >&2
|
||||
exit 1
|
||||
fi
|
||||
if "${OBJDUMP[@]}" -d "$tmp/gosys.elf" | grep -q -w adrp; then
|
||||
echo "build-gosys.sh: gosys.c uses adrp, which is not position-independent at any address" >&2
|
||||
exit 1
|
||||
fi
|
||||
sections=$("${OBJDUMP[@]}" -h "$tmp/gosys.elf" | awk '$1 ~ /^[0-9]+$/ {print $2}' | tr '\n' ' ')
|
||||
for s in $sections; do
|
||||
case $s in
|
||||
.hdr|.thunks|.text|.rodata|.trailer|.symtab|.strtab|.shstrtab) ;;
|
||||
*) echo "build-gosys.sh: unexpected section $s in gosys.elf (sections: $sections)" >&2; exit 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
size=$(stat -c %s "$tmp/gosys.bin")
|
||||
entry=$("${NM[@]}" "$tmp/gosys.elf" | awk '$3 == "cxgo_entry" {print $1}')
|
||||
thunks=$("${NM[@]}" "$tmp/gosys.elf" | awk '$3 == "g_thunks" {print $1}')
|
||||
if [ -z "$entry" ] || [ -z "$thunks" ]; then
|
||||
echo "build-gosys.sh: cxgo_entry or g_thunks not found" >&2
|
||||
exit 1
|
||||
fi
|
||||
[ "$(head -c 8 "$tmp/gosys.bin")" = "CXGOSYS1" ] || { echo "build-gosys.sh: the header is not first" >&2; exit 1; }
|
||||
[ "$(tail -c 64 "$tmp/gosys.bin" | head -c 8)" = "CXGOEND1" ] || { echo "build-gosys.sh: the trailer is not last" >&2; exit 1; }
|
||||
|
||||
{
|
||||
echo "// Generated by tools/nativeguest/build-gosys.sh from gosys/gosys.c. Do not edit."
|
||||
echo "#define GOSYS_BLOB_SIZE $size"
|
||||
echo "#define GOSYS_ENTRY_OFF 0x$entry"
|
||||
echo "#define GOSYS_THUNKS_OFF 0x$thunks"
|
||||
echo "static const unsigned char gosys_blob[GOSYS_BLOB_SIZE] = {"
|
||||
od -An -v -tx1 "$tmp/gosys.bin" | sed -e 's/^ *//' -e 's/ /, 0x/g' -e 's/^/0x/' -e 's/$/,/'
|
||||
echo "};"
|
||||
} > "$out.tmp"
|
||||
mv "$out.tmp" "$out"
|
||||
echo "build-gosys.sh: $out ($size bytes, entry 0x$entry)"
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
# Builds libddnatv-hook.so, the library that lets the glibc rootfs run on Android without proot.
|
||||
# usage: tools/nativeguest/build.sh <output.so> [compiler, default aarch64-linux-gnu-gcc]
|
||||
set -euo pipefail
|
||||
here=$(cd "$(dirname "$0")" && pwd)
|
||||
out=${1:?output path}
|
||||
cc=${2:-aarch64-linux-gnu-gcc}
|
||||
gen=$(mktemp -d)
|
||||
trap 'rm -rf "$gen"' EXIT
|
||||
mkdir -p "$gen/gosys"
|
||||
bash "$here/build-gosys.sh" "$gen/gosys/gosys_blob.h"
|
||||
"$cc" -std=gnu11 -shared -fPIC -O2 -fno-delete-null-pointer-checks -Wall -Wno-nonnull-compare \
|
||||
-Wno-format-truncation -Wno-stringop-truncation -Wl,-z,max-page-size=16384 -I "$gen" \
|
||||
-o "$out" "$here/ddnatv-hook.c" -ldl
|
||||
"${cc%gcc}strip" --strip-unneeded "$out"
|
||||
@@ -0,0 +1,87 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <dlfcn.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <spawn.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <ftw.h>
|
||||
#include <glob.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <ucontext.h>
|
||||
#include <sys/statfs.h>
|
||||
#include <sys/statvfs.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <resolv.h>
|
||||
#include <utime.h>
|
||||
#include <sys/time.h>
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
#include <shadow.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/sendfile.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sched.h>
|
||||
#include <time.h>
|
||||
#include <sys/xattr.h>
|
||||
#include <fnmatch.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <net/if.h>
|
||||
#include <sys/inotify.h>
|
||||
|
||||
#ifndef RENAME_EXCHANGE
|
||||
#define RENAME_EXCHANGE (1 << 1)
|
||||
#endif
|
||||
|
||||
// The code that is added to static Go programs (gosys/gosys.c), built by scripts/build-gosys.sh. A
|
||||
// build without it works as before, only without Go support. It is AArch64 code: the 32-bit hook
|
||||
// has none (a 32-bit phone cannot run 64-bit programs).
|
||||
#if defined(__has_include) && defined(__LP64__)
|
||||
#if __has_include("gosys/gosys_blob.h")
|
||||
#include "gosys/gosys_abi.h"
|
||||
#include "gosys/gosys_blob.h"
|
||||
#define DDNATV_HAVE_GOSYS 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// The hook is one translation unit split into parts by topic, in this order (later parts use
|
||||
// the static functions of earlier ones).
|
||||
#include "hook/syscalls.c"
|
||||
#include "hook/paths.c"
|
||||
#include "hook/hardlinks.c"
|
||||
#include "hook/signals.c"
|
||||
#include "hook/setup.c"
|
||||
#include "hook/files.c"
|
||||
#include "hook/fileops.c"
|
||||
#include "hook/identity.c"
|
||||
#include "hook/privileged.c"
|
||||
#include "hook/env.c"
|
||||
#include "hook/exec_probe.c"
|
||||
#include "hook/static_programs.c"
|
||||
#include "hook/go_programs.c"
|
||||
#include "hook/exec.c"
|
||||
#include "hook/net.c"
|
||||
#include "hook/sockets.c"
|
||||
#include "hook/sysv_shm.c"
|
||||
#include "hook/posix_shm.c"
|
||||
@@ -0,0 +1,510 @@
|
||||
// The code The native userland adds to a static Go program so that it sees the rootfs (see gosys_abi.h).
|
||||
//
|
||||
// Built freestanding (scripts/build-gosys.sh) as a flat, position-independent binary with no
|
||||
// writable data, no libc and no relocations: the hook copies it into the program's file. It is
|
||||
// entered from the one place where Go's system calls are made, in place of the "svc #0" of
|
||||
// internal/runtime/syscall.Syscall6, with the registers of that call: arguments in x0 to x5,
|
||||
// the number in x8 (passed again as the eighth argument). It returns what the kernel would, and it
|
||||
// may use every register but x28 (the current goroutine), x29, x30 and sp, which it keeps.
|
||||
//
|
||||
// It does for Go what libddnatv-hook.so does for C programs: maps absolute Ubuntu paths into the
|
||||
// rootfs (the same rules as hook/paths.c), shortens the Unix socket paths that do not fit, shows
|
||||
// the program the paths it expects from getcwd and /proc/self/exe, and starts Ubuntu programs
|
||||
// through the rootfs's dynamic linker and env, which then go through the hook.
|
||||
|
||||
#include "gosys_abi.h"
|
||||
|
||||
typedef int64_t i64;
|
||||
typedef uint64_t u64;
|
||||
typedef uint32_t u32;
|
||||
typedef uint16_t u16;
|
||||
|
||||
#define AT_FDCWD (-100)
|
||||
#define SLOT_PATH_A 0u
|
||||
#define SLOT_PATH_B 4096u
|
||||
#define SLOT_ARGV 8192u
|
||||
#define SLOT_ENV 16384u
|
||||
#define SLOT_PATH_CAP 4096u
|
||||
#define ARGV_CAP 1000u // entries of the argv array that starts a program
|
||||
#define ENV_CAP 2000u
|
||||
|
||||
// The numbers of AArch64 Linux (the same for every kernel)
|
||||
enum {
|
||||
NR_SETXATTR = 5, NR_LSETXATTR = 6, NR_GETXATTR = 8, NR_LGETXATTR = 9, NR_LISTXATTR = 11,
|
||||
NR_LLISTXATTR = 12, NR_REMOVEXATTR = 14, NR_LREMOVEXATTR = 15, NR_GETCWD = 17,
|
||||
NR_INOTIFY_ADD_WATCH = 27, NR_MKNODAT = 33, NR_MKDIRAT = 34, NR_UNLINKAT = 35, NR_SYMLINKAT = 36,
|
||||
NR_LINKAT = 37, NR_RENAMEAT = 38, NR_STATFS = 43, NR_TRUNCATE = 45, NR_FACCESSAT = 48,
|
||||
NR_CHDIR = 49, NR_CHROOT = 51, NR_FCHMODAT = 53, NR_FCHOWNAT = 54, NR_OPENAT = 56,
|
||||
NR_READLINKAT = 78, NR_NEWFSTATAT = 79, NR_UTIMENSAT = 88, NR_BIND = 200, NR_CONNECT = 203,
|
||||
NR_EXECVE = 221, NR_NAME_TO_HANDLE_AT = 264, NR_RENAMEAT2 = 276, NR_STATX = 291,
|
||||
NR_OPENAT2 = 437, NR_FACCESSAT2 = 439, NR_FCHMODAT2 = 452,
|
||||
};
|
||||
|
||||
__attribute__((section(".hdr"), used))
|
||||
static const volatile struct gosys_hdr g_hdr = { .magic = "CXGOSYS1", .version = GOSYS_VERSION };
|
||||
|
||||
// Filled by the patcher; the instructions that connect the call site to cxgo_entry
|
||||
__attribute__((section(".thunks"), used))
|
||||
static const volatile u32 g_thunks[GOSYS_THUNK_BYTES / 4];
|
||||
|
||||
__attribute__((section(".trailer"), used))
|
||||
static const volatile struct gosys_trailer g_trailer = { .magic = "CXGOEND1", .version = GOSYS_VERSION };
|
||||
|
||||
static inline const struct gosys_hdr *hdr(void) {
|
||||
const struct gosys_hdr *h = (const struct gosys_hdr *)&g_hdr;
|
||||
__asm__("" : "+r"(h)); // the compiler must not assume it knows the contents
|
||||
return h;
|
||||
}
|
||||
|
||||
static inline i64 sc(i64 nr, i64 a0, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5) {
|
||||
register i64 x8 __asm__("x8") = nr;
|
||||
register i64 x0 __asm__("x0") = a0;
|
||||
register i64 x1 __asm__("x1") = a1;
|
||||
register i64 x2 __asm__("x2") = a2;
|
||||
register i64 x3 __asm__("x3") = a3;
|
||||
register i64 x4 __asm__("x4") = a4;
|
||||
register i64 x5 __asm__("x5") = a5;
|
||||
__asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5) : "memory", "cc");
|
||||
return x0;
|
||||
}
|
||||
|
||||
static u64 slen(const char *s) {
|
||||
u64 n = 0;
|
||||
while (s[n]) n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
static void mcopy(char *d, const char *s, u64 n) {
|
||||
for (u64 i = 0; i < n; i++) d[i] = s[i];
|
||||
}
|
||||
|
||||
// Moves n bytes down (d < s)
|
||||
static void mmove_down(char *d, const char *s, u64 n) {
|
||||
for (u64 i = 0; i < n; i++) d[i] = s[i];
|
||||
}
|
||||
|
||||
static int pre(const char *path, const char *lit, u64 n) {
|
||||
for (u64 i = 0; i < n; i++)
|
||||
if (path[i] != lit[i]) return 0;
|
||||
return path[n] == '/' || path[n] == '\0';
|
||||
}
|
||||
|
||||
// Whether path is the rootfs or in it
|
||||
static int in_root(const struct gosys_hdr *h, const char *p) {
|
||||
u64 n = h->root_len;
|
||||
if (n == 0) return 0;
|
||||
for (u64 i = 0; i < n; i++)
|
||||
if (p[i] != h->root[i]) return 0;
|
||||
return p[n] == '/' || p[n] == '\0';
|
||||
}
|
||||
|
||||
// Android's own mounts and files, which must never be redirected into the rootfs (hook/paths.c)
|
||||
static int is_host_path(const char *path) {
|
||||
switch (path[1]) {
|
||||
case 'a': return pre(path, "/apex", 5);
|
||||
case 'c': return pre(path, "/config", 7);
|
||||
case 'd': return pre(path, "/dev", 4) || pre(path, "/d", 2) || pre(path, "/data", 5);
|
||||
case 'l': return pre(path, "/linkerconfig", 13);
|
||||
case 'o': return pre(path, "/odm", 4) || pre(path, "/oem", 4);
|
||||
case 'p': return pre(path, "/proc", 5) || pre(path, "/product", 8);
|
||||
case 's': return pre(path, "/sbin/su", 8) || pre(path, "/su", 3) || pre(path, "/sys", 4) ||
|
||||
pre(path, "/system", 7) || pre(path, "/system_ext", 11) ||
|
||||
pre(path, "/sdcard", 7) || pre(path, "/storage", 8);
|
||||
case 'v': return pre(path, "/vendor", 7);
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// An absolute Ubuntu path mapped into the rootfs: `path` itself when it needs no change, else buf
|
||||
// (hook/paths.c rewrite_path_in_root). A result that would not fit is the path unchanged: the
|
||||
// kernel then answers as it would for a path that long.
|
||||
static const char *rw_path(const struct gosys_hdr *h, const char *path, char *buf, u64 cap) {
|
||||
if (path[0] != '/' || h->root_len == 0) return path;
|
||||
while (path[0] == '/' && path[1] == '/') path++;
|
||||
while (path[0] == '/' && path[1] == '.' && (path[2] == '/' || path[2] == '\0')) {
|
||||
if (path[2] == '/') {
|
||||
path += 2;
|
||||
} else {
|
||||
path = "/";
|
||||
break;
|
||||
}
|
||||
}
|
||||
u64 rl = h->root_len;
|
||||
if (path[1] == '\0') {
|
||||
mcopy(buf, h->root, rl);
|
||||
buf[rl] = '\0';
|
||||
return buf;
|
||||
}
|
||||
if (in_root(h, path)) return path;
|
||||
if (pre(path, "/dev/shm", 8)) {
|
||||
u64 rest = slen(path + 8);
|
||||
if (rl + 8 + rest + 1 > cap) return path;
|
||||
mcopy(buf, h->root, rl);
|
||||
mcopy(buf + rl, "/tmp/shm", 8);
|
||||
mcopy(buf + rl + 8, path + 8, rest + 1);
|
||||
return buf;
|
||||
}
|
||||
if (is_host_path(path)) return path;
|
||||
u64 pl = slen(path);
|
||||
if (rl + pl + 1 > cap) return path;
|
||||
mcopy(buf, h->root, rl);
|
||||
mcopy(buf + rl, path, pl + 1);
|
||||
return buf;
|
||||
}
|
||||
|
||||
// Whether the rootfs is where the code was told it is. In root mode the program runs in a chroot of
|
||||
// that very rootfs, where the real paths are the Ubuntu ones and the rootfs's own path does not
|
||||
// exist: then nothing is mapped and the program works as any other. Looked at once for each process
|
||||
// (the scratch memory starts as zero, and a forked child keeps what its parent found).
|
||||
static int rewriting(const struct gosys_hdr *h) {
|
||||
volatile u32 *mode = (volatile u32 *)((char *)h + h->scratch_rel + 4);
|
||||
u32 m = *mode;
|
||||
if (m == 0) {
|
||||
m = sc(NR_FACCESSAT, AT_FDCWD, (i64)h->root, 0, 0, 0, 0) == 0 ? 1u : 2u;
|
||||
*mode = m;
|
||||
}
|
||||
return m == 1;
|
||||
}
|
||||
|
||||
#define NO_SLOT 0xffffffffu
|
||||
#define NR_SCHED_YIELD 124
|
||||
|
||||
// Who is making the call: the goroutine (x28 holds it, and the code is built not to touch x28). A
|
||||
// goroutine makes one call at a time, and the vfork child that a goroutine makes runs with the same
|
||||
// x28 as the goroutine does. (TPIDR_EL0 does not tell the threads of a Go program apart: its runtime
|
||||
// leaves it unset without cgo.)
|
||||
static inline u64 thread_tag(void) {
|
||||
u64 t;
|
||||
__asm__ volatile("mov %0, x28" : "=r"(t));
|
||||
return t;
|
||||
}
|
||||
|
||||
// How many execve calls have not returned (state area, offset 8). One that works never does: it is
|
||||
// made by a vfork child, and the slot its path was in stays marked as the goroutine's, which goes on
|
||||
// when the child is gone (the same x28, a call of its own). Without this, goroutines that each
|
||||
// started a program held a slot each for good, and after the sixty-fourth all the others waited.
|
||||
static inline u32 *execs_on_their_way(const struct gosys_hdr *h) {
|
||||
return (u32 *)((char *)h + h->scratch_rel + 8);
|
||||
}
|
||||
|
||||
static void count_down(u32 *count) {
|
||||
u32 n = __atomic_load_n(count, __ATOMIC_RELAXED);
|
||||
while (n != 0 && !__atomic_compare_exchange_n(count, &n, n - 1, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
|
||||
}
|
||||
}
|
||||
|
||||
// Gives back what this goroutine's vfork child left (see above). Every call looks at one counter
|
||||
// for it; the list of slots is only looked through while an execve is on its way.
|
||||
static void reclaim_left_slots(const struct gosys_hdr *h) {
|
||||
u32 *count = execs_on_their_way(h);
|
||||
if (__atomic_load_n(count, __ATOMIC_RELAXED) == 0) return;
|
||||
u64 *owner = (u64 *)((char *)h + h->scratch_rel + 64);
|
||||
u64 me = thread_tag() | 1;
|
||||
for (u32 i = 0; i < GOSYS_SLOTS; i++) {
|
||||
if (__atomic_load_n(&owner[i], __ATOMIC_RELAXED) != me) continue;
|
||||
__atomic_store_n(&owner[i], 0, __ATOMIC_RELEASE);
|
||||
count_down(count);
|
||||
}
|
||||
}
|
||||
|
||||
// A slot for the call this goroutine is making; *held is its number, to give back when the call is
|
||||
// done. Each slot has a word of its own that holds who has it (0: nobody), so that taking one is one
|
||||
// compare-and-swap, and no other goroutine can ever mistake it for its own.
|
||||
static char *take_slot(const struct gosys_hdr *h, u32 *held) {
|
||||
char *base = (char *)h + h->scratch_rel;
|
||||
u64 *owner = (u64 *)(base + 64);
|
||||
u64 me = thread_tag() | 1; // never 0
|
||||
u32 mine = NO_SLOT;
|
||||
// A slot of this goroutine that is still taken now (cxgo_entry gives back those of its vfork
|
||||
// children) is its own again
|
||||
for (u32 i = 0; i < GOSYS_SLOTS; i++) {
|
||||
if (__atomic_load_n(&owner[i], __ATOMIC_RELAXED) != me) continue;
|
||||
if (mine == NO_SLOT)
|
||||
mine = i;
|
||||
else
|
||||
__atomic_store_n(&owner[i], 0, __ATOMIC_RELEASE);
|
||||
}
|
||||
while (mine == NO_SLOT) {
|
||||
for (u32 i = 0; i < GOSYS_SLOTS && mine == NO_SLOT; i++) {
|
||||
u64 none = 0;
|
||||
if (__atomic_load_n(&owner[i], __ATOMIC_RELAXED) == 0 &&
|
||||
__atomic_compare_exchange_n(&owner[i], &none, me, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
|
||||
mine = i;
|
||||
}
|
||||
if (mine == NO_SLOT) sc(NR_SCHED_YIELD, 0, 0, 0, 0, 0, 0); // all taken: the others finish soon
|
||||
}
|
||||
*held = mine;
|
||||
return base + GOSYS_STATE_SIZE + (u64)mine * GOSYS_SLOT_SIZE;
|
||||
}
|
||||
|
||||
static void put_slot(const struct gosys_hdr *h, u32 held) {
|
||||
if (held == NO_SLOT) return;
|
||||
char *base = (char *)h + h->scratch_rel;
|
||||
__atomic_store_n((u64 *)(base + 64) + held, 0, __ATOMIC_RELEASE);
|
||||
}
|
||||
|
||||
// A path as the program expects to see it, from one of the kernel's: the rootfs prefix removed.
|
||||
// Returns the new length of the n bytes at buf (a path, not NUL-terminated unless `nul`).
|
||||
static i64 unmap_prefix(const struct gosys_hdr *h, char *buf, i64 n, int nul) {
|
||||
u64 rl = h->root_len;
|
||||
if (rl == 0 || n <= 0 || (u64)n < rl + (nul ? 1 : 0)) return n;
|
||||
for (u64 i = 0; i < rl; i++)
|
||||
if (buf[i] != h->root[i]) return n;
|
||||
u64 end = (u64)n - (nul ? 1 : 0); // the path's own length
|
||||
if (end == rl) { // exactly the rootfs
|
||||
buf[0] = '/';
|
||||
if (nul) {
|
||||
buf[1] = '\0';
|
||||
return 2;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if (buf[rl] != '/') return n;
|
||||
mmove_down(buf, buf + rl, (u64)n - rl);
|
||||
return n - (i64)rl;
|
||||
}
|
||||
|
||||
// ---- Unix sockets ------------------------------------------------------------------------
|
||||
|
||||
static void put_hex16(char *out, u64 v) {
|
||||
static const char digits[] = "0123456789abcdef";
|
||||
for (int i = 15; i >= 0; i--) {
|
||||
out[i] = digits[v & 15];
|
||||
v >>= 4;
|
||||
}
|
||||
}
|
||||
|
||||
// bind() and connect() of a Unix socket with a path: mapped like hook/setup.c does, and hashed
|
||||
// into /tmp when it does not fit the 108 bytes of sun_path (the same name, so a Go server and a
|
||||
// program using the C library find each other)
|
||||
static i64 with_sockaddr(const struct gosys_hdr *h, u32 *held, i64 nr, i64 a0, i64 a1, i64 a2) {
|
||||
const unsigned char *sa = (const unsigned char *)a1;
|
||||
if (!sa || a2 < 4 || a2 > 128 || sa[0] != 1 /* AF_UNIX */ || sa[1] != 0 || sa[2] != '/')
|
||||
return sc(nr, a0, a1, a2, 0, 0, 0);
|
||||
char *slot = take_slot(h, held);
|
||||
char *orig = slot + SLOT_PATH_A;
|
||||
u64 max = (u64)a2 - 2;
|
||||
if (max > 108) max = 108;
|
||||
mcopy(orig, (const char *)sa + 2, max);
|
||||
orig[max] = '\0';
|
||||
const char *rw = rw_path(h, orig, slot + SLOT_PATH_B, SLOT_PATH_CAP);
|
||||
u64 rl = slen(rw);
|
||||
char *out = slot + SLOT_ARGV;
|
||||
out[0] = 1;
|
||||
out[1] = 0;
|
||||
if (rl < 108) {
|
||||
mcopy(out + 2, rw, rl + 1);
|
||||
return sc(nr, a0, (i64)out, (i64)(2 + rl + 1), 0, 0, 0);
|
||||
}
|
||||
u64 hash = 14695981039346656037ULL;
|
||||
for (const char *p = rw; *p; p++) {
|
||||
hash ^= (unsigned char)*p;
|
||||
hash *= 1099511628211ULL;
|
||||
}
|
||||
u64 n = h->root_len;
|
||||
if (n + 5 + 11 + 16 + 1 > 108) return sc(nr, a0, a1, a2, 0, 0, 0);
|
||||
mcopy(out + 2, h->root, n);
|
||||
mcopy(out + 2 + n, "/tmp/.ctx_sock_", 15);
|
||||
put_hex16(out + 2 + n + 15, hash);
|
||||
out[2 + n + 15 + 16] = '\0';
|
||||
return sc(nr, a0, (i64)out, (i64)(2 + n + 15 + 16 + 1), 0, 0, 0);
|
||||
}
|
||||
|
||||
// ---- Starting programs -------------------------------------------------------------------
|
||||
|
||||
static int starts_with(const char *s, const char *lit) {
|
||||
for (u64 i = 0; lit[i]; i++)
|
||||
if (s[i] != lit[i]) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// A program of Android's gets the environment it expects, as from hook/env.c clean_env_for_system():
|
||||
// without the hook and the rootfs's libraries, and with Android's directories in its PATH
|
||||
static i64 exec_host(const struct gosys_hdr *h, char *slot, const char *p, i64 a1, char *const *envp) {
|
||||
(void)h;
|
||||
static const char android_path[] = "PATH=/system/bin:/system/xbin:/sbin:/vendor/bin";
|
||||
u32 envc = 0;
|
||||
while (envp && envp[envc]) envc++;
|
||||
if (envc + 2 > ENV_CAP) return sc(NR_EXECVE, (i64)p, a1, (i64)envp, 0, 0, 0);
|
||||
char **ne = (char **)(slot + SLOT_ENV);
|
||||
char *path_str = slot + SLOT_PATH_B;
|
||||
u32 n = 0;
|
||||
int has_path = 0;
|
||||
for (u32 i = 0; i < envc; i++) {
|
||||
char *e = envp[i];
|
||||
if (starts_with(e, "LD_PRELOAD=") || starts_with(e, "LD_LIBRARY_PATH=") || starts_with(e, "GLIBC_TUNABLES="))
|
||||
continue;
|
||||
if (starts_with(e, "PATH=")) {
|
||||
u64 len = slen(e);
|
||||
has_path = 1;
|
||||
if (len + sizeof(android_path) + 1 < SLOT_PATH_CAP) {
|
||||
mcopy(path_str, android_path, sizeof(android_path) - 1);
|
||||
path_str[sizeof(android_path) - 1] = ':';
|
||||
mcopy(path_str + sizeof(android_path), e + 5, len - 5 + 1);
|
||||
ne[n++] = path_str;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
ne[n++] = e;
|
||||
}
|
||||
if (!has_path) ne[n++] = (char *)android_path;
|
||||
ne[n] = 0;
|
||||
return sc(NR_EXECVE, (i64)p, a1, (i64)ne, 0, 0, 0);
|
||||
}
|
||||
|
||||
// execve(): a program of the rootfs is started as
|
||||
// <ld.so> --preload <hook> --library-path <dirs> <bash> -c 'exec -a "$0" "$@"' <argv0> <path> <args>...
|
||||
// so that bash, a program of the rootfs with the hook loaded, starts it the way every program of
|
||||
// The native userland is started (Ubuntu's dynamic programs through ld.so, scripts through their interpreter,
|
||||
// static programs patched first), with the argv[0] the caller chose. Without bash it is env,
|
||||
// which cannot set argv[0]. The path is passed as the caller wrote it.
|
||||
static i64 with_execve(const struct gosys_hdr *h, u32 *held, i64 a0, i64 a1, i64 a2) {
|
||||
const char *path = (const char *)a0;
|
||||
char *const *argv = (char *const *)a1;
|
||||
char *const *envp = (char *const *)a2;
|
||||
if (!path || path[0] == '\0') return sc(NR_EXECVE, a0, a1, a2, 0, 0, 0);
|
||||
|
||||
char *slot = take_slot(h, held);
|
||||
const char *p = path[0] == '/' ? rw_path(h, path, slot + SLOT_PATH_A, SLOT_PATH_CAP) : path;
|
||||
int wrap = h->ldso_len != 0 && h->runner_len != 0 && (path[0] != '/' || in_root(h, p));
|
||||
if (!wrap) {
|
||||
if (path[0] == '/' && !in_root(h, p)) return exec_host(h, slot, p, a1, envp);
|
||||
return sc(NR_EXECVE, (i64)p, a1, a2, 0, 0, 0);
|
||||
}
|
||||
|
||||
// A program that is not there is reported to the caller, as execve() does
|
||||
i64 r = sc(NR_FACCESSAT, AT_FDCWD, (i64)p, 0, 0, 0, 0);
|
||||
if (r < 0) return r;
|
||||
|
||||
u32 argc = 0;
|
||||
while (argv && argv[argc]) argc++;
|
||||
if (argc + 12 > ARGV_CAP) return sc(NR_EXECVE, (i64)p, a1, a2, 0, 0, 0);
|
||||
|
||||
// DDNATV_ROOT must reach env even when the caller built an environment of its own
|
||||
char *const *env = envp;
|
||||
u32 envc = 0;
|
||||
int has_root = 0;
|
||||
while (envp && envp[envc]) {
|
||||
const char *e = envp[envc];
|
||||
if (e[0] == 'D' && e[1] == 'D' && e[2] == 'N' && e[3] == 'A' && e[4] == 'T' && e[5] == 'V' &&
|
||||
e[6] == '_' && e[7] == 'R' && e[8] == 'O' && e[9] == 'O' && e[10] == 'T' && e[11] == '=')
|
||||
has_root = 1;
|
||||
envc++;
|
||||
}
|
||||
if (!has_root) {
|
||||
if (envc + 2 > ENV_CAP) return sc(NR_EXECVE, (i64)p, a1, a2, 0, 0, 0);
|
||||
char **ne = (char **)(slot + SLOT_ENV);
|
||||
char *kv = slot + SLOT_PATH_B;
|
||||
mcopy(kv, "DDNATV_ROOT=", 12);
|
||||
mcopy(kv + 12, h->root, h->root_len + 1);
|
||||
for (u32 i = 0; i < envc; i++) ne[i] = envp[i];
|
||||
ne[envc] = kv;
|
||||
ne[envc + 1] = 0;
|
||||
env = ne;
|
||||
}
|
||||
|
||||
char **v = (char **)(slot + SLOT_ARGV);
|
||||
u32 n = 0;
|
||||
v[n++] = (char *)h->ldso;
|
||||
if (h->hook_len) {
|
||||
v[n++] = (char *)"--preload";
|
||||
v[n++] = (char *)h->hook;
|
||||
}
|
||||
v[n++] = (char *)"--library-path";
|
||||
v[n++] = (char *)h->libs;
|
||||
v[n++] = (char *)h->runner;
|
||||
if (h->flags & GOSYS_FLAG_RUNNER_IS_BASH) {
|
||||
v[n++] = (char *)"-c";
|
||||
v[n++] = (char *)"exec -a \"$0\" \"$@\"";
|
||||
v[n++] = (argc > 0 && argv[0]) ? argv[0] : (char *)path;
|
||||
} else {
|
||||
v[n++] = (char *)"--";
|
||||
}
|
||||
v[n++] = (char *)path;
|
||||
for (u32 i = 1; i < argc; i++) v[n++] = argv[i];
|
||||
v[n] = 0;
|
||||
return sc(NR_EXECVE, (i64)h->ldso, (i64)v, (i64)env, 0, 0, 0);
|
||||
}
|
||||
|
||||
// ---- The entry ---------------------------------------------------------------------------
|
||||
|
||||
// Rewrites the path arguments `i1` and `i2` (an index into a[], -1: none) that are absolute, makes
|
||||
// the call, and shows the program the paths it expects in what the call returns.
|
||||
static i64 with_paths(const struct gosys_hdr *h, u32 *held, i64 nr, i64 *a, int i1, int i2) {
|
||||
char *slot = 0;
|
||||
int proc_link = 0;
|
||||
int idx[2] = { i1, i2 };
|
||||
for (int k = 0; k < 2; k++) {
|
||||
int i = idx[k];
|
||||
if (i < 0) continue;
|
||||
const char *p = (const char *)a[i];
|
||||
if (!p || p[0] != '/') continue;
|
||||
if (!slot) slot = take_slot(h, held);
|
||||
if (nr == NR_READLINKAT && k == 0 && pre(p, "/proc", 5)) proc_link = 1;
|
||||
a[i] = (i64)rw_path(h, p, slot + (k ? SLOT_PATH_B : SLOT_PATH_A), SLOT_PATH_CAP);
|
||||
}
|
||||
i64 r = sc(nr, a[0], a[1], a[2], a[3], a[4], a[5]);
|
||||
if (proc_link && r > 0) r = unmap_prefix(h, (char *)a[2], r, 0);
|
||||
return r;
|
||||
}
|
||||
|
||||
// The call itself; a slot that it takes is given back by cxgo_entry
|
||||
static i64 handle(const struct gosys_hdr *h, u32 *held, i64 nr, i64 a0, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5) {
|
||||
i64 a[6] = { a0, a1, a2, a3, a4, a5 };
|
||||
switch (nr) {
|
||||
case NR_OPENAT: case NR_FACCESSAT: case NR_FACCESSAT2: case NR_FCHMODAT: case NR_FCHMODAT2:
|
||||
case NR_FCHOWNAT: case NR_NEWFSTATAT: case NR_STATX: case NR_READLINKAT: case NR_MKDIRAT:
|
||||
case NR_MKNODAT: case NR_UNLINKAT: case NR_UTIMENSAT: case NR_OPENAT2: case NR_NAME_TO_HANDLE_AT:
|
||||
case NR_INOTIFY_ADD_WATCH: case NR_CHDIR: case NR_CHROOT: case NR_STATFS: case NR_TRUNCATE:
|
||||
case NR_SETXATTR: case NR_LSETXATTR: case NR_GETXATTR: case NR_LGETXATTR: case NR_LISTXATTR:
|
||||
case NR_LLISTXATTR: case NR_REMOVEXATTR: case NR_LREMOVEXATTR: case NR_RENAMEAT: case NR_RENAMEAT2:
|
||||
case NR_LINKAT: case NR_SYMLINKAT: case NR_GETCWD: case NR_BIND: case NR_CONNECT: case NR_EXECVE:
|
||||
// The calls that carry paths: not mapped where the rootfs is not in its own place (above)
|
||||
if (!rewriting(h)) return sc(nr, a0, a1, a2, a3, a4, a5);
|
||||
break;
|
||||
default:
|
||||
return sc(nr, a0, a1, a2, a3, a4, a5);
|
||||
}
|
||||
switch (nr) {
|
||||
case NR_OPENAT: case NR_FACCESSAT: case NR_FACCESSAT2: case NR_FCHMODAT: case NR_FCHMODAT2:
|
||||
case NR_FCHOWNAT: case NR_NEWFSTATAT: case NR_STATX: case NR_READLINKAT: case NR_MKDIRAT:
|
||||
case NR_MKNODAT: case NR_UNLINKAT: case NR_UTIMENSAT: case NR_OPENAT2: case NR_NAME_TO_HANDLE_AT:
|
||||
case NR_INOTIFY_ADD_WATCH:
|
||||
return with_paths(h, held, nr, a, 1, -1);
|
||||
case NR_CHDIR: case NR_CHROOT: case NR_STATFS: case NR_TRUNCATE: case NR_SETXATTR:
|
||||
case NR_LSETXATTR: case NR_GETXATTR: case NR_LGETXATTR: case NR_LISTXATTR: case NR_LLISTXATTR:
|
||||
case NR_REMOVEXATTR: case NR_LREMOVEXATTR:
|
||||
return with_paths(h, held, nr, a, 0, -1);
|
||||
case NR_RENAMEAT: case NR_RENAMEAT2: case NR_LINKAT:
|
||||
return with_paths(h, held, nr, a, 1, 3);
|
||||
case NR_SYMLINKAT: // the target is data, only the link is a path
|
||||
return with_paths(h, held, nr, a, 2, -1);
|
||||
case NR_GETCWD: {
|
||||
i64 r = sc(nr, a0, a1, 0, 0, 0, 0);
|
||||
return r > 0 ? unmap_prefix(h, (char *)a0, r, 1) : r;
|
||||
}
|
||||
case NR_BIND: case NR_CONNECT:
|
||||
return with_sockaddr(h, held, nr, a0, a1, a2);
|
||||
case NR_EXECVE: {
|
||||
// Counted while it is on its way: when it works it does not return (see execs_on_their_way)
|
||||
u32 *count = execs_on_their_way(h);
|
||||
__atomic_fetch_add(count, 1, __ATOMIC_RELAXED);
|
||||
i64 r = with_execve(h, held, a0, a1, a2);
|
||||
count_down(count); // it did return: it failed, and cxgo_entry gives its slot back
|
||||
return r;
|
||||
}
|
||||
default:
|
||||
return sc(nr, a0, a1, a2, a3, a4, a5);
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((section(".text.cxgo_entry"), used, noinline))
|
||||
i64 cxgo_entry(i64 a0, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5, i64 unused, i64 nr) {
|
||||
(void)unused;
|
||||
const struct gosys_hdr *h = hdr();
|
||||
u32 held = NO_SLOT;
|
||||
reclaim_left_slots(h);
|
||||
i64 r = handle(h, &held, nr, a0, a1, a2, a3, a4, a5);
|
||||
put_slot(h, held);
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/* The layout of gosys.c's flat binary: the header at 0 (the patcher fills it in), the room for the
|
||||
call site's connecting code, the code with its entry first, read-only data, the trailer last. */
|
||||
OUTPUT_FORMAT(elf64-littleaarch64)
|
||||
ENTRY(cxgo_entry)
|
||||
SECTIONS
|
||||
{
|
||||
. = 0;
|
||||
.hdr : { KEEP(*(.hdr)) }
|
||||
.thunks : { . = ALIGN(16); KEEP(*(.thunks)) }
|
||||
.text : { . = ALIGN(16); KEEP(*(.text.cxgo_entry)) *(.text .text.*) }
|
||||
.rodata : { . = ALIGN(16); *(.rodata .rodata.*) }
|
||||
.trailer : { . = ALIGN(64); KEEP(*(.trailer)) }
|
||||
/DISCARD/ : { *(.comment) *(.note*) *(.eh_frame*) *(.gnu*) *(.interp) }
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// What the hook and the code it adds to a static Go program (gosys.c) agree on.
|
||||
//
|
||||
// A Go program built without cgo makes its system calls itself, so the preloaded hook never sees
|
||||
// them and Ubuntu's paths (/etc/resolv.conf, /usr/bin/git, ...) mean nothing to it. Before such a
|
||||
// program starts, the hook adds a small piece of position-independent code to its file (see
|
||||
// go_programs.c): the program's one system call function, internal/runtime/syscall.Syscall6, jumps
|
||||
// to it, and it maps paths into the rootfs like the hook does, then makes the call.
|
||||
//
|
||||
// This header has no dependencies beyond <stdint.h>, so that the freestanding code and the hook,
|
||||
// which are built with different flags, can both include it.
|
||||
|
||||
#ifndef DDNATV_GOSYS_ABI_H
|
||||
#define DDNATV_GOSYS_ABI_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef GOSYS_VERSION
|
||||
#define GOSYS_VERSION 3u // raised when the code or its header changes: patched programs are patched again
|
||||
#endif
|
||||
|
||||
// The scratch memory for rewritten paths: a small state area and 64 slots, in zero-filled memory the
|
||||
// patcher appends to the program's data segment (so nothing is allocated per call). A slot belongs to
|
||||
// the thread that took it until its call is over: a word in the state area for each slot says
|
||||
// which goroutine (x28) has it. A slot that a vfork child kept by a successful execve is given back
|
||||
// at the next call of the goroutine that made the child (the state area counts the execve calls
|
||||
// that have not returned, [8]).
|
||||
// (A ring of slots shared by every call was tried first: a thread that was descheduled for a
|
||||
// moment between taking a slot and the call let sixteen others go round the ring and overwrite its
|
||||
// path, so a Go program opened another file than it asked for.)
|
||||
#define GOSYS_SLOTS 64u
|
||||
#define GOSYS_SLOT_SIZE 32768u // two paths (4K each), an argv array (8K), an environment array (16K)
|
||||
#define GOSYS_STATE_SIZE 1024u // [4] rewriting mode, [8] execve calls on their way, [64 + 8 * slot] who has the slot (0: nobody)
|
||||
#define GOSYS_SCRATCH_SIZE (GOSYS_STATE_SIZE + GOSYS_SLOTS * GOSYS_SLOT_SIZE)
|
||||
|
||||
#define GOSYS_THUNK_BYTES 256u // room for the small code that connects a call site to the code
|
||||
|
||||
#define GOSYS_ROOT_MAX 448
|
||||
#define GOSYS_PATH_MAX 448
|
||||
#define GOSYS_LIBS_MAX 1024
|
||||
|
||||
struct gosys_hdr {
|
||||
char magic[8]; // "CXGOSYS1"
|
||||
uint32_t version; // GOSYS_VERSION
|
||||
uint32_t flags;
|
||||
int64_t scratch_rel; // scratch memory, relative to this header (the same in any load address)
|
||||
uint32_t root_len; // the rootfs, as Android sees it
|
||||
uint32_t ldso_len; // the rootfs's dynamic linker
|
||||
uint32_t hook_len; // libddnatv-hook.so; 0: none
|
||||
uint32_t libs_len; // the rootfs's library directories, ':' separated
|
||||
uint32_t runner_len; // the rootfs's bash, or env without it; 0: Ubuntu programs cannot be started
|
||||
uint32_t reserved;
|
||||
char root[GOSYS_ROOT_MAX];
|
||||
char ldso[GOSYS_PATH_MAX];
|
||||
char hook[GOSYS_PATH_MAX];
|
||||
char libs[GOSYS_LIBS_MAX];
|
||||
char runner[GOSYS_PATH_MAX];
|
||||
};
|
||||
|
||||
#define GOSYS_FLAG_RUNNER_IS_BASH 1u // gosys_hdr.flags
|
||||
|
||||
// The last 64 bytes of the added code: where it is, and what the patcher changed to add it.
|
||||
struct gosys_trailer {
|
||||
char magic[8]; // "CXGOEND1"
|
||||
uint32_t version;
|
||||
uint32_t total_size; // the added code, header to trailer
|
||||
uint64_t text_orig_filesz; // the segment that holds it: its size before
|
||||
uint64_t rw_orig_memsz; // the data segment: its size before
|
||||
uint32_t text_index; // their numbers in the program header table
|
||||
uint32_t rw_index;
|
||||
uint32_t text_orig_flags;
|
||||
uint32_t reserved;
|
||||
uint64_t site_file_off; // the instruction that was replaced (a "svc #0")
|
||||
uint64_t reserved2;
|
||||
};
|
||||
|
||||
_Static_assert(sizeof(struct gosys_trailer) == 64, "the trailer is 64 bytes");
|
||||
_Static_assert(sizeof(struct gosys_hdr) % 16 == 0, "the header keeps the code after it aligned");
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,309 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
||||
// parts share their static functions and are built with the same command as before).
|
||||
//
|
||||
// The environment of started programs: Android programs get it without the hook, rootfs
|
||||
// programs with it.
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
|
||||
static const char *ddnatv_hook_library(char *buf, size_t bufsz);
|
||||
|
||||
static char **clean_env_for_system(char *const envp[]) {
|
||||
int count = 0;
|
||||
while (envp && envp[count]) count++;
|
||||
char **new_env = calloc(count + 3, sizeof(char *));
|
||||
int dst = 0;
|
||||
int has_path = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
char first = envp[i][0];
|
||||
if (!((first == 'L' && (strncmp(envp[i], "LD_PRELOAD=", 11) == 0 ||
|
||||
strncmp(envp[i], "LD_LIBRARY_PATH=", 16) == 0)) ||
|
||||
(first == 'G' && strncmp(envp[i], "GLIBC_TUNABLES=", 15) == 0))) {
|
||||
if (first == 'P' && strncmp(envp[i], "PATH=", 5) == 0) {
|
||||
has_path = 1;
|
||||
size_t plen = strlen(envp[i]);
|
||||
char *new_path = malloc(plen + 64);
|
||||
if (new_path) {
|
||||
snprintf(new_path, plen + 64, "PATH=/system/bin:/system/xbin:/sbin:/vendor/bin:%s", envp[i] + 5);
|
||||
new_env[dst++] = new_path;
|
||||
} else {
|
||||
new_env[dst++] = envp[i];
|
||||
}
|
||||
} else {
|
||||
new_env[dst++] = envp[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!has_path) {
|
||||
new_env[dst++] = strdup("PATH=/system/bin:/system/xbin:/sbin:/vendor/bin");
|
||||
}
|
||||
new_env[dst] = NULL;
|
||||
return new_env;
|
||||
}
|
||||
|
||||
// LD_PRELOAD with every absolute path that is not in the rootfs yet mapped into it. Entries are
|
||||
// separated by ':' or ' '. free() the result.
|
||||
static char *map_preload_paths(const char *value) {
|
||||
if (!value) return NULL;
|
||||
size_t cap = strlen(value) + (g_ddnatv_root_len + 1) * 16 + 16;
|
||||
char *out = malloc(cap);
|
||||
if (!out) return NULL;
|
||||
size_t used = 0;
|
||||
out[0] = '\0';
|
||||
const char *p = value;
|
||||
while (*p) {
|
||||
size_t len = strcspn(p, ": ");
|
||||
if (len > 0 && len < PATH_MAX) {
|
||||
char entry[PATH_MAX];
|
||||
memcpy(entry, p, len);
|
||||
entry[len] = '\0';
|
||||
char buf[PATH_MAX];
|
||||
const char *mapped = entry[0] == '/' ? rewrite_path_in_root(entry, buf, sizeof(buf)) : entry;
|
||||
size_t mlen = strlen(mapped);
|
||||
if (used + mlen + 2 >= cap) break;
|
||||
if (used) out[used++] = ':';
|
||||
memcpy(out + used, mapped, mlen);
|
||||
used += mlen;
|
||||
out[used] = '\0';
|
||||
}
|
||||
p += len;
|
||||
if (*p) p++;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static char **prepare_ddnatv_env(char *const envp[], const char *real_exe) {
|
||||
init_ddnatv_hook();
|
||||
int count = 0;
|
||||
int has_preload = 0;
|
||||
int has_ld_library_path = 0;
|
||||
int has_root = 0;
|
||||
int has_tunables = 0;
|
||||
int has_path = 0;
|
||||
int has_tmp = 0;
|
||||
int has_threads_max = 0;
|
||||
int has_xz_opt = 0;
|
||||
int has_xz_defaults = 0;
|
||||
int has_frontend = 0;
|
||||
int has_debconf_frontend = 0;
|
||||
int has_debconf_seen = 0;
|
||||
|
||||
const char *chosen_real_exe = (real_exe && real_exe[0] != '\0') ? real_exe : (g_real_exe[0] != '\0' ? g_real_exe : NULL);
|
||||
|
||||
// Every variable is looked at on every exec, so only the names that start with its first letter
|
||||
// are compared (this was 13 strncmp() calls for each of the 30 or so variables)
|
||||
int has_nt = 0;
|
||||
int has_binds = 0;
|
||||
int has_hostname = 0;
|
||||
int has_link_root = 0;
|
||||
int has_preload_list = 0;
|
||||
const char *preload_value = NULL;
|
||||
while (envp && envp[count]) {
|
||||
const char *e = envp[count];
|
||||
switch (e[0]) {
|
||||
case 'L':
|
||||
if (strncmp(e, "LD_PRELOAD=", 11) == 0) { has_preload = 1; preload_value = e + 11; }
|
||||
else if (strncmp(e, "LD_LIBRARY_PATH=", 16) == 0) has_ld_library_path = 1;
|
||||
break;
|
||||
case 'G':
|
||||
if (strncmp(e, "GLIBC_TUNABLES=", 15) == 0) has_tunables = 1;
|
||||
break;
|
||||
case 'P':
|
||||
if (strncmp(e, "PATH=", 5) == 0) has_path = 1;
|
||||
break;
|
||||
case 'T':
|
||||
if (strncmp(e, "TMPDIR=", 7) == 0) has_tmp = 1;
|
||||
break;
|
||||
case 'X':
|
||||
if (strncmp(e, "XZ_OPT=", 7) == 0) has_xz_opt = 1;
|
||||
else if (strncmp(e, "XZ_DEFAULTS=", 12) == 0) has_xz_defaults = 1;
|
||||
break;
|
||||
case 'D':
|
||||
if (strncmp(e, "DDNATV_ROOT=", 12) == 0) has_root = 1;
|
||||
else if (strncmp(e, "DDNATV_NT=", 10) == 0) has_nt = 1;
|
||||
else if (strncmp(e, "DDNATV_BINDS=", 13) == 0) has_binds = 1;
|
||||
else if (strncmp(e, "DDNATV_PRELOAD=", 15) == 0) has_preload_list = 1;
|
||||
else if (strncmp(e, "DDNATV_HOSTNAME=", 16) == 0) has_hostname = 1;
|
||||
else if (strncmp(e, "DDNATV_LINK_ROOT=", 17) == 0) has_link_root = 1;
|
||||
else if (strncmp(e, "DPKG_DEB_THREADS_MAX=", 21) == 0) has_threads_max = 1;
|
||||
else if (strncmp(e, "DEBIAN_FRONTEND=", 16) == 0) has_frontend = 1;
|
||||
else if (strncmp(e, "DEBCONF_FRONTEND=", 17) == 0) has_debconf_frontend = 1;
|
||||
else if (strncmp(e, "DEBCONF_NONINTERACTIVE_SEEN=", 28) == 0) has_debconf_seen = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
|
||||
char **new_env = calloc(count + 24, sizeof(char *));
|
||||
int dst = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (has_preload && envp[i][0] == 'L' && strncmp(envp[i], "LD_PRELOAD=", 11) == 0) {
|
||||
// ld.so opens these itself: the guest's paths are mapped to the rootfs, and the libraries every
|
||||
// program loads (DDNATV_PRELOAD) come first
|
||||
char *mapped = map_preload_paths(preload_value);
|
||||
if (mapped) {
|
||||
size_t chain = g_preload_host ? strlen(g_preload_host) : 0;
|
||||
int has_chain = chain > 0 && strstr(mapped, g_preload_host) != NULL;
|
||||
size_t len = 11 + chain + 1 + strlen(mapped) + 1;
|
||||
char *str = malloc(len);
|
||||
if (str) {
|
||||
if (chain > 0 && !has_chain) snprintf(str, len, "LD_PRELOAD=%s:%s", g_preload_host, mapped);
|
||||
else snprintf(str, len, "LD_PRELOAD=%s", mapped);
|
||||
new_env[dst++] = str;
|
||||
free(mapped);
|
||||
continue;
|
||||
}
|
||||
free(mapped);
|
||||
}
|
||||
}
|
||||
// If we have an explicit real executable, drop the inherited entry to replace with the fresh target
|
||||
if (chosen_real_exe && envp[i][0] == 'D' && strncmp(envp[i], "DDNATV_REAL_EXE=", 16) == 0) {
|
||||
continue;
|
||||
}
|
||||
new_env[dst++] = envp[i];
|
||||
}
|
||||
|
||||
// Exact-size strings: this runs before every exec, often in a freshly forked child where each
|
||||
// newly touched page is a copy-on-write fault.
|
||||
if (chosen_real_exe) {
|
||||
size_t len = strlen(chosen_real_exe);
|
||||
char *str = malloc(16 + len + 1);
|
||||
if (str) {
|
||||
memcpy(str, "DDNATV_REAL_EXE=", 16);
|
||||
memcpy(str + 16, chosen_real_exe, len + 1);
|
||||
new_env[dst++] = str;
|
||||
}
|
||||
}
|
||||
|
||||
char hook_buf[PATH_MAX];
|
||||
const char *hook = (!has_preload && g_ddnatv_root[0] != '\0') ? ddnatv_hook_library(hook_buf, sizeof(hook_buf)) : NULL;
|
||||
if (hook) {
|
||||
size_t hook_len = strlen(hook);
|
||||
size_t host_len = g_preload_host ? strlen(g_preload_host) + 1 : 0;
|
||||
size_t size = 11 + host_len + hook_len + 1;
|
||||
char *str = malloc(size);
|
||||
if (str) {
|
||||
size_t n = ddnatv_append(str, size, 0, "LD_PRELOAD=", 11);
|
||||
if (host_len) {
|
||||
n = ddnatv_append(str, size, n, g_preload_host, host_len - 1);
|
||||
n = ddnatv_append(str, size, n, ":", 1);
|
||||
}
|
||||
ddnatv_append(str, size, n, hook, hook_len);
|
||||
new_env[dst++] = str;
|
||||
}
|
||||
}
|
||||
if (!has_preload_list && g_preload_guest) {
|
||||
size_t len = strlen(g_preload_guest);
|
||||
char *str = malloc(15 + len + 1);
|
||||
if (str) {
|
||||
memcpy(str, "DDNATV_PRELOAD=", 15);
|
||||
memcpy(str + 15, g_preload_guest, len + 1);
|
||||
new_env[dst++] = str;
|
||||
}
|
||||
}
|
||||
if (!has_ld_library_path && g_ddnatv_root[0] != '\0') {
|
||||
size_t size = 16 + 1;
|
||||
for (size_t i = 0; i < sizeof(k_rootfs_lib_dirs) / sizeof(k_rootfs_lib_dirs[0]); i++) {
|
||||
char dir[PATH_MAX];
|
||||
size += 1 + strlen(rewrite_path_in_root(k_rootfs_lib_dirs[i], dir, sizeof(dir)));
|
||||
}
|
||||
char *str = malloc(size);
|
||||
if (str) {
|
||||
size_t n = ddnatv_append(str, size, 0, "LD_LIBRARY_PATH=", 16);
|
||||
append_rootfs_lib_dirs(str, size, n, 0);
|
||||
new_env[dst++] = str;
|
||||
}
|
||||
}
|
||||
if (!has_root && g_ddnatv_root[0] != '\0') {
|
||||
char *str = malloc(PATH_MAX + 16);
|
||||
if (str) {
|
||||
snprintf(str, PATH_MAX + 16, "DDNATV_ROOT=%s", g_ddnatv_root);
|
||||
new_env[dst++] = str;
|
||||
}
|
||||
}
|
||||
if (!has_link_root && g_link_root_len) {
|
||||
char *str = malloc(17 + g_link_root_len + 1);
|
||||
if (str) {
|
||||
memcpy(str, "DDNATV_LINK_ROOT=", 17);
|
||||
memcpy(str + 17, g_link_root, g_link_root_len + 1);
|
||||
new_env[dst++] = str;
|
||||
}
|
||||
}
|
||||
if (!has_hostname && g_hostname[0]) {
|
||||
char *str = malloc(16 + sizeof(g_hostname));
|
||||
if (str) {
|
||||
snprintf(str, 16 + sizeof(g_hostname), "DDNATV_HOSTNAME=%s", g_hostname);
|
||||
new_env[dst++] = str;
|
||||
}
|
||||
}
|
||||
if (!has_binds && g_binds_raw) {
|
||||
size_t len = strlen(g_binds_raw);
|
||||
char *str = malloc(13 + len + 1);
|
||||
if (str) {
|
||||
memcpy(str, "DDNATV_BINDS=", 13);
|
||||
memcpy(str + 13, g_binds_raw, len + 1);
|
||||
new_env[dst++] = str;
|
||||
}
|
||||
}
|
||||
// This process looked for the ARM translator's files and found none: the programs it starts
|
||||
// need not look again (it takes a look at /proc/self/fd in each of them, see privileged.c)
|
||||
if (!has_nt && g_no_translator) {
|
||||
new_env[dst++] = strdup("DDNATV_NT=0");
|
||||
}
|
||||
if (!has_tunables) {
|
||||
new_env[dst++] = strdup("GLIBC_TUNABLES=glibc.pthread.rseq=0");
|
||||
}
|
||||
if (!has_path) {
|
||||
new_env[dst++] = strdup("PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
|
||||
}
|
||||
if (!has_tmp && g_ddnatv_root[0] != '\0') {
|
||||
char *str = malloc(PATH_MAX + 16);
|
||||
if (str) {
|
||||
snprintf(str, PATH_MAX + 16, "TMPDIR=%s/tmp", g_ddnatv_root);
|
||||
new_env[dst++] = str;
|
||||
}
|
||||
}
|
||||
if (!has_threads_max) {
|
||||
new_env[dst++] = strdup("DPKG_DEB_THREADS_MAX=1");
|
||||
}
|
||||
if (!has_xz_opt) {
|
||||
new_env[dst++] = strdup("XZ_OPT=-T1");
|
||||
}
|
||||
if (!has_xz_defaults) {
|
||||
new_env[dst++] = strdup("XZ_DEFAULTS=-T1");
|
||||
}
|
||||
if (!has_frontend) {
|
||||
new_env[dst++] = strdup("DEBIAN_FRONTEND=noninteractive");
|
||||
}
|
||||
if (!has_debconf_frontend) {
|
||||
new_env[dst++] = strdup("DEBCONF_FRONTEND=noninteractive");
|
||||
}
|
||||
if (!has_debconf_seen) {
|
||||
new_env[dst++] = strdup("DEBCONF_NONINTERACTIVE_SEEN=true");
|
||||
}
|
||||
new_env[dst] = NULL;
|
||||
return new_env;
|
||||
}
|
||||
|
||||
static void free_modified_env(char **modified_env, char *const orig_env[]) {
|
||||
if (!modified_env) return;
|
||||
for (int i = 0; modified_env[i] != NULL; i++) {
|
||||
int is_orig = 0;
|
||||
if (orig_env) {
|
||||
for (int j = 0; orig_env[j] != NULL; j++) {
|
||||
if (modified_env[i] == orig_env[j]) {
|
||||
is_orig = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!is_orig) {
|
||||
free(modified_env[i]);
|
||||
}
|
||||
}
|
||||
free(modified_env);
|
||||
}
|
||||
|
||||
#endif /* !DDNATV_HOST_TEST */
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,805 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
||||
// parts share their static functions and are built with the same command as before).
|
||||
//
|
||||
// Other file operations: access, chmod, unlink, mkdir, rename, readlink, extended attributes,
|
||||
// links, times, truncate, statfs, the working directory, realpath and scandir.
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
|
||||
// Hook faccessat
|
||||
int faccessat(int dirfd, const char *pathname, int mode, int flags) {
|
||||
init_ddnatv_hook();
|
||||
if (!pathname) {
|
||||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
|
||||
struct stat st;
|
||||
int stat_flags = (flags & AT_SYMLINK_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0;
|
||||
if (fstatat(dirfd, pathname, &st, stat_flags) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (mode == F_OK) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (mode & X_OK) {
|
||||
if (S_ISDIR(st.st_mode)) return 0;
|
||||
if ((st.st_mode & 0111) != 0) return 0;
|
||||
if (strstr(pathname, "/bin/") || strstr(pathname, "/sbin/")) return 0;
|
||||
errno = EACCES;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int faccessat2(int dirfd, const char *pathname, int mode, int flags) {
|
||||
return faccessat(dirfd, pathname, mode, flags);
|
||||
}
|
||||
|
||||
// Hook access
|
||||
int access(const char *pathname, int mode) {
|
||||
return faccessat(AT_FDCWD, pathname, mode, 0);
|
||||
}
|
||||
|
||||
// Hook euidaccess and eaccess
|
||||
int euidaccess(const char *pathname, int mode) {
|
||||
return faccessat(AT_FDCWD, pathname, mode, AT_EACCESS);
|
||||
}
|
||||
|
||||
int eaccess(const char *pathname, int mode) {
|
||||
return faccessat(AT_FDCWD, pathname, mode, AT_EACCESS);
|
||||
}
|
||||
|
||||
// Hook chmod
|
||||
int chmod(const char *pathname, mode_t mode) {
|
||||
static int (*orig_chmod)(const char *, mode_t) = NULL;
|
||||
if (!orig_chmod) orig_chmod = (int (*)(const char *, mode_t))dlsym(RTLD_NEXT, "chmod");
|
||||
if (!orig_chmod) return -1;
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path_follow(pathname, buf, sizeof(buf));
|
||||
int ret = orig_chmod(target, mode);
|
||||
if (ret != 0 && ((mode & 06000) != 0 || errno == EPERM || errno == EACCES || errno == ENOSYS)) {
|
||||
ret = orig_chmod(target, mode & 01777);
|
||||
}
|
||||
if (ret != 0 && (errno == EPERM || errno == EACCES)) {
|
||||
errno = 0;
|
||||
return 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook fchmod
|
||||
int fchmod(int fd, mode_t mode) {
|
||||
static int (*orig_fchmod)(int, mode_t) = NULL;
|
||||
if (!orig_fchmod) orig_fchmod = (int (*)(int, mode_t))dlsym(RTLD_NEXT, "fchmod");
|
||||
if (!orig_fchmod) return -1;
|
||||
int ret = orig_fchmod(fd, mode);
|
||||
if (ret != 0 && ((mode & 06000) != 0 || errno == EPERM || errno == EACCES || errno == ENOSYS)) {
|
||||
ret = orig_fchmod(fd, mode & 01777);
|
||||
}
|
||||
if (ret != 0 && (errno == EPERM || errno == EACCES)) {
|
||||
errno = 0;
|
||||
return 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook fchmodat
|
||||
int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags) {
|
||||
static int (*orig_fchmodat)(int, const char *, mode_t, int) = NULL;
|
||||
if (!orig_fchmodat) orig_fchmodat = (int (*)(int, const char *, mode_t, int))dlsym(RTLD_NEXT, "fchmodat");
|
||||
if (!orig_fchmodat) return -1;
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
char buf[PATH_MAX];
|
||||
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
|
||||
int ret = orig_fchmodat(dirfd, target, mode, flags);
|
||||
if (ret != 0 && ((mode & 06000) != 0 || errno == EPERM || errno == EACCES || errno == ENOSYS)) {
|
||||
ret = orig_fchmodat(dirfd, target, mode & 01777, flags);
|
||||
}
|
||||
if (ret != 0 && (errno == EPERM || errno == EACCES)) {
|
||||
errno = 0;
|
||||
return 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook unlink
|
||||
int unlink(const char *pathname) {
|
||||
static int (*orig_unlink)(const char *) = NULL;
|
||||
if (!orig_unlink) orig_unlink = (int (*)(const char *))dlsym(RTLD_NEXT, "unlink");
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(pathname, buf, sizeof(buf));
|
||||
unlink_mapped_unix_socket(target);
|
||||
dev_t hl_dev = 0;
|
||||
ino_t hl_ino = 0;
|
||||
int hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, target, &hl_dev, &hl_ino);
|
||||
int ret = orig_unlink(target);
|
||||
if (ret != 0 && refused_in_repaired_dir(AT_FDCWD, target)) {
|
||||
// The name could not be looked up while its directory was closed
|
||||
hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, target, &hl_dev, &hl_ino);
|
||||
ret = orig_unlink(target);
|
||||
}
|
||||
if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook unlinkat
|
||||
int unlinkat(int dirfd, const char *pathname, int flags) {
|
||||
static int (*orig_unlinkat)(int, const char *, int) = NULL;
|
||||
if (!orig_unlinkat) orig_unlinkat = (int (*)(int, const char *, int))dlsym(RTLD_NEXT, "unlinkat");
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
char buf[PATH_MAX];
|
||||
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
|
||||
dev_t hl_dev = 0;
|
||||
ino_t hl_ino = 0;
|
||||
int hardlinked = 0;
|
||||
if (target) {
|
||||
unlink_mapped_unix_socket(target);
|
||||
hardlinked = lookup_emulated_hardlink_at(dirfd, target, &hl_dev, &hl_ino);
|
||||
}
|
||||
int ret = orig_unlinkat ? orig_unlinkat(dirfd, target, flags) : -1;
|
||||
if (ret != 0 && orig_unlinkat && refused_in_repaired_dir(dirfd, target)) {
|
||||
hardlinked = target ? lookup_emulated_hardlink_at(dirfd, target, &hl_dev, &hl_ino) : 0;
|
||||
ret = orig_unlinkat(dirfd, target, flags);
|
||||
}
|
||||
if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook rmdir
|
||||
int rmdir(const char *pathname) {
|
||||
static int (*orig_rmdir)(const char *) = NULL;
|
||||
if (!orig_rmdir) orig_rmdir = (int (*)(const char *))dlsym(RTLD_NEXT, "rmdir");
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(pathname, buf, sizeof(buf));
|
||||
int ret = orig_rmdir(target);
|
||||
if (ret != 0 && refused_in_repaired_dir(AT_FDCWD, target)) ret = orig_rmdir(target);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// glibc's remove() calls its internal unlinkat, which no preload can see: GLib's g_file_delete()
|
||||
// got ENOENT for every guest path, and Flatpak could not replace an app's "current" link
|
||||
int remove(const char *pathname) {
|
||||
if (unlink(pathname) == 0) return 0;
|
||||
if (errno != EISDIR) return -1;
|
||||
return rmdir(pathname);
|
||||
}
|
||||
|
||||
// glob() and nftw() walk directories with glibc's internal calls too. glob() is given the hook's
|
||||
// own through GLOB_ALTDIRFUNC; nftw() walks the mapped directory and its callback is given guest paths.
|
||||
static int glob_alt_stat(const char *path, void *st) { return stat(path, (struct stat *)st); }
|
||||
static int glob_alt_lstat(const char *path, void *st) { return lstat(path, (struct stat *)st); }
|
||||
static void *glob_alt_opendir(const char *path) { return opendir(path); }
|
||||
static void *glob_alt_readdir(void *dir) { return readdir((DIR *)dir); }
|
||||
static void glob_alt_closedir(void *dir) { closedir((DIR *)dir); }
|
||||
|
||||
int glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob) {
|
||||
static int (*orig_glob)(const char *, int, int (*)(const char *, int), glob_t *) = NULL;
|
||||
if (!orig_glob) orig_glob = (int (*)(const char *, int, int (*)(const char *, int), glob_t *))dlsym(RTLD_NEXT, "glob");
|
||||
if (pglob && !(flags & GLOB_ALTDIRFUNC)) {
|
||||
init_ddnatv_hook();
|
||||
pglob->gl_closedir = glob_alt_closedir;
|
||||
pglob->gl_readdir = (struct dirent *(*)(void *))glob_alt_readdir;
|
||||
pglob->gl_opendir = glob_alt_opendir;
|
||||
pglob->gl_lstat = (int (*)(const char *, struct stat *))glob_alt_lstat;
|
||||
pglob->gl_stat = (int (*)(const char *, struct stat *))glob_alt_stat;
|
||||
flags |= GLOB_ALTDIRFUNC;
|
||||
}
|
||||
return orig_glob ? orig_glob(pattern, flags, errfunc, pglob) : GLOB_NOSPACE;
|
||||
}
|
||||
|
||||
int glob64(const char *pattern, int flags, int (*errfunc)(const char *, int), glob64_t *pglob) {
|
||||
return glob(pattern, flags, errfunc, (glob_t *)pglob);
|
||||
}
|
||||
|
||||
static __thread struct {
|
||||
const char *host, *guest;
|
||||
size_t host_len, guest_len;
|
||||
int (*nftw_fn)(const char *, const struct stat *, int, struct FTW *);
|
||||
int (*ftw_fn)(const char *, const struct stat *, int);
|
||||
} t_walk;
|
||||
|
||||
static const char *walk_guest_path(const char *path, char *buf, size_t size, int *base) {
|
||||
if (!t_walk.host || strncmp(path, t_walk.host, t_walk.host_len) != 0) return path;
|
||||
const char *rest = path + t_walk.host_len;
|
||||
if (*rest != '\0' && *rest != '/') return path;
|
||||
// a walk of "/" gives "/etc", not "//etc"
|
||||
size_t guest_len = (t_walk.guest_len == 1 && *rest == '/') ? 0 : t_walk.guest_len;
|
||||
size_t n = ddnatv_append(buf, size, 0, t_walk.guest, guest_len);
|
||||
ddnatv_append(buf, size, n, rest, strlen(rest));
|
||||
if (base) *base = *base - (int)t_walk.host_len + (int)guest_len;
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int walk_nftw_cb(const char *path, const struct stat *st, int type, struct FTW *ftwbuf) {
|
||||
char buf[PATH_MAX];
|
||||
struct FTW guest_ftw = *ftwbuf;
|
||||
return t_walk.nftw_fn(walk_guest_path(path, buf, sizeof(buf), &guest_ftw.base), st, type, &guest_ftw);
|
||||
}
|
||||
|
||||
static int walk_ftw_cb(const char *path, const struct stat *st, int type) {
|
||||
char buf[PATH_MAX];
|
||||
return t_walk.ftw_fn(walk_guest_path(path, buf, sizeof(buf), NULL), st, type);
|
||||
}
|
||||
|
||||
int nftw(const char *dir, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fds, int flags) {
|
||||
static int (*orig_nftw)(const char *, int (*)(const char *, const struct stat *, int, struct FTW *), int, int) = NULL;
|
||||
if (!orig_nftw) orig_nftw = (int (*)(const char *, int (*)(const char *, const struct stat *, int, struct FTW *), int, int))dlsym(RTLD_NEXT, "nftw");
|
||||
if (!orig_nftw) { errno = ENOSYS; return -1; }
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(dir, buf, sizeof(buf));
|
||||
if (target == dir || !fn) return orig_nftw(dir, fn, fds, flags);
|
||||
__typeof__(t_walk) saved = t_walk;
|
||||
t_walk.host = target; t_walk.host_len = strlen(target);
|
||||
while (t_walk.host_len > 1 && target[t_walk.host_len - 1] == '/') t_walk.host_len--;
|
||||
t_walk.guest = dir; t_walk.guest_len = strlen(dir);
|
||||
while (t_walk.guest_len > 1 && dir[t_walk.guest_len - 1] == '/') t_walk.guest_len--;
|
||||
t_walk.nftw_fn = fn;
|
||||
int ret = orig_nftw(target, walk_nftw_cb, fds, flags);
|
||||
t_walk = saved;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int nftw64(const char *dir, int (*fn)(const char *, const struct stat64 *, int, struct FTW *), int fds, int flags) {
|
||||
return nftw(dir, (int (*)(const char *, const struct stat *, int, struct FTW *))fn, fds, flags);
|
||||
}
|
||||
|
||||
int ftw(const char *dir, int (*fn)(const char *, const struct stat *, int), int fds) {
|
||||
static int (*orig_ftw)(const char *, int (*)(const char *, const struct stat *, int), int) = NULL;
|
||||
if (!orig_ftw) orig_ftw = (int (*)(const char *, int (*)(const char *, const struct stat *, int), int))dlsym(RTLD_NEXT, "ftw");
|
||||
if (!orig_ftw) { errno = ENOSYS; return -1; }
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(dir, buf, sizeof(buf));
|
||||
if (target == dir || !fn) return orig_ftw(dir, fn, fds);
|
||||
__typeof__(t_walk) saved = t_walk;
|
||||
t_walk.host = target; t_walk.host_len = strlen(target);
|
||||
while (t_walk.host_len > 1 && target[t_walk.host_len - 1] == '/') t_walk.host_len--;
|
||||
t_walk.guest = dir; t_walk.guest_len = strlen(dir);
|
||||
while (t_walk.guest_len > 1 && dir[t_walk.guest_len - 1] == '/') t_walk.guest_len--;
|
||||
t_walk.ftw_fn = fn;
|
||||
int ret = orig_ftw(target, walk_ftw_cb, fds);
|
||||
t_walk = saved;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ftw64(const char *dir, int (*fn)(const char *, const struct stat64 *, int), int fds) {
|
||||
return ftw(dir, (int (*)(const char *, const struct stat *, int))fn, fds);
|
||||
}
|
||||
|
||||
// The app is root for the programs it runs, and root enters and writes every directory whatever its
|
||||
// mode; the app does not. dpkg makes each new directory with mode 0 and sets the mode a moment later,
|
||||
// and a chmod that fails is reported as done (see chmod), which left directories nobody could put a
|
||||
// file in: "Permission denied" in /usr/share/applications and others (#44). So a directory always
|
||||
// keeps its owner's read, write and search rights.
|
||||
#define NEW_DIR_OWNER_RIGHTS S_IRWXU
|
||||
|
||||
// Hook mkdir
|
||||
int mkdir(const char *pathname, mode_t mode) {
|
||||
static int (*orig_mkdir)(const char *, mode_t) = NULL;
|
||||
if (!orig_mkdir) orig_mkdir = (int (*)(const char *, mode_t))dlsym(RTLD_NEXT, "mkdir");
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(pathname, buf, sizeof(buf));
|
||||
int ret = orig_mkdir(target, mode | NEW_DIR_OWNER_RIGHTS);
|
||||
if (ret != 0 && refused_in_repaired_dir(AT_FDCWD, target)) ret = orig_mkdir(target, mode | NEW_DIR_OWNER_RIGHTS);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook mkdirat
|
||||
int mkdirat(int dirfd, const char *pathname, mode_t mode) {
|
||||
static int (*orig_mkdirat)(int, const char *, mode_t) = NULL;
|
||||
if (!orig_mkdirat) orig_mkdirat = (int (*)(int, const char *, mode_t))dlsym(RTLD_NEXT, "mkdirat");
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
char buf[PATH_MAX];
|
||||
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
|
||||
int ret = orig_mkdirat(dirfd, target, mode | NEW_DIR_OWNER_RIGHTS);
|
||||
if (ret != 0 && refused_in_repaired_dir(dirfd, target)) ret = orig_mkdirat(dirfd, target, mode | NEW_DIR_OWNER_RIGHTS);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void ensure_dir_exists(const char *path) {
|
||||
if (!path || path[0] == '\0') return;
|
||||
char tmp[PATH_MAX];
|
||||
strncpy(tmp, path, sizeof(tmp) - 1);
|
||||
tmp[sizeof(tmp) - 1] = '\0';
|
||||
for (char *p = tmp + 1; *p; p++) {
|
||||
if (*p == '/') {
|
||||
*p = '\0';
|
||||
mkdir(tmp, 0755);
|
||||
*p = '/';
|
||||
}
|
||||
}
|
||||
mkdir(tmp, 0755);
|
||||
}
|
||||
|
||||
// Hook inotify_add_watch
|
||||
int inotify_add_watch(int fd, const char *pathname, uint32_t mask) {
|
||||
static int (*orig_inotify_add_watch)(int, const char *, uint32_t) = NULL;
|
||||
if (!orig_inotify_add_watch) orig_inotify_add_watch = (int (*)(int, const char *, uint32_t))dlsym(RTLD_NEXT, "inotify_add_watch");
|
||||
if (!pathname) return -1;
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(pathname, buf, sizeof(buf));
|
||||
if (!orig_inotify_add_watch) return -1;
|
||||
int wd = orig_inotify_add_watch(fd, target, mask);
|
||||
if (wd < 0 && errno == ENOENT && (mask & IN_ONLYDIR)) {
|
||||
ensure_dir_exists(target);
|
||||
wd = orig_inotify_add_watch(fd, target, mask);
|
||||
}
|
||||
return wd;
|
||||
}
|
||||
|
||||
// Hook rename
|
||||
int rename(const char *oldpath, const char *newpath) {
|
||||
static int (*orig_rename)(const char *, const char *) = NULL;
|
||||
if (!orig_rename) orig_rename = (int (*)(const char *, const char *))dlsym(RTLD_NEXT, "rename");
|
||||
char buf1[PATH_MAX];
|
||||
char buf2[PATH_MAX];
|
||||
const char *target_old = rewrite_path(oldpath, buf1, sizeof(buf1));
|
||||
const char *target_new = rewrite_path(newpath, buf2, sizeof(buf2));
|
||||
dev_t hl_dev = 0;
|
||||
ino_t hl_ino = 0;
|
||||
int hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, target_new, &hl_dev, &hl_ino);
|
||||
int ret = orig_rename(target_old, target_new);
|
||||
if (ret != 0 && errno == EACCES) {
|
||||
// Both directories: the old name is taken out of one, the new put in the other
|
||||
int refused = errno;
|
||||
int fixed_old = refused_in_repaired_dir(AT_FDCWD, target_old);
|
||||
errno = refused;
|
||||
int fixed_new = refused_in_repaired_dir(AT_FDCWD, target_new);
|
||||
if (fixed_old || fixed_new) {
|
||||
hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, target_new, &hl_dev, &hl_ino);
|
||||
ret = orig_rename(target_old, target_new);
|
||||
}
|
||||
}
|
||||
if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook renameat
|
||||
int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) {
|
||||
static int (*orig_renameat)(int, const char *, int, const char *) = NULL;
|
||||
if (!orig_renameat) orig_renameat = (int (*)(int, const char *, int, const char *))dlsym(RTLD_NEXT, "renameat");
|
||||
DDNATV_AT_REMAP_NAMED(olddirfd, oldpath, ddnatv_at_old);
|
||||
DDNATV_AT_REMAP_NAMED(newdirfd, newpath, ddnatv_at_new);
|
||||
char buf1[PATH_MAX];
|
||||
char buf2[PATH_MAX];
|
||||
const char *target_old = (oldpath && oldpath[0] == '/') ? rewrite_path(oldpath, buf1, sizeof(buf1)) : oldpath;
|
||||
const char *target_new = (newpath && newpath[0] == '/') ? rewrite_path(newpath, buf2, sizeof(buf2)) : newpath;
|
||||
dev_t hl_dev = 0;
|
||||
ino_t hl_ino = 0;
|
||||
int hardlinked = lookup_emulated_hardlink_at(newdirfd, target_new, &hl_dev, &hl_ino);
|
||||
int ret = orig_renameat(olddirfd, target_old, newdirfd, target_new);
|
||||
if (ret != 0 && errno == EACCES) {
|
||||
int refused = errno;
|
||||
int fixed_old = refused_in_repaired_dir(olddirfd, target_old);
|
||||
errno = refused;
|
||||
int fixed_new = refused_in_repaired_dir(newdirfd, target_new);
|
||||
if (fixed_old || fixed_new) {
|
||||
hardlinked = lookup_emulated_hardlink_at(newdirfd, target_new, &hl_dev, &hl_ino);
|
||||
ret = orig_renameat(olddirfd, target_old, newdirfd, target_new);
|
||||
}
|
||||
}
|
||||
if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook renameat2
|
||||
int renameat2(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags) {
|
||||
static int (*orig_renameat2)(int, const char *, int, const char *, unsigned int) = NULL;
|
||||
if (!orig_renameat2) {
|
||||
orig_renameat2 = (int (*)(int, const char *, int, const char *, unsigned int))dlsym(RTLD_NEXT, "renameat2");
|
||||
if (!orig_renameat2) return renameat(olddirfd, oldpath, newdirfd, newpath);
|
||||
}
|
||||
DDNATV_AT_REMAP_NAMED(olddirfd, oldpath, ddnatv_at_old);
|
||||
DDNATV_AT_REMAP_NAMED(newdirfd, newpath, ddnatv_at_new);
|
||||
char buf1[PATH_MAX];
|
||||
char buf2[PATH_MAX];
|
||||
const char *target_old = (oldpath && oldpath[0] == '/') ? rewrite_path(oldpath, buf1, sizeof(buf1)) : oldpath;
|
||||
const char *target_new = (newpath && newpath[0] == '/') ? rewrite_path(newpath, buf2, sizeof(buf2)) : newpath;
|
||||
dev_t hl_dev = 0;
|
||||
ino_t hl_ino = 0;
|
||||
// RENAME_EXCHANGE keeps both files, so nothing is replaced.
|
||||
int hardlinked = !(flags & RENAME_EXCHANGE) && lookup_emulated_hardlink_at(newdirfd, target_new, &hl_dev, &hl_ino);
|
||||
int ret = orig_renameat2(olddirfd, target_old, newdirfd, target_new, flags);
|
||||
if (ret != 0 && errno == EACCES) {
|
||||
int refused = errno;
|
||||
int fixed_old = refused_in_repaired_dir(olddirfd, target_old);
|
||||
errno = refused;
|
||||
int fixed_new = refused_in_repaired_dir(newdirfd, target_new);
|
||||
if (fixed_old || fixed_new) {
|
||||
hardlinked = !(flags & RENAME_EXCHANGE) && lookup_emulated_hardlink_at(newdirfd, target_new, &hl_dev, &hl_ino);
|
||||
ret = orig_renameat2(olddirfd, target_old, newdirfd, target_new, flags);
|
||||
}
|
||||
}
|
||||
if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook readlink
|
||||
ssize_t readlink(const char *pathname, char *buf, size_t bufsiz) {
|
||||
if (is_proc_self_exe(pathname)) {
|
||||
ssize_t exe_len = handle_proc_self_exe(buf, bufsiz);
|
||||
if (exe_len >= 0) return exe_len;
|
||||
}
|
||||
static ssize_t (*orig_readlink)(const char *, char *, size_t) = NULL;
|
||||
if (!orig_readlink) orig_readlink = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink");
|
||||
char pbuf[PATH_MAX];
|
||||
const char *target = rewrite_path(pathname, pbuf, sizeof(pbuf));
|
||||
ssize_t ret = orig_readlink ? orig_readlink(target, buf, bufsiz) : -1;
|
||||
if (ret < 0 && errno == ENOENT) {
|
||||
char alt[PATH_MAX];
|
||||
const char *retry = usrmerge_retry_path(target, alt, sizeof(alt));
|
||||
if (retry) ret = orig_readlink(retry, buf, bufsiz);
|
||||
}
|
||||
return postprocess_readlink(pathname, buf, ret, bufsiz);
|
||||
}
|
||||
|
||||
// Hook readlinkat
|
||||
ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) {
|
||||
static ssize_t (*orig_readlinkat)(int, const char *, char *, size_t) = NULL;
|
||||
if (!orig_readlinkat) orig_readlinkat = (ssize_t (*)(int, const char *, char *, size_t))dlsym(RTLD_NEXT, "readlinkat");
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
// after the remap: "proc/self/exe" relative to a descriptor for "/" is /proc/self/exe as well
|
||||
if (is_proc_self_exe(pathname)) {
|
||||
ssize_t exe_len = handle_proc_self_exe(buf, bufsiz);
|
||||
if (exe_len >= 0) return exe_len;
|
||||
}
|
||||
char pbuf[PATH_MAX];
|
||||
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, pbuf, sizeof(pbuf)) : pathname;
|
||||
ssize_t ret = orig_readlinkat ? orig_readlinkat(dirfd, target, buf, bufsiz) : -1;
|
||||
if (ret < 0 && errno == ENOENT && pathname && pathname[0] == '/') {
|
||||
char alt[PATH_MAX];
|
||||
const char *retry = usrmerge_retry_path(target, alt, sizeof(alt));
|
||||
if (retry) ret = orig_readlinkat(dirfd, retry, buf, bufsiz);
|
||||
}
|
||||
return postprocess_readlink(pathname, buf, ret, bufsiz);
|
||||
}
|
||||
|
||||
// Fortified readlink variants used by dpkg / update-alternatives
|
||||
ssize_t __readlink_chk(const char *pathname, char *buf, size_t bufsiz, size_t buflen) {
|
||||
return readlink(pathname, buf, bufsiz);
|
||||
}
|
||||
|
||||
ssize_t __readlinkat_chk(int dirfd, const char *pathname, char *buf, size_t bufsiz, size_t buflen) {
|
||||
return readlinkat(dirfd, pathname, buf, bufsiz);
|
||||
}
|
||||
|
||||
// Ubuntu's programs find Android's SELinux (enforcing) through libselinux and then ask its policy
|
||||
// questions it has no answers for. The one that mattered: PAM's pam_rootok, which lets root
|
||||
// change its own entries, asks "may this user be root" and was refused, so chfn, chsh and
|
||||
// `adduser --gecos` (the installation of PostgreSQL) failed with "PAM: Authentication failure".
|
||||
// Ubuntu does not use SELinux, so for its programs it is off, as its labels are hidden below.
|
||||
int is_selinux_enabled(void) { return 0; }
|
||||
|
||||
// Extended attributes take the same paths as stat(). Without these hooks coreutils read
|
||||
// security.selinux with the Ubuntu path itself: `ls -l` printed an error line for every file
|
||||
// Android lacks (/etc/os-release) and showed Android's own label for the ones it has
|
||||
// (/etc/passwd). Ubuntu does not use SELinux, so its label is hidden altogether: `ls -l` looks as
|
||||
// on Ubuntu and cp/tar/rsync do not try to copy Android's labels.
|
||||
ssize_t getxattr(const char *path, const char *name, void *value, size_t size) {
|
||||
static ssize_t (*orig_getxattr)(const char *, const char *, void *, size_t) = NULL;
|
||||
if (!orig_getxattr) orig_getxattr = (ssize_t (*)(const char *, const char *, void *, size_t))dlsym(RTLD_NEXT, "getxattr");
|
||||
if (is_selinux_xattr(name)) {
|
||||
errno = ENODATA;
|
||||
return -1;
|
||||
}
|
||||
char pbuf[PATH_MAX];
|
||||
return orig_getxattr ? orig_getxattr(rewrite_path_follow(path, pbuf, sizeof(pbuf)), name, value, size) : -1;
|
||||
}
|
||||
|
||||
ssize_t lgetxattr(const char *path, const char *name, void *value, size_t size) {
|
||||
static ssize_t (*orig_lgetxattr)(const char *, const char *, void *, size_t) = NULL;
|
||||
if (!orig_lgetxattr) orig_lgetxattr = (ssize_t (*)(const char *, const char *, void *, size_t))dlsym(RTLD_NEXT, "lgetxattr");
|
||||
if (is_selinux_xattr(name)) {
|
||||
errno = ENODATA;
|
||||
return -1;
|
||||
}
|
||||
char pbuf[PATH_MAX];
|
||||
return orig_lgetxattr ? orig_lgetxattr(rewrite_path(path, pbuf, sizeof(pbuf)), name, value, size) : -1;
|
||||
}
|
||||
|
||||
ssize_t listxattr(const char *path, char *list, size_t size) {
|
||||
static ssize_t (*orig_listxattr)(const char *, char *, size_t) = NULL;
|
||||
if (!orig_listxattr) orig_listxattr = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "listxattr");
|
||||
char pbuf[PATH_MAX];
|
||||
ssize_t ret = orig_listxattr ? orig_listxattr(rewrite_path_follow(path, pbuf, sizeof(pbuf)), list, size) : -1;
|
||||
return size > 0 ? drop_selinux_xattr(list, ret) : ret;
|
||||
}
|
||||
|
||||
ssize_t llistxattr(const char *path, char *list, size_t size) {
|
||||
static ssize_t (*orig_llistxattr)(const char *, char *, size_t) = NULL;
|
||||
if (!orig_llistxattr) orig_llistxattr = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "llistxattr");
|
||||
char pbuf[PATH_MAX];
|
||||
ssize_t ret = orig_llistxattr ? orig_llistxattr(rewrite_path(path, pbuf, sizeof(pbuf)), list, size) : -1;
|
||||
return size > 0 ? drop_selinux_xattr(list, ret) : ret;
|
||||
}
|
||||
|
||||
ssize_t fgetxattr(int fd, const char *name, void *value, size_t size) {
|
||||
static ssize_t (*orig_fgetxattr)(int, const char *, void *, size_t) = NULL;
|
||||
if (!orig_fgetxattr) orig_fgetxattr = (ssize_t (*)(int, const char *, void *, size_t))dlsym(RTLD_NEXT, "fgetxattr");
|
||||
if (is_selinux_xattr(name)) {
|
||||
errno = ENODATA;
|
||||
return -1;
|
||||
}
|
||||
return orig_fgetxattr ? orig_fgetxattr(fd, name, value, size) : -1;
|
||||
}
|
||||
|
||||
ssize_t flistxattr(int fd, char *list, size_t size) {
|
||||
static ssize_t (*orig_flistxattr)(int, char *, size_t) = NULL;
|
||||
if (!orig_flistxattr) orig_flistxattr = (ssize_t (*)(int, char *, size_t))dlsym(RTLD_NEXT, "flistxattr");
|
||||
ssize_t ret = orig_flistxattr ? orig_flistxattr(fd, list, size) : -1;
|
||||
return size > 0 ? drop_selinux_xattr(list, ret) : ret;
|
||||
}
|
||||
|
||||
int setxattr(const char *path, const char *name, const void *value, size_t size, int flags) {
|
||||
static int (*orig_setxattr)(const char *, const char *, const void *, size_t, int) = NULL;
|
||||
if (!orig_setxattr) orig_setxattr = (int (*)(const char *, const char *, const void *, size_t, int))dlsym(RTLD_NEXT, "setxattr");
|
||||
char pbuf[PATH_MAX];
|
||||
return orig_setxattr ? orig_setxattr(rewrite_path_follow(path, pbuf, sizeof(pbuf)), name, value, size, flags) : -1;
|
||||
}
|
||||
|
||||
int lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags) {
|
||||
static int (*orig_lsetxattr)(const char *, const char *, const void *, size_t, int) = NULL;
|
||||
if (!orig_lsetxattr) orig_lsetxattr = (int (*)(const char *, const char *, const void *, size_t, int))dlsym(RTLD_NEXT, "lsetxattr");
|
||||
char pbuf[PATH_MAX];
|
||||
return orig_lsetxattr ? orig_lsetxattr(rewrite_path(path, pbuf, sizeof(pbuf)), name, value, size, flags) : -1;
|
||||
}
|
||||
|
||||
int removexattr(const char *path, const char *name) {
|
||||
static int (*orig_removexattr)(const char *, const char *) = NULL;
|
||||
if (!orig_removexattr) orig_removexattr = (int (*)(const char *, const char *))dlsym(RTLD_NEXT, "removexattr");
|
||||
char pbuf[PATH_MAX];
|
||||
return orig_removexattr ? orig_removexattr(rewrite_path(path, pbuf, sizeof(pbuf)), name) : -1;
|
||||
}
|
||||
|
||||
int lremovexattr(const char *path, const char *name) {
|
||||
static int (*orig_lremovexattr)(const char *, const char *) = NULL;
|
||||
if (!orig_lremovexattr) orig_lremovexattr = (int (*)(const char *, const char *))dlsym(RTLD_NEXT, "lremovexattr");
|
||||
char pbuf[PATH_MAX];
|
||||
return orig_lremovexattr ? orig_lremovexattr(rewrite_path(path, pbuf, sizeof(pbuf)), name) : -1;
|
||||
}
|
||||
|
||||
// Hook symlink
|
||||
int symlink(const char *target, const char *linkpath) {
|
||||
static int (*orig_symlink)(const char *, const char *) = NULL;
|
||||
if (!orig_symlink) orig_symlink = (int (*)(const char *, const char *))dlsym(RTLD_NEXT, "symlink");
|
||||
char pbuf[PATH_MAX], tbuf[PATH_MAX];
|
||||
const char *newlink = rewrite_path(linkpath, pbuf, sizeof(pbuf));
|
||||
const char *newtarget = (target && target[0] == '/') ? rewrite_path(target, tbuf, sizeof(tbuf)) : target;
|
||||
int ret = orig_symlink ? orig_symlink(newtarget, newlink) : -1;
|
||||
if (ret != 0 && orig_symlink && refused_in_repaired_dir(AT_FDCWD, newlink)) ret = orig_symlink(newtarget, newlink);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook symlinkat
|
||||
int symlinkat(const char *target, int newdirfd, const char *linkpath) {
|
||||
static int (*orig_symlinkat)(const char *, int, const char *) = NULL;
|
||||
if (!orig_symlinkat) orig_symlinkat = (int (*)(const char *, int, const char *))dlsym(RTLD_NEXT, "symlinkat");
|
||||
DDNATV_AT_REMAP(newdirfd, linkpath);
|
||||
char pbuf[PATH_MAX], tbuf[PATH_MAX];
|
||||
const char *newlink = (linkpath && linkpath[0] == '/') ? rewrite_path(linkpath, pbuf, sizeof(pbuf)) : linkpath;
|
||||
const char *newtarget = (target && target[0] == '/') ? rewrite_path(target, tbuf, sizeof(tbuf)) : target;
|
||||
int ret = orig_symlinkat ? orig_symlinkat(newtarget, newdirfd, newlink) : -1;
|
||||
if (ret != 0 && orig_symlinkat && refused_in_repaired_dir(newdirfd, newlink)) ret = orig_symlinkat(newtarget, newdirfd, newlink);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook linkat
|
||||
int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) {
|
||||
static int (*orig_linkat)(int, const char *, int, const char *, int) = NULL;
|
||||
if (!orig_linkat) orig_linkat = (int (*)(int, const char *, int, const char *, int))dlsym(RTLD_NEXT, "linkat");
|
||||
DDNATV_AT_REMAP_NAMED(olddirfd, oldpath, ddnatv_at_old);
|
||||
DDNATV_AT_REMAP_NAMED(newdirfd, newpath, ddnatv_at_new);
|
||||
char obuf[PATH_MAX], nbuf[PATH_MAX];
|
||||
const char *rold = (oldpath && oldpath[0] == '/') ? rewrite_path(oldpath, obuf, sizeof(obuf)) : oldpath;
|
||||
const char *rnew = (newpath && newpath[0] == '/') ? rewrite_path(newpath, nbuf, sizeof(nbuf)) : newpath;
|
||||
int ret = orig_linkat ? orig_linkat(olddirfd, rold, newdirfd, rnew, flags) : -1;
|
||||
if (ret != 0 && orig_linkat && refused_in_repaired_dir(newdirfd, rnew)) ret = orig_linkat(olddirfd, rold, newdirfd, rnew, flags);
|
||||
if (ret != 0 && (errno == EXDEV || errno == EPERM || errno == EACCES || errno == ENOTSUP || errno == ENOSYS)) {
|
||||
ret = ddnatv_emulate_hardlinkat(olddirfd, rold, newdirfd, rnew);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook link
|
||||
int link(const char *oldpath, const char *newpath) {
|
||||
return linkat(AT_FDCWD, oldpath, AT_FDCWD, newpath, 0);
|
||||
}
|
||||
|
||||
// Hook utime / utimes / lutimes / futimesat / utimensat
|
||||
int utime(const char *filename, const struct utimbuf *times) {
|
||||
static int (*orig_utime)(const char *, const struct utimbuf *) = NULL;
|
||||
if (!orig_utime) orig_utime = (int (*)(const char *, const struct utimbuf *))dlsym(RTLD_NEXT, "utime");
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path_follow(filename, buf, sizeof(buf));
|
||||
return orig_utime ? orig_utime(target, times) : -1;
|
||||
}
|
||||
|
||||
int utimes(const char *filename, const struct timeval times[2]) {
|
||||
static int (*orig_utimes)(const char *, const struct timeval[2]) = NULL;
|
||||
if (!orig_utimes) orig_utimes = (int (*)(const char *, const struct timeval[2]))dlsym(RTLD_NEXT, "utimes");
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path_follow(filename, buf, sizeof(buf));
|
||||
return orig_utimes ? orig_utimes(target, times) : -1;
|
||||
}
|
||||
|
||||
int lutimes(const char *filename, const struct timeval times[2]) {
|
||||
static int (*orig_lutimes)(const char *, const struct timeval[2]) = NULL;
|
||||
if (!orig_lutimes) orig_lutimes = (int (*)(const char *, const struct timeval[2]))dlsym(RTLD_NEXT, "lutimes");
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(filename, buf, sizeof(buf));
|
||||
return orig_lutimes ? orig_lutimes(target, times) : -1;
|
||||
}
|
||||
|
||||
int futimesat(int dirfd, const char *pathname, const struct timeval times[2]) {
|
||||
static int (*orig_futimesat)(int, const char *, const struct timeval[2]) = NULL;
|
||||
if (!orig_futimesat) orig_futimesat = (int (*)(int, const char *, const struct timeval[2]))dlsym(RTLD_NEXT, "futimesat");
|
||||
char buf[PATH_MAX];
|
||||
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
|
||||
return orig_futimesat ? orig_futimesat(dirfd, target, times) : -1;
|
||||
}
|
||||
|
||||
int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) {
|
||||
static int (*orig_utimensat)(int, const char *, const struct timespec[2], int) = NULL;
|
||||
if (!orig_utimensat) orig_utimensat = (int (*)(int, const char *, const struct timespec[2], int))dlsym(RTLD_NEXT, "utimensat");
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
char buf[PATH_MAX];
|
||||
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
|
||||
return orig_utimensat ? orig_utimensat(dirfd, target, times, flags) : -1;
|
||||
}
|
||||
|
||||
// Hook truncate
|
||||
int truncate(const char *path, off_t length) {
|
||||
static int (*orig_truncate)(const char *, off_t) = NULL;
|
||||
if (!orig_truncate) orig_truncate = (int (*)(const char *, off_t))dlsym(RTLD_NEXT, "truncate");
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path_follow(path, buf, sizeof(buf));
|
||||
return orig_truncate(target, length);
|
||||
}
|
||||
|
||||
// Hook statfs / statfs64 / statvfs / statvfs64
|
||||
int statfs(const char *path, struct statfs *buf) {
|
||||
static int (*orig_statfs)(const char *, struct statfs *) = NULL;
|
||||
if (!orig_statfs) orig_statfs = (int (*)(const char *, struct statfs *))dlsym(RTLD_NEXT, "statfs");
|
||||
char pbuf[PATH_MAX];
|
||||
const char *target = rewrite_path_follow(path, pbuf, sizeof(pbuf));
|
||||
return orig_statfs ? orig_statfs(target, buf) : -1;
|
||||
}
|
||||
|
||||
int statvfs(const char *path, struct statvfs *buf) {
|
||||
static int (*orig_statvfs)(const char *, struct statvfs *) = NULL;
|
||||
if (!orig_statvfs) orig_statvfs = (int (*)(const char *, struct statvfs *))dlsym(RTLD_NEXT, "statvfs");
|
||||
char pbuf[PATH_MAX];
|
||||
const char *target = rewrite_path_follow(path, pbuf, sizeof(pbuf));
|
||||
return orig_statvfs ? orig_statvfs(target, buf) : -1;
|
||||
}
|
||||
|
||||
// Hook chdir
|
||||
int chdir(const char *path) {
|
||||
static int (*orig_chdir)(const char *) = NULL;
|
||||
if (!orig_chdir) orig_chdir = (int (*)(const char *))dlsym(RTLD_NEXT, "chdir");
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path_follow(path, buf, sizeof(buf));
|
||||
return orig_chdir ? orig_chdir(target) : -1;
|
||||
}
|
||||
|
||||
// Hook fchdir
|
||||
int fchdir(int fd) {
|
||||
static int (*orig_fchdir)(int) = NULL;
|
||||
if (!orig_fchdir) orig_fchdir = (int (*)(int))dlsym(RTLD_NEXT, "fchdir");
|
||||
return orig_fchdir ? orig_fchdir(fd) : -1;
|
||||
}
|
||||
|
||||
// Hook getcwd
|
||||
char *getcwd(char *buf, size_t size) {
|
||||
static char *(*orig_getcwd)(char *, size_t) = NULL;
|
||||
if (!orig_getcwd) orig_getcwd = (char *(*)(char *, size_t))dlsym(RTLD_NEXT, "getcwd");
|
||||
char *res = orig_getcwd ? orig_getcwd(buf, size) : NULL;
|
||||
if (res) {
|
||||
size_t len = strlen(res);
|
||||
guest_path_of_mount_source(res, len, buf ? size : len + 1, NULL);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
extern void __chk_fail(void) __attribute__((__noreturn__));
|
||||
char *__getcwd_chk(char *buf, size_t size, size_t buflen) {
|
||||
if (size > buflen) __chk_fail();
|
||||
return getcwd(buf, size);
|
||||
}
|
||||
|
||||
// Hook realpath and canonicalize_file_name
|
||||
char *realpath(const char *path, char *resolved_path) {
|
||||
static char *(*orig_realpath)(const char *, char *) = NULL;
|
||||
if (!orig_realpath) orig_realpath = (char *(*)(const char *, char *))dlsym(RTLD_NEXT, "realpath");
|
||||
if (!path) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
init_ddnatv_hook();
|
||||
if (is_proc_self_exe(path)) {
|
||||
char tmp[PATH_MAX];
|
||||
ssize_t n = handle_proc_self_exe(tmp, sizeof(tmp) - 1);
|
||||
if (n > 0) {
|
||||
tmp[n] = '\0';
|
||||
if (resolved_path) {
|
||||
strncpy(resolved_path, tmp, PATH_MAX - 1);
|
||||
resolved_path[PATH_MAX - 1] = '\0';
|
||||
return resolved_path;
|
||||
} else {
|
||||
return strdup(tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
char pbuf[PATH_MAX];
|
||||
const char *target = rewrite_path(path, pbuf, sizeof(pbuf));
|
||||
char *res = orig_realpath ? orig_realpath(target, resolved_path) : NULL;
|
||||
if (res && g_ddnatv_root[0] != '\0') {
|
||||
size_t rlen = g_ddnatv_root_len;
|
||||
if (strncmp(res, g_ddnatv_root, rlen) == 0 && (res[rlen] == '/' || res[rlen] == '\0')) {
|
||||
size_t rem_len = strlen(res + rlen);
|
||||
if (rem_len == 0) {
|
||||
res[0] = '/';
|
||||
res[1] = '\0';
|
||||
} else {
|
||||
memmove(res, res + rlen, rem_len + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (res) guest_path_of_mount_source(res, strlen(res), resolved_path ? PATH_MAX : strlen(res) + 1, NULL);
|
||||
return res;
|
||||
}
|
||||
|
||||
char *__realpath_chk(const char *path, char *resolved_path, size_t resolved_len) {
|
||||
return realpath(path, resolved_path);
|
||||
}
|
||||
|
||||
char *canonicalize_file_name(const char *path) {
|
||||
return realpath(path, NULL);
|
||||
}
|
||||
|
||||
// Hook scandir and scandir64
|
||||
int scandir(const char *dirp, struct dirent ***namelist,
|
||||
int (*filter)(const struct dirent *),
|
||||
int (*compar)(const struct dirent **, const struct dirent **)) {
|
||||
static int (*orig_scandir)(const char *, struct dirent ***,
|
||||
int (*)(const struct dirent *),
|
||||
int (*)(const struct dirent **, const struct dirent **)) = NULL;
|
||||
if (!orig_scandir) orig_scandir = (int (*)(const char *, struct dirent ***,
|
||||
int (*)(const struct dirent *),
|
||||
int (*)(const struct dirent **, const struct dirent **)))dlsym(RTLD_NEXT, "scandir");
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(dirp, buf, sizeof(buf));
|
||||
return orig_scandir ? orig_scandir(target, namelist, filter, compar) : -1;
|
||||
}
|
||||
|
||||
#if defined(__LP64__)
|
||||
// Export 64-bit Large File Support (LFS) symbol aliases on 64-bit platforms so that
|
||||
// calls from libraries compiled with LFS (such as libstdc++ calling fopen64) are intercepted.
|
||||
__asm__(
|
||||
".globl open64\n" ".set open64, open\n"
|
||||
".globl openat64\n" ".set openat64, openat\n"
|
||||
".globl __open64_2\n" ".set __open64_2, __open_2\n"
|
||||
".globl __openat64_2\n" ".set __openat64_2, __openat_2\n"
|
||||
".globl creat64\n" ".set creat64, creat\n"
|
||||
".globl fopen64\n" ".set fopen64, fopen\n"
|
||||
".globl freopen64\n" ".set freopen64, freopen\n"
|
||||
".globl stat64\n" ".set stat64, stat\n"
|
||||
".globl lstat64\n" ".set lstat64, lstat\n"
|
||||
".globl fstatat64\n" ".set fstatat64, fstatat\n"
|
||||
".globl truncate64\n" ".set truncate64, truncate\n"
|
||||
".globl statfs64\n" ".set statfs64, statfs\n"
|
||||
".globl statvfs64\n" ".set statvfs64, statvfs\n"
|
||||
".globl scandir64\n" ".set scandir64, scandir\n"
|
||||
".globl mkstemp64\n" ".set mkstemp64, mkstemp\n"
|
||||
".globl mkostemp64\n" ".set mkostemp64, mkostemp\n"
|
||||
".globl mkstemps64\n" ".set mkstemps64, mkstemps\n"
|
||||
".globl mkostemps64\n" ".set mkostemps64, mkostemps\n"
|
||||
".globl tmpfile64\n" ".set tmpfile64, tmpfile\n"
|
||||
);
|
||||
#endif
|
||||
|
||||
#endif /* !DDNATV_HOST_TEST */
|
||||
@@ -0,0 +1,813 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
||||
// parts share their static functions and are built with the same command as before).
|
||||
//
|
||||
// Opening, creating and inspecting files: open, fopen, temporary files, directories, the stat family.
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
|
||||
#ifndef O_TMPFILE
|
||||
#define O_TMPFILE (020000000 | 00200000)
|
||||
#endif
|
||||
|
||||
static inline int open_needs_mode(int flags) {
|
||||
#ifdef O_TMPFILE
|
||||
return ((flags & O_CREAT) != 0) || ((flags & O_TMPFILE) == O_TMPFILE);
|
||||
#else
|
||||
return (flags & O_CREAT) != 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
// The app is root for the programs it runs, and root puts names in every directory and takes them out
|
||||
// whatever the mode of the directory; the app does not. A directory that is the app's own can have lost
|
||||
// its owner's rights: an earlier version left the directories dpkg makes (with mode 0) at mode 0 when the
|
||||
// chmod that follows did not take effect, and a package can ship a read-only one (#44). When a call that
|
||||
// makes or removes a name is refused in such a directory, the directory is given its owner's rights and the
|
||||
// call is made once more. Returns 1 when the directory was repaired (the call can be made again).
|
||||
static int give_dir_owner_rights(int dirfd, const char *name) {
|
||||
static int (*orig_fchmodat)(int, const char *, mode_t, int) = NULL;
|
||||
if (!name || !*name) return 0;
|
||||
char parent[PATH_MAX];
|
||||
size_t len = strlen(name);
|
||||
if (len >= sizeof(parent)) return 0;
|
||||
memcpy(parent, name, len + 1);
|
||||
while (len > 1 && parent[len - 1] == '/') parent[--len] = '\0';
|
||||
char *slash = strrchr(parent, '/');
|
||||
if (!slash) {
|
||||
strcpy(parent, ".");
|
||||
} else if (slash == parent) {
|
||||
parent[1] = '\0';
|
||||
} else {
|
||||
*slash = '\0';
|
||||
}
|
||||
struct stat st;
|
||||
if (real_fstatat(dirfd, parent, &st, 0) != 0 || !S_ISDIR(st.st_mode)) return 0;
|
||||
if (__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED) == (uid_t)-1) load_app_ids();
|
||||
if (st.st_uid != __atomic_load_n(&g_app_uid, __ATOMIC_RELAXED)) return 0;
|
||||
if ((st.st_mode & 0700) == 0700) return 0;
|
||||
if (!orig_fchmodat) orig_fchmodat = (int (*)(int, const char *, mode_t, int))dlsym(RTLD_NEXT, "fchmodat");
|
||||
return orig_fchmodat && orig_fchmodat(dirfd, parent, (st.st_mode | 0700) & 07777, 0) == 0;
|
||||
}
|
||||
|
||||
// For the calls that were refused (EACCES): whether the directory of `name` was repaired, so that the call
|
||||
// can be made again. errno stays what the call left when it was not.
|
||||
static int refused_in_repaired_dir(int dirfd, const char *name) {
|
||||
if (errno != EACCES) return 0;
|
||||
int saved = errno;
|
||||
int fixed = give_dir_owner_rights(dirfd, name);
|
||||
if (!fixed) errno = saved;
|
||||
return fixed;
|
||||
}
|
||||
|
||||
static int dlopen_preload_deps(const char *path, void *(*orig_dlopen)(const char *, int), int depth);
|
||||
|
||||
// Hook dlopen and dlmopen
|
||||
void *dlopen(const char *filename, int flags) {
|
||||
static void *(*orig_dlopen)(const char *, int) = NULL;
|
||||
if (!orig_dlopen) orig_dlopen = (void *(*)(const char *, int))dlsym(RTLD_NEXT, "dlopen");
|
||||
if (!filename) return orig_dlopen ? orig_dlopen(NULL, flags) : NULL;
|
||||
if (!orig_dlopen) return NULL;
|
||||
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path_follow(filename, buf, sizeof(buf));
|
||||
void *handle = orig_dlopen(target, flags);
|
||||
if (!handle && target[0] == '/' && !(flags & RTLD_NOLOAD)) {
|
||||
// Its dependencies may be in its RUNPATH, which ld.so looked for outside the rootfs
|
||||
int saved = errno;
|
||||
if (dlopen_preload_deps(target, orig_dlopen, 0) > 0) handle = orig_dlopen(target, flags);
|
||||
if (!handle) errno = saved;
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
void *dlmopen(Lmid_t lmid, const char *filename, int flags) {
|
||||
static void *(*orig_dlmopen)(Lmid_t, const char *, int) = NULL;
|
||||
if (!orig_dlmopen) orig_dlmopen = (void *(*)(Lmid_t, const char *, int))dlsym(RTLD_NEXT, "dlmopen");
|
||||
if (!filename) return orig_dlmopen ? orig_dlmopen(lmid, NULL, flags) : NULL;
|
||||
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(filename, buf, sizeof(buf));
|
||||
return orig_dlmopen ? orig_dlmopen(lmid, target, flags) : NULL;
|
||||
}
|
||||
|
||||
// /proc/<pid>/status (and a thread's) give the app's own ids, which every other call presents as
|
||||
// root: process monitors comparing them with getuid() showed none of the session's processes. Only
|
||||
// such tools are given root's there: Chromium (Steam's web helper) checks the ids of the processes it
|
||||
// sandboxes in those files, and with them all 0 every helper exited and Steam never drew a frame.
|
||||
static int reader_compares_process_ids(void) {
|
||||
static const char *const k_readers[] = {
|
||||
"ps", "top", "htop", "btop", "atop", "pgrep", "pkill", "pidof", "pstree", "w", "glances",
|
||||
"ksystemstats", "plasma-systemmonitor", "ksysguard", "ksysguardd", "kinfocenter", "gnome-system-monitor",
|
||||
};
|
||||
const char *me = program_invocation_short_name;
|
||||
if (!me) return 0;
|
||||
for (size_t i = 0; i < sizeof(k_readers) / sizeof(k_readers[0]); i++) {
|
||||
if (strcmp(me, k_readers[i]) == 0) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int is_proc_status_path(const char *p) {
|
||||
if (!p || strncmp(p, "/proc/", 6) != 0) return 0;
|
||||
p += 6;
|
||||
if (strncmp(p, "self/", 5) == 0) p += 5;
|
||||
else if (strncmp(p, "thread-self/", 12) == 0) p += 12;
|
||||
else {
|
||||
if (*p < '0' || *p > '9') return 0;
|
||||
while (*p >= '0' && *p <= '9') p++;
|
||||
if (*p++ != '/') return 0;
|
||||
if (strncmp(p, "task/", 5) == 0) {
|
||||
p += 5;
|
||||
if (*p < '0' || *p > '9') return 0;
|
||||
while (*p >= '0' && *p <= '9') p++;
|
||||
if (*p++ != '/') return 0;
|
||||
}
|
||||
}
|
||||
return strcmp(p, "status") == 0;
|
||||
}
|
||||
|
||||
static void proc_status_root_ids(char *text, size_t len, const char *field, unsigned long id) {
|
||||
size_t flen = strlen(field);
|
||||
for (size_t at = 0; at + flen <= len; at++) {
|
||||
if ((at != 0 && text[at - 1] != '\n') || memcmp(text + at, field, flen) != 0) continue;
|
||||
char *p = text + at + flen;
|
||||
char *end = memchr(p, '\n', len - (size_t)(p - text));
|
||||
if (!end) end = text + len;
|
||||
char out[128];
|
||||
size_t used = 0;
|
||||
while (p < end && used + 24 < sizeof(out)) {
|
||||
while (p < end && (*p == '\t' || *p == ' ')) out[used++] = *p++;
|
||||
char *num = p;
|
||||
while (p < end && *p >= '0' && *p <= '9') p++;
|
||||
if (p == num) break;
|
||||
unsigned long v = strtoul(num, NULL, 10);
|
||||
used += (size_t)snprintf(out + used, sizeof(out) - used, "%lu", v == id ? 0UL : v);
|
||||
}
|
||||
if (p != end) return;
|
||||
// the line shrinks or stays (an id becomes 0), so it is rewritten in place
|
||||
size_t old_len = (size_t)(end - (text + at + flen));
|
||||
if (used > old_len) return;
|
||||
memcpy(text + at + flen, out, used);
|
||||
memmove(text + at + flen + used, end, len - (size_t)(end - text) + 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// The status file with the app's ids as root's, in a memory file; -2 when `path` is no such file
|
||||
// or is opened for writing.
|
||||
static int open_proc_status(const char *path, int flags) {
|
||||
if ((flags & O_ACCMODE) != O_RDONLY || !is_proc_status_path(path) || !reader_compares_process_ids()) return -2;
|
||||
int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return -1;
|
||||
char text[16384];
|
||||
size_t len = 0;
|
||||
for (;;) {
|
||||
ssize_t n = (ssize_t)syscall(SYS_read, fd, text + len, sizeof(text) - 1 - len);
|
||||
if (n <= 0) break;
|
||||
len += (size_t)n;
|
||||
if (len >= sizeof(text) - 1) break;
|
||||
}
|
||||
syscall(SYS_close, fd);
|
||||
text[len] = '\0';
|
||||
if (__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED) == (uid_t)-1) load_app_ids();
|
||||
proc_status_root_ids(text, len, "Uid:", (unsigned long)__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED));
|
||||
len = strlen(text);
|
||||
proc_status_root_ids(text, len, "Gid:", (unsigned long)__atomic_load_n(&g_app_gid, __ATOMIC_RELAXED));
|
||||
len = strlen(text);
|
||||
int mfd = (int)syscall(SYS_memfd_create, "status", (flags & O_CLOEXEC) ? MFD_CLOEXEC : 0);
|
||||
if (mfd < 0) return (int)syscall(SYS_openat, AT_FDCWD, path, flags, 0);
|
||||
size_t written = 0;
|
||||
while (written < len) {
|
||||
ssize_t n = (ssize_t)syscall(SYS_write, mfd, text + written, len - written);
|
||||
if (n <= 0) break;
|
||||
written += (size_t)n;
|
||||
}
|
||||
syscall(SYS_lseek, mfd, 0, SEEK_SET);
|
||||
return mfd;
|
||||
}
|
||||
|
||||
// openat(dirfd, "status") with dirfd on /proc/<pid> or a thread of it
|
||||
static int open_proc_status_at(int dirfd, const char *name, int flags) {
|
||||
if (dirfd < 0 || !name || strcmp(name, "status") != 0 || (flags & O_ACCMODE) != O_RDONLY) return -2;
|
||||
char link[48], dir[PATH_MAX];
|
||||
snprintf(link, sizeof(link), "/proc/self/fd/%d", dirfd);
|
||||
long n = syscall(SYS_readlinkat, AT_FDCWD, link, dir, sizeof(dir) - 8);
|
||||
if (n <= 0) return -2;
|
||||
dir[n] = '\0';
|
||||
memcpy(dir + n, "/status", 8);
|
||||
return open_proc_status(dir, flags);
|
||||
}
|
||||
|
||||
// Hook open
|
||||
int open(const char *pathname, int flags, ...) {
|
||||
static int (*orig_open)(const char *, int, ...) = NULL;
|
||||
if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open");
|
||||
init_ddnatv_hook();
|
||||
char exe_buf[PATH_MAX];
|
||||
if (is_proc_self_exe(pathname)) {
|
||||
if (g_real_exe[0] != '\0') {
|
||||
pathname = g_real_exe;
|
||||
} else if (handle_proc_self_exe(exe_buf, sizeof(exe_buf)) > 0) {
|
||||
pathname = exe_buf;
|
||||
}
|
||||
}
|
||||
int status_fd = open_proc_status(pathname, flags);
|
||||
if (status_fd != -2) return status_fd;
|
||||
char buf[PATH_MAX];
|
||||
const char *target = ((flags & O_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf));
|
||||
if (open_needs_mode(flags)) {
|
||||
va_list args;
|
||||
va_start(args, flags);
|
||||
mode_t mode = va_arg(args, mode_t);
|
||||
va_end(args);
|
||||
int fd = orig_open(target, flags, mode);
|
||||
if (fd < 0 && (flags & O_CREAT) && refused_in_repaired_dir(AT_FDCWD, target)) fd = orig_open(target, flags, mode);
|
||||
return fd;
|
||||
}
|
||||
return orig_open(target, flags);
|
||||
}
|
||||
|
||||
// Hook openat
|
||||
int openat(int dirfd, const char *pathname, int flags, ...) {
|
||||
static int (*orig_openat)(int, const char *, int, ...) = NULL;
|
||||
if (!orig_openat) orig_openat = (int (*)(int, const char *, int, ...))dlsym(RTLD_NEXT, "openat");
|
||||
init_ddnatv_hook();
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
char exe_buf[PATH_MAX];
|
||||
if (is_proc_self_exe(pathname)) {
|
||||
if (g_real_exe[0] != '\0') {
|
||||
pathname = g_real_exe;
|
||||
} else if (handle_proc_self_exe(exe_buf, sizeof(exe_buf)) > 0) {
|
||||
pathname = exe_buf;
|
||||
}
|
||||
}
|
||||
int status_fd = (pathname && pathname[0] == '/') ? open_proc_status(pathname, flags) : open_proc_status_at(dirfd, pathname, flags);
|
||||
if (status_fd != -2) return status_fd;
|
||||
char buf[PATH_MAX];
|
||||
const char *target = (pathname && pathname[0] == '/') ? ((flags & O_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
|
||||
if (open_needs_mode(flags)) {
|
||||
va_list args;
|
||||
va_start(args, flags);
|
||||
mode_t mode = va_arg(args, mode_t);
|
||||
va_end(args);
|
||||
int fd = orig_openat(dirfd, target, flags, mode);
|
||||
if (fd < 0 && (flags & O_CREAT) && refused_in_repaired_dir(dirfd, target)) fd = orig_openat(dirfd, target, flags, mode);
|
||||
return fd;
|
||||
}
|
||||
return orig_openat(dirfd, target, flags);
|
||||
}
|
||||
|
||||
// Hook creat
|
||||
int creat(const char *pathname, mode_t mode) {
|
||||
return open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
|
||||
}
|
||||
|
||||
// Fortified open variants used by GNU tar and other coreutils compiled with _FORTIFY_SOURCE=2/3
|
||||
int __open_2(const char *pathname, int flags) {
|
||||
return open(pathname, flags);
|
||||
}
|
||||
|
||||
int __openat_2(int dirfd, const char *pathname, int flags) {
|
||||
return openat(dirfd, pathname, flags);
|
||||
}
|
||||
|
||||
// openat2/open_how local definition: avoids depending on <linux/openat2.h>,
|
||||
// which old NDK/cross headers may lack. Layout matches UAPI ver0
|
||||
// (flags, mode, resolve = 3 x __u64); extra fields are never read here.
|
||||
struct ddnatv_open_how {
|
||||
uint64_t flags;
|
||||
uint64_t mode;
|
||||
uint64_t resolve;
|
||||
};
|
||||
#ifndef RESOLVE_BENEATH
|
||||
#define RESOLVE_BENEATH 0x08
|
||||
#endif
|
||||
#ifndef __NR_openat2
|
||||
#define __NR_openat2 437
|
||||
#endif
|
||||
#ifndef __NR_fchmodat2
|
||||
#define __NR_fchmodat2 452
|
||||
#endif
|
||||
|
||||
static long call_orig_syscall(long number, unsigned long a1, unsigned long a2, unsigned long a3, unsigned long a4, unsigned long a5, unsigned long a6) {
|
||||
static long (*orig_syscall)(long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long) = NULL;
|
||||
if (!orig_syscall) {
|
||||
orig_syscall = (long (*)(long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long))dlsym(RTLD_NEXT, "syscall");
|
||||
}
|
||||
return orig_syscall ? orig_syscall(number, a1, a2, a3, a4, a5, a6) : syscall(number, a1, a2, a3, a4, a5, a6);
|
||||
}
|
||||
|
||||
// Hook openat2 (used by GNU tar 1.35+/gnulib for RESOLVE_BENEATH traversal).
|
||||
// Rewrites absolute paths like openat, then delegates; on ENOSYS (kernels
|
||||
// without openat2, e.g. Android < 5.6) emulates with plain openat instead of
|
||||
// trusting every caller to implement the fallback itself.
|
||||
int openat2(int dirfd, const char *pathname, struct ddnatv_open_how *how, size_t usize) {
|
||||
static int (*orig_openat2)(int, const char *, struct ddnatv_open_how *, size_t) = NULL;
|
||||
if (!orig_openat2) orig_openat2 = (int (*)(int, const char *, struct ddnatv_open_how *, size_t))dlsym(RTLD_NEXT, "openat2");
|
||||
if (!pathname) {
|
||||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
const char *given = pathname;
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
// a remapped path is absolute, which RESOLVE_BENEATH and the like refuse: the caller walks it itself
|
||||
if (pathname != given && how && how->resolve != 0) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
char exe_buf[PATH_MAX];
|
||||
if (is_proc_self_exe(pathname)) {
|
||||
if (g_real_exe[0] != '\0') {
|
||||
pathname = g_real_exe;
|
||||
} else if (handle_proc_self_exe(exe_buf, sizeof(exe_buf)) > 0) {
|
||||
pathname = exe_buf;
|
||||
}
|
||||
}
|
||||
char buf[PATH_MAX];
|
||||
const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
|
||||
if (orig_openat2) {
|
||||
int ret = orig_openat2(dirfd, target, how, usize);
|
||||
if (ret >= 0 || errno != ENOSYS) return ret;
|
||||
}
|
||||
#ifdef __NR_openat2
|
||||
long sret = call_orig_syscall(__NR_openat2, (unsigned long)dirfd, (unsigned long)target, (unsigned long)how, (unsigned long)usize, 0, 0);
|
||||
if (sret >= 0 || errno != ENOSYS) {
|
||||
return (int)sret;
|
||||
}
|
||||
#endif
|
||||
if (!how || usize < 16) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
int flags = (int)(how->flags & 0xffffffffu);
|
||||
mode_t mode = (mode_t)(how->mode & 07777u);
|
||||
|
||||
// RESOLVE_BENEATH, RESOLVE_NO_SYMLINKS and the other resolve flags restrict every component of
|
||||
// the path, which a plain openat() cannot do (a symlink in the middle would still be followed).
|
||||
// Say so: callers such as gnulib's openat2 (GNU tar) then walk the path safely themselves.
|
||||
if (how->resolve != 0) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (open_needs_mode(flags)) {
|
||||
return openat(dirfd, target, flags, mode);
|
||||
}
|
||||
return openat(dirfd, target, flags);
|
||||
}
|
||||
|
||||
// Hook fchmodat2 (used by GNU tar for lchmod-style symlink chmod).
|
||||
// Delegates when the libc symbol exists, otherwise emulates: symlinks cannot
|
||||
// be chmod'ed without kernel support, so skip them (return 0) like gnulib,
|
||||
// and route everything else through fchmodat.
|
||||
int fchmodat2(int dirfd, const char *pathname, mode_t mode, int flags) {
|
||||
static int (*orig_fchmodat2)(int, const char *, mode_t, int) = NULL;
|
||||
if (!orig_fchmodat2) orig_fchmodat2 = (int (*)(int, const char *, mode_t, int))dlsym(RTLD_NEXT, "fchmodat2");
|
||||
if (!pathname) {
|
||||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
char buf[PATH_MAX];
|
||||
const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
|
||||
if (orig_fchmodat2) {
|
||||
int ret = orig_fchmodat2(dirfd, target, mode, flags);
|
||||
if (ret == 0 || errno != ENOSYS) return ret;
|
||||
}
|
||||
if ((flags & AT_SYMLINK_NOFOLLOW) != 0) {
|
||||
struct stat st;
|
||||
if (fstatat(dirfd, target, &st, AT_SYMLINK_NOFOLLOW) == 0 && S_ISLNK(st.st_mode)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return fchmodat(dirfd, target, mode, 0);
|
||||
}
|
||||
|
||||
|
||||
// Hook fopen
|
||||
FILE *fopen(const char *pathname, const char *mode) {
|
||||
static FILE *(*orig_fopen)(const char *, const char *) = NULL;
|
||||
if (!orig_fopen) orig_fopen = (FILE *(*)(const char *, const char *))dlsym(RTLD_NEXT, "fopen");
|
||||
if (mode && mode[0] == 'r' && !strchr(mode, '+')) {
|
||||
int status_fd = open_proc_status(pathname, O_RDONLY | (strchr(mode, 'e') ? O_CLOEXEC : 0));
|
||||
if (status_fd >= 0) return fdopen(status_fd, mode);
|
||||
if (status_fd == -1) return NULL;
|
||||
}
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path_follow(pathname, buf, sizeof(buf));
|
||||
return orig_fopen(target, mode);
|
||||
}
|
||||
|
||||
// Hook freopen
|
||||
FILE *freopen(const char *pathname, const char *mode, FILE *stream) {
|
||||
static FILE *(*orig_freopen)(const char *, const char *, FILE *) = NULL;
|
||||
if (!orig_freopen) orig_freopen = (FILE *(*)(const char *, const char *, FILE *))dlsym(RTLD_NEXT, "freopen");
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path_follow(pathname, buf, sizeof(buf));
|
||||
return orig_freopen(target, mode, stream);
|
||||
}
|
||||
|
||||
// Hook write
|
||||
ssize_t write(int fd, const void *buf, size_t count) {
|
||||
static ssize_t (*orig_write)(int, const void *, size_t) = NULL;
|
||||
if (!orig_write) orig_write = (ssize_t (*)(int, const void *, size_t))dlsym(RTLD_NEXT, "write");
|
||||
return orig_write ? orig_write(fd, buf, count) : -1;
|
||||
}
|
||||
|
||||
// Hook mkstemp
|
||||
int mkstemp(char *template) {
|
||||
static int (*orig_mkstemp)(char *) = NULL;
|
||||
if (!orig_mkstemp) orig_mkstemp = (int (*)(char *))dlsym(RTLD_NEXT, "mkstemp");
|
||||
if (!template) { errno = EINVAL; return -1; }
|
||||
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(template, buf, sizeof(buf));
|
||||
if (target == template) {
|
||||
return orig_mkstemp ? orig_mkstemp(template) : -1;
|
||||
}
|
||||
|
||||
char tmp_buf[PATH_MAX];
|
||||
strncpy(tmp_buf, target, sizeof(tmp_buf) - 1);
|
||||
tmp_buf[sizeof(tmp_buf) - 1] = '\0';
|
||||
|
||||
int fd = orig_mkstemp ? orig_mkstemp(tmp_buf) : -1;
|
||||
if (fd >= 0) {
|
||||
size_t orig_len = strlen(template);
|
||||
size_t tmp_len = strlen(tmp_buf);
|
||||
if (orig_len >= 6 && tmp_len >= 6) {
|
||||
memcpy(template + orig_len - 6, tmp_buf + tmp_len - 6, 6);
|
||||
}
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
// Hook mkostemp
|
||||
int mkostemp(char *template, int flags) {
|
||||
static int (*orig_mkostemp)(char *, int) = NULL;
|
||||
if (!orig_mkostemp) orig_mkostemp = (int (*)(char *, int))dlsym(RTLD_NEXT, "mkostemp");
|
||||
if (!template) { errno = EINVAL; return -1; }
|
||||
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(template, buf, sizeof(buf));
|
||||
if (target == template) {
|
||||
return orig_mkostemp ? orig_mkostemp(template, flags) : -1;
|
||||
}
|
||||
|
||||
char tmp_buf[PATH_MAX];
|
||||
strncpy(tmp_buf, target, sizeof(tmp_buf) - 1);
|
||||
tmp_buf[sizeof(tmp_buf) - 1] = '\0';
|
||||
|
||||
int fd = orig_mkostemp ? orig_mkostemp(tmp_buf, flags) : -1;
|
||||
if (fd >= 0) {
|
||||
size_t orig_len = strlen(template);
|
||||
size_t tmp_len = strlen(tmp_buf);
|
||||
if (orig_len >= 6 && tmp_len >= 6) {
|
||||
memcpy(template + orig_len - 6, tmp_buf + tmp_len - 6, 6);
|
||||
}
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
// Hook mkstemps
|
||||
int mkstemps(char *template, int suffixlen) {
|
||||
static int (*orig_mkstemps)(char *, int) = NULL;
|
||||
if (!orig_mkstemps) orig_mkstemps = (int (*)(char *, int))dlsym(RTLD_NEXT, "mkstemps");
|
||||
if (!template) { errno = EINVAL; return -1; }
|
||||
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(template, buf, sizeof(buf));
|
||||
if (target == template) {
|
||||
return orig_mkstemps ? orig_mkstemps(template, suffixlen) : -1;
|
||||
}
|
||||
|
||||
char tmp_buf[PATH_MAX];
|
||||
strncpy(tmp_buf, target, sizeof(tmp_buf) - 1);
|
||||
tmp_buf[sizeof(tmp_buf) - 1] = '\0';
|
||||
|
||||
int fd = orig_mkstemps ? orig_mkstemps(tmp_buf, suffixlen) : -1;
|
||||
if (fd >= 0) {
|
||||
size_t orig_len = strlen(template);
|
||||
size_t tmp_len = strlen(tmp_buf);
|
||||
if (orig_len >= (size_t)(suffixlen + 6) && tmp_len >= (size_t)(suffixlen + 6)) {
|
||||
memcpy(template + orig_len - suffixlen - 6, tmp_buf + tmp_len - suffixlen - 6, 6);
|
||||
}
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
// Hook mkostemps
|
||||
int mkostemps(char *template, int suffixlen, int flags) {
|
||||
static int (*orig_mkostemps)(char *, int, int) = NULL;
|
||||
if (!orig_mkostemps) orig_mkostemps = (int (*)(char *, int, int))dlsym(RTLD_NEXT, "mkostemps");
|
||||
if (!template) { errno = EINVAL; return -1; }
|
||||
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(template, buf, sizeof(buf));
|
||||
if (target == template) {
|
||||
return orig_mkostemps ? orig_mkostemps(template, suffixlen, flags) : -1;
|
||||
}
|
||||
|
||||
char tmp_buf[PATH_MAX];
|
||||
strncpy(tmp_buf, target, sizeof(tmp_buf) - 1);
|
||||
tmp_buf[sizeof(tmp_buf) - 1] = '\0';
|
||||
|
||||
int fd = orig_mkostemps ? orig_mkostemps(tmp_buf, suffixlen, flags) : -1;
|
||||
if (fd >= 0) {
|
||||
size_t orig_len = strlen(template);
|
||||
size_t tmp_len = strlen(tmp_buf);
|
||||
if (orig_len >= (size_t)(suffixlen + 6) && tmp_len >= (size_t)(suffixlen + 6)) {
|
||||
memcpy(template + orig_len - suffixlen - 6, tmp_buf + tmp_len - suffixlen - 6, 6);
|
||||
}
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
// Hook mkdtemp
|
||||
char *mkdtemp(char *template) {
|
||||
static char *(*orig_mkdtemp)(char *) = NULL;
|
||||
if (!orig_mkdtemp) orig_mkdtemp = (char *(*)(char *))dlsym(RTLD_NEXT, "mkdtemp");
|
||||
if (!template) { errno = EINVAL; return NULL; }
|
||||
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(template, buf, sizeof(buf));
|
||||
if (target == template) {
|
||||
return orig_mkdtemp ? orig_mkdtemp(template) : NULL;
|
||||
}
|
||||
|
||||
char tmp_buf[PATH_MAX];
|
||||
strncpy(tmp_buf, target, sizeof(tmp_buf) - 1);
|
||||
tmp_buf[sizeof(tmp_buf) - 1] = '\0';
|
||||
|
||||
char *res = orig_mkdtemp ? orig_mkdtemp(tmp_buf) : NULL;
|
||||
if (res) {
|
||||
size_t orig_len = strlen(template);
|
||||
size_t tmp_len = strlen(tmp_buf);
|
||||
if (orig_len >= 6 && tmp_len >= 6) {
|
||||
memcpy(template + orig_len - 6, tmp_buf + tmp_len - 6, 6);
|
||||
}
|
||||
return template;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Hook tmpfile
|
||||
FILE *tmpfile(void) {
|
||||
init_ddnatv_hook();
|
||||
char template_buf[PATH_MAX];
|
||||
if (g_ddnatv_root[0] != '\0') {
|
||||
snprintf(template_buf, sizeof(template_buf), "%s/tmp/tmpfile.XXXXXX", g_ddnatv_root);
|
||||
} else {
|
||||
snprintf(template_buf, sizeof(template_buf), "/tmp/tmpfile.XXXXXX");
|
||||
}
|
||||
int fd = mkstemp(template_buf);
|
||||
if (fd < 0) return NULL;
|
||||
unlink(template_buf);
|
||||
FILE *f = fdopen(fd, "w+b");
|
||||
if (!f) {
|
||||
int saved_errno = errno;
|
||||
close(fd);
|
||||
errno = saved_errno;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
#ifndef __USE_FILE_OFFSET64
|
||||
// Programs built with _LARGEFILE64_SOURCE call this name. With _FILE_OFFSET_BITS=64 (the 32-bit ARM
|
||||
// build) the headers already export the definition above as tmpfile64.
|
||||
FILE *tmpfile64(void) {
|
||||
return tmpfile();
|
||||
}
|
||||
#endif
|
||||
|
||||
// Hook opendir
|
||||
DIR *opendir(const char *name) {
|
||||
static DIR *(*orig_opendir)(const char *) = NULL;
|
||||
if (!orig_opendir) orig_opendir = (DIR *(*)(const char *))dlsym(RTLD_NEXT, "opendir");
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path_follow(name, buf, sizeof(buf));
|
||||
return orig_opendir(target);
|
||||
}
|
||||
|
||||
// Hook stat
|
||||
int stat(const char *pathname, struct stat *statbuf) {
|
||||
static int (*orig_stat)(const char *, struct stat *) = NULL;
|
||||
if (!orig_stat) orig_stat = (int (*)(const char *, struct stat *))dlsym(RTLD_NEXT, "stat");
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path_follow(pathname, buf, sizeof(buf));
|
||||
int ret = orig_stat ? orig_stat(target, statbuf) : -1;
|
||||
if (ret != 0 && errno == ENOENT) {
|
||||
char alt[PATH_MAX];
|
||||
const char *retry = usrmerge_retry_path(target, alt, sizeof(alt));
|
||||
if (retry) ret = orig_stat(retry, statbuf);
|
||||
}
|
||||
if (ret == 0) apply_stat_emulation(statbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook lstat
|
||||
int lstat(const char *pathname, struct stat *statbuf) {
|
||||
static int (*orig_lstat)(const char *, struct stat *) = NULL;
|
||||
if (!orig_lstat) orig_lstat = (int (*)(const char *, struct stat *))dlsym(RTLD_NEXT, "lstat");
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path(pathname, buf, sizeof(buf));
|
||||
int ret = orig_lstat ? orig_lstat(target, statbuf) : -1;
|
||||
char alt[PATH_MAX];
|
||||
if (ret != 0 && errno == ENOENT) {
|
||||
const char *retry = usrmerge_retry_path(target, alt, sizeof(alt));
|
||||
if (retry) {
|
||||
ret = orig_lstat(retry, statbuf);
|
||||
if (ret == 0) target = retry;
|
||||
}
|
||||
}
|
||||
if (ret == 0 && statbuf && S_ISLNK(statbuf->st_mode) && g_ddnatv_root[0] != '\0' && target &&
|
||||
readlink_result_may_change(pathname, (long long)statbuf->st_size)) {
|
||||
char link_buf[PATH_MAX];
|
||||
static ssize_t (*orig_readlink)(const char *, char *, size_t) = NULL;
|
||||
if (!orig_readlink) orig_readlink = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink");
|
||||
ssize_t llen = orig_readlink ? orig_readlink(target, link_buf, sizeof(link_buf) - 1) : -1;
|
||||
if (llen > 0) {
|
||||
ssize_t post_len = postprocess_readlink(pathname, link_buf, llen, sizeof(link_buf));
|
||||
if (post_len > 0) {
|
||||
statbuf->st_size = post_len;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ret == 0) apply_stat_emulation(statbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook fstatat
|
||||
int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags) {
|
||||
static int (*orig_fstatat)(int, const char *, struct stat *, int) = NULL;
|
||||
if (!orig_fstatat) orig_fstatat = (int (*)(int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "fstatat");
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
char buf[PATH_MAX];
|
||||
const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
|
||||
int ret = orig_fstatat ? orig_fstatat(dirfd, target, statbuf, flags) : -1;
|
||||
char alt[PATH_MAX];
|
||||
if (ret != 0 && errno == ENOENT && pathname && pathname[0] == '/') {
|
||||
const char *retry = usrmerge_retry_path(target, alt, sizeof(alt));
|
||||
if (retry) {
|
||||
ret = orig_fstatat(dirfd, retry, statbuf, flags);
|
||||
if (ret == 0) target = retry;
|
||||
}
|
||||
}
|
||||
if (ret == 0 && statbuf && (flags & AT_SYMLINK_NOFOLLOW) && S_ISLNK(statbuf->st_mode) && g_ddnatv_root[0] != '\0' && target &&
|
||||
readlink_result_may_change(pathname, (long long)statbuf->st_size)) {
|
||||
char link_buf[PATH_MAX];
|
||||
static ssize_t (*orig_readlinkat)(int, const char *, char *, size_t) = NULL;
|
||||
if (!orig_readlinkat) orig_readlinkat = (ssize_t (*)(int, const char *, char *, size_t))dlsym(RTLD_NEXT, "readlinkat");
|
||||
ssize_t llen = orig_readlinkat ? orig_readlinkat(dirfd, target, link_buf, sizeof(link_buf) - 1) : -1;
|
||||
if (llen > 0) {
|
||||
ssize_t post_len = postprocess_readlink(pathname, link_buf, llen, sizeof(link_buf));
|
||||
if (post_len > 0) {
|
||||
statbuf->st_size = post_len;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ret == 0) apply_stat_emulation(statbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook fstat
|
||||
int fstat(int fd, struct stat *statbuf) {
|
||||
static int (*orig_fstat)(int, struct stat *) = NULL;
|
||||
if (!orig_fstat) orig_fstat = (int (*)(int, struct stat *))dlsym(RTLD_NEXT, "fstat");
|
||||
int ret = orig_fstat ? orig_fstat(fd, statbuf) : -1;
|
||||
if (ret == 0) apply_stat_emulation(statbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Glibc __xstat compatibility hooks, what programs built against glibc before 2.33 call (Firefox):
|
||||
// they present owners like stat() does, or Firefox saw $HOME owned by the app's uid while geteuid()
|
||||
// said 0 and refused to start ("as root in a regular user's session").
|
||||
int __xstat(int ver, const char *pathname, struct stat *statbuf) {
|
||||
static int (*orig___xstat)(int, const char *, struct stat *) = NULL;
|
||||
if (!orig___xstat) orig___xstat = (int (*)(int, const char *, struct stat *))dlsym(RTLD_NEXT, "__xstat");
|
||||
if (!orig___xstat) return stat(pathname, statbuf);
|
||||
char buf[PATH_MAX];
|
||||
int ret = orig___xstat(ver, rewrite_path_follow(pathname, buf, sizeof(buf)), statbuf);
|
||||
if (ret == 0) apply_stat_emulation(statbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int __xstat64(int ver, const char *pathname, struct stat64 *statbuf) {
|
||||
static int (*orig___xstat64)(int, const char *, struct stat64 *) = NULL;
|
||||
if (!orig___xstat64) orig___xstat64 = (int (*)(int, const char *, struct stat64 *))dlsym(RTLD_NEXT, "__xstat64");
|
||||
if (!orig___xstat64) return stat(pathname, (struct stat *)statbuf);
|
||||
char buf[PATH_MAX];
|
||||
int ret = orig___xstat64(ver, rewrite_path_follow(pathname, buf, sizeof(buf)), statbuf);
|
||||
if (ret == 0) apply_stat_emulation((struct stat *)statbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int __lxstat(int ver, const char *pathname, struct stat *statbuf) {
|
||||
static int (*orig___lxstat)(int, const char *, struct stat *) = NULL;
|
||||
if (!orig___lxstat) orig___lxstat = (int (*)(int, const char *, struct stat *))dlsym(RTLD_NEXT, "__lxstat");
|
||||
if (!orig___lxstat) return lstat(pathname, statbuf);
|
||||
char buf[PATH_MAX];
|
||||
int ret = orig___lxstat(ver, rewrite_path(pathname, buf, sizeof(buf)), statbuf);
|
||||
if (ret == 0) apply_stat_emulation(statbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int __lxstat64(int ver, const char *pathname, struct stat64 *statbuf) {
|
||||
static int (*orig___lxstat64)(int, const char *, struct stat64 *) = NULL;
|
||||
if (!orig___lxstat64) orig___lxstat64 = (int (*)(int, const char *, struct stat64 *))dlsym(RTLD_NEXT, "__lxstat64");
|
||||
if (!orig___lxstat64) return lstat(pathname, (struct stat *)statbuf);
|
||||
char buf[PATH_MAX];
|
||||
int ret = orig___lxstat64(ver, rewrite_path(pathname, buf, sizeof(buf)), statbuf);
|
||||
if (ret == 0) apply_stat_emulation((struct stat *)statbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int __fxstat(int ver, int fd, struct stat *statbuf) {
|
||||
static int (*orig___fxstat)(int, int, struct stat *) = NULL;
|
||||
if (!orig___fxstat) orig___fxstat = (int (*)(int, int, struct stat *))dlsym(RTLD_NEXT, "__fxstat");
|
||||
if (!orig___fxstat) return fstat(fd, statbuf);
|
||||
int ret = orig___fxstat(ver, fd, statbuf);
|
||||
if (ret == 0) apply_stat_emulation(statbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int __fxstat64(int ver, int fd, struct stat64 *statbuf) {
|
||||
static int (*orig___fxstat64)(int, int, struct stat64 *) = NULL;
|
||||
if (!orig___fxstat64) orig___fxstat64 = (int (*)(int, int, struct stat64 *))dlsym(RTLD_NEXT, "__fxstat64");
|
||||
if (!orig___fxstat64) return fstat(fd, (struct stat *)statbuf);
|
||||
int ret = orig___fxstat64(ver, fd, statbuf);
|
||||
if (ret == 0) apply_stat_emulation((struct stat *)statbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int __fxstatat(int ver, int dirfd, const char *pathname, struct stat *statbuf, int flags) {
|
||||
static int (*orig___fxstatat)(int, int, const char *, struct stat *, int) = NULL;
|
||||
if (!orig___fxstatat) orig___fxstatat = (int (*)(int, int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "__fxstatat");
|
||||
if (!orig___fxstatat) return fstatat(dirfd, pathname, statbuf, flags);
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
char buf[PATH_MAX];
|
||||
const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
|
||||
int ret = orig___fxstatat(ver, dirfd, target, statbuf, flags);
|
||||
if (ret == 0) apply_stat_emulation(statbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int __fxstatat64(int ver, int dirfd, const char *pathname, struct stat64 *statbuf, int flags) {
|
||||
static int (*orig___fxstatat64)(int, int, const char *, struct stat64 *, int) = NULL;
|
||||
if (!orig___fxstatat64) orig___fxstatat64 = (int (*)(int, int, const char *, struct stat64 *, int))dlsym(RTLD_NEXT, "__fxstatat64");
|
||||
if (!orig___fxstatat64) return fstatat(dirfd, pathname, (struct stat *)statbuf, flags);
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
char buf[PATH_MAX];
|
||||
const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
|
||||
int ret = orig___fxstatat64(ver, dirfd, target, statbuf, flags);
|
||||
if (ret == 0) apply_stat_emulation((struct stat *)statbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hook statx
|
||||
int statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf) {
|
||||
// Rust's standard library asks whether statx exists by calling it with a NULL path and buffer
|
||||
// (EFAULT means it does). glibc's header declares both as nonnull, so the compiler would drop
|
||||
// the NULL check below and read the path: every Rust program (rg, rustc, cargo...) crashed.
|
||||
__asm__ volatile("" : "+r"(pathname), "+r"(statxbuf));
|
||||
static int (*orig_statx)(int, const char *, int, unsigned int, struct statx *) = NULL;
|
||||
if (!orig_statx) orig_statx = (int (*)(int, const char *, int, unsigned int, struct statx *))dlsym(RTLD_NEXT, "statx");
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
char buf[PATH_MAX];
|
||||
const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
|
||||
int ret = orig_statx ? orig_statx(dirfd, target, flags, mask, statxbuf) : -1;
|
||||
if (ret == 0 && statxbuf && hardlinks_maybe_any()) {
|
||||
const unsigned int needed = STATX_INO | STATX_NLINK | STATX_MTIME;
|
||||
struct statx full;
|
||||
const struct statx *sx = statxbuf;
|
||||
if ((statxbuf->stx_mask & needed) != needed) {
|
||||
// The caller asked for less ("stat -c %h" for the link count only): what an emulated
|
||||
// link is recognized by
|
||||
int saved_errno = errno;
|
||||
sx = orig_statx(dirfd, target, flags, needed, &full) == 0 && (full.stx_mask & needed) == needed ? &full : NULL;
|
||||
errno = saved_errno;
|
||||
}
|
||||
if (sx) {
|
||||
struct stat st;
|
||||
memset(&st, 0, sizeof(st));
|
||||
st.st_dev = makedev(sx->stx_dev_major, sx->stx_dev_minor);
|
||||
st.st_ino = sx->stx_ino;
|
||||
st.st_nlink = sx->stx_nlink;
|
||||
st.st_mtim.tv_sec = sx->stx_mtime.tv_sec;
|
||||
st.st_mtim.tv_nsec = sx->stx_mtime.tv_nsec;
|
||||
apply_emulated_hardlink(&st);
|
||||
if (st.st_ino != sx->stx_ino || st.st_nlink != sx->stx_nlink) {
|
||||
statxbuf->stx_ino = st.st_ino;
|
||||
statxbuf->stx_nlink = st.st_nlink;
|
||||
statxbuf->stx_mask |= STATX_INO | STATX_NLINK;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ret == 0 && statxbuf) {
|
||||
uid_t uid = (uid_t)statxbuf->stx_uid;
|
||||
gid_t gid = (gid_t)statxbuf->stx_gid;
|
||||
present_owner_as_root((statxbuf->stx_mask & STATX_UID) ? &uid : NULL, (statxbuf->stx_mask & STATX_GID) ? &gid : NULL);
|
||||
statxbuf->stx_uid = (uint32_t)uid;
|
||||
statxbuf->stx_gid = (uint32_t)gid;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* !DDNATV_HOST_TEST */
|
||||
@@ -0,0 +1,465 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
||||
// parts share their static functions and are built with the same command as before).
|
||||
//
|
||||
// Static Go programs: they make their system calls themselves, so Ubuntu's paths mean nothing to
|
||||
// them. The code of gosys/gosys.c is added to their file.
|
||||
|
||||
// ---- Static Go programs -------------------------------------------------------------------
|
||||
// A Go program built without cgo (all of Ubuntu's, and the single files downloaded from a project's
|
||||
// releases) has no C library: the preloaded hook is never loaded into it, so "/etc/resolv.conf" is
|
||||
// a file of Android's that does not exist, and so is every program it starts. Go makes all of its
|
||||
// system calls but a few of its runtime's in one assembly function, internal/runtime/syscall.
|
||||
// Syscall6, which loads the number and the arguments from the stack and executes "svc #0".
|
||||
//
|
||||
// Before such a program (AArch64, Go 1.19 or newer) starts, The native userland changes its file in place, once:
|
||||
// - the code of gosys/gosys.c is put after the program's code, in the zero padding that the Go
|
||||
// linker leaves between its segments (the segment is made longer, and executable if it only
|
||||
// was readable),
|
||||
// - zero-filled memory for it is added to the end of the program's data segment,
|
||||
// - the "svc #0" of Syscall6 becomes a branch to a few instructions in that code, which call it
|
||||
// and go back to the instruction after the "svc".
|
||||
// The code maps the paths of the calls into the rootfs, as the hook does for C programs, and starts
|
||||
// the programs of the rootfs through the hook. The file keeps its size, its times and its mode;
|
||||
// only the program headers, the padding and one instruction differ. The file is changed back and
|
||||
// patched again when the code of a newer The native userland differs (the trailer says what to undo).
|
||||
|
||||
#ifdef DDNATV_HAVE_GOSYS
|
||||
|
||||
#define GO_MAX_PH 32
|
||||
#define GO_PF_X 1u
|
||||
#define GO_PF_W 2u
|
||||
#define GO_ET_EXEC 2
|
||||
#define GO_ET_DYN 3
|
||||
|
||||
struct go_ph {
|
||||
uint32_t type, flags;
|
||||
uint64_t offset, vaddr, filesz, memsz;
|
||||
uint64_t pos; // where the entry is in the file
|
||||
};
|
||||
|
||||
static uint64_t go_rd64(const unsigned char *p) { return (uint64_t)rd32(p) | ((uint64_t)rd32(p + 4) << 32); }
|
||||
static void go_wr64(unsigned char *p, uint64_t v) { wr32(p, (uint32_t)v); wr32(p + 4, (uint32_t)(v >> 32)); }
|
||||
|
||||
// The program headers of a 64-bit little-endian ELF; their number, or -1
|
||||
static int go_read_phdrs(int fd, struct go_ph *ph, uint16_t *etype, uint16_t *machine) {
|
||||
unsigned char eh[64];
|
||||
if (pread(fd, eh, sizeof(eh), 0) != (ssize_t)sizeof(eh) || memcmp(eh, "\x7f" "ELF", 4) != 0 || eh[4] != 2 || eh[5] != 1)
|
||||
return -1;
|
||||
*etype = rd16(eh + 16);
|
||||
*machine = rd16(eh + 18);
|
||||
uint64_t phoff = go_rd64(eh + 32);
|
||||
uint16_t phentsize = rd16(eh + 54), phnum = rd16(eh + 56);
|
||||
if (phentsize != 56 || phnum == 0 || phnum > GO_MAX_PH) return -1;
|
||||
for (int i = 0; i < phnum; i++) {
|
||||
unsigned char e[56];
|
||||
uint64_t pos = phoff + (uint64_t)i * 56;
|
||||
if (pread(fd, e, sizeof(e), (off_t)pos) != (ssize_t)sizeof(e)) return -1;
|
||||
ph[i].type = rd32(e);
|
||||
ph[i].flags = rd32(e + 4);
|
||||
ph[i].offset = go_rd64(e + 8);
|
||||
ph[i].vaddr = go_rd64(e + 16);
|
||||
ph[i].filesz = go_rd64(e + 32);
|
||||
ph[i].memsz = go_rd64(e + 40);
|
||||
ph[i].pos = pos;
|
||||
}
|
||||
return phnum;
|
||||
}
|
||||
|
||||
// The instructions of Syscall6 up to its system call: x8 and x0 to x5 loaded from the stack
|
||||
// (ldr x8, [sp, #8]; ldr x0, [sp, #16] ... ldr x5, [sp, #56]), then "svc #0" and "cmn x0, #4095"
|
||||
static const uint32_t k_go_syscall6[7] = {
|
||||
0xf94007e8u, 0xf9400be0u, 0xf9400fe1u, 0xf94013e2u, 0xf94017e3u, 0xf9401be4u, 0xf9401fe5u,
|
||||
};
|
||||
#define GO_SVC 0xd4000001u
|
||||
#define GO_CMN_X0_4095 0xb13ffc1fu
|
||||
|
||||
// Where Syscall6 makes its call: the file offset of the instruction after the seven loads (the
|
||||
// "svc #0", or the branch that replaced it). *found counts the places; the first is returned.
|
||||
static int64_t go_find_syscall6(int fd, const struct go_ph *ph, int n, int *found) {
|
||||
int64_t first = -1;
|
||||
*found = 0;
|
||||
enum { CHUNK = 1 << 20, KEEP = 36 };
|
||||
unsigned char *buf = (unsigned char *)malloc(CHUNK + KEEP);
|
||||
if (!buf) return -1;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (ph[i].type != 1 /* PT_LOAD */ || !(ph[i].flags & GO_PF_X) || ph[i].filesz == 0) continue;
|
||||
uint64_t pos = 0;
|
||||
while (pos < ph[i].filesz) {
|
||||
uint64_t want = ph[i].filesz - pos;
|
||||
if (want > CHUNK + KEEP) want = CHUNK + KEEP;
|
||||
ssize_t got = pread(fd, buf, (size_t)want, (off_t)(ph[i].offset + pos));
|
||||
if (got <= KEEP) break;
|
||||
for (ssize_t o = 0; o + 36 <= got; o += 4) {
|
||||
if (rd32(buf + o) != k_go_syscall6[0]) continue;
|
||||
int same = 1;
|
||||
for (int k = 1; k < 7 && same; k++) same = rd32(buf + o + 4 * k) == k_go_syscall6[k];
|
||||
if (!same || rd32(buf + o + 32) != GO_CMN_X0_4095) continue;
|
||||
if (*found == 0) first = (int64_t)(ph[i].offset + pos + (uint64_t)o + 28);
|
||||
(*found)++;
|
||||
}
|
||||
if (pos + (uint64_t)got >= ph[i].filesz) break;
|
||||
pos += (uint64_t)got - (KEEP - 4); // the next chunk starts where the last place of this could begin, no place is split or counted twice
|
||||
}
|
||||
}
|
||||
free(buf);
|
||||
return first;
|
||||
}
|
||||
|
||||
// "b <delta>" / "bl <delta>" for a distance in bytes, or 0 when it does not fit
|
||||
static uint32_t go_branch(uint32_t base, int64_t delta) {
|
||||
if ((delta & 3) != 0 || delta >= (1LL << 27) || delta < -(1LL << 27)) return 0;
|
||||
return base | (uint32_t)((delta >> 2) & 0x3ffffff);
|
||||
}
|
||||
|
||||
static uint64_t go_align_up(uint64_t v, uint64_t a) { return (v + a - 1) & ~(a - 1); }
|
||||
|
||||
// The strings the added code needs, as they are now
|
||||
static int go_make_header(struct gosys_hdr *h) {
|
||||
if (g_ddnatv_root_len == 0 || g_ddnatv_root_len >= GOSYS_ROOT_MAX) return -1;
|
||||
memcpy(h->root, g_ddnatv_root, g_ddnatv_root_len);
|
||||
h->root_len = (uint32_t)g_ddnatv_root_len;
|
||||
|
||||
char buf[PATH_MAX];
|
||||
if (find_dynamic_linker_for_machine(g_ddnatv_root, EM_AARCH64, buf, sizeof(buf)) && strlen(buf) < GOSYS_PATH_MAX) {
|
||||
h->ldso_len = (uint32_t)strlen(buf);
|
||||
memcpy(h->ldso, buf, h->ldso_len);
|
||||
}
|
||||
static const char *const hooks[] = { "/usr/lib/libddnatv-hook.so", "/lib/libddnatv-hook.so" };
|
||||
for (size_t i = 0; i < sizeof(hooks) / sizeof(hooks[0]); i++) {
|
||||
size_t n = ddnatv_append(buf, sizeof(buf), 0, g_ddnatv_root, g_ddnatv_root_len);
|
||||
n = ddnatv_append(buf, sizeof(buf), n, hooks[i], strlen(hooks[i]));
|
||||
if (n < GOSYS_PATH_MAX && access(buf, F_OK) == 0) {
|
||||
h->hook_len = (uint32_t)n;
|
||||
memcpy(h->hook, buf, n);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// The program that starts the others: bash, which can give them the argv[0] they are asked for
|
||||
static const char *const runners[] = { "/usr/bin/bash", "/bin/bash", "/usr/bin/env", "/bin/env" };
|
||||
for (size_t i = 0; i < sizeof(runners) / sizeof(runners[0]); i++) {
|
||||
size_t n = ddnatv_append(buf, sizeof(buf), 0, g_ddnatv_root, g_ddnatv_root_len);
|
||||
n = ddnatv_append(buf, sizeof(buf), n, runners[i], strlen(runners[i]));
|
||||
if (n < GOSYS_PATH_MAX && access(buf, F_OK) == 0) {
|
||||
h->runner_len = (uint32_t)n;
|
||||
memcpy(h->runner, buf, n);
|
||||
if (i < 2) h->flags |= GOSYS_FLAG_RUNNER_IS_BASH;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// The directories the programs of an AArch64 rootfs load their libraries from
|
||||
static const int dirs[] = { 0, 1, 4, 5, 6 };
|
||||
size_t used = 0;
|
||||
for (size_t i = 0; i < sizeof(dirs) / sizeof(dirs[0]); i++) {
|
||||
const char *d = k_rootfs_lib_dirs[dirs[i]];
|
||||
if (i > 0) used = ddnatv_append(buf, sizeof(buf), used, ":", 1);
|
||||
used = ddnatv_append(buf, sizeof(buf), used, g_ddnatv_root, g_ddnatv_root_len);
|
||||
used = ddnatv_append(buf, sizeof(buf), used, d, strlen(d));
|
||||
}
|
||||
if (used < GOSYS_LIBS_MAX) {
|
||||
h->libs_len = (uint32_t)used;
|
||||
memcpy(h->libs, buf, used);
|
||||
}
|
||||
if (h->ldso_len == 0 || h->runner_len == 0) h->runner_len = 0; // Ubuntu programs cannot be started
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int go_pwrite_all(int fd, const void *buf, size_t len, uint64_t off) {
|
||||
return pwrite(fd, buf, len, (off_t)off) == (ssize_t)len ? 0 : -1;
|
||||
}
|
||||
|
||||
static int go_open_for_write(int fd, const struct stat *st, const char *path, int *made_writable) {
|
||||
int wfd = open(path, O_WRONLY | O_CLOEXEC);
|
||||
if (wfd < 0 && errno == EACCES && fchmod(fd, (st->st_mode | S_IWUSR) & 07777) == 0) {
|
||||
*made_writable = 1;
|
||||
wfd = open(path, O_WRONLY | O_CLOEXEC);
|
||||
}
|
||||
return wfd; // ETXTBSY: the program is running right now, patched at its next start
|
||||
}
|
||||
|
||||
// The bytes of the added code for a given place: the code with its header (the strings, and where
|
||||
// the scratch memory is), the instructions that connect the call site to it, and the trailer that
|
||||
// says what was changed to put it there.
|
||||
static void go_build_image(unsigned char *img, const struct gosys_hdr *strings, uint64_t cave_v, uint64_t scratch_v,
|
||||
uint32_t to_entry, uint32_t back, const struct gosys_trailer *info) {
|
||||
memcpy(img, gosys_blob, GOSYS_BLOB_SIZE);
|
||||
struct gosys_hdr h = *strings;
|
||||
h.scratch_rel = (int64_t)scratch_v - (int64_t)cave_v;
|
||||
memcpy(img, &h, sizeof(h));
|
||||
unsigned char *th = img + GOSYS_THUNKS_OFF;
|
||||
wr32(th + 0, 0xd10043ffu); // sub sp, sp, #16
|
||||
wr32(th + 4, 0xf90003feu); // str x30, [sp]
|
||||
wr32(th + 8, 0xaa0803e7u); // mov x7, x8 (the number is the eighth argument)
|
||||
wr32(th + 12, to_entry); // bl entry
|
||||
wr32(th + 16, 0xf94003feu); // ldr x30, [sp]
|
||||
wr32(th + 20, 0x910043ffu); // add sp, sp, #16
|
||||
wr32(th + 24, back); // b <the instruction after the call>
|
||||
struct gosys_trailer t;
|
||||
memcpy(&t, img + GOSYS_BLOB_SIZE - sizeof(t), sizeof(t));
|
||||
t.version = GOSYS_VERSION;
|
||||
t.total_size = GOSYS_BLOB_SIZE;
|
||||
t.text_orig_filesz = info->text_orig_filesz;
|
||||
t.text_orig_flags = info->text_orig_flags;
|
||||
t.rw_orig_memsz = info->rw_orig_memsz;
|
||||
t.text_index = info->text_index;
|
||||
t.rw_index = info->rw_index;
|
||||
t.site_file_off = info->site_file_off;
|
||||
memcpy(img + GOSYS_BLOB_SIZE - sizeof(t), &t, sizeof(t));
|
||||
}
|
||||
|
||||
// A short fingerprint of the added code, so that a program patched by an older The native userland is looked at
|
||||
// again (the marker of a prepared program has it)
|
||||
static uint32_t go_blob_fingerprint(void) {
|
||||
static uint32_t fp;
|
||||
if (fp == 0) {
|
||||
uint32_t h = 2166136261u;
|
||||
for (size_t i = 0; i < GOSYS_BLOB_SIZE; i++) h = (h ^ gosys_blob[i]) * 16777619u;
|
||||
fp = h ? h : 1;
|
||||
}
|
||||
return fp;
|
||||
}
|
||||
|
||||
// Gives the program its old segments and call back: the added code is zeroed, the data segment
|
||||
// is shortened, the instruction is a "svc #0" again.
|
||||
static int go_undo(int wfd, struct go_ph *ph, int cave_seg, const struct gosys_trailer *tr, int64_t site_off) {
|
||||
uint64_t cave_off = ph[cave_seg].offset + ph[cave_seg].filesz - tr->total_size;
|
||||
unsigned char word[4];
|
||||
wr32(word, GO_SVC);
|
||||
if (go_pwrite_all(wfd, word, 4, (uint64_t)site_off) != 0) return -1;
|
||||
unsigned char sz[16];
|
||||
go_wr64(sz, tr->text_orig_filesz);
|
||||
go_wr64(sz + 8, tr->text_orig_filesz);
|
||||
if (go_pwrite_all(wfd, sz, 16, ph[cave_seg].pos + 32) != 0) return -1;
|
||||
wr32(word, tr->text_orig_flags);
|
||||
if (go_pwrite_all(wfd, word, 4, ph[cave_seg].pos + 4) != 0) return -1;
|
||||
if (tr->rw_index < GO_MAX_PH) {
|
||||
unsigned char mem[8];
|
||||
go_wr64(mem, tr->rw_orig_memsz);
|
||||
if (go_pwrite_all(wfd, mem, 8, ph[tr->rw_index].pos + 40) != 0) return -1;
|
||||
}
|
||||
unsigned char *zero = (unsigned char *)calloc(1, tr->total_size);
|
||||
if (!zero) return -1;
|
||||
int rc = go_pwrite_all(wfd, zero, tr->total_size, cave_off);
|
||||
free(zero);
|
||||
return rc;
|
||||
}
|
||||
|
||||
// DDNATV_NO_GOSYS=1 (any value but empty or 0): no code is added to Go programs, and the code that is
|
||||
// in one is taken out again, so that a program can be run as it was built, to see whether a fault
|
||||
// is the program's or the code's. The marker of a prepared program says it (static_marker).
|
||||
static int go_disabled(void) {
|
||||
const char *v = getenv("DDNATV_NO_GOSYS");
|
||||
return v && v[0] != '\0' && strcmp(v, "0") != 0;
|
||||
}
|
||||
|
||||
// The three branches that connect the call site and the code: site -> thunk, thunk -> entry (bl),
|
||||
// thunk -> the instruction after the site. 0 when one of them is too far.
|
||||
static int go_branches(uint64_t cave_v, uint64_t site_v, uint32_t *to_thunk, uint32_t *to_entry, uint32_t *back) {
|
||||
uint64_t thunk_v = cave_v + GOSYS_THUNKS_OFF;
|
||||
*to_thunk = go_branch(0x14000000u, (int64_t)thunk_v - (int64_t)site_v);
|
||||
*to_entry = go_branch(0x94000000u, (int64_t)(cave_v + GOSYS_ENTRY_OFF) - (int64_t)(thunk_v + 12));
|
||||
*back = go_branch(0x14000000u, (int64_t)(site_v + 4) - (int64_t)(thunk_v + 24));
|
||||
return *to_thunk && *to_entry && *back;
|
||||
}
|
||||
|
||||
// Adds the code to the program, or brings it up to date. Returns 1 when it changed the file, 0 when
|
||||
// there was nothing to do (not a Go program of a kind the code can serve, or already done), -1 when
|
||||
// the file could not be changed (it is started as it is).
|
||||
static int prepare_go_program(int fd, const char *path, const struct stat *st) {
|
||||
struct go_ph ph[GO_MAX_PH];
|
||||
uint16_t etype = 0, machine = 0;
|
||||
int n = go_read_phdrs(fd, ph, &etype, &machine);
|
||||
if (n < 0) return -1;
|
||||
if (machine != EM_AARCH64 || (etype != GO_ET_EXEC && etype != GO_ET_DYN)) return 0;
|
||||
|
||||
int found = 0;
|
||||
int64_t site_off = go_find_syscall6(fd, ph, n, &found);
|
||||
if (found != 1) return 0; // not a Go of 1.19 or newer (or its function is not there once)
|
||||
|
||||
// The loadable segments, by address
|
||||
int order[GO_MAX_PH], nl = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (ph[i].type != 1 /* PT_LOAD */) continue;
|
||||
int k = nl++;
|
||||
while (k > 0 && ph[order[k - 1]].vaddr > ph[i].vaddr) {
|
||||
order[k] = order[k - 1];
|
||||
k--;
|
||||
}
|
||||
order[k] = i;
|
||||
}
|
||||
if (nl < 2) return 0;
|
||||
|
||||
// The call site must be in a segment that is mapped as code
|
||||
uint64_t site_v = 0;
|
||||
for (int i = 0; i < nl; i++) {
|
||||
const struct go_ph *s = &ph[order[i]];
|
||||
if ((s->flags & GO_PF_X) && (uint64_t)site_off >= s->offset && (uint64_t)site_off < s->offset + s->filesz)
|
||||
site_v = s->vaddr + ((uint64_t)site_off - s->offset);
|
||||
}
|
||||
if (site_v == 0) return 0;
|
||||
|
||||
struct gosys_hdr strings;
|
||||
memset(&strings, 0, sizeof(strings));
|
||||
memcpy(strings.magic, "CXGOSYS1", 8);
|
||||
strings.version = GOSYS_VERSION;
|
||||
if (go_make_header(&strings) != 0) return 0;
|
||||
|
||||
unsigned char *img = (unsigned char *)malloc(GOSYS_BLOB_SIZE);
|
||||
unsigned char *have = (unsigned char *)malloc(GOSYS_BLOB_SIZE);
|
||||
if (!img || !have) {
|
||||
free(img);
|
||||
free(have);
|
||||
return -1;
|
||||
}
|
||||
int wfd = -1, made_writable = 0, changed = 0, rc = 0;
|
||||
unsigned char word[4];
|
||||
|
||||
// An earlier run's code: it ends the segment it is in, and says what it changed
|
||||
int cave_seg = -1;
|
||||
struct gosys_trailer tr;
|
||||
memset(&tr, 0, sizeof(tr));
|
||||
for (int i = 0; i < nl && cave_seg < 0; i++) {
|
||||
const struct go_ph *s = &ph[order[i]];
|
||||
if (s->filesz < sizeof(tr) + sizeof(struct gosys_hdr)) continue;
|
||||
struct gosys_trailer t;
|
||||
if (pread(fd, &t, sizeof(t), (off_t)(s->offset + s->filesz - sizeof(t))) != (ssize_t)sizeof(t)) continue;
|
||||
if (memcmp(t.magic, "CXGOEND1", 8) != 0 || t.total_size > s->filesz || t.total_size < sizeof(t) + sizeof(struct gosys_hdr)) continue;
|
||||
tr = t;
|
||||
cave_seg = order[i];
|
||||
}
|
||||
|
||||
if (go_disabled()) {
|
||||
if (cave_seg >= 0) {
|
||||
wfd = go_open_for_write(fd, st, path, &made_writable);
|
||||
if (wfd < 0) {
|
||||
rc = -1;
|
||||
} else {
|
||||
rc = go_undo(wfd, ph, cave_seg, &tr, site_off);
|
||||
changed = rc == 0;
|
||||
}
|
||||
}
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (cave_seg >= 0) {
|
||||
uint64_t cave_off = ph[cave_seg].offset + ph[cave_seg].filesz - tr.total_size;
|
||||
uint64_t cave_v = ph[cave_seg].vaddr + (cave_off - ph[cave_seg].offset);
|
||||
int same = 0, site_ok = 0;
|
||||
uint32_t to_thunk = 0, to_entry = 0, back = 0;
|
||||
// Is it what would be put there now? (the code of an older The native userland differs)
|
||||
if (tr.total_size == GOSYS_BLOB_SIZE && tr.rw_index < (uint32_t)n && ph[tr.rw_index].type == 1 &&
|
||||
go_branches(cave_v, site_v, &to_thunk, &to_entry, &back)) {
|
||||
uint64_t scratch_v = go_align_up(ph[tr.rw_index].vaddr + tr.rw_orig_memsz, 64);
|
||||
go_build_image(img, &strings, cave_v, scratch_v, to_entry, back, &tr);
|
||||
same = pread(fd, have, GOSYS_BLOB_SIZE, (off_t)cave_off) == (ssize_t)GOSYS_BLOB_SIZE &&
|
||||
memcmp(have, img, GOSYS_BLOB_SIZE) == 0 &&
|
||||
ph[tr.rw_index].memsz == scratch_v - ph[tr.rw_index].vaddr + GOSYS_SCRATCH_SIZE;
|
||||
}
|
||||
unsigned char cur[4];
|
||||
site_ok = same && pread(fd, cur, 4, (off_t)site_off) == 4 && rd32(cur) == to_thunk;
|
||||
if (same && site_ok) goto done;
|
||||
|
||||
wfd = go_open_for_write(fd, st, path, &made_writable);
|
||||
if (wfd < 0) {
|
||||
rc = -1;
|
||||
goto done;
|
||||
}
|
||||
if (same) {
|
||||
// The code is there, only the branch is not (a patch that was cut short)
|
||||
wr32(word, to_thunk);
|
||||
rc = go_pwrite_all(wfd, word, 4, (uint64_t)site_off);
|
||||
changed = rc == 0;
|
||||
goto done;
|
||||
}
|
||||
// An older code, or other strings: back to the plain program, then patch again
|
||||
if (go_undo(wfd, ph, cave_seg, &tr, site_off) != 0) {
|
||||
rc = -1;
|
||||
goto done;
|
||||
}
|
||||
close(wfd);
|
||||
wfd = -1;
|
||||
if (made_writable) fchmod(fd, st->st_mode & 07777);
|
||||
free(img);
|
||||
free(have);
|
||||
return prepare_go_program(fd, path, st);
|
||||
}
|
||||
|
||||
// A place for the code: the end of a segment that is followed by padding up to the next one
|
||||
uint64_t total = GOSYS_BLOB_SIZE;
|
||||
int text = -1;
|
||||
uint64_t cave_v = 0, cave_off = 0;
|
||||
for (int pass = 0; pass < 2 && text < 0; pass++) {
|
||||
for (int k = 0; k + 1 < nl && text < 0; k++) {
|
||||
const struct go_ph *s = &ph[order[k]], *nx = &ph[order[k + 1]];
|
||||
if (pass == 0 ? !(s->flags & GO_PF_X) : (s->flags & GO_PF_W)) continue;
|
||||
if (s->memsz != s->filesz || s->filesz == 0) continue;
|
||||
uint64_t v = go_align_up(s->vaddr + s->filesz, 64);
|
||||
uint64_t off = s->offset + (v - s->vaddr);
|
||||
if (v + total > nx->vaddr || (nx->vaddr & 0x3fff) != 0) continue;
|
||||
if (off + total > nx->offset || off + total > (uint64_t)st->st_size) continue;
|
||||
// Only padding may be written over
|
||||
uint64_t pad_from = s->offset + s->filesz, pad_len = off + total - pad_from;
|
||||
unsigned char *pad = (unsigned char *)malloc(pad_len);
|
||||
if (!pad) continue;
|
||||
int zero = pread(fd, pad, pad_len, (off_t)pad_from) == (ssize_t)pad_len;
|
||||
for (uint64_t i = 0; zero && i < pad_len; i++) zero = pad[i] == 0;
|
||||
free(pad);
|
||||
if (!zero) continue;
|
||||
text = order[k];
|
||||
cave_v = v;
|
||||
cave_off = off;
|
||||
}
|
||||
}
|
||||
int rw = order[nl - 1];
|
||||
uint32_t to_thunk = 0, to_entry = 0, back = 0;
|
||||
if (text < 0 || !(ph[rw].flags & GO_PF_W) || rw == text || !go_branches(cave_v, site_v, &to_thunk, &to_entry, &back)) goto done;
|
||||
|
||||
uint64_t scratch_v = go_align_up(ph[rw].vaddr + ph[rw].memsz, 64);
|
||||
struct gosys_trailer info;
|
||||
memset(&info, 0, sizeof(info));
|
||||
info.text_orig_filesz = ph[text].filesz;
|
||||
info.text_orig_flags = ph[text].flags;
|
||||
info.rw_orig_memsz = ph[rw].memsz;
|
||||
info.text_index = (uint32_t)text;
|
||||
info.rw_index = (uint32_t)rw;
|
||||
info.site_file_off = (uint64_t)site_off;
|
||||
go_build_image(img, &strings, cave_v, scratch_v, to_entry, back, &info);
|
||||
|
||||
wfd = go_open_for_write(fd, st, path, &made_writable);
|
||||
if (wfd < 0) {
|
||||
rc = -1;
|
||||
goto done;
|
||||
}
|
||||
// The code first and the call site last: whatever is cut short leaves a program that runs
|
||||
unsigned char sz[16];
|
||||
unsigned char mem[8];
|
||||
go_wr64(mem, scratch_v - ph[rw].vaddr + GOSYS_SCRATCH_SIZE);
|
||||
go_wr64(sz, cave_v + GOSYS_BLOB_SIZE - ph[text].vaddr);
|
||||
go_wr64(sz + 8, cave_v + GOSYS_BLOB_SIZE - ph[text].vaddr);
|
||||
rc = go_pwrite_all(wfd, img, GOSYS_BLOB_SIZE, cave_off);
|
||||
if (rc == 0) rc = go_pwrite_all(wfd, mem, 8, ph[rw].pos + 40);
|
||||
if (rc == 0) rc = go_pwrite_all(wfd, sz, 16, ph[text].pos + 32);
|
||||
if (rc == 0 && !(ph[text].flags & GO_PF_X)) {
|
||||
wr32(word, ph[text].flags | GO_PF_X);
|
||||
rc = go_pwrite_all(wfd, word, 4, ph[text].pos + 4);
|
||||
}
|
||||
if (rc == 0) {
|
||||
wr32(word, to_thunk);
|
||||
rc = go_pwrite_all(wfd, word, 4, (uint64_t)site_off);
|
||||
}
|
||||
changed = rc == 0;
|
||||
|
||||
done:
|
||||
free(img);
|
||||
free(have);
|
||||
if (wfd >= 0) {
|
||||
// Keep the times: make and friends must not see a rebuilt program.
|
||||
const struct timespec times[2] = { st->st_atim, st->st_mtim };
|
||||
futimens(wfd, times);
|
||||
close(wfd);
|
||||
}
|
||||
if (made_writable) fchmod(fd, st->st_mode & 07777);
|
||||
if (rc != 0) return -1;
|
||||
return changed ? 1 : 0;
|
||||
}
|
||||
|
||||
#endif /* DDNATV_HAVE_GOSYS */
|
||||
@@ -0,0 +1,628 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
||||
// parts share their static functions and are built with the same command as before).
|
||||
//
|
||||
// Hard links: link() falls back to a copy that reports the link count of a real link, in every
|
||||
// process of the rootfs.
|
||||
|
||||
// Emulated hard links. Android does not let apps create hard links, so link() falls back to a copy
|
||||
// (ddnatv_emulate_hardlinkat). Programs that check the result through the link count, such as the
|
||||
// lock files of shadow (link(file.PID, file.lock), then stat(file.PID).st_nlink == 2), and those
|
||||
// that look for files linked to each other (tar, rsync -H, du, find -samefile), are shown what a
|
||||
// real link would give: every copy of one emulated link reports the inode number of the original
|
||||
// and the number of copies as its link count.
|
||||
//
|
||||
// The copies are recorded in a table that every process of the rootfs maps
|
||||
// ($DDNATV_ROOT/var/lib/ddnatv/hardlinks), so "ln a b" in one process and "stat a" in the next
|
||||
// agree, also after the app restarts. Copies are recorded by device and inode, so a relative name,
|
||||
// an absolute name and an open descriptor of the same file all match, and with their modification
|
||||
// time: a copy written to since (it no longer shares the original's content) or a new file that got
|
||||
// the inode number of a removed copy (removed outside the hook, e.g. in root mode) do not match.
|
||||
//
|
||||
// Readers (every stat) take no lock: a sequence number that writers make odd while they change the
|
||||
// table tells them to read again. Writers (link, unlink, rename) take an fcntl lock on the file.
|
||||
// Without a rootfs (or when the file cannot be made) the table is private to the process and its
|
||||
// children, as it was before.
|
||||
|
||||
#define DDNATV_HL_MAGIC 0x6b6e6c43u // "Clnk"
|
||||
#define DDNATV_HL_VERSION 1u
|
||||
#define DDNATV_HL_SLOTS 8192u // a power of two
|
||||
#define DDNATV_HL_MAX_USED 6144u // at most 3/4 full, so that lookups stay short
|
||||
#define DDNATV_HL_EMPTY 0u
|
||||
#define DDNATV_HL_USED 1u
|
||||
#define DDNATV_HL_DELETED 2u
|
||||
|
||||
typedef struct {
|
||||
uint64_t dev, ino; // this copy
|
||||
int64_t mtime_sec, mtime_nsec; // its modification time when it was linked (compared in whole
|
||||
// seconds: stat() and statx() need not agree on the fraction)
|
||||
uint64_t group_dev, group_ino; // the original, whose inode number every copy reports
|
||||
uint32_t state;
|
||||
uint32_t nlink; // copies in the group
|
||||
uint32_t pad[2];
|
||||
} ddnatv_hl_slot_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t magic, version;
|
||||
uint32_t seq; // odd while a writer changes the table
|
||||
uint32_t used, deleted;
|
||||
uint32_t pad[11];
|
||||
ddnatv_hl_slot_t slots[DDNATV_HL_SLOTS];
|
||||
} ddnatv_hl_table_t;
|
||||
|
||||
// The file and the mapping, in whole 64 KB: a size that is a multiple of every page size
|
||||
#define DDNATV_HL_MAP_SIZE ((sizeof(ddnatv_hl_table_t) + 0xffff) & ~(size_t)0xffff)
|
||||
|
||||
static ddnatv_hl_table_t *g_hl_table = NULL; // set once, then never changed (until a test resets it)
|
||||
static int g_hl_shared = 0; // mapped from the rootfs' file
|
||||
static long g_hl_next_try = 0; // when to look for the file again (monotonic seconds)
|
||||
static pthread_mutex_t g_hardlinks_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
static void init_ddnatv_hook(void);
|
||||
#endif
|
||||
|
||||
static size_t hl_table_path(char *buf, size_t size) {
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
// The rootfs is known once a path was rewritten; "ln a b" or "stat a" name only relative ones
|
||||
init_ddnatv_hook();
|
||||
#endif
|
||||
if (g_ddnatv_root_len == 0) return 0;
|
||||
size_t n = ddnatv_append(buf, size, 0, g_ddnatv_root, g_ddnatv_root_len);
|
||||
static const char rel[] = "/var/lib/ddnatv/hardlinks";
|
||||
n = ddnatv_append(buf, size, n, rel, sizeof(rel) - 1);
|
||||
return n == g_ddnatv_root_len + sizeof(rel) - 1 ? n : 0;
|
||||
}
|
||||
|
||||
static long hl_now_seconds(void) {
|
||||
struct timespec ts;
|
||||
#ifdef CLOCK_MONOTONIC_COARSE
|
||||
if (clock_gettime(CLOCK_MONOTONIC_COARSE, &ts) == 0) return (long)ts.tv_sec;
|
||||
#endif
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) return (long)ts.tv_sec;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int hl_open_file(const char *path, int create) {
|
||||
return (int)syscall(SYS_openat, AT_FDCWD, path, O_RDWR | O_CLOEXEC | (create ? O_CREAT : 0), 0600);
|
||||
}
|
||||
|
||||
// Takes the writers' lock on the table's file (blocking); -1 when there is no file to lock.
|
||||
static int hl_lock_file(void) {
|
||||
if (!g_hl_shared) return -1;
|
||||
char path[PATH_MAX];
|
||||
if (!hl_table_path(path, sizeof(path))) return -1;
|
||||
int fd = hl_open_file(path, 0);
|
||||
if (fd < 0) return -1;
|
||||
struct flock fl;
|
||||
memset(&fl, 0, sizeof(fl));
|
||||
fl.l_type = F_WRLCK;
|
||||
fl.l_whence = SEEK_SET;
|
||||
while (fcntl(fd, F_SETLKW, &fl) != 0 && errno == EINTR) {
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
static void hl_unlock_file(int fd) {
|
||||
if (fd >= 0) syscall(SYS_close, fd); // closing the file drops the lock
|
||||
}
|
||||
|
||||
static void hl_init_header(ddnatv_hl_table_t *t) {
|
||||
memset(t, 0, sizeof(*t));
|
||||
t->version = DDNATV_HL_VERSION;
|
||||
__atomic_store_n(&t->magic, DDNATV_HL_MAGIC, __ATOMIC_RELEASE);
|
||||
}
|
||||
|
||||
// The size of an open file. Not fstat(): this library's own fstat() looks into the table, which
|
||||
// would wait for the lock that the caller holds.
|
||||
static off_t hl_file_size(int fd) {
|
||||
return lseek(fd, 0, SEEK_END);
|
||||
}
|
||||
|
||||
// Maps the table. Readers (create = 0) get NULL while no process has made the file yet, and look
|
||||
// for it again a second later; writers make it, or fall back to a table of this process. Readers
|
||||
// never wait for the lock (a stat() while it is held, or in a child forked while another thread
|
||||
// held it, would wait forever): they find no table then.
|
||||
static ddnatv_hl_table_t *hl_table(int create) {
|
||||
ddnatv_hl_table_t *t = __atomic_load_n(&g_hl_table, __ATOMIC_ACQUIRE);
|
||||
if (t) return t;
|
||||
if (!create && __atomic_load_n(&g_hl_next_try, __ATOMIC_RELAXED) > hl_now_seconds()) return NULL;
|
||||
int saved_errno = errno;
|
||||
if (create) {
|
||||
pthread_mutex_lock(&g_hardlinks_lock);
|
||||
} else if (pthread_mutex_trylock(&g_hardlinks_lock) != 0) {
|
||||
errno = saved_errno;
|
||||
return NULL;
|
||||
}
|
||||
t = g_hl_table;
|
||||
if (!t) {
|
||||
char path[PATH_MAX];
|
||||
path[0] = '\0';
|
||||
int have_path = hl_table_path(path, sizeof(path)) != 0;
|
||||
int fd = have_path ? hl_open_file(path, 0) : -1;
|
||||
if (fd < 0 && create && path[0]) {
|
||||
// var/lib/ddnatv may not exist yet
|
||||
char dir[PATH_MAX];
|
||||
size_t n = ddnatv_append(dir, sizeof(dir), 0, path, strlen(path) - strlen("/hardlinks"));
|
||||
if (n > 0) syscall(SYS_mkdirat, AT_FDCWD, dir, 0700);
|
||||
fd = hl_open_file(path, 1);
|
||||
}
|
||||
if (fd >= 0) {
|
||||
off_t size = hl_file_size(fd);
|
||||
int ok = size >= 0;
|
||||
if (ok && (size_t)size < DDNATV_HL_MAP_SIZE) {
|
||||
// New (or an older, shorter format): made by a writer, under the lock
|
||||
ok = 0;
|
||||
if (create) {
|
||||
struct flock fl;
|
||||
memset(&fl, 0, sizeof(fl));
|
||||
fl.l_type = F_WRLCK;
|
||||
fl.l_whence = SEEK_SET;
|
||||
while (fcntl(fd, F_SETLKW, &fl) != 0 && errno == EINTR) {
|
||||
}
|
||||
size = hl_file_size(fd);
|
||||
ok = size >= 0 &&
|
||||
((size_t)size >= DDNATV_HL_MAP_SIZE || ftruncate(fd, DDNATV_HL_MAP_SIZE) == 0);
|
||||
}
|
||||
}
|
||||
if (ok) {
|
||||
void *m = mmap(NULL, DDNATV_HL_MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (m != MAP_FAILED) {
|
||||
t = (ddnatv_hl_table_t *)m;
|
||||
if (t->magic != DDNATV_HL_MAGIC || t->version != DDNATV_HL_VERSION) {
|
||||
if (create) {
|
||||
// Still under the lock taken above when the file was new; otherwise take it
|
||||
struct flock fl;
|
||||
memset(&fl, 0, sizeof(fl));
|
||||
fl.l_type = F_WRLCK;
|
||||
fl.l_whence = SEEK_SET;
|
||||
while (fcntl(fd, F_SETLKW, &fl) != 0 && errno == EINTR) {
|
||||
}
|
||||
if (t->magic != DDNATV_HL_MAGIC || t->version != DDNATV_HL_VERSION) hl_init_header(t);
|
||||
} else {
|
||||
munmap(m, DDNATV_HL_MAP_SIZE);
|
||||
t = NULL;
|
||||
}
|
||||
}
|
||||
if (t) g_hl_shared = 1;
|
||||
}
|
||||
}
|
||||
syscall(SYS_close, fd); // the mapping stays; also drops the lock
|
||||
}
|
||||
if (!t && create) {
|
||||
// No rootfs file: this process and its children share a table
|
||||
void *m = mmap(NULL, DDNATV_HL_MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
||||
if (m != MAP_FAILED) {
|
||||
t = (ddnatv_hl_table_t *)m;
|
||||
hl_init_header(t);
|
||||
g_hl_shared = 0;
|
||||
}
|
||||
}
|
||||
if (t) {
|
||||
__atomic_store_n(&g_hl_table, t, __ATOMIC_RELEASE);
|
||||
} else if (have_path) {
|
||||
// No table yet: looked for again a second later (without a rootfs there is none to wait for)
|
||||
__atomic_store_n(&g_hl_next_try, hl_now_seconds() + 1, __ATOMIC_RELAXED);
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&g_hardlinks_lock);
|
||||
errno = saved_errno;
|
||||
return t;
|
||||
}
|
||||
|
||||
// Whether any emulated link may exist; what the stat() hooks check first.
|
||||
static inline int hardlinks_maybe_any(void) {
|
||||
ddnatv_hl_table_t *t = hl_table(0);
|
||||
return t && __atomic_load_n(&t->used, __ATOMIC_RELAXED) > 0;
|
||||
}
|
||||
|
||||
static inline uint32_t hl_hash(uint64_t dev, uint64_t ino) {
|
||||
uint64_t h = ino * 0x9E3779B97F4A7C15ull ^ (dev + 0x632BE59BD9B4E019ull) * 0xC2B2AE3D27D4EB4Full;
|
||||
return (uint32_t)(h >> 40) & (DDNATV_HL_SLOTS - 1);
|
||||
}
|
||||
|
||||
// The slot of a copy, or -1. Writers only (under the locks).
|
||||
static long hl_find_locked(ddnatv_hl_table_t *t, uint64_t dev, uint64_t ino) {
|
||||
uint32_t i = hl_hash(dev, ino);
|
||||
for (uint32_t n = 0; n < DDNATV_HL_SLOTS; n++, i = (i + 1) & (DDNATV_HL_SLOTS - 1)) {
|
||||
ddnatv_hl_slot_t *s = &t->slots[i];
|
||||
if (s->state == DDNATV_HL_EMPTY) return -1;
|
||||
if (s->state == DDNATV_HL_USED && s->ino == ino && s->dev == dev) return (long)i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Writers: the process' mutex, the file's lock, then the sequence number made odd.
|
||||
static int hl_write_begin(ddnatv_hl_table_t *t) {
|
||||
pthread_mutex_lock(&g_hardlinks_lock);
|
||||
int fd = hl_lock_file();
|
||||
uint32_t seq = __atomic_load_n(&t->seq, __ATOMIC_RELAXED);
|
||||
// An odd number left by a writer that died: the lock was given up with it
|
||||
if (!(seq & 1)) seq++;
|
||||
__atomic_store_n(&t->seq, seq, __ATOMIC_RELAXED);
|
||||
__atomic_thread_fence(__ATOMIC_RELEASE);
|
||||
return fd;
|
||||
}
|
||||
|
||||
static void hl_write_end(ddnatv_hl_table_t *t, int fd) {
|
||||
__atomic_store_n(&t->seq, __atomic_load_n(&t->seq, __ATOMIC_RELAXED) + 1, __ATOMIC_RELEASE);
|
||||
hl_unlock_file(fd);
|
||||
pthread_mutex_unlock(&g_hardlinks_lock);
|
||||
}
|
||||
|
||||
// Rebuilds the table without the slots of removed copies, which make every lookup longer.
|
||||
static void hl_compact_locked(ddnatv_hl_table_t *t) {
|
||||
ddnatv_hl_slot_t *keep = (ddnatv_hl_slot_t *)malloc(sizeof(ddnatv_hl_slot_t) * DDNATV_HL_MAX_USED);
|
||||
if (!keep) return;
|
||||
uint32_t n = 0;
|
||||
for (uint32_t i = 0; i < DDNATV_HL_SLOTS && n < DDNATV_HL_MAX_USED; i++) {
|
||||
if (t->slots[i].state == DDNATV_HL_USED) keep[n++] = t->slots[i];
|
||||
}
|
||||
memset(t->slots, 0, sizeof(t->slots));
|
||||
for (uint32_t k = 0; k < n; k++) {
|
||||
uint32_t i = hl_hash(keep[k].dev, keep[k].ino);
|
||||
while (t->slots[i].state != DDNATV_HL_EMPTY) i = (i + 1) & (DDNATV_HL_SLOTS - 1);
|
||||
t->slots[i] = keep[k];
|
||||
}
|
||||
t->used = n;
|
||||
t->deleted = 0;
|
||||
free(keep);
|
||||
}
|
||||
|
||||
static int hl_put_locked(ddnatv_hl_table_t *t, const struct stat *st, uint64_t group_dev, uint64_t group_ino) {
|
||||
long i = hl_find_locked(t, st->st_dev, st->st_ino);
|
||||
if (i < 0) {
|
||||
if (t->used >= DDNATV_HL_MAX_USED) return -1;
|
||||
if (t->used + t->deleted >= DDNATV_HL_SLOTS - DDNATV_HL_SLOTS / 8) hl_compact_locked(t);
|
||||
uint32_t j = hl_hash(st->st_dev, st->st_ino);
|
||||
while (t->slots[j].state == DDNATV_HL_USED) j = (j + 1) & (DDNATV_HL_SLOTS - 1);
|
||||
if (t->slots[j].state == DDNATV_HL_DELETED) t->deleted--;
|
||||
i = (long)j;
|
||||
t->used++;
|
||||
}
|
||||
ddnatv_hl_slot_t *s = &t->slots[i];
|
||||
s->dev = st->st_dev;
|
||||
s->ino = st->st_ino;
|
||||
s->mtime_sec = st->st_mtim.tv_sec;
|
||||
s->mtime_nsec = st->st_mtim.tv_nsec;
|
||||
s->group_dev = group_dev;
|
||||
s->group_ino = group_ino;
|
||||
s->nlink = 1;
|
||||
s->state = DDNATV_HL_USED;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void hl_remove_locked(ddnatv_hl_table_t *t, long i) {
|
||||
t->slots[i].state = DDNATV_HL_DELETED;
|
||||
t->used--;
|
||||
t->deleted++;
|
||||
}
|
||||
|
||||
// Gives every copy of a group its size; a group of one is a plain file again.
|
||||
static void hl_update_group_locked(ddnatv_hl_table_t *t, uint64_t group_dev, uint64_t group_ino) {
|
||||
uint32_t n = 0;
|
||||
long last = -1;
|
||||
for (uint32_t i = 0; i < DDNATV_HL_SLOTS; i++) {
|
||||
ddnatv_hl_slot_t *s = &t->slots[i];
|
||||
if (s->state == DDNATV_HL_USED && s->group_ino == group_ino && s->group_dev == group_dev) {
|
||||
n++;
|
||||
last = (long)i;
|
||||
}
|
||||
}
|
||||
if (n == 1) {
|
||||
hl_remove_locked(t, last);
|
||||
return;
|
||||
}
|
||||
for (uint32_t i = 0; i < DDNATV_HL_SLOTS && n > 1; i++) {
|
||||
ddnatv_hl_slot_t *s = &t->slots[i];
|
||||
if (s->state == DDNATV_HL_USED && s->group_ino == group_ino && s->group_dev == group_dev) s->nlink = n;
|
||||
}
|
||||
}
|
||||
|
||||
// Removes the copy in slot i. A group reports the inode number of one of its copies (the original);
|
||||
// when that copy goes, the group reports another one's: the number of a removed file can be given to
|
||||
// a new file, which must not show up as linked to the copies that are left.
|
||||
static void hl_remove_copy_locked(ddnatv_hl_table_t *t, long i) {
|
||||
uint64_t group_dev = t->slots[i].group_dev, group_ino = t->slots[i].group_ino;
|
||||
int reported = t->slots[i].dev == group_dev && t->slots[i].ino == group_ino;
|
||||
hl_remove_locked(t, i);
|
||||
if (reported) {
|
||||
uint64_t new_dev = 0, new_ino = 0;
|
||||
int found = 0;
|
||||
for (uint32_t j = 0; j < DDNATV_HL_SLOTS; j++) {
|
||||
ddnatv_hl_slot_t *s = &t->slots[j];
|
||||
if (s->state != DDNATV_HL_USED || s->group_ino != group_ino || s->group_dev != group_dev) continue;
|
||||
if (!found) {
|
||||
new_dev = s->dev;
|
||||
new_ino = s->ino;
|
||||
found = 1;
|
||||
}
|
||||
s->group_dev = new_dev;
|
||||
s->group_ino = new_ino;
|
||||
}
|
||||
if (found) {
|
||||
group_dev = new_dev;
|
||||
group_ino = new_ino;
|
||||
}
|
||||
}
|
||||
hl_update_group_locked(t, group_dev, group_ino);
|
||||
}
|
||||
|
||||
// Records that the file [copy] is an emulated hard link of [old].
|
||||
static void register_emulated_hardlink(const struct stat *old, const struct stat *copy) {
|
||||
ddnatv_hl_table_t *t = hl_table(1);
|
||||
if (!t) return;
|
||||
int saved_errno = errno;
|
||||
int fd = hl_write_begin(t);
|
||||
uint64_t group_dev = old->st_dev, group_ino = old->st_ino;
|
||||
long i = hl_find_locked(t, old->st_dev, old->st_ino);
|
||||
if (i >= 0 && t->slots[i].mtime_sec == old->st_mtim.tv_sec) {
|
||||
// Linking a copy again: the new copy joins the same group
|
||||
group_dev = t->slots[i].group_dev;
|
||||
group_ino = t->slots[i].group_ino;
|
||||
} else {
|
||||
// A stale slot of an earlier file with this inode number
|
||||
if (i >= 0) hl_remove_copy_locked(t, i);
|
||||
hl_put_locked(t, old, group_dev, group_ino);
|
||||
}
|
||||
if (hl_put_locked(t, copy, group_dev, group_ino) == 0 || hl_find_locked(t, old->st_dev, old->st_ino) >= 0) {
|
||||
hl_update_group_locked(t, group_dev, group_ino);
|
||||
}
|
||||
hl_write_end(t, fd);
|
||||
errno = saved_errno;
|
||||
}
|
||||
|
||||
// Forgets a copy whose name was removed; the last copy of a group goes back to a plain file.
|
||||
static void unregister_emulated_hardlink(dev_t dev, ino_t ino) {
|
||||
ddnatv_hl_table_t *t = hl_table(0);
|
||||
if (!t || __atomic_load_n(&t->used, __ATOMIC_RELAXED) == 0) return;
|
||||
int saved_errno = errno;
|
||||
int fd = hl_write_begin(t);
|
||||
long i = hl_find_locked(t, dev, ino);
|
||||
if (i >= 0) hl_remove_copy_locked(t, i);
|
||||
hl_write_end(t, fd);
|
||||
errno = saved_errno;
|
||||
}
|
||||
|
||||
// Reads the slot of a copy without a lock: 1 and [out] when there is one.
|
||||
static int hl_read(ddnatv_hl_table_t *t, uint64_t dev, uint64_t ino, ddnatv_hl_slot_t *out) {
|
||||
for (int tries = 0; tries < 64; tries++) {
|
||||
uint32_t seq = __atomic_load_n(&t->seq, __ATOMIC_ACQUIRE);
|
||||
if (seq & 1) {
|
||||
sched_yield();
|
||||
continue;
|
||||
}
|
||||
int found = 0;
|
||||
uint32_t i = hl_hash(dev, ino);
|
||||
for (uint32_t n = 0; n < DDNATV_HL_SLOTS; n++, i = (i + 1) & (DDNATV_HL_SLOTS - 1)) {
|
||||
const volatile ddnatv_hl_slot_t *s = &t->slots[i];
|
||||
uint32_t state = s->state;
|
||||
if (state == DDNATV_HL_EMPTY) break;
|
||||
if (state == DDNATV_HL_USED && s->ino == ino && s->dev == dev) {
|
||||
memcpy(out, (const void *)s, sizeof(*out));
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
__atomic_thread_fence(__ATOMIC_ACQUIRE);
|
||||
if (__atomic_load_n(&t->seq, __ATOMIC_RELAXED) == seq) return found;
|
||||
}
|
||||
return 0; // a writer that does not finish: as if there were no links
|
||||
}
|
||||
|
||||
// Makes a stat() result of an emulated hard link look like a real one: the inode number of the
|
||||
// original and the number of copies as the link count.
|
||||
static void apply_emulated_hardlink(struct stat *st) {
|
||||
if (!st || !hardlinks_maybe_any()) return;
|
||||
ddnatv_hl_slot_t s;
|
||||
if (!hl_read(g_hl_table, st->st_dev, st->st_ino, &s)) return;
|
||||
if (s.mtime_sec != (int64_t)st->st_mtim.tv_sec) return;
|
||||
if (s.nlink > (uint32_t)st->st_nlink) st->st_nlink = (nlink_t)s.nlink;
|
||||
st->st_ino = (ino_t)s.group_ino;
|
||||
}
|
||||
|
||||
// The app's own user and group, which the hook presents as root: getuid() and getpwuid() say so,
|
||||
// and stat() does as well, so that a program that compares a file's owner with getuid() sees the
|
||||
// files it made as its own (git's "dubious ownership", sshd's /run/sshd, tmux's socket directory,
|
||||
// ssh's ~/.ssh/config). -1 until known; a test sets them.
|
||||
static uid_t g_app_uid = (uid_t)-1;
|
||||
static gid_t g_app_gid = (gid_t)-1;
|
||||
|
||||
static void load_app_ids(void) {
|
||||
#ifdef DDNATV_HOST_TEST
|
||||
uid_t uid = getuid();
|
||||
gid_t gid = getgid();
|
||||
#else
|
||||
// The real ones (getuid() and syscall() are the hook's own, which answer 0)
|
||||
static uid_t (*orig_getuid)(void) = NULL;
|
||||
static gid_t (*orig_getgid)(void) = NULL;
|
||||
if (!orig_getuid) orig_getuid = (uid_t (*)(void))dlsym(RTLD_NEXT, "getuid");
|
||||
if (!orig_getgid) orig_getgid = (gid_t (*)(void))dlsym(RTLD_NEXT, "getgid");
|
||||
if (!orig_getuid || !orig_getgid) return;
|
||||
uid_t uid = orig_getuid();
|
||||
gid_t gid = orig_getgid();
|
||||
#endif
|
||||
__atomic_store_n(&g_app_gid, gid, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&g_app_uid, uid, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
static void present_owner_as_root(uid_t *uid, gid_t *gid) {
|
||||
if (__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED) == (uid_t)-1) load_app_ids();
|
||||
uid_t app_uid = __atomic_load_n(&g_app_uid, __ATOMIC_RELAXED);
|
||||
gid_t app_gid = __atomic_load_n(&g_app_gid, __ATOMIC_RELAXED);
|
||||
if (app_uid == (uid_t)-1) return;
|
||||
if (uid && *uid == app_uid) *uid = 0;
|
||||
if (gid && *gid == app_gid) *gid = 0;
|
||||
}
|
||||
|
||||
// What the stat() family returns: emulated hard links as real ones, the app's files as root's.
|
||||
static void apply_stat_emulation(struct stat *st) {
|
||||
if (!st) return;
|
||||
apply_emulated_hardlink(st);
|
||||
present_owner_as_root(&st->st_uid, &st->st_gid);
|
||||
}
|
||||
|
||||
static int real_fstatat(int dirfd, const char *path, struct stat *st, int flags) {
|
||||
#ifdef DDNATV_HOST_TEST
|
||||
return fstatat(dirfd, path, st, flags);
|
||||
#else
|
||||
static int (*orig_fstatat)(int, const char *, struct stat *, int) = NULL;
|
||||
if (!orig_fstatat) orig_fstatat = (int (*)(int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "fstatat");
|
||||
return orig_fstatat ? orig_fstatat(dirfd, path, st, flags) : fstatat(dirfd, path, st, flags);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Before a name is removed or replaced (unlink, rename over it): whether it is an emulated hard
|
||||
// link, and which. Costs nothing until the process has made one.
|
||||
static int lookup_emulated_hardlink_at(int dirfd, const char *path, dev_t *dev, ino_t *ino) {
|
||||
if (!path || !hardlinks_maybe_any()) return 0;
|
||||
int saved_errno = errno;
|
||||
struct stat st;
|
||||
int found = 0;
|
||||
if (real_fstatat(dirfd, path, &st, AT_SYMLINK_NOFOLLOW) == 0) {
|
||||
ddnatv_hl_slot_t s;
|
||||
found = hl_read(g_hl_table, st.st_dev, st.st_ino, &s);
|
||||
*dev = st.st_dev;
|
||||
*ino = st.st_ino;
|
||||
}
|
||||
errno = saved_errno;
|
||||
return found;
|
||||
}
|
||||
|
||||
static int ddnatv_emulate_hardlinkat(int olddirfd, const char *rold, int newdirfd, const char *rnew) {
|
||||
if (!rold || !rnew) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
struct stat st;
|
||||
#ifdef DDNATV_HOST_TEST
|
||||
if (fstatat(olddirfd, rold, &st, 0) != 0) {
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
static int (*orig_fstatat)(int, const char *, struct stat *, int) = NULL;
|
||||
if (!orig_fstatat) orig_fstatat = (int (*)(int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "fstatat");
|
||||
if (orig_fstatat) {
|
||||
if (orig_fstatat(olddirfd, rold, &st, 0) != 0) return -1;
|
||||
} else {
|
||||
if (fstatat(olddirfd, rold, &st, 0) != 0) return -1;
|
||||
}
|
||||
#endif
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
errno = EPERM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Clean up stale dangling symlink or stale dead-process lock file at rnew
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
struct stat lst;
|
||||
int have_lst = 0;
|
||||
if (orig_fstatat) {
|
||||
if (orig_fstatat(newdirfd, rnew, &lst, AT_SYMLINK_NOFOLLOW) == 0) have_lst = 1;
|
||||
} else {
|
||||
if (fstatat(newdirfd, rnew, &lst, AT_SYMLINK_NOFOLLOW) == 0) have_lst = 1;
|
||||
}
|
||||
if (have_lst) {
|
||||
if (S_ISLNK(lst.st_mode)) {
|
||||
unlinkat(newdirfd, rnew, 0);
|
||||
} else if (strstr(rnew, ".lock")) {
|
||||
static int (*orig_openat)(int, const char *, int, ...) = NULL;
|
||||
if (!orig_openat) orig_openat = (int (*)(int, const char *, int, ...))dlsym(RTLD_NEXT, "openat");
|
||||
int rfd = orig_openat ? orig_openat(newdirfd, rnew, O_RDONLY | O_CLOEXEC)
|
||||
: openat(newdirfd, rnew, O_RDONLY | O_CLOEXEC);
|
||||
if (rfd >= 0) {
|
||||
char pbuf[32] = {0};
|
||||
ssize_t pr = read(rfd, pbuf, sizeof(pbuf) - 1);
|
||||
close(rfd);
|
||||
if (pr > 0) {
|
||||
pid_t lpid = (pid_t)atoi(pbuf);
|
||||
if (lpid > 0 && kill(lpid, 0) == -1 && errno == ESRCH) {
|
||||
unlinkat(newdirfd, rnew, 0);
|
||||
}
|
||||
} else {
|
||||
unlinkat(newdirfd, rnew, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef DDNATV_HOST_TEST
|
||||
int fd_new = openat(newdirfd, rnew, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, st.st_mode & 0777);
|
||||
if (fd_new < 0) return -1;
|
||||
int fd_old = openat(olddirfd, rold, O_RDONLY | O_CLOEXEC);
|
||||
#else
|
||||
static int (*orig_openat)(int, const char *, int, ...) = NULL;
|
||||
if (!orig_openat) orig_openat = (int (*)(int, const char *, int, ...))dlsym(RTLD_NEXT, "openat");
|
||||
int fd_new = orig_openat ? orig_openat(newdirfd, rnew, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, st.st_mode & 0777)
|
||||
: openat(newdirfd, rnew, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, st.st_mode & 0777);
|
||||
if (fd_new < 0) return -1;
|
||||
int fd_old = orig_openat ? orig_openat(olddirfd, rold, O_RDONLY | O_CLOEXEC)
|
||||
: openat(olddirfd, rold, O_RDONLY | O_CLOEXEC);
|
||||
#endif
|
||||
if (fd_old < 0) {
|
||||
int err = errno;
|
||||
close(fd_new);
|
||||
unlinkat(newdirfd, rnew, 0);
|
||||
errno = err;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t sent = 0;
|
||||
int copy_failed = 0;
|
||||
while (sent < st.st_size) {
|
||||
ssize_t s = sendfile(fd_new, fd_old, NULL, (size_t)(st.st_size - sent));
|
||||
if (s > 0) {
|
||||
sent += s;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (sent < st.st_size) {
|
||||
char cbuf[16384];
|
||||
ssize_t nread;
|
||||
while ((nread = read(fd_old, cbuf, sizeof(cbuf))) > 0) {
|
||||
ssize_t nwritten = 0;
|
||||
while (nwritten < nread) {
|
||||
ssize_t w = write(fd_new, cbuf + nwritten, nread - nwritten);
|
||||
if (w < 0) {
|
||||
copy_failed = 1;
|
||||
break;
|
||||
}
|
||||
nwritten += w;
|
||||
}
|
||||
if (copy_failed) break;
|
||||
}
|
||||
if (nread < 0) copy_failed = 1;
|
||||
}
|
||||
|
||||
fchmod(fd_new, st.st_mode & 0777);
|
||||
// A hard link shares the times of the original (tar, dpkg and make compare them).
|
||||
const struct timespec times[2] = { st.st_atim, st.st_mtim };
|
||||
futimens(fd_new, times);
|
||||
struct stat st_new;
|
||||
memset(&st_new, 0, sizeof(st_new));
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
static int (*orig_fstat)(int, struct stat *) = NULL;
|
||||
if (!orig_fstat) orig_fstat = (int (*)(int, struct stat *))dlsym(RTLD_NEXT, "fstat");
|
||||
if (orig_fstat) orig_fstat(fd_new, &st_new);
|
||||
else fstat(fd_new, &st_new);
|
||||
#else
|
||||
fstat(fd_new, &st_new);
|
||||
#endif
|
||||
|
||||
close(fd_old);
|
||||
close(fd_new);
|
||||
|
||||
if (copy_failed) {
|
||||
unlinkat(newdirfd, rnew, 0);
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
register_emulated_hardlink(&st, &st_new);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int ddnatv_emulate_hardlink(const char *rold, const char *rnew) {
|
||||
return ddnatv_emulate_hardlinkat(AT_FDCWD, rold, AT_FDCWD, rnew);
|
||||
}
|
||||
@@ -0,0 +1,838 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
||||
// parts share their static functions and are built with the same command as before).
|
||||
//
|
||||
// Fake root: user and group ids, the shadow lock, and passwd/group lookups from the rootfs.
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
|
||||
// Fakeroot identity hooks for APT and DPKG to operate without superuser restrictions
|
||||
uid_t getuid(void) { return 0; }
|
||||
uid_t geteuid(void) { return 0; }
|
||||
gid_t getgid(void) { return 0; }
|
||||
gid_t getegid(void) { return 0; }
|
||||
|
||||
int setuid(uid_t uid) { (void)uid; return 0; }
|
||||
int seteuid(uid_t uid) { (void)uid; return 0; }
|
||||
int setgid(gid_t gid) { (void)gid; return 0; }
|
||||
int setegid(gid_t gid) { (void)gid; return 0; }
|
||||
int setreuid(uid_t ruid, uid_t euid) { (void)ruid; (void)euid; return 0; }
|
||||
int setregid(gid_t rgid, gid_t egid) { (void)rgid; (void)egid; return 0; }
|
||||
int setresuid(uid_t ruid, uid_t euid, uid_t suid) { (void)ruid; (void)euid; (void)suid; return 0; }
|
||||
int setresgid(gid_t rgid, gid_t egid, gid_t sgid) { (void)rgid; (void)egid; (void)sgid; return 0; }
|
||||
|
||||
int getgroups(int size, gid_t list[]) {
|
||||
if (size > 0 && list != NULL) list[0] = 0;
|
||||
return 1;
|
||||
}
|
||||
// What programs built with _FORTIFY_SOURCE call when the buffer's size is known (the check is
|
||||
// glibc's): without it the count came from the hook (1) and the list from the kernel (the app's
|
||||
// groups, more than one), and sshd ended the login on "getgroups: Invalid argument"
|
||||
extern void __chk_fail(void) __attribute__((__noreturn__));
|
||||
int __getgroups_chk(int size, gid_t list[], size_t listlen) {
|
||||
if (size > 0 && (size_t)size * sizeof(gid_t) > listlen) __chk_fail();
|
||||
return getgroups(size, list);
|
||||
}
|
||||
int setgroups(size_t size, const gid_t *list) { (void)size; (void)list; return 0; }
|
||||
int initgroups(const char *user, gid_t group) { (void)user; (void)group; return 0; }
|
||||
|
||||
int getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups) {
|
||||
(void)user;
|
||||
if (!ngroups) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (*ngroups > 0 && groups) {
|
||||
groups[0] = group;
|
||||
*ngroups = 1;
|
||||
return 1;
|
||||
}
|
||||
// Too small (a caller asking for the size first): -1 and the number needed, as glibc does.
|
||||
*ngroups = 1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// The session's host name, as proot's --kernel-release gave it: Steam names the device after it
|
||||
int uname(struct utsname *buf) {
|
||||
static int (*orig_uname)(struct utsname *) = NULL;
|
||||
if (!orig_uname) orig_uname = (int (*)(struct utsname *))dlsym(RTLD_NEXT, "uname");
|
||||
int ret = orig_uname ? orig_uname(buf) : -1;
|
||||
if (ret == 0 && buf && g_hostname[0]) {
|
||||
ddnatv_copy_cstr(buf->nodename, sizeof(buf->nodename), g_hostname);
|
||||
ddnatv_copy_cstr(buf->domainname, sizeof(buf->domainname), "localdomain");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int gethostname(char *name, size_t len) {
|
||||
static int (*orig_gethostname)(char *, size_t) = NULL;
|
||||
if (!orig_gethostname) orig_gethostname = (int (*)(char *, size_t))dlsym(RTLD_NEXT, "gethostname");
|
||||
if (!g_hostname[0]) return orig_gethostname ? orig_gethostname(name, len) : -1;
|
||||
size_t n = strlen(g_hostname) + 1;
|
||||
if (!name || n > len) {
|
||||
if (name && len) memcpy(name, g_hostname, len);
|
||||
errno = ENAMETOOLONG;
|
||||
return -1;
|
||||
}
|
||||
memcpy(name, g_hostname, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* !DDNATV_HOST_TEST */
|
||||
|
||||
static uid_t real_uid(void) {
|
||||
#ifdef DDNATV_HOST_TEST
|
||||
return 10000;
|
||||
#else
|
||||
static uid_t (*orig_getuid)(void) = NULL;
|
||||
if (!orig_getuid) orig_getuid = (uid_t (*)(void))dlsym(RTLD_NEXT, "getuid");
|
||||
return orig_getuid ? orig_getuid() : 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static gid_t real_gid(void) {
|
||||
#ifdef DDNATV_HOST_TEST
|
||||
return 10000;
|
||||
#else
|
||||
static gid_t (*orig_getgid)(void) = NULL;
|
||||
if (!orig_getgid) orig_getgid = (gid_t (*)(void))dlsym(RTLD_NEXT, "getgid");
|
||||
return orig_getgid ? orig_getgid() : 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef DDNATV_HOST_TEST
|
||||
#define DDNATV_NSS_SYM(name) hook_##name
|
||||
#define DDNATV_SHADOW_SYM(name) hook_##name
|
||||
static int g_test_orig_nss_r_ret = 0;
|
||||
static int g_test_orig_nss_r_found = 0;
|
||||
|
||||
int hook_link(const char *oldpath, const char *newpath) {
|
||||
int ret = link(oldpath, newpath);
|
||||
if (ret != 0 && (errno == EXDEV || errno == EPERM || errno == EACCES || errno == ENOTSUP || errno == ENOSYS)) {
|
||||
ret = ddnatv_emulate_hardlink(oldpath, newpath);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int hook_linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) {
|
||||
int ret = linkat(olddirfd, oldpath, newdirfd, newpath, flags);
|
||||
if (ret != 0 && (errno == EXDEV || errno == EPERM || errno == EACCES || errno == ENOTSUP || errno == ENOSYS)) {
|
||||
ret = ddnatv_emulate_hardlinkat(olddirfd, oldpath, newdirfd, newpath);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int hook_stat(const char *path, struct stat *st) {
|
||||
int ret = stat(path, st);
|
||||
if (ret == 0) apply_stat_emulation(st);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int hook_unlink(const char *path) {
|
||||
dev_t hl_dev = 0;
|
||||
ino_t hl_ino = 0;
|
||||
int hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, path, &hl_dev, &hl_ino);
|
||||
int ret = unlink(path);
|
||||
if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino);
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
#define DDNATV_NSS_SYM(name) name
|
||||
#define DDNATV_SHADOW_SYM(name) name
|
||||
#endif
|
||||
|
||||
// Hook lckpwdf and ulckpwdf (shadow password database locking)
|
||||
// In glibc, lckpwdf() attempts to open /etc/.pwd.lock directly using internal syscalls
|
||||
// which fails with EROFS on Android because host /etc is mounted read-only.
|
||||
static int g_pwd_lock_fd = -1;
|
||||
static pthread_mutex_t g_pwd_lock_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
int DDNATV_SHADOW_SYM(lckpwdf)(void) {
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
init_ddnatv_hook();
|
||||
pthread_mutex_lock(&g_pwd_lock_mutex);
|
||||
if (g_pwd_lock_fd != -1) {
|
||||
pthread_mutex_unlock(&g_pwd_lock_mutex);
|
||||
return 0; // Already locked
|
||||
}
|
||||
char buf[PATH_MAX];
|
||||
const char *target = rewrite_path("/etc/.pwd.lock", buf, sizeof(buf));
|
||||
static int (*orig_open)(const char *, int, ...) = NULL;
|
||||
if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open");
|
||||
if (orig_open && target) {
|
||||
g_pwd_lock_fd = orig_open(target, O_WRONLY | O_CREAT | O_CLOEXEC, 0600);
|
||||
if (g_pwd_lock_fd >= 0) {
|
||||
struct flock fl;
|
||||
memset(&fl, 0, sizeof(fl));
|
||||
fl.l_type = F_WRLCK;
|
||||
fl.l_whence = SEEK_SET;
|
||||
if (fcntl(g_pwd_lock_fd, F_SETLK, &fl) != 0) {
|
||||
int saved = errno;
|
||||
close(g_pwd_lock_fd);
|
||||
g_pwd_lock_fd = -1;
|
||||
pthread_mutex_unlock(&g_pwd_lock_mutex);
|
||||
errno = saved;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (g_pwd_lock_fd < 0) {
|
||||
g_pwd_lock_fd = -2; // In-memory lock held
|
||||
}
|
||||
pthread_mutex_unlock(&g_pwd_lock_mutex);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DDNATV_SHADOW_SYM(ulckpwdf)(void) {
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
pthread_mutex_lock(&g_pwd_lock_mutex);
|
||||
if (g_pwd_lock_fd >= 0) {
|
||||
close(g_pwd_lock_fd);
|
||||
}
|
||||
g_pwd_lock_fd = -1;
|
||||
pthread_mutex_unlock(&g_pwd_lock_mutex);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// Group lookup helper: reads $DDNATV_ROOT/etc/group or falls back to known entries
|
||||
static int ddnatv_lookup_group(const char *search_name, gid_t search_gid, int by_name,
|
||||
struct group *grp, char *buf, size_t buflen) {
|
||||
if (!grp || !buf || buflen < 64) {
|
||||
return ERANGE;
|
||||
}
|
||||
|
||||
int found = 0;
|
||||
char found_name[64] = {0};
|
||||
char found_pass[32] = {0};
|
||||
gid_t found_gid = 0;
|
||||
char found_members[256] = {0};
|
||||
|
||||
// 1. Try reading /etc/group from The native userland rootfs
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
char path_buf[PATH_MAX];
|
||||
const char *grp_path = rewrite_path("/etc/group", path_buf, sizeof(path_buf));
|
||||
static int (*orig_open)(const char *, int, ...) = NULL;
|
||||
if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open");
|
||||
int fd = (orig_open && grp_path) ? orig_open(grp_path, O_RDONLY | O_CLOEXEC) : -1;
|
||||
if (fd >= 0) {
|
||||
char file_buf[8192];
|
||||
ssize_t n = read(fd, file_buf, sizeof(file_buf) - 1);
|
||||
close(fd);
|
||||
if (n > 0) {
|
||||
file_buf[n] = '\0';
|
||||
char *line = file_buf;
|
||||
while (*line && !found) {
|
||||
char *next_line = strchr(line, '\n');
|
||||
if (next_line) {
|
||||
*next_line = '\0';
|
||||
}
|
||||
size_t llen = strlen(line);
|
||||
if (llen > 0 && line[llen - 1] == '\r') line[llen - 1] = '\0';
|
||||
|
||||
// format: name:pass:gid:members
|
||||
char *p1 = strchr(line, ':');
|
||||
if (p1) {
|
||||
*p1 = '\0';
|
||||
char *p2 = strchr(p1 + 1, ':');
|
||||
if (p2) {
|
||||
*p2 = '\0';
|
||||
char *p3 = strchr(p2 + 1, ':');
|
||||
if (p3) {
|
||||
*p3 = '\0';
|
||||
const char *gname = line;
|
||||
const char *gpass = p1 + 1;
|
||||
gid_t ggid = (gid_t)strtoul(p2 + 1, NULL, 10);
|
||||
const char *gmem = p3 + 1;
|
||||
|
||||
if (by_name && search_name && strcmp(gname, search_name) == 0) {
|
||||
snprintf(found_name, sizeof(found_name), "%s", gname);
|
||||
snprintf(found_pass, sizeof(found_pass), "%s", gpass);
|
||||
found_gid = ggid;
|
||||
snprintf(found_members, sizeof(found_members), "%s", gmem);
|
||||
found = 1;
|
||||
} else if (!by_name && ggid == search_gid) {
|
||||
snprintf(found_name, sizeof(found_name), "%s", gname);
|
||||
snprintf(found_pass, sizeof(found_pass), "%s", gpass);
|
||||
found_gid = ggid;
|
||||
snprintf(found_members, sizeof(found_members), "%s", gmem);
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!next_line) break;
|
||||
line = next_line + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// 2. Fallbacks for well-known groups if absent from file
|
||||
if (!found) {
|
||||
if (by_name) {
|
||||
if (!search_name) return ENOENT;
|
||||
if (strcmp(search_name, "root") == 0 || strcmp(search_name, "ddnatv") == 0) {
|
||||
snprintf(found_name, sizeof(found_name), "%s", search_name);
|
||||
snprintf(found_pass, sizeof(found_pass), "x");
|
||||
found_gid = 0;
|
||||
snprintf(found_members, sizeof(found_members), "%s", search_name);
|
||||
found = 1;
|
||||
} else if (strcmp(search_name, "_ssh") == 0) {
|
||||
snprintf(found_name, sizeof(found_name), "_ssh");
|
||||
snprintf(found_pass, sizeof(found_pass), "x");
|
||||
found_gid = 100;
|
||||
found = 1;
|
||||
} else if (strcmp(search_name, "ssh") == 0) {
|
||||
snprintf(found_name, sizeof(found_name), "ssh");
|
||||
snprintf(found_pass, sizeof(found_pass), "x");
|
||||
found_gid = 101;
|
||||
found = 1;
|
||||
} else if (strcmp(search_name, "nogroup") == 0 || strcmp(search_name, "nobody") == 0) {
|
||||
snprintf(found_name, sizeof(found_name), "%s", search_name);
|
||||
snprintf(found_pass, sizeof(found_pass), "x");
|
||||
found_gid = 65534;
|
||||
found = 1;
|
||||
}
|
||||
} else {
|
||||
if (search_gid == 0 || search_gid == real_gid()) {
|
||||
snprintf(found_name, sizeof(found_name), "root");
|
||||
snprintf(found_pass, sizeof(found_pass), "x");
|
||||
found_gid = search_gid;
|
||||
snprintf(found_members, sizeof(found_members), "root");
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
return ENOENT;
|
||||
}
|
||||
|
||||
// 3. Assemble struct group into caller's buf
|
||||
size_t mem_ptr_count = 1;
|
||||
char *m_copy = found_members;
|
||||
while (*m_copy) {
|
||||
if (*m_copy == ',') mem_ptr_count++;
|
||||
m_copy++;
|
||||
}
|
||||
if (found_members[0] != '\0') mem_ptr_count++;
|
||||
|
||||
size_t mem_array_bytes = (mem_ptr_count + 1) * sizeof(char *);
|
||||
mem_array_bytes = (mem_array_bytes + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
|
||||
|
||||
size_t name_len = strlen(found_name) + 1;
|
||||
size_t pass_len = strlen(found_pass) + 1;
|
||||
size_t mem_str_len = strlen(found_members) + 1;
|
||||
|
||||
if (mem_array_bytes + name_len + pass_len + mem_str_len > buflen) {
|
||||
return ERANGE;
|
||||
}
|
||||
|
||||
char **mem_array = (char **)buf;
|
||||
char *str_cursor = buf + mem_array_bytes;
|
||||
|
||||
grp->gr_name = str_cursor;
|
||||
memcpy(str_cursor, found_name, name_len);
|
||||
str_cursor += name_len;
|
||||
|
||||
grp->gr_passwd = str_cursor;
|
||||
memcpy(str_cursor, found_pass, pass_len);
|
||||
str_cursor += pass_len;
|
||||
|
||||
grp->gr_gid = found_gid;
|
||||
|
||||
size_t m_idx = 0;
|
||||
if (found_members[0] != '\0') {
|
||||
char *m_tok = str_cursor;
|
||||
memcpy(str_cursor, found_members, mem_str_len);
|
||||
str_cursor += mem_str_len;
|
||||
|
||||
char *curr = m_tok;
|
||||
while (curr && *curr) {
|
||||
char *comma = strchr(curr, ',');
|
||||
if (comma) {
|
||||
*comma = '\0';
|
||||
}
|
||||
if (*curr) {
|
||||
mem_array[m_idx++] = curr;
|
||||
}
|
||||
curr = comma ? comma + 1 : NULL;
|
||||
}
|
||||
}
|
||||
mem_array[m_idx] = NULL;
|
||||
grp->gr_mem = mem_array;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Passwd lookup helper: reads $DDNATV_ROOT/etc/passwd or falls back to known entries
|
||||
static int ddnatv_lookup_passwd(const char *search_name, uid_t search_uid, int by_name,
|
||||
struct passwd *pwd, char *buf, size_t buflen) {
|
||||
if (!pwd || !buf || buflen < 128) {
|
||||
return ERANGE;
|
||||
}
|
||||
|
||||
int found = 0;
|
||||
char found_name[64] = {0};
|
||||
char found_pass[32] = {0};
|
||||
uid_t found_uid = 0;
|
||||
gid_t found_gid = 0;
|
||||
char found_gecos[64] = {0};
|
||||
char found_dir[128] = {0};
|
||||
char found_shell[64] = {0};
|
||||
|
||||
// 1. Try reading /etc/passwd from The native userland rootfs
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
char path_buf[PATH_MAX];
|
||||
const char *pwd_path = rewrite_path("/etc/passwd", path_buf, sizeof(path_buf));
|
||||
static int (*orig_open)(const char *, int, ...) = NULL;
|
||||
if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open");
|
||||
int fd = (orig_open && pwd_path) ? orig_open(pwd_path, O_RDONLY | O_CLOEXEC) : -1;
|
||||
if (fd >= 0) {
|
||||
char file_buf[8192];
|
||||
ssize_t n = read(fd, file_buf, sizeof(file_buf) - 1);
|
||||
close(fd);
|
||||
if (n > 0) {
|
||||
file_buf[n] = '\0';
|
||||
char *line = file_buf;
|
||||
while (*line && !found) {
|
||||
char *next_line = strchr(line, '\n');
|
||||
if (next_line) {
|
||||
*next_line = '\0';
|
||||
}
|
||||
size_t llen = strlen(line);
|
||||
if (llen > 0 && line[llen - 1] == '\r') line[llen - 1] = '\0';
|
||||
|
||||
// format: name:pass:uid:gid:gecos:dir:shell
|
||||
char *f[7] = {0};
|
||||
char *curr = line;
|
||||
int f_idx = 0;
|
||||
while (curr && f_idx < 7) {
|
||||
f[f_idx++] = curr;
|
||||
char *col = strchr(curr, ':');
|
||||
if (col && f_idx < 7) {
|
||||
*col = '\0';
|
||||
curr = col + 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (f_idx >= 7) {
|
||||
const char *uname = f[0];
|
||||
const char *upass = f[1];
|
||||
uid_t uuid = (uid_t)strtoul(f[2], NULL, 10);
|
||||
gid_t ugid = (gid_t)strtoul(f[3], NULL, 10);
|
||||
const char *ugecos = f[4];
|
||||
const char *udir = f[5];
|
||||
const char *ushell = f[6];
|
||||
|
||||
if (by_name && search_name && strcmp(uname, search_name) == 0) {
|
||||
snprintf(found_name, sizeof(found_name), "%s", uname);
|
||||
snprintf(found_pass, sizeof(found_pass), "%s", upass);
|
||||
found_uid = uuid;
|
||||
found_gid = ugid;
|
||||
snprintf(found_gecos, sizeof(found_gecos), "%s", ugecos);
|
||||
snprintf(found_dir, sizeof(found_dir), "%s", udir);
|
||||
snprintf(found_shell, sizeof(found_shell), "%s", ushell);
|
||||
found = 1;
|
||||
} else if (!by_name && uuid == search_uid) {
|
||||
snprintf(found_name, sizeof(found_name), "%s", uname);
|
||||
snprintf(found_pass, sizeof(found_pass), "%s", upass);
|
||||
found_uid = uuid;
|
||||
found_gid = ugid;
|
||||
snprintf(found_gecos, sizeof(found_gecos), "%s", ugecos);
|
||||
snprintf(found_dir, sizeof(found_dir), "%s", udir);
|
||||
snprintf(found_shell, sizeof(found_shell), "%s", ushell);
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
if (!next_line) break;
|
||||
line = next_line + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// 2. Fallbacks for well-known users if absent from file
|
||||
if (!found) {
|
||||
if (by_name) {
|
||||
if (!search_name) return ENOENT;
|
||||
if (strcmp(search_name, "root") == 0) {
|
||||
snprintf(found_name, sizeof(found_name), "root");
|
||||
snprintf(found_pass, sizeof(found_pass), "x");
|
||||
found_uid = 0;
|
||||
found_gid = 0;
|
||||
snprintf(found_gecos, sizeof(found_gecos), "root");
|
||||
snprintf(found_dir, sizeof(found_dir), "/root");
|
||||
snprintf(found_shell, sizeof(found_shell), "/bin/bash");
|
||||
found = 1;
|
||||
} else if (strcmp(search_name, "ddnatv") == 0) {
|
||||
snprintf(found_name, sizeof(found_name), "ddnatv");
|
||||
snprintf(found_pass, sizeof(found_pass), "x");
|
||||
found_uid = 0;
|
||||
found_gid = 0;
|
||||
snprintf(found_gecos, sizeof(found_gecos), "The native userland");
|
||||
snprintf(found_dir, sizeof(found_dir), "/home/ddnatv");
|
||||
snprintf(found_shell, sizeof(found_shell), "/bin/bash");
|
||||
found = 1;
|
||||
} else if (strcmp(search_name, "_ssh") == 0) {
|
||||
snprintf(found_name, sizeof(found_name), "_ssh");
|
||||
snprintf(found_pass, sizeof(found_pass), "x");
|
||||
found_uid = 100;
|
||||
found_gid = 100;
|
||||
snprintf(found_gecos, sizeof(found_gecos), "OpenSSH daemon");
|
||||
snprintf(found_dir, sizeof(found_dir), "/run/sshd");
|
||||
snprintf(found_shell, sizeof(found_shell), "/usr/sbin/nologin");
|
||||
found = 1;
|
||||
} else if (strcmp(search_name, "sshd") == 0) {
|
||||
snprintf(found_name, sizeof(found_name), "sshd");
|
||||
snprintf(found_pass, sizeof(found_pass), "x");
|
||||
found_uid = 101;
|
||||
found_gid = 101;
|
||||
snprintf(found_gecos, sizeof(found_gecos), "Privilege-separated SSH");
|
||||
snprintf(found_dir, sizeof(found_dir), "/run/sshd");
|
||||
snprintf(found_shell, sizeof(found_shell), "/usr/sbin/nologin");
|
||||
found = 1;
|
||||
} else if (strcmp(search_name, "nobody") == 0) {
|
||||
snprintf(found_name, sizeof(found_name), "nobody");
|
||||
snprintf(found_pass, sizeof(found_pass), "x");
|
||||
found_uid = 65534;
|
||||
found_gid = 65534;
|
||||
snprintf(found_gecos, sizeof(found_gecos), "nobody");
|
||||
snprintf(found_dir, sizeof(found_dir), "/nonexistent");
|
||||
snprintf(found_shell, sizeof(found_shell), "/usr/sbin/nologin");
|
||||
found = 1;
|
||||
}
|
||||
} else {
|
||||
if (search_uid == 0 || search_uid == real_uid()) {
|
||||
snprintf(found_name, sizeof(found_name), "root");
|
||||
snprintf(found_pass, sizeof(found_pass), "x");
|
||||
found_uid = search_uid;
|
||||
found_gid = 0;
|
||||
snprintf(found_gecos, sizeof(found_gecos), "root");
|
||||
snprintf(found_dir, sizeof(found_dir), "/home");
|
||||
snprintf(found_shell, sizeof(found_shell), "/bin/bash");
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
return ENOENT;
|
||||
}
|
||||
|
||||
// 3. Assemble struct passwd into caller's buf
|
||||
size_t name_len = strlen(found_name) + 1;
|
||||
size_t pass_len = strlen(found_pass) + 1;
|
||||
size_t gecos_len = strlen(found_gecos) + 1;
|
||||
size_t dir_len = strlen(found_dir) + 1;
|
||||
size_t shell_len = strlen(found_shell) + 1;
|
||||
|
||||
size_t total_str_bytes = name_len + pass_len + gecos_len + dir_len + shell_len;
|
||||
if (total_str_bytes > buflen) {
|
||||
return ERANGE;
|
||||
}
|
||||
|
||||
char *str_cursor = buf;
|
||||
|
||||
pwd->pw_name = str_cursor;
|
||||
memcpy(str_cursor, found_name, name_len);
|
||||
str_cursor += name_len;
|
||||
|
||||
pwd->pw_passwd = str_cursor;
|
||||
memcpy(str_cursor, found_pass, pass_len);
|
||||
str_cursor += pass_len;
|
||||
|
||||
pwd->pw_uid = found_uid;
|
||||
pwd->pw_gid = found_gid;
|
||||
|
||||
pwd->pw_gecos = str_cursor;
|
||||
memcpy(str_cursor, found_gecos, gecos_len);
|
||||
str_cursor += gecos_len;
|
||||
|
||||
pwd->pw_dir = str_cursor;
|
||||
memcpy(str_cursor, found_dir, dir_len);
|
||||
str_cursor += dir_len;
|
||||
|
||||
pwd->pw_shell = str_cursor;
|
||||
memcpy(str_cursor, found_shell, shell_len);
|
||||
str_cursor += shell_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
// The shadow database (/etc/shadow) of the rootfs. libc reads the real /etc/shadow of the machine,
|
||||
// which Android does not have: pam_unix's account check for root found no entry and answered
|
||||
// "Authentication failure", so chfn, chsh and `adduser --gecos` (the installation of PostgreSQL)
|
||||
// failed. The fields are "name:hash:lastchange:min:max:warn:inactive:expire:flag", numbers empty
|
||||
// when unset (libc stores -1 then).
|
||||
static int ddnatv_lookup_shadow(const char *name, struct spwd *sp, char *buf, size_t buflen) {
|
||||
if (!name || !sp || !buf) return EINVAL;
|
||||
char path_buf[PATH_MAX];
|
||||
const char *path = rewrite_path("/etc/shadow", path_buf, sizeof(path_buf));
|
||||
static int (*orig_open)(const char *, int, ...) = NULL;
|
||||
if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open");
|
||||
int fd = (orig_open && path) ? orig_open(path, O_RDONLY | O_CLOEXEC) : -1;
|
||||
if (fd < 0) return ENOENT;
|
||||
FILE *f = fdopen(fd, "r");
|
||||
if (!f) {
|
||||
close(fd);
|
||||
return ENOENT;
|
||||
}
|
||||
size_t name_len = strlen(name);
|
||||
char line[1024];
|
||||
int err = ENOENT;
|
||||
while (fgets(line, sizeof(line), f)) {
|
||||
if (strncmp(line, name, name_len) != 0 || line[name_len] != ':') continue;
|
||||
line[strcspn(line, "\r\n")] = '\0';
|
||||
char *fields[9] = {0};
|
||||
int n = 0;
|
||||
char *cursor = line;
|
||||
while (n < 9) {
|
||||
fields[n++] = cursor;
|
||||
char *colon = strchr(cursor, ':');
|
||||
if (!colon) break;
|
||||
*colon = '\0';
|
||||
cursor = colon + 1;
|
||||
}
|
||||
size_t need = strlen(fields[0]) + 1 + strlen(fields[1] ? fields[1] : "") + 1;
|
||||
if (n < 2 || need > buflen) {
|
||||
err = n < 2 ? ENOENT : ERANGE;
|
||||
break;
|
||||
}
|
||||
char *out = buf;
|
||||
sp->sp_namp = out;
|
||||
memcpy(out, fields[0], strlen(fields[0]) + 1);
|
||||
out += strlen(fields[0]) + 1;
|
||||
sp->sp_pwdp = out;
|
||||
memcpy(out, fields[1], strlen(fields[1]) + 1);
|
||||
long number[6];
|
||||
for (int i = 0; i < 6; i++) {
|
||||
const char *v = (2 + i < n) ? fields[2 + i] : NULL;
|
||||
number[i] = (v && v[0] != '\0') ? strtol(v, NULL, 10) : -1;
|
||||
}
|
||||
sp->sp_lstchg = number[0];
|
||||
sp->sp_min = number[1];
|
||||
sp->sp_max = number[2];
|
||||
sp->sp_warn = number[3];
|
||||
sp->sp_inact = number[4];
|
||||
sp->sp_expire = number[5];
|
||||
sp->sp_flag = (8 < n && fields[8][0] != '\0') ? strtoul(fields[8], NULL, 10) : (unsigned long)-1;
|
||||
err = 0;
|
||||
break;
|
||||
}
|
||||
fclose(f);
|
||||
return err;
|
||||
}
|
||||
|
||||
int getspnam_r(const char *name, struct spwd *spbuf, char *buf, size_t buflen, struct spwd **result) {
|
||||
static int (*orig_getspnam_r)(const char *, struct spwd *, char *, size_t, struct spwd **) = NULL;
|
||||
if (!orig_getspnam_r) orig_getspnam_r = (int (*)(const char *, struct spwd *, char *, size_t, struct spwd **))dlsym(RTLD_NEXT, "getspnam_r");
|
||||
int ret = ENOENT;
|
||||
if (orig_getspnam_r) {
|
||||
ret = orig_getspnam_r(name, spbuf, buf, buflen, result);
|
||||
if ((ret == 0 && result && *result) || ret == ERANGE) return ret;
|
||||
}
|
||||
int err = ddnatv_lookup_shadow(name, spbuf, buf, buflen);
|
||||
if (err == 0) {
|
||||
if (result) *result = spbuf;
|
||||
return 0;
|
||||
}
|
||||
if (result) *result = NULL;
|
||||
return err == ENOENT ? (ret != 0 ? ret : ENOENT) : err;
|
||||
}
|
||||
|
||||
struct spwd *getspnam(const char *name) {
|
||||
static struct spwd entry;
|
||||
static char entry_buf[1024];
|
||||
struct spwd *found = NULL;
|
||||
int err = getspnam_r(name, &entry, entry_buf, sizeof(entry_buf), &found);
|
||||
if (err != 0) {
|
||||
errno = err;
|
||||
return NULL;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
#endif /* !DDNATV_HOST_TEST */
|
||||
|
||||
static struct group s_grp_storage;
|
||||
static char s_grp_buf[2048];
|
||||
static struct passwd s_pwd_storage;
|
||||
static char s_pwd_buf[2048];
|
||||
|
||||
struct group *DDNATV_NSS_SYM(getgrgid)(gid_t gid) {
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
static struct group *(*orig_getgrgid)(gid_t) = NULL;
|
||||
if (!orig_getgrgid) orig_getgrgid = (struct group *(*)(gid_t))dlsym(RTLD_NEXT, "getgrgid");
|
||||
struct group *res = orig_getgrgid ? orig_getgrgid(gid) : NULL;
|
||||
if (res) return res;
|
||||
#endif
|
||||
|
||||
int err = ddnatv_lookup_group(NULL, gid, 0, &s_grp_storage, s_grp_buf, sizeof(s_grp_buf));
|
||||
if (err == 0) return &s_grp_storage;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct group *DDNATV_NSS_SYM(getgrnam)(const char *name) {
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
static struct group *(*orig_getgrnam)(const char *) = NULL;
|
||||
if (!orig_getgrnam) orig_getgrnam = (struct group *(*)(const char *))dlsym(RTLD_NEXT, "getgrnam");
|
||||
struct group *res = orig_getgrnam ? orig_getgrnam(name) : NULL;
|
||||
if (res) return res;
|
||||
#endif
|
||||
|
||||
int err = ddnatv_lookup_group(name, 0, 1, &s_grp_storage, s_grp_buf, sizeof(s_grp_buf));
|
||||
if (err == 0) return &s_grp_storage;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int DDNATV_NSS_SYM(getgrgid_r)(gid_t gid, struct group *grp, char *buf, size_t buflen, struct group **result) {
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
static int (*orig_getgrgid_r)(gid_t, struct group *, char *, size_t, struct group **) = NULL;
|
||||
if (!orig_getgrgid_r) orig_getgrgid_r = (int (*)(gid_t, struct group *, char *, size_t, struct group **))dlsym(RTLD_NEXT, "getgrgid_r");
|
||||
int ret = orig_getgrgid_r ? orig_getgrgid_r(gid, grp, buf, buflen, result) : ENOENT;
|
||||
#else
|
||||
int ret = g_test_orig_nss_r_ret;
|
||||
if (result) *result = (ret == 0 && g_test_orig_nss_r_found) ? grp : NULL;
|
||||
#endif
|
||||
if (ret == 0 && result && *result != NULL) return 0;
|
||||
if (ret != 0 && ret != ENOENT) {
|
||||
if (result) *result = NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int err = ddnatv_lookup_group(NULL, gid, 0, grp, buf, buflen);
|
||||
if (err == 0) {
|
||||
if (result) *result = grp;
|
||||
return 0;
|
||||
}
|
||||
if (result) *result = NULL;
|
||||
return (err == ERANGE) ? ERANGE : 0;
|
||||
}
|
||||
|
||||
int DDNATV_NSS_SYM(getgrnam_r)(const char *name, struct group *grp, char *buf, size_t buflen, struct group **result) {
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
static int (*orig_getgrnam_r)(const char *, struct group *, char *, size_t, struct group **) = NULL;
|
||||
if (!orig_getgrnam_r) orig_getgrnam_r = (int (*)(const char *, struct group *, char *, size_t, struct group **))dlsym(RTLD_NEXT, "getgrnam_r");
|
||||
int ret = orig_getgrnam_r ? orig_getgrnam_r(name, grp, buf, buflen, result) : ENOENT;
|
||||
#else
|
||||
int ret = g_test_orig_nss_r_ret;
|
||||
if (result) *result = (ret == 0 && g_test_orig_nss_r_found) ? grp : NULL;
|
||||
#endif
|
||||
if (ret == 0 && result && *result != NULL) return 0;
|
||||
if (ret != 0 && ret != ENOENT) {
|
||||
if (result) *result = NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int err = ddnatv_lookup_group(name, 0, 1, grp, buf, buflen);
|
||||
if (err == 0) {
|
||||
if (result) *result = grp;
|
||||
return 0;
|
||||
}
|
||||
if (result) *result = NULL;
|
||||
return (err == ERANGE) ? ERANGE : 0;
|
||||
}
|
||||
|
||||
// sshd's privilege-separated user is root's ids: The native userland cannot drop privileges (setuid() and
|
||||
// setgid() only pretend to), and sshd's unprivileged part checks after dropping them that it cannot
|
||||
// take them back, which ended it ("permanently_set_uid: was able to restore old [e]gid"). Like apt's
|
||||
// sandbox user (APT::Sandbox::User "root"), it then runs as the rest does.
|
||||
//
|
||||
// The app's own ids are root's as well: the rootfs lists root with them for proot, and a server
|
||||
// that allows "localuser:root" compares getpwnam("root") with a peer's credentials, which the hook
|
||||
// reports as 0. Xwayland refused gamescope's own connection when the two disagreed.
|
||||
static struct passwd *present_privsep_user_as_root(struct passwd *pwd) {
|
||||
if (pwd && pwd->pw_name && strcmp(pwd->pw_name, "sshd") == 0) {
|
||||
pwd->pw_uid = 0;
|
||||
pwd->pw_gid = 0;
|
||||
}
|
||||
if (pwd && pwd->pw_uid == real_uid()) pwd->pw_uid = 0;
|
||||
if (pwd && pwd->pw_gid == real_gid()) pwd->pw_gid = 0;
|
||||
return pwd;
|
||||
}
|
||||
|
||||
struct passwd *DDNATV_NSS_SYM(getpwuid)(uid_t uid) {
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
static struct passwd *(*orig_getpwuid)(uid_t) = NULL;
|
||||
if (!orig_getpwuid) orig_getpwuid = (struct passwd *(*)(uid_t))dlsym(RTLD_NEXT, "getpwuid");
|
||||
struct passwd *res = orig_getpwuid ? orig_getpwuid(uid) : NULL;
|
||||
if (res) return present_privsep_user_as_root(res);
|
||||
#endif
|
||||
|
||||
int err = ddnatv_lookup_passwd(NULL, uid, 0, &s_pwd_storage, s_pwd_buf, sizeof(s_pwd_buf));
|
||||
if (err == 0) return present_privsep_user_as_root(&s_pwd_storage);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct passwd *DDNATV_NSS_SYM(getpwnam)(const char *name) {
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
static struct passwd *(*orig_getpwnam)(const char *) = NULL;
|
||||
if (!orig_getpwnam) orig_getpwnam = (struct passwd *(*)(const char *))dlsym(RTLD_NEXT, "getpwnam");
|
||||
struct passwd *res = orig_getpwnam ? orig_getpwnam(name) : NULL;
|
||||
if (res) return present_privsep_user_as_root(res);
|
||||
#endif
|
||||
|
||||
int err = ddnatv_lookup_passwd(name, 0, 1, &s_pwd_storage, s_pwd_buf, sizeof(s_pwd_buf));
|
||||
if (err == 0) return present_privsep_user_as_root(&s_pwd_storage);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int DDNATV_NSS_SYM(getpwuid_r)(uid_t uid, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result) {
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
static int (*orig_getpwuid_r)(uid_t, struct passwd *, char *, size_t, struct passwd **) = NULL;
|
||||
if (!orig_getpwuid_r) orig_getpwuid_r = (int (*)(uid_t, struct passwd *, char *, size_t, struct passwd **))dlsym(RTLD_NEXT, "getpwuid_r");
|
||||
int ret = orig_getpwuid_r ? orig_getpwuid_r(uid, pwd, buf, buflen, result) : ENOENT;
|
||||
#else
|
||||
int ret = g_test_orig_nss_r_ret;
|
||||
if (result) *result = (ret == 0 && g_test_orig_nss_r_found) ? pwd : NULL;
|
||||
#endif
|
||||
if (ret == 0 && result && *result != NULL) {
|
||||
present_privsep_user_as_root(*result);
|
||||
return 0;
|
||||
}
|
||||
if (ret != 0 && ret != ENOENT) {
|
||||
if (result) *result = NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int err = ddnatv_lookup_passwd(NULL, uid, 0, pwd, buf, buflen);
|
||||
if (err == 0) {
|
||||
present_privsep_user_as_root(pwd);
|
||||
if (result) *result = pwd;
|
||||
return 0;
|
||||
}
|
||||
if (result) *result = NULL;
|
||||
return (err == ERANGE) ? ERANGE : 0;
|
||||
}
|
||||
|
||||
int DDNATV_NSS_SYM(getpwnam_r)(const char *name, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result) {
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
static int (*orig_getpwnam_r)(const char *, struct passwd *, char *, size_t, struct passwd **) = NULL;
|
||||
if (!orig_getpwnam_r) orig_getpwnam_r = (int (*)(const char *, struct passwd *, char *, size_t, struct passwd **))dlsym(RTLD_NEXT, "getpwnam_r");
|
||||
int ret = orig_getpwnam_r ? orig_getpwnam_r(name, pwd, buf, buflen, result) : ENOENT;
|
||||
#else
|
||||
int ret = g_test_orig_nss_r_ret;
|
||||
if (result) *result = (ret == 0 && g_test_orig_nss_r_found) ? pwd : NULL;
|
||||
#endif
|
||||
if (ret == 0 && result && *result != NULL) {
|
||||
present_privsep_user_as_root(*result);
|
||||
return 0;
|
||||
}
|
||||
if (ret != 0 && ret != ENOENT) {
|
||||
if (result) *result = NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int err = ddnatv_lookup_passwd(name, 0, 1, pwd, buf, buflen);
|
||||
if (err == 0) {
|
||||
present_privsep_user_as_root(pwd);
|
||||
if (result) *result = pwd;
|
||||
return 0;
|
||||
}
|
||||
if (result) *result = NULL;
|
||||
return (err == ERANGE) ? ERANGE : 0;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,818 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
||||
// parts share their static functions and are built with the same command as before).
|
||||
//
|
||||
// Path translation: Ubuntu paths (/usr, /etc, ...) mapped into the rootfs, Android's own mounts
|
||||
// left alone, and the library directories of the rootfs.
|
||||
|
||||
// ---- Path translation -------------------------------------------------------------------
|
||||
// Pure string functions (no syscalls), so tests/native can check them directly. They run on
|
||||
// every file-system call, so they avoid snprintf and compute the rootfs prefix length once.
|
||||
|
||||
static char g_ddnatv_root[PATH_MAX] = {0};
|
||||
static size_t g_ddnatv_root_len = 0;
|
||||
|
||||
static void ddnatv_set_root(const char *root, size_t len) {
|
||||
if (!root) len = 0;
|
||||
if (len > sizeof(g_ddnatv_root) - 1) len = sizeof(g_ddnatv_root) - 1;
|
||||
if (len > 0) memcpy(g_ddnatv_root, root, len);
|
||||
g_ddnatv_root[len] = '\0';
|
||||
g_ddnatv_root_len = len;
|
||||
}
|
||||
|
||||
// Mounts that proot used to provide: "src:dst" pairs from DDNATV_BINDS ('|' separated), longest dst first.
|
||||
// A path under dst is read from src; src equal to dst keeps a host path that would otherwise be
|
||||
// looked up in the rootfs.
|
||||
struct ddnatv_bind {
|
||||
char *src, *dst;
|
||||
size_t src_len, dst_len;
|
||||
int identity;
|
||||
int order; // position in DDNATV_BINDS: the earlier of two mounts of one folder is its guest path
|
||||
};
|
||||
// as many as the session has: a fixed table dropped what came after its 128th entry
|
||||
static struct ddnatv_bind *g_binds = NULL;
|
||||
static int g_nbinds = 0;
|
||||
static char *g_binds_raw = NULL;
|
||||
// The session's host name (DDNATV_HOSTNAME), what proot put in uname(): Android's kernel says "localhost"
|
||||
static char g_hostname[65];
|
||||
// A Flatpak sandbox's: the session's rootfs (DDNATV_LINK_ROOT), whose hook stored absolute guest
|
||||
// link targets as paths in it, as the kernel follows them there; in the sandbox they are guest paths
|
||||
static char g_link_root[PATH_MAX];
|
||||
static size_t g_link_root_len = 0;
|
||||
|
||||
static int ddnatv_bind_longer_first(const void *a, const void *b) {
|
||||
const struct ddnatv_bind *x = a, *y = b;
|
||||
return (y->dst_len > x->dst_len) - (y->dst_len < x->dst_len);
|
||||
}
|
||||
|
||||
static void ddnatv_load_binds(const char *spec) {
|
||||
if (!spec || !spec[0]) return;
|
||||
char *raw = strdup(spec);
|
||||
char *copy = strdup(spec);
|
||||
if (!raw || !copy) { free(raw); free(copy); return; }
|
||||
char *save = NULL;
|
||||
size_t count = 1;
|
||||
for (const char *c = spec; *c; c++) count += *c == '|';
|
||||
g_binds = calloc(count, sizeof(*g_binds));
|
||||
if (!g_binds) { free(raw); free(copy); return; }
|
||||
for (char *entry = strtok_r(copy, "|", &save); entry && (size_t)g_nbinds < count; entry = strtok_r(NULL, "|", &save)) {
|
||||
char *colon = strchr(entry, ':');
|
||||
const char *src = entry;
|
||||
const char *dst = colon ? colon + 1 : entry;
|
||||
if (colon) *colon = '\0';
|
||||
if (src[0] != '/' || dst[0] != '/' || strcmp(dst, "/") == 0) continue;
|
||||
struct ddnatv_bind *b = &g_binds[g_nbinds++];
|
||||
b->src = strdup(src);
|
||||
b->dst = strdup(dst);
|
||||
b->src_len = strlen(src);
|
||||
b->dst_len = strlen(dst);
|
||||
while (b->dst_len > 1 && b->dst[b->dst_len - 1] == '/') b->dst[--b->dst_len] = '\0';
|
||||
while (b->src_len > 1 && b->src[b->src_len - 1] == '/') b->src[--b->src_len] = '\0';
|
||||
b->identity = strcmp(b->src, b->dst) == 0;
|
||||
b->order = g_nbinds - 1;
|
||||
}
|
||||
free(copy);
|
||||
qsort(g_binds, (size_t)g_nbinds, sizeof(g_binds[0]), ddnatv_bind_longer_first);
|
||||
g_binds_raw = raw;
|
||||
}
|
||||
|
||||
// Appends s[0..len) to out[0..used) and NUL-terminates, truncating like snprintf.
|
||||
// Returns the new length.
|
||||
static size_t ddnatv_append(char *out, size_t outsz, size_t used, const char *s, size_t len) {
|
||||
if (outsz == 0) return 0;
|
||||
size_t room = used < outsz - 1 ? outsz - 1 - used : 0;
|
||||
if (len > room) len = room;
|
||||
if (len > 0) memcpy(out + used, s, len);
|
||||
out[used + len] = '\0';
|
||||
return used + len;
|
||||
}
|
||||
|
||||
// Copies the string src into out[0..outsz), truncated and NUL-terminated as strncpy() followed by
|
||||
// out[outsz - 1] = '\0' leaves it, but writes only the bytes of the string: strncpy() fills the rest
|
||||
// of a PATH_MAX buffer with zeros, 4 KB of writes for a path of 30 characters, and in a child that a
|
||||
// shell has just forked every page written is a copy-on-write fault.
|
||||
static size_t ddnatv_copy_cstr(char *out, size_t outsz, const char *src) {
|
||||
if (outsz == 0) return 0;
|
||||
size_t len = strnlen(src, outsz - 1);
|
||||
memcpy(out, src, len);
|
||||
out[len] = '\0';
|
||||
return len;
|
||||
}
|
||||
|
||||
static inline int is_path_prefix(const char *path, const char *prefix, size_t prefix_len) {
|
||||
if (strncmp(path, prefix, prefix_len) != 0) return 0;
|
||||
return path[prefix_len] == '/' || path[prefix_len] == '\0';
|
||||
}
|
||||
|
||||
static inline const char *get_rootfs_subpath(const char *target) {
|
||||
if (!target || g_ddnatv_root_len == 0) return NULL;
|
||||
if (strncmp(target, g_ddnatv_root, g_ddnatv_root_len) == 0 &&
|
||||
(target[g_ddnatv_root_len] == '/' || target[g_ddnatv_root_len] == '\0')) {
|
||||
return target + g_ddnatv_root_len;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// A guest path that a session mount puts outside the rootfs (/run/droiddeck, a game library): a
|
||||
// program or script there is the guest's, and a script's "#!/bin/sh" is the rootfs shell. The
|
||||
// kernel ran them with Android's /system/bin/sh, which found no /usr/bin/kwin_wayland.
|
||||
static int is_mapped_guest_path(const char *path) {
|
||||
if (!path || path[0] != '/') return 0;
|
||||
for (int i = 0; i < g_nbinds; i++) {
|
||||
const struct ddnatv_bind *b = &g_binds[i];
|
||||
if (b->identity) continue;
|
||||
if (is_path_prefix(path, b->dst, b->dst_len)) return 1;
|
||||
// its real place too: a relative program is made absolute from the real working directory
|
||||
if (is_path_prefix(path, b->src, b->src_len) && get_rootfs_subpath(b->src) == NULL) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Android's own mounts and files, which must never be redirected into the rootfs.
|
||||
// `path` starts with '/'; a prefix can only match when its second character does.
|
||||
static int is_android_host_path(const char *path) {
|
||||
switch (path[1]) {
|
||||
case 'a': return is_path_prefix(path, "/apex", 5);
|
||||
case 'c': return is_path_prefix(path, "/config", 7);
|
||||
case 'd': return is_path_prefix(path, "/dev", 4) || is_path_prefix(path, "/d", 2) ||
|
||||
is_path_prefix(path, "/data", 5);
|
||||
case 'l': return is_path_prefix(path, "/linkerconfig", 13);
|
||||
case 'o': return is_path_prefix(path, "/odm", 4) || is_path_prefix(path, "/oem", 4);
|
||||
case 'p': return is_path_prefix(path, "/proc", 5) || is_path_prefix(path, "/product", 8);
|
||||
case 's': return strcmp(path, "/sbin/su") == 0 || strcmp(path, "/su") == 0 ||
|
||||
is_path_prefix(path, "/sys", 4) || is_path_prefix(path, "/system", 7) ||
|
||||
is_path_prefix(path, "/system_ext", 11) || is_path_prefix(path, "/sdcard", 7) ||
|
||||
is_path_prefix(path, "/storage", 8);
|
||||
case 'v': return is_path_prefix(path, "/vendor", 7);
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static const char *rewrite_path_in_root(const char *path, char *buffer, size_t bufsize);
|
||||
|
||||
#if defined(__aarch64__)
|
||||
static long raw_readlinkat(int dirfd, const char *path, char *buf, size_t size) {
|
||||
register long x8 __asm__("x8") = 78; // readlinkat, made directly: the hook's own readlink would map the path again
|
||||
register long x0 __asm__("x0") = dirfd;
|
||||
register long x1 __asm__("x1") = (long)path;
|
||||
register long x2 __asm__("x2") = (long)buf;
|
||||
register long x3 __asm__("x3") = (long)size;
|
||||
__asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3) : "memory");
|
||||
return x0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static __thread int t_bind_link_depth = 0;
|
||||
|
||||
static void normalize_path(char *path);
|
||||
|
||||
#if defined(__aarch64__)
|
||||
static long raw_link_dir(const char *link, char *out, size_t size) {
|
||||
const char *slash = strrchr(link, '/');
|
||||
if (!slash || (size_t)(slash - link) >= PATH_MAX) return -1;
|
||||
char dir[PATH_MAX];
|
||||
size_t dlen = slash == link ? 1 : (size_t)(slash - link);
|
||||
memcpy(dir, link, dlen);
|
||||
dir[dlen] = '\0';
|
||||
register long x8 __asm__("x8") = 56;
|
||||
register long x0 __asm__("x0") = AT_FDCWD;
|
||||
register long x1 __asm__("x1") = (long)dir;
|
||||
register long x2 __asm__("x2") = O_PATH | O_DIRECTORY | O_CLOEXEC;
|
||||
register long x3 __asm__("x3") = 0;
|
||||
__asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3) : "memory");
|
||||
long fd = x0;
|
||||
if (fd < 0) return -1;
|
||||
char proc[32];
|
||||
snprintf(proc, sizeof(proc), "/proc/self/fd/%ld", fd);
|
||||
long n = raw_readlinkat(AT_FDCWD, proc, out, size - 1);
|
||||
register long c8 __asm__("x8") = 57;
|
||||
register long c0 __asm__("x0") = fd;
|
||||
__asm__ volatile("svc #0" : "+r"(c0) : "r"(c8) : "memory");
|
||||
if (n <= 0 || out[0] != '/') return -1;
|
||||
out[n] = '\0';
|
||||
return n;
|
||||
}
|
||||
|
||||
static int climbs_past_root(const char *dir, const char *target) {
|
||||
long depth = 0;
|
||||
for (const char *p = dir; *p; p++)
|
||||
if (*p != '/' && (p == dir || p[-1] == '/')) depth++;
|
||||
for (const char *p = target; *p;) {
|
||||
while (*p == '/') p++;
|
||||
const char *end = p;
|
||||
while (*end && *end != '/') end++;
|
||||
size_t len = (size_t)(end - p);
|
||||
if (len == 2 && p[0] == '.' && p[1] == '.') {
|
||||
if (--depth < 0) return 1;
|
||||
} else if (len > 0 && !(len == 1 && p[0] == '.')) {
|
||||
depth++;
|
||||
}
|
||||
p = end;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t guest_path_of_mount_source(char *buf, size_t len, size_t cap, int *converted);
|
||||
|
||||
static const char *guest_path_of_host_dir(const char *dir, char *out, size_t size) {
|
||||
const char *sub = get_rootfs_subpath(dir);
|
||||
if (sub) {
|
||||
ddnatv_copy_cstr(out, size, sub[0] ? sub : "/");
|
||||
return out;
|
||||
}
|
||||
if (dir[0] != '/') return NULL;
|
||||
size_t len = ddnatv_copy_cstr(out, size, dir);
|
||||
int converted = 0;
|
||||
guest_path_of_mount_source(out, len, size, &converted);
|
||||
if (converted) return out;
|
||||
for (int i = 0; i < g_nbinds; i++) {
|
||||
const struct ddnatv_bind *b = &g_binds[i];
|
||||
if (b->identity && is_path_prefix(dir, b->dst, b->dst_len)) {
|
||||
ddnatv_copy_cstr(out, size, dir);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int join_into_bind(const char *dir, const char *target, const char *rest, char *out, size_t size) {
|
||||
char guest_dir_buf[PATH_MAX];
|
||||
const char *guest_dir = guest_path_of_host_dir(dir, guest_dir_buf, sizeof(guest_dir_buf));
|
||||
if (!guest_dir) return 0;
|
||||
char joined[PATH_MAX];
|
||||
size_t used = ddnatv_append(joined, sizeof(joined), 0, guest_dir, strlen(guest_dir));
|
||||
used = ddnatv_append(joined, sizeof(joined), used, "/", 1);
|
||||
used = ddnatv_append(joined, sizeof(joined), used, target, strlen(target));
|
||||
if (used + 1 >= sizeof(joined)) return 0;
|
||||
normalize_path(joined);
|
||||
used = ddnatv_append(out, size, 0, joined, strlen(joined));
|
||||
if (used + 1 >= size) return 0;
|
||||
if (rest[0] == '/' && used > 0 && out[used - 1] == '/') rest++;
|
||||
size_t rest_len = strlen(rest);
|
||||
if (used + rest_len + 1 >= size) return 0;
|
||||
ddnatv_append(out, size, used, rest, rest_len);
|
||||
return out[0] == '/';
|
||||
}
|
||||
|
||||
static int relative_link_into_bind(const char *link, const char *target, const char *rest, char *out, size_t size) {
|
||||
char dir[PATH_MAX];
|
||||
if (raw_link_dir(link, dir, sizeof(dir)) >= 0) return join_into_bind(dir, target, rest, out, size);
|
||||
const char *slash = strrchr(link, '/');
|
||||
if (!slash) return 0;
|
||||
size_t dlen = (size_t)(slash - link);
|
||||
memcpy(dir, link, dlen);
|
||||
dir[dlen] = '\0';
|
||||
return join_into_bind(dir, target, rest, out, size);
|
||||
}
|
||||
#endif
|
||||
|
||||
// A mount stand-in can hold symbolic links with absolute guest targets (/sys/class/hidraw/hidraw16 ->
|
||||
// /sys/devices/droiddeck/...): proot follows them inside the guest, the kernel on Android's own /sys.
|
||||
// A link met on the way to a deeper path is replaced by its target, mapped again; the last component is
|
||||
// left as it is (lstat() and readlink() must see the link).
|
||||
static const char *resolve_bind_links(const char *host, size_t src_len, char *buffer, size_t bufsize) {
|
||||
#if defined(__aarch64__)
|
||||
size_t len = strlen(host);
|
||||
if (len >= PATH_MAX || t_bind_link_depth >= 8) return host;
|
||||
for (size_t i = src_len + 1; i < len; i++) {
|
||||
if (host[i] != '/') continue;
|
||||
char prefix[PATH_MAX];
|
||||
memcpy(prefix, host, i);
|
||||
prefix[i] = '\0';
|
||||
char target[PATH_MAX];
|
||||
long n = raw_readlinkat(AT_FDCWD, prefix, target, sizeof(target) - 1);
|
||||
if (n <= 0) continue;
|
||||
target[n] = '\0';
|
||||
char guest[PATH_MAX];
|
||||
if (target[0] != '/') {
|
||||
if (!relative_link_into_bind(prefix, target, host + i, guest, sizeof(guest))) continue;
|
||||
} else {
|
||||
size_t used = ddnatv_append(guest, sizeof(guest), 0, target, (size_t)n);
|
||||
ddnatv_append(guest, sizeof(guest), used, host + i, len - i);
|
||||
}
|
||||
t_bind_link_depth++;
|
||||
const char *mapped = rewrite_path_in_root(guest, buffer, bufsize);
|
||||
if (mapped != buffer) ddnatv_copy_cstr(buffer, bufsize, mapped);
|
||||
t_bind_link_depth--;
|
||||
return buffer;
|
||||
}
|
||||
#else
|
||||
(void)src_len; (void)bufsize;
|
||||
#endif
|
||||
return host == buffer ? buffer : host;
|
||||
}
|
||||
|
||||
// Wine opens every file through its prefix's drive link (dosdevices/z: -> /), so the link must be
|
||||
// followed in the guest's view even where the prefix is plain rootfs: the kernel would look for the
|
||||
// rest of the path on Android's own tree. Only that component is checked, not each one of every path.
|
||||
static const char *resolve_drive_links(const char *host, char *buffer, size_t bufsize) {
|
||||
const char *devices = strstr(host, "/dosdevices/");
|
||||
if (!devices) return host;
|
||||
return resolve_bind_links(host, (size_t)(devices - host) + 11, buffer, bufsize);
|
||||
}
|
||||
|
||||
// Maps an absolute Ubuntu path into the rootfs. Returns `path` (possibly advanced past
|
||||
// redundant leading "/" and "/." components) when it needs no change, else `buffer`.
|
||||
static const char *rewrite_path_in_root(const char *path, char *buffer, size_t bufsize) {
|
||||
if (!path) return NULL;
|
||||
if (path[0] != '/' || g_ddnatv_root_len == 0) return path;
|
||||
|
||||
// Strip multiple redundant leading slashes: //foo -> /foo
|
||||
while (path[0] == '/' && path[1] == '/') {
|
||||
path++;
|
||||
}
|
||||
|
||||
// Strip leading /./: /./boot -> /boot
|
||||
while (path[0] == '/' && path[1] == '.' && (path[2] == '/' || path[2] == '\0')) {
|
||||
if (path[2] == '/') {
|
||||
path += 2;
|
||||
} else {
|
||||
path = "/";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Root directory
|
||||
if (path[1] == '\0') {
|
||||
ddnatv_append(buffer, bufsize, 0, g_ddnatv_root, g_ddnatv_root_len);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
for (int i = 0; i < g_nbinds; i++) {
|
||||
const struct ddnatv_bind *b = &g_binds[i];
|
||||
if (path[1] == b->dst[1] && is_path_prefix(path, b->dst, b->dst_len)) {
|
||||
if (b->identity) return path;
|
||||
size_t n = ddnatv_append(buffer, bufsize, 0, b->src, b->src_len);
|
||||
ddnatv_append(buffer, bufsize, n, path + b->dst_len, strlen(path + b->dst_len));
|
||||
return resolve_bind_links(buffer, b->src_len, buffer, bufsize);
|
||||
}
|
||||
}
|
||||
|
||||
// Already inside The native userland rootfs
|
||||
const char *sub = get_rootfs_subpath(path);
|
||||
if (sub != NULL) {
|
||||
if (sub[0] == '/' && t_bind_link_depth < 8) {
|
||||
for (int i = 0; i < g_nbinds; i++) {
|
||||
const struct ddnatv_bind *b = &g_binds[i];
|
||||
if (b->identity || sub[1] != b->dst[1] || !is_path_prefix(sub, b->dst, b->dst_len)) continue;
|
||||
char guest[PATH_MAX];
|
||||
ddnatv_copy_cstr(guest, sizeof(guest), sub);
|
||||
t_bind_link_depth++;
|
||||
const char *mapped = rewrite_path_in_root(guest, buffer, bufsize);
|
||||
if (mapped != buffer) ddnatv_copy_cstr(buffer, bufsize, mapped);
|
||||
t_bind_link_depth--;
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
return resolve_drive_links(path, buffer, bufsize);
|
||||
}
|
||||
|
||||
// The real place of a mounted folder, which getcwd() and /proc/self/fd links answer for it: Steam
|
||||
// made a game's path from one on the SD card library and found no sekiro.exe in the rootfs
|
||||
for (int i = 0; i < g_nbinds; i++) {
|
||||
const struct ddnatv_bind *b = &g_binds[i];
|
||||
if (!b->identity && path[1] == b->src[1] && is_path_prefix(path, b->src, b->src_len))
|
||||
return resolve_drive_links(path, buffer, bufsize);
|
||||
}
|
||||
|
||||
// POSIX shared memory (/dev/shm) redirection to The native userland rootfs tmp/shm
|
||||
if (strncmp(path, "/dev/shm", 8) == 0 && (path[8] == '/' || path[8] == '\0')) {
|
||||
size_t n = ddnatv_append(buffer, bufsize, 0, g_ddnatv_root, g_ddnatv_root_len);
|
||||
n = ddnatv_append(buffer, bufsize, n, "/tmp/shm", 8);
|
||||
ddnatv_append(buffer, bufsize, n, path + 8, strlen(path + 8));
|
||||
return buffer;
|
||||
}
|
||||
|
||||
// Real host Android kernel & system mounts
|
||||
if (is_android_host_path(path)) {
|
||||
return path;
|
||||
}
|
||||
|
||||
// Any other absolute path belongs to The native userland rootfs (/boot, /media, /snap, /etc, /usr, ...)
|
||||
size_t n = ddnatv_append(buffer, bufsize, 0, g_ddnatv_root, g_ddnatv_root_len);
|
||||
ddnatv_append(buffer, bufsize, n, path, strlen(path));
|
||||
return resolve_drive_links(buffer, buffer, bufsize);
|
||||
}
|
||||
|
||||
// A link target the session's hook stored as a path in its rootfs, given back as the guest path it
|
||||
// stands for (see g_link_root). Rewrites target[0..len) in place; returns the new length.
|
||||
static size_t link_root_guest_target(char *target, size_t len) {
|
||||
if (!g_link_root_len || len < g_link_root_len || memcmp(target, g_link_root, g_link_root_len) != 0) return len;
|
||||
if (len == g_link_root_len) { target[0] = '/'; target[1] = '\0'; return 1; }
|
||||
if (target[g_link_root_len] != '/') return len;
|
||||
memmove(target, target + g_link_root_len, len - g_link_root_len + 1);
|
||||
return len - g_link_root_len;
|
||||
}
|
||||
|
||||
// The guest path a symbolic link at absolute guest path `path` leads to, followed in the guest's
|
||||
// view: the kernel resolves a relative target next to the link's real place, and in a mount that is
|
||||
// elsewhere (Flatpak's /run/user/0/wayland-0 -> ../../flatpak/wayland-0, where /run/flatpak/wayland-0
|
||||
// is another mount). Returns `path` when it is no link.
|
||||
static const char *follow_guest_link(const char *path, char *out, size_t outsz) {
|
||||
#if defined(__aarch64__)
|
||||
const char *cur = path;
|
||||
for (int depth = 0; depth < 8; depth++) {
|
||||
char host_buf[PATH_MAX];
|
||||
const char *host = rewrite_path_in_root(cur, host_buf, sizeof(host_buf));
|
||||
// /dev/stdin and /dev/fd are mounts of /proc/self/fd, whose links ("pipe:[123]") only the kernel opens
|
||||
if (strncmp(host, "/proc/", 6) == 0) return cur;
|
||||
char target[PATH_MAX];
|
||||
long n = raw_readlinkat(AT_FDCWD, host, target, sizeof(target) - 1);
|
||||
if (n <= 0) return cur;
|
||||
target[n] = '\0';
|
||||
n = (long)link_root_guest_target(target, (size_t)n);
|
||||
char joined[PATH_MAX];
|
||||
if (target[0] == '/') {
|
||||
ddnatv_copy_cstr(joined, sizeof(joined), target);
|
||||
} else {
|
||||
const char *slash = strrchr(cur, '/');
|
||||
size_t dir_len = slash ? (size_t)(slash - cur) : 0;
|
||||
size_t used = ddnatv_append(joined, sizeof(joined), 0, cur, dir_len);
|
||||
used = ddnatv_append(joined, sizeof(joined), used, "/", 1);
|
||||
ddnatv_append(joined, sizeof(joined), used, target, strlen(target));
|
||||
}
|
||||
// lexically, as the guest sees it
|
||||
size_t len = 0;
|
||||
out[0] = '\0';
|
||||
for (const char *p = joined; *p;) {
|
||||
while (*p == '/') p++;
|
||||
const char *end = strchr(p, '/');
|
||||
size_t part = end ? (size_t)(end - p) : strlen(p);
|
||||
if (part == 0) break;
|
||||
if (part == 1 && p[0] == '.') {
|
||||
} else if (part == 2 && p[0] == '.' && p[1] == '.') {
|
||||
while (len > 0 && out[len - 1] != '/') len--;
|
||||
if (len > 0) len--;
|
||||
out[len] = '\0';
|
||||
} else if (len + 1 + part < outsz) {
|
||||
out[len++] = '/';
|
||||
memcpy(out + len, p, part);
|
||||
len += part;
|
||||
out[len] = '\0';
|
||||
}
|
||||
p += part;
|
||||
}
|
||||
if (len == 0) { out[0] = '/'; out[1] = '\0'; }
|
||||
cur = out;
|
||||
}
|
||||
return cur;
|
||||
#else
|
||||
(void)out; (void)outsz;
|
||||
return path;
|
||||
#endif
|
||||
}
|
||||
|
||||
// On ENOENT, stat()/lstat()/readlink() retry /usr/bin <-> /bin and /usr/sbin <-> /sbin, for
|
||||
// rootfs trees where bin and sbin are real directories. Returns 1 for the bin pair, 2 for the
|
||||
// sbin pair (with the other spelling in `alt`), 0 when `sub` is in neither.
|
||||
#define DDNATV_USRMERGE_BIN 1
|
||||
#define DDNATV_USRMERGE_SBIN 2
|
||||
static int usrmerge_alt_path(const char *sub, char *alt, size_t altsz) {
|
||||
const char *dir;
|
||||
const char *rest;
|
||||
int pair;
|
||||
if (strncmp(sub, "/usr/bin/", 9) == 0) {
|
||||
dir = "/bin/"; rest = sub + 9; pair = DDNATV_USRMERGE_BIN;
|
||||
} else if (strncmp(sub, "/bin/", 5) == 0) {
|
||||
dir = "/usr/bin/"; rest = sub + 5; pair = DDNATV_USRMERGE_BIN;
|
||||
} else if (strncmp(sub, "/usr/sbin/", 10) == 0) {
|
||||
dir = "/sbin/"; rest = sub + 10; pair = DDNATV_USRMERGE_SBIN;
|
||||
} else if (strncmp(sub, "/sbin/", 6) == 0) {
|
||||
dir = "/usr/sbin/"; rest = sub + 6; pair = DDNATV_USRMERGE_SBIN;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
size_t n = ddnatv_append(alt, altsz, 0, g_ddnatv_root, g_ddnatv_root_len);
|
||||
n = ddnatv_append(alt, altsz, n, dir, strlen(dir));
|
||||
ddnatv_append(alt, altsz, n, rest, strlen(rest));
|
||||
return pair;
|
||||
}
|
||||
|
||||
// Whether postprocess_readlink() below can change a link's target of length target_len, read
|
||||
// through linkpath: only a target that starts with the rootfs path (at least that long) or a relative
|
||||
// one into /etc/alternatives (at least "../alternatives", 15 characters, from a directory next to it;
|
||||
// any "../" under /etc/alternatives itself). lstat() of a link reads it only then, to report its
|
||||
// rewritten length.
|
||||
static int readlink_result_may_change(const char *linkpath, long long target_len) {
|
||||
if (g_ddnatv_root_len > 0 && target_len >= (long long)g_ddnatv_root_len) return 1;
|
||||
if (!linkpath || linkpath[0] != '/') return 0;
|
||||
if (strncmp(linkpath, "/etc/alternatives", 17) == 0) return target_len >= 2;
|
||||
return target_len >= 15;
|
||||
}
|
||||
|
||||
// Helper for path normalization
|
||||
static void normalize_path(char *path) {
|
||||
if (!path || !*path) return;
|
||||
int is_abs = (path[0] == '/');
|
||||
char *stack[128];
|
||||
int top = 0;
|
||||
|
||||
char tmp[4096];
|
||||
strncpy(tmp, path, sizeof(tmp) - 1);
|
||||
tmp[sizeof(tmp) - 1] = '\0';
|
||||
|
||||
char *saveptr = NULL;
|
||||
char *tok = strtok_r(tmp, "/", &saveptr);
|
||||
while (tok) {
|
||||
if (strcmp(tok, ".") == 0) {
|
||||
// ignore
|
||||
} else if (strcmp(tok, "..") == 0) {
|
||||
if (top > 0) top--;
|
||||
} else {
|
||||
if (top < 128) stack[top++] = tok;
|
||||
}
|
||||
tok = strtok_r(NULL, "/", &saveptr);
|
||||
}
|
||||
|
||||
char *dst = path;
|
||||
if (is_abs) *dst++ = '/';
|
||||
for (int i = 0; i < top; i++) {
|
||||
size_t len = strlen(stack[i]);
|
||||
memcpy(dst, stack[i], len);
|
||||
dst += len;
|
||||
if (i + 1 < top) *dst++ = '/';
|
||||
}
|
||||
if (dst == path && is_abs) *dst++ = '/';
|
||||
*dst = '\0';
|
||||
}
|
||||
|
||||
// /proc/self/fd/N, /proc/thread-self/fd/N and /proc/PID/fd/N: the real path of an open file. A program
|
||||
// that finds a path this way (Bun and Zig programs do: their realpath() is an open and a readlink of
|
||||
// this) goes on with system calls of its own, which the hook does not see, so a path inside the rootfs
|
||||
// has to keep its real beginning there: "/home" for $DDNATV_ROOT/home is a path that does not exist
|
||||
// for them ("ENOENT, realpath '/home'" in Kilo). It is what getcwd() answers, too.
|
||||
static int is_proc_fd_link(const char *path) {
|
||||
if (!path || strncmp(path, "/proc/", 6) != 0) return 0;
|
||||
const char *p = path + 6;
|
||||
if (strncmp(p, "self/", 5) == 0) {
|
||||
p += 5;
|
||||
} else if (strncmp(p, "thread-self/", 12) == 0) {
|
||||
p += 12;
|
||||
} else {
|
||||
if (*p < '0' || *p > '9') return 0;
|
||||
while (*p >= '0' && *p <= '9') p++;
|
||||
if (*p != '/') return 0;
|
||||
p++;
|
||||
}
|
||||
// fd/N and nothing after it: a path beneath the link goes through it to somewhere else
|
||||
if (strncmp(p, "fd/", 3) != 0) return 0;
|
||||
p += 3;
|
||||
if (*p < '0' || *p > '9') return 0;
|
||||
while (*p >= '0' && *p <= '9') p++;
|
||||
return *p == '\0';
|
||||
}
|
||||
|
||||
// The guest path of a folder that a session mount puts outside the rootfs, for what the kernel
|
||||
// reports as its real place (getcwd(), /proc/self/cwd and fd links). Wine found no DOS drive for
|
||||
// Sekiro's folder on the SD card library ("starting in the Windows directory"), and the game quit.
|
||||
// Rewrites buf[0..len) in place when the guest path fits in cap; returns the new length.
|
||||
static size_t guest_path_of_mount_source(char *buf, size_t len, size_t cap, int *converted) {
|
||||
if (converted) *converted = 0;
|
||||
const struct ddnatv_bind *best = NULL;
|
||||
for (int i = 0; i < g_nbinds; i++) {
|
||||
const struct ddnatv_bind *b = &g_binds[i];
|
||||
if (b->identity || get_rootfs_subpath(b->src) != NULL || len < b->src_len) continue;
|
||||
if (memcmp(buf, b->src, b->src_len) != 0 || (len > b->src_len && buf[b->src_len] != '/')) continue;
|
||||
if (!best || b->src_len > best->src_len || (b->src_len == best->src_len && b->order < best->order)) best = b;
|
||||
}
|
||||
if (!best) return len;
|
||||
size_t rest = len - best->src_len;
|
||||
if (best->dst_len + rest + 1 > cap) return len;
|
||||
memmove(buf + best->dst_len, buf + best->src_len, rest);
|
||||
memcpy(buf, best->dst, best->dst_len);
|
||||
buf[best->dst_len + rest] = '\0';
|
||||
if (converted) *converted = 1;
|
||||
return best->dst_len + rest;
|
||||
}
|
||||
|
||||
static ssize_t postprocess_readlink(const char *linkpath, char *buf, ssize_t ret, size_t bufsiz) {
|
||||
if (ret <= 0) return ret;
|
||||
if ((size_t)ret < bufsiz) {
|
||||
buf[ret] = '\0';
|
||||
ret = (ssize_t)link_root_guest_target(buf, (size_t)ret);
|
||||
}
|
||||
ret = (ssize_t)guest_path_of_mount_source(buf, (size_t)ret, bufsiz, NULL);
|
||||
|
||||
// 1. Strip g_ddnatv_root if target starts with it
|
||||
if (g_ddnatv_root_len > 0 && !is_proc_fd_link(linkpath)) {
|
||||
size_t rlen = g_ddnatv_root_len;
|
||||
if ((size_t)ret >= rlen && memcmp(buf, g_ddnatv_root, rlen) == 0 &&
|
||||
((size_t)ret == rlen || buf[rlen] == '/')) {
|
||||
size_t new_len = ret - rlen;
|
||||
if (new_len == 0) {
|
||||
buf[0] = '/';
|
||||
return 1;
|
||||
} else {
|
||||
memmove(buf, buf + rlen, new_len);
|
||||
return (ssize_t)new_len;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Normalize relative alternatives links. The result can only start with /etc/alternatives
|
||||
// (or the link be in it) when "alternatives" is in the target or in the link's own path: the
|
||||
// normalized path is made of their components. Most relative links have neither (the ones of
|
||||
// /usr/lib and /usr/share/doc), and these skip the copies and the normalization, which were a
|
||||
// tenth of what "cp -a" of a tree of them cost.
|
||||
if (linkpath && linkpath[0] == '/' && (size_t)ret < PATH_MAX && ret >= 2 && buf[0] == '.' && buf[1] == '.' &&
|
||||
(memmem(buf, (size_t)ret, "alternatives", 12) != NULL || strstr(linkpath, "alternatives") != NULL)) {
|
||||
char link_copy[PATH_MAX];
|
||||
memcpy(link_copy, buf, ret);
|
||||
link_copy[ret] = '\0';
|
||||
|
||||
if (strncmp(link_copy, "..", 2) == 0) {
|
||||
char parent[PATH_MAX];
|
||||
snprintf(parent, sizeof(parent), "%s", linkpath);
|
||||
char *last_slash = strrchr(parent, '/');
|
||||
if (last_slash) {
|
||||
if (last_slash == parent) {
|
||||
*(last_slash + 1) = '\0';
|
||||
} else {
|
||||
*last_slash = '\0';
|
||||
}
|
||||
}
|
||||
char combined[PATH_MAX * 2];
|
||||
snprintf(combined, sizeof(combined), "%s/%s", parent, link_copy);
|
||||
normalize_path(combined);
|
||||
|
||||
if (strncmp(combined, "/etc/alternatives", 17) == 0 ||
|
||||
strncmp(linkpath, "/etc/alternatives", 17) == 0) {
|
||||
size_t clen = strlen(combined);
|
||||
if (clen <= bufsiz) {
|
||||
memcpy(buf, combined, clen);
|
||||
return (ssize_t)clen;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Android's SELinux label, which the xattr hooks keep away from Ubuntu programs.
|
||||
static const char k_selinux_xattr[] = "security.selinux";
|
||||
|
||||
static int is_selinux_xattr(const char *name) {
|
||||
return name != NULL && strcmp(name, k_selinux_xattr) == 0;
|
||||
}
|
||||
|
||||
// Removes "security.selinux" from a listxattr() result (NUL-separated names); returns the new length.
|
||||
static ssize_t drop_selinux_xattr(char *list, ssize_t len) {
|
||||
if (!list || len <= 0) return len;
|
||||
ssize_t out = 0;
|
||||
ssize_t i = 0;
|
||||
while (i < len) {
|
||||
size_t name_len = strnlen(list + i, (size_t)(len - i));
|
||||
ssize_t entry = (ssize_t)name_len + 1;
|
||||
if (entry > len - i) entry = len - i;
|
||||
if (name_len != sizeof(k_selinux_xattr) - 1 || memcmp(list + i, k_selinux_xattr, name_len) != 0) {
|
||||
memmove(list + out, list + i, (size_t)entry);
|
||||
out += entry;
|
||||
}
|
||||
i += entry;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Library directories of the rootfs, in the order ld.so searches them. ld.so tries every
|
||||
// directory for every library, so the multiarch ones that hold nearly all libraries come first
|
||||
// (as in glibc's own default order). Keep in sync with Environment.kt, 00-env.sh and su.sh.
|
||||
static const char *const k_rootfs_lib_dirs[] = {
|
||||
"/lib/aarch64-linux-gnu", "/usr/lib/aarch64-linux-gnu",
|
||||
"/lib/arm-linux-gnueabihf", "/usr/lib/arm-linux-gnueabihf",
|
||||
"/lib", "/usr/lib", "/usr/local/lib",
|
||||
"/usr/lib/systemd", "/lib/systemd",
|
||||
"/usr/lib/aarch64-linux-gnu/systemd", "/lib/aarch64-linux-gnu/systemd",
|
||||
"/usr/lib/arm-linux-gnueabihf/systemd", "/lib/arm-linux-gnueabihf/systemd",
|
||||
};
|
||||
|
||||
// Whether the ':'-separated list holds the directory dir[0..dir_len) as one of its entries.
|
||||
static int lib_dir_list_has(const char *list, const char *dir, size_t dir_len) {
|
||||
if (!list) return 0;
|
||||
for (const char *p = list; *p;) {
|
||||
const char *end = strchr(p, ':');
|
||||
size_t len = end ? (size_t)(end - p) : strlen(p);
|
||||
if (len == dir_len && memcmp(p, dir, len) == 0) return 1;
|
||||
if (!end) break;
|
||||
p = end + 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Appends the rootfs library directories as a ':'-separated list (with a leading ':' when
|
||||
// `separate` is set), leaving out those that `have` (a list too, or NULL) names already: the
|
||||
// caller's LD_LIBRARY_PATH is usually this very list, and ld.so spends time on every directory of
|
||||
// the list it is given, in every program that is started, and tries it again for each library that
|
||||
// is not found. Returns the new length.
|
||||
static size_t append_rootfs_lib_dirs_missing_from(char *out, size_t outsz, size_t used, int separate, const char *have) {
|
||||
for (size_t i = 0; i < sizeof(k_rootfs_lib_dirs) / sizeof(k_rootfs_lib_dirs[0]); i++) {
|
||||
// a Flatpak sandbox mounts /usr from its runtime
|
||||
char buf[PATH_MAX];
|
||||
const char *full = rewrite_path_in_root(k_rootfs_lib_dirs[i], buf, sizeof(buf));
|
||||
size_t n = strlen(full);
|
||||
if (have && have[0] != '\0' && lib_dir_list_has(have, full, n)) continue;
|
||||
// whole directories only: a cut-off one would be searched in the wrong place
|
||||
if (used + (separate ? 1 : 0) + n >= outsz) continue;
|
||||
if (separate) used = ddnatv_append(out, outsz, used, ":", 1);
|
||||
separate = 1;
|
||||
used = ddnatv_append(out, outsz, used, full, n);
|
||||
}
|
||||
return used;
|
||||
}
|
||||
|
||||
// All of them.
|
||||
static size_t append_rootfs_lib_dirs(char *out, size_t outsz, size_t used, int separate) {
|
||||
return append_rootfs_lib_dirs_missing_from(out, outsz, used, separate, NULL);
|
||||
}
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
// Relative *at() calls. A program that walks a path one component at a time (systemd's chase(), used
|
||||
// by libudev; fts) opens "/" and then "sys", "class", ... relative to the last descriptor, and those
|
||||
// names never pass through rewrite_path(): "/" is the rootfs, whose own sys/ is empty, so the stand-ins
|
||||
// mounted at /sys/class/hidraw and the host's /sys were out of reach. The directories on the way to a
|
||||
// mount (every proper ancestor of a mount's guest path) are recorded by device and inode; a name given
|
||||
// relative to one of them is joined to its guest path and mapped like an absolute one.
|
||||
struct ddnatv_at_dir {
|
||||
unsigned long dev, ino;
|
||||
char *guest;
|
||||
};
|
||||
static struct ddnatv_at_dir *g_at_dirs = NULL;
|
||||
static int g_n_at_dirs = 0;
|
||||
static pthread_once_t g_at_once = PTHREAD_ONCE_INIT;
|
||||
|
||||
#if defined(__aarch64__)
|
||||
static long raw_fstatat(int dirfd, const char *path, struct stat *st, int flags) {
|
||||
register long x8 __asm__("x8") = 79; // newfstatat
|
||||
register long x0 __asm__("x0") = dirfd;
|
||||
register long x1 __asm__("x1") = (long)path;
|
||||
register long x2 __asm__("x2") = (long)st;
|
||||
register long x3 __asm__("x3") = flags;
|
||||
__asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3) : "memory");
|
||||
return x0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void ddnatv_add_at_dir(const char *guest) {
|
||||
#if defined(__aarch64__)
|
||||
for (int i = 0; i < g_n_at_dirs; i++) if (strcmp(g_at_dirs[i].guest, guest) == 0) return;
|
||||
char buf[PATH_MAX];
|
||||
const char *host = rewrite_path_in_root(guest, buf, sizeof(buf));
|
||||
struct stat st;
|
||||
if (raw_fstatat(AT_FDCWD, host, &st, 0) != 0 || !S_ISDIR(st.st_mode)) return;
|
||||
struct ddnatv_at_dir *grown = realloc(g_at_dirs, sizeof(*grown) * (size_t)(g_n_at_dirs + 1));
|
||||
if (!grown) return;
|
||||
g_at_dirs = grown;
|
||||
g_at_dirs[g_n_at_dirs].dev = (unsigned long)st.st_dev;
|
||||
g_at_dirs[g_n_at_dirs].ino = (unsigned long)st.st_ino;
|
||||
g_at_dirs[g_n_at_dirs].guest = strdup(guest);
|
||||
if (g_at_dirs[g_n_at_dirs].guest) g_n_at_dirs++;
|
||||
#else
|
||||
(void)guest;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void ddnatv_build_at_dirs(void) {
|
||||
if (g_nbinds == 0) return;
|
||||
ddnatv_add_at_dir("/");
|
||||
for (int i = 0; i < g_nbinds; i++) {
|
||||
char guest[PATH_MAX];
|
||||
ddnatv_copy_cstr(guest, sizeof(guest), g_binds[i].dst);
|
||||
for (char *slash = strrchr(guest, '/'); slash && slash > guest; slash = strrchr(guest, '/')) {
|
||||
*slash = '\0';
|
||||
ddnatv_add_at_dir(guest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The absolute host path for `rel` given relative to `dirfd`, or NULL when the call needs no change.
|
||||
static const char *map_at(int dirfd, const char *rel, char *buf, size_t size) {
|
||||
#if defined(__aarch64__)
|
||||
if (dirfd == AT_FDCWD || !rel || rel[0] == '\0' || rel[0] == '/' || g_nbinds == 0) return NULL;
|
||||
pthread_once(&g_at_once, ddnatv_build_at_dirs);
|
||||
if (g_n_at_dirs == 0) return NULL;
|
||||
struct stat st;
|
||||
if (raw_fstatat(dirfd, "", &st, AT_EMPTY_PATH) != 0) return NULL;
|
||||
for (int i = 0; i < g_n_at_dirs; i++) {
|
||||
if (g_at_dirs[i].ino != (unsigned long)st.st_ino || g_at_dirs[i].dev != (unsigned long)st.st_dev) continue;
|
||||
char guest[PATH_MAX];
|
||||
const char *base = g_at_dirs[i].guest;
|
||||
size_t used = ddnatv_append(guest, sizeof(guest), 0, base, strlen(base));
|
||||
if (used > 1) used = ddnatv_append(guest, sizeof(guest), used, "/", 1);
|
||||
ddnatv_append(guest, sizeof(guest), used, rel, strlen(rel));
|
||||
const char *mapped = rewrite_path_in_root(guest, buf, size);
|
||||
if (mapped != buf) ddnatv_copy_cstr(buf, size, mapped);
|
||||
return buf;
|
||||
}
|
||||
#else
|
||||
(void)dirfd; (void)rel; (void)buf; (void)size;
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#define DDNATV_AT_REMAP_NAMED(dirfd, pathname, buf) \
|
||||
char buf[PATH_MAX]; \
|
||||
if (map_at((dirfd), (pathname), buf, sizeof(buf))) { \
|
||||
(dirfd) = AT_FDCWD; \
|
||||
(pathname) = buf; \
|
||||
}
|
||||
#define DDNATV_AT_REMAP(dirfd, pathname) DDNATV_AT_REMAP_NAMED(dirfd, pathname, ddnatv_at_buf)
|
||||
#endif /* !DDNATV_HOST_TEST */
|
||||
@@ -0,0 +1,174 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c.
|
||||
//
|
||||
// shm_open() and shm_unlink() of glibc open /dev/shm/NAME with a system call of their own, which
|
||||
// the path hooks never see and Android has no /dev/shm for. They go through open() and unlink()
|
||||
// here, which map /dev/shm.
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
|
||||
static int posix_shm_path(const char *name, char *path, size_t size) {
|
||||
if (!name) { errno = EINVAL; return -1; }
|
||||
while (*name == '/') name++;
|
||||
size_t len = strlen(name);
|
||||
if (len == 0 || len > NAME_MAX || strchr(name, '/') || strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
|
||||
errno = len > NAME_MAX ? ENAMETOOLONG : EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (snprintf(path, size, "/dev/shm/%s", name) >= (int)size) { errno = ENAMETOOLONG; return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
int shm_open(const char *name, int oflag, mode_t mode) {
|
||||
char path[PATH_MAX];
|
||||
if (posix_shm_path(name, path, sizeof(path)) < 0) return -1;
|
||||
return open(path, (oflag & (O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) | O_NOFOLLOW | O_CLOEXEC, mode);
|
||||
}
|
||||
|
||||
int shm_unlink(const char *name) {
|
||||
char path[PATH_MAX];
|
||||
if (posix_shm_path(name, path, sizeof(path)) < 0) return -1;
|
||||
return unlink(path);
|
||||
}
|
||||
|
||||
#endif /* !DDNATV_HOST_TEST */
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
// glibc's posix_spawn child resets every signal handler to its default, the hook's SIGSYS handler
|
||||
// included, before it runs the file actions, and "close everything from N" is close_range(), which
|
||||
// Android's seccomp filter answers with SIGSYS: the child died of it (Python's subprocess with
|
||||
// close_fds does this). It becomes one close per descriptor, added when the program is spawned so
|
||||
// that descriptors opened in between and the ones earlier actions make are closed too; the targets
|
||||
// of actions added after it stay open, as they would. A close of a descriptor that is not open is
|
||||
// no error to glibc.
|
||||
#define CLOSEFROM_SLOTS 64
|
||||
#define CLOSEFROM_KEEP 32
|
||||
static struct closefrom_entry {
|
||||
posix_spawn_file_actions_t *actions;
|
||||
int from; // -1: no close-from yet, only targets of actions seen
|
||||
int highest; // the highest descriptor an action before close-from makes
|
||||
int nkeep;
|
||||
int keep[CLOSEFROM_KEEP];
|
||||
int closed_to; // the descriptors up to this one are closed by actions already added
|
||||
int bad_source; // a dup2 after close-from duplicates a descriptor it closed: glibc's EBADF
|
||||
} g_closefrom[CLOSEFROM_SLOTS];
|
||||
static pthread_mutex_t g_closefrom_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
static struct closefrom_entry *closefrom_entry(posix_spawn_file_actions_t *actions, int create) {
|
||||
struct closefrom_entry *free_slot = NULL;
|
||||
for (int i = 0; i < CLOSEFROM_SLOTS; i++) {
|
||||
if (g_closefrom[i].actions == actions) return &g_closefrom[i];
|
||||
if (!g_closefrom[i].actions && !free_slot) free_slot = &g_closefrom[i];
|
||||
}
|
||||
if (!create || !free_slot) return NULL;
|
||||
memset(free_slot, 0, sizeof(*free_slot));
|
||||
free_slot->actions = actions;
|
||||
free_slot->from = -1;
|
||||
free_slot->highest = -1;
|
||||
free_slot->closed_to = -1;
|
||||
return free_slot;
|
||||
}
|
||||
|
||||
static void closefrom_note_target(posix_spawn_file_actions_t *actions, int fd) {
|
||||
pthread_mutex_lock(&g_closefrom_lock);
|
||||
struct closefrom_entry *e = closefrom_entry(actions, 1);
|
||||
if (e) {
|
||||
if (e->from < 0) {
|
||||
if (fd > e->highest) e->highest = fd;
|
||||
} else if (e->nkeep < CLOSEFROM_KEEP) {
|
||||
e->keep[e->nkeep++] = fd;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&g_closefrom_lock);
|
||||
}
|
||||
|
||||
int posix_spawn_file_actions_addclosefrom_np(posix_spawn_file_actions_t *actions, int from) {
|
||||
if (!actions || from < 0) return EINVAL;
|
||||
pthread_mutex_lock(&g_closefrom_lock);
|
||||
struct closefrom_entry *e = closefrom_entry(actions, 1);
|
||||
if (e && (e->from < 0 || from < e->from)) e->from = from;
|
||||
pthread_mutex_unlock(&g_closefrom_lock);
|
||||
return e ? 0 : ENOMEM;
|
||||
}
|
||||
|
||||
int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *actions, int fd, const char *path, int oflag, mode_t mode) {
|
||||
static int (*orig)(posix_spawn_file_actions_t *, int, const char *, int, mode_t) = NULL;
|
||||
if (!orig) orig = (int (*)(posix_spawn_file_actions_t *, int, const char *, int, mode_t))dlsym(RTLD_NEXT, "posix_spawn_file_actions_addopen");
|
||||
int ret = orig ? orig(actions, fd, path, oflag, mode) : ENOSYS;
|
||||
if (ret == 0) closefrom_note_target(actions, fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *actions, int fd, int newfd) {
|
||||
static int (*orig)(posix_spawn_file_actions_t *, int, int) = NULL;
|
||||
if (!orig) orig = (int (*)(posix_spawn_file_actions_t *, int, int))dlsym(RTLD_NEXT, "posix_spawn_file_actions_adddup2");
|
||||
int ret = orig ? orig(actions, fd, newfd) : ENOSYS;
|
||||
if (ret == 0) {
|
||||
pthread_mutex_lock(&g_closefrom_lock);
|
||||
struct closefrom_entry *e = closefrom_entry(actions, 0);
|
||||
if (e && e->from >= 0 && fd >= e->from) {
|
||||
int reopened = 0;
|
||||
for (int k = 0; k < e->nkeep; k++) reopened |= e->keep[k] == fd;
|
||||
if (!reopened) e->bad_source = 1;
|
||||
}
|
||||
pthread_mutex_unlock(&g_closefrom_lock);
|
||||
closefrom_note_target(actions, newfd);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int posix_spawn_file_actions_init(posix_spawn_file_actions_t *actions) {
|
||||
static int (*orig)(posix_spawn_file_actions_t *) = NULL;
|
||||
if (!orig) orig = (int (*)(posix_spawn_file_actions_t *))dlsym(RTLD_NEXT, "posix_spawn_file_actions_init");
|
||||
pthread_mutex_lock(&g_closefrom_lock);
|
||||
struct closefrom_entry *e = closefrom_entry(actions, 0);
|
||||
if (e) e->actions = NULL;
|
||||
pthread_mutex_unlock(&g_closefrom_lock);
|
||||
return orig ? orig(actions) : ENOSYS;
|
||||
}
|
||||
|
||||
int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *actions) {
|
||||
static int (*orig)(posix_spawn_file_actions_t *) = NULL;
|
||||
if (!orig) orig = (int (*)(posix_spawn_file_actions_t *))dlsym(RTLD_NEXT, "posix_spawn_file_actions_destroy");
|
||||
pthread_mutex_lock(&g_closefrom_lock);
|
||||
struct closefrom_entry *e = closefrom_entry(actions, 0);
|
||||
if (e) e->actions = NULL;
|
||||
pthread_mutex_unlock(&g_closefrom_lock);
|
||||
return orig ? orig(actions) : ENOSYS;
|
||||
}
|
||||
|
||||
static int apply_closefrom(posix_spawn_file_actions_t *actions) {
|
||||
pthread_mutex_lock(&g_closefrom_lock);
|
||||
struct closefrom_entry *e = closefrom_entry(actions, 0);
|
||||
if (!e || e->from < 0) {
|
||||
pthread_mutex_unlock(&g_closefrom_lock);
|
||||
return 0;
|
||||
}
|
||||
if (e->bad_source) {
|
||||
pthread_mutex_unlock(&g_closefrom_lock);
|
||||
return EBADF;
|
||||
}
|
||||
int highest = e->highest;
|
||||
DIR *dir = opendir("/proc/self/fd");
|
||||
if (dir) {
|
||||
int self = dirfd(dir);
|
||||
struct dirent *entry;
|
||||
while ((entry = readdir(dir)) != NULL) {
|
||||
if (entry->d_name[0] < '0' || entry->d_name[0] > '9') continue;
|
||||
int fd = atoi(entry->d_name);
|
||||
if (fd != self && fd > highest) highest = fd;
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
int start = e->from > e->closed_to + 1 ? e->from : e->closed_to + 1;
|
||||
for (int fd = start; fd <= highest; fd++) {
|
||||
int kept = 0;
|
||||
for (int k = 0; k < e->nkeep; k++) kept |= e->keep[k] == fd;
|
||||
if (!kept && posix_spawn_file_actions_addclose(actions, fd) != 0) break;
|
||||
e->closed_to = fd;
|
||||
}
|
||||
pthread_mutex_unlock(&g_closefrom_lock);
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
static int apply_closefrom(posix_spawn_file_actions_t *actions) { (void)actions; return 0; }
|
||||
#endif
|
||||
@@ -0,0 +1,815 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
||||
// parts share their static functions and are built with the same command as before).
|
||||
//
|
||||
// Calls an app may not make (chown, mknod, capabilities, prctl, landlock, seccomp, namespaces,
|
||||
// close_range) and the generic syscall() entry point.
|
||||
|
||||
// The ARM translator of x86 devices and emulators (ndk_translation, houdini) keeps memory files of
|
||||
// its own open in the process (ndk_translation at descriptor 3) and needs them whenever it
|
||||
// translates more code: a program that closes every descriptor it did not open (sshd at start,
|
||||
// closefrom(3)) made it abort later ("CHECK failed" in mmap_posix.cc). Its memory files open when
|
||||
// the hook starts survive close(), close_range() and closefrom(); phones that run ARM code
|
||||
// themselves have none.
|
||||
#define DDNATV_MAX_KEPT_FDS 4
|
||||
static int g_kept_fds[DDNATV_MAX_KEPT_FDS]; // ascending
|
||||
static int g_kept_fd_count = 0;
|
||||
|
||||
static int is_kept_fd(int fd) {
|
||||
for (int i = 0; i < g_kept_fd_count; i++) {
|
||||
if (g_kept_fds[i] == fd) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Calls fn for each part of [first, last] that leaves out the descriptors in kept (ascending);
|
||||
// the first nonzero answer of fn is returned, else 0.
|
||||
static int for_each_range_without(unsigned int first, unsigned int last, const int *kept, int nkept,
|
||||
int (*fn)(unsigned int, unsigned int, void *), void *arg) {
|
||||
unsigned int from = first;
|
||||
for (int i = 0; i < nkept && from <= last; i++) {
|
||||
if (kept[i] < 0 || (unsigned int)kept[i] < from || (unsigned int)kept[i] > last) continue;
|
||||
unsigned int k = (unsigned int)kept[i];
|
||||
if (k > from) {
|
||||
int r = fn(from, k - 1, arg);
|
||||
if (r != 0) return r;
|
||||
}
|
||||
if (k == last) return 0;
|
||||
from = k + 1;
|
||||
}
|
||||
return from <= last ? fn(from, last, arg) : 0;
|
||||
}
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
|
||||
// ndk_translation's file is "/memfd:child"; any memory file counts in a process whose executable is
|
||||
// a translator's runner (houdini). ndk_translation shows a program its own path as /proc/self/exe,
|
||||
// so the name is what tells there.
|
||||
//
|
||||
// This runs at the start of every program, so it costs three system calls on a phone that runs ARM
|
||||
// code itself: the descriptors that are open (/proc/self/fd, read at once) are all that are asked
|
||||
// about, where asking about each of the numbers from 3 to 63 was 61 readlink() calls in every
|
||||
// process that was started.
|
||||
// Set once this process knows that it has none (it looked, or was told by DDNATV_NT=0 in its
|
||||
// environment, which prepare_ddnatv_env hands on to the programs it starts)
|
||||
static int g_no_translator = 0;
|
||||
|
||||
__attribute__((constructor(102))) static void find_translator_files(void) {
|
||||
// The first program of a session looks; the ones it starts are told (a translator is there for
|
||||
// all the programs of a machine or for none, and where it is not known they look as before)
|
||||
const char *known = getenv("DDNATV_NT");
|
||||
if (known && known[0] == '0' && known[1] == '\0') {
|
||||
g_no_translator = 1;
|
||||
return;
|
||||
}
|
||||
ssize_t (*real_readlink)(const char *, char *, size_t) = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink");
|
||||
int (*real_open)(const char *, int, ...) = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open");
|
||||
int (*real_close)(int) = (int (*)(int))dlsym(RTLD_NEXT, "close");
|
||||
ssize_t (*real_getdents64)(int, void *, size_t) = (ssize_t (*)(int, void *, size_t))dlsym(RTLD_NEXT, "getdents64");
|
||||
if (!real_readlink || !real_open || !real_close) return;
|
||||
int dir = real_getdents64 ? real_open("/proc/self/fd", O_RDONLY | O_DIRECTORY | O_CLOEXEC) : -1;
|
||||
if (dir < 0) return;
|
||||
// The numbers of the descriptors open now, those from 3 to 63 (the directory's own left out)
|
||||
char entries[2048];
|
||||
unsigned char open_fds[64];
|
||||
int nopen = 0;
|
||||
for (;;) {
|
||||
long n = real_getdents64(dir, entries, sizeof(entries));
|
||||
if (n <= 0) break;
|
||||
for (long off = 0; off < n;) {
|
||||
// struct linux_dirent64: d_ino (8), d_off (8), d_reclen (2), d_type (1), then d_name
|
||||
unsigned short reclen;
|
||||
memcpy(&reclen, entries + off + 16, sizeof(reclen));
|
||||
const char *name = entries + off + 19;
|
||||
off += reclen ? reclen : n; // a record without a length ends the buffer
|
||||
if (name[0] < '0' || name[0] > '9') continue; // "." and ".."
|
||||
int fd = atoi(name);
|
||||
if (fd >= 3 && fd < 64 && fd != dir) open_fds[nopen++] = (unsigned char)fd;
|
||||
}
|
||||
// A short answer is the whole directory; asking again would only be told "no more"
|
||||
if ((size_t)n + 64 <= sizeof(entries)) break;
|
||||
}
|
||||
real_close(dir);
|
||||
if (nopen == 0) {
|
||||
g_no_translator = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
char path[PATH_MAX];
|
||||
int runner = 0;
|
||||
ssize_t n = real_readlink("/proc/self/exe", path, sizeof(path) - 1);
|
||||
if (n > 0) {
|
||||
path[n] = '\0';
|
||||
const char *base = strrchr(path, '/');
|
||||
base = base ? base + 1 : path;
|
||||
runner = strncmp(base, "ndk_translation", 15) == 0 || strncmp(base, "houdini", 7) == 0;
|
||||
}
|
||||
// In ascending order, as the list is kept
|
||||
for (int i = 0; i < nopen - 1; i++) {
|
||||
for (int j = i + 1; j < nopen; j++) {
|
||||
if (open_fds[j] < open_fds[i]) {
|
||||
unsigned char t = open_fds[i];
|
||||
open_fds[i] = open_fds[j];
|
||||
open_fds[j] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < nopen && g_kept_fd_count < DDNATV_MAX_KEPT_FDS; i++) {
|
||||
char link[32], target[64];
|
||||
snprintf(link, sizeof(link), "/proc/self/fd/%d", open_fds[i]);
|
||||
ssize_t m = real_readlink(link, target, sizeof(target) - 1);
|
||||
if (m <= 0) continue;
|
||||
target[m] = '\0';
|
||||
if (strncmp(target, "/memfd:child", 12) == 0 || (runner && strncmp(target, "/memfd:", 7) == 0)) {
|
||||
g_kept_fds[g_kept_fd_count++] = open_fds[i];
|
||||
}
|
||||
}
|
||||
if (g_kept_fd_count == 0) g_no_translator = 1;
|
||||
}
|
||||
|
||||
int chown(const char *pathname, uid_t owner, gid_t group) { (void)pathname; (void)owner; (void)group; return 0; }
|
||||
int fchown(int fd, uid_t owner, gid_t group) { (void)fd; (void)owner; (void)group; return 0; }
|
||||
int lchown(const char *pathname, uid_t owner, gid_t group) { (void)pathname; (void)owner; (void)group; return 0; }
|
||||
int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags) {
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
(void)dirfd; (void)pathname; (void)owner; (void)group; (void)flags; return 0;
|
||||
}
|
||||
int chroot(const char *path) { (void)path; return 0; }
|
||||
|
||||
// Hook mkfifo / mkfifoat / mknod / mknodat
|
||||
int mkfifo(const char *pathname, mode_t mode);
|
||||
int mkfifoat(int dirfd, const char *pathname, mode_t mode);
|
||||
|
||||
int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev) {
|
||||
DDNATV_AT_REMAP(dirfd, pathname);
|
||||
(void)dev;
|
||||
if (!pathname) {
|
||||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
char buf[PATH_MAX];
|
||||
const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
|
||||
// A file type of 0 means a regular file, as in mknod(2), which also fails with EEXIST instead
|
||||
// of touching an existing file.
|
||||
if (S_ISREG(mode) || (mode & S_IFMT) == 0) {
|
||||
// mknod(2) returns 0 on success, never a file descriptor: close the
|
||||
// transient fd instead of leaking one per call (tar/dpkg extract
|
||||
// thousands of files; leaked fds exhaust the table -> EBADF noise).
|
||||
int fd = openat(dirfd, target, O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, mode & 07777);
|
||||
if (fd >= 0) {
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
if (S_ISFIFO(mode)) {
|
||||
return mkfifoat(dirfd, target, mode);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mknod(const char *pathname, mode_t mode, dev_t dev) {
|
||||
return mknodat(AT_FDCWD, pathname, mode, dev);
|
||||
}
|
||||
|
||||
int mkfifoat(int dirfd, const char *pathname, mode_t mode) {
|
||||
static int (*orig_mkfifoat)(int, const char *, mode_t) = NULL;
|
||||
if (!orig_mkfifoat) orig_mkfifoat = (int (*)(int, const char *, mode_t))dlsym(RTLD_NEXT, "mkfifoat");
|
||||
if (!pathname) {
|
||||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
char buf[PATH_MAX];
|
||||
const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
|
||||
if (orig_mkfifoat) {
|
||||
return orig_mkfifoat(dirfd, target, mode);
|
||||
}
|
||||
static int (*orig_mkfifo)(const char *, mode_t) = NULL;
|
||||
if (!orig_mkfifo) orig_mkfifo = (int (*)(const char *, mode_t))dlsym(RTLD_NEXT, "mkfifo");
|
||||
if (orig_mkfifo && dirfd == AT_FDCWD) {
|
||||
return orig_mkfifo(target, mode);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int mkfifo(const char *pathname, mode_t mode) {
|
||||
return mkfifoat(AT_FDCWD, pathname, mode);
|
||||
}
|
||||
|
||||
// Hook sync / syncfs to prevent Android flash storage stalls during dpkg operations
|
||||
void sync(void) {
|
||||
// No-op
|
||||
}
|
||||
|
||||
int syncfs(int fd) {
|
||||
(void)fd;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int capget(void *hdrp, void *datap) {
|
||||
static int (*orig_capget)(void *, void *) = NULL;
|
||||
if (!orig_capget) orig_capget = (int (*)(void *, void *))dlsym(RTLD_NEXT, "capget");
|
||||
if (orig_capget && orig_capget(hdrp, datap) == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (datap) {
|
||||
uint32_t ver = hdrp ? *(const uint32_t *)hdrp : 0;
|
||||
size_t nbytes = (ver == 0x20071026U || ver == 0x20080522U) ? 24 : 12;
|
||||
memset(datap, 0, nbytes);
|
||||
}
|
||||
errno = 0;
|
||||
return 0;
|
||||
}
|
||||
int capset(void *hdrp, const void *datap) { (void)hdrp; (void)datap; return 0; }
|
||||
|
||||
int prctl(int option, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, option);
|
||||
unsigned long arg2 = va_arg(ap, unsigned long);
|
||||
unsigned long arg3 = va_arg(ap, unsigned long);
|
||||
unsigned long arg4 = va_arg(ap, unsigned long);
|
||||
unsigned long arg5 = va_arg(ap, unsigned long);
|
||||
va_end(ap);
|
||||
|
||||
// PR_SET_NO_NEW_PRIVS = 38, PR_GET_NO_NEW_PRIVS = 39
|
||||
#ifdef PR_SET_NO_NEW_PRIVS
|
||||
if (option == PR_SET_NO_NEW_PRIVS) return 0;
|
||||
#endif
|
||||
if (option == 38) return 0;
|
||||
#ifdef PR_GET_NO_NEW_PRIVS
|
||||
if (option == PR_GET_NO_NEW_PRIVS) return 1;
|
||||
#endif
|
||||
if (option == 39) return 1;
|
||||
|
||||
// PR_SET_SECCOMP = 22, PR_GET_SECCOMP = 21
|
||||
#ifdef PR_SET_SECCOMP
|
||||
if (option == PR_SET_SECCOMP) { errno = EINVAL; return -1; }
|
||||
#endif
|
||||
if (option == 22) { errno = EINVAL; return -1; }
|
||||
#ifdef PR_GET_SECCOMP
|
||||
if (option == PR_GET_SECCOMP) return 0;
|
||||
#endif
|
||||
if (option == 21) return 0;
|
||||
|
||||
// PR_SET_SPECULATION_CTRL = 47
|
||||
if (option == 47) return 0;
|
||||
|
||||
static int (*orig_prctl)(int, unsigned long, unsigned long, unsigned long, unsigned long) = NULL;
|
||||
if (!orig_prctl) orig_prctl = (int (*)(int, unsigned long, unsigned long, unsigned long, unsigned long))dlsym(RTLD_NEXT, "prctl");
|
||||
return orig_prctl ? orig_prctl(option, arg2, arg3, arg4, arg5) : 0;
|
||||
}
|
||||
|
||||
// Limits that leave a process no new descriptors (RLIMIT_NOFILE below 16) or no file writes
|
||||
// (RLIMIT_FSIZE 0) are not applied. Sandboxes give them to their unprivileged part (sshd's before
|
||||
// login sets 1 and 0, with the seccomp filter above), and Android's ARM translator (emulators, x86
|
||||
// devices) needs new descriptors and memory files in the process: sshd's died in it ("CHECK failed"
|
||||
// in its mmap) and the client's connection was reset. Other limits are set as asked.
|
||||
static int sandbox_only_limit(int resource, const struct rlimit *rlim) {
|
||||
if (!rlim) return 0;
|
||||
if (resource == RLIMIT_NOFILE) return rlim->rlim_cur < 16;
|
||||
if (resource == RLIMIT_FSIZE) return rlim->rlim_cur == 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlim) {
|
||||
if (sandbox_only_limit((int)resource, rlim)) return 0;
|
||||
static int (*orig_setrlimit)(__rlimit_resource_t, const struct rlimit *) = NULL;
|
||||
#if defined(__USE_FILE_OFFSET64) && !defined(__LP64__)
|
||||
// With 64-bit file offsets on a 32-bit system this function is setrlimit64
|
||||
if (!orig_setrlimit) orig_setrlimit = (int (*)(__rlimit_resource_t, const struct rlimit *))dlsym(RTLD_NEXT, "setrlimit64");
|
||||
#else
|
||||
if (!orig_setrlimit) orig_setrlimit = (int (*)(__rlimit_resource_t, const struct rlimit *))dlsym(RTLD_NEXT, "setrlimit");
|
||||
#endif
|
||||
if (!orig_setrlimit) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
return orig_setrlimit(resource, rlim);
|
||||
}
|
||||
#if defined(__LP64__)
|
||||
__asm__(".globl setrlimit64\n" ".set setrlimit64, setrlimit\n");
|
||||
#endif
|
||||
|
||||
#ifndef LANDLOCK_CREATE_RULESET_VERSION
|
||||
#define LANDLOCK_CREATE_RULESET_VERSION (1U << 0)
|
||||
#endif
|
||||
|
||||
#ifndef __NR_landlock_create_ruleset
|
||||
#define __NR_landlock_create_ruleset 444
|
||||
#endif
|
||||
#ifndef __NR_landlock_add_rule
|
||||
#define __NR_landlock_add_rule 445
|
||||
#endif
|
||||
#ifndef __NR_landlock_restrict_self
|
||||
#define __NR_landlock_restrict_self 446
|
||||
#endif
|
||||
|
||||
#ifndef __NR_seccomp
|
||||
#if defined(__aarch64__)
|
||||
#define __NR_seccomp 277
|
||||
#elif defined(__x86_64__)
|
||||
#define __NR_seccomp 317
|
||||
#elif defined(__arm__)
|
||||
#define __NR_seccomp 383
|
||||
#else
|
||||
#define __NR_seccomp 277
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef __NR_unshare
|
||||
#if defined(__aarch64__)
|
||||
#define __NR_unshare 97
|
||||
#elif defined(__x86_64__)
|
||||
#define __NR_unshare 272
|
||||
#elif defined(__arm__)
|
||||
#define __NR_unshare 337
|
||||
#else
|
||||
#define __NR_unshare 97
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef __NR_setns
|
||||
#if defined(__aarch64__)
|
||||
#define __NR_setns 268
|
||||
#elif defined(__x86_64__)
|
||||
#define __NR_setns 308
|
||||
#elif defined(__arm__)
|
||||
#define __NR_setns 375
|
||||
#else
|
||||
#define __NR_setns 268
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef __NR_clone3
|
||||
#define __NR_clone3 435
|
||||
#endif
|
||||
#ifndef __NR_pidfd_open
|
||||
#define __NR_pidfd_open 434
|
||||
#endif
|
||||
#ifndef __NR_pidfd_send_signal
|
||||
#define __NR_pidfd_send_signal 424
|
||||
#endif
|
||||
#ifndef __NR_pidfd_getfd
|
||||
#define __NR_pidfd_getfd 438
|
||||
#endif
|
||||
#ifndef __NR_close_range
|
||||
#define __NR_close_range 436
|
||||
#endif
|
||||
#ifndef __NR_faccessat
|
||||
#if defined(__aarch64__)
|
||||
#define __NR_faccessat 48
|
||||
#elif defined(__arm__)
|
||||
#define __NR_faccessat 334
|
||||
#elif defined(__x86_64__)
|
||||
#define __NR_faccessat 269
|
||||
#endif
|
||||
#endif
|
||||
#ifndef __NR_faccessat2
|
||||
#define __NR_faccessat2 439
|
||||
#endif
|
||||
#ifndef __NR_statx
|
||||
#if defined(__aarch64__) || defined(__arm__)
|
||||
#define __NR_statx 291
|
||||
#elif defined(__x86_64__)
|
||||
#define __NR_statx 332
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef SECCOMP_SET_MODE_STRICT
|
||||
#define SECCOMP_SET_MODE_STRICT 0
|
||||
#endif
|
||||
#ifndef SECCOMP_SET_MODE_FILTER
|
||||
#define SECCOMP_SET_MODE_FILTER 1
|
||||
#endif
|
||||
#ifndef SECCOMP_GET_ACTION_AVAIL
|
||||
#define SECCOMP_GET_ACTION_AVAIL 2
|
||||
#endif
|
||||
|
||||
int landlock_create_ruleset(const void *attr, size_t size, uint32_t flags) {
|
||||
(void)attr;
|
||||
(void)size;
|
||||
(void)flags;
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int landlock_add_rule(int ruleset_fd, int rule_type, const void *rule_attr, uint32_t flags) {
|
||||
(void)ruleset_fd;
|
||||
(void)rule_type;
|
||||
(void)rule_attr;
|
||||
(void)flags;
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int landlock_restrict_self(int ruleset_fd, uint32_t flags) {
|
||||
(void)ruleset_fd;
|
||||
(void)flags;
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int seccomp(unsigned int operation, unsigned int flags, void *args) {
|
||||
(void)flags;
|
||||
(void)args;
|
||||
if (operation != SECCOMP_SET_MODE_STRICT &&
|
||||
operation != SECCOMP_SET_MODE_FILTER &&
|
||||
operation != SECCOMP_GET_ACTION_AVAIL &&
|
||||
operation != 3 /* SECCOMP_GET_NOTIF_SIZES */) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int unshare(int flags) {
|
||||
(void)flags;
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int setns(int fd, int nstype) {
|
||||
(void)fd;
|
||||
(void)nstype;
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifndef CLOSE_RANGE_CLOEXEC
|
||||
#define CLOSE_RANGE_CLOEXEC (1U << 2)
|
||||
#endif
|
||||
|
||||
static int close_range_all(unsigned int first, unsigned int last, int flags);
|
||||
|
||||
static int close_range_part(unsigned int first, unsigned int last, void *flags) {
|
||||
return close_range_all(first, last, *(int *)flags);
|
||||
}
|
||||
|
||||
int close_range(unsigned int first, unsigned int last, int flags) {
|
||||
// Close-on-exec only (CLOSE_RANGE_CLOEXEC) leaves the translator's files open
|
||||
if (g_kept_fd_count > 0 && !(flags & CLOSE_RANGE_CLOEXEC)) {
|
||||
return for_each_range_without(first, last, g_kept_fds, g_kept_fd_count, close_range_part, &flags);
|
||||
}
|
||||
return close_range_all(first, last, flags);
|
||||
}
|
||||
|
||||
// glibc's closefrom() makes the close_range system call itself, past the function above
|
||||
void closefrom(int lowfd) {
|
||||
close_range(lowfd < 0 ? 0U : (unsigned int)lowfd, ~0U, 0);
|
||||
}
|
||||
|
||||
static int close_range_all(unsigned int first, unsigned int last, int flags) {
|
||||
static int (*orig_close_range)(unsigned int, unsigned int, int) = NULL;
|
||||
if (!orig_close_range) {
|
||||
orig_close_range = (int (*)(unsigned int, unsigned int, int))dlsym(RTLD_NEXT, "close_range");
|
||||
}
|
||||
#ifdef __NR_close_range
|
||||
static long (*orig_raw_syscall)(long, ...) = NULL;
|
||||
if (!orig_raw_syscall) orig_raw_syscall = (long (*)(long, ...))dlsym(RTLD_NEXT, "syscall");
|
||||
if (orig_raw_syscall) {
|
||||
long sret = orig_raw_syscall(__NR_close_range, first, last, flags);
|
||||
if (sret == 0) return 0;
|
||||
if (sret < 0 && errno != ENOSYS) return -1;
|
||||
}
|
||||
#endif
|
||||
if (orig_close_range) {
|
||||
int cret = orig_close_range(first, last, flags);
|
||||
if (cret == 0) return 0;
|
||||
if (cret < 0 && errno != ENOSYS) return -1;
|
||||
}
|
||||
|
||||
int set_cloexec = (flags & CLOSE_RANGE_CLOEXEC) != 0;
|
||||
|
||||
DIR *d = opendir("/proc/self/fd");
|
||||
if (d) {
|
||||
int dfd = dirfd(d);
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
int fd = atoi(de->d_name);
|
||||
if (fd >= (int)first && (last == ~0U || (unsigned int)fd <= last) && fd != dfd) {
|
||||
if (set_cloexec) {
|
||||
int fl = fcntl(fd, F_GETFD);
|
||||
if (fl >= 0) {
|
||||
fcntl(fd, F_SETFD, fl | FD_CLOEXEC);
|
||||
}
|
||||
} else {
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
return 0;
|
||||
}
|
||||
int max_fd = (int)sysconf(_SC_OPEN_MAX);
|
||||
if (max_fd <= 0 || max_fd > 1024) max_fd = 1024;
|
||||
int end = (last < (unsigned int)max_fd) ? (int)last : max_fd;
|
||||
for (int fd = (int)first; fd <= end; fd++) {
|
||||
if (set_cloexec) {
|
||||
int fl = fcntl(fd, F_GETFD);
|
||||
if (fl >= 0) {
|
||||
fcntl(fd, F_SETFD, fl | FD_CLOEXEC);
|
||||
}
|
||||
} else {
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static __thread int t_syscall_redirect = 0;
|
||||
|
||||
long syscall(long number, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, number);
|
||||
unsigned long arg1 = va_arg(ap, unsigned long);
|
||||
unsigned long arg2 = va_arg(ap, unsigned long);
|
||||
unsigned long arg3 = va_arg(ap, unsigned long);
|
||||
unsigned long arg4 = va_arg(ap, unsigned long);
|
||||
unsigned long arg5 = va_arg(ap, unsigned long);
|
||||
unsigned long arg6 = va_arg(ap, unsigned long);
|
||||
va_end(ap);
|
||||
|
||||
#if defined(__NR_readlinkat)
|
||||
if (number == __NR_readlinkat) {
|
||||
return (long)readlinkat((int)arg1, (const char *)arg2, (char *)arg3, (size_t)arg4);
|
||||
}
|
||||
#endif
|
||||
#if defined(__NR_readlink)
|
||||
if (number == __NR_readlink) {
|
||||
return (long)readlink((const char *)arg1, (char *)arg2, (size_t)arg3);
|
||||
}
|
||||
#endif
|
||||
#if defined(__NR_linkat)
|
||||
if (number == __NR_linkat) {
|
||||
return (long)linkat((int)arg1, (const char *)arg2, (int)arg3, (const char *)arg4, (int)arg5);
|
||||
}
|
||||
#endif
|
||||
#if defined(__NR_link)
|
||||
if (number == __NR_link) {
|
||||
return (long)link((const char *)arg1, (const char *)arg2);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __NR_landlock_create_ruleset
|
||||
if (number == __NR_landlock_create_ruleset) {
|
||||
return (long)landlock_create_ruleset((const void *)arg1, (size_t)arg2, (uint32_t)arg3);
|
||||
}
|
||||
if (number == __NR_landlock_add_rule) {
|
||||
return (long)landlock_add_rule((int)arg1, (int)arg2, (const void *)arg3, (uint32_t)arg4);
|
||||
}
|
||||
if (number == __NR_landlock_restrict_self) {
|
||||
return (long)landlock_restrict_self((int)arg1, (uint32_t)arg2);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __NR_seccomp
|
||||
if (number == __NR_seccomp) {
|
||||
return (long)seccomp((unsigned int)arg1, (unsigned int)arg2, (void *)arg3);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __NR_unshare
|
||||
if (number == __NR_unshare) {
|
||||
return (long)unshare((int)arg1);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __NR_setns
|
||||
if (number == __NR_setns) {
|
||||
return (long)setns((int)arg1, (int)arg2);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __NR_clone3
|
||||
if (number == __NR_clone3) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __NR_pidfd_open
|
||||
if (number == __NR_pidfd_open) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
#ifdef __NR_pidfd_send_signal
|
||||
if (number == __NR_pidfd_send_signal) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
#ifdef __NR_pidfd_getfd
|
||||
if (number == __NR_pidfd_getfd) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __NR_close_range
|
||||
if (number == __NR_close_range) {
|
||||
// libblsession's close_range() calls syscall() again: the nested call goes straight to the kernel
|
||||
if (t_syscall_redirect) return call_orig_syscall(number, arg1, arg2, arg3, 0, 0, 0);
|
||||
t_syscall_redirect++;
|
||||
long ret = (long)close_range((unsigned int)arg1, (unsigned int)arg2, (int)arg3);
|
||||
t_syscall_redirect--;
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __NR_openat2
|
||||
if (number == __NR_openat2) {
|
||||
// libblsession's openat2() calls syscall() again (an endless loop until the stack ran out): the nested
|
||||
// call maps the path and goes straight to the kernel
|
||||
if (t_syscall_redirect) {
|
||||
char nested_buf[PATH_MAX];
|
||||
const char *path = (const char *)arg2;
|
||||
if (path && path[0] == '/') path = rewrite_path(path, nested_buf, sizeof(nested_buf));
|
||||
return call_orig_syscall(number, arg1, (unsigned long)path, arg3, arg4, 0, 0);
|
||||
}
|
||||
t_syscall_redirect++;
|
||||
long ret = (long)openat2((int)arg1, (const char *)arg2, (struct ddnatv_open_how *)arg3, (size_t)arg4);
|
||||
t_syscall_redirect--;
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __NR_fchmodat2
|
||||
if (number == __NR_fchmodat2) {
|
||||
return (long)fchmodat2((int)arg1, (const char *)arg2, (mode_t)arg3, (int)arg4);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__NR_statx)
|
||||
if (number == __NR_statx) {
|
||||
return (long)statx((int)arg1, (const char *)arg2, (int)arg3, (unsigned int)arg4, (struct statx *)arg5);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__NR_faccessat)
|
||||
if (number == __NR_faccessat) {
|
||||
if (t_syscall_redirect) {
|
||||
char nested_buf[PATH_MAX];
|
||||
const char *path = (const char *)arg2;
|
||||
if (path && path[0] == '/') path = rewrite_path(path, nested_buf, sizeof(nested_buf));
|
||||
return call_orig_syscall(number, arg1, (unsigned long)path, arg3, 0, 0, 0);
|
||||
}
|
||||
t_syscall_redirect++;
|
||||
long ret = (long)faccessat((int)arg1, (const char *)arg2, (int)arg3, 0);
|
||||
t_syscall_redirect--;
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__NR_faccessat2)
|
||||
if (number == __NR_faccessat2) {
|
||||
return (long)faccessat2((int)arg1, (const char *)arg2, (int)arg3, (int)arg4);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__NR_mknodat)
|
||||
if (number == __NR_mknodat) {
|
||||
return (long)mknodat((int)arg1, (const char *)arg2, (mode_t)arg3, (dev_t)arg4);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__NR_mknod)
|
||||
if (number == __NR_mknod) {
|
||||
return (long)mknod((const char *)arg1, (mode_t)arg2, (dev_t)arg3);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__NR_mkfifoat)
|
||||
if (number == __NR_mkfifoat) {
|
||||
return (long)mkfifoat((int)arg1, (const char *)arg2, (mode_t)arg3);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__NR_setresuid)
|
||||
if (number == __NR_setresuid) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setresuid32)
|
||||
if (number == __NR_setresuid32) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setresgid)
|
||||
if (number == __NR_setresgid) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setresgid32)
|
||||
if (number == __NR_setresgid32) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setuid)
|
||||
if (number == __NR_setuid) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setuid32)
|
||||
if (number == __NR_setuid32) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setgid)
|
||||
if (number == __NR_setgid) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setgid32)
|
||||
if (number == __NR_setgid32) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setreuid)
|
||||
if (number == __NR_setreuid) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setreuid32)
|
||||
if (number == __NR_setreuid32) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setregid)
|
||||
if (number == __NR_setregid) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setregid32)
|
||||
if (number == __NR_setregid32) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setgroups)
|
||||
if (number == __NR_setgroups) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setgroups32)
|
||||
if (number == __NR_setgroups32) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setfsuid)
|
||||
if (number == __NR_setfsuid) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setfsuid32)
|
||||
if (number == __NR_setfsuid32) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setfsgid)
|
||||
if (number == __NR_setfsgid) return 0;
|
||||
#endif
|
||||
#if defined(__NR_setfsgid32)
|
||||
if (number == __NR_setfsgid32) return 0;
|
||||
#endif
|
||||
#if defined(__NR_getuid)
|
||||
if (number == __NR_getuid) return 0;
|
||||
#endif
|
||||
#if defined(__NR_getuid32)
|
||||
if (number == __NR_getuid32) return 0;
|
||||
#endif
|
||||
#if defined(__NR_geteuid)
|
||||
if (number == __NR_geteuid) return 0;
|
||||
#endif
|
||||
#if defined(__NR_geteuid32)
|
||||
if (number == __NR_geteuid32) return 0;
|
||||
#endif
|
||||
#if defined(__NR_getgid)
|
||||
if (number == __NR_getgid) return 0;
|
||||
#endif
|
||||
#if defined(__NR_getgid32)
|
||||
if (number == __NR_getgid32) return 0;
|
||||
#endif
|
||||
#if defined(__NR_getegid)
|
||||
if (number == __NR_getegid) return 0;
|
||||
#endif
|
||||
#if defined(__NR_getegid32)
|
||||
if (number == __NR_getegid32) return 0;
|
||||
#endif
|
||||
#if defined(__NR_getresuid)
|
||||
if (number == __NR_getresuid) {
|
||||
uid_t *ruid = (uid_t *)arg1;
|
||||
uid_t *euid = (uid_t *)arg2;
|
||||
uid_t *suid = (uid_t *)arg3;
|
||||
if (ruid) *ruid = 0;
|
||||
if (euid) *euid = 0;
|
||||
if (suid) *suid = 0;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#if defined(__NR_getresuid32)
|
||||
if (number == __NR_getresuid32) {
|
||||
uid_t *ruid = (uid_t *)arg1;
|
||||
uid_t *euid = (uid_t *)arg2;
|
||||
uid_t *suid = (uid_t *)arg3;
|
||||
if (ruid) *ruid = 0;
|
||||
if (euid) *euid = 0;
|
||||
if (suid) *suid = 0;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#if defined(__NR_getresgid)
|
||||
if (number == __NR_getresgid) {
|
||||
gid_t *rgid = (gid_t *)arg1;
|
||||
gid_t *egid = (gid_t *)arg2;
|
||||
gid_t *sgid = (gid_t *)arg3;
|
||||
if (rgid) *rgid = 0;
|
||||
if (egid) *egid = 0;
|
||||
if (sgid) *sgid = 0;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#if defined(__NR_getresgid32)
|
||||
if (number == __NR_getresgid32) {
|
||||
gid_t *rgid = (gid_t *)arg1;
|
||||
gid_t *egid = (gid_t *)arg2;
|
||||
gid_t *sgid = (gid_t *)arg3;
|
||||
if (rgid) *rgid = 0;
|
||||
if (egid) *egid = 0;
|
||||
if (sgid) *sgid = 0;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
return call_orig_syscall(number, arg1, arg2, arg3, arg4, arg5, arg6);
|
||||
}
|
||||
|
||||
#endif /* !DDNATV_HOST_TEST */
|
||||
@@ -0,0 +1,384 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
||||
// parts share their static functions and are built with the same command as before).
|
||||
//
|
||||
// Start-up (DDNATV_ROOT, certificate and time zone defaults), /proc/self/exe, rewrite_path() and
|
||||
// the short names of Unix sockets.
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
|
||||
static char g_real_exe[PATH_MAX] = {0};
|
||||
// DDNATV_PRELOAD: guest paths of libraries every program loads before the hook (what
|
||||
// /etc/ld.so.preload does for programs run by the rootfs's own ld.so), and the same as host paths.
|
||||
static char *g_preload_guest = NULL;
|
||||
static char *g_preload_host = NULL;
|
||||
static int g_initialized = 0;
|
||||
|
||||
static void init_ddnatv_hook(void) {
|
||||
if (g_initialized) return;
|
||||
const char *root = getenv("DDNATV_ROOT");
|
||||
if (root && strlen(root) > 0) {
|
||||
size_t len = strlen(root);
|
||||
if (len > sizeof(g_ddnatv_root) - 1) len = sizeof(g_ddnatv_root) - 1;
|
||||
if (root[len - 1] == '/') len--;
|
||||
ddnatv_set_root(root, len);
|
||||
} else {
|
||||
Dl_info info;
|
||||
if (dladdr((void *)init_ddnatv_hook, &info) && info.dli_fname) {
|
||||
const char *p = strstr(info.dli_fname, "/usr/lib/libddnatv-hook");
|
||||
if (p && p > info.dli_fname) {
|
||||
size_t len = p - info.dli_fname;
|
||||
if (len < sizeof(g_ddnatv_root)) {
|
||||
ddnatv_set_root(info.dli_fname, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ddnatv_load_binds(getenv("DDNATV_BINDS"));
|
||||
const char *hostname = getenv("DDNATV_HOSTNAME");
|
||||
if (hostname && hostname[0] && strlen(hostname) < sizeof(g_hostname)) ddnatv_copy_cstr(g_hostname, sizeof(g_hostname), hostname);
|
||||
const char *link_root = getenv("DDNATV_LINK_ROOT");
|
||||
if (link_root && link_root[0] == '/' && strlen(link_root) < sizeof(g_link_root)) {
|
||||
ddnatv_copy_cstr(g_link_root, sizeof(g_link_root), link_root);
|
||||
g_link_root_len = strlen(g_link_root);
|
||||
while (g_link_root_len > 1 && g_link_root[g_link_root_len - 1] == '/') g_link_root[--g_link_root_len] = 0;
|
||||
}
|
||||
const char *preload = getenv("DDNATV_PRELOAD");
|
||||
if (preload && preload[0] && g_ddnatv_root_len > 0) {
|
||||
size_t libs = 1;
|
||||
for (const char *p = preload; *p; p++) libs += *p == ':';
|
||||
size_t cap = libs * (PATH_MAX + 1);
|
||||
char *host = malloc(cap);
|
||||
if (host) {
|
||||
size_t used = 0;
|
||||
host[0] = '\0';
|
||||
char *copy = strdup(preload);
|
||||
char *save = NULL;
|
||||
for (char *lib = copy ? strtok_r(copy, ":", &save) : NULL; lib; lib = strtok_r(NULL, ":", &save)) {
|
||||
if (lib[0] != '/') continue;
|
||||
char mapped_buf[PATH_MAX];
|
||||
const char *mapped = rewrite_path_in_root(lib, mapped_buf, sizeof(mapped_buf));
|
||||
size_t mapped_len = strlen(mapped);
|
||||
if (used + mapped_len + 2 > cap) break;
|
||||
if (used) host[used++] = ':';
|
||||
used = ddnatv_append(host, cap, used, mapped, mapped_len);
|
||||
}
|
||||
free(copy);
|
||||
g_preload_host = host;
|
||||
g_preload_guest = strdup(preload);
|
||||
}
|
||||
}
|
||||
const char *curr_real_exe = getenv("DDNATV_REAL_EXE");
|
||||
if (curr_real_exe && curr_real_exe[0] != '\0') {
|
||||
ddnatv_copy_cstr(g_real_exe, sizeof(g_real_exe), curr_real_exe);
|
||||
}
|
||||
if (g_ddnatv_root[0] != '\0') {
|
||||
const char *curr_tzdir = getenv("TZDIR");
|
||||
if (!curr_tzdir || curr_tzdir[0] == '\0') {
|
||||
char tzdir_buf[PATH_MAX];
|
||||
snprintf(tzdir_buf, sizeof(tzdir_buf), "%s/usr/share/zoneinfo", g_ddnatv_root);
|
||||
setenv("TZDIR", tzdir_buf, 0);
|
||||
}
|
||||
|
||||
const char *curr_cert = getenv("SSL_CERT_FILE");
|
||||
if (!curr_cert || curr_cert[0] == '\0' || strcmp(curr_cert, "/etc/ssl/certs/ca-certificates.crt") == 0) {
|
||||
char cert_buf[PATH_MAX];
|
||||
snprintf(cert_buf, sizeof(cert_buf), "%s/etc/ssl/certs/ca-certificates.crt", g_ddnatv_root);
|
||||
setenv("SSL_CERT_FILE", cert_buf, 1);
|
||||
}
|
||||
|
||||
const char *curr_cert_dir = getenv("SSL_CERT_DIR");
|
||||
if (!curr_cert_dir || curr_cert_dir[0] == '\0' || strcmp(curr_cert_dir, "/etc/ssl/certs") == 0) {
|
||||
char cert_dir_buf[PATH_MAX];
|
||||
snprintf(cert_dir_buf, sizeof(cert_dir_buf), "%s/etc/ssl/certs:/system/etc/security/cacerts", g_ddnatv_root);
|
||||
setenv("SSL_CERT_DIR", cert_dir_buf, 1);
|
||||
}
|
||||
|
||||
const char *curr_curl = getenv("CURL_CA_BUNDLE");
|
||||
if (!curr_curl || curr_curl[0] == '\0' || strcmp(curr_curl, "/etc/ssl/certs/ca-certificates.crt") == 0) {
|
||||
char curl_buf[PATH_MAX];
|
||||
snprintf(curl_buf, sizeof(curl_buf), "%s/etc/ssl/certs/ca-certificates.crt", g_ddnatv_root);
|
||||
setenv("CURL_CA_BUNDLE", curl_buf, 1);
|
||||
}
|
||||
|
||||
const char *curr_godebug = getenv("GODEBUG");
|
||||
if (!curr_godebug || curr_godebug[0] == '\0') {
|
||||
setenv("GODEBUG", "netdns=cgo", 0);
|
||||
} else if (strstr(curr_godebug, "netdns") == NULL) {
|
||||
char godebug_buf[512];
|
||||
snprintf(godebug_buf, sizeof(godebug_buf), "%s,netdns=cgo", curr_godebug);
|
||||
setenv("GODEBUG", godebug_buf, 1);
|
||||
}
|
||||
}
|
||||
g_initialized = 1;
|
||||
}
|
||||
|
||||
// Before the program runs: init_ddnatv_hook() sets variables, and setenv() may move the block the
|
||||
// environment is kept in. A program that had changed its environment (putenv, setenv) and passed it
|
||||
// to execv() before its first call into the hook handed the exec code a block that was freed by the
|
||||
// time it was read.
|
||||
__attribute__((constructor(103))) static void init_ddnatv_hook_early(void) {
|
||||
init_ddnatv_hook();
|
||||
// The kernel names the process after what it executed, the rootfs ld.so: process monitors, and
|
||||
// the app's own checks of /proc/<pid>/comm (steam, wineserver), saw only "ld-linux-aarch64.so.1"
|
||||
const char *name = program_invocation_short_name;
|
||||
if (g_ddnatv_root[0] != '\0' && name && name[0] && strncmp(name, "ld-linux", 8) != 0) {
|
||||
prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void tzset(void) {
|
||||
static void (*orig_tzset)(void) = NULL;
|
||||
if (!orig_tzset) orig_tzset = (void (*)(void))dlsym(RTLD_NEXT, "tzset");
|
||||
init_ddnatv_hook();
|
||||
if (g_ddnatv_root[0] != '\0') {
|
||||
const char *curr_tzdir = getenv("TZDIR");
|
||||
if (!curr_tzdir || curr_tzdir[0] == '\0') {
|
||||
char tzdir_buf[PATH_MAX];
|
||||
snprintf(tzdir_buf, sizeof(tzdir_buf), "%s/usr/share/zoneinfo", g_ddnatv_root);
|
||||
setenv("TZDIR", tzdir_buf, 0);
|
||||
}
|
||||
}
|
||||
if (orig_tzset) orig_tzset();
|
||||
}
|
||||
|
||||
static int is_proc_self_exe(const char *path) {
|
||||
if (!path) return 0;
|
||||
if (strcmp(path, "/proc/self/exe") == 0 ||
|
||||
strcmp(path, "/proc/thread-self/exe") == 0) {
|
||||
return 1;
|
||||
}
|
||||
if (strncmp(path, "/proc/", 6) == 0) {
|
||||
const char *p = path + 6;
|
||||
while (*p >= '0' && *p <= '9') p++;
|
||||
if (strcmp(p, "/exe") == 0) {
|
||||
pid_t pid = (pid_t)atoi(path + 6);
|
||||
if (pid == getpid()) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static ssize_t handle_proc_self_exe(char *buf, size_t bufsiz) {
|
||||
if (!buf || bufsiz == 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
init_ddnatv_hook();
|
||||
if (g_real_exe[0] == '\0') {
|
||||
int fd = open("/proc/self/cmdline", O_RDONLY);
|
||||
if (fd >= 0) {
|
||||
char cmdline[8192];
|
||||
ssize_t n = read(fd, cmdline, sizeof(cmdline) - 1);
|
||||
close(fd);
|
||||
if (n > 0) {
|
||||
cmdline[n] = '\0';
|
||||
char *args[64] = {0};
|
||||
int ac = 0;
|
||||
char *p = cmdline;
|
||||
while (p < cmdline + n && ac < 64) {
|
||||
args[ac++] = p;
|
||||
p += strlen(p) + 1;
|
||||
}
|
||||
for (int i = 0; i < ac; i++) {
|
||||
if (strcmp(args[i], "--argv0") == 0 && i + 2 < ac) {
|
||||
strncpy(g_real_exe, args[i + 2], sizeof(g_real_exe) - 1);
|
||||
g_real_exe[sizeof(g_real_exe) - 1] = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (g_real_exe[0] == '\0' && ac > 0 && args[0] && args[0][0] != '\0') {
|
||||
strncpy(g_real_exe, args[0], sizeof(g_real_exe) - 1);
|
||||
g_real_exe[sizeof(g_real_exe) - 1] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (g_real_exe[0] != '\0') {
|
||||
char stripped[PATH_MAX];
|
||||
const char *out_path = g_real_exe;
|
||||
if (g_ddnatv_root[0] != '\0') {
|
||||
size_t rlen = g_ddnatv_root_len;
|
||||
if (strncmp(g_real_exe, g_ddnatv_root, rlen) == 0 &&
|
||||
(g_real_exe[rlen] == '/' || g_real_exe[rlen] == '\0')) {
|
||||
out_path = g_real_exe + rlen;
|
||||
if (out_path[0] == '\0') out_path = "/";
|
||||
}
|
||||
}
|
||||
strncpy(stripped, out_path, sizeof(stripped) - 1);
|
||||
stripped[sizeof(stripped) - 1] = '\0';
|
||||
|
||||
size_t len = strlen(stripped);
|
||||
size_t copy_len = (len < bufsiz) ? len : bufsiz;
|
||||
memcpy(buf, stripped, copy_len);
|
||||
return (ssize_t)copy_len;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static const char *rewrite_path(const char *path, char *buffer, size_t bufsize) {
|
||||
if (!path) return NULL;
|
||||
// Relative paths must NEVER be rewritten to rootfs root
|
||||
if (path[0] != '/') return path;
|
||||
init_ddnatv_hook();
|
||||
return rewrite_path_in_root(path, buffer, bufsize);
|
||||
}
|
||||
|
||||
// rewrite_path() for calls that follow a symbolic link in the last component. In a mount whose
|
||||
// source is elsewhere the kernel would follow the link's absolute guest target on Android's own
|
||||
// tree (a Flatpak app's /app/bin/PINBALL.DAT -> /app/extra/Pinball/PINBALL.DAT), so the link is
|
||||
// followed in the guest's view first. The rootfs's own links were made relative already.
|
||||
static const char *rewrite_path_follow(const char *path, char *buffer, size_t bufsize) {
|
||||
if (!path) return NULL;
|
||||
if (path[0] != '/') return path;
|
||||
init_ddnatv_hook();
|
||||
int moved = 0;
|
||||
for (int i = 0; i < g_nbinds; i++) {
|
||||
const struct ddnatv_bind *b = &g_binds[i];
|
||||
if (path[1] == b->dst[1] && is_path_prefix(path, b->dst, b->dst_len)) { moved = !b->identity; break; }
|
||||
}
|
||||
char from_source[PATH_MAX];
|
||||
if (!moved) {
|
||||
// or a mount's real place, as /proc/self/exe and getcwd() give it (a Flatpak app's checkout)
|
||||
const struct ddnatv_bind *best = NULL;
|
||||
for (int i = 0; i < g_nbinds; i++) {
|
||||
const struct ddnatv_bind *b = &g_binds[i];
|
||||
if (b->identity || !is_path_prefix(path, b->src, b->src_len)) continue;
|
||||
if (!best || b->src_len > best->src_len) best = b;
|
||||
}
|
||||
if (!best) {
|
||||
const char *host = rewrite_path_in_root(path, buffer, bufsize);
|
||||
#if defined(__aarch64__)
|
||||
// A link in the rootfs with an absolute guest target that the hook did not write (one made
|
||||
// under proot, or copied from Proton's template prefix, as Wine's "z:" -> "/") is followed
|
||||
// by the kernel on Android's own tree: Wine found no drive for any path and gave games
|
||||
// \\?\unix\ names, which an NW.js game could not load its DLLs from.
|
||||
if (is_android_host_path(path)) return host;
|
||||
char target[PATH_MAX];
|
||||
long n = raw_readlinkat(AT_FDCWD, host, target, sizeof(target) - 1);
|
||||
if (n <= 0 || target[0] != '/') return host;
|
||||
target[n] = '\0';
|
||||
if (is_android_host_path(target) ||
|
||||
(g_ddnatv_root_len && strncmp(target, g_ddnatv_root, g_ddnatv_root_len) == 0)) return host;
|
||||
char guest_path[PATH_MAX];
|
||||
ddnatv_copy_cstr(guest_path, sizeof(guest_path), path);
|
||||
char guest_target[PATH_MAX];
|
||||
const char *followed = follow_guest_link(guest_path, guest_target, sizeof(guest_target));
|
||||
char mapped[PATH_MAX];
|
||||
ddnatv_copy_cstr(buffer, bufsize, rewrite_path_in_root(followed, mapped, sizeof(mapped)));
|
||||
return buffer;
|
||||
#else
|
||||
return host;
|
||||
#endif
|
||||
}
|
||||
size_t n = ddnatv_append(from_source, sizeof(from_source), 0, best->dst, best->dst_len);
|
||||
ddnatv_append(from_source, sizeof(from_source), n, path + best->src_len, strlen(path + best->src_len));
|
||||
path = from_source;
|
||||
}
|
||||
char guest[PATH_MAX];
|
||||
const char *followed = follow_guest_link(path, guest, sizeof(guest));
|
||||
char mapped[PATH_MAX];
|
||||
const char *host = rewrite_path_in_root(followed, mapped, sizeof(mapped));
|
||||
// the result may be `followed` or `path` itself, which can live here
|
||||
ddnatv_copy_cstr(buffer, bufsize, host);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
// Whether the rootfs keeps bin (pair DDNATV_USRMERGE_BIN) or sbin as a symlink into /usr, as
|
||||
// Ubuntu 24.04 does. Checked once per process with one readlink().
|
||||
static int rootfs_pair_is_merged(int pair) {
|
||||
static int known[3] = { 0, -1, -1 };
|
||||
if (known[pair] >= 0) return known[pair];
|
||||
static ssize_t (*orig_readlink)(const char *, char *, size_t) = NULL;
|
||||
if (!orig_readlink) orig_readlink = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink");
|
||||
const char *dir = (pair == DDNATV_USRMERGE_BIN) ? "/bin" : "/sbin";
|
||||
const char *usr_dir = (pair == DDNATV_USRMERGE_BIN) ? "usr/bin" : "usr/sbin";
|
||||
char path[PATH_MAX];
|
||||
char link[16];
|
||||
size_t n = ddnatv_append(path, sizeof(path), 0, g_ddnatv_root, g_ddnatv_root_len);
|
||||
ddnatv_append(path, sizeof(path), n, dir, strlen(dir));
|
||||
int saved_errno = errno;
|
||||
ssize_t len = orig_readlink ? orig_readlink(path, link, sizeof(link) - 1) : -1;
|
||||
errno = saved_errno;
|
||||
int merged = 0;
|
||||
if (len > 0) {
|
||||
link[len] = '\0';
|
||||
size_t ulen = strlen(usr_dir);
|
||||
merged = strncmp(link, usr_dir, ulen) == 0 && (link[ulen] == '\0' || (link[ulen] == '/' && link[ulen + 1] == '\0'));
|
||||
}
|
||||
known[pair] = merged;
|
||||
return merged;
|
||||
}
|
||||
|
||||
// The other spelling of a missing rootfs path under bin/sbin, or NULL when there is none or both
|
||||
// spellings reach the same directory (the retry could only fail again).
|
||||
static const char *usrmerge_retry_path(const char *target, char *alt, size_t altsz) {
|
||||
const char *sub = get_rootfs_subpath(target);
|
||||
if (!sub) return NULL;
|
||||
int pair = usrmerge_alt_path(sub, alt, altsz);
|
||||
if (pair == 0 || rootfs_pair_is_merged(pair)) return NULL;
|
||||
return alt;
|
||||
}
|
||||
|
||||
static const char *rewrite_unix_socket_path(const char *sun_path, char *out_buf, size_t out_size) {
|
||||
if (!sun_path || out_size == 0) return sun_path;
|
||||
// Abstract sockets start with null byte (\0); leave untouched
|
||||
if (sun_path[0] == '\0') {
|
||||
return sun_path;
|
||||
}
|
||||
|
||||
char full[PATH_MAX];
|
||||
const char *rw = (sun_path[0] == '/') ? rewrite_path(sun_path, full, sizeof(full)) : sun_path;
|
||||
size_t rw_len = strlen(rw);
|
||||
|
||||
// If rewritten path fits safely in sockaddr_un.sun_path (usually 108 bytes)
|
||||
if (rw_len < out_size) {
|
||||
strncpy(out_buf, rw, out_size - 1);
|
||||
out_buf[out_size - 1] = '\0';
|
||||
return out_buf;
|
||||
}
|
||||
|
||||
// Path exceeds sockaddr_un 108-byte limit!
|
||||
// Deterministically hash the path into /tmp to guarantee it fits safely (< 80 bytes)
|
||||
// while remaining 100% deterministic between server and client.
|
||||
unsigned long long hash = 14695981039346656037ULL;
|
||||
for (const char *p = rw; *p; p++) {
|
||||
hash ^= (unsigned char)(*p);
|
||||
hash *= 1099511628211ULL;
|
||||
}
|
||||
|
||||
init_ddnatv_hook();
|
||||
if (g_ddnatv_root[0] != '\0') {
|
||||
snprintf(out_buf, out_size, "%s/tmp/.ctx_sock_%016llx", g_ddnatv_root, hash);
|
||||
} else {
|
||||
snprintf(out_buf, out_size, "/tmp/.ctx_sock_%016llx", hash);
|
||||
}
|
||||
out_buf[out_size - 1] = '\0';
|
||||
return out_buf;
|
||||
}
|
||||
|
||||
static void unlink_mapped_unix_socket(const char *target) {
|
||||
if (!target) return;
|
||||
size_t t_len = strlen(target);
|
||||
if (t_len >= 107) {
|
||||
unsigned long long hash = 14695981039346656037ULL;
|
||||
for (const char *p = target; *p; p++) {
|
||||
hash ^= (unsigned char)(*p);
|
||||
hash *= 1099511628211ULL;
|
||||
}
|
||||
char short_path[PATH_MAX];
|
||||
init_ddnatv_hook();
|
||||
if (g_ddnatv_root[0] != '\0') {
|
||||
snprintf(short_path, sizeof(short_path), "%s/tmp/.ctx_sock_%016llx", g_ddnatv_root, hash);
|
||||
} else {
|
||||
snprintf(short_path, sizeof(short_path), "/tmp/.ctx_sock_%016llx", hash);
|
||||
}
|
||||
static int (*orig_unlink)(const char *) = NULL;
|
||||
if (!orig_unlink) orig_unlink = (int (*)(const char *))dlsym(RTLD_NEXT, "unlink");
|
||||
if (orig_unlink) orig_unlink(short_path);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* !DDNATV_HOST_TEST */
|
||||
@@ -0,0 +1,233 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
||||
// parts share their static functions and are built with the same command as before).
|
||||
//
|
||||
// The SIGSYS handler for system calls Android's seccomp filter blocks, and the signal hooks that
|
||||
// keep programs from replacing or blocking it.
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
|
||||
#ifndef SYS_SECCOMP
|
||||
#define SYS_SECCOMP 1
|
||||
#endif
|
||||
|
||||
static int (*get_real_sigaction(void))(int, const struct sigaction *, struct sigaction *);
|
||||
|
||||
// A program's own SIGSYS handler, for the traps of a seccomp filter it installed itself (Firefox's
|
||||
// and Chromium's sandboxes emulate the calls they trap there). Android's app filter traps with data
|
||||
// 0; such a sandbox's traps carry their own nonzero data, which the kernel gives as si_errno.
|
||||
static struct sigaction g_app_sigsys;
|
||||
static int g_app_sigsys_set = 0;
|
||||
|
||||
static void remember_app_sigsys(const struct sigaction *act, struct sigaction *oldact) {
|
||||
if (oldact) {
|
||||
if (g_app_sigsys_set) {
|
||||
*oldact = g_app_sigsys;
|
||||
} else {
|
||||
memset(oldact, 0, sizeof(*oldact));
|
||||
oldact->sa_handler = SIG_DFL;
|
||||
}
|
||||
}
|
||||
if (act) {
|
||||
g_app_sigsys = *act;
|
||||
g_app_sigsys_set = act->sa_handler != SIG_DFL && act->sa_handler != SIG_IGN;
|
||||
}
|
||||
}
|
||||
|
||||
// Intercept SECCOMP blocked syscalls (SIGSYS), resolve sandbox/landlock gracefully, and advance PC
|
||||
static void ddnatv_sigsys_handler(int sig, siginfo_t *info, void *ctx) {
|
||||
if (!ctx || !info) return;
|
||||
if (info->si_code != SYS_SECCOMP) {
|
||||
int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction();
|
||||
if (real_sig) {
|
||||
struct sigaction sa_dfl;
|
||||
memset(&sa_dfl, 0, sizeof(sa_dfl));
|
||||
sa_dfl.sa_handler = SIG_DFL;
|
||||
sigemptyset(&sa_dfl.sa_mask);
|
||||
real_sig(sig, &sa_dfl, NULL);
|
||||
}
|
||||
raise(sig);
|
||||
return;
|
||||
}
|
||||
if (info->si_errno != 0 && g_app_sigsys_set) {
|
||||
if (g_app_sigsys.sa_flags & SA_SIGINFO) g_app_sigsys.sa_sigaction(sig, info, ctx);
|
||||
else g_app_sigsys.sa_handler(sig);
|
||||
return;
|
||||
}
|
||||
ucontext_t *uctx = (ucontext_t *)ctx;
|
||||
|
||||
int sys_nr = info->si_syscall;
|
||||
long ret_val = is_sigsys_synthetic_success_syscall(sys_nr) ? 0 : -ENOSYS;
|
||||
|
||||
#if defined(__aarch64__)
|
||||
uctx->uc_mcontext.regs[0] = (uint64_t)ret_val;
|
||||
if (uctx->uc_mcontext.pc >= 4) {
|
||||
uint32_t insn_at_pc = 0;
|
||||
uint32_t insn_before_pc = 0;
|
||||
memcpy(&insn_at_pc, (const void *)uctx->uc_mcontext.pc, sizeof(insn_at_pc));
|
||||
memcpy(&insn_before_pc, (const void *)(uctx->uc_mcontext.pc - 4), sizeof(insn_before_pc));
|
||||
// On SECCOMP_RET_TRAP, the kernel already advanced PC past svc #0 (*(pc - 4) is svc #0).
|
||||
// Only advance PC if PC itself points to svc #0 and *(pc - 4) does not.
|
||||
int at_is_svc = ((insn_at_pc & 0xffe0001fU) == 0xd4000001U);
|
||||
int prev_is_svc = ((insn_before_pc & 0xffe0001fU) == 0xd4000001U);
|
||||
if (at_is_svc && !prev_is_svc) {
|
||||
uctx->uc_mcontext.pc += 4;
|
||||
}
|
||||
}
|
||||
#elif defined(__arm__)
|
||||
uctx->uc_mcontext.arm_r0 = (unsigned long)ret_val;
|
||||
if (uctx->uc_mcontext.arm_pc >= 4) {
|
||||
if (uctx->uc_mcontext.arm_cpsr & 0x20) {
|
||||
uint16_t insn_at_pc = 0;
|
||||
uint16_t insn_before_pc = 0;
|
||||
memcpy(&insn_at_pc, (const void *)uctx->uc_mcontext.arm_pc, sizeof(insn_at_pc));
|
||||
memcpy(&insn_before_pc, (const void *)(uctx->uc_mcontext.arm_pc - 2), sizeof(insn_before_pc));
|
||||
int at_is_svc = ((insn_at_pc & 0xff00U) == 0xdf00U);
|
||||
int prev_is_svc = ((insn_before_pc & 0xff00U) == 0xdf00U);
|
||||
if (at_is_svc && !prev_is_svc) {
|
||||
uctx->uc_mcontext.arm_pc += 2;
|
||||
}
|
||||
} else {
|
||||
uint32_t insn_at_pc = 0;
|
||||
uint32_t insn_before_pc = 0;
|
||||
memcpy(&insn_at_pc, (const void *)uctx->uc_mcontext.arm_pc, sizeof(insn_at_pc));
|
||||
memcpy(&insn_before_pc, (const void *)(uctx->uc_mcontext.arm_pc - 4), sizeof(insn_before_pc));
|
||||
int at_is_svc = ((insn_at_pc & 0x0f000000U) == 0x0f000000U);
|
||||
int prev_is_svc = ((insn_before_pc & 0x0f000000U) == 0x0f000000U);
|
||||
if (at_is_svc && !prev_is_svc) {
|
||||
uctx->uc_mcontext.arm_pc += 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
#elif defined(__x86_64__) && defined(REG_RAX)
|
||||
uctx->uc_mcontext.gregs[REG_RAX] = (greg_t)ret_val;
|
||||
if (uctx->uc_mcontext.gregs[REG_RIP] >= 2) {
|
||||
unsigned char insn_at[2] = {0};
|
||||
unsigned char insn_prev[2] = {0};
|
||||
memcpy(insn_at, (const void *)uctx->uc_mcontext.gregs[REG_RIP], sizeof(insn_at));
|
||||
memcpy(insn_prev, (const void *)(uctx->uc_mcontext.gregs[REG_RIP] - 2), sizeof(insn_prev));
|
||||
int at_is_sys = (insn_at[0] == 0x0f && insn_at[1] == 0x05);
|
||||
int prev_is_sys = (insn_prev[0] == 0x0f && insn_prev[1] == 0x05);
|
||||
if (at_is_sys && !prev_is_sys) {
|
||||
uctx->uc_mcontext.gregs[REG_RIP] += 2;
|
||||
}
|
||||
}
|
||||
#elif defined(__i386__) && defined(REG_EAX)
|
||||
uctx->uc_mcontext.gregs[REG_EAX] = (greg_t)ret_val;
|
||||
if (uctx->uc_mcontext.gregs[REG_EIP] >= 2) {
|
||||
unsigned char insn_at[2] = {0};
|
||||
unsigned char insn_prev[2] = {0};
|
||||
memcpy(insn_at, (const void *)uctx->uc_mcontext.gregs[REG_EIP], sizeof(insn_at));
|
||||
memcpy(insn_prev, (const void *)(uctx->uc_mcontext.gregs[REG_EIP] - 2), sizeof(insn_prev));
|
||||
int at_is_sys = (insn_at[0] == 0xcd && insn_at[1] == 0x80);
|
||||
int prev_is_sys = (insn_prev[0] == 0xcd && insn_prev[1] == 0x80);
|
||||
if (at_is_sys && !prev_is_sys) {
|
||||
uctx->uc_mcontext.gregs[REG_EIP] += 2;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static int (*get_real_sigaction(void))(int, const struct sigaction *, struct sigaction *) {
|
||||
static int (*real_sigaction)(int, const struct sigaction *, struct sigaction *) = NULL;
|
||||
if (!real_sigaction) {
|
||||
real_sigaction = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_NEXT, "sigaction");
|
||||
if (!real_sigaction) {
|
||||
real_sigaction = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_DEFAULT, "sigaction");
|
||||
}
|
||||
}
|
||||
return real_sigaction;
|
||||
}
|
||||
|
||||
__attribute__((constructor(101))) static void install_sigsys_handler(void) {
|
||||
int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction();
|
||||
if (!real_sig) return;
|
||||
|
||||
struct sigaction sa;
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sa_sigaction = ddnatv_sigsys_handler;
|
||||
sa.sa_flags = SA_SIGINFO | SA_RESTART;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
real_sig(SIGSYS, &sa, NULL);
|
||||
}
|
||||
|
||||
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) {
|
||||
int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction();
|
||||
if (!real_sig) return -1;
|
||||
|
||||
if (signum == SIGSYS) {
|
||||
if (act && act->sa_sigaction == ddnatv_sigsys_handler) {
|
||||
return real_sig(signum, act, oldact);
|
||||
}
|
||||
remember_app_sigsys(act, oldact);
|
||||
return 0;
|
||||
}
|
||||
return real_sig(signum, act, oldact);
|
||||
}
|
||||
|
||||
int rt_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact, size_t sigsetsize) {
|
||||
static int (*orig_rt_sigaction)(int, const struct sigaction *, struct sigaction *, size_t) = NULL;
|
||||
if (!orig_rt_sigaction) orig_rt_sigaction = (int (*)(int, const struct sigaction *, struct sigaction *, size_t))dlsym(RTLD_NEXT, "rt_sigaction");
|
||||
|
||||
if (signum == SIGSYS) {
|
||||
if (act && act->sa_sigaction == ddnatv_sigsys_handler) {
|
||||
return orig_rt_sigaction ? orig_rt_sigaction(signum, act, oldact, sigsetsize) : 0;
|
||||
}
|
||||
remember_app_sigsys(act, oldact);
|
||||
return 0;
|
||||
}
|
||||
return orig_rt_sigaction ? orig_rt_sigaction(signum, act, oldact, sigsetsize) : -1;
|
||||
}
|
||||
|
||||
int __libc_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) {
|
||||
return sigaction(signum, act, oldact);
|
||||
}
|
||||
|
||||
typedef void (*sighandler_t)(int);
|
||||
sighandler_t signal(int signum, sighandler_t handler) {
|
||||
static sighandler_t (*orig_signal)(int, sighandler_t) = NULL;
|
||||
if (!orig_signal) orig_signal = (sighandler_t (*)(int, sighandler_t))dlsym(RTLD_NEXT, "signal");
|
||||
if (signum == SIGSYS) {
|
||||
struct sigaction act, old;
|
||||
memset(&act, 0, sizeof(act));
|
||||
act.sa_handler = handler;
|
||||
remember_app_sigsys(&act, &old);
|
||||
return old.sa_handler;
|
||||
}
|
||||
return orig_signal ? orig_signal(signum, handler) : NULL;
|
||||
}
|
||||
|
||||
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset) {
|
||||
static int (*orig_sigprocmask)(int, const sigset_t *, sigset_t *) = NULL;
|
||||
if (!orig_sigprocmask) orig_sigprocmask = (int (*)(int, const sigset_t *, sigset_t *))dlsym(RTLD_NEXT, "sigprocmask");
|
||||
if (set && (how == SIG_BLOCK || how == SIG_SETMASK)) {
|
||||
sigset_t mod_set = *set;
|
||||
sigdelset(&mod_set, SIGSYS);
|
||||
return orig_sigprocmask ? orig_sigprocmask(how, &mod_set, oldset) : 0;
|
||||
}
|
||||
return orig_sigprocmask ? orig_sigprocmask(how, set, oldset) : 0;
|
||||
}
|
||||
|
||||
int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset) {
|
||||
static int (*orig_pthread_sigmask)(int, const sigset_t *, sigset_t *) = NULL;
|
||||
if (!orig_pthread_sigmask) orig_pthread_sigmask = (int (*)(int, const sigset_t *, sigset_t *))dlsym(RTLD_NEXT, "pthread_sigmask");
|
||||
if (set && (how == SIG_BLOCK || how == SIG_SETMASK)) {
|
||||
sigset_t mod_set = *set;
|
||||
sigdelset(&mod_set, SIGSYS);
|
||||
return orig_pthread_sigmask ? orig_pthread_sigmask(how, &mod_set, oldset) : 0;
|
||||
}
|
||||
return orig_pthread_sigmask ? orig_pthread_sigmask(how, set, oldset) : 0;
|
||||
}
|
||||
|
||||
int rt_sigprocmask(int how, const sigset_t *set, sigset_t *oldset, size_t sigsetsize) {
|
||||
static int (*orig_rt_sigprocmask)(int, const sigset_t *, sigset_t *, size_t) = NULL;
|
||||
if (!orig_rt_sigprocmask) orig_rt_sigprocmask = (int (*)(int, const sigset_t *, sigset_t *, size_t))dlsym(RTLD_NEXT, "rt_sigprocmask");
|
||||
if (set && (how == SIG_BLOCK || how == SIG_SETMASK)) {
|
||||
sigset_t mod_set;
|
||||
memcpy(&mod_set, set, sizeof(mod_set));
|
||||
sigdelset(&mod_set, SIGSYS);
|
||||
return orig_rt_sigprocmask ? orig_rt_sigprocmask(how, &mod_set, oldset, sigsetsize) : 0;
|
||||
}
|
||||
return orig_rt_sigprocmask ? orig_rt_sigprocmask(how, set, oldset, sigsetsize) : 0;
|
||||
}
|
||||
|
||||
#endif /* !DDNATV_HOST_TEST */
|
||||
@@ -0,0 +1,351 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
||||
// parts share their static functions and are built with the same command as before).
|
||||
//
|
||||
// Sockets: Unix socket paths in the rootfs, the redirected DNS queries of loopback DNS, and the
|
||||
// TCP servers programs start (the app offers to open them in the browser).
|
||||
|
||||
// The TCP port a listening socket was bound to, 0 for other sockets.
|
||||
static int listening_tcp_port(int fd) {
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t len = sizeof(ss);
|
||||
memset(&ss, 0, sizeof(ss));
|
||||
if (syscall(SYS_getsockname, fd, (struct sockaddr *)&ss, &len) != 0) return 0;
|
||||
if (ss.ss_family == AF_INET && len >= (socklen_t)sizeof(struct sockaddr_in))
|
||||
return ntohs(((struct sockaddr_in *)&ss)->sin_port);
|
||||
if (ss.ss_family == AF_INET6 && len >= (socklen_t)sizeof(struct sockaddr_in6))
|
||||
return ntohs(((struct sockaddr_in6 *)&ss)->sin6_port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Tells the app that this program listens on [port]: $DDNATV_ROOT/run/ddnatv/listening/PORT with
|
||||
// "PID NAME" (ListeningPorts). Raw system calls: nothing of this library runs again.
|
||||
static void report_listening_port(const char *root, size_t root_len, int port, const char *name) {
|
||||
if (root_len == 0 || port <= 0 || port > 65535) return;
|
||||
char path[PATH_MAX];
|
||||
int n = snprintf(path, sizeof(path), "%.*s/run/ddnatv/listening/%d", (int)root_len, root, port);
|
||||
if (n <= 0 || n >= (int)sizeof(path)) return;
|
||||
int out = (int)syscall(SYS_openat, AT_FDCWD, path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
|
||||
if (out < 0) return; // no app watching (the directory is the app's)
|
||||
char line[160];
|
||||
int m = snprintf(line, sizeof(line), "%d %.100s\n", (int)getpid(), name && name[0] ? name : "?");
|
||||
if (m > 0) syscall(SYS_write, out, line, (size_t)(m < (int)sizeof(line) ? m : (int)sizeof(line) - 1));
|
||||
syscall(SYS_close, out);
|
||||
}
|
||||
|
||||
// The first port of the kernel's range for ports it picks itself, from the text of
|
||||
// /proc/sys/net/ipv4/ip_local_port_range ("32768\t60999"); 32768, Linux's default, when there is none.
|
||||
static int ephemeral_port_low(const char *range) {
|
||||
if (!range) return 32768;
|
||||
while (*range == ' ' || *range == '\t') range++;
|
||||
int low = 0, digits = 0;
|
||||
while (*range >= '0' && *range <= '9' && digits < 6) low = low * 10 + (*range++ - '0'), digits++;
|
||||
return (digits > 0 && low > 0 && low <= 65535) ? low : 32768;
|
||||
}
|
||||
|
||||
// Whether listen() tells the app about a server on `port`, which it then connects to and sends an
|
||||
// HTTP request (LocalServers). Only for a port the program chose (`chosen`) below the kernel's own
|
||||
// range: a server whose port the kernel picked, or one in that range, is how programs talk to
|
||||
// each other (Gradle's daemon, the Kotlin daemon, a JVM's debugger, test runners). Such a server
|
||||
// often accepts one connection, so the app's own took the place of the program it waited for:
|
||||
// that one read EOF ("Did not receive connection preamble" from Gradle, #51). Web servers people
|
||||
// open (npm run dev, vite, python3 -m http.server, jupyter) are on ports such as 3000, 5173, 8000.
|
||||
static int listening_port_is_reported(int chosen, int port, int ephemeral_low) {
|
||||
return chosen && port > 0 && port < ephemeral_low;
|
||||
}
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
|
||||
// The kernel's range, read once (raw system calls: nothing of this library runs again)
|
||||
static int kernel_ephemeral_port_low(void) {
|
||||
static int low = 0;
|
||||
int v = __atomic_load_n(&low, __ATOMIC_RELAXED);
|
||||
if (v) return v;
|
||||
char text[64];
|
||||
ssize_t n = -1;
|
||||
int fd = (int)syscall(SYS_openat, AT_FDCWD, "/proc/sys/net/ipv4/ip_local_port_range", O_RDONLY | O_CLOEXEC);
|
||||
if (fd >= 0) {
|
||||
n = syscall(SYS_read, fd, text, sizeof(text) - 1);
|
||||
syscall(SYS_close, fd);
|
||||
}
|
||||
if (n > 0) text[n] = '\0';
|
||||
v = ephemeral_port_low(n > 0 ? text : NULL);
|
||||
__atomic_store_n(&low, v, __ATOMIC_RELAXED);
|
||||
return v;
|
||||
}
|
||||
|
||||
int listen(int sockfd, int backlog) {
|
||||
static int (*orig_listen)(int, int) = NULL;
|
||||
if (!orig_listen) orig_listen = (int (*)(int, int))dlsym(RTLD_NEXT, "listen");
|
||||
if (!orig_listen) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
int ret = orig_listen(sockfd, backlog);
|
||||
if (ret == 0) {
|
||||
int saved_errno = errno;
|
||||
int port = is_chosen_port_fd(sockfd) ? listening_tcp_port(sockfd) : 0;
|
||||
if (listening_port_is_reported(port > 0, port, kernel_ephemeral_port_low())) {
|
||||
init_ddnatv_hook();
|
||||
extern char *program_invocation_short_name;
|
||||
report_listening_port(g_ddnatv_root, g_ddnatv_root_len, port, program_invocation_short_name);
|
||||
}
|
||||
errno = saved_errno;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
|
||||
static int (*orig_bind)(int, const struct sockaddr *, socklen_t) = NULL;
|
||||
if (!orig_bind) orig_bind = (int (*)(int, const struct sockaddr *, socklen_t))dlsym(RTLD_NEXT, "bind");
|
||||
|
||||
if (addr && addrlen > offsetof(struct sockaddr_un, sun_path) && addr->sa_family == AF_UNIX) {
|
||||
const struct sockaddr_un *sun = (const struct sockaddr_un *)addr;
|
||||
if (sun->sun_path[0] != '\0') {
|
||||
struct sockaddr_un mod_sun;
|
||||
memset(&mod_sun, 0, sizeof(mod_sun));
|
||||
mod_sun.sun_family = AF_UNIX;
|
||||
rewrite_unix_socket_path(sun->sun_path, mod_sun.sun_path, sizeof(mod_sun.sun_path));
|
||||
return orig_bind ? orig_bind(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1;
|
||||
}
|
||||
}
|
||||
int ret = orig_bind ? orig_bind(sockfd, addr, addrlen) : -1;
|
||||
if (ret == 0 && addr) {
|
||||
int saved_errno = errno;
|
||||
int chosen = 0;
|
||||
if (addr->sa_family == AF_INET && addrlen >= (socklen_t)sizeof(struct sockaddr_in))
|
||||
chosen = ((const struct sockaddr_in *)addr)->sin_port != 0;
|
||||
else if (addr->sa_family == AF_INET6 && addrlen >= (socklen_t)sizeof(struct sockaddr_in6))
|
||||
chosen = ((const struct sockaddr_in6 *)addr)->sin6_port != 0;
|
||||
set_chosen_port_fd(sockfd, chosen);
|
||||
errno = saved_errno;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
|
||||
static int (*orig_connect)(int, const struct sockaddr *, socklen_t) = NULL;
|
||||
if (!orig_connect) orig_connect = (int (*)(int, const struct sockaddr *, socklen_t))dlsym(RTLD_NEXT, "connect");
|
||||
|
||||
if (addr && addrlen > offsetof(struct sockaddr_un, sun_path) && addr->sa_family == AF_UNIX) {
|
||||
const struct sockaddr_un *sun = (const struct sockaddr_un *)addr;
|
||||
if (sun->sun_path[0] != '\0') {
|
||||
struct sockaddr_un mod_sun;
|
||||
memset(&mod_sun, 0, sizeof(mod_sun));
|
||||
mod_sun.sun_family = AF_UNIX;
|
||||
char guest[sizeof(sun->sun_path) + 1];
|
||||
size_t max = addrlen - offsetof(struct sockaddr_un, sun_path);
|
||||
if (max > sizeof(sun->sun_path)) max = sizeof(sun->sun_path);
|
||||
memcpy(guest, sun->sun_path, max);
|
||||
guest[max] = '\0';
|
||||
char followed[PATH_MAX];
|
||||
const char *path = guest[0] == '/' ? follow_guest_link(guest, followed, sizeof(followed)) : guest;
|
||||
rewrite_unix_socket_path(path, mod_sun.sun_path, sizeof(mod_sun.sun_path));
|
||||
return orig_connect ? orig_connect(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (addr && is_loopback_dns(addr, addrlen)) {
|
||||
struct sockaddr_storage redirected;
|
||||
socklen_t redirected_len = dns_redirect_target(addr, &redirected);
|
||||
int ret = orig_connect ? orig_connect(sockfd, (struct sockaddr *)&redirected, redirected_len) : -1;
|
||||
if (ret == 0 || errno == EINPROGRESS) {
|
||||
int saved_errno = errno;
|
||||
record_dns_redirect(sockfd, addr, addrlen);
|
||||
errno = saved_errno;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
return orig_connect ? orig_connect(sockfd, addr, addrlen) : -1;
|
||||
}
|
||||
|
||||
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
|
||||
const struct sockaddr *dest_addr, socklen_t addrlen) {
|
||||
static ssize_t (*orig_sendto)(int, const void *, size_t, int, const struct sockaddr *, socklen_t) = NULL;
|
||||
if (!orig_sendto) orig_sendto = (ssize_t (*)(int, const void *, size_t, int, const struct sockaddr *, socklen_t))dlsym(RTLD_NEXT, "sendto");
|
||||
|
||||
if (dest_addr && is_loopback_dns(dest_addr, addrlen)) {
|
||||
struct sockaddr_storage redirected;
|
||||
socklen_t redirected_len = dns_redirect_target(dest_addr, &redirected);
|
||||
record_dns_redirect(sockfd, dest_addr, addrlen);
|
||||
return orig_sendto ? orig_sendto(sockfd, buf, len, flags, (struct sockaddr *)&redirected, redirected_len) : -1;
|
||||
}
|
||||
return orig_sendto ? orig_sendto(sockfd, buf, len, flags, dest_addr, addrlen) : -1;
|
||||
}
|
||||
|
||||
#if defined(__NR_getuid32)
|
||||
#define DDNATV_NR_REAL_GETUID __NR_getuid32
|
||||
#define DDNATV_NR_REAL_GETGID __NR_getgid32
|
||||
#else
|
||||
#define DDNATV_NR_REAL_GETUID __NR_getuid
|
||||
#define DDNATV_NR_REAL_GETGID __NR_getgid
|
||||
#endif
|
||||
|
||||
// Credentials sent over a Unix socket (SCM_CREDENTIALS) have to be the process's own: the kernel
|
||||
// refuses others with EPERM, and programs fill them in with getuid(), which is root's 0 here.
|
||||
// PulseAudio's clients send them on every connection, which then failed ("Connection terminated").
|
||||
// Returns 1 with the message copied into mod (its control data into ctrl) and the ids made real.
|
||||
static int real_scm_credentials(const struct msghdr *msg, struct msghdr *mod, char *ctrl, size_t ctrl_size) {
|
||||
if (!msg || !msg->msg_control || msg->msg_controllen < sizeof(struct cmsghdr) || msg->msg_controllen > ctrl_size) return 0;
|
||||
int found = 0;
|
||||
for (struct cmsghdr *c = CMSG_FIRSTHDR(msg); c; c = CMSG_NXTHDR((struct msghdr *)msg, c)) {
|
||||
if (c->cmsg_level == SOL_SOCKET && c->cmsg_type == SCM_CREDENTIALS) found = 1;
|
||||
}
|
||||
if (!found) return 0;
|
||||
memcpy(mod, msg, sizeof(*mod));
|
||||
memcpy(ctrl, msg->msg_control, msg->msg_controllen);
|
||||
mod->msg_control = ctrl;
|
||||
for (struct cmsghdr *c = CMSG_FIRSTHDR(mod); c; c = CMSG_NXTHDR(mod, c)) {
|
||||
if (c->cmsg_level != SOL_SOCKET || c->cmsg_type != SCM_CREDENTIALS || c->cmsg_len < CMSG_LEN(sizeof(struct ucred))) continue;
|
||||
// a record that claims more than the buffer holds is the kernel's to refuse, not ours to read
|
||||
if (c->cmsg_len > mod->msg_controllen - (size_t)((char *)c - ctrl)) break;
|
||||
struct ucred cred;
|
||||
memcpy(&cred, CMSG_DATA(c), sizeof(cred));
|
||||
cred.pid = getpid();
|
||||
cred.uid = (uid_t)call_orig_syscall(DDNATV_NR_REAL_GETUID, 0, 0, 0, 0, 0, 0);
|
||||
cred.gid = (gid_t)call_orig_syscall(DDNATV_NR_REAL_GETGID, 0, 0, 0, 0, 0, 0);
|
||||
memcpy(CMSG_DATA(c), &cred, sizeof(cred));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags) {
|
||||
static ssize_t (*orig_sendmsg)(int, const struct msghdr *, int) = NULL;
|
||||
if (!orig_sendmsg) orig_sendmsg = (ssize_t (*)(int, const struct msghdr *, int))dlsym(RTLD_NEXT, "sendmsg");
|
||||
struct msghdr cred_msg;
|
||||
char cred_ctrl[512] __attribute__((aligned(8)));
|
||||
if (real_scm_credentials(msg, &cred_msg, cred_ctrl, sizeof(cred_ctrl))) msg = &cred_msg;
|
||||
|
||||
if (msg && msg->msg_name && is_loopback_dns((const struct sockaddr *)msg->msg_name, msg->msg_namelen)) {
|
||||
struct sockaddr_storage redirected;
|
||||
socklen_t redirected_len = dns_redirect_target((const struct sockaddr *)msg->msg_name, &redirected);
|
||||
struct msghdr mod_msg;
|
||||
memcpy(&mod_msg, msg, sizeof(mod_msg));
|
||||
mod_msg.msg_name = &redirected;
|
||||
mod_msg.msg_namelen = redirected_len;
|
||||
record_dns_redirect(sockfd, (const struct sockaddr *)msg->msg_name, msg->msg_namelen);
|
||||
return orig_sendmsg ? orig_sendmsg(sockfd, &mod_msg, flags) : -1;
|
||||
}
|
||||
return orig_sendmsg ? orig_sendmsg(sockfd, msg, flags) : -1;
|
||||
}
|
||||
|
||||
static void present_peer_as_root(struct ucred *cred) {
|
||||
if (cred->uid == (uid_t)call_orig_syscall(DDNATV_NR_REAL_GETUID, 0, 0, 0, 0, 0, 0)) cred->uid = 0;
|
||||
if (cred->gid == (gid_t)call_orig_syscall(DDNATV_NR_REAL_GETGID, 0, 0, 0, 0, 0, 0)) cred->gid = 0;
|
||||
}
|
||||
|
||||
// A server compares a peer's credentials with the user the peer claims, which is getuid()'s 0
|
||||
// here, while the kernel reports the app's ids: dbus-daemon refused every EXTERNAL login ("Did not
|
||||
// receive a reply"), so the Plasma desktop never started.
|
||||
int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen) {
|
||||
static int (*orig_getsockopt)(int, int, int, void *, socklen_t *) = NULL;
|
||||
if (!orig_getsockopt) orig_getsockopt = (int (*)(int, int, int, void *, socklen_t *))dlsym(RTLD_NEXT, "getsockopt");
|
||||
if (!orig_getsockopt) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
int ret = orig_getsockopt(sockfd, level, optname, optval, optlen);
|
||||
if (ret == 0 && level == SOL_SOCKET && optname == SO_PEERCRED && optval && optlen && *optlen >= sizeof(struct ucred)) {
|
||||
struct ucred cred;
|
||||
memcpy(&cred, optval, sizeof(cred));
|
||||
present_peer_as_root(&cred);
|
||||
memcpy(optval, &cred, sizeof(cred));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void received_credentials_as_root(struct msghdr *msg) {
|
||||
if (!msg || !msg->msg_control || msg->msg_controllen < sizeof(struct cmsghdr)) return;
|
||||
for (struct cmsghdr *c = CMSG_FIRSTHDR(msg); c; c = CMSG_NXTHDR(msg, c)) {
|
||||
if (c->cmsg_level != SOL_SOCKET || c->cmsg_type != SCM_CREDENTIALS || c->cmsg_len < CMSG_LEN(sizeof(struct ucred))) continue;
|
||||
struct ucred cred;
|
||||
memcpy(&cred, CMSG_DATA(c), sizeof(cred));
|
||||
present_peer_as_root(&cred);
|
||||
memcpy(CMSG_DATA(c), &cred, sizeof(cred));
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
|
||||
struct sockaddr *src_addr, socklen_t *addrlen) {
|
||||
static ssize_t (*orig_recvfrom)(int, void *, size_t, int, struct sockaddr *, socklen_t *) = NULL;
|
||||
if (!orig_recvfrom) orig_recvfrom = (ssize_t (*)(int, void *, size_t, int, struct sockaddr *, socklen_t *))dlsym(RTLD_NEXT, "recvfrom");
|
||||
|
||||
socklen_t cap = addrlen ? *addrlen : 0;
|
||||
ssize_t ret = orig_recvfrom ? orig_recvfrom(sockfd, buf, len, flags, src_addr, addrlen) : -1;
|
||||
if (ret > 0 && src_addr && addrlen) {
|
||||
int saved_errno = errno;
|
||||
restore_dns_source(sockfd, src_addr, cap, addrlen);
|
||||
errno = saved_errno;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// What programs built with _FORTIFY_SOURCE call when the buffer's size is known; the check is
|
||||
// glibc's
|
||||
extern void __chk_fail(void) __attribute__((__noreturn__));
|
||||
ssize_t __recvfrom_chk(int sockfd, void *buf, size_t len, size_t buflen, int flags,
|
||||
struct sockaddr *src_addr, socklen_t *addrlen) {
|
||||
if (len > buflen) __chk_fail();
|
||||
return recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
|
||||
}
|
||||
|
||||
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags) {
|
||||
static ssize_t (*orig_recvmsg)(int, struct msghdr *, int) = NULL;
|
||||
if (!orig_recvmsg) orig_recvmsg = (ssize_t (*)(int, struct msghdr *, int))dlsym(RTLD_NEXT, "recvmsg");
|
||||
|
||||
socklen_t cap = msg ? msg->msg_namelen : 0;
|
||||
ssize_t ret = orig_recvmsg ? orig_recvmsg(sockfd, msg, flags) : -1;
|
||||
if (ret >= 0) received_credentials_as_root(msg);
|
||||
if (ret > 0 && msg && msg->msg_name) {
|
||||
int saved_errno = errno;
|
||||
restore_dns_source(sockfd, (struct sockaddr *)msg->msg_name, cap, &msg->msg_namelen);
|
||||
errno = saved_errno;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
|
||||
static int (*orig_getpeername)(int, struct sockaddr *, socklen_t *) = NULL;
|
||||
if (!orig_getpeername) orig_getpeername = (int (*)(int, struct sockaddr *, socklen_t *))dlsym(RTLD_NEXT, "getpeername");
|
||||
|
||||
socklen_t cap = addrlen ? *addrlen : 0;
|
||||
int ret = orig_getpeername ? orig_getpeername(sockfd, addr, addrlen) : -1;
|
||||
if (ret == 0 && addr && addrlen) restore_dns_source(sockfd, addr, cap, addrlen);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// accept() as accept4() without flags, the call bionic makes for both: Android's seccomp filter
|
||||
// for apps is made from what bionic calls. On the x86_64 emulator a translated accept() answered
|
||||
// with its system call number (43) as the new descriptor, and sshd failed every connection with
|
||||
// "fcntl(43, F_GETFL): Bad file descriptor" (Perl, Python and nginx use accept4 themselves).
|
||||
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
|
||||
static int (*orig_accept4)(int, struct sockaddr *, socklen_t *, int) = NULL;
|
||||
if (!orig_accept4) orig_accept4 = (int (*)(int, struct sockaddr *, socklen_t *, int))dlsym(RTLD_NEXT, "accept4");
|
||||
if (!orig_accept4) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
return orig_accept4(sockfd, addr, addrlen, 0);
|
||||
}
|
||||
|
||||
#ifndef AF_NETLINK
|
||||
#define AF_NETLINK 16
|
||||
#endif
|
||||
#ifndef NETLINK_AUDIT
|
||||
#define NETLINK_AUDIT 9
|
||||
#endif
|
||||
|
||||
int socket(int domain, int type, int protocol) {
|
||||
static int (*orig_socket)(int, int, int) = NULL;
|
||||
if (!orig_socket) orig_socket = (int (*)(int, int, int))dlsym(RTLD_NEXT, "socket");
|
||||
if (domain == AF_NETLINK && protocol == NETLINK_AUDIT) {
|
||||
errno = EPROTONOSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
int fd = orig_socket ? orig_socket(domain, type, protocol) : -1;
|
||||
// A new socket has not chosen a port, whatever the descriptor's last one had (close_range() and
|
||||
// raw close system calls do not pass the hook's close())
|
||||
if (fd >= 0) set_chosen_port_fd(fd, 0);
|
||||
return fd;
|
||||
}
|
||||
|
||||
#endif /* !DDNATV_HOST_TEST */
|
||||
@@ -0,0 +1,345 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
||||
// parts share their static functions and are built with the same command as before).
|
||||
//
|
||||
// Statically linked programs: their seccomp-blocked system calls are patched before they run.
|
||||
|
||||
// ---- Static programs ----------------------------------------------------------------------
|
||||
// A statically linked program (gcc -static) carries its own copy of glibc, whose system calls the
|
||||
// preloaded hook never sees. The ones Android's seccomp filter answers with SIGSYS therefore kill it:
|
||||
// set_robust_list at start-up, clone3 for every thread, rseq, and the others the hook's SIGSYS
|
||||
// handler catches for dynamic programs. Before such a program runs, The native userland patches those call sites
|
||||
// in its file to what that handler answers (0 for set_robust_list and the set*id family, -ENOSYS,
|
||||
// which glibc falls back from, for the rest), as scripts/patch-bootstrap-ld.py does to the rootfs
|
||||
// ld.so and libc at build time. The scanning is a pure function over a buffer, so tests/native can
|
||||
// check it against the script.
|
||||
|
||||
#define DDNATV_STATIC_WINDOW 16 // instructions between "mov x8, #nr" and its "svc #0"
|
||||
|
||||
// AArch64: what the SIGSYS handler answers for syscall `nr`: 1 for 0, 2 for -ENOSYS, 0 to leave it.
|
||||
// set_robust_list keeps the script's no-op (x0 then holds its non-error first argument).
|
||||
static int a64_blocked_syscall(unsigned int nr) {
|
||||
switch (nr) {
|
||||
case 99: // set_robust_list
|
||||
return 3;
|
||||
case 100: // get_robust_list
|
||||
case 143: case 144: case 145: case 146: case 147: case 149: // setregid setgid setreuid setuid setresuid setresgid
|
||||
case 151: case 152: case 159: // setfsuid setfsgid setgroups
|
||||
return 1;
|
||||
case 97: case 217: case 218: case 219: // unshare add_key request_key keyctl
|
||||
case 268: case 277: case 280: case 293: // setns seccomp bpf rseq
|
||||
return 2;
|
||||
default:
|
||||
return (nr >= 424 && nr < 512) ? 2 : 0; // newer than Android's filter (clone3, close_range, ...)
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t rd32(const unsigned char *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); }
|
||||
static uint16_t rd16(const unsigned char *p) { return (uint16_t)(p[0] | (p[1] << 8)); }
|
||||
static void wr32(unsigned char *p, uint32_t v) { p[0] = v & 0xff; p[1] = (v >> 8) & 0xff; p[2] = (v >> 16) & 0xff; p[3] = (v >> 24) & 0xff; }
|
||||
static void wr16(unsigned char *p, uint16_t v) { p[0] = v & 0xff; p[1] = (v >> 8) & 0xff; }
|
||||
|
||||
// An AArch64 instruction that ends the search for the "svc #0" of a "mov x8, #nr": a branch, another
|
||||
// svc, or anything with x8 in its first register field (another write to x8, most likely).
|
||||
static int a64_ends_syscall_window(uint32_t insn) {
|
||||
if ((insn & 0x1f) == 8) return 1;
|
||||
if ((insn & 0xffe0001f) == 0xd4000001u) return 1; // svc #imm
|
||||
if ((insn & 0x7c000000u) == 0x14000000u) return 1; // b, bl
|
||||
if ((insn & 0xff000010u) == 0x54000000u) return 1; // b.cond
|
||||
if ((insn & 0x7e000000u) == 0x34000000u) return 1; // cbz, cbnz
|
||||
if ((insn & 0x7e000000u) == 0x36000000u) return 1; // tbz, tbnz
|
||||
if ((insn & 0xfe000000u) == 0xd6000000u) return 1; // br, blr, ret
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t a64_patch_seccomp_syscalls(unsigned char *data, size_t start, size_t end) {
|
||||
size_t patched = 0;
|
||||
start = (start + 3) & ~(size_t)3;
|
||||
for (size_t pos = start; pos + 4 <= end; pos += 4) {
|
||||
uint32_t insn = rd32(data + pos);
|
||||
// movz x8, #nr / movz w8, #nr
|
||||
if ((insn & 0x7fe0001fu) != 0x52800008u) continue;
|
||||
int result = a64_blocked_syscall((insn >> 5) & 0xffff);
|
||||
if (!result) continue;
|
||||
for (size_t s = pos + 4, i = 0; s + 4 <= end && i < DDNATV_STATIC_WINDOW; s += 4, i++) {
|
||||
uint32_t next = rd32(data + s);
|
||||
if (next == 0xd4000001u) { // svc #0
|
||||
wr32(data + s, result == 3 ? 0xd503201fu /* nop */
|
||||
: result == 1 ? 0xd2800000u /* mov x0, #0 */
|
||||
: 0x928004a0u); /* mov x0, #-38 */
|
||||
patched++;
|
||||
break;
|
||||
}
|
||||
if (a64_ends_syscall_window(next)) break;
|
||||
}
|
||||
}
|
||||
return patched;
|
||||
}
|
||||
|
||||
// 32-bit ARM: the three calls glibc makes on its own, with the exact patterns of the script
|
||||
// (ARM mode, Thumb-2 with r7, and Thumb-2 through __libc_do_syscall with r12).
|
||||
static long thumb_bl_target(const unsigned char *data, size_t size, size_t off) {
|
||||
if (off + 4 > size) return -1;
|
||||
uint16_t hw1 = rd16(data + off), hw2 = rd16(data + off + 2);
|
||||
if ((hw1 & 0xf800) != 0xf000 || (hw2 & 0xd000) != 0xd000) return -1;
|
||||
long s = (hw1 >> 10) & 1;
|
||||
long imm10 = hw1 & 0x3ff;
|
||||
long j1 = (hw2 >> 13) & 1, j2 = (hw2 >> 11) & 1;
|
||||
long imm11 = hw2 & 0x7ff;
|
||||
long i1 = (j1 ^ s) ^ 1, i2 = (j2 ^ s) ^ 1;
|
||||
long imm25 = (s << 24) | (i1 << 23) | (i2 << 22) | (imm10 << 12) | (imm11 << 1);
|
||||
if (s) imm25 -= (1L << 25);
|
||||
return (long)off + 4 + imm25;
|
||||
}
|
||||
|
||||
static size_t arm_patch_seccomp_syscalls(unsigned char *data, size_t start, size_t end) {
|
||||
static const struct { uint32_t movw_r7; uint32_t repl; } arm[] = {
|
||||
{ 0xe3007152u, 0xe320f000u }, // movw r7, #338 (set_robust_list) -> nop
|
||||
{ 0xe30071b3u, 0xe3e00025u }, // movw r7, #435 (clone3) -> mvn r0, #37
|
||||
{ 0xe300718eu, 0xe3e00025u }, // movw r7, #398 (rseq) -> mvn r0, #37
|
||||
};
|
||||
static const struct { uint16_t hw1, hw2; int nop_only; } thumb_r7[] = {
|
||||
{ 0xf240, 0x1752, 1 }, // movw r7, #338
|
||||
{ 0xf240, 0x17b3, 0 }, // movw r7, #435
|
||||
{ 0xf240, 0x178e, 0 }, // movw r7, #398
|
||||
};
|
||||
static const struct { uint16_t hw1, hw2; uint16_t repl1, repl2; } thumb_r12[] = {
|
||||
{ 0xf44f, 0x7ca9, 0xf04f, 0x0000 }, // mov.w r12, #338 -> mov.w r0, #0
|
||||
{ 0xf44f, 0x7cc7, 0xf06f, 0x0025 }, // mov.w r12, #398 -> mvn.w r0, #37
|
||||
{ 0xf240, 0x1cb3, 0xf06f, 0x0025 }, // movw r12, #435 -> mvn.w r0, #37
|
||||
};
|
||||
static const unsigned char do_syscall_stub[6] = { 0x80, 0xb5, 0x67, 0x46, 0x00, 0xdf };
|
||||
size_t patched = 0;
|
||||
|
||||
size_t a4 = (start + 3) & ~(size_t)3, e4 = end & ~(size_t)3;
|
||||
for (size_t pos = a4; pos + 4 < e4; pos += 4) {
|
||||
uint32_t insn = rd32(data + pos);
|
||||
for (size_t k = 0; k < sizeof(arm) / sizeof(arm[0]); k++) {
|
||||
if (insn != arm[k].movw_r7) continue;
|
||||
for (size_t s = pos + 4; s < e4 && s < pos + 68; s += 4) {
|
||||
if (rd32(data + s) == 0xef000000u) { // svc #0
|
||||
wr32(data + s, arm[k].repl);
|
||||
patched++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t a2 = (start + 1) & ~(size_t)1, e2 = end & ~(size_t)1;
|
||||
for (size_t t = a2; t + 4 < e2; t += 2) {
|
||||
uint16_t hw1 = rd16(data + t), hw2 = rd16(data + t + 2);
|
||||
int matched_r7 = 0;
|
||||
for (size_t k = 0; k < sizeof(thumb_r7) / sizeof(thumb_r7[0]); k++) {
|
||||
if (hw1 != thumb_r7[k].hw1 || hw2 != thumb_r7[k].hw2) continue;
|
||||
matched_r7 = 1;
|
||||
size_t limit = (e2 - 2 < t + 64 ? e2 - 2 : t + 64) + 2;
|
||||
for (size_t s = t + 4; s < limit; s += 2) {
|
||||
if (rd16(data + s) == 0xdf00) { // svc #0
|
||||
if (!thumb_r7[k].nop_only) {
|
||||
wr16(data + t, 0xf06f); // mvn.w r0, #37 in place of the movw
|
||||
wr16(data + t + 2, 0x0025);
|
||||
}
|
||||
wr16(data + s, 0xbf00); // nop
|
||||
patched++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (matched_r7) continue;
|
||||
for (size_t k = 0; k < sizeof(thumb_r12) / sizeof(thumb_r12[0]); k++) {
|
||||
if (hw1 != thumb_r12[k].hw1 || hw2 != thumb_r12[k].hw2) continue;
|
||||
size_t limit = (e2 - 4 < t + 48 ? e2 - 4 : t + 48) + 2;
|
||||
for (size_t s = t + 4; s < limit; s += 2) {
|
||||
long target = thumb_bl_target(data, end, s);
|
||||
if (target >= (long)start && target + 6 <= (long)end && memcmp(data + target, do_syscall_stub, 6) == 0) {
|
||||
wr16(data + s, thumb_r12[k].repl1);
|
||||
wr16(data + s + 2, thumb_r12[k].repl2);
|
||||
patched++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return patched;
|
||||
}
|
||||
|
||||
#ifndef EM_ARM
|
||||
#define EM_ARM 40
|
||||
#endif
|
||||
#ifndef EM_AARCH64
|
||||
#define EM_AARCH64 183
|
||||
#endif
|
||||
|
||||
// Patches the blocked syscalls in data[start, end), code of an ELF for `e_machine`; returns how many.
|
||||
static size_t patch_seccomp_syscalls(unsigned char *data, size_t start, size_t end, uint16_t e_machine) {
|
||||
if (!data || end <= start) return 0;
|
||||
if (e_machine == EM_AARCH64) return a64_patch_seccomp_syscalls(data, start, end);
|
||||
if (e_machine == EM_ARM) return arm_patch_seccomp_syscalls(data, start, end);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Reads the program headers of an ELF file into `ph` (as 64-bit fields); returns their number, or -1.
|
||||
struct ddnatv_phdr { uint32_t type, flags; uint64_t offset, filesz; };
|
||||
static int read_elf_phdrs(int fd, uint16_t *machine, struct ddnatv_phdr *ph, int max) {
|
||||
unsigned char eh[64];
|
||||
if (pread(fd, eh, sizeof(eh), 0) < 52 || memcmp(eh, "\x7f" "ELF", 4) != 0 || eh[5] != 1) return -1;
|
||||
int is64 = eh[4] == 2;
|
||||
uint64_t phoff = is64 ? ((uint64_t)rd32(eh + 32) | ((uint64_t)rd32(eh + 36) << 32)) : rd32(eh + 28);
|
||||
uint16_t phentsize = rd16(eh + (is64 ? 54 : 42));
|
||||
uint16_t phnum = rd16(eh + (is64 ? 56 : 44));
|
||||
*machine = rd16(eh + 18);
|
||||
if (phentsize < (is64 ? 56 : 32) || phnum == 0 || phnum > max) return -1;
|
||||
for (int i = 0; i < phnum; i++) {
|
||||
unsigned char e[64];
|
||||
if (pread(fd, e, phentsize < sizeof(e) ? phentsize : sizeof(e), (off_t)(phoff + (uint64_t)i * phentsize)) < (is64 ? 56 : 32)) return -1;
|
||||
ph[i].type = rd32(e);
|
||||
if (is64) {
|
||||
ph[i].flags = rd32(e + 4);
|
||||
ph[i].offset = (uint64_t)rd32(e + 8) | ((uint64_t)rd32(e + 12) << 32);
|
||||
ph[i].filesz = (uint64_t)rd32(e + 32) | ((uint64_t)rd32(e + 36) << 32);
|
||||
} else {
|
||||
ph[i].offset = rd32(e + 4);
|
||||
ph[i].filesz = rd32(e + 16);
|
||||
ph[i].flags = rd32(e + 24);
|
||||
}
|
||||
}
|
||||
return phnum;
|
||||
}
|
||||
|
||||
// Whether the ELF has a Go build-ID note: Go programs make their own system calls and never these.
|
||||
static int elf_is_go(int fd, const struct ddnatv_phdr *ph, int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (ph[i].type != 4 /* PT_NOTE */ || ph[i].filesz < 16) continue;
|
||||
unsigned char note[16];
|
||||
if (pread(fd, note, sizeof(note), (off_t)ph[i].offset) != sizeof(note)) continue;
|
||||
if (rd32(note) == 4 && memcmp(note + 12, "Go\0\0", 4) == 0) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define DDNATV_STATIC_XATTR "user.ddnatv.seccomp"
|
||||
#define DDNATV_STATIC_MAX_CODE (256u * 1024 * 1024)
|
||||
|
||||
#ifdef DDNATV_HAVE_GOSYS
|
||||
static int prepare_go_program(int fd, const char *path, const struct stat *st);
|
||||
static uint32_t go_blob_fingerprint(void);
|
||||
static int go_disabled(void);
|
||||
#endif
|
||||
|
||||
// What was done to a file is recorded with its size and time, with the rootfs (a Go program carries
|
||||
// the paths of it) and with the code Go programs are given (go_programs.c): a newer The native userland looks at
|
||||
// the programs again.
|
||||
static void static_marker(const struct stat *st, char *buf, size_t size) {
|
||||
unsigned fingerprint = 0;
|
||||
const char *off = "";
|
||||
#ifdef DDNATV_HAVE_GOSYS
|
||||
fingerprint = go_blob_fingerprint();
|
||||
if (go_disabled()) off = " off";
|
||||
#endif
|
||||
snprintf(buf, size, "3 %lld %lld.%09ld %08x %s%s", (long long)st->st_size, (long long)st->st_mtim.tv_sec,
|
||||
(long)st->st_mtim.tv_nsec, fingerprint, g_ddnatv_root, off);
|
||||
}
|
||||
|
||||
// Makes a static program safe to start (see above). Runs before every start of one, so a program
|
||||
// already handled (marked by an extended attribute that holds its size and modification time) costs
|
||||
// one fstat and one getxattr; without extended attributes it is scanned again each time. Returns the
|
||||
// number of patched calls, 0 when there was nothing to do, -1 when the file could not be handled
|
||||
// (it is then started as it is).
|
||||
// go_only: leave the file alone unless it is an AArch64 Go program. That is for a program that asks for
|
||||
// Android's linker (GOOS=android): it is started like the dynamic ones, but it makes its own system
|
||||
// calls, so it gets the code of the static Go programs (#42).
|
||||
static int prepare_static_program_ex(const char *path, int go_only) {
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return -1;
|
||||
struct stat st;
|
||||
char marker[PATH_MAX + 96], seen[PATH_MAX + 96];
|
||||
if (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
static_marker(&st, marker, sizeof(marker));
|
||||
ssize_t seen_len = fgetxattr(fd, DDNATV_STATIC_XATTR, seen, sizeof(seen) - 1);
|
||||
if (seen_len > 0) {
|
||||
seen[seen_len] = '\0';
|
||||
if (strcmp(seen, marker) == 0) {
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
struct ddnatv_phdr ph[64];
|
||||
uint16_t machine = 0;
|
||||
int n = read_elf_phdrs(fd, &machine, ph, 64);
|
||||
if (go_only && !(n >= 0 && machine == EM_AARCH64 && elf_is_go(fd, ph, n))) {
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
if (n < 0 || (machine != EM_AARCH64 && machine != EM_ARM) || elf_is_go(fd, ph, n)) {
|
||||
// A Go program has no C library for the hook to be loaded into: it gets code of its own
|
||||
int go_rc = 0;
|
||||
#ifdef DDNATV_HAVE_GOSYS
|
||||
if (n >= 0 && machine == EM_AARCH64 && elf_is_go(fd, ph, n)) go_rc = prepare_go_program(fd, path, &st);
|
||||
#endif
|
||||
if (n >= 0 && go_rc >= 0) {
|
||||
struct stat after;
|
||||
if (fstat(fd, &after) == 0) static_marker(&after, marker, sizeof(marker));
|
||||
fsetxattr(fd, DDNATV_STATIC_XATTR, marker, strlen(marker), 0);
|
||||
}
|
||||
close(fd);
|
||||
return n < 0 || go_rc < 0 ? -1 : 0;
|
||||
}
|
||||
|
||||
int total = 0;
|
||||
int wfd = -1;
|
||||
int made_writable = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (ph[i].type != 1 /* PT_LOAD */ || !(ph[i].flags & 1 /* PF_X */) || ph[i].filesz == 0) continue;
|
||||
if (ph[i].filesz > DDNATV_STATIC_MAX_CODE || ph[i].offset > (uint64_t)st.st_size ||
|
||||
ph[i].filesz > (uint64_t)st.st_size - ph[i].offset) continue;
|
||||
size_t len = (size_t)ph[i].filesz;
|
||||
unsigned char *code = (unsigned char *)malloc(len);
|
||||
if (!code) continue;
|
||||
if (pread(fd, code, len, (off_t)ph[i].offset) != (ssize_t)len) {
|
||||
free(code);
|
||||
continue;
|
||||
}
|
||||
size_t patched = patch_seccomp_syscalls(code, 0, len, machine);
|
||||
if (patched > 0) {
|
||||
if (wfd < 0) {
|
||||
wfd = open(path, O_WRONLY | O_CLOEXEC);
|
||||
if (wfd < 0 && errno == EACCES && fchmod(fd, (st.st_mode | S_IWUSR) & 07777) == 0) {
|
||||
made_writable = 1;
|
||||
wfd = open(path, O_WRONLY | O_CLOEXEC);
|
||||
}
|
||||
}
|
||||
// ETXTBSY: the program is running right now, so it was handled when it started.
|
||||
if (wfd < 0 || pwrite(wfd, code, len, (off_t)ph[i].offset) != (ssize_t)len) {
|
||||
free(code);
|
||||
total = -1;
|
||||
break;
|
||||
}
|
||||
total += (int)patched;
|
||||
}
|
||||
free(code);
|
||||
}
|
||||
if (wfd >= 0) {
|
||||
// Keep the times: make and friends must not see a rebuilt program.
|
||||
const struct timespec times[2] = { st.st_atim, st.st_mtim };
|
||||
futimens(wfd, times);
|
||||
close(wfd);
|
||||
}
|
||||
if (made_writable) fchmod(fd, st.st_mode & 07777);
|
||||
if (total >= 0) {
|
||||
struct stat after;
|
||||
if (fstat(fd, &after) == 0) {
|
||||
static_marker(&after, marker, sizeof(marker));
|
||||
fsetxattr(fd, DDNATV_STATIC_XATTR, marker, strlen(marker), 0);
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
return total;
|
||||
}
|
||||
|
||||
static int prepare_static_program(const char *path) { return prepare_static_program_ex(path, 0); }
|
||||
@@ -0,0 +1,128 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
||||
// parts share their static functions and are built with the same command as before).
|
||||
//
|
||||
// The system calls that Android's seccomp filter answers with SIGSYS, and which of them the
|
||||
// SIGSYS handler (signals.c) turns into a fake success.
|
||||
|
||||
static int is_sigsys_synthetic_success_syscall(int sys_nr) {
|
||||
#if defined(__NR_setuid)
|
||||
if (sys_nr == __NR_setuid) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setuid32)
|
||||
if (sys_nr == __NR_setuid32) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setgid)
|
||||
if (sys_nr == __NR_setgid) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setgid32)
|
||||
if (sys_nr == __NR_setgid32) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setreuid)
|
||||
if (sys_nr == __NR_setreuid) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setreuid32)
|
||||
if (sys_nr == __NR_setreuid32) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setregid)
|
||||
if (sys_nr == __NR_setregid) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setregid32)
|
||||
if (sys_nr == __NR_setregid32) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setresuid)
|
||||
if (sys_nr == __NR_setresuid) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setresuid32)
|
||||
if (sys_nr == __NR_setresuid32) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setresgid)
|
||||
if (sys_nr == __NR_setresgid) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setresgid32)
|
||||
if (sys_nr == __NR_setresgid32) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setfsuid)
|
||||
if (sys_nr == __NR_setfsuid) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setfsuid32)
|
||||
if (sys_nr == __NR_setfsuid32) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setfsgid)
|
||||
if (sys_nr == __NR_setfsgid) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setfsgid32)
|
||||
if (sys_nr == __NR_setfsgid32) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setgroups)
|
||||
if (sys_nr == __NR_setgroups) return 1;
|
||||
#endif
|
||||
#if defined(__NR_setgroups32)
|
||||
if (sys_nr == __NR_setgroups32) return 1;
|
||||
#endif
|
||||
#if defined(__NR_set_robust_list)
|
||||
if (sys_nr == __NR_set_robust_list) return 1;
|
||||
#endif
|
||||
#if defined(__NR_get_robust_list)
|
||||
if (sys_nr == __NR_get_robust_list) return 1;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int is_handled_sigsys_syscall(int sys_nr) {
|
||||
if (is_sigsys_synthetic_success_syscall(sys_nr)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Unified Linux 5.1+ syscall numbers across architectures
|
||||
if (sys_nr >= 424 && sys_nr < 512) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Architecture-specific syscall numbers guarded per target ABI
|
||||
#if defined(__aarch64__)
|
||||
if (sys_nr == 97 /* __NR_unshare */ ||
|
||||
sys_nr == 217 /* __NR_add_key */ ||
|
||||
sys_nr == 218 /* __NR_request_key */ ||
|
||||
sys_nr == 219 /* __NR_keyctl */ ||
|
||||
sys_nr == 268 /* __NR_setns */ ||
|
||||
sys_nr == 277 /* __NR_seccomp */ ||
|
||||
sys_nr == 280 /* __NR_bpf */ ||
|
||||
sys_nr == 293 /* __NR_rseq */) {
|
||||
return 1;
|
||||
}
|
||||
#elif defined(__arm__)
|
||||
if (sys_nr == 309 /* __NR_add_key */ ||
|
||||
sys_nr == 310 /* __NR_request_key */ ||
|
||||
sys_nr == 311 /* __NR_keyctl */ ||
|
||||
sys_nr == 337 /* __NR_unshare */ ||
|
||||
sys_nr == 375 /* __NR_setns */ ||
|
||||
sys_nr == 383 /* __NR_seccomp */ ||
|
||||
sys_nr == 386 /* __NR_bpf */ ||
|
||||
sys_nr == 398 /* __NR_rseq */) {
|
||||
return 1;
|
||||
}
|
||||
#elif defined(__x86_64__)
|
||||
if (sys_nr == 248 /* __NR_add_key */ ||
|
||||
sys_nr == 249 /* __NR_request_key */ ||
|
||||
sys_nr == 250 /* __NR_keyctl */ ||
|
||||
sys_nr == 272 /* __NR_unshare */ ||
|
||||
sys_nr == 308 /* __NR_setns */ ||
|
||||
sys_nr == 317 /* __NR_seccomp */ ||
|
||||
sys_nr == 321 /* __NR_bpf */ ||
|
||||
sys_nr == 334 /* __NR_rseq */) {
|
||||
return 1;
|
||||
}
|
||||
#elif defined(__i386__)
|
||||
if (sys_nr == 286 /* __NR_add_key */ ||
|
||||
sys_nr == 287 /* __NR_request_key */ ||
|
||||
sys_nr == 288 /* __NR_keyctl */ ||
|
||||
sys_nr == 310 /* __NR_unshare */ ||
|
||||
sys_nr == 346 /* __NR_setns */ ||
|
||||
sys_nr == 354 /* __NR_seccomp */ ||
|
||||
sys_nr == 357 /* __NR_bpf */ ||
|
||||
sys_nr == 386 /* __NR_rseq */) {
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,561 @@
|
||||
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
||||
// parts share their static functions and are built with the same command as before).
|
||||
//
|
||||
// System V shared memory (shmget, shmat, shmdt, shmctl). Android's seccomp filter blocks these system
|
||||
// calls for apps (the SIGSYS handler turns them into ENOSYS), but X uses them: a program draws an image
|
||||
// into a segment and the X server reads it from there (the MIT-SHM extension), and Qt, GTK and SDL
|
||||
// programs expect it. The segments are files of their own in /tmp/.ddnatv-sysv-shm of the rootfs,
|
||||
// which every process of the app can open and map:
|
||||
//
|
||||
// id-N the segment's description: "PID KEY MODE SIZE" (who made it, its key, its rights, its size)
|
||||
// seg-N its memory (a file of SIZE bytes, mapped MAP_SHARED)
|
||||
// key-K for a segment with a key: the N it belongs to (K in hex)
|
||||
// att-N-P process P has the segment attached (made by shmat(), removed by its last shmdt())
|
||||
//
|
||||
// N is chosen at random and claimed with O_EXCL, so processes never pick the same one. IPC_RMID
|
||||
// removes the files at once: programs that attached the segment keep their mapping (as on Linux),
|
||||
// later attaches fail, which is what X programs expect (they remove a segment as soon as the X
|
||||
// server has attached it). A segment nobody removed stays until the session's directory is cleared.
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
|
||||
#ifndef MAP_FIXED_NOREPLACE
|
||||
#define MAP_FIXED_NOREPLACE 0x100000
|
||||
#endif
|
||||
#ifndef SHM_REMAP
|
||||
#define SHM_REMAP 040000
|
||||
#endif
|
||||
|
||||
#define SHM_DIR "/tmp/.ddnatv-sysv-shm"
|
||||
#define SHM_MAX_ATTACH 512
|
||||
|
||||
struct shm_meta {
|
||||
int pid;
|
||||
long key;
|
||||
unsigned mode;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
struct shm_attach {
|
||||
void *addr;
|
||||
size_t size;
|
||||
int id;
|
||||
};
|
||||
|
||||
static struct shm_attach g_shm_attach[SHM_MAX_ATTACH];
|
||||
static pthread_mutex_t g_shm_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
// root + /tmp/.ddnatv-sysv-shm[/name]
|
||||
static int shm_path(char *out, size_t outsz, const char *name) {
|
||||
int n = snprintf(out, outsz, "%s%s%s%s", g_ddnatv_root, SHM_DIR, name ? "/" : "", name ? name : "");
|
||||
return n > 0 && (size_t)n < outsz ? 0 : -1;
|
||||
}
|
||||
|
||||
static int shm_open_file(const char *name, int flags, unsigned mode) {
|
||||
char path[PATH_MAX];
|
||||
if (shm_path(path, sizeof(path), name) != 0) return -1;
|
||||
return (int)syscall(SYS_openat, AT_FDCWD, path, flags | O_CLOEXEC, mode);
|
||||
}
|
||||
|
||||
static void shm_unlink_file(const char *name) {
|
||||
char path[PATH_MAX];
|
||||
if (shm_path(path, sizeof(path), name) == 0) syscall(SYS_unlinkat, AT_FDCWD, path, 0);
|
||||
}
|
||||
|
||||
static int shm_ensure_dir(void) {
|
||||
char path[PATH_MAX];
|
||||
if (shm_path(path, sizeof(path), NULL) != 0) return -1;
|
||||
if (syscall(SYS_mkdirat, AT_FDCWD, path, 0700) == 0 || errno == EEXIST) return 0;
|
||||
// /tmp of the rootfs missing too (a rootfs being set up)
|
||||
char tmp[PATH_MAX];
|
||||
snprintf(tmp, sizeof(tmp), "%s/tmp", g_ddnatv_root);
|
||||
syscall(SYS_mkdirat, AT_FDCWD, tmp, 01777);
|
||||
return (syscall(SYS_mkdirat, AT_FDCWD, path, 0700) == 0 || errno == EEXIST) ? 0 : -1;
|
||||
}
|
||||
|
||||
static int shm_read_small(const char *name, char *buf, size_t bufsz) {
|
||||
int fd = shm_open_file(name, O_RDONLY, 0);
|
||||
if (fd < 0) return -1;
|
||||
ssize_t n = (ssize_t)syscall(SYS_read, fd, buf, bufsz - 1);
|
||||
syscall(SYS_close, fd);
|
||||
if (n < 0) return -1;
|
||||
buf[n] = '\0';
|
||||
return (int)n;
|
||||
}
|
||||
|
||||
// The description of segment `id`; waits a moment for one being written. 0, or -1 (errno EINVAL).
|
||||
static int shm_read_meta(int id, struct shm_meta *meta) {
|
||||
char name[32], text[128];
|
||||
snprintf(name, sizeof(name), "id-%d", id);
|
||||
for (int attempt = 0; attempt < 50; attempt++) {
|
||||
int n = shm_read_small(name, text, sizeof(text));
|
||||
if (n < 0) break;
|
||||
unsigned long size = 0;
|
||||
if (n > 0 && sscanf(text, "%d %lx %o %lu", &meta->pid, &meta->key, &meta->mode, &size) == 4) {
|
||||
meta->size = (size_t)size;
|
||||
return 0;
|
||||
}
|
||||
struct timespec pause = { 0, 1000000 };
|
||||
nanosleep(&pause, NULL);
|
||||
}
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int shm_process_alive(int pid) {
|
||||
return pid > 0 && (kill(pid, 0) == 0 || errno == EPERM);
|
||||
}
|
||||
|
||||
static void shm_remove(int id, long key) {
|
||||
char name[32];
|
||||
if (key != IPC_PRIVATE) {
|
||||
// only when the key still names this segment
|
||||
char text[32];
|
||||
snprintf(name, sizeof(name), "key-%lx", key);
|
||||
if (shm_read_small(name, text, sizeof(text)) > 0 && atoi(text) == id) shm_unlink_file(name);
|
||||
}
|
||||
snprintf(name, sizeof(name), "seg-%d", id);
|
||||
shm_unlink_file(name);
|
||||
snprintf(name, sizeof(name), "id-%d", id);
|
||||
shm_unlink_file(name);
|
||||
}
|
||||
|
||||
static int shm_mark_attached(int id, int attached) {
|
||||
char name[48];
|
||||
snprintf(name, sizeof(name), "att-%d-%d", id, (int)getpid());
|
||||
if (!attached) {
|
||||
shm_unlink_file(name);
|
||||
return 0;
|
||||
}
|
||||
int fd = shm_open_file(name, O_WRONLY | O_CREAT, 0600);
|
||||
if (fd < 0) return -1;
|
||||
syscall(SYS_close, fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The collector holds this exclusively while it scans and removes, shmat() shared while it checks a
|
||||
// segment, maps it and marks itself, so that no segment is collected while it is being attached.
|
||||
static int shm_lock_dir(int op) {
|
||||
int fd = shm_open_file(".lock", O_RDWR | O_CREAT, 0600);
|
||||
if (fd < 0) return -1;
|
||||
long rc;
|
||||
do {
|
||||
rc = syscall(SYS_flock, fd, op);
|
||||
} while (rc != 0 && errno == EINTR);
|
||||
if (rc != 0) {
|
||||
syscall(SYS_close, fd);
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
static void shm_unlock_dir(int fd) {
|
||||
if (fd >= 0) syscall(SYS_close, fd);
|
||||
}
|
||||
|
||||
// Deletes the marks of attached processes that have ended. Every segment stays until IPC_RMID, as on
|
||||
// Linux: a private one's maker can hand its id to a process that attaches after the maker has ended.
|
||||
static void shm_collect(void) {
|
||||
char path[PATH_MAX];
|
||||
if (shm_path(path, sizeof(path), NULL) != 0) return;
|
||||
int lock = shm_lock_dir(LOCK_EX);
|
||||
if (lock < 0) return;
|
||||
int dfd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
||||
if (dfd < 0) {
|
||||
shm_unlock_dir(lock);
|
||||
return;
|
||||
}
|
||||
char dents[4096];
|
||||
for (;;) {
|
||||
long n = syscall(SYS_getdents64, dfd, dents, sizeof(dents));
|
||||
if (n <= 0) break;
|
||||
for (long at = 0; at < n;) {
|
||||
unsigned short reclen;
|
||||
memcpy(&reclen, dents + at + 16, sizeof(reclen));
|
||||
if (reclen == 0) break;
|
||||
const char *entry = dents + at + 19;
|
||||
int id = 0, pid = 0;
|
||||
if (strncmp(entry, "att-", 4) == 0 && sscanf(entry + 4, "%d-%d", &id, &pid) == 2 && !shm_process_alive(pid))
|
||||
shm_unlink_file(entry);
|
||||
at += reclen;
|
||||
}
|
||||
}
|
||||
syscall(SYS_close, dfd);
|
||||
shm_unlock_dir(lock);
|
||||
}
|
||||
|
||||
static int shm_create(long key, size_t size, unsigned mode) {
|
||||
// a different sequence of ids for every call, also within one second and one process
|
||||
static unsigned calls = 0;
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
unsigned seed = (unsigned)getpid() * 2654435761u ^ (unsigned)now.tv_nsec ^ (unsigned)now.tv_sec * 40503u ^
|
||||
__atomic_add_fetch(&calls, 1, __ATOMIC_RELAXED) * 0x9e3779b9u;
|
||||
for (int attempt = 0; attempt < 64; attempt++) {
|
||||
seed = seed * 1103515245u + 12345u;
|
||||
int id = (int)((seed >> 1) & 0x3fffffff);
|
||||
if (id == 0) continue;
|
||||
char name[32];
|
||||
snprintf(name, sizeof(name), "id-%d", id);
|
||||
int meta_fd = shm_open_file(name, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
||||
if (meta_fd < 0) {
|
||||
if (errno == EEXIST) continue;
|
||||
return -1;
|
||||
}
|
||||
snprintf(name, sizeof(name), "seg-%d", id);
|
||||
int seg_fd = shm_open_file(name, O_RDWR | O_CREAT | O_TRUNC, 0600);
|
||||
long page = sysconf(_SC_PAGESIZE);
|
||||
if (page <= 0) page = 4096;
|
||||
if (size > (size_t)INTPTR_MAX - (size_t)page) {
|
||||
// no file (and no mapping) can be that large
|
||||
if (seg_fd >= 0) syscall(SYS_close, seg_fd);
|
||||
syscall(SYS_close, meta_fd);
|
||||
shm_remove(id, IPC_PRIVATE);
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
size_t rounded = (size + (size_t)page - 1) / (size_t)page * (size_t)page;
|
||||
if (seg_fd < 0 || ftruncate(seg_fd, (off_t)rounded) != 0) {
|
||||
int saved = errno;
|
||||
if (seg_fd >= 0) syscall(SYS_close, seg_fd);
|
||||
syscall(SYS_close, meta_fd);
|
||||
shm_remove(id, IPC_PRIVATE);
|
||||
errno = saved == EFBIG ? EINVAL : (saved ? saved : ENOMEM);
|
||||
return -1;
|
||||
}
|
||||
syscall(SYS_close, seg_fd);
|
||||
char text[128];
|
||||
int len = snprintf(text, sizeof(text), "%d %lx %o %lu\n", getpid(), key, mode & 0777, (unsigned long)size);
|
||||
ssize_t written = (ssize_t)syscall(SYS_write, meta_fd, text, (size_t)len);
|
||||
syscall(SYS_close, meta_fd);
|
||||
if (written != len) {
|
||||
shm_remove(id, IPC_PRIVATE);
|
||||
errno = ENOSPC;
|
||||
return -1;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
errno = ENOSPC;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Writes `text` to the file `name` at once: into "new-ID" first, then renamed over it (`exclusive`: only
|
||||
// when `name` does not exist yet, else -1 with EEXIST). Readers see the old file or the whole new one.
|
||||
static int shm_publish(const char *name, int id, const char *text, size_t len, int exclusive) {
|
||||
char tmp_name[32], tmp[PATH_MAX], path[PATH_MAX];
|
||||
snprintf(tmp_name, sizeof(tmp_name), "new-%d", id);
|
||||
if (shm_path(tmp, sizeof(tmp), tmp_name) != 0 || shm_path(path, sizeof(path), name) != 0) {
|
||||
errno = ENAMETOOLONG;
|
||||
return -1;
|
||||
}
|
||||
int fd = (int)syscall(SYS_openat, AT_FDCWD, tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
|
||||
if (fd < 0) return -1;
|
||||
ssize_t written = (ssize_t)syscall(SYS_write, fd, text, len);
|
||||
syscall(SYS_close, fd);
|
||||
if (written != (ssize_t)len) {
|
||||
syscall(SYS_unlinkat, AT_FDCWD, tmp, 0);
|
||||
errno = ENOSPC;
|
||||
return -1;
|
||||
}
|
||||
long rc;
|
||||
if (exclusive) {
|
||||
rc = syscall(SYS_renameat2, AT_FDCWD, tmp, AT_FDCWD, path, 1 /* RENAME_NOREPLACE */);
|
||||
if (rc != 0 && (errno == ENOSYS || errno == EINVAL)) {
|
||||
// a kernel or file system without RENAME_NOREPLACE: claim the name, then write it
|
||||
int kfd = (int)syscall(SYS_openat, AT_FDCWD, path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
|
||||
syscall(SYS_unlinkat, AT_FDCWD, tmp, 0);
|
||||
if (kfd < 0) return -1;
|
||||
written = (ssize_t)syscall(SYS_write, kfd, text, len);
|
||||
syscall(SYS_close, kfd);
|
||||
if (written == (ssize_t)len) return 0;
|
||||
syscall(SYS_unlinkat, AT_FDCWD, path, 0);
|
||||
errno = ENOSPC;
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
rc = syscall(SYS_renameat2, AT_FDCWD, tmp, AT_FDCWD, path, 0);
|
||||
#ifdef SYS_renameat
|
||||
if (rc != 0 && errno == ENOSYS) rc = syscall(SYS_renameat, AT_FDCWD, tmp, AT_FDCWD, path);
|
||||
#endif
|
||||
}
|
||||
if (rc != 0) {
|
||||
int saved = errno;
|
||||
syscall(SYS_unlinkat, AT_FDCWD, tmp, 0);
|
||||
errno = saved;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int shmget(key_t key, size_t size, int shmflg) {
|
||||
init_ddnatv_hook();
|
||||
if (g_ddnatv_root[0] == '\0') {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
if (shm_ensure_dir() != 0) return -1;
|
||||
if ((shmflg & IPC_CREAT) || key == IPC_PRIVATE) shm_collect();
|
||||
if (key == IPC_PRIVATE) {
|
||||
if (size == 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
return shm_create(IPC_PRIVATE, size, (unsigned)shmflg);
|
||||
}
|
||||
char key_name[32];
|
||||
snprintf(key_name, sizeof(key_name), "key-%lx", (long)key);
|
||||
for (int attempt = 0; attempt < 8; attempt++) {
|
||||
char text[32];
|
||||
if (shm_read_small(key_name, text, sizeof(text)) > 0) {
|
||||
int id = atoi(text);
|
||||
struct shm_meta meta;
|
||||
if (id > 0 && shm_read_meta(id, &meta) == 0) {
|
||||
if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
|
||||
errno = EEXIST;
|
||||
return -1;
|
||||
}
|
||||
if (size > meta.size) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
shm_unlink_file(key_name); // names a segment that is gone
|
||||
}
|
||||
if (!(shmflg & IPC_CREAT)) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
if (size == 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
int id = shm_create((long)key, size, (unsigned)shmflg);
|
||||
if (id < 0) return -1;
|
||||
// The key file appears with its id in it, never empty: written under a name of its own, then
|
||||
// renamed to the key's name unless another process made the key's segment meanwhile
|
||||
int len = snprintf(text, sizeof(text), "%d\n", id);
|
||||
int published = shm_publish(key_name, id, text, (size_t)len, 1);
|
||||
if (published == 0) return id;
|
||||
int saved = errno;
|
||||
shm_remove(id, IPC_PRIVATE);
|
||||
if (saved != EEXIST) {
|
||||
errno = saved;
|
||||
return -1;
|
||||
}
|
||||
// another process made the key's segment at the same time: use that one
|
||||
}
|
||||
errno = EAGAIN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void *shmat_locked(int shmid, const void *shmaddr, int shmflg);
|
||||
|
||||
void *shmat(int shmid, const void *shmaddr, int shmflg) {
|
||||
init_ddnatv_hook();
|
||||
// without the lock the collector could remove the segment mid-attach
|
||||
int lock = g_ddnatv_root[0] != '\0' ? shm_lock_dir(LOCK_SH) : -1;
|
||||
if (lock < 0) {
|
||||
errno = EINVAL;
|
||||
return (void *)-1;
|
||||
}
|
||||
void *addr = shmat_locked(shmid, shmaddr, shmflg);
|
||||
int saved = errno;
|
||||
shm_unlock_dir(lock);
|
||||
errno = saved;
|
||||
return addr;
|
||||
}
|
||||
|
||||
static int shm_attached_here_locked(int id);
|
||||
|
||||
static void *shmat_locked(int shmid, const void *shmaddr, int shmflg) {
|
||||
struct shm_meta meta;
|
||||
if (g_ddnatv_root[0] == '\0' || shm_read_meta(shmid, &meta) != 0) {
|
||||
errno = EINVAL;
|
||||
return (void *)-1;
|
||||
}
|
||||
int readonly = (shmflg & SHM_RDONLY) != 0;
|
||||
char name[32];
|
||||
snprintf(name, sizeof(name), "seg-%d", shmid);
|
||||
int fd = shm_open_file(name, readonly ? O_RDONLY : O_RDWR, 0);
|
||||
if (fd < 0) {
|
||||
errno = EINVAL;
|
||||
return (void *)-1;
|
||||
}
|
||||
void *want = (void *)shmaddr;
|
||||
int flags = MAP_SHARED;
|
||||
if (want) {
|
||||
if (shmflg & SHM_RND) want = (void *)((uintptr_t)want & ~((uintptr_t)SHMLBA - 1));
|
||||
else if ((uintptr_t)want & ((uintptr_t)sysconf(_SC_PAGESIZE) - 1)) {
|
||||
syscall(SYS_close, fd);
|
||||
errno = EINVAL;
|
||||
return (void *)-1;
|
||||
}
|
||||
// an address in use is replaced only with SHM_REMAP, as on Linux (MAP_FIXED_NOREPLACE: a kernel
|
||||
// older than 4.17 takes it as a hint, which the check below catches)
|
||||
flags |= (shmflg & SHM_REMAP) ? MAP_FIXED : MAP_FIXED_NOREPLACE;
|
||||
}
|
||||
// a place in the table first, so that every attach can be detached
|
||||
int slot = -1;
|
||||
pthread_mutex_lock(&g_shm_lock);
|
||||
for (int i = 0; i < SHM_MAX_ATTACH; i++) {
|
||||
if (!g_shm_attach[i].addr && !g_shm_attach[i].size) {
|
||||
g_shm_attach[i].size = 1; // reserved
|
||||
slot = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&g_shm_lock);
|
||||
if (slot < 0) {
|
||||
syscall(SYS_close, fd);
|
||||
errno = EMFILE;
|
||||
return (void *)-1;
|
||||
}
|
||||
void *addr = mmap(want, meta.size, readonly ? PROT_READ : (PROT_READ | PROT_WRITE), flags, fd, 0);
|
||||
int saved = errno;
|
||||
syscall(SYS_close, fd);
|
||||
if (addr != MAP_FAILED && want && addr != want) {
|
||||
munmap(addr, meta.size);
|
||||
addr = MAP_FAILED;
|
||||
saved = EINVAL;
|
||||
}
|
||||
pthread_mutex_lock(&g_shm_lock);
|
||||
// the mark keeps the segment from being collected; without one the attach is undone
|
||||
if (addr != MAP_FAILED && shm_mark_attached(shmid, 1) != 0) {
|
||||
saved = errno;
|
||||
if (shm_attached_here_locked(shmid) == 0) shm_mark_attached(shmid, 0);
|
||||
munmap(addr, meta.size);
|
||||
addr = MAP_FAILED;
|
||||
if (saved != ENOMEM) saved = EINVAL;
|
||||
}
|
||||
if (addr == MAP_FAILED) {
|
||||
g_shm_attach[slot].size = 0;
|
||||
} else {
|
||||
g_shm_attach[slot].addr = addr;
|
||||
g_shm_attach[slot].size = meta.size;
|
||||
g_shm_attach[slot].id = shmid;
|
||||
}
|
||||
pthread_mutex_unlock(&g_shm_lock);
|
||||
if (addr == MAP_FAILED) {
|
||||
errno = saved == ENOMEM ? ENOMEM : EINVAL;
|
||||
return (void *)-1;
|
||||
}
|
||||
return addr;
|
||||
}
|
||||
|
||||
int shmdt(const void *shmaddr) {
|
||||
pthread_mutex_lock(&g_shm_lock);
|
||||
for (int i = 0; i < SHM_MAX_ATTACH; i++) {
|
||||
if (g_shm_attach[i].addr && g_shm_attach[i].addr == shmaddr) {
|
||||
void *addr = g_shm_attach[i].addr;
|
||||
size_t size = g_shm_attach[i].size;
|
||||
int id = g_shm_attach[i].id;
|
||||
g_shm_attach[i].addr = NULL;
|
||||
g_shm_attach[i].size = 0;
|
||||
// counted and unmarked under the lock, so that an attach of this process meanwhile keeps its mark
|
||||
if (shm_attached_here_locked(id) == 0) shm_mark_attached(id, 0);
|
||||
pthread_mutex_unlock(&g_shm_lock);
|
||||
munmap(addr, size);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&g_shm_lock);
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int shm_attached_here_locked(int id) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < SHM_MAX_ATTACH; i++) {
|
||||
if (g_shm_attach[i].addr && g_shm_attach[i].id == id) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
static int shm_attached_here(int id) {
|
||||
int count = 0;
|
||||
pthread_mutex_lock(&g_shm_lock);
|
||||
for (int i = 0; i < SHM_MAX_ATTACH; i++) {
|
||||
if (g_shm_attach[i].addr && g_shm_attach[i].id == id) count++;
|
||||
}
|
||||
pthread_mutex_unlock(&g_shm_lock);
|
||||
return count;
|
||||
}
|
||||
|
||||
int shmctl(int shmid, int cmd, struct shmid_ds *buf) {
|
||||
init_ddnatv_hook();
|
||||
if (g_ddnatv_root[0] == '\0') {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
switch (cmd) {
|
||||
case IPC_INFO:
|
||||
case SHM_INFO:
|
||||
if (buf) {
|
||||
// the caller's buffer is the structure of the command (struct shminfo or struct shm_info)
|
||||
memset(buf, 0, cmd == IPC_INFO ? sizeof(struct shminfo) : sizeof(struct shm_info));
|
||||
if (cmd == IPC_INFO) {
|
||||
struct shminfo *info = (struct shminfo *)buf;
|
||||
info->shmmax = sizeof(unsigned long) > 4 ? (unsigned long)1 << 40 : 0x7fffffffUL;
|
||||
info->shmmin = 1;
|
||||
info->shmmni = 4096;
|
||||
info->shmseg = 4096;
|
||||
info->shmall = (unsigned long)1 << 30;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
case SHM_LOCK:
|
||||
case SHM_UNLOCK:
|
||||
return 0;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
struct shm_meta meta;
|
||||
if (shm_read_meta(shmid, &meta) != 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
switch (cmd) {
|
||||
case IPC_STAT:
|
||||
case SHM_STAT:
|
||||
case SHM_STAT_ANY:
|
||||
if (!buf) {
|
||||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
memset(buf, 0, sizeof(*buf));
|
||||
buf->shm_perm.__key = (key_t)meta.key;
|
||||
// The owner's rights for everyone of the app: X compares the uid of its client with the owner's,
|
||||
// and the two can differ in The native userland (root emulated for one, not the other)
|
||||
unsigned owner = meta.mode & 0600;
|
||||
buf->shm_perm.mode = (unsigned short)(owner | (owner >> 3) | (owner >> 6));
|
||||
buf->shm_perm.uid = buf->shm_perm.cuid = geteuid();
|
||||
buf->shm_perm.gid = buf->shm_perm.cgid = getegid();
|
||||
buf->shm_segsz = meta.size;
|
||||
buf->shm_cpid = meta.pid;
|
||||
buf->shm_lpid = getpid();
|
||||
buf->shm_nattch = (shmatt_t)(shm_attached_here(shmid) + (meta.pid != getpid() ? 1 : 0));
|
||||
buf->shm_ctime = time(NULL);
|
||||
return cmd == IPC_STAT ? 0 : shmid;
|
||||
case IPC_SET: {
|
||||
if (!buf) {
|
||||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
// the new rights are kept (owner and group stay the app's: there is no other user to give it to)
|
||||
char name[32], text[128];
|
||||
snprintf(name, sizeof(name), "id-%d", shmid);
|
||||
int len = snprintf(text, sizeof(text), "%d %lx %o %lu\n", meta.pid, meta.key, buf->shm_perm.mode & 0777u,
|
||||
(unsigned long)meta.size);
|
||||
return shm_publish(name, shmid, text, (size_t)len, 0) == 0 ? 0 : -1;
|
||||
}
|
||||
case IPC_RMID:
|
||||
shm_remove(shmid, meta.key);
|
||||
return 0;
|
||||
default:
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* !DDNATV_HOST_TEST */
|
||||
@@ -0,0 +1,92 @@
|
||||
// Runs a program under a seccomp filter that answers the system calls Android's app policy blocks
|
||||
// with SIGSYS, the way a stock app process does, for testing on builds that have no filter:
|
||||
// seccomp-sim [-a NR,NR...] PROGRAM [ARGS...]
|
||||
// seccomp-sim -f FILTER PROGRAM [ARGS...]
|
||||
// -a leaves the listed system calls allowed (to find which one kills a program). -f installs a
|
||||
// filter as it is (raw struct sock_filter entries), such as the app filter a device's zygote makes.
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <linux/audit.h>
|
||||
#include <linux/filter.h>
|
||||
#include <linux/seccomp.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static const int blocked[] = {99, 100, 97, 217, 218, 219, 268, 277, 280, 293, 143, 144, 145, 146, 147, 149, 151, 152, 159};
|
||||
|
||||
#define MAX_ALLOWED 64
|
||||
#define NBLOCKED (sizeof(blocked) / sizeof(blocked[0]))
|
||||
|
||||
static int run_filter_file(const char *path, char **argv) {
|
||||
static struct sock_filter filter[BPF_MAXINSNS];
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) {
|
||||
perror("seccomp-sim: -f");
|
||||
return 2;
|
||||
}
|
||||
size_t n = fread(filter, sizeof(filter[0]), BPF_MAXINSNS, f);
|
||||
int extra = fgetc(f) != EOF;
|
||||
fclose(f);
|
||||
if (n == 0 || extra) {
|
||||
fprintf(stderr, "seccomp-sim: %s holds no filter of at most %d instructions\n", path, BPF_MAXINSNS);
|
||||
return 2;
|
||||
}
|
||||
struct sock_fprog prog = { .len = (unsigned short)n, .filter = filter };
|
||||
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
|
||||
perror("seccomp-sim");
|
||||
return 1;
|
||||
}
|
||||
execv(argv[0], argv);
|
||||
perror("seccomp-sim: exec");
|
||||
return 127;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc > 3 && strcmp(argv[1], "-f") == 0) return run_filter_file(argv[2], argv + 3);
|
||||
int skip[MAX_ALLOWED], nskip = 0, arg = 1;
|
||||
if (argc > 3 && strcmp(argv[1], "-a") == 0) {
|
||||
for (char *p = argv[2];; p++) {
|
||||
char *end = NULL;
|
||||
long nr = strtol(p, &end, 10);
|
||||
if (end == p || (*end != '\0' && *end != ',') || *p < '0' || *p > '9' ||
|
||||
nr < 0 || nr > 1023 || nskip == MAX_ALLOWED) {
|
||||
fprintf(stderr, "seccomp-sim: -a takes up to %d comma-separated system call numbers, not \"%s\"\n", MAX_ALLOWED, argv[2]);
|
||||
return 2;
|
||||
}
|
||||
skip[nskip++] = (int)nr;
|
||||
if (*end == '\0') break;
|
||||
p = end;
|
||||
}
|
||||
arg = 3;
|
||||
}
|
||||
if (arg >= argc) return 2;
|
||||
struct sock_filter filter[4 + 2 * MAX_ALLOWED + 2 + 2 * NBLOCKED + 1];
|
||||
int n = 0;
|
||||
filter[n++] = (struct sock_filter)BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, arch));
|
||||
filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AUDIT_ARCH_AARCH64, 1, 0);
|
||||
filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW);
|
||||
filter[n++] = (struct sock_filter)BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr));
|
||||
for (int i = 0; i < nskip; i++) {
|
||||
filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, skip[i], 0, 1);
|
||||
filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW);
|
||||
}
|
||||
filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 424, 0, 1);
|
||||
filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP);
|
||||
for (size_t i = 0; i < NBLOCKED; i++) {
|
||||
filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, blocked[i], 0, 1);
|
||||
filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP);
|
||||
}
|
||||
filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW);
|
||||
struct sock_fprog prog = { .len = (unsigned short)n, .filter = filter };
|
||||
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
|
||||
perror("seccomp-sim");
|
||||
return 1;
|
||||
}
|
||||
execv(argv[arg], argv + arg);
|
||||
perror("seccomp-sim: exec");
|
||||
return 127;
|
||||
}
|
||||
@@ -77,6 +77,7 @@ pack tracking "$work/tracking" hand_landmarker.task hand_landmarker_lite.task po
|
||||
pack xbox "$linuxfs/droiddeck-xbox" version xodus-cli xodus-service
|
||||
pack comet "$linuxfs/droiddeck-comet" version comet
|
||||
pack powervr "$repo/app/src/main/assets/hybris" runtime.tzst version.txt
|
||||
[ ! -f "$linuxfs/droiddeck-ubisoft/droiddeck-upc.exe" ] || pack ubisoft "$linuxfs/droiddeck-ubisoft" droiddeck-upc.exe
|
||||
|
||||
mkdir "$work/butler"
|
||||
fetch "https://broth.itch.zone/butler/linux-arm64/${BUTLER_VERSION}/archive/default" \
|
||||
|
||||
@@ -162,9 +162,26 @@ Prototype (inert unless `PROOT_FASTPATH` is set; see `docs/development/proot-per
|
||||
`ENOSYS`, because its arguments are host paths; the fast path then stops for that process and
|
||||
repeats the call through a plain `svc`, which the guest's filter traps with the guest path.
|
||||
`bench/guest-trap.c` installs a tagged `openat` trap and checks its handler answers the call.
|
||||
The kernel and filesystems also return `ENOSYS` themselves: Android's FUSE daemon has no
|
||||
`symlink`, so `symlinkat` on a storage card fails with it. The fast path therefore asks again
|
||||
with the same call number and invalid arguments. A trap answers `ENOSYS` again and the fast path
|
||||
stops for the process; any other error means the call reached the kernel, so only that one call
|
||||
is repeated through a plain `svc` and the fast path stays on.
|
||||
|
||||
Old kernel fixes:
|
||||
|
||||
- `0028-fchdir-open-directory.patch` - `fchdir` resolved the directory again from its guest path,
|
||||
one `lstat` per path component, then an `lstat` and an `O_PATH` open of the result. The
|
||||
descriptor is already open, so `/proc/<pid>/fd/<n>` gives its host path and one `stat` through
|
||||
that link confirms it is a live directory. The guest path it maps to is checked to map back to
|
||||
the same host path, and the parent of the binding it falls under is checked to still resolve
|
||||
to itself, so the cwd PRoot records is the one the old walk produced; a deleted directory, a
|
||||
path outside the guest, a binding whose ancestor became a symlink, or anything else that does
|
||||
not round-trip takes the old walk. Listing a directory in Wine does `fchdir` there and back: on a memory card behind
|
||||
Android's FUSE daemon the old walk cost about 4 ms per call in the single tracer thread, 8.4 s
|
||||
of an 8.8 s tracer total across one program start, and is now under 0.1 ms.
|
||||
`bench/fchdir.py [directory...]` checks the cwd after `fchdir` to plain, bound, symlinked,
|
||||
renamed and deleted directories and prints the per-call cost.
|
||||
- `0015-lost-fork-events.patch` - works around a problem reported on Xiaomi's msm 4.14 kernel:
|
||||
the kernel sometimes reports a new process or thread but gives its ID as zero. Without that ID,
|
||||
PRoot cannot finish setting it up, which can leave the session stuck. The patch searches `/proc`
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
import errno
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
|
||||
def report(label, check):
|
||||
try:
|
||||
print(label, check())
|
||||
except OSError as error:
|
||||
print(label, "ERR", errno.errorcode.get(error.errno, error.errno))
|
||||
|
||||
|
||||
def enter(path):
|
||||
fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY)
|
||||
try:
|
||||
os.fchdir(fd)
|
||||
return os.getcwd(), os.readlink("/proc/self/cwd"), os.path.samefile(".", path)
|
||||
finally:
|
||||
os.close(fd)
|
||||
|
||||
|
||||
def enter_file(base):
|
||||
fd = os.open(base + "/real/file", os.O_RDONLY)
|
||||
try:
|
||||
os.fchdir(fd)
|
||||
finally:
|
||||
os.close(fd)
|
||||
|
||||
|
||||
def enter_deleted(base):
|
||||
os.mkdir(base + "/gone")
|
||||
fd = os.open(base + "/gone", os.O_RDONLY)
|
||||
os.rmdir(base + "/gone")
|
||||
try:
|
||||
os.fchdir(fd)
|
||||
return "ok"
|
||||
finally:
|
||||
os.close(fd)
|
||||
os.chdir("/")
|
||||
|
||||
|
||||
def enter_renamed(base):
|
||||
os.mkdir(base + "/before")
|
||||
fd = os.open(base + "/before", os.O_RDONLY)
|
||||
os.rename(base + "/before", base + "/after")
|
||||
try:
|
||||
os.fchdir(fd)
|
||||
return os.getcwd() == base + "/after"
|
||||
finally:
|
||||
os.close(fd)
|
||||
os.chdir("/")
|
||||
os.rmdir(base + "/after")
|
||||
|
||||
|
||||
def relative(base):
|
||||
fd = os.open(base, os.O_RDONLY)
|
||||
os.fchdir(fd)
|
||||
os.close(fd)
|
||||
seen = os.path.exists("real/file"), os.path.exists("link/file")
|
||||
os.chdir("real")
|
||||
inside = os.getcwd() == base + "/real"
|
||||
os.chdir("..")
|
||||
return seen + (inside, os.getcwd() == base)
|
||||
|
||||
|
||||
def cost(path, rounds=300):
|
||||
fd = os.open(path, os.O_RDONLY)
|
||||
back = os.open("/", os.O_RDONLY)
|
||||
start = time.perf_counter()
|
||||
for _ in range(rounds):
|
||||
os.fchdir(fd)
|
||||
os.fchdir(back)
|
||||
elapsed = time.perf_counter() - start
|
||||
os.close(fd)
|
||||
os.close(back)
|
||||
return round(elapsed / rounds / 2 * 1e6, 1)
|
||||
|
||||
|
||||
def main():
|
||||
extra = sys.argv[1:]
|
||||
base = tempfile.mkdtemp(prefix="fchdir-")
|
||||
os.mkdir(base + "/real")
|
||||
os.symlink(base + "/real", base + "/link")
|
||||
os.mkdir(base + "/dot.")
|
||||
open(base + "/real/file", "w").close()
|
||||
for path in ["/", "/tmp", "/usr/lib", "/proc/self", "/dev", base + "/real", base + "/link", base + "/dot."] + extra:
|
||||
report(path, lambda: enter(path))
|
||||
report("file", lambda: enter_file(base))
|
||||
report("deleted", lambda: enter_deleted(base))
|
||||
report("renamed", lambda: enter_renamed(base))
|
||||
report("badfd", lambda: os.fchdir(987))
|
||||
report("relative", lambda: relative(base))
|
||||
os.chdir("/")
|
||||
for path in ["/usr/lib"] + extra:
|
||||
print("fchdir_us", path, cost(path))
|
||||
os.unlink(base + "/real/file")
|
||||
os.unlink(base + "/link")
|
||||
os.rmdir(base + "/dot.")
|
||||
os.rmdir(base + "/real")
|
||||
os.rmdir(base)
|
||||
|
||||
|
||||
main()
|
||||
+243
-25
@@ -41,7 +41,7 @@ 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; int dropped; };
|
||||
struct bind { char *host; size_t hlen; char *guest; size_t glen; int canon; int dropped; int fuse; };
|
||||
static struct bind binds[MAX_BINDS];
|
||||
static int nbinds;
|
||||
static char root[PATH_MAX];
|
||||
@@ -59,10 +59,10 @@ static int optional_syscall(long nr) {
|
||||
nr == SYS_openat2 || nr == SYS_renameat2;
|
||||
}
|
||||
static long sc(long nr, long a, long b, long c, long d, long e) {
|
||||
if (fp_refused) return -ENOSYS;
|
||||
if (fp_refused && nr != SYS_close) return -ENOSYS;
|
||||
long r = fp_sys(nr, a, b, c, d, e, 0);
|
||||
if (r == -ENOSYS && !optional_syscall(nr)) {
|
||||
__atomic_store_n(&fp_on, 0, __ATOMIC_RELAXED);
|
||||
if (fp_sys(nr, -1, -1, -1, -1, -1, 0) == -ENOSYS) __atomic_store_n(&fp_on, 0, __ATOMIC_RELAXED);
|
||||
fp_refused = 1;
|
||||
}
|
||||
return r;
|
||||
@@ -133,7 +133,8 @@ static long tracer;
|
||||
static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static pthread_mutex_t bind_lock;
|
||||
static pthread_mutex_t long_lock;
|
||||
static void after_fork(void) { pthread_mutex_init(&cache_lock, NULL); pthread_mutex_init(&bind_lock, NULL); pthread_mutex_init(&long_lock, NULL); }
|
||||
static pthread_mutex_t dir_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static void after_fork(void) { pthread_mutex_init(&cache_lock, NULL); pthread_mutex_init(&bind_lock, NULL); pthread_mutex_init(&long_lock, NULL); pthread_mutex_init(&dir_lock, NULL); }
|
||||
|
||||
static int traced_by_match(const char *key) {
|
||||
static char buf[32768];
|
||||
@@ -414,6 +415,212 @@ static int host_bind(const char *h) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
static unsigned hash_of(const char *s);
|
||||
static long long now_ns(void);
|
||||
#define DIRS 64
|
||||
#define DIR_FD_FLOOR 1000
|
||||
struct dirslot { unsigned hash, epoch; int fd, refs; dev_t dev; ino_t ino; long tag; long long stamp; char path[PATH_MAX]; char seen[PATH_MAX]; };
|
||||
static struct dirslot dirs[DIRS];
|
||||
static unsigned dir_epoch;
|
||||
struct near { long fd; int slot, own; const char *name; };
|
||||
|
||||
static int fuse_bind(const char *h) {
|
||||
int i = host_bind(h);
|
||||
if (i < 0) return -1;
|
||||
int f = __atomic_load_n(&binds[i].fuse, __ATOMIC_ACQUIRE);
|
||||
if (!f) {
|
||||
struct statfs fs;
|
||||
f = sc(SYS_statfs, (long)binds[i].host, (long)&fs, 0, 0, 0) == 0 && fs.f_type == 0x65735546 ? 1 : 2;
|
||||
__atomic_store_n(&binds[i].fuse, f, __ATOMIC_RELEASE);
|
||||
}
|
||||
return f == 1 ? i : -1;
|
||||
}
|
||||
|
||||
static long fd_path(long fd, char *out) {
|
||||
char link[40];
|
||||
snprintf(link, sizeof link, "/proc/self/fd/%ld", fd);
|
||||
long n = sc(SYS_readlinkat, AT_FDCWD, (long)link, (long)out, PATH_MAX - 1, 0);
|
||||
if (n <= 0 || n >= PATH_MAX - 1) return -1;
|
||||
out[n] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int fd_id(long fd, dev_t *dev, ino_t *ino) {
|
||||
struct stat st;
|
||||
if (sc(SYS_newfstatat, fd, (long)"", (long)&st, AT_EMPTY_PATH, 0) != 0) return -1;
|
||||
*dev = st.st_dev;
|
||||
*ino = st.st_ino;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static long dir_tag(void) {
|
||||
static uint64_t state;
|
||||
uint64_t z = __atomic_add_fetch(&state, 0x9e3779b97f4a7c15ULL, __ATOMIC_RELAXED), seed = 0;
|
||||
const void *random = (const void *)getauxval(AT_RANDOM);
|
||||
if (random) memcpy(&seed, random, sizeof seed);
|
||||
z += seed;
|
||||
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
|
||||
z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
|
||||
return (long)((z ^ (z >> 31)) & 0xffffffffffULL) + 1;
|
||||
}
|
||||
|
||||
static int dir_owned(const struct dirslot *e) {
|
||||
dev_t dev;
|
||||
ino_t ino;
|
||||
return e->fd > 0 && sc(SYS_lseek, e->fd, 0, SEEK_CUR, 0, 0) == e->tag
|
||||
&& fd_id(e->fd, &dev, &ino) == 0 && dev == e->dev && ino == e->ino;
|
||||
}
|
||||
|
||||
static int dir_fresh(struct dirslot *e, const char *dir) {
|
||||
long long t = now_ns();
|
||||
if (t - __atomic_load_n(&e->stamp, __ATOMIC_RELAXED) < ttl_ns) return 1;
|
||||
struct stat st;
|
||||
if (sc(SYS_newfstatat, AT_FDCWD, (long)dir, (long)&st, 0, 0) != 0 || st.st_dev != e->dev || st.st_ino != e->ino) {
|
||||
__atomic_add_fetch(&dir_epoch, 1, __ATOMIC_RELEASE);
|
||||
return 0;
|
||||
}
|
||||
__atomic_store_n(&e->stamp, t, __ATOMIC_RELAXED);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void dirs_drop(const char *host) {
|
||||
size_t len = strlen(host);
|
||||
if (pthread_mutex_trylock(&dir_lock) != 0) {
|
||||
__atomic_add_fetch(&dir_epoch, 1, __ATOMIC_RELEASE);
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < DIRS; i++)
|
||||
if (dirs[i].path[0] && under(dirs[i].path, host, len)) dirs[i].path[0] = 0;
|
||||
pthread_mutex_unlock(&dir_lock);
|
||||
}
|
||||
|
||||
static void near_put(struct near *n) {
|
||||
if (n->own) sc(SYS_close, n->fd, 0, 0, 0, 0);
|
||||
else if (n->slot >= 0) __atomic_sub_fetch(&dirs[n->slot].refs, 1, __ATOMIC_RELEASE);
|
||||
n->fd = AT_FDCWD;
|
||||
n->slot = -1;
|
||||
n->own = 0;
|
||||
}
|
||||
|
||||
static int dir_cached(const char *dir, struct near *n) {
|
||||
unsigned h = hash_of(dir);
|
||||
struct dirslot *e = &dirs[h % DIRS];
|
||||
if (pthread_mutex_trylock(&dir_lock) != 0) return 0;
|
||||
int hit = e->fd >= 0 && e->hash == h && e->path[0] && strcmp(e->path, dir) == 0
|
||||
&& e->epoch == __atomic_load_n(&dir_epoch, __ATOMIC_ACQUIRE);
|
||||
if (hit) __atomic_add_fetch(&e->refs, 1, __ATOMIC_ACQUIRE);
|
||||
pthread_mutex_unlock(&dir_lock);
|
||||
if (!hit) return 0;
|
||||
char now[PATH_MAX];
|
||||
if (fd_path(e->fd, now) == 0 && strcmp(now, e->seen) == 0 && dir_fresh(e, dir)) {
|
||||
n->fd = e->fd;
|
||||
n->slot = (int)(e - dirs);
|
||||
n->own = 0;
|
||||
return 1;
|
||||
}
|
||||
__atomic_sub_fetch(&e->refs, 1, __ATOMIC_RELEASE);
|
||||
if (pthread_mutex_trylock(&dir_lock) == 0) {
|
||||
if (e->hash == h && strcmp(e->path, dir) == 0) e->path[0] = 0;
|
||||
pthread_mutex_unlock(&dir_lock);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dir_keep(const char *dir, long fd, struct near *n) {
|
||||
long high = sc(SYS_fcntl, fd, F_DUPFD_CLOEXEC, DIR_FD_FLOOR, 0, 0);
|
||||
sc(SYS_close, fd, 0, 0, 0, 0);
|
||||
if (high < 0) return -1;
|
||||
n->fd = high;
|
||||
n->slot = -1;
|
||||
n->own = 1;
|
||||
char seen[PATH_MAX];
|
||||
dev_t dev;
|
||||
ino_t ino;
|
||||
long tag = dir_tag();
|
||||
struct stat st;
|
||||
if (fd_id(high, &dev, &ino) < 0 || strlen(dir) >= PATH_MAX
|
||||
|| sc(SYS_newfstatat, AT_FDCWD, (long)dir, (long)&st, 0, 0) != 0 || st.st_dev != dev || st.st_ino != ino) {
|
||||
near_put(n);
|
||||
return -1;
|
||||
}
|
||||
if (fd_path(high, seen) < 0 || sc(SYS_lseek, high, tag, SEEK_SET, 0, 0) != tag) return 0;
|
||||
unsigned h = hash_of(dir);
|
||||
struct dirslot *e = &dirs[h % DIRS];
|
||||
if (pthread_mutex_trylock(&dir_lock) != 0) return 0;
|
||||
if (__atomic_load_n(&e->refs, __ATOMIC_ACQUIRE) == 0) {
|
||||
if (dir_owned(e)) sc(SYS_close, e->fd, 0, 0, 0, 0);
|
||||
e->hash = h;
|
||||
e->epoch = __atomic_load_n(&dir_epoch, __ATOMIC_ACQUIRE);
|
||||
e->tag = tag;
|
||||
e->stamp = now_ns();
|
||||
e->fd = (int)high;
|
||||
e->dev = dev;
|
||||
e->ino = ino;
|
||||
e->refs = 1;
|
||||
strcpy(e->path, dir);
|
||||
strcpy(e->seen, seen);
|
||||
n->slot = (int)(e - dirs);
|
||||
n->own = 0;
|
||||
}
|
||||
pthread_mutex_unlock(&dir_lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dir_get(char *dir, size_t floor, struct near *n) {
|
||||
size_t len = strlen(dir), at = len;
|
||||
while (!dir_cached(dir, n)) {
|
||||
if (at <= floor) {
|
||||
long fd = sc(SYS_openat, AT_FDCWD, (long)dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0, 0);
|
||||
if (fd < 0 || dir_keep(dir, fd, n) < 0) return -1;
|
||||
break;
|
||||
}
|
||||
char *slash = strrchr(dir, '/');
|
||||
if (!slash || (size_t)(slash - dir) < floor) return -1;
|
||||
*slash = 0;
|
||||
at = slash - dir;
|
||||
}
|
||||
while (at < len) {
|
||||
dir[at] = '/';
|
||||
const char *name = dir + at + 1;
|
||||
size_t next = at + 1 + strlen(name);
|
||||
long fd = sc(SYS_openat, n->fd, (long)name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC, 0, 0);
|
||||
near_put(n);
|
||||
if (fd < 0 || dir_keep(dir, fd, n) < 0) return -1;
|
||||
at = next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static long near_get(const char *host, struct near *n) {
|
||||
n->fd = AT_FDCWD;
|
||||
n->slot = -1;
|
||||
n->own = 0;
|
||||
n->name = host;
|
||||
if (ttl_ns <= 0 || host[0] != '/') return n->fd;
|
||||
int i = fuse_bind(host);
|
||||
if (i < 0) return n->fd;
|
||||
const char *slash = strrchr(host, '/');
|
||||
size_t plen = slash - host;
|
||||
if (plen < binds[i].hlen || !slash[1] || plen >= PATH_MAX) return n->fd;
|
||||
char dir[PATH_MAX];
|
||||
memcpy(dir, host, plen);
|
||||
dir[plen] = 0;
|
||||
if (dir_get(dir, binds[i].hlen, n) < 0) {
|
||||
near_put(n);
|
||||
return n->fd;
|
||||
}
|
||||
n->name = slash + 1;
|
||||
return n->fd;
|
||||
}
|
||||
|
||||
static long hsc(long nr, const char *host, long a, long b, long c) {
|
||||
struct near n;
|
||||
long fd = near_get(host, &n);
|
||||
long rs = sc(nr, fd, (long)n.name, a, b, c);
|
||||
near_put(&n);
|
||||
return rs;
|
||||
}
|
||||
|
||||
static int guest_text(const char *h, char *g) {
|
||||
if (h[0] == '/' && under(h, root, rootlen)) return to_guest(h, g, PATH_MAX);
|
||||
int i = h[0] == '/' ? host_bind(h) : -1;
|
||||
@@ -556,7 +763,7 @@ static long proc_readlink(const struct res *r, char *text, size_t size, int *cha
|
||||
return name[0] == 'e' ? own_exe(text) : own_cwd(text);
|
||||
}
|
||||
char h[PATH_MAX];
|
||||
long t = sc(SYS_readlinkat, AT_FDCWD, (long)r->host, (long)h, sizeof h - 1, 0);
|
||||
long t = hsc(SYS_readlinkat, 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;
|
||||
@@ -579,7 +786,7 @@ static long proc_link(struct res *r, char *text, int final) {
|
||||
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);
|
||||
long t = hsc(SYS_readlinkat, r->host, (long)text, PATH_MAX - 1, 0);
|
||||
if (t <= 0 || t >= PATH_MAX - 1) return FP_SLOW;
|
||||
text[t] = 0;
|
||||
return t;
|
||||
@@ -628,7 +835,7 @@ static long walk(struct res *r, const char *text, int follow, int last) {
|
||||
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);
|
||||
long s = hsc(SYS_newfstatat, r->host, (long)&r->st, AT_SYMLINK_NOFOLLOW, 0);
|
||||
if (s < 0) {
|
||||
int glue = glue_above_binding(r->guest, glen + n);
|
||||
if (glue < 0) return FP_SLOW;
|
||||
@@ -648,13 +855,13 @@ static long walk(struct res *r, const char *text, int follow, int last) {
|
||||
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;
|
||||
if (hsc(SYS_newfstatat, 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);
|
||||
t = hsc(SYS_readlinkat, 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;
|
||||
@@ -676,7 +883,7 @@ static long walk(struct res *r, const char *text, int follow, int last) {
|
||||
}
|
||||
if (!stated) {
|
||||
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);
|
||||
long s = hsc(SYS_newfstatat, 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;
|
||||
}
|
||||
@@ -699,7 +906,7 @@ static long bound_literal(const char *literal, struct res *r) {
|
||||
struct bind *bd;
|
||||
if (reserved(literal) || bind_of(literal, &bd) < 0 || !bd || bd->glen != strlen(literal) || bd->glen < 2) return FP_SLOW;
|
||||
if (snprintf(r->host, PATH_MAX, "%s", bd->host) >= PATH_MAX) return FP_SLOW;
|
||||
if (sc(SYS_newfstatat, AT_FDCWD, (long)r->host, (long)&r->st, AT_SYMLINK_NOFOLLOW, 0) < 0 || S_ISLNK(r->st.st_mode)) return FP_SLOW;
|
||||
if (hsc(SYS_newfstatat, r->host, (long)&r->st, AT_SYMLINK_NOFOLLOW, 0) < 0 || S_ISLNK(r->st.st_mode)) return FP_SLOW;
|
||||
snprintf(r->guest, PATH_MAX, "%s", literal);
|
||||
r->exists = 1;
|
||||
r->moving = 0;
|
||||
@@ -822,8 +1029,8 @@ static long fp_open(int dirfd, const char *path, int flags, mode_t mode) {
|
||||
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);
|
||||
return sc(SYS_openat, AT_FDCWD, (long)r.host, flags | (follow && !r.magic ? O_NOFOLLOW : 0), (flags & O_CREAT) ? mode : 0, 0);
|
||||
if (rs == UNSEEN) return hsc(SYS_openat, r.host, flags, (flags & O_CREAT) ? mode : 0, 0);
|
||||
return hsc(SYS_openat, 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, ...)) {
|
||||
@@ -864,7 +1071,7 @@ static long fp_stat(int dirfd, const char *path, int flags, struct stat *st) {
|
||||
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);
|
||||
if (rs == UNSEEN) return hsc(SYS_newfstatat, r.host, (long)st, flags, 0);
|
||||
*st = r.st;
|
||||
return 0;
|
||||
}
|
||||
@@ -1007,7 +1214,7 @@ static long fp_access(int dirfd, const char *path, int mode, int flags) {
|
||||
? 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 == 0 || rs == UNSEEN) rs = hsc(SYS_faccessat, r.host, mode, 0, 0);
|
||||
return rs;
|
||||
}
|
||||
|
||||
@@ -1030,7 +1237,7 @@ static long link_text(const struct res *r, char *text, size_t size, int *changed
|
||||
*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);
|
||||
long t = hsc(SYS_readlinkat, 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;
|
||||
@@ -1135,7 +1342,7 @@ char *__getcwd_chk(char *buf, size_t size, size_t room) {
|
||||
static long fp_mkdir(int dirfd, const char *path, mode_t mode) {
|
||||
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);
|
||||
if (rs == 0 || rs == 1 || rs == UNSEEN) rs = hsc(SYS_mkdirat, r.host, mode, 0, 0);
|
||||
return rs;
|
||||
}
|
||||
int mkdirat(int dirfd, const char *path, mode_t mode) {
|
||||
@@ -1172,7 +1379,7 @@ static long fp_unlink(int dirfd, const char *path, int flags) {
|
||||
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);
|
||||
rs = hsc(SYS_unlinkat, r.host, flags, 0, 0);
|
||||
if (rs == 0 && changes_paths) forget();
|
||||
}
|
||||
return rs;
|
||||
@@ -1196,9 +1403,14 @@ static long fp_rename(int olddirfd, const char *old, int newdirfd, const char *n
|
||||
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();
|
||||
struct near a, b;
|
||||
near_get(from.host, &a);
|
||||
near_get(to.host, &b);
|
||||
rs = flags ? sc(SYS_renameat2, a.fd, (long)a.name, b.fd, (long)b.name, flags)
|
||||
: sc(SYS_renameat, a.fd, (long)a.name, b.fd, (long)b.name, 0);
|
||||
near_put(&a);
|
||||
near_put(&b);
|
||||
if (rs == 0 && (S_ISLNK(from.st.st_mode) || (rt == 0 && S_ISLNK(to.st.st_mode)))) forget();
|
||||
}
|
||||
}
|
||||
return rs;
|
||||
@@ -1236,7 +1448,10 @@ static long fp_chmod(int dirfd, const char *path, mode_t mode, int flags) {
|
||||
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);
|
||||
if (rs == 0 || rs == UNSEEN) {
|
||||
rs = hsc(SYS_fchmodat, r.host, mode, 0, 0);
|
||||
if (rs == 0) dirs_drop(r.host);
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
int fchmodat(int dirfd, const char *path, mode_t mode, int flags) {
|
||||
@@ -1249,7 +1464,10 @@ static long fp_chown(int dirfd, const char *path, uid_t uid, gid_t gid, int flag
|
||||
struct res r;
|
||||
long 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_fchownat, AT_FDCWD, (long)r.host, (long)uid, (long)gid, flags);
|
||||
if (rs == 0 || rs == UNSEEN) {
|
||||
rs = hsc(SYS_fchownat, r.host, (long)uid, (long)gid, flags);
|
||||
if (rs == 0) dirs_drop(r.host);
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
int fchownat(int dirfd, const char *path, uid_t uid, gid_t gid, int flags) {
|
||||
@@ -1267,7 +1485,7 @@ static long fp_utimens(int dirfd, const char *path, const struct timespec times[
|
||||
} 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);
|
||||
if (rs == 0 || rs == UNSEEN) rs = hsc(SYS_utimensat, r.host, (long)times, flags, 0);
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
@@ -1509,7 +1727,7 @@ static long socket_address(const struct sockaddr *addr, socklen_t len, int follo
|
||||
char *slash = strrchr(r.host, '/');
|
||||
if (!slash || slash == r.host) return FP_SLOW;
|
||||
*slash = 0;
|
||||
*dir = sc(SYS_openat, AT_FDCWD, (long)r.host, O_PATH | O_DIRECTORY | O_CLOEXEC, 0, 0);
|
||||
*dir = hsc(SYS_openat, r.host, O_PATH | O_DIRECTORY | O_CLOEXEC, 0, 0);
|
||||
if (*dir < 0) return FP_SLOW;
|
||||
hlen = snprintf(out->sun_path, room, "/proc/self/fd/%ld/%s", *dir, slash + 1);
|
||||
if (hlen >= room) { sc(SYS_close, *dir, 0, 0, 0, 0); *dir = -1; return FP_SLOW; }
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: maxjivi05 <maxjividen@gmail.com>
|
||||
Date: Thu, 12 Nov 2026 22:00:00 -0500
|
||||
Subject: [PATCH] fchdir: take the path of the open directory instead of resolving
|
||||
it again component by component
|
||||
|
||||
---
|
||||
--- a/src/syscall/enter.c
|
||||
+++ b/src/syscall/enter.c
|
||||
@@ -46,8 +46,66 @@
|
||||
#include "tracee/abi.h"
|
||||
#include "path/path.h"
|
||||
#include "path/canon.h"
|
||||
+#include "path/binding.h"
|
||||
#include "arch.h"
|
||||
|
||||
+static int translate_open_directory(Tracee *tracee, char path[PATH_MAX], int fd, struct stat *statl)
|
||||
+{
|
||||
+ char guest[PATH_MAX];
|
||||
+ char mount[PATH_MAX];
|
||||
+ char parent[PATH_MAX];
|
||||
+ char link[32];
|
||||
+ char *slash;
|
||||
+ const Binding *binding;
|
||||
+ size_t length;
|
||||
+ int status;
|
||||
+
|
||||
+ status = readlink_proc_pid_fd(tracee->pid, fd, path);
|
||||
+ if (status < 0 || path[0] != '/')
|
||||
+ return 1;
|
||||
+
|
||||
+ strcpy(guest, path);
|
||||
+ status = detranslate_path(tracee, guest, NULL);
|
||||
+ if (status < 0)
|
||||
+ return 1;
|
||||
+
|
||||
+ binding = get_binding(tracee, GUEST, guest);
|
||||
+ if (binding == NULL)
|
||||
+ return 1;
|
||||
+
|
||||
+ strcpy(parent, binding->guest.path);
|
||||
+ slash = strrchr(parent, '/');
|
||||
+ if (slash != NULL && slash != parent) {
|
||||
+ if ((size_t) (slash - parent) + 3 > PATH_MAX)
|
||||
+ return 1;
|
||||
+
|
||||
+ strcpy(slash, "/.");
|
||||
+ strcpy(mount, "/");
|
||||
+ status = canonicalize(tracee, parent, true, mount, 0);
|
||||
+ if (status < 0 || strcmp(mount, parent) != 0)
|
||||
+ return 1;
|
||||
+ }
|
||||
+
|
||||
+ status = substitute_binding(tracee, GUEST, guest);
|
||||
+ if (status < 0 || strcmp(guest, path) != 0)
|
||||
+ return 1;
|
||||
+
|
||||
+ status = snprintf(link, sizeof(link), "/proc/%d/fd/%d", tracee->pid, fd);
|
||||
+ if (status < 0 || (size_t) status >= sizeof(link))
|
||||
+ return 1;
|
||||
+
|
||||
+ status = stat(link, statl);
|
||||
+ if (status < 0 || !S_ISDIR(statl->st_mode) || statl->st_nlink == 0)
|
||||
+ return 1;
|
||||
+
|
||||
+ length = strlen(path);
|
||||
+ if (length + 2 >= PATH_MAX)
|
||||
+ return 1;
|
||||
+
|
||||
+ strcpy(path + (length == 1 ? 0 : length), "/.");
|
||||
+ return 0;
|
||||
+}
|
||||
+
|
||||
/**
|
||||
* Translate @path and put the result in the @tracee's memory address
|
||||
* space pointed to by the @reg argument of the current syscall. See
|
||||
@@ -193,13 +251,16 @@
|
||||
dirfd = peek_reg(tracee, CURRENT, SYSARG_1);
|
||||
}
|
||||
|
||||
- status = translate_path(tracee, path, dirfd, oldpath, true);
|
||||
- if (status < 0)
|
||||
- break;
|
||||
+ if (syscall_number == PR_chdir
|
||||
+ || translate_open_directory(tracee, path, dirfd, &statl) != 0) {
|
||||
+ status = translate_path(tracee, path, dirfd, oldpath, true);
|
||||
+ if (status < 0)
|
||||
+ break;
|
||||
|
||||
- status = lstat(path, &statl);
|
||||
- if (status < 0)
|
||||
- break;
|
||||
+ status = lstat(path, &statl);
|
||||
+ if (status < 0)
|
||||
+ break;
|
||||
+ }
|
||||
|
||||
/* Check this directory is accessible. */
|
||||
if ((statl.st_mode & S_IXUSR) == 0)
|
||||
@@ -208,7 +269,7 @@
|
||||
|
||||
|
||||
|
||||
- if (getenv("PROOT_FASTPATH") != NULL) {
|
||||
+ if (syscall_number == PR_chdir && getenv("PROOT_FASTPATH") != NULL) {
|
||||
int fd = open(path, O_PATH | O_DIRECTORY | O_CLOEXEC);
|
||||
|
||||
|
||||
@@ -17,8 +17,9 @@ $(cat signed/SHA256SUMS.txt)
|
||||
gh release delete "$tag" -R "$CI_REPO" --yes --cleanup-tag 2>/dev/null || true
|
||||
gh release create "$tag" -R "$CI_REPO" --target main --prerelease \
|
||||
--title "main ${GITHUB_SHA::7}" --notes "$notes" signed/*
|
||||
gh release list -R "$CI_REPO" -L 200 --json tagName,publishedAt \
|
||||
--jq '[.[] | select(.tagName | startswith("main-"))] | sort_by(.publishedAt) | reverse | .['"$KEEP"':][] | .tagName' |
|
||||
cutoff=$(date -u -d "-${KEEP_DAYS} days" +%Y-%m-%dT%H:%M:%SZ)
|
||||
gh release list -R "$CI_REPO" -L 1000 --exclude-drafts --json tagName,publishedAt \
|
||||
--jq '.[] | select((.tagName | startswith("main-")) and .publishedAt < "'"$cutoff"'") | .tagName' |
|
||||
while IFS= read -r old; do
|
||||
if [ "$old" = "$tag" ]; then
|
||||
continue
|
||||
|
||||
@@ -14,19 +14,20 @@ ROOT = Path(__file__).resolve().parents[2]
|
||||
MANIFEST = Path('app/build/runtime-inputs.json')
|
||||
PACKS = Path('app/build/packs')
|
||||
PACKED = {'assets/linuxfs/usr/local/lib/droiddeck-xbox/': 'xbox', 'assets/linuxfs/usr/local/lib/droiddeck-comet/': 'comet',
|
||||
'assets/linuxfs/usr/local/lib/droiddeck-ubisoft/': 'ubisoft',
|
||||
'assets/directaudio/': 'directaudio', 'assets/hybris/': 'powervr'}
|
||||
PACKED_FILES = {'lib/arm64-v8a/libproot.so': ('proot', 'libproot.so'),
|
||||
'lib/arm64-v8a/libproot-loader.so': ('proot', 'libproot-loader.so')}
|
||||
PACKED_LIBRARIES = ['lib/arm64-v8a/libopencv_java4.so', 'lib/arm64-v8a/libmediapipe_tasks_jni.so']
|
||||
PACK_NAMES = ['proot', 'directaudio', 'tracking', 'xbox', 'comet', 'butler', 'powervr']
|
||||
PACK_NAMES = ['proot', 'directaudio', 'tracking', 'xbox', 'comet', 'butler', 'powervr', 'ubisoft']
|
||||
SOURCE_PATHS = ['app/build.gradle', 'app/src/main/cpp/fakeinput_steam.cpp', 'app/src/main/assets/pulseaudio.tzst',
|
||||
'tools/linuxfs', 'tools/proot', 'tools/aaudio-sink', 'tools/directaudio', 'tools/packs',
|
||||
'tools/gamescope/release.env', 'tools/droiddeck-esync/release.env',
|
||||
'tools/mangoapp', 'tools/msitools', 'tools/pipewire', 'tools/comet', 'tools/xbox', 'tools/dirt-ws2', 'tools/components', 'tools/ci', 'tools/build_local.sh',
|
||||
'tools/mangoapp', 'tools/msitools', 'tools/pipewire', 'tools/comet', 'tools/xbox', 'tools/dirt-ws2', 'tools/ubisoft-launcher', 'tools/rockstar-launcher', 'tools/components', 'tools/ci', 'tools/build_local.sh',
|
||||
'tools/powervr', 'tools/hybris', 'tools/dxvk', 'tools/vulkan-wsi',
|
||||
'tools/release/runtime_inputs.py', 'app/src/main/assets/desktop-removal',
|
||||
'tools/quest', 'tools/vr/src', 'tools/vr/driver_droiddeck', 'tools/vr/layer', 'tools/vr/build.sh',
|
||||
'tools/gpuload',
|
||||
'tools/gpuload', 'app/src/main/resources/rockstar/legacy-cipher.bin',
|
||||
'app/src/main/cpp/waylandcomp/src/headpose_shm.h',
|
||||
'app/src/main/cpp/venc/venc_proto.h',
|
||||
'app/src/main/cpp/waylandcomp/src/spatialpose_shm.h',
|
||||
@@ -76,7 +77,7 @@ def required():
|
||||
paths += [f'assets/graphics_driver/adrenotools-{name}.tzst'
|
||||
for name in ['turnip26.3.0-a6xx', 'turnip26.3.0-quest2']]
|
||||
paths += ['assets/linuxfs/' + name for name in ['libfakeinput.so', 'libblsession.so',
|
||||
'libblfastpath.so', 'libssbs.so', 'usr/local/bin/droiddeck-clipboard',
|
||||
'libblfastpath.so', 'libddnatv-hook.so', 'libssbs.so', 'usr/local/bin/droiddeck-clipboard',
|
||||
'usr/local/bin/droiddeck-gog-maintenance',
|
||||
'usr/local/bin/gamescope', 'usr/local/lib/droiddeck/uruntime',
|
||||
'usr/local/lib/droiddeck-pipewire.tzst']]
|
||||
@@ -211,7 +212,8 @@ def pack_digests(root, name):
|
||||
|
||||
def check_apk(apk, root=ROOT):
|
||||
data = verify(root)
|
||||
packs = {name: pack_digests(root, name) for name in PACK_NAMES}
|
||||
packs = {name: pack_digests(root, name) for name in PACK_NAMES
|
||||
if name != 'ubisoft' or (root / PACKS / 'ubisoft.tar.zst').is_file()}
|
||||
with ZipFile(apk) as package:
|
||||
names = set(package.namelist())
|
||||
for library in PACKED_LIBRARIES:
|
||||
@@ -231,7 +233,7 @@ def check_apk(apk, root=ROOT):
|
||||
if prefix:
|
||||
if name in names:
|
||||
raise ValueError('APK still carries ' + name + ', which the ' + PACKED[prefix] + ' pack provides')
|
||||
if packs[PACKED[prefix]].get(name[len(prefix):]) != output['sha256']:
|
||||
if packs.get(PACKED[prefix], {}).get(name[len(prefix):]) != output['sha256']:
|
||||
raise ValueError('Add-on pack ' + PACKED[prefix] + ' is missing or has a stale ' + name)
|
||||
continue
|
||||
try:
|
||||
|
||||
@@ -27,7 +27,7 @@ if [ "$pkg" = "$P" ]; then
|
||||
else
|
||||
python3 "$here/axml_rename.py" "$in" "$work/renamed.apk" \
|
||||
"$P=$pkg" "$P.androidx-startup=$pkg.androidx-startup" "$P.logs=$pkg.logs" \
|
||||
"$P.pairingguide=$pkg.pairingguide" "$P.pin=$pkg.pin" \
|
||||
"$P.pairingguide=$pkg.pairingguide" "$P.devicecode=$pkg.devicecode" "$P.pin=$pkg.pin" \
|
||||
"$P.documents=$pkg.documents" "$P.home=$pkg.home" "$P.agent=$pkg.agent" \
|
||||
"$P.vr_recovery=$pkg.vr_recovery" "$P.dualscreen=$pkg.dualscreen" "$P.typing=$pkg.typing" \
|
||||
"$P.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION=$pkg.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"
|
||||
|
||||
+126
-33
@@ -17,6 +17,7 @@ API = "https://api.github.com"
|
||||
SHA256 = re.compile(r"^[0-9a-f]{64}$")
|
||||
PR_TAG = re.compile(r"^pr-([1-9][0-9]*)$")
|
||||
MAIN_TAG = re.compile(r"^main-([0-9a-f]{7,40})$")
|
||||
MERGED_PR = re.compile(r"\[PR #([1-9][0-9]*)\]\(")
|
||||
|
||||
|
||||
class ApiError(RuntimeError):
|
||||
@@ -53,6 +54,20 @@ class GitHub:
|
||||
def get(self, path: str):
|
||||
return self.request("GET", path)
|
||||
|
||||
def get_all(self, path: str, limit: int = 1000) -> list:
|
||||
items = []
|
||||
page = 1
|
||||
sep = "&" if "?" in path else "?"
|
||||
while len(items) < limit:
|
||||
batch = self.get(f"{path}{sep}per_page=100&page={page}")
|
||||
if not batch:
|
||||
break
|
||||
items.extend(batch)
|
||||
if len(batch) < 100:
|
||||
break
|
||||
page += 1
|
||||
return items
|
||||
|
||||
def put(self, path: str, body):
|
||||
return self.request("PUT", path, body)
|
||||
|
||||
@@ -100,15 +115,20 @@ def parse_variants(text: str) -> list[tuple[str, str]]:
|
||||
return out
|
||||
|
||||
|
||||
def published_millis(release: dict, prefer_updated: bool = False) -> int:
|
||||
value = (
|
||||
release.get("updated_at") if prefer_updated else None
|
||||
) or release.get("published_at") or release.get("created_at")
|
||||
def iso_millis(value: str | None) -> int:
|
||||
if not value:
|
||||
return 0
|
||||
return int(dt.datetime.fromisoformat(value.replace("Z", "+00:00")).timestamp() * 1000)
|
||||
|
||||
|
||||
def published_millis(release: dict, prefer_updated: bool = False) -> int:
|
||||
return iso_millis(
|
||||
(release.get("updated_at") if prefer_updated else None)
|
||||
or release.get("published_at")
|
||||
or release.get("created_at")
|
||||
)
|
||||
|
||||
|
||||
def paragraphs(markdown: str) -> list[str]:
|
||||
return [p.strip() for p in re.split(r"\n\s*\n", markdown.replace("\r", "")) if p.strip()]
|
||||
|
||||
@@ -195,6 +215,7 @@ def release_entry(
|
||||
title: str,
|
||||
summary: str,
|
||||
prefer_updated: bool = False,
|
||||
extra: dict | None = None,
|
||||
):
|
||||
suffixes = [suffix for _, suffix in variants]
|
||||
apks = {}
|
||||
@@ -220,6 +241,7 @@ def release_entry(
|
||||
"versionCode": version_code,
|
||||
"publishedAt": published_millis(release, prefer_updated),
|
||||
"url": release.get("html_url") or "",
|
||||
**(extra or {}),
|
||||
"apks": apks,
|
||||
}
|
||||
|
||||
@@ -289,8 +311,52 @@ def stable_app_release(releases: list[dict]) -> dict | None:
|
||||
return max(apps, key=lambda r: r.get("published_at") or r.get("created_at") or "", default=None)
|
||||
|
||||
|
||||
def build_catalog(source: GitHub, ci: GitHub, source_repo: str, ci_repo: str) -> dict:
|
||||
stable_release = stable_app_release(source.get(f"/repos/{source_repo}/releases?per_page=100"))
|
||||
def merged_pr(release: dict) -> int:
|
||||
ps = paragraphs(release.get("body") or "")
|
||||
match = MERGED_PR.search(ps[0]) if ps else None
|
||||
return int(match.group(1)) if match else 0
|
||||
|
||||
|
||||
class Cache:
|
||||
"""Commit facts carried over from the published catalog, so a run only asks GitHub about new builds."""
|
||||
|
||||
def __init__(self, previous: dict | None):
|
||||
self.commits: dict[str, dict] = {}
|
||||
for entry in (previous or {}).get("previews") or []:
|
||||
commit = (entry.get("commit") or "").lower()
|
||||
if commit and entry.get("committedAt"):
|
||||
self.commits[commit] = {"author": entry.get("author") or "", "committedAt": entry["committedAt"]}
|
||||
|
||||
def commit(self, gh: GitHub, repo: str, sha: str) -> dict:
|
||||
sha = sha.lower()
|
||||
if sha not in self.commits:
|
||||
try:
|
||||
obj = gh.get(f"/repos/{repo}/commits/{q(sha)}")
|
||||
self.commits[sha] = {
|
||||
"author": (obj.get("author") or {}).get("login") or "",
|
||||
"committedAt": iso_millis(((obj.get("commit") or {}).get("committer") or {}).get("date")),
|
||||
}
|
||||
except ApiError:
|
||||
self.commits[sha] = {"author": "", "committedAt": 0}
|
||||
return self.commits[sha]
|
||||
|
||||
|
||||
def unique_previews(previews: list[dict]) -> list[dict]:
|
||||
ordered = sorted(previews, key=lambda r: (r.get("committedAt") or r["publishedAt"], r["publishedAt"]), reverse=True)
|
||||
result = []
|
||||
seen = set()
|
||||
for preview in ordered:
|
||||
commit = preview["commit"].lower()
|
||||
if commit in seen:
|
||||
continue
|
||||
seen.add(commit)
|
||||
result.append(preview)
|
||||
return result
|
||||
|
||||
|
||||
def build_catalog(source: GitHub, ci: GitHub, source_repo: str, ci_repo: str, previous: dict | None = None) -> dict:
|
||||
cache = Cache(previous)
|
||||
stable_release = stable_app_release(source.get_all(f"/repos/{source_repo}/releases", limit=300))
|
||||
|
||||
stable = None
|
||||
if stable_release:
|
||||
@@ -307,9 +373,11 @@ def build_catalog(source: GitHub, ci: GitHub, source_repo: str, ci_repo: str) ->
|
||||
signer,
|
||||
title,
|
||||
summary,
|
||||
extra={"committedAt": cache.commit(source, source_repo, stable_commit)["committedAt"]},
|
||||
)
|
||||
|
||||
releases = ci.get(f"/repos/{ci_repo}/releases?per_page=100")
|
||||
main_code, _, main_variants, main_signer = source_meta(source, source_repo, "main")
|
||||
releases = ci.get_all(f"/repos/{ci_repo}/releases")
|
||||
previews = []
|
||||
preview_history = []
|
||||
tests = []
|
||||
@@ -322,12 +390,20 @@ def build_catalog(source: GitHub, ci: GitHub, source_repo: str, ci_repo: str) ->
|
||||
commit = main_commit(release)
|
||||
if not commit:
|
||||
continue
|
||||
try:
|
||||
code, _, variants, signer = source_meta(source, source_repo, commit)
|
||||
except ApiError:
|
||||
continue
|
||||
code_text = release_body_value(release.get("body") or "", "VersionCode")
|
||||
if code_text and code_text.isdigit():
|
||||
code, variants, signer = int(code_text), main_variants, main_signer
|
||||
else:
|
||||
try:
|
||||
code, _, variants, signer = source_meta(source, source_repo, commit)
|
||||
except ApiError:
|
||||
continue
|
||||
facts = cache.commit(source, source_repo, commit)
|
||||
title, summary = ci_description(release)
|
||||
preview = release_entry(release, commit, 0, None, code, variants, signer, title, summary)
|
||||
preview = release_entry(
|
||||
release, commit, 0, None, code, variants, signer, title, summary,
|
||||
extra={"mergedPr": merged_pr(release), "author": facts["author"], "committedAt": facts["committedAt"]},
|
||||
)
|
||||
previews.append(preview)
|
||||
history_entry = preview_history_entry(release, preview, variants)
|
||||
if history_entry:
|
||||
@@ -350,16 +426,15 @@ def build_catalog(source: GitHub, ci: GitHub, source_repo: str, ci_repo: str) ->
|
||||
commit = release_body_value(body, "Commit")
|
||||
if not commit or not re.fullmatch(r"[0-9a-f]{7,40}", commit):
|
||||
continue
|
||||
base_ref = release_body_value(body, "Base") or pull["base"]["sha"]
|
||||
try:
|
||||
base_code, _, variants, signer = source_meta(source, source_repo, base_ref)
|
||||
except ApiError:
|
||||
continue
|
||||
version_code_text = release_body_value(body, "VersionCode")
|
||||
if version_code_text and version_code_text.isdigit():
|
||||
version_code = int(version_code_text)
|
||||
else:
|
||||
version_code = base_code
|
||||
base_ref = release_body_value(body, "Base") or pull["base"]["sha"]
|
||||
try:
|
||||
version_code = source_meta(source, source_repo, base_ref)[0]
|
||||
except ApiError:
|
||||
continue
|
||||
try:
|
||||
changed = source.get(f"/repos/{source_repo}/pulls/{pr}/files?per_page=100")
|
||||
if any(item.get("filename") == "app/build.gradle" for item in changed):
|
||||
@@ -375,52 +450,69 @@ def build_catalog(source: GitHub, ci: GitHub, source_repo: str, ci_repo: str) ->
|
||||
pr,
|
||||
None,
|
||||
version_code,
|
||||
variants,
|
||||
signer,
|
||||
main_variants,
|
||||
main_signer,
|
||||
title,
|
||||
summary,
|
||||
prefer_updated=True,
|
||||
extra={
|
||||
"author": (pull.get("user") or {}).get("login") or "",
|
||||
"headCommit": (pull.get("head") or {}).get("sha") or "",
|
||||
"builtAt": max(
|
||||
(iso_millis(a.get("updated_at") or a.get("created_at"))
|
||||
for a in release.get("assets") or [] if (a.get("name") or "").endswith(".apk")),
|
||||
default=0,
|
||||
),
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
previews.sort(key=lambda r: r["publishedAt"], reverse=True)
|
||||
ordered = unique_previews(previews)
|
||||
tests.sort(key=lambda r: r["publishedAt"], reverse=True)
|
||||
return {
|
||||
"schema": 1,
|
||||
"sourceRepo": source_repo,
|
||||
"ciRepo": ci_repo,
|
||||
"generatedAt": int(time.time() * 1000),
|
||||
"mainVersionCode": main_code,
|
||||
"stable": stable,
|
||||
"preview": previews[0] if previews else None,
|
||||
"preview": ordered[0] if ordered else None,
|
||||
"recentPreviews": recent_previews(preview_history),
|
||||
"previews": ordered,
|
||||
"tests": tests,
|
||||
}
|
||||
|
||||
|
||||
def current_file(ci: GitHub, repo: str, path: str):
|
||||
def current_file(ci: GitHub, repo: str, path: str, branch: str = "main"):
|
||||
try:
|
||||
return ci.get(f"/repos/{repo}/contents/{q(path)}?ref=main")
|
||||
return ci.get(f"/repos/{repo}/contents/{q(path)}?ref={q(branch)}")
|
||||
except ApiError as e:
|
||||
if e.status == 404:
|
||||
return None
|
||||
raise
|
||||
|
||||
|
||||
def publish_catalog(source: GitHub, ci: GitHub, source_repo: str, ci_repo: str, path: str):
|
||||
def previous_catalog(old: dict | None) -> dict | None:
|
||||
try:
|
||||
return json.loads(base64.b64decode(old["content"])) if old and old.get("content") else None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def publish_catalog(source: GitHub, ci: GitHub, source_repo: str, ci_repo: str, path: str, branch: str = "main"):
|
||||
for attempt in range(4):
|
||||
catalog = build_catalog(source, ci, source_repo, ci_repo)
|
||||
content = (json.dumps(catalog, indent=2, sort_keys=False) + "\n").encode()
|
||||
old = current_file(ci, ci_repo, path)
|
||||
old = current_file(ci, ci_repo, path, branch)
|
||||
catalog = build_catalog(source, ci, source_repo, ci_repo, previous_catalog(old))
|
||||
content = (json.dumps(catalog, separators=(",", ":")) + "\n").encode()
|
||||
body = {
|
||||
"message": "ci: refresh DroidDeck update catalog",
|
||||
"content": base64.b64encode(content).decode(),
|
||||
"branch": "main",
|
||||
"branch": branch,
|
||||
}
|
||||
if old:
|
||||
body["sha"] = old["sha"]
|
||||
try:
|
||||
old_bytes = base64.b64decode(old["content"])
|
||||
old_json = json.loads(old_bytes)
|
||||
old_json = dict(previous_catalog(old) or {})
|
||||
old_json.pop("generatedAt", None)
|
||||
new_json = dict(catalog)
|
||||
new_json.pop("generatedAt", None)
|
||||
@@ -431,7 +523,7 @@ def publish_catalog(source: GitHub, ci: GitHub, source_repo: str, ci_repo: str,
|
||||
pass
|
||||
try:
|
||||
ci.put(f"/repos/{ci_repo}/contents/{q(path)}", body)
|
||||
print(f"published https://raw.githubusercontent.com/{ci_repo}/main/{path}")
|
||||
print(f"published https://raw.githubusercontent.com/{ci_repo}/{branch}/{path}")
|
||||
return catalog
|
||||
except ApiError as e:
|
||||
if e.status not in (409, 422) or attempt == 3:
|
||||
@@ -445,6 +537,7 @@ def main():
|
||||
parser.add_argument("--source-repo", default="Droid-Deck/DroidDeck")
|
||||
parser.add_argument("--ci-repo", default="Droid-Deck/DroidDeck-CI")
|
||||
parser.add_argument("--path", default="catalog.json")
|
||||
parser.add_argument("--branch", default="main")
|
||||
parser.add_argument("--dry-run", action="store_true")
|
||||
args = parser.parse_args()
|
||||
|
||||
@@ -455,7 +548,7 @@ def main():
|
||||
else:
|
||||
if not os.environ.get("CI_GITHUB_TOKEN"):
|
||||
raise SystemExit("CI_GITHUB_TOKEN is required to publish")
|
||||
publish_catalog(source, ci, args.source_repo, args.ci_repo, args.path)
|
||||
publish_catalog(source, ci, args.source_repo, args.ci_repo, args.path, args.branch)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
/target/
|
||||
Generated
+105
@@ -0,0 +1,105 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "droiddeck-rockstar-launcher"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.107"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.47"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.229"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.229"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.229"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.154"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"serde",
|
||||
"serde_core",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "3.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8593e8e72159ed2257d083c7a454a85cbf854f37a0966d8d483aff8c8a3ebcee"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954"
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
|
||||
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "droiddeck-rockstar-launcher"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "GPL-3.0-only"
|
||||
publish = false
|
||||
|
||||
[[bin]]
|
||||
name = "Launcher"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
serde_json = "1"
|
||||
|
||||
[profile.release]
|
||||
opt-level = "s"
|
||||
lto = true
|
||||
panic = "abort"
|
||||
strip = true
|
||||
@@ -0,0 +1,36 @@
|
||||
# Rockstar launcher service
|
||||
|
||||
`Launcher.exe` starts the selected installed game and serves its launcher pipes. The Android app obtains title authorization from Rockstar. Refresh credentials stay in Android Keystore-backed storage.
|
||||
|
||||
The handoff is mode 0600, expires after 90 seconds and is deleted before starting the game. Legacy launches carry server-issued launch material. SDK launches also carry a short-lived ROS ticket and account identity; those values are kept in memory after the handoff is consumed and are not logged. Pipes reject remote clients and check the client process against the launched child. No game files are patched.
|
||||
|
||||
GTA III, Vice City and San Andreas use body-length MTLX replies. GTA IV and L.A. Noire use total-length, terminated replies. Titles with installed Social Club metadata or SDK binaries use the SDK IPC adapter, including user-info, login, ticket, initialization and authorized title-state synchronization. The title metadata supplies the ROS identity and principal executable; it is checked against Rockstar's catalog before title authorization. Other titles retain Play and Steam shortcuts and attempt the normal executable launch path.
|
||||
|
||||
The SDK runtime is extracted from the game's own Social-Club-Setup.exe when present. Architecture-specific payloads are staged into the prefix, archive paths and sizes are checked, interrupted copies are retried, and both registry views are registered. The helper discovers launcher identity pipe names in the game executable and answers bounded INIT challenges from the launched process. No vendor executables or runtime files are bundled with this tool. SDK initialization has a 90-second deadline. SDK wire tests and fixture clients do not establish gameplay compatibility for every title; per-title ROS ticket exchange and secondary SDK channels remain unverified.
|
||||
|
||||
GTA III defaults to full x87 precision for FEX before Wine starts, because reduced precision crashed new-game initialization on the test device. San Andreas defaults to WineD3D, because DXVK intermittently corrupted its textures; full precision and conservative memory ordering did not prevent that corruption. These defaults use DroidDeck's per-game settings and preserve explicit per-game overrides.
|
||||
|
||||
Raw launcher identity requests use `INIT:<nonce>:` with a terminating colon. Framed requests carry their payload length. The reply echoes the complete challenge after `TINI:launcher:`; partial reads do not complete a raw request.
|
||||
|
||||
`build.sh` builds 32-bit and 64-bit executables with the GNU Windows targets and stages them as APK resources. `legacy-cipher.bin` contains the fixed client protocol tables, not account credentials or APK signing keys.
|
||||
|
||||
Run the protocol tests with:
|
||||
|
||||
```sh
|
||||
cargo test --locked --manifest-path tools/rockstar-launcher/Cargo.toml
|
||||
```
|
||||
|
||||
The optional `capture` example records a first-party launcher reply to a new local file. Run it on Windows or Wine while launching the corresponding game in Rockstar's launcher:
|
||||
|
||||
```powershell
|
||||
cargo run --release --example capture --manifest-path tools/rockstar-launcher/Cargo.toml -- --pipe GTA-VC_Pipe --output 'C:\captures\reply.bin'
|
||||
```
|
||||
|
||||
On Linux, cross-compile the example and run it in the same Wine prefix as Rockstar's launcher:
|
||||
|
||||
```sh
|
||||
cargo build --release --locked --example capture --target x86_64-pc-windows-gnu --manifest-path tools/rockstar-launcher/Cargo.toml
|
||||
WINEPREFIX=/path/to/launcher-prefix wine tools/rockstar-launcher/target/x86_64-pc-windows-gnu/release/examples/capture.exe --pipe GTA-VC_Pipe --output 'Z:\tmp\reply.bin'
|
||||
```
|
||||
|
||||
Captures contain live launch material. Keep them outside source control; the tool prints only the message tag and payload length.
|
||||
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
here=$(cd "$(dirname "$0")" && pwd)
|
||||
out=${1:-"$here/../../app/src/main/resources/rockstar/helpers"}
|
||||
mkdir -p "$out"
|
||||
for target in i686-pc-windows-gnu x86_64-pc-windows-gnu; do
|
||||
rustup target add "$target" >/dev/null 2>&1 || true
|
||||
cargo build --manifest-path "$here/Cargo.toml" --release --locked --target "$target"
|
||||
name=Launcher64.exe
|
||||
[[ "$target" == i686-* ]] && name=Launcher32.exe
|
||||
install -m644 "${CARGO_TARGET_DIR:-$here/target}/$target/release/Launcher.exe" "$out/$name"
|
||||
done
|
||||
@@ -0,0 +1,65 @@
|
||||
#[cfg(windows)]
|
||||
fn main() -> std::io::Result<()> {
|
||||
use droiddeck_rockstar_launcher::{pipe::Pipe, Frame, Length};
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::time::Duration;
|
||||
|
||||
struct Recording<'a> {
|
||||
source: &'a mut Pipe,
|
||||
output: &'a mut std::fs::File,
|
||||
}
|
||||
|
||||
impl Read for Recording<'_> {
|
||||
fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
|
||||
let size = self.source.read(buffer)?;
|
||||
self.output.write_all(&buffer[..size])?;
|
||||
Ok(size)
|
||||
}
|
||||
}
|
||||
|
||||
let args: Vec<_> = std::env::args_os().collect();
|
||||
let output = args
|
||||
.windows(2)
|
||||
.find(|pair| pair[0] == "--output")
|
||||
.map(|pair| pair[1].clone())
|
||||
.map(std::path::PathBuf::from)
|
||||
.unwrap_or(std::env::current_exe()?.with_file_name("reference-reply.bin"));
|
||||
let mut file = OpenOptions::new()
|
||||
.create_new(true)
|
||||
.write(true)
|
||||
.open(output)?;
|
||||
let pipe_name = args
|
||||
.windows(2)
|
||||
.find(|pair| pair[0] == "--pipe")
|
||||
.map(|pair| pair[1].to_string_lossy().into_owned())
|
||||
.unwrap_or_else(|| "GTA-VC_Pipe".to_owned());
|
||||
let mut pipe = Pipe::connect(&pipe_name, Duration::from_secs(90))?;
|
||||
Frame {
|
||||
tag: *b"COLE",
|
||||
payload: Vec::new(),
|
||||
nul_terminated: true,
|
||||
}
|
||||
.write(&mut pipe, Length::Total)?;
|
||||
let reply = Frame::read(
|
||||
&mut Recording {
|
||||
source: &mut pipe,
|
||||
output: &mut file,
|
||||
},
|
||||
Length::Body,
|
||||
);
|
||||
file.sync_all()?;
|
||||
let reply = reply?;
|
||||
println!(
|
||||
"Received {} ({} payload bytes)",
|
||||
String::from_utf8_lossy(&reply.tag),
|
||||
reply.payload.len()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
fn main() {
|
||||
eprintln!("The reference pipe client runs on Windows or Wine.");
|
||||
std::process::exit(1);
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
#[cfg(windows)]
|
||||
fn run() -> Result<(), Box<dyn std::error::Error>> {
|
||||
use droiddeck_rockstar_launcher::{pipe::Pipe, sdk::Message, Frame, Length};
|
||||
use std::io::{Read, Write};
|
||||
for (path, framed) in [
|
||||
(b"\\\\.\\pipe\\ProbeLauncherPipe\0".as_slice(), true),
|
||||
(b"\\\\.\\pipe\\RawProbeLauncher_Pipe\0".as_slice(), false),
|
||||
] {
|
||||
let name = std::str::from_utf8(&path[9..path.len() - 1])?;
|
||||
let mut pipe = Pipe::connect(name, std::time::Duration::from_secs(10))?;
|
||||
let request = b"INIT:42:";
|
||||
if framed {
|
||||
let mut bytes = (request.len() as u32).to_le_bytes().to_vec();
|
||||
bytes.extend_from_slice(request);
|
||||
pipe.write_all(&bytes)?;
|
||||
let response = Frame::read(&mut pipe, Length::Body)?;
|
||||
if response.tag != *b"TINI" || response.payload != b"launcher:42:" {
|
||||
return Err("invalid framed launcher identity reply".into());
|
||||
}
|
||||
} else {
|
||||
pipe.write_all(b"INIT:4")?;
|
||||
std::thread::sleep(std::time::Duration::from_millis(20));
|
||||
pipe.write_all(b"2:")?;
|
||||
let mut response = vec![0; b"TINI:launcher:42:".len()];
|
||||
pipe.read_exact(&mut response)?;
|
||||
if response != b"TINI:launcher:42:" {
|
||||
return Err("invalid raw launcher identity reply".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
let args: Vec<_> = std::env::args().collect();
|
||||
let instance: u32 = args
|
||||
.iter()
|
||||
.find_map(|arg| arg.strip_prefix("-processInstance="))
|
||||
.ok_or("missing instance")?
|
||||
.parse()?;
|
||||
let output = args
|
||||
.windows(2)
|
||||
.find(|pair| pair[0] == "--result")
|
||||
.map(|pair| &pair[1])
|
||||
.ok_or("missing result path")?;
|
||||
let mut pipe = Pipe::connect(
|
||||
&format!("chrome.rgsc_gameinst_{instance}"),
|
||||
std::time::Duration::from_secs(10),
|
||||
)?;
|
||||
if Message::read(&mut pipe)?.kind != 0xffff {
|
||||
return Err("missing transport hello".into());
|
||||
}
|
||||
Message {
|
||||
routing: i32::MAX,
|
||||
kind: 0x40000,
|
||||
flags: 2,
|
||||
payload: 4u32.to_le_bytes().to_vec(),
|
||||
}
|
||||
.write(&mut pipe)?;
|
||||
let expected = [0x30002, 0x30003, 0x30004, 0x30000];
|
||||
for kind in expected {
|
||||
let message = Message::read(&mut pipe)?;
|
||||
if message.kind != kind || message.routing != i32::MAX || message.flags != 2 {
|
||||
return Err("unexpected SDK message envelope".into());
|
||||
}
|
||||
if kind == 0x30002 && message.payload != [3, 0, 0, 0, 123, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
{
|
||||
return Err("unexpected fixture account".into());
|
||||
}
|
||||
if kind == 0x30000 && message.payload != 4u32.to_le_bytes() {
|
||||
return Err("launcher hello lost its context".into());
|
||||
}
|
||||
}
|
||||
std::fs::write(output, "SDK handshake accepted")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
#[cfg(windows)]
|
||||
if let Err(error) = run() {
|
||||
eprintln!("SDK probe failed: {error}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
use std::collections::BTreeSet;
|
||||
use std::io::{self, Read};
|
||||
|
||||
const LIMIT: usize = 4096;
|
||||
|
||||
fn invalid() -> io::Error {
|
||||
io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"invalid launcher identity request",
|
||||
)
|
||||
}
|
||||
|
||||
fn collect(bytes: &[u8], names: &mut BTreeSet<String>) {
|
||||
let prefix = br"\\.\pipe\";
|
||||
for (index, window) in bytes.windows(prefix.len()).enumerate() {
|
||||
if window != prefix {
|
||||
continue;
|
||||
}
|
||||
let tail = &bytes[index + prefix.len()..];
|
||||
let Some(end) = tail.iter().take(97).position(|byte| *byte == 0) else {
|
||||
continue;
|
||||
};
|
||||
let name = &tail[..end];
|
||||
if !name.is_empty()
|
||||
&& name
|
||||
.iter()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || b"_-".contains(byte))
|
||||
{
|
||||
let name = std::str::from_utf8(name).unwrap();
|
||||
if name.ends_with("LauncherPipe") || name.ends_with("Launcher_Pipe") {
|
||||
names.insert(name.to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pipe_names(reader: &mut impl Read) -> io::Result<Vec<String>> {
|
||||
let mut names = BTreeSet::new();
|
||||
let mut carry = Vec::new();
|
||||
let mut chunk = [0; 65536];
|
||||
let mut total = 0usize;
|
||||
loop {
|
||||
let count = reader.read(&mut chunk)?;
|
||||
if count == 0 {
|
||||
break;
|
||||
}
|
||||
total += count;
|
||||
if total > 256 * 1024 * 1024 {
|
||||
return Err(invalid());
|
||||
}
|
||||
carry.extend_from_slice(&chunk[..count]);
|
||||
collect(&carry, &mut names);
|
||||
for offset in 0..2 {
|
||||
let ascii: Vec<_> = carry[offset..]
|
||||
.chunks_exact(2)
|
||||
.map(|pair| if pair[1] == 0 { pair[0] } else { 0 })
|
||||
.collect();
|
||||
collect(&ascii, &mut names);
|
||||
}
|
||||
if names.len() > 4 {
|
||||
return Err(invalid());
|
||||
}
|
||||
carry.drain(..carry.len().saturating_sub(256));
|
||||
}
|
||||
Ok(names.into_iter().collect())
|
||||
}
|
||||
|
||||
pub fn exchange(reader: &mut impl Read) -> io::Result<Vec<u8>> {
|
||||
let mut header = [0; 4];
|
||||
reader.read_exact(&mut header)?;
|
||||
let framed = header != *b"INIT";
|
||||
let mut request = if framed {
|
||||
let size = u32::from_le_bytes(header) as usize;
|
||||
if !(6..=LIMIT).contains(&size) {
|
||||
return Err(invalid());
|
||||
}
|
||||
let mut bytes = vec![0; size];
|
||||
reader.read_exact(&mut bytes)?;
|
||||
bytes
|
||||
} else {
|
||||
let mut bytes = header.to_vec();
|
||||
let mut byte = [0];
|
||||
reader.read_exact(&mut byte)?;
|
||||
if byte[0] != b':' {
|
||||
return Err(invalid());
|
||||
}
|
||||
bytes.push(b':');
|
||||
loop {
|
||||
reader.read_exact(&mut byte)?;
|
||||
if !(32..127).contains(&byte[0]) || bytes.len() >= 261 {
|
||||
return Err(invalid());
|
||||
}
|
||||
bytes.push(byte[0]);
|
||||
if byte[0] == b':' {
|
||||
break;
|
||||
}
|
||||
}
|
||||
bytes
|
||||
};
|
||||
if request.last() == Some(&0) {
|
||||
request.pop();
|
||||
}
|
||||
let challenge = request.strip_prefix(b"INIT:").ok_or_else(invalid)?;
|
||||
if challenge.is_empty()
|
||||
|| !framed && challenge == b":"
|
||||
|| challenge.len() > 256
|
||||
|| !challenge.iter().all(|byte| (32..127).contains(byte))
|
||||
{
|
||||
return Err(invalid());
|
||||
}
|
||||
let mut reply = b"TINI:launcher:".to_vec();
|
||||
reply.extend_from_slice(challenge);
|
||||
if framed {
|
||||
let mut bytes = (reply.len() as u32).to_le_bytes().to_vec();
|
||||
bytes.extend_from_slice(&reply);
|
||||
Ok(bytes)
|
||||
} else {
|
||||
Ok(reply)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::Cursor;
|
||||
|
||||
#[test]
|
||||
fn discovers_ascii_and_wide_pipe_names_across_chunks() {
|
||||
let mut bytes = vec![0; 65530];
|
||||
bytes.extend_from_slice(br"\\.\pipe\GameLauncherPipe");
|
||||
bytes.push(0);
|
||||
for value in "\\\\.\\pipe\\OtherLauncher_Pipe\0".encode_utf16() {
|
||||
bytes.extend_from_slice(&value.to_le_bytes());
|
||||
}
|
||||
bytes.extend_from_slice(br"\\.\pipe\..\outsideLauncherPipe");
|
||||
bytes.push(0);
|
||||
assert_eq!(
|
||||
pipe_names(&mut Cursor::new(bytes)).unwrap(),
|
||||
["GameLauncherPipe", "OtherLauncher_Pipe"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn identity_replies_echo_only_a_valid_challenge() {
|
||||
struct Split<R>(R);
|
||||
impl<R: Read> Read for Split<R> {
|
||||
fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
|
||||
let size = buffer.len().min(2);
|
||||
self.0.read(&mut buffer[..size])
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
exchange(&mut Split(Cursor::new(b"INIT:123456789:\0"))).unwrap(),
|
||||
b"TINI:launcher:123456789:"
|
||||
);
|
||||
let request = b"INIT:nonce:123:";
|
||||
let mut bytes = (request.len() as u32).to_le_bytes().to_vec();
|
||||
bytes.extend_from_slice(request);
|
||||
let reply = exchange(&mut Cursor::new(bytes)).unwrap();
|
||||
assert_eq!(
|
||||
u32::from_le_bytes(reply[..4].try_into().unwrap()) as usize,
|
||||
reply.len() - 4
|
||||
);
|
||||
assert_eq!(&reply[4..], b"TINI:launcher:nonce:123:");
|
||||
assert!(exchange(&mut Cursor::new(b"INIT:")).is_err());
|
||||
assert!(exchange(&mut Cursor::new(b"INIT:a\0b")).is_err());
|
||||
assert!(exchange(&mut Cursor::new(b"INIT:unterminated")).is_err());
|
||||
assert!(exchange(&mut Cursor::new([255; 4])).is_err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,351 @@
|
||||
use std::io::{self, Read, Write};
|
||||
|
||||
#[cfg(windows)]
|
||||
pub mod pipe;
|
||||
|
||||
pub mod identity;
|
||||
pub mod sdk;
|
||||
|
||||
pub const MAX_FRAME_BYTES: usize = 64 * 1024;
|
||||
const OVERHEAD: usize = 4 + 4 + 1;
|
||||
|
||||
pub fn wait_for_game(
|
||||
child: &mut std::process::Child,
|
||||
handshake: std::sync::mpsc::Receiver<io::Result<()>>,
|
||||
) -> io::Result<std::process::ExitStatus> {
|
||||
use std::sync::mpsc::RecvTimeoutError;
|
||||
loop {
|
||||
match handshake.recv_timeout(std::time::Duration::from_millis(10)) {
|
||||
Ok(Ok(())) => return child.wait(),
|
||||
result @ (Ok(Err(_)) | Err(RecvTimeoutError::Disconnected)) => {
|
||||
let error = match result {
|
||||
Ok(Err(error)) => error,
|
||||
_ => io::Error::new(
|
||||
io::ErrorKind::BrokenPipe,
|
||||
"Rockstar handshake worker stopped",
|
||||
),
|
||||
};
|
||||
let _ = child.kill();
|
||||
child.wait()?;
|
||||
return Err(error);
|
||||
}
|
||||
Err(RecvTimeoutError::Timeout) => {
|
||||
if let Some(status) = child.try_wait()? {
|
||||
return Ok(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Length {
|
||||
Total,
|
||||
Body,
|
||||
}
|
||||
|
||||
impl Length {
|
||||
fn header_bytes(self) -> usize {
|
||||
match self {
|
||||
Self::Total => 0,
|
||||
Self::Body => 4,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct Frame {
|
||||
pub tag: [u8; 4],
|
||||
pub payload: Vec<u8>,
|
||||
pub nul_terminated: bool,
|
||||
}
|
||||
|
||||
fn invalid(message: &'static str) -> io::Error {
|
||||
io::Error::new(io::ErrorKind::InvalidData, message)
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
pub fn read(reader: &mut impl Read, length: Length) -> io::Result<Self> {
|
||||
let mut header = [0; 4];
|
||||
reader.read_exact(&mut header)?;
|
||||
let size = (u32::from_le_bytes(header) as usize)
|
||||
.checked_add(length.header_bytes())
|
||||
.ok_or_else(|| invalid("invalid Rockstar pipe frame length"))?;
|
||||
if !(OVERHEAD..=MAX_FRAME_BYTES).contains(&size) {
|
||||
return Err(invalid("invalid Rockstar pipe frame length"));
|
||||
}
|
||||
let mut body = vec![0; size - header.len()];
|
||||
reader.read_exact(&mut body)?;
|
||||
let nul_terminated = body.last() == Some(&0);
|
||||
let end = body.len() - usize::from(nul_terminated);
|
||||
if body[4] != b':' || body[..4].contains(&b':') || body[..end].contains(&0) {
|
||||
return Err(invalid("invalid Rockstar pipe frame envelope"));
|
||||
}
|
||||
Ok(Self {
|
||||
tag: body[..4].try_into().unwrap(),
|
||||
payload: body[5..end].to_vec(),
|
||||
nul_terminated,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn write(&self, writer: &mut impl Write, length: Length) -> io::Result<()> {
|
||||
let size = self
|
||||
.payload
|
||||
.len()
|
||||
.checked_add(OVERHEAD + usize::from(self.nul_terminated))
|
||||
.filter(|size| *size <= MAX_FRAME_BYTES)
|
||||
.ok_or_else(|| invalid("Rockstar pipe frame exceeds size limit"))?;
|
||||
if self.tag.contains(&0) || self.tag.contains(&b':') || self.payload.contains(&0) {
|
||||
return Err(invalid("Rockstar pipe frame contains a delimiter"));
|
||||
}
|
||||
let mut encoded = Vec::with_capacity(size);
|
||||
encoded.extend_from_slice(&((size - length.header_bytes()) as u32).to_le_bytes());
|
||||
encoded.extend_from_slice(&self.tag);
|
||||
encoded.push(b':');
|
||||
encoded.extend_from_slice(&self.payload);
|
||||
if self.nul_terminated {
|
||||
encoded.push(0);
|
||||
}
|
||||
writer.write_all(&encoded)?;
|
||||
writer.flush()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn launcher_era_reply_uses_total_length_and_a_terminator() {
|
||||
let mut bytes = Vec::new();
|
||||
Frame {
|
||||
tag: *b"DUNN",
|
||||
payload: vec![b'A'; 640],
|
||||
nul_terminated: true,
|
||||
}
|
||||
.write(&mut bytes, Length::Total)
|
||||
.unwrap();
|
||||
assert_eq!(bytes.len(), 650);
|
||||
assert_eq!(&bytes[..9], &[138, 2, 0, 0, b'D', b'U', b'N', b'N', b':']);
|
||||
assert_eq!(bytes[649], 0);
|
||||
}
|
||||
use std::io::Cursor;
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn sleeping_game() {
|
||||
std::thread::sleep(std::time::Duration::from_secs(30));
|
||||
}
|
||||
|
||||
fn sleeping_child() -> std::process::Child {
|
||||
std::process::Command::new(std::env::current_exe().unwrap())
|
||||
.args(["--exact", "tests::sleeping_game", "--ignored"])
|
||||
.stdout(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.spawn()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_handshake_terminates_and_reaps_the_game() {
|
||||
let mut child = sleeping_child();
|
||||
let (sender, receiver) = std::sync::mpsc::channel();
|
||||
sender
|
||||
.send(Err(io::Error::new(io::ErrorKind::TimedOut, "test timeout")))
|
||||
.unwrap();
|
||||
let start = std::time::Instant::now();
|
||||
assert_eq!(
|
||||
wait_for_game(&mut child, receiver).unwrap_err().kind(),
|
||||
io::ErrorKind::TimedOut
|
||||
);
|
||||
assert!(start.elapsed() < std::time::Duration::from_secs(3));
|
||||
assert!(child.try_wait().unwrap().is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stopped_handshake_worker_terminates_and_reaps_the_game() {
|
||||
let mut child = sleeping_child();
|
||||
let (sender, receiver) = std::sync::mpsc::channel();
|
||||
drop(sender);
|
||||
assert_eq!(
|
||||
wait_for_game(&mut child, receiver).unwrap_err().kind(),
|
||||
io::ErrorKind::BrokenPipe
|
||||
);
|
||||
assert!(child.try_wait().unwrap().is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_exited_game_does_not_wait_for_its_handshake() {
|
||||
let mut child = std::process::Command::new(std::env::current_exe().unwrap())
|
||||
.args(["--exact", "no_test_with_this_name"])
|
||||
.stdout(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
let (_sender, receiver) = std::sync::mpsc::channel();
|
||||
assert!(wait_for_game(&mut child, receiver).unwrap().success());
|
||||
}
|
||||
|
||||
struct Fragmented<T>(T);
|
||||
|
||||
impl<T: Read> Read for Fragmented<T> {
|
||||
fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
|
||||
let size = buffer.len().min(1);
|
||||
self.0.read(&mut buffer[..size])
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Write> Write for Fragmented<T> {
|
||||
fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
|
||||
self.0.write(&buffer[..buffer.len().min(1)])
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
self.0.flush()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_observed_greeting_across_partial_reads() {
|
||||
let bytes = b"\x0a\0\0\0COLE:\0";
|
||||
assert_eq!(
|
||||
Frame::read(&mut Fragmented(&bytes[..]), Length::Total).unwrap(),
|
||||
Frame {
|
||||
tag: *b"COLE",
|
||||
payload: Vec::new(),
|
||||
nul_terminated: true,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writes_the_observed_total_length_even_with_partial_writes() {
|
||||
let mut bytes = Fragmented(Vec::new());
|
||||
Frame {
|
||||
tag: *b"COLE",
|
||||
payload: Vec::new(),
|
||||
nul_terminated: true,
|
||||
}
|
||||
.write(&mut bytes, Length::Total)
|
||||
.unwrap();
|
||||
assert_eq!(bytes.0, b"\x0a\0\0\0COLE:\0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_colons_in_payload_and_leaves_the_next_frame_unread() {
|
||||
let frame = Frame {
|
||||
tag: *b"DUNN",
|
||||
payload: b"one:two".to_vec(),
|
||||
nul_terminated: false,
|
||||
};
|
||||
let mut bytes = Vec::new();
|
||||
frame.write(&mut bytes, Length::Total).unwrap();
|
||||
bytes.extend_from_slice(b"\x0a\0\0\0COLE:\0");
|
||||
let mut reader = Cursor::new(bytes);
|
||||
assert_eq!(Frame::read(&mut reader, Length::Total).unwrap(), frame);
|
||||
assert_eq!(
|
||||
Frame::read(&mut reader, Length::Total).unwrap().tag,
|
||||
*b"COLE"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_lengths_before_reading_a_body() {
|
||||
for size in [0u32, 4, 8, MAX_FRAME_BYTES as u32 + 1, u32::MAX] {
|
||||
let mut reader = Cursor::new(size.to_le_bytes());
|
||||
assert_eq!(
|
||||
Frame::read(&mut reader, Length::Total).unwrap_err().kind(),
|
||||
io::ErrorKind::InvalidData
|
||||
);
|
||||
assert_eq!(reader.position(), 4);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_truncated_or_malformed_frames() {
|
||||
for bytes in [&b"\x0a\0\0"[..], &b"\x0a\0\0\0COLE:"[..]] {
|
||||
assert_eq!(
|
||||
Frame::read(&mut &bytes[..], Length::Total)
|
||||
.unwrap_err()
|
||||
.kind(),
|
||||
io::ErrorKind::UnexpectedEof
|
||||
);
|
||||
}
|
||||
for bytes in [
|
||||
b"\x0a\0\0\0COLE=\0",
|
||||
b"\x0a\0\0\0CO:E:x",
|
||||
b"\x0a\0\0\0CO\0E:\0",
|
||||
] {
|
||||
assert_eq!(
|
||||
Frame::read(&mut &bytes[..], Length::Total)
|
||||
.unwrap_err()
|
||||
.kind(),
|
||||
io::ErrorKind::InvalidData
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_output_before_writing_any_bytes() {
|
||||
let mut bytes = Vec::new();
|
||||
for frame in [
|
||||
Frame {
|
||||
tag: *b"DUNN",
|
||||
payload: vec![b'x'; MAX_FRAME_BYTES],
|
||||
nul_terminated: false,
|
||||
},
|
||||
Frame {
|
||||
tag: *b"DUNN",
|
||||
payload: b"one\0two".to_vec(),
|
||||
nul_terminated: false,
|
||||
},
|
||||
Frame {
|
||||
tag: *b"CO:E",
|
||||
payload: Vec::new(),
|
||||
nul_terminated: false,
|
||||
},
|
||||
] {
|
||||
assert!(frame.write(&mut bytes, Length::Total).is_err());
|
||||
assert!(bytes.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_the_observed_unterminated_launch_reply_shape() {
|
||||
let payload = vec![b'A'; 640];
|
||||
let mut bytes = 645u32.to_le_bytes().to_vec();
|
||||
bytes.extend_from_slice(b"DUNN:");
|
||||
bytes.extend_from_slice(&payload);
|
||||
let frame = Frame::read(&mut Fragmented(&bytes[..]), Length::Body).unwrap();
|
||||
assert_eq!(bytes.len(), 649);
|
||||
assert_eq!(frame.tag, *b"DUNN");
|
||||
assert_eq!(frame.payload, payload);
|
||||
assert!(!frame.nul_terminated);
|
||||
let mut encoded = Vec::new();
|
||||
frame.write(&mut encoded, Length::Body).unwrap();
|
||||
assert_eq!(encoded, bytes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn presents_a_complete_reply_to_the_pipe_in_one_write() {
|
||||
struct Writes(Vec<usize>);
|
||||
impl Write for Writes {
|
||||
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
|
||||
self.0.push(bytes.len());
|
||||
Ok(bytes.len())
|
||||
}
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
let mut writer = Writes(Vec::new());
|
||||
Frame {
|
||||
tag: *b"DUNN",
|
||||
payload: vec![b'A'; 640],
|
||||
nul_terminated: false,
|
||||
}
|
||||
.write(&mut writer, Length::Body)
|
||||
.unwrap();
|
||||
assert_eq!(writer.0, vec![649]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
#![cfg_attr(windows, windows_subsystem = "windows")]
|
||||
|
||||
#[cfg(windows)]
|
||||
fn log(message: &str) {
|
||||
use std::io::Write;
|
||||
let Ok(exe) = std::env::current_exe() else {
|
||||
return;
|
||||
};
|
||||
let path = exe.with_file_name("launcher.log");
|
||||
if let Ok(metadata) = std::fs::symlink_metadata(&path) {
|
||||
if !metadata.is_file() || metadata.len() > 65536 {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if let Ok(mut file) = std::fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(path)
|
||||
{
|
||||
let _ = writeln!(file, "{message}");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
use droiddeck_rockstar_launcher::{
|
||||
pipe::Listener,
|
||||
sdk::{Message, Session},
|
||||
wait_for_game, Frame, Length,
|
||||
};
|
||||
use std::io::{Read, Write};
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
let config_path = std::env::current_exe()?.with_file_name("launch.json");
|
||||
let mut data = Vec::new();
|
||||
std::fs::File::open(&config_path)?
|
||||
.take(65537)
|
||||
.read_to_end(&mut data)?;
|
||||
std::fs::remove_file(&config_path)?;
|
||||
if data.len() > 65536 {
|
||||
return Err("launch handoff exceeds size limit".into());
|
||||
}
|
||||
let config: serde_json::Value = serde_json::from_slice(&data)?;
|
||||
let text = |key| {
|
||||
config
|
||||
.get(key)
|
||||
.and_then(|value| value.as_str())
|
||||
.ok_or("missing launch field")
|
||||
};
|
||||
let expires = config
|
||||
.get("expiresAt")
|
||||
.and_then(|value| value.as_u64())
|
||||
.ok_or("missing launch expiry")?;
|
||||
let now = SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis() as u64;
|
||||
if expires <= now || expires - now > 120_000 {
|
||||
return Err("launch handoff expired".into());
|
||||
}
|
||||
let legacy = if config.get("pipe").is_some() {
|
||||
let pipe_name = text("pipe")?.to_owned();
|
||||
if !matches!(
|
||||
pipe_name.as_str(),
|
||||
"GTA3_Pipe" | "GTA-VC_Pipe" | "GTA_SA_Pipe" | "GTAIV_Pipe" | "LANOIRE_Pipe"
|
||||
) {
|
||||
return Err("invalid Rockstar launch pipe".into());
|
||||
}
|
||||
let material = text("material")?.as_bytes().to_vec();
|
||||
if material.len() != 640 || !material.iter().all(u8::is_ascii_hexdigit) {
|
||||
return Err("invalid launch material".into());
|
||||
}
|
||||
Some((pipe_name, material))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let session = config
|
||||
.get("session")
|
||||
.map(|value| -> Result<Session, Box<dyn std::error::Error>> {
|
||||
let account = value
|
||||
.get("accountId")
|
||||
.and_then(|v| v.as_str())
|
||||
.ok_or("missing SDK account")?
|
||||
.parse::<u64>()?;
|
||||
let identity = value
|
||||
.get("identity")
|
||||
.and_then(|v| v.as_str())
|
||||
.ok_or("missing SDK identity")?;
|
||||
let ticket = value
|
||||
.get("ticket")
|
||||
.and_then(|v| v.as_str())
|
||||
.ok_or("missing SDK ticket")?;
|
||||
let session = Session::new(account, identity.to_owned(), ticket.to_owned())?;
|
||||
Ok(if let Some(title) = value.get("titleId") {
|
||||
session.with_title(title.as_str().ok_or("invalid SDK title")?.to_owned())?
|
||||
} else {
|
||||
session
|
||||
})
|
||||
})
|
||||
.transpose()?;
|
||||
if legacy.is_none() && session.is_none() {
|
||||
return Err("missing launch protocol".into());
|
||||
}
|
||||
let root = std::fs::canonicalize(text("folder")?)?;
|
||||
let exe = std::fs::canonicalize(text("exe")?)?;
|
||||
let workdir = std::fs::canonicalize(text("workdir")?)?;
|
||||
if !exe.is_file()
|
||||
|| !exe.starts_with(&root)
|
||||
|| !workdir.starts_with(&root)
|
||||
|| legacy.is_some()
|
||||
&& !root.join("MTLX.dll").is_file()
|
||||
&& !exe.parent().unwrap_or(&root).join("MTLX.dll").is_file()
|
||||
{
|
||||
return Err("invalid Rockstar game path".into());
|
||||
}
|
||||
if let Some(index) =
|
||||
std::env::args().position(|arg| arg.eq_ignore_ascii_case("-launchTitleInFolder"))
|
||||
{
|
||||
let folder = std::env::args()
|
||||
.nth(index + 1)
|
||||
.ok_or("missing game folder")?;
|
||||
if std::fs::canonicalize(folder)? != root {
|
||||
return Err("Rockstar launch folder mismatch".into());
|
||||
}
|
||||
}
|
||||
let args = config
|
||||
.get("args")
|
||||
.and_then(|value| value.as_array())
|
||||
.ok_or("missing game arguments")?;
|
||||
let args: Result<Vec<&str>, _> = args
|
||||
.iter()
|
||||
.map(|value| value.as_str().ok_or("invalid game argument"))
|
||||
.collect();
|
||||
log("launch handoff accepted");
|
||||
let legacy = legacy
|
||||
.map(|(name, material)| {
|
||||
let terminated = matches!(name.as_str(), "GTAIV_Pipe" | "LANOIRE_Pipe");
|
||||
Listener::new(&name, Duration::from_secs(90))
|
||||
.map(|listener| (listener, material, terminated))
|
||||
})
|
||||
.transpose()?;
|
||||
let instance = std::process::id();
|
||||
let sdk_listener = if session.is_some() {
|
||||
Some(Listener::new(
|
||||
&format!("chrome.rgsc_gameinst_{instance}"),
|
||||
Duration::from_secs(90),
|
||||
)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let mut identity_listeners = Vec::new();
|
||||
if session.is_some() {
|
||||
for name in
|
||||
droiddeck_rockstar_launcher::identity::pipe_names(&mut std::fs::File::open(&exe)?)?
|
||||
{
|
||||
identity_listeners.push(Listener::new(&name, Duration::from_secs(90))?);
|
||||
}
|
||||
}
|
||||
log("launcher pipes created");
|
||||
let mut command = std::process::Command::new(exe);
|
||||
command.current_dir(workdir).args(args?);
|
||||
if session.is_some() {
|
||||
command.arg(format!("-processInstance={instance}"));
|
||||
}
|
||||
let mut child = command.spawn()?;
|
||||
let pid = child.id();
|
||||
log("game process started");
|
||||
for listener in identity_listeners {
|
||||
std::thread::spawn(move || {
|
||||
let serve = || -> std::io::Result<()> {
|
||||
let mut pipe = listener.accept()?.with_deadline(Duration::from_secs(90));
|
||||
if pipe.client_pid()? != pid {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::PermissionDenied,
|
||||
"unexpected launcher identity client",
|
||||
));
|
||||
}
|
||||
let reply = droiddeck_rockstar_launcher::identity::exchange(&mut pipe)?;
|
||||
pipe.write_all(&reply)?;
|
||||
log("launcher identity challenge complete");
|
||||
pipe = pipe.with_timeout(Duration::from_secs(86400));
|
||||
let mut progress = [0; 4096];
|
||||
while matches!(pipe.read(&mut progress), Ok(size) if size > 0) {}
|
||||
Ok(())
|
||||
};
|
||||
if let Err(error) = serve() {
|
||||
log(&format!("launcher identity exchange ended: {error}"));
|
||||
}
|
||||
});
|
||||
}
|
||||
let (result, handshake) = std::sync::mpsc::sync_channel(1);
|
||||
std::thread::spawn(move || {
|
||||
let serve = || -> std::io::Result<()> {
|
||||
if let Some((listener, material, terminated)) = legacy {
|
||||
let mut pipe = listener.accept()?;
|
||||
log("legacy launcher pipe connected");
|
||||
if pipe.client_pid()? != pid {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::PermissionDenied,
|
||||
"unexpected Rockstar pipe client",
|
||||
));
|
||||
}
|
||||
let request = Frame::read(&mut pipe, Length::Total)?;
|
||||
if request.tag != *b"COLE" || !request.payload.is_empty() {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
"unexpected Rockstar greeting",
|
||||
));
|
||||
}
|
||||
Frame {
|
||||
tag: *b"DUNN",
|
||||
payload: material,
|
||||
nul_terminated: terminated,
|
||||
}
|
||||
.write(
|
||||
&mut pipe,
|
||||
if terminated {
|
||||
Length::Total
|
||||
} else {
|
||||
Length::Body
|
||||
},
|
||||
)?;
|
||||
log("legacy launcher handshake complete");
|
||||
}
|
||||
if let (Some(listener), Some(session)) = (sdk_listener, session) {
|
||||
let mut pipe = listener.accept()?.with_deadline(Duration::from_secs(90));
|
||||
if pipe.client_pid()? != pid {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::PermissionDenied,
|
||||
"unexpected Rockstar SDK client",
|
||||
));
|
||||
}
|
||||
Message::hello(instance).write(&mut pipe)?;
|
||||
let mut authenticated = false;
|
||||
loop {
|
||||
let request = match Message::read(&mut pipe) {
|
||||
Ok(request) => request,
|
||||
Err(_) if authenticated => return Ok(()),
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
let responses = session.respond(&request)?;
|
||||
let sends_login = request.kind == 0x40000;
|
||||
for response in responses {
|
||||
response.write(&mut pipe)?;
|
||||
}
|
||||
if sends_login && !authenticated {
|
||||
authenticated = true;
|
||||
pipe = pipe.with_timeout(Duration::from_secs(86400));
|
||||
log("SDK login sent");
|
||||
let _ = result.send(Ok(()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
let outcome = serve();
|
||||
if let Err(error) = &outcome {
|
||||
log(&format!("launcher handshake failed: {error}"));
|
||||
}
|
||||
let _ = result.send(outcome);
|
||||
});
|
||||
Ok(wait_for_game(&mut child, handshake)?.code().unwrap_or(1))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
#[cfg(windows)]
|
||||
let code = match run() {
|
||||
Ok(code) => code,
|
||||
Err(error) => {
|
||||
log(&format!("launch failed: {error}"));
|
||||
eprintln!("Rockstar launch failed: {error}");
|
||||
1
|
||||
}
|
||||
};
|
||||
#[cfg(not(windows))]
|
||||
let code = 1;
|
||||
std::process::exit(code);
|
||||
}
|
||||
@@ -0,0 +1,450 @@
|
||||
use std::ffi::c_void;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use std::ptr::{null, null_mut};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
type Handle = *mut c_void;
|
||||
const INVALID_HANDLE: Handle = -1isize as Handle;
|
||||
const ERROR_IO_PENDING: i32 = 997;
|
||||
const ERROR_MORE_DATA: i32 = 234;
|
||||
|
||||
#[repr(C)]
|
||||
struct Overlapped {
|
||||
internal: usize,
|
||||
internal_high: usize,
|
||||
offset: u32,
|
||||
offset_high: u32,
|
||||
event: Handle,
|
||||
}
|
||||
|
||||
#[link(name = "kernel32")]
|
||||
extern "system" {
|
||||
fn CreateNamedPipeW(
|
||||
name: *const u16,
|
||||
open: u32,
|
||||
mode: u32,
|
||||
max: u32,
|
||||
output: u32,
|
||||
input: u32,
|
||||
timeout: u32,
|
||||
security: *const c_void,
|
||||
) -> Handle;
|
||||
fn ConnectNamedPipe(handle: Handle, overlapped: *mut Overlapped) -> i32;
|
||||
fn GetNamedPipeClientProcessId(handle: Handle, pid: *mut u32) -> i32;
|
||||
fn CreateFileW(
|
||||
name: *const u16,
|
||||
access: u32,
|
||||
share: u32,
|
||||
security: *const c_void,
|
||||
creation: u32,
|
||||
flags: u32,
|
||||
template: Handle,
|
||||
) -> Handle;
|
||||
fn WaitNamedPipeW(name: *const u16, timeout: u32) -> i32;
|
||||
fn CreateEventW(security: *const c_void, manual: i32, initial: i32, name: *const u16)
|
||||
-> Handle;
|
||||
fn CloseHandle(handle: Handle) -> i32;
|
||||
fn ReadFile(
|
||||
handle: Handle,
|
||||
buffer: *mut u8,
|
||||
size: u32,
|
||||
read: *mut u32,
|
||||
overlapped: *mut Overlapped,
|
||||
) -> i32;
|
||||
fn WriteFile(
|
||||
handle: Handle,
|
||||
buffer: *const u8,
|
||||
size: u32,
|
||||
written: *mut u32,
|
||||
overlapped: *mut Overlapped,
|
||||
) -> i32;
|
||||
fn WaitForSingleObject(handle: Handle, timeout: u32) -> u32;
|
||||
fn CancelIoEx(handle: Handle, overlapped: *mut Overlapped) -> i32;
|
||||
fn GetOverlappedResult(
|
||||
handle: Handle,
|
||||
overlapped: *mut Overlapped,
|
||||
transferred: *mut u32,
|
||||
wait: i32,
|
||||
) -> i32;
|
||||
}
|
||||
|
||||
struct OwnedHandle(Handle);
|
||||
|
||||
unsafe impl Send for OwnedHandle {}
|
||||
|
||||
impl Drop for OwnedHandle {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
CloseHandle(self.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Pipe {
|
||||
handle: OwnedHandle,
|
||||
timeout: Duration,
|
||||
deadline: Option<Instant>,
|
||||
}
|
||||
|
||||
pub struct Listener(Pipe);
|
||||
|
||||
impl Listener {
|
||||
pub fn new(name: &str, timeout: Duration) -> io::Result<Self> {
|
||||
if name.is_empty() || name.contains(['\\', '/', '\0']) {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"invalid pipe name",
|
||||
));
|
||||
}
|
||||
let path = format!(r"\\.\pipe\{name}");
|
||||
let wide: Vec<_> = std::ffi::OsStr::new(&path)
|
||||
.encode_wide()
|
||||
.chain(Some(0))
|
||||
.collect();
|
||||
let handle = unsafe {
|
||||
CreateNamedPipeW(
|
||||
wide.as_ptr(),
|
||||
3 | 0x4000_0000 | 0x0008_0000,
|
||||
8,
|
||||
1,
|
||||
65536,
|
||||
65536,
|
||||
0,
|
||||
null(),
|
||||
)
|
||||
};
|
||||
if handle == INVALID_HANDLE {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
Ok(Self(Pipe {
|
||||
handle: OwnedHandle(handle),
|
||||
timeout,
|
||||
deadline: None,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn accept(self) -> io::Result<Pipe> {
|
||||
let event = unsafe { CreateEventW(null(), 1, 0, null()) };
|
||||
if event.is_null() {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
let event = OwnedHandle(event);
|
||||
let mut overlapped = Overlapped {
|
||||
internal: 0,
|
||||
internal_high: 0,
|
||||
offset: 0,
|
||||
offset_high: 0,
|
||||
event: event.0,
|
||||
};
|
||||
if unsafe { ConnectNamedPipe(self.0.handle.0, &mut overlapped) } != 0 {
|
||||
return Ok(self.0);
|
||||
}
|
||||
let error = io::Error::last_os_error();
|
||||
if error.raw_os_error() == Some(535) {
|
||||
return Ok(self.0);
|
||||
}
|
||||
if error.raw_os_error() != Some(ERROR_IO_PENDING) {
|
||||
return Err(error);
|
||||
}
|
||||
let wait = unsafe {
|
||||
WaitForSingleObject(
|
||||
event.0,
|
||||
self.0.timeout.as_millis().min(u32::MAX as u128 - 1) as u32,
|
||||
)
|
||||
};
|
||||
let mut transferred = 0;
|
||||
if wait != 0 {
|
||||
let error = if wait == 258 {
|
||||
io::Error::new(io::ErrorKind::TimedOut, "waiting for Rockstar game")
|
||||
} else {
|
||||
io::Error::last_os_error()
|
||||
};
|
||||
unsafe {
|
||||
CancelIoEx(self.0.handle.0, &mut overlapped);
|
||||
GetOverlappedResult(self.0.handle.0, &mut overlapped, &mut transferred, 1);
|
||||
}
|
||||
return Err(error);
|
||||
}
|
||||
if unsafe { GetOverlappedResult(self.0.handle.0, &mut overlapped, &mut transferred, 0) }
|
||||
== 0
|
||||
{
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
Ok(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Pipe {
|
||||
pub fn with_timeout(mut self, timeout: Duration) -> Self {
|
||||
self.timeout = timeout;
|
||||
self.deadline = None;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_deadline(mut self, timeout: Duration) -> Self {
|
||||
self.timeout = timeout;
|
||||
self.deadline = Some(Instant::now() + timeout);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn client_pid(&self) -> io::Result<u32> {
|
||||
let mut pid = 0;
|
||||
if unsafe { GetNamedPipeClientProcessId(self.handle.0, &mut pid) } == 0 || pid == 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
Ok(pid)
|
||||
}
|
||||
pub fn connect(name: &str, timeout: Duration) -> io::Result<Self> {
|
||||
if name.is_empty() || name.contains(['\\', '/', '\0']) {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"invalid pipe name",
|
||||
));
|
||||
}
|
||||
let path = format!(r"\\.\pipe\{name}");
|
||||
let wide: Vec<_> = std::ffi::OsStr::new(&path)
|
||||
.encode_wide()
|
||||
.chain(Some(0))
|
||||
.collect();
|
||||
let deadline = Instant::now() + timeout;
|
||||
loop {
|
||||
let handle = unsafe {
|
||||
CreateFileW(
|
||||
wide.as_ptr(),
|
||||
0xc000_0000,
|
||||
0,
|
||||
null(),
|
||||
3,
|
||||
0x4000_0000,
|
||||
null_mut(),
|
||||
)
|
||||
};
|
||||
if handle != INVALID_HANDLE {
|
||||
return Ok(Self {
|
||||
handle: OwnedHandle(handle),
|
||||
timeout,
|
||||
deadline: None,
|
||||
});
|
||||
}
|
||||
let error = io::Error::last_os_error();
|
||||
if !matches!(error.raw_os_error(), Some(2 | 231)) {
|
||||
return Err(error);
|
||||
}
|
||||
let remaining = deadline.saturating_duration_since(Instant::now());
|
||||
if remaining.is_zero() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::TimedOut,
|
||||
"waiting for Rockstar pipe",
|
||||
));
|
||||
}
|
||||
unsafe {
|
||||
WaitNamedPipeW(wide.as_ptr(), remaining.as_millis().min(50) as u32);
|
||||
}
|
||||
std::thread::sleep(remaining.min(Duration::from_millis(2)));
|
||||
}
|
||||
}
|
||||
|
||||
fn transfer(&self, buffer: *mut u8, size: usize, writing: bool) -> io::Result<usize> {
|
||||
if size == 0 {
|
||||
return Ok(0);
|
||||
}
|
||||
let timeout = self
|
||||
.deadline
|
||||
.map(|deadline| deadline.saturating_duration_since(Instant::now()))
|
||||
.unwrap_or(self.timeout);
|
||||
if timeout.is_zero() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::TimedOut,
|
||||
"Rockstar pipe deadline exceeded",
|
||||
));
|
||||
}
|
||||
let event = unsafe { CreateEventW(null(), 1, 0, null()) };
|
||||
if event.is_null() {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
let event = OwnedHandle(event);
|
||||
let mut overlapped = Overlapped {
|
||||
internal: 0,
|
||||
internal_high: 0,
|
||||
offset: 0,
|
||||
offset_high: 0,
|
||||
event: event.0,
|
||||
};
|
||||
let mut transferred = 0;
|
||||
let size = size.min(u32::MAX as usize) as u32;
|
||||
let ok = unsafe {
|
||||
if writing {
|
||||
WriteFile(
|
||||
self.handle.0,
|
||||
buffer,
|
||||
size,
|
||||
&mut transferred,
|
||||
&mut overlapped,
|
||||
)
|
||||
} else {
|
||||
ReadFile(
|
||||
self.handle.0,
|
||||
buffer,
|
||||
size,
|
||||
&mut transferred,
|
||||
&mut overlapped,
|
||||
)
|
||||
}
|
||||
};
|
||||
if ok != 0 {
|
||||
return Ok(transferred as usize);
|
||||
}
|
||||
let error = io::Error::last_os_error();
|
||||
if error.raw_os_error() == Some(ERROR_MORE_DATA) && !writing && transferred > 0 {
|
||||
return Ok(transferred as usize);
|
||||
}
|
||||
if error.raw_os_error() != Some(ERROR_IO_PENDING) {
|
||||
return Err(error);
|
||||
}
|
||||
let wait = unsafe {
|
||||
WaitForSingleObject(
|
||||
event.0,
|
||||
timeout.as_millis().min(u32::MAX as u128 - 1) as u32,
|
||||
)
|
||||
};
|
||||
if wait != 0 {
|
||||
let error = if wait == 258 {
|
||||
io::Error::new(io::ErrorKind::TimedOut, "Rockstar pipe operation timed out")
|
||||
} else {
|
||||
io::Error::last_os_error()
|
||||
};
|
||||
unsafe {
|
||||
CancelIoEx(self.handle.0, &mut overlapped);
|
||||
GetOverlappedResult(self.handle.0, &mut overlapped, &mut transferred, 1);
|
||||
}
|
||||
return Err(error);
|
||||
}
|
||||
let ok =
|
||||
unsafe { GetOverlappedResult(self.handle.0, &mut overlapped, &mut transferred, 0) };
|
||||
if ok != 0 {
|
||||
return Ok(transferred as usize);
|
||||
}
|
||||
let error = io::Error::last_os_error();
|
||||
if error.raw_os_error() == Some(ERROR_MORE_DATA) && !writing && transferred > 0 {
|
||||
return Ok(transferred as usize);
|
||||
}
|
||||
Err(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for Pipe {
|
||||
fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
|
||||
self.transfer(buffer.as_mut_ptr(), buffer.len(), false)
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for Pipe {
|
||||
fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
|
||||
self.transfer(buffer.as_ptr() as *mut u8, buffer.len(), true)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{Frame, Length};
|
||||
use std::sync::mpsc;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
fn name() -> String {
|
||||
format!(
|
||||
"dd-rockstar-test-{}-{}",
|
||||
std::process::id(),
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos()
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn initialization_deadline_survives_partial_reads() {
|
||||
let name = name();
|
||||
let listener = Listener::new(&name, Duration::from_secs(2)).unwrap();
|
||||
let worker = std::thread::spawn(move || {
|
||||
let mut server = listener
|
||||
.accept()
|
||||
.unwrap()
|
||||
.with_deadline(Duration::from_millis(150));
|
||||
let mut byte = [0];
|
||||
server.read_exact(&mut byte).unwrap();
|
||||
std::thread::sleep(Duration::from_millis(180));
|
||||
assert_eq!(
|
||||
server.read(&mut byte).unwrap_err().kind(),
|
||||
io::ErrorKind::TimedOut
|
||||
);
|
||||
});
|
||||
let mut client = Pipe::connect(&name, Duration::from_secs(2)).unwrap();
|
||||
client.write_all(&[1]).unwrap();
|
||||
worker.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn real_pipe_exchanges_both_framing_modes_and_identifies_the_client() {
|
||||
let name = name();
|
||||
let listener = Listener::new(&name, Duration::from_secs(2)).unwrap();
|
||||
let worker = std::thread::spawn(move || {
|
||||
let mut server = listener.accept().unwrap();
|
||||
assert_eq!(server.client_pid().unwrap(), std::process::id());
|
||||
assert_eq!(
|
||||
Frame::read(&mut server, Length::Total).unwrap().tag,
|
||||
*b"COLE"
|
||||
);
|
||||
Frame {
|
||||
tag: *b"DUNN",
|
||||
payload: vec![b'A'; 640],
|
||||
nul_terminated: false,
|
||||
}
|
||||
.write(&mut server, Length::Body)
|
||||
.unwrap();
|
||||
});
|
||||
let mut client = Pipe::connect(&name, Duration::from_secs(2)).unwrap();
|
||||
Frame {
|
||||
tag: *b"COLE",
|
||||
payload: Vec::new(),
|
||||
nul_terminated: true,
|
||||
}
|
||||
.write(&mut client, Length::Total)
|
||||
.unwrap();
|
||||
let response = Frame::read(&mut client, Length::Body).unwrap();
|
||||
assert_eq!(response.payload.len(), 640);
|
||||
assert!(!response.nul_terminated);
|
||||
worker.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timed_out_accept_releases_the_first_pipe_instance() {
|
||||
let name = name();
|
||||
let listener = Listener::new(&name, Duration::from_millis(20)).unwrap();
|
||||
assert!(matches!(listener.accept(), Err(error) if error.kind() == io::ErrorKind::TimedOut));
|
||||
assert!(Listener::new(&name, Duration::from_millis(20)).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timed_out_read_cancels_the_pending_operation() {
|
||||
let name = name();
|
||||
let listener = Listener::new(&name, Duration::from_secs(2)).unwrap();
|
||||
let (done, wait) = mpsc::channel();
|
||||
let worker = std::thread::spawn(move || {
|
||||
let _server = listener.accept().unwrap();
|
||||
wait.recv_timeout(Duration::from_secs(2)).unwrap();
|
||||
});
|
||||
let mut client = Pipe::connect(&name, Duration::from_millis(30)).unwrap();
|
||||
assert_eq!(
|
||||
client.read(&mut [0u8; 4]).unwrap_err().kind(),
|
||||
io::ErrorKind::TimedOut
|
||||
);
|
||||
done.send(()).unwrap();
|
||||
worker.join().unwrap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
use std::io::{self, Read, Write};
|
||||
|
||||
const HEADER: usize = 16;
|
||||
const MAX_BYTES: usize = 64 * 1024;
|
||||
const CONTROL: i32 = i32::MAX;
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub struct Message {
|
||||
pub routing: i32,
|
||||
pub kind: u32,
|
||||
pub flags: u32,
|
||||
pub payload: Vec<u8>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Message {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
formatter
|
||||
.debug_struct("Message")
|
||||
.field("routing", &self.routing)
|
||||
.field("kind", &self.kind)
|
||||
.field("flags", &self.flags)
|
||||
.field("payload_bytes", &self.payload.len())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
fn invalid() -> io::Error {
|
||||
io::Error::new(io::ErrorKind::InvalidData, "invalid Rockstar SDK message")
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn read(reader: &mut impl Read) -> io::Result<Self> {
|
||||
let mut header = [0; HEADER];
|
||||
reader.read_exact(&mut header)?;
|
||||
let size = u32::from_le_bytes(header[..4].try_into().unwrap()) as usize;
|
||||
if size > MAX_BYTES - HEADER {
|
||||
return Err(invalid());
|
||||
}
|
||||
let mut payload = vec![0; size];
|
||||
reader.read_exact(&mut payload)?;
|
||||
Ok(Self {
|
||||
routing: i32::from_le_bytes(header[4..8].try_into().unwrap()),
|
||||
kind: u32::from_le_bytes(header[8..12].try_into().unwrap()),
|
||||
flags: u32::from_le_bytes(header[12..16].try_into().unwrap()),
|
||||
payload,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn write(&self, writer: &mut impl Write) -> io::Result<()> {
|
||||
if self.payload.len() > MAX_BYTES - HEADER {
|
||||
return Err(invalid());
|
||||
}
|
||||
let mut bytes = Vec::with_capacity(HEADER + self.payload.len());
|
||||
bytes.extend_from_slice(&(self.payload.len() as u32).to_le_bytes());
|
||||
bytes.extend_from_slice(&self.routing.to_le_bytes());
|
||||
bytes.extend_from_slice(&self.kind.to_le_bytes());
|
||||
bytes.extend_from_slice(&self.flags.to_le_bytes());
|
||||
bytes.extend_from_slice(&self.payload);
|
||||
writer.write_all(&bytes)
|
||||
}
|
||||
|
||||
fn control(kind: u32, payload: Payload) -> Self {
|
||||
Self {
|
||||
routing: CONTROL,
|
||||
kind,
|
||||
flags: 2,
|
||||
payload: payload.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hello(pid: u32) -> Self {
|
||||
Self {
|
||||
routing: -2,
|
||||
kind: 0xffff,
|
||||
flags: 2,
|
||||
payload: pid.to_le_bytes().to_vec(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Payload(Vec<u8>);
|
||||
|
||||
impl Payload {
|
||||
fn raw(mut self, bytes: &[u8]) -> Self {
|
||||
self.0.resize((self.0.len() + 3) & !3, 0);
|
||||
self.0.extend_from_slice(bytes);
|
||||
self
|
||||
}
|
||||
|
||||
fn u32(self, value: u32) -> Self {
|
||||
self.raw(&value.to_le_bytes())
|
||||
}
|
||||
fn u64(self, value: u64) -> Self {
|
||||
self.raw(&value.to_le_bytes())
|
||||
}
|
||||
fn string(self, value: &str) -> Self {
|
||||
self.u32(value.len() as u32).raw(value.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Session {
|
||||
account: u64,
|
||||
identity: String,
|
||||
ticket: String,
|
||||
title: Option<String>,
|
||||
api: std::cell::Cell<u32>,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
pub fn new(account: u64, identity: String, ticket: String) -> io::Result<Self> {
|
||||
if account == 0
|
||||
|| identity.is_empty()
|
||||
|| identity.len() > 512
|
||||
|| ticket.is_empty()
|
||||
|| ticket.len() > 48 * 1024
|
||||
|| identity.contains('\0')
|
||||
|| ticket.contains('\0')
|
||||
{
|
||||
return Err(invalid());
|
||||
}
|
||||
Ok(Self {
|
||||
account,
|
||||
identity,
|
||||
ticket,
|
||||
title: None,
|
||||
api: std::cell::Cell::new(3),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn with_title(mut self, title: String) -> io::Result<Self> {
|
||||
if title.is_empty()
|
||||
|| title.len() > 64
|
||||
|| !title.as_bytes()[0].is_ascii_lowercase()
|
||||
|| !title
|
||||
.bytes()
|
||||
.all(|value| value.is_ascii_lowercase() || value.is_ascii_digit() || value == b'_')
|
||||
{
|
||||
return Err(invalid());
|
||||
}
|
||||
self.title = Some(title);
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn app_sync(&self, challenge: u64) -> io::Result<Message> {
|
||||
let mut payload = Payload::default().u32(u32::from(self.title.is_some()));
|
||||
if let Some(title) = &self.title {
|
||||
payload = payload
|
||||
.u64(u64::MAX)
|
||||
.u64(0)
|
||||
.u64(0)
|
||||
.string(title)
|
||||
.u32(1)
|
||||
.u32(1)
|
||||
.string("");
|
||||
}
|
||||
let mut inner = Vec::new();
|
||||
Message::control(0x80006, payload).write(&mut inner)?;
|
||||
Ok(Message::control(
|
||||
0x80002,
|
||||
Payload::default().u64(challenge).string(&base64(&inner)),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn login(&self) -> Vec<Message> {
|
||||
let offset = u32::from(self.api.get() >= 4);
|
||||
vec![
|
||||
Message::control(
|
||||
0x30001 + offset,
|
||||
Payload::default().u32(3).u64(self.account).u32(0),
|
||||
),
|
||||
Message::control(
|
||||
0x30002 + offset,
|
||||
Payload::default()
|
||||
.u64(self.account)
|
||||
.u32(0)
|
||||
.string(&self.identity),
|
||||
),
|
||||
Message::control(
|
||||
0x30003 + offset,
|
||||
Payload::default()
|
||||
.string(&self.identity)
|
||||
.string(&self.ticket),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
pub fn respond(&self, message: &Message) -> io::Result<Vec<Message>> {
|
||||
match message.kind {
|
||||
0x80000 => {
|
||||
if message.payload.len() != 8 {
|
||||
return Err(invalid());
|
||||
}
|
||||
let challenge = u64::from_le_bytes(message.payload[..8].try_into().unwrap());
|
||||
Ok(vec![self.app_sync(challenge)?])
|
||||
}
|
||||
0x40000 => {
|
||||
if message.payload.len() < 4 {
|
||||
return Err(invalid());
|
||||
}
|
||||
let context = u32::from_le_bytes(message.payload[..4].try_into().unwrap());
|
||||
self.api.set(context);
|
||||
let mut result = self.login();
|
||||
result.push(Message::control(0x30000, Payload::default().u32(context)));
|
||||
Ok(result)
|
||||
}
|
||||
0x40005 | 0x40007 => Ok(vec![Message::control(
|
||||
0x30004 + u32::from(self.api.get() >= 4),
|
||||
Payload::default(),
|
||||
)]),
|
||||
_ => Ok(Vec::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn base64(bytes: &[u8]) -> String {
|
||||
const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
let mut output = String::with_capacity(bytes.len().div_ceil(3) * 4);
|
||||
for part in bytes.chunks(3) {
|
||||
let value = ((part[0] as u32) << 16)
|
||||
| ((part.get(1).copied().unwrap_or(0) as u32) << 8)
|
||||
| part.get(2).copied().unwrap_or(0) as u32;
|
||||
output.push(ALPHABET[(value >> 18) as usize] as char);
|
||||
output.push(ALPHABET[((value >> 12) & 63) as usize] as char);
|
||||
output.push(if part.len() > 1 {
|
||||
ALPHABET[((value >> 6) & 63) as usize] as char
|
||||
} else {
|
||||
'='
|
||||
});
|
||||
output.push(if part.len() > 2 {
|
||||
ALPHABET[(value & 63) as usize] as char
|
||||
} else {
|
||||
'='
|
||||
});
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::Cursor;
|
||||
|
||||
#[test]
|
||||
fn app_sync_echoes_challenge_without_inventing_owned_titles() {
|
||||
let session = Session::new(123, "name".into(), "ticket".into()).unwrap();
|
||||
let request = Message::control(0x80000, Payload::default().u64(42));
|
||||
let response = session.respond(&request).unwrap().remove(0);
|
||||
assert_eq!(response.kind, 0x80002);
|
||||
assert_eq!(&response.payload[..8], &42u64.to_le_bytes());
|
||||
assert_eq!(&response.payload[12..], b"BAAAAP///38GAAgAAgAAAAAAAAA=");
|
||||
assert_eq!(base64(b"f"), "Zg==");
|
||||
assert_eq!(base64(b"fo"), "Zm8=");
|
||||
assert_eq!(base64(b"foo"), "Zm9v");
|
||||
assert!(session.with_title("../game".into()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transport_hello_matches_wire_layout() {
|
||||
let mut bytes = Vec::new();
|
||||
Message::hello(0x1234).write(&mut bytes).unwrap();
|
||||
assert_eq!(
|
||||
bytes,
|
||||
[4, 0, 0, 0, 254, 255, 255, 255, 255, 255, 0, 0, 2, 0, 0, 0, 52, 18, 0, 0]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn login_preserves_string_lengths_and_alignment() {
|
||||
let session = Session::new(0x1122334455667788, "a@b".into(), "ticket".into()).unwrap();
|
||||
let login = session.login();
|
||||
assert_eq!(
|
||||
login[0].payload,
|
||||
[3, 0, 0, 0, 136, 119, 102, 85, 68, 51, 34, 17, 0, 0, 0, 0]
|
||||
);
|
||||
assert_eq!(
|
||||
login[1].payload,
|
||||
[136, 119, 102, 85, 68, 51, 34, 17, 0, 0, 0, 0, 3, 0, 0, 0, 97, 64, 98]
|
||||
);
|
||||
assert_eq!(
|
||||
login[2].payload,
|
||||
[3, 0, 0, 0, 97, 64, 98, 0, 6, 0, 0, 0, 116, 105, 99, 107, 101, 116]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn init_sends_authenticated_state_before_launcher_hello() {
|
||||
let session = Session::new(123, "name".into(), "ticket".into()).unwrap();
|
||||
for (api, expected) in [
|
||||
(3, [0x30001, 0x30002, 0x30003, 0x30000]),
|
||||
(4, [0x30002, 0x30003, 0x30004, 0x30000]),
|
||||
] {
|
||||
let request = Message::control(0x40000, Payload::default().u32(api));
|
||||
let result = session.respond(&request).unwrap();
|
||||
assert_eq!(result.iter().map(|m| m.kind).collect::<Vec<_>>(), expected);
|
||||
assert_eq!(
|
||||
result[0].payload,
|
||||
[3, 0, 0, 0, 123, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
);
|
||||
assert_eq!(result[3].payload, api.to_le_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_frames_and_sessions_are_rejected() {
|
||||
assert!(Session::new(0, "name".into(), "ticket".into()).is_err());
|
||||
assert!(Session::new(123, "name".into(), "".into()).is_err());
|
||||
let session = Session::new(123, "name".into(), "ticket".into()).unwrap();
|
||||
assert!(session
|
||||
.respond(&Message::control(0x40000, Payload::default()))
|
||||
.is_err());
|
||||
assert!(Message::read(&mut Cursor::new(vec![255; 16])).is_err());
|
||||
assert!(Message::read(&mut Cursor::new(vec![0; 15])).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn concatenated_messages_remain_separate() {
|
||||
let session = Session::new(123, "name".into(), "ticket".into()).unwrap();
|
||||
let mut bytes = Vec::new();
|
||||
for message in session.login() {
|
||||
message.write(&mut bytes).unwrap();
|
||||
}
|
||||
let mut reader = Cursor::new(bytes);
|
||||
for message in session.login() {
|
||||
assert_eq!(Message::read(&mut reader).unwrap(), message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -198,6 +198,155 @@ class ConfigAndMenuTest(Temp):
|
||||
self.assertEqual(path.stat().st_mtime_ns, mtime)
|
||||
|
||||
|
||||
class DriveTest(Temp):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.root = str(self.tmp / "Steam")
|
||||
self.devices = Path(BNET["dosdevices"](self.root))
|
||||
self.card = self.tmp / "card"
|
||||
self.library = self.card / "Games/Battle.net"
|
||||
self.library.mkdir(parents=True)
|
||||
self.running = []
|
||||
patcher = mock.patch.dict(BNET, {"say": lambda message: None, "wine_processes": lambda pfx, proc="/proc": list(self.running),
|
||||
"volume_root": lambda path: str(self.card) if BNET["inside"](path, str(self.card)) else "/"})
|
||||
patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
def test_a_volume_ends_where_the_device_changes(self):
|
||||
devices = {"/": 1, "/mnt": 1, "/mnt/card": 7, "/mnt/card/Games": 7, "/mnt/card/Games/Battle.net": 7, "/root": 1, "/root/Games": 1}
|
||||
with mock.patch.object(os, "stat", lambda path: mock.Mock(st_dev=devices[path])):
|
||||
volume_root = runpy.run_path(str(BIN / "droiddeck-battlenet"))["volume_root"]
|
||||
self.assertEqual(volume_root("/mnt/card/Games/Battle.net"), "/mnt/card")
|
||||
self.assertEqual(volume_root("/mnt/card"), "/mnt/card")
|
||||
self.assertEqual(volume_root("/root/Games"), "/")
|
||||
self.assertEqual(volume_root("/"), "/")
|
||||
|
||||
def test_a_library_on_the_root_volume_keeps_its_path_and_gets_no_drive(self):
|
||||
library = self.tmp / "root/Games/Battle.net"
|
||||
library.mkdir(parents=True)
|
||||
self.assertEqual(BNET["install_path"](self.root, str(library) + "/"), "Z:" + str(library))
|
||||
self.assertFalse(self.devices.exists())
|
||||
|
||||
def test_a_library_on_another_volume_gets_a_drive_at_the_volume_root(self):
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library)), "D:\\Games\\Battle.net")
|
||||
self.assertEqual(os.readlink(self.devices / "d:"), str(self.card))
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library / "World of Warcraft")), "D:\\Games\\Battle.net\\World of Warcraft")
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.card)), "D:\\")
|
||||
self.assertEqual(sorted(os.listdir(self.devices)), ["d:"])
|
||||
|
||||
def test_drives_in_use_and_the_ones_proton_owns_are_left_alone(self):
|
||||
self.devices.mkdir(parents=True)
|
||||
os.symlink("/somewhere/else", self.devices / "d:")
|
||||
os.symlink(str(self.card), self.devices / "s:")
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library)), "E:\\Games\\Battle.net")
|
||||
self.assertEqual(os.readlink(self.devices / "d:"), "/somewhere/else")
|
||||
for letter in BNET["DRIVES"]:
|
||||
if not os.path.lexists(self.devices / (letter + ":")):
|
||||
os.symlink("/somewhere/else", self.devices / (letter + ":"))
|
||||
os.unlink(self.devices / "e:")
|
||||
os.symlink("/somewhere/else", self.devices / "e:")
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library)), "Z:" + str(self.library))
|
||||
self.assertNotIn("s", BNET["DRIVES"])
|
||||
self.assertNotIn("t", BNET["DRIVES"])
|
||||
|
||||
def test_a_running_prefix_gets_no_new_drive_but_uses_the_ones_it_started_with(self):
|
||||
self.running = [(5, ["C:\\Program Files (x86)\\Battle.net\\Battle.net.exe"])]
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library)), "Z:" + str(self.library))
|
||||
self.assertFalse(self.devices.exists())
|
||||
self.running = []
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library)), "D:\\Games\\Battle.net")
|
||||
self.running = [(5, ["C:\\Program Files (x86)\\Battle.net\\Battle.net.exe"])]
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library)), "D:\\Games\\Battle.net")
|
||||
|
||||
def test_a_drive_mapped_inside_the_volume_is_reused(self):
|
||||
self.devices.mkdir(parents=True)
|
||||
os.symlink(str(self.card / "Games"), self.devices / "g:")
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library)), "G:\\Battle.net")
|
||||
self.assertEqual(sorted(os.listdir(self.devices)), ["g:"])
|
||||
|
||||
def test_the_config_stores_the_drive_path_once(self):
|
||||
path = self.tmp / ("Steam/steamapps/compatdata/%s/pfx/drive_c/users/steamuser/AppData/Roaming/Battle.net/Battle.net.config"
|
||||
% BNET["APP_ID"])
|
||||
BNET["write_config"](self.root)
|
||||
self.assertEqual(BNET["write_config"](self.root, str(self.library)), "library")
|
||||
self.assertEqual(json.loads(path.read_text())["Client"]["Install"]["DefaultInstallPath"], "D:\\Games\\Battle.net")
|
||||
self.assertIsNone(BNET["write_config"](self.root, str(self.library)))
|
||||
|
||||
def test_registered_paths_resolve_through_their_drive(self):
|
||||
BNET["install_path"](self.root, str(self.library))
|
||||
self.assertEqual(BNET["windows_path"](self.root, "D:/Games/Battle.net/World of Warcraft"),
|
||||
str(self.library / "World of Warcraft"))
|
||||
self.assertEqual(BNET["windows_path"](self.root, "Z:\\mnt\\card\\World of Warcraft"), "/mnt/card/World of Warcraft")
|
||||
self.assertEqual(BNET["windows_path"](self.root, "C:/Program Files (x86)/World of Warcraft"),
|
||||
os.path.join(BNET["drive_c"](self.root), "Program Files (x86)/World of Warcraft"))
|
||||
self.assertIsNone(BNET["windows_path"](self.root, "E:/Games/X"))
|
||||
self.assertIsNone(BNET["windows_path"](self.root, "D:/../etc"))
|
||||
self.assertEqual(BNET["windows_path"](self.root, "D:\\"), str(self.library.parent.parent))
|
||||
|
||||
def field(self, number, value):
|
||||
varint = BNET["encode_varint"]
|
||||
return varint(number << 3 | 2) + varint(len(value)) + value
|
||||
|
||||
def product(self, uid, folder, filler=b""):
|
||||
settings = self.field(1, folder.encode()) + self.field(2, b"us") + b"\x18\x02" + filler
|
||||
return self.field(1, self.field(1, uid.encode()) + self.field(2, uid.encode()) + self.field(3, settings)
|
||||
+ b"\x21" + bytes(8) + b"\x2d" + bytes(4))
|
||||
|
||||
def database(self, *products):
|
||||
path = Path(BNET["drive_c"](self.root)) / "ProgramData/Battle.net/Agent/product.db"
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_bytes(b"".join(products) + self.field(7, b"tail"))
|
||||
path.chmod(0o600)
|
||||
return path
|
||||
|
||||
def test_games_registered_through_the_root_drive_move_to_their_volume_drive(self):
|
||||
game = self.library / "World of Warcraft"
|
||||
game.mkdir()
|
||||
other = self.tmp / "root/Games/Other"
|
||||
other.mkdir(parents=True)
|
||||
client = self.product("battle.net", "C:/Program Files (x86)/Battle.net")
|
||||
local = self.product("other", "Z:" + str(other))
|
||||
gone = self.product("gone", "Z:" + str(self.card / "Missing"))
|
||||
path = self.database(client, self.product("wow", "Z:" + str(game), bytes(200)), local, gone)
|
||||
self.assertTrue(BNET["move_installs"](self.root))
|
||||
self.assertEqual(path.read_bytes(), client + self.product("wow", "D:/Games/Battle.net/World of Warcraft", bytes(200))
|
||||
+ local + gone + self.field(7, b"tail"))
|
||||
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
|
||||
self.assertEqual(os.readlink(self.devices / "d:"), str(self.card))
|
||||
self.assertFalse(BNET["move_installs"](self.root))
|
||||
|
||||
def test_registrations_stay_while_the_prefix_runs_or_the_database_is_unreadable(self):
|
||||
game = self.library / "World of Warcraft"
|
||||
game.mkdir()
|
||||
path = self.database(self.product("wow", "Z:" + str(game)))
|
||||
before = path.read_bytes()
|
||||
self.running = [(5, ["C:\\ProgramData\\Battle.net\\Agent\\Agent.exe"])]
|
||||
self.assertFalse(BNET["move_installs"](self.root))
|
||||
self.assertEqual(path.read_bytes(), before)
|
||||
self.running = []
|
||||
for broken in (before[:-3], b"\x0a\xff", b"\x0b\x01"):
|
||||
path.write_bytes(broken)
|
||||
self.assertFalse(BNET["move_installs"](self.root))
|
||||
self.assertEqual(path.read_bytes(), broken)
|
||||
os.unlink(path)
|
||||
self.assertFalse(BNET["move_installs"](self.root))
|
||||
|
||||
def test_a_database_written_while_it_was_being_read_is_left_as_written(self):
|
||||
game = self.library / "World of Warcraft"
|
||||
game.mkdir()
|
||||
path = self.database(self.product("wow", "Z:" + str(game)))
|
||||
newer = path.read_bytes() + self.field(7, b"newer")
|
||||
|
||||
def processes(pfx, proc="/proc"):
|
||||
path.write_bytes(newer)
|
||||
return []
|
||||
|
||||
with mock.patch.dict(BNET, {"wine_processes": processes}):
|
||||
self.assertFalse(BNET["move_installs"](self.root))
|
||||
self.assertEqual(path.read_bytes(), newer)
|
||||
self.assertTrue(BNET["move_installs"](self.root))
|
||||
|
||||
|
||||
class GamepadTest(Temp):
|
||||
def registry(self, *entries):
|
||||
rows = ["WINE REGISTRY Version 2"]
|
||||
@@ -772,7 +921,7 @@ class Harness(Temp):
|
||||
"stop": lambda processes, wait=0: self.calls.append(("stop", sorted(pid for pid, _ in processes))),
|
||||
"start": lambda exe, verb=None: self.calls.append(("start", os.path.basename(exe), verb)) or self.Proton(),
|
||||
"spawn": lambda program, verb, args=(): self.calls.append(("spawn", os.path.basename(program), verb, list(args))),
|
||||
"acquire": lambda folder=None: self.lock,
|
||||
"acquire": lambda folder=None: self.lock, "volume_root": lambda path: "/",
|
||||
"POLL": 0, "READY_SETTLE": 0, "GAME_GRACE": 0, "CLIENT_WAIT": 0,
|
||||
})
|
||||
patcher.start()
|
||||
@@ -1170,6 +1319,7 @@ class AgentTest(Temp):
|
||||
script = BNET["AGENT_SCRIPT"]
|
||||
self.assertIn("port = a(0), action = a(1), uid = a(2), product = a(3), dir = a(4)", script)
|
||||
self.assertIn("lang = a(5), country = a(6), region = a(7), out = a(8)", script)
|
||||
self.assertIn(r'''"game_dir":"' + dir.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '","geo''', script)
|
||||
self.assertIn('"--internalclienttools"', (BIN / "droiddeck-battlenet").read_text())
|
||||
|
||||
def test_the_driver_asks_blizzards_chinese_repository_for_chinese_installs(self):
|
||||
@@ -1230,6 +1380,40 @@ class AgentJobTest(Harness):
|
||||
self.assertEqual([kind for kind, _ in self.lines], ["STATE", "PROGRESS", "DONE"])
|
||||
self.assertEqual(json.loads(self.lines[-1][1]), {"install_dir": "Z:" + folder, "exe": "WarcraftRumble.exe", "version": "16.1.2.119725"})
|
||||
|
||||
def test_an_install_on_another_volume_is_handed_over_by_its_drive_once_the_prefix_is_stopped(self):
|
||||
self.install()
|
||||
card = self.tmp / "card"
|
||||
folder = str(card / "Games/Battle.net/Warcraft Rumble")
|
||||
self.files = {".json": json.dumps({"progress": 1.0, "download_complete": True, "state": 1004}),
|
||||
".game": json.dumps({"installed": True, "install_dir": "D:/Games/Battle.net/Warcraft Rumble", "local_version": "1",
|
||||
"binaries": {"game": {"relative_path": "WarcraftRumble.exe"}}})}
|
||||
self.states = [[(7, self.AGENT)]]
|
||||
events = []
|
||||
real_map = BNET["map_drive"]
|
||||
|
||||
def acquire(folder=None):
|
||||
events.append("lock")
|
||||
return self.lock
|
||||
|
||||
def stop(pfx):
|
||||
events.append(("stop", not self.lock.closed))
|
||||
self.states = [[]]
|
||||
return True
|
||||
|
||||
def map_drive(root, volume):
|
||||
events.append(("map", not self.lock.closed))
|
||||
return real_map(root, volume)
|
||||
|
||||
with mock.patch.dict(BNET, {"volume_root": lambda path: str(card), "stop_leftovers": stop, "map_drive": map_drive,
|
||||
"acquire": acquire}):
|
||||
code = BNET["agent_job"](self.root, "install", "gryphon", "gryphon", folder, "enUS", "USA", "US", poll=0)
|
||||
self.assertEqual(code, 0)
|
||||
self.assertEqual(events, ["lock", ("stop", True), ("map", True)])
|
||||
self.assertTrue(self.lock.closed)
|
||||
cmd = self.started[0]
|
||||
self.assertEqual(cmd[cmd.index("--") + 6], "D:\\Games\\Battle.net\\Warcraft Rumble")
|
||||
self.assertEqual(os.readlink(os.path.join(BNET["dosdevices"](self.root), "d:")), str(card))
|
||||
|
||||
def test_an_agent_error_is_reported_with_its_code(self):
|
||||
self.install()
|
||||
self.files = {".json": json.dumps({"error": 2421, "progress": 0.0})}
|
||||
@@ -1257,6 +1441,138 @@ class AgentJobTest(Harness):
|
||||
self.assertEqual(BNET["agent_job"](self.root, "update", "gryphon", poll=0), 1)
|
||||
self.assertEqual(self.lines[-1][0], "ERROR")
|
||||
|
||||
def test_a_job_that_stops_moving_is_ended_as_stalled_and_its_agent_stopped(self):
|
||||
self.install()
|
||||
self.files = {".json": json.dumps({"state": 1003, "paused": True, "download_current": [10, 0, 0], "download_total": [99, 0, 0]})}
|
||||
killed = []
|
||||
stopped = []
|
||||
|
||||
class Hung:
|
||||
returncode = None
|
||||
|
||||
def __init__(self, cmd, **kwargs):
|
||||
base = cmd[-1][2:].replace("\\", "/")
|
||||
with open(base + ".json", "wb") as f:
|
||||
f.write(self_files[".json"].encode("utf-16"))
|
||||
|
||||
def poll(self):
|
||||
return None
|
||||
|
||||
def kill(self):
|
||||
killed.append(True)
|
||||
|
||||
def wait(self):
|
||||
return -9
|
||||
|
||||
self_files = self.files
|
||||
with mock.patch.object(BNET["subprocess"], "Popen", Hung), \
|
||||
mock.patch.dict(BNET, {"wine_processes": lambda pfx: [(5, ["agent.exe"])], "stop": lambda p: stopped.append(p)}):
|
||||
code = BNET["agent_job"](self.root, "install", "s1", "s1", str(self.tmp / "S1"), "enUS", "USA", "US", poll=0, stall=0.05)
|
||||
self.assertEqual(code, 6)
|
||||
self.assertEqual(killed, [True])
|
||||
self.assertEqual(stopped, [[(5, ["agent.exe"])]])
|
||||
self.assertEqual(self.lines[-1], ("ERROR", "stalled"))
|
||||
|
||||
def test_a_stalled_job_leaves_a_game_or_client_in_the_prefix_running(self):
|
||||
self.install()
|
||||
self.files = {".json": json.dumps({"state": 1003, "download_current": [10, 0, 0], "download_total": [99, 0, 0]})}
|
||||
stopped = []
|
||||
calls = []
|
||||
|
||||
class Hung:
|
||||
returncode = None
|
||||
|
||||
def __init__(self, cmd, **kwargs):
|
||||
calls.append(True)
|
||||
|
||||
def poll(self):
|
||||
return None
|
||||
|
||||
def kill(self):
|
||||
pass
|
||||
|
||||
def wait(self):
|
||||
return -9
|
||||
|
||||
def processes(pfx):
|
||||
if not calls:
|
||||
return [(3, ["agent.exe"])]
|
||||
return [(3, ["agent.exe"]), (5, ["Agent.exe"]), (6, ["wineserver"]), (7, ["Wow.exe"])]
|
||||
|
||||
with mock.patch.object(BNET["subprocess"], "Popen", Hung), \
|
||||
mock.patch.dict(BNET, {"wine_processes": processes, "stop": lambda p: stopped.append(p)}):
|
||||
code = BNET["agent_job"](self.root, "install", "s1", "s1", str(self.tmp / "S1"), "enUS", "USA", "US", poll=0, stall=0.05)
|
||||
self.assertEqual(code, 6)
|
||||
self.assertEqual(stopped, [[(5, ["Agent.exe"])]])
|
||||
|
||||
def test_only_a_changing_rate_or_estimate_does_not_hold_off_the_stall(self):
|
||||
self.install()
|
||||
clock = [0.0]
|
||||
|
||||
class Spinning:
|
||||
returncode = None
|
||||
|
||||
def __init__(self, cmd, **kwargs):
|
||||
self.base = cmd[-1][2:].replace("\\", "/")
|
||||
self.polls = 0
|
||||
|
||||
def poll(self):
|
||||
self.polls += 1
|
||||
if self.polls > 10:
|
||||
raise AssertionError("the stall timeout never ended the job")
|
||||
clock[0] += 10
|
||||
with open(self.base + ".json", "wb") as f:
|
||||
f.write(json.dumps({"state": 1003, "download_current": [4, 0, 0], "download_total": [9, 0, 0],
|
||||
"download_rate": self.polls, "estimated_time_in_sec": self.polls}).encode("utf-16"))
|
||||
return None
|
||||
|
||||
def kill(self):
|
||||
pass
|
||||
|
||||
def wait(self):
|
||||
return -9
|
||||
|
||||
fake = mock.Mock(monotonic=lambda: clock[0], sleep=lambda s: None)
|
||||
with mock.patch.object(BNET["subprocess"], "Popen", Spinning), mock.patch.dict(BNET, {"time": fake, "stop": lambda p: None}):
|
||||
code = BNET["agent_job"](self.root, "update", "gryphon", poll=0, stall=25)
|
||||
self.assertEqual(code, 6)
|
||||
self.assertEqual(self.lines[-1], ("ERROR", "stalled"))
|
||||
|
||||
def test_a_moving_job_is_not_ended_as_stalled(self):
|
||||
self.install()
|
||||
done = json.dumps({"installed": True, "playable": True, "install_dir": "Z:/g", "local_version": "2"})
|
||||
clock = [0.0]
|
||||
|
||||
class Moving:
|
||||
returncode = 0
|
||||
|
||||
def __init__(self, cmd, **kwargs):
|
||||
self.base = cmd[-1][2:].replace("\\", "/")
|
||||
self.polls = 0
|
||||
|
||||
def poll(self):
|
||||
self.polls += 1
|
||||
clock[0] += 10
|
||||
with open(self.base + ".json", "wb") as f:
|
||||
f.write(json.dumps({"state": 1003, "download_current": [self.polls, 0, 0], "download_total": [9, 0, 0]}).encode("utf-16"))
|
||||
if self.polls < 6:
|
||||
return None
|
||||
with open(self.base + ".game", "wb") as f:
|
||||
f.write(done.encode("utf-16"))
|
||||
return 0
|
||||
|
||||
fake = mock.Mock(monotonic=lambda: clock[0], sleep=lambda s: None)
|
||||
with mock.patch.object(BNET["subprocess"], "Popen", Moving), mock.patch.dict(BNET, {"time": fake}):
|
||||
code = BNET["agent_job"](self.root, "update", "gryphon", poll=0, stall=15)
|
||||
self.assertEqual(code, 0)
|
||||
self.assertNotIn(("ERROR", "stalled"), self.lines)
|
||||
|
||||
def test_only_a_real_job_reply_counts_as_started(self):
|
||||
script = BNET["AGENT_SCRIPT"]
|
||||
self.assertIn("if (state == 1000 || state == 1003 || job(s) && !complete) started = true;", script)
|
||||
self.assertIn('return /"state"\\s*:\\s*\\d+/.test(bare) || /"download_current"\\s*:/.test(bare);', script)
|
||||
self.assertIn('if (/"error"\\s*:\\s*[1-9]/.test(text)) return false;', script)
|
||||
|
||||
def test_the_driver_only_stops_once_the_agent_has_really_run(self):
|
||||
script = BNET["AGENT_SCRIPT"]
|
||||
self.assertIn("if (started && (state == 1004 || complete && /\"playable\"\\s*:\\s*true/.test(s))) break;", script)
|
||||
|
||||
@@ -22,22 +22,29 @@ extern char **environ;
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 3) return 2;
|
||||
const char *how = argv[1], *file = argv[2];
|
||||
char *node_env[] = {"ELECTRON_RUN_AS_NODE=1", NULL};
|
||||
char *const *child_env = !strcmp(how, "execve-node") || !strcmp(how, "execvpe-node") ||
|
||||
!strcmp(how, "spawn-node") || !strcmp(how, "spawnp-node") ? node_env : environ;
|
||||
char **args = argv + 2;
|
||||
if (!strcmp(how, "access")) {
|
||||
int result = access(file, X_OK);
|
||||
printf("access %d %d\n", result, result ? errno : 0);
|
||||
return 0;
|
||||
}
|
||||
if (!strcmp(how, "spawn") || !strcmp(how, "spawnp")) {
|
||||
if (!strcmp(how, "spawn") || !strcmp(how, "spawnp") || !strcmp(how, "spawn-node") ||
|
||||
!strcmp(how, "spawnp-node")) {
|
||||
pid_t pid;
|
||||
int error = how[5] ? posix_spawnp(&pid, file, NULL, NULL, args, environ) : posix_spawn(&pid, file, NULL, NULL, args, environ);
|
||||
int spawnp = !strcmp(how, "spawnp") || !strcmp(how, "spawnp-node");
|
||||
int error = spawnp ? posix_spawnp(&pid, file, NULL, NULL, args, child_env)
|
||||
: posix_spawn(&pid, file, NULL, NULL, args, child_env);
|
||||
if (error) { printf("errno %d\n", error); return 3; }
|
||||
int status;
|
||||
waitpid(pid, &status, 0);
|
||||
printf("exit %d\n", WEXITSTATUS(status));
|
||||
return 0;
|
||||
}
|
||||
if (!strcmp(how, "execve")) execve(file, args, environ);
|
||||
if (!strcmp(how, "execve") || !strcmp(how, "execve-node")) execve(file, args, (char *const *)child_env);
|
||||
else if (!strcmp(how, "execvpe-node")) execvpe(file, args, (char *const *)child_env);
|
||||
else if (!strcmp(how, "execv")) execv(file, args);
|
||||
else if (!strcmp(how, "execvp")) execvp(file, args);
|
||||
else if (!strcmp(how, "execvpe")) execvpe(file, args, environ);
|
||||
@@ -88,6 +95,15 @@ class BinfmtPreloadTest(unittest.TestCase):
|
||||
path.chmod(mode)
|
||||
return path
|
||||
|
||||
def chromium_script(self):
|
||||
folder = self.dir / "vscode"
|
||||
folder.mkdir()
|
||||
executable = folder / "code"
|
||||
executable.write_text("#!/bin/sh\nprintf '%s\\n' \"$@\"\n")
|
||||
executable.chmod(0o755)
|
||||
(folder / "chrome-sandbox").touch()
|
||||
return executable
|
||||
|
||||
def environment(self, desktop=True, **extra):
|
||||
env = {"PATH": "%s:/usr/bin:/bin" % self.dir, "LD_PRELOAD": str(self.library), "HANDLER_LOG": str(self.log)}
|
||||
if desktop:
|
||||
@@ -161,6 +177,17 @@ class BinfmtPreloadTest(unittest.TestCase):
|
||||
self.file("script.sh", b"#!/bin/sh\nexit 5\n")
|
||||
self.assertEqual(self.run_program("spawnp", "script.sh"), (0, "exit 5", None))
|
||||
|
||||
def test_explicit_node_child_environment_skips_injection_for_execve_and_spawn(self):
|
||||
code = self.chromium_script()
|
||||
self.assertEqual(self.run_program("execve-node", code, "--version"), (0, "--version", None))
|
||||
self.assertEqual(self.run_program("execvpe-node", code, "--version"), (0, "--version", None))
|
||||
self.assertEqual(self.run_program("spawn-node", code, "--version"), (0, "--version\nexit 0", None))
|
||||
self.assertEqual(self.run_program("spawnp-node", code, "--version"), (0, "--version\nexit 0", None))
|
||||
|
||||
def test_chromium_gui_launch_without_node_mode_still_receives_no_sandbox(self):
|
||||
code = self.chromium_script()
|
||||
self.assertEqual(self.run_program("execve", code, "--version"), (0, "--no-sandbox\n--version", None))
|
||||
|
||||
def test_the_hand_off_switch_works_without_the_desktop(self):
|
||||
tool = self.x86()
|
||||
for how in ("execve", "execvp"):
|
||||
|
||||
@@ -58,11 +58,17 @@ class CiChecksTest(unittest.TestCase):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
sudo = root / 'sudo'
|
||||
sudo.write_text('#!/bin/bash\nprintf "%s\\n" "$*" >> "$APT_LOG"\nexit "${APT_RESULT:-0}"\n')
|
||||
sudo.write_text('#!/bin/bash\nprintf "%s\\n" "$*" >> "$APT_LOG"\n'
|
||||
'for arg; do case "$arg" in Dir::Etc::sourcelist=*) '
|
||||
'grep -h "^URIs:" "${arg#*=}" >> "$APT_LOG.mirrors";; esac; done\n'
|
||||
'exit "${APT_RESULT:-0}"\n')
|
||||
sudo.chmod(0o755)
|
||||
query = root / 'dpkg-query'
|
||||
query.write_text('#!/bin/sh\nexit 1\n')
|
||||
query.chmod(0o755)
|
||||
dpkg = root / 'dpkg'
|
||||
dpkg.write_text('#!/bin/sh\necho "${DPKG_ARCH:-amd64}"\n')
|
||||
dpkg.chmod(0o755)
|
||||
log = root / 'apt.log'
|
||||
sources = root / 'ubuntu.sources'
|
||||
sources.write_text('URIs: http://mirror.invalid/ubuntu\n')
|
||||
@@ -71,8 +77,23 @@ class CiChecksTest(unittest.TestCase):
|
||||
installer = ROOT / 'tools/ci/install-ubuntu-packages.sh'
|
||||
result = subprocess.run(['bash', str(installer), 'qemu-user'], env=dict(env, APT_RESULT='17'))
|
||||
self.assertEqual(result.returncode, 17)
|
||||
self.assertEqual(len(log.read_text().splitlines()), 2)
|
||||
self.assertEqual(len(log.read_text().splitlines()), 3)
|
||||
mirrors = Path(str(log) + '.mirrors')
|
||||
self.assertEqual(mirrors.read_text().splitlines(), [
|
||||
'URIs: http://mirror.invalid/ubuntu',
|
||||
'URIs: https://archive.ubuntu.com/ubuntu',
|
||||
'URIs: https://mirrors.edge.kernel.org/ubuntu',
|
||||
])
|
||||
log.unlink()
|
||||
mirrors.unlink()
|
||||
result = subprocess.run(['bash', str(installer), 'qemu-user'], env=dict(env, APT_RESULT='17', DPKG_ARCH='arm64'))
|
||||
self.assertEqual(result.returncode, 17)
|
||||
self.assertEqual(mirrors.read_text().splitlines()[1:], [
|
||||
'URIs: https://ports.ubuntu.com/ubuntu-ports',
|
||||
'URIs: https://mirrors.ocf.berkeley.edu/ubuntu-ports',
|
||||
])
|
||||
log.unlink()
|
||||
mirrors.unlink()
|
||||
subprocess.run(['bash', str(installer), 'qemu-user'], env=env, check=True)
|
||||
update, install = log.read_text().splitlines()
|
||||
for command in (update, install):
|
||||
|
||||
@@ -121,6 +121,7 @@ step() { :; }
|
||||
bl_zram_scan() { :; }
|
||||
bl_memory_read() { :; }
|
||||
bl_client_pressure() { :; }
|
||||
bl_client_pressure_finish() { :; }
|
||||
bl_session_pressure() { :; }
|
||||
bl_client_can_pageout() { return 0; }
|
||||
bl_client_can_readback() { [ "${readback:-1}" = 1 ]; }
|
||||
|
||||
@@ -1,19 +1,34 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import queue
|
||||
import re
|
||||
import shlex
|
||||
import shutil
|
||||
import struct
|
||||
import subprocess
|
||||
import tempfile
|
||||
import threading
|
||||
import unittest
|
||||
|
||||
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
|
||||
|
||||
|
||||
def clock_reset_command(control):
|
||||
script = (PRELOAD.parent / "overlay/usr/local/bin/droiddeck-session").read_text()
|
||||
start = script.index('if [ -z "${BL_INSIDE:-}" ]; then\n if [ "${BL_ZRAM:-0}"')
|
||||
end = script.index("\nfi", start) + len("\nfi")
|
||||
paths = {"/tmp/.bl-clock": shlex.quote(str(control)),
|
||||
"/tmp/.bl-zram": shlex.quote(str(control.parent / "zram"))}
|
||||
pattern = "|".join(re.escape(token) for token in paths)
|
||||
return 'BL_INSIDE=; BL_ZRAM=0; ' + re.sub(pattern, lambda match: paths[match.group(0)], script[start:end])
|
||||
|
||||
PROGRAM = r"""
|
||||
#define _GNU_SOURCE
|
||||
#include <linux/futex.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/timerfd.h>
|
||||
@@ -36,12 +51,26 @@ static int64_t start;
|
||||
static void begin(void) { start = ns(CLOCK_BOOTTIME); }
|
||||
static void end(const char *name) { printf("%s %lld\n", name, (long long)((ns(CLOCK_BOOTTIME) - start) / 1000000LL)); }
|
||||
|
||||
static unsigned long long memory_probe(void) {
|
||||
static volatile unsigned char memory[4 * 1024 * 1024];
|
||||
unsigned long long checksum = 0;
|
||||
for (size_t i = 0; i < sizeof(memory); i += 4096) memory[i] = (unsigned char)(i / 4096 + 1);
|
||||
for (size_t i = 0; i < sizeof(memory); i += 4096) checksum += memory[i];
|
||||
return checksum;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
const int64_t wait = 60000000LL;
|
||||
printf("gap %lld\n", (long long)((ns(CLOCK_BOOTTIME) - ns(CLOCK_MONOTONIC)) / 1000000LL));
|
||||
struct timespec raw;
|
||||
syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &raw);
|
||||
printf("rawgap %lld\n", (long long)((ns(CLOCK_BOOTTIME) - ((int64_t)raw.tv_sec * 1000000000LL + raw.tv_nsec)) / 1000000LL));
|
||||
printf("memory %llu\n", memory_probe());
|
||||
if (getenv("CLOCK_PAUSE_WAIT")) { puts("ready"); fflush(stdout); getchar(); }
|
||||
if (getenv("CLOCK_PAUSE_WAIT")) {
|
||||
printf("livegap %lld\n", (long long)((ns(CLOCK_BOOTTIME) - ns(CLOCK_MONOTONIC)) / 1000000LL));
|
||||
printf("livememory %llu\n", memory_probe());
|
||||
}
|
||||
|
||||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_condattr_t attr;
|
||||
@@ -124,14 +153,124 @@ class ClockPausePreloadTest(unittest.TestCase):
|
||||
self.assertGreaterEqual(paused[name], 50, name)
|
||||
self.assertLess(paused[name], 1000, name)
|
||||
|
||||
def test_other_programs_keep_the_real_clock(self):
|
||||
def test_program_without_preload_keeps_the_real_clock(self):
|
||||
plain = self.run_program(self.other, 0, preload=False)
|
||||
paused = self.run_program(self.other, SHIFT_MS)
|
||||
paused = self.run_program(self.other, SHIFT_MS, preload=False)
|
||||
self.assertAlmostEqual(paused["gap"], plain["gap"], delta=50)
|
||||
for name in ("cond_mono", "clockwait", "sem", "nanosleep", "timerfd", "futex"):
|
||||
self.assertGreaterEqual(paused[name], 50, name)
|
||||
self.assertLess(paused[name], 1000, name)
|
||||
|
||||
def test_every_session_program_stops_monotonic_clock_and_keeps_memory_access(self):
|
||||
plain = self.run_program(self.other, 0)
|
||||
paused = self.run_program(self.other, SHIFT_MS)
|
||||
self.assertAlmostEqual(paused["gap"] - plain["gap"], SHIFT_MS, delta=50)
|
||||
self.assertAlmostEqual(paused["rawgap"] - plain["rawgap"], SHIFT_MS, delta=50)
|
||||
self.assertEqual(plain["memory"], 130560)
|
||||
self.assertEqual(paused["memory"], 130560)
|
||||
|
||||
def test_running_session_program_observes_pause_offset_update(self):
|
||||
self.check_running_offset_update(False)
|
||||
|
||||
def test_session_reset_preserves_live_guest_mapping(self):
|
||||
self.check_running_offset_update(True)
|
||||
|
||||
def check_running_offset_update(self, reset):
|
||||
self.control.write_bytes(struct.pack("<q", 0))
|
||||
env = dict(os.environ)
|
||||
env["LD_PRELOAD"] = str(self.preload)
|
||||
env["CLOCK_PAUSE_WAIT"] = "1"
|
||||
with tempfile.TemporaryFile(mode="w+") as errors:
|
||||
process = subprocess.Popen([str(self.other)], env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
||||
stderr=errors, text=True)
|
||||
lines = queue.Queue()
|
||||
|
||||
def read_handshake():
|
||||
for _ in range(4):
|
||||
line = process.stdout.readline()
|
||||
lines.put(line)
|
||||
if not line:
|
||||
break
|
||||
|
||||
threading.Thread(target=read_handshake, daemon=True).start()
|
||||
try:
|
||||
handshake = []
|
||||
for _ in range(4):
|
||||
try:
|
||||
line = lines.get(timeout=5)
|
||||
except queue.Empty:
|
||||
line = ""
|
||||
if not line:
|
||||
if process.poll() is None:
|
||||
process.kill()
|
||||
process.wait(timeout=5)
|
||||
errors.seek(0)
|
||||
self.fail(f"child handshake timed out or ended early (exit {process.returncode}): {errors.read()}")
|
||||
handshake.append(line.strip())
|
||||
self.assertEqual(handshake[3], "ready")
|
||||
self.assertTrue(handshake[0].startswith("gap "))
|
||||
initial_gap = int(handshake[0].split()[1])
|
||||
rawgap = handshake[1]
|
||||
memory = handshake[2]
|
||||
self.assertTrue(rawgap.startswith("rawgap "))
|
||||
self.assertEqual(memory, "memory 130560")
|
||||
if reset:
|
||||
inode = self.control.stat().st_ino
|
||||
subprocess.run(["bash", "-c", clock_reset_command(self.control)],
|
||||
check=True, timeout=5)
|
||||
self.assertEqual(self.control.stat().st_ino, inode)
|
||||
self.assertEqual(self.control.read_bytes(), struct.pack("<q", 0))
|
||||
with self.control.open("r+b") as control:
|
||||
control.write(struct.pack("<q", SHIFT_MS * 1000000))
|
||||
control.flush()
|
||||
process.stdin.write("\n")
|
||||
process.stdin.flush()
|
||||
output, _ = process.communicate(timeout=5)
|
||||
errors.seek(0)
|
||||
self.assertEqual(process.returncode, 0, errors.read())
|
||||
values = dict(line.split() for line in output.splitlines())
|
||||
self.assertAlmostEqual(int(values["livegap"]) - initial_gap, SHIFT_MS, delta=50)
|
||||
self.assertEqual(int(values["livememory"]), 130560)
|
||||
finally:
|
||||
if process.poll() is None:
|
||||
process.kill()
|
||||
process.communicate()
|
||||
|
||||
|
||||
class ClockResetCommandTest(unittest.TestCase):
|
||||
def test_reset_keeps_paths_with_spaces_and_shell_syntax_as_single_arguments(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
folder = root / "session dir; touch injected $(touch substituted) 'quoted'"
|
||||
folder.mkdir()
|
||||
control = folder / "clock"
|
||||
control.write_bytes(struct.pack("<q", 10_000_000_000) + b"tail")
|
||||
(folder / "zram").write_text("1")
|
||||
bystander = root / "session"
|
||||
bystander.write_text("keep")
|
||||
inode = control.stat().st_ino
|
||||
subprocess.run(["bash", "-c", clock_reset_command(control)], cwd=root, check=True, timeout=5)
|
||||
self.assertEqual(control.stat().st_ino, inode)
|
||||
self.assertEqual(control.read_bytes(), struct.pack("<q", 0) + b"tail")
|
||||
self.assertFalse((folder / "zram").exists())
|
||||
self.assertEqual(bystander.read_text(), "keep")
|
||||
self.assertEqual(sorted(path.name for path in root.iterdir()), sorted([folder.name, bystander.name]))
|
||||
|
||||
def test_reset_does_not_rewrite_placeholders_inside_substituted_paths(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
folder = root / "tmp/.bl-zram/$(touch injected)"
|
||||
folder.mkdir(parents=True)
|
||||
control = folder / "clock"
|
||||
control.write_bytes(struct.pack("<q", 10_000_000_000))
|
||||
(folder / "zram").write_text("1")
|
||||
inode = control.stat().st_ino
|
||||
subprocess.run(["bash", "-c", clock_reset_command(control)], cwd=root, check=True, timeout=5)
|
||||
self.assertEqual(control.stat().st_ino, inode)
|
||||
self.assertEqual(control.read_bytes(), struct.pack("<q", 0))
|
||||
self.assertFalse((folder / "zram").exists())
|
||||
self.assertEqual([path.name for path in root.iterdir()], ["tmp"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,69 @@
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
|
||||
|
||||
SCRIPT = Path(__file__).resolve().parents[1] / 'agent/dd-lab-build'
|
||||
|
||||
|
||||
class LabBuildPackageTest(unittest.TestCase):
|
||||
def test_package_override_survives_app_handoff_without_its_environment(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
inbox = root / 'agent'
|
||||
inbox.mkdir()
|
||||
cache = root / '.cache'
|
||||
env = {**os.environ, 'HOME': tmp, 'DD_AGENT': '1',
|
||||
'DD_AGENT_INBOX': str(inbox), 'DROIDDECK_PACKAGE': 'com.example.target'}
|
||||
env.pop('DD_LAB_DETACHED', None)
|
||||
child = subprocess.Popen(['bash', str(SCRIPT), '--prepare'], env=env,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||
try:
|
||||
request = inbox / 'lab-request'
|
||||
deadline = time.monotonic() + 10
|
||||
while not request.exists() and child.poll() is None and time.monotonic() < deadline:
|
||||
time.sleep(0.01)
|
||||
self.assertTrue(request.exists())
|
||||
payload = json.loads(request.read_text())
|
||||
self.assertEqual(['--prepare', '--package=com.example.target'], payload['args'])
|
||||
progress = cache / 'dd-lab-build.json'
|
||||
progress.write_text('{"stage":"prepared"}')
|
||||
stdout, stderr = child.communicate(timeout=10)
|
||||
self.assertEqual(0, child.returncode, stdout + stderr)
|
||||
finally:
|
||||
if child.poll() is None:
|
||||
child.kill()
|
||||
child.communicate()
|
||||
|
||||
bin_dir = root / 'bin'
|
||||
bin_dir.mkdir()
|
||||
(bin_dir / 'adb').write_text('#!/bin/sh\nexit 0\n')
|
||||
(bin_dir / 'python3').write_text(
|
||||
'#!/bin/sh\nprintf "%s" "$DROIDDECK_PACKAGE" > "$HOME/selected-package"\nexit 1\n')
|
||||
for tool in bin_dir.iterdir():
|
||||
tool.chmod(0o755)
|
||||
env.pop('DROIDDECK_PACKAGE')
|
||||
env['DD_LAB_DETACHED'] = '1'
|
||||
env['PATH'] = str(bin_dir) + os.pathsep + env['PATH']
|
||||
result = subprocess.run(['bash', str(SCRIPT), *payload['args']], env=env,
|
||||
capture_output=True, text=True, timeout=10, cwd=SCRIPT.parents[2])
|
||||
self.assertNotEqual(0, result.returncode)
|
||||
self.assertIn('could not read DroidDeck', result.stderr)
|
||||
self.assertEqual('com.example.target', (root / 'selected-package').read_text())
|
||||
|
||||
def test_invalid_packages_are_rejected_before_handoff(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
inbox = Path(tmp) / 'agent'
|
||||
inbox.mkdir()
|
||||
for package in ('bad;touch injected', '../other', '', 'com.example/other'):
|
||||
with self.subTest(package=package):
|
||||
env = {**os.environ, 'HOME': tmp, 'DD_AGENT': '1', 'DD_AGENT_INBOX': str(inbox)}
|
||||
result = subprocess.run(['bash', str(SCRIPT), '--package=' + package], env=env,
|
||||
capture_output=True, text=True, timeout=10)
|
||||
self.assertNotEqual(0, result.returncode)
|
||||
self.assertIn('invalid Android package', result.stderr)
|
||||
self.assertFalse((inbox / 'lab-request').exists())
|
||||
@@ -0,0 +1,245 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
|
||||
|
||||
FAKE = r"""
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int answer(int fd) {
|
||||
char link[32], path[4096];
|
||||
snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
|
||||
long n = syscall(SYS_readlinkat, AT_FDCWD, link, path, sizeof(path) - 1);
|
||||
path[n > 0 ? n : 0] = '\0';
|
||||
if (strstr(path, "/card/")) return ENOTSUP;
|
||||
if (strstr(path, "/denied/")) return EACCES;
|
||||
return ENODATA;
|
||||
}
|
||||
|
||||
ssize_t fgetxattr(int fd, const char *name, void *value, size_t size) {
|
||||
(void)name; (void)value; (void)size;
|
||||
errno = answer(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int fsetxattr(int fd, const char *name, const void *value, size_t size, int flags) {
|
||||
(void)name; (void)value; (void)size; (void)flags;
|
||||
int error = answer(fd);
|
||||
if (error == ENODATA) return 0;
|
||||
errno = error;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int fremovexattr(int fd, const char *name) {
|
||||
(void)name;
|
||||
errno = answer(fd);
|
||||
return -1;
|
||||
}
|
||||
"""
|
||||
|
||||
PROGRAM = r"""
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/xattr.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static const char *error(void) {
|
||||
return errno == ENOTSUP ? "unsupported" : errno == ENODATA ? "none" : errno == ERANGE ? "range" :
|
||||
errno == EACCES ? "denied" : "other";
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
for (int i = 1; i < argc; i++) {
|
||||
const char *op = argv[i];
|
||||
if (!strcmp(op, "unlink") || !strcmp(op, "rmdir")) {
|
||||
const char *path = argv[++i];
|
||||
printf("%s\n", (op[0] == 'u' ? unlink(path) : rmdir(path)) == 0 ? "ok" : "failed");
|
||||
continue;
|
||||
}
|
||||
if (!strcmp(op, "remove-path")) {
|
||||
printf("%s\n", remove(argv[++i]) == 0 ? "ok" : "failed");
|
||||
continue;
|
||||
}
|
||||
if (!strcmp(op, "rename")) {
|
||||
const char *from = argv[++i], *to = argv[++i];
|
||||
printf("%s\n", rename(from, to) == 0 ? "ok" : "failed");
|
||||
continue;
|
||||
}
|
||||
int fd = open(argv[++i], O_RDONLY);
|
||||
char value[64];
|
||||
if (!strcmp(op, "set")) {
|
||||
const char *text = argv[++i];
|
||||
errno = 1234;
|
||||
int result = fsetxattr(fd, "user.wine.sd", text, strlen(text), 0);
|
||||
printf("%s\n", result == 0 ? errno == 1234 ? "ok" : "errno" : error());
|
||||
} else if (!strcmp(op, "get")) {
|
||||
ssize_t n = fgetxattr(fd, "user.wine.sd", value, sizeof(value));
|
||||
if (n < 0) printf("%s\n", error());
|
||||
else printf("=%.*s\n", (int)n, value);
|
||||
} else if (!strcmp(op, "size")) {
|
||||
ssize_t n = fgetxattr(fd, "user.wine.sd", NULL, 0);
|
||||
if (n < 0) printf("%s\n", error());
|
||||
else printf("%d\n", (int)n);
|
||||
} else if (!strcmp(op, "short")) {
|
||||
ssize_t n = fgetxattr(fd, "user.wine.sd", value, 2);
|
||||
printf("%s\n", n < 0 ? error() : "read");
|
||||
} else if (!strcmp(op, "remove")) {
|
||||
errno = 1234;
|
||||
int result = fremovexattr(fd, "user.wine.sd");
|
||||
printf("%s\n", result == 0 ? errno == 1234 ? "ok" : "errno" : error());
|
||||
} else if (!strcmp(op, "other")) {
|
||||
ssize_t n = fgetxattr(fd, "user.other", value, sizeof(value));
|
||||
printf("%s\n", n < 0 ? error() : "read");
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
@unittest.skipUnless(shutil.which("cc"), "needs a C compiler")
|
||||
class DescriptorPreloadTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.build_dir = tempfile.TemporaryDirectory()
|
||||
build = Path(cls.build_dir.name)
|
||||
(build / "fake.c").write_text(FAKE)
|
||||
(build / "program.c").write_text(PROGRAM)
|
||||
cls.preload = build / "libblsession.so"
|
||||
cls.fake = build / "fake.so"
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-o", str(cls.preload),
|
||||
*map(str, sorted(PRELOAD.glob("*.c"))), "-ldl"], check=True)
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.fake), str(build / "fake.c")], check=True)
|
||||
cls.wineserver = build / "wineserver"
|
||||
subprocess.run(["cc", "-O2", "-o", str(cls.wineserver), str(build / "program.c")], check=True)
|
||||
cls.other = build / "wine64-preloader"
|
||||
shutil.copy(cls.wineserver, cls.other)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.build_dir.cleanup()
|
||||
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.dir = Path(os.path.realpath(self.temp.name))
|
||||
self.prefix = self.dir / "prefix"
|
||||
self.prefix.mkdir()
|
||||
self.store = self.prefix / ".droiddeck-descriptors"
|
||||
for kind in ("card", "disk", "denied"):
|
||||
(self.dir / kind / "game").mkdir(parents=True)
|
||||
(self.dir / kind / "game" / "file").write_text("x")
|
||||
|
||||
def tearDown(self):
|
||||
self.temp.cleanup()
|
||||
|
||||
def path(self, *parts):
|
||||
return str(self.dir.joinpath(*parts))
|
||||
|
||||
def run_as(self, program, *args, prefix=True):
|
||||
env = dict(os.environ, LD_PRELOAD="%s:%s" % (self.preload, self.fake))
|
||||
env.pop("WINEPREFIX", None)
|
||||
if prefix:
|
||||
env["WINEPREFIX"] = str(self.prefix)
|
||||
result = subprocess.run([str(program), *args], env=env, capture_output=True, text=True, check=True)
|
||||
return result.stdout.split()
|
||||
|
||||
def server(self, *args, **kwargs):
|
||||
return self.run_as(self.wineserver, *args, **kwargs)
|
||||
|
||||
def test_a_descriptor_is_kept_where_the_volume_cannot_store_it(self):
|
||||
file, folder = self.path("card", "game", "file"), self.path("card", "game")
|
||||
self.assertEqual(["none", "ok", "ok"], self.server("get", file, "set", file, "one", "set", folder, "two"))
|
||||
self.assertEqual(["=one", "3", "=two"], self.server("get", file, "size", file, "get", folder))
|
||||
|
||||
def test_a_descriptor_is_replaced_and_removed(self):
|
||||
file = self.path("card", "game", "file")
|
||||
self.assertEqual(["ok", "ok", "=longer"], self.server("set", file, "one", "set", file, "longer", "get", file))
|
||||
self.assertEqual(["range", "ok", "none", "none"], self.server("short", file, "remove", file, "get", file, "remove", file))
|
||||
|
||||
def test_a_volume_that_stores_descriptors_is_left_alone(self):
|
||||
file = self.path("disk", "game", "file")
|
||||
self.assertEqual(["ok", "none", "none"], self.server("set", file, "one", "get", file, "remove", file))
|
||||
self.assertFalse(self.store.exists())
|
||||
|
||||
def test_other_failures_keep_their_error(self):
|
||||
file = self.path("denied", "game", "file")
|
||||
self.assertEqual(["denied", "denied", "denied"], self.server("set", file, "one", "get", file, "remove", file))
|
||||
self.assertFalse(self.store.exists())
|
||||
|
||||
def test_other_programs_and_other_attributes_are_unchanged(self):
|
||||
file = self.path("card", "game", "file")
|
||||
self.assertEqual(["unsupported", "unsupported", "unsupported"],
|
||||
self.run_as(self.other, "set", file, "one", "get", file, "remove", file))
|
||||
self.assertEqual(["ok", "unsupported"], self.server("set", file, "one", "other", file))
|
||||
self.assertEqual(["unsupported"], self.run_as(self.other, "get", file))
|
||||
|
||||
def test_nothing_is_kept_without_a_prefix(self):
|
||||
file = self.path("card", "game", "file")
|
||||
self.assertEqual(["unsupported", "unsupported", "unsupported"],
|
||||
self.server("set", file, "one", "get", file, "remove", file, prefix=False))
|
||||
|
||||
def test_a_deleted_file_loses_its_descriptor(self):
|
||||
file, folder = self.path("card", "game", "file"), self.path("card", "game")
|
||||
self.assertEqual(["ok", "ok", "ok"], self.server("set", file, "one", "set", folder, "two", "unlink", file))
|
||||
Path(file).write_text("x")
|
||||
self.assertEqual(["none", "ok", "ok"], self.server("get", file, "unlink", file, "rmdir", folder))
|
||||
Path(folder).mkdir()
|
||||
self.assertEqual(["none"], self.server("get", folder))
|
||||
|
||||
def test_a_removed_file_or_folder_loses_its_descriptor(self):
|
||||
file, folder = self.path("card", "game", "file"), self.path("card", "game")
|
||||
self.assertEqual(["ok", "ok", "ok", "ok"],
|
||||
self.server("set", file, "one", "set", folder, "two", "remove-path", file, "remove-path", folder))
|
||||
Path(folder).mkdir()
|
||||
Path(file).write_text("x")
|
||||
self.assertEqual(["none", "none"], self.server("get", folder, "get", file))
|
||||
|
||||
def test_a_file_deleted_through_a_link_loses_its_descriptor(self):
|
||||
file = self.path("card", "game", "file")
|
||||
os.symlink(self.path("card"), self.path("drive"))
|
||||
self.assertEqual(["ok", "ok"], self.server("set", file, "one", "unlink", self.path("drive", "game", "file")))
|
||||
Path(file).write_text("x")
|
||||
self.assertEqual(["none"], self.server("get", file))
|
||||
|
||||
def test_a_descriptor_follows_a_rename(self):
|
||||
file, moved = self.path("card", "game", "file"), self.path("card", "game", "moved")
|
||||
self.assertEqual(["ok", "ok", "=one"], self.server("set", file, "one", "rename", file, moved, "get", moved))
|
||||
Path(file).write_text("x")
|
||||
self.assertEqual(["none", "ok", "none"], self.server("get", file, "rename", file, moved, "get", moved))
|
||||
|
||||
def test_descriptors_follow_a_renamed_folder(self):
|
||||
file, folder, moved = self.path("card", "game", "file"), self.path("card", "game"), self.path("card", "moved")
|
||||
self.assertEqual(["ok", "ok", "ok"], self.server("set", file, "one", "set", folder, "two", "rename", folder, moved))
|
||||
self.assertEqual(["=two", "=one"], self.server("get", moved, "get", self.path("card", "moved", "file")))
|
||||
Path(folder).mkdir()
|
||||
(Path(folder) / "file").write_text("x")
|
||||
self.assertEqual(["none", "none"], self.server("get", folder, "get", file))
|
||||
|
||||
def test_deletes_outside_the_store_cost_nothing_before_it_exists(self):
|
||||
file = self.path("disk", "game", "file")
|
||||
self.assertEqual(["ok"], self.server("unlink", file))
|
||||
self.assertFalse(self.store.exists())
|
||||
|
||||
def test_a_name_too_long_to_keep_reports_no_support(self):
|
||||
long_file = self.path("card", "game", "n" * 254)
|
||||
Path(long_file).write_text("x")
|
||||
self.assertEqual(["unsupported", "unsupported"], self.server("set", long_file, "one", "get", long_file))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -136,6 +136,30 @@ class CommandTest(Temp):
|
||||
self.assertEqual(OPEN["command"](script, [], "x86 script")[0],
|
||||
[OPEN["FEX"], "run", "--mode", "on", "--for", str(self.tmp), "--", "/bin/sh", script])
|
||||
|
||||
def test_a_store_games_exe_runs_through_its_launcher(self):
|
||||
exe, launcher = str(self.tmp / "game.exe"), str(self.tmp / "launch.bat")
|
||||
with mock.patch.dict(OPEN, {"store_launcher": lambda path: launcher if path == exe else None,
|
||||
"exe_prefix": lambda path: ["--prefix", "4242", "--compatdata", "/c/4242"]}):
|
||||
keep = ["--prefix", "4242", "--compatdata", "/c/4242"]
|
||||
self.assertEqual(OPEN["command"](exe, [], "windows")[0], [OPEN["PROTON_RUN"]] + keep + [launcher])
|
||||
self.assertEqual(OPEN["command"](exe, [], "windows", True)[0],
|
||||
[OPEN["PROTON_RUN"], "--window"] + keep + [launcher])
|
||||
self.assertEqual(OPEN["command"](exe, ["-x"], "windows")[0], [OPEN["PROTON_RUN"], exe, "-x"])
|
||||
other = str(self.tmp / "other.exe")
|
||||
self.assertEqual(OPEN["command"](other, [], "windows")[0], [OPEN["PROTON_RUN"], other])
|
||||
|
||||
def test_a_store_games_launcher_keeps_the_exes_prefix(self):
|
||||
exe = self.tmp / "game.exe"
|
||||
exe.write_text("x")
|
||||
root = str(self.tmp / "steam")
|
||||
with mock.patch.dict(os.environ, {"STEAM_COMPAT_CLIENT_INSTALL_PATH": root}), \
|
||||
mock.patch.dict(OPEN, {"PROTON_RUN": str(BIN / "droiddeck-proton-run"),
|
||||
"store_launcher": lambda path: str(self.tmp / "launch.bat")}):
|
||||
command = OPEN["command"](str(exe), [], "windows")[0]
|
||||
located = RUN["locate"](str(exe), root)
|
||||
self.assertEqual(command[1:5], ["--prefix", located.appid, "--compatdata", located.compatdata])
|
||||
self.assertEqual(str(RUN["own_id"](str(exe))), str(RUN["locate"](str(exe), root).appid))
|
||||
|
||||
def test_a_native_program_on_noexec_storage_runs_from_a_copy(self):
|
||||
program = self.write("storage/tool", elf(183))
|
||||
x86 = self.write("storage/game.x86_64", elf(62))
|
||||
@@ -737,6 +761,20 @@ class ProtonRunTest(Temp):
|
||||
self.assertEqual(env["STEAM_COMPAT_DATA_PATH"], str(signed))
|
||||
self.assertFalse((self.root / "steamapps/compatdata/2733369024").exists())
|
||||
|
||||
def test_a_launcher_run_keeps_the_resolved_compatdata_folder(self):
|
||||
bat = self.write("Games/Epic/Game/.droiddeck-launch.bat", b"@echo off")
|
||||
signed = self.root / "steamapps/compatdata/-1561598272"
|
||||
signed.mkdir(parents=True)
|
||||
other = self.tmp / "Library2/steamapps/compatdata/555"
|
||||
other.mkdir(parents=True)
|
||||
for compatdata, appid in ((signed, "2733369024"), (other, "555")):
|
||||
with self.subTest(compatdata=str(compatdata)):
|
||||
code, calls, dirs = self.run_main(["--prefix", appid, "--compatdata", str(compatdata), str(bat)])
|
||||
self.assertIsNone(code)
|
||||
self.assertEqual(calls[0][2]["STEAM_COMPAT_DATA_PATH"], str(compatdata))
|
||||
code, calls, dirs = self.run_main(["--prefix", "555", str(bat)])
|
||||
self.assertEqual(calls[0][2]["STEAM_COMPAT_DATA_PATH"], str(self.root / "steamapps/compatdata/555"))
|
||||
|
||||
def test_prefix_folder_prefers_initialized_unsigned_over_empty_signed(self):
|
||||
signed = self.root / "steamapps/compatdata/-1561598272"
|
||||
signed.mkdir(parents=True)
|
||||
|
||||
@@ -83,9 +83,25 @@ int inotify_add_watch(int fd, const char *path, uint32_t mask) {
|
||||
errno = ENOSPC;
|
||||
return -1;
|
||||
}
|
||||
if (getenv("NESTED") && (mask & IN_ONLYDIR)) {
|
||||
static DIR *(*uncounted)(const char *);
|
||||
if (!uncounted) uncounted = dlsym(RTLD_NEXT, "opendir");
|
||||
mkdir(path, 0755);
|
||||
DIR *d = uncounted("/proc/self/fd");
|
||||
while (d && readdir(d)) {}
|
||||
if (d) closedir(d);
|
||||
}
|
||||
return next(fd, path, mask);
|
||||
}
|
||||
|
||||
int remove(const char *path) {
|
||||
static int (*next)(const char *);
|
||||
if (!next) next = dlsym(RTLD_NEXT, "remove");
|
||||
if (!getenv("NESTED")) return next(path);
|
||||
if (unlink(path) == 0) return 0;
|
||||
return errno == EISDIR ? rmdir(path) : -1;
|
||||
}
|
||||
|
||||
int statfs(const char *path, struct statfs *out) {
|
||||
static int (*next)(const char *, struct statfs *);
|
||||
if (!next) next = dlsym(RTLD_NEXT, "statfs");
|
||||
@@ -337,9 +353,9 @@ class DirCachePreloadTest(unittest.TestCase):
|
||||
environment.update(env or {})
|
||||
return environment
|
||||
|
||||
def run_program(self, *args, program=None, env=None):
|
||||
def run_program(self, *args, program=None, env=None, timeout=60):
|
||||
result = subprocess.run([str(program or self.steam), *map(str, args)], env=self.environment(env),
|
||||
capture_output=True, text=True, check=True, timeout=60)
|
||||
capture_output=True, text=True, check=True, timeout=timeout)
|
||||
return result.stdout.splitlines()
|
||||
|
||||
def run_paused(self, *args, between, env=None):
|
||||
@@ -675,6 +691,20 @@ class DirCachePreloadTest(unittest.TestCase):
|
||||
env={"BL_NO_DIR_CACHE": "1"})
|
||||
self.assertEqual(["-1 2", "-1 2", "2"], out[1:])
|
||||
|
||||
def test_a_preload_below_calling_back_while_a_folder_is_watched_does_not_hang(self):
|
||||
common = self.lib / "common/Game"
|
||||
common.mkdir(parents=True)
|
||||
(common / "game.exe").touch()
|
||||
out = self.run_program("list", self.staging, "list", common, "list", common, "opens",
|
||||
env={"NESTED": "1"}, timeout=20)
|
||||
self.assertEqual(["a.bundle,b.bundle", "game.exe", "game.exe", "2"], out)
|
||||
|
||||
def test_a_preload_below_removing_through_unlink_does_not_hang(self):
|
||||
s = self.staging
|
||||
out = self.run_program("list", s, "remove", s / "a.bundle", "list", s, "raw", s, "opens",
|
||||
env={"NESTED": "1"}, timeout=20)
|
||||
self.assertEqual(["a.bundle,b.bundle", "b.bundle", "b.bundle", "1"], out)
|
||||
|
||||
def test_can_be_turned_off(self):
|
||||
out = self.run_program("list", self.staging, "list", self.staging, "opens", env={"BL_NO_DIR_CACHE": "1"})
|
||||
self.assertEqual("2", out[-1])
|
||||
|
||||
@@ -175,17 +175,27 @@ class RuntimeExtractTest(EpicTestCase):
|
||||
old = self.state / "app/0.1"
|
||||
old.mkdir(parents=True)
|
||||
|
||||
def extract_app(image, dest):
|
||||
os.makedirs(os.path.join(dest, "usr/bin"))
|
||||
Path(dest, "AppRun").write_text("run")
|
||||
Path(dest, "usr/bin/EpicGamesLauncher").write_text("bin")
|
||||
layers = []
|
||||
|
||||
def extract_app(image, dest, desktop=()):
|
||||
layers.append(list(desktop))
|
||||
os.makedirs(os.path.join(dest, "usr/bin"))
|
||||
os.makedirs(os.path.join(dest, "desktop/usr/lib/x86_64-linux-gnu"))
|
||||
Path(dest, "usr/bin/EpicGamesLauncher").write_text("bin")
|
||||
Path(dest, "desktop/usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.1.so.0").write_text("webkit")
|
||||
|
||||
package = self.tmp / "libwebkit.deb"
|
||||
package.write_bytes(b"x")
|
||||
with mock.patch.dict(G, {"extract_app": extract_app, "fex_tool": lambda request=False: {"bin": "/fex", "server": "/s"}}), \
|
||||
mock.patch("sys.stdout", new_callable=io.StringIO) as out:
|
||||
image = self.tmp / "image"
|
||||
image.write_bytes(b"x")
|
||||
with self.assertRaises(G["Failure"]):
|
||||
G["install"]({"app": str(image), "app-version": "0.95.2+g1", "runtime": str(tarball),
|
||||
"runtime-version": "4.0.1", "gles": str(deb), "desktop": [str(self.tmp / "missing.deb")]})
|
||||
code = G["install"]({"app": str(image), "app-version": "0.95.2+g1", "runtime": str(tarball),
|
||||
"runtime-version": "4.0.1", "gles": str(deb)})
|
||||
"runtime-version": "4.0.1", "gles": str(deb), "desktop": [str(package)]})
|
||||
self.assertEqual([[str(package)]], layers)
|
||||
self.assertEqual(0, code)
|
||||
report = json.loads(out.getvalue())
|
||||
self.assertTrue(report["installed"])
|
||||
@@ -197,6 +207,147 @@ class RuntimeExtractTest(EpicTestCase):
|
||||
self.assertTrue((self.state / "app/0.95.2+g1/.droiddeck-ready").is_file())
|
||||
|
||||
|
||||
def deb(path, entries, compression="gz"):
|
||||
data = io.BytesIO()
|
||||
with tarfile.open(fileobj=data, mode="w:" + compression) as archive:
|
||||
for entry in entries:
|
||||
tar_add(archive, *entry[:1], **entry[1])
|
||||
path.write_bytes(ar([("debian-binary", b"2.0\n"), ("control.tar.xz", b"c"), ("data.tar." + compression, data.getvalue())]))
|
||||
return path
|
||||
|
||||
|
||||
class LauncherDebTest(EpicTestCase):
|
||||
def launcher(self, extra=()):
|
||||
return deb(self.tmp / "EpicGamesLauncher-x86_64.deb", [
|
||||
("./usr", {"kind": "dir"}),
|
||||
("./usr/bin/EpicGamesLauncher", {"data": b"egl", "mode": 0o755}),
|
||||
("./usr/lib/epic-games-launcher/libEOSSDK-Linux-Shipping.so", {"data": b"eos"}),
|
||||
("./usr/share/icons/hicolor/256x256@2/apps/EpicGamesLauncher.png", {"data": b"png"}),
|
||||
("./usr/share/applications/epic-games-launcher.desktop", {"data": b"[Desktop Entry]\n"}),
|
||||
("./usr/bin/egl", {"link": "/usr/bin/EpicGamesLauncher"}),
|
||||
("../escape", {"data": b"x"}),
|
||||
*extra,
|
||||
])
|
||||
|
||||
def webkit(self):
|
||||
return deb(self.tmp / "libwebkit2gtk-4.1-0_amd64.deb", [
|
||||
("./usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.1.so.0.19.1", {"data": b"webkit"}),
|
||||
("./usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.1.so.0", {"link": "libwebkit2gtk-4.1.so.0.19.1"}),
|
||||
("./usr/lib/x86_64-linux-gnu/webkit2gtk-4.1/WebKitNetworkProcess", {"data": b"net", "mode": 0o755}),
|
||||
("./usr/lib/x86_64-linux-gnu/webkit2gtk-4.1/injected-bundle/libwebkit2gtkinjectedbundle.so", {"data": b"bundle"}),
|
||||
("./usr/share/glib-2.0/schemas/org.gnome.GWeather4.gschema.xml", {"data": b"schema"}),
|
||||
("./usr/share/doc/libwebkit2gtk-4.1-0/copyright", {"data": b"c"}),
|
||||
("./usr/share/locale/fr/LC_MESSAGES/WebKitGTK-4.1.mo", {"data": b"fr"}),
|
||||
], "xz")
|
||||
|
||||
def test_the_launcher_and_its_desktop_layer_come_out_of_debs(self):
|
||||
dest = self.state / "app/0.113.0+g1"
|
||||
G["extract_app"](str(self.launcher()), str(dest), [str(self.webkit())])
|
||||
self.assertEqual(b"egl", (dest / "usr/bin/EpicGamesLauncher").read_bytes())
|
||||
self.assertTrue(os.access(dest / "usr/bin/EpicGamesLauncher", os.X_OK))
|
||||
self.assertEqual(b"eos", (dest / "usr/lib/epic-games-launcher/libEOSSDK-Linux-Shipping.so").read_bytes())
|
||||
self.assertEqual("EpicGamesLauncher", os.readlink(dest / "usr/bin/egl"))
|
||||
self.assertEqual(b"png", (dest / "epic-games-launcher.png").read_bytes())
|
||||
libs = dest / "desktop/usr/lib/x86_64-linux-gnu"
|
||||
self.assertEqual(b"webkit", (libs / "libwebkit2gtk-4.1.so.0").read_bytes())
|
||||
self.assertEqual(b"net", (libs / "webkit2gtk-4.1/WebKitNetworkProcess").read_bytes())
|
||||
self.assertEqual(b"schema", (dest / "desktop/usr/share/glib-2.0/schemas/org.gnome.GWeather4.gschema.xml").read_bytes())
|
||||
self.assertFalse((dest / "desktop/usr/share/doc").exists())
|
||||
self.assertFalse((self.state / "app/escape").exists())
|
||||
self.assertFalse((self.state / "app/0.113.0+g1.staging").exists())
|
||||
self.assertTrue(G["app_complete"](str(dest)))
|
||||
self.assertEqual(str(dest / "usr/bin/EpicGamesLauncher"), G["app_command"](str(dest)))
|
||||
self.assertEqual(str(libs), G["desktop_libs"](str(dest)))
|
||||
|
||||
def test_archive_symlinks_cannot_redirect_the_desktop_layer_or_icon(self):
|
||||
outside = self.tmp / "outside"
|
||||
outside.mkdir()
|
||||
dest = self.state / "app/0.113.0+g1"
|
||||
escape = os.path.relpath(outside, Path(str(dest) + ".staging"))
|
||||
launcher = self.launcher([("./desktop", {"link": escape}),
|
||||
("./epic-games-launcher.png", {"link": escape + "/target"})])
|
||||
with self.assertRaises(G["Failure"]) as caught:
|
||||
G["extract_app"](str(launcher), str(dest), [str(self.webkit())])
|
||||
self.assertEqual("app-extract", str(caught.exception))
|
||||
self.assertFalse((outside / "usr").exists())
|
||||
self.assertFalse((outside / "target").exists())
|
||||
|
||||
icon_dest = self.state / "app/0.113.0+g2"
|
||||
icon_escape = os.path.relpath(outside / "target", Path(str(icon_dest) + ".staging"))
|
||||
G["extract_app"](str(self.launcher([("./epic-games-launcher.png", {"link": icon_escape})])),
|
||||
str(icon_dest), [str(self.webkit())])
|
||||
self.assertEqual(b"png", (icon_dest / "epic-games-launcher.png").read_bytes())
|
||||
self.assertFalse((outside / "target").exists())
|
||||
|
||||
def test_a_launcher_without_its_desktop_layer_is_refused(self):
|
||||
dest = self.state / "app/0.113.0+g1"
|
||||
with self.assertRaises(G["Failure"]) as caught:
|
||||
G["extract_app"](str(self.launcher()), str(dest))
|
||||
self.assertEqual("app-extract", str(caught.exception))
|
||||
self.assertFalse(dest.exists())
|
||||
self.assertFalse(Path(str(dest) + ".staging").exists())
|
||||
|
||||
def test_anything_but_a_deb_is_refused_and_leaves_nothing_behind(self):
|
||||
image = self.tmp / "EpicGamesLauncher-x86_64.AppImage"
|
||||
image.write_bytes(b"\x7fELF\x02\x01\x01\x00AI\x02" + bytes(64))
|
||||
dest = self.state / "app/0.95.2+g1"
|
||||
with self.assertRaises(G["Failure"]) as caught:
|
||||
G["extract_app"](str(image), str(dest), [str(self.webkit())])
|
||||
self.assertEqual("app-format", str(caught.exception))
|
||||
plain = self.tmp / "plain.deb"
|
||||
plain.write_bytes(ar([("data.tar.gz", b"x")]))
|
||||
with self.assertRaises(G["Failure"]) as caught:
|
||||
G["extract_app"](str(self.launcher()), str(dest), [str(plain)])
|
||||
self.assertEqual("desktop-format", str(caught.exception))
|
||||
self.assertFalse(dest.exists())
|
||||
self.assertFalse(Path(str(dest) + ".staging").exists())
|
||||
|
||||
def test_an_installed_appimage_launcher_still_runs_through_apprun(self):
|
||||
app = self.state / "app/0.95.2+g1"
|
||||
(app / "usr/bin").mkdir(parents=True)
|
||||
(app / "AppRun").write_text("run")
|
||||
(app / "usr/bin/EpicGamesLauncher").write_text("bin")
|
||||
self.assertTrue(G["app_complete"](str(app)))
|
||||
self.assertEqual(str(app / "AppRun"), G["app_command"](str(app)))
|
||||
self.assertIsNone(G["desktop_libs"](str(app)))
|
||||
(app / "usr/bin/EpicGamesLauncher").unlink()
|
||||
self.assertFalse(G["app_complete"](str(app)))
|
||||
|
||||
def test_the_webkit_helpers_are_linked_into_the_runtime(self):
|
||||
dest = self.state / "app/0.113.0+g1"
|
||||
G["extract_app"](str(self.launcher()), str(dest), [str(self.webkit())])
|
||||
files = self.state / "runtime/4.0.1/files"
|
||||
(files / "lib/x86_64-linux-gnu").mkdir(parents=True)
|
||||
link = files / "lib/x86_64-linux-gnu/webkit2gtk-4.1"
|
||||
G["link_webkit"](str(files), G["desktop_libs"](str(dest)))
|
||||
self.assertFalse(os.path.isabs(os.readlink(link)))
|
||||
self.assertEqual(b"net", (link / "WebKitNetworkProcess").read_bytes())
|
||||
G["link_webkit"](str(files), G["desktop_libs"](str(dest)))
|
||||
self.assertEqual(b"net", (link / "WebKitNetworkProcess").read_bytes())
|
||||
G["link_webkit"](str(files), None)
|
||||
self.assertFalse(os.path.lexists(link))
|
||||
link.mkdir()
|
||||
(link / "own").write_text("x")
|
||||
G["link_webkit"](str(files), G["desktop_libs"](str(dest)))
|
||||
self.assertTrue((link / "own").is_file())
|
||||
self.assertTrue((dest / "desktop/usr/lib/x86_64-linux-gnu/webkit2gtk-4.1/WebKitNetworkProcess").is_file())
|
||||
|
||||
def test_only_the_deb_launcher_gets_the_desktop_libraries(self):
|
||||
with mock.patch.dict(G, {"fex_environment": lambda fex, files, base: {}}):
|
||||
plain = G["client_environment"]({"LD_LIBRARY_PATH": "/host"}, None, None, ":0", "bus", "broker")
|
||||
layered = G["client_environment"]({}, None, None, ":0", "bus", "broker", libs="/app/desktop/usr/lib/x86_64-linux-gnu")
|
||||
self.assertNotIn("LD_LIBRARY_PATH", plain)
|
||||
self.assertEqual("/app/desktop/usr/lib/x86_64-linux-gnu", layered["LD_LIBRARY_PATH"])
|
||||
self.assertEqual("/app/desktop/usr/share:/usr/local/share:/usr/share", layered["XDG_DATA_DIRS"])
|
||||
self.assertEqual("/app/desktop/usr/share/glib-2.0/schemas", layered["GSETTINGS_SCHEMA_DIR"])
|
||||
|
||||
def test_every_desktop_package_reaches_the_install(self):
|
||||
options, rest = G["parse_options"](["--app", "a.deb", "--desktop", "one.deb", "--desktop", "two.deb", "x"], lists=("desktop",))
|
||||
self.assertEqual({"app": "a.deb", "desktop": ["one.deb", "two.deb"]}, options)
|
||||
self.assertEqual(["x"], rest)
|
||||
self.assertEqual({"desktop": "two.deb"}, G["parse_options"](["--desktop", "one.deb", "--desktop", "two.deb"])[0])
|
||||
|
||||
|
||||
class ArgumentsTest(EpicTestCase):
|
||||
def test_client_arguments_win_and_paths_become_windows_paths(self):
|
||||
ovt = self.tmp / "ovt"
|
||||
|
||||
@@ -213,6 +213,18 @@ class DroiddeckctlTest(FakeAdbTest):
|
||||
started = [e["args"] for e in self.calls() if e["args"][:3] == ["shell", "am", "start"]]
|
||||
self.assertIn("com.antutu.benchmark.full/com.droiddeck.launcher.agent.AgentStartActivity", started[0])
|
||||
|
||||
def test_a_start_delivered_to_an_existing_task_is_a_failure(self):
|
||||
output = (b"Starting: Intent { cmp=com.droiddeck.launcher/.agent.AgentStartActivity (has extras) }\n"
|
||||
b"Warning: Activity not started, intent has been delivered to currently running top-most instance.\n"
|
||||
b"Status: ok\nActivity: com.droiddeck.launcher/.FrontEndActivity\nComplete\n")
|
||||
|
||||
def adb(_adb, _serial, *args, timeout=30):
|
||||
return ctl.subprocess.CompletedProcess(args, 0, output, b"")
|
||||
|
||||
with unittest.mock.patch.object(ctl, "run_adb", adb), self.assertRaises(ctl.ControlError) as caught:
|
||||
ctl.start_agent_activity("adb", "fake-1", {"mode": "steam"})
|
||||
self.assertEqual("ACTIVITY_START_FAILED", caught.exception.code)
|
||||
|
||||
def test_a_malformed_package_is_refused_before_adb(self):
|
||||
with unittest.mock.patch.dict(os.environ, {"DROIDDECK_PACKAGE": "com.example;reboot"}):
|
||||
variant = load("droiddeckctl")
|
||||
@@ -291,7 +303,7 @@ class DroiddeckctlTest(FakeAdbTest):
|
||||
code, out = self.run_ctl("run", "/usr/bin/sh", "--", "-c", "true")
|
||||
self.assertEqual(7, code)
|
||||
self.assertEqual("AGENT_COMMANDS_DISABLED", out["error"]["code"])
|
||||
self.assertIn("Setup > Diagnostics > Debugging tools", out["error"]["message"])
|
||||
self.assertIn("Setup > Developer > Debugging tools", out["error"]["message"])
|
||||
self.assertEqual(["access"], [c.get("method") for c in self.calls()])
|
||||
|
||||
def test_override_checks_names_before_pushing_any_file(self):
|
||||
@@ -417,12 +429,14 @@ class DroiddeckctlTest(FakeAdbTest):
|
||||
return ctl.subprocess.CompletedProcess(args, 127, b"", b"/system/bin/sh: content: inaccessible or not found")
|
||||
if args[:2] == ("shell", "am"):
|
||||
return ctl.subprocess.CompletedProcess(args, 0, b"Broadcast completed: result=0", b"")
|
||||
if args[:2] == ("shell", "pidof " + ctl.PACKAGE):
|
||||
return ctl.subprocess.CompletedProcess(args, 0, b"4940\n", b"")
|
||||
return ctl.subprocess.CompletedProcess(args, 0, next(answers), b"")
|
||||
|
||||
with unittest.mock.patch.object(ctl, "run_adb", adb), unittest.mock.patch.object(ctl.time, "sleep"):
|
||||
answer = ctl.provider_call("adb", "fake-1", "state", arg="a b", request={"x": 1})
|
||||
self.assertEqual({"ok": True, "command": "state"}, answer)
|
||||
broadcast = calls[1]
|
||||
broadcast = next(c for c in calls if c[:2] == ("shell", "am"))
|
||||
self.assertEqual(("shell", "am", "broadcast", "-a", ctl.PACKAGE + ".AGENT_CALL", "-p", ctl.PACKAGE), broadcast[:7])
|
||||
self.assertIn("'a b'", broadcast)
|
||||
self.assertEqual(ctl.encode_request({"x": 1}), broadcast[broadcast.index("request") + 1])
|
||||
@@ -823,5 +837,48 @@ class ScenarioTest(FakeAdbTest):
|
||||
self.assertEqual([{"appId": 7, "timeout": 15.0}] * 2, [c["request"] for c in self.calls("quit")])
|
||||
|
||||
|
||||
class ProviderFallbackTest(unittest.TestCase):
|
||||
MISSING = "/system/bin/sh: content: inaccessible or not found\n"
|
||||
|
||||
def adb(self, answers):
|
||||
calls = []
|
||||
|
||||
def run(adb, serial, *args, timeout=None):
|
||||
calls.append(args)
|
||||
for prefix, (code, out, err) in answers:
|
||||
if " ".join(args).startswith(" ".join(prefix)):
|
||||
return unittest.mock.Mock(returncode=code, stdout=out.encode(), stderr=err.encode())
|
||||
raise AssertionError(f"unexpected adb call {args}")
|
||||
return calls, unittest.mock.patch.object(ctl, "run_adb", run)
|
||||
|
||||
def test_a_command_that_prints_the_shell_error_is_not_run_again(self):
|
||||
result = 'Result: Bundle[{json={"ok":true,"stdout":{"text":"' + self.MISSING.strip() + '"}}}]'
|
||||
calls, patch = self.adb([(["shell", "content"], (0, result, ""))])
|
||||
with patch:
|
||||
self.assertTrue(ctl.provider_call("adb", "s", "native", request={"argv": ["x"]})["ok"])
|
||||
self.assertEqual(1, len(calls))
|
||||
|
||||
def test_a_shell_without_content_uses_the_broadcast(self):
|
||||
calls, patch = self.adb([
|
||||
(["shell", "content"], (127, "", self.MISSING)),
|
||||
(["shell", "pidof"], (0, "4940\n", "")),
|
||||
(["shell", "am", "broadcast"], (0, "Broadcast completed", "")),
|
||||
(["shell"], (0, '{"ok": true}', "")),
|
||||
])
|
||||
with patch:
|
||||
self.assertTrue(ctl.provider_call("adb", "s", "state")["ok"])
|
||||
self.assertEqual(["shell", "am", "broadcast"], list(calls[2][:3]))
|
||||
|
||||
def test_the_broadcast_needs_the_app_running(self):
|
||||
calls, patch = self.adb([
|
||||
(["shell", "content"], (127, "", self.MISSING)),
|
||||
(["shell", "pidof"], (1, "", "")),
|
||||
])
|
||||
with patch, self.assertRaises(ctl.ControlError) as raised:
|
||||
ctl.provider_call("adb", "s", "state")
|
||||
self.assertEqual("APP_NOT_RUNNING", raised.exception.code)
|
||||
self.assertFalse(any(c[:3] == ("shell", "am", "broadcast") for c in calls))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,314 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
USERNS = shutil.which("unshare") is not None and subprocess.run(["unshare", "-Urm", "true"], capture_output=True).returncode == 0
|
||||
|
||||
FASTPATH = Path(__file__).resolve().parents[1] / "proot/fastpath/fastpath.c"
|
||||
|
||||
PROGRAM = r"""
|
||||
#include "FASTPATH"
|
||||
#include <sys/mount.h>
|
||||
|
||||
static const char *race_from, *race_to;
|
||||
static dev_t race_dev;
|
||||
static ino_t race_ino;
|
||||
|
||||
static long trapped_nr = -1, unsupported_nr = -1;
|
||||
|
||||
static long host_sys(long nr, long a, long b, long c, long d, long e, long f) {
|
||||
if (nr == trapped_nr || (nr == unsupported_nr && a != -1)) return -ENOSYS;
|
||||
long r = syscall(nr, a, b, c, d, e, f);
|
||||
struct stat fs;
|
||||
if (r >= 0 && nr == SYS_fcntl && b == F_DUPFD_CLOEXEC && c == DIR_FD_FLOOR && race_from
|
||||
&& syscall(SYS_fstat, a, &fs) == 0 && fs.st_dev == race_dev && fs.st_ino == race_ino) {
|
||||
syscall(SYS_renameat, AT_FDCWD, race_from, AT_FDCWD, race_to);
|
||||
race_from = NULL;
|
||||
}
|
||||
return r < 0 ? -errno : r;
|
||||
}
|
||||
|
||||
static void show(long r) {
|
||||
if (r == FP_SLOW) puts("SLOW");
|
||||
else if (r < 0) printf("E%ld\n", -r);
|
||||
else puts("ok");
|
||||
}
|
||||
|
||||
static int high_fds(void) {
|
||||
int n = 0;
|
||||
for (int fd = DIR_FD_FLOOR; fd < DIR_FD_FLOOR + 4 * DIRS; fd++)
|
||||
if (fcntl(fd, F_GETFD) >= 0) n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
static char hosts[MAX_BINDS][PATH_MAX], guests[MAX_BINDS][512];
|
||||
static int taken[DIRS];
|
||||
int ntaken = 0;
|
||||
fp_sys = host_sys;
|
||||
snprintf(root, sizeof root, "%s", argv[1]);
|
||||
rootlen = strlen(root);
|
||||
int i = 2;
|
||||
for (; i + 1 < argc && strcmp(argv[i], "--"); i += 2) {
|
||||
struct bind *bd = &binds[nbinds];
|
||||
bd->host = hosts[nbinds];
|
||||
bd->guest = guests[nbinds];
|
||||
snprintf(bd->host, PATH_MAX, "%s", argv[i]);
|
||||
snprintf(bd->guest, sizeof guests[0], "%s", argv[i + 1]);
|
||||
bd->hlen = strlen(bd->host);
|
||||
bd->glen = strlen(bd->guest);
|
||||
nbinds++;
|
||||
}
|
||||
pthread_once(&init_once, fp_init);
|
||||
fp_on = 1;
|
||||
for (i++; i < argc; i++) {
|
||||
const char *op = argv[i];
|
||||
if (!strcmp(op, "fuse")) {
|
||||
for (int b = 0; b < nbinds; b++) binds[b].fuse = 1;
|
||||
} else if (!strcmp(op, "touch")) {
|
||||
long fd = fp_open(AT_FDCWD, argv[++i], O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
|
||||
if (fd >= 0) close((int)fd);
|
||||
show(fd);
|
||||
} else if (!strcmp(op, "stat")) {
|
||||
struct stat st;
|
||||
show(fp_stat(AT_FDCWD, argv[++i], AT_SYMLINK_NOFOLLOW, &st));
|
||||
} else if (!strcmp(op, "symlink")) {
|
||||
long r = fp_symlink("target", AT_FDCWD, argv[++i]);
|
||||
if (refused()) puts("REFUSED");
|
||||
else show(r);
|
||||
} else if (!strcmp(op, "trap-symlink")) {
|
||||
trapped_nr = SYS_symlinkat;
|
||||
} else if (!strcmp(op, "no-symlinks")) {
|
||||
unsupported_nr = SYS_symlinkat;
|
||||
} else if (!strcmp(op, "on")) {
|
||||
printf("%d\n", fp_ready());
|
||||
} else if (!strcmp(op, "no-folders")) {
|
||||
unsupported_nr = SYS_mkdirat;
|
||||
} else if (!strcmp(op, "busy-cache")) {
|
||||
pthread_mutex_lock(&dir_lock);
|
||||
} else if (!strcmp(op, "mkdir")) {
|
||||
long r = fp_mkdir(AT_FDCWD, argv[++i], 0755);
|
||||
if (refused()) puts("REFUSED");
|
||||
else show(r);
|
||||
} else if (!strcmp(op, "mv")) {
|
||||
show(fp_rename(AT_FDCWD, argv[i + 1], AT_FDCWD, argv[i + 2], 0));
|
||||
i += 2;
|
||||
} else if (!strcmp(op, "rm")) {
|
||||
show(fp_unlink(AT_FDCWD, argv[++i], 0));
|
||||
} else if (!strcmp(op, "host-mv")) {
|
||||
show(host_sys(SYS_renameat, AT_FDCWD, (long)argv[i + 1], AT_FDCWD, (long)argv[i + 2], 0, 0));
|
||||
i += 2;
|
||||
} else if (!strcmp(op, "race-mv")) {
|
||||
struct stat rs;
|
||||
race_from = argv[i + 1];
|
||||
race_to = argv[i + 2];
|
||||
i += 2;
|
||||
show(host_sys(SYS_newfstatat, AT_FDCWD, (long)race_from, (long)&rs, 0, 0, 0));
|
||||
race_dev = rs.st_dev;
|
||||
race_ino = rs.st_ino;
|
||||
} else if (!strcmp(op, "host-rmdir")) {
|
||||
show(host_sys(SYS_unlinkat, AT_FDCWD, (long)argv[++i], AT_REMOVEDIR, 0, 0, 0));
|
||||
} else if (!strcmp(op, "host-mkdir")) {
|
||||
show(host_sys(SYS_mkdirat, AT_FDCWD, (long)argv[++i], 0755, 0, 0, 0));
|
||||
} else if (!strcmp(op, "clobber")) {
|
||||
long null = host_sys(SYS_openat, AT_FDCWD, (long)"/dev/null", O_RDONLY, 0, 0, 0);
|
||||
for (int fd = DIR_FD_FLOOR; fd < DIR_FD_FLOOR + DIRS; fd++) host_sys(SYS_dup3, null, fd, 0, 0, 0, 0);
|
||||
host_sys(SYS_close, null, 0, 0, 0, 0, 0);
|
||||
puts("ok");
|
||||
} else if (!strcmp(op, "first-fd")) {
|
||||
char link[40], out[PATH_MAX];
|
||||
snprintf(link, sizeof link, "/proc/self/fd/%d", DIR_FD_FLOOR);
|
||||
long n = host_sys(SYS_readlinkat, AT_FDCWD, (long)link, (long)out, sizeof out - 1, 0, 0);
|
||||
out[n > 0 ? n : 0] = 0;
|
||||
puts(out);
|
||||
} else if (!strcmp(op, "fds")) {
|
||||
printf("%d\n", high_fds());
|
||||
} else if (!strcmp(op, "ttl")) {
|
||||
ttl_ns = atoll(argv[++i]) * 1000000LL;
|
||||
} else if (!strcmp(op, "sleep")) {
|
||||
usleep(atoi(argv[++i]) * 1000);
|
||||
} else if (!strcmp(op, "chmod")) {
|
||||
show(fp_chmod(AT_FDCWD, argv[i + 1], strtol(argv[i + 2], NULL, 8), 0));
|
||||
i += 2;
|
||||
} else if (!strcmp(op, "host-mount")) {
|
||||
show(mount("tmpfs", argv[++i], "tmpfs", 0, NULL) == 0 ? 0 : -errno);
|
||||
} else if (!strcmp(op, "take-over")) {
|
||||
for (int fd = DIR_FD_FLOOR; fd < DIR_FD_FLOOR + DIRS; fd++) {
|
||||
char link[40], out[PATH_MAX];
|
||||
snprintf(link, sizeof link, "/proc/self/fd/%d", fd);
|
||||
long n = host_sys(SYS_readlinkat, AT_FDCWD, (long)link, (long)out, sizeof out - 1, 0, 0);
|
||||
if (n <= 0) continue;
|
||||
out[n] = 0;
|
||||
long mine = host_sys(SYS_openat, AT_FDCWD, (long)out, O_RDONLY | O_DIRECTORY, 0, 0, 0);
|
||||
host_sys(SYS_dup3, mine, fd, 0, 0, 0, 0);
|
||||
host_sys(SYS_close, mine, 0, 0, 0, 0, 0);
|
||||
taken[ntaken++] = fd;
|
||||
}
|
||||
puts("ok");
|
||||
} else if (!strcmp(op, "taken")) {
|
||||
int open_fds = 0;
|
||||
for (int t = 0; t < ntaken; t++)
|
||||
if (lseek(taken[t], 0, SEEK_CUR) == 0) open_fds++;
|
||||
printf("%d/%d\n", open_fds, ntaken);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
class FastPathFuseDirsTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
if shutil.which("gcc") is None:
|
||||
raise unittest.SkipTest("gcc is not available")
|
||||
cls.build = tempfile.TemporaryDirectory()
|
||||
source = Path(cls.build.name) / "fuse_dirs.c"
|
||||
source.write_text(PROGRAM.replace("FASTPATH", str(FASTPATH)))
|
||||
cls.program = Path(cls.build.name) / "fuse_dirs"
|
||||
subprocess.run(["gcc", "-O1", "-Wall", "-Wextra", "-Werror", "-Wno-format-truncation", "-pthread", "-o", str(cls.program), str(source), "-ldl"], check=True)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.build.cleanup()
|
||||
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
base = Path(self.temp.name)
|
||||
self.root = base / "root"
|
||||
(self.root / "mnt").mkdir(parents=True)
|
||||
self.card = base / "card"
|
||||
(self.card / "Games/steamapps/common/Game/Data").mkdir(parents=True)
|
||||
|
||||
def ask(self, *ops, wrap=()):
|
||||
argv = [*wrap, str(self.program), str(self.root), str(self.card), "/mnt/card", "--", *ops]
|
||||
return subprocess.run(argv, capture_output=True, text=True, timeout=20, check=True).stdout.split()
|
||||
|
||||
def test_files_deep_in_a_fuse_library_are_created_renamed_and_removed(self):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "touch", f"{data}/a.tmp", "mv", f"{data}/a.tmp", f"{data}/a.idx",
|
||||
"stat", f"{data}/a.idx", "stat", f"{data}/a.tmp", "rm", f"{data}/a.idx",
|
||||
"stat", f"{data}/a.idx", "fds")
|
||||
self.assertEqual(["ok", "ok", "ok", "E2", "ok", "E2"], out[:-1])
|
||||
self.assertGreater(int(out[-1]), 0)
|
||||
self.assertEqual([], list((self.card / "Games/steamapps/common/Game/Data").iterdir()))
|
||||
|
||||
def test_a_folder_renamed_behind_the_cache_is_not_reused(self):
|
||||
game = self.card / "Games/steamapps/common/Game"
|
||||
(game / "Data/keep").write_text("x")
|
||||
out = self.ask("fuse", "stat", "/mnt/card/Games/steamapps/common/Game/Data/keep",
|
||||
"host-mv", str(game), str(game.parent / "Moved"),
|
||||
"stat", "/mnt/card/Games/steamapps/common/Game/Data/keep",
|
||||
"stat", "/mnt/card/Games/steamapps/common/Moved/Data/keep")
|
||||
self.assertEqual(["ok", "ok", "E2", "ok"], out)
|
||||
|
||||
def test_a_folder_renamed_while_it_is_opened_is_not_used_under_its_old_name(self):
|
||||
game = self.card / "Games/steamapps/common/Game"
|
||||
(game / "Data/keep").write_text("x")
|
||||
out = self.ask("fuse", "race-mv", str(game / "Data"), str(game / "Moved"),
|
||||
"stat", "/mnt/card/Games/steamapps/common/Game/Data/keep",
|
||||
"touch", "/mnt/card/Games/steamapps/common/Game/Data/new", "fds")
|
||||
self.assertEqual(["ok", "E2", "E2"], out[:3])
|
||||
self.assertEqual(["keep"], [p.name for p in (game / "Moved").iterdir()])
|
||||
self.assertLessEqual(int(out[-1]), 64)
|
||||
|
||||
def test_a_folder_deleted_and_made_again_is_looked_up_fresh(self):
|
||||
data = self.card / "Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "touch", "/mnt/card/Games/steamapps/common/Game/Data/old",
|
||||
"rm", "/mnt/card/Games/steamapps/common/Game/Data/old",
|
||||
"host-rmdir", str(data), "host-mkdir", str(data),
|
||||
"touch", "/mnt/card/Games/steamapps/common/Game/Data/new")
|
||||
self.assertEqual(["ok"] * 5, out)
|
||||
self.assertEqual(["new"], [p.name for p in data.iterdir()])
|
||||
|
||||
def test_folders_deleted_and_made_again_many_times_keep_descriptors_bounded(self):
|
||||
data = self.card / "Games/steamapps/common/Game/Data"
|
||||
ops = ["fuse"]
|
||||
for _ in range(200):
|
||||
ops += ["touch", "/mnt/card/Games/steamapps/common/Game/Data/x",
|
||||
"rm", "/mnt/card/Games/steamapps/common/Game/Data/x",
|
||||
"host-rmdir", str(data), "host-mkdir", str(data)]
|
||||
out = self.ask(*ops, "fds")
|
||||
self.assertEqual(["ok"] * 800, out[:-1])
|
||||
self.assertLessEqual(int(out[-1]), 64)
|
||||
|
||||
def test_descriptors_the_program_takes_over_are_left_alone(self):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "touch", f"{data}/a", "clobber", "touch", f"{data}/b",
|
||||
"stat", f"{data}/a", "first-fd")
|
||||
self.assertEqual(["ok", "ok", "ok", "ok", "/dev/null"], out)
|
||||
self.assertEqual({"a", "b"}, {p.name for p in (self.card / "Games/steamapps/common/Game/Data").iterdir()})
|
||||
|
||||
def test_a_folder_the_program_opened_itself_stays_open_when_the_cache_moves_on(self):
|
||||
data = self.card / "Games/steamapps/common/Game/Data"
|
||||
ops = ["fuse", "touch", "/mnt/card/Games/steamapps/common/Game/Data/a", "take-over"]
|
||||
for n in range(200):
|
||||
(data / f"d{n}").mkdir()
|
||||
ops += ["touch", f"/mnt/card/Games/steamapps/common/Game/Data/d{n}/x"]
|
||||
out = self.ask(*ops, "taken")
|
||||
self.assertEqual(["ok"] * 202, out[:-1])
|
||||
opened, total = map(int, out[-1].split("/"))
|
||||
self.assertGreater(total, 0)
|
||||
self.assertEqual(total, opened)
|
||||
self.assertEqual(200, len(list(data.glob("d*/x"))))
|
||||
|
||||
@unittest.skipIf(os.geteuid() == 0, "root passes folder permissions")
|
||||
def test_a_parent_folder_closed_by_the_program_stops_cached_access_at_once(self):
|
||||
game = self.card / "Games/steamapps/common/Game"
|
||||
(game / "Data/keep").write_text("x")
|
||||
self.addCleanup(os.chmod, game, 0o755)
|
||||
out = self.ask("fuse", "stat", "/mnt/card/Games/steamapps/common/Game/Data/keep",
|
||||
"chmod", "/mnt/card/Games/steamapps/common/Game", "000",
|
||||
"stat", "/mnt/card/Games/steamapps/common/Game/Data/keep",
|
||||
"touch", "/mnt/card/Games/steamapps/common/Game/Data/new")
|
||||
self.assertEqual(["ok", "ok"], out[:2])
|
||||
self.assertNotIn("ok", out[2:])
|
||||
os.chmod(game, 0o755)
|
||||
self.assertEqual(["keep"], [p.name for p in (game / "Data").iterdir()])
|
||||
|
||||
@unittest.skipUnless(USERNS, "mount namespaces are not available")
|
||||
def test_a_mount_placed_over_a_cached_folder_receives_later_writes(self):
|
||||
data = self.card / "Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "ttl", "1", "touch", "/mnt/card/Games/steamapps/common/Game/Data/under",
|
||||
"host-mount", str(data), "sleep", "20",
|
||||
"touch", "/mnt/card/Games/steamapps/common/Game/Data/over",
|
||||
"stat", "/mnt/card/Games/steamapps/common/Game/Data/under", wrap=("unshare", "-Urm"))
|
||||
self.assertEqual(["ok", "ok", "ok", "E2"], out)
|
||||
self.assertEqual(["under"], [p.name for p in data.iterdir()])
|
||||
|
||||
def test_a_call_the_filesystem_does_not_implement_leaves_the_fast_path_on(self):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "no-symlinks", "touch", f"{data}/a", "symlink", f"{data}/link", "on",
|
||||
"stat", f"{data}/a", "symlink", f"{data}/link", "on", "touch", f"{data}/b")
|
||||
self.assertEqual(["ok", "REFUSED", "1", "ok", "REFUSED", "1", "ok"], out)
|
||||
self.assertEqual(["a", "b"], sorted(p.name for p in (self.card / "Games/steamapps/common/Game/Data").iterdir()))
|
||||
|
||||
def test_repeated_unsupported_calls_close_the_folders_they_opened(self):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
ops = [op for n in range(40) for op in ("mkdir", f"{data}/new{n}")]
|
||||
out = self.ask("fuse", "no-folders", "busy-cache", *ops, "on", "fds")
|
||||
self.assertEqual(["REFUSED"] * 40 + ["1", "0"], out)
|
||||
|
||||
def test_a_trapped_call_still_stops_the_fast_path_for_the_process(self):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "trap-symlink", "symlink", f"{data}/link", "on")
|
||||
self.assertEqual(["REFUSED", "0"], out)
|
||||
|
||||
def test_a_symlink_is_made_where_the_filesystem_allows_it(self):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "symlink", f"{data}/link", "on")
|
||||
self.assertEqual(["ok", "1"], out)
|
||||
self.assertEqual("target", os.readlink(self.card / "Games/steamapps/common/Game/Data/link"))
|
||||
|
||||
def test_other_filesystems_keep_absolute_paths_and_open_no_folders(self):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
out = self.ask("touch", f"{data}/a", "mv", f"{data}/a", f"{data}/b", "rm", f"{data}/b", "fds")
|
||||
self.assertEqual(["ok", "ok", "ok", "0"], out)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -950,6 +950,22 @@ class GameEnvironmentTest(unittest.TestCase):
|
||||
self.assertEqual(MODULE["apply_config"](env, config, "42")["TU_DEBUG"], "sysmem")
|
||||
self.assertNotIn("TU_DEBUG", MODULE["apply_config"](env, config, "43"))
|
||||
|
||||
def test_games_run_with_gl_error_checks_unless_the_user_turns_them_off(self):
|
||||
gpu = MODULE["game_gpu"]
|
||||
session = {"MESA_NO_ERROR": "1", "DROIDDECK_GAME_MESA_NO_ERROR": "", "KEEP": "1"}
|
||||
self.assertEqual(gpu(session), {"KEEP": "1"})
|
||||
self.assertEqual(gpu(session, True), {"MESA_NO_ERROR": "1", "KEEP": "1"})
|
||||
self.assertEqual(gpu({"MESA_NO_ERROR": "1"}), {"MESA_NO_ERROR": "1"})
|
||||
self.assertEqual(gpu({"MESA_NO_ERROR": "1", "DROIDDECK_GAME_MESA_NO_ERROR": "1"}), {"MESA_NO_ERROR": "1"})
|
||||
both = gpu({"MESA_NO_ERROR": "1", "DROIDDECK_GAME_MESA_NO_ERROR": "", "TU_DEBUG": "sysmem", "DROIDDECK_GAME_TU_DEBUG": "nolrz"})
|
||||
self.assertEqual(both, {"TU_DEBUG": "nolrz"})
|
||||
self.assertEqual(gpu({"TU_DEBUG": "sysmem", "DROIDDECK_GAME_TU_DEBUG": "", "MESA_NO_ERROR": "1"}),
|
||||
{"MESA_NO_ERROR": "1"})
|
||||
config = {"version": 1, "games": {"42": {"MESA_NO_ERROR": "1"}}}
|
||||
env = gpu(session)
|
||||
self.assertEqual(MODULE["apply_config"](env, config, "42")["MESA_NO_ERROR"], "1")
|
||||
self.assertNotIn("MESA_NO_ERROR", MODULE["apply_config"](env, config, "43"))
|
||||
|
||||
def test_a_chromium_app_keeps_the_sessions_turnip_debug(self):
|
||||
gpu = MODULE["game_gpu"]
|
||||
self.assertEqual(gpu({"TU_DEBUG": "sysmem", "DROIDDECK_GAME_TU_DEBUG": "", "KEEP": "1"}, True),
|
||||
@@ -992,9 +1008,11 @@ class GameEnvironmentTest(unittest.TestCase):
|
||||
|
||||
def test_the_vram_cap_comes_before_the_games_own_dxvk_settings(self):
|
||||
cap = MODULE["vram_cap"]
|
||||
self.assertEqual(cap({"BL_VRAM_MB": "4096"})["DXVK_CONFIG"], "dxgi.maxDeviceMemory = 4096")
|
||||
self.assertEqual(cap({"BL_VRAM_MB": "4096"})["DXVK_CONFIG"], "dxgi.maxDeviceMemory = 4096; dxgi.maxSharedMemory = 4096")
|
||||
self.assertEqual(cap({"BL_VRAM_MB": "8192"})["DXVK_CONFIG"], "dxgi.maxDeviceMemory = 8192; dxgi.maxSharedMemory = 8192")
|
||||
self.assertEqual(cap({"BL_VRAM_MB": "4095"})["DXVK_CONFIG"], "dxgi.maxDeviceMemory = 4095; dxgi.maxSharedMemory = 4095; d3d9.maxAvailableMemory = 4095")
|
||||
self.assertEqual(cap({"BL_VRAM_MB": "2048", "DXVK_CONFIG": "dxvk.maxFrameRate = 60;"})["DXVK_CONFIG"],
|
||||
"dxgi.maxDeviceMemory = 2048; dxvk.maxFrameRate = 60")
|
||||
"dxgi.maxDeviceMemory = 2048; dxgi.maxSharedMemory = 2048; d3d9.maxAvailableMemory = 2048; dxvk.maxFrameRate = 60")
|
||||
for off in ({}, {"BL_VRAM_MB": "0"}, {"BL_VRAM_MB": "lots"}, {"BL_VRAM_MB": "²"}, {"BL_VRAM_MB": "-1"}):
|
||||
self.assertNotIn("DXVK_CONFIG", cap(off))
|
||||
self.assertNotIn("DROIDDECK_VRAM_CAP", cap(off))
|
||||
@@ -1124,6 +1142,23 @@ class GameEnvironmentTest(unittest.TestCase):
|
||||
scope = MODULE["game_id"](env)
|
||||
expected = "untouched" if scope is None else scope if scope in {"42", "4294967295", "2147483648"} else "*"
|
||||
self.assertEqual(target.read_text(), expected)
|
||||
for app, expected in (("42", "42"), ("4294967295", "4294967295"), ("43", "*"), ("", "*"), ("x", "*")):
|
||||
with self.subTest(shared=app):
|
||||
env = dict(os.environ, HOME=str(root), STEAM_COMPAT_DATA_PATH="/steam/compatdata/2929927072", SteamAppId=app)
|
||||
target.write_text("untouched")
|
||||
subprocess.run(["bash", "-c", script, "test", str(base)], env=env, check=True, capture_output=True)
|
||||
self.assertEqual(MODULE["game_id"](env), app if app in {"42", "4294967295", "43"} else "2929927072")
|
||||
self.assertEqual(target.read_text(), expected)
|
||||
for game, expected in (
|
||||
(str(42 << 32 | 0x02000000), "42"), (str(4294967295 << 32 | 0x02000000), "4294967295"),
|
||||
(str(42 << 32 | 0x02000001), "*"), (str(42 << 32 | 0x02000000 | 1 << 64), "*"), ("33554432", "*"), ("x", "*"),
|
||||
):
|
||||
with self.subTest(direct=game):
|
||||
env = dict(os.environ, HOME=str(root), STEAM_COMPAT_DATA_PATH="/steam/compatdata/2929927072", SteamAppId="0", SteamGameId=game)
|
||||
target.write_text("untouched")
|
||||
subprocess.run(["bash", "-c", script, "test", str(base)], env=env, check=True, capture_output=True)
|
||||
self.assertEqual(MODULE["game_id"](env), expected if expected != "*" else "2929927072")
|
||||
self.assertEqual(target.read_text(), expected)
|
||||
|
||||
def test_engine_fixes_follow_the_games_files(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
@@ -1503,6 +1538,19 @@ class GameEnvironmentTest(unittest.TestCase):
|
||||
config = {"version": 1, "shared": {}, "games": {"42": values}}
|
||||
self.assertEqual(MODULE["apply_config"]({}, config, "42", env), values, name)
|
||||
|
||||
def test_ff7_rebirth_runs_at_feature_level_12_2_unless_the_game_overrides_it(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp) / "FINAL FANTASY VII REBIRTH"
|
||||
game = root / "End/Binaries/Win64/ff7rebirth_.exe"
|
||||
game.parent.mkdir(parents=True)
|
||||
for exe in (root / "ff7rebirth.exe", game):
|
||||
exe.touch()
|
||||
env, extra, notes = MODULE["engine_fixes"](str(exe), [])
|
||||
self.assertEqual(env, {"VKD3D_FEATURE_LEVEL": "12_2"}, exe.name)
|
||||
self.assertTrue(notes)
|
||||
config = {"version": 1, "shared": {}, "games": {"42": {"VKD3D_FEATURE_LEVEL": "12_1"}}}
|
||||
self.assertEqual(MODULE["apply_config"]({}, config, "42", env), {"VKD3D_FEATURE_LEVEL": "12_1"})
|
||||
|
||||
def test_unity_players_keep_tso_over_the_preset(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
def game(*names):
|
||||
@@ -1628,6 +1676,10 @@ class GameEnvironmentTest(unittest.TestCase):
|
||||
self.assertIsNone(MODULE["game_id"]({"STEAM_COMPAT_DATA_PATH": prefix}))
|
||||
self.assertEqual(MODULE["game_id"]({"STEAM_COMPAT_DATA_PATH": "/a/compatdata/42/"}), "42")
|
||||
self.assertEqual(MODULE["game_id"]({"STEAM_COMPAT_DATA_PATH": "/a/compatdata/-1"}), "4294967295")
|
||||
shared = {"STEAM_COMPAT_DATA_PATH": "/a/compatdata/2929927072", "SteamAppId": "2424921676"}
|
||||
self.assertEqual(MODULE["game_id"](shared), "2424921676")
|
||||
self.assertEqual(MODULE["game_id"]({"STEAM_COMPAT_DATA_PATH": "/a/compatdata/2929927072"}), "2929927072")
|
||||
self.assertEqual(MODULE["game_id"]({"STEAM_COMPAT_DATA_PATH": "/a/compatdata/42", "SteamAppId": "7"}), "42")
|
||||
|
||||
def test_launch_reads_updates_preserves_argv_and_does_not_execute_values(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
@@ -2717,6 +2769,17 @@ class ForeignSystemDllTest(unittest.TestCase):
|
||||
self.assertFalse((system32 / "ucrtbase.dll").is_symlink())
|
||||
self.assertEqual(0x8664, MODULE["pe_machine"](system32 / "userlib.dll"))
|
||||
|
||||
def test_x64_visual_cpp_runtime_in_arm64_system32_is_kept(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
proton, compat, windows = self.prefix(tmp)
|
||||
builtins = proton.parent / "files/lib/wine/aarch64-windows"
|
||||
for name in ("msvcp140.dll", "VCRUNTIME140.dll", "msvcp_win.dll"):
|
||||
(builtins / name.lower()).write_bytes(pe_image(0xAA64))
|
||||
(windows / "system32" / name).write_bytes(pe_image(0x8664))
|
||||
self.assertEqual(["system32/msvcp_win.dll (machine 0x8664)"], self.run_repair(compat, proton))
|
||||
self.assertFalse((windows / "system32/msvcp140.dll").is_symlink())
|
||||
self.assertFalse((windows / "system32/VCRUNTIME140.dll").is_symlink())
|
||||
|
||||
def test_final_arm64ec_image_is_not_repaired_as_a_foreign_x64_dll(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
proton, compat, windows = self.prefix(tmp)
|
||||
|
||||
@@ -9,7 +9,7 @@ from pathlib import Path
|
||||
|
||||
BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin"
|
||||
KEYS = ("TU_DEBUG", "DROIDDECK_GAME_TU_DEBUG", "MESA_LOADER_DRIVER_OVERRIDE", "GALLIUM_DRIVER",
|
||||
"LIBGL_KOPPER_DRI2", "VK_ICD_FILENAMES", "VK_DRIVER_FILES")
|
||||
"LIBGL_KOPPER_DRI2", "VK_ICD_FILENAMES", "VK_DRIVER_FILES", "MESA_NO_ERROR", "DROIDDECK_GAME_MESA_NO_ERROR")
|
||||
SESSION = {"TU_DEBUG": "sysmem", "DROIDDECK_GAME_TU_DEBUG": ""}
|
||||
GPU_SESSION = {"MESA_LOADER_DRIVER_OVERRIDE": "zink", "GALLIUM_DRIVER": "zink",
|
||||
"LIBGL_KOPPER_DRI2": "true", "VK_ICD_FILENAMES": "/runtime/turnip.json",
|
||||
@@ -68,6 +68,15 @@ class GameGpuLaunchersTest(unittest.TestCase):
|
||||
self.chromium()
|
||||
self.assertEqual("sysmem", self.wrapper("--for", str(self.app / "files"))["TU_DEBUG"])
|
||||
|
||||
def test_programs_get_gl_error_checks_back_while_chromium_keeps_the_client_setting(self):
|
||||
session = dict(GPU_SESSION, MESA_NO_ERROR="1", DROIDDECK_GAME_MESA_NO_ERROR="")
|
||||
values = self.wrapper(session=session)
|
||||
self.assertEqual(("unset", "unset"), (values["MESA_NO_ERROR"], values["DROIDDECK_GAME_MESA_NO_ERROR"]))
|
||||
self.assertEqual(GPU_SESSION, {key: values[key] for key in GPU_SESSION})
|
||||
self.assertEqual("1", self.wrapper("--chromium", session=session)["MESA_NO_ERROR"])
|
||||
self.assertEqual("unset", self.flatpak(session)["MESA_NO_ERROR"])
|
||||
self.assertEqual("1", self.wrapper(session={"MESA_NO_ERROR": "1"})["MESA_NO_ERROR"])
|
||||
|
||||
def test_without_the_game_setting_the_wrapper_changes_nothing(self):
|
||||
self.assertEqual("sysmem", self.wrapper(session={"TU_DEBUG": "sysmem"})["TU_DEBUG"])
|
||||
self.assertEqual("unset", self.wrapper(session={})["TU_DEBUG"])
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
SESSION = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-session'
|
||||
|
||||
|
||||
class GameResolutionTest(unittest.TestCase):
|
||||
def launch(self, mode, **overrides):
|
||||
source = SESSION.read_text()
|
||||
start = source.index(' width=${BL_WIDTH:-1280}')
|
||||
end = source.index('\nfi\n\nbl_displays()', start)
|
||||
block = source[start:end].replace('/usr/local/bin/droiddeck-pipewire', 'pipewire_stub')
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
env = dict(os.environ, XDG_RUNTIME_DIR=directory, BL_REFRESH='60', BL_MANGOAPP='0',
|
||||
BL_WIDTH='2560', BL_HEIGHT='1600')
|
||||
for name in ('BL_GAME_WIDTH', 'BL_GAME_HEIGHT', 'BL_FOLLOW_OUTPUT', 'BL_GAMESCOPE_STRETCH_16X9'):
|
||||
env.pop(name, None)
|
||||
env.update(overrides)
|
||||
fake = Path(directory) / 'gamescope'
|
||||
fake.write_text('#!/bin/bash\nprintf "ARG:%s\\n" "$@"\n')
|
||||
fake.chmod(0o755)
|
||||
env['PATH'] = directory + os.pathsep + env['PATH']
|
||||
script = f'''
|
||||
set -u
|
||||
mode={mode}
|
||||
pipewire_stub() {{ return 1; }}
|
||||
run_gamescope() {{ printf 'ARG:%s\n' "$@"; }}
|
||||
''' + block
|
||||
result = subprocess.run(['bash', '-c', script], env=env, capture_output=True, text=True, timeout=5)
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
return [line.removeprefix('ARG:') for line in result.stdout.splitlines() if line.startswith('ARG:')]
|
||||
|
||||
def test_a_game_started_without_steam_runs_at_its_resolution_with_proportions_kept(self):
|
||||
args = self.launch('run', BL_GAME_WIDTH='1280', BL_GAME_HEIGHT='720')
|
||||
self.assertEqual(['2560', '1600'], [args[args.index('-W') + 1], args[args.index('-H') + 1]])
|
||||
self.assertEqual(['1280', '720'], [args[args.index('-w') + 1], args[args.index('-h') + 1]])
|
||||
self.assertNotIn('-S', args)
|
||||
|
||||
def test_without_a_game_resolution_the_game_matches_the_output(self):
|
||||
args = self.launch('run')
|
||||
self.assertEqual(['2560', '1600'], [args[args.index('-w') + 1], args[args.index('-h') + 1]])
|
||||
|
||||
def test_steam_ignores_the_game_resolution(self):
|
||||
args = self.launch('steam', BL_GAME_WIDTH='1280', BL_GAME_HEIGHT='720')
|
||||
self.assertEqual(['2560', '1600'], [args[args.index('-w') + 1], args[args.index('-h') + 1]])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -2,7 +2,7 @@ from pathlib import Path
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from tools.tests.test_client_zram_focus import function_source, run_bash
|
||||
from tools.tests.test_client_zram_focus import SCRIPT, function_source, run_bash
|
||||
|
||||
|
||||
class MemoryPressureTest(unittest.TestCase):
|
||||
@@ -20,8 +20,8 @@ class MemoryPressureTest(unittest.TestCase):
|
||||
|
||||
def run_policy(self, body):
|
||||
functions = '\n'.join(function_source(name) for name in (
|
||||
'bl_memory_read', 'bl_memory_tight', 'bl_client_can_pageout',
|
||||
'bl_client_can_readback', 'bl_client_pressure'))
|
||||
'bl_memory_read', 'bl_memory_tight', 'bl_memory_critical', 'bl_client_can_pageout',
|
||||
'bl_client_can_readback', 'bl_client_pressure', 'bl_client_pressure_result', 'bl_zram_has_room'))
|
||||
functions = functions.replace('/proc/meminfo', str(self.root / 'meminfo'))
|
||||
functions = functions.replace('/proc/pressure/memory', str(self.root / 'pressure'))
|
||||
harness = '''
|
||||
@@ -29,7 +29,7 @@ set -u
|
||||
_bl_pids=(123) _bl_game=1 _bl_packed=-1 _bl_pressure_at=-1 _bl_pressure_backoff=30
|
||||
rss=$((384 * 1024)) client_swap=$((512 * 1024)) reclaimed=$((128 * 1024))
|
||||
step() { :; }
|
||||
bl_zram_usage() { _bl_rss=$rss _bl_swap=$client_swap; }
|
||||
bl_zram_usage() { _bl_rss=$rss _bl_anon=${anon:-$rss} _bl_swap=$client_swap; }
|
||||
bl_zram_send() { echo "send:$1:$3"; rss=$((rss - reclaimed)); _bl_pageout_complete=${pageout_complete:-1}; }
|
||||
''' + functions + '\nbl_memory_read\n' + body
|
||||
result = run_bash('NOW=0\n' + harness.replace('SECONDS', 'NOW'))
|
||||
@@ -80,6 +80,13 @@ if bl_client_can_readback; then echo yes; else echo no; fi
|
||||
self.assertEqual(['no'], self.run_policy('''
|
||||
bl_client_pressure
|
||||
if bl_client_can_pageout; then echo yes; else echo no; fi
|
||||
'''))
|
||||
|
||||
def test_client_swap_budget_excludes_file_backed_memory(self):
|
||||
self.memory(swap_free=1536)
|
||||
self.assertEqual(['send:1:client'], self.run_policy('''
|
||||
rss=$((2048 * 1024)) anon=$((256 * 1024))
|
||||
bl_client_pressure
|
||||
'''))
|
||||
|
||||
def test_without_swap_automatic_pageout_is_disabled(self):
|
||||
@@ -152,20 +159,20 @@ class SessionPressureTest(unittest.TestCase):
|
||||
|
||||
def run_policy(self, body):
|
||||
functions = '\n'.join(function_source(name) for name in (
|
||||
'bl_memory_read', 'bl_memory_tight', 'bl_memory_critical', 'bl_low_ram',
|
||||
'bl_session_pressure', 'bl_watch_nap', 'bl_zram_pick', 'bl_tier_note', 'bl_tier_readback'))
|
||||
'bl_memory_read', 'bl_memory_tight', 'bl_memory_critical',
|
||||
'bl_session_pressure', 'bl_zram_has_room', 'bl_watch_nap', 'bl_zram_pick', 'bl_tier_note', 'bl_tier_readback'))
|
||||
functions = functions.replace('/proc/meminfo', str(self.root / 'meminfo'))
|
||||
functions = functions.replace('/proc/pressure/memory', str(self.root / 'pressure'))
|
||||
harness = '''
|
||||
set -u
|
||||
_bl_game=1 _bl_tier_at=-1 _bl_tier_backoff=30 _bl_tier_pushed=0 _bl_tier_back=0 _bl_tier_targets=() _bl_tier_swaps=() _bl_tier_starts=() _bl_pids=(999)
|
||||
_bl_pressure_at=-1 _bl_packed=-1
|
||||
rss=$((1536 * 1024)) swap=0 pushed=$((1024 * 1024)) alive=999 started=100 complete=1
|
||||
rss=$((1536 * 1024)) swap=0 pushed=$((1024 * 1024)) alive="999 321 654" started=100 complete=1
|
||||
step() { :; }
|
||||
bl_proc_start() { _bl_start=; case " $alive " in *" $1 "*) _bl_start=$started; return 0 ;; esac; return 1; }
|
||||
nap() { echo "nap:$1"; }
|
||||
bl_session_others() { _bl_session=(321 654); }
|
||||
bl_zram_usage() { _bl_rss=$rss _bl_swap=$swap; }
|
||||
bl_zram_usage() { _bl_rss=$rss _bl_anon=${anon:-$rss} _bl_swap=$swap; }
|
||||
bl_zram_send() { echo "send:$1:$3"; rss=$((rss - pushed)); swap=$((swap + pushed)); _bl_pageout_complete=$complete; }
|
||||
''' + functions + '\nbl_memory_read\n' + body
|
||||
result = run_bash('NOW=0\n' + harness.replace('SECONDS', 'NOW'))
|
||||
@@ -174,19 +181,27 @@ bl_zram_send() { echo "send:$1:$3"; rss=$((rss - pushed)); swap=$((swap + pushed
|
||||
return result.stdout.splitlines()
|
||||
|
||||
def test_low_headroom_compresses_the_game_session_at_most_every_30_seconds(self):
|
||||
self.assertEqual(['send:1:game', 'send:1:game'], self.run_policy('''
|
||||
self.assertEqual(['send:1:session', 'send:1:session'], self.run_policy('''
|
||||
SECONDS=0; bl_session_pressure
|
||||
SECONDS=10; bl_session_pressure
|
||||
SECONDS=29; bl_session_pressure
|
||||
SECONDS=30; bl_session_pressure
|
||||
'''))
|
||||
|
||||
def test_twelve_gigabyte_devices_are_included_and_larger_ones_are_not(self):
|
||||
for total, expected in ((11264, ['send:1:game']), (12288, ['send:1:game']), (15360, [])):
|
||||
def test_pressure_policy_is_independent_of_ram_size(self):
|
||||
for total, expected in ((11264, ['send:1:session']), (12288, ['send:1:session']), (16384, ['send:1:session'])):
|
||||
with self.subTest(total=total):
|
||||
self.memory(total=total)
|
||||
self.assertEqual(expected, self.run_policy('bl_session_pressure'))
|
||||
|
||||
def test_large_device_with_real_headroom_does_not_reclaim(self):
|
||||
self.memory(total=16384, available=5120, psi='12.34')
|
||||
self.assertEqual([], self.run_policy('bl_session_pressure'))
|
||||
|
||||
def test_large_device_with_psi_and_moderate_headroom_reclaims(self):
|
||||
self.memory(total=16384, available=3072, psi='1.23')
|
||||
self.assertEqual(['send:1:session'], self.run_policy('bl_session_pressure'))
|
||||
|
||||
def test_no_game_does_not_compress_the_session(self):
|
||||
self.assertEqual([], self.run_policy('_bl_game=0; bl_session_pressure'))
|
||||
|
||||
@@ -196,15 +211,26 @@ SECONDS=30; bl_session_pressure
|
||||
|
||||
def test_stalls_with_moderate_headroom_compress_before_the_hard_limit(self):
|
||||
self.memory(available=2048, psi='1.50')
|
||||
self.assertEqual(['send:1:game'], self.run_policy('bl_session_pressure'))
|
||||
self.assertEqual(['send:1:session'], self.run_policy('bl_session_pressure'))
|
||||
|
||||
def test_moderate_headroom_without_stalls_waits(self):
|
||||
self.memory(available=2048)
|
||||
self.assertEqual([], self.run_policy('bl_session_pressure'))
|
||||
|
||||
def test_same_second_memory_read_preserves_the_pressure_baseline(self):
|
||||
self.memory(available=2048)
|
||||
meminfo = self.root / 'meminfo'
|
||||
self.assertEqual(['1048576:524288:2097152'], self.run_policy(f'''
|
||||
SECONDS=100
|
||||
_bl_sample_at=100 _bl_sample_available=2097152 _bl_growth=0 _bl_fall=524288
|
||||
sed -i 's/MemAvailable: 2097152 kB/MemAvailable: 1048576 kB/' {meminfo}
|
||||
bl_memory_read
|
||||
echo "$_bl_available:$_bl_fall:$_bl_sample_available"
|
||||
'''))
|
||||
|
||||
def test_game_reading_back_most_of_a_pageout_skips_and_doubles_the_wait(self):
|
||||
self.assertEqual(['send:1:game', 'backoff:60', 'send:1:game', 'backoff:60',
|
||||
'send:1:game', 'backoff:30'], self.run_policy('''
|
||||
self.assertEqual(['send:1:session', 'backoff:60', 'send:1:session', 'backoff:60',
|
||||
'send:1:session', 'backoff:30'], self.run_policy('''
|
||||
pushed=$((512 * 1024))
|
||||
SECONDS=0; bl_session_pressure
|
||||
swap=$((swap / 4))
|
||||
@@ -226,7 +252,7 @@ echo "backoff:$_bl_tier_backoff"
|
||||
|
||||
def test_nearly_out_of_memory_ignores_the_backoff_but_keeps_30_seconds(self):
|
||||
self.memory(available=512)
|
||||
self.assertEqual(['send:1:game', 'send:1:game'], self.run_policy('''
|
||||
self.assertEqual(['send:1:session', 'send:1:session'], self.run_policy('''
|
||||
_bl_tier_backoff=300
|
||||
SECONDS=0; bl_session_pressure
|
||||
SECONDS=20; bl_session_pressure
|
||||
@@ -235,7 +261,7 @@ SECONDS=30; bl_session_pressure
|
||||
|
||||
def test_nearly_out_of_memory_still_backs_off_when_the_game_reads_its_pageout_back(self):
|
||||
self.memory(available=512)
|
||||
self.assertEqual(['send:1:game', 'backoff:60', 'send:1:game'], self.run_policy('''
|
||||
self.assertEqual(['send:1:session', 'backoff:60', 'send:1:session'], self.run_policy('''
|
||||
SECONDS=0; bl_session_pressure
|
||||
swap=0
|
||||
SECONDS=30; bl_session_pressure
|
||||
@@ -247,7 +273,7 @@ SECONDS=90; bl_session_pressure
|
||||
|
||||
def test_nearly_out_of_memory_retries_a_skipped_pageout_after_30_seconds(self):
|
||||
self.memory(available=512)
|
||||
self.assertEqual(['send:1:game', 'send:1:game'], self.run_policy('''
|
||||
self.assertEqual(['send:1:session', 'send:1:session'], self.run_policy('''
|
||||
SECONDS=0; bl_session_pressure
|
||||
swap=0
|
||||
SECONDS=30; bl_session_pressure
|
||||
@@ -263,6 +289,24 @@ SECONDS=360; bl_session_pressure
|
||||
self.memory(swap_free=1536, swap_total=2048)
|
||||
self.assertEqual([], self.run_policy('bl_session_pressure'))
|
||||
|
||||
def test_file_backed_resident_memory_does_not_consume_the_swap_budget(self):
|
||||
self.memory(swap_free=3088, swap_total=6144)
|
||||
self.assertEqual(['send:1:session'], self.run_policy('''
|
||||
rss=$((4096 * 1024)) anon=$((1024 * 1024))
|
||||
bl_session_pressure
|
||||
'''))
|
||||
|
||||
def test_anonymous_memory_and_pending_client_still_need_the_full_swap_reserve(self):
|
||||
self.memory(swap_free=3088, swap_total=6144)
|
||||
self.assertEqual([], self.run_policy('''
|
||||
rss=$((4096 * 1024)) anon=$((2048 * 1024))
|
||||
bl_session_pressure
|
||||
'''))
|
||||
self.assertEqual([], self.run_policy('''
|
||||
rss=$((4096 * 1024)) anon=$((1536 * 1024)) _bl_client_reserve=$((512 * 1024))
|
||||
bl_session_pressure
|
||||
'''))
|
||||
|
||||
def test_small_session_is_left_alone(self):
|
||||
self.assertEqual([], self.run_policy('rss=$((128 * 1024)); bl_session_pressure'))
|
||||
|
||||
@@ -275,10 +319,10 @@ SECONDS=360; bl_session_pressure
|
||||
self.assertEqual(['nap:5'], self.run_policy('bl_watch_nap'))
|
||||
self.assertEqual(['nap:5'], self.run_policy('_bl_game=0; bl_watch_nap'))
|
||||
|
||||
def test_one_push_compresses_the_game_and_the_launcher_and_restarts_the_launcher_wait(self):
|
||||
self.assertEqual(['send:1:game', 'pids:321 654 999', 'client:0:0'], self.run_policy('''
|
||||
def test_session_push_preserves_client_timestamps(self):
|
||||
self.assertEqual(['send:1:session', 'pids:321 654', 'client:-1:-1'], self.run_policy('''
|
||||
SECONDS=0; bl_session_pressure
|
||||
bl_zram_pick game; echo "pids:${_pids[*]}"
|
||||
bl_zram_pick session; echo "pids:${_pids[*]}"
|
||||
echo "client:$_bl_pressure_at:$_bl_packed"
|
||||
'''))
|
||||
|
||||
@@ -286,19 +330,19 @@ echo "client:$_bl_pressure_at:$_bl_packed"
|
||||
self.assertEqual(['pids:321 654', 'client:-1:-1'], self.run_policy('''
|
||||
BL_ZRAM_CLIENT=0
|
||||
SECONDS=0; bl_session_pressure
|
||||
bl_zram_pick game; echo "pids:${_pids[*]}"
|
||||
bl_zram_pick session; echo "pids:${_pids[*]}"
|
||||
echo "client:$_bl_pressure_at:$_bl_packed"
|
||||
''')[1:])
|
||||
|
||||
def test_low_ram_devices_compress_with_a_quarter_of_memory_left(self):
|
||||
self.memory(available=1700)
|
||||
self.assertEqual(['send:1:game'], self.run_policy('bl_session_pressure'))
|
||||
self.assertEqual(['send:1:session'], self.run_policy('bl_session_pressure'))
|
||||
self.memory(available=1900)
|
||||
self.assertEqual([], self.run_policy('bl_session_pressure'))
|
||||
|
||||
|
||||
def test_a_compressed_process_that_exits_is_not_counted_as_read_back(self):
|
||||
self.assertEqual(['send:1:game', 'send:1:game', 'backoff:30'], self.run_policy('''
|
||||
self.assertEqual(['send:1:session', 'send:1:session', 'backoff:30'], self.run_policy('''
|
||||
SECONDS=0; bl_session_pressure
|
||||
alive=""; swap=0
|
||||
SECONDS=30; bl_session_pressure
|
||||
@@ -306,7 +350,7 @@ echo "backoff:$_bl_tier_backoff"
|
||||
'''))
|
||||
|
||||
def test_a_process_that_starts_after_a_pageout_is_not_in_its_read_back(self):
|
||||
self.assertEqual(['send:1:game', 'targets:999', 'send:1:game', 'backoff:30'], self.run_policy('''
|
||||
self.assertEqual(['send:1:session', 'targets:321 654', 'send:1:session', 'backoff:30'], self.run_policy('''
|
||||
SECONDS=0; bl_session_pressure
|
||||
echo "targets:${_bl_tier_targets[*]}"
|
||||
alive="999 1234"
|
||||
@@ -315,7 +359,7 @@ echo "backoff:$_bl_tier_backoff"
|
||||
'''))
|
||||
|
||||
def test_an_incomplete_pageout_does_not_restart_the_launcher_wait(self):
|
||||
self.assertEqual(['send:1:game', 'client:-1:-1'], self.run_policy('''
|
||||
self.assertEqual(['send:1:session', 'client:-1:-1'], self.run_policy('''
|
||||
complete=0
|
||||
SECONDS=0; bl_session_pressure
|
||||
echo "client:$_bl_pressure_at:$_bl_packed"
|
||||
@@ -323,7 +367,7 @@ echo "client:$_bl_pressure_at:$_bl_packed"
|
||||
|
||||
|
||||
def test_a_reused_process_id_is_not_counted_as_read_back(self):
|
||||
self.assertEqual(['send:1:game', 'send:1:game', 'backoff:30'], self.run_policy('''
|
||||
self.assertEqual(['send:1:session', 'send:1:session', 'backoff:30'], self.run_policy('''
|
||||
SECONDS=0; bl_session_pressure
|
||||
started=200; swap=0
|
||||
SECONDS=30; bl_session_pressure
|
||||
@@ -344,5 +388,112 @@ echo "backoff:$_bl_tier_backoff"
|
||||
self.assertEqual(['gone'], result.stdout.split())
|
||||
|
||||
|
||||
class ReclaimDispatchTest(unittest.TestCase):
|
||||
def run_cycle(self, client=1, game=1, anonymous=384, complete=1, reclaimed=128, body=None, available=1024, fall=0, tick=0):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
(root / 'meminfo').write_text(
|
||||
f'MemTotal: 7340032 kB\nMemAvailable: {available * 1024} kB\n'
|
||||
'SwapTotal: 6291456 kB\nSwapFree: 4194304 kB\n')
|
||||
(root / 'pressure').write_text('some avg10=0.00 avg60=0.00 avg300=0.00 total=0\n')
|
||||
names = ('bl_memory_read', 'bl_memory_tight', 'bl_memory_critical',
|
||||
'bl_zram_has_room', 'bl_client_can_pageout', 'bl_client_pressure',
|
||||
'bl_client_pressure_result', 'bl_client_pressure_finish',
|
||||
'bl_session_pressure', 'bl_zram_send', 'bl_zram_finish', 'bl_zram_pick')
|
||||
source = '\n'.join(function_source(name) for name in names)
|
||||
source = source.replace('/proc/meminfo', str(root / 'meminfo'))
|
||||
source = source.replace('/proc/pressure/memory', str(root / 'pressure'))
|
||||
source = source.replace('/tmp/.bl-zram', str(root / 'ctl'))
|
||||
monitor = SCRIPT.read_text()
|
||||
start = monitor.index(' _bl_preserved_growth=$_bl_growth', monitor.index('while kill -0 $$'))
|
||||
stop = monitor.index(' if [ "$_bl_game" = 1 ] && bl_game_focused', start)
|
||||
cycle = monitor[start:stop] if body is None else body
|
||||
script = f'''
|
||||
set -u
|
||||
BL_ZRAM_CLIENT={client} _bl_game={game} _bl_pids=(123) _bl_others=(321)
|
||||
_bl_pressure_at=-1 _bl_pressure_backoff=30 _bl_packed=-1
|
||||
_bl_tier_at=-1 _bl_tier_backoff=30 _bl_tier_pushed=0
|
||||
_bl_client_reserve=0 client_rss=393216 client_anon={anonymous * 1024}
|
||||
step() {{ :; }}
|
||||
bl_session_others() {{ _bl_session=(321); }}
|
||||
bl_zram_scan() {{ _bl_pids=(456); }}
|
||||
nap() {{ echo "nap:$*:at:$NOW"; }}
|
||||
bl_zram_usage() {{
|
||||
if [ "$1" = 123 ]; then
|
||||
_bl_rss=$client_rss _bl_anon=$client_anon _bl_swap=0
|
||||
else
|
||||
_bl_rss=3145728 _bl_anon=1572864 _bl_swap=0
|
||||
fi
|
||||
}}
|
||||
kill() {{ echo "signal:$*:at:$NOW"; NOW=$((NOW + {tick})); }}
|
||||
bl_zram_wait() {{
|
||||
echo "wait:$*:at:$NOW"
|
||||
NOW=$((NOW + 12))
|
||||
_bl_pageout_complete={complete}
|
||||
[ "$1" != 123 ] || client_rss=$((client_rss - {reclaimed * 1024}))
|
||||
}}
|
||||
bl_tier_note() {{ :; }}
|
||||
''' + source + f'''
|
||||
bl_memory_read
|
||||
_bl_fall={fall * 1024}
|
||||
''' + cycle + '''
|
||||
echo "client:$_bl_pressure_backoff:${_bl_client_reserve:-0}:${_bl_client_pending:-0}"
|
||||
'''
|
||||
result = run_bash('NOW=0\n' + script.replace('SECONDS', 'NOW'))
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
self.assertEqual('', result.stderr)
|
||||
return result.stdout.splitlines()
|
||||
|
||||
def test_both_requests_are_sent_before_either_wait(self):
|
||||
self.assertEqual(['signal:-URG 123:at:0', 'signal:-URG 321:at:0',
|
||||
'wait:321:at:0', 'wait:123:at:12', 'client:30:0:0'], self.run_cycle())
|
||||
|
||||
def test_disabled_client_is_never_signalled_or_waited_for(self):
|
||||
self.assertEqual(['signal:-URG 321:at:0', 'wait:321:at:0', 'client:30:0:0'],
|
||||
self.run_cycle(client=0))
|
||||
|
||||
def test_no_game_requests_nothing(self):
|
||||
self.assertEqual(['client:30:0:0'], self.run_cycle(game=0))
|
||||
|
||||
def test_a_client_with_no_anonymous_pages_still_finishes_its_request(self):
|
||||
self.assertEqual(['signal:-URG 123:at:0', 'signal:-URG 321:at:0',
|
||||
'wait:321:at:0', 'wait:123:at:12', 'client:30:0:0'],
|
||||
self.run_cycle(anonymous=0))
|
||||
|
||||
def test_falling_headroom_survives_a_tick_between_requests(self):
|
||||
self.assertEqual(['signal:-URG 123:at:0', 'signal:-URG 321:at:1',
|
||||
'wait:321:at:2', 'wait:123:at:14', 'client:30:0:0'],
|
||||
self.run_cycle(available=2304, fall=512, tick=1))
|
||||
|
||||
def test_deferred_ineffective_reclaim_still_doubles_the_backoff(self):
|
||||
self.assertEqual('client:60:0:0', self.run_cycle(reclaimed=0)[-1])
|
||||
|
||||
def test_deferred_incomplete_reclaim_does_not_double_the_backoff(self):
|
||||
self.assertEqual('client:30:0:0', self.run_cycle(reclaimed=0, complete=0)[-1])
|
||||
|
||||
def test_explicit_combined_send_keeps_its_synchronous_wait(self):
|
||||
self.assertEqual(['signal:-URG 321 123:at:0', 'wait:321 123:at:0', 'client:30:0:0'],
|
||||
self.run_cycle(body='bl_zram_send 1 cap game\n'))
|
||||
self.assertEqual(['signal:-URG 321:at:0', 'wait:321:at:0', 'client:30:0:0'],
|
||||
self.run_cycle(client=0, body='bl_zram_send 1 cap game\n'))
|
||||
|
||||
def test_readback_keeps_its_synchronous_delay(self):
|
||||
self.assertEqual(['signal:-URG 123:at:0', 'nap:5:at:0', 'client:30:0:0'],
|
||||
self.run_cycle(body='bl_zram_send 0 readback client\n'))
|
||||
|
||||
def test_usage_sums_anonymous_rss_and_falls_back_per_process(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
for pid, contents in ((123, 'VmRSS: 2000 kB\nRssAnon: 300 kB\nVmSwap: 100 kB\n'),
|
||||
(321, 'VmRSS: 4000 kB\nVmSwap: 200 kB\n'),
|
||||
(654, 'VmRSS: 1000 kB\nRssAnon: 0 kB\n')):
|
||||
(root / str(pid)).mkdir()
|
||||
(root / str(pid) / 'status').write_text(contents)
|
||||
source = function_source('bl_zram_usage').replace('/proc/', str(root) + '/')
|
||||
result = run_bash(source + '\nbl_zram_usage 123 321 654 > /dev/null; echo "$_bl_rss:$_bl_anon:$_bl_swap"')
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
self.assertEqual('7000:4300:300', result.stdout.strip())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,224 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from tools.tests.test_usr2_preload import runner
|
||||
|
||||
HOOK_BUILD = Path(__file__).resolve().parents[1] / "nativeguest/build.sh"
|
||||
|
||||
PROBE = r"""
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
char cwd[4096];
|
||||
for (int i = 1; i < argc; i++) {
|
||||
struct stat st;
|
||||
if (strncmp(argv[i], "cd:", 3) == 0) {
|
||||
int ok = chdir(argv[i] + 3) == 0 && getcwd(cwd, sizeof(cwd)) != NULL;
|
||||
printf("%s\t%s\n", argv[i], ok ? cwd : "-");
|
||||
continue;
|
||||
}
|
||||
if (strncmp(argv[i], "at:", 3) == 0) {
|
||||
int dfd = open(argv[i] + 3, O_RDONLY | O_DIRECTORY);
|
||||
printf("%s\t%d\t%d\n", argv[i], fstatat(AT_FDCWD, argv[i] + 3, &st, 0) == 0, dfd >= 0);
|
||||
if (dfd >= 0) close(dfd);
|
||||
continue;
|
||||
}
|
||||
if (strncmp(argv[i], "ls:", 3) == 0) {
|
||||
DIR *dir = opendir(argv[i] + 3);
|
||||
char names[1024] = "";
|
||||
struct dirent *de;
|
||||
while (dir && (de = readdir(dir)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
if (names[0]) strncat(names, ",", sizeof(names) - strlen(names) - 1);
|
||||
strncat(names, de->d_name, sizeof(names) - strlen(names) - 1);
|
||||
}
|
||||
if (dir) closedir(dir);
|
||||
printf("%s\t%s\n", argv[i], dir ? names : "-");
|
||||
continue;
|
||||
}
|
||||
int fd = open(argv[i], O_RDONLY);
|
||||
char data[64] = "-";
|
||||
if (fd >= 0) {
|
||||
ssize_t n = read(fd, data, sizeof(data) - 1);
|
||||
data[n > 0 ? n : 0] = 0;
|
||||
close(fd);
|
||||
}
|
||||
printf("%s\t%d\t%s\n", argv[i], stat(argv[i], &st) == 0, data);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
GAME = "/mnt/library/Some Game"
|
||||
|
||||
|
||||
class NativeHookMountTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.cc, cls.prefix = runner()
|
||||
if cls.cc is None:
|
||||
raise unittest.SkipTest("no aarch64 compiler and runner")
|
||||
cls.tmp = tempfile.TemporaryDirectory()
|
||||
tmp = Path(cls.tmp.name)
|
||||
cls.hook = tmp / "hook.so"
|
||||
subprocess.run(["bash", str(HOOK_BUILD), str(cls.hook), cls.cc], check=True, capture_output=True)
|
||||
(tmp / "probe.c").write_text(PROBE)
|
||||
subprocess.run([cls.cc, "-O1", "-o", str(tmp / "probe"), str(tmp / "probe.c")], check=True)
|
||||
cls.probe = tmp / "probe"
|
||||
cls.root = tmp / "root"
|
||||
cls.library = tmp / "storage/PC GAMES"
|
||||
(cls.root / "etc").mkdir(parents=True)
|
||||
(cls.root / "etc/hostname").write_text("rootfs")
|
||||
(cls.root / "mnt/library/Some Game").mkdir(parents=True)
|
||||
(cls.root / "mnt/library/Some Game/game.txt").write_text("mount point")
|
||||
(cls.library / "Some Game").mkdir(parents=True)
|
||||
(cls.library / "Some Game/game.txt").write_text("library")
|
||||
for name, target in (("native", str(cls.root)), ("guest", "/")):
|
||||
devices = cls.root / "pfx" / name / "dosdevices"
|
||||
devices.mkdir(parents=True)
|
||||
os.symlink(target, devices / "z:")
|
||||
relinked = cls.root / "pfx/relinked"
|
||||
(relinked / "dosdevices").mkdir(parents=True)
|
||||
(relinked / "drive_c").mkdir()
|
||||
(relinked / "drive_c/c.txt").write_text("drive c")
|
||||
os.symlink(os.path.relpath(cls.root, relinked / "dosdevices"), relinked / "dosdevices/z:")
|
||||
os.symlink("../drive_c", relinked / "dosdevices/c:")
|
||||
copied = cls.root / "pfx/copied/dosdevices"
|
||||
copied.mkdir(parents=True)
|
||||
os.symlink("../" * 12 + "..", copied / "z:")
|
||||
elsewhere = tmp / "elsewhere"
|
||||
(elsewhere / "outside/pfx/dosdevices").mkdir(parents=True)
|
||||
(elsewhere / "mnt/library/Some Game").mkdir(parents=True)
|
||||
(elsewhere / "mnt/library/Some Game/game.txt").write_text("elsewhere")
|
||||
os.symlink("../../..", elsewhere / "outside/pfx/dosdevices/z:")
|
||||
os.symlink(str(elsewhere / "outside/pfx"), cls.root / "pfx/outside")
|
||||
cls.private = tmp / "private"
|
||||
private_pfx = cls.private / "123/pfx/dosdevices"
|
||||
private_pfx.mkdir(parents=True)
|
||||
os.symlink(os.path.relpath("/", "/mnt/private/123/pfx/dosdevices"), private_pfx / "z:")
|
||||
private_copy = cls.private / "456/pfx/dosdevices"
|
||||
private_copy.mkdir(parents=True)
|
||||
os.symlink("../" * 12 + "..", private_copy / "z:")
|
||||
cls.eqlen_src = tmp / "eqsrcdir"
|
||||
cls.eqlen_dst = "/" + "d" * (len(str(cls.eqlen_src)) - 1)
|
||||
eqlen_pfx = cls.eqlen_src / "123/pfx/dosdevices"
|
||||
eqlen_pfx.mkdir(parents=True)
|
||||
os.symlink(os.path.relpath("/", f"{cls.eqlen_dst}/123/pfx/dosdevices"), eqlen_pfx / "z:")
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.tmp.cleanup()
|
||||
|
||||
def run_probe(self, *args):
|
||||
binds = f"{self.library}:/mnt/library|{self.private}:/mnt/private|{self.eqlen_src}:{self.eqlen_dst}"
|
||||
hook_env = {"DDNATV_ROOT": str(self.root), "DDNATV_BINDS": binds, "LD_PRELOAD": str(self.hook)}
|
||||
env = {k: v for k, v in os.environ.items() if k != "LD_PRELOAD"}
|
||||
if self.prefix:
|
||||
cmd = list(self.prefix) + [arg for key, value in hook_env.items() for arg in ("-E", f"{key}={value}")]
|
||||
else:
|
||||
cmd = []
|
||||
env.update(hook_env)
|
||||
out = subprocess.run(cmd + [str(self.probe), *args], env=env, capture_output=True, text=True, timeout=120,
|
||||
cwd=self.tmp.name)
|
||||
self.assertEqual(out.returncode, 0, out.stderr)
|
||||
return {line.split("\t")[0]: line.split("\t")[1:] for line in out.stdout.splitlines()}
|
||||
|
||||
def test_drive_link_stored_as_rootfs_path_reaches_the_mount(self):
|
||||
path = f"/pfx/native/dosdevices/z:{GAME}/game.txt"
|
||||
self.assertEqual(self.run_probe(path)[path], ["1", "library"])
|
||||
|
||||
def test_working_directory_through_a_drive_link_is_the_mount(self):
|
||||
arg = f"cd:/pfx/native/dosdevices/z:{GAME}/"
|
||||
self.assertEqual(self.run_probe(arg)[arg], [GAME])
|
||||
|
||||
def test_rootfs_path_under_a_mount_point_reaches_the_mount(self):
|
||||
path = f"{self.root}{GAME}/game.txt"
|
||||
self.assertEqual(self.run_probe(path)[path], ["1", "library"])
|
||||
|
||||
def test_guest_drive_link_is_unchanged(self):
|
||||
path = f"/pfx/guest/dosdevices/z:{GAME}/game.txt"
|
||||
self.assertEqual(self.run_probe(path)[path], ["1", "library"])
|
||||
|
||||
def test_rootfs_paths_outside_mounts_are_unchanged(self):
|
||||
paths = ["/pfx/native/dosdevices/z:/etc/hostname", f"{self.root}/etc/hostname", "/etc/hostname"]
|
||||
result = self.run_probe(*paths)
|
||||
for path in paths:
|
||||
self.assertEqual(result[path], ["1", "rootfs"], path)
|
||||
|
||||
def test_wine_lookup_steps_reach_the_mount(self):
|
||||
for pfx in ("/pfx/native", "/pfx/guest", "/pfx/relinked", f"{self.root}/pfx/native", f"{self.root}/pfx/guest",
|
||||
f"{self.root}/pfx/relinked"):
|
||||
drive = f"{pfx}/dosdevices/z:"
|
||||
args = [f"at:{drive}{GAME}/", f"at:{drive}{GAME}", f"ls:{drive}/mnt/library", f"ls:{drive}{GAME}"]
|
||||
result = self.run_probe(*args)
|
||||
self.assertEqual(result[args[0]], ["1", "1"], pfx)
|
||||
self.assertEqual(result[args[1]], ["1", "1"], pfx)
|
||||
self.assertEqual(result[args[2]], ["Some Game"], pfx)
|
||||
self.assertEqual(result[args[3]], ["game.txt"], pfx)
|
||||
|
||||
def test_relative_drive_link_reaches_the_mount(self):
|
||||
path = f"/pfx/relinked/dosdevices/z:{GAME}/game.txt"
|
||||
cd = f"cd:/pfx/relinked/dosdevices/z:{GAME}/"
|
||||
result = self.run_probe(path, cd)
|
||||
self.assertEqual(result[path], ["1", "library"])
|
||||
self.assertEqual(result[cd], [GAME])
|
||||
|
||||
def test_relative_links_outside_mounts_are_unchanged(self):
|
||||
paths = ["/pfx/relinked/dosdevices/z:/etc/hostname", "/pfx/relinked/dosdevices/c:/c.txt",
|
||||
f"{self.root}/pfx/relinked/dosdevices/c:/c.txt"]
|
||||
result = self.run_probe(*paths)
|
||||
self.assertEqual(result[paths[0]], ["1", "rootfs"])
|
||||
self.assertEqual(result[paths[1]], ["1", "drive c"])
|
||||
self.assertEqual(result[paths[2]], ["1", "drive c"])
|
||||
|
||||
def test_suffix_after_a_relative_link_is_not_collapsed(self):
|
||||
path = "/pfx/relinked/dosdevices/z:/mnt/library/missing/../Some Game/game.txt"
|
||||
self.assertEqual(self.run_probe(path)[path], ["0", "-"])
|
||||
|
||||
def test_relative_drive_link_climbing_past_the_root_reaches_the_mount(self):
|
||||
path = f"/pfx/copied/dosdevices/z:{GAME}/game.txt"
|
||||
cd = f"cd:/pfx/copied/dosdevices/z:{GAME}/"
|
||||
result = self.run_probe(path, cd)
|
||||
self.assertEqual(result[path], ["1", "library"])
|
||||
self.assertEqual(result[cd], [GAME])
|
||||
|
||||
def test_relative_link_climbing_past_the_root_stays_in_the_rootfs(self):
|
||||
path = "/pfx/copied/dosdevices/z:/etc/hostname"
|
||||
self.assertEqual(self.run_probe(path)[path], ["1", "rootfs"])
|
||||
|
||||
def test_relative_link_is_resolved_from_its_real_directory(self):
|
||||
path = f"/pfx/outside/dosdevices/z:{GAME}/game.txt"
|
||||
self.assertEqual(self.run_probe(path)[path], ["1", "elsewhere"])
|
||||
|
||||
def test_drive_link_in_a_bound_prefix_reaches_the_mount(self):
|
||||
path = f"/mnt/private/123/pfx/dosdevices/z:{GAME}/game.txt"
|
||||
cd = f"cd:/mnt/private/123/pfx/dosdevices/z:{GAME}/"
|
||||
result = self.run_probe(path, cd)
|
||||
self.assertEqual(result[path], ["1", "library"])
|
||||
self.assertEqual(result[cd], [GAME])
|
||||
|
||||
def test_drive_link_copied_into_a_bound_prefix_reaches_the_mount(self):
|
||||
path = f"/mnt/private/456/pfx/dosdevices/z:{GAME}/game.txt"
|
||||
cd = f"cd:/mnt/private/456/pfx/dosdevices/z:{GAME}/"
|
||||
result = self.run_probe(path, cd)
|
||||
self.assertEqual(result[path], ["1", "library"])
|
||||
self.assertEqual(result[cd], [GAME])
|
||||
|
||||
def test_drive_link_in_equal_length_bound_prefix_reaches_the_mount(self):
|
||||
path = f"{self.eqlen_dst}/123/pfx/dosdevices/z:{GAME}/game.txt"
|
||||
cd = f"cd:{self.eqlen_dst}/123/pfx/dosdevices/z:{GAME}/"
|
||||
result = self.run_probe(path, cd)
|
||||
self.assertEqual(result[path], ["1", "library"])
|
||||
self.assertEqual(result[cd], [GAME])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,241 @@
|
||||
from pathlib import Path
|
||||
import runpy
|
||||
import struct
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin'
|
||||
MODULE = runpy.run_path(str(BIN / 'droiddeck-pace-cores'))
|
||||
Frames = MODULE['Frames']
|
||||
Parker = MODULE['Parker']
|
||||
Governor = MODULE['Governor']
|
||||
efficient_cores = MODULE['efficient_cores']
|
||||
paced_interval = MODULE['paced_interval']
|
||||
|
||||
ALL = set(range(8))
|
||||
EFFICIENT = set(range(6))
|
||||
VBLANK = 1e9 / 60
|
||||
CAPACITY = 1024
|
||||
|
||||
|
||||
class FakeCores:
|
||||
|
||||
def __init__(self, tids):
|
||||
self.state = {tid: set(ALL) for tid in tids}
|
||||
|
||||
def get(self, tid):
|
||||
if tid not in self.state:
|
||||
raise ProcessLookupError(tid)
|
||||
return set(self.state[tid])
|
||||
|
||||
def set(self, tid, cpus):
|
||||
if tid not in self.state:
|
||||
raise ProcessLookupError(tid)
|
||||
self.state[tid] = set(cpus)
|
||||
|
||||
|
||||
class FeedWriter:
|
||||
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
self.count = 0
|
||||
self.data = bytearray(128 + CAPACITY * 16)
|
||||
struct.pack_into('<IIII', self.data, 0, 0x46484444, 1, CAPACITY, 0)
|
||||
|
||||
def focus(self, pid, steam=False):
|
||||
struct.pack_into('<I', self.data, 12, pid)
|
||||
struct.pack_into('<IIII', self.data, 28, 1920, 1080, 60000, 1 if steam else 0)
|
||||
self.flush()
|
||||
|
||||
def frames(self, *intervals):
|
||||
for value in intervals:
|
||||
struct.pack_into('<QQ', self.data, 128 + (self.count % CAPACITY) * 16, int(value), 0)
|
||||
self.count += 1
|
||||
struct.pack_into('<Q', self.data, 16, self.count)
|
||||
self.flush()
|
||||
|
||||
def flush(self):
|
||||
self.path.write_bytes(bytes(self.data))
|
||||
|
||||
|
||||
class PacedIntervalTest(unittest.TestCase):
|
||||
def test_a_steady_cap_at_30_fps_or_more_is_paced(self):
|
||||
self.assertAlmostEqual(paced_interval([VBLANK] * 60), VBLANK)
|
||||
self.assertAlmostEqual(paced_interval([1e9 / 30] * 30), 1e9 / 30)
|
||||
jitter = [1e9 / 60 * (1 + 0.03 * (-1) ** i) for i in range(60)]
|
||||
self.assertIsNotNone(paced_interval(jitter))
|
||||
|
||||
def test_under_30_fps_is_not(self):
|
||||
self.assertIsNone(paced_interval([1e9 / 24] * 24))
|
||||
self.assertIsNone(paced_interval([1e9 / 15] * 15))
|
||||
|
||||
def test_a_spread_or_missed_frames_are_not(self):
|
||||
self.assertIsNone(paced_interval([VBLANK] * 50 + [2 * VBLANK] * 10))
|
||||
self.assertIsNone(paced_interval([VBLANK * (0.8 + 0.4 * (i % 5) / 4) for i in range(60)]))
|
||||
self.assertIsNone(paced_interval([]))
|
||||
|
||||
|
||||
class EfficientCoresTest(unittest.TestCase):
|
||||
def test_everything_below_the_top_capacity(self):
|
||||
with tempfile.TemporaryDirectory() as temp:
|
||||
cpu = Path(temp)
|
||||
(cpu / 'online').write_text('0-7\n')
|
||||
for core in ALL:
|
||||
(cpu / f'cpu{core}').mkdir()
|
||||
(cpu / f'cpu{core}/cpu_capacity').write_text('1024\n' if core >= 6 else '792\n')
|
||||
self.assertEqual(efficient_cores(cpu, set(ALL)), EFFICIENT)
|
||||
self.assertEqual(efficient_cores(cpu, {6, 7}), set())
|
||||
|
||||
def test_one_tier_has_none(self):
|
||||
with tempfile.TemporaryDirectory() as temp:
|
||||
cpu = Path(temp)
|
||||
(cpu / 'online').write_text('0-3\n')
|
||||
for core in range(4):
|
||||
(cpu / f'cpu{core}').mkdir()
|
||||
(cpu / f'cpu{core}/cpu_capacity').write_text('1024\n')
|
||||
self.assertEqual(efficient_cores(cpu, set(range(4))), set())
|
||||
|
||||
|
||||
class GovernorTest(unittest.TestCase):
|
||||
PID = 400
|
||||
TIDS = (400, 401, 402)
|
||||
|
||||
def setUp(self):
|
||||
temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(temp.cleanup)
|
||||
root = Path(temp.name)
|
||||
for tid in self.TIDS:
|
||||
(root / f'proc/{self.PID}/task/{tid}').mkdir(parents=True)
|
||||
self.cores = FakeCores(self.TIDS)
|
||||
self.feed = FeedWriter(root / 'frames')
|
||||
self.feed.focus(self.PID)
|
||||
self.now = 0.0
|
||||
self.governor = Governor(Frames(root / 'frames'), Parker(EFFICIENT, root / 'proc', self.cores),
|
||||
clock=lambda: self.now)
|
||||
self.governor.tick()
|
||||
|
||||
def run_for(self, seconds, interval):
|
||||
for _ in range(int(seconds / 0.5)):
|
||||
self.now += 0.5
|
||||
self.feed.frames(*([interval] * int(0.5e9 / interval)))
|
||||
self.governor.tick()
|
||||
|
||||
def test_parks_a_game_that_holds_its_pace(self):
|
||||
self.run_for(4, VBLANK)
|
||||
self.assertEqual(self.governor.parker.pid, self.PID)
|
||||
self.assertTrue(all(cpus == EFFICIENT for cpus in self.cores.state.values()))
|
||||
|
||||
def test_releases_when_frames_fall_behind_and_backs_off(self):
|
||||
self.run_for(4, VBLANK)
|
||||
self.run_for(2, 2 * VBLANK)
|
||||
self.assertIsNone(self.governor.parker.pid)
|
||||
self.assertTrue(all(cpus == ALL for cpus in self.cores.state.values()))
|
||||
self.run_for(10, VBLANK)
|
||||
self.assertIsNone(self.governor.parker.pid)
|
||||
self.run_for(30, VBLANK)
|
||||
self.assertEqual(self.governor.parker.pid, self.PID)
|
||||
|
||||
def test_struggling_game_stays_on_every_core(self):
|
||||
for step in range(20):
|
||||
self.now += 0.5
|
||||
self.feed.frames(*([VBLANK, 2 * VBLANK] * 10))
|
||||
self.governor.tick()
|
||||
self.assertIsNone(self.governor.parker.pid)
|
||||
self.assertTrue(all(cpus == ALL for cpus in self.cores.state.values()))
|
||||
|
||||
def test_steam_in_front_releases_without_backoff(self):
|
||||
self.run_for(4, VBLANK)
|
||||
self.feed.focus(self.PID, steam=True)
|
||||
self.now += 0.5
|
||||
self.governor.tick()
|
||||
self.assertTrue(all(cpus == ALL for cpus in self.cores.state.values()))
|
||||
self.feed.focus(self.PID)
|
||||
self.run_for(4, VBLANK)
|
||||
self.assertEqual(self.governor.parker.pid, self.PID)
|
||||
|
||||
def test_a_mask_the_game_set_itself_is_kept(self):
|
||||
self.run_for(4, VBLANK)
|
||||
self.cores.state[401] = {7}
|
||||
self.run_for(2, 2 * VBLANK)
|
||||
self.assertEqual(self.cores.state[401], {7})
|
||||
self.assertEqual(self.cores.state[400], ALL)
|
||||
|
||||
def idle_for(self, seconds):
|
||||
for _ in range(int(seconds / 0.5)):
|
||||
self.now += 0.5
|
||||
self.governor.tick()
|
||||
|
||||
def test_thread_born_parked_is_restored(self):
|
||||
self.run_for(4, VBLANK)
|
||||
(self.governor.parker.proc / f'{self.PID}/task/403').mkdir()
|
||||
self.cores.state[403] = set(EFFICIENT)
|
||||
self.run_for(1, VBLANK)
|
||||
self.run_for(2, 2 * VBLANK)
|
||||
self.assertEqual(self.cores.state[403], ALL)
|
||||
|
||||
def test_a_thread_already_on_efficient_cores_keeps_its_mask(self):
|
||||
self.cores.state[402] = set(EFFICIENT)
|
||||
self.run_for(4, VBLANK)
|
||||
self.run_for(1, VBLANK)
|
||||
self.run_for(2, 2 * VBLANK)
|
||||
self.assertEqual(self.cores.state[402], EFFICIENT)
|
||||
self.assertEqual(self.cores.state[400], ALL)
|
||||
|
||||
def test_a_child_of_a_game_pinned_thread_keeps_its_mask(self):
|
||||
self.cores.state[402] = set(EFFICIENT)
|
||||
self.run_for(4, VBLANK)
|
||||
(self.governor.parker.proc / f'{self.PID}/task/404').mkdir()
|
||||
self.cores.state[404] = set(EFFICIENT)
|
||||
self.run_for(1, VBLANK)
|
||||
self.run_for(2, 2 * VBLANK)
|
||||
self.assertEqual(self.cores.state[404], EFFICIENT)
|
||||
self.assertEqual(self.cores.state[400], ALL)
|
||||
|
||||
def test_a_child_of_a_game_pinned_thread_that_exited_keeps_its_mask(self):
|
||||
self.cores.state[402] = set(EFFICIENT)
|
||||
self.run_for(4, VBLANK)
|
||||
(self.governor.parker.proc / f'{self.PID}/task/402').rmdir()
|
||||
del self.cores.state[402]
|
||||
(self.governor.parker.proc / f'{self.PID}/task/404').mkdir()
|
||||
self.cores.state[404] = set(EFFICIENT)
|
||||
self.run_for(1, VBLANK)
|
||||
self.run_for(2, 2 * VBLANK)
|
||||
self.assertEqual(self.cores.state[404], EFFICIENT)
|
||||
self.assertEqual(self.cores.state[400], ALL)
|
||||
|
||||
def test_born_parked_thread_gets_every_original_it_could_have_had(self):
|
||||
for tid in self.TIDS:
|
||||
self.cores.state[tid] = EFFICIENT | {6}
|
||||
self.cores.state[402] = EFFICIENT | {7}
|
||||
self.run_for(4, VBLANK)
|
||||
(self.governor.parker.proc / f'{self.PID}/task/404').mkdir()
|
||||
self.cores.state[404] = set(EFFICIENT)
|
||||
self.run_for(1, VBLANK)
|
||||
self.run_for(2, 2 * VBLANK)
|
||||
self.assertEqual(self.cores.state[404], ALL)
|
||||
self.assertEqual(self.cores.state[402], EFFICIENT | {7})
|
||||
|
||||
def test_a_stalled_game_is_not_parked_on_old_frames(self):
|
||||
self.run_for(0.5, VBLANK)
|
||||
self.idle_for(3)
|
||||
self.assertIsNone(self.governor.parker.pid)
|
||||
self.run_for(2, VBLANK)
|
||||
self.assertIsNone(self.governor.parker.pid)
|
||||
|
||||
def test_a_parked_game_that_stops_presenting_is_released(self):
|
||||
self.run_for(4, VBLANK)
|
||||
self.run_for(1, 1e9)
|
||||
self.idle_for(1)
|
||||
self.assertIsNone(self.governor.parker.pid)
|
||||
self.assertTrue(all(cpus == ALL for cpus in self.cores.state.values()))
|
||||
|
||||
def test_threads_pinned_to_fast_cores_are_left_alone(self):
|
||||
self.cores.state[402] = {6, 7}
|
||||
self.run_for(4, VBLANK)
|
||||
self.assertEqual(self.cores.state[402], {6, 7})
|
||||
self.assertEqual(self.cores.state[400], EFFICIENT)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Loaded 100 of 562 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user