Repository navigation
fix: reject cyclic directory graphs before rendering - #100
Conversation
PackFileMeta::Deserialize accepted child maps without validating the directory graph, and RenderDirListing recursed without a visited set. A crafted archive whose root or a subdirectory lists an ancestor made ls, tree and dump recurse until stack overflow; repeated child edges could also blow up output exponentially. Validate the graph after metadata deserialization: root presence, one parent per non-root directory, no self/root edges or duplicate parents, reachability and cycle checks via BFS, and a depth limit of 1024. Add an active-set and depth guard to the renderer as defense in depth, and cover both with unit and malformed-archive tests. Empty data blocks are also documented as invalid (the reader already rejects them). No format change; archive bytes are unchanged. Closes #75
blocksmith314
left a comment
There was a problem hiding this comment.
Review
LGTM. The graph validation is well structured:
- root existence, one-parent-per-directory, self/root-as-child, unknown/missing/duplicate edges are rejected before traversal
- BFS catches disconnected cycles via the reachability check and enforces the depth bound
RenderDirListingImpladds a real active-set/depth guard for callers that bypass load-time validation- the
entry_typecaveat is handled correctly by usingdir_entry_recordsmembership
Verified locally: Release ctest 199/199, ASan ctest 199/199, legacy 1.0.0 stat/tree/ls OK, CI 5/5 green.
No blockers.
Minor suggestions:
DirectoryGraph.RejectsDepthAboveLimitcovers the validator, but there is no renderer-depth test. ARendererRejectsDepthcase (a chain abovekMaxDirectoryDepth, rendered directly withoutValidateDirectoryGraph) would cover the second guard.- The cyclic end-to-end test goes through
unpack; since all four commands callRestoreBlocksFromPackfirst, that is reasonable. Atree/lscyclic case would additionally pin the CLI wiring, but the unit renderer test already covers the defense-in-depth path. - The CN doc line adds “空 data block 是非法的” in both the directory-graph and metadata bullets; harmless duplication.
- The +1.4% pack result was a first-run outlier and pack does not call this code; unpack -2.4% is the relevant number.
DirectoryGraph.RendererRejectsDepth exposed that a 1025-level chain overflows the ASan stack before the depth guard can return. Lower kMaxDirectoryDepth to 128 so both the loader and the recursive renderer stay within realistic stack budgets; real filesystem paths are far shallower. Add the renderer-depth test and remove the duplicated empty-block note from the metadata bullet. Refs #75
blocksmith314
left a comment
There was a problem hiding this comment.
Review
The cycle/tree validation is good, but the new depth cap is applied to all readers, including unpack and metadata-only stat, which do not recurse. That rejects valid archives produced by ctar itself.
Reproduced on b2058d9:
# created /tmp/ctar-deep with 130 nested directories and one file
$ ctar pack /tmp/ctar-deep /tmp/ctar-deep.ctar
pack file bytes: 71626 ...
pack exit=0
$ ctar unpack /tmp/ctar-deep.ctar /tmp/ctar-deep-out
failed to unpack archive ...: corrupt archive ...: directory graph: depth exceeds the limit 128
unpack exit=1
tree also fails, but it recurses and is the command the limit is intended to protect. unpack does not recurse and should be able to restore a valid deep tree. Legacy stat is similarly rejected even though it only parses metadata.
Suggested fix
- Keep cycle/parent validation in
ValidateDirectoryGraph()for all readers. - Move the depth check to the recursive renderer only, or make it an optional/parameterized check that
ls/tree/dumppass andunpack/statdo not. - Keep the active-set/depth guard in
RenderDirListingImpl— returningkCorruptionfor a too-deep tree is better than a stack overflow. - Adjust or remove
DirectoryGraph.RejectsDepthAboveLimit(it currently asserts all readers reject); keepRendererRejectsDepth. - Update the docs to say 128 is a renderer limit, not an archive-reader/format invariant. If deep tree rendering should work, an iterative renderer is the real fix.
A regression test that packs ~130 nested directories and then unpacks successfully (while tree may return kCorruption) would pin this.
Verified locally: Release ctest 200/200, ASan ctest 200/200, legacy 1.0.0 stat/tree/ls OK, CI 5/5 green.
The depth cap was enforced by ValidateDirectoryGraph, so unpack and stat rejected valid deep trees produced by ctar even though they do not recurse. Keep the acyclic/single-parent/reachability validation for every reader, but move the depth bound to RenderDirListing only; unpack and stat now accept deep valid trees, while tree/ls/dump return kCorruption instead of overflowing the stack. Add a regression test that packs and unpacks a tree deeper than the renderer limit and verifies rendering fails cleanly. Refs #75
blocksmith314
left a comment
There was a problem hiding this comment.
Re-reviewed 57409f8.
The previous blocker is fixed: the depth limit now belongs only to the recursive renderer.
ValidateDirectoryGraph()still checks root/parents/cycles/reachability but no longer rejects deep valid treesunpackand metadata-onlystataccept a 130-level treetree/ls/dumpstill returnkCorruptionfrom the renderer instead of overflowingDeepTreeUnpacksButDoesNotRendercovers the end-to-end behavior, andAcceptsDeepChainreplaces the old validator-depth assertion- docs now describe 128 as a rendering limit, not an archive invariant
Verified locally: Release ctest 201/201, ASan ctest 201/201, legacy 1.0.0 stat/tree/ls OK, CI 5/5 green.
I also reproduced the CLI case: pack a 130-deep tree, unpack succeeds, stat succeeds, tree reports directory tree depth exceeds the limit 128 instead of crashing.
No blockers. The rendering limit is intentionally conservative for valid deep trees, but that is documented and the safer choice for the recursive renderer.
Summary
RestoreBlocksFromPackbefore block parsing: root (kRootDirId) must exist; every non-root directory has exactly one parent; reject self-edges, root-as-child, unknown parents/children, duplicate edges/parents; BFS checks reachability and cycles.unpackandstataccept deeper valid trees;tree,lsanddumpreturnkCorruptioninstead of overflowing the recursive renderer.RenderDirListingas defense in depth, so a future caller cannot recurse forever even if the load-time graph check is bypassed.Note: directory entries'
entry_typeis not used for validation.ScanDirectorycurrently re-adds each directory with a defaultEntryMeta, so existing archives do not reliably storeDIRECTORY; membership indir_entry_recordsis authoritative.Tests
ctest: 201/201 passedctest: 201/201 passedDirectoryGraph.*: valid root-only/tree, missing root, self-cycle, root-as-child, multiple parents, disconnected cycle, unknown/missing parent/child, duplicate child, deep-chain validation, renderer depth guard, renderer cycle guard.DirectoryGraph.DeepTreeUnpacksButDoesNotRender: packs and unpacks a 130-level tree successfully, then verifiestreerendering returnskCorruptioncleanly.MalformedArchive.RejectsCyclicDirectoryGraph: patches the root's child list to form a self-cycle, recomputes the metadata checksum, and expectskCorruptionbefore rendering.Performance gate
Against
origin/main(7e4a23d):No attributable regression.
Closes #75