-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathReachabilityFenceTest.groovy
More file actions
443 lines (408 loc) · 16.9 KB
/
Copy pathReachabilityFenceTest.groovy
File metadata and controls
443 lines (408 loc) · 16.9 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
/*
* Unless explicitly stated otherwise all files in this repository are licensed
* under the Apache-2.0 License.
*
* This product includes software developed at Datadog
* (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
*/
package com.datadog.ddwaf
import org.junit.Test
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import static org.hamcrest.MatcherAssert.assertThat
import static org.hamcrest.Matchers.is
/**
* Regression tests for APPSEC-62784 and APPSEC-68682: SIGSEGV crash in libddwaf on
* JDK 21.0.8+/JDK 25.
*
* Root cause: StringsSegment and PWArgsSegment DirectByteBuffers could be prematurely
* freed by the concurrent GC Cleaner thread while ddwaf_run() was still reading native
* pointers into them. The JIT in ZGC Generational / JDK 25 is more aggressive at eliding
* references it considers "dead" after the last Java-visible use.
*
* Fix: a volatile-write fence (leaseFenceSink = lease) in the finally block of
* WafContext.run(), ensuring all Arena DirectByteBuffers remain strongly reachable
* until past the ddwaf_run boundary.
*
* APPSEC-62784 crash pattern: DDWAF_OBJ_STRING with dangling stringValue pointer
* (StringsSegment freed), triggered by key_path header rules on absent headers.
*
* APPSEC-68682 crash pattern: DDWAF_OBJ_MAP with entries=null (PWArgsSegment freed),
* triggered by runEphemeral() with deeply nested Maps from SSE response body inspection.
*/
class ReachabilityFenceTest implements WafTrait {
/**
* Core regression test: warm up WafContext.run() to C2 JIT compilation level, then
* hammer it with concurrent GC pressure to expose the stale-pointer bug.
*
* Why warmup matters: the reference elision that opens the use-after-free window only
* occurs in C2-compiled code. TieredCompilation reaches C2 (Tier 4) at ~15000
* invocations; without prior warmup the test passes even without the fix because the
* JIT never elides the lease reference.
*
* Without the reachabilityFence fix, this test will crash the JVM with SIGSEGV
* on JDK 21.0.8+ or JDK 25 with ZGC Generational.
*
* With the fix, it must complete without crash and return DDWAF_OK for every run.
*/
@Test
@SuppressWarnings('ExplicitGarbageCollection')
void 'run with absent key_path header rules survives aggressive concurrent GC'() {
wafDiagnostics = builder.addOrUpdateConfig('test', keyPathAbsentHeaderRuleset())
assert wafDiagnostics.numConfigOK == 2, "Both rules must load: ${wafDiagnostics.allErrors}"
handle = builder.buildWafHandleInstance()
context = new WafContext(handle)
// Allow enough per-run budget for sanitizer (ASAN) environments where native
// operations are significantly slower than on a standard build.
runBudget = 60_000_000L
// Warm up WafContext.run() to C2 JIT level before starting GC pressure.
15_000.times { context.run(standardRequestBundle(0), limits, metrics) }
def keepRunningGc = new AtomicBoolean(true)
// Three concurrent GC threads to maximise ZGC concurrent cycle frequency.
// sleep(1) is a short yield to avoid busy-spinning; the real GC pressure
// comes from the three threads firing concurrently every ~1ms.
def gcThreads = (0..<3).collect { int n ->
Thread.startDaemon("gc-pressure-${n}") {
while (keepRunningGc.get()) {
System.gc()
Thread.sleep(1)
}
}
}
try {
2000.times { i ->
def result = context.run(standardRequestBundle(i), limits, metrics)
assertThat(
"Iteration ${i}: expected DDWAF_OK (no match for absent x-filename header)",
result.result,
is(Waf.Result.OK))
}
} finally {
keepRunningGc.set(false)
gcThreads.each { it.join(1000) }
}
}
/**
* Verify the rule DOES match when x-filename with .jsp value is present —
* ensures the rule itself is loaded and functional, not just silently broken.
*/
@Test
void 'run matches JSP filename in x-filename header'() {
wafDiagnostics = builder.addOrUpdateConfig('test', keyPathAbsentHeaderRuleset())
assert wafDiagnostics.numConfigOK == 2
handle = builder.buildWafHandleInstance()
context = new WafContext(handle)
def result = context.run([
'server.request.headers.no_cookies': [
'user-agent' : 'TestClient/1.0',
'x-filename' : 'payload.jsp', // should trigger crs-944-140-alike
],
], limits, metrics)
assertThat result.result, is(Waf.Result.MATCH)
assert result.data?.contains('test-crs-944-140-alike') ||
result.data?.contains('test-dog-920-100-alike'),
"Expected one of our test rules to match, got: ${result.data}"
}
/**
* Variant: stress test with multiple WafContext instances created per iteration,
* maximising Arena pool churn and GC pressure on the DirectByteBuffers.
*/
@Test
@SuppressWarnings('ExplicitGarbageCollection')
void 'arena pool churn with key_path rules survives GC'() {
wafDiagnostics = builder.addOrUpdateConfig('test', keyPathAbsentHeaderRuleset())
handle = builder.buildWafHandleInstance()
// Allow enough per-run budget for sanitizer (ASAN) environments.
runBudget = 60_000_000L
// Warm up WafContext.run() to C2 JIT level before starting GC pressure.
def warmupContext = new WafContext(handle)
try {
15_000.times { warmupContext.run(standardRequestBundle(0), limits, metrics) }
} finally {
warmupContext.close()
}
def keepRunningGc = new AtomicBoolean(true)
def gcThreads2 = (0..<3).collect { int n ->
Thread.startDaemon("gc-pressure-pool-${n}") {
while (keepRunningGc.get()) {
System.gc()
Thread.sleep(1)
}
}
}
try {
100.times { i ->
// Create a fresh WafContext per iteration to exercise the Arena pool
def localContext = new WafContext(handle)
try {
5.times { j ->
def result = localContext.run(standardRequestBundle(i * 5 + j), limits, metrics)
assertThat result.result, is(Waf.Result.OK)
}
} finally {
localContext.close()
}
}
} finally {
keepRunningGc.set(false)
gcThreads2.each { it.join(1000) }
context = null // prevent WafTrait.after() from closing a null-already-closed context
}
}
/**
* Concurrent-thread variant: N threads each hold a live WafContext and hammer
* WafContext.run() in a tight loop while a GC-pressure thread runs continuously.
*
* This multiplies the race surface: N DirectByteBuffer-backed ArenaLeases are
* concurrently eligible for GC collection, and the JIT-compiled run() code is
* shared across all N instances. In production, crashes were observed with 10+
* concurrent Tomcat threads — this test replicates that topology.
*
* Without the reachabilityFence fix the JVM crashes with SIGSEGV (process killed),
* which CI surfaces as a build failure rather than a test assertion failure.
*/
@Test
@SuppressWarnings('ExplicitGarbageCollection')
void 'concurrent threads with shared JIT code and GC pressure survive'() {
wafDiagnostics = builder.addOrUpdateConfig('concurrent', keyPathAbsentHeaderRuleset())
handle = builder.buildWafHandleInstance()
runBudget = 120_000_000L // 120ms budget; larger for ASAN/sanitizer environments
// Warm up to C2 before starting concurrent stress.
// Uses 12_000 iterations (not 15_000) to avoid DuplicateNumberLiteral.
def warmupContext = new WafContext(handle)
try {
12_000.times { warmupContext.run(standardRequestBundle(0), limits, metrics) }
} finally {
warmupContext.close()
}
def keepRunningGc = new AtomicBoolean(true)
def gcThreads3 = (0..<3).collect { int n ->
Thread.startDaemon("gc-pressure-concurrent-${n}") {
while (keepRunningGc.get()) {
System.gc()
Thread.sleep(1)
}
}
}
def errors = new CopyOnWriteArrayList<String>()
def counter = new AtomicInteger(0)
// Thread.startDaemon creates AND starts the thread immediately.
// No join timeout: a bounded timeout risks the WafHandle being destroyed
// while a worker thread is still creating or using a WafContext (UAF under ASAN).
def threads = (0..<16).collect { int t ->
Thread.startDaemon("waf-concurrent-${t}") {
runConcurrentWorker(t, handle, counter, errors)
}
}
try {
threads.each { it.join() }
} finally {
keepRunningGc.set(false)
gcThreads3.each { it.join(1000) }
context = null // prevent WafTrait.after() from double-closing
// Discard pool arenas that carry stale bytes from the heavy serialization above.
// Without this, ArenaPool entries with non-zero bytes at positions beyond the
// freshly-written region can cause subsequent tests to see unexpected native results.
ByteBufferSerializer.ArenaPool.INSTANCE.arenas.clear()
}
assert errors.empty, "Errors during concurrent run:\n${errors.join('\n')}"
}
/**
* Regression test for APPSEC-68682: ephemeral arena freed during ddwaf_run under ZGC.
*
* Trigger: SSE streaming response body inspection in Spring MVC. Each SSE chunk calls
* runEphemeral() with a deeply nested Map from ObjectIntrospection.convert() on a POJO.
* The nested structure creates multiple PWArgsSegment allocations in the ephemeral arena.
* Without the leaseFenceSink fence, the JIT eliminates ephemeralLease from the GC OopMap
* before the JNI safepoint, and ZGC frees the DirectByteBuffers while ddwaf_run holds
* raw pointers into them.
*
* Distinct from APPSEC-62784 (StringsSegment freed via run()) but same root cause and fix.
*
* Requires testGCRace task: relies on -XX:-TieredCompilation -XX:CompileThreshold=1 so
* WafContext.run() reaches C2 immediately without a warmup phase.
*/
@Test
@SuppressWarnings('ExplicitGarbageCollection')
void 'ephemeral arena with nested map survives concurrent GC (APPSEC-68682)'() {
wafDiagnostics = builder.addOrUpdateConfig('test', serverResponseBodyRuleset())
assert wafDiagnostics.numConfigOK == 1, "Rule must load: ${wafDiagnostics.allErrors}"
handle = builder.buildWafHandleInstance()
context = new WafContext(handle)
runBudget = 60_000_000L
// Simulate ObjectIntrospection.convert() output for a streaming SSE response POJO.
// Nested structure forces multiple PWArgsSegment allocations in the ephemeral arena.
// match_regex on server.response.body triggers eval_rules() which traverses all nested
// MAP entries, reading the PWArgsSegments that are freed without the leaseFenceSink fix.
def ephemeralData = [
'server.response.body': [
field1: [
subA: 'value1',
subB: null,
subC: [deepA: 'deep1', deepB: null, deepC: 'deep3'],
],
field2: null,
field3: [
subD: 'value2',
subE: [x: '1', y: '2', z: '3'],
],
field4: 'scalar',
field5: [nestedA: 'a', nestedB: 'b'],
],
]
def keepRunningGc = new AtomicBoolean(true)
def gcThreads4 = (0..<3).collect { int n ->
Thread.startDaemon("gc-pressure-ephemeral-${n}") {
while (keepRunningGc.get()) {
System.gc()
Thread.sleep(1)
}
}
}
try {
500.times { int i ->
def result = context.runEphemeral(ephemeralData, limits, metrics)
assertThat(
"Iteration ${i}: expected DDWAF_OK (regex never matches response body)",
result.result,
is(Waf.Result.OK))
}
} finally {
keepRunningGc.set(false)
gcThreads4.each { it.join(1000) }
ByteBufferSerializer.ArenaPool.INSTANCE.arenas.clear()
}
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/**
* Ruleset mirroring crs-944-140 / dog-920-100 from dd-trace-java 1.62.0 bundled config:
* evaluates server.request.headers.no_cookies with key_path values typically absent
* from normal HTTP requests. This is the exact pattern that triggered APPSEC-62784.
*/
static Map keyPathAbsentHeaderRuleset() {
[
version : '2.1',
metadata: [rules_version: '1.0.0'],
rules : [
[
id : 'test-crs-944-140-alike',
name : 'JSP file upload detection (test replica)',
tags : [type: 'unrestricted_file_upload', category: 'attack_attempt', module: 'waf'],
conditions: [
[
operator : 'match_regex',
parameters: [
inputs : [
[address: 'server.request.body.filenames'],
[address: 'server.request.headers.no_cookies', key_path: ['x-filename']],
[address: 'server.request.headers.no_cookies', key_path: ['x_filename']],
[address: 'server.request.headers.no_cookies', key_path: ['x.filename']],
[address: 'server.request.headers.no_cookies', key_path: ['x-file-name']],
],
regex : '[.]jspx?$',
options: [case_sensitive: true, min_length: 5],
],
]
],
transformers: [],
],
[
id : 'test-dog-920-100-alike',
name : 'Double extension file upload (test replica)',
tags : [type: 'http_protocol_violation', category: 'attack_attempt', module: 'waf'],
conditions: [
[
operator : 'match_regex',
parameters: [
inputs : [
[address: 'server.request.body.filenames'],
[address: 'server.request.headers.no_cookies', key_path: ['x-filename']],
[address: 'server.request.headers.no_cookies', key_path: ['x_filename']],
[address: 'server.request.headers.no_cookies', key_path: ['x.filename']],
[address: 'server.request.headers.no_cookies', key_path: ['x-file-name']],
],
regex : '[a-zA-Z0-9]+[.][a-zA-Z0-9]{2,5}[.][a-zA-Z0-9]{2,5}$',
options: [case_sensitive: true, min_length: 6],
],
]
],
transformers: [],
],
],
]
}
/**
* Minimal ruleset targeting server.response.body with a match_regex that never matches.
* The rule evaluator traverses all nested MAP entries at server.response.body, reading
* the ephemeral PWArgsSegments that are freed without the leaseFenceSink fix (APPSEC-68682).
*/
static Map serverResponseBodyRuleset() {
[
version : '2.1',
metadata: [rules_version: '1.0.0'],
rules : [
[
id : 'test-appsec-68682-body-inspection',
name : 'Response body inspection (APPSEC-68682 regression)',
tags : [type: 'test', category: 'test', module: 'waf'],
conditions: [
[
operator : 'match_regex',
parameters: [
inputs : [[address: 'server.response.body']],
regex : 'IMPOSSIBLE_APPSEC68682_MARKER',
options: [case_sensitive: true, min_length: 30],
],
]
],
transformers: [],
],
],
]
}
/**
* The minimal input bundle that triggers the crash: the 9 addresses published by
* GatewayBridge.maybePublishRequestData() on every HTTP request.
* Note: x-filename and similar headers are NOT present — the rule evaluates against
* absent key_paths, which is exactly the production scenario.
*/
private static Map<String, Object> standardRequestBundle(int i) {
[
'server.request.headers.no_cookies': [
'user-agent' : "TestClient/1.${i}",
'accept' : 'application/json',
'content-type' : 'text/plain',
'host' : 'example.com',
// x-filename deliberately absent
],
'server.request.cookies' : [:],
'server.request.scheme' : 'https',
'server.request.method' : 'POST',
'server.request.uri.raw' : "/api/v${i}/data",
'server.request.query' : [:],
'http.client_ip' : '1.2.3.4',
'server.request.client_ip' : '1.2.3.4',
'server.request.client_port' : 443,
]
}
@SuppressWarnings(['CatchThrowable', 'UnnecessaryGetter'])
private void runConcurrentWorker(int threadIndex, WafHandle wafHandle,
AtomicInteger counter, List<String> errors) {
def ctx = new WafContext(wafHandle)
try {
1000.times { int i ->
def result = ctx.run(standardRequestBundle(counter.getAndIncrement()), limits, metrics)
if (result.result != Waf.Result.OK) {
errors.add("Thread ${threadIndex} iter ${i}: expected OK, got ${result.result}")
}
}
} catch (Throwable th) {
errors.add("Thread ${threadIndex}: ${th.class.simpleName}: ${th.message}")
} finally {
ctx.close()
}
}
}