Skip to content

fix stack overflow building absolute path from relative symlink target - #6425

Merged
larsewi merged 1 commit into
cfengine:masterfrom
Dhanu-dynamic:symlink-target-path-overflow
Nov 6, 2026
Merged

larsewi merged 1 commit into
cfengine:masterfrom
Dhanu-dynamic:symlink-target-path-overflow

Conversation

@Dhanu-dynamic

Copy link
Copy Markdown
Contributor

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:

// files_links.c, linkpath is char[CF_BUFSIZE]
strcat(linkpath, linkbuf);

// verify_files_utils.c, vbuff is char[CF_BUFSIZE]
strncat(vbuff, linkbuf, CF_BUFSIZE - 1);

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.

@cf-bottom

Copy link
Copy Markdown

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

@larsewi larsewi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🚀 A few notes:

A link target too long is silently truncated. The truncated path could produce an absolute link pointing to somewhere else (unintended). It would be safer to fail with error.

The unit test fails on macOS, because it rejects too long targets. Maybe you can skip if symlink() sets errno to ENAMETOOLONG?

@Dhanu-dynamic

Copy link
Copy Markdown
Contributor Author

Agreed, a truncated target could end up as a valid absolute link pointing somewhere unintended. LinkCopy() now records a failure and returns PROMISE_RESULT_FAIL when the rebuilt absolute target doesn't fit, and it checks the copy back into linkbuf as well, since the old strlcpy(..., CF_BUFSIZE - 1) into a CF_BUFSIZE - 1 buffer could still drop the last byte. KillGhostLink() already bails out with an error. The test now returns early when symlink() fails with ENAMETOOLONG, as it does on macOS, and asserts on any other errno.

Comment thread libpromises/files_links.c Dismissed
@larsewi

larsewi commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

@cf-bottom Jenkins please :) @Dhanu-dynamic can you please squash the two commits?

@cf-bottom

Copy link
Copy Markdown

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>
@Dhanu-dynamic
Dhanu-dynamic force-pushed the symlink-target-path-overflow branch from 8a156de to dac3db2 Compare November 6, 2026 08:11
@Dhanu-dynamic

Copy link
Copy Markdown
Contributor Author

Squashed into a single commit, no code changes. The commit message now also covers LinkCopy() failing the promise instead of truncating, and the ENAMETOOLONG skip in the test.

@larsewi larsewi added the cherry-pick? Fixes which may need to be cherry-picked to LTS branches label Nov 6, 2026
@larsewi
larsewi merged commit 5030bf8 into cfengine:master Nov 6, 2026
13 checks passed
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Backport to 3.24.x failed, manual backport needed: https://github.com/cfengine/core/actions/runs/42216474113

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Backport to 3.27.x failed, manual backport needed: https://github.com/cfengine/core/actions/runs/42216474113

@larsewi

larsewi commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

@larsewi larsewi removed the cherry-pick? Fixes which may need to be cherry-picked to LTS branches label Nov 6, 2026
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