Skip to content

Commit b427548

Browse files
TMHSDigitalclaude
andcommitted
Bound the names a zone transfer keeps
A transfer stops at 100,000 names or 4 MiB of names and returns what it has, reported as cut; the names kept over all of -axfr's name servers have the same bound. The record and message counts alone let a large zone hold far more memory than its stream size. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 56133d3 commit b427548

4 files changed

Lines changed: 90 additions & 11 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ subenum -axfr -type A,AAAA,CNAME example.com
491491

492492
The same step asks one name server about a name that does not exist, with DNSSEC records requested. A zone signed with plain NSEC proves the absence by naming its neighbours, so anyone can walk the chain to list every name, transfer or not: subenum reports that as `Zone walkable` and records `zone_denial` (`nsec`, `nsec3` or `none`) in the report. It does not walk the zone itself.
493493

494-
The transfers and the probe go straight to the target's name servers, not through `-dns-server`: at most 8 servers and 2 addresses each, one query per address, plus the NS and address lookups and one denial probe, all counted in the report's `queries_sent` but not paced by `-rate`. Simulation ignores `-axfr`, lab mode refuses it, and a resumed scan reuses the names the interrupted run got.
494+
The transfers and the probe go straight to the target's name servers, not through `-dns-server`: at most 8 servers and 2 addresses each, one query per address, at most 100,000 names kept (a larger zone is cut there and the cut is reported), plus the NS and address lookups and one denial probe, all counted in the report's `queries_sent` but not paced by `-rate`. Simulation ignores `-axfr`, lab mode refuses it, and a resumed scan reuses the names the interrupted run got.
495495

496496
</details>
497497

