Skip to content

Saturate timerfd timeout conversions - #368

Merged
jserv merged 4 commits into
sysprog21:mainfrom
Suzu1Dev:fix/timerfd-time-overflow
Sep 5, 2026
Merged

Saturate timerfd timeout conversions#368
jserv merged 4 commits into
sysprog21:mainfrom
Suzu1Dev:fix/timerfd-time-overflow

Conversation

@Suzu1Dev

@Suzu1Dev Suzu1Dev commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Large valid timerfd_settime requests can expire immediately or return
negative timerfd_gettime intervals because clamping seconds leaves the
nanosecond addition able to overflow. Reuse the existing saturating timespec
helper for the initial value, interval and absolute deadline, and derive the
host timeout from the same bounded value.

The regression includes tv_sec=INT64_MAX/1000000000, tv_nsec=999999999,
plus microsecond-boundary, maximum-seconds, interval and absolute-clock
cases. All six fail before the fix and pass afterward. The baseline UBSAN
build aborts on signed overflow.

Validated on Apple M3 Pro, macOS 27.0 (26A5425a), SDK 26.5:

  • make elfuse and make check-format pass.
  • make -j6 check passes, including the throughput guardrail and sharun.
  • make test-matrix-elfuse-aarch64: 280 pass, 0 fail, 10 skip, compared
    with the baseline's 279 pass, 0 fail, 10 skip.
  • Existing timerfd tests: 24/24; new tests: 6/6 in release and UBSAN builds.
  • QEMU Linux reference: all six new cases pass.
  • make verify-timespec: 45/45 proof obligations discharged with Frama-C
    33.0, Why3 1.8.2, Alt-Ergo 2.6.3 and Z3 4.16.0.
  • make verify-mutants MUTANT_TARGET=timespec MUTANT_JOBS=3: all nine
    mutations caught. The existing helper and ACSL contracts are unchanged.

Summary by cubic

Fixes timerfd timeout conversions so large valid timerfd_settime requests no longer expire immediately or return negative intervals, and applies the same saturating conversion to host clock reads.

  • Clamping only seconds could overflow the nanosecond addition; the fix reuses the existing saturating timespec helper for the initial value, interval, absolute deadline, and host clock conversions.
  • Adds seven regression tests covering nanosecond, microsecond, maximum-seconds, interval, absolute-clock, and rearmed-interval boundaries, validating remaining values against clock samples bracketing settime and gettime.

Written for commit 28388ed. Summary will update on new commits.

Review in cubic

Clamping only seconds leaves the nanosecond addition able to overflow.
Large valid timerfd requests then expire immediately or return
negative intervals. Use the existing proved timespec conversion for
the deadline, initial value and interval, and derive the host timeout
from the same bounded value.

Add six boundary cases to the unit and matrix suites. They fail under
the original host, pass after the fix and pass on the QEMU Linux
reference.
cubic-dev-ai[bot]

This comment was marked as resolved.

Comment thread src/syscall/fd.c
int_sec = INT64_MAX / NS_PER_SEC;
int64_t value_us = val_sec * US_PER_SEC + its.it_value_nsec / 1000;
int64_t interval_ns = int_sec * NS_PER_SEC + its.it_interval_nsec;
int64_t value_ns = timespec_to_ns_sat(its.it_value_sec, its.it_value_nsec);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timespec_to_ns_sat saturates at sec > TIMESPEC_SEC_MAX; ktime_set saturates at sec >= KTIME_SEC_MAX, same 9223372036. For {9223372036, 0} Linux stores KTIME_MAX and timerfd_gettime reports 9223372036.854775807, this reports 9223372036.000000000. 0.85 s on a 292 year timer, so the behavior may not be worth changing, but the comment above claims the conversion matches timespec64_to_ktime and the thresholds differ. Either clamp sec >= TIMESPEC_SEC_MAX here or soften the claim.

Comment thread src/syscall/fd.c Outdated
Comment thread tests/test-timerfd-overflow.c Outdated
@Suzu1Dev
Suzu1Dev marked this pull request as draft September 4, 2026 22:06
Host clock conversions still multiply seconds without saturation.
Reuse the existing helper for timer setup, reads and fdinfo snapshots,
and describe the shared conversion without implying Linux's exact
ktime saturation threshold.

Require over a billion seconds remaining in the large-timeout cases,
while keeping the 30-second floor for the 60-second initial expiry.
This rejects large timers accidentally shortened to an hour.
The overflow cases query timers before their first expiration, so they
do not check the state recorded when a large interval is rearmed.

Read a short initial expiration before checking the remaining time and
repeat interval. Check the old value returned when disarming that timer.
Bound the readiness wait to keep a missed expiration from hanging tests.
@Suzu1Dev
Suzu1Dev marked this pull request as ready for review September 4, 2026 23:05
cubic-dev-ai[bot]

This comment was marked as resolved.

@Suzu1Dev
Suzu1Dev marked this pull request as draft September 4, 2026 23:23
A lower bound on seconds accepts malformed nanoseconds and incorrect
absolute remaining times. Reject invalid or unrepresentable timer values
and check absolute timers against clock samples bracketing settime and
gettime. Raw clock syscalls keep the bounds on the timer backend clock.

Relative and rearmed timers retain their existing lower bounds. The case
whose initial expiration is 60 seconds keeps its separate threshold.

The regression passes all eight checks on elfuse release, the existing
UBSAN host build and QEMU Linux. Six synthetic readback faults pass the
original assertions and fail the strengthened assertions;
make check-format also passes.
@Suzu1Dev
Suzu1Dev marked this pull request as ready for review September 5, 2026 00:04
cubic-dev-ai[bot]

This comment was marked as resolved.

@jserv
jserv merged commit 6b23080 into sysprog21:main Sep 5, 2026
15 checks passed
@jserv

jserv commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Thank @Suzu1Dev for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants