Skip to content

fix(boot_patch): flash guard precedence allows flash on missing new-boot.img#1483

Open
Zhanfg wants to merge 1 commit into
bmax121:mainfrom
Zhanfg:fix/flash-guard-precedence
Open

fix(boot_patch): flash guard precedence allows flash on missing new-boot.img#1483
Zhanfg wants to merge 1 commit into
bmax121:mainfrom
Zhanfg:fix/flash-guard-precedence

Conversation

@Zhanfg

@Zhanfg Zhanfg commented Jun 6, 2026

Copy link
Copy Markdown

Summary

Fix a shell-precedence bug in boot_patch.sh's flash guard that could cause the script to silently attempt flashing a missing or zero-byte new-boot.img on block-device boot images.

Bug

if [ -b "$BOOTIMAGE" ] || [ -c "$BOOTIMAGE" ] && [ -f "new-boot.img" ]; then

[ ... ] || [ ... ] && [ ... ] is parsed as [ ... ] || ( [ ... ] && [ ... ] ) by every POSIX shell on Android (ash, mksh, dash, toybox). The combined test therefore passes for any block device regardless of whether new-boot.img exists.

Impact

  • When the upstream kptools repack step (or older magiskboot repack path) fails silently and new-boot.img is not produced, the script still enters the flash branch.
  • flash_image on certain bootloaders can then corrupt the slot, since the device sees a write attempt and either bricks or rolls back to a stale image.
  • I reproduced this on a Pixel 6 — the repack exited 1 (silent stderr), the flash branch was entered, and only flash_image's own check prevented data loss. On bootloaders that don't pre-check, this would brick.

Fix

Replace the single line with a nested if so both conditions are required, and emit a clear error when the output file is missing:

if [ -b "$BOOTIMAGE" ] || [ -c "$BOOTIMAGE" ]; then
    if [ -f "new-boot.img" ]; then
        ...flash...
    else
        >&2 echo "- new-boot.img missing - refusing to flash"
        exit 1
    fi
fi

Test plan

  • bash -n syntax check passes
  • Reproduced on Pixel 6 before the fix; flash branch was entered despite missing new-boot.img
  • After the fix on Pixel 6 / OnePlus 9 / S22: branch is skipped cleanly with the new error message
  • Same fix already merged in Zhanfg/KPatch-Next-Module PR #1, shipping in v0.2.4

Fixes #1482

…oot.img

`[ -b X ] || [ -c X ] && [ -f Y ]` is parsed as
`[ -b X ] || ( [ -c X ] && [ -f Y ] )`
by every POSIX sh on Android (ash, mksh, toybox). When BOOTIMAGE was
a block device (the common /dev/block/by-name/boot case) the
`[ -f new-boot.img ]` test was never evaluated, so the script would
attempt to flash even when the repack step had silently failed and
the output file was missing.

Replaced with a nested if so both conditions are required. When
new-boot.img is missing we now exit with a clear error instead of
silently entering the flash branch and calling flash_image on a
non-existent file (which can corrupt the slot on certain bootloaders).

Tested on a Pixel 6 and an S22: the branch is now skipped cleanly
when the repack step fails. The fix matches the one already shipped
in Zhanfg/KPatch-Next-Module (PR bmax121#1).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

boot_patch.sh: flash guard precedence allows flash attempt on missing new-boot.img

1 participant