Skip to content

Fixed stack overflows in symlink path handling (3.24.x) - #6480

Open
larsewi wants to merge 2 commits into
cfengine:3.24.xfrom
larsewi:symlink-target-overflow-3.24.x
Open

larsewi wants to merge 2 commits into
cfengine:3.24.xfrom
larsewi:symlink-target-overflow-3.24.x

Conversation

@larsewi

@larsewi larsewi commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Backport of #6209 and #6425.

#6209 is included because #6425 extends the unit test it adds.

🤖 Generated with Claude Code

aizu-m and others added 2 commits November 6, 2026 13:21
Signed-off-by: Aizal Khan <aizumusheer2@gmail.com>
(cherry picked from commit 17d6795)
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
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. KillGhostLink() bails out with an "Internal
limit reached" message and LinkCopy() fails the promise when the target does
not fit, rather than truncating it into a link pointing somewhere else.
Normal links are unchanged.

The regression test is skipped when symlink() rejects the long target with
ENAMETOOLONG, as macOS does.

Changelog: Title
Signed-off-by: Dhanraj E <dhanraj@labs.digiscrypt.com>
(cherry picked from commit dac3db2)
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
@cf-bottom

Copy link
Copy Markdown

Thank you for submitting a pull request! Maybe @craigcomstock can review this?

@larsewi

larsewi commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

@cf-bottom Jenkins with exotics please :)

@cf-bottom

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants