diff --git a/tests/install-phone-initramfs b/tests/install-phone-initramfs index 8d0cb6469..769dc3ce2 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() { @@ -199,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. @@ -252,14 +277,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 +307,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..1faafc16e 100755 --- a/tools/install-phone +++ b/tools/install-phone @@ -65,8 +65,10 @@ # (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 -# the phone is contacted +# manifest.json, root image and slot B's ramdisk, +# 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 @@ -402,6 +404,75 @@ 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 + +# --- 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) +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(" "$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 < "$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)" exit 0 @@ -519,13 +590,12 @@ 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"; } +cleanup() { rm -f "$conf_file" "$keys_file" "$boot_conf" "$copy" "$sparse"; rm -rf "$ramdisk_dir" "$verify_dir"; } trap cleanup EXIT ( umask 077 @@ -607,73 +677,18 @@ 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 # 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" && @@ -682,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"