net/sockstats: prevent crash in setNetMon (#13985)

(cherry picked from commit 6985369479db2c9d5bacccbde6d66630a81eb1ab)
This commit is contained in:
Andrea Gottardo 2024-10-31 12:00:34 -07:00 committed by Andrea Gottardo
parent d2914f5ef2
commit b73831b2b5

View File

@ -279,7 +279,13 @@ func setNetMon(netMon *netmon.Monitor) {
if ifName == "" {
return
}
ifIndex := state.Interface[ifName].Index
// DefaultRouteInterface and Interface are gathered at different points in time.
// Check for existence first, to avoid a nil pointer dereference.
iface, ok := state.Interface[ifName]
if !ok {
return
}
ifIndex := iface.Index
sockStats.mu.Lock()
defer sockStats.mu.Unlock()
// Ignore changes to unknown interfaces -- it would require