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

   ## Summary
   
   - **fs/tmpfs: keep the data buffer in place while the file is mapped.**
     `tmpfs_mmap()` returns a pointer into `tfo_data`, but a later `write()`,
     `ftruncate()` or `open(O_TRUNC)` goes through `tmpfs_realloc_file()`,
     which may `realloc()` or free that buffer.  The mapping then points at
     freed memory: writes made through the file are no longer visible in the
     mapping, and writes through the mapping land in freed heap memory.
   
     The file object now counts its mappings in `tfo_maps`.  While the count is
     non-zero, `tmpfs_realloc_file()` only changes the file size within the
     current allocation (zeroing the part past the new size) and returns
     `-ENOMEM` when more space is needed.  The count lives in the file object,
     so mappings from every process are covered; process exit drops it through
     `tmpfs_unmap()` from `mm_map_destroy()`.
   
     `tmpfs_mmap()` now takes the mm_map lock before the file lock, the order
     `munmap()` uses, checks the range under the file lock and counts a
     mapping only once `mm_map_add()` has succeeded.  It fails with `-ENOMEM`
     when the file's 8-bit reference count would overflow, which also bounds
     `tfo_maps`.  `tmpfs_unmap()` takes the file lock before it removes the
     entry, so an interrupted lock can no longer leave the mapping removed but
     still counted.
   
   ## Impact
   
   - New feature / API change: NO.
   - Behavior: while a tmpfs file is mapped, growing it beyond its current
     allocation (`CONFIG_FS_TMPFS_FILE_ALLOCGUARD` past the size at the time
     of the last reallocation) fails with `ENOMEM` instead of moving the
     buffer under the mapping.  Growth within the allocation, shrinking and
     `O_TRUNC` still work.  Unmapped files behave as before.
   - Memory: one byte per tmpfs file (fits in existing padding on most ABIs).
   - Build, hardware, documentation, security, compatibility: NO.
   
   ## Testing
   
   Host: Ubuntu 22.04 x86_64, gcc 13.1.0.  Base: master c890786083.
   
   `sim:nsh` with `CONFIG_FS_TMPFS`.  A test app creates a 100-byte file on a
   tmpfs, maps it with `MAP_SHARED` (`PROT_READ | PROT_WRITE`, 100 bytes),
   resizes it one of four ways, then writes `'W'` at offset 0 with `pwrite()`
   and checks whether the mapping sees it.  The mapping is only read, so a
   stale pointer cannot corrupt the heap.
   
   | resize while mapped | master | this PR |
   |---|---|---|
   | `pwrite()` 8192 bytes at offset 100 | ok, size 8292, mapping does **not** 
see the write | `ENOMEM`, size 100, mapping sees the write |
   | `ftruncate()` to 8192 | ok, size 8192, mapping does **not** see the write 
| `ENOMEM`, size 100, mapping sees the write |
   | `pwrite()` 200 bytes at offset 100 (within the allocation) | ok, size 300, 
mapping sees the write | same |
   | `open(O_TRUNC)` | ok, size 1, mapping sees the write | same |
   | `pwrite()` 8192 bytes after `munmap()` | ok | ok |
   | map one file `MAP_SHARED` 300 times | all succeed; the 8-bit reference 
count wraps and `munmap()` then fails `DEBUGASSERT(tfo->tfo_refs > 0)` | 254 
succeed, then `ENOMEM`; `munmap()` and reopening the file work |
   
   On master the first two cases read `0xe0` from the mapping, i.e. memory of
   the buffer that was freed by `realloc()`.
   
   - `./tools/checkpatch.sh -m -g master..HEAD`: all checks pass.
   - `sim:nsh` with `CONFIG_FS_TMPFS`: Make and CMake builds pass with no
     warnings in the changed files; `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