Development discussion of WireGuard
 help / color / mirror / Atom feed
* [PATCH] device: add write queue mutex for peer
@ 2020-11-18 12:53 liuhaichao
  2020-11-18 13:23 ` Jason A. Donenfeld
  0 siblings, 1 reply; 2+ messages in thread
From: liuhaichao @ 2020-11-18 12:53 UTC (permalink / raw)
  To: wireguard; +Cc: Haichao Liu

From: Haichao Liu <liuhaichao@bytedance.com>

fix panic: send on closed channel when remove peer

Change-Id: Ica0e2447cd941bd3a26948aacafc837904ea18b2
Signed-off-by: Haichao Liu <liuhaichao@bytedance.com>
---
 device/peer.go    | 6 +++++-
 device/receive.go | 2 ++
 device/send.go    | 4 ++++
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/device/peer.go b/device/peer.go
index ef6c010..78204bb 100644
--- a/device/peer.go
+++ b/device/peer.go
@@ -58,6 +58,7 @@ type Peer struct {
 	}
 
 	queue struct {
+		sync.RWMutex
 		nonce                           chan *QueueOutboundElement // nonce / pre-handshake queue
 		outbound                        chan *QueueOutboundElement // sequential ordering of work
 		inbound                         chan *QueueInboundElement  // sequential ordering of work
@@ -195,10 +196,11 @@ func (peer *Peer) Start() {
 	peer.routines.stopping.Add(PeerRoutineNumber)
 
 	// prepare queues
-
+	peer.queue.Lock()
 	peer.queue.nonce = make(chan *QueueOutboundElement, QueueOutboundSize)
 	peer.queue.outbound = make(chan *QueueOutboundElement, QueueOutboundSize)
 	peer.queue.inbound = make(chan *QueueInboundElement, QueueInboundSize)
+	peer.queue.Unlock()
 
 	peer.timersInit()
 	peer.handshake.lastSentHandshake = time.Now().Add(-(RekeyTimeout + time.Second))
@@ -284,9 +286,11 @@ func (peer *Peer) Stop() {
 
 	// close queues
 
+	peer.queue.Lock()
 	close(peer.queue.nonce)
 	close(peer.queue.outbound)
 	close(peer.queue.inbound)
+	peer.queue.Unlock()
 
 	peer.ZeroAndFlushAll()
 }
diff --git a/device/receive.go b/device/receive.go
index b53c9c0..e4a94b5 100644
--- a/device/receive.go
+++ b/device/receive.go
@@ -184,11 +184,13 @@ func (device *Device) RoutineReceiveIncoming(IP int, bind conn.Bind) {
 
 			// add to decryption queues
 
+			peer.queue.RLock()
 			if peer.isRunning.Get() {
 				if device.addToInboundAndDecryptionQueues(peer.queue.inbound, device.queue.decryption, elem) {
 					buffer = device.GetMessageBuffer()
 				}
 			}
+			peer.queue.RUnlock()
 
 			continue
 
diff --git a/device/send.go b/device/send.go
index c0bdba3..d202b62 100644
--- a/device/send.go
+++ b/device/send.go
@@ -107,6 +107,8 @@ func addToOutboundAndEncryptionQueues(outboundQueue chan *QueueOutboundElement,
 /* Queues a keepalive if no packets are queued for peer
  */
 func (peer *Peer) SendKeepalive() bool {
+	peer.queue.RLock()
+	defer peer.queue.RUnlock()
 	if len(peer.queue.nonce) != 0 || peer.queue.packetInNonceQueueIsAwaitingKey.Get() || !peer.isRunning.Get() {
 		return false
 	}
@@ -310,6 +312,7 @@ func (device *Device) RoutineReadFromTUN() {
 
 		// insert into nonce/pre-handshake queue
 
+		peer.queue.RLock()
 		if peer.isRunning.Get() {
 			if peer.queue.packetInNonceQueueIsAwaitingKey.Get() {
 				peer.SendHandshakeInitiation(false)
@@ -317,6 +320,7 @@ func (device *Device) RoutineReadFromTUN() {
 			addToNonceQueue(peer.queue.nonce, elem, device)
 			elem = nil
 		}
+		peer.queue.RUnlock()
 	}
 }
 
-- 
2.19.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] device: add write queue mutex for peer
  2020-11-18 12:53 [PATCH] device: add write queue mutex for peer liuhaichao
@ 2020-11-18 13:23 ` Jason A. Donenfeld
  0 siblings, 0 replies; 2+ messages in thread
From: Jason A. Donenfeld @ 2020-11-18 13:23 UTC (permalink / raw)
  To: liuhaichao; +Cc: WireGuard mailing list

Thanks! Applied.

https://git.zx2c4.com/wireguard-go/commit/?id=913f68ce3820801e045632cb05c6f11e0df50b47

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-11-18 13:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-18 12:53 [PATCH] device: add write queue mutex for peer liuhaichao
2020-11-18 13:23 ` Jason A. Donenfeld

Development discussion of WireGuard

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/wireguard

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 wireguard wireguard/ http://inbox.vuxu.org/wireguard \
		wireguard@lists.zx2c4.com
	public-inbox-index wireguard

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.wireguard


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git