Skip to content

cf-serverd: Fixed off-by-one stack overflow in acl_CheckPath() - #6518

Open
Dhanu-dynamic wants to merge 1 commit into
cfengine:masterfrom
Dhanu-dynamic:acl-checkpath-mangled-path-bound
Open

Dhanu-dynamic wants to merge 1 commit into
cfengine:masterfrom
Dhanu-dynamic:acl-checkpath-mangled-path-bound

Conversation

@Dhanu-dynamic

Copy link
Copy Markdown
Contributor

acl_CheckPath() copies the requested path, reqpath_len + 1 bytes, into
char mangled_path[PATH_MAX] before substituting the $(connection.*)
variables, without checking that it fits. The TLS OPENDIR handler, and
STAT when the target is a directory, append a trailing slash to the
PreprocessRequestPath() result before calling acl_CheckPath(). That
result lives in filename[CF_BUFSIZE + 1] and realpath() returns up to
PATH_MAX - 1 characters, so a directory whose canonical path is 4095
characters reaches acl_CheckPath() as a 4096 character string, and the
memcpy() writes the terminating NUL one byte past mangled_path on the
stack of cf-serverd.

This happens before any access decision, so a client that completed
the TLS handshake can trigger it with an OPENDIR or STAT naming such a
directory, for example through a short symlink to a deep directory that
any local user can create under /tmp.

A path that long cannot hold the expanded variables either, so skip the
special variable check for it, the same as when ReplaceSpecialVariables()
overflows. Paths shorter than PATH_MAX are unchanged.

Checked with an AddressSanitizer build of acl_CheckPath(), ReplaceSpecialVariables(), PathAppendTrailingSlash() and StringReplace() taken from the tree, fed a 4095 character canonical path the way the OPENDIR handler does: stack-buffer-overflow WRITE of size 4097 into the 4096 byte mangled_path before the change, clean after.

acl_CheckPath() copies the requested path, reqpath_len + 1 bytes, into
char mangled_path[PATH_MAX] before substituting the $(connection.*)
variables, without checking that it fits. The TLS OPENDIR handler, and
STAT when the target is a directory, append a trailing slash to the
PreprocessRequestPath() result before calling acl_CheckPath(). That
result lives in filename[CF_BUFSIZE + 1] and realpath() returns up to
PATH_MAX - 1 characters, so a directory whose canonical path is 4095
characters reaches acl_CheckPath() as a 4096 character string, and the
memcpy() writes the terminating NUL one byte past mangled_path on the
stack of cf-serverd.

This happens before any access decision, so a client that completed
the TLS handshake can trigger it with an OPENDIR or STAT naming such a
directory, for example through a short symlink to a deep directory that
any local user can create under /tmp.

A path that long cannot hold the expanded variables either, so skip the
special variable check for it, the same as when ReplaceSpecialVariables()
overflows. Paths shorter than PATH_MAX are unchanged.

Changelog: Title
Signed-off-by: Dhanraj E <dhanraj@labs.digiscrypt.com>

@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 🚀 The commit message is rather long and confusing for such a small and trivial fix. Please shorten it.

@larsewi

larsewi commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

@cf-bottom Jenkins 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.

3 participants