Skip to content

Commit d51abeb

Browse files
committed
Prevent panic when write packet with bad address type
1 parent 323b956 commit d51abeb

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

gvisor_udp.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/sagernet/gvisor/pkg/tcpip/header"
1414
"github.com/sagernet/gvisor/pkg/tcpip/stack"
1515
"github.com/sagernet/sing/common/buf"
16+
E "github.com/sagernet/sing/common/exceptions"
1617
M "github.com/sagernet/sing/common/metadata"
1718
N "github.com/sagernet/sing/common/network"
1819
"github.com/sagernet/sing/common/udpnat"
@@ -62,6 +63,12 @@ type UDPBackWriter struct {
6263
}
6364

6465
func (w *UDPBackWriter) WritePacket(packetBuffer *buf.Buffer, destination M.Socksaddr) error {
66+
if destination.IsIPv4() && w.sourceNetwork == header.IPv6ProtocolNumber {
67+
destination = M.SocksaddrFrom(netip.AddrFrom16(destination.Addr.As16()), destination.Port)
68+
} else if destination.IsIPv6() && (w.sourceNetwork == header.IPv4AddressSizeBits) {
69+
return E.New("send IPv6 packet to IPv4 connection")
70+
}
71+
6572
defer packetBuffer.Release()
6673

6774
route, err := w.stack.FindRoute(

0 commit comments

Comments
 (0)