Fix race in packetInNonceQueueIsAwaitingKey

This commit is contained in:
Jason A. Donenfeld 2018-05-20 03:24:14 +02:00
parent fa003b6933
commit e04f9543c0
2 changed files with 7 additions and 7 deletions

View file

@ -54,7 +54,7 @@ type Peer struct {
nonce chan *QueueOutboundElement // nonce / pre-handshake queue nonce chan *QueueOutboundElement // nonce / pre-handshake queue
outbound chan *QueueOutboundElement // sequential ordering of work outbound chan *QueueOutboundElement // sequential ordering of work
inbound chan *QueueInboundElement // sequential ordering of work inbound chan *QueueInboundElement // sequential ordering of work
packetInNonceQueueIsAwaitingKey bool packetInNonceQueueIsAwaitingKey AtomicBool
} }
routines struct { routines struct {

12
send.go
View file

@ -108,7 +108,7 @@ func addToEncryptionQueue(
/* Queues a keepalive if no packets are queued for peer /* Queues a keepalive if no packets are queued for peer
*/ */
func (peer *Peer) SendKeepalive() bool { 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 return false
} }
elem := peer.device.NewOutboundElement() elem := peer.device.NewOutboundElement()
@ -304,7 +304,7 @@ func (device *Device) RoutineReadFromTUN() {
// insert into nonce/pre-handshake queue // insert into nonce/pre-handshake queue
if peer.isRunning.Get() { if peer.isRunning.Get() {
if peer.queue.packetInNonceQueueIsAwaitingKey { if peer.queue.packetInNonceQueueIsAwaitingKey.Get() {
peer.SendHandshakeInitiation(false) peer.SendHandshakeInitiation(false)
} }
addToOutboundQueue(peer.queue.nonce, elem) addToOutboundQueue(peer.queue.nonce, elem)
@ -334,7 +334,7 @@ func (peer *Peer) RoutineNonce() {
defer func() { defer func() {
logDebug.Println(peer, ": Routine: nonce worker - stopped") logDebug.Println(peer, ": Routine: nonce worker - stopped")
peer.queue.packetInNonceQueueIsAwaitingKey = false peer.queue.packetInNonceQueueIsAwaitingKey.Set(false)
peer.routines.stopping.Done() peer.routines.stopping.Done()
}() }()
@ -353,7 +353,7 @@ func (peer *Peer) RoutineNonce() {
for { for {
NextPacket: NextPacket:
peer.queue.packetInNonceQueueIsAwaitingKey = false peer.queue.packetInNonceQueueIsAwaitingKey.Set(false)
select { select {
case <-peer.routines.stop: case <-peer.routines.stop:
@ -381,7 +381,7 @@ func (peer *Peer) RoutineNonce() {
break break
} }
} }
peer.queue.packetInNonceQueueIsAwaitingKey = true peer.queue.packetInNonceQueueIsAwaitingKey.Set(true)
// no suitable key pair, request for new handshake // no suitable key pair, request for new handshake
@ -408,7 +408,7 @@ func (peer *Peer) RoutineNonce() {
return return
} }
} }
peer.queue.packetInNonceQueueIsAwaitingKey = false peer.queue.packetInNonceQueueIsAwaitingKey.Set(false)
// populate work element // populate work element