Skip to content

Fixed simulate diff mode aborting on repeated unversioned package installs - #45

Open
djbclark wants to merge 90 commits into
masterfrom
fix/simulate-pkg-unversioned-install-assert
Open

djbclark wants to merge 90 commits into
masterfrom
fix/simulate-pkg-unversioned-install-assert

Conversation

@djbclark

@djbclark djbclark commented Oct 9, 2026

Copy link
Copy Markdown
Owner

Fork-internal PR for CI; not an upstream submission. One commit: --simulate=diff aborted an assert-enabled cf-agent when the same package was recorded as installed twice without a version, because the two empty versions were compared. The manifest report already guards that comparison; the diff report now uses the same guard and keeps the same record (versioned over unversioned, otherwise the higher version, otherwise the first). Two unit tests cover it; the first aborts on the unguarded code. Adversary security review: no findings.


ELI5 — auditable without C

You know shell and a little Python. One fact.

What the code is for. cf-agent --simulate doesn't install or remove packages. It writes one CSV line per package action it would take, op,name,version,architecture, and at the end turns that log into a report. The version column may be empty when the policy did not ask for one.

The fact. When the diff report sees a second install line for a package it already has, it asks "is the new version greater than the one I kept?" The compare function refuses empty versions (it asserts, which aborts the program in debug builds):

# before
if prev is None or version_greater(version, prev.version):   # aborts on "" vs ""
    keep(new)

# after (the rule the manifest report already used)
if prev is None \
   or (prev.version == "" and version != "") \
   or (version != "" and prev.version != "" and version_greater(version, prev.version)):
    keep(new)

So two unversioned installs of foo now report Package 'foo' would be installed once, and an unversioned then a versioned install reports the versioned one. Release builds compile the assert out, and there the old comparison already picked the same record in every case, so the fix changes nothing for them.

How you'd catch me lying, without reading C. The unit test feeds i,foo,, twice and checks the diff says Package 'foo' would be installed exactly once; with the library built with asserts on, it aborts on the unfixed code and passes on the fixed code. A second test feeds i,foo,, then i,foo,1.2.3, and checks only 'foo [1.2.3]' is reported.

🤖 Generated with Claude Code

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I approved this PR. Cursor Bugbot was not present on the checks after the first poll, so that automated-review signal was skipped. No applicable approval policy required human review.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Router and Approver

FnCallFormat() hands each format piece straight to snprintf() or
BufferAppendF() with exactly one argument. The modifier denylist
("hLqjzt") lets through '*', which takes a width or precision from the
argument list, and '$', which selects a positional argument, so
format("%*d", 7) prints whatever is in the next vararg slot and
format("%2$s", "a") or format("%*s", "a") dereference a slot that was
never passed as a char *. 'l' on %s/%S makes printf read the char *
argument as a wchar_t *, 4 bytes per character past the end of the
string.

The format argument is expanded before the function runs, so a
variable interpolated into it (file contents, module output, data
container values) can carry such a specifier.

Changelog: Title
Signed-off-by: Dhanraj E <dhanraj@labs.digiscrypt.com>
GetServerLiteral() zeroed and formatted CF_BUFSIZE bytes into the
sendbuffer it was handed, but BusyWithNewProtocol() allocates that
buffer as CF_BUFSIZE - CF_INBAND_OFFSET bytes (the most a single
transaction can carry). Any VAR request admitted by the literals ACL
therefore wrote 8 bytes past the end of the handler's stack buffer.

Pass the buffer size into GetServerLiteral(), as DoExec2() already
does, and bound the writes to it. Both callers pass sizeof(sendbuffer).

Changelog: Title
Signed-off-by: Dhanraj E <dhanraj@labs.digiscrypt.com>
@djbclark
djbclark force-pushed the fix/simulate-pkg-unversioned-install-assert branch from 60d5124 to d5052b7 Compare October 20, 2026 08:21

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale comment

Agentic security review found one medium issue: cf-reactor fail-opens events then bundles when a watcher check fails, contrary to the documented HandleReactorEvent contract.

Open in Web View Automation 

Sent by Cursor Security Agent: Security Reviewer

Comment thread cf-reactor/reactor_transform.c Resolved
Dhanu-dynamic and others added 21 commits October 21, 2026 13:42
The "xattr" branch of filestat() reads each extended attribute value
into an uninitialized stack buffer and then uses it as a C string:

    char data[CF_BUFSIZE];
    int datasize = lgetxattr(path, current, data, sizeof(data));
    ...
    if (!StringIsPrintable(data))
    ...
    BufferPrintf(printattr, "%s=%s", current, data);

lgetxattr() returns raw bytes and never adds a terminating NUL. An
unprivileged user can set user.* attributes on files they own, so when
a policy run as root calls filestat() on such a file, a printable value
of exactly 4096 bytes makes StringIsPrintable() read past the end of
data. Shorter values are followed by whatever stale stack bytes come
before the next NUL, which either makes a valid attribute get skipped
as non-printable or copies stack contents into the returned string.

Read at most sizeof(data) - 1 bytes and terminate the value at
datasize. CopyFileExtendedAttributesDisk() in files_copy.c, the other
lgetxattr() caller, handles values by length and is not affected.

Changelog: Title
Signed-off-by: Dhanraj E <dhanraj@labs.digiscrypt.com>
Ticket: CFE-3898
Changelog: None
Signed-off-by: Nick Anderson <nick@cmdln.org>
Added the reactor tests to jenkins, to be able to run them in jenkins. This
means we can test cf-reactor on all the different platforms.

