From 229b30d877d88545fc25bc333bd7de14af67ba02 Mon Sep 17 00:00:00 2001 From: "Xiaokang Wang (Shelikhoo)" Date: Sat, 12 Oct 2024 19:25:59 +0100 Subject: [PATCH] drop unsupported domain address type in packet addr (#3186) --- common/net/packetaddr/connection_adaptor.go | 5 +++++ common/net/packetaddr/packetaddr.go | 4 ++++ transport/internet/udp/dispatcher_packetaddr.go | 6 ++++++ 3 files changed, 15 insertions(+) diff --git a/common/net/packetaddr/connection_adaptor.go b/common/net/packetaddr/connection_adaptor.go index 4364a6d33ca..6cb0a3908c2 100644 --- a/common/net/packetaddr/connection_adaptor.go +++ b/common/net/packetaddr/connection_adaptor.go @@ -79,6 +79,11 @@ func (c *packetConnectionAdaptor) ReadFrom(p []byte) (n int, addr gonet.Addr, er } func (c *packetConnectionAdaptor) WriteTo(p []byte, addr gonet.Addr) (n int, err error) { + _, ok := addr.(*gonet.UDPAddr) + if !ok { + // address other than UDPAddr is not supported, and will be dropped. + return 0, nil + } payloadLen := len(p) var buffer *buf.Buffer buffer, err = AttachAddressToPacket(buf.FromBytes(p), addr) diff --git a/common/net/packetaddr/packetaddr.go b/common/net/packetaddr/packetaddr.go index 58e58675c59..dbe83fc7718 100644 --- a/common/net/packetaddr/packetaddr.go +++ b/common/net/packetaddr/packetaddr.go @@ -2,6 +2,7 @@ package packetaddr import ( "bytes" + "github.com/v2fly/v2ray-core/v5/common/errors" gonet "net" "github.com/v2fly/v2ray-core/v5/common/buf" @@ -45,6 +46,9 @@ func ExtractAddressFromPacket(data *buf.Buffer) (*buf.Buffer, gonet.Addr, error) if err != nil { return nil, nil, err } + if address.Family().IsDomain() { + return nil, nil, errors.New("invalid address type") + } addr := &gonet.UDPAddr{ IP: address.IP(), Port: int(port.Value()), diff --git a/transport/internet/udp/dispatcher_packetaddr.go b/transport/internet/udp/dispatcher_packetaddr.go index 938447407b4..45583566a90 100644 --- a/transport/internet/udp/dispatcher_packetaddr.go +++ b/transport/internet/udp/dispatcher_packetaddr.go @@ -27,6 +27,12 @@ func (p PacketAddrDispatcher) Dispatch(ctx context.Context, destination net.Dest if destination.Network != net.Network_UDP { return } + + // Processing of domain address is unsupported as it adds unpredictable overhead, it will be dropped. + if destination.Address.Family().IsDomain() { + return + } + p.conn.WriteTo(payload.Bytes(), &net.UDPAddr{IP: destination.Address.IP(), Port: int(destination.Port.Value())}) }