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

   ### Description / Steps to reproduce the issue
   
   ### Summary
   `fs_heap_strndup()` in `fs/fs_heap.c` copies `strnlen(s, size) + 1` bytes 
from `s`. When `s` is not NUL-terminated within `size` bytes, that extra byte 
is one past the bound the caller promised:
   
   ```c
   FAR char *fs_heap_strndup(FAR const char *s, size_t size)
   {
     size_t len = strnlen(s, size) + 1;
     FAR char *copy = fs_heap_malloc(len);
   
     if (copy != NULL)
       {
         memcpy(copy, s, len);
         copy[len - 1] = '\0';
       }
   
     return copy;
   }
   ```
   
   ### Root Cause
   1. POSIX `strndup(s, n)` copies at most `n` bytes of `s` and always appends 
a NUL.
   2. NuttX `strndup()` / `nx_strndup()` in `libs/libc/string/lib_strndup.c` 
already do that: `memcpy(news, s, allocsize)` with `allocsize = strnlen(s, 
size)`, then `news[allocsize] = '\0'`.
   3. `fs_heap_strndup()` instead `memcpy`s `allocsize + 1` bytes. If 
`strnlen(s, size) == size`, it reads `s[size]`, which the caller did not 
guarantee to be mapped or initialized.
   4. `tmpfs_add_dirent()` calls `fs_heap_strndup(name, namelen)` after 
optionally dropping a trailing `'/'`. That path happens to keep `name[namelen]` 
inside the original C string, so it often works by accident. The helper is 
still wrong for any caller that passes an unterminated `size`-byte buffer.
   
   ### Steps to Reproduce
   1. Build a 4-byte buffer with no NUL: `char buf[4] = {'a','b','c','d'};`
   2. Call the current `fs_heap_strndup(buf, 4)` logic: `strnlen` is 4, 
`memcpy(..., 5)` reads `buf[4]`.
   3. Under ASan / a guard page after `buf`, that is a 1-byte overread.
   
   Host check of the copy length (gcc 13.3, Ubuntu 24.04):
   
   ```
   strnlen(unterminated, 4) + 1 = 5   /* current, overread */
   strnlen(unterminated, 4)     = 4   /* libc strndup, then write NUL */
   ```
   
   ### Expected Behavior
   Copy `strnlen(s, size)` bytes, then write a NUL at `copy[n]`. Do not read 
`s[size]` unless a NUL appears earlier.
   
   ### Proposed Fix
   Match `libs/libc/string/lib_strndup.c`:
   
   ```c
   size_t allocsize = strnlen(s, size);
   copy = fs_heap_malloc(allocsize + 1);
   if (copy != NULL)
     {
       memcpy(copy, s, allocsize);
       copy[allocsize] = '\0';
     }
   ```
   
   ### On which OS does this issue occur?
   
   [OS: Linux]
   
   ### What is the version of your OS?
   
   Ubuntu 24.04
   
   ### NuttX Version
   
   master
   
   ### Issue Architecture
   
   [Arch: all]
   
   ### Issue Area
   
   [Area: File System]
   
   ### Host information
   
   Generic architecture-independent C defect in fs/fs_heap.c. Compared with 
libs/libc/string/lib_strndup.c. Reproduced the copy-length mismatch with a host 
C program.
   
   ### Verification
   
   - [x] I have verified before submitting the report.


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