Swatantra-66 opened a new pull request, #21852:
URL: https://github.com/apache/nuttx/pull/21852
## Summary
This PR validates parameters and fixes error handling and buffer alignment
in `fs/vfs/fs_signalfd.c`:
1. **`signalfd()`**:
- **`mask == NULL` Validation**: Added validation that `mask != NULL`,
returning `EFAULT` (`-1` with `errno = EFAULT`) per Linux `signalfd(2)` / POSIX
specification. Previously, passing a NULL `mask` directly dereferenced `*mask`
at line 472, causing a kernel panic (CWE-476 NULL pointer dereference).
- **Non-Inode Descriptor Safety**: Validated that `filep->f_inode !=
NULL` before accessing `filep->f_inode->u.i_ops`. If `signalfd(fd, ...)` is
called on a valid non-inode descriptor (such as a socket or pseudo-descriptor
without an inode), dereferencing `f_inode->u.i_ops` caused a NULL pointer
dereference kernel panic. It now returns `EINVAL` as specified by `signalfd(2)`.
2. **`signalfd_file_read()`**:
- **POSIX Error Separation**: Separated `buffer == NULL` and size check.
`buffer == NULL` returns `-EFAULT` per POSIX `read(2)` (`[EFAULT] buf is
outside your accessible address space`). A buffer size less than `sizeof(struct
signalfd_siginfo)` returns `-EINVAL` per `signalfd(2)`.
- **Buffer Alignment Hardening**: Replaced direct pointer cast `(FAR
struct signalfd_siginfo *)buffer` and direct structure member assignments with
a local structure on the stack and `memcpy`. Because `struct signalfd_siginfo`
contains 64-bit integers (`ssi_ptr`, `ssi_utime`, `ssi_stime`, `ssi_addr`),
accessing an unaligned user-provided buffer directly can cause unaligned memory
access traps on strict architectures (e.g., ARM Cortex-M0/M4).
3. **`signalfd_notify()`**:
- Added `group == NULL` defensive check to prevent NULL pointer
dereference if invoked during early teardown.
---
## Comparison Table
| Issue / System Call | Prior Behavior | Hardened POSIX Behavior |
Specification |
|---|---|---|---|
| `signalfd(-1, NULL, 0)` | Kernel Panic (CWE-476 NULL dereference of
`*mask`) | Returns `-1` with `errno = EFAULT` | Linux `signalfd(2)` |
| `signalfd(sock_fd, &mask, 0)` | Kernel Panic (dereference
`f_inode->u.i_ops` when `f_inode == NULL`) | Returns `-1` with `errno = EINVAL`
| Linux `signalfd(2)` |
| `read(sfd, NULL, len)` | Returns `-1` with `errno = EINVAL` | Returns `-1`
with `errno = EFAULT` | POSIX `read(2)` |
| `read(sfd, buf, < sizeof(siginfo))` | Returns `-1` with `errno = EINVAL` |
Returns `-1` with `errno = EINVAL` | Linux `signalfd(2)` |
| `read(sfd, unaligned_buf, len)` | Potential unaligned access trap on
64-bit fields | Safe copy via `memcpy` | C standard & Architecture ABI |
---
## Impact
- Is new feature added? No.
- Impact on user: None. Corrects POSIX error codes and prevents kernel
crashes on invalid inputs.
- Impact on build: None.
- Impact on hardware: None.
- Impact on documentation: None.
- Impact on security: Hardens against CWE-476 NULL pointer dereferences and
unaligned memory traps.
---
## Testing
- **Host OS**: Windows 11 (Docker container `nuttx-builder` Linux x86_64)
- **Target**: `sim:nsh` with `CONFIG_SIGNAL_FD=y`
- **Compiler**: GCC / Ubuntu clang
### 1. Code Style Check (`nxstyle`)
```bash
$ ./tools/nxstyle fs/vfs/fs_signalfd.c
# Exit code: 0 (0 errors, 0 warnings)
```
### 2. Build Verification
```bash
$ make -j$(nproc)
...
CC: fs/vfs/fs_signalfd.c
AR: staging/libfs.a fs_signalfd.o
LD: nuttx
Pac SIM with dynamic libs..
SIM elf with dynamic libs archive in nuttx.tgz
# Exit code: 0
```
### 3. Binary Size
```text
text data bss dec hex filename
2343 72 0 2415 96f /repos/nuttx/fs/fs_signalfd.o
604233 5340 46752 656325 a03c5 /repos/nuttx/nuttx
```
### 4. Simulator Boot and Runtime Test
```text
nsh> help
help usage: help [-v] [<cmd>]
. break exec mkdir rm true
[ cat exit mkfifo rmdir
truncate
? cd expr mkrd rmmod uname
addroute cp false mount set umount
alias cmp help mv kill unset
unalias dirname hexdump nslookup sleep uptime
arp date insmod poweroff usleep watch
base64dec delroute losetup quit source xd
base64enc dmesg ls printf test
basename echo du pwd time
Builtin Apps:
dd init nsh sh
nsh> exit
```
--
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]