From 38c7e1524a94ef16968a165b9a72dec38abd5d96 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Tue, 10 Nov 2026 15:32:23 +0000 Subject: [PATCH] install-phone: stop when cpio can't list the shipped ramdisk The maintainer's follow-up on #596: the key check still read the decompressed ramdisk with "cpio -it ... 2>/dev/null | grep -c ... || true", so an archive that lz4 decodes but cpio can't list was counted as holding no keys, and the key was appended to it. A stand-in that fails only the cpio -it call showed it, and so did a bundle whose ramdisk is a truncated cpio inside valid lz4, run through ./install --unsigned with only fastboot faked: the install appended the key to the broken archive, showed the plan and flashed every partition. The listing is now its own step with its own message ("cpio couldn't list vendor_kernel_boot.img's ramdisk: "), before the plan and before any flash. With --quiet, cpio -it says nothing when it lists; given bytes that aren't an archive, GNU cpio 2.15 prints a "Malformed number " line for every offset it tries (786 lines for about 1 KB of text) and then its reason ("premature end of archive"), so its stderr goes to a file in the ramdisk work directory and the message carries the last line. The key check greps the listing from a here-string with grep -q, so the no-match case needs no "|| true": a ramdisk without authorized_keys still gets the key. The flashed bytes don't change: tests/install-phone-initramfs's byte comparison passes with install under umask 077 (the test's), 022 and 002. Its failing-step test adds cpio -it: before this change it failed at "refuses a failing cpio -it"; with the new die replaced by "|| true" it fails there again. Co-Authored-By: Claude Opus 5.5 --- tests/install-phone-initramfs | 5 +++-- tools/install-phone | 9 ++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/install-phone-initramfs b/tests/install-phone-initramfs index 6f837a861..8d0cb6469 100755 --- a/tests/install-phone-initramfs +++ b/tests/install-phone-initramfs @@ -242,9 +242,10 @@ pass "an image the owner's key makes too large for its partition is refused befo # Each step of the append that fails (a stand-in that exits 1 on that call) # stops the install with a message, rather than flashing the ramdisk without # the key: copying the key, cpio packing it, lz4 decompressing the shipped -# ramdisk, lz4 recompressing +# ramdisk, cpio listing it, lz4 recompressing for step in "install 600 can't stage the owner's key" "cpio -o cpio couldn't pack the owner's key" \ "lz4 -dc lz4 couldn't decompress vendor_kernel_boot.img's ramdisk" \ + "cpio -it cpio couldn't list vendor_kernel_boot.img's ramdisk" \ "lz4 -l lz4 couldn't recompress vendor_kernel_boot.img's ramdisk"; do read -r tool arg why <<<"$step" mkdir -p "$t/bad-$tool" @@ -252,7 +253,7 @@ for step in "install 600 can't stage the owner's key" "cpio -o cpio couldn't pac chmod +x "$t/bad-$tool/$tool" PATH=$t/bad-$tool:$PATH refused "a failing $tool $arg in the key's append" "$why" done -pass "a failing step of the key's append (install, cpio, lz4 -dc 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" # 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" diff --git a/tools/install-phone b/tools/install-phone index 9db28c29a..eaa0ca6a0 100755 --- a/tools/install-phone +++ b/tools/install-phone @@ -634,9 +634,12 @@ if [[ -n $ramdisk_image ]]; then 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" - keyfiles=$(cpio -it --quiet < "$ramdisk_dir/shipped.cpio" 2>/dev/null \ - | grep -cE '^(\./)?root/\.ssh/authorized_keys$' || true) - if [[ $keyfiles != 0 ]]; then + # 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