Repository navigation
Keep fwTPM state across a power loss on the file NV backend - #687
Conversation
There was a problem hiding this comment.
🟡 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
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
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
4344ffb to
0315209
Compare
aidangarske
left a comment
There was a problem hiding this comment.
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
0315209 to
f8010fb
Compare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
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


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.newfile is removed at start.wolftpm/fwtpm/fwtpm.h- two optionalFWTPM_NV_HALcallbacks,beginandcommit, appended at the end of the structure. A HAL that leaves them unset behaves as before.link(), directory fsync) before the first journal write. An existing key file is used unchanged.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
FWTPM_Init()and is left untouched. It was replaced with fresh seeds before. Blank media still start with fresh seeds.rename()and hard links.FWTPM_NV_FILE_NO_ATOMICrestores 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.