Repository navigation
fs/vfs: validate parameters and fix error handling in timerfd - #21842
Merged
acassis merged 1 commit intoNov 15, 2026
Merged
Conversation
Swatantra-66
requested review from
Donny9,
pkarashchenko and
xiaoxiang781216
as code owners
November 13, 2026 13:13
xiaoxiang781216
approved these changes
Nov 13, 2026
|
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
force-pushed
the
fix/vfs-timerfd-posix-validation
branch
from
November 14, 2026 07:26
ac202b5 to
d4864a3
Compare
acassis
approved these changes
Nov 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 thetimerfdfamily of system calls:timerfd_settime()timerfd_settime()previously jumped toerrout_with_filepwithret == 0(fromfile_get()), silently returningOK(0) without arming any timer or reporting an error. Setsret = -EINVAL;before jumping to returnERRORwitherrno = EINVAL.filep->f_inode != NULLbefore dereferencingfilep->f_inode->u.i_ops.itimerspecstructures wheretv_nsec < 0,tv_nsec >= NSEC_PER_SEC, ortv_sec < 0returningERRORwitherrno = EINVAL.timerfd_setup_cancel(): acceptsTFD_TIMER_CANCEL_ON_SETwithout failing on relative timers, but gatesregister_clock_notifier()to only arm cancellation whenTFD_TIMER_CANCEL_ON_SETandTFD_TIMER_ABSTIMEare both selected and the clock isCLOCK_REALTIME.timerfd_gettime()erroutwithret == 0, executingset_errno(-0)and returningERRORwitherrno = 0. Setsret = -EINVAL;before jumping.filep->f_inode != NULLbefore dereferencingfilep->f_inode->u.i_ops.curr_value != NULLreturningERRORwitherrno = EFAULT.timerfd_read()buffer == NULL(-EFAULT) fromlen < sizeof(timerfd_t)(-EINVAL) perread(2)/timerfd(2).*(FAR timerfd_t *)bufferwithmemcpy(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
timerfd_settime()inode->u.i_ops != &g_timerfd_fops)OK(0)ERROR,errno = EINVALtimerfd_settime()filep->f_inode == NULL(socket / pseudo-fd)ERROR,errno = EINVALtimerfd_settime()tv_nsec < 0or>= 1e9, ortv_sec < 0ERROR,errno = EINVALtimerfd_gettime()ERROR,errno = 0ERROR,errno = EINVALtimerfd_gettime()filep->f_inode == NULLERROR,errno = EINVALtimerfd_read()buffer == NULL-EINVAL-EFAULTtimerfd_read()memcpyImpact
timerfdbehavior; eliminated silent success bugs, missing error codes, and crashes on invalid file descriptors.Testing
ubuntu:22.04).sim:nshwithCONFIG_TIMER_FD=y.tools/nxstyle fs/vfs/fs_timerfd.c-> 0 errors, 0 warnings.1. Style Check (
nxstyle)2. Build and Linkage Log
3. Simulator Runtime Verification