Skip to content

fix: add DoS protection for UTXO deserialization#94

Merged
ordishs merged 7 commits into
bsv-blockchain:mainfrom
ordishs:fix-issue-4248-clean
Nov 3, 2025
Merged

fix: add DoS protection for UTXO deserialization#94
ordishs merged 7 commits into
bsv-blockchain:mainfrom
ordishs:fix-issue-4248-clean

Conversation

@ordishs

@ordishs ordishs commented Oct 31, 2025

Copy link
Copy Markdown
Collaborator

Summary

Fixes a critical DoS vulnerability in UTXO deserialization where malicious actors could provide extremely large script length values causing memory exhaustion.

Changes

  • Add configurable maxUTXOScriptSize variable loaded from settings.Policy.MaxScriptSizePolicy (default 500KB)
  • Validate script length before memory allocation in NewUTXOFromReader() and NewUTXOValueFromReader()
  • Add comprehensive DoS protection tests including concurrent attack simulation
  • Add overflow protection capping at math.MaxUint32

Vulnerability Details

The vulnerability existed in:

  • services/utxopersister/UTXO.go:336 - NewUTXOFromReader()
  • services/utxopersister/UTXO.go:357 - NewUTXOValueFromReader()

Both functions read a 4-byte script length from untrusted input and immediately allocate memory without bounds checking. An attacker could provide a script length up to 4.3GB (max uint32), causing OOM crashes.

Security Impact

Before: Attacker could crash Teranode by providing malicious UTXO files/streams with 4.3GB script lengths
After: Script lengths validated against policy limit (default 500KB) before allocation, preventing memory exhaustion

Test Coverage

  • ✅ Rejection of scripts exceeding limit
  • ✅ Rejection of max uint32 values (4.3GB)
  • ✅ Acceptance of scripts at exact limit
  • ✅ Concurrent attack simulation (20 goroutines × 4GB = 80GB blocked)
  • ✅ All existing tests pass

Configuration

The limit is configurable via maxscriptsizepolicy in settings:

  • Defaults to 500KB matching BSV policy for transaction validation
  • Post-Genesis BSV has no hard consensus limit on script sizes
  • Operators can adjust if needed for specific use cases

Closes #4248

Add configurable script size validation to prevent memory exhaustion
attacks in UTXO deserialization functions.

The vulnerability existed in NewUTXOFromReader() and
NewUTXOValueFromReader() where malicious actors could provide
extremely large script length values (up to 4.3GB) causing OOM
crashes. The fix validates script lengths against the configurable
maxscriptsizepolicy setting (default 500KB) before allocation.

Changes:
- Add maxUTXOScriptSize variable loaded from settings.Policy
- Validate script length before memory allocation in both reader functions
- Add comprehensive DoS protection tests including concurrent attack simulation
- Add overflow protection capping at math.MaxUint32

Fixes #4248
@ordishs ordishs requested a review from icellan October 31, 2025 15:36
@github-actions

github-actions Bot commented Oct 31, 2025

Copy link
Copy Markdown
Contributor

🤖 Claude Code Review

Status: Complete


Summary: No critical issues found. The DoS protection implementation is well-designed and properly addresses the vulnerability.

Previously Fixed:

  • ✅ Overflow protection now correctly handles negative values in CalculateMaxScriptSize()

Implementation Quality:

  • Security fix correctly validates script lengths before memory allocation
  • All call sites properly pass maxScriptSize parameter
  • Comprehensive test coverage including concurrent attack simulation
  • Appropriate use of configurable policy setting with safe defaults
  • Clean API design making maxScriptSize an explicit parameter rather than global state

The fix effectively prevents memory exhaustion attacks while maintaining compatibility with BSV's post-Genesis unbounded script sizes through policy-based limits.

Comment thread services/utxopersister/UTXO.go Outdated
Comment thread services/utxopersister/UTXO.go Outdated
@ordishs ordishs requested a review from icellan November 3, 2025 10:32
@sonarqubecloud

sonarqubecloud Bot commented Nov 3, 2025

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
72.2% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@ordishs ordishs merged commit 3e3412e into bsv-blockchain:main Nov 3, 2025
9 checks passed
oskarszoon added a commit that referenced this pull request Nov 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants