Skip to content

vaultmgr: recover a wrong vault key mode - #6451

Merged
eriknordmark merged 13 commits into
lf-edge:masterfrom
eriknordmark:vault-mode-recovery
Sep 26, 2026
Merged

eriknordmark merged 13 commits into
lf-edge:masterfrom
eriknordmark:vault-mode-recovery

Conversation

@eriknordmark

@eriknordmark eriknordmark commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

A device whose recorded vault key derivation does not match its vault locks
itself out permanently, and nothing on the device recovers it.

/persist/status/vaultmgr/VaultConfig records one bit: whether the vault key
is the TPM key alone, or that key merged with a build-time constant (the
pre-7.10.0 form). The mode is sticky — an existing fscrypt protector or ZFS
key cannot be re-derived from a different key — so a wrong bit means the key
EVE builds is not the key the vault accepts. The failure does not look like
what it is:

  • the TPM unseal succeeds and only fscrypt refuses, so it reads as a broken
    seal (Vault key unavailable, mismatchingPCRs=[]);
  • the controller-key recovery re-seals and fails identically;
  • the device parks in maintenance mode with
    MaintenanceModeReasonVaultLockedUp;
  • nothing tries the other derivation, so a reboot lands in the same place.

Two paths put a wrong bit there.

A /persist repair. /persist is mutable and can come back from a boot
only partially intact: a power loss or reset mid-write leaves a filesystem
e2fsck then repairs, and that repair is free to clear directory entries. Most
consumers of /persist survive that — pubsub state is rebuilt, caches are
refetched, onboot.sh discards several trees outright. VaultConfig did not.
With nothing to read, the mode was derived from whether /persist/vault
exists, which for an existing vault always answers "merged" — wrong for every
device installed since 7.10.0 — and the guess was written out before anything
tried to unlock with it. One reboot turned a recoverable state into a permanent
one, and a device in that state stays there across an upgrade, because the file
it reads back looks like a record rather than a guess.

A vault recreate. The RemoveDefaultVault() + SetupDefaultVault()
fallback keys its replacement with the TPM key alone, whatever the destroyed
vault used, leaving whatever was recorded describing a vault that no longer
exists.

What changes

The recorded mode becomes the derivation to try first rather than a fact:

  • A failed unlock is retried against the other derivation
    (resolveKeyMode, pkg/pillar/vault/keymode.go), whatever the first one came
    from. This is safe — a key that does not match changes nothing on disk, and
    stageKey shreds it — and it is the only thing that separates a wrong mode
    from a genuinely unopenable vault, since both surface identically. When
    neither derivation works the error names both rather than blaming either.
  • The derivation that opened the vault is persisted whenever it disagrees with
    what is on disk, so the recreate path rewrites a stale record instead of
    leaving it.
  • With no record at all the first attempt is the TPM key alone, which is how
    every vault created since 7.10.0 is keyed. The filesystem probe that used to
    guess is gone, and so is HandlerOptions.TpmKeyOnlyModeInferred — nothing
    needs to know where a mode came from any more.
  • VaultStatus.KeyDerivation follows the same resolution.

ZFS retries just the key load, not the mount — a mount failure is not a key
problem.

The config bookkeeping stays rather than making the two-derivation probe the
only path: without a record, every boot on a pre-7.10.0 device pays a failed
unseal and a failed fscrypt unlock before reaching the working derivation,
and its log then reads like a seal problem on every boot.

Note on blast radius, since it is easy to over-read: the cost of a wrong mode
is a locked vault, not a wipe. The recreate path needs an empty
EncryptedVaultKeyFromController and the
/persist/status/allow-vault-clean sentinel, which DisallowVaultCleanup()
removes at the first content tree/volume or successful attest escrow — so it is
unreachable for any device with data to lose, exactly as designed.

Why the old key scheme has to stay

Collapsing the two derivations — assuming TpmKeyOnly=true and deleting this
class of problem rather than making it recoverable — is not available. A small
but non-zero percentage of deployed production devices still unlock a vault
created with the merged key, which shows up in their logs as Calling mergeKeys. Dropping support for the old scheme would leave exactly those
devices unable to open their vault.

PR dependencies

Depends on #6530, and is branched on top of it: the diff and the checks below
cover both until that one merges.

