Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cf-agent/verify_files_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1338,15 +1338,15 @@ static PromiseResult LinkCopy(EvalContext *ctx, char *sourcefile, char *destfile
strlcpy(vbuff, changes_sourcefile, CF_BUFSIZE);
ChopLastNode(vbuff);
AddSlash(vbuff);
strncat(vbuff, chrooted_linkbuf, CF_BUFSIZE - 1);
strlcat(vbuff, chrooted_linkbuf, sizeof(vbuff));
strlcpy(chrooted_linkbuf, vbuff, CF_BUFSIZE - 1);
}
else
{
strlcpy(vbuff, sourcefile, CF_BUFSIZE);
ChopLastNode(vbuff);
AddSlash(vbuff);
strncat(vbuff, linkbuf, CF_BUFSIZE - 1);
strlcat(vbuff, linkbuf, sizeof(vbuff));
strlcpy(linkbuf, vbuff, CF_BUFSIZE - 1);
}
}
Expand Down
8 changes: 7 additions & 1 deletion libpromises/files_links.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,13 @@ bool KillGhostLink(EvalContext *ctx, const char *name, const Attributes *attr, c
}
}

strcat(linkpath, linkbuf);
if (strlcat(linkpath, linkbuf, sizeof(linkpath)) >= sizeof(linkpath))
{
Log(LOG_LEVEL_ERR,
"Internal limit reached in KillGhostLink(),"
" path too long: '%s' + '%s'", linkpath, linkbuf);
return false;
}
CompressPath(tmp, sizeof(tmp), linkpath);

if (stat(tmp, &statbuf) == -1) /* link points nowhere */
Expand Down
33 changes: 32 additions & 1 deletion tests/unit/files_links_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include <test.h>

#include <cf3.defs.h>
#include <files_links.h> /* ExpandLinks */
#include <files_links.h> /* ExpandLinks, KillGhostLink */
#include <attributes.h> /* Attributes */
#include <unistd.h> /* symlink, unlink */

/* A single path component longer than the internal node buffer
* (CF_MAXLINKSIZE) overflowed the stack in ExpandLinks(): the parse into
Expand All @@ -44,12 +46,41 @@ static void test_expand_links_long_component(void)
assert_false(ret);
}

/* KillGhostLink() rebuilds the absolute target of a relative symlink by
* appending the readlink() result onto the link's directory in a CF_BUFSIZE
* buffer. The append was an unbounded strcat(), so a symlink whose relative
* target is near CF_BUFSIZE overflowed the stack. It must bail out instead. */
static void test_kill_ghost_link_long_target(void)
{
char tmpl[] = "/tmp/cfghostXXXXXX";
const char *dir = mkdtemp(tmpl);
assert_true(dir != NULL);

char linkpath[256];
xsnprintf(linkpath, sizeof(linkpath), "%s/l", dir);

char target[CF_BUFSIZE];
memset(target, 'x', sizeof(target) - 1);
target[sizeof(target) - 1] = '\0';
assert_int_equal(symlink(target, linkpath), 0);

Attributes attr = { 0 };
attr.link.when_no_file = cfa_delete;
PromiseResult result = PROMISE_RESULT_NOOP;

assert_false(KillGhostLink(NULL, linkpath, &attr, NULL, &result));

unlink(linkpath);
rmdir(dir);
}

int main()
{
PRINT_TEST_BANNER();

const UnitTest tests[] = {
unit_test(test_expand_links_long_component),
unit_test(test_kill_ghost_link_long_target),
};

return run_tests(tests);
Expand Down
Loading