Skip to content

Compute the RTT statistics deltas in float64 to avoid overflow - #243

Open
amlaban65 wants to merge 1 commit into
prometheus-community:mainfrom
amlaban65:fix-avgrtt-overflow
Open

amlaban65 wants to merge 1 commit into
prometheus-community:mainfrom
amlaban65:fix-avgrtt-overflow

Conversation

@amlaban65

Copy link
Copy Markdown

Fixes #240

updateStatistics computes the Welford deltas (pkt.Rtt - p.avgRtt) as time.Duration. The RTT is receivedAt.Sub(timestamp) with the timestamp taken from the echoed payload, so a forged reply can produce RTTs more than ~292 years apart. Their difference then wraps int64, and AvgRtt (and StdDevRtt, which uses the same deltas) end up wrong — with the RTTs from the issue, AvgRtt becomes -2124047h47m16.85s instead of 50 years.

This change computes both deltas in float64, as the issue suggests, and keeps the stored average as a time.Duration. For RTTs below 2^53 ns (about 104 days) the float64 values are exact and the truncating time.Duration(delta / n) conversion gives the same result as the previous integer division, so the statistics for normal pings are unchanged.

Testing

  • Added TestStatisticsAvgRttOverflow (the reproduction from the issue, plus a StdDevRtt check). It fails on main and passes with this change.
  • The existing TestStatisticsSunny, TestStatisticsLossy and TestStatisticsZeroDivision still pass with the same exact values.
  • gofmt and go vet are clean. I ran each test in the package individually before and after the change: the only difference is the new test going from fail to pass. Six tests that open raw ICMP sockets (e.g. TestNewPingerValid) can't run in my sandbox (socket: permission denied) and fail the same way with and without the change, so those rely on CI.

updateStatistics computed the Welford deltas as time.Duration. The RTT
comes from the timestamp echoed in the reply, so a forged reply can give
two RTTs more than ~292 years apart; their difference then wraps int64
and AvgRtt and StdDevRtt end up wrong (AvgRtt even changes sign).

Compute the deltas in float64. For RTTs below 2^53 ns (about 104 days)
the float64 values are exact and the truncating conversion matches the
previous integer division, so normal results are unchanged.

Fixes prometheus-community#240

Signed-off-by: Ahmed Laban <79754427+amlaban65@users.noreply.github.com>
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.

updateStatistics: AvgRtt wraps int64 for RTTs more than 292 years apart (forged replies)

1 participant