Skip to content

fix: prevent arithmetic panics on clock jumps and oversized packets - #101

Merged
lance0 merged 1 commit into
masterfrom
fix/arithmetic-safety
Jul 6, 2026
Merged

lance0 merged 1 commit into
masterfrom
fix/arithmetic-safety

Conversation

@lance0

@lance0 lance0 commented Jul 1, 2026 •

Copy link
Copy Markdown
Owner

Summary

Prevents arithmetic panics caused by clock jumps (VM migration, hypervisor time-sync) and oversized packet construction.

Changes

  • Locks up w/ Multiple Traces #19 (low): Use saturating_duration_since instead of duration_since in receiver RTT calculation (L261), timeout cleanup (L466), and engine.rs IPv6 Echo Reply polling (L1441). Prevents receiver/engine-thread panic if the monotonic clock moves backward relative to a stored Instant. (Rebase picked up the engine.rs site flagged in review — now all three Instant::now().duration_since sites are converted.)
  • Getting error when trying to 'brew install' #13 (medium): Use saturating_sub in PeeringDB cache expiry (is_expired), cache status display, and backoff period checks. Prevents u64 underflow panic if fetched_at or last_failure timestamp is somehow in the future (clock jump, corrupt cache).
  • ttl PMTUD settles on an MTU 6 bytes lower than available MTU #23 (low): Add assert! in build_ipv4_packet to catch IPv4 total_len u16 wrap on oversized transport payloads instead of silently truncating the length field.

Testing

  • All 551 tests pass (including 3 new)
  • cargo clippy --all-targets -- -D warnings clean
  • New tests: test_build_ipv4_packet_max_size (exactly u16::MAX), test_build_ipv4_packet_oversized_panics (should_panic), test_ix_cache_future_fetched_at_does_not_panic

Risk

Low — mechanical saturating/checked arithmetic replacements, no logic changes. Rebased onto current master; conflict in receiver.rs timeout-cleanup block resolved by keeping master's refactored structure and applying saturating_duration_since.

@lance0 lance0 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Review: PR #101 — fix: prevent arithmetic panics on clock jumps and oversized packets

Verdict: COMMENT (changes are correct and safe to merge as-is; one scope gap worth a follow-up).

Reviewed against the PR branch (fix/arithmetic-safety) and the three changed files. I verified each of the six review-focus points called out in the assignment.

