Commit 2e4e1be
committed
vfs: prevent stack overflow in recursive readdir on circular symlinks
MemoryProvider#readdirSync with `recursive: true` follows symlinks to
directories during traversal. The traversal was recursive and unbounded,
so a circular symlink 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.
Rewrite the traversal to be iterative, using an explicit queue instead
of recursion so deeply nested trees can no longer exhaust the call
stack.
Each queued directory carries the number of symlink hops taken to reach
it; a directory reached by following a symlink is not enqueued once that
count would exceed kMaxSymlinkDepth, mirroring the ELOOP guard in
#lookupEntry and the real filesystem, which follows directory symlinks
until the OS symlink limit is reached. Entries are still listed, so
non-circular symlinks continue to be followed as before.
Fixes: #64148
Signed-off-by: AkshatOP <hunterdevil0987@gmail.com>1 parent 74567b2 commit 2e4e1be
2 files changed
Lines changed: 81 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
612 | 612 | | |
613 | 613 | | |
614 | 614 | | |
615 | | - | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
616 | 629 | | |
617 | 630 | | |
618 | 631 | | |
| |||
634 | 647 | | |
635 | 648 | | |
636 | 649 | | |
637 | | - | |
| 650 | + | |
638 | 651 | | |
| 652 | + | |
639 | 653 | | |
640 | 654 | | |
641 | 655 | | |
| |||
644 | 658 | | |
645 | 659 | | |
646 | 660 | | |
| 661 | + | |
647 | 662 | | |
648 | | - | |
649 | | - | |
650 | | - | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
651 | 671 | | |
652 | 672 | | |
653 | | - | |
| 673 | + | |
654 | 674 | | |
655 | | - | |
656 | 675 | | |
657 | 676 | | |
658 | 677 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
0 commit comments