Skip to content

rust: time: keep Instant invariant when adding/subtracting Delta - #1273

Open
Franklin-Qi wants to merge 1 commit into
Rust-for-Linux:rust-nextfrom
Franklin-Qi:fix/instant-delta-invariant
Open

Franklin-Qi wants to merge 1 commit into
Rust-for-Linux:rust-nextfrom
Franklin-Qi:fix/instant-delta-invariant

Conversation

@Franklin-Qi

Copy link
Copy Markdown

Instant documents inner in 0..=KTIME_MAX, but Add/Sub only asserted non-negativity under CONFIG_RUST_OVERFLOW_CHECKS. With checks disabled, a negative result can be returned and later Instant-Instant subtraction may no longer be overflow-safe.

Always assert the result is non-negative, and document the panic in the operators' rustdoc.

Fixes: 22b65a4 ("rust: time: Implement Add/Sub for Instant")
Cc: stable@vger.kernel.org
Reported-by: Georgios Androutsopoulos georgeandrout13@gmail.com
Closes: #1272

URL: https://lore.kernel.org/rust-for-linux/179610727734.137620.3884652165779521586@gmail.com/

Instant documents inner in 0..=KTIME_MAX, but Add/Sub<Delta> only
asserted non-negativity under CONFIG_RUST_OVERFLOW_CHECKS. With checks
disabled, a negative result can be returned, and an addition past
KTIME_MAX silently wraps. A later Instant-Instant subtraction relies on
the invariant and may no longer be overflow-safe.

Keep the existing behaviour under CONFIG_RUST_OVERFLOW_CHECKS and panic
if the result is out of range. Without overflow checks, saturate the
arithmetic and clamp a negative result to 0, printing a warning once, so
the invariant is preserved without crashing the kernel.

Document the panic and clamping conditions in the operators' rustdoc.

Fixes: 22b65a4 ("rust: time: Implement Add<Delta>/Sub<Delta> for Instant")
Cc: stable@vger.kernel.org
Reported-by: Georgios Androutsopoulos <georgeandrout13@gmail.com>
Closes: Rust-for-Linux#1272
Signed-off-by: Franklin-Qi <yusq77@gmail.com>
@Franklin-Qi
Franklin-Qi force-pushed the fix/instant-delta-invariant branch from 64858e0 to 60c6b49 Compare December 2, 2026 08:16

This branch has not been deployed

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

time: Instant addition and subtraction can trigger undocumented panics or violate the type invariant

1 participant