Skip to content

fs/vfs: validate parameters and fix error handling in timerfd - #21842

Merged
acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/vfs-timerfd-posix-validation
Nov 15, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/vfs-timerfd-posix-validation

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

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)

$ tools/nxstyle fs/vfs/fs_timerfd.c
$ echo $?
0

2. Build and Linkage Log

$ 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

$ ./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.

@github-actions github-actions Bot added Area: File System File System issues Size: S The size of the change in this PR is small labels Nov 13, 2026
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

POSIX IEEE Std 1003.1 and Linux specifications (timerfd_create(2),
timerfd_settime(2), timerfd_gettime(2), read(2)) define parameter
validation and error handling for timerfd descriptors:

1. In timerfd_settime():
   - Fix silent success bug on non-timer descriptors: previously,
     calling timerfd_settime() on a regular file, socket, or character
     device jumped to errout_with_filep where ret was 0, silently
     returning OK (0). Sets ret = -EINVAL before jumping.
   - Guard against filep->f_inode == NULL before dereferencing u.i_ops
     to prevent kernel NULL pointer dereference panics (CWE-476).
   - Validate input itimerspec bounds (tv_nsec < 0, tv_nsec >= 1e9,
     or tv_sec < 0) returning ERROR with errno = EINVAL.
   - Gate clock cancellation registration to match Linux v6.6
     timerfd_setup_cancel(): only registers clock notifier when
     TFD_TIMER_CANCEL_ON_SET and TFD_TIMER_ABSTIME are both present
     and clock is CLOCK_REALTIME.

2. In timerfd_gettime():
   - Fix missing errno bug on non-timer descriptors: previously jumped
     to errout with ret = 0, returning ERROR with errno = 0. Sets
     ret = -EINVAL before jumping.
   - Guard against filep->f_inode == NULL to prevent CWE-476 panic.
   - Validate curr_value != NULL returning ERROR with errno = EFAULT.

3. In timerfd_read():
   - Return -EFAULT when buffer == NULL and -EINVAL when
     len < sizeof(timerfd_t) per POSIX read(2) and timerfd(2).
   - Use memcpy() instead of direct 64-bit cast dereference to prevent
     hardware memory alignment faults on unaligned user buffers.

Assisted-by: Google Gemini <gemini-3.8-flash>
Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the fix/vfs-timerfd-posix-validation branch from ac202b5 to d4864a3 Compare November 14, 2026 07:26
@acassis
acassis merged commit ea6fa45 into apache:master Nov 15, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: File System File System issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants