mirror of
https://github.com/xtaci/kcp-go.git
synced 2026-10-19 16:14:38 +00:00
fix: initialize source address in readLoop from remote addr
Previously, the `readLoop` would lazily initialize the source address (`src`) based on the first incoming packet. This behavior meant that for a short window during initialization, the session might accept a packet from an arbitrary address, potentially allowing connection hijacking or address spoofing. This change ensures that if `s.remote` is already known (e.g., in `Dial` mode), `src` is initialized immediately before entering the loop. This locks the session to the expected remote address from thefix: initialize source address in readLoop from remote addr Previously, the `readLoop` would lazily initialize the source address (`src`) based on the first incoming packet. This behavior meant that for a short window during initialization, the session might accept a packet from an arbitrary address, potentially allowing connection hijacking or address spoofing. This change ensures that if `s.remote` is already known (e.g., in `Dial` mode), `src` is initialized immediately before entering the loop. This locks the session to the expected remote address from the start.
This commit is contained in:
+5
-7
@@ -33,6 +33,9 @@ func (s *UDPSession) defaultReadLoop() {
|
||||
buf := make([]byte, mtuLimit)
|
||||
|
||||
var src string
|
||||
if s.remote != nil {
|
||||
src = s.remote.String()
|
||||
}
|
||||
for {
|
||||
n, addr, err := s.conn.ReadFrom(buf)
|
||||
if err != nil {
|
||||
@@ -45,14 +48,9 @@ func (s *UDPSession) defaultReadLoop() {
|
||||
}
|
||||
|
||||
// make sure the packet is from the same source
|
||||
switch src {
|
||||
case "":
|
||||
// set source address if not set
|
||||
if src == "" { // set source address if nil
|
||||
src = addr.String()
|
||||
case addr.String():
|
||||
// source valid
|
||||
default:
|
||||
// source invalid
|
||||
} else if addr.String() != src {
|
||||
atomic.AddUint64(&DefaultSnmp.InErrs, 1)
|
||||
continue
|
||||
}
|
||||
|
||||
+5
-7
@@ -46,6 +46,9 @@ func (s *UDPSession) readLoop() {
|
||||
|
||||
// x/net version
|
||||
var src string
|
||||
if s.remote != nil {
|
||||
src = s.remote.String()
|
||||
}
|
||||
msgs := make([]ipv4.Message, batchSize)
|
||||
for k := range msgs {
|
||||
msgs[k].Buffers = [][]byte{make([]byte, mtuLimit)}
|
||||
@@ -66,14 +69,9 @@ func (s *UDPSession) readLoop() {
|
||||
msg := &msgs[i]
|
||||
|
||||
// make sure the packet is from the same source
|
||||
switch src {
|
||||
case "":
|
||||
// set source address if not set
|
||||
if src == "" { // set source address if nil
|
||||
src = msg.Addr.String()
|
||||
case msg.Addr.String():
|
||||
// source valid
|
||||
default:
|
||||
// source invalid
|
||||
} else if msg.Addr.String() != src {
|
||||
atomic.AddUint64(&DefaultSnmp.InErrs, 1)
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user