path: bound directory-walk recursion depth to prevent stack overflow#45
Open
acts-1631 wants to merge 1 commit into
Open
path: bound directory-walk recursion depth to prevent stack overflow#45acts-1631 wants to merge 1 commit into
acts-1631 wants to merge 1 commit into
Conversation
walk_path_recursive() recursed once per directory level with no depth limit. A remote SFTP server (malicious or compromised) can present an arbitrarily deep directory hierarchy, causing the client to recurse until the thread stack is exhausted and the process crashes (SIGSEGV), aborting the transfer. Reproduced locally: a 400-level-deep local directory tree crashes the unpatched walk_src_path() with a segmentation fault (stack overflow). With this patch, the same tree is rejected cleanly with a 'directory nesting too deep' error and no crash. Add a depth counter threaded through walk_path_recursive(), bailing out at 256 levels -- deep enough for any realistic directory layout while keeping stack usage to roughly 1-2MB, well within the default 8MB thread stack.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
walk_path_recursive() recurses once per directory level with no depth limit. A remote SFTP server (malicious or just compromised) can hand back an arbitrarily deep directory tree, and the client will keep recursing until the thread stack runs out and the process crashes.
I reproduced this locally: a 400-level-deep directory tree segfaults the current code. This patch adds a depth counter and bails out with an error past 256 levels, which is far deeper than any real directory layout needs and keeps stack usage to a couple MB.