Skip to content

Fixed simulate mode reporting a malformed package record as a removal - #48

Open
djbclark wants to merge 10 commits into
masterfrom
fix/simulate-pkg-record-op-validation
Open

djbclark wants to merge 10 commits into
masterfrom
fix/simulate-pkg-record-op-validation

Conversation

@djbclark

@djbclark djbclark commented Oct 9, 2026 •

Copy link
Copy Markdown
Owner

Fork-internal PR for CI; not an upstream submission. One commit: the diff and manifest reports of --simulate only assert()ed that a package record's operation code and name were set. In a build without asserts (the default release configuration), a record such as x,pkg,, fell through the if/else chain into the remove branch and was reported as a removal that no promise asked for; a debug build aborted. Both readers now check the two fields explicitly and skip a bad record with the error already used for a record with the wrong number of fields. The manifest reader also asserted that at least one record was valid, so a file in which none was aborted a debug build; it now logs that there is nothing to manifest and returns, as the diff reader already did. Three unit tests cover it; all fail on the unfixed code. Found by the adversary review of #41 (finding 1) and its follow-up note on this PR's first version. Adversary security review of this commit (6eb70ad, read-only, static): no findings.

Before/after for the guard, tests/unit/simulate_mode_test built with asserts on (macOS, -UNDEBUG), the library rebuilt in between:

# 00d53669a (validation only)
test_manifest_all_records_invalid: Starting test
Assertion failed: ((MapSize(present) != 0) || (MapSize(absent) != 0)), function ManifestPkgOperations, file simulate_mode.c, line 1054.
Abort trap: 6
# 6eb70adaf (this PR)
test_manifest_all_records_invalid: Test completed successfully.
All 5 tests passed

Ubuntu 24.04 (--enable-debug, as root): make -C tests/unit check → All 75 tests passed; tests/acceptance/29_simulate_mode/ → 4 of 4 Pass.


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. op is one letter: i install, r remove, p present, a absent.

The fact. The reader checked the number of fields, but trusted the letter and the name with assert, which release builds compile out. The branch chain ends in a plain else, so anything unknown counted as a removal:

# before
if op == "p": ...
elif op == "a": ...
elif op == "i": ...
else:            # assert op == "r"   <- gone in release builds
    report_removal(name)

# after
if op not in ("i", "r", "p", "a") or name == "":
    log_error("Invalid package operation record: ...")
    continue     # skip the line, like a line with the wrong field count

Skipping bad lines opened a second gap: the manifest report then asserted that something survived, so a file with only bad lines crashed a debug build (and a release build silently printed an empty report). Now it says "nothing to manifest" and returns, the same way the diff report already did:

# before
assert present or absent          # <- aborts when every line was skipped
# after
if not present and not absent:
    log_info("No present or absent packages to manifest")
    return True

Only the agent itself writes this file, so this takes a truncated or corrupted record, not an attacker. It matters more once the report is machine-readable (#41), where a phantom removal could be acted on.

How you'd catch me lying, without reading C. tests/unit/simulate_mode_test feeds x,bad,,, ,bad,,, r,,1.0, and a valid i,foo,1.2.3, and checks that the diff report says Package 'foo [1.2.3]' would be installed and nothing about bad or a removal; a second test does the same for the manifest report; a third feeds only x,bad,, and an empty line to the manifest report and checks that it returns, logs the two rejections and reports no package. On the unfixed code (asserts off) the first two fail with Package 'bad' would be removed / would be absent in the captured output, and with asserts on the third aborts on the old assertion (shown above); on the fixed code all five tests pass. Both runs were done with the library rebuilt in between.

🤖 Generated with Claude Code

victormlg and others added 9 commits September 30, 2026 17:16
Signed-off-by: Victor Moene <victor.moene@northern.tech>
GetProcessStat() asserted on unexpected open() errors, e.g. when the
process exited in the meantime. Release builds would loop forever
instead. Now logs the error and returns false.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ticket: CFE-4285
Changelog: Title
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
CFE-4285: Fixed cf-agent assertion failure when reading /proc/<pid>/stat
…ions.c

Linked TODOs to new tickets to fix old bugs/TODOs that were already in cf-agent
before moving the code.

Signed-off-by: Victor Moene <victor.moene@northern.tech>
Better concurrency for cf-reactor static state
- No need for map anymore, since watcher keeps track of the promise
- No need to keep key as variable anymore, since we do not use a map
- No need to run bundle inside watcher.c
- No risk of running a bundle from a wrong key between policy reads

Signed-off-by: Victor Moene <victor.moene@northern.tech>
Promises are skipped to prevent running them several times. However, on event, we want to run them every single time:
- we clear the promise lock cache
- we set the default if_elapsed time for bundles run from an events promise to be 0, so it doesn't skip the promises.

Fixed also connection cache and custom promise prologue and epilogue. Clear function cache before "then" bundle run

Signed-off-by: Victor Moene <victor.moene@northern.tech>
Signed-off-by: Victor Moene <victor.moene@northern.tech>
- Moved mod_methods.c's MethodsParseTreeCheck implementation to PromiseCheckBundleCallArity
in policy.c, and made it more generic
- Now, MethodsParseTreeCheck and EventsParseTreeCheck both call this new function when calling bundles

Signed-off-by: Victor Moene <victor.moene@northern.tech>

@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.

Approved. Cursor Bugbot was not present on this PR, so that automated-review signal was skipped, and no other required signals or approval policies called for human review. No prior approval from this automation needed to be dismissed.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Router and Approver

DiffPkgOperations() and ManifestPkgOperations() only asserted that the
operation code and the package name of a record were set, so in a build
without asserts a record with an unknown or empty operation code, or an
empty name, was reported as a removal. Such records are now skipped
with the same error as a record with the wrong number of fields.

ManifestPkgOperations() also asserted that at least one record was
valid, which aborted a debug build when none was. Like
DiffPkgOperations(), it now logs that there is nothing to manifest
and returns.

Ticket: none
Changelog: Title
Signed-off-by: Daniel JB Clark <djbclark@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@djbclark
djbclark force-pushed the fix/simulate-pkg-record-op-validation branch from 00d5366 to 6eb70ad Compare October 9, 2026 11:24
djbclark added a commit that referenced this pull request Oct 9, 2026
…m 08:30

Adds the Tier 2 handoff file itself (the first commit on this branch carried
only README.md because the checkout's .git/info/exclude lists docs/handoffs/,
which also applies to worktrees) with the advfix2 addendum: fork PR #48 tip
6eb70ad.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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.

3 participants