This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 447f74c8868 mtd/config_nvs: validate block size and count before 
buffer allocation
447f74c8868 is described below

commit 447f74c886805ad5651c3b6b03dae6bc23c7bb16
Author: Swatantra Yadav <[email protected]>
AuthorDate: Fri Nov 27 18:58:53 2026 +0530

    mtd/config_nvs: validate block size and count before buffer allocation
    
    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 #22270
    
    Assisted-by: Claude:claude-opus-4.6
    Signed-off-by: Swatantra Yadav <[email protected]>
---
 drivers/mtd/mtd_config_nvs.c | 39 ++++++++++++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 11 deletions(-)

diff --git a/drivers/mtd/mtd_config_nvs.c b/drivers/mtd/mtd_config_nvs.c
index 3cf4502e562..c18fbf8ab28 100644
--- a/drivers/mtd/mtd_config_nvs.c
+++ b/drivers/mtd/mtd_config_nvs.c
@@ -1340,13 +1340,38 @@ static int nvs_startup(FAR struct nvs_fs *fs)
 
   size_t ate_size = nvs_ate_size(fs);
 
-  NVS_ATE(second_ate, ate_size);
-  NVS_ATE(last_ate, ate_size);
+  /* Check the number of blocks, it should be at least 2. */
+
+  if (fs->nblocks < 2)
+    {
+      ferr("Configuration error - block count\n");
+      return -EINVAL;
+    }
+
+  /* Check block size: it must be large enough to hold the closing ATE,
+   * GC marker ATE, at least one entry ATE, and must not exceed the
+   * maximum addressable 16-bit offset.
+   */
+
+  if (fs->blocksize <= 3 * ate_size ||
+      fs->blocksize > (NVS_ADDR_OFFS_MASK + 1))
+    {
+      ferr("Configuration error - block size: %" PRIu32 "\n",
+           fs->blocksize);
+      return -EINVAL;
+    }
 
 #if CONFIG_MTD_CONFIG_BUFFER_SIZE > 0
-  DEBUGASSERT(ate_size <= CONFIG_MTD_CONFIG_BUFFER_SIZE);
+  if (ate_size > CONFIG_MTD_CONFIG_BUFFER_SIZE)
+    {
+      ferr("Configuration error - ATE size too large\n");
+      return -EINVAL;
+    }
 #endif
 
+  NVS_ATE(second_ate, ate_size);
+  NVS_ATE(last_ate, ate_size);
+
   rc = MTD_IOCTL(fs->mtd, MTDIOC_ERASESTATE,
                  (unsigned long)((uintptr_t)&fs->erasestate));
   if (rc < 0)
@@ -1355,14 +1380,6 @@ static int nvs_startup(FAR struct nvs_fs *fs)
       return rc;
     }
 
-  /* Check the number of blocks, it should be at least 2. */
-
-  if (fs->nblocks < 2)
-    {
-      ferr("Configuration error - block count\n");
-      return -EINVAL;
-    }
-
   /* Step through the blocks to find a open block following
    * a closed block, this is where NVS can write.
    */

Reply via email to