Repository navigation
fix stack overflow building absolute path from relative symlink target - #6425
Open
Dhanu-dynamic wants to merge 1 commit into
Open
Dhanu-dynamic wants to merge 1 commit into
Dhanu-dynamic wants to merge 1 commit into
Conversation
cf-agent reconstructs the absolute target of a relative symbolic link by appending the readlink() result onto the link's directory name in a CF_BUFSIZE stack buffer. KillGhostLink() in files_links.c did this with an unbounded strcat(linkpath, linkbuf), and LinkCopy() in verify_files_utils.c with strncat(vbuff, linkbuf, CF_BUFSIZE - 1), whose third argument is the maximum number of bytes to take from the source, not the space left in the destination. readlink() returns up to CF_BUFSIZE - 1 bytes, so a symlink whose relative target is near CF_BUFSIZE, left by an unprivileged user in a directory that a root files promise walks (depth_search, perms, link copy), overflows the stack of the agent. KillGhostLink() runs on every symlink met during a recursive descent, before the dead-link stat(). Bound both appends with strlcat(..., sizeof(buf)), the same way ExpandLinks() in files_links.c already does, and have KillGhostLink() bail out with an "Internal limit reached" message when the target does not fit. Normal links are unchanged. Changelog: Title Signed-off-by: Dhanraj E <dhanraj@labs.digiscrypt.com>
|
Thanks for submitting a pull request! Maybe @craigcomstock can review this? |
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.
KillGhostLink() in libpromises/files_links.c and LinkCopy() in
cf-agent/verify_files_utils.c reconstruct the absolute target of a relative
symbolic link by appending the readlink() result onto the link's directory
name in a CF_BUFSIZE stack buffer:
The strcat() is unbounded, and the strncat() third argument is the maximum
number of bytes to take from the source, not the space left in vbuff, so it
does not bound the destination either. readlink() returns up to CF_BUFSIZE-1
bytes, so a symlink whose relative target is near CF_BUFSIZE, placed by an
unprivileged user in any directory a root files promise walks (depth_search,
perms, or a link-type copy), overflows the agent's stack. KillGhostLink()
runs on every symlink met during a recursive descent, before the dead-link
stat(), so reaching it only takes one such link under a recursive promise.
Bound both appends with strlcat(..., sizeof(buf)), the same way ExpandLinks()
in files_links.c already bounds this exact operation, and have KillGhostLink()
bail out with an "Internal limit reached" message when the target does not
fit. Valid links are unchanged.
Verified with AddressSanitizer via a regression test in
tests/unit/files_links_test.c: a relative symlink with a CF_BUFSIZE-sized
target aborts KillGhostLink() with a stack overflow before the change and
returns cleanly after it.