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:
xtaci
2025-12-19 13:08:29 +08:00
parent 6c9035eb62
commit 508806662a
2 changed files with 10 additions and 14 deletions
+5 -7
View File
@@ -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
}