From 84791fc7c6bccb05e2bf28711f53d792760124b1 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Fri, 13 Nov 2026 09:38:23 +0000 Subject: [PATCH] install-phone: a second install can't replace the first's staged files The image copy and the other staged files have fixed names in --work (root.img, root.simg, firstboot.conf, authorized_keys, boot-update.conf, ramdisk/), and every install uses the same default work directory. With two installs waiting at "Type the phone's codename", the second had replaced the first one's files, so confirming the first flashed the second owner's firstboot.conf while it said it set up the first owner; and either one's exit removed the other's files. An install or a dry run now holds an exclusive flock until it exits, taken before it reads the ramdisk or writes in the work directory, and a second one with the same work directory stops there, before the phone: "another install is using the work directory DIR: let it finish, or give this one its own with --work DIR". The lock file is beside the resolved work directory, .NAME.install.lock in its parent (as make-bundle's lock is beside the bundles), so two spellings of one directory lock one file and the directory still ends empty. It is kept after the run, since unlinking it could let two runs lock different inodes. The work directory's parent must be writable: if the lock file can't be created there, the install stops with a message naming it. A lock that can't be taken for any other reason (a file system without flock) stops the install too, with "can't lock the work directory DIR", rather than going ahead unlocked. The kernel drops the lock with the run's last process, so an install killed outright keeps none out. The lock is python3's fcntl, as #682's is: the bundle ships the installer alone. --verify-only takes no part and still runs beside an install, so #682's sweep is unchanged. tests/install-phone: a second install with the same --work, and one through a symlink to it, is refused within a time bound, before it writes in --work or reads the ramdisk (a mktemp and an lz4 that note their calls are never run) and before any question, and sends the phone nothing; the waiting install's staged files are unchanged and it flashes its own owner; --verify-only passes beside it; after a SIGKILL of a waiting install (and its child) the next one installs; the work directory ends empty with the lock beside it; an unwritable parent is refused; and a python3 stand-in whose flock on the lock fails refuses the install before any question, staging write or fastboot call. It fails on main's tool at "a second install with the same --work is refused". tests/install-phone-initramfs: #682's case ran two full installs in one --work, which this forbids. Its killed run is now a --verify-only held in its lz4 by a stand-in and killed with SIGKILL; every assertion keeps its meaning, and the suite still fails without the exclusive check, with the shared lock let go after marking, without the mark check (also with the run's own directory spared), without the mark, and with a blocking exclusive lock. Tested: tools/pr-check, tests/install-phone, tests/install-phone-initramfs, tests/install-guided, tests/all. Not tested: a phone; a network-mounted home; a real file system that rejects flock (the failure is simulated in the test). Co-Authored-By: Claude Opus 5.5 --- tests/install-phone | 102 +++++++++++++++++++++++++++++++++- tests/install-phone-initramfs | 36 ++++++++---- tools/install-phone | 48 +++++++++++++++- 3 files changed, 172 insertions(+), 14 deletions(-) diff --git a/tests/install-phone b/tests/install-phone index 68d30b517..4a870e137 100755 --- a/tests/install-phone +++ b/tests/install-phone @@ -11,7 +11,7 @@ cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." SRC=$PWD fail() { printf 'not ok - %s\n' "$1" >&2; exit 1; } pass() { printf 'ok - %s\n' "$1"; } -for tool in debugfs mkfs.ext4 zstd openssl lz4 cpio; do command -v $tool >/dev/null || { echo "skip: no $tool"; exit 0; }; done +for tool in debugfs mkfs.ext4 zstd openssl lz4 cpio timeout; do command -v $tool >/dev/null || { echo "skip: no $tool"; exit 0; }; done t=$(mktemp -d) cleanup() { [[ ! -d $t/gnupg ]] || gpgconf --homedir "$t/gnupg" --kill gpg-agent 2>/dev/null || true @@ -450,6 +450,106 @@ if tinstall --verify-only --unsigned; then fail "--verify-only refuses a bundle [[ ! -s $t/fastboot.log ]] || fail "a refused --verify-only never runs fastboot" pass "as a bundle's tools/install-phone: finds the bundle, and --verify-only checks it with no phone" +# Two installs with one --work: the staged files have fixed names there, so a +# second install while the first waits at its codename would replace them (the +# first would flash the second owner's answers) and remove them when it ends. +# The second, by any spelling of that directory, stops before it writes in +# --work or reads the ramdisk (a mktemp and an lz4 that note their calls show +# either), so before it contacts the phone; --verify-only still runs beside +# the first; an install killed outright leaves the next one free to go; and +# the lock is beside --work, which ends empty, so a parent that can't be +# written stops the install, as does a lock that can't be taken (a python3 +# that fails that one flock call stands in for a file system without flock). +second_install() { + local status=0 + : > "$t/fastboot.log" + answers | HOME=$t/home PATH="$t/bin:$PATH" \ + timeout --kill-after=2s 30s "$R/tools/install-phone" \ + --device testdev --images "$t/images" --work "$t/work" "$@" \ + > "$t/out" 2>&1 || status=$? + ((status != 124 && status != 137)) || + fail "a second install refuses promptly instead of waiting for the lock" + return "$status" +} +waiting() { # NAME: an install for user NAME, waiting at the codename; fd 9 types it + mkfifo "$t/$1.in" + HOME=$t/home PATH="$t/bin:$PATH" "$R/tools/install-phone" --device testdev --images "$t/images" --work "$t/work" \ + < "$t/$1.in" > "$t/$1.out" 2>&1 & + exec 9> "$t/$1.in" + USERS=$1 answers | sed '$d' >&9 + for _ in $(seq 300); do grep -c 'are not touched' "$t/$1.out" >/dev/null && return; sleep 0.1; done + cat "$t/$1.out"; fail "an install waits at the codename prompt" +} +staged() { (cd "$t/work" && find . -name 'verify.*' -prune -o -type f -print | LC_ALL=C sort | xargs cksum); } +flashed_user() { + local f=$t/userdata.flashed + if command -v simg2img >/dev/null && [[ $(head -c4 "$f" | od -An -tx1 | tr -d ' ') == 3aff26ed ]]; then + simg2img "$f" "$t/flashed.img"; f=$t/flashed.img + fi + debugfs -R 'cat /etc/omamobile/firstboot.conf' "$f" 2>/dev/null | sed -n 's/^USERNAME=//p' +} +mkdir -p "$t/watch" +cat > "$t/watch/lz4" <<'EOF' +#!/bin/bash +# lz4 or mktemp (by the name it is called), noting the call +echo "${0##*/} $*" >> "$T/watched.log" +PATH=${PATH//"$T/watch:"/} exec "${0##*/}" "$@" +EOF +chmod +x "$t/watch/lz4" && ln -s lz4 "$t/watch/mktemp" +waiting alice +first=$! +before=$(staged) +if PATH=$t/watch:$PATH USERS=bob second_install 9>&-; then cat "$t/out"; fail "a second install with the same --work is refused"; fi +grep -c "another install is using the work directory $t/work" "$t/out" >/dev/null || { cat "$t/out"; fail "says another install uses the work directory"; } +grep -c "About the phone's owner" "$t/out" >/dev/null && fail "the second install asks no questions" +[[ ! -s $t/fastboot.log ]] || fail "the second install sends the phone nothing" +ln -s work "$t/work-link" +if PATH=$t/watch:$PATH USERS=bob second_install --work "$t/work-link" 9>&-; then cat "$t/out"; fail "a second install with another spelling of the --work is refused"; fi +[[ ! -e $t/watched.log ]] || { cat "$t/watched.log"; fail "a second install stops before it writes in --work or reads the ramdisk"; } +[[ $(staged) == "$before" ]] || fail "the second install leaves the first one's staged files as they were" +install --verify-only 9>&- || { cat "$t/out"; fail "--verify-only runs beside a waiting install"; } +[[ $(staged) == "$before" ]] || fail "--verify-only leaves the waiting install's staged files as they were" +: > "$t/fastboot.log" +echo testdev >&9 +exec 9>&- +wait $first || { cat "$t/alice.out"; fail "the waiting install goes ahead"; } +[[ $(flashed_user) == alice ]] || fail "the waiting install flashes its own owner's answers" +waiting carol +killed=$! +kill -KILL $killed $(cat /proc/$killed/task/*/children) +exec 9>&- +wait $killed 2>/dev/null || true +USERS=dave install || { cat "$t/out"; fail "an install killed outright leaves the next one free to go"; } +[[ $(flashed_user) == dave ]] || fail "the next install flashes its own answers" +[[ -z $(ls -A "$t/work") && -f $t/.work.install.lock ]] || fail "the work directory ends empty, the lock beside it" +if ((EUID != 0)); then + mkdir -p "$t/ro/work" && chmod 555 "$t/ro" + install --work "$t/ro/work" && refused=0 || refused=1 + chmod 755 "$t/ro" + ((refused)) || { cat "$t/out"; fail "an install whose lock can't be made beside --work is refused"; } + grep -c "can't create the lock file $t/ro/.work.install.lock beside the work directory" "$t/out" >/dev/null || { cat "$t/out"; fail "names the lock file it can't create"; } + [[ ! -s $t/fastboot.log ]] || fail "an install with no lock sends the phone nothing" +fi +mkdir -p "$t/nolock" +cat > "$t/nolock/python3" <<'EOF' +#!/bin/bash +# python3 ($PYTHON3), but the install lock's flock fails as on a file system without flock +if [[ $# == 3 && $1 == -I && $2 == - ]]; then + script=$(cat) + [[ $script != *'except BlockingIOError'* ]] || + script=$'import errno, fcntl\ndef flock(fd, op): raise OSError(errno.EOPNOTSUPP, "Operation not supported")\nfcntl.flock = flock\n'$script + exec "$PYTHON3" -I -c "$script" "$3" +fi +exec "$PYTHON3" "$@" +EOF +chmod +x "$t/nolock/python3" +if PYTHON3=$(command -v python3) PATH=$t/nolock:$PATH install; then cat "$t/out"; fail "an install whose lock can't be taken is refused"; fi +grep -c "can't lock the work directory $t/work (give a --work DIR on a file system that supports flock)" "$t/out" >/dev/null || + { cat "$t/out"; fail "says it can't lock the work directory"; } +grep -c "About the phone's owner" "$t/out" >/dev/null && fail "an install with no lock asks no questions" +[[ ! -s $t/fastboot.log && -z $(ls -A "$t/work") ]] || fail "an install with no lock stages nothing and sends the phone nothing" +pass "one install at a time per work directory: a second one stops before the phone, --verify-only runs beside it, a killed one keeps none out, the lock is beside --work, no lock stops the install" + # The guided installer has asked the questions: they aren't printed again, # the answers are still read and checked, and the closing hints stay out OMAMOBILE_INSTALL_GUIDED=1 install --dry-run || { cat "$t/out"; fail "a dry run for the guided installer succeeds"; } diff --git a/tests/install-phone-initramfs b/tests/install-phone-initramfs index ca585a9f4..550b71fbe 100755 --- a/tests/install-phone-initramfs +++ b/tests/install-phone-initramfs @@ -207,29 +207,41 @@ for dev in "twelve gs201 0x9855001 0x800 vendor_kernel_boot" "six gs101 0x984500 done # A run killed outright leaves its verify.XXXXXX in --work. While another -# run is live nothing is removed: here --verify-only beside an install waiting -# at the codename, which then appends the key from its own directory. The -# next run with no other live removes the killed one's, but never a directory -# no run marked (as the installer before the sweep left them, whose run may -# still be live). +# run is live nothing is removed: here a --verify-only killed part-way, and +# another beside an install waiting at the codename, which then appends the +# key from its own directory. The next run with no other live removes the +# killed one's, but never a directory no run marked (as the installer before +# the sweep left them, whose run may still be live). The killed run is a +# --verify-only held in its lz4 by a stand-in that waits on a fifo, because a +# second install with the same --work stops before it reads the ramdisk. held() { # name fd: an install of six waiting at the codename, which fd gives mkfifo "$t/$1.fifo" && eval "exec $2<>\"\$t/$1.fifo\"" # Its output is in OUT; the job's own shell would print bash's "Killed" - # notice for the one killed below, which in a passing log reads as a failure + # notice for a killed run, which in a passing log reads as a failure HOLD=$t/$1.fifo OUT=$t/$1.out install six 7>&- 8>&- 2>/dev/null & for _ in $(seq 600); do grep -c "Type the phone's codename" "$t/$1.out" >/dev/null 2>&1 && return; sleep 0.1; done cat "$t/$1.out"; fail "an install waits at the codename prompt" } +mkdir -p "$t/hold" +printf '#!/bin/bash\necho "$BASHPID" > "$T/hold.pid"\nread -r _ < "$T/hold.fifo" || exit 1\nexec %q "$@"\n' "$(command -v lz4)" > "$t/hold/lz4" +chmod +x "$t/hold/lz4" +held_verify() { # fd: a --verify-only of six, held in its lz4 (which fd gives) + mkfifo "$t/hold.fifo" && eval "exec $1<>\"\$t/hold.fifo\"" + # As in held: no "Killed" notice from the job's own shell + PATH=$t/hold:$PATH OUT=$t/killed.out install six --verify-only 7>&- 8>&- 2>/dev/null & + for _ in $(seq 600); do [[ -s $t/hold.pid ]] && return; sleep 0.1; done + cat "$t/killed.out"; fail "a --verify-only is held part-way" +} verify_dirs() { (cd "$t/work" && ls -d verify.* | LC_ALL=C sort | tr '\n' ' '); } mkdir -p "$t/work/verify.unmarked" held live 7 live=$! -held killed 8 -kill -KILL "$(cat "$t/killed.out.pid")" +held_verify 8 +kill -KILL "$(cat "$t/killed.out.pid")" "$(cat "$t/hold.pid")" exec 8>&- wait $! || true both=$(verify_dirs) -[[ $(wc -w <<<"$both") == 3 ]] || fail "a second install, killed, leaves its directory and the waiting one's (got: $both)" +[[ $(wc -w <<<"$both") == 3 ]] || fail "a --verify-only, killed part-way, leaves its directory and the waiting install's (got: $both)" install six --verify-only || { cat "$t/out"; fail "--verify-only passes while an install waits"; } [[ $(verify_dirs) == "$both" ]] || fail "--verify-only beside a waiting install removes nothing (had: $both; now: $(verify_dirs))" echo six >&7 @@ -238,11 +250,11 @@ wait $live || { cat "$t/live.out"; fail "the waiting install goes ahead after -- cmp -s "$t/flashed/vendor_boot_b" "$t/hook-images-six/vendor_boot.img" || fail "the waiting install appends the key from its own directory after --verify-only" [[ $(wc -w <<<"$(verify_dirs)") == 2 ]] || fail "the install removes its own directory and no other" -install six --verify-only || { cat "$t/out"; fail "--verify-only passes after a killed install"; } +install six --verify-only || { cat "$t/out"; fail "--verify-only passes after a killed run"; } [[ $(verify_dirs) == 'verify.unmarked ' ]] || - fail "a run with no other live removes a killed install's directory, and nothing unmarked (left: $(verify_dirs))" + fail "a run with no other live removes a killed run's directory, and nothing unmarked (left: $(verify_dirs))" rm -r "$t/work" -pass "a killed install's verify directory is removed by the next run alone; a live install's and an unmarked one stay" +pass "a killed run's verify directory is removed by the next run alone; a live install's and an unmarked one stay" # An install waiting at "Type the phone's codename" has its images staged in # --work. A --verify-only run on the same --work, or a second install refused diff --git a/tools/install-phone b/tools/install-phone index 5af96b628..baa19e31d 100755 --- a/tools/install-phone +++ b/tools/install-phone @@ -77,6 +77,12 @@ # Answers are read from standard input, so they can be piped for tests and by # the guided installer, which sets OMAMOBILE_INSTALL_GUIDED=1: the questions # are then not printed again (it has asked them), nor the closing hints. +# +# One install or dry run at a time uses a work directory: each holds a lock +# beside it (.NAME.install.lock in its parent directory), and a second one +# with the same work directory stops before it contacts the phone (give it +# its own --work DIR). --verify-only runs beside them. +# Its parent directory must be writable; the lock file is kept after the run. set -euo pipefail shopt -s nullglob # Byte-wise character ranges in the checks below ([ -~] is printable ASCII) @@ -412,12 +418,52 @@ if [[ -f $manifest ]]; then if [[ -n $modules_release && -n $images_release ]]; then note "kernel $images_release, its modules in the root image"; fi fi +# --- One install at a time per work directory -------------------------------- +# The files staged below have fixed names in --work: a second install there +# would replace them while this one waits for its codename, and remove them +# when it ends. So an install (or a dry run; --verify-only takes no part) +# holds an exclusive flock until it exits, and a second one stops here, before +# it reads the ramdisk or writes in --work. The kernel drops the lock with the +# run's last process, however it ends. The lock file is beside the resolved +# work directory (as make-bundle's is beside the bundles), so two spellings of +# it lock one file and the directory still ends empty. It stays: unlinking it +# could let two runs lock different inodes. A parent that can't be written, or +# a lock that can't be taken for any other reason (a file system without +# flock), stops the install too: going ahead unlocked could let a second +# install replace this one's files. +work=${work:-${XDG_CACHE_HOME:-$HOME/.cache}/omamobile-install} +if ((!verify_only)); then + mkdir -p "$work" && work_real=$(cd -P -- "$work" && pwd -P) || + die "can't create or write to the work directory $work (give one you can write to with --work DIR)" + install_lock=${work_real%/*}/.${work_real##*/}.install.lock + exec {install_fd}>>"$install_lock" || + die "can't create the lock file $install_lock beside the work directory (give a --work DIR under a directory you can write to)" + if python3 -I - "$install_fd" <<'PY' +import fcntl, sys +try: + fcntl.flock(int(sys.argv[1]), fcntl.LOCK_EX | fcntl.LOCK_NB) +except BlockingIOError: + sys.exit(1) +except OSError as error: + print(f"flock: {error}", file=sys.stderr) + sys.exit(2) +PY + then + : + else + lock_status=$? + if ((lock_status == 1)); then + die "another install is using the work directory $work: let it finish, or give this one its own with --work DIR" + fi + die "can't lock the work directory $work (give a --work DIR on a file system that supports flock)" + fi +fi + # --- Slot B's shipped ramdisk ------------------------------------------------ # Read and listed before the phone is contacted, so --verify-only checks it # too; the owner's key is appended to it further down. The read has a # directory of its own in --work that only this run removes: another install # may be waiting there for its answers, with its images staged. -work=${work:-${XDG_CACHE_HOME:-$HOME/.cache}/omamobile-install} verify_dir="" # vendor_ramdisk get IMG OUT, or vendor_ramdisk set IMG RAMDISK OUT (inline, # because a bundle ships this script alone)