‎internal/axfr/axfr.go‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ func Try(ctx context.Context, target string, o Options) Result {
150150
wg.Go(func() { a.transfer(ctx, o, target, transferTimeout) })
151151
}
152152
wg.Wait()
153+
// The names kept over all servers are bounded like one transfer's, so
154+
// eight servers cannot add eight times as many candidates.
153155
names := map[string]struct{}{}
156+
capped := false
154157
for _, a := range attempts {
155158
for _, l := range a.logs {
156159
logf(l.f, "%s", l.msg)
@@ -160,10 +163,19 @@ func Try(ctx context.Context, target string, o Options) Result {
160163
if a.allowed {
161164
res.Allowed = append(res.Allowed, a.host)
162165
for _, n := range a.got {
163-
names[strings.TrimSuffix(n, "."+target)] = struct{}{}
166+
n = strings.TrimSuffix(n, "."+target)
167+
if _, ok := names[n]; !ok && len(names) >= dns.MaxTransferNames {
168+
capped = true
169+
break
170+
}
171+
names[n] = struct{}{}
164172
}
173+
a.got = nil
165174
}
166175
}
176+
if capped {
177+
logf(o.Info, "-axfr: the transfers gave more than %d names; only the first %d are used", dns.MaxTransferNames, dns.MaxTransferNames)
178+
}
167179
for n := range names {
168180
res.Names = append(res.Names, n)
169181
}
@@ -203,6 +215,11 @@ func (a *attempt) transfer(ctx context.Context, o Options, target string, timeou
203215
}
204216
a.tried++
205217
got, err := dns.Transfer(ctx, net.JoinHostPort(addr, o.Port), target, timeout)
218+
if errors.Is(err, dns.ErrTransferTruncated) {
219+
a.allowed, a.got = true, got
220+
a.logf(o.Info, "Zone transfer: %s (%s) allowed AXFR of %s, but it was stopped: %v. Anyone can list this zone; restrict transfers to your secondaries.", a.host, addr, target, err)
221+
return
222+
}
206223
if err != nil {
207224
if errors.Is(err, dns.ErrTransferRefused) {
208225
a.logf(o.Debug, "-axfr: %s (%s): %v", a.host, addr, err)

‎internal/dns/axfr.go‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,20 @@ import (
2020
const (
2121
axfrMaxRecords = 1 << 20
2222
axfrMaxMessages = 1 << 16
23+
// axfrMaxNameBytes bounds the bytes of names one transfer holds: a
24+
// compressed owner costs a few bytes on the wire and up to 253 in memory.
25+
axfrMaxNameBytes = 4 << 20
2326
)
2427

28+
// MaxTransferNames is the most names one transfer keeps, and the most an
29+
// -axfr attempt keeps over all its servers. A transfer that reaches it, or
30+
// axfrMaxNameBytes, stops and returns what it has with ErrTransferTruncated.
31+
const MaxTransferNames = 100_000
32+
33+
// ErrTransferTruncated is returned by Transfer, with the names read so far,
34+
// when the zone has more names than a transfer keeps.
35+
var ErrTransferTruncated = errors.New("zone transfer truncated")
36+
2537
// rcodeNotAuth is NOTAUTH (RFC 2845), which some servers send to refuse a
2638
// transfer; dnsmessage has no constant for it.
2739
const rcodeNotAuth dnsmessage.RCode = 9
@@ -34,7 +46,8 @@ var ErrTransferRefused = errors.New("zone transfer refused")
3446
// of zone over TCP and returns the names in the zone: lowercase, without the
3547
// trailing dot, sorted, without duplicates, and without the apex itself.
3648
// Every record's owner counts, whatever its type. timeout bounds the whole
37-
// transfer, connection included.
49+
// transfer, connection included. A zone with more names than a transfer
50+
// keeps returns the names so far and an error wrapping ErrTransferTruncated.
3851
func Transfer(ctx context.Context, server, zone string, timeout time.Duration) ([]string, error) {
3952
zone = strings.ToLower(strings.TrimSuffix(zone, "."))
4053
ctx, cancel := context.WithTimeout(ctx, timeout)
@@ -61,7 +74,7 @@ func Transfer(ctx context.Context, server, zone string, timeout time.Duration) (
6174
}
6275

6376
names := map[string]struct{}{}
64-
soas, records := 0, 0
77+
soas, records, held := 0, 0, 0
6578
for range axfrMaxMessages {
6679
msg, err := readStreamMessage(conn)
6780
if err != nil {
@@ -75,7 +88,10 @@ func Transfer(ctx context.Context, server, zone string, timeout time.Duration) (
7588
}
7689
return nil, fmt.Errorf("zone transfer from %s: %w", server, err)
7790
}
78-
done, err := readTransferMessage(msg, id, zone, names, &soas, &records)
91+
done, err := readTransferMessage(msg, id, zone, names, &held, &soas, &records)
92+
if errors.Is(err, ErrTransferTruncated) {
93+
return sortedNames(names), fmt.Errorf("%w at %d names", err, len(names))
94+
}
7995
if err != nil || done {
8096
if err != nil {
8197
return nil, err
@@ -86,12 +102,16 @@ func Transfer(ctx context.Context, server, zone string, timeout time.Duration) (
86102
if soas < 2 {
87103
return nil, fmt.Errorf("zone transfer from %s ended before the closing SOA", server)
88104
}
105+
return sortedNames(names), nil
106+
}
107+
108+
func sortedNames(names map[string]struct{}) []string {
89109
out := make([]string, 0, len(names))
90110
for n := range names {
91111
out = append(out, n)
92112
}
93113
sort.Strings(out)
94-
return out, nil
114+
return out
95115
}
96116

97117
// axfrQuery is a length-prefixed AXFR query for zone.
@@ -129,9 +149,11 @@ func readStreamMessage(r io.Reader) ([]byte, error) {
129149
}
130150

131151
// readTransferMessage adds the owner names of one transfer message's answers
132-
// under zone to names, and reports whether the closing SOA arrived. The first
133-
// record of a transfer must be the zone's SOA, and the next SOA ends it.
134-
func readTransferMessage(msg []byte, id uint16, zone string, names map[string]struct{}, soas, records *int) (bool, error) {
152+
// under zone to names, counting their bytes in held, and reports whether the
153+
// closing SOA arrived. The first record of a transfer must be the zone's SOA,
154+
// and the next SOA ends it. Reaching MaxTransferNames or axfrMaxNameBytes
155+
// returns ErrTransferTruncated.
156+
func readTransferMessage(msg []byte, id uint16, zone string, names map[string]struct{}, held, soas, records *int) (bool, error) {
135157
var p dnsmessage.Parser
136158
hdr, err := p.Start(msg)
137159
if err != nil {
@@ -177,7 +199,14 @@ func readTransferMessage(msg []byte, id uint16, zone string, names map[string]st
177199
}
178200
name := strings.ToLower(strings.TrimSuffix(rh.Name.String(), "."))
179201
if name != zone && strings.HasSuffix(name, "."+zone) {
202+
if _, ok := names[name]; ok {
203+
continue
204+
}
205+
if len(names) >= MaxTransferNames || *held+len(name) > axfrMaxNameBytes {
206+
return false, ErrTransferTruncated
207+
}
180208
names[name] = struct{}{}
209+
*held += len(name)
181210
}
182211
}
183212
if *records == 0 {

‎internal/dns/axfr_hostile_test.go‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/binary"
66
"errors"
7+
"fmt"
78
"io"
89
"net"
910
"strings"
@@ -74,7 +75,7 @@ func TestReadTransferMessageRejectsBadMessages(t *testing.T) {
7475
{name: "garbage", msg: []byte{0, 42, 0x80}, want: ""},
7576
} {
7677
records, soas := tc.records, 1
77-
_, err := readTransferMessage(tc.msg, id, "example.test", map[string]struct{}{}, &soas, &records)
78+
_, err := readTransferMessage(tc.msg, id, "example.test", map[string]struct{}{}, new(int), &soas, &records)
7879
switch {
7980
case err == nil:
8081
t.Errorf("%s: accepted", tc.name)
@@ -86,6 +87,18 @@ func TestReadTransferMessageRejectsBadMessages(t *testing.T) {
8687
}
8788
}
8889

90+
// TestReadTransferMessageNameBytes: long names stop a transfer at the byte
91+
// bound before the name count is reached.
92+
func TestReadTransferMessageNameBytes(t *testing.T) {
93+
long := strings.Repeat("a", 60) + "." + strings.Repeat("b", 60) + ".example.test."
94+
msg := msgWith(t, dnsmessage.Header{ID: 1, Response: true}, []rr{{"x." + long, dnsmessage.TypeA}, {"y." + long, dnsmessage.TypeA}})
95+
names, held, soas, records := map[string]struct{}{}, axfrMaxNameBytes-200, 1, 1
96+
_, err := readTransferMessage(msg, 1, "example.test", names, &held, &soas, &records)
97+
if !errors.Is(err, ErrTransferTruncated) || len(names) != 1 {
98+
t.Errorf("err = %v with %d names, want ErrTransferTruncated after 1", err, len(names))
99+
}
100+
}
101+
89102
// tcpServer accepts one connection, reads the AXFR query, and hands the
90103
// query's ID and the connection to serve.
91104
func tcpServer(t *testing.T, serve func(id uint16, conn net.Conn)) string {
@@ -137,6 +150,26 @@ func TestTransferFromHostileServers(t *testing.T) {
137150
t.Errorf("err = %v, want io.ErrUnexpectedEOF", err)
138151
}
139152
})
153+
t.Run("more names than a transfer keeps", func(t *testing.T) {
154+
addr := tcpServer(t, func(id uint16, conn net.Conn) {
155+
writeFramed(conn, msgWith(t, dnsmessage.Header{ID: id, Response: true}, []rr{soa}))
156+
for m := 0; m*1000 <= MaxTransferNames; m++ {
157+
batch := make([]rr, 1000)
158+
for i := range batch {
159+
batch[i] = rr{fmt.Sprintf("h%d.example.test.", m*1000+i), dnsmessage.TypeA}
160+
}
161+
writeFramed(conn, msgWith(t, dnsmessage.Header{ID: id, Response: true}, batch))
162+
}
163+
writeFramed(conn, msgWith(t, dnsmessage.Header{ID: id, Response: true}, []rr{soa}))
164+
})
165+
names, err := Transfer(context.Background(), addr, "example.test", 10*time.Second)
166+
if !errors.Is(err, ErrTransferTruncated) {
167+
t.Fatalf("err = %v, want ErrTransferTruncated", err)
168+
}
169+
if len(names) != MaxTransferNames {
170+
t.Errorf("kept %d names, want %d", len(names), MaxTransferNames)
171+
}
172+
})
140173
t.Run("cancelled mid-transfer", func(t *testing.T) {
141174
hold := make(chan struct{})
142175
t.Cleanup(func() { close(hold) })
@@ -196,7 +229,7 @@ func FuzzReadTransferMessage(f *testing.F) {
196229
f.Fuzz(func(t *testing.T, msg []byte) {
197230
names := map[string]struct{}{}
198231
soas, records := 0, 0
199-
_, _ = readTransferMessage(msg, 1, "example.test", names, &soas, &records)
232+
_, _ = readTransferMessage(msg, 1, "example.test", names, new(int), &soas, &records)
200233
for n := range names {
201234
if !strings.HasSuffix(n, ".example.test") {
202235
t.Errorf("admitted %q, outside the zone", n)

0 commit comments

Comments
 (0)