diff --git a/AGENTS.md b/AGENTS.md index 796b11262..63d0c1d25 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -13,7 +13,7 @@ Omarchy Mobile runs native Omarchy (Arch Linux ARM, Hyprland, Omarchy's packages - **`tools/install-phone`, and the installer `omamobile-install` under the same rules, change the bootloader's slot flags in two named cases.** `bin/omamobile flash`'s `--set-active=b` and the boot-success mark (`omamobile-boot-success`) stay as they are; nothing else gains one. The first is its `fastboot --set-active=b` after the writes. The second is a round trip that reads the other slot's bootloader version where the bootloader reports only the current slot's ([INSTALL-EXPERIENCE-PLAN](docs/INSTALL-EXPERIENCE-PLAN.md) G4, D4, decided 2026-11-10): `--set-active=`, `reboot bootloader`, read `version-bootloader`, `--set-active=`, `reboot bootloader`, check. It is off unless the device's descriptor says `BOOTLOADER_SLOT_PROBE=yes`, a line the maintainer adds after an attended run on that device is in its notes; no device has the line, and the round trip has not run on any phone. It runs only through `tools/install-phone` (which `tools/install-guided` calls with the owner's yes) and `omamobile-install` (derekross/omamobile-installer: its `install --write`, `json --write` and the web page's write preview; never a dry run and never the page that ships; the descriptor line read from the release's signed `device.conf`, and the device on its own list, which no Qualcomm phone is on), and only on what the tool can see of a first install: the phone on slot A, no `--reinstall`, slot B not reading as a slot that has started and can still start (`slot-successful` yes with `slot-unbootable` no: that is an installed Omarchy Mobile sent back to slot A, and also stock after a system update, and fastboot can't tell them apart), and the owner's yes, typed on a terminal or passed by the calling tool as `--measure-other-slot`. It flashes and erases nothing, but it is a write: the bootloader keeps the slot flags in its own storage (`devinfo` on our Pixels), and both slots' flags change. Stock's slot A may lose its successful mark and, once the install has taken `userdata`, may never earn it back; what that costs is not measured. It also starts the other slot's bootloader before its version is known, which is what the check exists to prevent: [Google's factory images page](https://developers.google.com/android/images) warns that on the Pixel 6, Pixel 8 and Pixel 10 families an older bootloader in the inactive slot can leave the phone unbootable, so a phone of those families gets the line only by the maintainer's separate decision. If a step fails while the phone still answers, the installer sets the first slot active again; a phone not seen to leave the bus and come back gets nothing more, and the owner gets the buttons and the command, which are also printed before the question. It never runs with `--reinstall`: `--set-active` clears a slot's successful mark, which with slot B ours costs boot tries on the Pixel 9a and can end in a fallback that erases `userdata` (boot chain notes below; on the 9a, ask the maintainer before any fastboot write to slot B, a first install's included). The installer can't see what is on slot B, so pass `--reinstall` whenever it holds Omarchy Mobile. Everywhere else it stops and asks for `--bootloader-slots-match VERSION`, as before. - **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 cf08a8271..e6fd3d7eb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -84,11 +84,11 @@ Phones with an entry: 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`), and on a LUN the descriptor's `UFS_RW_LUNS` names: 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. With an NFS root nothing on the UFS is written: the allowlist isn't loaded, and `sda` keeps its node and is read-only at the disk level as well. +- 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 fb234d0e9..e18a3e172 100755 --- a/bin/omamobile +++ b/bin/omamobile @@ -93,6 +93,10 @@ Usage: [DEVICE=] omamobile (--from-slot-a: a development phone that fell back to stock on slot A) 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 } @@ -265,6 +269,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 "* ]]; } @@ -1257,6 +1283,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 cur serial=$(fastboot_serial) || exit 1 @@ -1336,6 +1369,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 new file mode 100644 index 000000000..d568d91a1 --- /dev/null +++ b/device/cheetah/STORAGE-GUARD-PLAN.md @@ -0,0 +1,175 @@ + +# 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. + +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): 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 §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. + +### 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: `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 `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) + +```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. 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)" + 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, and the timer 30 s later if the session dropped). + +### 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) + +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. + +```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 +``` + +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 f0ffb0e8e..e9393563e 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/docs/RELEASES.md b/docs/RELEASES.md index 9ace1d840..17d926774 100644 --- a/docs/RELEASES.md +++ b/docs/RELEASES.md @@ -82,7 +82,7 @@ Bring-up tools (`*-stage`, `display-hz`, `usbc-deadman`) stay per plan: they pok | `BOOT_IMAGES` | `boot init_boot vendor_boot vendor_kernel_boot dtbo` | The images written to slot B (the `_b` suffix is added by the tools and is not configurable). `boot`, `vendor_boot` and `dtbo` are always there. Without `init_boot`, `boot` carries the empty generic ramdisk and the OS version; without `vendor_kernel_boot`, `vendor_boot` carries the DTB and the initramfs (the Pixel 6: `boot vendor_boot dtbo`) | | `BOOT_UPDATE_AUTO_WRITE` | (unset; defaults to `yes`) | Optional: `yes` or `no`. The installer records it as `AUTO_WRITE` in `/etc/omamobile/boot-update.conf`; `no` keeps an installed development root from writing slot B through package updates. It does not change the installer's own allowed writes | | `UFS_RW` | `boot_b … dtbo_b userdata` | The storage guard's allowlist, packed into the initramfs as `/etc/omamobile-ufs-rw`. Only slot B's boot images (`boot_b`, `init_boot_b`, `vendor_boot_b`, `vendor_kernel_boot_b`, `dtbo_b`) and `userdata` are accepted, `userdata` and every `BOOT_IMAGES` entry must be on it, and each name only once. The init fails closed: a missing, empty or malformed file, or any other name in it, leaves every partition read-only | -| `UFS_RW_LUNS` | (unset) | Optional. The LUNs besides `sda` that hold `UFS_RW` partitions, as disk names `sdb` to `sdz`, packed as `/etc/omamobile-ufs-rw-luns`. The init guards each as it guards `sda`: the whole-disk node removed, the disk left writable, and only its `UFS_RW` partitions left writable. Absent (every descriptor so far), malformed, or with a closed allowlist: every LUN but `sda` stays read-only at the disk level. Boot-image writers on the phone (`omamobile-boot-update`) still look only on `sda` | +| `UFS_RW_LUNS` | (unset) | Optional. The LUNs besides `sda` that hold `UFS_RW` partitions, as disk names `sdb` to `sdz`, packed as `/etc/omamobile-ufs-rw-luns`. The init guards every LUN alike (the whole-disk node removed, the disk's own flag clear, each partition's own flag set, #252); on a listed LUN its `UFS_RW` partitions are left writable, as on `sda`. Absent (every descriptor so far), malformed, or with a closed allowlist: every partition off `sda` stays read-only. Boot-image writers on the phone (`omamobile-boot-update`) still look only on `sda` | | `INITRAMFS_INIT` | `/initramfs/init` | The init script. A leading `/` is relative to the repo root, so devices share one script. Any other value is relative to the descriptor | | `INITRAMFS_ROOT_LABEL` | (unset) | Optional: `omamobile-root` enables read-only label/header checks before ext4 repair or mounting; only `e2fsck -p` statuses 0 and 1 proceed. Omitted: existing root handling, including unlabelled hand-installed roots, is unchanged | | `INITRAMFS_ROOT_MODE` | (unset) | Optional: `persistent-development` or `ram-rescue`, requiring `INITRAMFS_ROOT_LABEL=omamobile-root`. The former skips growth and writes `/var/lib/omamobile/root-boot-pending` before handover; an installed, enabled SSH-ready helper must clear it after USB SSH is ready, or subsequent boots stay in RAM until the marker is cleared. The latter always stays in RAM. Omitted: existing handover behavior | diff --git a/initramfs/init b/initramfs/init index 810799965..34753420d 100755 --- a/initramfs/init +++ b/initramfs/init @@ -46,14 +46,20 @@ fi # Nothing in this initramfs runs mdev/udev, so no one else touches the disks. 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: the running slot'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; a LUN the descriptor's UFS_RW_LUNS names is guarded the same way. -# The other LUNs (on Pixels sdb, sdc, sdd: bootloader images) are read-only -# at the disk level. Calibration partitions (efs, persist, modem_userdata...) -# and all of the other slot stay read-only. +# partitions we own: the running slot's boot images (in-place kernel updates) and +# 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), a LUN the descriptor's +# UFS_RW_LUNS names and the other LUNs (on Pixels sdb, sdc, sdd: bootloader +# images, devinfo) differ only in that the allowlist can name partitions of +# sda and of the listed LUNs 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 the other slot stay read-only. With +# an NFS root nothing on the UFS is ours to write: the allowlist is not +# loaded, and sda is read-only at the disk level as well. # 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 @@ -170,6 +176,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 @@ -177,8 +185,8 @@ ufs_ro_pass() { name=${s##*/} [ -e "/run/ufs-ro/$name" ] && continue [ -b "/dev/$name" ] || continue # devtmpfs node not there yet: next pass - if ufs_rw_disk "$name"; then - if [ -s /etc/omamobile-nfsroot ]; then + if ufs_is_disk "$s"; then + if ufs_rw_disk "$name" && [ -s /etc/omamobile-nfsroot ]; then blockdev --setro "/dev/$name" read -r ro < "$s/ro" if [ "$ro" = 1 ]; then @@ -190,7 +198,16 @@ ufs_ro_pass() { echo "omamobile: WARNING: /dev/$name is NOT read-only" > /dev/tty0 2>/dev/null continue fi - rm -f "/dev/$name" + # 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/$name: whole-disk node removed (partitions guarded individually)" continue @@ -214,14 +231,22 @@ 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##*/} - ufs_rw_disk "$name" && [ ! -s /etc/omamobile-nfsroot ] && continue + if ufs_is_disk "$s"; then + if ufs_rw_disk "$name" && [ -s /etc/omamobile-nfsroot ]; then + read -r ro < "$s/ro" + [ "$ro" = 0 ] && echo "$name" + else + [ -b "/dev/$name" ] && echo "$name (whole-disk node)" + fi + continue + fi read -r ro < "$s/ro" [ "$ro" = 0 ] || continue part=$(ufs_partname "$s") @@ -416,7 +441,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-akita/vendor/udev/rules.d/10-omamobile-storage.rules b/packages/omamobile-device-akita/vendor/udev/rules.d/10-omamobile-storage.rules index 89c02cf30..30206c818 100644 --- a/packages/omamobile-device-akita/vendor/udev/rules.d/10-omamobile-storage.rules +++ b/packages/omamobile-device-akita/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-cheetah/bin/omamobile-boot-success b/packages/omamobile-device-cheetah/bin/omamobile-boot-success index 58514e116..11bedceeb 100755 --- a/packages/omamobile-device-cheetah/bin/omamobile-boot-success +++ b/packages/omamobile-device-cheetah/bin/omamobile-boot-success @@ -5,9 +5,13 @@ # 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. +# 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) @@ -18,16 +22,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 f16557f69..a9b20663e 100755 --- a/packages/omamobile-device-cheetah/test/all +++ b/packages/omamobile-device-cheetah/test/all @@ -18,6 +18,10 @@ done sh -n vendor/systemd/system-generators/omamobile-charger-mode-generator || fail "the charger-mode generator parses" 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..23b7bfb41 --- /dev/null +++ b/packages/omamobile-device-cheetah/test/boot-success @@ -0,0 +1,160 @@ +#!/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 + +# 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" +[[ $(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" + +# 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 58514e116..11bedceeb 100755 --- a/packages/omamobile-device-lynx/bin/omamobile-boot-success +++ b/packages/omamobile-device-lynx/bin/omamobile-boot-success @@ -5,9 +5,13 @@ # 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. +# 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) @@ -18,16 +22,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/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-oriole/vendor/udev/rules.d/10-omamobile-storage.rules b/packages/omamobile-device-oriole/vendor/udev/rules.d/10-omamobile-storage.rules index 89c02cf30..30206c818 100644 --- a/packages/omamobile-device-oriole/vendor/udev/rules.d/10-omamobile-storage.rules +++ b/packages/omamobile-device-oriole/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 76efb5d7f..31b77d8b8 100755 --- a/packages/omamobile-device-raven/bin/omamobile-boot-success +++ b/packages/omamobile-device-raven/bin/omamobile-boot-success @@ -6,9 +6,13 @@ # # 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. +# 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) @@ -19,16 +23,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 de62c046a..8ede26bae 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/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-raven/version b/packages/omamobile-device-raven/version index 072d0fa39..9f42295fc 100644 --- a/packages/omamobile-device-raven/version +++ b/packages/omamobile-device-raven/version @@ -1 +1 @@ -0.1.36 +0.1.37 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 2c1099426..80185f642 100755 --- a/tests/images-layout +++ b/tests/images-layout @@ -281,6 +281,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 diff --git a/tests/storage-guard b/tests/storage-guard index 7a07a206a..299bf27d5 100755 --- a/tests/storage-guard +++ b/tests/storage-guard @@ -6,16 +6,19 @@ # 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. A LUN named in UFS_RW_LUNS (a -# test descriptor's sde, enchilada's layout) is guarded as sda is; without -# that list, or with a bad one or a closed allowlist, it stays read-only at -# the disk level. The packed list is in slot form (ACTIVE-SLOT-PLAN §4): -# booted from slot B (androidboot.slot_suffix in /proc/bootconfig, else -# /proc/cmdline) it holds as packed; from slot A each X_b becomes X_a and -# slot B stays read-only; with no readable suffix (none, two that disagree, -# another value) only userdata is writable. bin/omamobile packs the marker -# /etc/omamobile-ufs-rw-slot ("running"); any other content closes the guard. +# 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). A LUN named in UFS_RW_LUNS +# (a test descriptor's sde, enchilada's layout) may hold allowlisted +# partitions as sda does; without that list, or with a bad one or a closed +# allowlist, every partition on it is read-only. The packed list is in slot +# form (ACTIVE-SLOT-PLAN §4): booted from slot B (androidboot.slot_suffix in +# /proc/bootconfig, else /proc/cmdline) it holds as packed; from slot A each +# X_b becomes X_a and slot B stays read-only; with no readable suffix (none, +# two that disagree, another value) only userdata is writable. bin/omamobile +# packs the marker /etc/omamobile-ufs-rw-slot ("running"); any other content +# closes the guard. set -euo pipefail cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." fail() { printf 'not ok - %s\n' "$1" >&2; exit 1; } @@ -91,8 +94,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" @@ -105,42 +111,55 @@ grep -c "$B/proc/bootconfig" "$t/guard-test.sh" >/dev/null && grep -c "$B/proc/c 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" +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 +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" parts+=" init_boot_a vendor_boot_a vendor_kernel_boot_a dtbo_a modem_a vbmeta_a" -# fake_disks [SLOT]: the boot runs from slot B unless SLOT says otherwise -# (bootconfig as the Pixels pass it; "none" for no suffix at all) +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" +lun_parts="boot_a boot_b dtbo_b vbmeta_b" +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 [SLOT]: 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. The boot runs from slot B unless SLOT says otherwise (bootconfig +# as the Pixels pass it; "none" for no suffix at all) fake_disks() { - rm -rf "$B" && mkdir -p "$B/dev" "$B/etc" "$B/proc" "$B/sys/block/sda" "$B/sys/block/sdb/sdb1" + rm -rf "$B" && mkdir -p "$B/dev" "$B/etc" "$B/own" "$B/proc" "$B/sys/block" + : > "$B/blockdev.log" printf 'androidboot.hardware = "test"\n' > "$B/proc/bootconfig" [[ ${1:-_b} == none ]] || printf 'androidboot.slot_suffix = "%s"\n' "${1:-_b}" >> "$B/proc/bootconfig" printf 'console=ttynull panic=10\n' > "$B/proc/cmdline" - 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" - 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" + 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 # A LUN that holds slot B's boot image beside slot A's and vbmeta (enchilada) - mkdir -p "$B/sys/block/sde"; echo 0 > "$B/sys/block/sde/ro" - : > "$B/dev/sde"; i=0 - for p in boot_a boot_b dtbo_b vbmeta_b; do - i=$((i + 1)) - mkdir -p "$B/sys/block/sde/sde$i" - printf 'PARTNAME=%s\n' "$p" > "$B/sys/block/sde/sde$i/uevent" - echo 0 > "$B/sys/block/sde/sde$i/ro"; : > "$B/dev/sde$i" - done + mkdisk sde + i=0 + for p in $lun_parts; do i=$((i + 1)); mkpart sde "sde$i" "$p"; done + "$t/bin/sync-ro" } # GUARD_SH: the shell the guard runs under (default sh; the phone's is # busybox ash, e.g. GUARD_SH="busybox sh") @@ -148,25 +167,29 @@ GSH=${GUARD_SH:-sh} guard() { # run the guard's load and one pass PATH="$t/bin:$PATH" $GSH -c ". '$t/guard-test.sh'; ufs_ro_pass" } -writable() { # names of the partitions left writable on a disk (sda), in GPT order +writable() { # names of the partitions left writable, in order: on every LUN, or on the one named local s out="" - for s in "$B"/sys/block/${1:-sda}/${1:-sda}*; do + for s in "$B"/sys/block/${1:-sd*}/sd*; do [[ $(cat "$s/ro") == 0 ]] && out+="$(sed -n 's/^PARTNAME=//p' "$s/uevent") " done printf '%s' "${out% }" } sorted() { tr ' ' '\n' | sort | tr '\n' ' ' | sed 's/ $//'; } disk_checks() { # $1: case name; $2: sde when sde is a listed LUN - [[ ! -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 '/dev/sda: whole-disk node removed' "$B/dev/kmsg" || fail "$1: the sda removal is logged" - if [[ ${2:-} == sde ]]; then - [[ ! -e $B/dev/sde && $(cat "$B/sys/block/sde/ro") == 0 ]] || fail "$1: sde is guarded as sda is" - else - [[ -e $B/dev/sde && $(cat "$B/sys/block/sde/ro") == 1 && -z $(writable sde) ]] \ - || fail "$1: an unlisted LUN is read-only, boot_b on it too (writable: $(writable sde))" + local d s + for d in sda sdb sdc sdd sde; 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 + if [[ ${2:-} != sde ]]; then + [[ -z $(writable sde) ]] || fail "$1: an unlisted LUN's partitions are read-only, boot_b on it too (writable: $(writable sde))" fi + ! grep -qE ' sd[a-z]+$' "$B/blockdev.log" || fail "$1: blockdev never touches a whole disk ($(tr '\n' ' ' < "$B/blockdev.log"))" } fake_disks @@ -246,6 +269,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 sdf; mkpart sdf sdf1 userdata; mkpart sdf sdf2 boot_b; "$t/bin/sync-ro" +[[ $(unguarded | tr '\n' ' ') == "sdf (whole-disk node) sdf1 (userdata) sdf2 (boot_b) " ]] \ + || fail "a late LUN is reported before the next pass (got: $(unguarded | tr '\n' ' '))" +guard +[[ ! -e $B/dev/sdf && $(cat "$B/own/sdf") == 0 && $(cat "$B/own/sdf1") == 1 && $(cat "$B/own/sdf2") == 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-cheetah/omamobile-ufs-rw" "$B/etc/omamobile-ufs-rw" printf '172.16.42.2:/srv/nfs/dipper\n' > "$B/etc/omamobile-nfsroot" @@ -253,12 +325,17 @@ guard [[ -z $(writable) ]] || fail "NFS root's first pass leaves all UFS partitions read-only" ! grep -q 'ufs-ro: allowlist: ' "$B/dev/kmsg" || fail "NFS mode skips the packed allowlist" [[ $(cat "$B/sys/block/sda/ro") == 1 ]] || fail "NFS root marks the main UFS disk read-only" -[[ $(cat "$B/sys/block/sdb/ro") == 1 && $(cat "$B/sys/block/sdb/sdb1/ro") == 1 ]] || fail "NFS root marks boot LUNs read-only" +for d in sdb sdc sdd sde; do + [[ ! -e $B/dev/$d && $(cat "$B/own/$d") == 0 ]] || fail "NFS root guards $d as ever: the node removed, the disk flag clear" + for s in "$B"/sys/block/$d/sd*; do + [[ $(cat "$B/own/${s##*/}") == 1 ]] || fail "NFS root sets ${s##*/}'s own flag" + done +done grep -q 'NFS root requested: every UFS partition read-only' "$B/dev/kmsg" || fail "NFS mode is selected before first guard pass" [[ -z $(unguarded) ]] || fail "NFS root handover has no writable UFS device" echo 0 > "$B/sys/block/sda/ro" [[ $(unguarded) == "sda" ]] || fail "NFS mode reports a writable main UFS disk" -pass "NFS-root mode protects the main disk and every partition on the first guard pass" +pass "NFS-root mode protects the main disk and every partition on the first guard pass, the other LUNs by their partitions' own flags" awk '/^boot_nfs\(\) \{/ {on = 1} on && /omamobile.initramfs/ {marker = NR} on && /switch_root/ {handover = NR} on && /^}/ {exit} END {exit !(marker && handover && marker < handover)}' initramfs/init \ || fail "boot_nfs honors omamobile.initramfs before switching roots" @@ -306,18 +383,19 @@ fake_disks cp "$t/etc-lun/omamobile-ufs-rw" "$t/etc-lun/omamobile-ufs-rw-luns" "$B/etc/" guard [[ $(writable sde) == boot_b ]] || fail "lun: only boot_b is writable on sde (got: $(writable sde))" -[[ $(writable) == "boot_b userdata" ]] || fail "lun: its names on sda (got: $(writable))" +[[ $(writable sda) == "boot_b userdata" ]] || fail "lun: its names on sda (got: $(writable sda))" +[[ -z $(writable 'sd[b-d]') ]] || fail "lun: nothing on the boot LUNs (got: $(writable 'sd[b-d]'))" grep -q "LUNs with allowlisted partitions: sda sde" "$B/dev/kmsg" || fail "lun: the LUN list is logged" disk_checks lun sde [[ -z $(unguarded) ]] || fail "lun: nothing outside the allowlist is writable (got: $(unguarded))" -echo 0 > "$B/sys/block/sde/sde4/ro" +PATH="$t/bin:$PATH" blockdev --setrw "$B/dev/sde4" [[ $(unguarded) == "sde4 (vbmeta_b)" ]] || fail "lun: a reopened partition on sde is reported (got: $(unguarded))" -pass "the lun lists: sde guarded as sda, boot_b alone writable there; the hand-over check covers sde" +pass "the lun lists: boot_b alone writable on sde; the hand-over check covers sde" fake_disks _a cp "$t/etc-lun/omamobile-ufs-rw" "$t/etc-lun/omamobile-ufs-rw-luns" "$B/etc/" guard -[[ $(writable sde) == boot_a && $(writable) == "boot_a userdata" ]] || fail "lun on slot A: boot_a on sde and sda (got: sde $(writable sde), sda $(writable))" +[[ $(writable sde) == boot_a && $(writable sda) == "boot_a userdata" ]] || fail "lun on slot A: boot_a on sde and sda (got: sde $(writable sde), sda $(writable sda))" disk_checks "lun on slot A" sde pass "the lun lists on slot A: boot_a writable on sde, boot_b there read-only" @@ -325,7 +403,7 @@ fake_disks cp "$t/etc-lun/omamobile-ufs-rw" "$B/etc/" guard disk_checks "lun without its LUN list" -pass "without UFS_RW_LUNS, sde stays read-only at the disk level, as before" +pass "without UFS_RW_LUNS, every partition on sde stays read-only, boot_b too" for bad in 'sde sda' 'sde;sdf' '/dev/sde' 'sde1' 'SDE'; do fake_disks @@ -336,7 +414,7 @@ for bad in 'sde sda' 'sde;sdf' '/dev/sde' 'sde1' 'SDE'; do disk_checks "LUN list '$bad'" [[ $(writable) == "boot_b userdata" ]] || fail "LUN list '$bad': sda keeps its allowlist (got: $(writable))" done -pass "a malformed LUN list leaves every LUN but sda read-only" +pass "a malformed LUN list opens nothing off sda" fake_disks printf 'boot_b userdata persist\n' > "$B/etc/omamobile-ufs-rw"; printf 'sde\n' > "$B/etc/omamobile-ufs-rw-luns"