Skip to content

Commit 67bbd98

Browse files
TMHSDigitalclaude
andcommitted
Retry an oversized UDP CNAME answer over TCP (#375)
Since #321 the UDP read buffer is the advertised 1232-byte EDNS payload. A larger answer from a server that ignores that size and does not set TC was cut by the read (silently on Linux, with WSAEMSGSIZE on Windows), failed to parse, and was skipped as a stray packet, so the lookup waited out its timeout. cnameExchange now matches a reply by its header and question alone; a matching answer cut to the buffer comes back marked truncated so rawLookup asks again over TCP, and a matching answer that fits but does not parse fails at once. Also put cnameQuery's doc comment back above cnameQuery. Fixes #375 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e5eeb2e commit 67bbd98

3 files changed

Lines changed: 112 additions & 14 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7272

7373
### Fixed
7474
- A lookup is counted as `refused` only when the resolver answered with RCODE REFUSED or a DNS-over-HTTPS server shed load with HTTP 429 or 503. A socket error such as "connection refused", from a closed `-dns-server` port or a resolver that is down, used to be counted as `refused` too, so the verdict, the preflight error and the reliability guard pointed at rate limiting when the address was wrong; it is now counted as `other`, the same on every platform (#377).
75+
- A `CNAME` lookup no longer waits out its `-timeout` when a resolver ignores the advertised 1232-byte EDNS size and sends a larger UDP answer without setting TC. The read cut the answer, which then failed to parse and was skipped as a stray packet (on Windows the lookup failed as `other`); it is now asked again over TCP as if truncated. A matching UDP answer that fits but does not parse fails at once instead of timing out (#375).
7576
- The GitHub Action carries a time-boxed scan across runs. With `max-duration` and `baseline-artifact`, a run the budget stops saves its resume state in the baseline artifact, next to the previous results it still diffs against, and the next run continues that scan with `-resume` instead of starting from the first name again, so a large wordlist is worked through over several scheduled runs and the baseline is replaced once a run completes. The state is saved even when `fail-on` fails the step. A change to the action's inputs discards the state with a notice, as does subenum refusing to resume (for example a changed wordlist file), and a new scan starts. A new `resumed` output says whether the run continued a stopped scan. A later use of the action in the same job with the same `baseline-artifact` reads the copy the earlier use saved instead of the artifact list, which can lag an upload by seconds and made the later use start over or miss the baseline (#352).
7677
- The config file and `SUBENUM_W`/`SUBENUM_O` can set several `-w` wordlists or `-o` files, as the command line can. The config file takes a JSON array for `w` and `o`, one file per entry (`"o": ["names.txt", "jsonl:results.jsonl"]`), and for `sources`, as it already did for `exclude` and `type`; a string for `w` or `o` there or in the environment is a comma-separated list, as it is for the other list settings, where it used to become one path with commas in it. `-print-config` shows `w` and `o` as a JSON array, so one file whose name has a comma is told apart from two (#353).
7778
- A name whose lookup of one requested type timed out, was refused or failed while another type answered is no longer a plain found result with the records that came back. The whole lookup is tried again up to `-attempts`, and when the failed type never answers the result is marked partial: a `partial` field in JSON and JSONL, `PARTIAL` in text with `-show-records`, a `partial answers` line in the breakdown, and `Stats.Partial` in the library. Partial answers lower the run's verdict as failed lookups do (degraded at 1%), and `-diff-records` does not report a partial name as changed, since its missing records may only be a lookup that failed (#357).

‎internal/dns/cname.go‎

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ func canonicalName(msg *dnsmessage.Message, name string) string {
108108
// cnameExchange sends one qtype query (CNAME for lookupCNAME) for qname over
109109
// a connection from resolver.Dial and returns the matching reply. On UDP,
110110
// replies to other queries (a pooled socket can hold a late answer to an
111-
// earlier one) are skipped until the deadline.
111+
// earlier one) are skipped until the deadline, and an answer cut to the read
112+
// buffer comes back marked truncated.
112113
func cnameExchange(ctx context.Context, resolver *net.Resolver, network string, qname dnsmessage.Name, qtype dnsmessage.Type) (*dnsmessage.Message, error) {
113114
conn, err := resolver.Dial(ctx, network, "")
114115
if err != nil {
@@ -148,12 +149,15 @@ func cnameExchange(ctx context.Context, resolver *net.Resolver, network string,
148149
}
149150
for {
150151
var reply []byte
152+
cut := false
151153
if packet {
152154
n, err := conn.Read(buf)
153-
if err != nil {
155+
// A datagram longer than buf is cut to fit: silently on Linux,
156+
// with WSAEMSGSIZE on Windows. Its header is still there to match.
157+
if err != nil && n < len(buf) {
154158
return nil, err
155159
}
156-
reply = buf[:n]
160+
reply, cut = buf[:n], n == len(buf)
157161
} else {
158162
var lenb [2]byte
159163
if _, err := io.ReadFull(conn, lenb[:]); err != nil {
@@ -164,29 +168,47 @@ func cnameExchange(ctx context.Context, resolver *net.Resolver, network string,
164168
return nil, err
165169
}
166170
}
167-
var msg dnsmessage.Message
168-
if err := msg.Unpack(reply); err != nil {
171+
h, ok := replyMatches(reply, id, qname, qtype)
172+
if !ok {
169173
if packet {
170-
continue // not a DNS message for us
174+
continue // a late answer to an earlier query on this socket, or not DNS
171175
}
172-
return nil, err
176+
return nil, errors.New("reply does not match the query")
173177
}
174-
if msg.ID != id || !msg.Response || len(msg.Questions) != 1 ||
175-
msg.Questions[0].Type != qtype || !strings.EqualFold(msg.Questions[0].Name.String(), qname.String()) {
176-
if packet {
177-
continue // a late answer to an earlier query on this socket
178+
var msg dnsmessage.Message
179+
if err := msg.Unpack(reply); err != nil {
180+
if cut {
181+
// The server ignored the advertised size and did not set TC:
182+
// ask again over TCP as if it had, rather than skip the
183+
// answer and wait out the deadline (#375).
184+
h.Truncated = true
185+
return &dnsmessage.Message{Header: h}, nil
178186
}
179-
return nil, errors.New("reply does not match the query")
187+
return nil, err
180188
}
181189
return &msg, nil
182190
}
183191
}
184192

193+
// replyMatches reports whether reply answers the query with this ID, name
194+
// and type, reading only its header and question, and returns the header.
195+
func replyMatches(reply []byte, id uint16, qname dnsmessage.Name, qtype dnsmessage.Type) (dnsmessage.Header, bool) {
196+
var p dnsmessage.Parser
197+
h, err := p.Start(reply)
198+
if err != nil || h.ID != id || !h.Response {
199+
return h, false
200+
}
201+
q, err := p.Question()
202+
if err != nil || q.Type != qtype || !strings.EqualFold(q.Name.String(), qname.String()) {
203+
return h, false
204+
}
205+
_, err = p.Question()
206+
return h, errors.Is(err, dnsmessage.ErrSectionDone) // exactly one question
207+
}
208+
185209
// ednsPayload is the UDP answer size cnameQuery advertises, Go's resolver's.
186210
const ednsPayload = 1232
187211

188-
// cnameQuery builds a recursive qtype query with an EDNS(0) OPT record
189-
// advertising 1232-byte UDP answers, as Go's resolver sends.
190212
// validLabels reports whether every label of name, which may end in a dot,
191213
// is 1 to 63 bytes long.
192214
func validLabels(name string) bool {
@@ -198,6 +220,8 @@ func validLabels(name string) bool {
198220
return true
199221
}
200222

223+
// cnameQuery builds a recursive qtype query with an EDNS(0) OPT record
224+
// advertising 1232-byte UDP answers, as Go's resolver sends.
201225
func cnameQuery(id uint16, qname dnsmessage.Name, qtype dnsmessage.Type) ([]byte, error) {
202226
b := dnsmessage.NewBuilder(nil, dnsmessage.Header{ID: id, RecursionDesired: true})
203227
b.EnableCompression()

‎internal/dns/cname_test.go‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,79 @@ func TestCNAMEOverTCPRejectsBadReplies(t *testing.T) {
332332
}
333333
}
334334

335+
// TestCNAMEOversizedUDPAnswer covers #375: a UDP answer over the advertised
336+
// 1232 bytes, without TC, is cut by the read; it is asked again over TCP as
337+
// if truncated, not skipped until the lookup times out. A matching answer
338+
// that fits but does not parse fails at once.
339+
func TestCNAMEOversizedUDPAnswer(t *testing.T) {
340+
const name, target = "big.example.com.", "edge.example.net."
341+
oversized := func(h dnsmessage.Header, q dnsmessage.Question) []byte {
342+
b := dnsmessage.NewBuilder(nil, dnsmessage.Header{ID: h.ID, Response: true})
343+
_ = b.StartQuestions()
344+
_ = b.Question(q)
345+
_ = b.StartAnswers()
346+
_ = b.CNAMEResource(dnsmessage.ResourceHeader{Name: q.Name, Class: dnsmessage.ClassINET, TTL: 60}, dnsmessage.CNAMEResource{CNAME: dnsmessage.MustNewName(target)})
347+
_ = b.StartAdditionals()
348+
pad := strings.Repeat("x", 250)
349+
_ = b.TXTResource(dnsmessage.ResourceHeader{Name: q.Name, Class: dnsmessage.ClassINET, TTL: 60}, dnsmessage.TXTResource{TXT: []string{pad, pad, pad, pad, pad, pad}})
350+
msg, err := b.Finish()
351+
if err != nil {
352+
t.Error(err)
353+
}
354+
if len(msg) <= ednsPayload {
355+
t.Errorf("padded answer is %d bytes, want over %d", len(msg), ednsPayload)
356+
}
357+
return msg
358+
}
359+
for _, tc := range []struct {
360+
name string
361+
udpReply func(h dnsmessage.Header, q dnsmessage.Question) []byte
362+
wantTCP int64
363+
wantFound bool
364+
}{
365+
{name: "over 1232 bytes", udpReply: oversized, wantTCP: 1, wantFound: true},
366+
{name: "fits but does not parse", udpReply: func(h dnsmessage.Header, q dnsmessage.Question) []byte {
367+
msg := cnameMsg(t, dnsmessage.Header{ID: h.ID, Response: true}, q, q.Name, target)
368+
return msg[:len(msg)-3] // the CNAME record stops short
369+
}},
370+
} {
371+
t.Run(tc.name, func(t *testing.T) {
372+
udp, udpQueries := scriptedUDP(t, func(h dnsmessage.Header, q dnsmessage.Question) [][]byte {
373+
return [][]byte{tc.udpReply(h, q)}
374+
})
375+
var tcpQueries atomic.Int64
376+
q := dnsmessage.Question{Name: dnsmessage.MustNewName(name), Type: dnsmessage.TypeCNAME, Class: dnsmessage.ClassINET}
377+
tcp := tcpServer(t, func(id uint16, conn net.Conn) {
378+
tcpQueries.Add(1)
379+
writeFramed(conn, cnameMsg(t, dnsmessage.Header{ID: id, Response: true}, q, q.Name, target))
380+
})
381+
r := &net.Resolver{PreferGo: true, Dial: func(ctx context.Context, network, _ string) (net.Conn, error) {
382+
addr := udp.LocalAddr().String()
383+
if network == "tcp" {
384+
addr = tcp
385+
}
386+
var d net.Dialer
387+
return d.DialContext(ctx, network, addr)
388+
}}
389+
start := time.Now()
390+
got, err := lookupCNAME(contextWithTimeout(t, 5*time.Second), r, name)
391+
if d := time.Since(start); d > 2*time.Second {
392+
t.Errorf("lookup took %v, want an answer well before the 5s deadline", d)
393+
}
394+
if tc.wantFound {
395+
if err != nil || got != target {
396+
t.Errorf("lookupCNAME = %q, %v; want %q", got, err, target)
397+
}
398+
} else if err == nil || Classify(err) != OutcomeOther {
399+
t.Errorf("lookupCNAME = %q, %v; want an error classed other", got, err)
400+
}
401+
if u, c := udpQueries.Load(), tcpQueries.Load(); u != 1 || c != tc.wantTCP {
402+
t.Errorf("%d UDP and %d TCP queries, want 1 and %d", u, c, tc.wantTCP)
403+
}
404+
})
405+
}
406+
}
407+
335408
// timeoutError is a net.Error that reports a timeout, as a read past a
336409
// connection's deadline does.
337410
type timeoutError struct{}

0 commit comments

Comments
 (0)