Signed-off-by: Victor Moene <victor.moene@northern.tech>
Made the logs prettier and added a log file where the xtrace is
printed

Signed-off-by: Victor Moene <victor.moene@northern.tech>
The guest environment cannot contain "-". On hosts containing "-", the
promise fails. The host name is now canonofied.

Signed-off-by: Victor Moene <victor.moene@northern.tech>
X509_get_subject_name() now returns a const name. Build a new
X509_NAME and set it as both subject and issuer instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
Tests that print Pass message but have logs errors will fail with `FAIL (Error printed)` message.
Tests which are expected to log errors should use `.error.cf` or `.error.pass.cf` suffix. see: `tests/acceptance/README`

Ticket: CFE-3041
Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
These tests read files created by their own init bundle, but pre-evaluation calls the read functions before the files exist, logging "Could not examine file" errors which now fail regular tests.

Ticket: CFE-3041
Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
Errors printed by the command were reported on lines starting with "error:", so testall mistook them for errors of the test itself.

Ticket: CFE-3041

Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
These tests read files and directories created by their own policy, but pre-evaluation calls the functions before they exist, logging errors which now fail regular tests.

Ticket: CFE-3041
Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
The agent loads def.json from the directory of the input policy file, so tests in 00_basics/ tried to read this directory as their augments file and logged "Could not open JSON file" errors.

Ticket: CFE-3041
Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
The test used the 'immediate' body from plucked.sub.cf before it was
loaded during pre-evaluation, so the promise is now skipped until the
test framework is loaded.

Ticket: CFE-3041
Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
The test referred to a promise module which never existed and did not run its init and cleanup bundles.

Ticket: CFE-3041
Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
The check bundle counted lines in files which do not exist until the test bundle has run, but the agent evaluates the functions of all promises before any bundle runs.

Ticket: CFE-3041
Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
wrong-bundle-type.cf printed the sub-agent output in its DEBUG report, so the sub-agent errors were mistaken for errors of the test, and ifelse.cf used invalid class expressions as false conditions.

Ticket: CFE-3041
Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
These tests intentionally exercise error paths, the reason is documented in the header of each test.

Ticket: CFE-3041
Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
Ticket: CFE-3041
Signed-off-by: Nick Anderson <nick@cmdln.org>
Ticket: CFE-3041
Signed-off-by: Nick Anderson <nick@cmdln.org>
The errorpass branch only checked for logged errors and the Pass
message, so a test marked test_soft_fail or test_skip_unsupported was
reported as FAIL (e.g. process_select_fncalls.error.pass.cf on Windows,
which soft fails due to ENT-10217 and logs no errors there). Errorpass
tests now go through the regular outcome handling and only differ in
requiring at least one logged error on Pass.
larsewi and others added 26 commits November 4, 2026 09:48
…sendbuffer-size

Fixed stack buffer overflow in GetServerLiteral for TLS VAR requests
…l-terminate

Fixed out-of-bounds read of extended attribute values in filestat()
RHEL 8 and 9 arm64 build hosts have no published CFEngine package
to install yet, so setup-cfengine-build-host.sh falls back to
building core from source, and this script rejected anything older
than RHEL 10.

Ticket: ENT-14716
Changelog: none
Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
…upport

ENT-14716: Added RHEL 8 and 9 support to ci/dependencies.sh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
This enables buildscripts/ci/setup-cfengine-build-host.sh to use --with-pcre2=/opt/pcre2 for rhel-8 build hosts with insufficient version of pcre2. libntech needs 10.34+

Ticket: ENT-14716
Changelog: none
This is needed on systems like rhel-8 which prefer /lib64.
build_lmdb() installs to /usr/lib and ldconfig takes care of linking things to /lib64 as it should.

Ticket: ENT-14716
Changelog: none
Added support for OPTS environment variable to ci/configure.sh
st_uid/st_gid are always 0 on Windows, so the owner SID is compared instead

Signed-off-by: Victor Moene <victor.moene@northern.tech>
LogCheckError() passed errno values to GetErrorStrFromCode(), which on
Windows expects a Windows error code, so the logged reason was wrong.

Signed-off-by: Victor Moene <victor.moene@northern.tech>
cf-check: Fix segfault on missing state directory
…assed-args

Made format() reject specifiers that read unpassed printf arguments
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
Co-authored-by: Lars Erik Wik <53906608+larsewi@users.noreply.github.com>
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
…ror-messages

cf-reactor: fix watchers on windows
ENT-14717: Made the backport action a reusable workflow
…changelog-master

docs: update changelog for master
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>
…th-overflow

fix stack overflow building absolute path from relative symlink target
Ticket: ENT-14717
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
ENT-14717: Now removes the cherry-pick? label after successful backports
olehermanse and others added 2 commits November 10, 2026 13:58
ENT-14927: Fixed cf-serverd crash when policy reload races with a new connection
…talls

DiffPkgOperations() compared the versions of two recorded installations
of the same package without checking that both were set, which
PkgVersionIsGreater() asserts. The diff report now keeps the record the
way the manifest report does: a versioned record over an unversioned
one, otherwise the higher version, otherwise the first one recorded.

Ticket: none
Changelog: Title
Signed-off-by: Daniel JB Clark <djbclark@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@djbclark
djbclark force-pushed the fix/simulate-pkg-unversioned-install-assert branch from 94b2157 to ab0408f Compare November 10, 2026 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants