Repository navigation
Expand file tree
/
Copy pathprocess_defense.go
More file actions
134 lines (117 loc) · 4.53 KB
/
Copy pathprocess_defense.go
File metadata and controls
134 lines (117 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
File: process_defense.go
Version: 1.21.0
Last Updated: 02-Nov-2026 19:35 CET
Description:
UDP Fragmentation and Amplification Defense.
Extracts RFC 8482 and DNS Flag Day constraints into a modular helper to
harden UDP connections from being abused for volumetric denial-of-service.
Changes:
1.21.0 - [SECURITY/FIX] Restricted `EDNS0_SUBNET` (ECS) response echoing in
`enforceUDPDefenses` to trusted forwarder proxies (`isTrustedProxy(clientAddr)`
when `group.ECSAction == "pass"`). Prevents untrusted clients from sending
an `EDNS0_SUBNET` option to probe or leak the `EDNS0_SUBNET` scope returned
by an `ecs: add` upstream.
1.20.0 - [SECURITY/FIX] Resolved a severe Payload Truncation Vulnerability.
`enforceUDPDefenses` previously evaluated `upstreamSize > limitApplied`
before the response payload was cleanly repackaged without `Extra` bounds.
1.19.0 - [SECURITY/FIX] Restored `extRcode` bounds to `uint16` to correctly
satisfy the `SetExtendedRcode()` interface contract expected by `miekg/dns`.
*/
package main
import (
"net/netip"
"github.com/miekg/dns"
)
// enforceUDPDefenses applies RFC 8482 and DNS Flag Day 2020 constraints
// to UDP responses to mitigate amplification and fragmentation vectors.
// It simultaneously enforces strict EDNS0 normalization, nullifying cache-poisoning payloads.
// Returns the original upstream payload size and the client's advertised buffer size for telemetry.
func enforceUDPDefenses(r *dns.Msg, resp *dns.Msg, protocol string, clientAddr netip.Addr, group *UpstreamGroup) (upstreamSize int, clientAdvertised int) {
// RFC 1035 default fallback size if no EDNS0 constraints are present
clientAdvertised = 512
limitApplied := 512
var clientSentTrustedECS bool
// Parse EDNS0 options universally to ensure TCP/DoH/DoT queries accurately
// log their advertised buffer capacities in the Web UI telemetry, rather than
// erroneously falling back to 512 bytes on non-UDP connections.
if opt := r.IsEdns0(); opt != nil {
clientAdvertised = int(opt.UDPSize())
if clientAdvertised >= 512 {
limitApplied = clientAdvertised
}
// Only echo upstream ECS scope back when the query arrived from a trusted proxy
// and the upstream group is configured to pass ECS.
if group != nil && group.ECSAction == "pass" && isTrustedProxy(clientAddr) {
for _, o := range opt.Option {
if _, ok := o.(*dns.EDNS0_SUBNET); ok {
clientSentTrustedECS = true
break
}
}
}
}
// Truncation and Fragmentation limits apply strictly to stateless UDP payloads
if protocol == "UDP" {
// DNS Flag Day 2020 constraint: Cap UDP payloads at 1232 bytes natively to
// thwart IP fragmentation attacks and amplification reflection vectors.
if limitApplied > 1232 {
limitApplied = 1232
}
}
// [SECURITY] EDNS0 Normalization & Cache Poisoning Prevention
// RFC 6891 §6.1.1 strictly dictates that OPT records must not be cached
// and returned verbatim. Furthermore, we must strip all unrelated records
// from the Extra section to thwart downstream Cache Poisoning attacks.
var respOpt *dns.OPT
var extRcode uint16
var edeOptions []*dns.EDNS0_EDE
var ecsOption *dns.EDNS0_SUBNET
// Capture the upstream's Extended RCODE, EDE, and ECS payloads natively before stripping the payload
if upOpt := resp.IsEdns0(); upOpt != nil {
extRcode = uint16(upOpt.ExtendedRcode())
for _, o := range upOpt.Option {
if ede, ok := o.(*dns.EDNS0_EDE); ok {
edeOptions = append(edeOptions, ede)
}
if ecs, ok := o.(*dns.EDNS0_SUBNET); ok {
ecsOption = ecs
}
}
}
if opt := r.IsEdns0(); opt != nil {
respOpt = new(dns.OPT)
respOpt.Hdr.Name = "."
respOpt.Hdr.Rrtype = dns.TypeOPT
respOpt.SetUDPSize(uint16(limitApplied))
if opt.Do() {
respOpt.SetDo()
}
if extRcode > 0 {
respOpt.SetExtendedRcode(extRcode)
}
if len(edeOptions) > 0 {
for _, ede := range edeOptions {
respOpt.Option = append(respOpt.Option, ede)
}
}
// Exclusively relay ECS scope if the originating trusted proxy supplied an ECS option.
if ecsOption != nil && clientSentTrustedECS {
respOpt.Option = append(respOpt.Option, ecsOption)
}
}
// Preemptively isolate and restrict the Extra slice to strictly authorized limits
if respOpt != nil {
resp.Extra = []dns.RR{respOpt}
} else {
resp.Extra = nil
}
upstreamSize = resp.Len()
if protocol == "UDP" && upstreamSize > limitApplied {
resp.Truncated = true
resp.Authoritative = false
resp.Answer = nil
resp.Ns = nil
}
return upstreamSize, clientAdvertised
}