include/hyperman/hm_sa_abuse.h

filter: Statementstmt 94.6% 52 / 56 (1 uncoverable) Branchbran 83.3% 50 / 72 (10 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.2% 102 / 128 (11 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hyperman/hm_workerhook.h ↑ Summary include/hyperman/hm_sa_bus.h »
1 /* hm_sa_abuse.h - the abuse controls on Hyperman's Shared::Arena: an IP
2 * denylist checked at accept and fixed-window rate counters checked per
3 * request, on two Map tenants of the one arena hm_sa.h creates.
4 *
5 * Perl-free. The includer defines `HM_SA` (the sa_abi table, or NULL) and
6 * `hm_sa_region` (the arena, or NULL) before this header: hm_sa.h does in
7 * the server, and bench/arena_bench.c does with no interpreter at all, which
8 * is how the accept path is measured.
9 *
10 * THE CONTRACT, which every consumer relies on and 00-overview of the port
11 * plan spells out: deny_add/remove/check take IP strings; ratelimit_hit is a
12 * FIXED WINDOW - at most `limit` hits per key in each `window`-second
13 * wall-clock slot, `reset` the epoch second the slot rolls, `remaining` never
14 * below zero, `limit <= 0` unlimited with remaining -1 - and everything
15 * FAILS OPEN: no table, no arena, a full map or a contended stripe answer
16 * "not denied" and "allowed". The limiter must never be the reason a good
17 * request is refused.
18 *
19 * ---- the denylist ------------------------------------------------------------
20 *
21 * A Map keyed by the IP string with an empty value and a per-key deadline.
22 * The check is a fetch with a zero-length buffer: a hash, a probe, a seqlock
23 * read, and the clock only for an entry that carries a deadline. The empty
24 * home slot, which is nearly every accept, is a hash and one load - and no
25 * time(2) per connection, which the old table paid unconditionally.
26 *
27 * ---- the fixed window --------------------------------------------------------
28 *
29 * A Map keyed by the 64-bit FNV of the caller's opaque bytes, exactly the key
30 * the old table used, so the collision set is unchanged and a key may be any
31 * length with NULs in it. The first hit in a window CREATES the counter with
32 * a deadline at the window's END; map_incr_ttl sets a deadline only on a
33 * create or a reset, so later hits inside the window count against the same
34 * deadline and the window does not slide. Every process, whichever wall-clock
35 * second it read, agrees on when the counter lapses.
36 *
37 * Over capacity the map refuses the new key and the request is allowed. The
38 * old table evicted the key's home slot instead, resetting a live counter.
39 * Both leak looser, never tighter; this one no longer disturbs anybody else's.
40 */
41  
42 #ifndef HM_SA_ABUSE_H
43 #define HM_SA_ABUSE_H
44  
45 #include <string.h>
46 #include <time.h>
47 #include <stdint.h>
48 #include <stddef.h>
49 #ifdef _WIN32
50 # include <windows.h>
51 #else
52 # include <sys/time.h>
53 #endif
54  
55 #include "sa_abi.h"
56 #include "hm_atomic.h"
57  
58 #define HM_SA_DENY_CAP 1024
59 #define HM_SA_RATE_CAP 4096
60 #define HM_SA_DENY_SLOT 96 /* 39 of header, room for an IPv6 string */
61 #define HM_SA_RATE_SLOT 64 /* 39 of header, 8 of key, 8 of counter */
62  
63 static sa_hash *hm_sa_deny = NULL; /* NULL = fail open */
64 static sa_hash *hm_sa_rate = NULL;
65  
66 /* The same wall clock the map's deadlines are on, in milliseconds. sa_time.h
67 * is not an installed header, so this is its twin, coarse clock included:
68 * on Linux CLOCK_REALTIME_COARSE is a vDSO read of the last tick with no
69 * counter access, a quarter of gettimeofday's cost, and a tick's granularity
70 * on a deadline a second wide is nothing. Makefile.PL probes for it by
71 * running it (HM_HAVE_COARSE_CLOCK); the harness inherits Shared::Arena's
72 * verdict (SA_HAVE_COARSE_CLOCK). */
73 15 static uint64_t hm_sa_now_ms(void) {
74 #ifdef _WIN32
75 FILETIME ft;
76 ULARGE_INTEGER u;
77 GetSystemTimeAsFileTime(&ft);
78 u.LowPart = ft.dwLowDateTime;
79 u.HighPart = ft.dwHighDateTime;
80 return (uint64_t)((u.QuadPart - 116444736000000000ULL) / 10000ULL);
81 #else
82 struct timeval tv;
83 # if defined(HM_HAVE_COARSE_CLOCK) || defined(SA_HAVE_COARSE_CLOCK)
84 struct timespec ts;
85 /*
86 # uncoverable branch false
87 # uncoverable condition right count:1 */
88 15 ▶ if (clock_gettime(CLOCK_REALTIME_COARSE, &ts) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (clock_gettime(CLOCK_REALTIME_COARSE, &ts) == 0) 15 0 15
89 15 return (uint64_t)ts.tv_sec * 1000ULL + (uint64_t)(ts.tv_nsec / 1000000);
90 # endif
91 0 gettimeofday(&tv, NULL); /* # uncoverable statement class:ignore_covered_err */
92 0 return (uint64_t)tv.tv_sec * 1000ULL + (uint64_t)(tv.tv_usec / 1000); /* # uncoverable statement class:ignore_covered_err */
93 #endif
94 }
95  
96 /* Open both maps in the arena. Idempotent; zero caps take the defaults.
97 * Returns 0, or -1 with *which naming the tenant that could not be carved
98 * ("deny" or "rate"), so the caller can say so. Both stay NULL then: the
99 * controls fail open together rather than one of them half working. */
100 97 static int hm_sa_abuse_open(unsigned deny_cap, unsigned rate_cap,
101 const char **which, int *err) {
102 sa_hash *d, *r;
103 97 int e = 0;
104 97 ▶ if (which) *which = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (which) *which = NULL; 96 1 97
105 97 ▶ if (err) *err = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = 0; 96 1 97
106 97 ▶ if (!HM_SA || !hm_sa_region || hm_sa_deny) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!HM_SA || !hm_sa_region || hm_sa_deny) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 94 3 97
if (!HM_SA || !hm_sa_region || hm_sa_deny) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 94 0 94
if (!HM_SA || !hm_sa_region || hm_sa_deny) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 13 81 94
107 81 ▶ if (!deny_cap) deny_cap = HM_SA_DENY_CAP;
Branch: 1 branch
BranchTrueFalseTotal
if (!deny_cap) deny_cap = HM_SA_DENY_CAP; 80 1 81
108 /*
109 # uncoverable branch false class:ignore_covered_err
110 # uncoverable condition right count:1 class:ignore_covered_err */
111 81 ▶ if (!rate_cap) rate_cap = HM_SA_RATE_CAP;
Branch: 1 branch
BranchTrueFalseTotal
if (!rate_cap) rate_cap = HM_SA_RATE_CAP; 81 0 81
112 81 d = HM_SA->map_open(hm_sa_region, "deny", 4, deny_cap, HM_SA_DENY_SLOT, &e);
113 /*
114 # uncoverable condition right count:2 class:ignore_covered_err
115 # uncoverable condition right count:3 class:ignore_covered_err */
116 81 ▶ if (!d) { if (which) *which = "deny"; if (err) *err = e; return -1; }
Branch: 3 branches
BranchTrueFalseTotal
if (!d) { if (which) *which = "deny"; if (err) *err = e; return -1; } 1 80 81
if (!d) { if (which) *which = "deny"; if (err) *err = e; return -1; } 1 0 1
if (!d) { if (which) *which = "deny"; if (err) *err = e; return -1; } 1 0 1
117 80 r = HM_SA->map_open(hm_sa_region, "rate", 4, rate_cap, HM_SA_RATE_SLOT, &e);
118 80 ▶ if (!r) {
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { 1 79 80
119 1 HM_SA->map_release(d);
120 /*
121 # uncoverable branch false class:ignore_covered_err
122 # uncoverable condition right count:1 class:ignore_covered_err */
123 1 ▶ if (which) *which = "rate";
Branch: 1 branch
BranchTrueFalseTotal
if (which) *which = "rate"; 1 0 1
124 /*
125 # uncoverable branch false class:ignore_covered_err
126 # uncoverable condition right count:1 class:ignore_covered_err */
127 1 ▶ if (err) *err = e;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = e; 1 0 1
128 1 return -1;
129 }
130 79 hm_sa_deny = d;
131 79 hm_sa_rate = r;
132 79 return 0;
133 }
134  
135 1 static int hm_sa_abuse_live(void) { return hm_sa_deny != NULL; }
136  
137 /* ---- the denylist ------------------------------------------------------------ */
138  
139 /* 1 if ip is on the list and not lapsed, else 0. The accept path. */
140 981 static int hm_sa_deny_check(const char *ip) {
141 char none[1];
142 981 uint32_t vlen = 0;
143 981 ▶ if (!hm_sa_deny || !ip || !ip[0]) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!hm_sa_deny || !ip || !ip[0]) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 971 10 981
if (!hm_sa_deny || !ip || !ip[0]) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 971 0 971
if (!hm_sa_deny || !ip || !ip[0]) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 2 969 971
144 969 return HM_SA->map_fetch(hm_sa_deny, ip, (uint32_t)strlen(ip), none, 0, &vlen)
145 969 == SA_MAP_HIT;
146 }
147  
148 /* Add or refresh for ttl_secs (0 = until the arena goes). Silent when the
149 * map is full or the stripe is contended past its bound, as before. */
150 33 static void hm_sa_deny_add(const char *ip, long ttl_secs) {
151 /*
152 # uncoverable condition left count:2 class:ignore_covered_err
153 # uncoverable condition left count:3 class:ignore_covered_err */
154 33 ▶ if (!hm_sa_deny || !ip || !ip[0]) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!hm_sa_deny || !ip || !ip[0]) return; 31 2 33
if (!hm_sa_deny || !ip || !ip[0]) return; 31 0 31
if (!hm_sa_deny || !ip || !ip[0]) return; 0 31 31
155 34 ▶ (void)HM_SA->map_store_ttl(hm_sa_deny, ip, (uint32_t)strlen(ip), "", 0,
Branch: 1 branch
BranchTrueFalseTotal
(void)HM_SA->map_store_ttl(hm_sa_deny, ip, (uint32_t)strlen(ip), "", 0, 3 28 31
156 3 ttl_secs > 0 ? (uint64_t)ttl_secs * 1000u : 0);
157 }
158  
159 4 static void hm_sa_deny_remove(const char *ip) {
160 /*
161 # uncoverable condition left count:2 class:ignore_covered_err
162 # uncoverable condition left count:3 class:ignore_covered_err */
163 4 ▶ if (!hm_sa_deny || !ip || !ip[0]) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!hm_sa_deny || !ip || !ip[0]) return; 2 2 4
if (!hm_sa_deny || !ip || !ip[0]) return; 2 0 2
if (!hm_sa_deny || !ip || !ip[0]) return; 0 2 2
164 2 (void)HM_SA->map_delete(hm_sa_deny, ip, (uint32_t)strlen(ip));
165 }
166  
167 /* ---- the fixed window -------------------------------------------------------- */
168  
169 /* Count one hit against the opaque key under `limit` per `window` seconds.
170 * Returns 1 within the limit, 0 over. *remaining and *reset are filled when
171 * non-NULL, whichever way it went. limit <= 0 is unlimited. */
172 15 static int hm_sa_ratelimit_hit(const void *key, size_t klen,
173 long limit, long window,
174 long *remaining, long *reset) {
175 15 ▶ long w = window > 0 ? window : 60; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
long w = window > 0 ? window : 60; /* # uncoverable condition right count:1 class:ignore_covered_err */ 15 0 15
176 /* ONE clock read: the second the window is computed from and the
177 * millisecond the deadline is measured from are the same reading, so
178 * they cannot straddle a boundary, and a hit pays one clock here rather
179 * than time() and gettimeofday both. */
180 15 uint64_t now_ms = hm_sa_now_ms();
181 15 long now = (long)(now_ms / 1000u);
182 15 long wstart = now - (now % w);
183 15 uint64_t hkey, count = 0, end_ms, ttl_ms;
184 int rc;
185  
186 /*
187 # uncoverable branch false class:ignore_covered_err
188 # uncoverable condition right count:1 class:ignore_covered_err */
189 15 ▶ if (reset) *reset = wstart + w;
Branch: 1 branch
BranchTrueFalseTotal
if (reset) *reset = wstart + w; 15 0 15
190 15 ▶ if (limit <= 0) { if (remaining) *remaining = -1; return 1; } /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (limit <= 0) { if (remaining) *remaining = -1; return 1; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 12 15
if (limit <= 0) { if (remaining) *remaining = -1; return 1; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
191 12 ▶ if (!hm_sa_rate) { if (remaining) *remaining = limit - 1; return 1; } /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!hm_sa_rate) { if (remaining) *remaining = limit - 1; return 1; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 10 12
if (!hm_sa_rate) { if (remaining) *remaining = limit - 1; return 1; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
192  
193 10 hkey = hm_at_fnv(key, klen);
194 /*
195 # uncoverable branch true class:ignore_covered_err
196 # uncoverable condition left count:1 class:ignore_covered_err */
197 10 ▶ if (!hkey) hkey = 1; /* the old table's empty marker */
Branch: 1 branch
BranchTrueFalseTotal
if (!hkey) hkey = 1; /* the old table's empty marker */ 0 10 10
198  
199 /* The deadline is the window's END, so the first hit of a window sets
200 * what every later hit inherits. */
201 10 end_ms = (uint64_t)(wstart + w) * 1000u;
202 10 ▶ ttl_ms = end_ms > now_ms ? end_ms - now_ms : 1; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
ttl_ms = end_ms > now_ms ? end_ms - now_ms : 1; /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
203  
204 /* On the clock reading above: the map neither reads it again for the
205 * deadline it sets nor for the deadline it checks. */
206 10 rc = HM_SA->map_incr_at(hm_sa_rate, (const char *)&hkey, 8, 1, ttl_ms,
207 now_ms, &count);
208 /*
209 # uncoverable branch true
210 # uncoverable condition left count:1 class:ignore_covered_err */
211 10 ▶ if (rc != SA_MAP_STORE_OK) { /* full, contended, or not ours */
Branch: 1 branch
BranchTrueFalseTotal
if (rc != SA_MAP_STORE_OK) { /* full, contended, or not ours */ 0 10 10
212 /*
213 # uncoverable statement
214 # uncoverable branch true
215 # uncoverable branch false
216 # uncoverable condition left count:1
217 # uncoverable condition right count:1 */
218 0 ▶ if (remaining) *remaining = limit - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (remaining) *remaining = limit - 1; 0 0 0
219 0 return 1; /* # uncoverable statement */
220 }
221 /*
222 # uncoverable branch false class:ignore_covered_err
223 # uncoverable condition right count:1 class:ignore_covered_err */
224 10 ▶ if (remaining)
Branch: 1 branch
BranchTrueFalseTotal
if (remaining) 10 0 10
225 10 ▶ *remaining = count >= (uint64_t)limit ? 0 : limit - (long)count;
Branch: 1 branch
BranchTrueFalseTotal
*remaining = count >= (uint64_t)limit ? 0 : limit - (long)count; 6 4 10
226 10 return count <= (uint64_t)limit ? 1 : 0;
227 }
228  
229 #endif /* HM_SA_ABUSE_H */
« include/hyperman/hm_workerhook.h ↑ Summary include/hyperman/hm_sa_bus.h »