Skip to content

Bound node:fs cp's recursion - #8719

Merged
csjh merged 3 commits into
mainfrom
cormick/fs-cp-bound-recursion
Nov 23, 2026
Merged

csjh merged 3 commits into
mainfrom
cormick/fs-cp-bound-recursion

Conversation

@csjh

@csjh csjh commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

cpSync() could crash the process with a native stack overflow in
handleCpDir(). Two inputs give that stack:

  • A destination inside the source, reached through a symbolic link.
    cpImpl() refuses a destination whose path lies under the source, but
    it compares the paths as written. Through a symlink the copy gets
    past that check, and each level then creates a directory inside the
    source which the next level copies, without end.
  • A tree deeper than the stack can hold. Paths are limited to 48
    segments, but renaming one directory into another builds a deeper
    tree, and handleCpDir() spends a native stack frame per level.

The fix:

  • Compares the directories themselves and throws ERR_FS_CP_EINVAL, as
    Node.js does for a copy into a subdirectory of itself, before the
    copy writes anything. The check runs where a symlink can put the
    destination inside the source: on the destination given to cp() (or
    the nearest existing ancestor of one yet to be created), and on each
    existing destination directory reached by following a link.
  • Refuses, with ENAMETOOLONG, to recurse more than 48 levels, the
    limit already put on a path's segments.

The first commit adds the tests. The three symlink variants and a tree
2,461 levels deep each crash with SIGSEGV until the second commit. Two
more pass before and after, to keep the fix from refusing valid copies:
a destination symlink pointing at a sibling of the directory copied
through it, and a tree as deep as a path can reach.

Behavior change to note for review: a tree nested deeper than 48 levels
(which can only be built with renames or links) and shallow enough not
to overflow the stack used to copy, and now fails with ENAMETOOLONG.

🤖 Generated with Claude Code

@csjh
csjh requested review from a team as code owners November 20, 2026 05:49

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 5155f80a.

This review, of 3956e37a

✅ Approved — The fix bounds cp's recursion correctly with cycle detection and a depth cap that matches the existing path-segment limit, and it comes with tests. No defects found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change only adds checks. It closes a process crash (a native stack overflow) that a worker could trigger with symlinks or a very deep tree.


📖 AGENTS.md: ✅ LGTM

The change does not call for any AGENTS.md update.


📋 Code Quality: ✅ LGTM

Cycle detection and the depth bound work as described. Depth unwinds on throw through KJ_DEFER, and each top-level copy gets its own state.


⚡ Performance: ✅ LGTM

Each source directory costs one O(1) hash lookup. The map grows with the number of destination directories and is freed when the copy finishes.


📝 Documentation: ✅ LGTM

The comments describe the current behavior and explain why the limit is 48.


Reviewed commit: 3956e37a

@csjh
csjh force-pushed the cormick/fs-cp-bound-recursion branch from 3956e37 to 5155f80 Compare November 20, 2026 06:11

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of c9edee6a.

This review, of 5155f80a

✅ Approved — Since the last approval, only the test file has changed. The fix still bounds cp's recursion with cycle detection and a depth cap that matches the existing 48-segment path limit, and the new tests line up with the code.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change only adds checks. It closes a process crash (a native stack overflow) that a worker could trigger with symlinks or a very deep tree.


📖 AGENTS.md: ✅ LGTM

The change does not call for any AGENTS.md update.


📋 Code Quality: ✅ LGTM

Only the test file changed since the last review, and filesystem.c++ is the same as before.

  • Depth cap: kMaxCpDepth = 48 matches the path-segment limit.
  • Cycle detection: each source directory is checked against the directories the copy has written into.
  • Unwinding: KJ_DEFER decrements the depth even when an exception is thrown.
  • Boundary test: in FsCpDeepestPathTree, the 48-segment path recurses through 46 directories, below the cap of 48.
  • Other tests: the cycle tests expect ERR_FS_CP_EINVAL and FsCpDeepTree expects ENAMETOOLONG, as the code throws.

⚡ Performance: ✅ LGTM

Each source directory costs one hash lookup. The set of destination directories is freed when the copy finishes.


📝 Documentation: ✅ LGTM

The comments describe the current behavior and explain why the limit is 48.


Reviewed commit: 5155f80a

Comment thread src/workerd/api/filesystem.c++ Resolved
@csjh
csjh force-pushed the cormick/fs-cp-bound-recursion branch from 5155f80 to c9edee6 Compare November 20, 2026 19:34

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of a02ae63e.

This review, of c9edee6a

✅ Approved — No issues found. The checks run before anything is written into the source tree, and the new tests cover the symlink cycles and both depth limits.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change only adds checks. It closes a process crash (a native stack overflow) that a worker could trigger with symbolic links or a very deep tree. A refused copy no longer modifies the source tree.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md update is needed.


📋 Code Quality: ✅ LGTM

No issues found.

  • Ordering: requireDestOutsideSrc and the nearest-existing-ancestor check in cpImpl() run before handleCpDir().
  • Links inside the tree: a destination directory reached through a link is checked before the copy recurses into it (filesystem.c++:1718).
  • Depth cap: kMaxCpDepth is checked when handleCpDir() starts, before anything at that level is written.
  • isWithin(): it walks the tree with an explicit stack, so it adds no native stack depth.
  • Tests: they cover the three symlink variants, a valid link to a sibling directory, a tree that is too deep and the deepest valid tree.

⚡ Performance: ✅ LGTM

  • Cost of the walk: isWithin() runs only for the top-level destination and for destinations reached through a link, and its cost is linear in the size of the source subtree.
  • Recursion: the depth cap bounds it, and the change adds no cost on the common path.

