Skip to content

[SECURITY] Request: Add recursion depth limit to KlvDecoder #6934

Closed

Description

@montge

Security Request: Recursion Depth Limit in KlvDecoder

Requested By: Alliance project (codice/alliance)
Issue: Uncontrolled recursion in KlvDecoder allows stack overflow DoS attacks
CVSS: 7.5 (High)
Alliance Tracking: montge/alliance#53


Problem

DDF's KlvDecoder.decode() recursively processes nested KlvLocalSet structures without depth limits. This enables Denial of Service attacks via deeply nested KLV structures.

Attack Scenario:

Malicious MPEG-TS stream with 5,000-10,000 nested KLV Local Sets
→ KlvDecoder.decode() recursively processes each level
→ Stack overflow after ~1,000-5,000 levels (platform dependent)
→ JVM crash → DoS

Affected Code:

  • org.codice.ddf.libs.klv.KlvDecoder.decode()
  • Used by: Alliance STANAG 4609 FMV processing, DDF metadata extraction

Proposed Fix

Add maximum recursion depth parameter to KlvDecoder:

public class KlvDecoder {
    private static final int MAX_RECURSION_DEPTH = 32;
    
    public KlvContext decode(byte[] data) throws KlvDecodingException {
        return decode(data, 0);
    }
    
    private KlvContext decode(byte[] data, int depth) throws KlvDecodingException {
        if (depth > MAX_RECURSION_DEPTH) {
            throw new KlvDecodingException(
                "KLV nesting depth exceeds maximum: " + depth);
        }
        
        // ... existing decode logic ...
        // For nested Local Sets, call: decode(nestedData, depth + 1)
    }
}

Alliance Interim Mitigation

Alliance has implemented best-effort pre-validation in KlvSecurityValidator that scans KLV structure before decoding. This provides partial protection but cannot catch all nesting patterns without fully duplicating KlvDecoder logic.

Commit: montge/alliance@b26e0f6c


Benefits

✅ Prevents stack overflow DoS attacks
✅ Graceful failure with clear error message
✅ Minimal performance impact (depth counter is cheap)
✅ Backward compatible (legitimate KLV rarely exceeds 5-10 levels)
✅ Protects all DDF-based applications


Request

Could the DDF team:

  1. Review this security concern
  2. Consider adding recursion depth limit to KlvDecoder
  3. Provide timeline for potential fix

We're happy to contribute a pull request if helpful.

Thank you for maintaining DDF!


Contact: Alliance security team via montge/alliance repository
References:

Activity

  1. montge commented on Dec 4, 2025

    @montge
    Author

    A fix for this security vulnerability has been committed to the DDF codebase:

    Commit: f79aca3 - "Security: Add recursion depth limit to KlvDecoder to prevent DoS attacks"

    Changes:

    • KlvDecoder now tracks recursion depth when decoding nested KlvLocalSet structures
    • Default maximum depth is 32 (configurable via constructor)
    • KlvDecodingException thrown when depth limit exceeded
    • Added comprehensive unit tests for the depth limiting functionality

    This prevents malicious MPEG-TS streams with thousands of nested KLV Local Sets from causing JVM stack overflow crashes.

    The fix is now available in the master branch.

  2. montge commented on Dec 6, 2025

    @montge
    Author

    Fixed in commit f79aca3 - Added MAX_RECURSION_DEPTH limit to KlvDecoder to prevent stack overflow DoS attacks from deeply nested KLV structures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions