mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-12-01 06:08:37 +00:00
SS2022 inbound: Fix data races and duplicate acceptance when decoding one UDP session concurrently
Packets of one client session can be processed by two inbound workers at the same time (e.g. the client's old and new source address after a NAT rebinding). The sliding window was checked and updated in two separate critical sections, and clientBodyCipher and the session's User were read without the lock, so the same packet ID could be accepted more than once and go test -race reported data races. Re-check the window and publish the cached body cipher in one critical section after the packet is fully validated, and always initialize the session's User under the lock.
This commit is contained in:
@@ -106,13 +106,37 @@ func (s *ServerUDPSession) CheckPacketID(packetID uint64) bool {
|
||||
return s.Window.Check(packetID)
|
||||
}
|
||||
|
||||
func (s *ServerUDPSession) AddPacketID(packetID uint64) {
|
||||
// CommitPacketID records a packet ID after the packet has been fully validated.
|
||||
// It re-checks the window under the lock, so when the same packet is validated
|
||||
// concurrently (e.g. by the workers of two client source addresses), only one is accepted.
|
||||
func (s *ServerUDPSession) CommitPacketID(packetID uint64) bool {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
if s.Window == nil {
|
||||
s.Window = new(SlidingWindow)
|
||||
}
|
||||
s.Window.Add(packetID)
|
||||
return s.Window.CheckAndAdd(packetID)
|
||||
}
|
||||
|
||||
func (s *ServerUDPSession) getClientBodyCipher() cipher.AEAD {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
return s.clientBodyCipher
|
||||
}
|
||||
|
||||
func (s *ServerUDPSession) commitClientPacket(packetID uint64, bodyAead cipher.AEAD) bool {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
if s.Window == nil {
|
||||
s.Window = new(SlidingWindow)
|
||||
}
|
||||
if !s.Window.CheckAndAdd(packetID) {
|
||||
return false
|
||||
}
|
||||
if s.clientBodyCipher == nil {
|
||||
s.clientBodyCipher = bodyAead
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type UDPSessionManager struct {
|
||||
|
||||
Reference in New Issue
Block a user