From 0010db46e4616fd78ad26d76b647703797e8eff8 Mon Sep 17 00:00:00 2001 From: Darren Hoo Date: Tue, 22 Sep 2020 09:41:46 +0800 Subject: [PATCH] Fix a data race on message counter (#284) 3. ================== WARNING: DATA RACE Write at 0x00c00030e020 by goroutine 17: sync/atomic.AddInt64() runtime/race_amd64.s:276 +0xb github.com/slackhq/nebula.(*Interface).sendNoMetrics() github.com/slackhq/nebula/inside.go:226 +0x9c github.com/slackhq/nebula.(*Interface).send() github.com/slackhq/nebula/inside.go:214 +0x149 github.com/slackhq/nebula.(*Interface).readOutsidePackets() github.com/slackhq/nebula/outside.go:94 +0x1213 github.com/slackhq/nebula.(*udpConn).ListenOut() github.com/slackhq/nebula/udp_generic.go:109 +0x3b5 github.com/slackhq/nebula.(*Interface).listenOut() github.com/slackhq/nebula/interface.go:147 +0x15e Previous read at 0x00c00030e020 by goroutine 18: github.com/slackhq/nebula.(*Interface).consumeInsidePacket() github.com/slackhq/nebula/inside.go:58 +0x892 github.com/slackhq/nebula.(*Interface).listenIn() github.com/slackhq/nebula/interface.go:164 +0x178 --- inside.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/inside.go b/inside.go index eb1e72a..3e36b49 100644 --- a/inside.go +++ b/inside.go @@ -54,8 +54,8 @@ func (f *Interface) consumeInsidePacket(packet []byte, fwPacket *FirewallPacket, dropReason := f.firewall.Drop(packet, *fwPacket, false, hostinfo, trustedCAs) if dropReason == nil { - f.sendNoMetrics(message, 0, ci, hostinfo, hostinfo.remote, packet, nb, out) - if f.lightHouse != nil && *ci.messageCounter%5000 == 0 { + mc := f.sendNoMetrics(message, 0, ci, hostinfo, hostinfo.remote, packet, nb, out) + if f.lightHouse != nil && mc%5000 == 0 { f.lightHouse.Query(fwPacket.RemoteIP, f) } @@ -214,10 +214,10 @@ func (f *Interface) send(t NebulaMessageType, st NebulaMessageSubType, ci *Conne f.sendNoMetrics(t, st, ci, hostinfo, remote, p, nb, out) } -func (f *Interface) sendNoMetrics(t NebulaMessageType, st NebulaMessageSubType, ci *ConnectionState, hostinfo *HostInfo, remote *udpAddr, p, nb, out []byte) { +func (f *Interface) sendNoMetrics(t NebulaMessageType, st NebulaMessageSubType, ci *ConnectionState, hostinfo *HostInfo, remote *udpAddr, p, nb, out []byte) uint64 { if ci.eKey == nil { //TODO: log warning - return + return 0 } var err error @@ -237,7 +237,7 @@ func (f *Interface) sendNoMetrics(t NebulaMessageType, st NebulaMessageSubType, WithField("udpAddr", remote).WithField("counter", c). WithField("attemptedCounter", ci.messageCounter). Error("Failed to encrypt outgoing packet") - return + return c } err = f.outside.WriteTo(out, remote) @@ -245,6 +245,7 @@ func (f *Interface) sendNoMetrics(t NebulaMessageType, st NebulaMessageSubType, hostinfo.logger().WithError(err). WithField("udpAddr", remote).Error("Failed to write outgoing packet") } + return c } func isMulticast(ip uint32) bool {