Skip to content

cf-execd: Passed the runagent socket request to cf-runagent as one argument - #6506

Open
Dhanu-dynamic wants to merge 1 commit into
cfengine:masterfrom
Dhanu-dynamic:execd-runagent-socket-arg-injection
Open

Dhanu-dynamic wants to merge 1 commit into
cfengine:masterfrom
Dhanu-dynamic:execd-runagent-socket-arg-injection

Conversation

@Dhanu-dynamic

@Dhanu-dynamic Dhanu-dynamic commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

HandleRunagentRequest() reads one line from runagent.socket, the host to
hail, and for a non-localhost request built the command string

<bindir>/cf-runagent -b -H <request>

which cf_popen() runs as root after ArgSplitCommand() splits it into argv
on whitespace, treating quotes as delimiters. The request was interpolated
verbatim, so one containing a space, tab or quote appended further
arguments to cf-runagent. A caller allowed on the socket through
runagent_socket_allow_users (cfapache on an Enterprise hub) is only meant
to pick a host, but a request like "host -f /tmp/evil.cf" made cf-runagent
load another policy as root, and injected "-D", "-s" or "--remote-bundles"
reached the remote agents, which the socket interface is documented not to
allow. There is no shell, so this is argument injection, not command
injection.

Build the argv array directly and exec it, so the request is always a
single argument of -H whatever it contains, through a new cf_popen_argv()
that takes a ready-made argument vector; cf_popen_select() now splits the
command string and calls the same code. An empty request is still refused,
because -H "" would make cf-runagent fall back to the hosts list from
policy instead of hailing nothing.

The unit test points GetBinDir() at a stub cf-runagent that prints its
argv and checks that requests carrying whitespace or quotes arrive as one
argument.

@cf-bottom

Copy link
Copy Markdown

Thank you for submitting a PR! 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.

Rather than building a command string and rejecting characters that ArgSplitCommand() would split on, could you instead build the argv array directly and pass it straight to execv()? Might need a new variant of cf_open().

…gument

HandleRunagentRequest() reads one line from runagent.socket, the host to
hail, and for a non-localhost request built the command string

    <bindir>/cf-runagent -b -H <request>

which cf_popen() runs as root after ArgSplitCommand() splits it into argv
on whitespace, treating quotes as delimiters. The request was interpolated
verbatim, so one containing a space, tab or quote appended further
arguments to cf-runagent. A caller allowed on the socket through
runagent_socket_allow_users (cfapache on an Enterprise hub) is only meant
to pick a host, but a request like "host -f /tmp/evil.cf" made cf-runagent
load another policy as root, and injected "-D", "-s" or "--remote-bundles"
reached the remote agents, which the socket interface is documented not to
allow. There is no shell, so this is argument injection, not command
injection.

Build the argv array directly and exec it, so the request is always a
single argument of -H whatever it contains, through a new cf_popen_argv()
that takes a ready-made argument vector; cf_popen_select() now splits the
command string and calls the same code. An empty request is still refused,
because '-H ""' would make cf-runagent fall back to the hosts list from
policy instead of hailing nothing.

The unit test points GetBinDir() at a stub cf-runagent that prints its
argv and checks that requests carrying whitespace or quotes arrive as one
argument.

Changelog: Title
Signed-off-by: Dhanraj E <dhanraj@labs.digiscrypt.com>
@Dhanu-dynamic
Dhanu-dynamic force-pushed the execd-runagent-socket-arg-injection branch from 46c6a67 to ac928bf Compare November 23, 2026 10:28
@Dhanu-dynamic Dhanu-dynamic changed the title cf-execd: Rejected runagent socket requests injecting cf-runagent args cf-execd: Passed the runagent socket request to cf-runagent as one argument Nov 23, 2026
@Dhanu-dynamic

Copy link
Copy Markdown
Contributor Author

Makes sense, that's the better fix. Reworked it: the remote path now builds { <bindir>/cf-runagent, "-b", "-H", request, NULL } and hands it to a new cf_popen_argv(), which is the old cf_popen_select() body split into a static helper taking the argv; cf_popen_select() just does the ArgSplitCommand() and calls the same helper, so nothing changes for the string-based callers. The character blacklist is gone. I kept one check, for an empty request, since -H "" gives cf-runagent an empty host list and it then falls back to the hosts list from policy, which the old command string never did.

The unit test now points GetBinDir() at a stub cf-runagent that prints its argv and asserts that requests with whitespace or quotes arrive as a single -H argument. It fails on the old code and the full unit suite passes with the change.

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