fix: truncated dns resp (#203)

This commit is contained in:
mzz 2023-07-11 01:25:05 +08:00 committed by GitHub
parent 11d2ea945f
commit 1cc9edc0c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 18 deletions

5
common/consts/net.go Normal file
View File

@ -0,0 +1,5 @@
package consts
const (
EthernetMtu = 1500
)

View File

@ -15,6 +15,7 @@ import (
"sync"
"time"
"github.com/daeuniverse/dae/common/consts"
dnsmessage "github.com/miekg/dns"
"github.com/mzz2017/softwind/netproxy"
"github.com/mzz2017/softwind/pkg/fastrand"
@ -240,8 +241,8 @@ func resolve(ctx context.Context, d netproxy.Dialer, dns netip.AddrPort, host st
}()
}
go func() {
buf := pool.Get(512)
defer pool.Put(buf)
buf := pool.GetFullCap(consts.EthernetMtu)
defer buf.Put()
if magicNetwork.Network == "tcp" {
// Read DNS response length
_, err := io.ReadFull(c, buf[:2])
@ -250,7 +251,7 @@ func resolve(ctx context.Context, d netproxy.Dialer, dns netip.AddrPort, host st
return
}
n := binary.BigEndian.Uint16(buf)
if n > 512 {
if int(n) > cap(buf) {
ch <- fmt.Errorf("too big dns resp")
return
}

View File

@ -705,15 +705,16 @@ func (c *ControlPlane) Serve(readyChan chan<- bool, listener *Listener) (err err
}
}()
go func() {
buf := pool.GetFullCap(consts.EthernetMtu)
var oob [120]byte // Size for original dest
defer buf.Put()
for {
select {
case <-c.ctx.Done():
return
default:
}
var buf [EthernetMtu]byte
var oob [120]byte // Size for original dest
n, oobn, _, src, err := udpConn.ReadMsgUDPAddrPort(buf[:], oob[:])
n, oobn, _, src, err := udpConn.ReadMsgUDPAddrPort(buf, oob[:])
if err != nil {
if !strings.Contains(err.Error(), "use of closed network connection") {
c.log.Errorf("ReadFromUDPAddrPort: %v, %v", src.String(), err)
@ -722,8 +723,8 @@ func (c *ControlPlane) Serve(readyChan chan<- bool, listener *Listener) (err err
}
newBuf := pool.Get(n)
copy(newBuf, buf[:n])
go func(data []byte, src netip.AddrPort) {
defer pool.Put(data)
go func(data pool.PB, src netip.AddrPort) {
defer data.Put()
var realDst netip.AddrPort
var routingResult *bpfRoutingResult
pktDst := RetrieveOriginalDest(oob[:oobn])

View File

@ -639,7 +639,7 @@ func (c *DnsController) dialSend(invokingDepth int, req *udpRequest, data []byte
}()
// We can block here because we are in a coroutine.
respBuf := pool.Get(512)
respBuf := pool.GetFullCap(consts.EthernetMtu)
defer pool.Put(respBuf)
for {
// Wait for response.

View File

@ -11,15 +11,12 @@ import (
"sync"
"time"
"github.com/daeuniverse/dae/common/consts"
"github.com/daeuniverse/dae/component/outbound/dialer"
"github.com/mzz2017/softwind/netproxy"
"github.com/mzz2017/softwind/pool"
)
const (
EthernetMtu = 1500
)
type UdpHandler func(data []byte, from netip.AddrPort) error
type UdpEndpoint struct {
@ -34,8 +31,7 @@ type UdpEndpoint struct {
}
func (ue *UdpEndpoint) start() {
buf := pool.Get(EthernetMtu)
buf = buf[:cap(buf)]
buf := pool.GetFullCap(consts.EthernetMtu)
defer pool.Put(buf)
for {
n, from, err := ue.conn.ReadFrom(buf[:])

2
go.mod
View File

@ -12,7 +12,7 @@ require (
github.com/json-iterator/go v1.1.12
github.com/miekg/dns v1.1.55
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/mzz2017/softwind v0.0.0-20230708102709-26ff44839573
github.com/mzz2017/softwind v0.0.0-20230710142544-73a557cea4a4
github.com/okzk/sdnotify v0.0.0-20180710141335-d9becc38acbd
github.com/safchain/ethtool v0.3.0
github.com/sirupsen/logrus v1.9.3

4
go.sum
View File

@ -91,8 +91,8 @@ github.com/mzz2017/disk-bloom v1.0.1 h1:rEF9MiXd9qMW3ibRpqcerLXULoTgRlM21yqqJl1B
github.com/mzz2017/disk-bloom v1.0.1/go.mod h1:JLHETtUu44Z6iBmsqzkOtFlRvXSlKnxjwiBRDapizDI=
github.com/mzz2017/quic-go v0.0.0-20230706143320-cc858d4932b7 h1:9zmZilN02x3byMB2X3x+B4iyKHkucv70WA4hsyZkjo8=
github.com/mzz2017/quic-go v0.0.0-20230706143320-cc858d4932b7/go.mod h1:3H6d55CEofIWWr3gQThiB27+hA3WG5tATtPovzEYPAA=
github.com/mzz2017/softwind v0.0.0-20230708102709-26ff44839573 h1:fDndoUP5FyJKZM0LJ9nqZJhZF9eLhgfG46xwxO4UHww=
github.com/mzz2017/softwind v0.0.0-20230708102709-26ff44839573/go.mod h1:Fz8fgR7/dbnfR6RLpeOMkUDyebq4xShdmjj+cE5jnJ4=
github.com/mzz2017/softwind v0.0.0-20230710142544-73a557cea4a4 h1:U6oSJf+dwVXpBZGi73l77igid+sOy4jgJucjSrfowFU=
github.com/mzz2017/softwind v0.0.0-20230710142544-73a557cea4a4/go.mod h1:Fz8fgR7/dbnfR6RLpeOMkUDyebq4xShdmjj+cE5jnJ4=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=