Skip to content

vfs: prevent stack overflow in recursive readdir on circular symlinks#64149

Open
AkshatOP wants to merge 1 commit into
nodejs:mainfrom
AkshatOP:fix-vfs-readdir-circular-symlink
Open

vfs: prevent stack overflow in recursive readdir on circular symlinks#64149
AkshatOP wants to merge 1 commit into
nodejs:mainfrom
AkshatOP:fix-vfs-readdir-circular-symlink

Conversation

@AkshatOP

Copy link
Copy Markdown

MemoryProvider#readdirSync with recursive: true follows symlinks to directories during traversal but did not bound the number of symlinks followed along a branch. A circular symlink therefore caused unbounded recursion in the internal walk() helper until the call stack was exhausted, crashing the process with RangeError: Maximum call stack size exceeded. Both the synchronous and promise-based variants were affected. The existing kMaxSymlinkDepth guard in #lookupEntry did not help, because walk() resolved each symlink target with a fresh depth of zero.

Track the number of symlink hops along the current branch and stop recursing once it would exceed kMaxSymlinkDepth, mirroring the ELOOP guard in #lookupEntry and the behavior of the real filesystem, which follows directory symlinks until the OS symlink limit is reached. The entries themselves are still listed, so non-circular symlinks continue to be followed as before.

Fixes: #64148

MemoryProvider#readdirSync with `recursive: true` follows symlinks to
directories during traversal but did not bound the number of symlinks
followed along a branch. A circular symlink therefore caused unbounded
recursion in the internal walk() helper until the call stack was
exhausted, crashing the process with `RangeError: Maximum call stack
size exceeded`. Both the synchronous and promise-based variants were
affected. The existing kMaxSymlinkDepth guard in #lookupEntry did not
help, because walk() resolved each symlink target with a fresh depth of
zero.

Track the number of symlink hops along the current branch and stop
recursing once it would exceed kMaxSymlinkDepth, mirroring the ELOOP
guard in #lookupEntry and the behavior of the real filesystem, which
follows directory symlinks until the OS symlink limit is reached. The
entries themselves are still listed, so non-circular symlinks continue
to be followed as before.

Fixes: nodejs#64148
Signed-off-by: AkshatOP <hunterdevil0987@gmail.com>
@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. vfs Issues and PRs related to the virtual filesystem subsystem. labels Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. vfs Issues and PRs related to the virtual filesystem subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

node:vfs MemoryProvider: readdirSync({recursive:true}) crashes via circular symlinks (stack overflow)

2 participants