1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-17 23:06:30 -05:00

Check lenth of the udp packet before parsing

This commit is contained in:
V2Ray 2015-11-03 18:20:28 +01:00
parent d58384ced0
commit d9ebd008d3
2 changed files with 24 additions and 6 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/transport"
) )
var ( var (
@ -37,7 +38,12 @@ func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) {
buffer.Append(request.Data.Value) buffer.Append(request.Data.Value)
} }
func ReadUDPRequest(packet []byte) (request Socks5UDPRequest, err error) { func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
if len(packet) < 5 {
return nil, transport.CorruptedPacket
}
request := new(Socks5UDPRequest)
// packet[0] and packet[1] are reserved // packet[0] and packet[1] are reserved
request.Fragment = packet[2] request.Fragment = packet[2]
@ -46,28 +52,38 @@ func ReadUDPRequest(packet []byte) (request Socks5UDPRequest, err error) {
switch addrType { switch addrType {
case AddrTypeIPv4: case AddrTypeIPv4:
if len(packet) < 10 {
return nil, transport.CorruptedPacket
}
ip := packet[4:8] ip := packet[4:8]
port := binary.BigEndian.Uint16(packet[8:10]) port := binary.BigEndian.Uint16(packet[8:10])
request.Address = v2net.IPAddress(ip, port) request.Address = v2net.IPAddress(ip, port)
dataBegin = 10 dataBegin = 10
case AddrTypeIPv6: case AddrTypeIPv6:
if len(packet) < 22 {
return nil, transport.CorruptedPacket
}
ip := packet[4:20] ip := packet[4:20]
port := binary.BigEndian.Uint16(packet[20:22]) port := binary.BigEndian.Uint16(packet[20:22])
request.Address = v2net.IPAddress(ip, port) request.Address = v2net.IPAddress(ip, port)
dataBegin = 22 dataBegin = 22
case AddrTypeDomain: case AddrTypeDomain:
domainLength := int(packet[4]) domainLength := int(packet[4])
if len(packet) < 5+domainLength+2 {
return nil, transport.CorruptedPacket
}
domain := string(packet[5 : 5+domainLength]) domain := string(packet[5 : 5+domainLength])
port := binary.BigEndian.Uint16(packet[5+domainLength : 5+domainLength+2]) port := binary.BigEndian.Uint16(packet[5+domainLength : 5+domainLength+2])
request.Address = v2net.DomainAddress(domain, port) request.Address = v2net.DomainAddress(domain, port)
dataBegin = 5 + domainLength + 2 dataBegin = 5 + domainLength + 2
default: default:
log.Warning("Unknown address type %d", addrType) log.Warning("Unknown address type %d", addrType)
err = ErrorUnknownAddressType return nil, ErrorUnknownAddressType
return
} }
request.Data = alloc.NewBuffer().Clear().Append(packet[dataBegin:]) if len(packet) > dataBegin {
request.Data = alloc.NewBuffer().Clear().Append(packet[dataBegin:])
}
return return request, nil
} }

View File

@ -46,7 +46,9 @@ func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error {
buffer.Release() buffer.Release()
if err != nil { if err != nil {
log.Error("Socks failed to parse UDP request: %v", err) log.Error("Socks failed to parse UDP request: %v", err)
request.Data.Release() continue
}
if request.Data == nil || request.Data.Len() == 0 {
continue continue
} }
if request.Fragment != 0 { if request.Fragment != 0 {