jingfei195887 opened a new pull request, #22020:
URL: https://github.com/apache/nuttx/pull/22020

   ## Summary
   
   - **mtd: filemtd: check file_read() return to prevent stack canary
     corruption.**
     `filemtd_write()` stores the result of `file_read()` in `buflen` and
     `remain` without checking it. If the backing file becomes invalid,
     `file_read()` returns a negative errno. The loop only resets `pout` and
     writes back when `buflen` or `remain` is exactly zero, so with a negative
     value `pout` runs past the 128-byte stack buffer and corrupts the stack.
     Return the error (or `-EIO` at end of file) instead.
   
   - **drivers/mtd/filemtd: report I/O errors of the backing file.**
     filemtd ignored the results of `file_seek()`, `file_read()` and
     `file_write()`: `bread`, `bwrite`, `byteread`, `bytewrite` and `erase`
     always reported success, and `filemtd_write()` returned 0 because it
     counted `len` down to zero. A failing backing file therefore looked like
     working flash. Access the backing file with `file_pread()` /
     `file_pwrite()`, propagate every error, report short transfers as
     `-EIO`, and return the number of bytes written from `filemtd_write()`.
   
   ## Impact
   
   - New feature / API change: NO.
   - Behavior: filemtd operations now fail with the backing file's error, or
     `EIO` for a short transfer, instead of reporting success. Normal I/O on
     a backing file of the right size is unchanged.
   - Build, hardware, documentation, security, compatibility: NO.
   
   ## Testing
   
   Host: Ubuntu 22.04 x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0.
   
   `sim:nsh` with `CONFIG_MTD`, `CONFIG_FILEMTD` and tmpfs, and a small test
   app that creates a 16 KiB backing file on tmpfs, calls
   `filemtd_initialize(path, 0, 512, 4096)`, writes and reads one block,
   then truncates the backing file to 0 bytes and reads and writes again.
   
   Testing logs before change (master fc8d354f9a):
   
   ```
   filemtdio: bwrite ret=1
   filemtdio: bread ret=1 data=match
   filemtdio: backing file truncated to 0
   filemtdio: bread after truncate ret=1
   filemtdio: bwrite after truncate ret=1
   ```
   
   Testing logs after change:
   
   ```
   filemtdio: bwrite ret=1
   filemtdio: bread ret=1 data=match
   filemtdio: backing file truncated to 0
   filemtdio: bread after truncate ret=-5
   filemtdio: bwrite after truncate ret=-5
   ```
   
   The stack corruption from a negative `file_read()` result was not
   reproduced in this test.
   
   - `./tools/checkpatch.sh -g master..HEAD`: all checks pass.
   - `sim:nsh` with `CONFIG_MTD` and `CONFIG_FILEMTD`: Make and CMake builds
     pass with no new warnings in the changed file; `ostest` exits with
     status 0.
   - Verified on sim only.
   
   ## PR verification Self-Check
   
   * [x] This PR introduces only one functional change.
   * [x] I have updated all required description fields above.
   * [x] My PR adheres to Contributing Guidelines and Documentation (git commit 
title and message, coding standard, etc).
   * [ ] My PR is still work in progress (not ready for review).
   * [x] My PR is ready for review and can be safely merged into a codebase.
   


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