Repository navigation
Fixed one-byte stack overflow in ChrootSymlinkDeep() - #6531
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
The target buffer is sized from the st_size of the caller's lstat(), plus one for the terminator, but readlink() was given the whole buffer. If the link is replaced with a longer one in between, readlink() fills the buffer and the NUL is written one byte past it. This runs as root in --simulate mode while walking the promised tree. Changelog: Title Signed-off-by: Dhanraj E <dhanraj@labs.digiscrypt.com>
larsewi
requested changes
Dec 2, 2026
larsewi
left a comment
Contributor
There was a problem hiding this comment.
A better fix would be to stop if truncation is detected, rather than proceeding with a truncated buffer.
ssize_t ret = readlink(path, target, target_size);
if (ret < 0 || (size_t) ret >= target_size)
{
/* Error, or the link changed since lstat() and was truncated. */
return;
}
target[ret] = '\0';
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.
ChrootSymlinkDeep() sizes target from the st_size that PrepareChangesChroot() got from lstat(), plus one for the NUL, but passed the full size to readlink(). readlink() does not terminate and returns up to bufsiz bytes, so if the link is swapped for a longer one between the lstat() and the readlink(), target[ret] = '\0' lands one byte past the VLA. cf-agent --simulate=diff/manifest runs this as root and PrepareChangesChroot() recurses into every entry of the promised tree, so a user who owns a directory in it can retry the swap with rename() in a loop. readlink() now gets target_size - 1, like the other callers in the tree; a link that grew is truncated instead. checked with a model of the function built with zig cc's ubsan: "index 2 out of bounds for type 'char[2]'" before, clean after.