Skip to content

Commit

Permalink
fix: truncated dns resp (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzz2017 committed Jul 10, 2023
1 parent 11d2ea9 commit 1cc9edc
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
5 changes: 5 additions & 0 deletions common/consts/net.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package consts

const (
EthernetMtu = 1500
)
7 changes: 4 additions & 3 deletions common/netutils/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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])
Expand All @@ -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
}
Expand Down
11 changes: 6 additions & 5 deletions control/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion control/dns_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 2 additions & 6 deletions control/udp_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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[:])
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 1cc9edc

Please sign in to comment.