Saturate timerfd timeout conversions - #368
Merged
Merged
Conversation
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.
jserv
reviewed
Sep 4, 2026
| 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); |
Contributor
There was a problem hiding this comment.
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.
jserv
reviewed
Sep 4, 2026
jserv
reviewed
Sep 4, 2026
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
marked this pull request as ready for review
September 4, 2026 23:05
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
marked this pull request as ready for review
September 5, 2026 00:04
Contributor
|
Thank @Suzu1Dev for contributing! |
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.
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:
with the baseline's 279 pass, 0 fail, 10 skip.
33.0, Why3 1.8.2, Alt-Ergo 2.6.3 and Z3 4.16.0.
mutations caught. The existing helper and ACSL contracts are unchanged.
Summary by cubic
Fixes timerfd timeout conversions so large valid
timerfd_settimerequests no longer expire immediately or return negative intervals, and applies the same saturating conversion to host clock reads.timespechelper for the initial value, interval, absolute deadline, and host clock conversions.Written for commit 28388ed. Summary will update on new commits.