From ec391b7ccd05141b9f4db18e7518d1a6e8b4b31b Mon Sep 17 00:00:00 2001 From: Derek Ross Date: Sat, 7 Nov 2026 11:00:08 -0500 Subject: [PATCH 1/2] initramfs: guard the boot LUNs as the main LUN, partition flags only (#252) The kernel counts a partition read-only when its own flag or its disk's is set (bdev_read_only). The guard removed /dev/sda and left sda's flag clear, but set the boot LUNs' (sdb, sdc, sdd) disk flags and kept their whole-disk nodes, so writing any one boot-LUN partition (devinfo) meant clearing the disk flag, and while it was clear /dev/sdd reached every partition on the LUN. #483 found it; the maintainer's ruling on #252: guard the boot LUNs the way the main LUN is guarded. Every LUN is now guarded the same way: its whole-disk node removed (a node that won't go is retried, warned about and reported), its own flag never set, each partition's own flag set except the allowlist, which still opens partitions of sda only. The hand-over check also refuses Arch while any whole-disk node is there. Nothing becomes writable that wasn't, and a tool can open one partition by clearing that partition's own flag alone. omamobile-boot-success (device-cheetah 0.1.45, raven 0.1.33, lynx 0.2.3) ran blockdev --setrw on /dev/sdd first; under the new guard that node is gone and the marker fails whenever slot B needs marking. It now clears devinfo's own flag alone when the disk flag is clear, and keeps the old disk-then-partition sequence when an older initramfs set the disk flag, so a fastboot rollback to an older image set still marks slot B. push, omamobile-boot-update, nv-backup, phone-provision, omamobile-fp and phone-smoke use partitions found by PARTNAME and never a whole-disk node. Tested without a phone or a build: tests/storage-guard (fake sysfs whose ro files follow the own-or-disk rule: every disk node gone and its flag clear, each boot-LUN partition's own flag set, the allowlist unchanged and sda-only, devinfo's own flag opening devinfo alone with no disk flag, a returning or stuck node and a late LUN), tests/initramfs-steps, sh -n, and the new packages/omamobile-device-cheetah/test/boot-success (new and old guard, a flag that won't clear, a failed write, a non-LUN devinfo; it fails on the old marker), also run on raven's and lynx's markers. Ten mutations of the guard and five of the marker were each caught. Never booted: device/cheetah/STORAGE-GUARD-PLAN.md is the runbook for the 7 Pro. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- CONTRIBUTING.md | 10 +- device/cheetah/STORAGE-GUARD-PLAN.md | 127 +++++++++++++++ initramfs/init | 44 ++++-- .../bin/omamobile-boot-success | 37 ++++- packages/omamobile-device-cheetah/test/all | 4 + .../test/boot-success | 144 ++++++++++++++++++ packages/omamobile-device-cheetah/version | 2 +- .../bin/omamobile-boot-success | 37 ++++- packages/omamobile-device-lynx/version | 2 +- .../bin/omamobile-boot-success | 37 ++++- packages/omamobile-device-raven/test/all | 10 ++ packages/omamobile-device-raven/version | 2 +- tests/storage-guard | 133 ++++++++++++---- 14 files changed, 520 insertions(+), 71 deletions(-) create mode 100644 device/cheetah/STORAGE-GUARD-PLAN.md create mode 100755 packages/omamobile-device-cheetah/test/boot-success diff --git a/AGENTS.md b/AGENTS.md index cb6441f2d..43f9d96ed 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -12,7 +12,7 @@ Omarchy Mobile runs native Omarchy (Arch Linux ARM, Hyprland, Omarchy's packages - **Only slot B and `userdata` are ours.** Write only `boot_b`, `init_boot_b`, `vendor_boot_b`, `vendor_kernel_boot_b` and `dtbo_b`, and only through `bin/omamobile flash|push` or, on a phone whose `/etc/omamobile/boot-update.conf` says `AUTO_WRITE=yes` (only `tools/install-phone` writes it, for a release install, or a human by hand in an approved runbook), `linux-omamobile`'s pacman hook `omamobile-boot-update` ([RELEASES](docs/RELEASES.md) §5.3, §12 decisions 3 and 10): it writes only `_b` for the descriptor's `BOOT_IMAGES`, each found by GPT name on the main LUN, on `UFS_RW` and left `ro` 0 by the storage guard, read back by sha256, and never reboots. A development phone has no such file, so `push` stays its only writer. Never touch bootloader, radio, `devinfo`, `persist`, `efs*`, `modem*` or `vbmeta`, and never run an unsuffixed `fastboot flash`, with one exception: `userdata`, and only through `tools/install-phone` (serial pinned, codename typed, allowlist, dry run first). Slot A's stock Android is the way back. - **Qualcomm A/B phones (Pixel 4 family) keep the slot flags in GPT attributes,** so slot B can only be marked bootable by writing the partition table. That write is a narrow, named exception, never automatic: by hand, once per install of slot B's boot image, and only for a device whose entry in [CONTRIBUTING](CONTRIBUTING.md) §3.1 names the LUN, the four sectors (LBAs 1, 2, last-1 and last), the attribute bit and the CRCs it rewrites. That device's plan must also record the slot flags before and after, the rollback for a corrupted GPT, and the way back: stock restored on slot A first, or the owner's explicit acceptance of buttons-only recovery. The storage guard can't block this write, so it never runs from a service or at boot. - **Linux never writes the modem's NV partitions** (`efs`, `efs_backup`, `modem_userdata`, `persist`, `modem_a/b`). The modem's own NV writes persist only in its per-phone overlay on userdata (`/var/lib/omamobile/modem-nv-overlay`, `omamobile-modem` 0.1.15+, off until `/etc/omamobile/modem-nv-overlay-enable` exists), only for the files on the overlay's allow-list (`efs/nv_normal.bin` and its checksum), never for identity or calibration files (`nv_protected.*`, all of `efs_backup`), and only while bound to the checksums in `/etc/omamobile/modem-nv.sha256`, so a new backup sets it aside. `omamobile-modem nv-overlay reset`, then a reboot, returns the modem to the backup alone. Don't add a file to the allow-list without the human's say-so, never write or edit the overlay by hand, and never commit it or copy it to another phone: it is that phone's NV. [MODEM-PLAN](device/cheetah/MODEM-PLAN.md) "NV overlay" -- **Never weaken the storage guard.** Don't `blockdev --setrw` a partition or recreate `/dev/sda`. Don't mount Android partitions read-write, and don't add names to the `UFS_RW` allowlist without the human's say-so. The guard is what keeps a bad command from erasing the IMEI. +- **Never weaken the storage guard.** Don't `blockdev --setrw` a partition or recreate a LUN's whole-disk node (`/dev/sda`, or a boot LUN's `/dev/sdb`-`/dev/sdd`). Don't mount Android partitions read-write, and don't add names to the `UFS_RW` allowlist without the human's say-so. The guard is what keeps a bad command from erasing the IMEI. - **Check backups before radio, PMIC, AoC or Trusty work.** Run the checksum check of `efs`, `efs_backup`, `modem_userdata`, `persist` and `modem_a/b` against the off-repo backup first ([MODEM-PLAN.md](device/cheetah/MODEM-PLAN.md) M0.1). If there's no backup, stop. Find each partition by `PARTNAME` in `/sys/class/block/*/uevent`: the Arch root has no `/dev/disk/by-partlabel`. **Take a fresh read-only backup after any stock Android boot** (a slot A fallback counts): stock's modem stack writes `efs`, `persist` and `modem_userdata`, so the old checksums stop matching and the next check can't tell a change of ours from stock's. - **Never read or write PMU registers from userspace** (no `devmem`, no `debugfs/regmap/*pmu*/registers`). PMU writes from userspace are silently ignored, and one read of a PMU G3D register crashed the phone. Use the kernel's SMC-backed `exynos-pmu` regmap. - **`devmem` is `devmem ADDR [VALUE]`.** A second argument is written, so `devmem ADDR 32` writes 0x20. This once clobbered live DSIM timing registers. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 43d63d13d..49956a659 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -77,11 +77,11 @@ Keeping stock on slot A is how we avoid bricking phones during development, not UFS holds the per-device calibration. The initramfs ([`initramfs/init`](initramfs/init)) guards it before anything else runs: -- It sets every SCSI disk and partition read-only (`blockdev --setro`) as soon as it appears, and keeps a watcher running for the life of PID 1. -- The only exceptions are an allowlist of GPT partition names: the device's `BOOT_IMAGES` on slot B, and `userdata`. -- It deletes the whole-disk node of the main LUN (`/dev/sda`), so nothing can write across partitions. The boot LUNs stay read-only at the disk level. -- In Arch, [`10-omamobile-storage.rules`](packages/omamobile-device-cheetah/vendor/udev/rules.d/10-omamobile-storage.rules) keeps the whole-disk node unusable and hides the Android partitions from desktop tools. -- The one write outside the allowlist is the boot-success marker (`omamobile-boot-success`). It opens exactly `devinfo` for a single verified write of the slot flag bytes, then sets it read-only again. +- It sets every SCSI partition's own read-only flag (`blockdev --setro`) as soon as it appears, and keeps a watcher running for the life of PID 1. +- The only exceptions are an allowlist of GPT partition names on the main LUN (`sda`): the device's `BOOT_IMAGES` on slot B, and `userdata`. +- It deletes the whole-disk node of every LUN (`/dev/sda`, and the boot LUNs' `/dev/sdb`, `/dev/sdc`, `/dev/sdd`), so nothing can write across partitions, and leaves the disks' own flags clear. The kernel counts a partition read-only when its own flag or its disk's is set (`bdev_read_only()`), so a disk flag would hide every partition on that LUN, and opening one would mean opening all of them through the whole-disk node ([#252](https://github.com/derekross/omarchy-mobile/issues/252)). Before handing over to Arch it stays in the initramfs if any whole-disk node is still there or any partition outside the allowlist is writable. +- In Arch, [`10-omamobile-storage.rules`](packages/omamobile-device-cheetah/vendor/udev/rules.d/10-omamobile-storage.rules) keeps the main LUN's whole-disk node unusable and hides the Android partitions from desktop tools. +- The one write outside the allowlist is the boot-success marker (`omamobile-boot-success`). It clears `devinfo`'s own flag alone for a single verified write of the slot flag bytes, then sets it read-only again; no disk flag is touched (under an initramfs from before #252, which still sets the disk flag, it opens the disk too, as it always did). **A new device gets this guard before its UFS driver is ever enabled.** Check the device's LUN layout and partition names first, because the allowlist works by name. diff --git a/device/cheetah/STORAGE-GUARD-PLAN.md b/device/cheetah/STORAGE-GUARD-PLAN.md new file mode 100644 index 000000000..1ed1aecec --- /dev/null +++ b/device/cheetah/STORAGE-GUARD-PLAN.md @@ -0,0 +1,127 @@ + +# The storage guard on the boot LUNs + +Status (2026-11-07): **written, never booted.** Branch `guard-boot-luns` in this repository (no kernel change, no kernel branch). It needs the runbook below on the reference Pixel 7 Pro before it merges, and blocks #483. + +## 1. What changes, and why + +[#252](https://github.com/derekross/omarchy-mobile/issues/252), the maintainer's ruling of 2026-11-07: guard the boot LUNs the way the main LUN is already guarded. + +The kernel counts a partition read-only when its own flag or its disk's flag is set (`bdev_read_only()`). Until now the guard in `initramfs/init` removed `/dev/sda` and left `sda`'s own flag clear, but set the boot LUNs' (`sdb`, `sdc`, `sdd`) **disk** flags and kept their whole-disk nodes. So a partition on a boot LUN could only be written with its disk's flag cleared too, and while it was clear, `/dev/sdd` reached every partition on that LUN (`pinfo`, `blenv`, `mfg_data` beside `devinfo`). #483 found this while writing the 8 Pro and 9a marking tool. + +Now every LUN is guarded the same way: + +- every whole-disk node is removed (`/dev/sda` to `/dev/sdd`), and the disks' own flags are never set; +- every partition gets its own flag, except the allowlist (`UFS_RW`), which still opens partitions of `sda` only; +- a node that won't go is retried and warned about, and the hand-over check (`ufs_unguarded`) now also refuses Arch while any whole-disk node is there, as it already did for a writable partition outside the allowlist. + +Nothing becomes writable that wasn't: the boot LUNs' partitions are read-only by their own flags instead of their disks'. A tool that must write one of them clears that partition's own flag alone. + +**`omamobile-boot-success` changes with it** (device-cheetah 0.1.45, raven 0.1.33, lynx 0.2.3). The marker ran `blockdev --setrw` on the whole bootloader LUN (`/dev/sdd`), then on `devinfo`. Under the new guard `/dev/sdd` is gone, so that first call fails and the marker fails (`packages/omamobile-device-cheetah/test/boot-success` shows it against the old script). It runs at every boot but writes only when slot B isn't marked yet, so a marked phone wouldn't notice until the next `flash` or `--set-active=b`. The new marker reads the disk's own flag: clear (the new guard) means it clears `devinfo`'s own flag alone and sets it again after; set (an initramfs from before this change) means it opens the disk and `devinfo` as it always did, so a fastboot rollback to an older image set still gets slot B marked. + +`bin/omamobile push`, `omamobile-boot-update`, `tools/nv-backup`, `tools/phone-provision`, `omamobile-fp` and `tools/phone-smoke` work on partitions found by `PARTNAME` (and `push` and `boot-update` on `sda` alone) and never open a whole-disk node; none changes. + +## 2. Runbook + +Read-only steps first. Each step: commands, Expect, Pass, **Stop**, Rollback, risk. Laptop steps run in this branch's worktree; phone steps over `tools/phone-ssh` (root). Record results in §3 with the date and `uname -r`. Announce to Derek before every step marked "affects the phone". + +This runbook uses `bin/omamobile push` and keeps the running kernel: only the initramfs changes. If the phone runs a packaged kernel (`uname -r` without `-push1`), stop and ask: the images then come from the device package's boot files instead. + +### R1. The guard and the marker as they are (read-only; risk none) + +```sh +tools/phone-ssh 'uname -r; grep slot_suffix /proc/bootconfig; pacman -Q omamobile-device-cheetah + for d in /sys/block/sd*; do n=${d##*/}; echo "$n disk-ro=$(cat $d/ro) node=$([ -e /dev/$n ] && echo present || echo gone)"; done + for s in /sys/block/sd*/sd*; do [ "$(cat $s/ro)" = 0 ] && echo "writable: ${s##*/} $(sed -n s/^PARTNAME=//p $s/uevent)"; done + d=$(grep -lx PARTNAME=devinfo /sys/block/sd*/sd*/uevent); d=${d%/uevent}; echo "devinfo ${d##*/}" + omamobile-bootctl dump /dev/${d##*/} | sed -n "/^slot b /,/retry-count/p" + journalctl -b -u omamobile-boot-success --no-pager | tail -2' +``` + +- Expect: a push1 release, slot `_b`; `sda disk-ro=0 node=gone`, `sdb`, `sdc`, `sdd` `disk-ro=1 node=present` (the old guard); writable exactly `boot_b`, `init_boot_b`, `vendor_boot_b`, `vendor_kernel_boot_b`, `dtbo_b`, `userdata`; slot b `successful: yes`. +- Pass: all of it. **Stop:** anything else writable, slot b not marked (find out why first), a packaged kernel (see above). +- Rollback: none. + +### R2. The laptop's known-good copy of slot B (read-only on the phone; risk none) + +As [KERNEL-UPDATE-PLAN](KERNEL-UPDATE-PLAN.md) R2, with `OUT` the output directory the running push1 kernel came from and `KG=~/omamobile-kernel/known-good/`: copy `$OUT/images/{boot,init_boot,vendor_boot,vendor_kernel_boot,dtbo}.img` and `DEVICE` into `$KG/images`, and compare each with slot B on the phone. + +- Pass: five equal pairs. **Stop:** any difference (there is no known-good set to go back to). +- Rollback: none. + +### L1. This branch's initramfs and images on the running kernel (laptop, no phone; risk none) + +```sh +KERNEL_OUT=$OUT bin/omamobile initramfs +KERNEL_OUT=$OUT bin/omamobile images +lz4 -dc $OUT/initramfs.cpio.lz4 | grep -ac 'ufs_is_disk' +``` + +- Expect: the images build; the last line is 3 or more (the new guard is in the image); the kernel and modules are the running ones, so `push` skips `modules-push`. +- Pass: both. **Stop:** a build error, or 0. +- Rollback: copy `$KG/images/*.img` back into `$OUT/images/`. + +### P1. The new marker, under the old initramfs (affects the phone: root file system only; risk low) + +```sh +tools/phone-install-package packages/omamobile-device-cheetah # 0.1.45 +tools/phone-ssh 'pacman -Q omamobile-device-cheetah; omamobile-boot-success; echo exit $?' +``` + +- Expect: `0.1.45`; `slot b already marked successful`, `exit 0`; no flag touched. +- Pass: both. **Stop:** anything else. +- Rollback: reinstall 0.1.44 from the previous checkout. + +### P2. Push the images (affects the phone: reboot; risk medium) + +```sh +OMAMOBILE_SSH_SHARE=1 KERNEL_OUT=$OUT bin/omamobile push +``` + +- Expect: five `_b` writes, each verified; the phone reboots into the same `uname -r` within about a minute. +- **Stop:** the phone doesn't come back over USB in 3 minutes, or stays in the initramfs (`ssh` answers but `/proc/1/comm` is not `systemd`): read `dmesg | grep ufs-ro` from the initramfs shell first, then roll back. +- Rollback: `cp $KG/images/*.img $OUT/images/ && KERNEL_OUT=$OUT bin/omamobile push`. If nothing answers: Power held about 30 s, then Volume Down + Power about 20 s for fastboot, and `KERNEL_OUT=$KG bin/omamobile flash` (it clears slot B's mark; the old initramfs's guard sets the disk flag, and the 0.1.45 marker's old-guard path marks B again at that boot). + +### R3. The guard after the boot (read-only; risk none) + +```sh +tools/phone-ssh 'uname -r + for d in /sys/block/sd*; do n=${d##*/}; echo "$n disk-ro=$(cat $d/ro) node=$([ -e /dev/$n ] && echo present || echo gone)"; done + for s in /sys/block/sd*/sd*; do [ "$(cat $s/ro)" = 0 ] && echo "writable: ${s##*/} $(sed -n s/^PARTNAME=//p $s/uevent)"; done + echo "partitions: $(ls -d /sys/block/sd*/sd* | wc -l), read-only: $(cat /sys/block/sd*/sd*/ro | grep -c 1)" + dmesg | grep -c "ufs-ro: /dev/sd[a-z]*: whole-disk node removed"; dmesg | grep "ufs-ro: FAILED" + journalctl -b -u omamobile-boot-success --no-pager | tail -2 + d=$(grep -lx PARTNAME=devinfo /sys/block/sd*/sd*/uevent); d=${d%/uevent} + omamobile-bootctl dump /dev/${d##*/} | sed -n "/^slot b /,/retry-count/p"' +tools/phone-smoke +``` + +- Expect: the same release; every disk `disk-ro=0 node=gone`; writable exactly the six names of R1; read-only equals the partition count minus six; `4` removal lines and no FAILED line; the marker's `slot b already marked successful`; slot b `successful: yes`; `phone-smoke`'s required checks ok (its storage guard check among them). +- Pass: all of it. **Stop:** a whole-disk node present, a disk flag set, anything else writable, a FAILED line, the marker failed, a smoke failure: roll back as in P2. +- Rollback: as in P2. + +### R4. The window #483 needs, without a write (affects nothing written; risk low) + +Clears `devinfo`'s own flag for a moment, as the marker does at a boot with slot B unmarked, and sets it again. Nothing is written. Derek's call. + +```sh +tools/phone-ssh 'set -e; d=$(grep -lx PARTNAME=devinfo /sys/block/sd*/sd*/uevent); d=${d%/uevent}; p=${d##*/}; k=$(basename ${d%/*}) + trap "blockdev --setro /dev/$p" EXIT + blockdev --setrw /dev/$p + echo "devinfo $p ro=$(cat $d/ro); disk $k ro=$(cat /sys/block/$k/ro); siblings read-only: $(for s in /sys/block/$k/$k*; do [ $s = $d ] || cat $s/ro; done | tr -d "\n")" + blockdev --setro /dev/$p; echo "devinfo ro=$(cat $d/ro)"' +``` + +- Expect: `devinfo ro=0`, `disk ro=0`, every sibling `1`; then `devinfo ro=1`. +- Pass: both lines. **Stop:** a sibling at 0, or `devinfo` still 0 at the end (set it with `blockdev --setro` by hand, then stop). +- Rollback: `blockdev --setro` on the `devinfo` partition (the trap does it). + +A real mark under the new guard needs slot B unmarked, which only `flash` or `--set-active=b` does; it is not part of this runbook. Its first occasion (the next `flash` of the 7 Pro) checks `journalctl -b -u omamobile-boot-success` for `slot b marked successful`. + +### After the merge + +Restage the boot files for every device (`tools/stage-boot-files`) and release their device packages, so each phone's next boot image carries the new guard; and install device-cheetah, raven and lynx's new markers with or before them. Then #483 rebases onto it, its window `devinfo` alone. + +## 3. Results + +None yet. diff --git a/initramfs/init b/initramfs/init index 36cd2d946..2c09c880c 100755 --- a/initramfs/init +++ b/initramfs/init @@ -47,12 +47,16 @@ fi ufs_ro_log() { echo "omamobile: ufs-ro: $*" > /dev/kmsg; } # Storage guard. Everything on the UFS is read-only except an allowlist of # partitions we own: slot B's boot images (in-place kernel updates) and -# userdata (the Arch root). The kernel only lets a partition be written if -# its whole disk is writable too, so the main disk (sda) stays writable but -# its whole-disk node is removed, so nothing can write across partitions by -# accident. The boot LUNs (sdb, sdc, sdd: bootloader images) are read-only -# at the disk level. Calibration partitions (efs, persist, modem_userdata...) -# and all of slot A stay read-only. +# userdata (the Arch root). The kernel counts a partition read-only when its +# own flag or its disk's flag is set (bdev_read_only), so every LUN is +# guarded the same way: its whole-disk node is removed, so nothing can write +# across partitions, the disk's own flag is left clear, and each partition +# gets its own read-only flag. The main LUN (sda) and the boot LUNs (sdb, +# sdc, sdd: bootloader images, devinfo) differ only in that the allowlist +# can name partitions of sda alone. With the disk flags clear, a tool that +# must write one boot-LUN partition (omamobile-boot-success, devinfo) clears +# that partition's own flag and nothing else (#252). Calibration partitions +# (efs, persist, modem_userdata...) and all of slot A stay read-only. # The allowlist is the descriptor's UFS_RW, which bin/omamobile initramfs # checks and packs into /etc/omamobile-ufs-rw. It fails closed: a missing, # empty or malformed file leaves every partition read-only, so Arch does not @@ -92,6 +96,8 @@ ufs_rw_load() { ufs_rw_load mkdir -p /run/ufs-ro ufs_partname() { sed -n 's/^PARTNAME=//p' "$1/uevent" 2>/dev/null; } +# A whole disk is /sys/block/, a partition /sys/block// +ufs_is_disk() { case "${1#/sys/block/}" in */*) return 1 ;; esac; } ufs_ro_pass() { local s ro name part disk for s in /sys/block/sd* /sys/block/sd*/sd*; do @@ -99,10 +105,19 @@ ufs_ro_pass() { name=${s##*/} [ -e "/run/ufs-ro/$name" ] && continue [ -b "/dev/$name" ] || continue # devtmpfs node not there yet: next pass - if [ "$name" = sda ]; then - rm -f /dev/sda + if ufs_is_disk "$s"; then + # A whole disk (LUN): remove its node, never set its flag + rm -f "/dev/$name" 2>/dev/null + if [ -e "/dev/$name" ]; then + if [ ! -e "/run/ufs-ro/$name.failed" ]; then + : > "/run/ufs-ro/$name.failed" # keep retrying, but log once + ufs_ro_log "FAILED to remove the whole-disk node /dev/$name" + echo "omamobile: WARNING: /dev/$name is NOT removed" > /dev/tty0 2>/dev/null + fi + continue + fi : > "/run/ufs-ro/$name" - ufs_ro_log "/dev/sda: whole-disk node removed (partitions guarded individually)" + ufs_ro_log "/dev/$name: whole-disk node removed (partitions guarded individually)" continue fi part=$(ufs_partname "$s") @@ -124,14 +139,17 @@ ufs_ro_pass() { fi done } -# Prints every disk or partition that is writable but not on the allowlist -# (sda itself excepted: its node is removed). Empty means the guard holds. +# Prints every whole-disk node still present and every partition that is +# writable but not on the allowlist. Empty means the guard holds. ufs_unguarded() { local s name part ro for s in /sys/block/sd* /sys/block/sd*/sd*; do [ -r "$s/ro" ] || continue name=${s##*/} - [ "$name" = sda ] && continue + if ufs_is_disk "$s"; then + [ -b "/dev/$name" ] && echo "$name (whole-disk node)" + continue + fi read -r ro < "$s/ro" [ "$ro" = 0 ] || continue part=$(ufs_partname "$s") @@ -320,7 +338,7 @@ grep -q omamobile.debug /proc/cmdline && { # UFS probe report when the cmdline has omamobile.ufs-probe: wait for the # disks to settle, then list them and the GPT partition names. Read-only: it -# only reads /proc and sysfs, after the guard above has marked the disks ro. +# only reads /proc and sysfs, after the guard above has marked the partitions ro. grep -q omamobile.ufs-probe /proc/cmdline && ( last=-1 for i in $(seq 1 30); do diff --git a/packages/omamobile-device-cheetah/bin/omamobile-boot-success b/packages/omamobile-device-cheetah/bin/omamobile-boot-success index 58514e116..34946af88 100755 --- a/packages/omamobile-device-cheetah/bin/omamobile-boot-success +++ b/packages/omamobile-device-cheetah/bin/omamobile-boot-success @@ -5,8 +5,10 @@ # retry each boot and eventually abandons the slot. # # devinfo sits on a bootloader LUN (sdd) that the storage guard keeps -# read-only. Open exactly that disk and partition for the one write, then -# close them again. omamobile-bootctl only ever changes the slot flag bytes +# read-only. The guard removes the LUN's whole-disk node, leaves the disk's +# own flag clear and sets each partition's own flag (#252), so clearing +# devinfo's own flag opens devinfo alone, for the one write; it is set +# again afterwards. omamobile-bootctl only ever changes the slot flag bytes # (0x30-0x37) and verifies the whole partition afterwards. set -euo pipefail @@ -18,16 +20,37 @@ for s in /sys/block/sd*/sd*; do grep -qx PARTNAME=devinfo "$s/uevent" 2>/dev/null && dev=/dev/${s##*/} done [[ -n "$dev" ]] || { echo "devinfo partition not found"; exit 1; } -disk=/dev/$(basename "$(dirname "$(readlink -f "/sys/class/block/${dev#/dev/}")")") +part=${dev#/dev/} +disk=$(basename "$(dirname "$(readlink -f "/sys/class/block/$part")")") +[[ $disk == sd* && $disk != "$part" && -r /sys/block/$disk/$part/ro ]] \ + || { echo "devinfo ($part) is not a partition of a UFS LUN"; exit 1; } if omamobile-bootctl dump "$dev" | sed -n "/^slot $slot /,/retry-count/p" | grep -q 'successful: yes'; then echo "slot $slot already marked successful" exit 0 fi -relock() { blockdev --setro "$dev"; blockdev --setro "$disk"; } -trap relock EXIT -blockdev --setrw "$disk" -blockdev --setrw "$dev" +part_ro() { cat "/sys/block/$disk/$part/ro"; } +if [[ $(<"/sys/block/$disk/ro") == 0 ]]; then + # The guard as it is: only devinfo's own flag is cleared. + relock() { + blockdev --setro "$dev" + [[ $(part_ro) == 1 ]] || echo "WARNING: $dev ($part) is still writable" + } + trap relock EXIT + blockdev --setrw "$dev" +else + # An initramfs from before #252 still sets the disk's own flag, which + # also keeps devinfo read-only: open the disk too, as before, until the + # phone boots an initramfs with the new guard. + relock() { + blockdev --setro "$dev"; blockdev --setro "/dev/$disk" + [[ $(part_ro) == 1 ]] || echo "WARNING: $dev ($part) is still writable" + } + trap relock EXIT + blockdev --setrw "/dev/$disk" + blockdev --setrw "$dev" +fi +[[ $(part_ro) == 0 ]] || { echo "$dev ($part) did not become writable"; exit 1; } omamobile-bootctl mark-successful "$dev" "$slot" --i-understand echo "slot $slot marked successful" diff --git a/packages/omamobile-device-cheetah/test/all b/packages/omamobile-device-cheetah/test/all index fba7eb760..9504c075e 100755 --- a/packages/omamobile-device-cheetah/test/all +++ b/packages/omamobile-device-cheetah/test/all @@ -11,6 +11,10 @@ tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT for script in bin/*; do bash -n "$script" || fail "$script parses"; done pass "scripts parse" +# The boot-success marker under the storage guard, old and new (#252) +test/boot-success > "$tmp/boot-success.log" 2>&1 || { cat "$tmp/boot-success.log"; fail "test/boot-success"; } +pass "the boot-success marker clears only devinfo's own flag under the storage guard (test/boot-success)" + grep -q '\.\./\.\.' build && fail "build reaches outside the package directory" pass "build stays inside the package directory" diff --git a/packages/omamobile-device-cheetah/test/boot-success b/packages/omamobile-device-cheetah/test/boot-success new file mode 100755 index 000000000..a74cc1e0d --- /dev/null +++ b/packages/omamobile-device-cheetah/test/boot-success @@ -0,0 +1,144 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# omamobile-boot-success against a fake root (no phone, nothing compiled). +# Each "block device" is a file, each read-only flag a file in sysfs that +# follows the kernel's rule (bdev_read_only: a partition is read-only when +# its own flag or its disk's flag is set), and blockdev and omamobile-bootctl +# are stubs; the bootctl stub refuses to write a devinfo that reads +# read-only, as the kernel would. +# +# With the storage guard of #252 (whole-disk nodes removed, disk flags +# clear, every partition's own flag set), the marker clears devinfo's own +# flag alone and never a disk's; with an initramfs from before it (the disk +# flag set, its node present) it opens the disk too, as it always did. +# +# test/boot-success [SCRIPT] default bin/omamobile-boot-success +set -euo pipefail +cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." +fail() { printf 'not ok - %s\n' "$1" >&2; exit 1; } +pass() { printf 'ok - %s\n' "$1"; } +script=${1:-bin/omamobile-boot-success} +t=$(mktemp -d); trap '[[ -n ${KEEP:-} ]] || rm -rf "$t"' EXIT +R=$t/r + +# The marker with its paths pointed into $R +sed -e "s|/proc/bootconfig|$R/proc/bootconfig|g" -e "s|/sys/|$R/sys/|g" -e "s|/dev/|$R/dev/|g" \ + "$script" > "$t/marker" +[[ $(grep -c "$R" "$t/marker") -ge 4 ]] || fail "the marker's paths were redirected" + +mkdir -p "$t/bin" +# blockdev --setro|--setrw NODE: fails on a missing node, as the real one +# does; sets the node's own flag, then rewrites every sysfs ro file. +cat > "$t/bin/blockdev" <&2; exit 1; } +n=\${2##*/} +echo "\$1 \$n" >> "\$R/blockdev.log" +echo \$v > "\$R/own/\$n" +for d in "\$R"/sys/block/*; do + dn=\${d##*/}; echo "\$(<"\$R/own/\$dn")" > "\$d/ro" + for p in "\$d"/sd*; do + [[ -d \$p ]] || continue + pn=\${p##*/} + if [[ \$(<"\$R/own/\$pn") == 1 || \$(<"\$R/own/\$dn") == 1 ]]; then echo 1; else echo 0; fi > "\$p/ro" + done +done +EOF +# omamobile-bootctl dump|mark-successful DEV SLOT: the slot's state is a +# word in the device file; a write needs the device to read writable. +cat > "$t/bin/omamobile-bootctl" <&2; exit 1; } + echo "mark \$n" >> "\$R/blockdev.log" + echo yes > "\$2" ;; + *) exit 1 ;; +esac +EOF +chmod +x "$t/bin/blockdev" "$t/bin/omamobile-bootctl" +cp -p "$t/bin/blockdev" "$t/bin/omamobile-bootctl" "$t/" +stubs() { cp -p "$t/blockdev" "$t/omamobile-bootctl" "$t/bin/"; } # undo a case's change + +# A gs201-shaped layout: sda (main LUN), sdd (devinfo, pinfo, mfg_data). +# $1 new: the guard of #252; old: the guard from before it. +fresh() { + rm -rf "$R"; mkdir -p "$R/proc" "$R/dev" "$R/own" "$R/sys/class/block" + printf 'androidboot.slot_suffix = "_b"\n' > "$R/proc/bootconfig" + : > "$R/blockdev.log" + local d p n + for d in sda sdd; do + mkdir -p "$R/sys/block/$d"; echo 0 > "$R/own/$d" + done + n=0 + for p in sda:persist sda:userdata sdd:devinfo sdd:pinfo sdd:mfg_data; do + d=${p%%:*} n=$((n + 1)) + mkdir -p "$R/sys/block/$d/$d$n" + printf 'PARTN=%s\nPARTNAME=%s\n' "$n" "${p#*:}" > "$R/sys/block/$d/$d$n/uevent" + echo 1 > "$R/own/$d$n" + [[ ${p#*:} == userdata ]] && echo 0 > "$R/own/$d$n" + ln -s "../../block/$d/$d$n" "$R/sys/class/block/$d$n" + echo no > "$R/dev/$d$n" + done + if [[ $1 == old ]]; then + echo 1 > "$R/own/sdd"; : > "$R/dev/sdd" + fi + # write the sysfs ro files from the own flags + PATH="$t/bin:$PATH" blockdev --setro "$R/dev/sda1"; : > "$R/blockdev.log" +} +run() { PATH="$t/bin:$PATH" bash "$t/marker" > "$t/out" 2>&1; } +ro() { cat "$R"/sys/block/*/"$1"/ro 2>/dev/null || cat "$R/sys/block/$1/ro"; } + +fresh new +run || { cat "$t/out"; fail "the marker marks slot b under the new guard"; } +[[ $(<"$R/dev/sdd3") == yes ]] || fail "devinfo was written" +[[ $(<"$R/blockdev.log") == $'--setrw sdd3\nmark sdd3\n--setro sdd3' ]] \ + || fail "only devinfo's own flag was cleared, and set again (log: $(tr '\n' ' ' < "$R/blockdev.log"))" +[[ $(ro sdd) == 0 && $(ro sdd3) == 1 && $(ro sdd4) == 1 && $(ro sdd5) == 1 && $(ro sda1) == 1 ]] \ + || fail "devinfo is read-only again and nothing else changed" +[[ ! -e $R/dev/sdd ]] || fail "no whole-disk node was made" +pass "new guard: devinfo's own flag alone is cleared for the write and set again; no disk flag is touched" + +run || fail "a second run succeeds" +grep -q 'already marked successful' "$t/out" || fail "a marked slot is left alone" +[[ $(<"$R/blockdev.log") == $'--setrw sdd3\nmark sdd3\n--setro sdd3' ]] || fail "a marked slot clears no flag" +pass "a slot already marked: no flag is touched" + +fresh old +run || { cat "$t/out"; fail "the marker marks slot b under the old guard"; } +[[ $(<"$R/dev/sdd3") == yes ]] || fail "devinfo was written (old guard)" +[[ $(<"$R/blockdev.log") == $'--setrw sdd\n--setrw sdd3\nmark sdd3\n--setro sdd3\n--setro sdd' ]] \ + || fail "old guard: the disk and devinfo opened and closed as before (log: $(tr '\n' ' ' < "$R/blockdev.log"))" +[[ $(ro sdd) == 1 && $(ro sdd3) == 1 ]] || fail "old guard: the disk and devinfo are read-only again" +pass "old guard (disk flag set): the disk and devinfo are opened and closed again, as before" + +# A devinfo whose flag won't clear: nothing written, and the reason said +fresh new +sed -i 's#^echo \$v > #[[ $n == sdd3 \&\& $v == 0 ]] || echo $v > #' "$t/bin/blockdev" +st=0; run || st=$? +[[ $st != 0 && $(<"$R/dev/sdd3") == no ]] || fail "a devinfo that stays read-only is not written (exit $st)" +grep -q 'did not become writable' "$t/out" || { cat "$t/out"; fail "the marker says why"; } +pass "a devinfo flag that won't clear: nothing written, the marker fails and says why" +stubs + +# The write fails: devinfo is still set read-only again +fresh new +sed -i 's|^ echo yes > "\$2" ;;| exit 1 ;;|' "$t/bin/omamobile-bootctl" +st=0; run || st=$? +[[ $st != 0 && $(ro sdd3) == 1 && $(ro sdd) == 0 ]] || fail "a failed write still sets devinfo read-only again (exit $st)" +pass "a failed write: devinfo is set read-only again" +stubs + +# devinfo that isn't on a LUN's partition list is refused before any flag +fresh new +rm "$R/sys/class/block/sdd3"; ln -s "../../block/sdd" "$R/sys/class/block/sdd3" +st=0; run || st=$? +[[ $st != 0 && ! -s $R/blockdev.log ]] || { cat "$t/out"; fail "a devinfo that resolves to a whole disk is refused before any flag changes (exit $st)"; } +pass "a devinfo that isn't a partition of a LUN is refused before any flag changes" diff --git a/packages/omamobile-device-cheetah/version b/packages/omamobile-device-cheetah/version index 6871b9e7a..6ee33ba22 100644 --- a/packages/omamobile-device-cheetah/version +++ b/packages/omamobile-device-cheetah/version @@ -1 +1 @@ -0.1.44 +0.1.45 diff --git a/packages/omamobile-device-lynx/bin/omamobile-boot-success b/packages/omamobile-device-lynx/bin/omamobile-boot-success index 58514e116..34946af88 100755 --- a/packages/omamobile-device-lynx/bin/omamobile-boot-success +++ b/packages/omamobile-device-lynx/bin/omamobile-boot-success @@ -5,8 +5,10 @@ # retry each boot and eventually abandons the slot. # # devinfo sits on a bootloader LUN (sdd) that the storage guard keeps -# read-only. Open exactly that disk and partition for the one write, then -# close them again. omamobile-bootctl only ever changes the slot flag bytes +# read-only. The guard removes the LUN's whole-disk node, leaves the disk's +# own flag clear and sets each partition's own flag (#252), so clearing +# devinfo's own flag opens devinfo alone, for the one write; it is set +# again afterwards. omamobile-bootctl only ever changes the slot flag bytes # (0x30-0x37) and verifies the whole partition afterwards. set -euo pipefail @@ -18,16 +20,37 @@ for s in /sys/block/sd*/sd*; do grep -qx PARTNAME=devinfo "$s/uevent" 2>/dev/null && dev=/dev/${s##*/} done [[ -n "$dev" ]] || { echo "devinfo partition not found"; exit 1; } -disk=/dev/$(basename "$(dirname "$(readlink -f "/sys/class/block/${dev#/dev/}")")") +part=${dev#/dev/} +disk=$(basename "$(dirname "$(readlink -f "/sys/class/block/$part")")") +[[ $disk == sd* && $disk != "$part" && -r /sys/block/$disk/$part/ro ]] \ + || { echo "devinfo ($part) is not a partition of a UFS LUN"; exit 1; } if omamobile-bootctl dump "$dev" | sed -n "/^slot $slot /,/retry-count/p" | grep -q 'successful: yes'; then echo "slot $slot already marked successful" exit 0 fi -relock() { blockdev --setro "$dev"; blockdev --setro "$disk"; } -trap relock EXIT -blockdev --setrw "$disk" -blockdev --setrw "$dev" +part_ro() { cat "/sys/block/$disk/$part/ro"; } +if [[ $(<"/sys/block/$disk/ro") == 0 ]]; then + # The guard as it is: only devinfo's own flag is cleared. + relock() { + blockdev --setro "$dev" + [[ $(part_ro) == 1 ]] || echo "WARNING: $dev ($part) is still writable" + } + trap relock EXIT + blockdev --setrw "$dev" +else + # An initramfs from before #252 still sets the disk's own flag, which + # also keeps devinfo read-only: open the disk too, as before, until the + # phone boots an initramfs with the new guard. + relock() { + blockdev --setro "$dev"; blockdev --setro "/dev/$disk" + [[ $(part_ro) == 1 ]] || echo "WARNING: $dev ($part) is still writable" + } + trap relock EXIT + blockdev --setrw "/dev/$disk" + blockdev --setrw "$dev" +fi +[[ $(part_ro) == 0 ]] || { echo "$dev ($part) did not become writable"; exit 1; } omamobile-bootctl mark-successful "$dev" "$slot" --i-understand echo "slot $slot marked successful" diff --git a/packages/omamobile-device-lynx/version b/packages/omamobile-device-lynx/version index ee1372d33..717903969 100644 --- a/packages/omamobile-device-lynx/version +++ b/packages/omamobile-device-lynx/version @@ -1 +1 @@ -0.2.2 +0.2.3 diff --git a/packages/omamobile-device-raven/bin/omamobile-boot-success b/packages/omamobile-device-raven/bin/omamobile-boot-success index 76efb5d7f..aab279b23 100755 --- a/packages/omamobile-device-raven/bin/omamobile-boot-success +++ b/packages/omamobile-device-raven/bin/omamobile-boot-success @@ -6,8 +6,10 @@ # # Copied from omamobile-device-cheetah: raven has the same gs-common devinfo # (checked with omamobile-bootctl dump). devinfo sits on a bootloader LUN (sdd) -# that the storage guard keeps read-only. Open exactly that disk and partition for the one write, then -# close them again. omamobile-bootctl only ever changes the slot flag bytes +# that the storage guard keeps read-only. The guard removes the LUN's whole-disk node, leaves the disk's +# own flag clear and sets each partition's own flag (#252), so clearing +# devinfo's own flag opens devinfo alone, for the one write; it is set +# again afterwards. omamobile-bootctl only ever changes the slot flag bytes # (0x30-0x37) and verifies the whole partition afterwards. set -euo pipefail @@ -19,16 +21,37 @@ for s in /sys/block/sd*/sd*; do grep -qx PARTNAME=devinfo "$s/uevent" 2>/dev/null && dev=/dev/${s##*/} done [[ -n "$dev" ]] || { echo "devinfo partition not found"; exit 1; } -disk=/dev/$(basename "$(dirname "$(readlink -f "/sys/class/block/${dev#/dev/}")")") +part=${dev#/dev/} +disk=$(basename "$(dirname "$(readlink -f "/sys/class/block/$part")")") +[[ $disk == sd* && $disk != "$part" && -r /sys/block/$disk/$part/ro ]] \ + || { echo "devinfo ($part) is not a partition of a UFS LUN"; exit 1; } if omamobile-bootctl dump "$dev" | sed -n "/^slot $slot /,/retry-count/p" | grep -q 'successful: yes'; then echo "slot $slot already marked successful" exit 0 fi -relock() { blockdev --setro "$dev"; blockdev --setro "$disk"; } -trap relock EXIT -blockdev --setrw "$disk" -blockdev --setrw "$dev" +part_ro() { cat "/sys/block/$disk/$part/ro"; } +if [[ $(<"/sys/block/$disk/ro") == 0 ]]; then + # The guard as it is: only devinfo's own flag is cleared. + relock() { + blockdev --setro "$dev" + [[ $(part_ro) == 1 ]] || echo "WARNING: $dev ($part) is still writable" + } + trap relock EXIT + blockdev --setrw "$dev" +else + # An initramfs from before #252 still sets the disk's own flag, which + # also keeps devinfo read-only: open the disk too, as before, until the + # phone boots an initramfs with the new guard. + relock() { + blockdev --setro "$dev"; blockdev --setro "/dev/$disk" + [[ $(part_ro) == 1 ]] || echo "WARNING: $dev ($part) is still writable" + } + trap relock EXIT + blockdev --setrw "/dev/$disk" + blockdev --setrw "$dev" +fi +[[ $(part_ro) == 0 ]] || { echo "$dev ($part) did not become writable"; exit 1; } omamobile-bootctl mark-successful "$dev" "$slot" --i-understand echo "slot $slot marked successful" diff --git a/packages/omamobile-device-raven/test/all b/packages/omamobile-device-raven/test/all index 0f3332569..488d93d56 100755 --- a/packages/omamobile-device-raven/test/all +++ b/packages/omamobile-device-raven/test/all @@ -10,6 +10,16 @@ tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT for script in bin/*; do bash -n "$script" || fail "$script parses"; done pass "scripts parse" +# The boot-success marker is cheetah's, comments aside; run cheetah's test +# on it when the cheetah package is beside this one (#252) +if [[ -x ../omamobile-device-cheetah/test/boot-success ]]; then + cmp -s <(grep -v '^#' bin/omamobile-boot-success) <(grep -v '^#' ../omamobile-device-cheetah/bin/omamobile-boot-success) \ + || fail "omamobile-boot-success drifted from the cheetah package" + ../omamobile-device-cheetah/test/boot-success "$PWD/bin/omamobile-boot-success" > "$tmp/boot-success.log" 2>&1 \ + || { cat "$tmp/boot-success.log"; fail "cheetah's test/boot-success on raven's marker"; } + pass "the boot-success marker matches cheetah's and clears only devinfo's own flag" +fi + ./build "$tmp/build" || fail "C tools compile" pass "C tools compile" diff --git a/packages/omamobile-device-raven/version b/packages/omamobile-device-raven/version index 28d007539..50140e353 100644 --- a/packages/omamobile-device-raven/version +++ b/packages/omamobile-device-raven/version @@ -1 +1 @@ -0.1.32 +0.1.33 diff --git a/tests/storage-guard b/tests/storage-guard index efcec83a3..4f15c02c6 100755 --- a/tests/storage-guard +++ b/tests/storage-guard @@ -6,8 +6,10 @@ # initramfs/init reads that file and fails closed. Its guard functions run # here against a fake sysfs and /dev: a missing, empty or malformed file, or # a forbidden name, leaves every partition read-only; cheetah's list leaves -# exactly its six partitions writable, as the hard-coded list did; /dev/sda's -# whole-disk node is removed either way. +# exactly its six partitions writable, as the hard-coded list did. Every +# LUN's whole-disk node is removed and its own flag left clear, and each +# boot-LUN partition gets its own flag, so one partition (devinfo) can be +# opened alone without a disk flag (#252, #483). set -euo pipefail cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." fail() { printf 'not ok - %s\n' "$1" >&2; exit 1; } @@ -67,8 +69,11 @@ cp "$t/good.conf" "$R/device/six/device.conf" # --- Boot time: initramfs/init's guard against a fake sysfs and /dev --- # The guard as init has it: ufs_ro_log through the ufs_rw_load call and -# ufs_ro_pass, with its paths pointed into $t/b. blockdev --setro writes 1 -# into the partition's ro file, as the kernel would. +# ufs_ro_pass, with its paths pointed into $t/b. Each disk and partition has +# its own read-only flag ($B/own/), and the sysfs ro files follow the +# kernel's rule (bdev_read_only): a disk reads its own flag, a partition its +# own flag or its disk's. The blockdev stub sets one own flag, as BLKROSET +# does, refuses a node that isn't there, and logs every call. awk '/^ufs_ro_log\(\) \{/ {on = 1} /^ufs_ro_pass$/ {exit} on' initramfs/init > "$t/guard.sh" grep -q '^ufs_rw_load() {$' "$t/guard.sh" || fail "ufs_rw_load found in initramfs/init" grep -q '^ufs_rw_load$' "$t/guard.sh" || fail "the init loads the allowlist before its first pass" @@ -79,43 +84,66 @@ sed -e "s|> /dev/kmsg|>> /dev/kmsg|" -e "s|/sys/block/|$B/sys/block/|g" -e "s|/d mkdir -p "$t/bin" cat > "$t/bin/blockdev" <&2; exit 1; } n=\${2##*/} -for f in $B/sys/block/\$n/ro $B/sys/block/*/\$n/ro; do [ -f "\$f" ] && echo 1 > "\$f"; done -exit 0 +echo "\$1 \$n" >> $B/blockdev.log +echo \$v > $B/own/\$n +$t/bin/sync-ro EOF -chmod +x "$t/bin/blockdev" -parts="boot_a boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b userdata persist efs modem_b vbmeta_b devinfo" -fake_disks() { - rm -rf "$B" && mkdir -p "$B/dev" "$B/etc" "$B/sys/block/sda" "$B/sys/block/sdb/sdb1" - echo 0 > "$B/sys/block/sda/ro"; : > "$B/dev/sda" - local i=0 p - for p in $parts; do - i=$((i + 1)) - mkdir -p "$B/sys/block/sda/sda$i" - printf 'MAJOR=8\nPARTN=%s\nPARTNAME=%s\n' "$i" "$p" > "$B/sys/block/sda/sda$i/uevent" - echo 0 > "$B/sys/block/sda/sda$i/ro"; : > "$B/dev/sda$i" +cat > "$t/bin/sync-ro" < \$d/ro + for p in \$d/sd*; do + [ -d "\$p" ] || continue + if [ "\$(cat $B/own/\${p##*/})" = 1 ] || [ "\$(cat $B/own/\$dn)" = 1 ]; then echo 1; else echo 0; fi > \$p/ro done - # A boot LUN, read-only at the disk level - echo 0 > "$B/sys/block/sdb/ro"; : > "$B/dev/sdb" - printf 'PARTNAME=bl1_a\n' > "$B/sys/block/sdb/sdb1/uevent" - echo 0 > "$B/sys/block/sdb/sdb1/ro"; : > "$B/dev/sdb1" +done +EOF +chmod +x "$t/bin/blockdev" "$t/bin/sync-ro" +parts="boot_a boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b userdata persist efs modem_b vbmeta_b devinfo" +boot_luns="sdb:bl1_a sdb:abl_a sdb:bl1_b sdc:bl1_b sdc:abl_b sdc:dtbo_b sdd:devinfo sdd:pinfo sdd:mfg_data" +mkdisk() { mkdir -p "$B/sys/block/$1"; echo 0 > "$B/own/$1"; : > "$B/dev/$1"; } +mkpart() { # disk name partname + mkdir -p "$B/sys/block/$1/$2" + printf 'MAJOR=8\nPARTN=%s\nPARTNAME=%s\n' "${2#"$1"}" "$3" > "$B/sys/block/$1/$2/uevent" + echo 0 > "$B/own/$2"; : > "$B/dev/$2" +} +fake_disks() { # the main LUN, three boot LUNs (gs201's shape), every flag clear; + # sdc carries a dtbo_b, which the allowlist never opens off the main LUN + rm -rf "$B" && mkdir -p "$B/dev" "$B/etc" "$B/own" "$B/sys/block" + : > "$B/blockdev.log" + local i=0 p d + mkdisk sda + for p in $parts; do i=$((i + 1)); mkpart sda "sda$i" "$p"; done + for d in sdb sdc sdd; do mkdisk $d; done + i=0 + for p in $boot_luns; do d=${p%%:*} i=$((i + 1)); mkpart "$d" "$d$i" "${p#*:}"; done + "$t/bin/sync-ro" } guard() { # run the guard's load and one pass PATH="$t/bin:$PATH" sh -c ". '$t/guard-test.sh'; ufs_ro_pass" } -writable() { # names of the partitions left writable, in GPT order +writable() { # names of the partitions left writable, in order (every LUN) local s out="" - for s in "$B"/sys/block/sda/sda*; do + for s in "$B"/sys/block/sd*/sd*; do [[ $(cat "$s/ro") == 0 ]] && out+="$(sed -n 's/^PARTNAME=//p' "$s/uevent") " done printf '%s' "${out% }" } disk_checks() { # $1: case name - [[ ! -e $B/dev/sda ]] || fail "$1: /dev/sda's whole-disk node is removed" - [[ $(cat "$B/sys/block/sda/ro") == 0 ]] || fail "$1: sda itself stays writable (the kernel needs it for its partitions)" - [[ $(cat "$B/sys/block/sdb/ro") == 1 && $(cat "$B/sys/block/sdb/sdb1/ro") == 1 ]] || fail "$1: the boot LUN is read-only" - grep -q 'whole-disk node removed' "$B/dev/kmsg" || fail "$1: the sda removal is logged" + local d s + for d in sda sdb sdc sdd; do + [[ ! -e $B/dev/$d ]] || fail "$1: /dev/$d's whole-disk node is removed" + [[ $(cat "$B/own/$d") == 0 && $(cat "$B/sys/block/$d/ro") == 0 ]] \ + || fail "$1: $d's own flag stays clear (the kernel ORs it into every partition)" + grep -q "/dev/$d: whole-disk node removed" "$B/dev/kmsg" || fail "$1: the $d removal is logged" + done + for s in "$B"/sys/block/sd[b-d]/sd*; do + [[ $(cat "$B/own/${s##*/}") == 1 ]] || fail "$1: boot-LUN partition ${s##*/}'s own flag is set" + done + ! grep -qE ' sd[a-z]+$' "$B/blockdev.log" || fail "$1: blockdev never touches a whole disk ($(tr '\n' ' ' < "$B/blockdev.log"))" } fake_disks @@ -139,6 +167,55 @@ awk '/^boot_arch\(\) \{/ {on = 1} on && /ufs_unguarded/ {u = NR} on && /kill -9 || fail "boot_arch checks ufs_unguarded before it stops the initramfs's processes" pass "the hand-over check reports a late or reopened partition, and runs before SSH is stopped" +# The boot LUNs as #252 rules: the whole-disk nodes gone and unusable, the +# disk flags clear, so one partition's own flag opens that partition alone. +fake_disks +printf '%s\n' "$cheetah_rw" > "$B/etc/omamobile-ufs-rw" +guard +[[ -z $(unguarded) ]] || fail "the guard holds before the window (got: $(unguarded))" +dv=$(grep -l '^PARTNAME=devinfo$' "$B"/sys/block/sdd/sd*/uevent); dv=${dv%/uevent}; dv=${dv##*/} +: > "$B/blockdev.log" +for d in sdb sdc sdd; do + if PATH="$t/bin:$PATH" blockdev --setrw "$B/dev/$d" 2>/dev/null; then fail "a whole-disk node is unusable: blockdev --setrw /dev/$d failed"; fi +done +[[ ! -s $B/blockdev.log ]] || fail "no disk flag could be reached" +PATH="$t/bin:$PATH" blockdev --setrw "$B/dev/$dv" +[[ $(writable) == "$cheetah_rw devinfo" ]] || fail "clearing devinfo's own flag opens devinfo alone (writable: $(writable))" +for d in sda sdb sdc sdd; do [[ $(cat "$B/own/$d") == 0 ]] || fail "the window touches no disk flag ($d)"; done +[[ $(unguarded) == "$dv (devinfo)" ]] || fail "the hand-over check names the open partition (got: $(unguarded))" +PATH="$t/bin:$PATH" blockdev --setro "$B/dev/$dv" +[[ $(writable) == "$cheetah_rw" && -z $(unguarded) ]] || fail "setting devinfo's own flag again closes it" +pass "a boot-LUN partition's own flag opens it alone, with every disk flag clear and no whole-disk node to write through" + +# A whole-disk node that comes back is reported; one the guard can't remove +# is retried, warned about and reported, and its LUN's partitions still set. +: > "$B/dev/sdd" +[[ $(unguarded) == "sdd (whole-disk node)" ]] || fail "a whole-disk node that comes back is reported (got: $(unguarded))" +fake_disks +printf '%s\n' "$cheetah_rw" > "$B/etc/omamobile-ufs-rw" +rm "$B/dev/sdc"; mkdir -p "$B/dev/sdc/x" +guard; guard +[[ $(grep -c 'FAILED to remove the whole-disk node .*/dev/sdc$' "$B/dev/kmsg") == 1 ]] || { cat "$B/dev/kmsg"; fail "a node that won't go is logged once"; } +[[ ! -e $B/run/ufs-ro/sdc ]] || fail "a node that won't go is retried on the next pass" +[[ $(unguarded) == "sdc (whole-disk node)" ]] || fail "a node that won't go stops the hand-over (got: $(unguarded))" +[[ $(writable) == "$cheetah_rw" ]] || fail "its partitions are still set read-only (writable: $(writable))" +rm -r "$B/dev/sdc"; : > "$B/dev/sdc"; guard +[[ ! -e $B/dev/sdc && -e $B/run/ufs-ro/sdc && -z $(unguarded) ]] || fail "the next pass removes it once it can" +pass "a whole-disk node that comes back or won't go is reported, retried and keeps the phone in the initramfs" + +# A LUN that appears after the first pass is guarded by the next +fake_disks +printf '%s\n' "$cheetah_rw" > "$B/etc/omamobile-ufs-rw" +guard +mkdisk sde; mkpart sde sde1 userdata; mkpart sde sde2 boot_b; "$t/bin/sync-ro" +[[ $(unguarded | tr '\n' ' ') == "sde (whole-disk node) sde1 (userdata) sde2 (boot_b) " ]] \ + || fail "a late LUN is reported before the next pass (got: $(unguarded | tr '\n' ' '))" +guard +[[ ! -e $B/dev/sde && $(cat "$B/own/sde") == 0 && $(cat "$B/own/sde1") == 1 && $(cat "$B/own/sde2") == 1 ]] \ + || fail "the next pass removes the late LUN's node and sets its partitions, allowlisted names too" +[[ -z $(unguarded) ]] || fail "the guard holds again (got: $(unguarded))" +pass "a LUN that appears late is guarded by the next pass; the allowlist opens nothing off the main LUN" + fake_disks cp "$t/etc-oriole/omamobile-ufs-rw" "$B/etc/omamobile-ufs-rw" guard From a9135d834b38773e07a7af023ca3863a081e3b93 Mon Sep 17 00:00:00 2001 From: Derek Ross Date: Sat, 7 Nov 2026 11:21:34 -0500 Subject: [PATCH 2/2] initramfs, bin/omamobile: the marker check in push and flash, review round 1 (#484) The review of #484 found the order unsafe on development phones: push and flash build the init from the checkout, but omamobile-boot-success comes from the installed device package, and an old marker under the new guard never marks slot B, so the next flash would end in a fallback to slot A. - The new marker carries "# guard: partition flags (#252)". For a device whose package ships the marker (cheetah, raven, lynx), push reads the phone's marker over SSH and refuses images with the new guard unless the line is there (a missing marker too); flash can't read the phone, so it refuses those images unless OMAMOBILE_MARKER_CHECKED=1. A warning would scroll by, and the failure shows only three boots later. Images whose initramfs reads as the old guard skip the check; a missing or unreadable initramfs doesn't. tests/images-layout covers each case; eight mutations, seven caught (the survivor drops grep's "|| true", which still refuses, with another message). - STORAGE-GUARD-PLAN: R5 runs the marker's new write path on the 7 Pro (a flash that unmarks B, a devinfo copy before and after, rollback to the old-init set whose old-guard path marks B); R4 arms a detached relock; "after the first pass" with the new-partition window explained; the merge order, the owners' notice and the version collisions. - test/boot-success: the guard line, and a phone without devinfo. - 10-omamobile-storage.rules: the comment said udev would recreate /dev/sda; devtmpfs makes a node only in device_add. Comment only, the rule is unchanged, so felix, husky, panther and tegu aren't bumped. - husky UFS-PLAN: its future checks no longer expect sdb-sdd to read ro. Tested without a phone or a build: tests/storage-guard, tests/initramfs-steps, tests/images-layout (image part skipped), tests/modules-push, tests/boot-method, test/boot-success on all three markers, sh -n, tools/pr-check. Co-Authored-By: Claude Opus 5.5 --- CONTRIBUTING.md | 2 +- bin/omamobile | 48 +++++++++++++ device/cheetah/STORAGE-GUARD-PLAN.md | 72 +++++++++++++++---- device/husky/UFS-PLAN.md | 4 +- .../bin/omamobile-boot-success | 2 + .../test/boot-success | 16 +++++ .../udev/rules.d/10-omamobile-storage.rules | 7 +- .../udev/rules.d/10-omamobile-storage.rules | 7 +- .../udev/rules.d/10-omamobile-storage.rules | 7 +- .../bin/omamobile-boot-success | 2 + .../udev/rules.d/10-omamobile-storage.rules | 7 +- .../udev/rules.d/10-omamobile-storage.rules | 7 +- .../bin/omamobile-boot-success | 2 + .../udev/rules.d/10-omamobile-storage.rules | 7 +- .../udev/rules.d/10-omamobile-storage.rules | 7 +- tests/images-layout | 45 ++++++++++++ 16 files changed, 206 insertions(+), 36 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 49956a659..ddeb36c3b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -80,7 +80,7 @@ UFS holds the per-device calibration. The initramfs ([`initramfs/init`](initramf - It sets every SCSI partition's own read-only flag (`blockdev --setro`) as soon as it appears, and keeps a watcher running for the life of PID 1. - The only exceptions are an allowlist of GPT partition names on the main LUN (`sda`): the device's `BOOT_IMAGES` on slot B, and `userdata`. - It deletes the whole-disk node of every LUN (`/dev/sda`, and the boot LUNs' `/dev/sdb`, `/dev/sdc`, `/dev/sdd`), so nothing can write across partitions, and leaves the disks' own flags clear. The kernel counts a partition read-only when its own flag or its disk's is set (`bdev_read_only()`), so a disk flag would hide every partition on that LUN, and opening one would mean opening all of them through the whole-disk node ([#252](https://github.com/derekross/omarchy-mobile/issues/252)). Before handing over to Arch it stays in the initramfs if any whole-disk node is still there or any partition outside the allowlist is writable. -- In Arch, [`10-omamobile-storage.rules`](packages/omamobile-device-cheetah/vendor/udev/rules.d/10-omamobile-storage.rules) keeps the main LUN's whole-disk node unusable and hides the Android partitions from desktop tools. +- In Arch, [`10-omamobile-storage.rules`](packages/omamobile-device-cheetah/vendor/udev/rules.d/10-omamobile-storage.rules) keeps the main LUN's whole-disk node unusable, should it ever come back (devtmpfs makes a node only when the kernel adds a device), and hides the Android partitions from desktop tools. - The one write outside the allowlist is the boot-success marker (`omamobile-boot-success`). It clears `devinfo`'s own flag alone for a single verified write of the slot flag bytes, then sets it read-only again; no disk flag is touched (under an initramfs from before #252, which still sets the disk flag, it opens the disk too, as it always did). **A new device gets this guard before its UFS driver is ever enabled.** Check the device's LUN layout and partition names first, because the allowlist works by name. diff --git a/bin/omamobile b/bin/omamobile index b43e609ad..2e5810fb6 100755 --- a/bin/omamobile +++ b/bin/omamobile @@ -92,6 +92,10 @@ Usage: [DEVICE=] omamobile flash Flash the experiment images to slot B and boot them push Write the images into slot B from the running phone over SSH, verify them, and reboot into them (no fastboot) + On a phone whose device package ships omamobile-boot-success, + both check that its marker knows the images' storage guard + (#252): push reads it over SSH, flash needs + OMAMOBILE_MARKER_CHECKED=1 after checking by hand kernel-shell Open a shell in the build environment, in the kernel tree USAGE } @@ -226,6 +230,28 @@ images_for_device() { # Is _b on the device's storage-guard allowlist? slot_b_allowed() { [[ " $DEV_UFS_RW " == *" ${1}_b "* ]]; } +# The storage guard of #252 removes the boot LUNs' whole-disk nodes. A +# boot-success marker from before it opens /dev/sdd first, so under the new +# guard it never marks slot B: harmless while B is marked, but after the next +# flash (which unmarks B) every boot spends one of B's tries until ABL falls +# back to slot A. Development phones get the init from this checkout and the +# marker from the installed device package, so for a device whose package +# ships the marker, push and flash check that the two match. The new marker +# carries this exact line. +MARKER_TAG='# guard: partition flags (#252)' +ships_boot_marker() { [[ -f $ROOT/packages/$DEV_PACKAGE/bin/omamobile-boot-success ]]; } +# Does $KERNEL_OUT's initramfs carry the #252 guard? Yes when it can't be +# read (no file, no lz4), so a doubt never skips the check. +initramfs_new_guard() { + local f=$KERNEL_OUT/initramfs.cpio.lz4 n + [[ -f $f ]] && command -v lz4 >/dev/null || return 0 + n=$(lz4 -dc "$f" 2>/dev/null | grep -ac 'ufs_is_disk') || true + [[ ${n:-0} != 0 ]] || ! lz4 -t "$f" >/dev/null 2>&1 +} +marker_check_hint() { + echo "check on the phone: grep -cxF '$MARKER_TAG' /usr/bin/omamobile-boot-success (1: ready; anything else: tools/phone-install-package packages/$DEV_PACKAGE first)" +} + # Does the device have this boot image? gs101 phones have no init_boot or # vendor_kernel_boot. has_image() { [[ " $DEV_BOOT_IMAGES " == *" $1 "* ]]; } @@ -1096,6 +1122,13 @@ cmd_flash() { [[ $DEV_BOOT_METHOD != qcom-abl ]] || die "flash refuses BOOT_METHOD=qcom-abl (no in-tree day-2 update path yet; see device/$DEVICE/BRINGUP-M1.md)" images_for_device "$img" + # flash unmarks slot B, so the phone's marker must be able to mark it under + # these images' guard (#252). Fastboot can't read the installed package, so + # refuse unless the owner says it was checked: a warning would scroll by, and + # a wrong marker shows only as a fallback to slot A three boots later. + if ships_boot_marker && initramfs_new_guard && [[ ${OMAMOBILE_MARKER_CHECKED:-} != 1 ]]; then + die "flash unmarks slot B, and only an omamobile-boot-success that knows these images' storage guard (#252) marks it again; $(marker_check_hint), then run again with OMAMOBILE_MARKER_CHECKED=1" + fi in_fastboot || die "phone not in fastboot (try: omamobile fastboot)" local serial serial=$(fastboot_serial) || exit 1 @@ -1141,6 +1174,21 @@ cmd_push() { booted=$("$ssh" 'cat /proc/sys/kernel/random/boot_id') || die "cannot read the phone's boot id" # dropbear's login PATH omits /usr/local/bin, where the initramfs installs reboot-to "$ssh" 'PATH=/usr/local/bin:$PATH command -v reboot-to >/dev/null' || die "phone has no reboot-to; refusing to write" + # The phone's marker must know the guard the new initramfs brings (#252) + if ships_boot_marker && initramfs_new_guard; then + if [[ ${OMAMOBILE_MARKER_CHECKED:-} == 1 ]]; then + say "OMAMOBILE_MARKER_CHECKED=1: not checking the phone's omamobile-boot-success" + else + local mk + mk=$("$ssh" "if [ -f /usr/bin/omamobile-boot-success ]; then grep -cxF '$MARKER_TAG' /usr/bin/omamobile-boot-success || true; else echo missing; fi") \ + || die "cannot read the phone's omamobile-boot-success; refusing to write" + case $mk in + 1) ;; + missing) die "the phone has no /usr/bin/omamobile-boot-success (the initramfs shell, or $DEV_PACKAGE not installed), so push can't tell whether it can mark slot B under these images' storage guard (#252); $(marker_check_hint), then run again with OMAMOBILE_MARKER_CHECKED=1" ;; + *) die "the phone's omamobile-boot-success predates these images' storage guard (#252) and could never mark slot B after the next flash: install $DEV_PACKAGE from this checkout first (tools/phone-install-package packages/$DEV_PACKAGE), then push" ;; + esac + fi + fi # A kernel whose modules aren't installed boots without its drivers (the GPU # among them): install them first. A config change can keep the release # name, so compare the tarball's checksum, not just the directory. diff --git a/device/cheetah/STORAGE-GUARD-PLAN.md b/device/cheetah/STORAGE-GUARD-PLAN.md index 1ed1aecec..d568d91a1 100644 --- a/device/cheetah/STORAGE-GUARD-PLAN.md +++ b/device/cheetah/STORAGE-GUARD-PLAN.md @@ -15,15 +15,19 @@ Now every LUN is guarded the same way: - every partition gets its own flag, except the allowlist (`UFS_RW`), which still opens partitions of `sda` only; - a node that won't go is retried and warned about, and the hand-over check (`ufs_unguarded`) now also refuses Arch while any whole-disk node is there, as it already did for a writable partition outside the allowlist. -Nothing becomes writable that wasn't: the boot LUNs' partitions are read-only by their own flags instead of their disks'. A tool that must write one of them clears that partition's own flag alone. +After the guard's first pass, nothing is writable that wasn't before: the boot LUNs' partitions are read-only by their own flags instead of their disks'. A tool that must write one of them clears that partition's own flag alone. Inside the initramfs there is one narrow difference. The kernel creates a disk's node before it scans the disk's partitions, and each partition starts with its own flag clear. So a boot-LUN partition can be writable from its birth until the guard's next pass, about 0.2 s, where before the disk flag could cover it from birth. Nothing in the initramfs writes a boot-LUN partition, and `sda`'s partitions (`efs` and `persist` among them) have always had the same window. The hand-over check reads the live state before Arch starts. -**`omamobile-boot-success` changes with it** (device-cheetah 0.1.45, raven 0.1.33, lynx 0.2.3). The marker ran `blockdev --setrw` on the whole bootloader LUN (`/dev/sdd`), then on `devinfo`. Under the new guard `/dev/sdd` is gone, so that first call fails and the marker fails (`packages/omamobile-device-cheetah/test/boot-success` shows it against the old script). It runs at every boot but writes only when slot B isn't marked yet, so a marked phone wouldn't notice until the next `flash` or `--set-active=b`. The new marker reads the disk's own flag: clear (the new guard) means it clears `devinfo`'s own flag alone and sets it again after; set (an initramfs from before this change) means it opens the disk and `devinfo` as it always did, so a fastboot rollback to an older image set still gets slot B marked. +**`omamobile-boot-success` changes with it** (device-cheetah 0.1.45, raven 0.1.33, lynx 0.2.3). The marker ran `blockdev --setrw` on the whole bootloader LUN (`/dev/sdd`), then on `devinfo`. Under the new guard `/dev/sdd` is gone, so that first call fails and the marker fails (`packages/omamobile-device-cheetah/test/boot-success` shows it against the old script). It runs at every boot but writes only when slot B isn't marked yet, so a marked phone wouldn't notice until the next `flash` or `--set-active=b`. The new marker reads the disk's own flag: +- clear (the new guard): it clears `devinfo`'s own flag alone and sets it again after; +- set (an initramfs from before this change): it opens the disk and `devinfo` as it always did, so a fastboot rollback to an older image set still gets slot B marked. + +The new marker carries the line `# guard: partition flags (#252)`. On a phone whose device package ships the marker (cheetah, raven, lynx), `bin/omamobile push` reads `/usr/bin/omamobile-boot-success` over SSH and refuses images with the new guard unless that line is there. `flash` can't read the phone, so for those phones it refuses images with the new guard unless `OMAMOBILE_MARKER_CHECKED=1` says the owner checked. A warning would scroll by, and a wrong marker shows only three boots later, as a fallback to slot A. Images whose initramfs reads as the old guard skip the check; an initramfs that is missing or can't be read doesn't. `bin/omamobile push`, `omamobile-boot-update`, `tools/nv-backup`, `tools/phone-provision`, `omamobile-fp` and `tools/phone-smoke` work on partitions found by `PARTNAME` (and `push` and `boot-update` on `sda` alone) and never open a whole-disk node; none changes. ## 2. Runbook -Read-only steps first. Each step: commands, Expect, Pass, **Stop**, Rollback, risk. Laptop steps run in this branch's worktree; phone steps over `tools/phone-ssh` (root). Record results in §3 with the date and `uname -r`. Announce to Derek before every step marked "affects the phone". +Read-only steps first. Each step: commands, Expect, Pass, **Stop**, Rollback, risk. Laptop steps run in this branch's worktree; phone steps over `tools/phone-ssh` (root). Record results in §4 with the date and `uname -r`. Announce to Derek before every step marked "affects the phone". This runbook uses `bin/omamobile push` and keeps the running kernel: only the initramfs changes. If the phone runs a packaged kernel (`uname -r` without `-push1`), stop and ask: the images then come from the device package's boot files instead. @@ -78,9 +82,9 @@ tools/phone-ssh 'pacman -Q omamobile-device-cheetah; omamobile-boot-success; ech OMAMOBILE_SSH_SHARE=1 KERNEL_OUT=$OUT bin/omamobile push ``` -- Expect: five `_b` writes, each verified; the phone reboots into the same `uname -r` within about a minute. +- Expect: `push` reads the 0.1.45 marker and goes on (a refusal naming the marker means P1 didn't take: stop); five `_b` writes, each verified; the phone reboots into the same `uname -r` within about a minute. - **Stop:** the phone doesn't come back over USB in 3 minutes, or stays in the initramfs (`ssh` answers but `/proc/1/comm` is not `systemd`): read `dmesg | grep ufs-ro` from the initramfs shell first, then roll back. -- Rollback: `cp $KG/images/*.img $OUT/images/ && KERNEL_OUT=$OUT bin/omamobile push`. If nothing answers: Power held about 30 s, then Volume Down + Power about 20 s for fastboot, and `KERNEL_OUT=$KG bin/omamobile flash` (it clears slot B's mark; the old initramfs's guard sets the disk flag, and the 0.1.45 marker's old-guard path marks B again at that boot). +- Rollback: `cp $KG/images/*.img $OUT/images/ && KERNEL_OUT=$OUT bin/omamobile push`. If nothing answers: Power held about 30 s, then Volume Down + Power about 20 s for fastboot, and `OMAMOBILE_MARKER_CHECKED=1 KERNEL_OUT=$KG bin/omamobile flash`. `$KG` holds no initramfs file, so `flash` can't tell which guard its images carry and asks for the override; P1 installed 0.1.45. The flash clears slot B's mark, the old initramfs's guard sets the disk flag, and the 0.1.45 marker's old-guard path marks B again at that boot. ### R3. The guard after the boot (read-only; risk none) @@ -102,26 +106,70 @@ tools/phone-smoke ### R4. The window #483 needs, without a write (affects nothing written; risk low) -Clears `devinfo`'s own flag for a moment, as the marker does at a boot with slot B unmarked, and sets it again. Nothing is written. Derek's call. +Clears `devinfo`'s own flag for a moment, as the marker does at a boot with slot B unmarked, and sets it again. Nothing is written. Derek's call. A detached timer sets the flag again after 30 s even if the SSH session drops before its trap runs. ```sh tools/phone-ssh 'set -e; d=$(grep -lx PARTNAME=devinfo /sys/block/sd*/sd*/uevent); d=${d%/uevent}; p=${d##*/}; k=$(basename ${d%/*}) + systemd-run --unit=x-devinfo-relock --on-active=30 /usr/bin/blockdev --setro /dev/$p trap "blockdev --setro /dev/$p" EXIT blockdev --setrw /dev/$p echo "devinfo $p ro=$(cat $d/ro); disk $k ro=$(cat /sys/block/$k/ro); siblings read-only: $(for s in /sys/block/$k/$k*; do [ $s = $d ] || cat $s/ro; done | tr -d "\n")" - blockdev --setro /dev/$p; echo "devinfo ro=$(cat $d/ro)"' + blockdev --setro /dev/$p; echo "devinfo ro=$(cat $d/ro)" + systemctl stop x-devinfo-relock.timer' ``` - Expect: `devinfo ro=0`, `disk ro=0`, every sibling `1`; then `devinfo ro=1`. - Pass: both lines. **Stop:** a sibling at 0, or `devinfo` still 0 at the end (set it with `blockdev --setro` by hand, then stop). -- Rollback: `blockdev --setro` on the `devinfo` partition (the trap does it). +- Rollback: `blockdev --setro` on the `devinfo` partition (the trap does it, and the timer 30 s later if the session dropped). -A real mark under the new guard needs slot B unmarked, which only `flash` or `--set-active=b` does; it is not part of this runbook. Its first occasion (the next `flash` of the 7 Pro) checks `journalctl -b -u omamobile-boot-success` for `slot b marked successful`. +### R5. The marker's new write path: a real mark under the new guard (affects the phone: fastboot, flash, reboot; slot B unmarked until the marker runs; risk medium) -### After the merge +R3 only sees `slot b already marked successful`, which is the early exit and unchanged code. This step unmarks slot B on purpose, so the new branch (clear `devinfo`'s own flag, check it, write, set it again) runs once on the phone. Otherwise its first real run would come during a recovery `flash`. It is attended and Derek's call. Announce the reboot into fastboot, the flash and the boot. From the flash until the marker runs, every boot spends one of slot B's three tries: count the boots. -Restage the boot files for every device (`tools/stage-boot-files`) and release their device packages, so each phone's next boot image carries the new guard; and install device-cheetah, raven and lynx's new markers with or before them. Then #483 rebases onto it, its window `devinfo` alone. +```sh +# 1. A read-only copy of devinfo, off the repository (never commit it), and its slot bytes now +d=$(tools/phone-ssh 'd=$(grep -lx PARTNAME=devinfo /sys/block/sd*/sd*/uevent); d=${d%/uevent}; echo ${d##*/}') +tools/phone-ssh "cat /dev/$d" > ~/android/reference/cheetah-devinfo-r5-before.img +tools/phone-ssh "sha256sum < /dev/$d"; sha256sum < ~/android/reference/cheetah-devinfo-r5-before.img +tools/phone-ssh "omamobile-bootctl dump /dev/$d" | sed -n '/^slot b /,/retry-count/p' +# 2. Fastboot, then flash the images R3 just checked (the new initramfs; P1's 0.1.45 marker is installed) +tools/phone-ssh 'reboot-to bootloader' # or: bin/omamobile fastboot +fastboot getvar slot-successful:b 2>&1 | head -1 +OMAMOBILE_MARKER_CHECKED=1 KERNEL_OUT=$OUT bin/omamobile flash +# 3. After the boot (flash waits for SSH) +tools/phone-ssh "journalctl -b -u omamobile-boot-success --no-pager | tail -3 + omamobile-bootctl dump /dev/$d | sed -n '/^slot b /,/retry-count/p' + cat > /tmp/devinfo-r5-after.img < /dev/$d" +tools/phone-ssh 'cat /tmp/devinfo-r5-after.img' > ~/android/reference/cheetah-devinfo-r5-after.img +cmp -l ~/android/reference/cheetah-devinfo-r5-before.img ~/android/reference/cheetah-devinfo-r5-after.img +``` -## 3. Results +Then R3's commands again. + +- Expect: + - step 1: the two checksums are equal, and slot b reads `successful: yes`; + - step 2: `slot-successful:b: yes` before the flash; five flashes, `--set-active=b`, and SSH back within about 90 s; + - step 3: the journal shows `slot b marked successful` (not `already marked`); slot b reads `successful: yes` (record its retry count: it shows the tries this boot spent); `cmp -l` lists only offsets 49-56 (bytes 0x30-0x37, which `cmp` numbers from 1; the flash's `--set-active` and the mark both change only those); + - R3 as before: every disk `disk-ro=0 node=gone`, only the six names writable. +- Pass: all of it. +- **Stop:** the marker failed or says anything else, slot b not `successful: yes`, or any R3 failure: count the boots since the flash and go to the rollback before the next reboot. A `cmp` difference outside 49-56 with slot b marked is ABL's or someone else's: stop, keep both copies and read them before anything else, but there is no try clock running. +- Rollback, in order, each within slot B's remaining tries: + 1. If the marker failed but the phone is up: read `journalctl -b -u omamobile-boot-success` and run `omamobile-boot-success` once by hand. That is the same write, so only if the log shows nothing new was tried (for example, a timing problem). + 2. `tools/phone-ssh 'reboot-to bootloader'`, then `OMAMOBILE_MARKER_CHECKED=1 KERNEL_OUT=$KG bin/omamobile flash`: the set with the old initramfs. Its guard sets the disk flag, and the 0.1.45 marker's old-guard path marks B at that boot (`slot b marked successful` in the journal). + 3. If that fails too: reinstall 0.1.44 from the previous checkout under the `$KG` images (the state before this PR) and run `omamobile-boot-success` by hand. + 4. At zero tries ABL boots stock on slot A. On the 7 Pro that is recoverable: on stock's recovery screen choose neither option, hold Volume Down + Power about 20 s for fastboot, flash `$KG`, and take a fresh read-only NV backup (AGENTS §4). + +## 3. Merging this + +- **Versions:** device-cheetah 0.1.45 is also claimed by #423 and #403, and device-lynx 0.2.3 by #438. Whichever merges later takes the next free version at merge, and the squash title is written from the `version` files then. raven 0.1.33 is free. +- **Order:** + 1. The 7 Pro run above passes. + 2. The device packages are published to the development channel, with their new markers: device-cheetah, device-raven and device-lynx. + 3. The raven and lynx owners get the notice below. + 4. Only then does the init change reach `main`. `push` and `flash` built from `main` check the marker from then on. +- **Notice to the raven and lynx owners** (raven: robotosaith; lynx: cx3301J, FlorencioJP, latentoperator): before your next `push` or `flash` from `main`, install the new device package (`tools/phone-install-package packages/omamobile-device-`, or `omarchy update` on the development channel). The new initramfs removes the boot LUN's whole-disk node, and the old `omamobile-boot-success` opens that node first. With the old marker, slot B is never marked after a `flash`, and ABL falls back to slot A after three boots. `push` now refuses in that state. If you have already flashed: install the package within B's remaining tries, then run `omamobile-boot-success` once by hand. +- **After the merge:** restage the boot files for every device (`tools/stage-boot-files`) and release their device packages, so each phone's next boot image carries the new guard. Then #483 rebases onto this, with its window `devinfo` alone. The 8 Pro owner offered on #252 to boot the guard on a Kioxia UFS 4.0 part, a second vendor and LUN layout: worth taking before #483. + +## 4. Results None yet. diff --git a/device/husky/UFS-PLAN.md b/device/husky/UFS-PLAN.md index 088cd42c3..ede1a3bbe 100644 --- a/device/husky/UFS-PLAN.md +++ b/device/husky/UFS-PLAN.md @@ -79,7 +79,7 @@ Then run the same `install-phone` without `--dry-run`. It needs the human's go, - **Pass:** - ssh as root with the installed key; - `systemctl --failed` is empty; - - the guard's flags hold under Arch: only `UFS_RW` writable, `sdb`-`sdd` `ro`, no `/dev/sda`; + - the guard's flags hold under Arch: only `UFS_RW` writable, every `sdb`-`sdd` partition `ro`, no whole-disk node (`/dev/sda` to `/dev/sdd`; since #484 the disks themselves read `ro` 0); - `reboot-to bootloader` reaches fastboot; - a second boot comes up the same. - **Stop if:** no ssh after 2 minutes: the init's log is on the panel; then Power + Volume Up, then Power + Volume Down for fastboot. Also stop if any partition outside `UFS_RW` is writable (`reboot-to bootloader` at once). @@ -126,7 +126,7 @@ Let the UFS device runtime-suspend, with the link in hibern8. zuma's CAL has hib - Read one block of `boot_b` with `dd iflag=direct`. Without it, the page cache answers every read after the first, and the device never wakes. - **Pass:** ten cycles, each with: - the device W-LUN suspends and resumes, and the direct read succeeds; - - the guard's flags hold after the cycle (only `UFS_RW` writable, `sdb`-`sdd` `ro`, no `/dev/sda`); + - the guard's flags hold after the cycle (only `UFS_RW` writable, every `sdb`-`sdd` partition `ro`, no whole-disk node; since #484 the disks themselves read `ro` 0); - kmsg shows no UIC error, no `ufshcd_err_handler` run and no reset. - **Stop if:** - **a guard flag changed:** `reboot-to bootloader` at once (Power + Volume Up if it won't go down, then Power + Volume Down), and run no more cycles. The initramfs guard marks each disk once and never looks at that name again, and this boot never reaches the check before `switch_root`, so a LUN that a host reset dropped and re-added would come back writable. The per-cycle check is the only thing that would see it. diff --git a/packages/omamobile-device-cheetah/bin/omamobile-boot-success b/packages/omamobile-device-cheetah/bin/omamobile-boot-success index 34946af88..11bedceeb 100755 --- a/packages/omamobile-device-cheetah/bin/omamobile-boot-success +++ b/packages/omamobile-device-cheetah/bin/omamobile-boot-success @@ -10,6 +10,8 @@ # devinfo's own flag opens devinfo alone, for the one write; it is set # again afterwards. omamobile-bootctl only ever changes the slot flag bytes # (0x30-0x37) and verifies the whole partition afterwards. +# bin/omamobile push and flash look for the next line; keep it exact. +# guard: partition flags (#252) set -euo pipefail slot=$(sed -n 's/^androidboot.slot_suffix = "_\([ab]\)"$/\1/p' /proc/bootconfig) diff --git a/packages/omamobile-device-cheetah/test/boot-success b/packages/omamobile-device-cheetah/test/boot-success index a74cc1e0d..23b7bfb41 100755 --- a/packages/omamobile-device-cheetah/test/boot-success +++ b/packages/omamobile-device-cheetah/test/boot-success @@ -21,6 +21,14 @@ script=${1:-bin/omamobile-boot-success} t=$(mktemp -d); trap '[[ -n ${KEEP:-} ]] || rm -rf "$t"' EXIT R=$t/r +# bin/omamobile push and flash recognise a marker that knows the guard by +# this exact line (#252) +grep -qxF '# guard: partition flags (#252)' "$script" || fail "the marker carries the guard line push and flash look for" +if [[ -f ../../bin/omamobile ]]; then + grep -qxF "MARKER_TAG='# guard: partition flags (#252)'" ../../bin/omamobile || fail "bin/omamobile looks for the same line" +fi +pass "the marker carries the line push and flash look for" + # The marker with its paths pointed into $R sed -e "s|/proc/bootconfig|$R/proc/bootconfig|g" -e "s|/sys/|$R/sys/|g" -e "s|/dev/|$R/dev/|g" \ "$script" > "$t/marker" @@ -142,3 +150,11 @@ rm "$R/sys/class/block/sdd3"; ln -s "../../block/sdd" "$R/sys/class/block/sdd3" st=0; run || st=$? [[ $st != 0 && ! -s $R/blockdev.log ]] || { cat "$t/out"; fail "a devinfo that resolves to a whole disk is refused before any flag changes (exit $st)"; } pass "a devinfo that isn't a partition of a LUN is refused before any flag changes" + +# No devinfo partition: refused before any flag changes +fresh new +sed -i 's/^PARTNAME=devinfo$/PARTNAME=notdevinfo/' "$R/sys/block/sdd/sdd3/uevent" +st=0; run || st=$? +[[ $st != 0 && ! -s $R/blockdev.log ]] && grep -q 'devinfo partition not found' "$t/out" \ + || { cat "$t/out"; fail "a phone without devinfo is refused before any flag changes (exit $st)"; } +pass "no devinfo partition: refused before any flag changes" diff --git a/packages/omamobile-device-cheetah/vendor/udev/rules.d/10-omamobile-storage.rules b/packages/omamobile-device-cheetah/vendor/udev/rules.d/10-omamobile-storage.rules index 89c02cf30..30206c818 100644 --- a/packages/omamobile-device-cheetah/vendor/udev/rules.d/10-omamobile-storage.rules +++ b/packages/omamobile-device-cheetah/vendor/udev/rules.d/10-omamobile-storage.rules @@ -1,8 +1,9 @@ # SPDX-License-Identifier: MIT # Omarchy Mobile storage guard, continued in Arch. The initramfs marks every UFS # partition read-only except slot B's boot images and userdata, and removes -# the whole-disk node of the main LUN. udev would recreate /dev/sda, so make -# the whole-disk node inaccessible and keep desktop tools from touching any -# Android partition. +# every LUN's whole-disk node. devtmpfs creates a node only when the kernel +# adds the device (device_add), so udev never brings one back; mode 0000 on +# /dev/sda is a second line should it ever reappear. UDISKS_IGNORE keeps +# desktop tools from touching any Android partition. SUBSYSTEM=="block", KERNEL=="sda", MODE="0000", ENV{UDISKS_IGNORE}="1" SUBSYSTEM=="block", KERNEL=="sd[a-d][0-9]*", ENV{ID_PART_ENTRY_NAME}!="userdata", ENV{UDISKS_IGNORE}="1" diff --git a/packages/omamobile-device-felix/vendor/udev/rules.d/10-omamobile-storage.rules b/packages/omamobile-device-felix/vendor/udev/rules.d/10-omamobile-storage.rules index 89c02cf30..30206c818 100644 --- a/packages/omamobile-device-felix/vendor/udev/rules.d/10-omamobile-storage.rules +++ b/packages/omamobile-device-felix/vendor/udev/rules.d/10-omamobile-storage.rules @@ -1,8 +1,9 @@ # SPDX-License-Identifier: MIT # Omarchy Mobile storage guard, continued in Arch. The initramfs marks every UFS # partition read-only except slot B's boot images and userdata, and removes -# the whole-disk node of the main LUN. udev would recreate /dev/sda, so make -# the whole-disk node inaccessible and keep desktop tools from touching any -# Android partition. +# every LUN's whole-disk node. devtmpfs creates a node only when the kernel +# adds the device (device_add), so udev never brings one back; mode 0000 on +# /dev/sda is a second line should it ever reappear. UDISKS_IGNORE keeps +# desktop tools from touching any Android partition. SUBSYSTEM=="block", KERNEL=="sda", MODE="0000", ENV{UDISKS_IGNORE}="1" SUBSYSTEM=="block", KERNEL=="sd[a-d][0-9]*", ENV{ID_PART_ENTRY_NAME}!="userdata", ENV{UDISKS_IGNORE}="1" diff --git a/packages/omamobile-device-husky/vendor/udev/rules.d/10-omamobile-storage.rules b/packages/omamobile-device-husky/vendor/udev/rules.d/10-omamobile-storage.rules index 89c02cf30..30206c818 100644 --- a/packages/omamobile-device-husky/vendor/udev/rules.d/10-omamobile-storage.rules +++ b/packages/omamobile-device-husky/vendor/udev/rules.d/10-omamobile-storage.rules @@ -1,8 +1,9 @@ # SPDX-License-Identifier: MIT # Omarchy Mobile storage guard, continued in Arch. The initramfs marks every UFS # partition read-only except slot B's boot images and userdata, and removes -# the whole-disk node of the main LUN. udev would recreate /dev/sda, so make -# the whole-disk node inaccessible and keep desktop tools from touching any -# Android partition. +# every LUN's whole-disk node. devtmpfs creates a node only when the kernel +# adds the device (device_add), so udev never brings one back; mode 0000 on +# /dev/sda is a second line should it ever reappear. UDISKS_IGNORE keeps +# desktop tools from touching any Android partition. SUBSYSTEM=="block", KERNEL=="sda", MODE="0000", ENV{UDISKS_IGNORE}="1" SUBSYSTEM=="block", KERNEL=="sd[a-d][0-9]*", ENV{ID_PART_ENTRY_NAME}!="userdata", ENV{UDISKS_IGNORE}="1" diff --git a/packages/omamobile-device-lynx/bin/omamobile-boot-success b/packages/omamobile-device-lynx/bin/omamobile-boot-success index 34946af88..11bedceeb 100755 --- a/packages/omamobile-device-lynx/bin/omamobile-boot-success +++ b/packages/omamobile-device-lynx/bin/omamobile-boot-success @@ -10,6 +10,8 @@ # devinfo's own flag opens devinfo alone, for the one write; it is set # again afterwards. omamobile-bootctl only ever changes the slot flag bytes # (0x30-0x37) and verifies the whole partition afterwards. +# bin/omamobile push and flash look for the next line; keep it exact. +# guard: partition flags (#252) set -euo pipefail slot=$(sed -n 's/^androidboot.slot_suffix = "_\([ab]\)"$/\1/p' /proc/bootconfig) diff --git a/packages/omamobile-device-lynx/vendor/udev/rules.d/10-omamobile-storage.rules b/packages/omamobile-device-lynx/vendor/udev/rules.d/10-omamobile-storage.rules index 89c02cf30..30206c818 100644 --- a/packages/omamobile-device-lynx/vendor/udev/rules.d/10-omamobile-storage.rules +++ b/packages/omamobile-device-lynx/vendor/udev/rules.d/10-omamobile-storage.rules @@ -1,8 +1,9 @@ # SPDX-License-Identifier: MIT # Omarchy Mobile storage guard, continued in Arch. The initramfs marks every UFS # partition read-only except slot B's boot images and userdata, and removes -# the whole-disk node of the main LUN. udev would recreate /dev/sda, so make -# the whole-disk node inaccessible and keep desktop tools from touching any -# Android partition. +# every LUN's whole-disk node. devtmpfs creates a node only when the kernel +# adds the device (device_add), so udev never brings one back; mode 0000 on +# /dev/sda is a second line should it ever reappear. UDISKS_IGNORE keeps +# desktop tools from touching any Android partition. SUBSYSTEM=="block", KERNEL=="sda", MODE="0000", ENV{UDISKS_IGNORE}="1" SUBSYSTEM=="block", KERNEL=="sd[a-d][0-9]*", ENV{ID_PART_ENTRY_NAME}!="userdata", ENV{UDISKS_IGNORE}="1" diff --git a/packages/omamobile-device-panther/vendor/udev/rules.d/10-omamobile-storage.rules b/packages/omamobile-device-panther/vendor/udev/rules.d/10-omamobile-storage.rules index 89c02cf30..30206c818 100644 --- a/packages/omamobile-device-panther/vendor/udev/rules.d/10-omamobile-storage.rules +++ b/packages/omamobile-device-panther/vendor/udev/rules.d/10-omamobile-storage.rules @@ -1,8 +1,9 @@ # SPDX-License-Identifier: MIT # Omarchy Mobile storage guard, continued in Arch. The initramfs marks every UFS # partition read-only except slot B's boot images and userdata, and removes -# the whole-disk node of the main LUN. udev would recreate /dev/sda, so make -# the whole-disk node inaccessible and keep desktop tools from touching any -# Android partition. +# every LUN's whole-disk node. devtmpfs creates a node only when the kernel +# adds the device (device_add), so udev never brings one back; mode 0000 on +# /dev/sda is a second line should it ever reappear. UDISKS_IGNORE keeps +# desktop tools from touching any Android partition. SUBSYSTEM=="block", KERNEL=="sda", MODE="0000", ENV{UDISKS_IGNORE}="1" SUBSYSTEM=="block", KERNEL=="sd[a-d][0-9]*", ENV{ID_PART_ENTRY_NAME}!="userdata", ENV{UDISKS_IGNORE}="1" diff --git a/packages/omamobile-device-raven/bin/omamobile-boot-success b/packages/omamobile-device-raven/bin/omamobile-boot-success index aab279b23..31b77d8b8 100755 --- a/packages/omamobile-device-raven/bin/omamobile-boot-success +++ b/packages/omamobile-device-raven/bin/omamobile-boot-success @@ -11,6 +11,8 @@ # devinfo's own flag opens devinfo alone, for the one write; it is set # again afterwards. omamobile-bootctl only ever changes the slot flag bytes # (0x30-0x37) and verifies the whole partition afterwards. +# bin/omamobile push and flash look for the next line; keep it exact. +# guard: partition flags (#252) set -euo pipefail slot=$(sed -n 's/^androidboot.slot_suffix = "_\([ab]\)"$/\1/p' /proc/bootconfig) diff --git a/packages/omamobile-device-raven/vendor/udev/rules.d/10-omamobile-storage.rules b/packages/omamobile-device-raven/vendor/udev/rules.d/10-omamobile-storage.rules index 89c02cf30..30206c818 100644 --- a/packages/omamobile-device-raven/vendor/udev/rules.d/10-omamobile-storage.rules +++ b/packages/omamobile-device-raven/vendor/udev/rules.d/10-omamobile-storage.rules @@ -1,8 +1,9 @@ # SPDX-License-Identifier: MIT # Omarchy Mobile storage guard, continued in Arch. The initramfs marks every UFS # partition read-only except slot B's boot images and userdata, and removes -# the whole-disk node of the main LUN. udev would recreate /dev/sda, so make -# the whole-disk node inaccessible and keep desktop tools from touching any -# Android partition. +# every LUN's whole-disk node. devtmpfs creates a node only when the kernel +# adds the device (device_add), so udev never brings one back; mode 0000 on +# /dev/sda is a second line should it ever reappear. UDISKS_IGNORE keeps +# desktop tools from touching any Android partition. SUBSYSTEM=="block", KERNEL=="sda", MODE="0000", ENV{UDISKS_IGNORE}="1" SUBSYSTEM=="block", KERNEL=="sd[a-d][0-9]*", ENV{ID_PART_ENTRY_NAME}!="userdata", ENV{UDISKS_IGNORE}="1" diff --git a/packages/omamobile-device-tegu/vendor/udev/rules.d/10-omamobile-storage.rules b/packages/omamobile-device-tegu/vendor/udev/rules.d/10-omamobile-storage.rules index 89c02cf30..30206c818 100644 --- a/packages/omamobile-device-tegu/vendor/udev/rules.d/10-omamobile-storage.rules +++ b/packages/omamobile-device-tegu/vendor/udev/rules.d/10-omamobile-storage.rules @@ -1,8 +1,9 @@ # SPDX-License-Identifier: MIT # Omarchy Mobile storage guard, continued in Arch. The initramfs marks every UFS # partition read-only except slot B's boot images and userdata, and removes -# the whole-disk node of the main LUN. udev would recreate /dev/sda, so make -# the whole-disk node inaccessible and keep desktop tools from touching any -# Android partition. +# every LUN's whole-disk node. devtmpfs creates a node only when the kernel +# adds the device (device_add), so udev never brings one back; mode 0000 on +# /dev/sda is a second line should it ever reappear. UDISKS_IGNORE keeps +# desktop tools from touching any Android partition. SUBSYSTEM=="block", KERNEL=="sda", MODE="0000", ENV{UDISKS_IGNORE}="1" SUBSYSTEM=="block", KERNEL=="sd[a-d][0-9]*", ENV{ID_PART_ENTRY_NAME}!="userdata", ENV{UDISKS_IGNORE}="1" diff --git a/tests/images-layout b/tests/images-layout index 1955f54ab..dfc68e29e 100755 --- a/tests/images-layout +++ b/tests/images-layout @@ -244,6 +244,51 @@ grep -q 'AVB_FOOTERS pads boot to 67108864 bytes, but boot_b' "$t/log" || { cat cp "$t/six.conf" "$R/device/six/device.conf" pass "push refuses an AVB_FOOTERS image whose partition is not the descriptor's size" +# The boot-success marker check (#252): for a device whose package ships +# omamobile-boot-success, push reads the phone's marker for the guard line +# and refuses an old or missing one before any write, and flash refuses +# without OMAMOBILE_MARKER_CHECKED=1; an initramfs with the old guard skips +# the check, an unreadable one doesn't. +mkdir -p "$R/packages/omamobile-device-six/bin" +: > "$R/packages/omamobile-device-six/bin/omamobile-boot-success" +sed -i 's|^ true\|\*.command -v reboot-to.\*) exit 0 ;;$|&\n *omamobile-boot-success*) echo "${MARKER:-1}" ;;|' "$R/tools/phone-ssh" +grep -q 'omamobile-boot-success\*) echo' "$R/tools/phone-ssh" || fail "the fake phone answers the marker check" +nothing_written() { [[ $(find "$P" -name 'sda_*' | wc -l) == 0 && ! -s $P/log ]]; } +push old new || { cat "$t/log"; fail "push accepts a phone whose marker knows the guard"; } +pass "push accepts a phone whose marker carries the guard line" +if MARKER=0 push old new; then fail "push refuses an old marker"; fi +grep -q 'predates these images. storage guard' "$t/log" && grep -q 'tools/phone-install-package packages/omamobile-device-six' "$t/log" \ + || { cat "$t/log"; fail "push says the marker is old and how to update it"; } +nothing_written || fail "an old marker: nothing written, no reboot" +pass "push refuses an old marker before writing anything, and names the package to install" +if MARKER=missing push old new; then fail "push refuses a missing marker"; fi +grep -q 'has no /usr/bin/omamobile-boot-success' "$t/log" || { cat "$t/log"; fail "push says the marker is missing"; } +nothing_written || fail "a missing marker: nothing written" +MARKER=0 OMAMOBILE_MARKER_CHECKED=1 push old new || { cat "$t/log"; fail "OMAMOBILE_MARKER_CHECKED=1 lets push go on"; } +if MARKER=0 OMAMOBILE_MARKER_CHECKED=yes push old new; then fail "only the value 1 overrides"; fi +pass "a missing marker is refused too; only OMAMOBILE_MARKER_CHECKED=1 overrides" +rm -f "$t/fastboot.log"; stub_images six boot vendor_boot dtbo +if om six flash; then fail "flash refuses without OMAMOBILE_MARKER_CHECKED=1"; fi +grep -q 'flash unmarks slot B' "$t/log" && grep -qF "grep -cxF '# guard: partition flags (#252)'" "$t/log" \ + || { cat "$t/log"; fail "flash says why and what to check"; } +[[ ! -s $t/fastboot.log ]] || fail "flash refuses before talking to fastboot" +OMAMOBILE_MARKER_CHECKED=1 om six flash || { cat "$t/log"; fail "flash goes on with OMAMOBILE_MARKER_CHECKED=1"; } +[[ $(flashed) == "boot_b vendor_boot_b dtbo_b " ]] || fail "the checked flash writes its images" +pass "flash refuses without OMAMOBILE_MARKER_CHECKED=1, before any fastboot call, and goes on with it" +if command -v lz4 >/dev/null; then + printf 'ufs_ro_pass() {\n' | lz4 -q -f - "$K/initramfs.cpio.lz4" + MARKER=0 push old new || { cat "$t/log"; fail "an initramfs with the old guard skips the marker check"; } + rm -f "$t/fastboot.log"; om six flash || { cat "$t/log"; fail "flash of an old-guard initramfs needs no override"; } + printf 'ufs_is_disk() {\n' | lz4 -q -f - "$K/initramfs.cpio.lz4" + if MARKER=0 push old new; then fail "an initramfs with the new guard is checked"; fi + printf 'not lz4\n' > "$K/initramfs.cpio.lz4" + if MARKER=0 push old new; then fail "an unreadable initramfs is checked"; fi + rm -f "$K/initramfs.cpio.lz4" + pass "only an initramfs that reads as the old guard skips the check" +fi +rm -rf "$R/packages" +stub_images six boot vendor_boot dtbo + # Building the images needs AOSP's mkbootimg and mkdtboimg, lz4 and a dtc; # without them only this part skips. mkbootimg=$SRC_DIR/system/tools/mkbootimg