-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathratelimit.go
More file actions
472 lines (405 loc) · 15.1 KB
/
ratelimit.go
File metadata and controls
472 lines (405 loc) · 15.1 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
File: ratelimit.go
Version: 1.9.0
Updated: 21-Apr-2026 13:25 CEST
Description:
High-performance, lock-free (sharded) Token Bucket Rate Limiter and Penalty Box.
Designed specifically to harden sdproxy when exposed to the public internet
as an Open Resolver. Defends against DNS amplification, reflection, and
volumetric DDoS attacks.
- Groups queries by client IP address or subnet prefix (IPv4/IPv6 masks).
- Pre-allocated 256 shards to completely eliminate mutex contention across
highly parallel UDP/TCP/QUIC workers.
- Background sweeper automatically reclaims memory from stale IPs.
- Hard cap eviction logic prevents OOM from malicious spoofed-IP floods.
- Integrated Penalty Box (blackhole) isolates severely abusive clients, silencing logs
and saving significant CPU/IO bandwidth automatically.
Changes:
1.9.0 - [SECURITY] Resolved a Time-Of-Check to Time-Of-Use (TOCTOU) race condition in
the Penalty Box fast-path tracker. Implemented `LoadAndDelete` across Sweeper
and Admission threads to guarantee deterministic exact-counting of
`fastPenaltyBoxCount`, preventing map-capacity starvation during continuous
high-volume DDoS floods.
1.8.0 - [SECURITY] Added `fastPenaltyBoxCount` atomic tracker bounded by `MaxTrackedIPs`.
This physically halts OOM (Out Of Memory) conditions during extreme DDoS
spoofing floods where malicious actors attempt to infinitely populate
the `sync.Map` fast-path with distinct malformed packet IPs.
[PERF] Migrated to `groupIPFast`, eliminating redundant `netip.ParseAddr`
calls by accepting the pre-parsed object directly from the `ProcessDNS` pipeline.
*/
package main
import (
"hash/maphash"
"log"
"net/netip"
"sync"
"sync/atomic"
"time"
)
// rlShardCount determines the number of independent lock-shards.
// 256 effectively eliminates contention for normal/high multi-core traffic.
const rlShardCount = 256
// rlBucket represents a single Token Bucket state for one Client IP / Subnet.
type rlBucket struct {
tokens float64
lastRefill int64 // unix nanoseconds
lastLog int64 // unix nanoseconds (used to debounce limit-hit logs)
strikes int // consecutive rate limit violations
bannedUntil int64 // unix nanoseconds (Penalty Box status)
}
// rlShard holds a chunk of the buckets, protected by its own Mutex.
type rlShard struct {
sync.Mutex
buckets map[string]*rlBucket
}
// fastBanEntry represents a lock-free cache item for actively blackholed clients.
type fastBanEntry struct {
expires int64
lastLog atomic.Int64
}
var (
rlShards [rlShardCount]*rlShard
// Configured parameters
rlQPS float64
rlBurst float64
rlV4Bits int
rlV6Bits int
rlMaxPerShard int
// Exemptions (Bypass list)
rlExempt []netip.Prefix
// Penalty Box Config
rlPenaltyEnabled bool
rlStrikeThreshold int
rlBanDurationNanos int64
// fastPenaltyBox provides a lock-free, O(1) fast-path for actively blackholed IPs.
// This prevents shard-mutex contention during high-volume spoofed DDoS attacks.
fastPenaltyBox sync.Map
// fastPenaltyBoxCount strictly limits the unbounded growth of the sync.Map above.
fastPenaltyBoxCount atomic.Int32
// rlHashSeed provides cryptographic randomization for the rate limit shard distribution.
rlHashSeed maphash.Seed
)
// InitRateLimiter wires up the rate limiting subsystem based on the config.
func InitRateLimiter() {
if !cfg.Server.RateLimit.Enabled {
return
}
// Initialize the randomized seed for HashDoS protection
rlHashSeed = maphash.MakeSeed()
rlQPS = cfg.Server.RateLimit.QPS
rlBurst = cfg.Server.RateLimit.Burst
if rlQPS <= 0 {
rlQPS = 20
}
if rlBurst <= 0 {
rlBurst = 100
}
rlV4Bits = cfg.Server.RateLimit.IPv4PrefixLength
rlV6Bits = cfg.Server.RateLimit.IPv6PrefixLength
if rlV4Bits <= 0 || rlV4Bits > 32 {
rlV4Bits = 32
}
if rlV6Bits <= 0 || rlV6Bits > 128 {
rlV6Bits = 128
}
maxIPs := cfg.Server.RateLimit.MaxTrackedIPs
if maxIPs <= 0 {
maxIPs = 100000 // Default: safely bound to ~100k distinct tracked IPs
}
rlMaxPerShard = maxIPs / rlShardCount
if rlMaxPerShard < 1 {
rlMaxPerShard = 1
}
// Parse Exemption List
rlExempt = parseACL(cfg.Server.RateLimit.Exempt)
// Setup Penalty Box Configs
rlPenaltyEnabled = cfg.Server.RateLimit.PenaltyBox.Enabled
rlStrikeThreshold = cfg.Server.RateLimit.PenaltyBox.StrikeThreshold
if rlStrikeThreshold <= 0 {
rlStrikeThreshold = 100 // Reasonable default threshold
}
banMins := cfg.Server.RateLimit.PenaltyBox.BanDurationMin
if banMins <= 0 {
banMins = 15 // Default 15 min ban
}
rlBanDurationNanos = int64(banMins) * 60 * 1e9
for i := 0; i < rlShardCount; i++ {
rlShards[i] = &rlShard{
buckets: make(map[string]*rlBucket),
}
}
log.Printf("[RATELIMIT] Active: %.1f QPS (Burst: %.0f) | Grouping: IPv4 /%d, IPv6 /%d | Max Tracked IPs: %d",
rlQPS, rlBurst, rlV4Bits, rlV6Bits, maxIPs)
if rlPenaltyEnabled {
log.Printf("[RATELIMIT] Penalty Box Enabled: Blackholing offenders for %d mins after %d strikes.", banMins, rlStrikeThreshold)
}
if len(rlExempt) > 0 {
log.Printf("[RATELIMIT] Exemption List: %d IP/Subnet rule(s) configured for unlimited access.", len(rlExempt))
}
// Sweeper prevents OOM by clearing stale IPs
go runRateLimitSweeper()
}
// isExempt verifies if a parsed netip.Addr belongs to the RateLimit exempt list.
// Evaluated natively to eliminate redundant string parsing.
func isExempt(addr netip.Addr) bool {
if len(rlExempt) == 0 || !addr.IsValid() {
return false
}
for _, prefix := range rlExempt {
if prefix.Contains(addr) {
return true
}
}
return false
}
// getRLShard hashes a string key to one of the 256 shards using maphash.
func getRLShard(key string) *rlShard {
h := maphash.String(rlHashSeed, key)
return rlShards[h&(rlShardCount-1)]
}
// groupIPFast masks the incoming IP address according to configured prefix lengths.
// Utilizes pre-parsed netip.Addr to eliminate redundant allocations on the hot path.
func groupIPFast(ipStr string, addr netip.Addr) string {
if !addr.IsValid() {
return ipStr
}
if addr.Is4() {
if rlV4Bits == 32 {
return ipStr
}
prefix, _ := addr.Prefix(rlV4Bits)
return prefix.Masked().Addr().String()
} else if addr.Is6() {
if rlV6Bits == 128 {
return ipStr
}
prefix, _ := addr.Prefix(rlV6Bits)
return prefix.Masked().Addr().String()
}
return ipStr
}
// AllowClient checks whether the client IP has enough tokens to execute a query.
// It also evaluates the Penalty Box (blackhole) status.
// Returns two booleans: `allowed` indicating if the query can proceed, and
// `isBanned` indicating if the client is currently in the Penalty Box.
func AllowClient(clientIP string, addr netip.Addr) (allowed bool, isBanned bool) {
if clientIP == "" {
return true, false // Allow internal queries if they lack an IP
}
key := groupIPFast(clientIP, addr)
// -----------------------------------------------------------------------
// 1. LOCK-FREE FAST-PATH (Penalty Box)
// -----------------------------------------------------------------------
// Bypasses shard-mutex locking and exemption loops entirely for actively
// blackholed IPs. Extremely critical for maintaining node stability during
// volumetric spoofed-IP or brute-force DDoS attacks.
if entryRaw, ok := fastPenaltyBox.Load(key); ok {
banEntry := entryRaw.(*fastBanEntry)
nowNanos := time.Now().UnixNano()
if nowNanos < banEntry.expires {
// Debounce the silent-drop log using atomic operations to protect I/O capacity
last := banEntry.lastLog.Load()
if nowNanos-last > int64(10*time.Second) {
if banEntry.lastLog.CompareAndSwap(last, nowNanos) {
log.Printf("[RATELIMIT] SECURITY: Silently dropping traffic from %s (IP is blackholed/in Penalty Box - Fast Path)", key)
}
}
return false, true
}
// Ban has naturally expired, remove from fast-path and fall through.
// LoadAndDelete guarantees exact-counting, preventing multiple parallel queries
// or the sweeper from decrementing the active limit tracker multiple times.
if _, loaded := fastPenaltyBox.LoadAndDelete(key); loaded {
fastPenaltyBoxCount.Add(-1) // Free capacity immediately
}
}
// -----------------------------------------------------------------------
// 2. EXEMPTIONS
// -----------------------------------------------------------------------
// Exemptions natively bypass the shard locks and math. Checked strictly
// AFTER the fast penalty box drop to prevent DDoS abuse against the prefix loop.
if isExempt(addr) {
return true, false
}
shard := getRLShard(key)
now := time.Now().UnixNano()
shard.Lock()
defer shard.Unlock()
bucket, exists := shard.buckets[key]
if !exists {
// Hard memory limit check: Evict a pseudo-random entry if shard is full.
// Protects strictly against OOM from spoofed-source DNS floods.
if len(shard.buckets) >= rlMaxPerShard {
for k := range shard.buckets {
delete(shard.buckets, k)
break
}
}
// New client: grant full burst capacity minus the one token being consumed
shard.buckets[key] = &rlBucket{
tokens: rlBurst - 1.0,
lastRefill: now,
lastLog: 0,
}
return true, false
}
// -----------------------------------------------------------------------
// Slow-Path Penalty Box Check (Fail-Safe)
// -----------------------------------------------------------------------
if rlPenaltyEnabled && bucket.bannedUntil > 0 {
if bucket.bannedUntil > now {
// IP is actively blackholed but somehow bypassed the fast path map.
// Debounce the silent-drop log to protect I/O capacity.
if now-bucket.lastLog > int64(10*time.Second) {
bucket.lastLog = now
log.Printf("[RATELIMIT] SECURITY: Silently dropping traffic from %s (IP is blackholed/in Penalty Box)", key)
}
return false, true
} else {
// Penalty Box duration has naturally expired
bucket.bannedUntil = 0
bucket.strikes = 0
log.Printf("[RATELIMIT] SECURITY: Client/Subnet %s Penalty Box ban expired. Access restored.", key)
}
}
// Calculate tokens generated since last refill
deltaNanos := now - bucket.lastRefill
if deltaNanos > 0 {
deltaTokens := float64(deltaNanos) * rlQPS / 1e9
bucket.tokens += deltaTokens
if bucket.tokens > rlBurst {
bucket.tokens = rlBurst
}
bucket.lastRefill = now
}
// If bucket has fully refilled, we can assume the IP has behaved well for a
// full recovery cycle. Forgive any accumulated strikes to prevent false-positives over time.
if bucket.tokens >= rlBurst && bucket.strikes > 0 {
bucket.strikes = 0
}
if bucket.tokens >= 1.0 {
bucket.tokens -= 1.0
return true, false
}
// -----------------------------------------------------------------------
// Rate Limit Exceeded - Apply Strike or Drop
// -----------------------------------------------------------------------
if rlPenaltyEnabled {
bucket.strikes++
if bucket.strikes >= rlStrikeThreshold {
banUntil := now + rlBanDurationNanos
bucket.bannedUntil = banUntil
bucket.lastLog = now
// Inject offender into the lock-free fast-path map, strictly bounded by capacity
if fastPenaltyBoxCount.Load() < int32(cfg.Server.RateLimit.MaxTrackedIPs) {
fastEntry := &fastBanEntry{expires: banUntil}
fastEntry.lastLog.Store(now)
// LoadOrStore guarantees we only increment if we actually contributed the record
if _, loaded := fastPenaltyBox.LoadOrStore(key, fastEntry); !loaded {
fastPenaltyBoxCount.Add(1)
}
}
log.Printf("[RATELIMIT] SECURITY: Client/Subnet %s instantly blackholed for %d mins (Strike threshold reached)", key, cfg.Server.RateLimit.PenaltyBox.BanDurationMin)
return false, true
}
}
// The token bucket is exhausted. We will drop the query.
// To provide operational visibility without causing a log-storm (I/O exhaustion)
// during an active reflection/flood attack, we debounce the rate-limit log.
if now-bucket.lastLog > int64(10*time.Second) {
bucket.lastLog = now
log.Printf("[RATELIMIT] SECURITY: Client/Subnet %s exceeded limit of %.1f QPS (Burst: %.0f). Queries dropped.", key, rlQPS, rlBurst)
}
return false, false
}
// PenalizeClient is a direct-action hook allowing protocol handlers to immediately
// strike or instantly-ban IPs for extreme offenses (e.g., malformed packets, fuzzing).
// A severity of -1 issues an instant ban, regardless of current strike count.
func PenalizeClient(clientIP string, addr netip.Addr, severity int) {
if !cfg.Server.RateLimit.Enabled || !rlPenaltyEnabled || clientIP == "" {
return
}
key := groupIPFast(clientIP, addr)
// Exemptions are immune to direct penalties
if isExempt(addr) {
return
}
shard := getRLShard(key)
now := time.Now().UnixNano()
shard.Lock()
defer shard.Unlock()
bucket, exists := shard.buckets[key]
if !exists {
// Bound constraints identically to normal Allow logic
if len(shard.buckets) >= rlMaxPerShard {
for k := range shard.buckets {
delete(shard.buckets, k)
break
}
}
bucket = &rlBucket{
tokens: rlBurst,
lastRefill: now,
}
shard.buckets[key] = bucket
}
// Ignore if they are already serving a ban to prevent log spam
if bucket.bannedUntil > now {
return
}
if severity == -1 {
bucket.strikes = rlStrikeThreshold // Instant Ban Trigger
} else {
bucket.strikes += severity
}
if bucket.strikes >= rlStrikeThreshold {
banUntil := now + rlBanDurationNanos
bucket.bannedUntil = banUntil
bucket.lastLog = now
// Inject offender into the lock-free fast-path map, strictly bounded by capacity
if fastPenaltyBoxCount.Load() < int32(cfg.Server.RateLimit.MaxTrackedIPs) {
fastEntry := &fastBanEntry{expires: banUntil}
fastEntry.lastLog.Store(now)
if _, loaded := fastPenaltyBox.LoadOrStore(key, fastEntry); !loaded {
fastPenaltyBoxCount.Add(1)
}
}
log.Printf("[RATELIMIT] SECURITY: Client/Subnet %s instantly blackholed for %d mins (Severe Infraction/Malformed Traffic)", key, cfg.Server.RateLimit.PenaltyBox.BanDurationMin)
}
}
// runRateLimitSweeper periodically purges buckets that have been inactive
// long enough to fully refill their burst capacity, preventing memory leaks
// from short-lived client IPs or UDP spoofing scans.
func runRateLimitSweeper() {
// Calculate the exact time required to completely refill the burst bucket.
// We add 1 minute of margin to avoid premature eviction edge cases.
refillDurationSecs := rlBurst / rlQPS
idleHorizon := time.Duration(refillDurationSecs+60) * time.Second
ticker := time.NewTicker(2 * time.Minute)
defer ticker.Stop()
for range ticker.C {
now := time.Now().UnixNano()
horizonNanos := now - idleHorizon.Nanoseconds()
// Prune the lock-free fast-path map natively
fastPenaltyBox.Range(func(key, value any) bool {
if now > value.(*fastBanEntry).expires {
if _, loaded := fastPenaltyBox.LoadAndDelete(key); loaded {
fastPenaltyBoxCount.Add(-1)
}
}
return true
})
// Prune the sharded buckets
for i := 0; i < rlShardCount; i++ {
shard := rlShards[i]
shard.Lock()
for k, bucket := range shard.buckets {
// Evict the bucket ONLY if it's inactive AND its Penalty Box ban has fully expired
if bucket.lastRefill < horizonNanos && bucket.bannedUntil < now {
delete(shard.buckets, k)
}
}
shard.Unlock()
}
}
}