Repository navigation
mtd/config_nvs: validate block size and count before buffer allocation - #22503
Merged
GUIDINGLI merged 1 commit intoNov 28, 2026
Merged
Conversation
In drivers/mtd/mtd_config_nvs.c, NVS addresses encode the block index in the upper 16 bits (NVS_ADDR_BLOCK_SHIFT = 16) and the in-block byte offset in the lower 16 bits (NVS_ADDR_OFFS_MASK = 0x0000FFFF). Previously, nvs_startup() calculated fs->blocksize without checking whether it fits within the 16-bit offset space. When fs->blocksize exceeds 64 KiB (NVS_ADDR_OFFS_MASK + 1, e.g. on flash devices with 128 KiB or 256 KiB erase blocks), (uint16_t)(fs->blocksize - ate_size) wrapped around due to 16-bit truncation, corrupting the tail offset calculation and causing wrong-offset reads and writes. Furthermore, if fs->blocksize <= 3 * ate_size, the block cannot even hold the closing ATE, GC marker ATE, and an entry ATE. On an empty device, startup wrote the GC-done ATE and subtracted ate_size, underflowing fs->ate_wra to 0xfffffff8 (outside configured blocks) and causing unsigned underflow in (fs->blocksize - 3 * ate_size) in nvs_write(). Additionally, fixed buffer configurations (CONFIG_MTD_CONFIG_BUFFER_SIZE) would hit a DEBUGASSERT or overflow if ate_size exceeded the fixed buffer size. This patch validates block geometry before allocating ATE buffers: - Rejects fs->nblocks < 2 with -EINVAL. - Rejects fs->blocksize <= 3 * ate_size or fs->blocksize > (NVS_ADDR_OFFS_MASK + 1) with -EINVAL. - Rejects ate_size > CONFIG_MTD_CONFIG_BUFFER_SIZE with -EINVAL. Fixes apache#22270 Assisted-by: Claude:claude-opus-4.6 Signed-off-by: Swatantra Yadav <[email protected]>
|
xiaoxiang781216
approved these changes
Nov 28, 2026
GUIDINGLI
approved these changes
Nov 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes Issue #22270
In drivers/mtd/mtd_config_nvs.c, NVS addresses encode the block index in the upper 16 bits (NVS_ADDR_BLOCK_SHIFT = 16) and the in-block byte offset in the lower 16 bits (NVS_ADDR_OFFS_MASK = 0x0000FFFF).
Previously, nvs_startup() calculated fs->blocksize from device geometry without checking whether it fits within the 16-bit offset space. When fs->blocksize exceeds 64 KiB (NVS_ADDR_OFFS_MASK + 1 = 65536, e.g. on flash devices with 128 KiB or 256 KiB erase blocks), (uint16_t)(fs->blocksize - ate_size) wrapped around due to 16-bit truncation, corrupting the tail offset calculation and causing nvs_flash_cmp_const() to read and write from wrong offsets.
Furthermore, if fs->blocksize <= 3 * ate_size, the block cannot even hold the closing ATE, GC marker ATE, and an entry ATE. On an empty device, startup wrote the GC-done ATE and subtracted ate_size, underflowing fs->ate_wra to 0xfffffff8 (outside configured memory) and causing unsigned underflow in (fs->blocksize - 3 * ate_size) in nvs_write().
Additionally, fixed buffer configurations (CONFIG_MTD_CONFIG_BUFFER_SIZE) would hit a DEBUGASSERT or overflow if ate_size exceeded the fixed buffer size.
This change validates block geometry early in nvs_startup() before allocating ATE buffers:
Impact
Testing
Before and After Comparison
Verification Logs
Code style check:
Build verification:
Configuration: same70-xplained:nsh with CONFIG_MTD_CONFIG_NVS=y (ARM Cortex-M7, arm-none-eabi-gcc 14.2.1)