From ff7dbbfe24a00e1521ee2b9f41f6126d6f208e59 Mon Sep 17 00:00:00 2001 From: Zeling Feng Date: Tue, 26 Mar 2024 12:04:26 -0700 Subject: [PATCH] Avoid panic opportunity for TCP keep-alive timers There is a panic opportunity if the timer fires but the socket is going through cleanup. In this case `e.route` might go away but the timer handler for keep-alive continues to execute and causes panics. The fix is to return early from the handler if we find out the route is already removed. PiperOrigin-RevId: 619268432 --- pkg/tcpip/transport/tcp/connect.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/tcpip/transport/tcp/connect.go b/pkg/tcpip/transport/tcp/connect.go index 4a7297734..b9e2b6399 100644 --- a/pkg/tcpip/transport/tcp/connect.go +++ b/pkg/tcpip/transport/tcp/connect.go @@ -1292,6 +1292,11 @@ func (e *Endpoint) handleSegmentLocked(s *segment) (cont bool, err tcpip.Error) func (e *Endpoint) keepaliveTimerExpired() tcpip.Error { userTimeout := e.userTimeout + // If the route is not ready or already cleaned up, then we don't need to + // send keepalives. + if e.route == nil { + return nil + } e.keepalive.Lock() if !e.SocketOptions().GetKeepAlive() || e.keepalive.timer.isUninitialized() || !e.keepalive.timer.checkExpiration() { e.keepalive.Unlock()