Skip to content

mtd/config_nvs: validate block size and count before buffer allocation - #22503

Merged
GUIDINGLI merged 1 commit into
apache:masterfrom
Swatantra-66:fix/mtd-nvs-blocksize-bound
Nov 28, 2026
Merged

GUIDINGLI merged 1 commit into
apache:masterfrom
Swatantra-66:fix/mtd-nvs-blocksize-bound

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

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:

  • 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.

Impact

  • Is new feature added? No.
  • User: Prevents silent flash offset corruption when NVS is used with block sizes exceeding 64 KiB, and prevents underflow crashes on undersized blocks.
  • Build: Clean compilation.

Testing

Before and After Comparison

Geometry Condition Before (master) After (this patch)
Block size > 64 KiB (e.g. 128 KiB) 16-bit offset wraps around; reads/writes wrong offsets on flash Rejected early in nvs_startup() with -EINVAL
Block size <= 3 * ate_size (e.g. 64 B with 24 B ATE) ate_wra underflows to 0xfffffff8; out-of-bounds writes Rejected early in nvs_startup() with -EINVAL
ate_size > CONFIG_MTD_CONFIG_BUFFER_SIZE DEBUGASSERT panic or stack buffer overflow Rejected early in nvs_startup() with -EINVAL
Valid geometry (4 KiB - 64 KiB) Functions normally Functions normally

Verification Logs

Code style check:

$ git diff --check
(clean, 0 warnings / errors)

Build verification:
Configuration: same70-xplained:nsh with CONFIG_MTD_CONFIG_NVS=y (ARM Cortex-M7, arm-none-eabi-gcc 14.2.1)

$ ./tools/configure.sh -l same70-xplained:nsh
$ sed -i 's/CONFIG_MTD_CONFIG=y/CONFIG_MTD_CONFIG_NVS=y/' .config
$ make -j4
...
CC:  mtd/mtd_config_nvs.c
...
LD: nuttx
Memory region         Used Size  Region Size  %age Used
           flash:      145684 B         2 MB      6.95%
            sram:       10784 B       384 KB      2.74%
CP: nuttx.bin

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]>
@github-actions github-actions Bot added Area: Drivers Drivers issues Size: S The size of the change in this PR is small labels Nov 27, 2026
@github-actions

Copy link
Copy Markdown

@xiaoxiang781216 xiaoxiang781216 linked an issue Nov 28, 2026 that may be closed by this pull request
@GUIDINGLI
GUIDINGLI merged commit 447f74c into apache:master Nov 28, 2026
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mtd: config NVS truncates the block-tail offset to 16 bits

3 participants