fix: tun dns hijack sending zero-filled or stale packets when PackBuffer reallocates (#3037)

D.Msg.PackBuffer sizes its scratch space by the uncompressed message length
and silently allocates a new slice when that exceeds the given buffer, even
though the compressed result would fit. The udp hijack path in
listener/sing_tun/dns.go always sends the original buffer, producing a
correct-length datagram full of zeros or stale bytes whenever a reply's
uncompressed size exceeds SafeDnsPacketSize (e.g. 100+ A records for one
name). Copy the packed result back into the caller's buffer when it fits.
The tcp path in RelayDnsConn already guards against this; the udp path did
not.
This commit is contained in:
TWO666 authored and GitHub committed 2026-07-27 13:03:03 +08:00
1 parent 95f7a0c512
commit fb002210ff
1 file changed
+11 -1
+11 -1
View File
@@ -94,7 +94,17 @@ func relayDnsPacket(ctx context.Context, payload []byte, target []byte, maxSize
r.Truncate(maxSize)
}
r.Compress = true
return r.PackBuffer(target)
data, err := r.PackBuffer(target)
if err != nil {
return nil, err
}
// PackBuffer sizes its scratch space by the *uncompressed* message length and may
// have allocated a new slice even though the compressed result fits into target.
// Callers (e.g. the tun dns hijack) assume the result is backed by target, so copy it back.
if len(data) > 0 && len(data) <= len(target) && &data[0] != &target[0] {
data = target[:copy(target, data)]
}
return data, nil
}
// RelayDnsPacket will truncate udp message up to SafeDnsPacketSize