Findings

  1. [Medium] Incomplete coverage of #19 — engine.rs:1438 — src/trace/engine.rs, poll_ipv6_echo_reply, line 1438 still uses Instant::now().duration_since(probe.sent_at). This is the identical panic pattern this PR fixes in receiver.rs. poll_ipv6_echo_reply is a live production path (single-target ProbeEngine polling ICMPv6 Echo Reply on Linux, since the kernel delivers Echo Reply only to the sending socket). A backward monotonic-clock jump (VM migration / suspend-resume) panics this thread exactly as it would the receiver thread. The PR body scopes #19 to "receiver RTT calculation," but the issue itself is the general Instant::now().duration_since(probe.sent_at) pattern; this site should be converted too. Recommend a one-line follow-up: Instant::now().saturating_duration_since(probe.sent_at). Not a merge blocker for this PR (the receiver sites it does touch are correct), but #19 should not be considered fully closed while this site remains.

  2. [Low] No test for the saturating_duration_since conversion — The three new tests cover #13 (test_ix_cache_future_fetched_at_does_not_panic) and #23 (test_build_ipv4_packet_max_size, test_build_ipv4_packet_oversized_panics), but none exercise the #19 saturating_duration_since paths in receiver.rs. Forcing a backward Instant directly is awkward, but the RTT computation could be extracted behind a small pure helper taking explicit Instant/Duration args so the zero-RTT degradation path is unit-testable. Flagging as a coverage gap, not a blocker.

  3. [Verified] Both receiver.rs duration_since sites converted — Confirmed: line 233 (RTT calc) and line 435 (timeout cleanup in the pending.retain closure) both use saturating_duration_since. No remaining .duration_since( calls on Instant in receiver.rs. The duration_since(SystemTime::UNIX_EPOCH) calls (used to derive unix seconds with unwrap_or_default()) are correctly left as-is — they are a different pattern and already guarded.

  4. [Verified] All three saturating_sub sites in ix.rs converted, no missed sites — Lines 87 (is_expired), 317 (backoff check), and 569 (get_cache_status cache-expiry display) all use saturating_sub. I scanned for any remaining plain - subtraction on the same u64 timestamps (fetched_at, last_failure/last_fail, now); the only remaining now - X occurrences (lines 822, 882, 886, 883, 887) are in #[cfg(test)] test setup/assertions, not production. Production coverage is complete.

  5. [Verified] build_ipv4_packet assert is correctly ordered and edge-safe — total_payload = IPV4_HEADER_SIZE + transport.len() is computed as usize (≥32-bit on all supported targets) before the as u16 cast, so there is no wrap preceding the bounds check. The assert total_payload <= u16::MAX as usize then gates the cast. Edge cases checked:

    • Empty transport (0 bytes): total_payload = 20, assert passes, total_len = 20 (valid header-only packet).
    • Max transport (65515 bytes): total_payload = 65535, assert passes, total_len = 65535.
    • Oversized (65516 bytes): total_payload = 65536, assert fails with the descriptive message.
      The should_panic test correctly pins the message substring.
  6. [Verified / Positive] assert placement protects all callers — build_ipv4_packet is called from engine.rs (5×: ICMP/UDP/TCP send + PMTUD) and receiver.rs paths, so the single assert in the shared builder covers every packet-construction entry point; no duplication needed.

  7. [Nit] Degradation behavior worth a comment — When saturating_duration_since clamps a backward clock to Duration::ZERO, the affected probe's RTT is reported as 0. This is strictly better than panicking the thread, but a single 0-ms sample can slightly skew min/avg RTT statistics for that one probe. Benign and rare (requires a monotonic-clock regression); a one-line comment at the call site noting "RTT may read 0 on a backward clock jump; preferred over panicking the receiver" would make the intent explicit for future readers.

Summary

The three changes the PR actually makes are correct, minimal, and well-targeted. saturating_sub coverage in ix.rs is complete; the build_ipv4_packet assert is correctly ordered and handles the empty/max/oversized edges; both receiver.rs sites are converted. The one substantive concern is that #19 is only partially fixed — the same Instant::now().duration_since(probe.sent_at) panic remains in engine.rs:1438. Recommend merging this PR and opening a tiny follow-up (or amending here) to convert that one line so #19 is fully closed.

- Use saturating_duration_since in receiver RTT and timeout calculations to
  prevent panic if monotonic clock moves backward (VM migration, hypervisor
  time-sync) (#19)
- Use saturating_sub in PeeringDB cache expiry/backoff checks to prevent
  underflow panic if fetched_at is in the future (clock jump) (#13)
- Add assert in build_ipv4_packet to catch IPv4 total_len u16 wrap on
  oversized transport payloads instead of silently truncating (#23)

All 499 tests pass (including 3 new); clippy clean.
@lance0
lance0 force-pushed the fix/arithmetic-safety branch from 7affc4e to 42693ae Compare July 6, 2026 13:37

@lance0 lance0 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Follow-up after rebase

Confirmed the rebased PR addresses the Medium finding from my earlier review: src/trace/engine.rs:1441 now uses Instant::now().saturating_duration_since(probe.sent_at) (the poll_ipv6_echo_reply RTT path). I verified there are no remaining Instant::now().duration_since / .duration_since(probe...) call sites in either engine.rs or receiver.rs. Issue #19 is now fully closed across all three RTT/timeout sites (receiver.rs L233 + L435, engine.rs L1441).

No further issues. The saturating_sub sites in ix.rs and the build_ipv4_packet assert are unchanged and remain correct per my prior review. Ship it.

@lance0
lance0 merged commit 6d80568 into master Jul 6, 2026
5 checks passed
@lance0
lance0 deleted the fix/arithmetic-safety branch July 6, 2026 13:50
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.

1 participant