Skip to content

Commit

Permalink
Prevent panic when write packet with bad address type
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jun 21, 2023
1 parent 323b956 commit d51abeb
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions gvisor_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/sagernet/gvisor/pkg/tcpip/header"
"github.com/sagernet/gvisor/pkg/tcpip/stack"
"github.com/sagernet/sing/common/buf"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/udpnat"
Expand Down Expand Up @@ -62,6 +63,12 @@ type UDPBackWriter struct {
}

func (w *UDPBackWriter) WritePacket(packetBuffer *buf.Buffer, destination M.Socksaddr) error {
if destination.IsIPv4() && w.sourceNetwork == header.IPv6ProtocolNumber {
destination = M.SocksaddrFrom(netip.AddrFrom16(destination.Addr.As16()), destination.Port)
} else if destination.IsIPv6() && (w.sourceNetwork == header.IPv4AddressSizeBits) {
return E.New("send IPv6 packet to IPv4 connection")
}

defer packetBuffer.Release()

route, err := w.stack.FindRoute(
Expand Down

0 comments on commit d51abeb

Please sign in to comment.