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
outbound chan *QueueOutboundElement // sequential ordering of work
inbound chan *QueueInboundElement // sequential ordering of work
packetInNonceQueueIsAwaitingKey bool
packetInNonceQueueIsAwaitingKey AtomicBool
}
routines struct {

12
send.go
View file

@ -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