Commit 0cc69c4
Fix WafContext TOCTOU race resurrecting orphaned native contexts (#12024)
Fix WafContext TOCTOU race resurrecting orphaned native contexts (APPSEC-69085)
getOrCreateWafContext() created a new native WafContext whenever the
wafContext field was null, without checking wafContextClosed. A late/async
RASP callback (e.g. reactive JDBC/r2dbc, async HTTP client) could race with
closeWafContext() and resurrect a brand-new context on an already-finished
request, which was never closed and leaked native memory.
Move the wafContextClosed check to the top of the existing synchronized
block in getOrCreateWafContext, returning null if the context is already
closed. WAFModule.doRunWaf and WAFDataCallback.onDataAvailable now treat a
null WafContext as a skip instead of dereferencing it.
review: pre-PR checks
- Set wafContextClosed unconditionally in closeWafContext(), even if no
WafContext was ever created, preventing a late/async caller from
resurrecting an orphaned native context on an already-finished request
- Add regression test for close-before-first-use ordering
- Add WAFModule-level test covering the doRunWaf null-skip path when the
context is closed concurrently
- Fix native WafContext leaks in 4 stub sites of
'reloading rules clears waf data and rule toggling' by wiring
ctx.closeWafContext() to actually close the ephemeral context, and shadow
the class-level wafContext field with a local to avoid a cleanup() double-close
- Remove premature closeWafContext() calls in two fingerprint tests that
relied on the pre-fix silent no-op behavior
- Move the WAF-context-closed-race metric test from Groovy to JUnit5 Java
Merge branch 'master' into fix-wafcontext-race
Address review comments: fallback close path and RASP eval double-count
- AppSecRequestContext.close() now always calls closeWafContext(), even
when no WafContext was ever created for the request. The previous
wafContext != null guard meant the fallback path (missed request-end
event) never set wafContextClosed for requests that hadn't run the WAF
yet, leaving the same TOCTOU window this PR fixes.
- WAFModule's null-skip branch no longer also reports rasp.rule.skipped:
raspRuleEval() is already counted before doRunWaf() runs, so counting
raspRuleSkipped() too double-counts the same callback as both evaluated
and skipped, unlike the isWafContextClosed() fast path which only
attempted eval when this branch is *not* taken.
Merge branch 'master' into fix-wafcontext-race
Skip fast-path in closeWafContext and fix telemetry for closed-context race
Add a fast-path return in closeWafContext() for redundant close() calls,
and stop misclassifying a WafContext closed concurrently between
getOrCreateWafContext() and run() as a real WAF error - it's the same
benign race already tracked via wafContextClosedRace().
Merge branch 'master' into fix-wafcontext-race
Merge branch 'master' into fix-wafcontext-race
Merge branch 'master' into fix-wafcontext-race
Co-authored-by: devflow.devflow-routing-intake <devflow.devflow-routing-intake@kubernetes.us1.ddbuild.io>1 parent 493dd2f commit 0cc69c4
7 files changed
Lines changed: 516 additions & 47 deletions
File tree
- dd-java-agent/appsec/src
- main/java/com/datadog/appsec
- ddwaf
- gateway
- test
- groovy/com/datadog/appsec/ddwaf
- java/com/datadog/appsec
- ddwaf
- gateway
- internal-api/src
- main/java/datadog/trace/api/telemetry
- test/java/datadog/trace/api/telemetry
Lines changed: 21 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
312 | 312 | | |
313 | 313 | | |
314 | 314 | | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
315 | 323 | | |
316 | 324 | | |
317 | 325 | | |
| |||
322 | 330 | | |
323 | 331 | | |
324 | 332 | | |
325 | | - | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
326 | 340 | | |
| 341 | + | |
327 | 342 | | |
328 | | - | |
329 | 343 | | |
330 | 344 | | |
331 | 345 | | |
| |||
560 | 574 | | |
561 | 575 | | |
562 | 576 | | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
563 | 582 | | |
564 | 583 | | |
565 | 584 | | |
| |||
Lines changed: 42 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
353 | 353 | | |
354 | 354 | | |
355 | 355 | | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
356 | 366 | | |
357 | 367 | | |
358 | | - | |
359 | | - | |
360 | | - | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
361 | 372 | | |
362 | | - | |
363 | | - | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
364 | 380 | | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
371 | | - | |
| 381 | + | |
| 382 | + | |
372 | 383 | | |
373 | | - | |
| 384 | + | |
374 | 385 | | |
| 386 | + | |
375 | 387 | | |
376 | | - | |
377 | 388 | | |
378 | 389 | | |
379 | 390 | | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
389 | 405 | | |
390 | 406 | | |
391 | 407 | | |
| |||
720 | 736 | | |
721 | 737 | | |
722 | 738 | | |
723 | | - | |
724 | 739 | | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
725 | 743 | | |
726 | 744 | | |
727 | 745 | | |
| |||
Lines changed: 28 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
280 | 280 | | |
281 | 281 | | |
282 | 282 | | |
283 | | - | |
| 283 | + | |
284 | 284 | | |
285 | 285 | | |
286 | 286 | | |
| |||
304 | 304 | | |
305 | 305 | | |
306 | 306 | | |
307 | | - | |
| 307 | + | |
308 | 308 | | |
309 | 309 | | |
310 | 310 | | |
| |||
356 | 356 | | |
357 | 357 | | |
358 | 358 | | |
359 | | - | |
| 359 | + | |
360 | 360 | | |
361 | 361 | | |
362 | 362 | | |
| |||
376 | 376 | | |
377 | 377 | | |
378 | 378 | | |
379 | | - | |
| 379 | + | |
380 | 380 | | |
381 | 381 | | |
382 | 382 | | |
| |||
428 | 428 | | |
429 | 429 | | |
430 | 430 | | |
431 | | - | |
| 431 | + | |
432 | 432 | | |
433 | 433 | | |
434 | 434 | | |
| |||
450 | 450 | | |
451 | 451 | | |
452 | 452 | | |
453 | | - | |
| 453 | + | |
454 | 454 | | |
455 | 455 | | |
456 | 456 | | |
| |||
1074 | 1074 | | |
1075 | 1075 | | |
1076 | 1076 | | |
| 1077 | + | |
1077 | 1078 | | |
1078 | 1079 | | |
1079 | 1080 | | |
| |||
1116 | 1117 | | |
1117 | 1118 | | |
1118 | 1119 | | |
1119 | | - | |
| 1120 | + | |
1120 | 1121 | | |
1121 | 1122 | | |
1122 | | - | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
1123 | 1126 | | |
1124 | 1127 | | |
1125 | 1128 | | |
| |||
1132 | 1135 | | |
1133 | 1136 | | |
1134 | 1137 | | |
1135 | | - | |
| 1138 | + | |
1136 | 1139 | | |
1137 | 1140 | | |
1138 | 1141 | | |
1139 | | - | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
1140 | 1145 | | |
1141 | 1146 | | |
1142 | 1147 | | |
| |||
1151 | 1156 | | |
1152 | 1157 | | |
1153 | 1158 | | |
1154 | | - | |
| 1159 | + | |
1155 | 1160 | | |
1156 | 1161 | | |
1157 | 1162 | | |
1158 | 1163 | | |
1159 | 1164 | | |
1160 | | - | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
1161 | 1168 | | |
1162 | 1169 | | |
1163 | 1170 | | |
| |||
1174 | 1181 | | |
1175 | 1182 | | |
1176 | 1183 | | |
1177 | | - | |
| 1184 | + | |
1178 | 1185 | | |
1179 | 1186 | | |
1180 | | - | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
1181 | 1190 | | |
1182 | 1191 | | |
1183 | 1192 | | |
| |||
1490 | 1499 | | |
1491 | 1500 | | |
1492 | 1501 | | |
1493 | | - | |
| 1502 | + | |
1494 | 1503 | | |
1495 | 1504 | | |
1496 | 1505 | | |
| |||
1523 | 1532 | | |
1524 | 1533 | | |
1525 | 1534 | | |
1526 | | - | |
| 1535 | + | |
1527 | 1536 | | |
1528 | 1537 | | |
1529 | 1538 | | |
| |||
1538 | 1547 | | |
1539 | 1548 | | |
1540 | 1549 | | |
1541 | | - | |
1542 | 1550 | | |
1543 | 1551 | | |
1544 | 1552 | | |
| |||
1567 | 1575 | | |
1568 | 1576 | | |
1569 | 1577 | | |
1570 | | - | |
1571 | 1578 | | |
1572 | 1579 | | |
1573 | 1580 | | |
| |||
1905 | 1912 | | |
1906 | 1913 | | |
1907 | 1914 | | |
1908 | | - | |
| 1915 | + | |
1909 | 1916 | | |
1910 | 1917 | | |
1911 | 1918 | | |
| |||
1923 | 1930 | | |
1924 | 1931 | | |
1925 | 1932 | | |
1926 | | - | |
| 1933 | + | |
1927 | 1934 | | |
1928 | 1935 | | |
1929 | 1936 | | |
| |||
1942 | 1949 | | |
1943 | 1950 | | |
1944 | 1951 | | |
1945 | | - | |
| 1952 | + | |
1946 | 1953 | | |
1947 | 1954 | | |
1948 | 1955 | | |
| |||
0 commit comments