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

Bug fix for use-after-close connection in AXFR #523

Merged
merged 2 commits into from
Feb 26, 2025
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/weppos/publicsuffix-go v0.40.3-0.20250127173806-e489a31678ca // indirect
github.com/zmap/rc2 v0.0.0-20190804163417-abaa70531248 // indirect
golang.org/x/crypto v0.33.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
33 changes: 24 additions & 9 deletions src/modules/axfr/axfr.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,23 @@ type AxfrLookupModule struct {
NSModule nslookup.NSLookupModule
BlacklistPath string `long:"blacklist-file" description:"path to blacklist file" default:""`
Blacklist *safeblacklist.SafeBlacklist
dns.Transfer
TransferFact TransferFactory
}

// TransferInterface used to enable mocking for dns.In
type TransferInterface interface {
In(m *dns.Msg, address string) (chan *dns.Envelope, error)
}

// TransferFactory each AXFR module isn't thread-safe, so we need to create a new Transfer object for each AXFR lookup
type TransferFactory interface {
NewTransfer() TransferInterface
}

type RealTransferFactory struct{}

func (f *RealTransferFactory) NewTransfer() TransferInterface {
return &dns.Transfer{}
}

type AXFRServerResult struct {
Expand All @@ -58,11 +74,7 @@ func dotName(name string) string {
return strings.Join([]string{name, "."}, "")
}

type TransferClient struct {
dns.Transfer
}

func (axfrMod *AxfrLookupModule) doAXFR(name string, server *zdns.NameServer) AXFRServerResult {
func (axfrMod *AxfrLookupModule) doAXFR(transfer TransferInterface, name string, server *zdns.NameServer) AXFRServerResult {
var retv AXFRServerResult
retv.Server = server.IP.String()
// check if the server address is blacklisted and if so, exclude
Expand All @@ -79,7 +91,7 @@ func (axfrMod *AxfrLookupModule) doAXFR(name string, server *zdns.NameServer) AX
}
m := new(dns.Msg)
m.SetAxfr(dotName(name))
if a, err := axfrMod.In(m, net.JoinHostPort(server.IP.String(), "53")); err != nil {
if a, err := transfer.In(m, net.JoinHostPort(server.IP.String(), "53")); err != nil {
retv.Status = zdns.StatusError
retv.Error = err.Error()
return retv
Expand All @@ -103,6 +115,8 @@ func (axfrMod *AxfrLookupModule) doAXFR(name string, server *zdns.NameServer) AX

func (axfrMod *AxfrLookupModule) Lookup(resolver *zdns.Resolver, name string, nameServer *zdns.NameServer) (interface{}, zdns.Trace, zdns.Status, error) {
var retv AXFRResult
// create a new AXFR transfer object
transfer := axfrMod.TransferFact.NewTransfer()
if nameServer == nil {
parsedNS, trace, status, err := axfrMod.NSModule.Lookup(resolver, name, nameServer)
if status != zdns.StatusNoError {
Expand All @@ -115,11 +129,11 @@ func (axfrMod *AxfrLookupModule) Lookup(resolver *zdns.Resolver, name string, na
for _, server := range castedNS.Servers {
if len(server.IPv4Addresses) > 0 {
ns := &zdns.NameServer{IP: net.ParseIP(server.IPv4Addresses[0])}
retv.Servers = append(retv.Servers, axfrMod.doAXFR(name, ns))
retv.Servers = append(retv.Servers, axfrMod.doAXFR(transfer, name, ns))
}
}
} else {
retv.Servers = append(retv.Servers, axfrMod.doAXFR(name, nameServer))
retv.Servers = append(retv.Servers, axfrMod.doAXFR(transfer, name, nameServer))
}
return retv, nil, zdns.StatusNoError, nil
}
Expand Down Expand Up @@ -168,5 +182,6 @@ func (axfrMod *AxfrLookupModule) CLIInit(gc *cli.CLIConf, rc *zdns.ResolverConfi
if err = axfrMod.BasicLookupModule.CLIInit(gc, rc); err != nil {
return errors.Wrap(err, "failed to initialize basic lookup module")
}
axfrMod.TransferFact = &RealTransferFactory{} // Default factory
return nil
}
16 changes: 15 additions & 1 deletion src/modules/axfr/axfr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/miekg/dns"
"github.com/stretchr/testify/mock"
"gotest.tools/v3/assert"

"github.com/zmap/zdns/src/cli"
Expand All @@ -45,7 +46,19 @@ func (err enError) Error() string {
return envelopeError
}

func (axfrMod *AxfrLookupModule) In(m *dns.Msg, server string) (chan *dns.Envelope, error) {
type MockTransfer struct {
mock.Mock
}

type MockTransferFactory struct {
Mock *MockTransfer
}

func (f *MockTransferFactory) NewTransfer() TransferInterface {
return f.Mock
}

func (mock *MockTransfer) In(m *dns.Msg, server string) (chan *dns.Envelope, error) {
var eError error = nil
if envelopeError != "" {
eError = enError{}
Expand Down Expand Up @@ -102,6 +115,7 @@ func InitTest() (*AxfrLookupModule, *zdns.Resolver) {
if err != nil {
panic("failed to initialize axfr test lookup with error: " + err.Error())
}
axfrMod.TransferFact = &MockTransferFactory{Mock: new(MockTransfer)} // configure the mock transfer factory
resolver, err := zdns.InitResolver(rc)
if err != nil {
panic("failed to initialize resolver: " + err.Error())
Expand Down
Loading