]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
device: clear pointers when returning elems to pools
authorJosh Bleecher Snyder <josh@tailscale.com>
Fri, 4 Dec 2020 23:36:21 +0000 (15:36 -0800)
committerJosh Bleecher Snyder <josh@tailscale.com>
Tue, 8 Dec 2020 22:25:02 +0000 (14:25 -0800)
Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
device/pools.go
device/receive.go
device/send.go

index e778d2e62143720b904366faa68ea705715718c6..6939eeb10618d1ff61ce015bca72c1f6cfa0d4e7 100644 (file)
@@ -65,6 +65,7 @@ func (device *Device) GetInboundElement() *QueueInboundElement {
 }
 
 func (device *Device) PutInboundElement(msg *QueueInboundElement) {
+       msg.clearPointers()
        if PreallocatedBuffersPerPool == 0 {
                device.pool.inboundElementPool.Put(msg)
        } else {
@@ -81,6 +82,7 @@ func (device *Device) GetOutboundElement() *QueueOutboundElement {
 }
 
 func (device *Device) PutOutboundElement(msg *QueueOutboundElement) {
+       msg.clearPointers()
        if PreallocatedBuffersPerPool == 0 {
                device.pool.outboundElementPool.Put(msg)
        } else {
index e4a94b51a17b9eebe5b589b9eb095e4ca87fc752..0a8228cf7f481e915961b95b280f0a75a6d3f4d0 100644 (file)
@@ -37,6 +37,17 @@ type QueueInboundElement struct {
        endpoint conn.Endpoint
 }
 
+// clearPointers clears elem fields that contain pointers.
+// This makes the garbage collector's life easier and
+// avoids accidentally keeping other objects around unnecessarily.
+// It also reduces the possible collateral damage from use-after-free bugs.
+func (elem *QueueInboundElement) clearPointers() {
+       elem.buffer = nil
+       elem.packet = nil
+       elem.keypair = nil
+       elem.endpoint = nil
+}
+
 func (elem *QueueInboundElement) Drop() {
        atomic.StoreInt32(&elem.dropped, AtomicTrue)
 }
index fa4da0e98e262cd58f50ff9b154d43f090f918e7..cb3e3f623ce3c5c8139a2918fcd2d7f7dcbfefd2 100644 (file)
@@ -58,9 +58,19 @@ func (device *Device) NewOutboundElement() *QueueOutboundElement {
        elem.buffer = device.GetMessageBuffer()
        elem.Mutex = sync.Mutex{}
        elem.nonce = 0
+       // keypair and peer were cleared (if necessary) by clearPointers.
+       return elem
+}
+
+// clearPointers clears elem fields that contain pointers.
+// This makes the garbage collector's life easier and
+// avoids accidentally keeping other objects around unnecessarily.
+// It also reduces the possible collateral damage from use-after-free bugs.
+func (elem *QueueOutboundElement) clearPointers() {
+       elem.buffer = nil
+       elem.packet = nil
        elem.keypair = nil
        elem.peer = nil
-       return elem
 }
 
 func (elem *QueueOutboundElement) Drop() {