📝 Documentation: ✅ LGTM

The comments describe the current behavior.


Reviewed commit: c9edee6a

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 20f3974f.

This review, of a02ae63e

✅ Approved — No issues found. Each check runs before anything is written into the source tree, the directory walk always ends, and the new tests cover the symlink cycles and both depth limits.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change only adds checks. It closes a process crash (a native stack overflow) that a worker could trigger with symlinks or a very deep tree, and the new directory walk always ends.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md update is needed.


📋 Code Quality: ✅ LGTM

  • Writes into the source: the top-level check and the nearest-existing-ancestor check run before handleCpDir() writes anything. A destination directory reached through a link is checked before the copy recurses into it. Entries copied earlier in the same directory go into real directories under the destination, so a copy that is refused later has not modified the source.
  • isWithin(): it walks with an explicit stack and a set of directories already seen, so it ends even if a directory can reach itself.
  • Depth cap: kMaxCpDepth is checked when handleCpDir() starts, before anything at that level is written. The deepest-path test stays under the cap, and the 2,461-level tree is refused.

⚡ Performance: ✅ LGTM

isWithin() takes time linear in the size of the source subtree. It runs only for the top-level destination and for destinations reached through a link. The ancestor loop is bounded by the 48-segment path limit, and the common copy path pays nothing extra.


📝 Documentation: ✅ LGTM

The comments describe the current behavior.


Reviewed commit: a02ae63e

csjh and others added 3 commits November 23, 2026 16:34
cpImpl() refuses a destination whose path lies under the source, but
it compares the two paths as written. A destination that reaches into
the source through a symbolic link passes that check, and
handleCpDir() then recurses without bound, copying each directory it
creates into a new one beneath it, until the stack overflows.

Three cases cover a destination path through a symlink to the source,
a dereferenced destination symlink that points into the source, and a
symlink inside the destination that points into the source's subtree.
The first two also check that the refused copy changed nothing in the
source.

A fourth copies a tree 2,461 levels deep, built by renaming one
directory into another, since paths are limited to 48 segments.
handleCpDir() spends a native stack frame on each level, so this
overflows the stack too, at about 1,600 levels in a debug build.

Each of those crashes workerd with SIGSEGV until the fix in the next
commit. Two more cases pass before and after it, to keep the fix from
refusing valid copies: a destination symlink that points at a sibling
of the directory copied through it, and a tree as deep as a path can
reach.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
handleCpDir() recursed into every source directory without checking
whether the destination lay inside it, or how deep it was. When the
destination lies inside the source through a symbolic link, which
cpImpl()'s check on the paths as written cannot see, each level
creates a directory that the next level then copies, until the stack
overflows and the process crashes. A tree deeper than the stack can
hold, built by renaming directories into one another, crashes the
same way.

Compare the directories themselves, and throw ERR_FS_CP_EINVAL, as
Node.js does for a copy into a subdirectory of itself, before the
copy writes anything. The check runs where a symbolic link can put
the destination inside the source: on the destination given to cp(),
or the nearest existing ancestor of one yet to be created, and on
each existing destination directory reached by following a link.
Directories the copy creates itself are outside the source as long
as their parent is.

Also refuse, with ENAMETOOLONG, to recurse more than 48 levels: the
limit already put on a path's segments, so only a tree built by
renaming or linking directories is affected. V8 must still be able
to throw at the deepest level, which a debug build could no longer do
at 256 levels: by then the stack had grown past V8's own limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Visit each directory once in isWithin(), so that it ends even if a
directory can reach itself. The VFS does not guarantee that its
directories form a tree, and without this the check would loop in
native code, where the CPU limit cannot interrupt it.

Name both paths in the error for a copy into the source's own tree,
as the check on the paths as written already did, from one helper
shared by all three sites that have the paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@csjh
csjh force-pushed the cormick/fs-cp-bound-recursion branch from a02ae63 to 20f3974 Compare November 23, 2026 21:34

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No issues found. Each check runs before anything is written into the source, the directory walk in isWithin() always ends, and the depth cap bounds recursion. Tests cover the symlink cases and both depth limits.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change only adds checks. It closes a process crash (a native stack overflow) that a worker could trigger with symlinks or a very deep tree. The new directory walk always ends.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md update is needed.


📋 Code Quality: ✅ LGTM

  • Writes into the source: the check against the top-level destination and the check against the nearest existing ancestor both run before handleCpDir() writes anything. A destination directory reached through a link is checked before the copy recurses into it. A copy that is refused partway through leaves the source unchanged.
  • isWithin(): it walks with an explicit stack and a set of directories it has already seen, so it ends even if a directory can reach itself.
  • Depth cap: kMaxCpDepth is checked at the start of handleCpDir(), before anything at that level is written.
  • Same directory: when the source and destination are the same directory, isDestInsideSrc() returns false and handleCpDir() reports EINVAL, as intended.

⚡ Performance: ✅ LGTM

isWithin() takes time linear in the size of the source subtree. It runs only for the top-level destination and for destinations reached through a link. The ancestor loop is bounded by the 48-segment path limit, and the common copy path pays nothing extra.


📝 Documentation: ✅ LGTM

The comments describe the current behavior.


Reviewed commit: 20f3974f

@csjh
csjh enabled auto-merge (squash) November 23, 2026 21:39
@csjh
csjh merged commit a00286a into main Nov 23, 2026
23 checks passed
@csjh
csjh deleted the cormick/fs-cp-bound-recursion branch November 23, 2026 22:07
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