Fix dispatcher race operation in WintunEndpoint

This commit is contained in:
wwqgtxx 2026-05-20 23:30:00 +08:00 committed by 世界
parent 1e975834ca
commit 23eaeb3198
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -3,6 +3,8 @@
package tun package tun
import ( import (
"sync"
"github.com/sagernet/gvisor/pkg/buffer" "github.com/sagernet/gvisor/pkg/buffer"
"github.com/sagernet/gvisor/pkg/tcpip" "github.com/sagernet/gvisor/pkg/tcpip"
"github.com/sagernet/gvisor/pkg/tcpip/header" "github.com/sagernet/gvisor/pkg/tcpip/header"
@ -23,6 +25,7 @@ var _ stack.LinkEndpoint = (*WintunEndpoint)(nil)
type WintunEndpoint struct { type WintunEndpoint struct {
tun *NativeTun tun *NativeTun
access sync.RWMutex
dispatcher stack.NetworkDispatcher dispatcher stack.NetworkDispatcher
} }
@ -49,6 +52,8 @@ func (e *WintunEndpoint) Capabilities() stack.LinkEndpointCapabilities {
} }
func (e *WintunEndpoint) Attach(dispatcher stack.NetworkDispatcher) { func (e *WintunEndpoint) Attach(dispatcher stack.NetworkDispatcher) {
e.access.Lock()
defer e.access.Unlock()
if dispatcher == nil && e.dispatcher != nil { if dispatcher == nil && e.dispatcher != nil {
e.dispatcher = nil e.dispatcher = nil
return return
@ -88,7 +93,9 @@ func (e *WintunEndpoint) dispatchLoop() {
Payload: packetBuffer, Payload: packetBuffer,
IsForwardedPacket: true, IsForwardedPacket: true,
}) })
e.access.RLock()
dispatcher := e.dispatcher dispatcher := e.dispatcher
e.access.RUnlock()
if dispatcher == nil { if dispatcher == nil {
pkt.DecRef() pkt.DecRef()
return return
@ -99,6 +106,8 @@ func (e *WintunEndpoint) dispatchLoop() {
} }
func (e *WintunEndpoint) IsAttached() bool { func (e *WintunEndpoint) IsAttached() bool {
e.access.RLock()
defer e.access.RUnlock()
return e.dispatcher != nil return e.dispatcher != nil
} }