Skip to content

Keep fwTPM state across a power loss on the file NV backend - #687

Merged
aidangarske merged 6 commits into
wolfSSL:masterfrom
dgarske:fwtpm_nv_powerloss
Dec 1, 2026
Merged

aidangarske merged 6 commits into
wolfSSL:masterfrom
dgarske:fwtpm_nv_powerloss

Conversation

@dgarske

@dgarske dgarske commented Dec 1, 2026

Copy link
Copy Markdown
Member

The default file backend of the fwTPM NV journal now commits every journal update by atomic file replace, so a power loss at any point leaves either the state before the commit or the state after it. There is no change to the journal format: a journal written by an earlier build loads and can be appended to, and the result loads on an earlier build.

What it adds

  • src/fwtpm/fwtpm_nv.c - each commit (one appended entry or one full save) is built in <journal>.new, fsynced, renamed over the journal, and the directory is fsynced. A leftover .new file is removed at start.
  • wolftpm/fwtpm/fwtpm.h - two optional FWTPM_NV_HAL callbacks, begin and commit, appended at the end of the structure. A HAL that leaves them unset behaves as before.
  • The journal key file is created durably (temporary file, fsync, link(), directory fsync) before the first journal write. An existing key file is used unchanged.
  • A journal is accepted only when it holds the three hierarchy seed records.
  • docs/fwtpm/hal-and-porting.md - the power loss section, the file system requirements, the measured commit cost and how to reset a damaged journal.

Behavior changes

  • On the file backend, a journal that is present but does not load (MAC mismatch, write position out of range, missing seed record, missing key file, not a journal) now fails FWTPM_Init() and is left untouched. It was replaced with fresh seeds before. Blank media still start with fresh seeds.
  • The file backend needs permission to create and rename files beside the journal, room for a second copy, and a file system with atomic rename() and hard links.
  • FWTPM_NV_FILE_NO_ATOMIC restores the in-place behavior. Windows keeps the in-place behavior.

Testing

Power-cut tests stop the file backend at every write, fsync, rename and directory fsync of each kind of commit, then start a new context on the same files and compare the loaded state field by field. A second cut during the restart is covered too. Tested in the default, v1.85, ASan, UBSan, small stack, append-only and opt-out builds. Not tested on Windows or macOS, and not on a real power cut.

Scope

Raw flash (append-only) and OP-TEE backends are unchanged and can still lose state on a power loss; they need a journal format change and are follow-on work. A command that writes several entries is several commits.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 00:43
@dgarske dgarske self-assigned this Dec 1, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

A directory-fsync failure after journal publication can leave running and restarted contexts accepting different authorizations despite a reported command failure, while another error path leaves sensitive journal data unscrubbed on the stack.

2 open issues
What changed in this PR

This PR adds power-loss resilience to fwTPM’s default POSIX file-backed NV storage through atomic journal replacement, without changing the journal format.

Changes:

  • Adds optional HAL commit callbacks, atomic file commits, and durable integrity-key creation.
  • Rejects damaged journals on the atomic file backend and requires all three hierarchy seed records.
  • Adds fault-injection and compatibility tests, plus documentation for recovery, filesystem requirements, and the in-place opt-out.
File Description
wolftpm/​fwtpm/​fwtpm.h Adds optional NV HAL begin and commit callbacks.
wolftpm/​fwtpm/​fwtpm_nv.h Documents initialization behavior and declares test fault hooks.
tests/​fwtpm_unit_tests.c Adds commit, interruption, recovery, and compatibility tests.
src/​fwtpm/​fwtpm_nv.c Implements atomic commits, durable key creation, and journal validation.
docs/​fwtpm/​usage.md Explains persistence and damaged-journal recovery.
docs/​fwtpm/​hal-and-porting.md Details durability guarantees, HAL callbacks, requirements, and costs.
docs/​fwtpm/​building.md Documents the in-place file-backend opt-out.
.gitignore Ignores temporary journal files and fault-test directories.

Review effort: Balanced

Comment thread src/fwtpm/fwtpm_nv.c Resolved
Comment thread src/fwtpm/fwtpm_nv.c Resolved

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #687

Scan targets checked: wolftpm-src, wolftpm-bugs
Not reviewed (no scan target covers them): .gitignore, docs/fwtpm/building.md, docs/fwtpm/hal-and-porting.md, docs/fwtpm/usage.md
Coverage: 4 of 4 in-scope changed file(s) opened by the reviewer

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Max

@dgarske
dgarske force-pushed the fwtpm_nv_powerloss branch from 4344ffb to 0315209 Compare December 1, 2026 15:15
@dgarske dgarske assigned wolfSSL-Bot and aidangarske and unassigned dgarske Dec 1, 2026

@aidangarske aidangarske left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Skoll Code Review

Scan type: review

Overall recommendation: REQUEST_CHANGES
Findings: 4 total — 4 posted, 0 skipped
4 finding(s) posted as inline comments (see file-level comments below)

Posted findings

  • [High] Initialization discards the pending directory-sync guard — src/fwtpm/fwtpm_nv.c:351-352
  • [High] Atomic replacement bypasses the journal's explicit 0600 hardening — src/fwtpm/fwtpm_nv.c:289-293
  • [High] Fixed 256-byte sidecar buffers reject previously valid journal paths — src/fwtpm/fwtpm_nv.c:124-138
  • [Medium] Usage guide overstates atomic and fail-closed behavior — docs/fwtpm/usage.md:82

Review generated by Skoll

Comment thread src/fwtpm/fwtpm_nv.c Resolved
Comment thread src/fwtpm/fwtpm_nv.c Resolved
Comment thread src/fwtpm/fwtpm_nv.c Resolved
Comment thread docs/fwtpm/usage.md Resolved

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #687

Scan targets checked: wolftpm-src, wolftpm-bugs
Not reviewed (no scan target covers them): .gitignore, docs/fwtpm/building.md, docs/fwtpm/hal-and-porting.md, docs/fwtpm/usage.md
Coverage: 2 of 4 in-scope changed file(s) opened by the reviewer; not opened: wolftpm/fwtpm/fwtpm.h, wolftpm/fwtpm/fwtpm_nv.h

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Lite

@aidangarske
aidangarske merged commit f5cd6c2 into wolfSSL:master Dec 1, 2026
262 checks passed
@aidangarske
aidangarske deleted the fwtpm_nv_powerloss branch December 1, 2026 21:28
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.

5 participants