]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
Fix race in packetInNonceQueueIsAwaitingKey
authorJason A. Donenfeld <Jason@zx2c4.com>
Sun, 20 May 2018 01:24:14 +0000 (03:24 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Sun, 20 May 2018 01:24:14 +0000 (03:24 +0200)
peer.go
send.go

diff --git a/peer.go b/peer.go
index e78f6c487ff2f9657ac1f089c60d6a57d69984e3..4d3ac2b68f364106fbd1ad86424f3fe8c0ecae27 100644 (file)
--- a/peer.go
+++ b/peer.go
@@ -54,7 +54,7 @@ type Peer struct {
                nonce                           chan *QueueOutboundElement // nonce / pre-handshake queue
                outbound                        chan *QueueOutboundElement // sequential ordering of work
                inbound                         chan *QueueInboundElement  // sequential ordering of work
-               packetInNonceQueueIsAwaitingKey bool
+               packetInNonceQueueIsAwaitingKey AtomicBool
        }
 
        routines struct {
diff --git a/send.go b/send.go
index 51ee7d3cf15e62ae73118832d72443e1a7a3addc..d57e11b4ea3388f80093a40741cad85c08cccc70 100644 (file)
--- a/send.go
+++ b/send.go
@@ -108,7 +108,7 @@ func addToEncryptionQueue(
 /* Queues a keepalive if no packets are queued for peer
  */
 func (peer *Peer) SendKeepalive() bool {
-       if len(peer.queue.nonce) != 0 || peer.queue.packetInNonceQueueIsAwaitingKey || !peer.isRunning.Get() {
+       if len(peer.queue.nonce) != 0 || peer.queue.packetInNonceQueueIsAwaitingKey.Get() || !peer.isRunning.Get() {
                return false
        }
        elem := peer.device.NewOutboundElement()
@@ -304,7 +304,7 @@ func (device *Device) RoutineReadFromTUN() {
                // insert into nonce/pre-handshake queue
 
                if peer.isRunning.Get() {
-                       if peer.queue.packetInNonceQueueIsAwaitingKey {
+                       if peer.queue.packetInNonceQueueIsAwaitingKey.Get() {
                                peer.SendHandshakeInitiation(false)
                        }
                        addToOutboundQueue(peer.queue.nonce, elem)
@@ -334,7 +334,7 @@ func (peer *Peer) RoutineNonce() {
 
        defer func() {
                logDebug.Println(peer, ": Routine: nonce worker - stopped")
-               peer.queue.packetInNonceQueueIsAwaitingKey = false
+               peer.queue.packetInNonceQueueIsAwaitingKey.Set(false)
                peer.routines.stopping.Done()
        }()
 
@@ -353,7 +353,7 @@ func (peer *Peer) RoutineNonce() {
 
        for {
        NextPacket:
-               peer.queue.packetInNonceQueueIsAwaitingKey = false
+               peer.queue.packetInNonceQueueIsAwaitingKey.Set(false)
 
                select {
                case <-peer.routines.stop:
@@ -381,7 +381,7 @@ func (peer *Peer) RoutineNonce() {
                                                break
                                        }
                                }
-                               peer.queue.packetInNonceQueueIsAwaitingKey = true
+                               peer.queue.packetInNonceQueueIsAwaitingKey.Set(true)
 
                                // no suitable key pair, request for new handshake
 
@@ -408,7 +408,7 @@ func (peer *Peer) RoutineNonce() {
                                        return
                                }
                        }
-                       peer.queue.packetInNonceQueueIsAwaitingKey = false
+                       peer.queue.packetInNonceQueueIsAwaitingKey.Set(false)
 
                        // populate work element