Swatantra-66 opened a new pull request, #22128:
URL: https://github.com/apache/nuttx/pull/22128
## Summary
POSIX IEEE Std 1003.1 and Linux specifications (`sendfile(2)`) parameter
validation, bounds checking, and pointer safety hardening across `sendfile`:
1. **Validate self-transfer (`outfd == infd` / `outfile == infile`)**:
- Linux `sendfile(2)` explicitly specifies: `[EINVAL] out_fd and in_fd
refer to the same file descriptor.`
- Previously, invoking `sendfile(fd, fd, ...)` caused `copyfile()` to
read a chunk from `fd` into the temporary buffer (advancing offset) and write
it right back to `fd` (advancing offset again), leading to file data corruption
or infinite transfer loops.
- Now validates `outfd == infd` in `sendfile()` and `outfile == infile`
in `file_sendfile()`, returning `-EINVAL`.
2. **Validate negative offset (`offset != NULL && *offset < 0`)**:
- Linux `sendfile(2)` specifies: `[EINVAL] offset is not NULL but the
value it points to is negative.`
- Rejects negative offsets upfront with `-EINVAL`.
3. **Validate file descriptor access modes**:
- Linux `sendfile(2)` specifies: `[EBADF] The input file was not opened
for reading or the output file was not opened for writing.`
- Validates that `infile` is opened for reading (`(infile->f_oflags &
O_ACCMODE) != O_WRONLY`) and `outfile` is opened for writing
(`(outfile->f_oflags & O_ACCMODE) != O_RDONLY`), returning `-EBADF`.
4. **Defensive NULL pointer checks**:
- Defensively validates `outfile == NULL || infile == NULL` returning
`-EBADF`.
- Verifies `infile->f_inode != NULL` before evaluating
`!INODE_IS_NOSEEK(infile->f_inode)`, eliminating CWE-476 NULL pointer
dereference kernel panics on descriptors without inodes (such as sockets).
5. **Clamp `count > SSIZE_MAX`**:
- Clamps `count` to `SSIZE_MAX` to prevent signed overflow on the return
value.
6. **Preserve 64-bit return width on LP64 architectures**:
- Widens result variable `ret` from `int` to `ssize_t` in `sendfile()`
and `file_sendfile()` (`psock_sendfile` fast path), preventing 32-bit signed
integer truncation and erroneous negative returns on large file transfers (> 2
GiB) on 64-bit architectures (ARM64, x86_64, RISC-V 64).
---
## Behavior Comparison
| Condition | Before (master) | After (this PR) | Standard Conformance |
| :--- | :--- | :--- | :--- |
| `outfd == infd` | Reads/writes same file in a loop (corrupts data) |
Returns `-1`, `errno = EINVAL` | Linux `sendfile(2)` `[EINVAL]` |
| `offset != NULL && *offset < 0` | Silent seek or undefined behavior |
Returns `-1`, `errno = EINVAL` | Linux `sendfile(2)` `[EINVAL]` |
| `outfd` opened `O_RDONLY` | Fails inside write loop or corrupts | Returns
`-1`, `errno = EBADF` | Linux `sendfile(2)` `[EBADF]` |
| `infd` opened `O_WRONLY` | Fails inside read loop or corrupts | Returns
`-1`, `errno = EBADF` | Linux `sendfile(2)` `[EBADF]` |
| `infile->f_inode == NULL` | Kernel panic (CWE-476 NULL dereference) |
Safely falls back or returns error | CWE-476 Mitigation |
| `count > 2 GiB` on LP64 | Truncated to 32-bit `int`, returns `-1` |
Preserved in 64-bit `ssize_t` | LP64 correctness |
---
## Impact
- **Is new feature added?**: No
- **Is existing feature changed?**: Yes (Parameter validation & safety
conformance in `sendfile`)
- **Impact on user**: Applications receive POSIX/Linux standard error codes
(`EINVAL`, `EBADF`) when passing invalid arguments instead of corrupted files
or kernel panics.
- **Impact on build**: None.
- **Impact on hardware**: None.
- **Impact on documentation**: Updated function comments in
`include/sys/sendfile.h` and `include/nuttx/fs/fs.h`.
---
## Testing
### Build Verification
- **Target**: `sim:nsh` (`x86_64`)
- Built with `gcc (Ubuntu 11.4.0)` inside Docker:
```
LD: nuttx
Pac SIM with dynamic libs..
SIM elf with dynamic libs archive in nuttx.tgz
```
### Runtime Test Execution Log
Executed on `sim:nsh` simulator:
```
nsh> hello
=== sendfile POSIX parameter validation test ===
[TEST 1] sendfile(same_fd, same_fd): ret=-1, errno=22 (Invalid argument) ->
PASS
[TEST 2] sendfile(outfd, infd, offset=-5): ret=-1, errno=22 (Invalid
argument) -> PASS
[TEST 3] sendfile(bad_outfd, infd): ret=-1, errno=9 (Bad file descriptor) ->
PASS
[TEST 4] sendfile(outfd, bad_infd): ret=-1, errno=9 (Bad file descriptor) ->
PASS
[TEST 5] sendfile(ro_outfd, infd): ret=-1, errno=9 (Bad file descriptor) ->
PASS
[TEST 6] sendfile(outfd, wo_infd): ret=-1, errno=9 (Bad file descriptor) ->
PASS
[TEST 7] sendfile(outfd, infd, count=0): ret=0, errno=0 -> PASS
[TEST 8] sendfile(outfd, infd, offset=4, count=6): ret=6, new_offset=10 ->
PASS
[TEST 9] Transferred content verify: '456789' (expected '456789') -> PASS
=== sendfile tests complete ===
nsh> poweroff
```
### Code Style Verification
Checked with `tools/nxstyle`:
```
tools/nxstyle fs/vfs/fs_sendfile.c
tools/nxstyle include/sys/sendfile.h
tools/nxstyle include/nuttx/fs/fs.h
# 0 errors, 0 warnings across all files
```
--
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]