Swatantra-66 opened a new pull request, #22503:
URL: https://github.com/apache/nuttx/pull/22503
## 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
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]