From b81b5883c747dcd019a228e109dbe52514710a88 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 28 Jan 2021 19:58:49 -0800 Subject: Acquire entry lock with cache lock held Avoid a race condition in which an entry is acquired while it is being evicted by overlapping the entry lock with the cache lock. PiperOrigin-RevId: 354452639 --- pkg/tcpip/stack/linkaddrcache.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'pkg') diff --git a/pkg/tcpip/stack/linkaddrcache.go b/pkg/tcpip/stack/linkaddrcache.go index 3ce7a57db..930b8f795 100644 --- a/pkg/tcpip/stack/linkaddrcache.go +++ b/pkg/tcpip/stack/linkaddrcache.go @@ -148,10 +148,10 @@ func (c *linkAddrCache) AddLinkAddress(k tcpip.Address, v tcpip.LinkAddress) { c.mu.Lock() entry := c.getOrCreateEntryLocked(k) - c.mu.Unlock() - entry.mu.Lock() defer entry.mu.Unlock() + c.mu.Unlock() + entry.mu.linkAddr = v entry.changeStateLocked(ready, expiration) } @@ -201,10 +201,10 @@ func (c *linkAddrCache) getOrCreateEntryLocked(k tcpip.Address) *linkAddrEntry { // get reports any known link address for k. func (c *linkAddrCache) get(k tcpip.Address, linkRes LinkAddressResolver, localAddr tcpip.Address, nic NetworkInterface, onResolve func(LinkResolutionResult)) (tcpip.LinkAddress, <-chan struct{}, tcpip.Error) { c.mu.Lock() - defer c.mu.Unlock() entry := c.getOrCreateEntryLocked(k) entry.mu.Lock() defer entry.mu.Unlock() + c.mu.Unlock() switch s := entry.mu.s; s { case ready: -- cgit v1.2.3