Skip to content

fix: fix overflow in tcp.sender.sendData() for 32-bit architectures. - #3

Merged
sreya merged 3 commits into
new-coder-mainfrom
jon/overflow
Mar 13, 2026
Merged

sreya merged 3 commits into
new-coder-mainfrom
jon/overflow

Conversation

@sreya

@sreya sreya commented Feb 26, 2026 •

Copy link
Copy Markdown

I figured I can just reset coder-main to the commit we're tracking in upstream gvisor at the moment but I wanted to make sure I'm not deleting anything we'd want to keep the history on. Given that, this PR merges into new a branch that points to the upstream commit that we're currently tracking in gvisor and cherry-picks the commit that fixes the overflow issue.

Bug repro:

  1. Panic shows gvisor.dev/gvisor/pkg/tcpip/transport/tcp.(*sender).splitSeg(0xc01bc86008, 0xc03d0ac800, 0xfe32de9401bff440)

splitSeg has the following signature: func (s *sender) splitSeg(seg *segment, size int) {

which means that 0xfe32de9401bff440 should be evaluated as an int (-129796711974439872), which is evidence that it overflowed.

  1. splitSeg is called from maybeSendSegment and is passed local variable available

available is optionally set to limit if it exceeds it, so if limit is negative available becomes negative.

  1. How does limit become negative?

The congestion window controls how many bytes we can have in flight. Every time we receive an ACK, CUBIC's getCwnd (cubic.go:215) grows the window using:

cwnd += (wtRtt - cwnd) / cwnd    (cubic.go:244)

where wtRtt = 0.4 * t³ + 30 (cubic.go:210) and t is the elapsed time since the CUBIC epoch start (c.T).

c.T is set once when entering congestion avoidance (cubic.go:102) and is only reset by loss events (HandleLossDetected at cubic.go:253, HandleRTOExpired at cubic.go:264). Assuming no loss or retransmission timeout, this means t grows unbounded.

Normally the per-ACK increment (wtRtt - cwnd) / cwnd would grow cwnd gradually, making the division flatten the curve. However, if a connection has no data segments flowing for longer than RTO, the idle check resets SndCwnd back to its initial value of 10 (snd.go:1015). RTO is SRTT + 4×RTTVar clamped to [200ms, 120s] (snd.go:33-36,420-426) — for Tailscale's virtual network with ~2ms RTT, RTO floors at 200ms. Note: TCP keepalives (sendEmptySegment at connect.go:1325) keep the connection alive during idle periods but bypass sendData entirely and don't trigger cc.Update, so they don't prevent the idle reset from firing on the next real data write.

Critically, the idle reset does NOT reset c.T. So after the reset, the next ACK computes (0.4 × days³ - 10) / 10 ≈ 0.4 × days³ / 10 in a single step, catapulting SndCwnd to:

1m:  0.4 * 60³ / 10      = 8,640
60m: 0.4 * 3600³ / 10    = 1,866,240,000
24h: 0.4 * 86400³ / 10   = 2.58 × 10¹³
72h: 0.4 * 259200³ / 10  = 6.97 × 10¹⁴
7d:  0.4 * 604800³ / 10  = 8.85 × 10¹⁵

After a sufficiently long persistent connection (~7 days) and a period of inactivity that resets SndCwnd, the next time we try to send a packet we overflow (snd.go:1021):

cwndLimit := (s.SndCwnd - s.Outstanding) * s.MaxPayloadSize
//           (8.85×10¹⁵ -       0)       *      1220
//         = 1.08 × 10¹⁹ // overflows int64 (max 9.22 × 10¹⁸)
if cwndLimit < limit {    // -7.65e18 < 65535? YES
    limit = cwndLimit     // limit = -7.65 × 10¹⁸
}

This limit is eventually reassigned to available (snd.go:895) and then our call on (snd.go:908) triggers a panic when checking the length (snd.go:639).

@sreya
sreya requested a review from spikecurtis March 4, 2026 04:44
@spikecurtis

Copy link
Copy Markdown
Collaborator

One thing to say is that the Linux implementation of CUBIC does reset the start of the congestion avoidance when the TCP connection comes back from being idle:

__bpf_kfunc static void cubictcp_cwnd_event(struct sock *sk, enum tcp_ca_event event)
{
	if (event == CA_EVENT_TX_START) {
		struct bictcp *ca = inet_csk_ca(sk);
		u32 now = tcp_jiffies32;
		s32 delta;

		delta = now - tcp_sk(sk)->lsndtime;

		/* We were application limited (idle) for a while.
		 * Shift epoch_start to keep cwnd growth to cubic curve.
		 */
		if (ca->epoch_start && delta > 0) {
			ca->epoch_start += delta;
			if (after(ca->epoch_start, now))
				ca->epoch_start = now;
		}
		return;
	}
}

@spikecurtis spikecurtis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This fixes one problem due to the overflow, but if the cubic congestion overflows and sets SndCwnd to a negative number, there will likely be other things that go awry.

We should handle it at the source and ensure the SndCwnd is never negative by clamping it to [0, math.MaxInt]. It might even be safer to clamp it to some big value that you'll never actually need, but we know fits in integer arithmetic. That should ensure we don't see any further bugs like this coming out of the woodwork.

Max TCP throughput goes like Window / Latency. So, if we made the max SndCwnd = 1 billion (segments), that's like 1TB window in bytes. Even with a very long round-trip like a second, that's 1TB/s throughput, which you're just never going to get.

Comment thread pkg/tcpip/transport/tcp/snd.go Resolved
@sreya
sreya requested a review from spikecurtis March 12, 2026 20:08
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.

3 participants