From c14a1a161869804af5ae2f1f73fd8eeb135ff043 Mon Sep 17 00:00:00 2001 From: Amanda Tait Date: Mon, 25 Feb 2019 10:01:25 -0800 Subject: Fix race condition in NIC.DeliverNetworkPacket cl/234850781 introduced a race condition in NIC.DeliverNetworkPacket by failing to hold a lock. This change fixes this regressesion by acquiring a read lock before iterating through n.endpoints, and then releasing the lock once iteration is complete. PiperOrigin-RevId: 235549770 Change-Id: Ib0133288be512d478cf759c3314dc95ec3205d4b --- pkg/tcpip/stack/nic.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/tcpip/stack/nic.go b/pkg/tcpip/stack/nic.go index 43d7c2ec4..a6b04faaf 100644 --- a/pkg/tcpip/stack/nic.go +++ b/pkg/tcpip/stack/nic.go @@ -403,14 +403,19 @@ func (n *NIC) DeliverNetworkPacket(linkEP LinkEndpoint, remote, _ tcpip.LinkAddr // route to each IPv4 network endpoint and let each endpoint handle the // packet. if dst == header.IPv4Broadcast { + // n.endpoints is mutex protected so acquire lock. + n.mu.RLock() for _, ref := range n.endpoints { + n.mu.RUnlock() if ref.protocol == header.IPv4ProtocolNumber && ref.tryIncRef() { r := makeRoute(protocol, dst, src, linkEP.LinkAddress(), ref) r.RemoteLinkAddress = remote ref.ep.HandlePacket(&r, vv) ref.decRef() } + n.mu.RLock() } + n.mu.RUnlock() return } -- cgit v1.2.3