Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: truncated dns resp #203

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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