Both change the block in vaultmgr.go that builds HandlerOptions, from
opposite directions -- #6530 hoists it out of the tpmEnabled branch so a
device without a TPM still reports its partition state, this one replaces the
key-mode probe feeding it. Resolved here rather than at merge time, since the
two ways of getting it wrong both compile: dropping
CurrentPartitionCommitted, and putting SetHandlerOptions back inside the
tpmEnabled branch. The vault recreate path reads the options back and changes
only the key mode, so it cannot drop a field it does not know about.

How to test and validate this PR

Automated. pkg/pillar/vault/keymode_test.go covers the decision logic: a
working mode costs no second attempt, a wrong one is retried whatever its
provenance, the mode that opened the vault is the one reported and stored back,
a double failure reports both errors, and an unresolved mode leaves the options
untouched so a caller cannot persist one on the strength of a failed unlock.

pkg/pillar/cmd/vaultmgr/keymode_test.go covers the branch feeding it: a
recorded mode is what gets tried first, TPM-key-only is the answer without one,
the resolved mode replaces a record that disagrees, and nothing is recorded
without a TPM.

Negative controls, both exercised: with the retry removed from resolveKeyMode,
TestResolveKeyModeRetriesARecordedMode,
TestResolveKeyModeRecoversFromAWrongPreference and
TestResolveUnlockRecordsTheModeThatWorked fail on their own assertions; with
recordVaultKeyMode restored to writing only when nothing is recorded,
TestRecordVaultKeyModeOverwritesAStaleMode fails on its.

On a device. evetest/tests/security/vault_keymode_test.go
(TestVaultKeyModeRecovery, one variant per /persist filesystem) injects the
fault and checks the recovery end to end: it overwrites the recorded mode with
the merged derivation, reboots through the controller, and requires the device's
own /persist/newlog to show that mode failing and the other one opening the
vault. A marker written into the vault beforehand separates a recovered vault
from a recreated empty one, VaultStatus.UnlockMethod has to be the local TPM
seal rather than the controller-key fallback, the corrected mode has to be
persisted, and a second reboot has to open the vault on it with no retry.

The fault records a wrong bit rather than removing the file because a device
with no record starts from the TPM key alone and opens such a vault on the first
attempt, exercising nothing.

The test settles the vault seal before injecting anything. A device seals on its
first boot, before onboarding writes the per-device /config files that
measure-config records into PCR14, so boot 2 always unlocks over the controller
key with mismatchingPCRs=[14]; injecting the fault there would measure that
re-seal rather than this change. Those settle boots double as the regression
check on the ordinary path — the record matches, and nothing retries.

The expected device-side sequence is:

Vault config inited with tpmkeyonly false
Vault did not open with tpmKeyOnly=false (exit status 1); trying tpmKeyOnly=true
Vault opened with tpmKeyOnly=true; recording it as the derivation in use
Publishing Vault Config with tpmKeyOnly true
vault Application Data Store unlocked: method=tpm-local-sealed

ZFS is the same sequence through the other handler, where the wrong derivation
fails as exit status 255 from zfs load-key instead of exit status 1 from
fscrypt.

The ext4 variant passes on amd64 (740s), and the same test on an EVE without
this change fails at the vault must be unlocked after the wrong key mode was recorded with the vault reporting DATASEC_AT_REST_ERROR -- the field symptom
this fixes. The ZFS variant has not been run.

Changelog notes

A device whose stored vault key-derivation mode did not match its vault — after
a filesystem repair of a partially corrupted /persist, or after the vault was
recreated — could no longer unlock the vault and stayed in maintenance mode. It
now recovers on its own and corrects the stored mode.

PR Backports

Not proposed for backport.

  • 17.0-stable: No.
  • 16.0-stable: No.
  • 14.5-stable: No.
  • 13.4-stable: No.

Checklist

  • I've provided a proper description
  • I've added the proper documentation
  • I've tested my PR on amd64 device — unit tests, their negative controls,
    and TestVaultKeyModeRecovery on ext4 with a negative control. Not the ZFS
    variant.
  • I've tested my PR on arm64 device
  • I've written the test verification instructions
  • I've set the proper labels to this PR — no stable label, since this is
    not being backported.

