xhh233 opened a new issue, #22319:
URL: https://github.com/apache/nuttx/issues/22319

   **Mechanism.** `mtdconfig_ramconsolidate()` allocates `pbuf = 
kmm_malloc(dev->erasesize)` (`:579`) and walks the source entries of one block. 
At `:685` it copies the entry payload with
   
   ```c
   mtdconfig_writebytes(dev, dst_offset, &pbuf[src_offset + sizeof(hdr)], 
phdr->len)
   ```
   
   using the on-flash `phdr->len` **without checking that `src_offset + 
sizeof(hdr) + phdr->len` fits inside `dev->erasesize`**. Destination space is 
checked; the source bound is not.
   
   **Effect.** A header at offset 499 with `len = 200` passes the header read 
(499 + 6 = 505 ≤ 512) but starts a 200-byte copy at 505, so 119 bytes are read 
past the 512-byte `pbuf`.
   
   **Trigger.** RAM consolidation processes a malformed active entry. Corrupt 
flash is a robustness case; this function-level test does not establish that an 
attacker can supply such an image on a deployed device.
   
   **Location.** NuttX `nuttx-13.1.0`, commit 
`2b5c0de5e933281c209f954a63b99cf10c132346`, `drivers/mtd/mtd_config.c`, sha256 
`30b2de5e6c7e4fc28df2401d3b6d13926abe84d70274c227efbdb28c12ab6aa3`:
   
   | Anchor | Line | Code |
   |---|---|---|
   | allocation | `:579` | `pbuf = kmm_malloc(dev->erasesize);` |
   | payload copy | `:685` | `mtdconfig_writebytes(dev, dst_offset, 
&pbuf[src_offset + sizeof(hdr)], phdr->len)` |
   | error exit | `:730` | label `errout:` |
   
   Line numbers in other renderings differ; the code text locates each anchor.
   
   **Evidence.** One before/after function-level ASan run, harness geometry 
`blocksize=128`, `erasesize=512`, `neraseblocks=2`, 
`CONFIG_MTD_CONFIG_RAM_CONSOLIDATE=1`, image with 31 well-formed active entries 
followed by an active header at offset 499 with `len=200`: `before.log` reports 
`heap-buffer-overflow`, `READ of size 119` at `mtd_config.c:685`, past a 
512-byte region allocated at `:579`; with the patch, `returned -5` and no ASan 
report.
   
   **Expected.** Consolidation rejects an entry whose declared payload runs 
past the block end instead of copying it.
   
   **Patch.** Validate `src_offset + sizeof(hdr) + phdr->len <= dev->erasesize` 
before the copy and take the `errout` path when it does not hold.


-- 
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]

Reply via email to