Skip to content

Copy sector in chunks with smaller NVM_CACHE - #996

Open
vhdirk wants to merge 1 commit into
wolfSSL:masterfrom
vhdirk:nvm-cache-chunks
Open

vhdirk wants to merge 1 commit into
wolfSSL:masterfrom
vhdirk:nvm-cache-chunks

Conversation

@vhdirk

@vhdirk vhdirk commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

This commit allows to specify an NVM_CACHE_SIZE that is smaller than SECTOR_SIZE. When writing to the trailer in the case of NVM_FLASH_WRITEONCE, you currently need WOLFBOOT_SECTOR_SIZE bytes available in ram, which we don't have.

By specifying a smaller NVM_CACHE_SIZE, we can copy the sector in chunks, patching in the modifications while copying. We're keeping FLASHBUFFER_SIZE as is, meaning that NVM_CACHE_SIZE has to be a strict multiple of FLASHBUFFER_SIZE.

I'm not sure how this will behave with regard to power loss during the chunked writes for write once memory, nor do I know how to test that. Looking at how FLASHBUFFER_SIZE handling is implemented, I don't expect this to introduce any more issues.

We could simplify this by also using FLASHBUFFER_SIZE as the amount of ram used, but I wasn't sure about te uses of it to begin with.

Copilot AI balanced review requested due to automatic review settings November 19, 2026 13:14

Copilot AI 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.

🟡 Changes recommended

Smaller-cache configurations retain unsafe sector-sized accesses in encryption-key and shared-trailer paths, risking buffer overruns and trailer corruption, while missing helper extraction breaks scrub-test linking.

3 open issues
What changed in this PR

This PR introduces chunked sector copying to reduce RAM usage when wolfBoot updates trailers on write-once flash with a cache smaller than one sector.

Changes:

  • Adds compile-time checks for cache size, sector divisibility, and write granularity.
  • Reuses a chunked copy-and-patch helper for trailer, magic, and update-trigger writes.
  • Checks destination erase and write results before erasing the source sector.
File Description
src/​libwolfboot.c Adds cache validation and chunked sector copying for write-once trailer updates.

Review effort: Balanced

Comment thread src/libwolfboot.c Resolved
Comment thread src/libwolfboot.c Resolved
Comment thread src/libwolfboot.c Resolved
@vhdirk
vhdirk force-pushed the nvm-cache-chunks branch 3 times, most recently from ca3ed85 to b86e7b0 Compare November 19, 2026 15:42
@dgarske

dgarske commented Nov 19, 2026

Copy link
Copy Markdown
Member

Okay to test. Contributor agreement on file for customer.

Comment thread src/libwolfboot.c Outdated
return sel;
}

static int RAMFUNCTION nvm_sector_copy_with_patch(uintptr_t src_sector,

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.

CI is failing on this.

gcc -o unit-nvm-cache-scrub unit-nvm-cache-scrub.c -I. -I../../src -I../../include -I/__w/wolfBoot/wolfBoot/lib/wolfssl -g -ggdb -fprofile-arcs -ftest-coverage --coverage -fprofile-update=single -DUNIT_TEST_COVERAGE -DUNIT_TEST -DWOLFSSL_USER_SETTINGS -std=gnu17 -lcheck -lm -pthread -lrt -lsubunit -fprofile-arcs -ftest-coverage
In file included from unit-nvm-cache-scrub.c:100:
nvm_cache_scrub_extract.h: In function 'trailer_write':
nvm_cache_scrub_extract.h:24:15: error: implicit declaration of function 'nvm_sector_copy_with_patch' [-Wimplicit-function-declaration]
   24 |         ret = nvm_sector_copy_with_patch(addr_read, addr_write, patch_offset,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~
make: *** [Makefile:2553: unit-nvm-cache-scrub] Error 1

@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 #996

Scan targets checked: wolfboot-src, wolfboot-bugs
Not reviewed (no scan target covers them): tools/unit-tests/Makefile
Coverage: 1 of 1 in-scope changed file(s) opened by the reviewer

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Review tier: Max

Comment thread src/libwolfboot.c

#if (NVM_CACHE_SIZE <= 0)
#error "NVM_CACHE_SIZE must be positive"
#elif (NVM_CACHE_SIZE > WOLFBOOT_SECTOR_SIZE)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

New NVM_CACHE_SIZE guard permits sub-sector cache that hal_set_key overflows with key material · Buffer overflows

The guard now accepts NVM_CACHE_SIZE < WOLFBOOT_SECTOR_SIZE, but under EXT_ENCRYPTED hal_set_key uses NVM_CACHE as ENCRYPT_CACHE and copies/writes a full sector (lines 2990-3011). This overflows it, and key/nonce bytes stay unscrubbed in the RAM next to it.

Suggested fix: Add an #error when EXT_ENCRYPTED key storage uses NVM_CACHE with NVM_CACHE_SIZE < WOLFBOOT_SECTOR_SIZE, or port hal_set_key to nvm_sector_copy_with_patch.
Basis: ISO C11 6.5.6p8: forming or dereferencing a pointer past the end of an array object (here the NVM_CACHE_SIZE-byte NVM_CACHE) is undefined behavior.

Related known findings (similar but distinct; listed for context, not part of this finding)

  • F-6567 (false_positive): (1) File/function: the candidate is in hal_set_key in src/libwolfboot.c, plus the NVM_CACHE_SIZE #error guard at line 365. F-6567 and #17448 are also in hal_set_key, so the function matches but the guard site is new. (2) Operation: the candidate is an out-of-bounds copy and write of a full sector into a smaller NVM_CACHE/ENCRYPT_CACHE. F-6567 and #17448 are a missing ForceZero, which leaves residual key material after the function returns. (3) Root cause: the candidate is a compile-time size guard

Copilot AI 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.

🟡 Changes recommended

Reduced caches can cause buffer overflows and RAM corruption in encrypted write-once key storage, while missing helper definitions and fixture updates break existing scrub regression tests.

1 open issue
3 resolved since last review

Review effort: Balanced

Comment thread tools/unit-tests/Makefile Outdated
Comment on lines 2547 to 2548
sed -n '/^static int RAMFUNCTION nvm_sector_copy_with_patch(/,/^}/p' $< >> $@
sed -n '/^static void RAMFUNCTION nvm_cache_scrub(/,/^}/p' $< > $@
@vhdirk
vhdirk force-pushed the nvm-cache-chunks branch 7 times, most recently from ef0d06e to 7ed1de2 Compare November 20, 2026 13:26
This commit allows to specify an NVM_CACHE_SIZE that is smaller than
SECTOR_SIZE. When writing to the trailer in the case of
NVM_FLASH_WRITEONCE, you currently need WOLFBOOT_SECTOR_SIZE bytes
available in ram, which we don't have.

By specifying a smaller NVM_CACHE_SIZE, we can copy the sector in
chunks, patching in the modifications while copying. We're keeping
FLASHBUFFER_SIZE as is, meaning that NVM_CACHE_SIZE has to be a
strict multiple of FLASHBUFFER_SIZE.

I'm not sure how this will behave with regard to power loss during the
chunked writes for write once memory, nor do I know how to test that.
Looking at how FLASHBUFFER_SIZE handling is implemented, I don't expect
this to introduce any more issues.

We could simplify this by also using FLASHBUFFER_SIZE as the amount of
ram used, but I wasn't sure about te uses of it to begin with.
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.

6 participants