Commit e5e34bb
Add DoS protection against expansion attacks (Billion Laughs style) (#211)
* Add DoS protection against expansion attacks (Billion Laughs style)
Implement defense in depth against exponential expansion attacks that could
cause Fickling to hang or consume excessive memory:
1. Static pattern detection via ExpansionAttackAnalysis:
- Detects high GET/PUT ratio (>10x suspicious, >50x likely unsafe)
- Detects excessive DUP operations (>100 suspicious)
2. Runtime resource limits via InterpreterLimits:
- max_opcodes: 1,000,000
- max_stack_depth: 10,000
- max_memo_size: 100,000
- max_get_ratio: 50 (GETs per PUT)
3. New exception types:
- ResourceExhaustionError for limit violations
- ExpansionAttackError for expansion attack detection
4. Updated opcode classes to track GET/PUT operations:
- BinGet, LongBinGet, Get call track_get()
- BinPut, Put, LongBinPut, Memoize call track_put()
5. AnalysisContext catches ResourceExhaustionError and returns
LIKELY_OVERTLY_MALICIOUS severity instead of propagating exception
Fixes #111
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Mark test_cyclic_pickle_dos as expected failure
The cyclic AST recursion issue (#196) is being fixed separately in PR #213.
Mark this test as expected failure so PR #211 CI can pass.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Fix review issues in DoS protection implementation
- Move _check_limits() after opcode.run() so counters are current
- Add ResourceExhaustionError handling in Pickled.ast (returns empty AST)
- Broaden AnalysisContext.analyze() catch to handle ValueError,
IndexError, RecursionError from malformed pickles
- Handle put_count == 0 edge case in ExpansionAttackAnalysis
- Simplify combined indicators logic (remove redundant condition)
- Make InterpreterLimits frozen with __post_init__ validation
- Hardcode resource_type in ExpansionAttackError
- Use round() instead of int() for ratio in error messages
- Add ResourceExhaustionError handling in CLI decompile path
- Split resource limit tests per limit type (opcodes, stack, memo)
- Add InterpreterLimits validation test
- Remove @expectedfailure from test_cyclic_pickle_dos (now handled)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Fix failing linting
* Fix failing linting
* Make ExpansionAttackAnalysis thresholds configurable via __init__
Allows callers to override GET/PUT ratio and DUP count thresholds
without monkey-patching class attributes.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add minimal test cases for nested Billion Laughs expansion patterns
Reproduces globalLaughs.pt (DUP-based) and billionLaughsAlt.pkl
(memo-based) DoS patterns that evade current flat thresholds by
spreading operations across nested LIST layers.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Make resource exhaustion detection intentional via dedicated analysis
Previously detection depended on UnusedVariables accidentally
re-triggering the error. Now Pickled sets a flag and a dedicated
analysis checks it.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Potential fix for pull request finding 'Unused import'
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
* Potential fix for pull request finding 'Imprecise assert'
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
* Potential fix for pull request finding 'Imprecise assert'
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Thomas Chauchefoin <thomas.chauchefoin@trailofbits.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>1 parent 240f61e commit e5e34bb
File tree
6 files changed
+538
-10
lines changed- fickling
- test
6 files changed
+538
-10
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
13 | 18 | | |
14 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
15 | 23 | | |
16 | 24 | | |
| 25 | + | |
17 | 26 | | |
18 | 27 | | |
19 | 28 | | |
| |||
35 | 44 | | |
36 | 45 | | |
37 | 46 | | |
38 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
39 | 70 | | |
40 | 71 | | |
41 | 72 | | |
| |||
210 | 241 | | |
211 | 242 | | |
212 | 243 | | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
213 | 255 | | |
214 | 256 | | |
215 | 257 | | |
| |||
401 | 443 | | |
402 | 444 | | |
403 | 445 | | |
404 | | - | |
405 | | - | |
| 446 | + | |
| 447 | + | |
406 | 448 | | |
407 | 449 | | |
408 | 450 | | |
| |||
415 | 457 | | |
416 | 458 | | |
417 | 459 | | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
418 | 556 | | |
419 | 557 | | |
420 | 558 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
183 | 184 | | |
184 | 185 | | |
185 | 186 | | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
191 | 200 | | |
192 | 201 | | |
193 | 202 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
0 commit comments