From 6bd7b0674da370a2a937454b9907db1d51eec1b7 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Mon, 9 Nov 2026 20:31:27 +0000 Subject: [PATCH 1/2] bin/omamobile, tools, rootfs: no pipe into grep -q under pipefail The follow-up to #575, which fixed the test scripts: the same pattern in the laptop-side scripts that set pipefail. grep -q exits at the first match, a writer still writing gets SIGPIPE, and pipefail fails the pipeline (AGENTS.md section 4, Userspace). Each site now uses grep -c ... >/dev/null, which reads all its input, with the same pattern, flags and truth value. The one that loses in practice is tools/fingerprint-capture's wait loop: sed prints the whole log from the step's start, and with the end line early in a verbose kmsg stream the old check never saw it and the step ran to its timeout. The others have short writers (adb get-state, android-info.txt, 64 bytes of the TA, the keyring, the image's passwd) that fit the pipe buffer before grep exits; they change so that no script keeps the pattern. Two of them failed open: build-inside's user-account check and signing.sh's revoked-key check would have passed on a SIGPIPE. tests/grep-q-pipes now scans bin/, tools/ and rootfs/ as well, plus tools/lib (sourced by pipefail scripts), and skips comment lines. It leaves out five stage tools whose remaining hits are command strings that tools/phone-ssh runs in the phone's shell, which has no pipefail. Nothing under packages/ or initramfs/ changes. Tested: the race harness (old 0/200, new 200/200 per form; the fingerprint-capture loop old 0/100, new 100/100), tests/grep-q-pipes with an injected pipe (fails, names the line) and a commented one (passes), tools/pr-check, tests/all. Co-Authored-By: Claude Opus 5.5 --- bin/omamobile | 4 ++-- rootfs/build-inside | 2 +- tests/all | 2 +- tests/grep-q-pipes | 31 +++++++++++++++++++------------ tools/extract-fingerprint-vendor | 4 ++-- tools/extract-vendor-chroot | 2 +- tools/extract-vendor-firmware | 2 +- tools/fingerprint-capture | 2 +- tools/lib/signing.sh | 4 ++-- 9 files changed, 30 insertions(+), 23 deletions(-) diff --git a/bin/omamobile b/bin/omamobile index 753244013..16802719c 100755 --- a/bin/omamobile +++ b/bin/omamobile @@ -1122,7 +1122,7 @@ cmd_fastboot() { die "fastboot refuses BOOT_METHOD=qcom-abl (panic path uncharacterized on this ABL; use the buttons — see device/$DEVICE/BRINGUP-M1.md)" in_fastboot && { say "Already in fastboot"; return 0; } for i in $(seq 1 12); do - if adb get-state 2>/dev/null | grep -q device; then + if adb get-state 2>/dev/null | grep -c device >/dev/null; then say "Stock Android: adb reboot bootloader" adb reboot bootloader elif "$ssh" true 2>/dev/null; then @@ -1138,7 +1138,7 @@ cmd_fastboot() { local t for t in $(seq 1 40); do in_fastboot && { say "In fastboot"; return 0; } - adb get-state 2>/dev/null | grep -q device && break + adb get-state 2>/dev/null | grep -c device >/dev/null && break "$ssh" true 2>/dev/null && break sleep 2 done diff --git a/rootfs/build-inside b/rootfs/build-inside index 18eb43d32..7766a2b54 100755 --- a/rootfs/build-inside +++ b/rootfs/build-inside @@ -198,7 +198,7 @@ echo omamobile > "$T/etc/hostname" say "Removing per-machine state" # No account but root, and root locked: the user comes from firstboot.conf grep -q '^alarm:' "$T/etc/passwd" && die "an alarm account in the image" -awk -F: '$3 >= 1000 && $3 != 65534 {print $1}' "$T/etc/passwd" | grep -q . && die "a user account in the image" +awk -F: '$3 >= 1000 && $3 != 65534 {print $1}' "$T/etc/passwd" | grep -c . >/dev/null && die "a user account in the image" sed -i 's/^root:[^:]*:/root:!*:/' "$T/etc/shadow" rm -rf "$T/etc/pacman.d/gnupg" # firstboot makes the phone's own keyring rm -f "$T"/etc/ssh/ssh_host_* # sshd makes the phone's own host keys diff --git a/tests/all b/tests/all index 21dd14c4f..efdfafbcc 100755 --- a/tests/all +++ b/tests/all @@ -6,7 +6,7 @@ # against a stand-in phone tree, # tools/phone-install-package against a stand-in phone, the initramfs's # first-boot growth, the bring-up overlays, the simplefb trigger, the init's per-board steps, the storage guard's packed -# allowlist and the USB product string, no test script piping into grep -q, +# allowlist and the USB product string, no pipefail script piping into grep -q, # bin/omamobile's boot-image layouts # (and omamobile-boot-update building the same bytes on the phone) # and flash against a fake fastboot, omamobile-firstboot's own tests, and the diff --git a/tests/grep-q-pipes b/tests/grep-q-pipes index 2f8dbd21b..4e9eaba57 100755 --- a/tests/grep-q-pipes +++ b/tests/grep-q-pipes @@ -1,11 +1,16 @@ #!/bin/bash # SPDX-License-Identifier: MIT -# No test script that sets pipefail pipes into grep -q (AGENTS.md section 4, -# Userspace): grep -q exits at the first match, the writer can get SIGPIPE, -# and pipefail then fails the pipeline, so a check fails at random, or an -# "&& fail" check passes. Use grep -c ... >/dev/null, which reads all its -# input. packages/omamobile-modem/test/nv-overlay is left out until the fix -# for #457 lands. +# No test script, and no laptop-side script in bin/, tools/ or rootfs/, that +# sets pipefail pipes into grep -q (AGENTS.md section 4, Userspace): grep -q +# exits at the first match, the writer can get SIGPIPE, and pipefail then +# fails the pipeline, so a check fails at random, or an "&& fail" check +# passes. Use grep -c ... >/dev/null, which reads all its input. Comment lines +# don't count. Left out: packages/omamobile-modem/test/nv-overlay until the +# fix for #457 lands; and acpm-stage, aoc-stage, fingerprint-capture, +# husky-deadman and wifi-w1-capture in tools/, whose remaining hits are +# command strings run on the phone through tools/phone-ssh, in the phone's +# shell without pipefail. tools/lib's files are sourced by pipefail scripts, +# so they count too. set -euo pipefail cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." fail() { printf 'not ok - %s\n' "$1" >&2; exit 1; } @@ -19,11 +24,13 @@ for good in 'x | grep -c y >/dev/null' 'x || grep -q y f' 'grep -q y <<<"$x"'; d grep -cE "$pattern" <<<"$good" >/dev/null && fail "the pattern leaves '$good' alone" done -mapfile -t scripts < <(git grep -l 'set -[a-z]*o pipefail' -- tests 'packages/*/test' \ - ':(exclude)tests/grep-q-pipes' ':(exclude)packages/omamobile-modem/test/nv-overlay') -[[ ${#scripts[@]} -gt 0 ]] || fail "found the pipefail test scripts" -if hits=$(git grep -nE "$pattern" -- "${scripts[@]}"); then +mapfile -t scripts < <({ git grep -l 'set -[a-z]*o pipefail' -- tests 'packages/*/test' bin tools rootfs \ + ':(exclude)tests/grep-q-pipes' ':(exclude)packages/omamobile-modem/test/nv-overlay' \ + ':(exclude)tools/'{acpm-stage,aoc-stage,fingerprint-capture,husky-deadman,wifi-w1-capture} + git ls-files tools/lib; } | sort -u) +[[ ${#scripts[@]} -gt 0 ]] || fail "found the pipefail scripts" +if hits=$(git grep -nE "$pattern" -- "${scripts[@]}" | grep -vE '^[^:]+:[0-9]+:[[:space:]]*#'); then printf '%s\n' "$hits" >&2 - fail "no test script under pipefail pipes into grep -q (use grep -c ... >/dev/null)" + fail "no script under pipefail pipes into grep -q (use grep -c ... >/dev/null)" fi -pass "no pipe into grep -q in ${#scripts[@]} pipefail test scripts" +pass "no pipe into grep -q in ${#scripts[@]} pipefail scripts" diff --git a/tools/extract-fingerprint-vendor b/tools/extract-fingerprint-vendor index eec9bd40b..7a3c1d279 100755 --- a/tools/extract-fingerprint-vendor +++ b/tools/extract-fingerprint-vendor @@ -150,7 +150,7 @@ fetch_images() { fi m=$(zip_member "$src" "android-info\.txt") if [[ -n "$m" && -z "$FORCE" ]]; then - unzip -p "$src" "$m" | grep -qE "^require board=(.*\|)?$DEVICE(\|.*)?\$" || + unzip -p "$src" "$m" | grep -cE "^require board=(.*\|)?$DEVICE(\|.*)?\$" >/dev/null || die "this image is not for $DEVICE (android-info.txt); use -f to override" fi for part in vendor system; do @@ -339,7 +339,7 @@ done < <(find "$root" -type l) # Sanity checks: the TA is a signed Trusty app; the HAL is the Goodix one. ta=g7.app; [[ $DEVICE == tegu ]] && ta=g7a.app -head -c 64 "$root/vendor/firmware/$ta" | grep -aq TrustyApp || +head -c 64 "$root/vendor/firmware/$ta" | grep -ac TrustyApp >/dev/null || die "$ta does not look like a Trusty application package" grep -aq 'g7_trusty' "$root/vendor/bin/hw/android.hardware.biometrics.fingerprint-service.goodix" || die "the fingerprint HAL is not the Goodix G7 one" diff --git a/tools/extract-vendor-chroot b/tools/extract-vendor-chroot index ef41bda73..088d4caa7 100755 --- a/tools/extract-vendor-chroot +++ b/tools/extract-vendor-chroot @@ -246,7 +246,7 @@ fetch_images() { fi m=$(zip_member "$src" "android-info\.txt") if [[ -n "$m" && -z "$FORCE" ]]; then - unzip -p "$src" "$m" | grep -qE "^require board=(.*\|)?$DEVICE(\|.*)?\$" || + unzip -p "$src" "$m" | grep -cE "^require board=(.*\|)?$DEVICE(\|.*)?\$" >/dev/null || die "this image is not for $DEVICE (android-info.txt); use -f to override" fi for part in vendor system; do diff --git a/tools/extract-vendor-firmware b/tools/extract-vendor-firmware index 702c77b0f..f8d3010e9 100755 --- a/tools/extract-vendor-firmware +++ b/tools/extract-vendor-firmware @@ -316,7 +316,7 @@ fetch_vendor() { fi m=$(zip_member "$src" "android-info\.txt") if [[ -n "$m" && -z "$FORCE" ]]; then - unzip -p "$src" "$m" | grep -qE "^require board=(.*\|)?$DEVICE(\|.*)?\$" || + unzip -p "$src" "$m" | grep -cE "^require board=(.*\|)?$DEVICE(\|.*)?\$" >/dev/null || die "this image is not for $DEVICE (android-info.txt); use -f to override" fi m=$(zip_member "$src" "vendor\.img") diff --git a/tools/fingerprint-capture b/tools/fingerprint-capture index 1f6c640eb..8d2bd2158 100755 --- a/tools/fingerprint-capture +++ b/tools/fingerprint-capture @@ -125,7 +125,7 @@ since_start() { sed -n "/fingerprint-capture: start $step/,\$p" "$log"; } result= while (( $(date +%s) - start < wait_s )); do sleep 1 - if since_start | grep -Eq "$done_re"; then result=done; break; fi + if since_start | grep -Ec "$done_re" >/dev/null; then result=done; break; fi done wait "$step_pid" 2>/dev/null sleep 2 diff --git a/tools/lib/signing.sh b/tools/lib/signing.sh index cee223f31..705190e63 100644 --- a/tools/lib/signing.sh +++ b/tools/lib/signing.sh @@ -36,11 +36,11 @@ signing_key_load() { echo "error: keys/fingerprint.release does not hold a 40-digit fingerprint" >&2 return 1 fi - if ! cut -d: -f1 "$keyring/omamobile-trusted" 2>/dev/null | grep -qx "$fpr"; then + if ! cut -d: -f1 "$keyring/omamobile-trusted" 2>/dev/null | grep -cx "$fpr" >/dev/null; then echo "error: omamobile-keyring does not trust $fpr (keys/fingerprint.release): phones would refuse what it signs" >&2 return 1 fi - if cut -d: -f1 "$keyring/omamobile-revoked" 2>/dev/null | grep -qx "$fpr"; then + if cut -d: -f1 "$keyring/omamobile-revoked" 2>/dev/null | grep -cx "$fpr" >/dev/null; then echo "error: $fpr (keys/fingerprint.release) is revoked in omamobile-keyring" >&2 return 1 fi From a81fcf331a28799cdab840fd54e3cdc438bda298 Mon Sep 17 00:00:00 2001 From: Derek Ross Date: Mon, 9 Nov 2026 20:32:54 -0500 Subject: [PATCH 2/2] tests/grep-q-pipes: scan the package tests too, and the two pipes they held The scan's 'packages/*/test' pathspec matched no file, so no package test was ever scanned (91 scripts). With 'packages/*/test/*' it reads 164, and found two pipes into grep -q that landed since: device-husky's speaker_protection check (#453) and omamobile-waydroid's render-node check (#541). Both now read the whole pipe. Co-Authored-By: Claude Fable 5.2 --- packages/omamobile-device-husky/test/all | 2 +- packages/omamobile-waydroid/test/all | 2 +- tests/grep-q-pipes | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/omamobile-device-husky/test/all b/packages/omamobile-device-husky/test/all index e2178e524..eb986ae82 100755 --- a/packages/omamobile-device-husky/test/all +++ b/packages/omamobile-device-husky/test/all @@ -166,7 +166,7 @@ grep -q '^ConditionPathExists=/sys/bus/platform/devices/154b0000.aoc$' vendor/sy [[ $(readlink "$tmp/root/usr/lib/systemd/system/multi-user.target.wants/omamobile-husky-aoc.service") == ../omamobile-husky-aoc.service ]] \ || fail "the AoC unit is enabled by a vendor wants link" ! grep -qE '^install ' vendor/modprobe.d/omamobile-husky-aoc.conf || fail "the AoC file only blacklists" -! grep -hv '^ *#' bin/omamobile-husky-aoc-start vendor/modprobe.d/omamobile-husky-aoc.conf | grep -q 'speaker_protection' \ +! grep -hv '^ *#' bin/omamobile-husky-aoc-start vendor/modprobe.d/omamobile-husky-aoc.conf | grep -c 'speaker_protection' >/dev/null \ || fail "the AoC start never raises the speaker ceiling" for m in gsa aoc_core aoc_alsa_dev snd_soc_cs35l41_spi gs201_aoc_boot; do grep -qx "blacklist $m" vendor/modprobe.d/omamobile-husky-aoc.conf || fail "$m is kept from autoloading" diff --git a/packages/omamobile-waydroid/test/all b/packages/omamobile-waydroid/test/all index 180ed7bd6..759eccaa4 100755 --- a/packages/omamobile-waydroid/test/all +++ b/packages/omamobile-waydroid/test/all @@ -74,7 +74,7 @@ if CONFIG_FILE=$t/config LXC_CONF=$t/old-binder.conf RENDER_RDEV=226:128 lib/oma fail "check passes with a binder major instead of the binder path" fi # The render node: its major and minor must be the ones the conf allows -CONFIG_FILE=$t/config LXC_CONF=lib/lxc-omamobile.conf RENDER_RDEV=226:128 lib/omamobile-waydroid-check | grep -q '^ok render node 226:128 is allowed' \ +CONFIG_FILE=$t/config LXC_CONF=lib/lxc-omamobile.conf RENDER_RDEV=226:128 lib/omamobile-waydroid-check | grep -c '^ok render node 226:128 is allowed' >/dev/null \ || fail "check does not report the render node" if CONFIG_FILE=$t/config LXC_CONF=lib/lxc-omamobile.conf RENDER_RDEV=226:129 lib/omamobile-waydroid-check > "$t/binder.log"; then fail "check passes when the render node is not the allowed one" diff --git a/tests/grep-q-pipes b/tests/grep-q-pipes index 4e9eaba57..b14991690 100755 --- a/tests/grep-q-pipes +++ b/tests/grep-q-pipes @@ -24,7 +24,7 @@ for good in 'x | grep -c y >/dev/null' 'x || grep -q y f' 'grep -q y <<<"$x"'; d grep -cE "$pattern" <<<"$good" >/dev/null && fail "the pattern leaves '$good' alone" done -mapfile -t scripts < <({ git grep -l 'set -[a-z]*o pipefail' -- tests 'packages/*/test' bin tools rootfs \ +mapfile -t scripts < <({ git grep -l 'set -[a-z]*o pipefail' -- tests 'packages/*/test/*' bin tools rootfs \ ':(exclude)tests/grep-q-pipes' ':(exclude)packages/omamobile-modem/test/nv-overlay' \ ':(exclude)tools/'{acpm-stage,aoc-stage,fingerprint-capture,husky-deadman,wifi-w1-capture} git ls-files tools/lib; } | sort -u)