From b8e06a16cfc535ae3cec2d769cca9a685985db75 Mon Sep 17 00:00:00 2001 From: ash-iir <254348575+ash-iir@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:32:01 +0000 Subject: [PATCH 01/12] worker: stop restores from wiping data they cannot restore - Encrypted special restore piped the decrypted stream into decomp, which reads its file argument and ignores stdin, so tar got no input after the live data was already removed. Add decomp_in (stdin, codec by filename) and use it there and in archive_safe. - archive_safe only checked member paths, so a listing that failed or was empty (wrong passphrase, unreadable archive) passed and the target was wiped before extraction failed. list_safe now also requires tar -t to succeed and list at least one member; the Neo import check uses it too. - cleanup() removed staging files matching *$OPID*, so op-1 also removed op-10/op-11 files of running workers. Match .$OPID and .$OPID.* only. Signed-off-by: ash-iir <254348575+ash-iir@users.noreply.github.com> (cherry picked from commit 534a2f09d1d129e3c6e4f59e0f6c7207d52279f1) --- module/scripts/worker.sh | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/module/scripts/worker.sh b/module/scripts/worker.sh index cc63ee9..2e9d6c5 100644 --- a/module/scripts/worker.sh +++ b/module/scripts/worker.sh @@ -286,7 +286,7 @@ _release_wakelock() { echo "vdbackup_$OPID" > /sys/power/wake_unlock 2>/dev/null # release into cleanup, and routing the signals into the same trap, fixes it for good. # also reap this op's staging transients (dump/xe/ic/batch/restore/...): a kill anywhere, # even during preparing (before the per-op DEST trap in do_backup arms), leaves no scratch. -cleanup() { _release_wakelock; verifier_restore; rm -f "$LOGS/$OPID.pid"; [ -n "$OPID" ] && rm -rf "$STATE/staging/"*"$OPID"* 2>/dev/null; } +cleanup() { _release_wakelock; verifier_restore; rm -f "$LOGS/$OPID.pid"; [ -n "$OPID" ] && rm -rf "$STATE/staging/"*".$OPID" "$STATE/staging/"*".$OPID."* 2>/dev/null; } trap cleanup EXIT trap 'exit 143' TERM trap 'exit 130' INT @@ -660,12 +660,18 @@ comp_ext() { case "$1" in none) echo tar ;; gzip) echo tar.gz ;; lz4) echo tar.l decomp() { case "$1" in *.tar.gz*) "$GZIP" -dc "$1" ;; *.tar.lz4*) "$ZSTD" -dq --format=lz4 -c "$1" ;; *.tar.zst*) "$ZSTD" -dqc "$1" ;; *.tar*) cat "$1" ;; *) "$ZSTD" -dqc "$1" ;; esac; } +# same, for a stream on stdin (decrypted age output); $1 only picks the codec. +decomp_in() { case "$1" in + *.tar.gz*) "$GZIP" -dc ;; *.tar.lz4*) "$ZSTD" -dq --format=lz4 -c ;; + *.tar.zst*) "$ZSTD" -dqc ;; *) cat ;; esac; } # Anti-path-traversal guard. Restore extracts tar archives AS ROOT, so a tampered # backup/import/special archive must never escape its target dir. scan_members reads # a tar listing on stdin and returns non-zero if any member is an absolute path or -# holds a ".." component; archive_safe lists an archive (decrypting/decompressing by -# encryption flag + filename) and returns 0 only when every member is contained. +# holds a ".." component; list_safe lists a tar stream on stdin and also fails when tar +# fails or lists nothing (wrong passphrase, unreadable or empty archive); archive_safe +# lists an archive (decrypting/decompressing by encryption flag + filename) and returns +# 0 only when it is readable and every member is contained. # Callers run this BEFORE the destructive wipe + extraction and abort on failure. scan_members() { while IFS= read -r _m; do @@ -673,13 +679,15 @@ scan_members() { done return 0 } +list_safe() { + _ls="$(exec_ns "$TAR" -t 2>/dev/null)" || return 1 + [ -n "$_ls" ] && printf '%s\n' "$_ls" | scan_members +} archive_safe() { # ; 0 = safe, 1 = unsafe { case "$2" in - age|age-scrypt) dec_age "$1" | { case "$1" in - *.tar.gz*) "$GZIP" -dc ;; *.tar.lz4*) "$ZSTD" -dq --format=lz4 -c ;; - *.tar.zst*) "$ZSTD" -dqc ;; *) cat ;; esac; } ;; + age|age-scrypt) dec_age "$1" | decomp_in "$1" ;; *) decomp "$1" ;; - esac; } | exec_ns "$TAR" -t 2>/dev/null | scan_members + esac; } | list_safe } # media_own [-R] : create and give it the ownership + SELinux context @@ -1874,7 +1882,7 @@ do_import_neo() { # anti-traversal: reject a tampered archive BEFORE the wipe (decrypted temp has # no suffix, so decompress by $ext, not by filename). if ! { case "$ext" in *.zst) "$ZSTD" -dqc "$src" ;; *.gz) "$GZIP" -dc "$src" ;; *) cat "$src" ;; esac; } \ - | exec_ns "$TAR" -t 2>/dev/null | scan_members; then + | list_safe; then [ "$src" != "$f" ] && rm -f "$src" audit "import-neo unsafe archive $1 $PKG"; finish false ERR_UNSAFE_ARCHIVE; return 1; fi exec_ns rm -rf "$2" 2>/dev/null # faithful: wipe then extract (migration) @@ -2160,7 +2168,7 @@ do_special_restore() { archive_safe "$REF/$arc" "$_senc" || { audit "special-restore unsafe archive $cat:$arc"; finish false ERR_UNSAFE_ARCHIVE; return; } exec_ns rm -rf "$src" 2>/dev/null mkdir -p "$parent" - if [ "$enc" = age-scrypt ]; then dec_age "$REF/$arc" | decomp "$arc" | exec_ns "$TAR" $TARX -x -C "$parent" 2>/dev/null + if [ "$enc" = age-scrypt ]; then dec_age "$REF/$arc" | decomp_in "$arc" | exec_ns "$TAR" $TARX -x -C "$parent" 2>/dev/null else decomp "$REF/$arc" | exec_ns "$TAR" $TARX -x -C "$parent" 2>/dev/null; fi [ $? -eq 0 ] || { audit "special-restore extract failed $cat:$arc"; finish false ERR_RESTORE; return; } exec_ns restorecon -R "$src" 2>/dev/null From 77ee84307a34cd1b2d4df4625e211540109af58b Mon Sep 17 00:00:00 2001 From: ash-iir <254348575+ash-iir@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:51:00 +0000 Subject: [PATCH 02/12] worker: feed the tar listing to scan_members through a here-doc Under mksh printf is an external command, so a large listing passed as one argument hit ARG_MAX, printf failed and scan_members read an empty stdin and returned 0. Read the listing from a here-doc instead, and fail on an empty listing directly. Signed-off-by: ash-iir <254348575+ash-iir@users.noreply.github.com> (cherry picked from commit a818ee67e201218e056b048f50be4bae1c176236) --- module/scripts/worker.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/module/scripts/worker.sh b/module/scripts/worker.sh index 2e9d6c5..4ee7d87 100644 --- a/module/scripts/worker.sh +++ b/module/scripts/worker.sh @@ -681,7 +681,12 @@ scan_members() { } list_safe() { _ls="$(exec_ns "$TAR" -t 2>/dev/null)" || return 1 - [ -n "$_ls" ] && printf '%s\n' "$_ls" | scan_members + [ -n "$_ls" ] || return 1 + # here-doc, not printf: under mksh printf is external, so a large listing hit ARG_MAX, + # printf failed and scan_members read nothing and passed the archive unchecked + scan_members < ; 0 = safe, 1 = unsafe { case "$2" in From c80681696697fc90bc781c517b4bf61a306f2d27 Mon Sep 17 00:00:00 2001 From: ash-iir <254348575+ash-iir@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:33:05 +0000 Subject: [PATCH 03/12] scripts: share one package-name check worker.sh and helper.sh each repeated a slightly different case pattern for package names. Replace them with valid_pkg, which accepts only [A-Za-z0-9._] without a leading dot, the same rule Android uses. Signed-off-by: ash-iir <254348575+ash-iir@users.noreply.github.com> (cherry picked from commit 42fff6b2069e32a5b7c5820de3c9d890f772a49b) --- module/scripts/helper.sh | 15 ++++++++------- module/scripts/worker.sh | 21 ++++++++++++--------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/module/scripts/helper.sh b/module/scripts/helper.sh index ec3518e..0dec751 100644 --- a/module/scripts/helper.sh +++ b/module/scripts/helper.sh @@ -28,6 +28,7 @@ d="${0%/*}"; [ "$d" = "$0" ] && d="."; MODDIR="$(cd "$d/.." 2>/dev/null && pwd || echo "${d%/*}")" STATE=/data/adb/vdbackup +valid_pkg() { case "$1" in ''|null|.*|*[!A-Za-z0-9._]*) return 1 ;; esac; } # same rule as worker.sh bdir() { case "$1" in ''|*[!0-9]*) set -- 0 ;; esac; printf '/data/media/%s/VDBackup' "$1"; } # /sdcard/VDBackup per Android user (credential-encrypted) # A backup sent to a local destination lives at that destination, not duplicated in the internal # store (destination-authoritative, issue #15): the user picks a local dest to keep the backup OFF @@ -255,7 +256,7 @@ auto_backup_scan() { # ; echoes detached | map(select(($s[.key] // null) != .value)) | .[].key' "$_cur" 2>/dev/null)" _n=0 for _pkg in $_changed; do - case "$_pkg" in ''|*..*|*/*|*' '*) continue ;; esac + valid_pkg "$_pkg" || continue "$_jq" -e --arg p "$_pkg" '((.blocklist // []) | index($p)) != null' "$STATE/config.json" >/dev/null 2>&1 && continue _j="$("$_jq" -nc --arg p "$_pkg" --argjson u "$_u" --arg m "$_mode" --arg d "$_dest" \ '{package:$p,user:$u,mode:$m,dest:$d,items:["apk","data","data_de","obb","external","media"],auto:true}')" @@ -505,7 +506,7 @@ case "$verb" in pkg="$(printf '%s' "$j" | "$JQ" -r '.package // empty' 2>/dev/null)" u="$(printf '%s' "$j" | "$JQ" -r '.user // 0' 2>/dev/null)"; case "$u" in ''|*[!0-9]*) u=0 ;; esac vc="$(printf '%s' "$j" | "$JQ" -r '.vc // 0' 2>/dev/null)"; case "$vc" in ''|*[!0-9]*) vc=0 ;; esac - case "$pkg" in ''|*/*|*..*|*' '*) err ERR_ICON "invalid package"; exit 0 ;; esac + valid_pkg "$pkg" || { err ERR_ICON "invalid package"; exit 0; } mkdir -p "$STATE/iconcache" 2>/dev/null # cache key includes versionCode so the icon refreshes when the app updates; # drop any stale entry cached under an older versionCode for this package. @@ -532,7 +533,7 @@ case "$verb" in TAB="$(printf '\t')" lst="$STATE/staging/icons.$$.lst"; : > "$lst" printf '%s' "$j" | "$JQ" -r '.items[]? | "\(.package // "")\t\(.vc // 0)"' 2>/dev/null | while IFS="$TAB" read -r pkg vc; do - case "$pkg" in ''|*/*|*..*|*' '*) continue ;; esac + valid_pkg "$pkg" || continue case "$vc" in ''|*[!0-9]*) vc=0 ;; esac png="$STATE/iconcache/$pkg-$vc.png" if [ ! -s "$png" ]; then @@ -548,7 +549,7 @@ case "$verb" in printf '{"ok":true,"icons":{' first=1 printf '%s' "$j" | "$JQ" -r '.items[]? | "\(.package // "")\t\(.vc // 0)"' 2>/dev/null | while IFS="$TAB" read -r pkg vc; do - case "$pkg" in ''|*/*|*..*|*' '*) continue ;; esac + valid_pkg "$pkg" || continue case "$vc" in ''|*[!0-9]*) vc=0 ;; esac png="$STATE/iconcache/$pkg-$vc.png"; [ -s "$png" ] || continue [ "$first" = 1 ] || printf ','; first=0 @@ -1424,7 +1425,7 @@ EOF j="$(printf '%s' "$ARGS_B64" | base64 -d 2>/dev/null)" pkg="$(printf '%s' "$j" | "$JQ" -r '.package // empty' 2>/dev/null)" priv="$(printf '%s' "$j" | "$JQ" -r '.priv // false' 2>/dev/null)" - case "$pkg" in ''|*..*|*/*|*' '*) err ERR_BAD_PKG "invalid package"; exit 0 ;; esac + valid_pkg "$pkg" || { err ERR_BAD_PKG "invalid package"; exit 0; } sub=app; [ "$priv" = true ] && sub=priv-app base="$(pm path "$pkg" 2>/dev/null | sed -n 's/^package://p' | grep '/base\.apk$' | head -1)" [ -n "$base" ] || base="$(pm path "$pkg" 2>/dev/null | sed -n 's/^package://p' | head -1)" @@ -1481,7 +1482,7 @@ EOF sysmount-del) JQ="$(command -v "$BIN/jq" 2>/dev/null || echo jq)" pkg="$(printf '%s' "$ARGS_B64" | base64 -d 2>/dev/null | "$JQ" -r '.package // empty' 2>/dev/null)" - case "$pkg" in ''|*..*|*/*|*' '*) err ERR_BAD_PKG "invalid package"; exit 0 ;; esac + valid_pkg "$pkg" || { err ERR_BAD_PKG "invalid package"; exit 0; } MM=/data/adb/modules/vdbackup_sysmount for sub in app priv-app; do d="$MM/system/$sub/$pkg"; [ -d "$d" ] || continue @@ -1550,7 +1551,7 @@ EOF pkg="$(printf '%s' "$j" | "$JQ" -r '.package // empty' 2>/dev/null)" u="$(printf '%s' "$j" | "$JQ" -r '.user // 0' 2>/dev/null)"; case "$u" in ''|*[!0-9]*) u=0 ;; esac keep="$(printf '%s' "$j" | "$JQ" -r '.keep // 5' 2>/dev/null)"; case "$keep" in ''|*[!0-9]*) keep=5 ;; esac - case "$pkg" in ''|*..*|*/*|*' '*) err ERR_BAD_REF "invalid package"; exit 0 ;; esac + valid_pkg "$pkg" || { err ERR_BAD_REF "invalid package"; exit 0; } printf '{"ok":true,"pruned":[' first=1 for r in $(prune_pkg "$u" "$pkg" "$keep"); do diff --git a/module/scripts/worker.sh b/module/scripts/worker.sh index 4ee7d87..8b80fe7 100644 --- a/module/scripts/worker.sh +++ b/module/scripts/worker.sh @@ -657,6 +657,9 @@ comp() { case "$1" in zstd:*) "$ZSTD" -q "-${1#zstd:}" ;; *) "$ZSTD" -q -10 ;; esac; } comp_ext() { case "$1" in none) echo tar ;; gzip) echo tar.gz ;; lz4) echo tar.lz4 ;; *) echo tar.zst ;; esac; } # decompress by filename (lz4 via zstd, so DataBackup imports also work). +# package names are [A-Za-z0-9._] and never start with a dot, so "." / ".." or a +# slash can't turn "/data/user/$U/$PKG" or "$BACKUPS/$PKG" into a parent path. +valid_pkg() { case "$1" in ''|null|.*|*[!A-Za-z0-9._]*) return 1 ;; esac; } decomp() { case "$1" in *.tar.gz*) "$GZIP" -dc "$1" ;; *.tar.lz4*) "$ZSTD" -dq --format=lz4 -c "$1" ;; *.tar.zst*) "$ZSTD" -dqc "$1" ;; *.tar*) cat "$1" ;; *) "$ZSTD" -dqc "$1" ;; esac; } @@ -1023,7 +1026,7 @@ do_mirror() { # ---------------------------------------------------------------------- backup() { PKG="$(jget "$ARGS" '.package')" - case "$PKG" in ''|null|*/*|*' '*) finish false ERR_NO_PACKAGE; return ;; esac + valid_pkg "$PKG" || { finish false ERR_NO_PACKAGE; return; } is_root_manager "$PKG" && { audit "refused root manager backup: $PKG"; finish false ERR_ROOT_MANAGER; return; } U="$(jget "$ARGS" '.user // 0')"; case "$U" in ""|*[!0-9]*) U=0 ;; esac BACKUPS="$(backups_dir "$U")" @@ -1425,7 +1428,7 @@ backup() { do_restore_snapshot() { DESTID="$(jget "$ARGS" '.dest')"; SNAP="$(jget "$ARGS" '.snapshot')" PKG="$(jget "$ARGS" '.package')"; U="$(jget "$ARGS" '.user // 0')"; case "$U" in ""|*[!0-9]*) U=0 ;; esac - case "$PKG" in ''|null|*/*|*' '*) finish false ERR_NO_PACKAGE; return ;; esac # same guard as backup()/restore(): a package with a slash feeds rm -rf "/data/user/$U/$PKG" + valid_pkg "$PKG" || { finish false ERR_NO_PACKAGE; return; } # same guard as backup()/restore(): a package with a slash feeds rm -rf "/data/user/$U/$PKG" is_root_manager "$PKG" && { audit "refused root manager restore: $PKG"; finish false ERR_ROOT_MANAGER; return; } { [ -z "$SNAP" ] || [ "$SNAP" = null ]; } && SNAP=latest resolve_dest @@ -1483,7 +1486,7 @@ do_restore_snapshot() { # Restore from a Mirror: pm install the mirrored APKs, rsync the data dirs back, remap. do_restore_mirror() { DESTID="$(jget "$ARGS" '.dest')"; PKG="$(jget "$ARGS" '.package')"; U="$(jget "$ARGS" '.user // 0')"; case "$U" in ""|*[!0-9]*) U=0 ;; esac - case "$PKG" in ''|null|*/*|*' '*) finish false ERR_NO_PACKAGE; return ;; esac # same guard as backup()/restore(): a package with a slash feeds rm -rf / rsync --delete on "/data/user/$U/$PKG" + valid_pkg "$PKG" || { finish false ERR_NO_PACKAGE; return; } # same guard as backup()/restore(): a package with a slash feeds rm -rf / rsync --delete on "/data/user/$U/$PKG" is_root_manager "$PKG" && { audit "refused root manager restore: $PKG"; finish false ERR_ROOT_MANAGER; return; } resolve_dest transport_guard mirror || return @@ -1554,7 +1557,7 @@ restore() { PKG="$("$JQ" -r '.package.name' "$M")"; U="$("$JQ" -r '.user_id // 0' "$M")" is_root_manager "$PKG" && { audit "refused root manager restore: $PKG"; finish false ERR_ROOT_MANAGER; return; } case "$U" in ""|*[!0-9]*) U=0 ;; esac - case "$PKG" in ''|null|*/*|*' '*) finish false ERR_NO_PACKAGE; return ;; esac + valid_pkg "$PKG" || { finish false ERR_NO_PACKAGE; return; } # restore-test: extract every archive into a temp dir under the state dir and report # whether each extracts cleanly and its sha256 matches the manifest. Touches no live @@ -1772,7 +1775,7 @@ do_import() { do_import_databackup() { PKG="$(basename "$(dirname "$REF")")" [ -d "$REF" ] || { finish false ERR_NO_BACKUP; return; } - case "$PKG" in ''|.|null|*/*|*' '*) finish false ERR_NO_PACKAGE; return ;; esac + valid_pkg "$PKG" || { finish false ERR_NO_PACKAGE; return; } prog 5 fetching "$PKG"; audit "import-db start $PKG"; notify restore_start "$PKG" freeze "$PKG" prog 30 installing "$PKG" @@ -1830,7 +1833,7 @@ do_import_databackup() { do_import_neo() { PROP="$REF.properties"; [ -f "$PROP" ] || { finish false ERR_NO_MANIFEST; return; } PKG="$("$JQ" -r '.packageName' "$PROP")" - case "$PKG" in ''|null|*/*|*' '*) finish false ERR_NO_PACKAGE; return ;; esac + valid_pkg "$PKG" || { finish false ERR_NO_PACKAGE; return; } # The real encryption signal is the per-file .enc suffix, NOT metadata cipherType/iv: # recent Neo writes cipherType/iv even when a given backup's archives are left plain. ENC=no; ls "$REF"/*.enc >/dev/null 2>&1 && ENC=yes @@ -2712,7 +2715,7 @@ do_telegram_pull() { # download a backup from Telegram into local storage; it U="$(jget "$ARGS" '.user // 0')"; case "$U" in ""|*[!0-9]*) U=0 ;; esac case "$NAME" in *__*.vdb.tar) : ;; *) finish false ERR_INVALID_REF; return ;; esac _rest="${NAME%.vdb.tar}"; PKG="${_rest%%__*}"; _tsvc="${_rest#*__}" - case "$PKG" in ''|null|*/*|*' '*) finish false ERR_NO_PACKAGE; return ;; esac + valid_pkg "$PKG" || { finish false ERR_NO_PACKAGE; return; } case "$_tsvc" in ''|*/*|*..*|*' '*) finish false ERR_INVALID_REF; return ;; esac # feeds a backups path net_guard || return [ -x "$TGBIN" ] || { finish false ERR_TG_BIN; return; } @@ -2735,7 +2738,7 @@ do_rclone_pull() { # download an rclone remote-only backup into local storage; U="$(jget "$ARGS" '.user // 0')"; case "$U" in ""|*[!0-9]*) U=0 ;; esac case "$_rref" in ''|/*|*..*) finish false ERR_INVALID_REF; return ;; esac PKG="${_rref%%/*}"; _tsvc="${_rref#*/}" - case "$PKG" in ''|null|*/*|*' '*) finish false ERR_NO_PACKAGE; return ;; esac + valid_pkg "$PKG" || { finish false ERR_NO_PACKAGE; return; } case "$_tsvc" in ''|*/*|*' '*) finish false ERR_INVALID_REF; return ;; esac resolve_dest { [ "$DTYPE" = rclone ] && [ -n "$DREMOTE" ]; } || { finish false ERR_INVALID_REF; return; } @@ -2756,7 +2759,7 @@ do_gdrive_pull() { # download a gdrive remote-only backup (its Drive folder) i U="$(jget "$ARGS" '.user // 0')"; case "$U" in ""|*[!0-9]*) U=0 ;; esac case "$_rref" in ''|/*|*..*) finish false ERR_INVALID_REF; return ;; esac PKG="${_rref%%/*}"; _tsvc="${_rref#*/}" - case "$PKG" in ''|null|*/*|*' '*) finish false ERR_NO_PACKAGE; return ;; esac + valid_pkg "$PKG" || { finish false ERR_NO_PACKAGE; return; } case "$_tsvc" in ''|*/*|*' '*) finish false ERR_INVALID_REF; return ;; esac case "$_gfolder" in ''|*[!A-Za-z0-9_-]*) finish false ERR_INVALID_REF; return ;; esac # Drive file-id charset resolve_dest From 1670fc104464332eb1ca46b2940020861598f24b Mon Sep 17 00:00:00 2001 From: ash-iir <254348575+ash-iir@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:35:15 +0000 Subject: [PATCH 04/12] worker: verify special archives before replacing provider data do_special_restore wiped the target before checking the item's sha256, and accepted any /data path from the manifest. Check the sha256 first, as restore() does, and only restore into a path the category owns (special_candidates, split out of special_paths). Signed-off-by: ash-iir <254348575+ash-iir@users.noreply.github.com> (cherry picked from commit 2aa52d7bc945adb60b0f9d6c676a78470048c6b6) --- module/scripts/worker.sh | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/module/scripts/worker.sh b/module/scripts/worker.sh index 8b80fe7..d633fa4 100644 --- a/module/scripts/worker.sh +++ b/module/scripts/worker.sh @@ -1925,7 +1925,7 @@ do_import_neo() { # via $TARC). Restore is same-device/same-ROM faithful. A portable content:// export # sidecar is produced separately by the dex helper. Restore is reboot-required and # gated behind heavy UI friction (it overwrites live system state). -special_paths() { # ; echoes each existing owned path, one per line +special_candidates() { # ; echoes every path the category owns, one per line case "$1" in messages) set -- "/data/user/${U:-0}/com.android.providers.telephony" "/data/user_de/${U:-0}/com.android.providers.telephony" ;; rcs) set -- "/data/user/${U:-0}/com.google.android.apps.messaging" "/data/user_de/${U:-0}/com.google.android.apps.messaging" ;; # RCS lives in Google Messages' own data (bugle_db), not the telephony provider @@ -1936,7 +1936,10 @@ special_paths() { # ; echoes each existing owned path, one per line wallpaper) set -- "/data/system/users/${U:-0}/wallpaper" "/data/system/users/${U:-0}/wallpaper_lock" "/data/system/users/${U:-0}/wallpaper_info.xml" ;; *) return 1 ;; esac - for _p in "$@"; do [ -e "$_p" ] && printf '%s\n' "$_p"; done + printf '%s\n' "$@" +} +special_paths() { # ; echoes each existing owned path, one per line + special_candidates "$1" | while IFS= read -r _p; do [ -e "$_p" ] && printf '%s\n' "$_p"; done } # Portable export sidecar for special backups: read the provider SQLite dbs with the @@ -2169,9 +2172,16 @@ do_special_restore() { { [ -z "$arc" ] || [ -z "$src" ]; } && continue want_cat "$cat" || continue case "$src" in *..*) audit "special-restore skip traversal $src"; continue ;; /data/*) : ;; *) audit "special-restore skip unsafe path $src"; continue ;; esac + # the manifest sits on /sdcard: only restore into a path this category owns + special_candidates "$cat" | grep -qxF "$src" || { audit "special-restore skip foreign path $cat:$src"; continue; } parent="$(dirname "$src")"; base="$(basename "$src")" prog $(( 10 + i * 80 / (n>0?n:1) )) extracting "$cat" - # anti-traversal: reject a tampered archive BEFORE the destructive wipe + # integrity + anti-traversal BEFORE the destructive wipe (same order as restore()) + want="$("$JQ" -r ".items[$((i-1))].sha256 // empty" "$M")" + if [ -n "$want" ]; then + got="$(sha256sum "$REF/$arc" 2>/dev/null | cut -d' ' -f1)" + [ "$want" = "$got" ] || { audit "special-restore sha mismatch $cat:$arc"; finish false ERR_VERIFY; return; } + fi _senc=none; [ "$enc" = age-scrypt ] && _senc=age archive_safe "$REF/$arc" "$_senc" || { audit "special-restore unsafe archive $cat:$arc"; finish false ERR_UNSAFE_ARCHIVE; return; } exec_ns rm -rf "$src" 2>/dev/null From 1cfe828501feae7b1d97ccdbe56cf2fda293026e Mon Sep 17 00:00:00 2001 From: ash-iir <254348575+ash-iir@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:35:47 +0000 Subject: [PATCH 05/12] worker: keep remote-only staging root-only The staging dir was created with the inherited umask and media_own -R then handed the finished backup to media_rw, so it was not root-only while it waited for upload. Create the staging root 0700 and skip media_own for remote-only backups, which are never listed under /sdcard. Signed-off-by: ash-iir <254348575+ash-iir@users.noreply.github.com> (cherry picked from commit e3ec8d61d12661a0d55dce6928225a95ee0e8db7) --- module/scripts/worker.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/module/scripts/worker.sh b/module/scripts/worker.sh index d633fa4..ab702c2 100644 --- a/module/scripts/worker.sh +++ b/module/scripts/worker.sh @@ -945,7 +945,8 @@ transfer_full() { [ "$_lfree" -gt 0 ] && [ "${_lsz:-0}" -gt 0 ] && [ "$_lfree" -lt $(( _lsz + 10485760 )) ] \ && { audit "no space at dest $DPATH $PKG"; finish false ERR_NO_SPACE; return 1; } mkdir -p "$(remote_pfx)/$PKG" && cp -a "$DEST" "$(remote_pfx)/$PKG/" \ - || { audit "local transfer failed $PKG"; finish false ERR_TRANSFER; return 1; }; fi ;; + || { audit "local transfer failed $PKG"; finish false ERR_TRANSFER; return 1; } + media_own -R "$(remote_pfx)/$PKG/$(basename "$DEST")"; fi ;; # staging copy is root-only esac } @@ -1152,7 +1153,8 @@ backup() { # root-only staging dir and cleaned on exit (stealth); otherwise it builds in BACKUPS, where it # lives and is listed. if remote_only; then - DEST="$(staging_root)/$PKG/${TS}_${VC}" + _sroot="$(staging_root)"; mkdir -p "$_sroot" && chmod 700 "$_sroot" # root-only: plaintext data waits here for upload + DEST="$_sroot/$PKG/${TS}_${VC}" else DEST="$BACKUPS/$PKG/${TS}_${VC}"; media_own "$BACKUPS"; media_own "$BACKUPS/$PKG" fi @@ -1381,7 +1383,7 @@ backup() { items:$items}' > "$DEST/manifest.json" rm -f "$IFILE" sha256sum "$DEST/manifest.json" | cut -d' ' -f1 > "$DEST/manifest.json.sha256" - media_own -R "$DEST" # make the finished backup visible under /sdcard/VDBackup + remote_only || media_own -R "$DEST" # make the finished backup visible under /sdcard/VDBackup (staging stays root-only) rm -f "$DUMPF" From f2b61a32c42f392ea5cac78b2d1e24eed25c4bc4 Mon Sep 17 00:00:00 2001 From: ash-iir <254348575+ash-iir@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:39:48 +0000 Subject: [PATCH 06/12] worker: freeze only app uids, in the right user freeze() killed every process of the uid that owns /data/data/. For a system app sharing uid 1000 that includes system_server (soft reboot), and /data/data is always user 0, so a secondary-user op stopped the user-0 instance. Read /data/user/$U and fall back to pidof below 10000. Signed-off-by: ash-iir <254348575+ash-iir@users.noreply.github.com> (cherry picked from commit 0d143f88aa7e76e1a96c2bee8acbd563075a3b37) --- module/scripts/worker.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/module/scripts/worker.sh b/module/scripts/worker.sh index ab702c2..ba971af 100644 --- a/module/scripts/worker.sh +++ b/module/scripts/worker.sh @@ -406,10 +406,11 @@ freeze() { # only, but the wall merely moved to ~120 - the AMS call itself is the problem. kill makes # ZERO AMS calls, so it scales to any number of apps. Kill every process of the app's uid # (the main process AND its :sub-processes, which a `pidof` comm match alone would miss). - _fu="$(stat -c %u "/data/data/$1" 2>/dev/null)" - case "$_fu" in - ''|*[!0-9]*|0) kill -9 $(pidof "$1" 2>/dev/null) 2>/dev/null || true; return 0 ;; - esac + # Only for an app uid: a system app can share uid 1000 with system_server, and killing + # that uid soft-reboots the phone, so those fall back to the pidof match. + _fu="$(stat -c %u "/data/user/${U:-0}/$1" 2>/dev/null)" + case "$_fu" in ''|*[!0-9]*) _fu=0 ;; esac + if [ $(( _fu % 100000 )) -lt 10000 ]; then kill -9 $(pidof "$1" 2>/dev/null) 2>/dev/null || true; return 0; fi for _fd in /proc/[0-9]*; do [ "$(stat -c %u "$_fd" 2>/dev/null)" = "$_fu" ] && kill -9 "${_fd##*/}" 2>/dev/null done From aa6dee79ebb5c5aff8c29e4658e0d95fe64df63d Mon Sep 17 00:00:00 2001 From: ash-iir <254348575+ash-iir@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:46:47 +0000 Subject: [PATCH 07/12] worker: refuse path restores onto whole storage roots do_path_restore wipes its target before extracting. Refuse targets that would take a whole user or storage root with it (/sdcard, /data/media/N, /data/user/N, /data/) and /data/adb. Signed-off-by: ash-iir <254348575+ash-iir@users.noreply.github.com> (cherry picked from commit b72a4a37f89bfa06dc68cc172860b32a95a32f76) --- module/scripts/worker.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/module/scripts/worker.sh b/module/scripts/worker.sh index ba971af..280ca5f 100644 --- a/module/scripts/worker.sh +++ b/module/scripts/worker.sh @@ -2309,6 +2309,18 @@ do_path_backup() { # Restore a standalone path backup: verify, anti-traversal guard, wipe the original # path and extract the archive back into place, restoring ownership/context. +# The restore target comes from a manifest on /sdcard and is wiped before extraction: +# refuse a whole user/storage root (or /data/x itself) and the root manager's /data/adb. +path_restore_ok() { # ; 0 = may be replaced + _p="${1%/}" + case "$_p" in + /data/adb|/data/adb/*) return 1 ;; + /data/user/*/*|/data/user_de/*/*|/data/media/*/*|/storage/emulated/*/*|/sdcard/?*) return 0 ;; + /data/user/*|/data/user_de/*|/data/media/*|/storage/emulated/*) return 1 ;; + /data/*/*) return 0 ;; + esac + return 1 +} do_path_restore() { REF="$(jget "$ARGS" '.ref')" case "$REF" in /data/media/*/VDBackup/.paths/*) : ;; *) finish false ERR_BAD_REF; return ;; esac @@ -2327,6 +2339,7 @@ do_path_restore() { uid="$("$JQ" -r '.owner_uid // 0' "$M")"; gid="$("$JQ" -r '.owner_gid // 0' "$M")" case "$SRCP" in /storage/emulated/*|/data/media/*|/sdcard/*|/data/*) : ;; *) finish false ERR_BAD_PATH; return ;; esac case "$SRCP" in *..*) finish false ERR_BAD_PATH; return ;; esac + path_restore_ok "$SRCP" || { audit "path-restore refused $SRCP"; finish false ERR_BAD_PATH; return; } { [ -n "$arc" ] && [ "$arc" != null ]; } || { finish false ERR_NO_BACKUP; return; } if [ "$enc" = age ] && [ -z "$PW" ]; then finish false ERR_CRYPTO_PASS; return; fi if [ "$enc" = age ] && ! crypto_available; then finish false ERR_CRYPTO_UNAVAIL; return; fi From 99ba9e5cff45bdcb70088f4ec2153de42ee6ea7e Mon Sep 17 00:00:00 2001 From: ash-iir <254348575+ash-iir@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:56:59 +0000 Subject: [PATCH 08/12] worker: harden archive member validation on restore App archives are extracted into a shared parent directory (the user's data dir, Android/data, the profiles dir). Validate that every member of such an archive stays under the app's own top-level entry before it is extracted: for data, data_de, external, obb and media archives, ref/ and cur// for ART profiles. The same check is used by restore-test and the DataBackup import. Archives without a base keep the existing absolute-path and '..' check. Signed-off-by: ash-iir <254348575+ash-iir@users.noreply.github.com> (cherry picked from commit 24aa5b979f223081923ce45b390fa3884b1141c4) --- module/scripts/worker.sh | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/module/scripts/worker.sh b/module/scripts/worker.sh index 280ca5f..d9a4e24 100644 --- a/module/scripts/worker.sh +++ b/module/scripts/worker.sh @@ -677,26 +677,36 @@ decomp_in() { case "$1" in # lists an archive (decrypting/decompressing by encryption flag + filename) and returns # 0 only when it is readable and every member is contained. # Callers run this BEFORE the destructive wipe + extraction and abort on failure. -scan_members() { +# Optional args: an archive extracted into a shared parent (/data/user/, +# Android/data, /data/misc/profiles) must also keep every member inside one of them +# (, /..., optionally ./-prefixed), so it can only touch its own app's dir. +scan_members() { # [...] while IFS= read -r _m; do case "$_m" in /*|..|../*|*/../*|*/..) return 1 ;; esac + [ $# -eq 0 ] && continue + _ok=1 + for _b in "$@"; do + case "${_m#./}" in "$_b"|"$_b"/*) _ok=0; break ;; esac + done + [ "$_ok" -eq 0 ] || return 1 done return 0 } -list_safe() { +list_safe() { # [...]; tar stream on stdin _ls="$(exec_ns "$TAR" -t 2>/dev/null)" || return 1 [ -n "$_ls" ] || return 1 # here-doc, not printf: under mksh printf is external, so a large listing hit ARG_MAX, # printf failed and scan_members read nothing and passed the archive unchecked - scan_members < ; 0 = safe, 1 = unsafe - { case "$2" in - age|age-scrypt) dec_age "$1" | decomp_in "$1" ;; - *) decomp "$1" ;; - esac; } | list_safe +archive_safe() { # [...]; 0 = safe, 1 = unsafe + _asf="$1"; _ase="$2"; shift 2 + { case "$_ase" in + age|age-scrypt) dec_age "$_asf" | decomp_in "$_asf" ;; + *) decomp "$_asf" ;; + esac; } | list_safe "$@" } # media_own [-R] : create and give it the ownership + SELinux context @@ -1573,6 +1583,7 @@ restore() { while [ "$i" -lt "${n:-0}" ]; do arc="$("$JQ" -r ".items[$i].archive // empty" "$M")" want="$("$JQ" -r ".items[$i].sha256 // empty" "$M")" + ttyp="$("$JQ" -r ".items[$i].type // empty" "$M")" i=$((i+1)) [ -n "$arc" ] || continue TTOTAL=$((TTOTAL+1)); af="$REF/$arc" @@ -1583,7 +1594,9 @@ restore() { fi sub="$TDIR/item$i"; mkdir -p "$sub" _tenc=none; echo "$arc" | grep -q '\.age$' && _tenc=age - archive_safe "$af" "$_tenc" || { TFAIL=$((TFAIL+1)); TERR="$TERR unsafe:$arc"; continue; } + if [ "$ttyp" = profiles ]; then archive_safe "$af" "$_tenc" "ref/$PKG" "cur/$U/$PKG" + else archive_safe "$af" "$_tenc" "$PKG"; fi \ + || { TFAIL=$((TFAIL+1)); TERR="$TERR unsafe:$arc"; continue; } if echo "$arc" | grep -q '\.age$'; then if [ -z "$PW" ]; then TFAIL=$((TFAIL+1)); TERR="$TERR nopass:$arc"; continue; fi dec_age "$af" | { case "$arc" in @@ -1694,7 +1707,9 @@ restore() { # anti-traversal: reject a tampered archive BEFORE the destructive wipe, so a # crafted member can never escape into the filesystem and no live data is lost. _renc=none; echo "$arc" | grep -q '\.age$' && _renc=age - archive_safe "$af" "$_renc" || { audit "restore unsafe archive $arc"; finish false ERR_UNSAFE_ARCHIVE; return; } + if [ "$typ" = profiles ]; then archive_safe "$af" "$_renc" "ref/$PKG" "cur/$U/$PKG" + else archive_safe "$af" "$_renc" "$PKG"; fi \ + || { audit "restore unsafe archive $arc"; finish false ERR_UNSAFE_ARCHIVE; return; } # faithful restore: reproduce the backed-up state exactly (wipe then extract), # so files created after the backup do not linger. Extraction is checked; a # failure after the wipe is reported (never a silent success). @@ -1801,7 +1816,7 @@ do_import_databackup() { prog 60 extracting "$PKG" db_item() { # f="$REF/$1"; [ -f "$f" ] || return 0 - archive_safe "$f" none || { audit "import-db unsafe archive $1 $PKG"; finish false ERR_UNSAFE_ARCHIVE; return 1; } + archive_safe "$f" none "$PKG" || { audit "import-db unsafe archive $1 $PKG"; finish false ERR_UNSAFE_ARCHIVE; return 1; } exec_ns rm -rf "$3" 2>/dev/null # faithful: wipe then extract (migration) mkdir -p "$2"; decomp "$f" | exec_ns "$TAR" $TARX -x -C "$2" 2>/dev/null [ $? -eq 0 ] || { audit "import-db extract failed $1 $PKG"; finish false ERR_RESTORE; return 1; } From 3134f18061bb7e75de50d7102222ff640403d2ba Mon Sep 17 00:00:00 2001 From: ash-iir <254348575+ash-iir@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:03:00 +0000 Subject: [PATCH 09/12] worker, helper: keep operation arguments off process argv detach() now hands the worker its arguments in a 0600 file under the root-only state dir instead of argv; the worker reads it once and removes it. jget and the age passphrase feed use here-docs instead of printf, which is not a builtin in mksh and would otherwise run as an external command with the data in its arguments. Signed-off-by: ash-iir <254348575+ash-iir@users.noreply.github.com> (cherry picked from commit a32aabc15db904d13c1b083b46b734626572f79c) --- module/scripts/helper.sh | 10 +++++++++- module/scripts/worker.sh | 23 ++++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/module/scripts/helper.sh b/module/scripts/helper.sh index 0dec751..7a7d5e6 100644 --- a/module/scripts/helper.sh +++ b/module/scripts/helper.sh @@ -133,7 +133,15 @@ detach() { # detach # seed a concrete progress state so the UI shows "starting" instantly instead of # an empty file (the worker overwrites it with the first real phase right away). printf '{"opid":"%s","percent":0,"phase":"starting","item":"","done":false}\n' "$opid" > "$LOGS/progress-$opid.json" - setsid nohup "$MODDIR/scripts/worker.sh" "$wverb" "$opid" "$wargs" \ + # the args (passphrases included) reach the worker in a root-only 0600 file, never on its + # argv, which other uids can read through /proc for the whole operation. A here-doc feeds + # cat on stdin (mksh has no printf builtin, so printf would put them on argv as well). + mkdir -p "$STATE/staging" 2>/dev/null + ( umask 077; cat > "$STATE/staging/args.$opid" <"$LOGS/$opid.out" 2>&1 "$LOGS/$opid.pid" } diff --git a/module/scripts/worker.sh b/module/scripts/worker.sh index d9a4e24..63d22b5 100644 --- a/module/scripts/worker.sh +++ b/module/scripts/worker.sh @@ -265,9 +265,10 @@ else TARC="-p"; fi if echo "$TARHELP" | grep -q -- '--no-absolute-names'; then NAN="--no-absolute-names"; else NAN=""; fi TARX="$TARC $NAN" -VERB="$1"; OPID="$2"; ARGS_B64="$3" +VERB="$1"; OPID="$2" PROG="$LOGS/progress-$OPID.json" -ARGS="$(printf '%s' "$ARGS_B64" | base64 -d 2>/dev/null)" +# args arrive in the 0600 file detach() wrote (never on argv); read once, then remove it. +ARGS="$(base64 -d < "$STATE/staging/args.$OPID" 2>/dev/null)"; rm -f "$STATE/staging/args.$OPID" # Survive Android's low-memory killer. A bulk backup backgrounds the root manager and the # user opens other apps (camera, etc.); without this the LMK kills this detached worker @@ -342,7 +343,12 @@ notify() { [ -f "$MODDIR/etc/vdbackup_helper.dex" ] && LD_LIBRARY_PATH= app_process -Djava.class.path="$MODDIR/etc/vdbackup_helper.dex" \ /system/bin ru.vd171.vdbackup.Helper notify "VDBackup" "$_nm" >/dev/null 2>&1; } -jget() { printf '%s' "$1" | "$JQ" -r "$2" 2>/dev/null; } +# here-doc, not printf: under mksh printf is external and would put the JSON (passphrases +# included) on its argv. +jget() { "$JQ" -r "$2" 2>/dev/null </dev/null; } # active network transport: wifi | mobile | other | unknown. Layered detection so @@ -627,12 +633,19 @@ enc_age() { # ; reads plaintext on stdin, writes an age scrypt file _ep="$STATE/staging/enc.$OPID.$$"; cat > "$_ep" # paths go through the environment, never spliced into the -c string: a path with a # double-quote would otherwise break out and run as root inside `script`. - printf '%s\n%s\n' "$PW" "$PW" | AGE_BIN="$AGE" AGE_IN="$_ep" AGE_OUT="$1" "$SCRIPT" -qec 'exec "$AGE_BIN" -p -o "$AGE_OUT" "$AGE_IN"' /dev/null >/dev/null 2>&1 + # the passphrase reaches age's pty on stdin from a here-doc: printf is external under + # mksh and would put it on argv. + AGE_BIN="$AGE" AGE_IN="$_ep" AGE_OUT="$1" "$SCRIPT" -qec 'exec "$AGE_BIN" -p -o "$AGE_OUT" "$AGE_IN"' /dev/null >/dev/null 2>&1 < ; writes the decrypted plaintext of an age scrypt file to stdout _dp="$STATE/staging/dec.$OPID.$$" - printf '%s\n' "$PW" | AGE_BIN="$AGE" AGE_IN="$1" AGE_OUT="$_dp" "$SCRIPT" -qec 'exec "$AGE_BIN" -d -o "$AGE_OUT" "$AGE_IN"' /dev/null >/dev/null 2>&1 + AGE_BIN="$AGE" AGE_IN="$1" AGE_OUT="$_dp" "$SCRIPT" -qec 'exec "$AGE_BIN" -d -o "$AGE_OUT" "$AGE_IN"' /dev/null >/dev/null 2>&1 </dev/null; rm -f "$_dp" } # Passphrase encryption needs the bundled `age` AND a pty provider (`script`): age reads From bc504f2b96d367f3b18dbe32d722c32ffff94c6a Mon Sep 17 00:00:00 2001 From: ash-iir <254348575+ash-iir@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:07:05 +0000 Subject: [PATCH 10/12] worker: report a wrong passphrase before an encrypted restore Add age_pass_ok, which decrypts a short prefix of an age file (one scrypt KDF, no full decrypt) to tell whether the passphrase opens it. App, special and path restores run it before the archive check and the wipe, so a wrong passphrase ends with ERR_CRYPTO_PASS and the WebUI asks for the passphrase again. restore-test reports it as pass:. The adb restore probe uses the same helper instead of a full decrypt. Signed-off-by: ash-iir <254348575+ash-iir@users.noreply.github.com> (cherry picked from commit a8f6f1a2491a4788c7812278a997d2ceb3a20445) --- module/scripts/worker.sh | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/module/scripts/worker.sh b/module/scripts/worker.sh index 63d22b5..2e63610 100644 --- a/module/scripts/worker.sh +++ b/module/scripts/worker.sh @@ -648,6 +648,14 @@ $PW EOF cat "$_dp" 2>/dev/null; rm -f "$_dp" } +# age_pass_ok : 0 when $PW opens this age file. age checks the passphrase on the +# header and writes plaintext as it goes, so a short prefix is enough: one scrypt KDF, no +# full decrypt. Run before archive_safe so a wrong passphrase reports ERR_CRYPTO_PASS. +age_pass_ok() { + _pp="$STATE/staging/probe.$OPID.$$"; head -c 262144 "$1" > "$_pp" 2>/dev/null + _pn="$(dec_age "$_pp" 2>/dev/null | head -c 1 | wc -c)"; rm -f "$_pp" + [ "${_pn:-0}" -ge 1 ] +} # Passphrase encryption needs the bundled `age` AND a pty provider (`script`): age reads # the passphrase from a controlling terminal, so enc_age/dec_age run it under `script`. An # install whose bundled binaries are incomplete (e.g. a stale/partial module missing @@ -1607,6 +1615,7 @@ restore() { fi sub="$TDIR/item$i"; mkdir -p "$sub" _tenc=none; echo "$arc" | grep -q '\.age$' && _tenc=age + if [ "$_tenc" = age ] && [ -n "$PW" ] && ! age_pass_ok "$af"; then TFAIL=$((TFAIL+1)); TERR="$TERR pass:$arc"; continue; fi if [ "$ttyp" = profiles ]; then archive_safe "$af" "$_tenc" "ref/$PKG" "cur/$U/$PKG" else archive_safe "$af" "$_tenc" "$PKG"; fi \ || { TFAIL=$((TFAIL+1)); TERR="$TERR unsafe:$arc"; continue; } @@ -1720,6 +1729,8 @@ restore() { # anti-traversal: reject a tampered archive BEFORE the destructive wipe, so a # crafted member can never escape into the filesystem and no live data is lost. _renc=none; echo "$arc" | grep -q '\.age$' && _renc=age + if [ "$_renc" = age ] && ! age_pass_ok "$af"; then + audit "restore wrong passphrase $arc"; finish false ERR_CRYPTO_PASS; return; fi if [ "$typ" = profiles ]; then archive_safe "$af" "$_renc" "ref/$PKG" "cur/$U/$PKG" else archive_safe "$af" "$_renc" "$PKG"; fi \ || { audit "restore unsafe archive $arc"; finish false ERR_UNSAFE_ARCHIVE; return; } @@ -2214,6 +2225,8 @@ do_special_restore() { [ "$want" = "$got" ] || { audit "special-restore sha mismatch $cat:$arc"; finish false ERR_VERIFY; return; } fi _senc=none; [ "$enc" = age-scrypt ] && _senc=age + if [ "$_senc" = age ] && ! age_pass_ok "$REF/$arc"; then + audit "special-restore wrong passphrase $cat:$arc"; finish false ERR_CRYPTO_PASS; return; fi archive_safe "$REF/$arc" "$_senc" || { audit "special-restore unsafe archive $cat:$arc"; finish false ERR_UNSAFE_ARCHIVE; return; } exec_ns rm -rf "$src" 2>/dev/null mkdir -p "$parent" @@ -2374,6 +2387,8 @@ do_path_restore() { prog 5 preparing "path"; audit "path-restore start $REF -> $SRCP"; notify restore_start "path" # anti-traversal: reject a tampered archive BEFORE the destructive wipe _renc=none; [ "$enc" = age ] && _renc=age + if [ "$_renc" = age ] && ! age_pass_ok "$REF/$arc"; then + audit "path-restore wrong passphrase"; finish false ERR_CRYPTO_PASS; return; fi archive_safe "$REF/$arc" "$_renc" || { audit "path-restore unsafe archive $arc"; finish false ERR_UNSAFE_ARCHIVE; return; } # integrity before the destructive restore want="$("$JQ" -r '.sha256 // empty' "$M")" @@ -2505,8 +2520,8 @@ do_adb_restore() { if [ "$enc" = age ] && ! crypto_available; then finish false ERR_CRYPTO_UNAVAIL; return; fi prog 5 preparing "adb"; audit "adb-restore start $REF"; notify restore_start "adb" # a wrong passphrase otherwise surfaces late as ERR_RESTORE (empty decrypt -> empty extract); probe - # once here so the user gets a clear ERR_CRYPTO_PASS. One scrypt KDF, cheap next to a real restore. - if [ "$enc" = age ] && [ "$(dec_age "$REF/$arc" 2>/dev/null | head -c 1 | wc -c)" -lt 1 ]; then + # once here so the user gets a clear ERR_CRYPTO_PASS. + if [ "$enc" = age ] && ! age_pass_ok "$REF/$arc"; then audit "adb-restore: wrong passphrase"; finish false ERR_CRYPTO_PASS; return; fi _renc=none; [ "$enc" = age ] && _renc=age archive_safe "$REF/$arc" "$_renc" || { audit "adb-restore unsafe archive"; finish false ERR_UNSAFE_ARCHIVE; return; } # anti-traversal BEFORE writing From 08a85a1d47030733ec7913a18303314c496ba3d8 Mon Sep 17 00:00:00 2001 From: ash-iir <254348575+ash-iir@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:20:51 +0000 Subject: [PATCH 11/12] worker: fail snapshot, mirror and import restores when the APK install fails Only restore() checked that the package was installed after the install session. Snapshot and Mirror restores and the Neo-Backup and DataBackup imports ignored a failed install, then restored the data and reported success. - New install_ok: the same pm path --user check as restore(), with ERR_INSTALL on failure; used after each of the four install steps. - Create the install session only when there are APKs to write. - Neo import: a decrypt failure (APK or data archive) now ends with ERR_IMPORT_ENCRYPTED, so the WebUI asks for the password again, instead of skipping the item and reporting success. A session opened for the APKs is abandoned. Signed-off-by: ash-iir <254348575+ash-iir@users.noreply.github.com> (cherry picked from commit e12faabb1678215ed62cd88d5a8c3147b9fde2d1) --- module/scripts/worker.sh | 52 +++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/module/scripts/worker.sh b/module/scripts/worker.sh index 2e63610..eab8cde 100644 --- a/module/scripts/worker.sh +++ b/module/scripts/worker.sh @@ -509,6 +509,13 @@ verifier_restore() { # wait for the freshly installed app's data dir to exist before chown (it may lag # a moment after install-commit; without this the chown is silently skipped). wait_datadir() { _w=0; while [ "$_w" -lt 25 ]; do [ -d "$1" ] && return 0; exec_ns sleep 0.2 2>/dev/null; _w=$((_w+1)); done; } +# install_ok : after an install step that had APKs, the package must be installed for $U, +# as restore() checks (issue #7); otherwise fail with ERR_INSTALL instead of restoring data +# into a package that is not there. +install_ok() { + fw pm path --user "$U" "$PKG" >/dev/null 2>&1 && return 0 + audit "$1: APK install failed for $PKG"; finish false ERR_INSTALL; return 1 +} # Re-map ownership + SELinux label of a restored tree onto the freshly installed app # (issue #8). Uses LIVEU/LIVEG/LIVECTX captured from the fresh install by the caller. @@ -1485,12 +1492,15 @@ do_restore_snapshot() { # install APKs found in the restored tree prog 55 installing "$PKG" verifier_off - SESSION="$(fw pm install-create $(pm_install_flags) $(installer_flag "") --user "$U" 2>/dev/null | sed -n 's/.*\[\([0-9]*\)\].*/\1/p')" - if [ -n "$SESSION" ]; then - find "$ST/data/app" -name '*.apk' 2>/dev/null | while IFS= read -r apk; do - fw pm install-write "$SESSION" "$(basename "$apk" .apk)" "$apk" >/dev/null 2>&1 - done - fw pm install-commit "$SESSION" >/dev/null 2>&1 + if [ -n "$(find "$ST/data/app" -name '*.apk' 2>/dev/null | head -n 1)" ]; then + SESSION="$(fw pm install-create $(pm_install_flags) $(installer_flag "") --user "$U" 2>/dev/null | sed -n 's/.*\[\([0-9]*\)\].*/\1/p')" + if [ -n "$SESSION" ]; then + find "$ST/data/app" -name '*.apk' 2>/dev/null | while IFS= read -r apk; do + fw pm install-write "$SESSION" "$(basename "$apk" .apk)" "$apk" >/dev/null 2>&1 + done + fw pm install-commit "$SESSION" >/dev/null 2>&1 + fi + install_ok restore-snapshot || { rm -rf "$ST"; return; } fi wait_datadir "/data/user/$U/$PKG" LIVEU="$(stat -c %u "/data/user/$U/$PKG" 2>/dev/null || echo 0)"; LIVEG="$(stat -c %g "/data/user/$U/$PKG" 2>/dev/null || echo 0)" @@ -1536,13 +1546,16 @@ do_restore_mirror() { freeze "$PKG" prog 35 installing "$PKG" verifier_off - SESSION="$(fw pm install-create $(pm_install_flags) $(installer_flag "") --user "$U" 2>/dev/null | sed -n 's/.*\[\([0-9]*\)\].*/\1/p')" - if [ -n "$SESSION" ] && [ -d "$SRC/apk" ]; then - TMP="$STATE/staging/mapk-r.$OPID"; rm -rf "$TMP"; mkdir -p "$TMP" - for af in "$SRC"/apk/*.zst; do [ -e "$af" ] || continue - bn="$(basename "$af" .zst)"; "$ZSTD" -dqc "$af" > "$TMP/$bn" - fw pm install-write "$SESSION" "${bn%.apk}" "$TMP/$bn" >/dev/null 2>&1; done - fw pm install-commit "$SESSION" >/dev/null 2>&1; rm -rf "$TMP" + if ls "$SRC"/apk/*.zst >/dev/null 2>&1; then + SESSION="$(fw pm install-create $(pm_install_flags) $(installer_flag "") --user "$U" 2>/dev/null | sed -n 's/.*\[\([0-9]*\)\].*/\1/p')" + if [ -n "$SESSION" ]; then + TMP="$STATE/staging/mapk-r.$OPID"; rm -rf "$TMP"; mkdir -p "$TMP" + for af in "$SRC"/apk/*.zst; do [ -e "$af" ] || continue + bn="$(basename "$af" .zst)"; "$ZSTD" -dqc "$af" > "$TMP/$bn" + fw pm install-write "$SESSION" "${bn%.apk}" "$TMP/$bn" >/dev/null 2>&1; done + fw pm install-commit "$SESSION" >/dev/null 2>&1; rm -rf "$TMP" + fi + install_ok restore-mirror || { [ "$DTYPE" = local ] || rm -rf "$SRC"; return; } fi wait_datadir "/data/user/$U/$PKG" LIVEU="$(stat -c %u "/data/user/$U/$PKG" 2>/dev/null || echo 0)"; LIVEG="$(stat -c %g "/data/user/$U/$PKG" 2>/dev/null || echo 0)" @@ -1826,13 +1839,14 @@ do_import_databackup() { TMP="$STATE/staging/dbimp.$OPID"; rm -rf "$TMP"; mkdir -p "$TMP" "$ZSTD" -dqc "$REF/apk.tar.zst" | exec_ns "$TAR" $NAN -x -C "$TMP" 2>/dev/null verifier_off - SESSION="$(fw pm install-create $(pm_install_flags) $(installer_flag "") --user "$U" 2>/dev/null | sed -n 's/.*\[\([0-9]*\)\].*/\1/p')" + SESSION=""; ls "$TMP"/*.apk >/dev/null 2>&1 && SESSION="$(fw pm install-create $(pm_install_flags) $(installer_flag "") --user "$U" 2>/dev/null | sed -n 's/.*\[\([0-9]*\)\].*/\1/p')" if [ -n "$SESSION" ]; then for apk in "$TMP"/*.apk; do [ -e "$apk" ] || continue fw pm install-write "$SESSION" "$(basename "$apk" .apk)" "$apk" >/dev/null 2>&1; done fw pm install-commit "$SESSION" >/dev/null 2>&1 fi rm -rf "$TMP" + install_ok import-db || return fi wait_datadir "/data/user/$U/$PKG" LIVEU="$(stat -c %u "/data/user/$U/$PKG" 2>/dev/null || echo 0)"; LIVEG="$(stat -c %g "/data/user/$U/$PKG" 2>/dev/null || echo 0)" @@ -1910,14 +1924,17 @@ do_import_neo() { src="$apk" case "$apk" in *.enc) [ "$ENC" = yes ] || continue - src="$APKTMP/$(basename "${apk%.enc}")"; neo_decrypt_to "$apk" "$src" || { rm -f "$src"; continue; } ;; + src="$APKTMP/$(basename "${apk%.enc}")" + neo_decrypt_to "$apk" "$src" || { fw pm install-abandon "$SESSION" >/dev/null 2>&1; rm -rf "$APKTMP" + audit "import-neo decrypt failed $(basename "$apk") $PKG"; finish false ERR_IMPORT_ENCRYPTED; return; } ;; esac fw pm install-write "$SESSION" "$(basename "${src%.apk}")" "$src" >/dev/null 2>&1 done fw pm install-commit "$SESSION" >/dev/null 2>&1 fi + rm -rf "$APKTMP" + install_ok import-neo || return fi - rm -rf "$APKTMP" wait_datadir "/data/user/$U/$PKG" LIVEU="$(stat -c %u "/data/user/$U/$PKG" 2>/dev/null || echo 0)"; LIVEG="$(stat -c %g "/data/user/$U/$PKG" 2>/dev/null || echo 0)" LIVECTX="$(exec_ns ls -Zd "/data/user/$U/$PKG" 2>/dev/null | cut -d' ' -f1)" @@ -1926,7 +1943,8 @@ do_import_neo() { for ext in "tar.$COMP" tar.zst tar.gz tar; do f="$REF/$1.$ext"; src="$f" if [ -f "$f.enc" ]; then # encrypted archive: decrypt via dex - src="$STATE/staging/neo.$OPID.$1"; neo_decrypt_to "$f.enc" "$src" || { rm -f "$src"; continue; } + src="$STATE/staging/neo.$OPID.$1" + neo_decrypt_to "$f.enc" "$src" || { rm -f "$src"; audit "import-neo decrypt failed $1 $PKG"; finish false ERR_IMPORT_ENCRYPTED; return 1; } elif [ -f "$f" ]; then : # plain archive else continue; fi # anti-traversal: reject a tampered archive BEFORE the wipe (decrypted temp has From 0450f54e6285f4540a84eee7ed7447a21fb36a36 Mon Sep 17 00:00:00 2001 From: ash-iir <254348575+ash-iir@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:23:56 +0000 Subject: [PATCH 12/12] worker: fail a special backup that is incomplete or empty A special backup reported success even when nothing usable was written: there was no crypto check, archive failures were dropped silently, the tar exit status was masked, and the only test was a non-empty manifest. - Check crypto_available up front when a passphrase is set. - Use the same .trc sentinel and read-back check as archive_dir for each archive; a failure removes that archive and fails the backup. - Fail when a selected category produced no item. - A failed special backup removes its incomplete directory. Signed-off-by: ash-iir <254348575+ash-iir@users.noreply.github.com> (cherry picked from commit ea24deb00595ec411680f68c70916d04f9f0b647) --- module/scripts/worker.sh | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/module/scripts/worker.sh b/module/scripts/worker.sh index eab8cde..78c73bc 100644 --- a/module/scripts/worker.sh +++ b/module/scripts/worker.sh @@ -2131,6 +2131,7 @@ do_special_backup() { # gated by an explicit .system_settings flag exactly like ssaid; default OFF. SYSSET="$(jget "$ARGS" '.system_settings')" use_special_age() { [ -n "$PW" ]; } + if use_special_age && ! crypto_available; then finish false ERR_CRYPTO_UNAVAIL; return; fi prog 2 preparing "special"; audit "special-backup start $CATS"; notify backup_start "special" BK="$(backups_dir "$U")/.special"; TS="$(date -u +%Y%m%dT%H%M%SZ)"; DEST="$BK/$TS" media_own "$(backups_dir "$U")"; media_own "$BK"; mkdir -p "$DEST" @@ -2158,20 +2159,37 @@ do_special_backup() { parent="$(dirname "$src")"; base="$(basename "$src")" ctx="$(ls -Zd "$src" 2>/dev/null | cut -d' ' -f1)" ext="$(comp_ext "$COMP")"; arc="$cat.$idx.$ext"; use_special_age && arc="$arc.age" + # same completeness checks as archive_dir: a fatal tar exit (>1) via the .trc sentinel, + # then the written archive must read back as a complete tar. A failure marks the whole + # backup failed (.sfail; this loop is a subshell) instead of silently dropping the path. + rm -f "$DEST/.trc"; _sok=true if use_special_age; then - exec_ns "$TAR" $TARC -C "$parent" -c "$base" 2>/dev/null | comp "$COMP" | enc_age "$DEST/$arc" \ - || { audit "special archive failed $cat:$src"; continue; } + { exec_ns "$TAR" $TARC -C "$parent" -c "$base" 2>/dev/null || { _tc=$?; [ "$_tc" -gt 1 ] && echo "$_tc" > "$DEST/.trc"; }; } | comp "$COMP" | enc_age "$DEST/$arc" \ + && dec_age "$DEST/$arc" 2>/dev/null | { case "$COMP" in gzip) "$GZIP" -dc ;; lz4) "$ZSTD" -dq --format=lz4 -c ;; none) cat ;; *) "$ZSTD" -dqc ;; esac; } | exec_ns "$TAR" -t >/dev/null 2>&1 || _sok=false else - exec_ns "$TAR" $TARC -C "$parent" -c "$base" 2>/dev/null | comp "$COMP" > "$DEST/$arc" \ - || { audit "special archive failed $cat:$src"; continue; } + { exec_ns "$TAR" $TARC -C "$parent" -c "$base" 2>/dev/null || { _tc=$?; [ "$_tc" -gt 1 ] && echo "$_tc" > "$DEST/.trc"; }; } | comp "$COMP" > "$DEST/$arc" \ + && decomp "$DEST/$arc" | exec_ns "$TAR" -t >/dev/null 2>&1 || _sok=false + fi + if [ "$_sok" != true ] || [ -f "$DEST/.trc" ]; then + audit "special archive failed $cat:$src"; rm -f "$DEST/$arc" "$DEST/.trc"; : > "$DEST/.sfail"; continue fi sha="$(sha256sum "$DEST/$arc" 2>/dev/null | cut -d' ' -f1)" emit_special "$cat" "$arc" "$src" "$ctx" "$sha" done [ "$PORTABLE" != false ] && special_export "$cat" "$DEST" ;; esac + # a selected category that produced nothing (no provider path on this device, or every + # archive failed) is a failed backup, not an empty success. + if [ "$cat" != system_settings ] || [ "$SYSSET" = true ]; then + "$JQ" -e --arg c "$cat" 'select(.category == $c)' "$IFILE" >/dev/null 2>&1 \ + || { audit "special-backup: nothing backed up for $cat"; : > "$DEST/.sfail"; } + fi done_c=$((done_c+1)); prog $(( 10 + done_c * 80 / nc )) archiving "$cat" done + if [ -f "$DEST/.sfail" ]; then + case "$DEST" in "$BK"/?*) rm -rf "$DEST" ;; esac + finish false ERR_BACKUP; return + fi prog 92 manifest "special" FP="$(getprop ro.build.fingerprint)"; SDK="$(getprop ro.build.version.sdk)"; SEL="$(getenforce 2>/dev/null | tr 'A-Z' 'a-z')" "$JQ" -n --arg producer "VDBackup $(sed -n 's/^version=//p' "$MODDIR/module.prop")" \