Skip to content

fix: Stop deeply nested Matroska SimpleTags from overflowing the stack - #151

Merged
decriptor merged 4 commits into
mainfrom
fix/matroska-cues-depth
Nov 22, 2026
Merged

decriptor merged 4 commits into
mainfrom
fix/matroska-cues-depth

Conversation

@decriptor

@decriptor decriptor commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Description

MatroskaFile.ReadSimpleTag and SimpleTag.Render recursed once per nesting level. A crafted file could make that depth anything, so MatroskaFile.Read (and MediaFile.Read) ended the process with a stack overflow, which no caller can catch.

  • RFC 9559 §5.1.8.1.2 lets a SimpleTag nest without a limit. The reader now parses 64 levels (MaxSimpleTagDepth).
  • Each deeper SimpleTag is kept as its stored bytes in SimpleTag.KeptChildren, and a save writes it back unchanged after the parsed children. Nothing is lost, and no tag mapping reads deeper than two levels.

Cues: this PR first also bounded the Cues walker. That half was dropped on the rebase, because main now walks Cues only along the spec paths (from #149, merged with #142). The Cues tests are kept; both pass on main's walker.

Motivation and Context

On main, a file with SimpleTags nested 100,000 deep (about 1 MB) crashes on open. One file in the review corpus (st100000.mkv) already does, and it still crashed after #142 merged.

Fixes #150

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Checklist

  • I have read the CONTRIBUTING guidelines
  • My code follows the project's code style
  • I have added tests that prove my fix/feature works
  • All new and existing tests pass (dotnet test)
  • I have updated documentation as needed
  • My changes generate no new warnings

Testing

  • Read_SimpleTagsNestedDeep_DoesNotOverflowAndSaveKeepsTheNesting (100,000 deep): overflows the stack on main, passes here. It also checks that the stored bytes below the parse depth occur exactly once in the saved file.
  • Save_SimpleTagsAtAndPastTheParseDepth_KeepOnlyTheDeeperOnesAsStored (rows 62–69, 8-octet sizes) pins the cap exactly:
    • row 62 parses every level and re-encodes it;
    • rows 63+ keep the deepest subtree byte for byte.
  • Targeted reverts:
    • re-encoding the kept bytes turns 10 tests red;
    • cap < → <= turns 10 red;
    • cap one level shallower turns 1 red;
    • not writing or not reading KeptChildren turns 11 red each.
  • Before/after: a review run over 478 Matroska files at main and head gave 467 identical results. The other 11 are the deeply nested SimpleTag files, which now read, and whose deep levels keep their 8-octet sizes. Fuzz: 28,339 cases, 0 throws.
  • Every commit builds. Release: 0 warnings on 4 TFMs. net8.0 and net10.0: 5183 passed / 26 skipped. Format clean.

Additional Notes

MatroskaTag.CopyTag copies only top-level SimpleTags, which never hold KeptChildren while the cap is above 1, so no copy path loses the kept bytes.

@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.08%. Comparing base (88f91b7) to head (623fd4d).
⚠️ Report is 25 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #151      +/-   ##
==========================================
+ Coverage   93.06%   93.08%   +0.01%     
==========================================
  Files         334      335       +1     
  Lines       70883    71027     +144     
  Branches     7468     7477       +9     
==========================================
+ Hits        65966    66112     +146     
  Misses       2911     2911              
+ Partials     2006     2004       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@decriptor
decriptor force-pushed the fix/matroska-cues-depth branch from ab82f19 to 623fd4d Compare November 22, 2026 06:17
@decriptor decriptor changed the title fix: Stop deeply nested Matroska Cues and SimpleTags from overflowing the stack fix: Stop deeply nested Matroska SimpleTags from overflowing the stack Nov 22, 2026
@decriptor

Copy link
Copy Markdown
Owner Author

Review: 0 findings at 623fd4d. (Independent review; the coordinator wrote this PR.)

What was checked:

  • Round 2 approved 623fd4d. Its one trivial finding, three stale numbers in the PR body, is fixed in the body.
  • Rebuild: the PR was rebuilt on main after feat: Write Matroska pictures as image attachments #142 merged with fix: Keep a moved Matroska element listed in the SeekHead and move later elements for a track flag #149's spec-path Cues walker.
    • This PR's own Cues commit is dropped. The SimpleTag fix commit is unchanged.
    • The tests differ only by the WideFile to DeepWideFile rename, and the CHANGELOG is trimmed to the SimpleTag fix.
    • All 4 commits build in Release with 0 warnings.
  • Round 1's findings are fixed:
    • The tests now assert that the stored bytes below the 64-level parse depth occur exactly once.
    • Row 62 pins the cap exactly.
    • Mutants that are now red: re-wrapping kept bytes (10), cap <= (10), cap one shallower (1), and dropping KeptChildren on write or read (11 each).
  • st100000.mkv:
    • On main it ends the process with Stack overflow.
    • At this head it reads and saves. The deep subtree (999,396 bytes) is written once and verbatim, the Clusters are unchanged, and a no-op save is byte-identical.
  • Grid: 478 Matroska files against main, 467 identical. The 11 that changed are the SimpleTag-depth files, all as intended. Fuzz: 28,065 cases, 0 throws.
  • Interaction with main: parsed child SimpleTags are read only at level 1. The render-on-read baseline and the attachment-removal render both go through SimpleTag.Render, so the kept bytes survive.
  • Gates: full suite 5183 passed / 26 skipped on net8.0 and net10.0. Format clean. Env-gated Matroska tests pass on 9 samples. CI 8/8 green.

@decriptor decriptor added the approved AI has reviewed and signed off label Nov 22, 2026
@decriptor
decriptor merged commit 577eaaf into main Nov 22, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved AI has reviewed and signed off

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Deeply nested Matroska Cues or SimpleTags crash the process with a stack overflow

1 participant