🤖 Generated with Claude Code

@eriknordmark
eriknordmark requested a review from shjala September 2, 2026 09:50
@eriknordmark
eriknordmark force-pushed the vault-mode-recovery branch 2 times, most recently from 54aedc2 to e94c7c4 Compare September 2, 2026 17:05
@eriknordmark
eriknordmark marked this pull request as ready for review September 2, 2026 17:09
@eriknordmark
eriknordmark requested review from rene and rucoder September 2, 2026 17:10
@codecov

codecov Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 47.44318% with 185 lines in your changes missing coverage. Please review.
✅ Project coverage is 26.95%. Comparing base (adb213c) to head (1000210).
⚠️ Report is 22 commits behind head on master.

Files with missing lines Patch % Lines
pkg/pillar/vault/handler_zfs.go 52.48% 79 Missing and 26 partials ⚠️
pkg/pillar/vault/handler_zfs_ops.go 21.31% 47 Missing and 1 partial ⚠️
pkg/pillar/cmd/vaultmgr/vaultmgr.go 51.28% 17 Missing and 2 partials ⚠️
pkg/pillar/vault/handler_ext4.go 0.00% 10 Missing ⚠️
pkg/pillar/vault/handler_unsupported.go 0.00% 2 Missing ⚠️
.../pillar/vault/faultinjection_migration_disabled.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6451      +/-   ##
==========================================
+ Coverage   26.51%   26.95%   +0.44%     
==========================================
  Files         519      532      +13     
  Lines       95765    97700    +1935     
==========================================
+ Hits        25393    26336     +943     
- Misses      68396    69172     +776     
- Partials     1976     2192     +216     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rene

rene commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

@claude review

@github-actions

github-actions Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @rene's task in 4m 49s —— View job


Review complete

  • Read the PR diff and context
  • Review keymode.go / resolveKeyMode logic
  • Review ext4/zfs/unsupported handler changes
  • Review vaultmgr.go changes
  • Review tests
  • Post findings

I reviewed the recovery logic end to end (resolveKeyMode, the ext4/zfs handlers, vaultKeyMode/recordVaultKeyMode, and the interface change) and it holds up well:

  • The "don't persist an inferred mode until something confirms it" invariant is respected on every success path — recordVaultKeyMode is guarded by vaultConfigInited/IsTpmEnabled and only reads the resolved TpmKeyOnlyMode back out of the handler.
  • The retry is correctly gated on inferred, so a genuine seal failure is not masked, and the both-failed error names both derivations rather than blaming one. The "trying the other key changes nothing on disk" claim checks out (fscrypt unlock / zfs load-key both verify against stored material without mutating it), and the resolved-mode-on-error is ignored by callers.
  • VaultConfig is self-published/consumed only by vaultmgr, so deferring its publication to after a confirmed unlock does not starve any other agent.
  • Deferred deprecated-vault unlock (setupDeprecatedVaults) runs as a separate CLI invocation, so it can't clobber the default-vault handler options.
  • Unit tests cover the decision matrix thoroughly (known-mode not second-guessed, inferred-right, inferred-wrong recovered, double-failure reports both).

One low-priority observation posted inline on the wipe-and-recreate path (vaultmgr.go): the recreated empty vault is created with the inferred TpmKeyOnlyMode (merged/false for a device that had an existing vault), so recovery-by-wipe can leave a modern device on the deprecated key scheme. It's functionally harmless and largely pre-existing.

No blocking issues.
· vault-mode-recovery

Comment thread pkg/pillar/cmd/vaultmgr/vaultmgr.go Resolved
@eriknordmark
eriknordmark force-pushed the vault-mode-recovery branch 2 times, most recently from 8cb672a to 6eb1e37 Compare September 4, 2026 16:57
@rene

rene commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

@eriknordmark , you have one conflict to fix....

@shjala

shjala commented Sep 14, 2026 •

Copy link
Copy Markdown
Member

I think there is issue with how the retry happens:

  • Boot 1. VaultConfig is gone
    • No config to read, so vaultmgr infers. Its only probe is whether /persist/vault exists.
    • The vault does exist, so the probe answers "merged".
    • The device was installed at 7.10.0 or later, so its vault is TPM-key-only. The guess is wrong.
    • The guess is written to disk before anything tries to unlock with it.
    • The unlock fails. Vault locked.
  • Boot 2. The file exists again and says "merged". Nothing is inferred. Unlock fails. Maintenance mode.
  • Device is upgraded to this PR.
  • Boot 3. The file still says "merged", so inferred is false. resolveKeyMode tries once and gives up. Still locked.

and a second issue:

  • Boot 1. Vault locked, controller has no key, cleanup still allowed.
    • RemoveDefaultVault() destroys the old vault.
    • vaultmgr.go:607 forces TpmKeyOnlyMode: true, so SetupDefaultVault() creates the replacement with the TPM key alone.
    • recordVaultKeyMode() then returns immediately, because vaultConfigInited is true.
    • The file still says merged. The vault is TPM-key-only and the device serves the rest of the boot with an open vault.
  • Boot 2. The file says merged, so the mode counts as known rather than inferred. The merged key cannot open a TPM-key-only vault, resolveKeyMode does not retry a known mode, and the device parks in maintenance mode.

@shjala

shjala commented Sep 14, 2026

Copy link
Copy Markdown
Member

In addition, I suggest dropping the inferring, fetch the TPM key once, try the recorded mode, fall back to the other one on failure regardless of where the mode came from, and persist the winner whenever it differs from what is on disk.

We can even drop the config bookkeeping if we make the recovery using both key the only path.

@eriknordmark

Copy link
Copy Markdown
Contributor Author

@shjala Both cases are real, and the second one is in this PR's own code — recordVaultKeyMode returned early whenever a mode was already on disk, so the recreate path keyed the replacement TPM-key-only and left the file saying merged.

Taken your suggestion, which fixes both: the inference is gone, the retry is unconditional, and the resolved mode is persisted whenever it disagrees with what is on disk.

  • With no record at all the first attempt is TPM-key-only (how every vault created since 7.10.0 is keyed) rather than a guess derived from whether the vault exists. inferTpmKeyOnly and the filesystem probe behind it are gone.
  • A recorded mode that does not unlock is retried against the other derivation regardless of provenance, so your boot-3 device recovers on the first boot after the upgrade.
  • recordVaultKeyMode persists the resolved mode whenever it differs from the record, so the recreate path rewrites the stale one. TpmKeyOnlyModeInferred is gone from HandlerOptions — nothing needs to know where the mode came from any more.
  • keyDerivation on VaultStatus follows the same resolution.

I kept the config bookkeeping rather than making recovery the only path: without it every boot on a pre-7.10.0 device pays a failed unseal and a failed fscrypt unlock before the working derivation, and the log then reads like a seal problem on every boot.

The e2e test's fault changed to match — it now records the wrong derivation instead of deleting the record, because a device with no record starts from TPM-key-only and would open a TPM-key-only vault on the first try, exercising nothing. That is your boot-2 scenario directly.

Also rebased on master.

@eriknordmark eriknordmark changed the title vaultmgr: recover a lost vault key mode vaultmgr: recover a wrong vault key mode Sep 14, 2026
@eriknordmark

Copy link
Copy Markdown
Contributor Author

Ran TestVaultKeyModeRecovery on ext4/amd64 against an EVE built from this branch — PASS (740s). Checkpoints in order: setup-done, rebooted-with-wrong-key-mode, rebooted-with-corrected-key-mode. The device log shows the merged derivation being read off disk, the unlock with it failing and being retried, the other derivation opening the vault, and the corrected mode replacing the record; UnlockMethod is the local TPM seal, so the recovery came from the retry rather than from the controller key or a recreate.

Negative control on an EVE without this change (same test source, only the image differs) — FAIL, at the vault must be unlocked after the wrong key mode was recorded, with the vault reporting DATASEC_AT_REST_ERROR instead of ENABLED. That is the field symptom, so the green run above is a result rather than an absence.

The second-boot assertions carry their own positive control: Vault config inited with tpmkeyonly true must grow while the wrong-mode and retry messages must not, so the two no-growth checks cannot pass by the grep silently finding nothing.

ZFS variant not run.

eriknordmark added a commit to eriknordmark/eve that referenced this pull request Sep 18, 2026
Integration branch on master with the conversion chain (lf-edge#6530, lf-edge#6063, fork#6)
and the robustness set (lf-edge#6442, lf-edge#6406, lf-edge#6478, lf-edge#6505, lf-edge#6451, lf-edge#6590, lf-edge#6599).
Records the tips each piece was replayed from, where two PRs collide and which
side won, the branch-local adapters that no PR carries, and the
storage-resizer content pin.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
eriknordmark added a commit to eriknordmark/eve that referenced this pull request Sep 18, 2026
Integration branch on master with the conversion chain plus the fault-injection
stress harness (lf-edge#6530, lf-edge#6063, fork#6, fork#7) and the robustness set (lf-edge#6442,
lf-edge#6406, lf-edge#6478, lf-edge#6505, lf-edge#6451, lf-edge#6590, lf-edge#6599). Records the tips each piece was
replayed from, where two PRs collide and which side won, the branch-local
adapters that no PR carries, and the chaos storage-resizer content pin.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@rene

rene commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

@claude

@github-actions

github-actions Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @rene's task in 2m 16s —— View job


Review complete

  • Read the changed vault key-mode files
  • Review resolveKeyMode / handler retry logic
  • Review vaultmgr.go record/recreate paths
  • Post review findings

I reviewed the recovery logic end to end. The two problems @shjala raised are addressed: inference is gone (vaultKeyMode starts from TPM-key-only when there's no record), the fallback is unconditional (resolveKeyMode tries the other derivation whatever the preferred came from), the resolved mode is persisted whenever it disagrees with the record (recordVaultKeyMode no longer returns early), and the recreate path now forces TpmKeyOnlyMode: true (vaultmgr.go:647). The unit tests cover the decision matrix well, including the negative case where an unresolved mode leaves the options untouched so a caller can't persist one on the strength of a failed unlock.

Two minor points, both posted inline (non-blocking):

  1. Retry reacts to non-key failures (low). resolveKeyMode retries on any error from the unlock callback, but ext4's unlockVaultWithMode also runs linkKeyrings() after the key check. A post-unlock keyctl failure on the correct derivation would trigger a retry with the wrong one — either surfacing a misleading "derivation unresolved" error (and an unneeded controller-key path) or, if fscrypt unlock is idempotent, persisting the wrong mode. ZFS deliberately scopes its retry to loadDatasetKey only; ext4 could do the same by moving linkKeyrings() outside the resolveUnlock closure.

  2. Stale comments. handler_ext4.go and handler_zfs.go still say "When the key-derivation mode was only inferred…", but inference was removed in this PR.

No blocking issues.
· vault-mode-recovery

Comment thread pkg/pillar/vault/handler_ext4.go Resolved
Comment thread pkg/pillar/vault/handler_ext4.go Resolved
eriknordmark and others added 2 commits September 23, 2026 16:17
A kvm->EVE-k vault migration copies the carried-over filesystem vault
into a staging zvol sized to the pool's free space. Every failure after
that zvol was created returned without destroying it, so the partial
copy stayed on the pool. On EVE-k the next boot's migration attempt
cleared it, but a device that falls back to EVE-kvm inherits a zvol
holding up to all the space the pool had free, and EVE-kvm has no
migration code that would ever reclaim it. A copy that runs out of room
part-way therefore left the pool full for the flavor the device fell
back to.

The migration now discards the staging zvol on every failure before the
swap, and puts the pre-migration vault back if the second rename of the
swap fails, so the vault is never left absent for a fallback boot to
find. The empty etcd zvol is created after the free-space checks rather
than before them, so a migration declined for lack of space leaves
nothing behind either.

The storage calls the migration and the recovery make now go through an
interface, which lets the tests drive both against an in-memory pool and
fail any single operation; they otherwise need a real ZFS pool and a
power cut.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Recovering an interrupted kvm->EVE-k vault migration decided purely on
which datasets were present: with the vault absent and a staging zvol
in place, that zvol was renamed into place as the vault. Nothing
recorded whether its copy had finished, so a staging zvol left by a
failed migration was indistinguishable from one whose swap lost power
between the two renames.

That is reachable without a power cut. A failed migration is reported as
a vault that cannot be unlocked, so the device waits for a key from the
controller; receiving none, and with cleanup still permitted, vaultmgr
destroys the vault and sets up a fresh one — which arrives at recovery
with no vault and a leftover staging zvol, and adopts its partial copy
as the device's vault.

The migration now records the staging dataset on /persist once its copy
is complete, and recovery promotes a staging zvol only when that record
names it; otherwise the leftover is discarded and, where the
pre-migration vault is still parked under its backup name, restored so
the migration can be retried. Removing the vault takes its migration
datasets with it, so nothing is left for a fresh vault to adopt.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
eriknordmark and others added 11 commits September 23, 2026 16:17
Record what the kvm->EVE-k vault migration leaves behind and when, so
the reason a staging zvol is only sometimes promoted to the vault is
readable without the code: the swap record that distinguishes a
complete copy from a partial one, the restore of the pre-migration
vault when the swap cannot finish, and the fact that a staging zvol
occupies only the space written to it.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The one failure the unit tests cannot reach is power loss inside the
rename swap. Faking the storage operations can make any step return an
error, but only losing the machine at that instant says whether ZFS's
rename and the swap record's fsync really survive it -- which is the
property the recovery path is built on.

Add a fault point that parks the migration in that window instead of
failing it, so a test can cut power from outside at a known moment. It
is compiled in only under FAULT_INJECTION=y, wrapping the storage
operations; production builds get an identity wrapper and consult no
marker. The marker lives on /persist because the migration runs on the
first EVE-k boot, which /tmp does not survive, and because file presence
under /config is measured into PCR14, which seals the vault.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H2fNrog4UH7mbhrHVGkLN3
A device that falls back to EVE-kvm inside the migration's rename swap
comes back with no vault at persist/vault -- it has been renamed aside --
so EVE-kvm creates a fresh, empty one. The contents then exist only in
the parked pre-migration vault and the completed staging copy. Recovery
treated any vault in place as authoritative and destroyed both, turning a
state that still holds the data into a permanent loss.

Recovery now discards leftovers only when the vault in place is the
migrated zvol. A filesystem vault beside migration datasets is not the
one the swap was producing, so the datasets are kept and the swap record
left alone, since it is what says the swap never completed. An unreadable
dataset type takes the keeping side.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H2fNrog4UH7mbhrHVGkLN3
Keeping the migration datasets in recovery only closed one of two doors.
The migration's own entry cleanup drops them before copying, on the
premise that the vault it is about to migrate is the one they came from.
After a fallback to EVE-kvm inside the swap window that premise is false:
the vault in place is the fresh one kvm created, so the migration would
destroy the parked original and the completed copy and then migrate an
empty vault over the top.

Both callers now ask the same question -- is a pre-migration vault parked
while the vault in place is not the migrated zvol -- and the migration
declines rather than proceeding. Recovery's condition narrows to match:
a staging zvol with no parked backup is debris from an attempt that never
reached the swap, and stays droppable, so a device that merely failed a
copy still retries normally.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H2fNrog4UH7mbhrHVGkLN3
The swap parks the pre-migration vault under <vault>.old so that losing
power between the two renames cannot leave the vault absent. Nothing
removed it on the path where it matters most: a device that reverts to
EVE-kvm keeps both that dataset and the completed copy, EVE-kvm runs no
vault migration code, and the space stays held for as long as the device
lives. Marking the partition active is the point where that fallback
stops being worth anything -- the device is not going back to the other
flavor -- so recovery now discards both datasets from then on, and the
migration stops declining to run over them.

vaultmgr reads the partition state once at startup and hands it to the
handler: a committed partition cannot become uncommitted, and one that
commits later is picked up on the next boot, which is when leftovers
matter again.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H2fNrog4UH7mbhrHVGkLN3
The vault swap runs while a device upgrades to EVE-k, so the boot that
lands inside its window is as often the EVE-kvm fallback that follows a
failed upgrade as an EVE-k retry. EVE-kvm reached that boot with
persist/vault absent, read it as a fresh install and created an empty
vault, stranding every blob in the datasets the swap had parked and
leaving the device running on a vault with a new key. Recovery now runs
on both flavors, before either may decide the vault is missing.

What the two flavors do differs, because EVE-kvm cannot mount a zvol:
it puts the parked filesystem vault back and leaves the copy to the
next EVE-k attempt, where EVE-k promotes the completed copy instead. A
vault already in place beside a parked one is likewise never the
migrated vault on EVE-kvm, so it keeps both datasets whatever the
partition state says rather than reclaiming the pool space over the
only surviving copy of the contents.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
A device converted from EVE-kvm runs the ZFS vault migration and its
recovery whether or not it has a TPM, and both ask whether the running
partition has been marked active before they let go of the pre-migration
vault and the staging copy. That answer was read only on the TPM path,
so on a no-TPM device it stayed false for the life of the boot: the
datasets were never released once the update committed, and a device
that fell back to EVE-kvm inside the swap window and acquired an empty
vault refused to migrate over it forever, leaving the contents in
datasets nothing would reclaim.

Read it for every device and leave the TPM key mode to the TPM path.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The migration creates the EVE-k etcd zvol on its way to the vault swap,
and a failure after that point left it on the pool. It is bounded by the
configured etcd size rather than free space, so it is milder than the
staging zvol, but it has the same ending: a device that falls back to
EVE-kvm runs no migration code and never reclaims it.

The failure cleanup now takes it along with the staging zvol, and only
when this attempt created it -- an etcd zvol already on the pool is the
device's etcd storage, not migration debris.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
A device whose recorded vault key derivation does not match its vault
locks itself out permanently. The two derivations are the TPM key alone
and that key merged with a build-time constant (pre-7.10.0), and the
record is one bit in a persistent pubsub publication. An unlock with the
wrong one fails the way a broken seal fails -- the TPM unseal succeeds
and only fscrypt or `zfs load-key` refuses -- so the controller-key
fallback re-seals, fails identically, and the device parks in
maintenance mode with its data unreachable.

The record can be wrong in two ways. A device that lost the publication
to an e2fsck repair of a torn /persist guessed the derivation from
whether the vault existed -- an answer that names the merged form for
every existing vault, wrong for anything installed since 7.10.0 -- and
persisted the guess before anything tried it. And the vault recreate
path, reached when no key can open the vault and the controller has
none either, keys its replacement with the TPM key alone whatever the
destroyed vault used.

Stop treating the record as authoritative. A failed unlock is retried
against the other derivation, and the one that opened the vault is
persisted whenever it disagrees with what is on disk. That costs one
extra unseal on a device that is locked out anyway, and a key that does
not match the vault changes nothing on disk. The derivation VaultStatus
reports follows the same resolution.

Each attempt derives a different key, so staging the key is a scope the
caller holds around everything that reads the staging file rather than a
step inside the key load: `zfs load-key` for the etcd zvol, and the
encrypted zvol creates that a carried-over kvm vault's migration
performs, both run inside it.

With no record at all, the first attempt is the TPM key alone, which is
how every vault created since 7.10.0 is keyed; the retry covers the
older ones.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Cover the three decisions the key mode turns on: which derivation is
tried first, which one is reported after an unlock resolved it, and when
a resolved one replaces the record. The recorded mode is read back off
an in-memory publication, so the assertion is on what a later boot would
find rather than on a call having happened.

Every test was checked against a mutation of the code it covers -- the
retry dropped, the store-back dropped, the record left alone when it
disagrees, the TPM guard removed -- and fails on its own assertions in
each case.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Drive the recovery end to end on a real device: record the merged
derivation for a vault created with the TPM key alone, reboot, and
require the vault to come back open on the local TPM seal.

The fault is a wrong bit rather than a missing file because a device
with no record at all starts from the TPM key alone and opens such a
vault on the first attempt. Evidence comes from the device's own
/persist/newlog, since vaultmgr logs this during early boot and those
records reach the controller long after the test would have to assert on
them. A marker written into the vault beforehand separates the original
vault being opened from an empty one created in its place, and the
unlock method separates the local seal from the controller-key and
recreate paths, which also end with a device reporting an open vault.

One variant per /persist filesystem: ext4 and ZFS are separate vault
handlers with separate unlock paths.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

@rene rene left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@eriknordmark
eriknordmark merged commit cc16a48 into lf-edge:master Sep 26, 2026
54 of 57 checks passed
@eriknordmark
eriknordmark deleted the vault-mode-recovery branch October 8, 2026 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants