Swatantra-66 opened a new pull request, #21884:
URL: https://github.com/apache/nuttx/pull/21884
## Summary
POSIX IEEE Std 1003.1 and Linux specifications (`dup3(2)`) specify parameter
validation, flag constraints, and descriptor handling for `dup3()`:
1. **POSIX / Linux Flags Validation**:
Linux `dup3(2)` explicitly mandates:
> *"EINVAL (dup3()) flags contain an invalid value. Or, oldfd was equal
to newfd."*
The only valid flag for `dup3()` is `O_CLOEXEC` (or `0`). Previously,
NuttX did not validate `flags`, silently accepting and ignoring invalid flag
bits. Validates `(flags & ~O_CLOEXEC) != 0` returning `-EINVAL`.
2. **Prevent Premature Heap Table Allocation on Invalid `oldfd`**:
In `fdlist_dup3()`, `fdlist_extend()` was invoked to expand the file
descriptor table on the heap *before* validating `fd1`. If `fd1` was invalid
(e.g. `fd1 = -1` or an unopened descriptor), the task's file descriptor table
was permanently expanded on the heap before `fdlist_get2()` returned `-EBADF`.
Moving `fdlist_get2()` before `fdlist_extend()` validates `fd1` first,
returning `-EBADF` with zero heap allocations and zero side-effects. If
`fdlist_extend()` fails later, `file_put(filep1)` safely releases the acquired
file reference.
3. **Defensive NULL List Validation**:
Defensively validates `list != NULL` returning `-EINVAL`, eliminating
CWE-476 NULL pointer dereferences in `fdlist_limit(list)`.
4. **Task-Specific Descriptor Resolution in `fdlist_dup2()`**:
When `fd1 == fd2`, `fdlist_dup2()` previously called `file_get(fd1,
&filep)` which queried `this_task()` rather than the provided `list`. Uses
`fdlist_get2(list, fd1, &filep, NULL)` so the descriptor is looked up in the
intended file list (e.g., during spawned task file actions in
`sched/task/task_spawnparms.c`).
---
## Before vs. After Behavior
| Scenario | Before | After |
| :--- | :--- | :--- |
| `dup3(fd1, fd2, 0x1234)` (invalid flags) | Silently ignored invalid bits,
succeeded | Fails immediately with `ERROR`, `errno = EINVAL` |
| `dup3(-1, 50, 0)` (invalid oldfd, large newfd) | Pre-allocated heap rows
expanding task fd table before returning `-EBADF` | Returns `-EBADF`
immediately; **zero heap memory allocated** |
| `fdlist_dup3(NULL, ...)` | Dereferenced `NULL->fl_limit` (Kernel Panic /
Crash) | Safely returns `-EINVAL` |
| `posix_spawn` with `dup2(5, 5)` action | Checked descriptor `5` in
caller's task instead of spawned task | Checks descriptor `5` in spawned task's
descriptor table |
---
## Impact
* **POSIX / Linux Conformance**: Rejects invalid flags with `-EINVAL`.
* **Memory & Resource Efficiency**: Prevents failing `dup3()` invocations
with invalid `oldfd` from permanently fragmenting and growing the process heap
descriptor table.
* **Security & Robustness**: Prevents NULL pointer dereferences and
incorrect task descriptor queries during process spawning.
---
## Testing
### 1. Build Verification
Cleanly built with `arm-none-eabi-gcc 10.3.1` on `stm32f103-minimum:nsh`
(`tools/ci/testlist/arm-04.dat`):
```text
$ ./tools/configure.sh stm32f103-minimum:nsh
$ make -j8
...
CC: inode/fs_files.c
...
LD: nuttx
CP: nuttx.bin
$ arm-none-eabi-size nuttx
text data bss dec hex filename
53723 468 3056 57247 df9f nuttx
```
### 2. Code Style Verification
Checked with `nxstyle` inside Docker:
```text
$ ./tools/nxstyle fs/inode/fs_files.c
No errors, no warnings.
```
### 3. What Was Not Tested
* Real hardware target (tested using cross-compilation for ARM Cortex-M3).
* Protected and Kernel build modes.
--
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]