Swatantra-66 opened a new pull request, #21842:
URL: https://github.com/apache/nuttx/pull/21842
## Summary
POSIX IEEE Std 1003.1 and Linux specifications (`timerfd_create(2)`,
`timerfd_settime(2)`, `timerfd_gettime(2)`, `read(2)`) explicitly specify
parameter validation and error handling for the `timerfd` family of system
calls:
1. **`timerfd_settime()`**
- **Silent Success Bug Fixed:** When invoked on a non-timer file
descriptor (e.g., a regular file, socket, pipe, or character device),
`timerfd_settime()` previously jumped to `errout_with_filep` with `ret == 0`
(from `file_get()`), silently returning `OK` (`0`) without arming any timer or
reporting an error. Sets `ret = -EINVAL;` before jumping to return `ERROR` with
`errno = EINVAL`.
- **CWE-476 NULL Pointer Dereference Fixed:** Verifies `filep->f_inode !=
NULL` before dereferencing `filep->f_inode->u.i_ops`.
- **Timespec Bounds Validation:** Rejects uninitialized or invalid
`itimerspec` structures where `tv_nsec < 0`, `tv_nsec >= NSEC_PER_SEC`, or
`tv_sec < 0` returning `ERROR` with `errno = EINVAL`.
- **Linux v6.6 Flag Compatibility:** Aligns cancellation setup with Linux
`timerfd_setup_cancel()`: accepts `TFD_TIMER_CANCEL_ON_SET` without failing on
relative timers, but gates `register_clock_notifier()` to only arm cancellation
when `TFD_TIMER_CANCEL_ON_SET` and `TFD_TIMER_ABSTIME` are both selected and
the clock is `CLOCK_REALTIME`.
2. **`timerfd_gettime()`**
- **Missing Errno Bug Fixed:** When invoked on a non-timer file
descriptor, previously jumped to `errout` with `ret == 0`, executing
`set_errno(-0)` and returning `ERROR` with `errno = 0`. Sets `ret = -EINVAL;`
before jumping.
- **CWE-476 NULL Pointer Dereference Fixed:** Verifies `filep->f_inode !=
NULL` before dereferencing `filep->f_inode->u.i_ops`.
- **Pointer Validation:** Validates `curr_value != NULL` returning
`ERROR` with `errno = EFAULT`.
3. **`timerfd_read()`**
- **POSIX Error Separation:** Distinguishes `buffer == NULL` (`-EFAULT`)
from `len < sizeof(timerfd_t)` (`-EINVAL`) per `read(2)` / `timerfd(2)`.
- **Unaligned Memory Safety:** Replaces `*(FAR timerfd_t *)buffer` with
`memcpy(buffer, &dev->counter, sizeof(timerfd_t))` to eliminate hardware
alignment faults on unaligned user buffers on strict architectures (e.g. ARM
Cortex-M0/M4).
---
### Comparison Table
| Function | Condition | Previous Behavior | Fixed Behavior (POSIX/Linux) |
| :--- | :--- | :--- | :--- |
| `timerfd_settime()` | Non-timerfd descriptor (`inode->u.i_ops !=
&g_timerfd_fops`) | Silently returns `OK` (`0`) | Returns `ERROR`, `errno =
EINVAL` |
| `timerfd_settime()` | `filep->f_inode == NULL` (socket / pseudo-fd) |
Kernel panic (NULL pointer dereference) | Returns `ERROR`, `errno = EINVAL` |
| `timerfd_settime()` | `tv_nsec < 0` or `>= 1e9`, or `tv_sec < 0` |
Unvalidated (corrupt tick conversions) | Returns `ERROR`, `errno = EINVAL` |
| `timerfd_gettime()` | Non-timerfd descriptor | Returns `ERROR`, `errno =
0` | Returns `ERROR`, `errno = EINVAL` |
| `timerfd_gettime()` | `filep->f_inode == NULL` | Kernel panic (NULL
pointer dereference) | Returns `ERROR`, `errno = EINVAL` |
| `timerfd_read()` | `buffer == NULL` | Returns `-EINVAL` | Returns
`-EFAULT` |
| `timerfd_read()` | Unaligned user buffer | Hardware alignment fault / CPU
trap | Safe transfer via `memcpy` |
---
## Impact
- Is new feature added? No.
- Impact on user: More robust and POSIX/Linux-compliant `timerfd` behavior;
eliminated silent success bugs, missing error codes, and crashes on invalid
file descriptors.
- Impact on build: None.
- Impact on hardware: Portable across all architectures; prevents hardware
unaligned memory faults on ARM Cortex-M.
---
## Testing
- **Host:** Linux x86_64 Docker container (`ubuntu:22.04`).
- **Configuration:** `sim:nsh` with `CONFIG_TIMER_FD=y`.
- **Code Style:** `tools/nxstyle fs/vfs/fs_timerfd.c` -> 0 errors, 0
warnings.
### 1. Style Check (`nxstyle`)
```console
$ tools/nxstyle fs/vfs/fs_timerfd.c
$ echo $?
0
```
### 2. Build and Linkage Log
```console
$ make -j$(nproc)
...
CC: vfs/fs_timerfd.c
IN: fs/libfs.a -> staging/libfs.a
...
LD: nuttx
Pac SIM with dynamic libs..
'/lib/x86_64-linux-gnu/libc.so.6' -> 'sim-pac/libs/libc.so.6'
'/lib64/ld-linux-x86-64.so.2' -> 'sim-pac/ld-linux-x86-64.so.2'
SIM elf with dynamic libs archive in nuttx.tgz
```
### 3. Simulator Runtime Verification
```console
$ ./nuttx
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
nsh>
```
- **What was not tested:** Protected and kernel builds, non-x86
architectures (ARM, RISC-V), and physical target hardware.
--
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]