fix: slices mis-reused in socks5UDPConn.ReadFrom

fix: #561
This commit is contained in:
Haruue Icymoon 2023-02-07 23:47:11 +08:00
parent 810bfd7022
commit 30e17a40c0
No known key found for this signature in database
GPG key ID: F6083B28CBCBC148

View file

@ -216,6 +216,11 @@ func (c *socks5UDPConn) Close() error {
}
func socks5AddrToUDPAddr(atyp byte, addr []byte, port []byte) (*net.UDPAddr, error) {
clone := func(b []byte) []byte {
c := make([]byte, len(b))
copy(c, b)
return c
}
iPort := int(binary.BigEndian.Uint16(port))
switch atyp {
case socks5.ATYPIPv4:
@ -223,7 +228,7 @@ func socks5AddrToUDPAddr(atyp byte, addr []byte, port []byte) (*net.UDPAddr, err
return nil, errors.New("invalid ipv4 address")
}
return &net.UDPAddr{
IP: net.IPv4(addr[0], addr[1], addr[2], addr[3]),
IP: clone(addr),
Port: iPort,
}, nil
case socks5.ATYPIPv6:
@ -231,7 +236,7 @@ func socks5AddrToUDPAddr(atyp byte, addr []byte, port []byte) (*net.UDPAddr, err
return nil, errors.New("invalid ipv6 address")
}
return &net.UDPAddr{
IP: addr,
IP: clone(addr),
Port: iPort,
}, nil
case socks5.ATYPDomain: