mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-07-16 13:17:19 +02:00
fix(doh,doq): reject oversized upstream DNS responses
DoH, DoH3, and DoQ response paths previously used io.ReadAll on attacker-controlled upstream responses before enforcing any protocol-level size limit. A malicious or compromised upstream could return an oversized body or stream and force ctrld to buffer unbounded data before eventually failing DNS parsing. Cap DoH/DoH3 response bodies at dns.MaxMsgSize and cap DoQ streams at the 2-byte length prefix plus dns.MaxMsgSize. Also limit non-200 DoH error bodies so error formatting cannot consume large upstream responses.
This commit is contained in:
committed by
Cuong Manh Le
parent
35455eb0b9
commit
3fe9b27fb4
@@ -17,6 +17,12 @@ import (
|
||||
"github.com/quic-go/quic-go"
|
||||
)
|
||||
|
||||
// doqMaxResponseSize caps the bytes read from a DoQ stream: a 2-byte
|
||||
// length prefix plus a DNS message bounded by dns.MaxMsgSize. Anything
|
||||
// larger cannot be a valid response and is rejected before buffering more
|
||||
// data from the upstream.
|
||||
const doqMaxResponseSize = 2 + dns.MaxMsgSize
|
||||
|
||||
type doqResolver struct {
|
||||
uc *UpstreamConfig
|
||||
}
|
||||
@@ -191,7 +197,13 @@ func (p *doqConnPool) doResolve(ctx context.Context, msg *dns.Msg) (*dns.Msg, er
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf, err := io.ReadAll(stream)
|
||||
// A DoQ response is a 2-byte length prefix followed by a DNS message.
|
||||
// The DNS message is bounded by the protocol at dns.MaxMsgSize, so a
|
||||
// well-formed response is at most doqMaxResponseSize bytes. Read one
|
||||
// byte past that cap to distinguish "at limit" from "over limit" and
|
||||
// reject oversized responses before they can drive memory growth from
|
||||
// a malicious or compromised upstream.
|
||||
buf, err := io.ReadAll(io.LimitReader(stream, doqMaxResponseSize+1))
|
||||
if err != nil {
|
||||
p.putConn(conn, false)
|
||||
return nil, err
|
||||
@@ -203,6 +215,11 @@ func (p *doqConnPool) doResolve(ctx context.Context, msg *dns.Msg) (*dns.Msg, er
|
||||
return nil, io.EOF
|
||||
}
|
||||
|
||||
if len(buf) > doqMaxResponseSize {
|
||||
p.putConn(conn, false)
|
||||
return nil, fmt.Errorf("DoQ response exceeds %d-byte maximum", doqMaxResponseSize)
|
||||
}
|
||||
|
||||
// RFC 9250: each DoQ DNS message is encoded as a 2-octet length field
|
||||
// followed by the DNS message. Reject responses that are shorter than
|
||||
// the prefix or whose prefix declares more bytes than were received,
|
||||
|
||||
Reference in New Issue
Block a user