From 7f57a927c3afd8050f98405044695e78f4b22c59 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Fri, 25 Sep 2026 15:08:06 -0500 Subject: [PATCH] feat(store): mount valid root as DegradedRecovery on malformed peer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implement the recovery-semantic amendment to System Store v1: when exactly one slot contains a fully integrity-validated root and the peer slot is physically malformed (invalid CRC, wrong magic, or decode-invalid shape), mount the surviving root as read-only DegradedRecovery. In-band transactions are prohibited. Unsupported versions and I/O failures continue to fail closed. Amend ADR 0015 ยง 5 item 8 with the ratified recovery semantics. Co-authored-by: AIEN local worker --- crates/aienos-kernel/src/store/engine.rs | 91 ++++++++++++++++++++++-- docs/adr/0015-system-store-v1-format.md | 2 +- 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/crates/aienos-kernel/src/store/engine.rs b/crates/aienos-kernel/src/store/engine.rs index 468be8ab..0b6f5571 100644 --- a/crates/aienos-kernel/src/store/engine.rs +++ b/crates/aienos-kernel/src/store/engine.rs @@ -160,13 +160,16 @@ impl Store { poisoned: false, }) } else if malformed[other] { - Err( - if unsupported_superblock(&raw[other]) && crc_valid(&raw[other]) { - MountError::UnsupportedVersion - } else { - MountError::CorruptRecoveryRequired - }, - ) + if unsupported_superblock(&raw[other]) && crc_valid(&raw[other]) { + Err(MountError::UnsupportedVersion) + } else { + Ok(Self { + device, + root, + state: MountState::DegradedRecovery, + poisoned: false, + }) + } } else if graph_bad[other] && crc_supported[other] { if generation_of(&raw[other]) > root.superblock.generation { Ok(Self { @@ -1262,4 +1265,78 @@ mod tests { assert_eq!(store.transact(&one(b"fault")), Err(StoreError::Io)); assert_eq!(store.transact(&one(b"again")), Err(StoreError::NeedsReopen)); } + + #[test] + fn store_malformed_peer_recovery_matrix() { + let rechecksum = |bytes: &mut [u8; STORE_UNIT_BYTES]| { + bytes[168..172].fill(0); + let crc = super::super::v1::crc32c(bytes); + bytes[168..172].copy_from_slice(&crc.to_le_bytes()); + }; + + // 1. Bad CRC peer: Valid root in slot 0 + bad CRC in slot 1 -> DegradedRecovery + let mut dev = seed_genesis(blank(32)); + dev.units[1][..8].copy_from_slice(b"AIENSTR1"); + dev.units[1][168] ^= 1; + let store = Store::open(dev).unwrap(); + assert_eq!(store.mount_state(), MountState::DegradedRecovery); + assert_eq!(store.generation(), 1); + + // 2. Wrong magic peer: Valid root in slot 0 + non-zero wrong magic in slot 1 -> DegradedRecovery + let mut dev = seed_genesis(blank(32)); + dev.units[1][..8].copy_from_slice(b"BADMAGIC"); + let store = Store::open(dev).unwrap(); + assert_eq!(store.mount_state(), MountState::DegradedRecovery); + assert_eq!(store.generation(), 1); + + // 3. Malformed shape peer: Valid root in slot 0 + non-zero reserved in slot 1 -> DegradedRecovery + let mut dev = seed_genesis(blank(32)); + let root = Superblock::decode(&dev.units[0], 0).unwrap(); + let mut peer = root; + peer.slot_id = 1; + dev.units[1] = peer.encode().unwrap(); + dev.units[1][200] = 0xff; // non-zero reserved byte + rechecksum(&mut dev.units[1]); + let store = Store::open(dev).unwrap(); + assert_eq!(store.mount_state(), MountState::DegradedRecovery); + assert_eq!(store.generation(), 1); + + // 4. Unsupported version peer: Valid root in slot 0 + CRC-valid major=2 in slot 1 -> UnsupportedVersion + let mut dev = seed_genesis(blank(32)); + let root = Superblock::decode(&dev.units[0], 0).unwrap(); + let mut peer = root; + peer.slot_id = 1; + dev.units[1] = peer.encode().unwrap(); + dev.units[1][8..10].copy_from_slice(&2u16.to_le_bytes()); // major = 2 + rechecksum(&mut dev.units[1]); + assert_eq!( + Store::open(dev).err().unwrap(), + MountError::UnsupportedVersion + ); + + // 5. I/O failure peer: Valid root in slot 0 + read error on slot 1 -> MountError::Io + let mut dev = seed_genesis(blank(32)); + dev.fail_read = Some(1); + assert_eq!(Store::open(dev).err().unwrap(), MountError::Io); + + // 6. Two bad roots: bad CRC on slot 0 and slot 1 -> MountError::CorruptRecoveryRequired + let mut dev = seed_genesis(blank(32)); + dev.units[0][168] ^= 1; + dev.units[1][..8].copy_from_slice(b"AIENSTR1"); + dev.units[1][168] ^= 1; + assert_eq!( + Store::open(dev).err().unwrap(), + MountError::CorruptRecoveryRequired + ); + + // 7. Transact on DegradedRecovery -> Err(StoreError::ReadOnlyDegraded) + let mut dev = seed_genesis(blank(32)); + dev.units[1][..8].copy_from_slice(b"BADMAGIC"); + let mut store = Store::open(dev).unwrap(); + assert_eq!(store.mount_state(), MountState::DegradedRecovery); + assert_eq!( + store.transact(&one(b"blocked mutation")), + Err(StoreError::ReadOnlyDegraded) + ); + } } diff --git a/docs/adr/0015-system-store-v1-format.md b/docs/adr/0015-system-store-v1-format.md index 1f3ddd4e..7e3acc7f 100644 --- a/docs/adr/0015-system-store-v1-format.md +++ b/docs/adr/0015-system-store-v1-format.md @@ -153,7 +153,7 @@ The two superblock units MUST both be read. Any actual read error while reading 5. If both roots validate and have the same generation, equivalent roots are redundant valid copies; non-equivalent roots are `ConflictingRoots`. 6. If both roots validate and generations differ by exactly one, they MUST have the same Store UUID and region geometry. The newer root is normal only when its predecessor generation, CommitRecord ID, and catalog ID identify the older root exactly. Otherwise classify `InconsistentHistory`. 7. If both roots validate but are not adjacent, classify `InconsistentHistory`. -8. If one root is completely valid and the other is all zero, the valid root is a valid Store. If both superblocks are CRC-valid and supported, and one is completely valid while the strictly newer root has a graph error, the valid older root MUST be exposed read-only as `DegradedRecovery`. If a newer root is completely valid while the older root has a graph error, select the newer valid root as a valid Store. A graph-invalid root at the same generation as the valid root, an invalid-CRC root whose generation cannot be trusted, a nonzero wrong-magic unit, or a valid Store paired with unsupported-version metadata classifies `Corrupt/RecoveryRequired` or `UnsupportedVersion` as specified above; do not silently fall back based only on readability. +8. If one root is completely valid and the other is all zero, the valid root is a valid Store. If both superblocks are CRC-valid and supported, and one is completely valid while the strictly newer root has a graph error, the valid older root MUST be exposed read-only as `DegradedRecovery`. If a newer root is completely valid while the older root has a graph error, select the newer valid root as a valid Store. If one root is completely valid and the peer slot has an invalid CRC, wrong magic, or malformed superblock structure (and does not constitute a CRC-valid unsupported format), the valid root MUST be exposed read-only as `DegradedRecovery`; in-band transactions MUST be prohibited while mounted in `DegradedRecovery`; any subsequent repair, replacement, or reprovisioning requires separately authorized out-of-band recovery authority. A graph-invalid root at the same generation as the valid root, or a valid Store paired with a CRC-valid unsupported-version superblock, MUST classify as `Corrupt/RecoveryRequired` or `UnsupportedVersion` as specified above; implementations MUST NOT silently fall back to a writable mount based only on single-slot readability. When both roots validate, the older/newer comparison uses generation without wrapping; overflow is invalid. A valid graph with a generation relationship error is history-inconsistent, not object-corrupt. Store v1 provides crash/corruption recovery and MUST NOT claim malicious full-device anti-rollback protection; trusted anti-rollback belongs to M5.