Swatantra-66 opened a new pull request, #21979:
URL: https://github.com/apache/nuttx/pull/21979
## Summary
POSIX IEEE Std 1003.1 (`fcntl(2)` `F_DUPFD`), Linux specifications, and file
descriptor allocation rules specify parameter validation, bounds constraints,
and descriptor decoding for `fdlist_dupfile()` and `fdcheck_restore()`:
1. **Validate `minfd >= 0` and `minfd < limit` in `fdlist_dupfile()`**:
Previously, `fdlist_dupfile()` only checked `minfd >= limit` without
verifying `minfd >= 0`. Passing a negative `minfd` (e.g., `-1` or `-9`)
bypassed the limit check because negative values are strictly `< limit`. This
caused:
* Negative array indexing: `j = minfd %
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK = -1`.
* Out-of-bounds heap memory access: accessing `&list->fl_fds[0][-1]` or
`&list->fl_fds[-1][-1]` (CWE-125 / CWE-129).
* Heap corruption: if `fdp->f_file == NULL`, it wrote into out-of-bounds
heap memory before the table (`atomic_add(&filep->f_refs, 1)` and `fdp->f_file
= filep`) (CWE-787).
Checking `minfd < 0` upfront before and after restoration safely returns
`-EINVAL`.
2. **Defensive NULL List and File Pointer Validation**:
Defensively validates `list == NULL || filep == NULL` returning
`-EINVAL`. This eliminates:
* CWE-476 NULL pointer dereference in `fdlist_limit(list)` when `list ==
NULL`.
* CWE-476 NULL pointer dereference in `atomic_add(&filep->f_refs, 1)`
when `filep == NULL` (previously only guarded by release-stripped
`DEBUGASSERT(filep)`).
3. **Avoid False Descriptor Decoding in `fdcheck_restore()`**:
Under `CONFIG_FDCHECK`, `fdcheck_protect()` never protects standard
descriptors (`fd <= 2`), and assigned tags are never 0 (`g_fdcheck_tag` skips
0). Previously, `fdcheck_restore()` only checked `(fd == 0 || val < 0)`.
Consequently:
* Bare integers $\ge 256$ (such as `minfd = 256`, `0x0100`, where
$\text{fd} = 1$ and $\text{tag} = 0$) were mistakenly interpreted as tagged
descriptors for descriptor 1 with tag 0.
* If descriptor 1 (stdout) was open, `ioctl(1, FIOC_GETTAG_FDCHECK,
&tag_store)` succeeded with `tag_store = 0`, matching `tag_expect = 0` and
returning `1`. This silently bypassed `minfd >= limit` checks on tasks with
`limit = 16`.
* If descriptor 1 was closed, `ioctl(1)` failed with `-EBADF` and invoked
`PANIC()`.
Updating `fdcheck_restore()` to check `(fd <= 2 || tag_expect == 0 || val
< 0)` ensures bare integers and untagged descriptors are returned directly
without false mutation or panics.
---
## Before vs. After Behavior
| Scenario | Before | After |
| :--- | :--- | :--- |
| `fdlist_dupfile(list, flags, -1, filep)` | Negative index calculation (`j
= -1`); out-of-bounds heap read/write at `fl_fds[0][-1]` | Fails immediately,
returns `-EINVAL` |
| `fdlist_dupfile(NULL, flags, 0, filep)` | Dereferenced `NULL->fl_limit`
(Kernel Panic / Crash) | Safely returns `-EINVAL` |
| `fdlist_dupfile(list, flags, 0, NULL)` | In release builds, dereferenced
`NULL->f_refs` (Crash) | Safely returns `-EINVAL` |
| `CONFIG_FDCHECK` with `minfd = 256`, `limit = 16`, fd 1 open | Decoded 256
as descriptor 1; bypassed limit check and allocated fd $\ge 1$ | Returns bare
256; correctly rejected with `-EINVAL` ($256 \ge 16$) |
| `CONFIG_FDCHECK` with `minfd = 256`, fd 1 closed | `ioctl(1)` failed with
`-EBADF` and triggered kernel `PANIC()` | Returns bare 256; correctly rejected
with `-EINVAL` |
---
## Impact
* **Memory Safety & Robustness**: Eliminates negative heap array subscripts
(CWE-125 / CWE-129 / CWE-787) and NULL pointer dereferences (CWE-476).
* **POSIX / System Conformance**: Correctly rejects negative or out-of-limit
`minfd` values with `-EINVAL`.
* **Subsystem Reliability**: Prevents false tagged descriptor decoding and
spurious `PANIC()` calls under `CONFIG_FDCHECK`.
---
## Testing
### 1. Build Verification
Cleanly built with `arm-none-eabi-gcc 10.3.1` on `stm32f103-minimum:nsh`:
```text
$ ./tools/configure.sh stm32f103-minimum:nsh
$ make -j4
...
CC: inode/fs_files.c
...
LD: nuttx
CP: nuttx.bin
$ arm-none-eabi-size nuttx
text data bss dec hex filename
53427 344 3144 56915 de53 nuttx
```
### 2. Code Style & Commit Verification
Verified with `nxstyle` inside Docker:
```text
$ ./tools/nxstyle fs/inode/fs_files.c
No errors, no warnings.
$ ./tools/nxstyle libs/libc/misc/lib_fdcheck.c
No errors, no warnings.
```
Verified with `checkpatch.sh`:
```text
$ ./tools/checkpatch.sh -r -m -g HEAD~1..HEAD
✔️ All checks pass.
```
### 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]