Skip to content

Fixed simulate mode mixing up packages whose names contain "-_-" - #49

Open
djbclark wants to merge 10 commits into
masterfrom
fix/simulate-pkg-map-key-collision
Open

djbclark wants to merge 10 commits into
masterfrom
fix/simulate-pkg-map-key-collision

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 keep per-package maps keyed on <name>-_-<architecture>. That key is not unique: name a-_-b with architecture c and name a with architecture b-_-c share it, so one package's record could replace or cancel the other's and a real install or removal went missing from the report. The key now prefixes the name with its length (<len>:<name>:<architecture>), which decodes to exactly one pair. Nothing parses the key back; the printed messages come from the records, not the key. Two unit tests cover it; both fail on the unfixed code. Found by the adversary review of #41 (finding 2). Adversary security review of this commit: 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, folding the lines about the same package (same name and architecture) into one message.

The fact. "Same package" was decided by gluing name and architecture together with -_-:

# before
key = f"{name}-_-{arch}"          # "a-_-b" + "c"  ==  "a" + "b-_-c"

# after
key = f"{len(name)}:{name}:{arch}" # "5:a-_-b:c"  !=  "1:a:b-_-c"

With the length in front there is only one way to cut the key back into a name and an architecture, whatever characters they contain. Names and architectures come from the package module, so the old key could make a removal of one package disappear when another, unrelated package was installed in the same run.

How you'd catch me lying, without reading C. tests/unit/simulate_mode_test feeds r,a-_-b,,c then i,a,,b-_-c and checks the diff report says both Package 'a-_-b-c' would be removed and Package 'a-b-_-c' would be installed; a second test checks the manifest report says absent and present for the two. On the unfixed code the removal line is missing in both (the install cancelled it); on the fixed code all four tests pass. Both runs were done with the library rebuilt in between.

🤖 Generated with Claude Code

victormlg and others added 10 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>
DiffPkgOperations() and ManifestPkgOperations() keyed their maps on
"<name>-_-<architecture>", so the records of two different packages
could share a key (name "a-_-b" with architecture "c" and name "a" with
architecture "b-_-c") and one package's operation could replace or
cancel the other's. The key now prefixes the name with its length,
which makes it unique for every pair of name and architecture.

Ticket: none
Changelog: Title
Signed-off-by: Daniel JB Clark <djbclark@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

@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 after the first check poll, so that automated-review signal was skipped; no approval policy required human review, and there are no unresolved automated findings on the current head.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Router and Approver

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