From f8897ffbf5826a4e4ced2f9e79eb347c5cc166db Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Wed, 11 Nov 2026 00:23:26 +0000 Subject: [PATCH 1/2] install-phone: --verify-only reads and lists the shipped ramdisk too --verify-only stopped before the ramdisk was read, so a bundle whose ramdisk lz4 decodes but cpio can't list passed it ("The files check out"), and only the full install of the same bundle stopped, after the phone and the owner's questions. The maintainer named this on #623. Reading the ramdisk (the AVB footer refusal, vendor_ramdisk get, lz4 -dc, cpio -it) now runs before the phone is contacted, for both runs, in the work directory, which is set up there with its cleanup. The key's append still comes after the questions and reuses that listing and archive, so what a full install flashes is unchanged; most of the diff is that move. Tested: a bundle with a truncated cpio in valid lz4, run as ./install --unsigned --verify-only, exits 0 on main and stops with cpio's reason and no fastboot call here. tests/install-phone-initramfs's new --verify-only cases each fail with main's tool, and its byte comparison passes under caller umask 077, 022 and 002; tests/install-phone, tests/all and tools/pr-check pass. Not run on a phone or with the guided installer. Co-Authored-By: Claude Opus 5.5 --- tests/install-phone-initramfs | 21 +++-- tools/install-phone | 144 ++++++++++++++++++---------------- 2 files changed, 92 insertions(+), 73 deletions(-) diff --git a/tests/install-phone-initramfs b/tests/install-phone-initramfs index 8d0cb6469..a5f7421d6 100755 --- a/tests/install-phone-initramfs +++ b/tests/install-phone-initramfs @@ -20,7 +20,8 @@ # # Then the refusals, each before anything is flashed: an image the key makes # larger than its partition, a failing step of the key's append, a ramdisk -# image under an AVB footer, an image that isn't a v4 vendor boot image; and +# image under an AVB footer, an image that isn't a v4 vendor boot image (the +# ones in reading the shipped ramdisk also by --verify-only, with no phone); and # a ramdisk that already holds SSH keys (a development initramfs) is flashed # as built. The hook runs under pacman's umask, 022, and install under 077: # its images must not depend on the owner's umask (uutils' install(1) gives @@ -147,11 +148,15 @@ install() { # codename [args]; RAMDISK_PART_SIZE: vendor_kernel_boot_b's size printf '%s\n' alice 1234 1234 "" UTC "" "" "" "$t/owner.pub" "$cn" | (umask 077; HOME=$t/home PATH="$t/fbin:$PATH" "$BUNDLE/install" --unsigned --work "$t/work" "$@") > "$t/out" 2>&1 } -refused() { # why message: install refused, said why, flashed nothing - if install twelve; then fail "refuses $1"; fi +refused() { # why message [args]: install refused, said why, flashed nothing + if install twelve "${@:3}"; then fail "refuses $1"; fi grep -cF -- "$2" "$t/out" >/dev/null || { cat "$t/out"; fail "says why it refuses $1"; } [[ -z $(ls -A "$t/flashed") ]] || fail "nothing is flashed when it refuses $1" } +verify_refused() { # why message: --verify-only refuses it too, and contacts no phone + refused "$1 with --verify-only" "$2" --verify-only + [[ ! -s $t/fastboot.log ]] || fail "--verify-only contacts no phone when it refuses $1" +} # slot_b root images: the stand-in phone's slot B partitions hold those # images, padded to 16 MiB, as fastboot leaves them slot_b() { @@ -252,14 +257,17 @@ for step in "install 600 can't stage the owner's key" "cpio -o cpio couldn't pac printf '#!/bin/bash\nfor a; do [[ $a == %q ]] && exit 1; done\nexec %q "$@"\n' "$arg" "$(type -P "$tool")" > "$t/bad-$tool/$tool" chmod +x "$t/bad-$tool/$tool" PATH=$t/bad-$tool:$PATH refused "a failing $tool $arg in the key's append" "$why" + # Reading the shipped ramdisk comes before the phone, so --verify-only does it too + [[ $arg != -dc && $arg != -it ]] || PATH=$t/bad-$tool:$PATH verify_refused "a failing $tool $arg" "$why" done -pass "a failing step of the key's append (install, cpio -o, lz4 -dc, cpio -it or lz4 -l) stops the install before anything is flashed" +pass "a failing step of the key's append (install, cpio -o, lz4 -dc, cpio -it or lz4 -l) stops the install before anything is flashed; --verify-only stops at lz4 -dc and cpio -it too" # The ramdisk image under an AVB footer can't take the key: refused printf 'AVB_FOOTERS="vendor_kernel_boot:0x4000000"\nAVB_ROLLBACK_INDEX=1\nAVB_SECURITY_PATCH=2026-07-05\n' >> "$BUNDLE/device.conf" seal refused "a ramdisk image with an AVB footer" "AVB_FOOTERS names vendor_kernel_boot, which carries the ramdisk" -pass "a ramdisk image the descriptor signs is refused before anything is flashed" +verify_refused "a ramdisk image with an AVB footer" "AVB_FOOTERS names vendor_kernel_boot, which carries the ramdisk" +pass "a ramdisk image the descriptor signs is refused before anything is flashed, and by --verify-only" # A development initramfs that already holds SSH keys (bin/omamobile # initramfs without --release): the hook refuses it, so its builder packs @@ -279,4 +287,5 @@ pass "a ramdisk that already holds SSH keys is flashed as built" echo 'vendor_kernel_boot image' > "$BUNDLE/images/vendor_kernel_boot.img" seal refused "a ramdisk image that isn't a vendor boot image" 'not an Android vendor boot image' -pass "an image that isn't a v4 vendor boot image is refused before anything is flashed" +verify_refused "a ramdisk image that isn't a vendor boot image" 'not an Android vendor boot image' +pass "an image that isn't a v4 vendor boot image is refused before anything is flashed, and by --verify-only" diff --git a/tools/install-phone b/tools/install-phone index b6ed7709f..55bb0c81c 100755 --- a/tools/install-phone +++ b/tools/install-phone @@ -65,7 +65,8 @@ # (the guided installer) passes it, so the phone # it looked at is the one that is written # --verify-only check the files (a bundle's signature, checksums, -# manifest.json and root image) and stop before +# manifest.json, root image and slot B's ramdisk, +# read in the --work directory) and stop before # the phone is contacted # # Answers are read from standard input, so they can be piped for tests and by @@ -402,6 +403,79 @@ if [[ -f $manifest ]]; then fi if [[ -n $modules_release && -n $images_release ]]; then note "kernel $images_release, its modules in the root image"; fi fi + +# --- The work directory: the ramdisk, then the root image copy --------------- +work=${work:-${XDG_CACHE_HOME:-$HOME/.cache}/omamobile-install} +mkdir -p "$work" +chmod 700 "$work" +copy=$work/root.img sparse=$work/root.simg conf_file=$work/firstboot.conf keys_file=$work/authorized_keys +boot_conf=$work/boot-update.conf ramdisk_dir=$work/ramdisk +# The copy holds the password hash (and any Wi-Fi password): never left behind +cleanup() { rm -f "$conf_file" "$keys_file" "$boot_conf" "$copy" "$sparse"; rm -rf "$ramdisk_dir"; } +trap cleanup EXIT + +# --- 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. +# vendor_ramdisk get IMG OUT, or vendor_ramdisk set IMG RAMDISK OUT (inline, +# because a bundle ships this script alone) +vendor_ramdisk() { + python3 -I - "$@" <<'PY' +import struct, sys + +def bad(why): + print("%s: %s" % (sys.argv[2], why), file=sys.stderr) + sys.exit(1) + +img = open(sys.argv[2], "rb").read() +# vendor_boot_img_hdr_v4: magic, header_version, page_size, kernel_addr, +# ramdisk_addr, vendor_ramdisk_size (offset 24), cmdline[2048], tags_addr, +# name[16], header_size (2096), dtb_size, dtb_addr (u64), +# vendor_ramdisk_table_size (2112), _entry_num, _entry_size, bootconfig_size +if len(img) < 2128 or img[:8] != b"VNDRBOOT": + bad("not an Android vendor boot image") +version, page, _, _, rd_size = struct.unpack_from("<5I", img, 8) +hdr_size, dtb_size = struct.unpack_from("<2I", img, 2096) +table_size, entries, entry_size, bootconfig_size = struct.unpack_from("<4I", img, 2112) +if version != 4 or hdr_size != 2128: + bad("not a v4 vendor boot image") +if page < 2048 or page & (page - 1): + bad("odd page size %d" % page) +if (entries, entry_size, table_size) != (1, 108, 108): + bad("expected one vendor ramdisk, as bin/omamobile images builds") +pad = lambda n: -(-n // page) * page +o_rd = pad(2128) +o_dtb = o_rd + pad(rd_size) +o_table = o_dtb + pad(dtb_size) +if len(img) != o_table + pad(table_size) + pad(bootconfig_size): + bad("its sections don't add up to its size (a footer?)") +if struct.unpack_from("<2I", img, o_table) != (rd_size, 0): + bad("its ramdisk table doesn't match its header") +if sys.argv[1] == "get": + open(sys.argv[3], "wb").write(img[o_rd:o_rd + rd_size]) + sys.exit(0) +ramdisk = open(sys.argv[3], "rb").read() +head, tail = bytearray(img[:o_rd]), bytearray(img[o_dtb:]) +struct.pack_into(" "$ramdisk_dir/shipped.cpio" || die "lz4 couldn't decompress $ramdisk_image.img's ramdisk" + # With --quiet, cpio -it says nothing when it lists the archive; given bytes + # that aren't one, it prints a line for every offset it tries and then its + # reason, so the message carries only that last line. + listing=$(cpio -it --quiet < "$ramdisk_dir/shipped.cpio" 2> "$ramdisk_dir/cpio.err") || + die "cpio couldn't list $ramdisk_image.img's ramdisk: $(tail -n 1 "$ramdisk_dir/cpio.err")" +fi if ((verify_only)); then say "The files check out (the phone was not contacted)" exit 0 @@ -519,14 +593,6 @@ done ssh_key=$(head -1 "$key_file") # --- A local copy of the root image, with the owner's first-boot file ------- -work=${work:-${XDG_CACHE_HOME:-$HOME/.cache}/omamobile-install} -mkdir -p "$work" -chmod 700 "$work" -copy=$work/root.img sparse=$work/root.simg conf_file=$work/firstboot.conf keys_file=$work/authorized_keys -boot_conf=$work/boot-update.conf ramdisk_dir=$work/ramdisk -# The copy holds the password hash (and any Wi-Fi password): never left behind -cleanup() { rm -f "$conf_file" "$keys_file" "$boot_conf" "$copy" "$sparse"; rm -rf "$ramdisk_dir"; } -trap cleanup EXIT ( umask 077 { @@ -607,67 +673,11 @@ fi # the copy's v4 header and ramdisk table, where mkbootimg puts it. With the # phone's lz4 and cpio that is the hook's image byte for byte, under any umask # (tests/install-phone-initramfs). -# Inline, because a bundle ships this script alone. A ramdisk that already -# holds authorized_keys (a development initramfs) is left as built. +# A ramdisk that already holds authorized_keys (a development initramfs) is +# left as built. declare -A image_file=() for p in $BOOT_IMAGES; do image_file[$p]=$images/$p.img; done -# vendor_ramdisk get IMG OUT, or vendor_ramdisk set IMG RAMDISK OUT -vendor_ramdisk() { - python3 -I - "$@" <<'PY' -import struct, sys - -def bad(why): - print("%s: %s" % (sys.argv[2], why), file=sys.stderr) - sys.exit(1) - -img = open(sys.argv[2], "rb").read() -# vendor_boot_img_hdr_v4: magic, header_version, page_size, kernel_addr, -# ramdisk_addr, vendor_ramdisk_size (offset 24), cmdline[2048], tags_addr, -# name[16], header_size (2096), dtb_size, dtb_addr (u64), -# vendor_ramdisk_table_size (2112), _entry_num, _entry_size, bootconfig_size -if len(img) < 2128 or img[:8] != b"VNDRBOOT": - bad("not an Android vendor boot image") -version, page, _, _, rd_size = struct.unpack_from("<5I", img, 8) -hdr_size, dtb_size = struct.unpack_from("<2I", img, 2096) -table_size, entries, entry_size, bootconfig_size = struct.unpack_from("<4I", img, 2112) -if version != 4 or hdr_size != 2128: - bad("not a v4 vendor boot image") -if page < 2048 or page & (page - 1): - bad("odd page size %d" % page) -if (entries, entry_size, table_size) != (1, 108, 108): - bad("expected one vendor ramdisk, as bin/omamobile images builds") -pad = lambda n: -(-n // page) * page -o_rd = pad(2128) -o_dtb = o_rd + pad(rd_size) -o_table = o_dtb + pad(dtb_size) -if len(img) != o_table + pad(table_size) + pad(bootconfig_size): - bad("its sections don't add up to its size (a footer?)") -if struct.unpack_from("<2I", img, o_table) != (rd_size, 0): - bad("its ramdisk table doesn't match its header") -if sys.argv[1] == "get": - open(sys.argv[3], "wb").write(img[o_rd:o_rd + rd_size]) - sys.exit(0) -ramdisk = open(sys.argv[3], "rb").read() -head, tail = bytearray(img[:o_rd]), bytearray(img[o_dtb:]) -struct.pack_into(" "$ramdisk_dir/shipped.cpio" || die "lz4 couldn't decompress $ramdisk_image.img's ramdisk" - # With --quiet, cpio -it says nothing when it lists the archive; given bytes - # that aren't one, it prints a line for every offset it tries and then its - # reason, so the message carries only that last line. - listing=$(cpio -it --quiet < "$ramdisk_dir/shipped.cpio" 2> "$ramdisk_dir/cpio.err") || - die "cpio couldn't list $ramdisk_image.img's ramdisk: $(tail -n 1 "$ramdisk_dir/cpio.err")" if grep -qE '^(\./)?root/\.ssh/authorized_keys$' <<<"$listing"; then note "$ramdisk_image.img's initramfs already holds SSH keys (a development build): left as built" else From 017d669b6738c8276fd85da48fcd6c1c301e3105 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Wed, 11 Nov 2026 15:37:49 +0000 Subject: [PATCH 2/2] install-phone: read the ramdisk in a directory of its own in --work Moving the ramdisk read ahead of the phone also moved the work directory's setup and cleanup trap there, so a --verify-only run, or an install refused before the owner's answers (another phone's --serial, a phone running from slot b, an answer it refuses), removed root.img, root.simg and ramdisk/ from --work on exit. If another install was waiting at "Type the phone's codename" with its images staged there, it then flashed boot_b, init_boot_b and vendor_boot_b and stopped on the missing vendor_kernel_boot image. On main neither run touched --work. The read now happens in mktemp -d "$work/verify.XXXXXX", which only its own run removes. The shared files, ramdisk/ and their cleanup are set up where main sets them up, after the answers, and the key's append reads the shipped archive from the read's directory. The flashed images and the paths the plan prints are unchanged. Tested: tests/install-phone-initramfs gains a case that stages root.simg and ramdisk/vendor_kernel_boot.img in --work, then runs --verify-only and an install refused for another --serial; both files must remain and nothing else may be left. It fails on the previous head's tool, each case on its own, and fails again with the read's trap removed, with the cleanup set up before the phone, or with the read's directory left out of the cleanup. tests/install-phone and tests/install-phone-initramfs pass, the latter under caller umask 077, 022 and 002 (the flashed images are the hook's byte for byte). No phone was used. Co-Authored-By: Claude Opus 5.5 --- tests/install-phone-initramfs | 20 ++++++++++++++++ tools/install-phone | 43 +++++++++++++++++++---------------- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/tests/install-phone-initramfs b/tests/install-phone-initramfs index a5f7421d6..769dc3ce2 100755 --- a/tests/install-phone-initramfs +++ b/tests/install-phone-initramfs @@ -204,6 +204,26 @@ for dev in "twelve gs201 0x9855001 0x800 vendor_kernel_boot" "six gs101 0x984500 pass "$cn: the bundle's install flashes the hook's own $rimg.img (the owner's key appended) and the hook's other images" done +# 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 +# before the owner's answers (here another phone's --serial), reads the +# ramdisk in a directory of its own: it leaves those files as they were and +# nothing of its own behind +stage() { mkdir -p "$t/work/ramdisk" && echo staged > "$t/work/root.simg" && echo staged > "$t/work/ramdisk/vendor_kernel_boot.img"; } +still_staged() { # what ran + [[ $(cat "$t/work/root.simg" "$t/work/ramdisk/vendor_kernel_boot.img" 2>/dev/null) == $'staged\nstaged' ]] || + fail "$1 leaves another install's root.simg and ramdisk/ in --work" + [[ $(cd "$t/work" && find . -mindepth 1 | LC_ALL=C sort | tr '\n' ' ') == "./ramdisk ./ramdisk/vendor_kernel_boot.img ./root.simg " ]] || + fail "$1 leaves nothing of its own in --work" +} +stage +install six --verify-only || { cat "$t/out"; fail "--verify-only passes the bundle"; } +still_staged --verify-only +refused "another phone's --serial" 'not the one this install was started for' --serial OTHER999 +still_staged "an install refused for another phone's --serial" +rm -r "$t/work" +pass "--verify-only, and an install refused before the answers, leave another install's staged files in --work alone" + # The hook's first run after the install: slot B already holds its images. # Slot B as the bundle's images left it (before this change) still needed # the rewrite #564 found. diff --git a/tools/install-phone b/tools/install-phone index 55bb0c81c..1faafc16e 100755 --- a/tools/install-phone +++ b/tools/install-phone @@ -66,8 +66,9 @@ # it looked at is the one that is written # --verify-only check the files (a bundle's signature, checksums, # manifest.json, root image and slot B's ramdisk, -# read in the --work directory) and stop before -# the phone is contacted +# read in a directory of its own in --work that +# it removes) and stop before the phone is +# contacted # # 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 @@ -404,19 +405,13 @@ if [[ -f $manifest ]]; then if [[ -n $modules_release && -n $images_release ]]; then note "kernel $images_release, its modules in the root image"; fi fi -# --- The work directory: the ramdisk, then the root image copy --------------- -work=${work:-${XDG_CACHE_HOME:-$HOME/.cache}/omamobile-install} -mkdir -p "$work" -chmod 700 "$work" -copy=$work/root.img sparse=$work/root.simg conf_file=$work/firstboot.conf keys_file=$work/authorized_keys -boot_conf=$work/boot-update.conf ramdisk_dir=$work/ramdisk -# The copy holds the password hash (and any Wi-Fi password): never left behind -cleanup() { rm -f "$conf_file" "$keys_file" "$boot_conf" "$copy" "$sparse"; rm -rf "$ramdisk_dir"; } -trap cleanup EXIT - # --- 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. +# 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) vendor_ramdisk() { @@ -467,14 +462,16 @@ if [[ " $BOOT_IMAGES " == *" vendor_kernel_boot "* ]]; then ramdisk_image=vendor elif [[ " $BOOT_IMAGES " == *" vendor_boot "* ]]; then ramdisk_image=vendor_boot; fi if [[ -n $ramdisk_image ]]; then [[ -z ${avb_size[$ramdisk_image]:-} ]] || die "$conf: AVB_FOOTERS names $ramdisk_image, which carries the ramdisk: the owner's key can't be added to a signed image" - rm -rf "$ramdisk_dir" && mkdir -p "$ramdisk_dir" - vendor_ramdisk get "$images/$ramdisk_image.img" "$ramdisk_dir/shipped.cpio.lz4" || die "can't read the ramdisk of $ramdisk_image.img" - lz4 -dc "$ramdisk_dir/shipped.cpio.lz4" > "$ramdisk_dir/shipped.cpio" || die "lz4 couldn't decompress $ramdisk_image.img's ramdisk" + mkdir -p "$work" + verify_dir=$(mktemp -d "$work/verify.XXXXXX") + trap 'rm -rf "$verify_dir"' EXIT + vendor_ramdisk get "$images/$ramdisk_image.img" "$verify_dir/shipped.cpio.lz4" || die "can't read the ramdisk of $ramdisk_image.img" + lz4 -dc "$verify_dir/shipped.cpio.lz4" > "$verify_dir/shipped.cpio" || die "lz4 couldn't decompress $ramdisk_image.img's ramdisk" # With --quiet, cpio -it says nothing when it lists the archive; given bytes # that aren't one, it prints a line for every offset it tries and then its # reason, so the message carries only that last line. - listing=$(cpio -it --quiet < "$ramdisk_dir/shipped.cpio" 2> "$ramdisk_dir/cpio.err") || - die "cpio couldn't list $ramdisk_image.img's ramdisk: $(tail -n 1 "$ramdisk_dir/cpio.err")" + listing=$(cpio -it --quiet < "$verify_dir/shipped.cpio" 2> "$verify_dir/cpio.err") || + die "cpio couldn't list $ramdisk_image.img's ramdisk: $(tail -n 1 "$verify_dir/cpio.err")" fi if ((verify_only)); then say "The files check out (the phone was not contacted)" @@ -593,6 +590,13 @@ done ssh_key=$(head -1 "$key_file") # --- A local copy of the root image, with the owner's first-boot file ------- +mkdir -p "$work" +chmod 700 "$work" +copy=$work/root.img sparse=$work/root.simg conf_file=$work/firstboot.conf keys_file=$work/authorized_keys +boot_conf=$work/boot-update.conf ramdisk_dir=$work/ramdisk +# The copy holds the password hash (and any Wi-Fi password): never left behind +cleanup() { rm -f "$conf_file" "$keys_file" "$boot_conf" "$copy" "$sparse"; rm -rf "$ramdisk_dir" "$verify_dir"; } +trap cleanup EXIT ( umask 077 { @@ -684,6 +688,7 @@ if [[ -n $ramdisk_image ]]; then # omamobile-boot-images' commands. The hook runs under pacman's umask # (022), so root/ is 0755; its mode is set here because not every # install(1) ignores the umask for parents (uutils' follows it). + rm -rf "$ramdisk_dir" && mkdir -p "$ramdisk_dir" extra=$ramdisk_dir/extra install -d -m 755 "$extra/root" && install -d -m 700 "$extra/root/.ssh" && install -m 600 "$keys_file" "$extra/root/.ssh/authorized_keys" && @@ -692,7 +697,7 @@ if [[ -n $ramdisk_image ]]; then cpio --help 2>/dev/null | grep -c -- --reproducible >/dev/null && reproducible=(--reproducible) (cd "$extra" && find . -mindepth 1 | LC_ALL=C sort | cpio -o -H newc --owner=0:0 --quiet "${reproducible[@]}") > "$ramdisk_dir/extra.cpio" || die "cpio couldn't pack the owner's key for the initramfs" - cat "$ramdisk_dir/shipped.cpio" "$ramdisk_dir/extra.cpio" | lz4 -l -9 -q > "$ramdisk_dir/initramfs.cpio.lz4" || + cat "$verify_dir/shipped.cpio" "$ramdisk_dir/extra.cpio" | lz4 -l -9 -q > "$ramdisk_dir/initramfs.cpio.lz4" || die "lz4 couldn't recompress $ramdisk_image.img's ramdisk with the owner's key" vendor_ramdisk set "$images/$ramdisk_image.img" "$ramdisk_dir/initramfs.cpio.lz4" "$ramdisk_dir/$ramdisk_image.img" || die "rewriting $ramdisk_image.img with the new ramdisk failed"